Subversion Repositories lagranto.ecmwf

Rev

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

Rev Author Line No. Line
3 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 changet    = 'false'
58
set noclean    = 'false'
59
set timecheck  = 'no' 
60
set intmode    = 'normal'
61
set radius     = '0'        
5 michaesp 62
set tropo_pv   = '2' 
63
set tropo_th   = '380'
3 michaesp 64
 
65
# Set flag for consistency
66
set isok = 1
67
 
68
while ( $#argv > 0 )
69
 
70
  switch ( $argv[1] )
71
 
72
   case -i
73
     set flag_i=$argv[2]
74
     echo "Flag '-i'     -> ${flag_i} (user defined)"
75
     shift;
76
   breaksw
77
 
78
   case -v
79
     set flag_v="-v"
80
     set tvfile=$argv[2]
81
     echo "Flag '-v'     -> ${tvfile} (user defined)"
82
     shift;
83
     if ( $isok == 2 ) set isok = 0
84
     if ( $isok == 1 ) set isok = 2
85
   breaksw
86
 
87
   case -f
88
     set flag_f="-f"
89
     set tvfile="tracevars.tmp"
90
     shift;
91
     set tvar="$argv[1]"
92
     shift;
93
     set tscale="$argv[1]"
94
     echo "Flag '-f'     -> ${tvar} ${tscale} (user defined)"
95
     if ( $isok == 2 ) set isok = 0
96
     if ( $isok == 1 ) set isok = 2
97
   breaksw
98
 
99
   case -changet
100
     set changet = 'true'
101
     echo "changet       -> true (user defined)"
102
   breaksw
103
 
104
   case -noclean
105
     set noclean = 'true'
106
     echo "noclean       -> true (user defined)"
107
   breaksw
108
 
109
   case -timecheck
110
     set timecheck = 'yes'
111
     echo "timecheck               -> yes (user defined)"
112
   breaksw
113
 
114
   case -nearest
115
     set intmode = 'nearest'
116
     echo "intmode                 -> nearest (user defined)"
117
   breaksw
118
 
119
   case -clustering
120
     set intmode = 'clustering'
121
     echo "intmode                 -> clustering (user defined)"
5 michaesp 122
     shift;
123
     if ( "$1" == "" ) then
124
        echo "ERROR (clustering): specify tropopause PV [pvu] and TH [K]! Example: -clustering 2 380"
125
        exit
126
     else
127
        set tropo_pv = $1
128
     endif
129
     shift;
130
     if ( "$1" == "" ) then
131
        echo "ERROR (clustering): specify tropopause PV [pvu] and TH [K]! Example: -clustering 2 380"
132
        exit
133
     else
134
        set tropo_th = $1
135
        echo 'intmode                 -> clustering tropo_pv = ' ${tropo_pv} 'pvu and tropo_th = ' ${tropo_th} 'K'
136
     endif
3 michaesp 137
   breaksw
138
 
139
   case -circle_avg
140
     set intmode = 'circle_avg'
141
     echo "intmode                 -> circle_avg (user defined)"
142
     shift;
143
     if ( "$1" == "" ) then
144
        echo "ERROR (circle_avg): specify radius in circle mode (km)! Example: -circle_avg 500"
145
        exit
146
     else
147
        set radius = $1
148
        echo 'intmode                 -> circle_avg radius =' ${radius} 'km'
149
     endif
150
   breaksw
151
 
152
   case -circle_min
153
     set intmode = 'circle_min'
154
     echo "intmode                 -> circle_min (user defined)"
155
     shift;
156
     if ( "$1" == "" ) then
157
        echo "ERROR (circle_min): specify radius in circle mode (km)! Example: -circle_min 400"
158
        exit
159
     else
160
        set radius = $1
161
        echo 'intmode                 -> circle_min radius =' ${radius} 'km'
162
     endif
163
   breaksw
164
 
165
   case -circle_max
166
     set intmode = 'circle_max'
167
     echo "intmode                 -> circle_max (user defined)"
168
     shift;
169
     if ( "$1" == "" ) then
170
        echo "ERROR (circle_max): specify radius in circle mode (km)! Example: -circle_max 600"
171
        exit
172
     else
173
        set radius = $1
174
        echo 'intmode                 -> circle_max radius =' ${radius} 'km'
175
     endif
176
 
177
   breaksw
178
 
179
  endsw
180
 
181
  shift;
182
 
183
end
184
 
185
# No change of times necessary if no check requested
186
if ( "${timecheck}" == "no" ) then
187
   set  changet = 'false'
188
endif
189
 
190
# Check consitency of arguments
191
if ( $isok == 0 ) then
192
    echo
193
    echo " ERROR: Use either option '-v' or '-f', but not both..."
194
    exit 1
195
endif
196
 
197
# ---------------------------------------------------------------------
198
# Handle the input trajectory file
199
# ---------------------------------------------------------------------
200
 
201
echo
202
echo '---- TIME RANGE -----------------------------------------'
203
echo
204
 
205
# Check whether the input file can be found
206
if ( ! -f ${inpfile} ) then
207
    echo " ERROR : Input file ${inpfile} is missing"
208
    exit 1
209
endif
210
 
211
# Get the start, end and reference date for the tracing
212
set startdate = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} startdate` 
213
set enddate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} enddate` 
214
set refdate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} refdate` 
5 michaesp 215
set ntra      = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
216
set ntim      = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
217
set ncol      = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
3 michaesp 218
 
219
# Check format of start and end date - must be the same
220
set ns=`echo $startdate | sed -e 's/_[0-9]*//' | wc -c`
221
set ne=`echo $enddate   | sed -e 's/_[0-9]*//' | wc -c`
222
if ( $ns != $ne ) then
223
  echo " ERROR: start and end date must be in the same format ***"
224
  exit 1
225
endif
226
if ( $ns != 9 ) then
227
  echo " ERROR: Date format must be yyyymmdd ***"
228
  exit 1
229
endif
230
set ns=`echo $startdate | sed -e 's/[0-9]*_//' | wc -c`
231
set ne=`echo $enddate   | sed -e 's/[0-9]*_//' | wc -c`
232
if ( $ns != $ne ) then
233
  echo " ERROR: start and end date must be in the same format ***"
234
  exit 1
235
endif
236
if ( ( $ns != 5 ) & ( $ns != 3 ) ) then
237
  echo " ERROR: Time format must be hh(mm) ***"
238
  exit 1
239
endif
240
 
241
# Split the start and end date into <yymmdd_hh and mm>
242
set startdate_ymdh = `echo $startdate | cut -c 1-11`
243
set startdate_min  = `echo $startdate | cut -c 12-13`
244
if ( $startdate_min == "" ) set startdate_min = 00
245
 
246
set enddate_ymdh = `echo $enddate | cut -c 1-11`
247
set enddate_min  = `echo $enddate | cut -c 12-13`
248
if ( $enddate_min == "" ) set enddate_min = 00
249
 
250
# Get the time difference between <start_ymdh> and <end_ymdh> date
251
# Decide whether trajectoriesare forward or backward
252
set timediff_hh = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${startdate_ymdh}`
253
 
254
if ( $timediff_hh == 0 ) then
255
  if ( $enddate_min > $startdate_min ) then
256
    set direction = f
257
    set idir      = 1
258
  else
259
    set direction = b
260
    set idir      = -1
261
  endif
262
else if ( $timediff_hh > 0 ) then
263
  set direction = f
264
  set idir      = 1
265
else
266
  set direction = b
267
  set idir      = -1
268
  @ timediff_hh = $idir * $timediff_hh
269
endif
270
 
271
# Get also minutes for time difference, if <start_min> or <end_min> != 0
272
set timediff_mm=
273
 
274
if ( $startdate_min != 00 || $enddate_min != 00 ) then
275
  @ min = ( $enddate_min - $startdate_min )
276
  if ( $min == 0 ) then
277
    set timediff_mm=
278
  else if ( $min > 0 ) then
279
    if ( $idir == 1 ) then
280
      set timediff_mm=$min
281
    else
282
      @ timediff_hh --
283
      @ timediff_mm = 60 - $min
284
    endif
285
  else
286
    if ( $idir == 1 ) then
287
      @ timediff_hh --
288
      @ timediff_mm = 60 + $min
289
    else
290
      @ timediff_mm = 0 - $min
291
    endif
292
  endif
293
endif
294
 
295
# Write status information
296
echo "Time range      : ${startdate} -> ${enddate}"
297
if ( ${timediff_mm} != "" ) then
298
   echo "Time difference : ${timediff_hh} h ${timediff_mm} min"
299
else
300
   echo "Time difference : ${timediff_hh} h"
301
endif
302
echo "Direction       : ${direction} (${idir})"
303
 
304
# ---------------------------------------------------------------------
305
# Check availability of input data 
306
# ---------------------------------------------------------------------
307
 
308
echo
309
echo '---- INPUT FILES ----------------------------------------'
310
echo
311
 
39 michaesp 312
 
3 michaesp 313
# Take the time increment from flag list ('nil', if not defined)
314
set timeinc = ${flag_i}
315
 
316
# Find a first data file (if possible corresponding to start/end date
317
# If starttime is not a data time, take the first file in the direectory
318
if ( $direction == "f" ) then
319
  set file=${charp}${startdate_ymdh}
320
else
321
  set file=${charp}${enddate_ymdh}
322
endif
323
if ( ! -f $file ) then
324
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
325
endif
326
 
327
# Determine timeinc (the time difference in hours between two data file)
328
# if not already defined with option -i
329
if ( ${timeinc} == "nil" ) then
330
  set date1=`echo $file | cut -c 2-12`
331
  set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
332
  @ n ++
333
  set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-12`
334
  set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
335
endif
336
if ( $timeinc == 0 ) then
337
    echo " ERROR: cannot set the time increment between input files ***"
338
    exit 1
339
endif
340
 
341
# Search the first file to use: We step through all P files and see whether they are
342
# good P files. Let's first do the test for the first data file found. If it's ok, we 
343
# take it; if not, we step through all P files and find the good one  
344
set flag=0
345
set td=
346
 
347
set date = `echo $file | cut -c 2-12`
348
set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
349
set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
350
 
351
if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
352
   set datfiles=$date
353
   if ( $td1 < $timeinc    ) set td=$td1
354
   if ( $td2 < $timeinc    ) set td=$td2
355
   if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
356
   goto label2      
357
endif
358
 
359
foreach i ( ${charp}????????_?? )
360
 
361
  set date = `echo $i | cut -c 2-12`
362
  set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
363
  set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
364
 
365
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
366
      set datfiles=$date
367
      if ( $td1 < $timeinc    ) set td=$td1
368
      if ( $td2 < $timeinc    ) set td=$td2
369
      if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
370
      goto label2
371
  endif
372
 
373
end
374
 
375
# if no P/T-files are available for the specified time period, then $td is
376
# still undefined
377
if ( $td == "" ) then
378
  echo " ERROR: no data files available for the specified time period"
379
  exit 1
380
endif
381
 
382
# Everything is fine so far: proceed
383
label2:
384
 
385
# Check whether first date is ok - before or at needed dates
386
if ( $direction == "f" ) then
387
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
388
else
389
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${date}`
390
endif
391
  if ( $tdiff0 < 0 ) then
392
  echo " ERROR: data files missing for the specified time period"
393
  exit 1
394
endif
395
 
396
# Calculate the number of further files
397
@ num = ( $timediff_hh + $td ) / $timeinc + 1
398
@ dum1 = ( $num - 1 ) * $timeinc
399
@ dum2 = $timediff_hh + $td
400
if ( $dum1 != $dum2 ) @ num ++
401
 
402
# Get a list of all needed files
403
set numfiles=$num
404
set sfiles=1
405
while ( $num > 1 )
406
 
407
  set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
408
  if ( ! -f ${charp}${date} ) then
409
    echo " ERROR: file with primary data is missing for $date"
410
    exit 1
411
  else if ( ! -f ${chars}${date} ) then
412
    set sfiles=0
413
    set datfiles=`echo $datfiles $date`
414
  else
415
    set datfiles=`echo $datfiles $date`
416
  endif
417
  @ num --
418
end
419
 
420
# Calculate the start and the end time relative to the first datfile
421
if ( $direction == f ) then
422
  set tstart = `${LAGRANTO}/goodies/gettidiff $startdate $datfiles[1]`
423
  set tend   = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $enddate`
424
else
425
  set tstart = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $startdate`
426
  set tend   = `${LAGRANTO}/goodies/gettidiff $enddate $datfiles[1]`
427
endif
428
 
429
 
430
# Write some status information
431
echo "Primary file prefix               : ${charp}"
432
echo "Secondary file prefix             : ${chars}"
433
echo "Time increment for input files    : ${timeinc}"
434
echo "# input files                     : ${numfiles}"
435
echo "First input file                  : $datfiles[1] " 
436
echo "Last input file                   : $datfiles[$numfiles] " 
437
echo "${charp} files availability              : 1"  
438
echo "${chars} files availability              : ${sfiles}"     
439
if ( $direction == f ) then
440
echo "Start time relative to first file : $datfiles[1] + ${tstart} "
441
echo "End time relative to last file    : $datfiles[$numfiles] - ${tend} "  
442
else
443
echo "Start time relative to last file  : $datfiles[$numfiles] - ${tstart} "
444
echo "End time relative to first file   : $datfiles[1] + ${tend} "
445
endif
446
 
447
# ---------------------------------------------------------------------
448
# Check availability of input data 
449
# ---------------------------------------------------------------------
450
 
451
echo
452
echo '---- TRACEVAR FILE --------------------------------------'
453
echo    
454
 
455
# If "-f" option is used, create a temporary tracevar file
456
if ( "${flag_f}" == "-f" ) then
457
 
458
#   Preset values for <compfl> and <tprefix>
459
    set tcompfl=1
460
    set tprefix='P'
461
 
462
#   Check availability on P file
463
    foreach var ( `${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]` )
464
       if ( "${var}" == "${tvar}" ) then
465
          set tcompfl=0
466
	  set tprefix="P"
467
       endif
468
    end
469
 
470
#   Check availability on S file 
471
    if ( ${sfiles} == 1 ) then
472
       foreach var ( `${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]` )
473
         if ( "${var}" == "${tvar}" ) then
474
            set tcompfl=0
475
	    set tprefix="S"
476
         endif
477
       end
478
    endif
479
 
480
#   Write the temporary <tracevars> file
481
    echo "${tvar} ${tscale} ${tcompfl} ${tprefix}" >! ${tvfile}
482
    echo "Temporary tracervar file <${tvfile}> created"
483
    echo
484
 
485
endif
486
 
487
 
488
# Check if tracevars-file exists
489
if ( ! -f $tvfile ) then
490
  echo  " ERROR:  file $tvfile was not found ***"
491
  exit 1
492
endif
493
 
494
# check if the variables contained in the tracevars-file are available in the
495
# data file and check also if there are no empty lines in the tracevars-file
496
 
497
set nlines = `cat $tvfile | wc -l`
498
set vars   = `cat $tvfile | awk '{print $1}'`
499
set nvars  = `echo $vars | wc -w`
500
if ( $nlines != $nvars ) then
501
  echo " ERROR: tracevars-files must not contain empty lines ***"
502
  exit 1
503
endif
504
set calf=`cat $tvfile | awk '{print $3}'`
505
set tfil=`cat $tvfile | awk '{print $4}'`
5 michaesp 506
 
507
# Write some status information
508
cat ${tvfile}
509
echo
510
echo "# Number of tracing variables        : ${nlines}"
511
echo "Fields are read from following files : ${tfil}"
512
 
513
# Loop over all variables - check availability
3 michaesp 514
foreach v ( $vars )
515
  if ( $calf[1] == 0 ) then
516
    set v0 = `echo $v | awk 'BEGIN {FS = ":"}; {print $1}'`
39 michaesp 517
    ${LAGRANTO}/goodies/getvars $tfil[1]$datfiles[1]
518
 
5 michaesp 519
    set flag=`${LAGRANTO}/goodies/getvars $tfil[1]$datfiles[1] | grep " $v0 " | wc -l`
3 michaesp 520
    set iscomment=`echo $v0 | cut -c 1` 
39 michaesp 521
 
522
    if ( "$v0" == "PMIN" ) set flag = 1
523
    if ( "$v0" == "PMAX" ) set flag = 1
524
 
3 michaesp 525
    if ( "${iscomment}" != "#" ) then 
526
	 if ( $flag == 0 ) then
527
           echo " ERROR: variable $v listed in $tvfile is not on the $tfil[1]-files ***"
528
           exit 1
529
	 endif
530
    endif
531
  endif
532
  shift calf
533
  shift tfil
534
end
535
set ntrace=${nlines} 
536
 
537
# ---------------------------------------------------------------------
538
# Prepare input file for trace and run it
539
# ---------------------------------------------------------------------
540
 
541
# Set times relative to the reference date
542
if ( "${changet}" == "true" ) then
543
  echo
544
  echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
545
  echo   
546
  foreach i ( $datfiles )
547
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}
548
  end
549
  if ( ${sfiles} == 1 ) then
550
    foreach i ( $datfiles )
551
      ${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}
552
    end
553
  endif
554
endif
555
 
556
# ---------------------------------------------------------------------
557
# Prepare input file for caltra and run it
558
# ---------------------------------------------------------------------
559
 
560
# Write parameter file
561
\rm -f ${parfile}
562
touch ${parfile}
563
 
564
echo $inpfile                                              >> $parfile
565
echo $outfile                                              >> $parfile    
566
echo $startdate                                            >> $parfile
567
echo $enddate                                              >> $parfile
568
echo $idir                                                 >> $parfile
569
echo $numfiles                                             >> $parfile
570
foreach i ( $datfiles )
571
  echo $i                                                  >> $parfile
572
end
573
echo $timeinc                                              >> $parfile
574
echo $tstart                                               >> $parfile
575
echo $tend                                                 >> $parfile
576
echo $ntra                                                 >> $parfile
577
echo $ntim                                                 >> $parfile
578
echo $ncol                                                 >> $parfile
579
echo $ntrace                                               >> $parfile
580
cat ${tvfile}                                              >> $parfile
581
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1] | wc -l   >> $parfile
582
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]           >> $parfile
583
if ( $sfiles == 1 ) then
584
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1] | wc -l >> $parfile
585
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]         >> $parfile
586
else
587
  echo 0                                                   >> $parfile
588
endif
589
echo \"${timecheck}\"                                      >> $parfile
590
echo \"${intmode}\"                                        >> $parfile
591
echo ${radius}                                             >> $parfile # Bojan circle mode
5 michaesp 592
echo ${tropo_pv}                                           >> $parfile # Bojan clustering mode
593
echo ${tropo_th}                                           >> $parfile # Bojan clustering mode
3 michaesp 594
 
595
# Finish the preprocessor
596
echo 
597
echo '       *** END OF PREPROCESSOR TRACE ***              '
598
echo '========================================================='
599
echo
600
 
601
# Run  trace
602
${prog}
603
 
604
if ( "${status}" != "0" ) then
605
  echo "ERROR:  Program <trace> failed"
606
  exit 1
607
endif
608
 
609
# ---------------------------------------------------------------------
610
# Final tasks (make clean)
611
# ---------------------------------------------------------------------
612
 
613
finish:
614
 
615
if ( "${noclean}" == "false" ) then
616
  \rm -f ${parfile}
617
endif
618
 
619
exit 0 
620