| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# Purpose: Check data file against DIWG (and, eventually, other) recommendations |
| 4 |
|
| 5 |
# Copyright (C) 2023--present Charlie Zender, ... (Contribuing DIWG members---Add your names!) |
| 6 |
|
| 7 |
# This program is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 10 |
# See the 3-Clause BSD License for more details. |
| 11 |
|
| 12 |
# Prerequisites: Bash, NCO |
| 13 |
# Script could use other shells, e.g., dash (Debian default) after rewriting function definitions and loops |
| 14 |
# Debug with 'bash -x ncchecker --dbg=dbg_lvl' where 0 <= dbg_lvl <= 5 |
| 15 |
|
| 16 |
# Insta-install: |
| 17 |
# scp ~/diwg/ncchecker dust.ess.uci.edu:bin |
| 18 |
# scp dust.ess.uci.edu:bin/ncchecker ${MY_BIN_DIR} |
| 19 |
|
| 20 |
# Set script name, directory, PID, run directory |
| 21 |
drc_pwd=${PWD} |
| 22 |
# Security: Explicitly unset IFS before wordsplitting, so Bash uses default IFS=<space><tab><newline> |
| 23 |
unset IFS |
| 24 |
# Set these before 'module' command which can overwrite ${BASH_SOURCE[0]} |
| 25 |
# NB: dash supports $0 syntax, not ${BASH_SOURCE[0]} syntax |
| 26 |
# http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in |
| 27 |
spt_src="${BASH_SOURCE[0]}" |
| 28 |
[[ -z "${spt_src}" ]] && spt_src="${0}" # Use ${0} when BASH_SOURCE is unavailable (e.g., dash) |
| 29 |
while [ -h "${spt_src}" ]; do # Recursively resolve ${spt_src} until file is no longer a symlink |
| 30 |
drc_spt="$( cd -P "$( dirname "${spt_src}" )" && pwd )" |
| 31 |
spt_src="$(readlink "${spt_src}")" |
| 32 |
[[ ${spt_src} != /* ]] && spt_src="${drc_spt}/${spt_src}" # If ${spt_src} was relative symlink, resolve it relative to path where symlink file was located |
| 33 |
done |
| 34 |
cmd_ln="${spt_src} ${@}" |
| 35 |
drc_spt="$( cd -P "$( dirname "${spt_src}" )" && pwd )" |
| 36 |
spt_nm=$(basename ${spt_src}) # [sng] Script name (unlike $0, ${BASH_SOURCE[0]} works well with 'source <script>') |
| 37 |
spt_pid=$$ # [nbr] Script PID (process ID) |
| 38 |
|
| 39 |
# Original Setup (prior to merger with NCO): |
| 40 |
# git clone [email protected]:diwg/diwg ~/diwg |
| 41 |
# ln -s ~/diwg/diwg_chk ~/bin/diwg_chk |
| 42 |
# ln -s ~/diwg/diwg_chk ~/sh/diwg_chk |
| 43 |
|
| 44 |
# Debugging and Benchmarking: |
| 45 |
# ncchecker ~/nco/data/in.nc ~/nco/data/in_4.nc |
| 46 |
# ncchecker ~/data/bm/elmv2_ne30pg2l15.nc |
| 47 |
# ncchecker ~/data/hdf/MODIS_L2N_20140304T1120.nc > ~/diwg.txt 2>&1 & |
| 48 |
# ncchecker ~/data/hdf/OMI-Aura_L2-OMAERUV_2013m1004t2338-o49057_v003-2013m1005t053932.nc |
| 49 |
|
| 50 |
# Production usage: |
| 51 |
# ncchecker --dbg=1 ~/nco/data/in_4.nc > ~/diwg.txt 2>&1 & |
| 52 |
# screen # Start screen |
| 53 |
# ncchecker --dbg=1 ~/nco/data/in_4.nc > ~/diwg.txt 2>&1 & |
| 54 |
# Ctl-A D # Detach screen |
| 55 |
# tail ~/diwg.txt # Monitor progress |
| 56 |
# screen -ls # List screens |
| 57 |
# screen -r <ID> # Re-attach screen |
| 58 |
|
| 59 |
# Configure paths at High-Performance Computer Centers (HPCCs) based on ${HOSTNAME} |
| 60 |
if [ -z "${HOSTNAME}" ]; then |
| 61 |
if [ -f /bin/hostname ] && [ -x /bin/hostname ]; then |
| 62 |
export HOSTNAME=`/bin/hostname` |
| 63 |
elif [ -f /usr/bin/hostname ] && [ -x /usr/bin/hostname ]; then |
| 64 |
export HOSTNAME=`/usr/bin/hostname` |
| 65 |
fi # !hostname |
| 66 |
fi # HOSTNAME |
| 67 |
|
| 68 |
# Set NCO version and directory |
| 69 |
nco_exe=`which ncks` |
| 70 |
if [ -z "${nco_exe}" ]; then |
| 71 |
echo "${spt_nm}: ERROR Unable to find NCO, \${nco_exe} = ${nco_exe}" |
| 72 |
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., Anaconda)" |
| 73 |
exit 1 |
| 74 |
fi # !nco_exe |
| 75 |
# StackOverflow method finds NCO directory |
| 76 |
while [ -h "${nco_exe}" ]; do |
| 77 |
drc_nco="$( cd -P "$( dirname "${nco_exe}" )" && pwd )" |
| 78 |
nco_exe="$(readlink "${nco_exe}")" |
| 79 |
[[ ${nco_exe} != /* ]] && nco_exe="${drc_nco}/${nco_exe}" |
| 80 |
done |
| 81 |
drc_nco="$( cd -P "$( dirname "${nco_exe}" )" && pwd )" |
| 82 |
nco_vrs=$(ncks --version 2>&1 > /dev/null | grep NCO | awk '{print $5}') |
| 83 |
nco_sng=$(ncks --version 2>&1 > /dev/null | grep NCO | awk -F '"' '{print $2}') |
| 84 |
|
| 85 |
# 20190218: Die quickly when NCO is found yet cannot run, e.g., due to linker errors |
| 86 |
if [ -z "${nco_vrs}" ]; then |
| 87 |
echo "${spt_nm}: ERROR ${nco_exe} dies with error message on next line:" |
| 88 |
$(ncks --version) |
| 89 |
exit 1 |
| 90 |
fi # !nco_vrs |
| 91 |
lbr_vrs=$(ncks --library 2>&1 > /dev/null | awk '{print $6}') |
| 92 |
|
| 93 |
# When running in a terminal window (not in an non-interactive batch queue)... |
| 94 |
if [ -n "${TERM}" ]; then |
| 95 |
# Set fonts for legibility |
| 96 |
if [ -x /usr/bin/tput ] && tput setaf 1 &> /dev/null; then |
| 97 |
fnt_bld=`tput bold` # Bold |
| 98 |
fnt_nrm=`tput sgr0` # Normal |
| 99 |
fnt_rvr=`tput smso` # Reverse |
| 100 |
fnt_tlc=`tput sitm` # Italic |
| 101 |
else |
| 102 |
fnt_bld="\e[1m" # Bold |
| 103 |
fnt_nrm="\e[0m" # Normal |
| 104 |
fnt_rvr="\e[07m" # Reverse |
| 105 |
fnt_tlc="\e[3m" # Italic |
| 106 |
fi # !tput |
| 107 |
fi # !TERM |
| 108 |
|
| 109 |
# Defaults for command-line options and some derived variables |
| 110 |
# Modify these defaults to save typing later |
| 111 |
caseid='elmforc.ERA5.c2018.0.25d' # [sng] Case ID |
| 112 |
dbg_lvl=0 # [nbr] Debugging level |
| 113 |
drc_in='' # [sng] Input file directory |
| 114 |
drc_in_xmp="${DATA}/era5/data_raw/1979" # [sng] Input file directory for examples |
| 115 |
drc_out='' # [sng] Output file directory |
| 116 |
drc_out_xmp="${DATA}/era5/data_out" # [sng] Output file directory for examples |
| 117 |
inp_aut='No' # [sng] Input file list automatically generated |
| 118 |
inp_glb='No' # [sng] Input file list from globbing directory |
| 119 |
inp_psn='No' # [sng] Input file list from positional arguments |
| 120 |
inp_std='No' # [sng] Input file list from stdin |
| 121 |
nco_opt='' # [sng] NCO options (e.g., '-6 -t 1') |
| 122 |
tst_flg='Yes' # [flg] Trigger test-selection code |
| 123 |
tst_lst='bnd,chr,mss,nan,tm,xtn' # [sng] Tests to conduct (subset of bnd,chr,mss,nan,tm,xtn) |
| 124 |
vrs_prn='No' # [sng] Print version information |
| 125 |
|
| 126 |
# NCO filters documented in http://nco.sf.net/nco.html#filter |
| 127 |
function ncvarlst { ncks --trd -m ${1} | grep -E ': type' | cut -f 1 -d ' ' | sed 's/://' | sort ; } |
| 128 |
function ncdmnlst { ncks --cdl -m ${1} | cut -d ':' -f 1 | cut -d '=' -s -f 1 ; } |
| 129 |
|
| 130 |
function fnc_usg_prn { # NB: dash supports fnc_nm (){} syntax, not function fnc_nm{} syntax |
| 131 |
# Print usage |
| 132 |
printf "${fnt_rvr}Basic usage:\n${fnt_nrm}${fnt_bld}${spt_nm} in.nc${fnt_nrm} # Default settings\n" |
| 133 |
printf "${fnt_bld}${spt_nm} -t bnd,nan${fnt_nrm} # Short option syntax with explicit tests\n" |
| 134 |
printf "${fnt_bld}${spt_nm} --tst=bnd,nan${fnt_nrm} # Long options with with explicit tests\n" |
| 135 |
echo "Command-line options [long-option synonyms in ${fnt_tlc}italics${fnt_nrm}]:" |
| 136 |
# echo "${fnt_rvr}-3${fnt_nrm} Output file format CLASSIC (netCDF3 classic CDF1) [${fnt_tlc}fl_fmt, file_format=classic${fnt_nrm}]" |
| 137 |
# echo "${fnt_rvr}-4${fnt_nrm} Output file format NETCDF4 (netCDF4 extended HDF5) [${fnt_tlc}fl_fmt, file_format=netcdf4${fnt_nrm}]" |
| 138 |
# echo "${fnt_rvr}-5${fnt_nrm} Output file format 64BIT_DATA (netCDF3/PnetCDF CDF5) [${fnt_tlc}fl_fmt, file_format=64bit_data${fnt_nrm}]" |
| 139 |
# echo "${fnt_rvr}-6${fnt_nrm} Output file format 64BIT_OFFSET (netCDF3 64bit CDF2) [${fnt_tlc}fl_fmt, file_format=64bit_offset${fnt_nrm}]" |
| 140 |
# echo "${fnt_rvr}-7${fnt_nrm} Output file format NETCDF4_CLASSIC (netCDF4 classic HDF5) [${fnt_tlc}fl_fmt, file_format=netcdf4_classic${fnt_nrm}]" |
| 141 |
# echo "${fnt_rvr}-c${fnt_nrm} ${fnt_bld}caseid${fnt_nrm} Case ID string to generate output filenames (default ${fnt_bld}${caseid}${fnt_nrm}) [${fnt_tlc}caseid, case_id, case${fnt_nrm}]" |
| 142 |
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}]" |
| 143 |
echo "${fnt_rvr}-i${fnt_nrm} ${fnt_bld}drc_in${fnt_nrm} Input directory (default ${fnt_bld}${drc_in}${fnt_nrm}) [${fnt_tlc}drc_in, in_drc, dir_in, in_dir, input${fnt_nrm}]" |
| 144 |
# echo "${fnt_rvr}-j${fnt_nrm} ${fnt_bld}job_nbr${fnt_nrm} Job simultaneity for parallelism (default ${fnt_bld}${job_nbr}${fnt_nrm}) [${fnt_tlc}job_nbr, job_number, jobs${fnt_nrm}]" |
| 145 |
# echo "${fnt_rvr}-o${fnt_nrm} ${fnt_bld}drc_out${fnt_nrm} Output directory (default ${fnt_bld}${drc_out}${fnt_nrm}) [${fnt_tlc}drc_out, out_drc, dir_out, out_dir, output${fnt_nrm}]" |
| 146 |
echo "${fnt_rvr}-p${fnt_nrm} ${fnt_bld}par_typ${fnt_nrm} Parallelism type (default ${fnt_bld}${par_typ}${fnt_nrm}) [${fnt_tlc}par_typ, par_md, parallel_type, parallel_mode, parallel${fnt_nrm}] [${fnt_tlc}serial | background | mpi${fnt_nrm}]" |
| 147 |
# echo " ${fnt_bld}--tpd_out${fnt_nrm} Timesteps-per-day in output (default ${fnt_bld}${tpd_out}${fnt_nrm}) [${fnt_tlc}tpd_out, tpd, timesteps_per_day${fnt_nrm}]" |
| 148 |
echo " ${fnt_bld}--tests${fnt_nrm} Tests to conduct ('none' means none) (default ${fnt_bld}${tst_lst}${fnt_nrm} [${fnt_tlc}tst_lst, tst, test_list${fnt_nrm}]" |
| 149 |
echo "${fnt_rvr}-v${fnt_nrm} ${fnt_bld}var_lst${fnt_nrm} Variable list (empty means all) (default ${fnt_bld}${var_lst}${fnt_nrm}) [${fnt_tlc}var_lst, variable_list, var, vars, variable, variables${fnt_nrm}]" |
| 150 |
echo " ${fnt_bld}--version${fnt_nrm} Version and configuration information [${fnt_tlc}version, vrs, config, configuration, cnf${fnt_nrm}]" |
| 151 |
echo "${fnt_rvr}-x${fnt_nrm} ${fnt_bld}--xcl_var${fnt_nrm} Exclude rather than extract var_lst [${fnt_tlc}xcl_var, xcl, exclude, exclude_variables${fnt_nrm}]" |
| 152 |
printf "\n" |
| 153 |
printf "${fnt_rvr}Examples:${fnt_nrm}\n" |
| 154 |
printf "${fnt_bld}${spt_nm} in1.nc in2.nc${fnt_nrm} # Run all tests on two files\n" |
| 155 |
printf "${fnt_bld}${spt_nm} -v var1,var2 in1.nc # Check only two variables\n" |
| 156 |
printf "${fnt_bld}${spt_nm} *.nc${fnt_nrm} # Glob input files via wildcard\n" |
| 157 |
printf "${fnt_bld}ls *.nc | ${spt_nm}${fnt_nrm} # Input files via stdin\n" |
| 158 |
printf "${fnt_bld}${spt_nm} --dbg=2 *.nc${fnt_nrm} # Debug ${spt_nm}\n" |
| 159 |
printf "${fnt_bld}${spt_nm} --tests=nan,mss *.nc # Select only two tests\n" |
| 160 |
printf "${fnt_bld}${spt_nm} --tests=xtn,tm,nan,mss,chr,bnd *.nc${fnt_nrm} # Change test ordering\n" |
| 161 |
printf "${fnt_bld}${spt_nm} "file://${HOME}/in_zarr4#mode=nczarr,file"${fnt_nrm} # Check Zarr object(s)\n" |
| 162 |
printf "\nComplete documentation at http://nco.sf.net/nco.html#${spt_nm}\n\n" |
| 163 |
exit 1 |
| 164 |
} # !fnc_usg_prn() |
| 165 |
|
| 166 |
# Check argument number and complain accordingly |
| 167 |
arg_nbr=$# |
| 168 |
#printf "\ndbg: Number of arguments: ${arg_nbr}" |
| 169 |
if [ ${arg_nbr} -eq 0 ]; then |
| 170 |
fnc_usg_prn |
| 171 |
fi # !arg_nbr |
| 172 |
|
| 173 |
# Parse command-line options: |
| 174 |
# http://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options (see method by Adam Katz) |
| 175 |
# http://tuxtweaks.com/2014/05/bash-getopts |
| 176 |
while getopts :34567c:d:f:i:o:p:s:v:x-: OPT; do |
| 177 |
case ${OPT} in |
| 178 |
3) fl_fmt='3' ;; # File format |
| 179 |
4) fl_fmt='4' ;; # File format |
| 180 |
5) fl_fmt='5' ;; # File format |
| 181 |
6) fl_fmt='6' ;; # File format |
| 182 |
7) fl_fmt='7' ;; # File format |
| 183 |
c) caseid="${OPTARG}" ;; # CASEID |
| 184 |
d) dbg_lvl="${OPTARG}" ;; # Debugging level |
| 185 |
f) fml_nm_usr="${OPTARG}" ;; # Family name |
| 186 |
i) drc_in="${OPTARG}" ;; # Input directory |
| 187 |
o) drc_out_usr="${OPTARG}" ;; # Output directory |
| 188 |
p) par_typ="${OPTARG}" ;; # Parallelism type |
| 189 |
v) var_lst="${OPTARG}" ;; # Variables |
| 190 |
x) xcl_flg='Yes' ;; # Exclude variable list |
| 191 |
-) LONG_OPTARG="${OPTARG#*=}" |
| 192 |
case ${OPTARG} in |
| 193 |
# Hereafter ${OPTARG} is long argument key, and ${LONG_OPTARG}, if any, is long argument value |
| 194 |
# Long options with no argument, no short option counterpart |
| 195 |
# Long options with argument, no short option counterpart |
| 196 |
# Long options with short counterparts, ordered by short option key |
| 197 |
caseid=?* | case_id=?* | case=?* ) caseid="${LONG_OPTARG}" ;; # -c # CASEID |
| 198 |
dbg_lvl=?* | dbg=?* | debug=?* | debug_level=?* ) dbg_lvl="${LONG_OPTARG}" ;; # -d # Debugging level |
| 199 |
drc_in=?* | in_drc=?* | dir_in=?* | in_dir=?* | input=?* ) drc_in="${LONG_OPTARG}" ;; # -i # Input directory |
| 200 |
drc_out=?* | out_drc=?* | dir_out=?* | out_dir=?* | output=?* ) drc_out_usr="${LONG_OPTARG}" ;; # -o # Output directory |
| 201 |
fl_fmt=?* | fmt_out=?* | file_format=?* | format_out=?* ) fl_fmt="${LONG_OPTARG}" ;; # # Output file format |
| 202 |
fml_nm=?* | fml=?* | family_name=?* | family=?* ) fml_nm_usr="${LONG_OPTARG}" ;; # -f # Family name |
| 203 |
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()) |
| 204 |
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 |
| 205 |
job_nbr=?* | job_number=?* | jobs=?* ) job_usr="${LONG_OPTARG}" ;; # -j # Job simultaneity |
| 206 |
par_typ=?* | par_md=?* | parallel_type=?* | parallel_mode=?* | parallel=?* ) par_typ="${LONG_OPTARG}" ;; # -p # Parallelism type |
| 207 |
tests=?* | tst_lst=?* | tst=?* | test_list=?* ) tst_lst="${LONG_OPTARG}" ;; # # Tests to conduct |
| 208 |
tpd_out=?* | tpd=?* | timesteps_per_day=?* ) tpd_usr="${LONG_OPTARG}" ;; # # Timesteps-per-day in output |
| 209 |
var_lst=?* | variable_list=?* | var=?* | vars=?* | variable=?* | variables=?* ) var_lst="${LONG_OPTARG}" ;; # -v # Variables |
| 210 |
version | vrs | config | configuration | cnf ) vrs_prn='Yes' ;; # # Print version information |
| 211 |
version=?* | vrs=?* | config=?* | configuration=?* | cnf=?* ) echo "No argument allowed for --${OPTARG switch}" >&2; exit 1 ;; # # Print version information |
| 212 |
xcl_var | xcl | exclude | exclude_variables ) xcl_flg='Yes' ;; # # Exclude rather than extract variable list |
| 213 |
xcl_var=?* | xcl=?* | exclude=?* | exclude_variables=?* ) echo "No argument allowed for --${OPTARG switch}" >&2; exit 1 ;; # # Exclude rather than extract variable list |
| 214 |
'' ) break ;; # "--" terminates argument processing |
| 215 |
* ) printf "\nERROR: Unrecognized option ${fnt_bld}--${OPTARG}${fnt_nrm}\n" >&2; fnc_usg_prn ;; |
| 216 |
esac ;; # !OPTARG |
| 217 |
\?) # Unrecognized option |
| 218 |
printf "\nERROR: Option ${fnt_bld}-${OPTARG}${fnt_nrm} not recognized\n" >&2 |
| 219 |
fnc_usg_prn ;; |
| 220 |
esac # !OPT |
| 221 |
done # !getopts |
| 222 |
shift $((OPTIND-1)) # Advance one argument |
| 223 |
psn_nbr=$# |
| 224 |
if [ ${psn_nbr} -ge 1 ]; then |
| 225 |
inp_psn='Yes' |
| 226 |
# 20200430 Input files on command-line mean we need not check standard-input |
| 227 |
std_chk='No' |
| 228 |
fi # !psn_nbr |
| 229 |
|
| 230 |
if [ ${vrs_prn} = 'Yes' ]; then |
| 231 |
printf "${spt_nm}, runs DIWG recommendation compliance checks, version ${nco_vrs} \"${nco_sng}\"\n" |
| 232 |
printf "Authors: \n" |
| 233 |
printf "Config: ${spt_nm} script located in directory ${drc_spt}\n" |
| 234 |
printf "Config: NCO binaries located in directory ${drc_nco}, linked to netCDF library version ${lbr_vrs}\n" |
| 235 |
if [ "${hrd_pth_fnd}" = 'Yes' ]; then |
| 236 |
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" |
| 237 |
else |
| 238 |
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" |
| 239 |
fi # !hrd_pth_fnd |
| 240 |
exit 0 |
| 241 |
fi # !vrs_prn |
| 242 |
|
| 243 |
# Determine mode first (this helps determine other defaults) |
| 244 |
if [ ${tst_lst} = 'none' ]; then |
| 245 |
tst_nbr=0 |
| 246 |
tst_flg='No' |
| 247 |
fi # !tst_lst |
| 248 |
|
| 249 |
if [ -z "${drc_in}" ]; then |
| 250 |
drc_in="${drc_pwd}" |
| 251 |
else # !drc_in |
| 252 |
if [ ! -d "${drc_in}" ]; then |
| 253 |
echo "${spt_nm}: ERROR specified input directory \"${drc_in}\" does not exist" |
| 254 |
exit 1 |
| 255 |
fi # !drc_in |
| 256 |
drc_in_usr='Yes' |
| 257 |
fi # !drc_in |
| 258 |
|
| 259 |
# 20170807 Custom tests (code borrowed from ncremap seasons (csn) treatment) |
| 260 |
if [ "${tst_flg}" = 'Yes' ]; then |
| 261 |
|
| 262 |
# Standard test enumeration |
| 263 |
# Index into test definition table |
| 264 |
tst_idx_srt=0 # [idx] Starting index for test enumeration |
| 265 |
tst_bnd=0 |
| 266 |
tst_chr=1 |
| 267 |
tst_mss=2 |
| 268 |
tst_nan=3 |
| 269 |
tst_tm=4 |
| 270 |
tst_xtn=5 |
| 271 |
tst_nbr_max=6 # [nbr] Maximum number of tests in definitions database |
| 272 |
|
| 273 |
# Test abbreviations, uppercase |
| 274 |
tst_abb[${tst_bnd}]='BND' |
| 275 |
tst_abb[${tst_chr}]='CHR' |
| 276 |
tst_abb[${tst_mss}]='MSS' |
| 277 |
tst_abb[${tst_nan}]='NAN' |
| 278 |
tst_abb[${tst_tm}]='TM' |
| 279 |
tst_abb[${tst_xtn}]='XTN' |
| 280 |
|
| 281 |
# Test abbreviations, lowercase |
| 282 |
tst_abb_lc[${tst_bnd}]='bnd' |
| 283 |
tst_abb_lc[${tst_chr}]='chr' |
| 284 |
tst_abb_lc[${tst_mss}]='mss' |
| 285 |
tst_abb_lc[${tst_nan}]='nan' |
| 286 |
tst_abb_lc[${tst_tm}]='tm' |
| 287 |
tst_abb_lc[${tst_xtn}]='xtn' |
| 288 |
|
| 289 |
# Recommendation (i.e., short exhortation) in ESDS RFC Title |
| 290 |
tst_rcm[${tst_bnd}]='Use CF Bounds Attributes' |
| 291 |
tst_rcm[${tst_chr}]='Identifier names shall comply with this regular expression: [A-Za-z][A-Za-z0-9_]*' |
| 292 |
tst_rcm[${tst_mss}]='Avoid use of the missing_value attribute in new Earth Science data products' |
| 293 |
tst_rcm[${tst_nan}]='Earth Science data products should avoid using Not-a-Number (NaN) in any field values or as an indicator of missing or invalid data' |
| 294 |
tst_rcm[${tst_tm}]='Use Double Precision When Archiving Time in Seconds Since a Specific Epoch' |
| 295 |
tst_rcm[${tst_xtn}]='Files created with the HDF5, HDF-EOS5, or netCDF APIs should have filename extensions \"h5\", \"he5\", or \"nc\", respectively' |
| 296 |
|
| 297 |
# ESDS RFC Recommendation Number |
| 298 |
tst_rfc[${tst_bnd}]='2.3' |
| 299 |
tst_rfc[${tst_chr}]='3.1' |
| 300 |
tst_rfc[${tst_mss}]='4.2' |
| 301 |
tst_rfc[${tst_nan}]='3.7' |
| 302 |
tst_rfc[${tst_tm}]='4.11' |
| 303 |
tst_rfc[${tst_xtn}]='3.8' |
| 304 |
|
| 305 |
# ESDS RFC Recommendation URL |
| 306 |
tst_url[${tst_bnd}]='https://wiki.earthdata.nasa.gov/pages/viewpage.action?pageId=182296291' |
| 307 |
tst_url[${tst_chr}]='https://wiki.earthdata.nasa.gov/display/ESDSWG/Character+Set+for+User-Defined+Group%2C+Variable%2C+and+Attribute+names' |
| 308 |
tst_url[${tst_mss}]='https://wiki.earthdata.nasa.gov/display/ESDSWG/Avoid+Use+of+the+missing_value+Attribute' |
| 309 |
tst_url[${tst_nan}]='https://wiki.earthdata.nasa.gov/display/ESDSWG/Not-a-Number+%28NaN%29+Value' |
| 310 |
tst_url[${tst_tm}]='https://wiki.earthdata.nasa.gov/display/ESDSWG/Use+Double+Precision+When+Archiving+Time+in+Seconds+Since+a+Specific+Epoch' |
| 311 |
tst_url[${tst_xtn}]='https://wiki.earthdata.nasa.gov/pages/viewpage.action?pageId=182297715' |
| 312 |
|
| 313 |
# Test names "in English" (complete phrase "Test checks that ___") |
| 314 |
tst_ngl[${tst_bnd}]='coordinates have bounds attributes' |
| 315 |
tst_ngl[${tst_chr}]='identifier names comprised of legal characters' |
| 316 |
tst_ngl[${tst_mss}]='variables do not have missing_value attributes' |
| 317 |
tst_ngl[${tst_nan}]='data values do not contain NaN (or NaNf)' |
| 318 |
tst_ngl[${tst_tm}]='time variables are stored in double precision' |
| 319 |
tst_ngl[${tst_xtn}]='filename extension matches recommended list' |
| 320 |
|
| 321 |
# Test short names (complete phrase "___ check passed/failed") |
| 322 |
tst_sht[${tst_bnd}]='coordinate bounds' |
| 323 |
tst_sht[${tst_chr}]='character-set' |
| 324 |
tst_sht[${tst_mss}]='missing_value' |
| 325 |
tst_sht[${tst_nan}]='NaN' |
| 326 |
tst_sht[${tst_tm}]='precision of time variables' |
| 327 |
tst_sht[${tst_xtn}]='filename extension' |
| 328 |
|
| 329 |
# Test invocation options to ncks |
| 330 |
tst_opt[${tst_bnd}]='--chk_bnd' |
| 331 |
tst_opt[${tst_chr}]='--chk_chr' |
| 332 |
tst_opt[${tst_mss}]='--chk_mss' |
| 333 |
tst_opt[${tst_nan}]='--chk_nan' |
| 334 |
tst_opt[${tst_tm}]='--chk_tm' |
| 335 |
tst_opt[${tst_xtn}]='--chk_xtn' |
| 336 |
|
| 337 |
# Which tests are requested? |
| 338 |
# http://stackoverflow.com/questions/27702452/loop-through-a-comma-separated-shell-variable |
| 339 |
tst_nbr=0 # [sng] Number of tests to conduct |
| 340 |
for tst in ${tst_lst//,/ }; do |
| 341 |
tst_rqs[${tst_nbr}]=${tst} |
| 342 |
# NB: Requested seasons are 0-based, defined seasons are 0-based |
| 343 |
for ((tst_dfn_idx=${tst_idx_srt};tst_dfn_idx<${tst_nbr_max};tst_dfn_idx++)); do |
| 344 |
if [[ "${tst}" =~ "${tst_abb[${tst_dfn_idx}]}" ]] || [[ "${tst}" =~ "${tst_abb_lc[${tst_dfn_idx}]}" ]]; then |
| 345 |
# Map requested to defined (r2d) tests and inverse (d2r) |
| 346 |
# map_r2d[0]=3 means first test that user requested (i.e., in tst_lst) is fourth defined in table |
| 347 |
# map_d2r[3]=0 means fourth defined test is first requested |
| 348 |
map_r2d[${tst_nbr}]=${tst_dfn_idx} |
| 349 |
map_d2r[${tst_dfn_idx}]=${tst_nbr} |
| 350 |
let tst_nbr=${tst_nbr}+1 |
| 351 |
break |
| 352 |
fi # !match |
| 353 |
done # !tst_dfn_idx |
| 354 |
if [ "${tst_dfn_idx}" -eq "${tst_nbr_max}" ]; then |
| 355 |
printf "${spt_nm}: ERROR Requested test ${tst} not defined\n" |
| 356 |
exit 1 |
| 357 |
fi # !match |
| 358 |
done # !tst_lst |
| 359 |
|
| 360 |
fi # !tst_flg |
| 361 |
|
| 362 |
# Read files from stdin pipe, positional arguments, or directory glob |
| 363 |
#printf "dbg: inp_aut = ${inp_aut}\n" |
| 364 |
#printf "dbg: inp_glb = ${inp_glb}\n" |
| 365 |
#printf "dbg: inp_psn = ${inp_psn}\n" |
| 366 |
#printf "dbg: inp_std = ${inp_std}\n" |
| 367 |
|
| 368 |
# Derived variables |
| 369 |
if [ -n "${drc_out_usr}" ]; then |
| 370 |
chr_fst=${drc_out_usr:0:1} |
| 371 |
if [ "${chr_fst}" = '-' ]; then |
| 372 |
printf "${spt_nm}: WARNING first character of user-specified output directory drc_out is \"${chr_fst}\", which looks like an option itself. Be sure the ${spt_nm} '-O' option is followed by an argument that specifies the output directory for processed files. The '-O' option requires a pathname argument, and should not be confused with the '-O' flag (which takes no argument) used in the rest of NCO to force overwriting files (${spt_nm} automatically overwrites existing output files).\n" |
| 373 |
fi # !chr_fst |
| 374 |
# Fancy %/ syntax removes trailing slash (e.g., from $TMPDIR) |
| 375 |
drc_out="${drc_out_usr%/}" |
| 376 |
fi # !drc_out_usr |
| 377 |
|
| 378 |
# Doubly-derived variables |
| 379 |
|
| 380 |
if [ ${inp_glb} = 'Yes' ]; then |
| 381 |
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 |
| 382 |
if [ -f "${fl}" ]; then |
| 383 |
fl_in[${fl_nbr}]=${fl} |
| 384 |
let fl_nbr=${fl_nbr}+1 |
| 385 |
fi # !file |
| 386 |
done |
| 387 |
fi # !inp_glb |
| 388 |
if [ ${inp_psn} = 'Yes' ]; then |
| 389 |
# Read any positional arguments |
| 390 |
for ((psn_idx=1;psn_idx<=psn_nbr;psn_idx++)); do |
| 391 |
fl_in[(${psn_idx}-1)]=${!psn_idx} |
| 392 |
fl_nbr=${psn_nbr} |
| 393 |
done # !psn_idx |
| 394 |
fi # !inp_psn |
| 395 |
if [ ${inp_std} = 'Yes' ]; then |
| 396 |
# Input awaits on unit 0, i.e., on stdin |
| 397 |
while read -r line; do # NeR05 p. 179 |
| 398 |
fl_in[${fl_nbr}]=${line} |
| 399 |
let fl_nbr=${fl_nbr}+1 |
| 400 |
done < /dev/stdin |
| 401 |
fi # !inp_std |
| 402 |
|
| 403 |
# Create output directory |
| 404 |
if [ -n "${drc_out}" ] && [ ! -d "${drc_out}" ]; then |
| 405 |
chr_fst=${drc_out:0:1} |
| 406 |
if [ "${chr_fst}" = '-' ]; then |
| 407 |
echo "${spt_nm}: ERROR Attempting to mkdir user-specified output directory \"${drc_out}\" will fail because directory name begins with '-' which is an option indicator" |
| 408 |
echo "${spt_nm}: HINT Specify an output directory name that does not begin with '-'" |
| 409 |
exit 1 |
| 410 |
fi # !chr_fst |
| 411 |
cmd_mkd="mkdir -p ${drc_out}" |
| 412 |
eval ${cmd_mkd} |
| 413 |
if [ "$?" -ne 0 ]; then |
| 414 |
printf "${spt_nm}: ERROR Failed to create output directory. Debug this:\n${cmd_mkd}\n" |
| 415 |
printf "${spt_nm}: HINT Creating a directory requires proper write permissions\n" |
| 416 |
exit 1 |
| 417 |
fi # !err |
| 418 |
fi # !drc_out |
| 419 |
|
| 420 |
# Print initial state |
| 421 |
if [ ${dbg_lvl} -ge 2 ]; then |
| 422 |
printf "dbg: dbg_lvl = ${dbg_lvl}\n" |
| 423 |
printf "dbg: drc_in = ${drc_in}\n" |
| 424 |
printf "dbg: drc_out = ${drc_out}\n" |
| 425 |
printf "dbg: hrd_pth = ${hrd_pth}\n" |
| 426 |
printf "dbg: job_nbr = ${job_nbr}\n" |
| 427 |
printf "dbg: mpi_flg = ${mpi_flg}\n" |
| 428 |
printf "dbg: mpi_nbr = ${mpi_nbr}\n" |
| 429 |
printf "dbg: nco_opt = ${nco_opt}\n" |
| 430 |
printf "dbg: tst_lst = ${tst_lst}\n" |
| 431 |
printf "dbg: tst_nbr = ${tst_nbr}\n" |
| 432 |
printf "dbg: var_lst = ${var_lst}\n" |
| 433 |
printf "dbg: xcl_flg = ${xcl_flg}\n" |
| 434 |
fi # !dbg |
| 435 |
if [ ${dbg_lvl} -ge 2 ]; then |
| 436 |
psn_nbr=$# |
| 437 |
if [ ${psn_nbr} -ge 1 ]; then |
| 438 |
printf "dbg: Found ${psn_nbr} positional parameters (besides \$0):\n" |
| 439 |
for ((psn_idx=1;psn_idx<=psn_nbr;psn_idx++)); do |
| 440 |
printf "dbg: psn_arg[${psn_idx}] = ${!psn_idx}\n" |
| 441 |
done # !psn_idx |
| 442 |
fi # !psn_nbr |
| 443 |
if [ "${fl_nbr}" -ge 1 ]; then |
| 444 |
printf "dbg: Processing ${fl_nbr} raw input files:\n" |
| 445 |
for ((fl_idx=0;fl_idx<fl_nbr;fl_idx++)); do |
| 446 |
printf "dbg: fl_in[${fl_idx}] = ${fl_in[${fl_idx}]}\n" |
| 447 |
done # !fl_idx |
| 448 |
fi # !fl_nbr |
| 449 |
fi # !dbg |
| 450 |
|
| 451 |
# Human-readable summary |
| 452 |
date_srt=$(date +"%s") |
| 453 |
if [ ${dbg_lvl} -ge 0 ]; then |
| 454 |
printf "ncchecker, a DIWG recommendation compliance checker, invoked with command:\n" |
| 455 |
echo "${cmd_ln}" |
| 456 |
printf "Started DIWG checker at `date`\n" |
| 457 |
printf "# of recommendations that will be checked: ${tst_nbr}/${tst_nbr_max}\n" |
| 458 |
printf "Recommendations that will be checked: " |
| 459 |
for ((tst_idx=0;tst_idx<tst_nbr;tst_idx++)); do |
| 460 |
idx_dfn=${map_r2d[${tst_idx}]} |
| 461 |
printf "${tst_rfc[${idx_dfn}]} (${tst_sht[${idx_dfn}]}) " |
| 462 |
done # !tst_idx |
| 463 |
printf "\n" |
| 464 |
fi # !dbg |
| 465 |
|
| 466 |
if [ "${fl_nbr}" -lt 1 ]; then |
| 467 |
echo "${spt_nm}: ERROR Checker did not receive any input files, fl_nbr = ${fl_nbr}" |
| 468 |
exit 1 |
| 469 |
fi # fl_nbr |
| 470 |
|
| 471 |
# Main Loop |
| 472 |
|
| 473 |
# Prepend input directory string to filename |
| 474 |
for ((fl_idx=0;fl_idx<fl_nbr;fl_idx++)); do |
| 475 |
fl_out[${fl_idx}]="${drc_out}/$(basename ${fl_in[${fl_idx}]})" |
| 476 |
#printf "fl_in[${fl_idx}] = ${fl_in[${fl_idx}]}\n" |
| 477 |
#printf "fl_out[${fl_idx}] = ${fl_out[${fl_idx}]}\n" |
| 478 |
|
| 479 |
printf "\nChecking file #${fl_idx}: ${fl_in[${fl_idx}]} ...\n" |
| 480 |
|
| 481 |
for ((tst_idx=0;tst_idx<tst_nbr;tst_idx++)); do |
| 482 |
|
| 483 |
let tst_idxp1=${tst_idx}+1 |
| 484 |
# Index of test in definition table |
| 485 |
idx_dfn=${map_r2d[${tst_idx}]} |
| 486 |
|
| 487 |
printf "\nTest ${tst_idxp1}: Check that ${tst_ngl[${idx_dfn}]} ...\n" |
| 488 |
printf "Recommendation ${tst_rfc[${idx_dfn}]}: ${tst_rcm[${idx_dfn}]}\n" |
| 489 |
echo "URL: ${tst_url[${idx_dfn}]}" |
| 490 |
[[ ${dbg_lvl} -ge 0 ]] && date_chr=$(date +"%s") |
| 491 |
|
| 492 |
cmd_tst[${fl_idx}]="ncks --dbg_lvl=${dbg_lvl} ${tst_opt[${idx_dfn}]} ${fl_in[${fl_idx}]}" |
| 493 |
|
| 494 |
if [ ${dbg_lvl} -ge 2 ]; then |
| 495 |
echo ${cmd_tst[${fl_idx}]} |
| 496 |
fi # !dbg |
| 497 |
if [ ${dbg_lvl} -le 1 ]; then |
| 498 |
if [ -z "${par_opt}" ]; then |
| 499 |
eval ${cmd_tst[${fl_idx}]} |
| 500 |
if [ "$?" -ne 0 ]; then |
| 501 |
printf "Result: FAILURE ${tst_sht[${idx_dfn}]} check failed. Reproduce this single check with:\n${cmd_tst[${fl_idx}]}\n" |
| 502 |
else # !err |
| 503 |
printf "Result: SUCCESS ${tst_sht[${idx_dfn}]} check passed.\n" |
| 504 |
fi # !err |
| 505 |
else # !par_opt |
| 506 |
eval ${cmd_bnd[${fl_idx}]} ${par_opt} # eval always returns 0 on backgrounded processes |
| 507 |
bnd_pid[${fl_idx}]=$! |
| 508 |
fi # !par_opt |
| 509 |
fi # !dbg |
| 510 |
|
| 511 |
if [ ${dbg_lvl} -ge 1 ]; then |
| 512 |
date_crr=$(date +"%s") |
| 513 |
date_dff=$((date_crr-date_chr)) |
| 514 |
echo "Elapsed time for ${tst_sht[${idx_dfn}]} check = $((date_dff/60))m$((date_dff % 60))s" |
| 515 |
fi # !dbg |
| 516 |
|
| 517 |
done # !tst_idx |
| 518 |
|
| 519 |
done # !fl_idx |
| 520 |
|
| 521 |
date_end=$(date +"%s") |
| 522 |
if [ ${dbg_lvl} -ge 0 ]; then |
| 523 |
printf "\nCompleted DIWG Compliance Checker at `date`\n" |
| 524 |
date_dff=$((date_end-date_srt)) |
| 525 |
echo "Elapsed time $((date_dff/60))m$((date_dff % 60))s" |
| 526 |
fi # !dbg |
| 527 |
|
| 528 |
exit 0 |