Subversion Repositories lagranto.wrf

Rev

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

Rev Author Line No. Line
2 michaesp 1
#!/bin/csh
2
 
3
# ---------------------------------------------------------------------
4
# Usage, parameter settings
5
# ---------------------------------------------------------------------
6
 
7
# Write usage information
8
if ( (${#argv} == 0) | (${#argv} < 2) ) then
9
  echo 
10
  ${LAGRANTO}/bin/lagrantohelp trace short
11
  echo  
12
  exit 0
13
endif
14
 
15
# Write title
16
echo 
17
echo '========================================================='
18
echo '       *** START OF PREPROCESSOR TRACE ***              '
19
echo
20
 
21
# Get the arguments
22
set inpfile   = $1
23
set outfile   = $2
24
 
25
# Set base directories (run+prog)
26
set cdfdir=${PWD}
27
set tradir=${PWD}
28
 
29
# Set program paths and filenames 
30
set parfile = ${tradir}/trace.param 
31
set prog    = ${LAGRANTO}/trace/trace
32
 
33
# Set the prefix of the primary and secondary data files
34
set charp = 'P'
35
set chars = 'S'
36
 
37
echo '---- DIRECTORIES AND PROGRAMS ---------------------------'
38
echo    
39
echo "CDF directory         : ${cdfdir}"
40
echo "TRA directory         : ${tradir}"
41
echo "PROGRAM TRACE         : ${prog}"
42
echo "PARAMETER file        : ${parfile}"
43
echo
44
 
45
# ---------------------------------------------------------------------
46
# Set optional flags
47
# ---------------------------------------------------------------------
48
 
49
echo '---- OPTIONAL FLAGS -------------------------------------'
50
echo
51
 
52
# Set some default values ("nil" must be set according to input files)
53
set flag_i     = "nil"
54
set flag_v     = "tracevars"
55
set flag_f     = "nil"
56
set tvfile     = 'tracevars'
57
set noclean    = 'false'
58
set timecheck  = 'no' 
59
set intmode    = 'normal'
60
 
61
# Set flag for consistency
62
set isok = 1
63
 
64
while ( $#argv > 0 )
65
 
66
  switch ( $argv[1] )
67
 
68
   case -i
69
     set flag_i=$argv[2]
70
     echo "Flag '-i'     -> ${flag_i} (user defined)"
71
     shift;
72
   breaksw
73
 
74
   case -v
75
     set flag_v="-v"
76
     set tvfile=$argv[2]
77
     echo "Flag '-v'     -> ${tvfile} (user defined)"
78
     shift;
79
     if ( $isok == 2 ) set isok = 0
80
     if ( $isok == 1 ) set isok = 2
81
   breaksw
82
 
83
   case -f
84
     set flag_f="-f"
85
     set tvfile="tracevars.tmp"
86
     shift;
87
     set tvar="$argv[1]"
88
     shift;
89
     set tscale="$argv[1]"
90
     echo "Flag '-f'     -> ${tvar} ${tscale} (user defined)"
91
     if ( $isok == 2 ) set isok = 0
92
     if ( $isok == 1 ) set isok = 2
93
   breaksw
94
 
95
   case -noclean
96
     set noclean = 'true'
97
     echo "noclean       -> true (user defined)"
98
   breaksw
99
 
100
   case -timecheck
101
     set timecheck = 'yes'
102
     echo "timecheck               -> yes (user defined)"
103
   breaksw
104
 
105
   case -nearest
106
     set intmode = 'nearest'
107
     echo "intmode                 -> nearest (user defined)"
108
   breaksw
109
 
110
  endsw
111
 
112
  shift;
113
 
114
end
115
 
116
# Check consitency of arguments
117
if ( $isok == 0 ) then
118
    echo
119
    echo " ERROR: Use either option '-v' or '-f', but not both..."
120
    exit 1
121
endif
122
 
123
# ---------------------------------------------------------------------
124
# Handle the input trajectory file
125
# ---------------------------------------------------------------------
126
 
127
echo
128
echo '---- TIME RANGE -----------------------------------------'
129
echo
130
 
131
# Check whether the input file can be found
132
if ( ! -f ${inpfile} ) then
133
    echo " ERROR : Input file ${inpfile} is missing"
134
    exit 1
135
endif
136
 
137
# Get the start, end and reference date for the tracing
138
set startdate = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} startdate` 
139
set enddate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} enddate` 
140
set refdate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} refdate` 
141
set ntra      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
142
set ntim      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
143
set ncol      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
144
 
145
# Check format of start and end date - must be the same
146
set ns=`echo $startdate | sed -e 's/_[0-9]*//' | wc -c`
147
set ne=`echo $enddate   | sed -e 's/_[0-9]*//' | wc -c`
148
if ( $ns != $ne ) then
149
  echo " ERROR: start and end date must be in the same format ***"
150
  exit 1
151
endif
152
if ( $ns != 9 ) then
153
  echo " ERROR: Date format must be yyyymmdd ***"
154
  exit 1
155
endif
156
set ns=`echo $startdate | sed -e 's/[0-9]*_//' | wc -c`
157
set ne=`echo $enddate   | sed -e 's/[0-9]*_//' | wc -c`
158
if ( $ns != $ne ) then
159
  echo " ERROR: start and end date must be in the same format ***"
160
  exit 1
161
endif
162
if ( ( $ns != 5 ) & ( $ns != 3 ) ) then
163
  echo " ERROR: Time format must be hh(mm) ***"
164
  exit 1
165
endif
166
 
167
# Split the start and end date into <yymmdd_hh and mm>
168
set startdate_ymdh = `echo $startdate | cut -c 1-11`
169
set startdate_min  = `echo $startdate | cut -c 12-13`
170
if ( $startdate_min == "" ) set startdate_min = 00
171
 
172
set enddate_ymdh = `echo $enddate | cut -c 1-11`
173
set enddate_min  = `echo $enddate | cut -c 12-13`
174
if ( $enddate_min == "" ) set enddate_min = 00
175
 
176
# Get the time difference between <start_ymdh> and <end_ymdh> date
177
# Decide whether trajectoriesare forward or backward
178
set timediff_hh = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${startdate_ymdh}`
179
 
180
if ( $timediff_hh == 0 ) then
181
  if ( $enddate_min > $startdate_min ) then
182
    set direction = f
183
    set idir      = 1
184
  else
185
    set direction = b
186
    set idir      = -1
187
  endif
188
else if ( $timediff_hh > 0 ) then
189
  set direction = f
190
  set idir      = 1
191
else
192
  set direction = b
193
  set idir      = -1
194
  @ timediff_hh = $idir * $timediff_hh
195
endif
196
 
197
# Get also minutes for time difference, if <start_min> or <end_min> != 0
198
set timediff_mm=
199
 
200
if ( $startdate_min != 00 || $enddate_min != 00 ) then
201
  @ min = ( $enddate_min - $startdate_min )
202
  if ( $min == 0 ) then
203
    set timediff_mm=
204
  else if ( $min > 0 ) then
205
    if ( $idir == 1 ) then
206
      set timediff_mm=$min
207
    else
208
      @ timediff_hh --
209
      @ timediff_mm = 60 - $min
210
    endif
211
  else
212
    if ( $idir == 1 ) then
213
      @ timediff_hh --
214
      @ timediff_mm = 60 + $min
215
    else
216
      @ timediff_mm = 0 - $min
217
    endif
218
  endif
219
endif
220
 
221
# Write status information
222
echo "Time range      : ${startdate} -> ${enddate}"
223
if ( ${timediff_mm} != "" ) then
224
   echo "Time difference : ${timediff_hh} h ${timediff_mm} min"
225
else
226
   echo "Time difference : ${timediff_hh} h"
227
endif
228
echo "Direction       : ${direction} (${idir})"
229
 
230
# ---------------------------------------------------------------------
231
# Check availability of input data 
232
# ---------------------------------------------------------------------
233
 
234
echo
235
echo '---- INPUT FILES ----------------------------------------'
236
echo
237
 
238
# Take the time increment from flag list ('nil', if not defined)
239
set timeinc = ${flag_i}
240
 
241
# Find a first data file (if possible corresponding to start/end date
242
# If starttime is not a data time, take the first file in the direectory
243
if ( $direction == "f" ) then
244
  set file=${charp}${startdate_ymdh}
245
else
246
  set file=${charp}${enddate_ymdh}
247
endif
248
if ( ! -f $file ) then
249
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
250
endif
251
 
252
# Determine timeinc (the time difference in hours between two data file)
253
# if not already defined with option -i
254
if ( ${timeinc} == "nil" ) then
255
  set date1=`echo $file | cut -c 2-12`
256
  set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
257
  @ n ++
258
  set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-12`
259
  set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
260
endif
261
if ( $timeinc == 0 ) then
262
    echo " ERROR: cannot set the time increment between input files ***"
263
    exit 1
264
endif
265
 
266
# Search the first file to use: We step through all P files and see whether they are
267
# good P files. Let's first do the test for the first data file found. If it's ok, we 
268
# take it; if not, we step through all P files and find the good one  
269
set flag=0
270
set td=
271
 
272
set date = `echo $file | cut -c 2-12`
273
set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
274
set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
275
 
276
if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
277
   set datfiles=$date
278
   if ( $td1 < $timeinc    ) set td=$td1
279
   if ( $td2 < $timeinc    ) set td=$td2
280
   if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
281
   goto label2      
282
endif
283
 
284
foreach i ( ${charp}????????_?? )
285
 
286
  set date = `echo $i | cut -c 2-12`
287
  set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
288
  set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
289
 
290
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
291
      set datfiles=$date
292
      if ( $td1 < $timeinc    ) set td=$td1
293
      if ( $td2 < $timeinc    ) set td=$td2
294
      if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
295
      goto label2
296
  endif
297
 
298
end
299
 
300
# if no P/T-files are available for the specified time period, then $td is
301
# still undefined
302
if ( $td == "" ) then
303
  echo " ERROR: no data files available for the specified time period"
304
  exit 1
305
endif
306
 
307
# Everything is fine so far: proceed
308
label2:
309
 
310
# Check whether first date is ok - before or at needed dates
311
if ( $direction == "f" ) then
312
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
313
else
314
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${date}`
315
endif
316
  if ( $tdiff0 < 0 ) then
317
  echo " ERROR: data files missing for the specified time period"
318
  exit 1
319
endif
320
 
321
# Calculate the number of further files
322
@ num = ( $timediff_hh + $td ) / $timeinc + 1
323
@ dum1 = ( $num - 1 ) * $timeinc
324
@ dum2 = $timediff_hh + $td
325
if ( $dum1 != $dum2 ) @ num ++
326
 
327
# Get a list of all needed files
328
set numfiles=$num
329
set sfiles=1
330
while ( $num > 1 )
331
 
332
  set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
333
  if ( ! -f ${charp}${date} ) then
334
    echo " ERROR: file with primary data is missing for $date"
335
    exit 1
336
  else if ( ! -f ${chars}${date} ) then
337
    set sfiles=0
338
    set datfiles=`echo $datfiles $date`
339
  else
340
    set datfiles=`echo $datfiles $date`
341
  endif
342
  @ num --
343
end
344
 
345
# Calculate the start and the end time relative to the first datfile
346
if ( $direction == f ) then
347
  set tstart = `${LAGRANTO}/goodies/gettidiff $startdate $datfiles[1]`
348
  set tend   = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $enddate`
349
else
350
  set tstart = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $startdate`
351
  set tend   = `${LAGRANTO}/goodies/gettidiff $enddate $datfiles[1]`
352
endif
353
 
354
 
355
# Write some status information
356
echo "Primary file prefix               : ${charp}"
357
echo "Secondary file prefix             : ${chars}"
358
echo "Time increment for input files    : ${timeinc}"
359
echo "# input files                     : ${numfiles}"
360
echo "First input file                  : $datfiles[1] " 
361
echo "Last input file                   : $datfiles[$numfiles] " 
362
echo "${charp} files availability              : 1"  
363
echo "${chars} files availability              : ${sfiles}"     
364
if ( $direction == f ) then
365
echo "Start time relative to first file : $datfiles[1] + ${tstart} "
366
echo "End time relative to last file    : $datfiles[$numfiles] - ${tend} "  
367
else
368
echo "Start time relative to last file  : $datfiles[$numfiles] - ${tstart} "
369
echo "End time relative to first file   : $datfiles[1] + ${tend} "
370
endif
371
 
372
# ---------------------------------------------------------------------
373
# Check availability of input data 
374
# ---------------------------------------------------------------------
375
 
376
echo
377
echo '---- TRACEVAR FILE --------------------------------------'
378
echo    
379
 
380
# If "-f" option is used, create a temporary tracevar file
381
if ( "${flag_f}" == "-f" ) then
382
 
383
#   Preset values for <compfl> and <tprefix>
384
    set tcompfl=1
385
    set tprefix='P'
386
 
387
#   Check availability on P file
388
    foreach var ( `${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]` )
389
       if ( "${var}" == "${tvar}" ) then
390
          set tcompfl=0
391
	  set tprefix="P"
392
       endif
393
    end
394
 
395
#   Check availability on S file 
396
    if ( ${sfiles} == 1 ) then
397
       foreach var ( `${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]` )
398
         if ( "${var}" == "${tvar}" ) then
399
            set tcompfl=0
400
	    set tprefix="S"
401
         endif
402
       end
403
    endif
404
 
405
#   Write the temporary <tracevars> file
406
    echo "${tvar} ${tscale} ${tcompfl} ${tprefix}" >! ${tvfile}
407
    echo "Temporary tracervar file <${tvfile}> created"
408
    echo
409
 
410
endif
411
 
412
 
413
# Check if tracevars-file exists
414
if ( ! -f $tvfile ) then
415
  echo  " ERROR:  file $tvfile was not found ***"
416
  exit 1
417
endif
418
 
419
# check if the variables contained in the tracevars-file are available in the
420
# data file and check also if there are no empty lines in the tracevars-file
421
 
422
set nlines = `cat $tvfile | wc -l`
423
set vars   = `cat $tvfile | awk '{print $1}'`
424
set nvars  = `echo $vars | wc -w`
425
if ( $nlines != $nvars ) then
426
  echo " ERROR: tracevars-files must not contain empty lines ***"
427
  exit 1
428
endif
429
set calf=`cat $tvfile | awk '{print $3}'`
430
set tfil=`cat $tvfile | awk '{print $4}'`
431
foreach v ( $vars )
432
  if ( $calf[1] == 0 ) then
433
    set v0 = `echo $v | awk 'BEGIN {FS = ":"}; {print $1}'`
434
    set flag=`${LAGRANTO}goodies/getvars $tfil[1]$datfiles[1] | grep " $v0 " | wc -l`
435
    set iscomment=`echo $v0 | cut -c 1` 
436
    if ( "${iscomment}" != "#" ) then 
437
	 if ( $flag == 0 ) then
438
           echo " ERROR: variable $v listed in $tvfile is not on the $tfil[1]-files ***"
439
           exit 1
440
	 endif
441
    endif
442
  endif
443
  shift calf
444
  shift tfil
445
end
446
set ntrace=${nlines} 
447
 
448
# Write some status information
449
cat ${tvfile}
450
echo
451
echo "# Number of tracing variables : ${ntrace}"
452
 
453
# ---------------------------------------------------------------------
454
# Prepare input file for caltra and run it
455
# ---------------------------------------------------------------------
456
 
457
# Write parameter file
458
\rm -f ${parfile}
459
touch ${parfile}
460
 
461
echo $inpfile                                              >> $parfile
462
echo $outfile                                              >> $parfile    
463
echo $startdate                                            >> $parfile
464
echo $enddate                                              >> $parfile
465
echo $idir                                                 >> $parfile
466
echo $numfiles                                             >> $parfile
467
foreach i ( $datfiles )
468
  echo $i                                                  >> $parfile
469
end
470
echo $timeinc                                              >> $parfile
471
echo $tstart                                               >> $parfile
472
echo $tend                                                 >> $parfile
473
echo $ntra                                                 >> $parfile
474
echo $ntim                                                 >> $parfile
475
echo $ncol                                                 >> $parfile
476
echo $ntrace                                               >> $parfile
477
cat ${tvfile}                                              >> $parfile
478
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1] | wc -l   >> $parfile
479
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]           >> $parfile
480
if ( $sfiles == 1 ) then
481
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1] | wc -l >> $parfile
482
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]         >> $parfile
483
else
484
  echo 0                                                   >> $parfile
485
endif
486
echo \"${timecheck}\"                                      >> $parfile
487
echo \"${intmode}\"                                        >> $parfile
488
 
489
# Finish the preprocessor
490
echo 
491
echo '       *** END OF PREPROCESSOR TRACE ***              '
492
echo '========================================================='
493
echo
494
 
495
# Run  trace
496
${prog}
497
 
498
if ( "${status}" != "0" ) then
499
  echo "ERROR:  Program <trace> failed"
500
  exit 1
501
endif
502
 
503
# ---------------------------------------------------------------------
504
# Final tasks (make clean)
505
# ---------------------------------------------------------------------
506
 
507
finish:
508
 
509
if ( "${noclean}" == "false" ) then
510
  \rm -f ${parfile}
511
endif
512
 
513
exit 0 
514