source: trunk/src/change_time_range.sh @ 202

Last change on this file since 202 was 202, checked in by pinsard, 11 years ago

end of 20130725 update data

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id URL
File size: 6.4 KB
RevLine 
[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#-
105system=$(uname)
106case "${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   ;;
122esac
123unset system
124#
125LANG=POSIX
126#
[101]127set -u
128#
[88]129command=$(basename ${0})
130log_date=$(date -u +"%Y%m%dT%H%M%SZ")
131#
[201]132usage=" Usage : ${command} [--debug] -d dirin -b yyyymmddb -e yyyymmdde"
[88]133#
134hostname=$(hostname)
135#
136# default
137debug=0
138#
139minargcount=0
140if [ ${#} -lt ${minargcount} ]
141then
142   echo "${command} : eee : not enought arguments"
143   echo "${usage}"
144   exit 1
145fi
146#
[101]147while [ ${#} -gt 0 ]
[88]148do
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
174done
175#
176# check parameters
[201]177if [ ! -d ${dirin} ]
[88]178then
[201]179   echo " eee : ${dirin} not found"
[88]180   exit 1
181fi
182#
[201]183# check for permission on dirin
184if [ ! -x ${dirin} ]
[88]185then
[201]186   echo " eee : ${dirin} not reachable"
[88]187   exit 1
188fi
[201]189#++ check yyyymmddb and yyyymmddb validity
[88]190#
191tool=ncatted
192type ${tool} 1> /dev/null 2>&1
193status=${?}
194if [ ${status} -ne 0 ]
195then
[201]196   echo "${command} : ${LINENO} : eee : tool ${tool} not found"
[88]197   exit 1
198fi
199unset status
200unset tool
201#
202# check for ${PROJECT_LOG} definition
203if [ "${PROJECT_LOG}" = "" ]
204then
[201]205   echo "${command} : ${LINENO} : eee : \${PROJECT_LOG} not defined"
[88]206   exit 1
207fi
208#
209# check for ${PROJECT_LOG} existence
210if [ ! -d ${PROJECT_LOG} ]
211then
212   echo "${command} : eee : ${PROJECT_LOG} not found"
213   exit 1
214fi
215#
216# check for permission access on PROJECT_LOG
217if [ ! -x ${PROJECT_LOG} ]
218then
219   echo "${command} : eee : ${PROJECT_LOG} not reachable"
220   exit 1
221fi
222#
223# check for write permission on PROJECT_LOG
224if [ ! -w ${PROJECT_LOG} ]
225then
226   echo "${command} : eee : ${PROJECT_LOG} not writable"
227   exit 1
228fi
229#
230log=${PROJECT_LOG}/$(basename ${0} .sh).log.${log_date}
231echo "[Context]" 1>> ${log}
232echo "command=$(basename ${0})" 1>>${log}
233echo "hostname=${hostname}" 1>> ${log}
234echo "runtime=${log_date}" 1>> ${log}
[188]235echo "log=${log}" 1>> ${log}
[88]236unset log_date
237#
238echo "" 1>> ${log}
239echo "[Parameters]" 1>> ${log}
[201]240echo "dirin=${dirin}" 1>> ${log}
241echo "yyyymmddb=${yyyymmddb}" 1>> ${log}
242echo "yyyymmdde=${yyyymmdde}" 1>> ${log}
[88]243echo "" 1>> ${log}
244#
[201]245# build list files
246varlist="lhf lwr netflux q2m shf sst swr t2m tau taux tauy ws"
247period_list="daily monthly"
248list_filein=""
249for var in $varlist
250do
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
269done
270unset period_list
271unset var
272unset varlist
[88]273#
[201]274# build time period
275datesec=$(${date_cmd} -u -d "${yyyymmddb}" +%s | awk '{printf "%10.10d",$1}')
276yyyyb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%Y)
277mmb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%m)
278ddb=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%d)
279unset datesec
280datesec=$(${date_cmd} -u -d "${yyyymmdde}" +%s | awk '{printf "%10.10d",$1}')
281yyyye=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%Y)
282mme=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%m)
283dde=$(${date_cmd} -u -d "1970-01-01 ${datesec} sec" +%d)
284unset datesec
285time_ranged="${yyyymmddb} - ${yyyymmdde}"
286time_rangem="${yyyyb}${mmb}15 - ${yyyye}${mme}15"
[88]287#
[201]288# new time_range attribute
289for filein in ${list_filein}
290do
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
309done
310unset filein
[88]311#
312# cleanning
[201]313#++
[88]314#++ set
315# end
316exit 0
Note: See TracBrowser for help on using the repository browser.