Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ncdeflate 3.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  1. #!/usr/bin/env bash
  2. #
  3. #
  4. # Copyright (c) 1991-2021 by the GMT Team (https://www.generic-mapping-tools.org/team.html)
  5. # See LICENSE.TXT file for copying and redistribution conditions.
  6. #
  7. # Compresses all variables in the given netcdf files with the
  8. # specified deflation level.
  9. function usage () {
  10. echo "usage: $(basename $0) [-d n] [-o dir] [-s] file [file ...]" >&2
  11. echo " [-d n] set compression level (0=min, 9=max, default=3)" >&2
  12. echo " [-o dir] output directory" >&2
  13. echo " [-s] add shuffle option to deflation compression" >&2
  14. echo " [-v] be verbose" >&2
  15. echo " [-x s] add suffix \"s\" to output filename" >&2
  16. exit 1
  17. }
  18. # set defaults
  19. NC_DEFLATE=3
  20. NC_SHUFFLE=
  21. NC_SUFFIX=""
  22. NC_VERBOSE=0
  23. OUTDIR=""
  24. # handle filenames with spaces
  25. IFS=$'\n'
  26. # test if nccopy in PATH
  27. if ! which nccopy >/dev/null 2>&1; then
  28. echo "$(basename $0): cannot find nccopy." >&2
  29. exit 1
  30. fi
  31. # find compatible stat command
  32. if stat -f%z "$0" >/dev/null 2>&1; then
  33. # BSD
  34. STAT=stat
  35. STATOPT="-f%z"
  36. elif stat -c%s "$0" >/dev/null 2>&1; then
  37. # GNU
  38. STAT=stat
  39. STATOPT="-c%s"
  40. elif gstat -c%s "$0" >/dev/null 2>&1; then
  41. # GNU
  42. STAT=gstat
  43. else
  44. echo "$(basename $0): no compatible stat command found." >&2
  45. exit 1
  46. fi
  47. function file1_greater_file2 () {
  48. # return TRUE if 1st file is larger than the 2nd
  49. size1=$($STAT $STATOPT "$1")
  50. size2=$($STAT $STATOPT "$2")
  51. if [ "$size1" -gt "$size2" ]; then
  52. # compression ratio
  53. NC_RATIO=$((100 - ${size2} * 100 / ${size1}))
  54. return 0
  55. fi
  56. NC_RATIO="NA"
  57. return 1
  58. }
  59. # parse command line args
  60. while getopts ":d:o:svx:" opt; do
  61. case $opt in
  62. d)
  63. NC_DEFLATE="$OPTARG"
  64. ;;
  65. o)
  66. OUTDIR="$OPTARG"
  67. ;;
  68. s)
  69. NC_SHUFFLE="-s"
  70. ;;
  71. v)
  72. NC_VERBOSE=TRUE
  73. ;;
  74. x)
  75. NC_SUFFIX="$OPTARG"
  76. ;;
  77. \?)
  78. echo "Invalid option: -$OPTARG" >&2
  79. usage
  80. ;;
  81. :)
  82. echo "Option -$OPTARG requires an argument." >&2
  83. usage
  84. ;;
  85. esac
  86. done
  87. # test if 0 <= NC_DEFLATE <= 9
  88. test "$NC_DEFLATE" -ge 0 -a "$NC_DEFLATE" -le 9 || usage
  89. # remove options from $@
  90. shift $((OPTIND-1))
  91. test -z "$1" && usage
  92. for file in $@; do
  93. if ! [ -f "$file" ]; then
  94. echo "$file: not found" >&2
  95. continue
  96. fi
  97. # set default chunking
  98. NC_CHUNK="-c$(ncdump -h "$file" | awk '/^dimensions:/ { getline ; while (match($0,"^\t")) {printf "%s/128,", $1; getline } } END {print "/"}')"
  99. # compress file
  100. if [ "$NC_VERBOSE" = "TRUE" ]; then
  101. echo -n "$(basename "${file}")... " >&2
  102. fi
  103. if [ "$NC_DEFLATE" -gt 0 ]; then
  104. nccopy -k 3 -d $NC_DEFLATE $NC_SHUFFLE $NC_CHUNK "$file" "${file}.$$.tmp" || continue
  105. else
  106. # write classic
  107. nccopy -k 1 "$file" "${file}.$$.tmp" || continue
  108. fi
  109. # check compressed file size
  110. if [ "$NC_DEFLATE" -eq 0 ] || file1_greater_file2 "$file" "${file}.$$.tmp" ; then
  111. # if no compression or if compressed file is smaller than original file:
  112. # replace original file with compressed/decompressed file
  113. if [ "${OUTDIR}" ]; then
  114. mv -f "${file}.$$.tmp" "${OUTDIR}/$(basename ${file})${NC_SUFFIX}"
  115. else
  116. mv -f "${file}.$$.tmp" "${file}${NC_SUFFIX}"
  117. fi
  118. if [ "$NC_VERBOSE" = "TRUE" ]; then
  119. if [ "$NC_DEFLATE" -eq 0 ]; then
  120. echo "done." >&2
  121. else
  122. echo "ratio: ${NC_RATIO}%." >&2
  123. fi
  124. fi
  125. else
  126. # keep original file and remove compressed file
  127. rm -f "${file}.$$.tmp"
  128. # copy original file
  129. if [ "${OUTDIR}" ]; then
  130. cp -f "${file}" "${OUTDIR}/$(basename ${file})${NC_SUFFIX}"
  131. fi
  132. if [ "$NC_VERBOSE" = "TRUE" ]; then
  133. echo "could not reduce file size. already compressed?" >&2
  134. fi
  135. fi
  136. done
  137. exit 0
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...