Subversion Repositories lagranto.ecmwf

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 michaesp 1
#!/bin/csh
2
 
3
# -----------------------------------------------------------------------------------------------------------------
4
# Usage and parameter handling
5
# -----------------------------------------------------------------------------------------------------------------
6
 
7
# Write usage information
8
if ( ${#argv} == 0 ) then
9
  echo 
10
  ${LAGRANTO}/bin/lagrantohelp create_startf short
11
  echo
12
  exit 1
13
endif
14
 
15
# -----------------------------------------------------------------------------------------------------------------
16
# Handle input parameters
17
# -----------------------------------------------------------------------------------------------------------------
18
 
19
# Prefix of primary and secondary file; and set the filenames
20
set charp='P'
21
set chars='S'
22
 
23
# Write title
24
echo 
25
echo '========================================================='
26
echo '       *** START OF PREPROCESSOR CREATE_STARTF ***              '
27
echo
28
 
29
# Extract arguments and split reference date
30
set refdate   = $1
31
set ofile     = $2
32
set specifier = `echo $3` 
33
shift
34
shift
35
shift
36
 
37
# Check whether specifier is a file or whether it is explicitly written
38
set flag_criterion = 'file'
39
set test = `echo ${specifier} | grep '@' | wc -c`
40
if ( "${test}" != "0" ) then 
41
  set flag_criterion = 'criterion'
42
endif
43
 
44
# Get the criterion from the file
45
if ( "${flag_criterion}" == "file" ) then
46
    if ( -f ${specifier} ) then
47
       set filename  = ${specifier}
48
       set specifier = `cat ${specifier}`
49
    else
50
       echo " ERROR: cannot read criterion from file ${specifier}... Stop"
51
       exit 1
52
    endif
53
endif
54
 
55
echo "---- INPUT PARAMETERS ----------------------------------"
56
echo 
57
echo "Reference date        : ${refdate}"
58
if ( "${flag_criterion}" == "criterion" ) then 
59
   echo "Specifier             : ${specifier}"
60
else
61
   echo "Specifier             : ${specifier} [from file ${filename}]"
62
endif
63
echo "Output file           : ${ofile}"
64
echo
65
 
66
# Handle optional arguments
67
set tvfile    = 'tracevars'
68
set changet   = 'false'
69
set noclean   = 'false'
70
set regionf   = 'regionf'
71
set timecheck = 'no' 
72
 
73
while ( $#argv > 0 )
74
 
75
  switch ( $argv[1] )
76
 
77
   case -t
78
     set tvfile = $argv[2]
79
     echo "tvfile                -> ${tvfile} (user defined)"
80
     shift;
81
   breaksw
82
 
83
   case -changet
84
     set changet = 'true'
85
     echo "changet               -> true (user defined)"
86
   breaksw
87
 
88
   case -noclean
89
     set noclean = 'true'
90
     echo "noclean               -> true (user defined)"
91
   breaksw
92
 
93
   case -timecheck
94
     set timecheck = 'yes'
95
     echo "timecheck               -> yes (user defined)"
96
   breaksw
97
 
98
   case -regionf
99
     set regionf = $argv[2]
100
     echo "regionf                -> ${regionf} (user defined)"
101
     shift;
102
   breaksw
103
 
104
   endsw
105
 
106
   shift;
107
 
108
end
109
 
110
# No change of times necessary if no check requested
111
if ( "${timecheck}" == "no" ) then
112
   set  changet = 'false'
113
endif
114
 
115
# Split the reference date
116
set yyyy=`echo ${refdate}   | cut -c 1-4` 
117
set   mm=`echo ${refdate}   | cut -c 5-6` 
118
set   dd=`echo ${refdate}   | cut -c 7-8` 
119
set   hh=`echo ${refdate}   | cut -c 10-11` 
120
set  min=`echo ${refdate}00 | cut -c 12-13` 
121
 
122
# Set base directories (run+prog)
123
set tradir=${PWD}
124
 
125
# Set program paths and filenames 
126
set parfile=${tradir}/create_startf.param 
127
set crifile=${tradir}/create_startf.criterion
128
 
129
# Decide whether a tracing and selection is necessary
130
# If so, some intermediate file are writen (in Fortran binary)
131
set flag = `${LAGRANTO}/startf/create_startf.perl "${specifier}" | tail -1` 
132
if ( "${flag}" != "nil" ) then
133
    set format1 = ".3"
134
    set format2 = ".3"
135
else
136
    set format1 = ""
137
    set format2 = ""
138
endif
139
 
140
# Write status information
141
echo
142
echo '---- DIRECTORIES AND PROGRAMS ---------------------------'
143
echo    
144
echo "PROGRAM CREATE_STARTF : ${LAGRANTO}/startf/create_startf"
145
echo "PARAMETER file        : ${parfile}"
146
echo "CRITERION file        : ${crifile}"
147
echo "RUN directory         : ${tradir}"
148
echo
149
 
150
# -----------------------------------------------------------------------------------------------------------------
151
# Set the primary and scecondary data files (necessary for interpolation if intermediate reference date)
152
# -----------------------------------------------------------------------------------------------------------------
153
 
154
# Find a first data file (if possible corresponding to reference date)
155
set file=${charp}${yyyy}${mm}${dd}_${hh}
156
if ( ( -f ${file} ) && ( ${min} == 0 ) ) then
157
   set timeshift=0
158
   set date0=${yyyy}${mm}${dd}_${hh}
159
   set date1=${yyyy}${mm}${dd}_${hh}
160
   set pfile0=${file1}
161
   set pfile1=${file}
162
   goto label3
163
else
164
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
165
endif
166
 
167
# Determine time increment (in hours) between data files
168
set date1=`echo $file | cut -c 2-12`
169
set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
170
@ n ++
171
set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-12`
172
set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
173
endif
174
if ( $timeinc == 0 ) then
175
    echo " ERROR: cannot set the time increment between input files ***"
176
    exit 1
177
endif
178
 
179
# Search the first file to use
180
set timeshift=
181
foreach i ( ${charp}????????_?? )
182
 
183
  set date0 = `echo $i | cut -c 2-12`
184
  set td1  = `${LAGRANTO}/goodies/gettidiff ${yyyy}${mm}${dd}_${hh} ${date0}`
185
 
186
  if ( ( $td1 >= 0 ) && ( $td1 < $timeinc ) )  then
187
      set timeshift=$td1
188
      set pfile0=${charp}${date0}
189
      goto label2
190
  endif
191
 
192
end
193
 
194
# Check if no P files are available for the specified time period
195
if ( $timeshift == "" ) then
196
  echo " ERROR: no data files available for the specified reference date"
197
  exit 1
198
endif
199
 
200
# Everything is fine so far: proceed
201
label2:
202
 
203
# Set the next date and check its availability
204
if ( ( ${timeshift} != 0 ) || ( ${min} > 0 ) ) then
205
    set date1=`${LAGRANTO}goodies/newtime $date0 $timeinc`
206
    if ( ! -f ${charp}${date1} ) then
207
       echo " ERROR: file with primary data is missing for $date1"
208
       exit 1
209
    else
210
       set pfile1=${charp}${date1}
211
    endif
212
else
213
    set date1=${date0}
214
    set pfile1=${pfile0}
215
endif
216
 
217
# Set the final timeshift 
218
if ( ${min} != 00 ) then
219
    set timeshift=${timeshift}.${min}
220
endif
221
 
222
# Everything is fine!
223
label3:
224
 
225
# Set secondary files and check their availability
226
set sfile0=${chars}${date0}
227
set sfile1=${chars}${date1}
228
 
229
# Write status information
230
echo '---- DATA FILES -----------------------------------------'
231
echo    
232
echo "Primary files                         : ${pfile0}"
233
echo "                                      : ${pfile1}"
234
echo "Secondary files                       : ${sfile0}"
235
echo "                                      : ${sfile1}"
236
echo "Timeshift to first data file (hh.mm)  : ${timeshift}"
237
echo "Time increment of data files          : ${timeinc}"
238
echo
239
 
240
# --------------------------------------------------------------------------------------------------------------
241
# Create the start positions (without selection)
242
# -----------------------------------------------------------------------------------------------------------------
243
 
244
# Set times relative to the reference date
245
if ( "${changet}" == "true" ) then
246
  echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
247
  echo   
248
  ${LAGRANTO}/goodies/changet.sh ${refdate} ${pfile0}
249
  ${LAGRANTO}/goodies/changet.sh ${refdate} ${pfile1}
250
  if ( -f  ${sfile0} ) then
251
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${sfile0}
252
  endif
253
  if ( -f  ${sfile1} ) then
254
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${sfile1}
255
  endif
256
endif
257
 
258
# Write parameters to parameter file and create the starting positions
259
\rm -f ${parfile}
260
echo \"${pfile0}\" \"${pfile1}\"    >! ${parfile}
261
echo \"${sfile0}\" \"${sfile1}\"    >> ${parfile}
262
echo \"${ofile}${format1}\"         >> ${parfile}
263
echo \"${regionf}\"                 >> ${parfile}
264
echo ${yyyy}                        >> ${parfile}
265
echo ${mm}                          >> ${parfile}
266
echo ${dd}                          >> ${parfile}
267
echo ${hh}                          >> ${parfile}
268
echo ${min}                         >> ${parfile}
269
echo 00                             >> ${parfile}
270
echo ${timeshift}                   >> ${parfile}
271
echo ${timeinc}                     >> ${parfile}
272
 
273
# Analyse the specifier and append to parameter file
274
${LAGRANTO}/startf/create_startf.perl "${specifier}" >> ${parfile}
275
 
276
if ( "${status}" != "0" ) then
277
  echo "ERROR:  Preprocessor <create_startf> failed"
278
  exit 1
279
endif
280
 
281
# Write selection criterion to file
282
\rm -f ${crifile}
283
tail -1 ${parfile} >! ${crifile} 
284
 
285
# Write flag for no time check
286
echo \"${timecheck}\"               >> ${parfile}
287
 
288
# Write title
289
echo 
290
echo '       *** END OF PREPROCESSOR CREATE_STARTF ***'        
291
echo '========================================================='
292
echo
293
 
294
# Create the startf
295
cd ${tradir}
296
${LAGRANTO}/startf/create_startf
297
 
298
if ( "${status}" != "0" ) then
299
  echo "ERROR:  Program <create_startf> failed"
300
  exit 1
301
endif
302
 
303
# --------------------------------------------------------------------------------------------------------------
304
# Apply selection (first tracing then selection)
305
# --------------------------------------------------------------------------------------------------------------
306
 
307
# Stop if no tracing and selection is necessary
308
if ( "${flag}" == "nil" ) goto finish
309
 
310
# Tracing of extra variables
311
if ( -f  ${ofile}${format2} ) then
312
  \rm -f ${ofile}${format2}
313
endif
314
if ( "${timecheck}" == "no" ) then
315
  ${LAGRANTO}/trace/trace.sh ${ofile}${format1} ${ofile}${format2} -v ${tvfile} -notimecheck
316
else
317
  ${LAGRANTO}/trace/trace.sh ${ofile}${format1} ${ofile}${format2} -v ${tvfile}
318
endif
319
\rm -f ${ofile}${format1}
320
 
321
# Selection
322
if ( -f  ${ofile} ) then
323
  \rm -f ${ofile}
324
endif
325
${LAGRANTO}/select/select.sh ${ofile}${format2} ${ofile} `cat ${crifile}` 
326
\rm -f ${ofile}${format2}
327
 
328
# --------------------------------------------------------------------------------------------------------------
329
# Final tasks (make clean)
330
# --------------------------------------------------------------------------------------------------------------
331
 
332
finish:
333
 
334
echo $noclean
335
 
336
if ( "${noclean}" == "false" ) then
337
  \rm -f ${crifile}
338
  \rm -f ${parfile}
339
 endif
340
 
341
exit 0 
342
 
343
 
344