Debian Codegraph @

data/ncz2psx

raw
1 #!/usr/bin/env bash
2
3 # Purpose: Convert POSIX file/directory names to/from NCZarr store-names
4
5 # Copyright (C) 2022--present Charlie Zender
6
7 # This file is part of NCO, the netCDF Operators. NCO is free software.
8 # You may redistribute and/or modify NCO under the terms of the
9 # 3-Clause BSD License.
10
11 # You are permitted to link NCO with the HDF, netCDF, OPeNDAP, and UDUnits
12 # libraries and to distribute the resulting executables under the terms
13 # of the BSD, but in addition obeying the extra stipulations of the
14 # HDF, netCDF, OPeNDAP, and UDUnits licenses.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 # See the 3-Clause BSD License for more details.
20
21 # The original author of this software, Charlie Zender, seeks to improve
22 # it with your suggestions, contributions, bug-reports, and patches.
23 # Please contact the NCO project at http://nco.sf.net or write to
24 # Charlie Zender
25 # Department of Earth System Science
26 # University of California, Irvine
27 # Irvine, CA 92697-3100
28
29 # Prerequisites: Bash, NCO
30 # Script could use other shells, e.g., dash (Debian default) after rewriting function definitions and loops
31 # Debug with 'bash -x ncz2psx --dbg=dbg_lvl' where 0 <= dbg_lvl <= 5
32
33 # Insta-install:
34 # scp ~/nco/data/ncz2psx [email protected]:bin
35 # scp ~/nco/data/ncz2psx andes.olcf.ornl.gov:bin_andes
36 # scp ~/nco/data/ncz2psx [email protected]:bin_blues
37 # scp ~/nco/data/ncz2psx cheyenne.ucar.edu:bin
38 # scp ~/nco/data/ncz2psx [email protected]:bin_chrysalis
39 # scp ~/nco/data/ncz2psx compy.pnl.gov:bin
40 # scp ~/nco/data/ncz2psx cooley.alcf.anl.gov:bin
41 # scp ~/nco/data/ncz2psx cori.nersc.gov:bin_cori
42 # scp ~/nco/data/ncz2psx dust.ess.uci.edu:bin
43 # scp ~/nco/data/ncz2psx e3sm.ess.uci.edu:bin
44 # scp ~/nco/data/ncz2psx frazil.ess.uci.edu:bin
45 # scp ~/nco/data/ncz2psx perlmutter-p1.nersc.gov:bin_perlmutter
46 # scp ~/nco/data/ncz2psx skyglow.ess.uci.edu:bin
47 # scp ~/nco/data/ncz2psx theta.alcf.anl.gov:bin_theta
48 # scp dust.ess.uci.edu:bin/ncz2psx ~/bin
49 # scp dust.ess.uci.edu:bin/ncz2psx ${MY_BIN_DIR}
50 # scp [email protected]:bin/ncz2psx ${MY_BIN_DIR}
51
52 # Set script name, directory, PID, run directory
53 drc_pwd=${PWD}
54 # Security: Explicitly unset IFS before wordsplitting, so Bash uses default IFS=<space><tab><newline>
55 unset IFS
56 # Set these before 'module' command which can overwrite ${BASH_SOURCE[0]}
57 # NB: dash supports $0 syntax, not ${BASH_SOURCE[0]} syntax
58 # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
59 spt_src="${BASH_SOURCE[0]}"
60 [[ -z "${spt_src}" ]] && spt_src="${0}" # Use ${0} when BASH_SOURCE is unavailable (e.g., dash)
61 while [ -h "${spt_src}" ]; do # Recursively resolve ${spt_src} until file is no longer a symlink
62 drc_spt="$( cd -P "$( dirname "${spt_src}" )" && pwd )"
63 spt_src="$(readlink "${spt_src}")"
64 [[ ${spt_src} != /* ]] && spt_src="${drc_spt}/${spt_src}" # If ${spt_src} was relative symlink, resolve it relative to path where symlink file was located
65 done
66 cmd_ln="${spt_src} ${@}"
67 drc_spt="$( cd -P "$( dirname "${spt_src}" )" && pwd )"
68 spt_nm=$(basename ${spt_src}) # [sng] Script name (unlike $0, ${BASH_SOURCE[0]} works well with 'source <script>')
69 spt_pid=$$ # [nbr] Script PID (process ID)
70
71 # Debugging and Benchmarking:
72 # /bin/ls -d ${HOME}/nco/data/h000? | ncz2psx
73 # /bin/ls -d ${HOME}/nco/data/h000? | ncz2psx | ncea -O ~/out.nc
74
75 # Production usage:
76 # /bin/ls -d ${HOME}/nco/data/h000? | ncz2psx | ncea -O ~/out.nc
77
78 # 20221108: Passing environment variable NCO_PATH_OVERRIDE (NPO) to ncremap in batch queues fails on Cori
79 # Approaches that fail include: 1. export NPO='Yes';ncremap ... 2. export NPO='Yes' ncremap ...
80 # Direct approach that works is to pass NPO flag to ncremap via command-line switch
81 # Require that path-override switch to be first command-line option (i.e., $1) found with shell syntax
82 # ncremap/ncclimo implement NPO (though not getopt) logic prior to invoking NCO
83 # This switch is a no-op in main getopt() block below (since it has already been parsed here)
84 hrd_pth='No' # [sng] Hard-code machine-dependent paths/modules if HOSTNAME in database
85 if [ -n "${1}" ]; then
86 if [ "${1}" = '--hrd_pth' ] || [ "${1}" = '--npo' ] || [ "${1}" = '--nco_path_override' ] || [ "${1}" = '--NCO_PATH_OVERRIDE' ]; then
87 hrd_pth='Yes'
88 NCO_PATH_OVERRIDE='Yes'
89 fi # !hrd_pth
90 fi # !$1
91
92 # Configure paths at High-Performance Computer Centers (HPCCs) based on ${HOSTNAME}
93 if [ -z "${HOSTNAME}" ]; then
94 if [ -f /bin/hostname ] && [ -x /bin/hostname ]; then
95 export HOSTNAME=`/bin/hostname`
96 elif [ -f /usr/bin/hostname ] && [ -x /usr/bin/hostname ]; then
97 export HOSTNAME=`/usr/bin/hostname`
98 fi # !hostname
99 fi # HOSTNAME
100
101 # Ensure batch jobs access correct 'mpirun' (or, with SLURM, 'srun') command, netCDF library, and NCO executables and library
102 # 20170914 Entire block is identical between ncclimo and ncremap---keep it that way!
103 # 20190421 Change override default from opt-out to opt-in
104 # 20221108 Implement hrd_pth in block above prior to getopt()
105 # Leave NCO_PATH_OVERRIDE unset or set to 'No' to prevent NCO from executing next block that overrides PATH
106 # Set NCO_PATH_OVERRIDE to 'Yes' in environment to cause NCO to execute next block and to override PATH:
107 # export NCO_PATH_OVERRIDE='Yes'
108 if [ "${hrd_pth}" = 'Yes' ] && [ "${NCO_PATH_OVERRIDE}" = 'Yes' ]; then
109 # If HOSTNAME is not in database, change hrd_pth_fnd to 'No' in case-statement default fall-through
110 hrd_pth_fnd='Yes' # [sng] Machine-dependent paths/modules for HOSTNAME found in database
111 case "${HOSTNAME}" in
112 acme1* )
113 export PATH='/home/zender1/bin:/p/user_pub/e3sm_unified/envs/base/envs/e3sm_unified_latest/bin'\:${PATH}
114 export LD_LIBRARY_PATH='/home/zender1/lib:/p/user_pub/e3sm_unified/envs/base/envs/e3sm_unified_latest/lib'\:${LD_LIBRARY_PATH} ; ;;
115 andes* )
116 # 20190827: Must guarantee finding mpirun
117 source ${MODULESHOME}/init/sh # 20150607: PMC Ensures find module commands will be found
118 if [ ${spt_nm} = 'ncremap' ]; then
119 module load esmf
120 fi # !ncremap
121 export PATH='/ccs/home/zender/bin_andes'\:${PATH}
122 export LD_LIBRARY_PATH='/ccs/home/zender/lib_andes:/ccs/proj/cli900/sw/andes/e3sm-unified/base/envs/e3sm_unified_latest/lib'\:${LD_LIBRARY_PATH} ; ;;
123 blues* | blogin* | b[0123456789][0123456789][0123456789] )
124 export PATH='/home/zender/bin_blues'\:${PATH}
125 export LD_LIBRARY_PATH='/home/zender/lib_blues'\:${LD_LIBRARY_PATH} ; ;;
126 chrysalis* | chrlogin* | chr-[0123456789][0123456789][0123456789][0123456789] )
127 # 20221006 Add build environment modules
128 module load gcc/9.2.0-ugetvbp
129 module load openmpi/4.0.4-hpcx-hghvhj5
130 if [ ${spt_nm} = 'ncremap' ]; then
131 E3SMU_ROOT='/lcrc/soft/climate/e3sm-unified/base/envs/e3sm_unified_latest'
132 fi # !ncremap
133 export PATH='/home/ac.zender/bin_chrysalis:/home/ac.zender/anaconda/bin'\:${PATH}
134 export LD_LIBRARY_PATH='/home/ac.zender/lib_chrysalis:/home/ac.zender/anaconda/lib'\:${LD_LIBRARY_PATH} ; ;;
135 *cheyenne* )
136 # 20180112: Cheyenne support not yet tested in batch mode
137 if [ ${spt_nm} = 'ncremap' ]; then
138 # On cheyenne, module load ncl installs ERWG in /glade/u/apps/ch/opt/ncl/6.4.0/intel/17.0.1/bin (i.e., ${NCARG_ROOT}/bin)
139 module load ncl
140 fi # !ncremap
141 if [ -n "${NCARG_ROOT}" ]; then
142 export PATH="${PATH}:/glade/u/apps/ch/opt/ncl/6.6.2/gnu/8.3.0/bin"
143 fi # !NCARG_ROOT
144 export PATH='/glade/u/home/zender/bin'\:${PATH}
145 export LD_LIBRARY_PATH='/glade/u/apps/ch/opt/netcdf/4.6.3/gnu/9.1.0/lib:/glade/u/apps/ch/opt/udunits/2.2.26/gnu/9.1.0/lib:/glade/u/apps/ch/opt/gsl/2.4/gnu/6.3.0/lib:/glade/u/home/zender/lib'\:${LD_LIBRARY_PATH} ; ;;
146 compy* | n[0123456789][0123456789][0123456789][0123456789] )
147 module purge
148 module load gcc/10.2.0
149 if [ ${spt_nm} = 'ncremap' ]; then
150 # 20210519: This script takes significant time (5-10 seconds) to load
151 source /compyfs/software/mbtempest.envs.sh
152 E3SMU_ROOT='/share/apps/E3SM/conda_envs/base/envs/e3sm_unified_latest'
153 fi # !ncremap
154 export PATH='/qfs/people/zender/bin:/qfs/people/zender/anaconda/bin'\:${PATH}
155 export LD_LIBRARY_PATH='/qfs/people/zender/lib:/qfs/people/zender/anaconda/lib'\:${LD_LIBRARY_PATH} ; ;;
156 cooley* | cc[0123456789][0123456789][0123456789] )
157 # 20160421: Split cooley from mira binary locations to allow for different system libraries
158 # http://www.mcs.anl.gov/hs/software/systems/softenv/softenv-intro.html
159 soft add +mvapich2
160 export PBS_NUM_PPN=12 # Spoof PBS on Soft (which knows nothing about node capabilities)
161 export PATH='/home/zender/bin_cooley'\:${PATH}
162 export LD_LIBRARY_PATH='/home/zender/lib_cooley'\:${LD_LIBRARY_PATH} ; ;;
163 cori* | cmem* | nid[0123456789][0123456789][0123456789][0123456789][0123456789] )
164 # 20220811 Add build environment modules
165 module purge
166 module load PrgEnv-intel
167 module load cray-netcdf
168 module load gsl/2.7
169 module load antlr/2.7.7-intel
170 module load udunits
171 if [ ${spt_nm} = 'ncremap' ]; then
172 MOAB_ROOT=/project/projectdirs/e3sm/software/moab
173 TEMPESTREMAP_ROOT=/project/projectdirs/e3sm/software/tempestremap
174 E3SMU_ROOT='/global/common/software/e3sm/anaconda_envs/base/envs/e3sm_unified_latest'
175 fi # !ncremap
176 if [ -n "${NCARG_ROOT}" ]; then
177 export PATH="${PATH}:${NCARG_ROOT}/bin"
178 fi # !NCARG_ROOT
179 export PATH='/global/homes/z/zender/bin_cori'\:${PATH}
180 export LD_LIBRARY_PATH='/global/homes/z/zender/lib_cori'\:${LD_LIBRARY_PATH} ; ;;
181 mira* )
182 export PATH='/home/zender/bin_mira'\:${PATH}
183 export LD_LIBRARY_PATH='/soft/libraries/netcdf/current/library:/home/zender/lib_mira'\:${LD_LIBRARY_PATH} ; ;;
184 perlmutter* | login[0123456789][0123456789] | nid[0123456789][0123456789][0123456789][0123456789][0123456789][0123456789] )
185 # 20221103 Add build environment modules
186 module load PrgEnv-gnu
187 module load gcc/11.2.0
188 module load cray-hdf5/1.12.2.1
189 module load cray-netcdf/4.9.0.1
190 if [ ${spt_nm} = 'ncremap' ]; then
191 MOAB_ROOT=/project/projectdirs/e3sm/software/moab
192 TEMPESTREMAP_ROOT=/project/projectdirs/e3sm/software/tempestremap
193 E3SMU_ROOT='/global/common/software/e3sm/anaconda_envs/base/envs/e3sm_unified_latest'
194 fi # !ncremap
195 if [ -n "${NCARG_ROOT}" ]; then
196 export PATH="${PATH}:${NCARG_ROOT}/bin"
197 export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${NCARG_ROOT}/lib"
198 fi # !NCARG_ROOT
199 export PATH='/global/homes/z/zender/bin_perlmutter'\:${PATH}
200 export LD_LIBRARY_PATH='/global/homes/z/zender/lib_perlmutter'\:${LD_LIBRARY_PATH} ; ;;
201 theta* )
202 export PATH='/opt/cray/pe/netcdf/4.6.1.2/gnu/7.1/bin'\:${PATH}
203 export LD_LIBRARY_PATH='/opt/cray/pe/netcdf/4.6.1.2/gnu/7.1/lib'\:${LD_LIBRARY_PATH} ; ;;
204 titan* )
205 source ${MODULESHOME}/init/sh # 20150607: PMC Ensures find module commands will be found
206 module load gcc
207 if [ ${spt_nm} = 'ncremap' ]; then
208 # 20170831: Use module load ncl (6.3.0 lacks ERWG)
209 module load ncl # 20170916 OK
210 fi # !ncremap
211 if [ -n "${NCARG_ROOT}" ]; then
212 export PATH="${PATH}:${NCARG_ROOT}/bin"
213 fi # !NCARG_ROOT
214 export PATH='/ccs/home/zender/bin_titan'\:${PATH}
215 export LD_LIBRARY_PATH='/opt/cray/netcdf/4.4.1.1/GNU/49/lib:/sw/xk6/udunits/2.1.24/sl_gcc4.5.3/lib:/ccs/home/zender/lib_titan'\:${LD_LIBRARY_PATH} ; ;;
216 * ) # Default fall-through
217 hrd_pth_fnd='No' ; ;;
218 esac # !HOSTNAME
219 # 20210519: E3SM-U supplies many uncommon executables, e.g., ESMF_RegridWeightGen
220 # Append E3SM-U to end of PATH so NCO binaries not redirected, e.g., from CSZ's development directory to E3SM-U
221 if [ -n "${E3SMU_ROOT}" ]; then
222 export PATH="${PATH}:${E3SMU_ROOT}/bin"
223 fi # !MOAB_ROOT
224 fi # !hrd_pth && !NCO_PATH_OVERRIDE
225
226 # 20220131 ncclimo/ncremap commands within scripts that open E3SMU environment
227 # (e.g., zppy-generated scripts) must be told where to find NCO binaries.
228 # Only necessary on login nodes since Spack handles this fine on compute nodes
229 if [ "${E3SMU_MPI}" = 'NOMPI' ] && [ -n "${E3SMU_SCRIPT}" ] && [ -n "${CONDA_PREFIX}" ]; then
230 export PATH="${CONDA_PREFIX}/bin"\:${PATH}
231 fi # !E3SMU_MPI
232 # Set NCO version and directory
233 nco_exe=`which ncks`
234 if [ -z "${nco_exe}" ]; then
235 echo "${spt_nm}: ERROR Unable to find NCO, \${nco_exe} = ${nco_exe}"
236 echo "${spt_nm}: HINT Carefully examine your environment setup (e.g., .bashrc) to avoid inadvertently overriding (with, e.g., conda-initialization) paths intended to be provided by an analysis-package environment (e.g., E3SM-Unified)"
237 exit 1
238 fi # !nco_exe
239 # StackOverflow method finds NCO directory
240 while [ -h "${nco_exe}" ]; do
241 drc_nco="$( cd -P "$( dirname "${nco_exe}" )" && pwd )"
242 nco_exe="$(readlink "${nco_exe}")"
243 [[ ${nco_exe} != /* ]] && nco_exe="${drc_nco}/${nco_exe}"
244 done
245 drc_nco="$( cd -P "$( dirname "${nco_exe}" )" && pwd )"
246 nco_vrs=$(ncks --version 2>&1 > /dev/null | grep NCO | awk '{print $5}')
247 nco_sng=$(ncks --version 2>&1 > /dev/null | grep NCO | awk -F '"' '{print $2}')
248
249 # 20190218: Die quickly when NCO is found yet cannot run, e.g., due to linker errors
250 if [ -z "${nco_vrs}" ]; then
251 echo "${spt_nm}: ERROR ${nco_exe} dies with error message on next line:"
252 $(ncks --version)
253 exit 1
254 fi # !nco_vrs
255 lbr_vrs=$(ncks --library 2>&1 > /dev/null | awk '{print $6}')
256
257 # Detect and warn about mixed modules (for Qi Tang 20170531)
258 if [ "${drc_spt}" != "${drc_nco}" ]; then
259 echo "INFO: Mixture of NCO scripts and binaries from different locations. Script ${spt_nm} is from directory ${drc_spt} while NCO binaries are from directory ${drc_nco}. Normally this script and the binaries are from the same executables directory. This INFO may be safely ignored for customized scripts and/or binaries that the user has intentionally split into different directories."
260 echo "HINT (All-users): Conflicting script and binary directories may result from 1) Hardcoding an NCO script and/or binary pathnames, 2) Incomplete NCO installations in one or more directories in the \$PATH environment variable, 3) (Re-)Installing or (re-)building NCO without issuing a \"hash -r\" command afterward to update the executable pathnames that the shell remembers from before."
261 echo "HINT (E3SM-only): In a Conda-based NCO environment, such as E3SM-Unified (which uses NCO in MPAS Analysis and E3SM-Diags), it is possible that some features may be unavailable because the upstream packages (e.g., UDUnits) were not properly linked by Conda. The ncclimo and ncremap scripts contain a mechanism to access the (presumably correctly linked) NCO binary executable and library paths in C. Zender's home directories on the major E3SM machines. Users may turn-on the machine-dependent, hard-coded path by invoking ncclimo and/or ncremap after altering their environment with: \"export NCO_PATH_OVERRIDE=Yes\"."
262 fi # drc_spt
263
264 # When running in a terminal window (not in an non-interactive batch queue)...
265 if [ -n "${TERM}" ]; then
266 # Set fonts for legibility
267 if [ -x /usr/bin/tput ] && tput setaf 1 &> /dev/null; then
268 fnt_bld=`tput bold` # Bold
269 fnt_nrm=`tput sgr0` # Normal
270 fnt_rvr=`tput smso` # Reverse
271 fnt_tlc=`tput sitm` # Italic
272 else
273 fnt_bld="\e[1m" # Bold
274 fnt_nrm="\e[0m" # Normal
275 fnt_rvr="\e[07m" # Reverse
276 fnt_tlc="\e[3m" # Italic
277 fi # !tput
278 fi # !TERM
279
280 # Pre-define enumerated types used in defaults
281 fl_nbr=0 # [nbr] Number of input filenames
282 frg_dfl='dfl' # [sng] Default fragment
283 frg_ncz='ncz' # [sng] NCZarr fragment
284 frg_s3='s3' # [sng] S3 fragment
285 frg_xry='xry' # [sng] Xarray fragment
286 frg_zip='zip' # [sng] Zip fragment
287 frg_zrr='zrr' # [sng] Pure Zarr fragment
288 scm_dfl='dfl' # [sng] Default scheme
289 scm_file='file' # [sng] File scheme
290 scm_s3='s3' # [sng] S3 scheme
291 scm_http='http' # [sng] HTTP scheme
292
293 # Defaults for command-line options and some derived variables
294 # Modify these defaults to save typing later
295 bch_pbs='No' # [sng] PBS batch (non-interactive) job
296 bch_slr='No' # [sng] SLURM batch (non-interactive) job
297 dbg_lvl=0 # [nbr] Debugging level
298 frg_sng='' # [sng] Fragment string to prepend/remove to/from filenames
299 frg_typ="${frg_dfl}" # [sng] Fragment type
300 inp_aut='No' # [sng] Input file list automatically generated
301 inp_glb='No' # [sng] Input file list from globbing directory
302 inp_psn='No' # [sng] Input file list from positional arguments
303 inp_std='No' # [sng] Input file list from stdin
304 scm_sng='' # [sng] Scheme string to prepend/remove to/from filenames
305 scm_typ="${scm_dfl}" # [sng] Scheme type
306 std_chk='Yes' # [sng] Check stdin for input file list
307 vrs_prn='No' # [sng] Print version information
308
309 function fnc_usg_prn { # NB: dash supports fnc_nm (){} syntax, not function fnc_nm{} syntax
310 # Print usage
311 printf "${fnt_rvr}Basic usage:\n${fnt_nrm}${fnt_bld}ls *.nc | ${spt_nm} | ncea ...${fnt_nrm} # Default settings\n"
312 printf "${fnt_bld}ls *.nc | ${spt_nm} -s scm_typ -f frg_typ | ncecat ...${fnt_nrm} # Short options with pre-defined types\n"
313 printf "${fnt_bld}ls *.nc | ${spt_nm} --scm=file --frg=ncz | ncecat ...${fnt_nrm} # Long options with pre-defined types\n"
314 printf "${fnt_bld}ls *.nc | ${spt_nm} --scm_sng=file:// --frg_sng='#mode=nczarr,file' | ncea ...${fnt_nrm} # User-defined scheme and fragment strings\n\n"
315 echo "Command-line options [long-option synonyms in ${fnt_tlc}italics${fnt_nrm}]:"
316 echo "${fnt_rvr}-d${fnt_nrm} ${fnt_bld}dbg_lvl${fnt_nrm} Debug level (default ${fnt_bld}${dbg_lvl}${fnt_nrm}) [${fnt_tlc}dbg_lvl, dbg, debug, debug_level${fnt_nrm}]"
317 echo "${fnt_rvr}-f${fnt_nrm} ${fnt_bld}frg_typ${fnt_nrm} Scheme type (default ${fnt_bld}${frg_typ}${fnt_nrm}) [${fnt_tlc}frg_typ, frg, fragment, fragment_type${fnt_nrm}] [${fnt_tlc}default | nczarr | s3 | xarray | zip | zarr${fnt_nrm}]"
318 echo " ${fnt_bld}--frg_sng${fnt_nrm} User-defined fragment string (empty means none) (default \"${fnt_bld}${frg_sng}${fnt_nrm}\") [${fnt_tlc}frg_sng, fragment_string${fnt_nrm}]"
319 # echo " ${fnt_bld}--hrd_pth${fnt_nrm} Use hard-coded paths on known machines (e.g., chrysalis, compy, cori) NB: Must be first option! [${fnt_tlc}hrd_pth, hard_path, npo, nco_path_override${fnt_nrm}]"
320 echo "${fnt_rvr}-s${fnt_nrm} ${fnt_bld}scm_typ${fnt_nrm} Scheme type (default ${fnt_bld}${scm_typ}${fnt_nrm}) [${fnt_tlc}scm_typ, scm, scheme, scheme_type${fnt_nrm}] [${fnt_tlc}default | file | http | s3${fnt_nrm}]"
321 echo " ${fnt_bld}--scm_sng${fnt_nrm} User-defined scheme string (empty means none) (default \"${fnt_bld}${scm_sng}${fnt_nrm}\") [${fnt_tlc}scm_sng, scheme_string${fnt_nrm}]"
322 echo " ${fnt_bld}--version${fnt_nrm} Version and configuration information [${fnt_tlc}version, vrs, config, configuration, cnf${fnt_nrm}]"
323 printf "\n"
324 printf "${fnt_rvr}Examples:${fnt_nrm}\n"
325 printf "${fnt_bld}ls *.nc | ${spt_nm} | ncea out.nc${fnt_nrm} # Default settings\n"
326 printf "${fnt_bld}ls *.nc | ${spt_nm} -s file -f nczarr | ncea out.nc${fnt_nrm} # Explicit type defaults\n"
327 printf "${fnt_bld}/bin/ls -d ${HOME}/nco/data/h000? | ${spt_nm} --dbg=2${fnt_nrm} # Debug ${spt_nm}\n"
328 printf "${fnt_bld}/bin/ls -d ${HOME}/nco/data/h000? | ${spt_nm} | ncea -O ~/out.nc${fnt_nrm} # Simple test\n"
329 printf "${fnt_bld}/bin/ls -d ${HOME}/nco/data/h000? | ${spt_nm} --scm_sng=file:// --frg_sng='#mode=nczarr,file' | ncea -O ~/out.nc${fnt_nrm} # User-defined scheme, mode\n"
330 printf "\nComplete documentation at http://nco.sf.net/nco.html#${spt_nm}\n\n"
331 exit 1
332 } # !fnc_usg_prn()
333
334 # Parse command-line options:
335 # http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options (see method by Adam Katz)
336 # http://tuxtweaks.com/2014/05/bash-getopts
337 while getopts :d:f:s:-: OPT; do
338 case ${OPT} in
339 d) dbg_lvl="${OPTARG}" ;; # Debugging level
340 f) frg_nm_usr="${OPTARG}" ;; # Fragment name
341 s) scm_nm_usr="${OPTARG}" ;; # Scheme name
342 -) LONG_OPTARG="${OPTARG#*=}"
343 case ${OPTARG} in
344 # Hereafter ${OPTARG} is long argument key, and ${LONG_OPTARG}, if any, is long argument value
345 # Long options with no argument, no short option counterpart
346 # Long options with argument, no short option counterpart
347 # Long options with short counterparts, ordered by short option key
348 dbg_lvl=?* | dbg=?* | debug=?* | debug_level=?* ) dbg_lvl="${LONG_OPTARG}" ;; # -d # Debugging level
349 frg_typ=?* | frg=?* | fragment=?* | fragment_type=?* ) frg_typ="${LONG_OPTARG}" ;; # -f # Fragment type
350 frg_sng=?* | fragment_string=?* ) frg_sng_usr="${LONG_OPTARG}" ;; # -f # Fragment string
351 hrd_pth | hard_path | npo | nco_path_override ) hrd_pth='Yes' ;; # # Use hard-coded paths on known machines (intentional no-op because already handled prior to getopt())
352 hrd_pth=?* | hard_path=?* | npo=?* | nco_path_override=?* ) echo "No argument allowed for --${OPTARG switch}" >&2; exit 1 ;; # # Use hard-coded paths on known machines
353 scm_typ=?* | scm=?* | scheme=?* | scheme_type=?* ) scm_typ="${LONG_OPTARG}" ;; # # Scheme type
354 scm_sng=?* | scheme_string=?* ) scm_sng_usr="${LONG_OPTARG}" ;; # # Scheme string
355 version | vrs | config | configuration | cnf ) vrs_prn='Yes' ;; # # Print version information
356 version=?* | vrs=?* | config=?* | configuration=?* | cnf=?* ) echo "No argument allowed for --${OPTARG switch}" >&2; exit 1 ;; # # Print version information
357 '' ) break ;; # "--" terminates argument processing
358 * ) printf "\nERROR: Unrecognized option ${fnt_bld}--${OPTARG}${fnt_nrm}\n" >&2; fnc_usg_prn ;;
359 esac ;; # !OPTARG
360 \?) # Unrecognized option
361 printf "\nERROR: Option ${fnt_bld}-${OPTARG}${fnt_nrm} not recognized\n" >&2
362 fnc_usg_prn ;;
363 esac # !OPT
364 done # !getopts
365 shift $((OPTIND-1)) # Advance one argument
366 psn_nbr=$#
367 if [ ${psn_nbr} -ge 1 ]; then
368 inp_psn='Yes'
369 # 20200430 Input files on command-line mean we need not check standard-input
370 std_chk='No'
371 fi # !psn_nbr
372
373 if [ ${vrs_prn} = 'Yes' ]; then
374 printf "${spt_nm}, the NCO POSIX<->NCZarr filename filter, version ${nco_vrs} \"${nco_sng}\"\n"
375 printf "Copyright (C) 2022--present Charlie Zender\n"
376 printf "This program is part of NCO, the netCDF Operators\n"
377 printf "NCO is free software and comes with a BIG FAT KISS and ABSOLUTELY NO WARRANTY\n"
378 printf "You may redistribute and/or modify NCO under the terms of the\n"
379 printf "3-Clause BSD License with exceptions described in the LICENSE file\n"
380 printf "BSD: https://opensource.org/licenses/BSD-3-Clause\n"
381 printf "LICENSE: https://github.com/nco/nco/tree/master/LICENSE\n"
382 printf "Config: ${spt_nm} script located in directory ${drc_spt}\n"
383 printf "Config: NCO binaries located in directory ${drc_nco}, linked to netCDF library version ${lbr_vrs}\n"
384 if [ "${hrd_pth_fnd}" = 'Yes' ]; then
385 printf "Config: Employ NCO machine-dependent hardcoded paths/modules for ${HOSTNAME}. (If desired, turn-off NCO hardcoded paths with \"export NCO_PATH_OVERRIDE=No\").\n"
386 else
387 printf "Config: No hardcoded machine-dependent path/module overrides. (If desired, turn-on NCO hardcoded paths at supported national labs with \"export NCO_PATH_OVERRIDE=Yes\").\n"
388 fi # !hrd_pth_fnd
389 exit 0
390 fi # !vrs_prn
391
392 # Detect input on pipe to stdin:
393 # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script
394 # http://unix.stackexchange.com/questions/33049/check-if-pipe-is-empty-and-run-a-command-on-the-data-if-it-isnt
395 # 20170119 "if [ ! -t 0 ]" tests whether unit 0 (stdin) is connected to terminal, not whether pipe has data
396 # Non-interactive batch mode (e.g., qsub, sbatch) disconnects stdin from terminal and triggers false-positives with ! -t 0
397 # 20170123 "if [ -p foo ]" tests whether foo exists and is a pipe or named pipe
398 # Non-interactive batch mode (i.e., sbatch) behaves as desired for -p /dev/stdin on SLURM
399 # Non-interactive batch mode (e.g., qsub) always returns true for -p /dev/stdin on PBS, leads to FALSE POSITIVES!
400 # This is because PBS uses stdin to set the job name
401 # Hence -p /dev/stdin test works everywhere tested except PBS non-interactive batch environment
402 # Check stdin if user has not explicitly disallowed it with --no_stdin
403 if [ "${std_chk}" = 'Yes' ]; then
404 if [ -n "${PBS_ENVIRONMENT}" ]; then
405 if [ "${PBS_ENVIRONMENT}" = 'PBS_BATCH' ]; then
406 # PBS batch detection suggested by OLCF ticket CCS #338970 on 20170127
407 bch_pbs='Yes'
408 fi # !PBS_ENVIRONMENT
409 fi # !PBS
410 if [ -n "${SLURM_JOBID}" ] && [ -z "${SLURM_PTY_PORT}" ]; then
411 # SLURM batch detection suggested by NERSC ticket INC0096873 on 20170127
412 bch_slr='Yes'
413 fi # !SLURM
414 if [ ${bch_pbs} = 'Yes' ] || [ ${bch_slr} = 'Yes' ]; then
415 # Batch environment
416 if [ ${bch_pbs} = 'Yes' ]; then
417 if [ ! -p /dev/stdin ]; then
418 # PBS batch jobs cause -p to return true except for stdin redirection
419 # When -p returns true we do not know whether stdin pipe contains any input
420 # User must explicitly indicate use of stdin pipes with --stdin option
421 # Redirection in PBS batch jobs unambiguously causes -p to return false
422 inp_std='Yes'
423 fi # !stdin
424 fi # !bch_slr
425 if [ ${bch_slr} = 'Yes' ]; then
426 if [ -p /dev/stdin ]; then
427 # SLURM batch jobs cause -p to return true for stdin pipes
428 # When -p returns false we do not know whether output was redirectd
429 # User must explicitly indicate use of redirection with --stdin option
430 # Stdin pipes in SLURM batch jobs unambiguously cause -p to return true
431 inp_std='Yes'
432 fi # !stdin
433 fi # !bch_slr
434 else # !bch
435 # Interactive environment
436 if [ -p /dev/stdin ] || [ ! -t 0 ]; then
437 # Interactive environments unambiguously cause -p to return true for stdin pipes
438 # Interactive environments unambiguously cause -t 0 to return false for stdin redirection
439 inp_std='Yes'
440 fi # !stdin
441 fi # !bch
442 if [ ${inp_std} = 'Yes' ] && [ ${inp_psn} = 'Yes' ]; then
443 echo "${spt_nm}: ERROR expecting input from both stdin and positional command-line arguments"
444 exit 1
445 fi # !inp_std
446 fi # !std_chk
447
448 # Read files from stdin pipe, positional arguments, or directory glob
449 #printf "dbg: inp_aut = ${inp_aut}\n"
450 #printf "dbg: inp_glb = ${inp_glb}\n"
451 #printf "dbg: inp_psn = ${inp_psn}\n"
452 #printf "dbg: inp_std = ${inp_std}\n"
453
454 # Check argument number and complain accordingly
455 arg_nbr=$#
456 #printf "\ndbg: Number of arguments: ${arg_nbr}"
457 if [ ${inp_std} != 'Yes' ]; then
458 if [ ${arg_nbr} -eq 0 ]; then
459 fnc_usg_prn
460 fi # !arg_nbr
461 fi # !inp_std
462
463 if [ -n "${frg_sng_usr}" ]; then
464 frg_typ='user-defined'
465 frg_sng="${frg_sng_usr}"
466 fi # !frg_sng_usr
467 if [ -n "${scm_sng_usr}" ]; then
468 sng_typ='user-defined'
469 scm_sng="${scm_sng_usr}"
470 fi # !scm_sng_usr
471
472 if [ -z "${frg_sng_usr}" ]; then
473 if [ "${frg_typ}" = ${frg_dfl} ] || [[ "${frg_typ}" =~ dfl ]] || [[ "${frg_typ}" =~ default ]]; then
474 frg_typ=${frg_ncz}
475 fi # !frg_dfl
476 if [ "${frg_typ}" = ${frg_ncz} ] || [[ "${frg_typ}" =~ (ncz|[nN][cC][zZ]arr) ]]; then
477 frg_typ=${frg_ncz}
478 frg_sng='#mode=nczarr,file'
479 elif [ "${frg_typ}" = ${frg_s3} ] || [ "${frg_typ}" = 's3' ] || [[ "${frg_typ}" =~ [sS]3 ]] || [[ "${frg_typ}" =~ (aws|AWS) ]] || [[ "${frg_typ}" =~ [aA]mazon ]]; then
480 frg_typ=${frg_s3}
481 frg_sng='#mode=nczarr,s3'
482 elif [ "${frg_typ}" = ${frg_xry} ] || [[ "${frg_typ}" =~ (xry|xarray) ]]; then
483 frg_typ=${frg_xry}
484 frg_sng='#mode=xarray,file'
485 elif [ "${frg_typ}" = ${frg_zip} ] || [[ "${frg_typ}" =~ [zZ]ip ]]; then
486 frg_typ=${frg_zip}
487 frg_sng='#mode=nczarr,zip'
488 elif [ "${frg_typ}" = ${frg_zrr} ] || [[ "${frg_typ}" =~ (zrr|ZRR|Zarr|zarr) ]]; then
489 frg_typ=${frg_zrr}
490 frg_sng='#mode=zarr,file'
491 else
492 echo "ERROR: Invalid -f frg_typ option = ${frg_typ}"
493 echo "HINT: Valid frg_typ arguments include '${frg_dfl}' (or 'default'), '${frg_ncz}' (or 'nczarr' or 'NCZarr'), '${frg_s3}' (or 'S3'), '${frg_xry}' (or 'xarray'), '${frg_zip}' (or 'Zip'), and '${frg_zrr}' (or 'zarr' or 'Zarr'). For testing, select '${frg_dfl}' which causes ${spt_nm} to do something useful."
494 exit 1
495 fi # !frg_typ
496 fi # !frg_sng_usr
497
498 if [ -z "${scm_sng_usr}" ]; then
499 if [ "${scm_typ}" = ${scm_dfl} ] || [[ "${scm_typ}" =~ dfl ]] || [[ "${scm_typ}" =~ default ]]; then
500 scm_typ=${scm_file}
501 fi # !scm_dfl
502 if [ "${scm_typ}" = ${scm_file} ] || [[ "${scm_typ}" =~ [fF]ile ]] || [[ "${scm_typ}" =~ [fF]l ]]; then
503 scm_typ=${scm_file}
504 scm_sng='file://'
505 elif [ "${scm_typ}" = ${scm_http} ] || [[ "${scm_typ}" =~ (http|HTTP) ]]; then
506 scm_typ=${scm_http}
507 scm_sng='https://'
508 elif [ "${scm_typ}" = ${scm_s3} ] || [ "${scm_typ}" = 's3' ] || [[ "${scm_typ}" =~ [sS]3 ]] || [[ "${scm_typ}" =~ (aws|AWS) ]] || [[ "${scm_typ}" =~ [aA]mazon ]]; then
509 scm_typ=${scm_s3}
510 scm_sng='s3://'
511 else
512 echo "ERROR: Invalid -s scm_typ option = ${scm_typ}"
513 echo "HINT: Valid scm_typ arguments include '${scm_file}' (or 'fl'), '${scm_http}' (or 'HTTP'), and '${scm_s3}' (or 'S3' or 'AWS' or 'amazon'). For testing, select '${scm_file}' which causes ${spt_nm} to do something useful."
514 exit 1
515 fi # !scm_typ
516 fi # !scm_sng_usr
517
518 if [ ${inp_glb} = 'Yes' ]; then
519 for fl in "${drc_in}"/*.nc "${drc_in}"/*.nc3 "${drc_in}"/*.nc4 "${drc_in}"/*.nc5 "${drc_in}"/*.nc6 "${drc_in}"/*.nc7 "${drc_in}"/*.cdf "${drc_in}"/*.hdf "${drc_in}"/*.he5 "${drc_in}"/*.h5 ; do
520 if [ -f "${fl}" ]; then
521 fl_in[${fl_nbr}]=${fl}
522 let fl_nbr=${fl_nbr}+1
523 fi # !file
524 done
525 fi # !inp_glb
526 if [ ${inp_psn} = 'Yes' ]; then
527 # Read any positional arguments
528 for ((psn_idx=1;psn_idx<=psn_nbr;psn_idx++)); do
529 fl_in[(${psn_idx}-1)]=${!psn_idx}
530 fl_nbr=${psn_nbr}
531 done # !psn_idx
532 fi # !inp_psn
533 if [ ${inp_std} = 'Yes' ]; then
534 # Input awaits on unit 0, i.e., on stdin
535 while read -r line; do # NeR05 p. 179
536 fl_in[${fl_nbr}]=${line}
537 let fl_nbr=${fl_nbr}+1
538 done < /dev/stdin
539 fi # !inp_std
540
541 # Prepend drc_in to fl_in in MFOs (ncea, ncecat, ncra, ncrcat)
542 ppn_opt="-p ${drc_in}"
543 # 20220111 If input files include absolute path, then use fl_in as-is later on
544 # 20220130 fl_in is defined here only for inp_[std,cmd,glb], do not test iff inp_aut
545 if [ "${fl_in[0]}" ] && [ "$(basename ${fl_in[0]})" != "${fl_in[0]}" ]; then
546 ppn_opt=''
547 fi # !basename
548
549 # Print initial state
550 if [ ${dbg_lvl} -ge 2 ]; then
551 printf "dbg: dbg_lvl = ${dbg_lvl}\n"
552 printf "dbg: frg_sng = ${frg_sng}\n"
553 printf "dbg: frg_typ = ${frg_typ}\n"
554 printf "dbg: hrd_pth = ${hrd_pth}\n"
555 printf "dbg: inp_aut = ${inp_aut}\n"
556 printf "dbg: inp_glb = ${inp_glb}\n"
557 printf "dbg: inp_psn = ${inp_psn}\n"
558 printf "dbg: inp_std = ${inp_std}\n"
559 printf "dbg: scm_sng = ${scm_sng}\n"
560 printf "dbg: scm_typ = ${scm_typ}\n"
561 fi # !dbg
562 if [ ${dbg_lvl} -ge 2 ]; then
563 psn_nbr=$#
564 if [ ${psn_nbr} -ge 1 ]; then
565 printf "dbg: Found ${psn_nbr} positional parameters (besides \$0):\n"
566 for ((psn_idx=1;psn_idx<=psn_nbr;psn_idx++)); do
567 printf "dbg: psn_arg[${psn_idx}] = ${!psn_idx}\n"
568 done # !psn_idx
569 fi # !psn_nbr
570 if [ "${fl_nbr}" -ge 1 ]; then
571 printf "dbg: Filtering ${fl_nbr} filenames from/to stdin:\n"
572 for ((fl_idx=0;fl_idx<fl_nbr;fl_idx++)); do
573 printf "dbg: fl_in[${fl_idx}] = ${fl_in[${fl_idx}]}\n"
574 done # !fl_idx
575 fi # !fl_nbr
576 fi # !dbg
577
578 # Human-readable summary
579 date_srt=$(date +"%s")
580 if [ ${dbg_lvl} -ge 2 ]; then
581 printf "ncz2psx, NCO's NCZarr<->POSIX filter, invoked with command:\n"
582 echo "${cmd_ln}"
583 fi # !dbg
584
585 # Main Loop
586 if [ "${fl_nbr}" -ge 1 ]; then
587 # Prepend scheme string to filename
588 for ((fl_idx=0;fl_idx<fl_nbr;fl_idx++)); do
589 fl_out[${fl_idx}]="${scm_sng}${fl_in[${fl_idx}]}${frg_sng}"
590 #printf "fl_out[${fl_idx}] = ${fl_out[${fl_idx}]}\n"
591 echo ${fl_out[${fl_idx}]}
592 done # !fl_idx
593 fi # !fl_nbr
594
595 if [ ${dbg_lvl} -ge 2 ]; then
596 date_end=$(date +"%s")
597 date_dff=$((date_end-date_srt))
598 echo "Elapsed time $((date_dff/60))m$((date_dff % 60))s"
599 fi # !dbg
600
601 exit 0