Subversion Repositories lagranto.um

Rev

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