Subversion Repositories lagranto.wrf

Rev

Rev 11 | 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
23 michaesp 201
set timediff_mm=00
2 michaesp 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
23 michaesp 258
  set date1=`echo $file | cut -c 2-`
259
  set n=`\ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
2 michaesp 260
  @ n ++
23 michaesp 261
  set date2=`\ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-`
2 michaesp 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
 
23 michaesp 269
# Decide whether the time increment is smaller than 1 h; if so set the hires
270
# flag and convert the timeincement into fractional hour
271
set flag = `echo ${timeinc} | grep -n '\.'`
272
set hires = 0
273
if ( "${flag}" != "" ) then
274
  echo
275
  echo "Input files availability < 1 hour ($timeinc): switch to hires"
276
  echo "mode. All input files must be of form: P{yyyymmdd_hhmm}"
277
  set hires = 1
278
  set timeinc_min = `echo "(${timeinc} * 100 + 0.5)/1" | bc`
279
  set timeinc     = `echo "scale=2; ${timeinc} * 100 / 60" | bc`
280
  goto hires
281
endif
282
 
2 michaesp 283
# Search the first file to use: We step through all P files and see whether they are
284
# good P files. Let's first do the test for the first data file found. If it's ok, we 
285
# take it; if not, we step through all P files and find the good one  
286
set flag=0
287
set td=
288
 
289
set date = `echo $file | 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
foreach i ( ${charp}????????_?? )
302
 
303
  set date = `echo $i | cut -c 2-12`
304
  set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
305
  set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
306
 
307
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
308
      set datfiles=$date
309
      if ( $td1 < $timeinc    ) set td=$td1
310
      if ( $td2 < $timeinc    ) set td=$td2
311
      if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
312
      goto label2
313
  endif
314
 
315
end
316
 
317
# if no P/T-files are available for the specified time period, then $td is
318
# still undefined
319
if ( $td == "" ) then
320
  echo " ERROR: no data files available for the specified time period"
321
  exit 1
322
endif
323
 
23 michaesp 324
# If the input files come with time increment < 1 hour, handle it in this
325
# special section. The aim is not to 'interfere' with the well established
326
# code handling the standard case with time increment >= 1 hour. In this mode
327
# we require that the start and end date are available as P files: no start is
328
# allowed between the model output files
329
hires:
330
 
331
set flag=0
332
set td=0
333
 
334
if ( $direction == "f" ) then
335
  set file=${charp}${startdate}
336
else 
337
  set file=${charp}${enddate}
338
endif
339
 
340
if ( -f $file ) then
341
  set date     = `echo ${file} | cut -c 2-14`
342
  set datfiles = $date
343
  set td       = 0
344
  goto label2
345
else
346
  echo
347
  echo " ERROR: no data files available for the specified time period"
348
  exit 1
349
endif
350
 
2 michaesp 351
# Everything is fine so far: proceed
352
label2:
353
 
354
# Check whether first date is ok - before or at needed dates
355
if ( $direction == "f" ) then
356
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
357
else
358
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${date}`
359
endif
360
  if ( $tdiff0 < 0 ) then
361
  echo " ERROR: data files missing for the specified time period"
362
  exit 1
363
endif
364
 
23 michaesp 365
# Calculate the number of further files; handle hires (timeinc<1 h) and
366
# standard mode (timeinc >= 1 h) separately
367
if ( ${hires} == 0 ) then
368
   @ num = ( $timediff_hh + $td ) / $timeinc + 1
369
   @ dum1 = ( $num - 1 ) * $timeinc
370
   @ dum2 = $timediff_hh + $td
371
   if ( $dum1 != $dum2 ) @ num ++
372
else
373
    set num = `echo "( ${timediff_hh}*60 + ${timediff_mm} ) / ( ${timeinc_min} ) + 1" | bc`
374
endif 
2 michaesp 375
 
376
# Get a list of all needed files
377
set numfiles=$num
378
set sfiles=1
23 michaesp 379
echo $datfiles 
2 michaesp 380
while ( $num > 1 )
23 michaesp 381
  if ( $hires == 0 ) then
382
     set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
383
  else
384
     echo ${date} ${timeinc_min}
385
     set date=`${LAGRANTO}/goodies/newtime $date $timeinc_min`
386
  endif
387
 
388
  echo ${date}
389
 
2 michaesp 390
  if ( ! -f ${charp}${date} ) then
391
    echo " ERROR: file with primary data is missing for $date"
392
    exit 1
393
  else if ( ! -f ${chars}${date} ) then
394
    set sfiles=0
395
    set datfiles=`echo $datfiles $date`
396
  else
397
    set datfiles=`echo $datfiles $date`
398
  endif
399
  @ num --
400
end
401
 
402
# Calculate the start and the end time relative to the first datfile
403
if ( $direction == f ) then
404
  set tstart = `${LAGRANTO}/goodies/gettidiff $startdate $datfiles[1]`
405
  set tend   = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $enddate`
406
else
407
  set tstart = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $startdate`
408
  set tend   = `${LAGRANTO}/goodies/gettidiff $enddate $datfiles[1]`
409
endif
410
 
411
# Write some status information
412
echo "Primary file prefix               : ${charp}"
413
echo "Secondary file prefix             : ${chars}"
23 michaesp 414
if ( ${hires} == 0 ) then
415
  echo "Time increment for input files : ${timeinc} h"
416
else
417
  echo "Time increment for input files : ${timeinc_min} min (0$timeinc h) "
418
endif
2 michaesp 419
echo "# input files                     : ${numfiles}"
420
echo "First input file                  : $datfiles[1] " 
421
echo "Last input file                   : $datfiles[$numfiles] " 
422
echo "${charp} files availability              : 1"  
423
echo "${chars} files availability              : ${sfiles}"     
424
if ( $direction == f ) then
425
echo "Start time relative to first file : $datfiles[1] + ${tstart} "
426
echo "End time relative to last file    : $datfiles[$numfiles] - ${tend} "  
427
else
428
echo "Start time relative to last file  : $datfiles[$numfiles] - ${tstart} "
429
echo "End time relative to first file   : $datfiles[1] + ${tend} "
430
endif
431
 
432
# ---------------------------------------------------------------------
433
# Check availability of input data 
434
# ---------------------------------------------------------------------
435
 
436
echo
437
echo '---- TRACEVAR FILE --------------------------------------'
438
echo    
439
 
440
# If "-f" option is used, create a temporary tracevar file
441
if ( "${flag_f}" == "-f" ) then
442
 
443
#   Preset values for <compfl> and <tprefix>
444
    set tcompfl=1
445
    set tprefix='P'
446
 
447
#   Check availability on P file
448
    foreach var ( `${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]` )
449
       if ( "${var}" == "${tvar}" ) then
450
          set tcompfl=0
451
	  set tprefix="P"
452
       endif
453
    end
454
 
455
#   Check availability on S file 
456
    if ( ${sfiles} == 1 ) then
457
       foreach var ( `${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]` )
458
         if ( "${var}" == "${tvar}" ) then
459
            set tcompfl=0
460
	    set tprefix="S"
461
         endif
462
       end
463
    endif
464
 
465
#   Write the temporary <tracevars> file
466
    echo "${tvar} ${tscale} ${tcompfl} ${tprefix}" >! ${tvfile}
467
    echo "Temporary tracervar file <${tvfile}> created"
468
    echo
469
 
470
endif
471
 
472
 
473
# Check if tracevars-file exists
474
if ( ! -f $tvfile ) then
475
  echo  " ERROR:  file $tvfile was not found ***"
476
  exit 1
477
endif
478
 
479
# check if the variables contained in the tracevars-file are available in the
480
# data file and check also if there are no empty lines in the tracevars-file
481
 
482
set nlines = `cat $tvfile | wc -l`
483
set vars   = `cat $tvfile | awk '{print $1}'`
484
set nvars  = `echo $vars | wc -w`
485
if ( $nlines != $nvars ) then
486
  echo " ERROR: tracevars-files must not contain empty lines ***"
487
  exit 1
488
endif
489
set calf=`cat $tvfile | awk '{print $3}'`
490
set tfil=`cat $tvfile | awk '{print $4}'`
491
foreach v ( $vars )
492
  if ( $calf[1] == 0 ) then
493
    set v0 = `echo $v | awk 'BEGIN {FS = ":"}; {print $1}'`
494
    set flag=`${LAGRANTO}goodies/getvars $tfil[1]$datfiles[1] | grep " $v0 " | wc -l`
495
    set iscomment=`echo $v0 | cut -c 1` 
496
    if ( "${iscomment}" != "#" ) then 
497
	 if ( $flag == 0 ) then
498
           echo " ERROR: variable $v listed in $tvfile is not on the $tfil[1]-files ***"
499
           exit 1
500
	 endif
501
    endif
502
  endif
503
  shift calf
504
  shift tfil
505
end
506
set ntrace=${nlines} 
507
 
508
# Write some status information
509
cat ${tvfile}
510
echo
511
echo "# Number of tracing variables : ${ntrace}"
512
 
513
# ---------------------------------------------------------------------
514
# Prepare input file for caltra and run it
515
# ---------------------------------------------------------------------
516
 
517
# Write parameter file
518
\rm -f ${parfile}
519
touch ${parfile}
520
 
521
echo $inpfile                                              >> $parfile
522
echo $outfile                                              >> $parfile    
523
echo $startdate                                            >> $parfile
524
echo $enddate                                              >> $parfile
525
echo $idir                                                 >> $parfile
526
echo $numfiles                                             >> $parfile
527
foreach i ( $datfiles )
528
  echo $i                                                  >> $parfile
529
end
530
echo $timeinc                                              >> $parfile
531
echo $tstart                                               >> $parfile
532
echo $tend                                                 >> $parfile
533
echo $ntra                                                 >> $parfile
534
echo $ntim                                                 >> $parfile
535
echo $ncol                                                 >> $parfile
536
echo $ntrace                                               >> $parfile
537
cat ${tvfile}                                              >> $parfile
538
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1] | wc -l   >> $parfile
539
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]           >> $parfile
540
if ( $sfiles == 1 ) then
541
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1] | wc -l >> $parfile
542
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]         >> $parfile
543
else
544
  echo 0                                                   >> $parfile
545
endif
546
echo \"${timecheck}\"                                      >> $parfile
547
echo \"${intmode}\"                                        >> $parfile
548
 
549
# Finish the preprocessor
550
echo 
551
echo '       *** END OF PREPROCESSOR TRACE ***              '
552
echo '========================================================='
553
echo
554
 
555
# Run  trace
556
${prog}
557
 
558
if ( "${status}" != "0" ) then
559
  echo "ERROR:  Program <trace> failed"
560
  exit 1
561
endif
562
 
563
# ---------------------------------------------------------------------
564
# Final tasks (make clean)
565
# ---------------------------------------------------------------------
566
 
567
finish:
568
 
569
if ( "${noclean}" == "false" ) then
570
  \rm -f ${parfile}
571
endif
572
 
573
exit 0 
574