Debian Codegraph @

share/tools/ncdeflate

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