Subversion Repositories lagranto.um

Rev

Rev 13 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7 michaesp 1
#!/bin/csh
2
 
15 michaesp 3
# Set Lagranto 
4
set LAGRANTO = ${LAGRANTOBASE}.${MODEL}/
5
 
7 michaesp 6
# -----------------------------------------------------------------------------------------------------------------
7
# Usage and parameter handling
8
# -----------------------------------------------------------------------------------------------------------------
9
 
10
# Write usage information
11
if ( ${#argv} == 0 ) then
12
  echo 
13
  echo "-----------------------------------------------------------------------------------------------------------"
14
  echo "Usage: startf <date> <ofile> <specifier> [ -t tvfile ] [ -changet ] [-noclean]"
15
  echo 
16
  echo "      date      : Date of input P and S file (e.g. 20100101_00)"
17
  echo "      ofile     : Output file with start points (e.g. startf)"
18
  echo "      tvfile    : Tracing file with variables for selection criteria "
19
  echo "      changet   : Flag whether to change times on P,S files before calculation"
20
  echo "      clean     : Flag whether to remove temporary files"
21
  echo "      specifier : Detailed description of starting positions"
22
  echo 
23
  echo "The specification <specifier> is of the form:"
24
  echo
25
  echo "      <specifier>  = <horizontal> @ <vertical> @ <unit> @ <selection>"
26
  echo 
27
  echo "      <horizontal> = file[filename]                  : Read lat/lon from file"
28
  echo "                     line[lon1,lon2,lat1,lat2,n]     : n points from (lon1,la1) to (lon2,lat2)"
29
  echo "                     box.eqd[lon1,lon2,lat1,lat2,ds] : lat/lon box with equdistant points (ds in [km])"
30
  echo "                     box.grid[lon1,lon2,lat1,lat2,]  : lat/lon box with grid points"
31
  echo "                     point[lon,lat]                  : Single lon/lat point"
32
  echo "                     shift[lon,lat,dlon,dlat]        : lon/at points and dlon/dlat shifhted ones"
33
  echo "                     polygon.eqd[filename,ds]        : Equidistant within arbirtrary polygon (ds in [km])"
34
  echo "                     polygon.grid[filename]          : Grid points within arbirtrary polygon"
35
  echo 
36
  echo "                     Polygon file is of the format   : lat/lon within polygon"
37
  echo "                                                       lat1,lon1"
38
  echo "                                                       lat2,lon2"
39
  echo "                                                       ..."
40
  echo
41
  echo "      <vertical>   = file[filename]                  : Read lev from file"
42
  echo "                   = level[lev]                      : Start from single level"
43
  echo "                   = list[l1v1,lev2,lev3,...]        : List of levels"
44
  echo "                   = profile[lev1,lev2,n]            : n equdistant levels betwenn lev1 and lev2"
45
  echo
46
  echo "      <unit>       = hPa                             : Pressure [hPa]"
47
  echo "                   = hPa,agl                         : Pressure above ground [hPa]"
48
  echo "                   = K                               : Isentropic level [K]"
49
  echo "                   = PVU                             : PV surface [PVU]"
50
  echo 
51
  echo "      <selection>  = same syntax as for the programme <select>"
52
  echo
53
  echo "      If <vertical=nil>, then <horizontal> must be equal to <file> and all lat/lon/lev coordinates are"
54
  echo "      read from a file. "
55
  echo 
56
  echo "      Example: 'box.eqd[10,30,30,70,50] @ profile[100,500,10] @ hPa @ GT:PV:2'"
57
  echo "------------------------------------------------------------------------------------------------------------"
58
  exit 1
59
endif
60
 
61
# -----------------------------------------------------------------------------------------------------------------
62
# Handle input parameters
63
# -----------------------------------------------------------------------------------------------------------------
64
 
65
# Prefix of primary and secondary file; and set the filenames
66
set charp='P'
67
set chars='S'
68
 
69
# Write title
70
echo 
71
echo '========================================================='
72
echo '       *** START OF PREPROCESSOR STARTF ***              '
73
echo
74
 
75
# Extract arguments and split reference date
76
set refdate   = $1
77
set ofile     = $2
78
set specifier = `echo $3` 
79
shift
80
shift
81
shift
82
 
83
# Check whether specifier is a file or whether it is explicitly written
84
set flag_criterion = 'file'
85
set test = `echo ${specifier} | grep '@' | wc -c`
86
if ( "${test}" != "0" ) then 
87
  set flag_criterion = 'criterion'
88
endif
89
 
90
# Get the criterion from the file
91
if ( "${flag_criterion}" == "file" ) then
92
    if ( -f ${specifier} ) then
93
       set filename  = ${specifier}
94
       set specifier = `cat ${specifier}`
95
    else
96
       echo " ERROR: cannot read criterion from file ${specifier}... Stop"
97
       exit 1
98
    endif
99
endif
100
 
101
echo "---- INPUT PARAMETERS ----------------------------------"
102
echo 
103
echo "Reference date        : ${refdate}"
104
if ( "${flag_criterion}" == "criterion" ) then 
105
   echo "Specifier             : ${specifier}"
106
else
107
   echo "Specifier             : ${specifier} [from file ${filename}]"
108
endif
109
echo "Output file           : ${ofile}"
110
echo
111
 
112
# Handle optional arguments
113
set tvfile    = 'tracevars'
114
set changet   = 'false'
115
set noclean   = 'false'
116
set regionf   = 'regionf'
117
set timecheck = 'no' 
118
 
119
while ( $#argv > 0 )
120
 
121
  switch ( $argv[1] )
122
 
123
   case -t
124
     set tvfile = $argv[2]
125
     echo "tvfile                -> ${tvfile} (user defined)"
126
     shift;
127
   breaksw
128
 
129
   case -changet
130
     set changet = 'true'
131
     echo "changet               -> true (user defined)"
132
   breaksw
133
 
134
   case -noclean
135
     set noclean = 'true'
136
     echo "noclean               -> true (user defined)"
137
   breaksw
138
 
139
   case -notimecheck
140
     set timecheck = 'no'
141
     echo "timecheck               -> no (user defined)"
142
   breaksw
143
 
144
   case -regionf
145
     set regionf = $argv[2]
146
     echo "regionf                -> ${regionf} (user defined)"
147
     shift;
148
   breaksw
149
 
150
   endsw
151
 
152
   shift;
153
 
154
end
155
 
156
# No change of times necessary if no check requested
157
if ( "${timecheck}" == "no" ) then
158
   set  changet = 'false'
159
endif
160
 
161
# Split the reference date
162
set yyyy=`echo ${refdate}   | cut -c 1-4` 
163
set   mm=`echo ${refdate}   | cut -c 5-6` 
164
set   dd=`echo ${refdate}   | cut -c 7-8` 
165
set   hh=`echo ${refdate}   | cut -c 10-11` 
166
set  min=`echo ${refdate}00 | cut -c 12-13` 
167
 
168
# Set base directories (run+prog)
169
set tradir=${PWD}
170
 
171
# Set program paths and filenames 
172
set parfile=${tradir}/startf.param 
173
set crifile=${tradir}/startf.criterion
174
 
175
# Decide whether a tracing and selection is necessary
176
# If so, some intermediate file are writen (in Fortran binary)
177
set flag_criterion = `${LAGRANTO}/startf/startf.perl "${specifier}" | tail -1` 
178
if ( "${flag_criterion}" != "nil" ) then
179
    set format1 = ".3"
180
    set format2 = ".3"
181
else
182
    set format1 = ""
183
    set format2 = ""
184
endif
185
 
186
# Write status information
187
echo
188
echo '---- DIRECTORIES AND PROGRAMS ---------------------------'
189
echo    
190
echo "PROGRAM CREATE_STARTF : ${LAGRANTO}/startf/startf"
191
echo "PARAMETER file        : ${parfile}"
192
echo "CRITERION file        : ${crifile}"
193
echo "RUN directory         : ${tradir}"
194
echo
195
 
196
# -----------------------------------------------------------------------------------------------------------------
197
# Set the primary and secondary data files (necessary for interpolation if intermediate reference date)
198
# -----------------------------------------------------------------------------------------------------------------
199
 
200
# Find a first data file (if possible corresponding to reference date) -> <file>
201
set file=${charp}${yyyy}${mm}${dd}_${hh}
202
if ( ( -f ${file} ) && ( ${min} == 0 ) ) then
203
   set timeshift=0
204
   set date0=${yyyy}${mm}${dd}_${hh}
205
   set date1=${yyyy}${mm}${dd}_${hh}
206
   set pfile0=${file}
207
   set pfile1=${file}
208
   goto label3
209
else
210
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
211
endif
212
 
213
# Determine time increment (in hours) between data files -> <timeinc>
214
set date1=`echo $file | cut -c 2-`
215
set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
216
@ n ++
217
set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-`
218
set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
219
if ( $timeinc == 0 ) then
220
    echo " ERROR: cannot set the time increment between input files ***"
221
    exit 1
222
endif
223
 
224
# Decide whether the time increment is smaller than 1 h; if so set the hires 
225
# flag and convert the time increment into fractional hour
226
set flag = `echo ${timeinc} | grep -n '\.'`
227
set hires = 0
228
 
229
if ( "${flag}" != "" ) then
230
  echo "Input files availability < 1 hour ($timeinc): switch to hires"
231
  echo "mode. All input files must be of form: P{yyyymmdd_hhmm}"
232
  set hires = 1
233
  set timeinc_min = `echo "(${timeinc} * 100 + 0.5)/1" | bc`
13 michaesp 234
  set timeinc     = `echo "scale=8; ${timeinc} * 100 / 60" | bc`
7 michaesp 235
  goto hires
236
endif
237
 
238
# Search the first file to use
239
set timeshift=
240
foreach i ( ${charp}????????_?? )
241
 
242
  set date0 = `echo $i | cut -c 2-12`
243
  set td1  = `${LAGRANTO}/goodies/gettidiff ${yyyy}${mm}${dd}_${hh} ${date0}`
244
 
245
  if ( ( $td1 >= 0 ) && ( $td1 < $timeinc ) )  then
246
      set timeshift=$td1
247
      set pfile0=${charp}${date0}
248
      goto label2
249
  endif
250
 
251
end
252
 
253
# Check if no P files are available for the specified time period
254
if ( $timeshift == "" ) then
255
  echo " ERROR: no data files available for the specified reference date"
256
  exit 1
257
endif
258
 
259
# If the input files come with time increment < 1 hour, handle it in this
260
# special section. The aim is not to 'interfere' with the well established
261
# code handling the standard case with time increment >= 1 hour. In this mode
262
# we require that the start and end date are available as P files: no start is
263
# allowed between the model output files
264
hires:
265
 
266
set flag = "nil"
267
set td = 0
268
 
269
set timeshift = 0
270
set date0 = ${yyyy}${mm}${dd}_${hh}${min}
271
set date1 = `${LAGRANTO}/goodies/newtime ${date0} ${timeinc_min}`
272
 
273
set pfile0 = ${charp}${date0}
274
set pfile1 = ${charp}${date1}
275
 
276
if ( ! -f ${pfile0} ) then
277
  echo
278
  echo " ERROR: no data files available for the specified time period (hires,$date0)"
279
  exit 1
280
endif 
281
if ( ! -f ${pfile1} ) then
282
  echo
283
  echo " ERROR: no data files available for the specified time period (hires,$date1)"
284
  exit 1
285
endif 
286
 
287
goto label3
288
 
289
# Everything is fine so far: proceed
290
label2:
291
 
292
# Set the next date and check its availability
293
if ( ( ${timeshift} != 00 ) || ( ${min} > 0 ) ) then
294
    set date1=`${LAGRANTO}/goodies/newtime $date0 $timeinc`
295
    if ( ! -f ${charp}${date1} ) then
296
       echo " ERROR: file with primary data is missing for $date"
297
       exit 1
298
    else
299
       set pfile1=${charp}${date1}
300
    endif
301
else
302
    set date1=${date0}
303
    set pfile1=${pfile0}
304
endif
305
 
306
# Set the final timeshift 
307
if ( ${min} != 00 ) then
308
    set timeshift=${timeshift}.${min}
309
endif
310
 
311
# Everything is fine!
312
label3:
313
 
314
# Set secondary files and check their availability
315
set sfile0=${chars}${date0}
316
set sfile1=${chars}${date1}
317
 
318
# Write status information
319
echo '---- DATA FILES -----------------------------------------'
320
echo    
321
echo "Primary files                         : ${pfile0}"
322
echo "                                      : ${pfile1}"
323
echo "Secondary files                       : ${sfile0}"
324
echo "                                      : ${sfile1}"
325
echo "Timeshift to first data file (hh.mm)  : ${timeshift}"
326
echo "Time increment of data files          : ${timeinc}"
327
echo
328
 
329
# --------------------------------------------------------------------------------------------------------------
330
# Create the start positions (without selection)
331
# -----------------------------------------------------------------------------------------------------------------
332
 
333
# Set times relative to the reference date
334
if ( "${changet}" == "true" ) then
335
  echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
336
  echo   
337
  ${LAGRANTO}/goodies/changet.sh ${refdate} ${pfile0}
338
  ${LAGRANTO}/goodies/changet.sh ${refdate} ${pfile1}
339
  if ( -f  ${sfile0} ) then
340
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${sfile0}
341
  endif
342
  if ( -f  ${sfile1} ) then
343
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${sfile1}
344
  endif
345
endif
346
 
347
# Write parameters to parameter file and create the starting positions
348
\rm -f ${parfile}
349
echo \"${pfile0}\" \"${pfile1}\"    >! ${parfile}
350
echo \"${sfile0}\" \"${sfile1}\"    >> ${parfile}
351
echo \"${ofile}${format1}\"         >> ${parfile}
352
echo \"${regionf}\"                 >> ${parfile}
353
echo ${yyyy}                        >> ${parfile}
354
echo ${mm}                          >> ${parfile}
355
echo ${dd}                          >> ${parfile}
356
echo ${hh}                          >> ${parfile}
357
echo ${min}                         >> ${parfile}
358
echo 00                             >> ${parfile}
359
echo ${timeshift}                   >> ${parfile}
360
echo ${timeinc}                     >> ${parfile}
361
 
362
# Analyse the specifier and append to parameter file
363
${LAGRANTO}/startf/startf.perl "${specifier}" >> ${parfile}
364
 
365
if ( "${status}" != "0" ) then
366
  echo "ERROR:  Preprocessor <startf> failed"
367
  exit 1
368
endif
369
 
370
# Write selection criterion to file
371
\rm -f ${crifile}
372
tail -1 ${parfile} >! ${crifile} 
373
 
374
# Write flag for no time check
375
echo \"${timecheck}\"               >> ${parfile}
376
 
377
# Write title
378
echo 
379
echo '       *** END OF PREPROCESSOR STARTF ***'        
380
echo '========================================================='
381
echo
382
 
383
# Create the startf
384
cd ${tradir}
385
${LAGRANTO}/startf/startf
386
 
387
if ( "${status}" != "0" ) then
388
  echo "ERROR:  Program <startf> failed"
389
  exit 1
390
endif
391
 
392
# --------------------------------------------------------------------------------------------------------------
393
# Apply selection (first tracing then selection)
394
# --------------------------------------------------------------------------------------------------------------
395
 
396
# Stop if no tracing and selection is necessary
397
if ( "${flag_criterion}" == "nil" ) goto finish
398
 
399
# Tracing of extra variables
400
if ( -f  ${ofile}${format2} ) then
401
  \rm -f ${ofile}${format2}
402
endif
403
if ( "${timecheck}" == "no" ) then
404
  ${LAGRANTO}/trace/trace.sh ${ofile}${format1} ${ofile}${format2} -v ${tvfile} -notimecheck
405
else
406
  ${LAGRANTO}/trace/trace.sh ${ofile}${format1} ${ofile}${format2} -v ${tvfile}
407
endif
408
\rm -f ${ofile}${format1}
409
 
410
# Selection
411
if ( -f  ${ofile} ) then
412
  \rm -f ${ofile}
413
endif
414
${LAGRANTO}/select/select.sh ${ofile}${format2} ${ofile} `cat ${crifile}` 
415
\rm -f ${ofile}${format2}
416
 
417
# --------------------------------------------------------------------------------------------------------------
418
# Final tasks (make clean)
419
# --------------------------------------------------------------------------------------------------------------
420
 
421
finish:
422
 
423
echo $noclean
424
 
425
if ( "${noclean}" == "false" ) then
426
  \rm -f ${crifile}
427
  \rm -f ${parfile}
428
 endif
429
 
430
exit 0 
431
 
432
 
433