Debian Codegraph @

data/nccf

raw
1 #!/bin/sh
2
3 # Purpose: CF-check netCDF3/netCDF4/HDF4/HDF5 files
4 # Dismember them first if necessary or requested
5 # Dismembering places each input file group in separate netCDF3 output file
6 # Described in NCO User Guide at http://nco.sf.net/nco.html#nccf
7 # Originally incarnated as ncdismember in NCO 4.3.6, September 2013
8 # Re-incarnated with getopt as nccf in NCO 4.5.1, July 2015
9 # Requirements: NCO 4.3.x+, UNIX shell utilities awk, grep, sed
10 # Optional: Decker CFchecker https://bitbucket.org/mde_/cfchecker
11 # Currently handles three checkers:
12 # NERC http://puma.nerc.ac.uk/cgi-bin/cf-checker.pl
13 # Decker checker cf
14 # IOOS checker (unsupported): https://data.ioos.us/compliance
15 # JPL MCC checker (unsupported, based on IOOS): http://podaac-uat.jpl.nasa.gov/mcc
16
17 # Usage:
18 # nccf -c cf_chk -f fl_in -i drc_in -o drc_out -n nco_opt -v cf_vrs
19 # where fl_in is input file/URL to check/dismember, drc_in/out are
20 # input and output directories, respectively
21 # CF-compliance check is performed by default, unless -c 'no' is given
22 # Default checker is Decker's cfchecker installed locally
23 # Specify cf_chk=nerc for smallified uploads to NERC checker
24 # Option cf_vrs is CF version to check
25 # Option nco_opt passes straight-through to ncks
26 # Arguments must not use shell expansion/globbing
27 # NB: nccf does not clean-up output directory, so user must
28 # chmod a+x ~/sh/nccf
29 # Examples:
30 # nccf ~/nco/data/mdl_1.nc /data/zender/tmp
31 # nccf http://dust.ess.uci.edu/nco/mdl_1.nc /tmp
32 # nccf http://thredds-test.ucar.edu/thredds/dodsC/testdods/foo.nc /tmp
33 # nccf ~/nco/data/mdl_1.nc /data/zender/nco/tmp cf
34 # nccf ~/nco/data/mdl_1.nc /data/zender/nco/tmp nerc
35 # nccf ~/nco/data/mdl_1.nc /data/zender/nco/tmp cf 1.3
36 # nccf ~/nco/data/mdl_1.nc /data/zender/nco/tmp cf 1.5 --fix_rec_dmn=all
37
38 # Set script name
39 spt_nm=`basename ${0}` # [sng] Script name
40
41 # Set fonts for legibility
42 fnt_nrm=`tput sgr0` # Normal
43 fnt_bld=`tput bold` # Bold
44 fnt_rvr=`tput smso` # Reverse
45
46 # Command-line argument defaults
47 fl_in="${HOME}/nco/data/mdl_1.nc" # [sng] Input file to dismember/check
48 drc_in='' # [sng] Input directory
49 drc_out="${DATA}/nco/tmp" # [sng] Output directory
50 #cf_chk='dck' # [sng] Checker
51 cf_chk='nerc' # [sng] Checker
52 cf_flg='Yes' # [flg] CF-check files?
53 cf_vrs='1.5' # [sng] Compliance-check this CF version (e.g., '1.5')
54 opt='' # [flg] Additional ncks options (e.g., '--fix_rec_dmn=all')
55 # Use single quotes to pass multiple arguments to opt=${5}
56 # Otherwise arguments would be seen as ${5}, ${6}, ${7} ...
57
58 function fnc_usg_prn {
59 # Print usage
60 printf "\nQuick documentation for ${fnt_bld}${spt_nm}${fnt_nrm} (read script for more thorough explanation)\n\n"
61 printf "${fnt_rvr}Basic usage:${fnt_nrm} ${fnt_bld}$spt_nm -c cf_chk -f fl_in -o drc_out${fnt_nrm}\n\n"
62 echo "Command-line options:"
63 echo "${fnt_rvr}-c${fnt_nrm} ${fnt_bld}cf_chk${fnt_nrm} CF checker to use (empty means none) (default ${fnt_bld}${cf_chk}${fnt_nrm})"
64 echo "${fnt_rvr}-d${fnt_nrm} ${fnt_bld}dbg_lvl${fnt_nrm} Debugging level (default ${fnt_bld}${dbg_lvl}${fnt_nrm})"
65 echo "${fnt_rvr}-f${fnt_nrm} ${fnt_bld}fl_in${fnt_nrm} Input file (default ${fnt_bld}${fl_in}${fnt_nrm})"
66 echo "${fnt_rvr}-i${fnt_nrm} ${fnt_bld}drc_in${fnt_nrm} Input directory ${fnt_bld}drc_in${fnt_nrm} (default ${fnt_bld}${drc_in}${fnt_nrm})"
67 echo "${fnt_rvr}-n${fnt_nrm} ${fnt_bld}nco_opt${fnt_nrm} NCO options (empty means none) (default ${fnt_bld}${nco_opt}${fnt_nrm})"
68 echo "${fnt_rvr}-o${fnt_nrm} ${fnt_bld}drc_out${fnt_nrm} Output directory (default ${fnt_bld}${drc_out}${fnt_nrm})"
69 echo "${fnt_rvr}-v${fnt_nrm} ${fnt_bld}cf_vrs${fnt_nrm} Version of CF to use (default ${fnt_bld}${cf_vrs}${fnt_nrm})"
70 printf "\n"
71 printf "Examples: ${fnt_bld}$spt_nm -c ${caseid} -s ${yyyy_srt} -e ${yyyy_end} -i ${drc_in} -o ${drc_out} ${fnt_nrm}\n"
72 printf "\n\n"
73 exit 1
74 } # end fnc_usg_prn()
75
76 # Check argument number and complain accordingly
77 arg_nbr=$#
78 #echo -e \\n"dbg: Number of arguments: ${arg_nbr}"
79 if [ ${arg_nbr} -eq 0 ]; then
80 fnc_usg_prn
81 fi # !arg_nbr
82
83 # Parse command-line options:
84 cmd_ln="${@}"
85 while getopts :c:d:f:hi:n:o::v: OPT; do
86 case ${OPT} in
87 c) cf_chk=${OPTARG} ;; # Checker to use
88 d) dbg_lvl=${OPTARG} ;; # Debugging level
89 f) fl_in=${OPTARG} ;; # Input file
90 i) drc_in=${OPTARG} ;; # Input directory
91 n) nco_opt=${OPTARG} ;; # NCO options
92 o) drc_out=${OPTARG} ;; # Output directory
93 v) cf_vrs=${OPTARG} ;; # CF Version
94 h) fnc_usg_prn ;; # Help
95 \?) # Unrecognized option
96 echo -e \\n"Option -${fnt_bld}$OPTARG${fnt_nrm} not allowed."
97 fnc_usg_prn ;;
98 esac
99 done
100 shift $((OPTIND-1)) # Advance one argument
101
102 # Derived variables
103 chk_dck='n'
104 chk_nrc='n'
105 if [ ${cf_chk} = 'nerc' ]; then
106 chk_nrc='y'
107 fi # chk_nrc
108 if [ ${cf_chk} != '0' ] && [ ${cf_chk} != 'nerc' ]; then
109 chk_dck='y'
110 hash cfchecker 2>/dev/null || { echo >&2 "Local cfchecker command not found, will smallify and upload to NERC checker instead"; chk_nrc='y'; chk_dck='n'; }
111 fi # !cf_chk
112
113 # Print initial state
114 if [ ${dbg_lvl} -ge 1 ]; then
115 printf "dbg: chk_dck = ${chk_dck}\n"
116 printf "dbg: chk_nerc = ${chk_nerc}\n"
117 printf "dbg: cf_chk = ${cf_chk}\n"
118 printf "dbg: cf_flg = ${cf_flg}\n"
119 printf "dbg: cf_vrs = ${cf_vrs}\n"
120 printf "dbg: dbg_lvl = ${dbg_lvl}\n"
121 printf "dbg: drc_in = ${drc_in}\n"
122 printf "dbg: drc_out = ${drc_out}\n"
123 printf "dbg: mdl_nm = ${mdl_nm}\n"
124 printf "dbg: nco_opt = ${nco_opt}\n"
125 fi # !dbg
126
127 # Human-readable summary
128 echo "Checking and/or dismembering file ${fl_in}"
129 if [ ${dbg_lvl} -ge 1 ]; then
130 printf "${spt_nm} invoked with command:\n"
131 echo "${spt_nm} ${cmd_ln}"
132 fi # !dbg
133 date_srt=$(date +"%s")
134 printf "Started at `date`.\n"
135
136 # Prepare and move-to output directory
137 fl_stb=$(basename ${fl_in})
138 drc_out=${drc_out}/${fl_stb}
139 mkdir -p ${drc_out}
140 cd ${drc_out}
141
142 # Obtain group list
143 grp_lst=`ncks --cdl -m ${fl_in} | grep '// group' | awk '{$1=$2=$3="";sub(/^ */,"",$0);print}'`
144 IFS=$'\n' # Change Internal-Field-Separator from <Space><Tab><Newline> to <Newline>
145 for grp_in in ${grp_lst} ; do
146 # Replace slashes by dots for output group filenames
147 grp_out=`echo ${grp_in} | sed 's/\///' | sed 's/\//./g'`
148 if [ "${grp_out}" = '' ]; then grp_out='root' ; fi
149 # Tell older NCO/netCDF if HDF4 with --hdf4 switch (signified by .hdf/.HDF suffix)
150 hdf4=`echo ${fl_in} | awk '{if(match(tolower($1),".hdf$")) hdf4="--hdf4"; print hdf4}'`
151 # Flatten to netCDF3, anchor, no history, no temporary file, padding, HDF4 flag, options
152 cmd="ncks -O -3 -G : -g ${grp_in}/ -h --no_tmp_fl --hdr_pad=40 ${hdf4} ${opt} ${fl_in} ${drc_out}/${grp_out}.nc"
153 # Use eval in case ${opt} contains multiple arguments separated by whitespace
154 eval ${cmd}
155 if [ ${chk_dck} = 'y' ]; then
156 # Decker checker needs Conventions <= 1.6
157 no_bck_sls=`echo ${drc_out}/${grp_out} | sed 's/\\\ / /g'`
158 ncatted -h -a Conventions,global,o,c,CF-${cf_vrs} ${no_bck_sls}.nc
159 else # !chk_dck
160 echo ${drc_out}/${grp_out}.nc
161 fi # !chk_dck
162 done
163 if [ ${chk_dck} = 'y' ]; then
164 echo 'Decker CFchecker reports CF-compliance of each group in flat netCDF3 format'
165 cfchecker -c ${cf_vrs} *.nc
166 fi
167 if [ ${chk_nrc} = 'y' ]; then
168 # Smallification and NERC upload from qdcf script by Phil Rasch (PJR)
169 echo 'Using remote CFchecker http://puma.nerc.ac.uk/cgi-bin/cf-checker.pl'
170 cf_lcn='http://puma.nerc.ac.uk/cgi-bin/cf-checker.pl'
171 for fl in ${drc_out}/*.nc ; do
172 fl_sml=${fl}
173 cf_out=${fl%.nc}.html
174 dmns=`ncdump -h ${fl_in} | sed -n -e '/dimensions/,/variables/p' | grep = | sed -e 's/=.*//'`
175 hyp_sml=''
176 for dmn in ${dmns}; do
177 dmn_lc=`echo ${dmn} | tr "[:upper:]" "[:lower:]"`
178 if [ ${dmn_lc} = 'lat' ] || [ ${dmn_lc} = 'latitude' ] || [ ${dmn_lc} = 'lon' ] || [ ${dmn_lc} = 'longitude' ] || [ ${dmn_lc} = 'time' ]; then
179 hyp_sml=`echo ${hyp_sml}" -d ${dmn},0"`
180 fi # !dmn_lc
181 done
182 # Create small version of input file by sampling only first element of lat, lon, time
183 ncks -O ${hyp_sml} ${fl} ${fl_sml}
184 # Send small file to NERC checker
185 curl --form cfversion=1.6 --form upload=@${fl_sml} --form press="Check%20file" ${cf_lcn} -o ${cf_out}
186 # Strip most HTML to improve readability
187 cat ${cf_out} | sed -e "s/<[^>]*>//g" -e "/DOCTYPE/,/\]\]/d" -e "s/CF-Convention//g" -e "s/Output of//g" -e "s/Compliance Checker//g" -e "s/Check another//g" -e "s/CF-Checker follows//g" -e "s/Received//g" -e "s/for NetCDF//g" -e "s/NetCDF format//g" -e "s/against CF version 1//g" -e "s/\.\.\.//g"
188 echo "Full NERC compliance-check log for ${fl} in ${cf_out}"
189 done
190 fi # !nerc
191
192 date_end=$(date +"%s")
193 printf "Completed climatology generation for model-run ${caseid} at `date`.\n"
194 date_dff=$((date_end-date_srt))
195 echo "Elapsed time $((date_dff/60))m$((date_dff % 60))s"
196
197 exit 0