Subversion Repositories lagranto.wrf

Rev

Rev 2 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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