source: trunk/src/change_time_range.sh @ 201

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

add some update data tools

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Id URL
File size: 6.4 KB
Line 
1#! /bin/sh
2#
3#+
4#
5# .. program:: change_time_range.sh
6#
7# .. _change_time_range.sh:
8#
9# =======================
10# change_time_range.sh
11# =======================
12#
13# SYNOPSIS
14# ========
15#
16# ::
17#
18#  $ change_time_range.sh [--debug] -b yyyymmddb -e yyyymmdde -d directory
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#
27# .. option:: -b <yyyymmddb>
28#
29#    first date to be written in the global attributes time_range
30#
31# .. option:: -e <yyyymmdde>
32#
33#    second date to be written in the global attributes time_range
34#
35# .. option:: -d <dirin>
36#
37#    base location of the dataset to be changed
38#
39# Modify time_range global attribute if the dataset based under dirin parameter
40#
41# Log file is written on :file:`${PROJECT_LOG}/change_time_range.sh.log.{YYYYMMDDTHHMMSSZ}`
42#
43#     .. graphviz::
44#
45#        digraph change_time_range {
46#
47#           change_time_range [shape=box,
48#           fontname=Courier,
49#           color=blue,
50#           URL="http://forge.ipsl.jussieu.fr/tropflux/browser/trunk/src/change_time_range.sh",
51#           label="${PROJECT}/src/change_time_range.sh"];
52#
53#           {filein} -> {change_time_range} -> {filein}
54#
55#       }
56#
57# EXAMPLES
58# ========
59#
60# To modify files under /usr/lodyc/incas/fplod/tropflux_d/to_be_published/::
61#
62#  $ change_time_range.sh -d /usr/lodyc/incas/fplod/tropflux_d/to_be_published/ -b 19790101 -e 20130331
63#
64# And look at log file with ::
65#
66#  $ tlogd.sh change_time_range
67#
68# TIPS
69# ====
70#
71# SEE ALSO
72# ========
73#
74# :ref:`updatedata`
75#
76# :ref:`project_profile.sh`
77#
78# :func:`ncatted <nco:ncatted>`
79#
80# TODO
81# ====
82#
83# coding rules
84#
85# overwrite : may be an other option to avoid scratch
86#
87# debug not used
88#
89# terminology march2013 non normative
90#
91# build time period according to time in the file instead of parameter
92#
93# EVOLUTIONS
94# ==========
95#
96# $Id$
97#
98# $URL$
99#
100# - fplod 20130726T112836Z cratos.locean-ipsl.upmc.fr (Linux)
101#
102#   * creation
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#
127set -u
128#
129command=$(basename ${0})
130log_date=$(date -u +"%Y%m%dT%H%M%SZ")
131#
132usage=" Usage : ${command} [--debug] -d dirin -b yyyymmddb -e yyyymmdde"
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#
147while [ ${#} -gt 0 ]
148do
149   case ${1} in
150      -b)
151         yyyymmddb=${2}
152         shift
153      ;;
154      -e)
155         yyyymmdde=${2}
156         shift
157      ;;
158      -d)
159         dirin=${2}
160         shift
161      ;;
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
177if [ ! -d ${dirin} ]
178then
179   echo " eee : ${dirin} not found"
180   exit 1
181fi
182#
183# check for permission on dirin
184if [ ! -x ${dirin} ]
185then
186   echo " eee : ${dirin} not reachable"
187   exit 1
188fi
189#++ check yyyymmddb and yyyymmddb validity
190#
191tool=ncatted
192type ${tool} 1> /dev/null 2>&1
193status=${?}
194if [ ${status} -ne 0 ]
195then
196   echo "${command} : ${LINENO} : eee : tool ${tool} not found"
197   exit 1
198fi
199unset status
200unset tool
201#
202# check for ${PROJECT_LOG} definition
203if [ "${PROJECT_LOG}" = "" ]
204then
205   echo "${command} : ${LINENO} : eee : \${PROJECT_LOG} not defined"
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}
235echo "log=${log}" 1>> ${log}
236unset log_date
237#
238echo "" 1>> ${log}
239echo "[Parameters]" 1>> ${log}
240echo "dirin=${dirin}" 1>> ${log}
241echo "yyyymmddb=${yyyymmddb}" 1>> ${log}
242echo "yyyymmdde=${yyyymmdde}" 1>> ${log}
243echo "" 1>> ${log}
244#
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
259         filein=${dirin}/${period}/${var}_tropflux_${suffix}_1979_march2013.nc
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
273#
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"
287#
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
311#
312# cleanning
313#++
314#++ set
315# end
316exit 0
Note: See TracBrowser for help on using the repository browser.