source: trunk/src/change_time_range.sh

Last change on this file was 204, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo

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