Debian Codegraph @

share/tools/ncdeflate

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/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 usagescip-shell shell shell-project 0.0.0 usage(). () {
22 echoscip-shell system . . echo. "usage: $(basenamescip-shell system . . basename. $0) [-d n] [-o dir] [-s] file [file ...]" >&2
23 echoscip-shell system . . echo. " [-d n] set compression level (0=min, 9=max, default=3)" >&2
24 echoscip-shell system . . echo. " [-o dir] output directory" >&2
25 echoscip-shell system . . echo. " [-s] add shuffle option to deflation compression" >&2
26 echoscip-shell system . . echo. " [-v] be verbose" >&2
27 echoscip-shell system . . 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 ! whichscip-shell system . . which. nccopy >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
43 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. $0): cannot find nccopy." >&2
44 exit 1
45 fi
46
47 # find compatible stat command
48 if statscip-shell system . . stat. -f%z "$0" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
49 # BSD
50 STAT=stat
51 STATOPT="-f%z"
52 elif statscip-shell system . . stat. -c%s "$0" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
53 # GNU
54 STAT=stat
55 STATOPT="-c%s"
56 elif gstat -c%s "$0" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
57 # GNU
58 STAT=gstat
59 else
60 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. $0): no compatible stat command found." >&2
61 exit 1
62 fi
63
64 function file1_greater_file2scip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. "$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 echoscip-shell system . . echo. "Invalid option: -$OPTARG" >&2
97 usagescip-shell shell shell-project 0.0.0 usage().
98 ;;
99 :)
100 echoscip-shell system . . echo. "Option -$OPTARG requires an argument." >&2
101 usagescip-shell shell shell-project 0.0.0 usage().
102 ;;
103 esac
104 done
105
106 # test if 0 <= NC_DEFLATE <= 9
107 testscip-shell system . . test. "$NC_DEFLATE" -ge 0 -a "$NC_DEFLATE" -le 9 || usagescip-shell shell shell-project 0.0.0 usage().
108
109 # remove options from $@
110 shift $((OPTIND-1))
111
112 testscip-shell system . . test. -z "$1" && usagescip-shell shell shell-project 0.0.0 usage().
113
114 for file in $@; do
115 if ! [scip-shell system . . `[`. -f "$file" ]; then
116 echoscip-shell system . . echo. "$file: not found" >&2
117 continue
118 fi
119
120 # set default chunking
121 NC_CHUNK="-c$(ncdump -h "$file" | awkscip-shell system . . awk. '/^dimensions:/ { getline ; while (match($0,"^\t")) {printf "%s/128,", $1; getline } } END {print "/"}scip-shell filesystem . . `/^dimensions:/ { getline ; while (match($0,"^\t")) {printf "%s/128,", $1; getline } } END {print "/"}`.')"
122
123 # compress file
124 if [scip-shell system . . `[`. "$NC_VERBOSE" = "TRUE" ]; then
125 echoscip-shell system . . echo. -n "$(basenamescip-shell system . . basename. "${file}")... " >&2
126 fi
127 if [scip-shell system . . `[`. "$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 [scip-shell system . . `[`. "$NC_DEFLATE" -eq 0 ] || file1_greater_file2scip-shell shell shell-project 0.0.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 [scip-shell system . . `[`. "${OUTDIR}" ]; then
139 mvscip-shell system . . mv. -f "${file}.$$.tmp" "${OUTDIR}/$(basenamescip-shell system . . basename. ${file})${NC_SUFFIX}"
140 else
141 mvscip-shell system . . mv. -f "${file}.$$.tmp" "${file}${NC_SUFFIX}"
142 fi
143
144 if [scip-shell system . . `[`. "$NC_VERBOSE" = "TRUE" ]; then
145 if [scip-shell system . . `[`. "$NC_DEFLATE" -eq 0 ]; then
146 echoscip-shell system . . echo. "done." >&2
147 else
148 echoscip-shell system . . echo. "ratio: ${NC_RATIO}%." >&2
149 fi
150 fi
151 else
152 # keep original file and remove compressed file
153 rmscip-shell system . . rm. -f "${file}.$$.tmp"
154 # copy original file
155 if [scip-shell system . . `[`. "${OUTDIR}" ]; then
156 cpscip-shell system . . cp. -f "${file}" "${OUTDIR}/$(basenamescip-shell system . . basename. ${file})${NC_SUFFIX}"
157 fi
158
159 if [scip-shell system . . `[`. "$NC_VERBOSE" = "TRUE" ]; then
160 echoscip-shell system . . echo. "could not reduce file size. already compressed?" >&2
161 fi
162 fi
163 done
164
165 exit 0