Debian Codegraph @

data/ncsplit

raw
1 #!/bin/bash
2
3 # Purpose: Split CESM'ish and ACME'ish raw history tape files into (possibly regridded) single variable timeseries
4
5 # Copyright (C) 2015-2016 Charlie Zender
6 # This file is part of NCO, the netCDF Operators. NCO is free software.
7 # You may redistribute and/or modify NCO under the terms of the
8 # GNU General Public License (GPL) Version 3.
9
10 # As a special exception to the terms of the GPL, you are permitted
11 # to link the NCO source code with the HDF, netCDF, OPeNDAP, and UDUnits
12 # libraries and to distribute the resulting executables under the terms
13 # of the GPL, 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 GNU General Public 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 definition and looping constructs
31
32 # Source: https://github.com/nco/nco/tree/master/data/ncsplit
33 # Documentation: http://nco.sf.net/nco.html#ncsplit
34 # Additional Documentation:
35 # HowTo: https://acme-climate.atlassian.net/wiki/display/ATM/Generating+Climo+files
36 # ACME Timeseries Requirements: https://acme-climate.atlassian.net/wiki/display/ATM/Climo+Files+-+v0.3+AMIP+runs
37
38 # Direct install:
39 # scp ~/nco/data/ncsplit aims4.llnl.gov:bin
40 # scp ~/nco/data/ncsplit cooley.alcf.anl.gov:bin
41 # scp ~/nco/data/ncsplit cori.nersc.gov:bin_cori
42 # scp ~/nco/data/ncsplit edison.nersc.gov:bin_edison
43 # scp ~/nco/data/ncsplit rhea.ccs.ornl.gov:bin_rhea
44 # scp ~/nco/data/ncsplit yellowstone.ucar.edu:bin
45 # scp dust.ess.uci.edu:nco/data/ncsplit ~/bin
46
47 # Set script name, directory, PID, run directory
48 drc_pwd=${PWD}
49 # Set these before 'module' command which can overwrite ${BASH_SOURCE[0]}
50 # NB: dash supports $0 syntax, not ${BASH_SOURCE[0]} syntax
51 # http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
52 spt_src="${BASH_SOURCE[0]}"
53 [[ -z "${spt_src}" ]] && spt_src="${0}" # Use ${0} when BASH_SOURCE is unavailable (e.g., dash)
54 while [ -h "${spt_src}" ]; do # Recursively resolve ${spt_src} until file is no longer a symlink
55 drc_spt="$( cd -P "$( dirname "${spt_src}" )" && pwd )"
56 spt_src="$(readlink "${spt_src}")"
57 [[ ${spt_src} != /* ]] && spt_src="${drc_spt}/${spt_src}" # If ${spt_src} was relative symlink, resolve it relative to path where symlink file was located
58 done
59 drc_spt="$( cd -P "$( dirname "${spt_src}" )" && pwd )"
60 spt_nm=$(basename ${spt_src}) # [sng] Script name (Unlike $0, ${BASH_SOURCE[0]} works well with 'source <script>')
61 spt_pid=$$ # [nbr] Script PID (process ID)
62
63 # Configure paths at High-Performance Computer Centers (HPCCs) based on ${HOSTNAME}
64 if [ -z "${HOSTNAME}" ]; then
65 if [ -f /bin/hostname ] && [ -x /bin/hostname ]; then
66 export HOSTNAME=`/bin/hostname`
67 elif [ -f /usr/bin/hostname ] && [ -x /usr/bin/hostname ]; then
68 export HOSTNAME=`/usr/bin/hostname`
69 fi # !hostname
70 fi # HOSTNAME
71 # Default input and output directory is ${DATA}
72 if [ -z "${DATA}" ]; then
73 case "${HOSTNAME}" in
74 constance* | node* ) DATA='/scratch' ; ;; # PNNL
75 cooley* | cc* | mira* ) DATA="/projects/HiRes_EarthSys/${USER}" ; ;; # ALCF cooley compute nodes named ccNNN, 384 GB/node
76 cori* | edison* ) DATA="${SCRATCH}" ; ;; # NERSC cori/edison compute nodes all named nidNNNNN, edison 24|64 cores|GB/node; cori 32|128 cores|GB/node (cori login nodes 512 GB)
77 pileus* ) DATA="/lustre/atlas/world-shared/cli115/${USER}" ; ;; # OLCF CADES
78 rhea* | titan* ) DATA="/lustre/atlas/world-shared/cli115/${USER}" ; ;; # OLCF rhea compute nodes named rheaNNN, 128 GB/node
79 ys* ) DATA="/glade/p/work/${USER}" ; ;; # NCAR yellowstone compute nodes named ysNNN, 32 GB/node
80 * ) DATA='/tmp' ; ;; # Other
81 esac # !HOSTNAME
82 fi # DATA
83 # Ensure batch jobs access correct 'mpirun' (or, with SLURM, 'srun') command, netCDF library, and NCO executables and library:
84 case "${HOSTNAME}" in
85 aims* )
86 export PATH='/export/zender1/bin'\:${PATH}
87 export LD_LIBRARY_PATH='/export/zender1/lib'\:${LD_LIBRARY_PATH} ; ;;
88 cooley* )
89 # 20160421: Split cooley from mira binary locations to allow for different system libraries
90 # http://www.mcs.anl.gov/hs/software/systems/softenv/softenv-intro.html
91 soft add +mvapich2
92 export PBS_NUM_PPN=12 # Spoof PBS on Soft (which knows nothing about node capabilities)
93 export PATH='/home/zender/bin_cooley'\:${PATH}
94 export LD_LIBRARY_PATH='/home/zender/lib_cooley'\:${LD_LIBRARY_PATH} ; ;;
95 mira* )
96 export PATH='/home/zender/bin_mira'\:${PATH}
97 export LD_LIBRARY_PATH='/soft/libraries/netcdf/current/library:/home/zender/lib_mira'\:${LD_LIBRARY_PATH} ; ;;
98 cori* )
99 # 20160407: Split cori from edison binary locations to allow for different system libraries
100 # 20160420: module load gsl, udunits required for non-interactive batch submissions by Wuyin Lin
101 # Not necessary for interactive, nor for CSZ non-interactive, batch submisssions
102 # Must be due to home environment differences between CSZ and other users
103 # Loading gsl and udunits seems to do no harm, so always do it
104 # This is equivalent to LD_LIBRARY_PATH method used for netCDF and SZIP on rhea
105 # Why do cori/edison and rhea require workarounds for different packages?
106 module load gsl
107 module load udunits
108 # On cori, module load ncl installs ERWG in ${NCARG_ROOT}/../intel/bin
109 if [ -n "${NCARG_ROOT}" ]; then
110 export PATH="${NCARG_ROOT}/bin:${PATH}"
111 fi # !NCARG_ROOT
112 export PATH='/global/homes/z/zender/bin_cori'\:${PATH}
113 export LD_LIBRARY_PATH='/global/homes/z/zender/lib_cori'\:${LD_LIBRARY_PATH} ; ;;
114 edison* )
115 module load gsl
116 module load udunits
117 export PATH='/global/homes/z/zender/bin_edison'\:${PATH}
118 export LD_LIBRARY_PATH='/global/homes/z/zender/lib_edison'\:${LD_LIBRARY_PATH} ; ;;
119 pileus* )
120 export PATH='/home/zender/bin'\:${PATH}
121 export LD_LIBRARY_PATH='/opt/ACME/uvcdat-2.2-build/install/Externals/lib:/home/zender/lib'\:${LD_LIBRARY_PATH} ; ;;
122 rhea* )
123 # 20151017: CSZ next three lines guarantee finding mpirun
124 source ${MODULESHOME}/init/sh # 20150607: PMC Ensures find module commands will be found
125 module unload PE-intel # Remove Intel-compiled mpirun environment
126 module load PE-gnu # Provides GCC-compiled mpirun environment (CSZ uses GCC to build NCO on rhea)
127 # 20160219: CSZ UVCDAT setup causes failures with mpirun, attempting a work-around
128 if [ -n "${UVCDAT_SETUP_PATH}" ]; then
129 module unload python ompi paraview PE-intel PE-gnu
130 module load gcc
131 source /lustre/atlas1/cli900/world-shared/sw/rhea/uvcdat/latest_full/bin/setup_runtime.sh
132 export ${UVCDAT_SETUP_PATH}
133 fi # !UVCDAT_SETUP_PATH
134 # On rhea, module load ncl installs ERWG in ${NCL_DIR}/bin
135 if [ -n "${NCL_DIR}" ]; then
136 export PATH="${NCL_DIR}/bin:${PATH}"
137 fi # !NCL_DIR
138 export PATH='/ccs/home/zender/bin_rhea'\:${PATH}
139 export LD_LIBRARY_PATH='/sw/redhat6/netcdf/4.3.3.1/rhel6.6_gcc4.8.2--with-dap+hdf4/lib:/sw/redhat6/szip/2.1/rhel6.6_gnu4.8.2/lib:/ccs/home/zender/lib_rhea'\:${LD_LIBRARY_PATH} ; ;;
140 titan* )
141 source ${MODULESHOME}/init/sh # 20150607: PMC Ensures find module commands will be found
142 module load gcc
143 export PATH='/ccs/home/zender/bin_titan'\:${PATH}
144 export LD_LIBRARY_PATH='/opt/cray/netcdf/4.3.2/GNU/49/lib:/sw/xk6/udunits/2.1.24/sl_gcc4.5.3/lib:/ccs/home/zender/lib_titan'\:${LD_LIBRARY_PATH} ; ;;
145 ys* )
146 # 20151018: Yellowstone support not yet tested in batch mode
147 # On yellowstone, module load ncl installs ERWG in /glade/apps/opt/ncl/6.3.0/intel/12.1.5/bin (not in ${NCARG_ROOT}/bin)
148 if [ -n "${NCARG_ROOT}" ]; then
149 # export PATH="${NCARG_ROOT}/bin:${PATH}"
150 export PATH="${PATH}:/glade/apps/opt/ncl/6.3.0/intel/12.1.5/bin"
151 fi # !NCARG_ROOT
152 export PATH='/glade/u/home/zender/bin'\:${PATH}
153 export LD_LIBRARY_PATH='/glade/apps/opt/netcdf/4.3.0/intel/12.1.5/lib:/glade/u/home/zender/lib'\:${LD_LIBRARY_PATH}
154 esac # !HOSTNAME
155
156 # Production usage:
157 # ncsplit -c famipc5_ne30_v0.3_00003 -s 1980 -e 1983 -i /lustre/atlas1/cli115/world-shared/mbranst/famipc5_ne30_v0.3_00003-wget-test -o ${DATA}/ne30/clm
158 # ncsplit -c famipc5_ne120_v0.3_00003 -s 1980 -e 1983 -i /lustre/atlas1/cli115/world-shared/mbranst/famipc5_ne120_v0.3_00003-wget-test -o ${DATA}/ne120/clm
159 # ncsplit -c B1850C5e1_ne30 -s 2 -e 199 -i /lustre/atlas1/cli115/world-shared/mbranst/B1850C5e1_ne30/atm/hist -o ${DATA}/ne30/clm
160
161 # Incremental climo testing:
162 # ncsplit -v FSNT,AODVIS -c famipc5_ne30_v0.3_00003 -s 1980 -e 1981 -i ${DATA}/ne30/raw -o ${DATA}/ne30/prv -r ${DATA}/maps/map_ne30np4_to_fv129x256_aave.20150901.nc
163 # ncsplit -v FSNT,AODVIS -c famipc5_ne30_v0.3_00003 -s 1982 -e 1983 -i ${DATA}/ne30/raw -o ${DATA}/ne30/clm -r ${DATA}/maps/map_ne30np4_to_fv129x256_aave.20150901.nc -x ${DATA}/ne30/prv -X ${DATA}/ne30/xtn -S 1980
164 # Binary climo testing:
165 # ncsplit -v FSNT,AODVIS -c famipc5_ne30_v0.3_00003 -S 1980 -E 1981 -x ${DATA}/ne30/prv -s 1982 -e 1983 -i ${DATA}/ne30/clm -X ${DATA}/ne30/xtn
166
167 # Debugging and Benchmarking:
168 # ncsplit -v FSNT,AODVIS -c famipc5_ne30_v0.3_00003 -s 1980 -e 1983 -i ${DATA}/ne30/raw -o ${DATA}/ne30/clm -r ${DATA}/maps/map_ne30np4_to_fv129x256_aave.20150901.nc
169 # ncsplit -v TOTEXTTAU -c merra2_198001.nc4 -s 1980 -e 2015 -a sdd -i ${DATA}/merra2/raw -o ${DATA}/merra2/clm
170 # ncsplit > ~/ncsplit.out 2>&1 &
171 # ncsplit -c B1850C5e1_ne30 -s 2 -e 199 > ~/ncsplit.out 2>&1 &
172 # ncsplit -c ne30_gx1.B1850c5d -s 6 -e 7 > ~/ncsplit.out 2>&1 &
173 # ncsplit -d 2 -v FSNT -m cam2 -c essgcm14 -s 1 -e 20 -i ${DATA}/essgcm14 -o ${DATA}/anl > ~/ncsplit.out 2>&1 &
174 # ncsplit -c famipc5_ne30_v0.3_00003 -s 1980 -e 1983 -i /lustre/atlas1/cli115/world-shared/mbranst/famipc5_ne30_v0.3_00003-wget-test -o ${DATA}/ne30/clm > ~/ncsplit.out 2>&1 &
175 # ncsplit -c famipc5_ne120_v0.3_00003 -s 1980 -e 1983 -i /lustre/atlas1/cli115/world-shared/mbranst/famipc5_ne120_v0.3_00003-wget-test -o ${DATA}/ne120/clm > ~/ncsplit.out 2>&1 &
176 # MPAS: Prior to running ncsplit on MPAS output, annotate missing values of input with, e.g.,
177 # for fl in `ls hist.*` ; do
178 # ncatted -O -t -a _FillValue,,o,d,-9.99999979021476795361e+33 ${fl}
179 # done
180 # ncsplit -v temperature -c hist -s 2 -e 3 -m ocn -i /lustre/atlas1/cli112/proj-shared/golaz/ACME_simulations/20160121.A_B2000ATMMOD.ne30_oEC.titan.a00/run -r ${DATA}/maps/map_oEC60to30_to_t62_bilin.20160301.nc -o ${DATA}/mpas/clm > ~/ncsplit.out 2>&1 &
181 # ncsplit -v iceAreaCell -c hist -s 2 -e 3 -m ice -i /lustre/atlas1/cli112/proj-shared/golaz/ACME_simulations/20160121.A_B2000ATMMOD.ne30_oEC.titan.a00/run -r ${DATA}/maps/map_oEC60to30_to_t62_bilin.20160301.nc -o ${DATA}/mpas/clm > ~/ncsplit.out 2>&1 &
182
183 # Best performance on resolutions finer than ne30 (~1x1 degree) requires a job scheduler/batch processor
184 # Cobalt (cooley), SLURM (cori,edison), Torque (a PBS-variant) (hopper), and PBS (rhea) schedulers allow both interactive and non-interactive (i.e., script) batch jobs
185 # ALCF Cobalt:
186 # softenv # lists available packages
187 # http://www.mcs.anl.gov/hs/software/systems/softenv/softenv-intro.html
188 # http://www.alcf.anl.gov/user-guides/using-cobalt-cooley
189 # https://www.alcf.anl.gov/user-guides/cobalt-job-control
190 # NERSC SLURM:
191 # https://www.nersc.gov/users/computational-systems/cori/running-jobs/slurm-introduction
192 # https://www.nersc.gov/users/computational-systems/cori/running-jobs/for-edison-users/torque-moab-vs-slurm-comparisons
193 # https://www.nersc.gov/users/computational-systems/cori/running-jobs/queues-and-policies/
194 # https://www.nersc.gov/users/computational-systems/edison/running-jobs/queues-and-policies/
195 # OLCF PBS:
196 # https://www.olcf.ornl.gov/support/system-user-guides/rhea-user-guide/#903
197 # Requesting interactive nodes, Submitting non-interactive batch jobs, Monitoring queues, Deleting jobs:
198 # Cobalt: qsub -I, qsub, qstat, qdel
199 # PBS: qsub -I, qsub, qstat, qdel
200 # SLURM: salloc, sbatch, squeue, scancel
201 # Interactive queue: a) Reserve nodes and acquire prompt on control node b) Execute ncsplit command interactively
202 # Cooley: qsub -I -A HiRes_EarthSys --nodecount=12 --time=00:30:00 --jobname=ncsplit
203 # Cori: salloc -A acme --nodes=12 --partition=debug --time=00:30:00 --job-name=ncsplit # NB: 30 minute limit, Edison too
204 # Hopper: qsub -I -A acme -V -l mppwidth=288 -l walltime=00:30:00 -q debug -N ncsplit # deprecated, old Edison
205 # Rhea: qsub -I -A CLI115 -V -l nodes=12 -l walltime=00:30:00 -N ncsplit # Bigmem: -l partition=gpu
206 # Yellow: fxm # Bigmem:
207 # Non-interactive batch procedure: a) Store ncsplit command in ncsplit.[cobalt|pbs|slurm] b) qsub ncsplit.[cobalt|pbs|slurm]
208 # Non-interactive batch queue differences (besides argument syntax):
209 # 1. Cobalt and SLURM require initial 'shebang' line to specify the shell interpreter (not required on PBS)
210 # 2. Cobalt appends stdout/stderr to existing output files, if any, whereas PBS overwrites existing files
211 # 3. Cobalt uses ${COBALT_NODEFILE} and (NA) whereas PBS uses ${PBS_NODEFILE} and ${PBS_NUM_PPN}, respectively, and SLURM uses ${SLURM_NODELIST} and ${SLURM_CPUS_ON_NODE}, respectively
212 # 4. SLURM automatically combines stdout and stderr, yet does not understand tilde (~ = home directory) expansion in error/output filenames
213 # Differences 1 & 2 impose slightly different invocations; difference 3 requires abstracting environment variables; difference 4 requires omitting ~'s
214 # Cooley a): /bin/rm -f ~/ncsplit.err ~/ncsplit.out
215 # echo '#!/bin/bash' > ~/ncsplit.cobalt
216 # echo "ncsplit -d 1 -p mpi -c b1850c5_m2a -s 55 -e 58 -i /home/taylorm/scratch1.qtang/b1850c5_m2a/run -o ${DATA}/ne120/clm" >> ~/ncsplit.cobalt;chmod a+x ~/ncsplit.cobalt
217 # Cori,Edison a): echo '#!/bin/bash' > ~/ncsplit.slurm
218 # echo "ncsplit -a scd -d 1 -p mpi -c famipc5_ne30_v0.3_00003 -s 1980 -e 1983 -i ${DATA}/ne30/raw -o ${DATA}/ne30/clm -r ${DATA}/maps/map_ne30np4_to_fv129x256_aave.20150901.nc" >> ~/ncsplit.slurm;chmod a+x ~/ncsplit.slurm
219 # Rhea a): echo "ncsplit -a scd -d 1 -p mpi -c famipc5_ne120_v0.3_00003 -s 1980 -e 1983 -i /lustre/atlas1/cli115/world-shared/mbranst/famipc5_ne120_v0.3_00003-wget-test -o ${DATA}/ne120/clm -r ${DATA}/maps/map_ne120np4_to_fv257x512_aave.20150901.nc" > ~/ncsplit.pbs;chmod a+x ~/ncsplit.pbs
220 # Cooley b): qsub -A HiRes_EarthSys --nodecount=12 --time=00:30:00 --jobname ncsplit --error ~/ncsplit.err --output ~/ncsplit.out --notify [email protected] ~/ncsplit.cobalt
221 # Cori,Edison b): sbatch -A acme --nodes=12 --time=00:30:00 --partition=regular --job-name=ncsplit --mail-type=END --output=ncsplit.out ~/ncsplit.slurm
222 # Hopper b): qsub -A acme -V -l mppwidth=288 -l walltime=00:30:00 -q regular -N ncsplit -j oe -m e -o ~/ncsplit.out ~/ncsplit.pbs
223 # Rhea b): qsub -A CLI115 -V -l nodes=12 -l walltime=00:30:00 -N ncsplit -j oe -m e -o ~/ncsplit.out ~/ncsplit.pbs
224
225 # Normal use: Set five "mandatory" inputs (caseid, yr_srt, yr_end, drc_in, drc_out), and possibly rgr_map, on command line
226 # caseid: Simulation name (filenames must start with ${caseid})
227 # drc_in: Input directory for raw data
228 # Years outside yr_srt and yr_end are ignored
229 # yr_srt should, and for SDD mode must, contain complete year of output
230 # SCD mode ignores Jan-Nov of yr_srt
231 # Dec of yr_end is excluded from the seasonal and monthly analysis in SCD mode
232 # yr_end should, and for SDD mode must, contain complete year of output
233 # drc_out: Output directory for processed native grid timeseries ("climo files")
234 # User needs write permission for ${drc_out}
235 # rgr_map: Regridding map, if non-NULL, invoke regridder with specified map on output datasets
236 # Pass options intended exclusively for the NCO regridder as arguments to the -R switch
237 # yr_srt: Year of first January to analyze
238 # yr_end: Year of last January to analyze
239
240 # Other options (often their default settings work well):
241 # clm_md: Timeseries mode, i.e., how to treat December. One of two options:
242 # Seasonally-contiguous-december (SCD) mode (clm_md=scd) (default)
243 # Seasonally-discontiguous-december (SDD) mode (clm_md=sdd)
244 # Both modes use an integral multiple of 12 months, and _never alter any input files_
245 # SCD climatologies begin in Dec of yr_srt-1, and end in Nov of yr_end
246 # SDD climatologies begin in Jan of yr_srt, and end in Dec of yr_end
247 # SCD excludes Jan-Nov of yr_srt-1 and Dec of yr_end (i.e., SCD excludes 12 months of available data)
248 # SDD uses all months of yr_srt through yr_end (i.e., SDD can use all available data)
249 # SCD seasonal averages are inconsistent with (calendar-year-based) annual averages, but better capture seasonal the "natural" (not calendar-year-based) climate year
250 # SDD seasonal averages are fully consistent with (calendar-year-based) annual averages
251 # drc_rgr: Regridding directory---store regridded files, if any, in drc_rgr rather than drc_out
252 # lnk_flg: Link ACME-climo to AMWG-climo filenames
253 # AMWG omits the YYYYMM components of climo filenames, resulting in shorter names
254 # This switch (on by default) symbolically links the full (ACME) filename to the shorter (AMWG) name
255 # AMWG diagnostics scripts can produce plots directly from these linked filenames
256 # par_typ: Parallelism type---all values _except_ exact matches to "bck" and "mpi" are interpreted as "nil" (and invoke serial mode)
257 # bck = Background: Spawn children (basic blocks) as background processes on control node then wait()
258 # Works best when available RAM > 12*4*sizeof(monthly input file), otherwise jobs swap-to-disk
259 # mpi = MPI: Spawn children (basic blocks) as MPI processes (one per node in batch environment) then wait()
260 # Requires batch system with PBS and MPI. Use when available RAM/node < 12*2.5*sizeof(monthly input file).
261 # Optimized for batch with 12 nodes. Factors thereof (6, 4, 3, 2 nodes) should also work.
262 # Remember to request 12 nodes if possible!
263 # nil = None: Execute script in serial mode on single node
264 # Works best when available RAM < 12*4*sizeof(monthly input file), otherwise jobs swap-to-disk
265 # var_lst: Variables to include, or, with nco_opt='-x', to exclude, in comma-separated list format, e.g.,
266 # 'FSNT,AODVIS'. Regular expressions work, too: 'AOD.?'
267
268 # Infrequently used options:
269 # bnd_nm: Name of bounds dimension (examples include 'nbnd' (default), 'tbnd' (CAM2, CAM3), 'hist_interval' (CLM2)
270 # dbg_lvl: 0 = Quiet, print basic status during evaluation
271 # 1 = Print configuration, full commands, and status to output during evaluation
272 # 2 = As in dbg_lvl=1, but do not evaluate commands
273 # 3 = As in dbg_lvl=2, with additional information (mainly for batch queues)
274 # fml_nm: Family name (nickname) of output files referring to $fml_nm character sequence used in output climo file names:
275 # fml_nm_XX_YYYYMM_YYYYMM.nc (examples include '' (default), 'control', 'experiment')
276 # By default, fml_nm=$caseid. Use fml_nm instead of $caseid to simplify long names, avoid overlap, etc.
277 # hst_nm: History volume name referring to the $hst_nm character sequence used in history tape names:
278 # caseid.mdl_nm.hst_nm.YYYY-MM.nc (examples include 'h0' (default, works for cam, clm), 'h1', 'h' (for cism))
279 # mdl_nm: Model name referring to the character sequence $mdl_nm used in history tape names:
280 # caseid.mdl_nm.h0.YYYY-MM.nc (examples include 'cam' (default), 'clm2', 'cam2', 'cism', 'pop')
281 # nco_opt: String of options to pass-through to NCO, e.g.,
282 # '-D 2 -7 -L 1' for NCO debugging level 2, netCDF4-classic output, compression level 1
283 # '--no_tmp_fl -x' to skip temporary files, turn extraction into exclusion list
284 # rgr_opt: String of options (besides thread-number) to pass-through exclusively to NCO regridder, e.g.,
285 # ncsplit -m clm2 ... -R col_nm=lndgrid -r map.nc ...
286 # thr_nbr: Thread number to use in NCO regridder, '-t 1' for one thread, '-t 2' for two threads...
287
288 # Set NCO version and directory
289 nco_exe=`which ncks`
290 if [ -z "${nco_exe}" ]; then
291 echo "ERROR: Unable to find NCO, nco_exe = ${nco_exe}"
292 exit 1
293 fi # !nco_exe
294 # Use StackOverflow method to find NCO directory
295 while [ -h "${nco_exe}" ]; do
296 drc_nco="$( cd -P "$( dirname "${nco_exe}" )" && pwd )"
297 nco_exe="$(readlink "${nco_exe}")"
298 [[ ${nco_exe} != /* ]] && nco_exe="${drc_nco}/${nco_exe}"
299 done
300 drc_nco="$( cd -P "$( dirname "${nco_exe}" )" && pwd )"
301 nco_vrs=$(ncks --version 2>&1 >/dev/null | grep NCO | awk '{print $5}')
302
303 # When running in a terminal window (not in an non-interactive batch queue)...
304 if [ -n "${TERM}" ]; then
305 # Set fonts for legibility
306 fnt_nrm=`tput sgr0` # Normal
307 fnt_bld=`tput bold` # Bold
308 fnt_rvr=`tput smso` # Reverse
309 fi # !TERM
310
311 # Defaults for command-line options and some derived variables
312 # Modify these defaults to save typing later
313 bnd_nm='nbnd' # [sng] Bounds dimension name (e.g., 'nbnd', 'tbnd')
314 bnr_flg='No' # [sng] Binary method
315 clm_md='scd' # [sng] Timeseries mode ('scd' or 'sdd' as per above)
316 caseid='' # [sng] Case ID
317 caseid_xmp='famipc5_ne30_v0.3_00003' # [sng] Case ID for examples
318 cf_flg='Yes' # [sng] Produce CF timeseries attribute?
319 lnk_flg='Yes' # [sng] Link ACME-climo to AMWG-climo filenames
320 dbg_lvl=0 # [nbr] Debugging level
321 drc_in='' # [sng] Input file directory
322 drc_in_xmp="${DATA}/ne30/raw" # [sng] Input file directory for examples
323 drc_in_mps="${DATA}/mpas/raw" # [sng] Input file directory for MPAS examples
324 drc_out='' # [sng] Output file directory
325 drc_out_xmp="${DATA}/ne30/clm" # [sng] Output file directory for examples
326 drc_out_mps="${DATA}/mpas/clm" # [sng] Output file directory for MPAS examples
327 drc_prv='' # [sng] Directory containing previous climatology to extend with current data
328 drc_rgr='' # [sng] Regridded file directory
329 drc_rgr_prv='' # [sng] Regridded file directory for previous climatology
330 drc_rgr_xmp="${DATA}/ne30/rgr" # [sng] Regrid file directory for examples
331 drc_rgr_xtn='' # [sng] Regridded file directory for for extended climatology
332 drc_xtn='' # [sng] Directory containing extended climatology
333 fml_nm='' # [sng] Family name (i.e., nickname, e.g., 'amip', 'control', 'experiment')
334 gaa_sng_std="--gaa climo_script=${spt_nm} --gaa climo_hostname=${HOSTNAME} --gaa climo_version=${nco_vrs}" # [sng] Global attributes to add
335 hdr_pad='1000' # [B] Pad at end of header section
336 hst_nm='h0' # [sng] History volume (e.g., 'h0', 'h1', 'h')
337 mdl_nm='cam' # [sng] Model name (e.g., 'cam', 'cam2', 'cice', 'cism', 'clm', 'clm2', 'ocn')
338 mdl_typ='cesm' # [sng] Model type ('cesm', 'mpas') (for filenames and regridding)
339 mpi_flg='No' # [sng] Parallelize over nodes
340 nco_opt='--no_tmp_fl' # [sng] NCO options (e.g., '-7 -D 1 -L 1')
341 ncr_flg='No' # [sng] Incremental method
342 nd_nbr=1 # [nbr] Number of nodes
343 par_opt='' # [sng] Parallel options to shell
344 par_typ='bck' # [sng] Parallelism type
345 rgr_map='' # [sng] Regridding map
346 #rgr_map="${DATA}/maps/map_ne30np4_to_fv129x256_aave.20150901.nc"
347 #rgr_map="${DATA}/maps/map_ne30np4_to_fv257x512_bilin.20150901.nc"
348 #rgr_map="${DATA}/maps/map_ne120np4_to_fv257x512_aave.20150901.nc"
349 #rgr_map="${DATA}/maps/map_ne120np4_to_fv801x1600_bilin.20150901.nc"
350 rgr_opt='' # [sng] Regridding options (e.g., '--rgr col_nm=lndgrid', '--rgr col_nm=nCells')
351 thr_nbr=2 # [nbr] Thread number for regridder
352 #var_lst='FSNT,AODVIS' # [sng] Variables to process (empty means all)
353 var_lst='' # [sng] Variables to process (empty means all)
354 xtn_flg='No' # [sng] Produce extended climatology
355 yr_end='1983' # [yr] End year
356 yr_srt='1980' # [yr] Start year
357
358 function fnc_usg_prn { # NB: dash supports fnc_nm (){} syntax, not function fnc_nm{} syntax
359 # Print usage
360 printf "\nQuick documentation for ${fnt_bld}${spt_nm}${fnt_nrm} (read script for more thorough explanations)\n\n"
361 printf "${fnt_rvr}Basic usage:${fnt_nrm} ${fnt_bld}$spt_nm -c caseid -s yr_srt -e yr_end -i drc_in -o drc_out -r rgr_map${fnt_nrm}\n\n"
362 echo "Command-line options:"
363 echo "${fnt_rvr}-a${fnt_nrm} ${fnt_bld}clm_md${fnt_nrm} Annual climatology mode (default ${fnt_bld}${clm_md}${fnt_nrm})"
364 echo "${fnt_rvr}-b${fnt_nrm} ${fnt_bld}bnd_nm${fnt_nrm} Bounds dimension name (default ${fnt_bld}${bnd_nm}${fnt_nrm})"
365 echo "${fnt_rvr}-c${fnt_nrm} ${fnt_bld}caseid${fnt_nrm} Case ID string (default ${fnt_bld}${caseid}${fnt_nrm})"
366 echo "${fnt_rvr}-d${fnt_nrm} ${fnt_bld}dbg_lvl${fnt_nrm} Debug level (default ${fnt_bld}${dbg_lvl}${fnt_nrm})"
367 echo "${fnt_rvr}-E${fnt_nrm} ${fnt_bld}yr_prv${fnt_nrm} Start year previous climo (empty means none) (default ${fnt_bld}${yr_end_prv}${fnt_nrm})"
368 echo "${fnt_rvr}-e${fnt_nrm} ${fnt_bld}yr_end${fnt_nrm} End year (default ${fnt_bld}${yr_end}${fnt_nrm})"
369 echo "${fnt_rvr}-f${fnt_nrm} ${fnt_bld}fml_nm${fnt_nrm} Family name (nickname) (empty means none) (default ${fnt_bld}${fml_nm}${fnt_nrm})"
370 echo "${fnt_rvr}-h${fnt_nrm} ${fnt_bld}hst_nm${fnt_nrm} History volume name (default ${fnt_bld}${hst_nm}${fnt_nrm})"
371 echo "${fnt_rvr}-i${fnt_nrm} ${fnt_bld}drc_in${fnt_nrm} Input directory (default ${fnt_bld}${drc_in}${fnt_nrm})"
372 echo "${fnt_rvr}-l${fnt_nrm} ${fnt_bld}lnk_flg${fnt_nrm} Link ACME-climo to AMWG-climo filenames (default ${fnt_bld}${lnk_flg}${fnt_nrm})"
373 echo "${fnt_rvr}-m${fnt_nrm} ${fnt_bld}mdl_nm${fnt_nrm} Model name (default ${fnt_bld}${mdl_nm}${fnt_nrm})"
374 echo "${fnt_rvr}-n${fnt_nrm} ${fnt_bld}nco_opt${fnt_nrm} NCO options (empty means none) (default ${fnt_bld}${nco_opt}${fnt_nrm})"
375 echo "${fnt_rvr}-O${fnt_nrm} ${fnt_bld}drc_rgr${fnt_nrm} Regridded directory (default ${fnt_bld}${drc_rgr}${fnt_nrm})"
376 echo "${fnt_rvr}-o${fnt_nrm} ${fnt_bld}drc_out${fnt_nrm} Output directory (default ${fnt_bld}${drc_out}${fnt_nrm})"
377 echo "${fnt_rvr}-p${fnt_nrm} ${fnt_bld}par_typ${fnt_nrm} Parallelism type (default ${fnt_bld}${par_typ}${fnt_nrm})"
378 echo "${fnt_rvr}-r${fnt_nrm} ${fnt_bld}rgr_map${fnt_nrm} Regrid map (empty means none) (default ${fnt_bld}${rgr_map}${fnt_nrm})"
379 echo "${fnt_rvr}-R${fnt_nrm} ${fnt_bld}rgr_opt${fnt_nrm} Regrid options (empty means none) (default ${fnt_bld}${rgr_opt}${fnt_nrm})"
380 echo "${fnt_rvr}-t${fnt_nrm} ${fnt_bld}thr_nbr${fnt_nrm} Thread number for regridder (default ${fnt_bld}${thr_nbr}${fnt_nrm})"
381 echo "${fnt_rvr}-s${fnt_nrm} ${fnt_bld}yr_srt${fnt_nrm} Start year (default ${fnt_bld}${yr_srt}${fnt_nrm})"
382 echo "${fnt_rvr}-S${fnt_nrm} ${fnt_bld}yr_prv${fnt_nrm} Start year previous climo (empty means none) (default ${fnt_bld}${yr_srt_prv}${fnt_nrm})"
383 echo "${fnt_rvr}-v${fnt_nrm} ${fnt_bld}var_lst${fnt_nrm} Variable list (empty means all) (default ${fnt_bld}${var_lst}${fnt_nrm})"
384 echo "${fnt_rvr}-X${fnt_nrm} ${fnt_bld}drc_xtn${fnt_nrm} Extended climo directory (default ${fnt_bld}${drcf_xtn}${fnt_nrm})"
385 echo "${fnt_rvr}-x${fnt_nrm} ${fnt_bld}drc_prv${fnt_nrm} Previous climo directory (default ${fnt_bld}${drc_prv}${fnt_nrm})"
386 echo "${fnt_rvr}-Y${fnt_nrm} ${fnt_bld}rgr_xtn${fnt_nrm} Regridded extended climo directory (default ${fnt_bld}${drc_rgr_xtn}${fnt_nrm})"
387 echo "${fnt_rvr}-y${fnt_nrm} ${fnt_bld}rgr_prv${fnt_nrm} Regridded previous climo directory (default ${fnt_bld}${drc_rgr_prv}${fnt_nrm})"
388 printf "\n"
389 printf "Examples: ${fnt_bld}$spt_nm -c ${caseid_xmp} -s ${yr_srt} -e ${yr_end} -i ${drc_in_xmp} -o ${drc_out_xmp} ${fnt_nrm}\n"
390 printf " ${fnt_bld}$spt_nm -c ${caseid_xmp} -s ${yr_srt} -e ${yr_end} -i ${drc_in_xmp} -o ${drc_out_xmp} -r ~zender/data/maps/map_ne30np4_to_fv129x256_aave.20150901.nc ${fnt_nrm}\n"
391 printf " ${fnt_bld}$spt_nm -c control -m clm2 -s ${yr_srt} -e ${yr_end} -i ${drc_in_xmp} -o ${drc_out_xmp} -r ~zender/data/maps/map_ne30np4_to_fv129x256_aave.20150901.nc ${fnt_nrm}\n"
392 printf " ${fnt_bld}$spt_nm -c hist -m ice -s ${yr_srt} -e ${yr_end} -i ${drc_in_mps} -o ${drc_out_mps} -r ~zender/data/maps/map_oEC60to30_to_t62_bilin.20160301.nc ${fnt_nrm}\n"
393 printf " ${fnt_bld}$spt_nm -c hist -m ocn -p mpi -s 1 -e 5 -i ${drc_in_mps} -o ${drc_out_mps} -r ~zender/data/maps/map_oEC60to30_to_t62_bilin.20160301.nc ${fnt_nrm}\n\n"
394 printf "Interactive batch queues on ...\n"
395 printf "cooley: qsub -I -A HiRes_EarthSys --nodecount=1 --time=00:30:00 --jobname=ncsplit\n"
396 printf "cori : salloc -A acme --nodes=1 --time=00:30:00 --partition=debug --job-name=ncsplit\n"
397 printf "edison: salloc -A acme --nodes=1 --time=00:30:00 --partition=debug --job-name=ncsplit\n"
398 printf "rhea : qsub -I -A CLI115 -V -l nodes=1 -l walltime=00:30:00 -N ncsplit\n"
399 printf "rhea : qsub -I -A CLI115 -V -l nodes=1 -l walltime=00:30:00 -lpartition=gpu -N ncsplit # Bigmem\n\n"
400 # echo "3-yrs ne30: ncsplit -c famipc5_ne30_v0.3_00003 -s 1980 -e 1982 -i /lustre/atlas1/cli115/world-shared/mbranst/famipc5_ne30_v0.3_00003-wget-test -o ${DATA}/ne30/clm -r ~zender/data/maps/map_ne30np4_to_fv129x256_aave.20150901.nc > ~/ncsplit.out 2>&1 &"
401 # printf "3-yrs ne120: ncsplit -p mpi -c famipc5_ne120_v0.3_00003 -s 1980 -e 1982 -i /lustre/atlas1/cli115/world-shared/mbranst/famipc5_ne120_v0.3_00003-wget-test -o ${DATA}/ne120/clm -r ~zender/data/maps/map_ne120np4_to_fv257x512_aave.20150901.nc > ~/ncsplit.out 2>&1 &\n\n"
402 exit 1
403 } # end fnc_usg_prn()
404
405 function trim_leading_zeros {
406 # Purpose: Trim leading zeros from string representing an integer
407 # Why, you ask? Because Bash treats zero-padded integers as octal!
408 # This is surprisingly hard to workaround
409 # My workaround is to remove leading zeros prior to arithmetic
410 # Usage: trim_leading zeros ${sng}
411 sng_trm=${1} # [sng] Trimmed string
412 # Use Bash 2.X pattern matching to remove up to three leading zeros, one at a time
413 sng_trm=${sng_trm##0} # NeR98 p. 99
414 sng_trm=${sng_trm##0}
415 sng_trm=${sng_trm##0}
416 # If all zeros removed, replace with single zero
417 if [ ${sng_trm} = '' ]; then
418 sng_trm='0'
419 fi # endif
420 } # end trim_leading_zeros()
421
422 get_spt_drc () {
423 # SMB (20150814):
424 # Get calling script location to call other utilities in the PreAndPostProcessingScripts package
425 # Resolve symlinks in case script is linked elsewhere with technique from
426 # http://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script
427 spt_src="${BASH_SOURCE[0]}"
428 # If ${spt_src} is a symlink, resolve it
429 while [ -h "${spt_src}" ]; do
430 spt_drc="$(cd -P "$(dirname "${spt_src}")" && pwd)"
431 spt_src="$(readlink "${spt_src}")"
432 # Resolve relative symlinks (no initial "/") against symlink base directory
433 [[ ${spt_src} != /* ]] && spt_src="${spt_drc}/${spt_src}"
434 done
435 spt_drc="$(cd -P "$(dirname "${spt_src}")" && pwd)"
436 echo ${spt_drc}
437 } # end get_spt_drc()
438
439 # Check argument number and complain accordingly
440 arg_nbr=$#
441 #printf "\ndbg: Number of arguments: ${arg_nbr}"
442 if [ ${arg_nbr} -eq 0 ]; then
443 fnc_usg_prn
444 fi # !arg_nbr
445
446 # Parse command-line options:
447 # http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options
448 # http://tuxtweaks.com/2014/05/bash-getopts
449 cmd_ln="${spt_nm} ${@}"
450 while getopts :a:b:c:d:E:e:f:h:i:l:m:n:O:o:p:R:r:S:s:t:v:X:x:Y:y: OPT; do
451 case ${OPT} in
452 a) clm_md=${OPTARG} ;; # Climatology mode
453 b) bnd_nm=${OPTARG} ;; # Bounds dimension name
454 c) caseid=${OPTARG} ;; # CASEID
455 d) dbg_lvl=${OPTARG} ;; # Debugging level
456 E) yr_end_prv=${OPTARG} ;; # End year previous
457 e) yr_end=${OPTARG} ;; # End year
458 f) fml_nm=${OPTARG} ;; # Family name
459 h) hst_nm=${OPTARG} ;; # History tape name
460 i) drc_in=${OPTARG} ;; # Input directory
461 l) lnk_flg=${OPTARG} ;; # Link ACME to AMWG name
462 m) mdl_nm=${OPTARG} ;; # Model name
463 n) nco_opt=${OPTARG} ;; # NCO options
464 o) drc_out_usr=${OPTARG} ;; # Output directory
465 O) drc_rgr_usr=${OPTARG} ;; # Regridded directory
466 p) par_typ=${OPTARG} ;; # Parallelism type
467 R) rgr_opt=${OPTARG} ;; # Regridding options
468 r) rgr_map=${OPTARG} ;; # Regridding map
469 S) yr_srt_prv=${OPTARG} ;; # Start year previous
470 s) yr_srt=${OPTARG} ;; # Start year
471 t) thr_usr=${OPTARG} ;; # Thread number
472 v) var_lst=${OPTARG} ;; # Variables
473 X) drc_xtn=${OPTARG} ;; # Extended climo directory
474 x) drc_prv=${OPTARG} ;; # Previous climo directory
475 Y) drc_rgr_xtn=${OPTARG} ;; # Regridded extended climo directory
476 y) drc_rgr_prv=${OPTARG} ;; # Regridded previous climo directory
477 \?) # Unrecognized option
478 printf "\nERROR: Option ${fnt_bld}-$OPTARG${fnt_nrm} not allowed"
479 fnc_usg_prn ;;
480 esac
481 done
482 shift $((OPTIND-1)) # Advance one argument
483
484 # Derived variables
485 if [ -n "${drc_out_usr}" ]; then
486 # Fancy %/ syntax removes trailing slash (e.g., from $TMPDIR)
487 drc_out="${drc_out_usr%/}"
488 fi # !drc_out_usr
489 if [ -n "${drc_rgr_usr}" ]; then
490 drc_rgr="${drc_rgr_usr%/}"
491 else
492 drc_rgr="${drc_out%/}"
493 fi # !drc_rgr_usr
494 if [ -n "${drc_prv}" ]; then
495 drc_prv="${drc_prv%/}"
496 else
497 drc_prv="${drc_out}"
498 fi # !drc_prv
499 if [ -n "${drc_xtn}" ]; then
500 drc_xtn="${drc_xtn%/}"
501 else
502 drc_xtn="${drc_prv}"
503 fi # !drc_xtn
504
505 # Doubly-derived variables
506 if [ -n "${drc_rgr_prv}" ]; then
507 drc_rgr_prv="${drc_rgr_prv%/}"
508 else
509 drc_rgr_prv="${drc_prv%/}"
510 fi # !drc_rgr_prv
511 if [ -n "${drc_rgr_xtn}" ]; then
512 drc_rgr_xtn="${drc_rgr_xtn%/}"
513 else
514 drc_rgr_xtn="${drc_xtn%/}"
515 fi # !drc_rgr_xtn
516
517 # Determine first full year
518 trim_leading_zeros ${yr_srt}
519 yr_srt_rth=${sng_trm}
520 yyyy_srt=`printf "%04d" ${yr_srt_rth}`
521 let yr_srtm1=${yr_srt_rth}-1
522 trim_leading_zeros ${yr_end}
523 yr_end_rth=${sng_trm}
524 yyyy_end=`printf "%04d" ${yr_end_rth}`
525 let yr_endm1=${yr_end_rth}-1
526 let yr_nbr=${yr_end_rth}-${yr_srt_rth}+1
527
528 if [ -n "${yr_srt_prv}" ]; then
529 # Specifying only yr_srt_prv implies incremental method
530 # Specifying both yr_srt_prv and yr_end_prv implies binary method
531 xtn_flg='Yes'
532 if [ -n "${yr_end_prv}" ]; then
533 bnr_flg='Yes'
534 else # !yr_end_prv binary method
535 ncr_flg='Yes'
536 fi # !yr_end_prv binary method
537 fi # !yr_srt_prv extended climo
538
539 # Derived variables
540 out_nm=${caseid}
541 if [ "${caseid}" = 'hist' ]; then
542 mdl_typ='mpas'
543 fi # !caseid
544 if [ "${mdl_typ}" = 'mpas' ]; then
545 out_nm="mpas_${mdl_nm}"
546 fi # !mdl_typ
547 if [ -n "${fml_nm}" ]; then
548 out_nm="${fml_nm}"
549 fi # !fml_nm
550 if [ "${mdl_nm}" = 'cam2' ]; then
551 bnd_nm='tbnd'
552 fi # !caseid
553 # http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
554 # http://stackoverflow.com/questions/17420994/bash-regex-match-string
555 if [[ "${caseid}" =~ ^(.*)([0-9][0-9][0-9][0-9][01][0-9].nc.?)$ ]]; then
556 mdl_typ='yyyymm'
557 bs_nm="${BASH_REMATCH[1]}"
558 bs_nm="$(basename ${bs_nm})"
559 bs_nm="${bs_nm%.*}"
560 bs_nm="${bs_nm%_*}"
561 out_nm=${bs_nm}
562 bs_sfx="${caseid#*.}"
563 fi # !caseid
564
565 if [ -n "${gaa_sng_std}" ]; then
566 if [ "${yr_nbr}" -gt 1 ] ; then
567 yrs_avg_sng="${yr_srt}-${yr_end}"
568 else
569 yrs_avg_sng="${yr_srt}"
570 fi # !yr_nbr
571 gaa_sng="${gaa_sng_std} --gaa yrs_averaged=${yrs_avg_sng}"
572 nco_opt="${nco_opt} ${gaa_sng}"
573 fi # !var_lst
574 if [ -n "${var_lst}" ]; then
575 nco_opt="${nco_opt} -v ${var_lst}"
576 fi # !var_lst
577 if [ -n "${hdr_pad}" ]; then
578 nco_opt="${nco_opt} --hdr_pad=${hdr_pad}"
579 fi # !hdr_pad
580 if [ "${par_typ}" = 'bck' ]; then
581 par_opt=' &'
582 par_opt_cf=''
583 elif [ "${par_typ}" = 'mpi' ]; then
584 mpi_flg='Yes'
585 par_opt=' &'
586 par_opt_cf=''
587 if [ -n "${UVCDAT_SETUP_PATH}" ]; then
588 printf "${spt_nm}: UVCDAT has been initialized in the shell running this job, and MPI-mode parallelization of ${spt_nm} is requested. Unfortunately UVCDAT's environment and the MPI-mode of ${spt_nm} do not play well together. The Workflow group is working toward a solution. The current workarounds are 1) do not use MPI-mode when UVCDAT is loaded or 2) do not initialize UVCDAT when invoking MPI-mode.\n"
589 fi # !UVCDAT_SETUP_PATH
590 fi # !par_typ
591 if [ -n "${rgr_map}" ]; then
592 if [ ! -f "${rgr_map}" ]; then
593 echo "ERROR: Unable to find specified regrid map ${rgr_map}"
594 echo "HINT: Supply the full path-name for the regridding map"
595 exit 1
596 fi # ! -f
597 rgr_opt="${rgr_opt} --map ${rgr_map}"
598 fi # !rgr_map
599 if [ -n "${thr_usr}" ]; then
600 thr_nbr="${thr_usr}"
601 fi # !thr_usr
602 yyyy_clm_srt=${yyyy_srt}
603 yyyy_clm_end=${yyyy_end}
604 yyyy_clm_srt_dec=${yyyy_srt}
605 yyyy_clm_end_dec=${yyyy_end}
606 mm_ann_srt='01' # [idx] First month used in annual climatology
607 mm_ann_end='12' # [idx] Last month used in annual climatology
608 mm_djf_srt='01' # [idx] First month used in DJF climatology
609 mm_djf_end='12' # [idx] Last month used in DJF climatology
610 yr_cln=${yr_nbr} # [nbr] Calendar years in climatology
611 if [ ${clm_md} = 'scd' ]; then
612 yyyy_clm_srt_dec=`printf "%04d" ${yr_srtm1}`
613 yyyy_clm_end_dec=`printf "%04d" ${yr_endm1}`
614 mm_ann_srt='12'
615 mm_ann_end='11'
616 mm_djf_srt='12'
617 mm_djf_end='02'
618 let yr_cln=${yr_cln}+1
619 fi # !scd
620
621 if [ "${mpi_flg}" = 'Yes' ]; then
622 if [ -n "${COBALT_NODEFILE}" ]; then
623 nd_fl="${COBALT_NODEFILE}"
624 elif [ -n "${PBS_NODEFILE}" ]; then
625 nd_fl="${PBS_NODEFILE}"
626 elif [ -n "${SLURM_NODELIST}" ]; then
627 # SLURM returns compressed lists (e.g., "nid00[076-078,559-567]")
628 # Convert this to file with uncompressed list (like Cobalt, PBS)
629 # http://www.ceci-hpc.be/slurm_faq.html#Q12
630 nd_fl='ncsplit.slurm_nodelist'
631 nd_lst=`scontrol show hostname ${SLURM_NODELIST}`
632 echo ${nd_lst} > ${nd_fl}
633 else
634 echo "ERROR: MPI job unable to find node list"
635 echo "HINT: ${spt_nm} uses first node list found in \$COBALT_NODEFILE (= \"${COBALT_NODEFILE}\"), \$PBS_NODEFILE (= \"${PBS_NODEFILE}\"), \$SLURM_NODELIST (= \"${SLURM_NODELIST}\")"
636 exit 1
637 fi # !PBS
638 if [ -n "${nd_fl}" ]; then
639 # NB: nodes are 0-based, e.g., [0..11]
640 nd_idx=0
641 for nd in `cat ${nd_fl} | uniq` ; do
642 nd_nm[${nd_idx}]=${nd}
643 let nd_idx=${nd_idx}+1
644 done # !nd
645 nd_nbr=${#nd_nm[@]}
646 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
647 case "${HOSTNAME}" in
648 # 20160502: Remove tasks-per-node limits (ntasks, npernode) so round-robin algorithm can schedule multiple jobs on same node
649 constance* | cori* | edison* | nid* | node* )
650 # 20160502: Non-interactive batch jobs at NERSC return HOSTNAME as nid*, not cori* or edison*
651 # 20160803: Non-interactive batch jobs at PNNL return HOSTNAME as node*, not constance*
652 # NB: NERSC staff says srun automatically assigns to unique nodes even without "-L $node" argument?
653 cmd_mpi[${clm_idx}]="srun --nodelist ${nd_nm[$(((${clm_idx}-1) % ${nd_nbr}))]} --nodes=1" ; ;; # SLURM
654 # cmd_mpi[${clm_idx}]="srun --nodelist ${nd_nm[$(((${clm_idx}-1) % ${nd_nbr}))]} --nodes=1 --ntasks=1" ; ;; # SLURM
655 hopper* )
656 # NB: NERSC migrated from aprun to srun in 201601. Hopper commands will soon be deprecated.
657 cmd_mpi[${clm_idx}]="aprun -L ${nd_nm[$(((${clm_idx}-1) % ${nd_nbr}))]} -n 1" ; ;; # NERSC
658 * )
659 cmd_mpi[${clm_idx}]="mpirun -H ${nd_nm[$(((${clm_idx}-1) % ${nd_nbr}))]} -n 1" ; ;; # Other (Cobalt, PBS)
660 # cmd_mpi[${clm_idx}]="mpirun -H ${nd_nm[$(((${clm_idx}-1) % ${nd_nbr}))]} -npernode 1 -n 1" ; ;; # Other
661 esac # !HOSTNAME
662 done # !clm_idx
663 if [ -n "${SLURM_NODELIST}" ]; then
664 /bin/rm -f ${nd_fl}
665 fi # !SLURM
666 else # !nd_fl
667 mpi_flg='No'
668 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
669 cmd_mpi[${clm_idx}]=''
670 done # !clm_idx
671 fi # !nd_fl
672 if [ -z "${thr_usr}" ]; then
673 if [ -n "${PBS_NUM_PPN}" ]; then
674 # NB: use export OMP_NUM_THREADS when thr_nbr > 8
675 # thr_nbr=${PBS_NUM_PPN}
676 thr_nbr=$((PBS_NUM_PPN > 8 ? 8 : PBS_NUM_PPN))
677 fi # !pbs
678 fi # !thr_usr
679 fi # !mpi
680
681 # Print initial state
682 if [ ${dbg_lvl} -ge 1 ]; then
683 printf "dbg: bnd_nm = ${bnd_nm}\n"
684 printf "dbg: bnr_flg = ${bnr_flg}\n"
685 printf "dbg: caseid = ${caseid}\n"
686 printf "dbg: cf_flg = ${cf_flg}\n"
687 printf "dbg: clm_md = ${clm_md}\n"
688 printf "dbg: dbg_lvl = ${dbg_lvl}\n"
689 printf "dbg: drc_in = ${drc_in}\n"
690 printf "dbg: drc_nco = ${drc_nco}\n"
691 printf "dbg: drc_out = ${drc_out}\n"
692 printf "dbg: drc_prv = ${drc_prv}\n"
693 printf "dbg: drc_pwd = ${drc_pwd}\n"
694 printf "dbg: drc_rgr = ${drc_rgr}\n"
695 printf "dbg: drc_spt = ${drc_spt}\n"
696 printf "dbg: drc_xtn = ${drc_xtn}\n"
697 printf "dbg: fml_nm = ${fml_nm}\n"
698 printf "dbg: gaa_sng = ${gaa_sng}\n"
699 printf "dbg: hdr_pad = ${hdr_pad}\n"
700 printf "dbg: hst_nm = ${hst_nm}\n"
701 printf "dbg: lnk_flg = ${lnk_flg}\n"
702 printf "dbg: mdl_nm = ${mdl_nm}\n"
703 printf "dbg: mpi_flg = ${mpi_flg}\n"
704 printf "dbg: nco_opt = ${nco_opt}\n"
705 printf "dbg: ncr_flg = ${ncr_flg}\n"
706 printf "dbg: nd_nbr = ${nd_nbr}\n"
707 printf "dbg: par_typ = ${par_typ}\n"
708 printf "dbg: rgr_map = ${rgr_map}\n"
709 printf "dbg: rgr_sfx = ${rgr_sfx}\n"
710 printf "dbg: thr_nbr = ${thr_nbr}\n"
711 printf "dbg: var_lst = ${var_lst}\n"
712 printf "dbg: xtn_flg = ${xtn_flg}\n"
713 printf "dbg: yyyy_end = ${yyyy_end}\n"
714 printf "dbg: yyyy_srt = ${yyyy_srt}\n"
715 fi # !dbg
716 if [ ${dbg_lvl} -ge 2 ]; then
717 printf "dbg: yyyy_srt = ${yyyy_srt}\n"
718 printf "dbg: yr_srt_rth = ${yr_srt_rth}\n"
719 printf "dbg: yr_srtm1 = ${yr_srtm1}\n"
720 printf "dbg: yr_endm1 = ${yr_endm1}\n"
721 if [ ${mpi_flg} = 'Yes' ]; then
722 for ((nd_idx=0;nd_idx<${nd_nbr};nd_idx++)); do
723 printf "dbg: nd_nm[${nd_idx}] = ${nd_nm[${nd_idx}]}\n"
724 done # !nd
725 fi # !mpi
726 fi # !dbg
727
728 # Create output directory
729 mkdir -p ${drc_out}
730 mkdir -p ${drc_rgr}
731
732 # Human-readable summary
733 date_srt=$(date +"%s")
734 if [ ${dbg_lvl} -ge 0 ]; then
735 printf "Timeseries generation invoked with command:\n"
736 echo "${cmd_ln}"
737 fi # !dbg
738 printf "Started timeseries generation for dataset ${caseid} at `date`.\n"
739 printf "Running timeseries script ${spt_nm} from directory ${drc_spt}\n"
740 printf "NCO binaries version ${nco_vrs} from directory ${drc_nco}\n"
741 if [ "${xtn_flg}" = 'No' ]; then
742 printf "Producing standard timeseries from raw monthly input files in directory ${drc_in}\n"
743 printf "Output files to directory ${drc_out}\n"
744 fi # !xtn_flg
745 if [ "${bnr_flg}" = 'Yes' ]; then
746 printf "Producing extended timeseries in binary mode: Will combine pre-computed timeseries in directory ${drc_prv} with pre-computed timeseries in directory ${drc_in}\n"
747 printf "Output files to directory ${drc_xtn}\n"
748 fi # !bnr_flg
749 if [ "${ncr_flg}" = 'Yes' ]; then
750 printf "Producing extended timeseries in incremental mode: Pre-computed timeseries in directory ${drc_prv} will be incremented by raw monthly input files in directory ${drc_in}\n"
751 printf "Output files to directory ${drc_xtn}\n"
752 fi # !ncr_flg
753 #printf "Intermediate/temporary files written to directory ${drc_tmp}\n"
754 if [ "${bnr_flg}" = 'No' ]; then
755 printf "Timeseries from ${yr_nbr} years of contiguous raw monthly data touching ${yr_cln} calendar years from YYYYMM = ${yyyy_clm_srt_dec}${mm_ann_srt} to ${yyyy_end}${mm_ann_end}.\n"
756 fi # !bnr_flg
757 if [ "${mdl_typ}" = 'yyyymm' ]; then
758 printf "Filenames will be constructed with generic conventions as ${bs_nm}_YYYYMM.${bs_sfx}\n"
759 else # !mdl_typ
760 printf "Filenames will be constructed with CESM'ish or ACME'ish conventions.\n"
761 fi # !mdl_typ
762 if [ ${clm_md} = 'scd' ]; then
763 printf "Winter statistics based on seasonally contiguous December (scd-mode): DJF sequences are consecutive months that cross calendar-year boundaries.\n"
764 else
765 printf "Winter statistics based on seasonally discontiguous December (sdd-mode): DJF sequences comprise three months from the same calendar year.\n"
766 fi # !scd
767 if [ ${cf_flg} = 'Yes' ]; then
768 printf "Annotation for CF timeseries attribute and timeseries_bounds variable will be performed.\n"
769 else
770 printf "Annotation for CF timeseries attribute and timeseries_bounds variable will not be performed.\n"
771 fi # !cf
772 if [ -n "${rgr_map}" ]; then
773 printf "This timeseries will be regridded.\n"
774 else
775 printf "This timeseries will not be regridded.\n"
776 fi # !rgr
777
778 # Block 1: Climatological monthly means
779 # Block 1 Loop 1: Generate, check, and store (but do not yet execute) monthly commands
780 clm_idx=0
781 for mth in {01..12}; do
782 let clm_idx=${clm_idx}+1
783 MM=`printf "%02d" ${clm_idx}`
784 yr_fl=''
785 for yr in `seq ${yyyy_srt} ${yyyy_end}`; do
786 YYYY=`printf "%04d" ${yr}`
787 if [ ${mdl_typ} = 'cesm' ]; then
788 yr_fl="${yr_fl} ${caseid}.${mdl_nm}.${hst_nm}.${YYYY}-${MM}.nc"
789 elif [ ${mdl_typ} = 'mpas' ]; then # Use MPAS not CESM conventions
790 yr_fl="${yr_fl} ${caseid}.${mdl_nm}.${YYYY}-${MM}-01_00.00.00.nc"
791 elif [ ${mdl_typ} = 'yyyymm' ]; then # Generate from caseid + YYYYMM
792 yr_fl="${yr_fl} ${bs_nm}_${YYYY}${MM}.${bs_sfx}"
793 fi # !cesm
794 done # !yr
795 if [ ${clm_md} = 'scd' ] && [ ${MM} = '12' ]; then
796 yr_fl=''
797 for yr in `seq ${yr_srtm1} ${yr_endm1}`; do
798 YYYY=`printf "%04d" ${yr}`
799 if [ ${mdl_typ} = 'cesm' ]; then
800 yr_fl="${yr_fl} ${caseid}.${mdl_nm}.${hst_nm}.${YYYY}-${MM}.nc"
801 elif [ ${mdl_typ} = 'mpas' ]; then # Use MPAS not CESM conventions
802 yr_fl="${yr_fl} ${caseid}.${mdl_nm}.${YYYY}-${MM}-01_00.00.00.nc"
803 elif [ ${mdl_typ} = 'yyyymm' ]; then # Generate from caseid + YYYYMM
804 yr_fl="${yr_fl} ${bs_nm}_${YYYY}${MM}.${bs_sfx}"
805 fi # !cesm
806 done # !yr
807 yyyy_clm_srt=${yyyy_clm_srt_dec}
808 yyyy_clm_end=${yyyy_clm_end_dec}
809 fi # !scd
810 # Check for raw monthly file existence only if file will be used
811 if [ "${bnr_flg}" = 'No' ]; then
812 for fl_in in ${yr_fl} ; do
813 if [ ! -f "${drc_in}/${fl_in}" ]; then
814 echo "ERROR: Unable to find required input file ${drc_in}/${fl_in}"
815 echo "HINT: All files implied to exist by the timeseries bounds (start/end year/month) must be in ${drc_in} before ${spt_nm} will proceed"
816 exit 1
817 fi # ! -f
818 done # !fl_in
819 else # !bnr_flg
820 # In binary mode drc_out is actually used to locate input files from timeseries B (same as output files in incremental mode)
821 drc_out="${drc_in}"
822 fi # !bnr_flg
823 fl_out[${clm_idx}]="${drc_out}/${out_nm}_${MM}_${yyyy_clm_srt}${MM}_${yyyy_clm_end}${MM}_climo.nc"
824 cmd_clm[${clm_idx}]="${cmd_mpi[${clm_idx}]} ncra --cb -O ${nco_opt} -p ${drc_in} ${yr_fl} ${fl_out[${clm_idx}]}"
825 done # !mth
826
827 # Monthly output filenames constructed above; specify remaining (seasonal, annual) output names
828 fl_out[13]="${drc_out}/${out_nm}_MAM_${yyyy_srt}03_${yyyy_end}05_climo.nc"
829 fl_out[14]="${drc_out}/${out_nm}_JJA_${yyyy_srt}06_${yyyy_end}08_climo.nc"
830 fl_out[15]="${drc_out}/${out_nm}_SON_${yyyy_srt}09_${yyyy_end}11_climo.nc"
831 fl_out[16]="${drc_out}/${out_nm}_DJF_${yyyy_clm_srt_dec}${mm_djf_srt}_${yyyy_end}${mm_djf_end}_climo.nc"
832 fl_out[17]="${drc_out}/${out_nm}_ANN_${yyyy_clm_srt_dec}${mm_ann_srt}_${yyyy_end}${mm_ann_end}_climo.nc"
833 # Derive all seventeen regridded and AMWG names from output names
834 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
835 fl_amwg[${clm_idx}]=`expr match "${fl_out[${clm_idx}]}" '\(.*\)_.*_.*_climo.nc'` # Prune _YYYYYMM_YYYYMM_climo.nc
836 fl_amwg[${clm_idx}]="${fl_amwg[${clm_idx}]}_climo.nc" # Replace with _climo.nc
837 fl_amwg[${clm_idx}]="${fl_amwg[${clm_idx}]/${drc_out}\//}" # Delete prepended path to ease symlinking
838 if [ -n "${rgr_map}" ]; then
839 fl_rgr[${clm_idx}]="${fl_out[${clm_idx}]/${drc_out}/${drc_rgr}}"
840 if [ "${drc_out}" = "${drc_rgr}" ]; then
841 # Append geometry suffix to regridded files in same directory as native climo
842 # http://tldp.org/LDP/abs/html/string-manipulation.html
843 dfl_sfx='rgr'
844 rgr_sfx=`expr match "${rgr_map}" '.*_to_\(.*\).nc'`
845 if [ "${#rgr_sfx}" -eq 0 ]; then
846 printf "${spt_nm}: WARNING Unable to extract geometric suffix from mapfile, will suffix regridded files with \"${dfl_sfx}\" instead\n"
847 rgr_sfx=${dfl_sfx}
848 else
849 yyyymmdd_sng=`expr match "${rgr_sfx}" '.*\(\.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\)'` # Find YYYYYMMDD
850 if [ "${#yyyymmdd_sng}" -ne 0 ]; then
851 rgr_sfx=${rgr_sfx%%${yyyymmdd_sng}} # Delete YYYYYMMDD
852 fi # !strlen
853 fi # !strlen
854 # rgr_sfx=`expr match "${rgr_sfx}" '\(.*\)\.[0-9][0-9][0-9][0-9][0-9][0-9]'` #
855 fl_rgr[${clm_idx}]="${fl_rgr[${clm_idx}]/.nc/_${rgr_sfx}.nc}"
856 fi # !drc_rgr
857 fi # !rgr_map
858 done # !clm_idx
859
860 # Many subsequent blocks only executed for normal and incremental climos, not for binary climos
861 if [ "${bnr_flg}" = 'No' ]; then
862
863 # Block 1 Loop 2: Execute and/or echo monthly timeseries commands
864 printf "Generating timeseries...\n"
865 for ((clm_idx=1;clm_idx<=12;clm_idx++)); do
866 printf "Climatological monthly mean for month ${clm_idx} ...\n"
867 if [ ${dbg_lvl} -ge 1 ]; then
868 echo ${cmd_clm[${clm_idx}]}
869 fi # !dbg
870 if [ ${dbg_lvl} -le 1 ]; then
871 if [ -z "${par_opt}" ]; then
872 eval ${cmd_clm[${clm_idx}]}
873 if [ $? -ne 0 ]; then
874 printf "${spt_nm}: ERROR monthly climo cmd_clm[${clm_idx}] failed. Debug this:\n${cmd_clm[${clm_idx}]}\n"
875 exit 1
876 fi # !err
877 else # !par_opt
878 eval ${cmd_clm[${clm_idx}]} ${par_opt} # eval always returns 0 on backgrounded processes
879 clm_pid[${clm_idx}]=$!
880 # Potential alternatives to eval:
881 # eval "${cmd_clm[${clm_idx}]}" # borken
882 # ${cmd_clm[${clm_idx}]} # borken
883 # "${cmd_clm[${clm_idx}]}" # borken
884 # exec "${cmd_clm[${clm_idx}]}" # borken
885 # $(${cmd_clm[${clm_idx}]}) # borken
886 # $("${cmd_clm[${clm_idx}]}") # works (when & inside cmd quotes)
887 fi # !par_opt
888 fi # !dbg
889 done # !clm_idx
890 if [ -n "${par_opt}" ]; then
891 for ((clm_idx=1;clm_idx<=12;clm_idx++)); do
892 wait ${clm_pid[${clm_idx}]}
893 if [ $? -ne 0 ]; then
894 printf "${spt_nm}: ERROR monthly climo cmd_clm[${clm_idx}] failed. Debug this:\n${cmd_clm[${clm_idx}]}\n"
895 exit 1
896 fi # !err
897 done # !clm_idx
898 fi # !par_opt
899 wait
900
901 # Block 1: Loop 4: Regrid first twelve files. Load-balance by using idle nodes (nodes not used for seasonal climatologies).
902 if [ -n "${rgr_map}" ]; then
903 printf "Regrid monthly data...\n"
904 for ((clm_idx=1;clm_idx<=12;clm_idx++)); do
905 # NB: Months, seasons, files are 1-based ([1..12], [13..16], [1..17]), nodes are 0-based ([0..11])
906 let nd_idx=$(((clm_idx-1+4) % nd_nbr))
907 if [ ${nd_idx} -lt 4 ]; then
908 let nd_idx=${nd_idx}+4
909 fi # !nd
910 cmd_rgr[${clm_idx}]="${cmd_mpi[${nd_idx}]} ncks -t ${thr_nbr} -O ${nco_opt} ${rgr_opt} ${fl_out[${clm_idx}]} ${fl_rgr[${clm_idx}]}"
911 if [ "${mdl_typ}" = 'mpas' ]; then
912 cmd_rgr[${clm_idx}]="${cmd_mpi[${nd_idx}]} ncremap -C -u .pid${spt_pid}.climo.${clm_idx}.tmp -P mpas -t ${thr_nbr} -m ${rgr_map} -i ${fl_out[${clm_idx}]} -o ${fl_rgr[${clm_idx}]}"
913 fi # !mdl_typ
914 if [ ${dbg_lvl} -ge 1 ]; then
915 echo ${cmd_rgr[${clm_idx}]}
916 fi # !dbg
917 if [ ${dbg_lvl} -le 1 ]; then
918 if [ -z "${par_opt}" ]; then
919 eval ${cmd_rgr[${clm_idx}]}
920 if [ $? -ne 0 ]; then
921 printf "${spt_nm}: ERROR monthly regrid cmd_rgr[${clm_idx}] failed. Debug this:\n${cmd_rgr[${clm_idx}]}\n"
922 exit 1
923 fi # !err
924 else # !par_opt
925 eval ${cmd_rgr[${clm_idx}]} ${par_opt}
926 rgr_pid[${clm_idx}]=$!
927 fi # !par_opt
928 fi # !dbg
929 done
930 # Start seasonal means first, then wait() for monthly regridding to finish
931 fi # !rgr_map
932
933 # Block 2: Climatological seasonal means
934 # Block 2 Loop 1: Generate seasonal commands
935 printf "Climatological seasonal means...\n"
936 cmd_clm[13]="${cmd_mpi[13]} ncra --cb -O -w 31,30,31 ${nco_opt} ${fl_out[3]} ${fl_out[4]} ${fl_out[5]} ${fl_out[13]}"
937 cmd_clm[14]="${cmd_mpi[14]} ncra --cb -O -w 30,31,31 ${nco_opt} ${fl_out[6]} ${fl_out[7]} ${fl_out[8]} ${fl_out[14]}"
938 cmd_clm[15]="${cmd_mpi[15]} ncra --cb -O -w 30,31,30 ${nco_opt} ${fl_out[9]} ${fl_out[10]} ${fl_out[11]} ${fl_out[15]}"
939 cmd_clm[16]="${cmd_mpi[16]} ncra --cb -O -w 31,31,28 ${nco_opt} ${fl_out[12]} ${fl_out[1]} ${fl_out[2]} ${fl_out[16]}"
940
941 # Block 2 Loop 2: Execute and/or echo seasonal timeseries commands
942 for ((clm_idx=13;clm_idx<=16;clm_idx++)); do
943 if [ ${dbg_lvl} -ge 1 ]; then
944 echo ${cmd_clm[${clm_idx}]}
945 fi # !dbg
946 if [ ${dbg_lvl} -le 1 ]; then
947 if [ -z "${par_opt}" ]; then
948 eval ${cmd_clm[${clm_idx}]}
949 if [ $? -ne 0 ]; then
950 printf "${spt_nm}: ERROR seasonal climo cmd_clm[${clm_idx}] failed. Debug this:\n${cmd_clm[${clm_idx}]}\n"
951 exit 1
952 fi # !err
953 else # !par_opt
954 eval ${cmd_clm[${clm_idx}]} ${par_opt}
955 clm_pid[${clm_idx}]=$!
956 fi # !par_opt
957 fi # !dbg
958 done # !clm_idx
959 # wait() for monthly regridding, if any, to finish
960 if [ -n "${rgr_map}" ]; then
961 if [ -n "${par_opt}" ]; then
962 for ((clm_idx=1;clm_idx<=12;clm_idx++)); do
963 wait ${rgr_pid[${clm_idx}]}
964 if [ $? -ne 0 ]; then
965 printf "${spt_nm}: ERROR monthly regrid cmd_rgr[${clm_idx}] failed. Debug this:\n${cmd_rgr[${clm_idx}]}\n"
966 exit 1
967 fi # !err
968 done # !clm_idx
969 fi # !par_opt
970 fi # !rgr_map
971 # wait() for seasonal climatologies to finish
972 if [ -n "${par_opt}" ]; then
973 for ((clm_idx=13;clm_idx<=16;clm_idx++)); do
974 wait ${clm_pid[${clm_idx}]}
975 if [ $? -ne 0 ]; then
976 printf "${spt_nm}: ERROR seasonal climo cmd_clm[${clm_idx}] failed. Debug this:\n${cmd_clm[${clm_idx}]}\n"
977 exit 1
978 fi # !err
979 done # !clm_idx
980 fi # !par_opt
981 wait
982
983 # Block 2: Loop 4: Regrid seasonal files. Load-balance by using idle nodes (nodes not used for annual mean).
984 if [ -n "${rgr_map}" ]; then
985 printf "Regrid seasonal data...\n"
986 for ((clm_idx=13;clm_idx<=16;clm_idx++)); do
987 let nd_idx=$(((clm_idx-1+4) % nd_nbr))
988 if [ ${nd_idx} -lt 4 ]; then
989 let nd_idx=${nd_idx}+4
990 fi # !nd
991 cmd_rgr[${clm_idx}]="${cmd_mpi[${nd_idx}]} ncks -t ${thr_nbr} -O ${nco_opt} ${rgr_opt} ${fl_out[${clm_idx}]} ${fl_rgr[${clm_idx}]}"
992 if [ "${mdl_typ}" = 'mpas' ]; then
993 cmd_rgr[${clm_idx}]="${cmd_mpi[${nd_idx}]} ncremap -C -u .pid${spt_pid}.climo.${clm_idx}.tmp -P mpas -t ${thr_nbr} -m ${rgr_map} -i ${fl_out[${clm_idx}]} -o ${fl_rgr[${clm_idx}]}"
994 fi # !mdl_typ
995 if [ ${dbg_lvl} -ge 1 ]; then
996 echo ${cmd_rgr[${clm_idx}]}
997 fi # !dbg
998 if [ ${dbg_lvl} -le 1 ]; then
999 if [ -z "${par_opt}" ]; then
1000 eval ${cmd_rgr[${clm_idx}]}
1001 if [ $? -ne 0 ]; then
1002 printf "${spt_nm}: ERROR seasonal regrid cmd_rgr[${clm_idx}] failed. Debug this:\n${cmd_rgr[${clm_idx}]}\n"
1003 exit 1
1004 fi # !err
1005 else # !par_opt
1006 eval ${cmd_rgr[${clm_idx}]} ${par_opt}
1007 rgr_pid[${clm_idx}]=$!
1008 fi # !par_opt
1009 fi # !dbg
1010 done
1011 # Start annual mean first, then wait() for seasonal regridding to finish
1012 fi # !rgr_map
1013
1014 # Block 3: Climatological annual mean (seventeenth file)
1015 printf "Climatological annual mean...\n"
1016 cmd_clm[17]="${cmd_mpi[17]} ncra --c2b -O -w 92,92,91,90 ${nco_opt} ${fl_out[13]} ${fl_out[14]} ${fl_out[15]} ${fl_out[16]} ${fl_out[17]}"
1017 if [ ${dbg_lvl} -ge 1 ]; then
1018 echo ${cmd_clm[17]}
1019 fi # !dbg
1020 if [ ${dbg_lvl} -le 1 ]; then
1021 if [ -z "${par_opt}" ]; then
1022 eval ${cmd_clm[17]}
1023 if [ $? -ne 0 ]; then
1024 printf "${spt_nm}: ERROR annual climo cmd_clm[17] failed. Debug this:\n${cmd_clm[17]}\n"
1025 exit 1
1026 fi # !err
1027 else # !par_opt
1028 eval ${cmd_clm[17]} ${par_opt}
1029 clm_pid[17]=$!
1030 fi # !par_opt
1031 fi # !dbg
1032 # wait() for seasonal regridding, if any, to finish
1033 if [ -n "${rgr_map}" ]; then
1034 if [ -n "${par_opt}" ]; then
1035 for ((clm_idx=13;clm_idx<=16;clm_idx++)); do
1036 wait ${rgr_pid[${clm_idx}]}
1037 if [ $? -ne 0 ]; then
1038 printf "${spt_nm}: ERROR seasonal regrid cmd_rgr[${clm_idx}] failed. Debug this:\n${cmd_rgr[${clm_idx}]}\n"
1039 exit 1
1040 fi # !err
1041 done # !clm_idx
1042 fi # !par_opt
1043 fi # !rgr_map
1044 # wait() for annual timeseries to finish
1045 if [ -n "${par_opt}" ]; then
1046 wait ${clm_pid[17]}
1047 if [ $? -ne 0 ]; then
1048 printf "${spt_nm}: ERROR annual climo cmd_clm[17] failed. Debug this:\n${cmd_clm[17]}\n"
1049 exit 1
1050 fi # !err
1051 fi # !par_opt
1052
1053 # Block 5: Regrid climatological annual mean
1054 if [ -n "${rgr_map}" ]; then
1055 printf "Regrid annual data...\n"
1056 for ((clm_idx=17;clm_idx<=17;clm_idx++)); do
1057 cmd_rgr[${clm_idx}]="${cmd_mpi[${clm_idx}]} ncks -t ${thr_nbr} -O ${nco_opt} ${rgr_opt} ${fl_out[${clm_idx}]} ${fl_rgr[${clm_idx}]}"
1058 if [ "${mdl_typ}" = 'mpas' ]; then
1059 cmd_rgr[${clm_idx}]="${cmd_mpi[${clm_idx}]} ncremap -C -u .pid${spt_pid}.climo.${clm_idx}.tmp -P mpas -t ${thr_nbr} -m ${rgr_map} -i ${fl_out[${clm_idx}]} -o ${fl_rgr[${clm_idx}]}"
1060 fi # !mdl_typ
1061 if [ ${dbg_lvl} -ge 1 ]; then
1062 echo ${cmd_rgr[${clm_idx}]}
1063 fi # !dbg
1064 if [ ${dbg_lvl} -le 1 ]; then
1065 # NB: Do not background climatological mean regridding
1066 eval ${cmd_rgr[${clm_idx}]}
1067 if [ $? -ne 0 ]; then
1068 printf "${spt_nm}: ERROR annual regrid cmd_rgr[${clm_idx}] failed. Debug this:\n${cmd_rgr[${clm_idx}]}\n"
1069 exit 1
1070 fi # !err
1071 fi # !dbg
1072 done
1073 fi # !rgr_map
1074
1075 # Link ACME-climo to AMWG-climo filenames
1076 # drc_pwd is always fully qualified path but drc_out and drc_rgr may be relative paths
1077 # Strategy: Start in drc_pwd, cd to drc_rgr, then link so return code comes from ln not cd
1078 if [ ${lnk_flg} = 'Yes' ]; then
1079 printf "Link ACME-climo to AMWG-climo filenames...\n"
1080 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1081 if [ -n "${rgr_map}" ]; then
1082 cmd_lnk[${clm_idx}]="cd ${drc_pwd};cd ${drc_rgr};ln -s -f ${fl_rgr[${clm_idx}]/${drc_rgr}\//} ${fl_amwg[${clm_idx}]/${drc_rgr}\//}"
1083 else
1084 cmd_lnk[${clm_idx}]="cd ${drc_pwd};cd ${drc_out};ln -s -f ${fl_out[${clm_idx}]/${drc_out}\//} ${fl_amwg[${clm_idx}]/${drc_out}\//}"
1085 fi # !rgr_map
1086 if [ ${dbg_lvl} -ge 1 ]; then
1087 echo ${cmd_lnk[${clm_idx}]}
1088 fi # !dbg
1089 if [ ${dbg_lvl} -le 1 ]; then
1090 eval ${cmd_lnk[${clm_idx}]}
1091 if [ $? -ne 0 ]; then
1092 printf "${spt_nm}: ERROR linking ACME to AMWG filename cmd_lnk[${clm_idx}] failed. Debug this:\n${cmd_lnk[${clm_idx}]}\n"
1093 exit 1
1094 fi # !err
1095 fi # !dbg
1096 done # !clm_idx
1097 cd ${drc_pwd}
1098 fi # !lnk_flg
1099 fi # !bnr_flg
1100
1101 # Extended climos
1102 if [ "${xtn_flg}" = 'Yes' ]; then
1103 mkdir -p ${drc_prv}
1104 mkdir -p ${drc_xtn}
1105
1106 trim_leading_zeros ${yr_srt_prv}
1107 yr_srt_rth_prv=${sng_trm}
1108 yyyy_srt_prv=`printf "%04d" ${yr_srt_rth_prv}`
1109 yyyy_clm_srt_dec_prv=${yyyy_srt_prv}
1110 let yr_srtm1_prv=${yr_srt_rth_prv}-1
1111 if [ "${ncr_flg}" = 'Yes' ]; then
1112 let yr_end_prv=${yr_srt_rth}-1
1113 fi # !ncr_flg
1114 trim_leading_zeros ${yr_end_prv}
1115 yr_end_rth_prv=${sng_trm}
1116 yyyy_end_prv=`printf "%04d" ${yr_end_rth_prv}`
1117 let yr_endm1_prv=${yr_end_rth_prv}-1
1118 let yr_nbr_prv=${yr_end_rth_prv}-${yr_srt_rth_prv}+1
1119 let yr_nbr_xtn=${yr_nbr_prv}+${yr_nbr}
1120
1121 wgt_prv=$(echo "${yr_nbr_prv}/${yr_nbr_xtn}" | bc -l)
1122 wgt_crr=$(echo "${yr_nbr}/${yr_nbr_xtn}" | bc -l)
1123 if [ "${bnr_flg}" = 'Yes' ]; then
1124 printf "Produce extended timeseries as weighted average of two previously computed climatologies:\n"
1125 else # !bnr_flg
1126 printf "Produce extended timeseries as weighted average of previously computed and incremental/new climatologies:\n"
1127 fi # !bnr_flg
1128 printf "Previous/first timeseries is ${yr_nbr_prv} years from ${yyyy_clm_srt_dec_prv}${mm_ann_srt} to ${yyyy_end_prv}${mm_ann_end}, weight = ${wgt_prv}\n"
1129 printf "Current/second timeseries is ${yr_nbr} years from ${yyyy_clm_srt_dec}${mm_ann_srt} to ${yyyy_end}${mm_ann_end}, weight = ${wgt_crr}\n"
1130 printf "Extended timeseries is ${yr_nbr_xtn} years from ${yyyy_clm_srt_dec_prv}${mm_ann_srt} to ${yyyy_end}${mm_ann_end}\n"
1131
1132 # Replace yr_srt by yr_srt_prv in "yrs_averaged" attribute
1133 nco_opt="${nco_opt/${yr_srt}-/${yr_srt_prv}-}"
1134
1135 clm_idx=0
1136 for mth in {01..12}; do
1137 let clm_idx=${clm_idx}+1
1138 MM=`printf "%02d" ${clm_idx}`
1139 fl_prv[${clm_idx}]="${drc_prv}/${out_nm}_${MM}_${yyyy_srt_prv}${MM}_${yyyy_end_prv}${MM}_climo.nc"
1140 fl_xtn[${clm_idx}]="${drc_xtn}/${out_nm}_${MM}_${yyyy_srt_prv}${MM}_${yyyy_end}${MM}_climo.nc"
1141 done # !mth
1142 if [ ${clm_md} = 'scd' ]; then
1143 yyyy_clm_srt_dec_prv=`printf "%04d" ${yr_srtm1_prv}`
1144 yyyy_clm_end_dec_prv=`printf "%04d" ${yr_endm1_prv}`
1145 clm_idx=12
1146 MM=`printf "%02d" ${clm_idx}`
1147 fl_prv[${clm_idx}]="${drc_prv}/${out_nm}_${MM}_${yyyy_clm_srt_dec_prv}${MM}_${yyyy_clm_end_dec_prv}${MM}_climo.nc"
1148 fl_xtn[${clm_idx}]="${drc_xtn}/${out_nm}_${MM}_${yyyy_clm_srt_dec_prv}${MM}_${yyyy_clm_end_dec}${MM}_climo.nc"
1149 fi # !scd
1150
1151 fl_prv[13]="${drc_prv}/${out_nm}_MAM_${yyyy_srt_prv}03_${yyyy_end_prv}05_climo.nc"
1152 fl_prv[14]="${drc_prv}/${out_nm}_JJA_${yyyy_srt_prv}06_${yyyy_end_prv}08_climo.nc"
1153 fl_prv[15]="${drc_prv}/${out_nm}_SON_${yyyy_srt_prv}09_${yyyy_end_prv}11_climo.nc"
1154 fl_prv[16]="${drc_prv}/${out_nm}_DJF_${yyyy_clm_srt_dec_prv}${mm_djf_srt}_${yyyy_end_prv}${mm_djf_end}_climo.nc"
1155 fl_prv[17]="${drc_prv}/${out_nm}_ANN_${yyyy_clm_srt_dec_prv}${mm_ann_srt}_${yyyy_end_prv}${mm_ann_end}_climo.nc"
1156
1157 fl_xtn[13]="${drc_xtn}/${out_nm}_MAM_${yyyy_srt_prv}03_${yyyy_end}05_climo.nc"
1158 fl_xtn[14]="${drc_xtn}/${out_nm}_JJA_${yyyy_srt_prv}06_${yyyy_end}08_climo.nc"
1159 fl_xtn[15]="${drc_xtn}/${out_nm}_SON_${yyyy_srt_prv}09_${yyyy_end}11_climo.nc"
1160 fl_xtn[16]="${drc_xtn}/${out_nm}_DJF_${yyyy_clm_srt_dec_prv}${mm_djf_srt}_${yyyy_end}${mm_djf_end}_climo.nc"
1161 fl_xtn[17]="${drc_xtn}/${out_nm}_ANN_${yyyy_clm_srt_dec_prv}${mm_ann_srt}_${yyyy_end}${mm_ann_end}_climo.nc"
1162
1163 # Derive all seventeen regridded and AMWG names from output names
1164 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1165 fl_rgr_prv[${clm_idx}]="${fl_rgr[${clm_idx}]/${drc_rgr}/${drc_rgr_prv}}"
1166 fl_rgr_prv[${clm_idx}]="${fl_rgr_prv[${clm_idx}]/_${yyyy_srt}/_${yyyy_srt_prv}}"
1167 fl_rgr_prv[${clm_idx}]="${fl_rgr_prv[${clm_idx}]/_${yyyy_end}/_${yyyy_end_prv}}"
1168
1169 fl_rgr_xtn[${clm_idx}]="${fl_rgr[${clm_idx}]/${drc_rgr}/${drc_rgr_xtn}}"
1170 fl_rgr_xtn[${clm_idx}]="${fl_rgr_xtn[${clm_idx}]/_${yyyy_srt}/_${yyyy_srt_prv}}"
1171
1172 fl_amwg_xtn[${clm_idx}]=`expr match "${fl_xtn[${clm_idx}]}" '\(.*\)_.*_.*_climo.nc'` # Prune _YYYYYMM_YYYYMM_climo.nc
1173 fl_amwg_xtn[${clm_idx}]="${fl_amwg[${clm_idx}]}_climo.nc" # Replace with _climo.nc
1174 fl_amwg_xtn[${clm_idx}]="${fl_amwg[${clm_idx}]/${drc_xtn}\//}" # Delete prepended path to ease symlinking
1175 if [ ${clm_md} = 'scd' ] ; then
1176 # Handle Dec, DJF, and ANN
1177 if [ ${clm_idx} -eq 12 ] || [ ${clm_idx} -eq 16 ] || [ ${clm_idx} -eq 17 ] ; then
1178 fl_rgr_prv[${clm_idx}]="${fl_rgr[${clm_idx}]/${drc_rgr}/${drc_rgr_prv}}"
1179 fl_rgr_prv[${clm_idx}]="${fl_rgr_prv[${clm_idx}]/_${yyyy_clm_srt_dec}/_${yyyy_clm_srt_dec_prv}}"
1180 if [ ${clm_idx} -eq 12 ] ; then
1181 fl_rgr_prv[${clm_idx}]="${fl_rgr_prv[${clm_idx}]/_${yyyy_clm_end_dec}/_${yyyy_clm_end_dec_prv}}"
1182 else
1183 fl_rgr_prv[${clm_idx}]="${fl_rgr_prv[${clm_idx}]/_${yyyy_end}/_${yyyy_end_prv}}"
1184 fi # !Dec
1185
1186 fl_rgr_xtn[${clm_idx}]="${fl_rgr[${clm_idx}]/${drc_rgr}/${drc_rgr_xtn}}"
1187 fl_rgr_xtn[${clm_idx}]="${fl_rgr_xtn[${clm_idx}]/_${yyyy_clm_srt_dec}/_${yyyy_clm_srt_dec_prv}}"
1188 fi # !Dec, DJF, ANN
1189 fi # !clm_md
1190 done # !clm_idx
1191
1192 printf "Weight input climos to produce extended climo...\n"
1193 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1194 cmd_xtn[${clm_idx}]="${cmd_mpi[${clm_idx}]} ncflint -O ${nco_opt} -w ${wgt_prv},${wgt_crr} ${fl_prv[${clm_idx}]} ${fl_out[${clm_idx}]} ${fl_xtn[${clm_idx}]}"
1195 if [ ${dbg_lvl} -ge 1 ]; then
1196 echo ${cmd_xtn[${clm_idx}]}
1197 fi # !dbg
1198 if [ ${dbg_lvl} -le 1 ]; then
1199 if [ -z "${par_opt}" ]; then
1200 eval ${cmd_xtn[${clm_idx}]}
1201 if [ $? -ne 0 ]; then
1202 printf "${spt_nm}: ERROR extended climo cmd_xtn[${clm_idx}] failed. Debug this:\n${cmd_xtn[${clm_idx}]}\n"
1203 exit 1
1204 fi # !err
1205 else # !par_opt
1206 eval ${cmd_xtn[${clm_idx}]} ${par_opt} # eval always returns 0 on backgrounded processes
1207 xtn_pid[${clm_idx}]=$!
1208 fi # !par_opt
1209 fi # !dbg
1210 done # !clm_idx
1211 if [ -n "${par_opt}" ]; then
1212 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1213 wait ${xtn_pid[${clm_idx}]}
1214 if [ $? -ne 0 ]; then
1215 printf "${spt_nm}: ERROR extended climo cmd_xtn[${clm_idx}] failed. Debug this:\n${cmd_xtn[${clm_idx}]}\n"
1216 exit 1
1217 fi # !err
1218 done # !clm_idx
1219 fi # !par_opt
1220 wait
1221
1222 if [ -n "${rgr_map}" ]; then
1223 printf "Weight input climos to produce extended regridded climo...\n"
1224 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1225 cmd_rgr_xtn[${clm_idx}]="${cmd_mpi[${clm_idx}]} ncflint -O ${nco_opt} -w ${wgt_prv},${wgt_crr} ${fl_rgr_prv[${clm_idx}]} ${fl_rgr[${clm_idx}]} ${fl_rgr_xtn[${clm_idx}]}"
1226 if [ ${dbg_lvl} -ge 1 ]; then
1227 echo ${cmd_rgr_xtn[${clm_idx}]}
1228 fi # !dbg
1229 if [ ${dbg_lvl} -le 1 ]; then
1230 if [ -z "${par_opt}" ]; then
1231 eval ${cmd_rgr_xtn[${clm_idx}]}
1232 if [ $? -ne 0 ]; then
1233 printf "${spt_nm}: ERROR extended climo cmd_rgr_xtn[${clm_idx}] failed. Debug this:\n${cmd_rgr_xtn[${clm_idx}]}\n"
1234 exit 1
1235 fi # !err
1236 else # !par_opt
1237 eval ${cmd_rgr_xtn[${clm_idx}]} ${par_opt} # eval always returns 0 on backgrounded processes
1238 rgr_xtn_pid[${clm_idx}]=$!
1239 fi # !par_opt
1240 fi # !dbg
1241 done # !clm_idx
1242 if [ -n "${par_opt}" ]; then
1243 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1244 wait ${rgr_xtn_pid[${clm_idx}]}
1245 if [ $? -ne 0 ]; then
1246 printf "${spt_nm}: ERROR extended climo cmd_rgr_xtn[${clm_idx}] failed. Debug this:\n${cmd_rgr_xtn[${clm_idx}]}\n"
1247 exit 1
1248 fi # !err
1249 done # !clm_idx
1250 fi # !par_opt
1251 wait
1252 fi # !rgr_map
1253
1254 # Link ACME-climo to AMWG-climo filenames
1255 # drc_pwd is always fully qualified path but drc_out and drc_rgr may be relative paths
1256 # Strategy: Start in drc_pwd, cd to drc_rgr, then link so return code comes from ln not cd
1257 if [ ${lnk_flg} = 'Yes' ]; then
1258 printf "Link extended ACME-climo to AMWG-climo filenames...\n"
1259 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1260 if [ -n "${rgr_map}" ]; then
1261 cmd_lnk_xtn[${clm_idx}]="cd ${drc_pwd};cd ${drc_rgr_xtn};ln -s -f ${fl_rgr_xtn[${clm_idx}]/${drc_rgr_xtn}\//} ${fl_amwg[${clm_idx}]/${drc_rgr_xtn}\//}"
1262 else
1263 cmd_lnk_xtn[${clm_idx}]="cd ${drc_pwd};cd ${drc_xtn};ln -s -f ${fl_xtn[${clm_idx}]/${drc_xtn}\//} ${fl_amwg[${clm_idx}]/${drc_xtn}\//}"
1264 fi # !rgr_map
1265 if [ ${dbg_lvl} -ge 1 ]; then
1266 echo ${cmd_lnk_xtn[${clm_idx}]}
1267 fi # !dbg
1268 if [ ${dbg_lvl} -le 1 ]; then
1269 eval ${cmd_lnk_xtn[${clm_idx}]}
1270 if [ $? -ne 0 ]; then
1271 printf "${spt_nm}: ERROR linking ACME to AMWG filename cmd_lnk_xtn[${clm_idx}] failed. Debug this:\n${cmd_lnk_xtn[${clm_idx}]}\n"
1272 exit 1
1273 fi # !err
1274 fi # !dbg
1275 done # !clm_idx
1276 cd ${drc_pwd}
1277 fi # !lnk_flg
1278
1279 else # !xtn_flg extended climos
1280
1281 yr_nbr_xtn=${yr_nbr}
1282
1283 fi # !xtn_flg extended climos
1284
1285 date_end=$(date +"%s")
1286 printf "Completed ${yr_nbr_xtn}-year timeseries generation for dataset ${caseid} at `date`.\n"
1287 date_dff=$((date_end-date_srt))
1288 echo "Quick plots of climatological annual mean:"
1289 if [ -n "${yr_srt_prv}" ]; then
1290 if [ -n "${rgr_map}" ]; then
1291 echo "ncview ${fl_rgr_xtn[17]} &"
1292 echo "panoply ${fl_rgr_xtn[17]} &"
1293 else
1294 echo "ncview ${fl_xtn[17]} &"
1295 echo "panoply ${fl_xtn[17]} &"
1296 fi # !rgr_map
1297 else
1298 if [ -n "${rgr_map}" ]; then
1299 echo "ncview ${fl_rgr[17]} &"
1300 echo "panoply ${fl_rgr[17]} &"
1301 else
1302 echo "ncview ${fl_out[17]} &"
1303 echo "panoply ${fl_out[17]} &"
1304 fi # !rgr_map
1305 fi # !yr_srt_prv
1306 echo "Elapsed time $((date_dff/60))m$((date_dff % 60))s"
1307
1308 # PMC: add SMB's Git (SHA1) hash info to climo files
1309 # Assumes utility to add Git hash resides in ../utils/add_git_hash_to_netcdf_metadata
1310 for ((clm_idx=1;clm_idx<=17;clm_idx++)); do
1311 fl_out_lst="${fl_out_lst} ${fl_out[${clm_idx}]}"
1312 done
1313 # CSZ: 20150826 disable until less fragile (than relative path) solution is found
1314 #cd ${drc_spt}
1315 # ../utils/add_git_hash_to_netcdf_metadata ${fl_out_lst}
1316
1317 exit 0