[88] | 1 | #! /bin/sh |
---|
| 2 | # |
---|
| 3 | #+ |
---|
| 4 | # |
---|
[201] | 5 | # .. program:: change_time_range.sh |
---|
[88] | 6 | # |
---|
[201] | 7 | # .. _change_time_range.sh: |
---|
[88] | 8 | # |
---|
[201] | 9 | # ======================= |
---|
| 10 | # change_time_range.sh |
---|
| 11 | # ======================= |
---|
[88] | 12 | # |
---|
| 13 | # SYNOPSIS |
---|
| 14 | # ======== |
---|
| 15 | # |
---|
| 16 | # :: |
---|
| 17 | # |
---|
[201] | 18 | # $ change_time_range.sh [--debug] -b yyyymmddb -e yyyymmdde -d directory |
---|
[88] | 19 | # |
---|
| 20 | # DESCRIPTION |
---|
| 21 | # =========== |
---|
| 22 | # |
---|
| 23 | # .. option:: --debug |
---|
| 24 | # |
---|
| 25 | # If this option is set, :samp:`ncdump -v tt` will be added to log file |
---|
| 26 | # |
---|
[201] | 27 | # .. option:: -b <yyyymmddb> |
---|
[88] | 28 | # |
---|
[201] | 29 | # first date to be written in the global attributes time_range |
---|
[88] | 30 | # |
---|
[201] | 31 | # .. option:: -e <yyyymmdde> |
---|
[88] | 32 | # |
---|
[201] | 33 | # second date to be written in the global attributes time_range |
---|
[88] | 34 | # |
---|
[201] | 35 | # .. option:: -d <dirin> |
---|
| 36 | # |
---|
| 37 | # base location of the dataset to be changed |
---|
| 38 | # |
---|
[202] | 39 | # Modify time_range global attribute in the dataset based under dirin parameter |
---|
[201] | 40 | # |
---|
| 41 | # Log file is written on :file:`${PROJECT_LOG}/change_time_range.sh.log.{YYYYMMDDTHHMMSSZ}` |
---|
| 42 | # |
---|
[88] | 43 | # .. graphviz:: |
---|
| 44 | # |
---|
[201] | 45 | # digraph change_time_range { |
---|
[88] | 46 | # |
---|
[201] | 47 | # change_time_range [shape=box, |
---|
[88] | 48 | # fontname=Courier, |
---|
| 49 | # color=blue, |
---|
[201] | 50 | # URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/change_time_range.sh", |
---|
| 51 | # label="${PROJECT}/src/change_time_range.sh"]; |
---|
[88] | 52 | # |
---|
[201] | 53 | # {filein} -> {change_time_range} -> {filein} |
---|
[88] | 54 | # |
---|
| 55 | # } |
---|
| 56 | # |
---|
| 57 | # EXAMPLES |
---|
| 58 | # ======== |
---|
| 59 | # |
---|
[201] | 60 | # To modify files under /usr/lodyc/incas/fplod/tropflux_d/to_be_published/:: |
---|
[88] | 61 | # |
---|
[201] | 62 | # $ change_time_range.sh -d /usr/lodyc/incas/fplod/tropflux_d/to_be_published/ -b 19790101 -e 20130331 |
---|
[88] | 63 | # |
---|
| 64 | # And look at log file with :: |
---|
| 65 | # |
---|
[201] | 66 | # $ tlogd.sh change_time_range |
---|
[88] | 67 | # |
---|
| 68 | # TIPS |
---|
| 69 | # ==== |
---|
| 70 | # |
---|
| 71 | # SEE ALSO |
---|
| 72 | # ======== |
---|
| 73 | # |
---|
[201] | 74 | # :ref:`updatedata` |
---|
| 75 | # |
---|
[88] | 76 | # :ref:`project_profile.sh` |
---|
| 77 | # |
---|
| 78 | # :func:`ncatted <nco:ncatted>` |
---|
| 79 | # |
---|
| 80 | # TODO |
---|
| 81 | # ==== |
---|
| 82 | # |
---|
| 83 | # coding rules |
---|
| 84 | # |
---|
[201] | 85 | # overwrite : may be an other option to avoid scratch |
---|
[88] | 86 | # |
---|
[201] | 87 | # debug not used |
---|
[88] | 88 | # |
---|
[202] | 89 | # terminology 1979_2013 hardcoded |
---|
[88] | 90 | # |
---|
[201] | 91 | # build time period according to time in the file instead of parameter |
---|
[88] | 92 | # |
---|
| 93 | # EVOLUTIONS |
---|
| 94 | # ========== |
---|
| 95 | # |
---|
| 96 | # $Id$ |
---|
| 97 | # |
---|
| 98 | # $URL$ |
---|
| 99 | # |
---|
[201] | 100 | # - fplod 20130726T112836Z cratos.locean-ipsl.upmc.fr (Linux) |
---|
[88] | 101 | # |
---|
[201] | 102 | # * creation |
---|
[88] | 103 | # |
---|
| 104 | #- |
---|
| 105 | system=$(uname) |
---|
| 106 | case "${system}" in |
---|
| 107 | AIX|IRIX64) |
---|
| 108 | echo "www : no specific posix checking" |
---|
| 109 | date_cmd=date |
---|
| 110 | ;; |
---|
| 111 | Darwin) |
---|
| 112 | set -o posix |
---|
| 113 | date_cmd=gdate |
---|
| 114 | ;; |
---|
| 115 | Linux) |
---|
| 116 | set -o posix |
---|
| 117 | date_cmd=date |
---|
| 118 | ;; |
---|
| 119 | *) |
---|
| 120 | set -o posix |
---|
| 121 | ;; |
---|
| 122 | esac |
---|
| 123 | unset system |
---|
| 124 | # |
---|
| 125 | LANG=POSIX |
---|
| 126 | # |
---|
[101] | 127 | set -u |
---|
| 128 | # |
---|
[88] | 129 | command=$(basename ${0}) |
---|
| 130 | log_date=$(date -u +"%Y%m%dT%H%M%SZ") |
---|
| 131 | # |
---|
[201] | 132 | usage=" Usage : ${command} [--debug] -d dirin -b yyyymmddb -e yyyymmdde" |
---|
[88] | 133 | # |
---|
| 134 | hostname=$(hostname) |
---|
| 135 | # |
---|
| 136 | # default |
---|
| 137 | debug=0 |
---|
| 138 | # |
---|
| 139 | minargcount=0 |
---|
| 140 | if [ ${#} -lt ${minargcount} ] |
---|
| 141 | then |
---|
| 142 | echo "${command} : eee : not enought arguments" |
---|
| 143 | echo "${usage}" |
---|
| 144 | exit 1 |
---|
| 145 | fi |
---|
| 146 | # |
---|
[101] | 147 | while [ ${#} -gt 0 ] |
---|
[88] | 148 | do |
---|
| 149 | case ${1} in |
---|
[201] | 150 | -b) |
---|
| 151 | yyyymmddb=${2} |
---|
| 152 | shift |
---|
| 153 | ;; |
---|
| 154 | -e) |
---|
| 155 | yyyymmdde=${2} |
---|
| 156 | shift |
---|
| 157 | ;; |
---|
| 158 | -d) |
---|
| 159 | dirin=${2} |
---|
| 160 | shift |
---|
| 161 | ;; |
---|
[88] | 162 | --debug) |
---|
| 163 | debug=1 |
---|
| 164 | ;; |
---|
| 165 | *) |
---|
| 166 | # anything else |
---|
| 167 | echo "${command} : eee : unknown option ${1}" |
---|
| 168 | echo "${command} : eee : ${usage}" |
---|
| 169 | exit 1 |
---|
| 170 | ;; |
---|
| 171 | esac |
---|
| 172 | # next flag |
---|
| 173 | shift |
---|
| 174 | done |
---|
| 175 | # |
---|
| 176 | # check parameters |
---|
[201] | 177 | if [ ! -d ${dirin} ] |
---|
[88] | 178 | then |
---|
[201] | 179 | echo " eee : ${dirin} not found" |
---|
[88] | 180 | exit 1 |
---|
| 181 | fi |
---|
| 182 | # |
---|
[201] | 183 | # check for permission on dirin |
---|
| 184 | if [ ! -x ${dirin} ] |
---|
[88] | 185 | then |
---|
[201] | 186 | echo " eee : ${dirin} not reachable" |
---|
[88] | 187 | exit 1 |
---|
| 188 | fi |
---|
[201] | 189 | #++ check yyyymmddb and yyyymmddb validity |
---|
[88] | 190 | # |
---|
| 191 | tool=ncatted |
---|
| 192 | type ${tool} 1> /dev/null 2>&1 |
---|
| 193 | status=${?} |
---|
| 194 | if [ ${status} -ne 0 ] |
---|
| 195 | then |
---|
[201] | 196 | echo "${command} : ${LINENO} : eee : tool ${tool} not found" |
---|
[88] | 197 | exit 1 |
---|
| 198 | fi |
---|
| 199 | unset status |
---|
| 200 | unset tool |
---|
| 201 | # |
---|
| 202 | # check for ${PROJECT_LOG} definition |
---|
| 203 | if [ "${PROJECT_LOG}" = "" ] |
---|
| 204 | then |
---|
[201] | 205 | echo "${command} : ${LINENO} : eee : \${PROJECT_LOG} not defined" |
---|
[88] | 206 | exit 1 |
---|
| 207 | fi |
---|
| 208 | # |
---|
| 209 | # check for ${PROJECT_LOG} existence |
---|
| 210 | if [ ! -d ${PROJECT_LOG} ] |
---|
| 211 | then |
---|
| 212 | echo "${command} : eee : ${PROJECT_LOG} not found" |
---|
| 213 | exit 1 |
---|
| 214 | fi |
---|
| 215 | # |
---|
| 216 | # check for permission access on PROJECT_LOG |
---|
| 217 | if [ ! -x ${PROJECT_LOG} ] |
---|
| 218 | then |
---|
| 219 | echo "${command} : eee : ${PROJECT_LOG} not reachable" |
---|
| 220 | exit 1 |
---|
| 221 | fi |
---|
| 222 | # |
---|
| 223 | # check for write permission on PROJECT_LOG |
---|
| 224 | if [ ! -w ${PROJECT_LOG} ] |
---|
| 225 | then |
---|
| 226 | echo "${command} : eee : ${PROJECT_LOG} not writable" |
---|
| 227 | exit 1 |
---|
| 228 | fi |
---|
| 229 | # |
---|
| 230 | log=${PROJECT_LOG}/$(basename ${0} .sh).log.${log_date} |
---|
| 231 | echo "[Context]" 1>> ${log} |
---|
| 232 | echo "command=$(basename ${0})" 1>>${log} |
---|
| 233 | echo "hostname=${hostname}" 1>> ${log} |
---|
| 234 | echo "runtime=${log_date}" 1>> ${log} |
---|
[188] | 235 | echo "log=${log}" 1>> ${log} |
---|
[88] | 236 | unset log_date |
---|
| 237 | # |
---|
| 238 | echo "" 1>> ${log} |
---|
| 239 | echo "[Parameters]" 1>> ${log} |
---|
[201] | 240 | echo "dirin=${dirin}" 1>> ${log} |
---|
| 241 | echo "yyyymmddb=${yyyymmddb}" 1>> ${log} |
---|
| 242 | echo "yyyymmdde=${yyyymmdde}" 1>> ${log} |
---|
[88] | 243 | echo "" 1>> ${log} |
---|
| 244 | # |
---|
[201] | 245 | # build list files |
---|
| 246 | varlist="lhf lwr netflux q2m shf sst swr t2m tau taux tauy ws" |
---|
| 247 | period_list="daily monthly" |
---|
| 248 | list_filein="" |
---|
| 249 | for var in $varlist |
---|
| 250 | do |
---|
| 251 | for period in ${period_list} |
---|
| 252 | do |
---|
| 253 | if [ "${period}" == "daily" ] |
---|
| 254 | then |
---|
| 255 | suffix=1d |
---|
| 256 | else |
---|
| 257 | suffix=1m |
---|
| 258 | fi |
---|
[202] | 259 | filein=${dirin}/${period}/${var}_tropflux_${suffix}_1979_2013.nc |
---|
[201] | 260 | if [ ! -f ${filein} ] |
---|
| 261 | then |
---|
| 262 | echo " eee : ${filein} not found" 1>> ${log} |
---|
| 263 | exit 1 |
---|
| 264 | else |
---|
| 265 | list_filein=${list_filein}" ${filein}" |
---|
| 266 | fi |
---|
| 267 | done |
---|
| 268 | unset period |
---|
| 269 | done |
---|
| 270 | unset period_list |
---|
| 271 | unset var |
---|
| 272 | unset varlist |
---|
[88] | 273 | # |
---|
[201] | 274 | # build time period |
---|
| 275 | datesec=$(${date_cmd} -u -d "${yyyymmddb}" +%s | awk '{printf "%10.10d",$1}') |
---|
| 276 | yyyyb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%Y) |
---|
| 277 | mmb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%m) |
---|
| 278 | ddb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%d) |
---|
| 279 | unset datesec |
---|
| 280 | datesec=$(${date_cmd} -u -d "${yyyymmdde}" +%s | awk '{printf "%10.10d",$1}') |
---|
| 281 | yyyye=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%Y) |
---|
| 282 | mme=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%m) |
---|
| 283 | dde=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%d) |
---|
| 284 | unset datesec |
---|
| 285 | time_ranged="${yyyymmddb} - ${yyyymmdde}" |
---|
| 286 | time_rangem="${yyyyb}${mmb}15 - ${yyyye}${mme}15" |
---|
[88] | 287 | # |
---|
[201] | 288 | # new time_range attribute |
---|
| 289 | for filein in ${list_filein} |
---|
| 290 | do |
---|
| 291 | echo "iii : change time_range in ${filein}" >> ${log} 2>&1 |
---|
| 292 | period=$(basename $(dirname $filein)) |
---|
| 293 | if [ "${period}" == "daily" ] |
---|
| 294 | then |
---|
| 295 | time_range=${time_ranged} |
---|
| 296 | else |
---|
| 297 | time_range=${time_rangem} |
---|
| 298 | fi |
---|
| 299 | ncatted -O -h -a time_range,global,o,c,"${time_range}" ${filein} >> ${log} 2>&1 |
---|
| 300 | status=${?} |
---|
| 301 | if [ ${status} -ne 0 ] |
---|
| 302 | then |
---|
| 303 | echo "eee : pb with ncatted" >> ${log} 2>&1 |
---|
| 304 | ncdump -h ${filein} >> ${log} 2>&1 |
---|
| 305 | exit 1 |
---|
| 306 | fi |
---|
| 307 | unset status |
---|
| 308 | ncdump -h ${filein} | grep time_range >> ${log} 2>&1 |
---|
| 309 | done |
---|
| 310 | unset filein |
---|
[88] | 311 | # |
---|
| 312 | # cleanning |
---|
[201] | 313 | #++ |
---|
[88] | 314 | #++ set |
---|
| 315 | # end |
---|
| 316 | exit 0 |
---|