Subversion Repositories lagranto.ecmwf

Rev

Rev 3 | Go to most recent revision | 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
 
312
# Take the time increment from flag list ('nil', if not defined)
313
set timeinc = ${flag_i}
314
 
315
# Find a first data file (if possible corresponding to start/end date
316
# If starttime is not a data time, take the first file in the direectory
317
if ( $direction == "f" ) then
318
  set file=${charp}${startdate_ymdh}
319
else
320
  set file=${charp}${enddate_ymdh}
321
endif
322
if ( ! -f $file ) then
323
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
324
endif
325
 
326
# Determine timeinc (the time difference in hours between two data file)
327
# if not already defined with option -i
328
if ( ${timeinc} == "nil" ) then
329
  set date1=`echo $file | cut -c 2-12`
330
  set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
331
  @ n ++
332
  set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-12`
333
  set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
334
endif
335
if ( $timeinc == 0 ) then
336
    echo " ERROR: cannot set the time increment between input files ***"
337
    exit 1
338
endif
339
 
340
# Search the first file to use: We step through all P files and see whether they are
341
# good P files. Let's first do the test for the first data file found. If it's ok, we 
342
# take it; if not, we step through all P files and find the good one  
343
set flag=0
344
set td=
345
 
346
set date = `echo $file | cut -c 2-12`
347
set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
348
set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
349
 
350
if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
351
   set datfiles=$date
352
   if ( $td1 < $timeinc    ) set td=$td1
353
   if ( $td2 < $timeinc    ) set td=$td2
354
   if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
355
   goto label2      
356
endif
357
 
358
foreach i ( ${charp}????????_?? )
359
 
360
  set date = `echo $i | cut -c 2-12`
361
  set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
362
  set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
363
 
364
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
365
      set datfiles=$date
366
      if ( $td1 < $timeinc    ) set td=$td1
367
      if ( $td2 < $timeinc    ) set td=$td2
368
      if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
369
      goto label2
370
  endif
371
 
372
end
373
 
374
# if no P/T-files are available for the specified time period, then $td is
375
# still undefined
376
if ( $td == "" ) then
377
  echo " ERROR: no data files available for the specified time period"
378
  exit 1
379
endif
380
 
381
# Everything is fine so far: proceed
382
label2:
383
 
384
# Check whether first date is ok - before or at needed dates
385
if ( $direction == "f" ) then
386
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
387
else
388
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${date}`
389
endif
390
  if ( $tdiff0 < 0 ) then
391
  echo " ERROR: data files missing for the specified time period"
392
  exit 1
393
endif
394
 
395
# Calculate the number of further files
396
@ num = ( $timediff_hh + $td ) / $timeinc + 1
397
@ dum1 = ( $num - 1 ) * $timeinc
398
@ dum2 = $timediff_hh + $td
399
if ( $dum1 != $dum2 ) @ num ++
400
 
401
# Get a list of all needed files
402
set numfiles=$num
403
set sfiles=1
404
while ( $num > 1 )
405
 
406
  set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
407
  if ( ! -f ${charp}${date} ) then
408
    echo " ERROR: file with primary data is missing for $date"
409
    exit 1
410
  else if ( ! -f ${chars}${date} ) then
411
    set sfiles=0
412
    set datfiles=`echo $datfiles $date`
413
  else
414
    set datfiles=`echo $datfiles $date`
415
  endif
416
  @ num --
417
end
418
 
419
# Calculate the start and the end time relative to the first datfile
420
if ( $direction == f ) then
421
  set tstart = `${LAGRANTO}/goodies/gettidiff $startdate $datfiles[1]`
422
  set tend   = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $enddate`
423
else
424
  set tstart = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $startdate`
425
  set tend   = `${LAGRANTO}/goodies/gettidiff $enddate $datfiles[1]`
426
endif
427
 
428
 
429
# Write some status information
430
echo "Primary file prefix               : ${charp}"
431
echo "Secondary file prefix             : ${chars}"
432
echo "Time increment for input files    : ${timeinc}"
433
echo "# input files                     : ${numfiles}"
434
echo "First input file                  : $datfiles[1] " 
435
echo "Last input file                   : $datfiles[$numfiles] " 
436
echo "${charp} files availability              : 1"  
437
echo "${chars} files availability              : ${sfiles}"     
438
if ( $direction == f ) then
439
echo "Start time relative to first file : $datfiles[1] + ${tstart} "
440
echo "End time relative to last file    : $datfiles[$numfiles] - ${tend} "  
441
else
442
echo "Start time relative to last file  : $datfiles[$numfiles] - ${tstart} "
443
echo "End time relative to first file   : $datfiles[1] + ${tend} "
444
endif
445
 
446
# ---------------------------------------------------------------------
447
# Check availability of input data 
448
# ---------------------------------------------------------------------
449
 
450
echo
451
echo '---- TRACEVAR FILE --------------------------------------'
452
echo    
453
 
454
# If "-f" option is used, create a temporary tracevar file
455
if ( "${flag_f}" == "-f" ) then
456
 
457
#   Preset values for <compfl> and <tprefix>
458
    set tcompfl=1
459
    set tprefix='P'
460
 
461
#   Check availability on P file
462
    foreach var ( `${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]` )
463
       if ( "${var}" == "${tvar}" ) then
464
          set tcompfl=0
465
	  set tprefix="P"
466
       endif
467
    end
468
 
469
#   Check availability on S file 
470
    if ( ${sfiles} == 1 ) then
471
       foreach var ( `${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]` )
472
         if ( "${var}" == "${tvar}" ) then
473
            set tcompfl=0
474
	    set tprefix="S"
475
         endif
476
       end
477
    endif
478
 
479
#   Write the temporary <tracevars> file
480
    echo "${tvar} ${tscale} ${tcompfl} ${tprefix}" >! ${tvfile}
481
    echo "Temporary tracervar file <${tvfile}> created"
482
    echo
483
 
484
endif
485
 
486
 
487
# Check if tracevars-file exists
488
if ( ! -f $tvfile ) then
489
  echo  " ERROR:  file $tvfile was not found ***"
490
  exit 1
491
endif
492
 
493
# check if the variables contained in the tracevars-file are available in the
494
# data file and check also if there are no empty lines in the tracevars-file
495
 
496
set nlines = `cat $tvfile | wc -l`
497
set vars   = `cat $tvfile | awk '{print $1}'`
498
set nvars  = `echo $vars | wc -w`
499
if ( $nlines != $nvars ) then
500
  echo " ERROR: tracevars-files must not contain empty lines ***"
501
  exit 1
502
endif
503
set calf=`cat $tvfile | awk '{print $3}'`
504
set tfil=`cat $tvfile | awk '{print $4}'`
5 michaesp 505
 
506
# Write some status information
507
cat ${tvfile}
508
echo
509
echo "# Number of tracing variables        : ${nlines}"
510
echo "Fields are read from following files : ${tfil}"
511
 
512
# Loop over all variables - check availability
3 michaesp 513
foreach v ( $vars )
514
  if ( $calf[1] == 0 ) then
515
    set v0 = `echo $v | awk 'BEGIN {FS = ":"}; {print $1}'`
5 michaesp 516
    set flag=`${LAGRANTO}/goodies/getvars $tfil[1]$datfiles[1] | grep " $v0 " | wc -l`
3 michaesp 517
    set iscomment=`echo $v0 | cut -c 1` 
518
    if ( "${iscomment}" != "#" ) then 
519
	 if ( $flag == 0 ) then
520
           echo " ERROR: variable $v listed in $tvfile is not on the $tfil[1]-files ***"
521
           exit 1
522
	 endif
523
    endif
524
  endif
525
  shift calf
526
  shift tfil
527
end
528
set ntrace=${nlines} 
529
 
530
# ---------------------------------------------------------------------
531
# Prepare input file for trace and run it
532
# ---------------------------------------------------------------------
533
 
534
# Set times relative to the reference date
535
if ( "${changet}" == "true" ) then
536
  echo
537
  echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
538
  echo   
539
  foreach i ( $datfiles )
540
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}
541
  end
542
  if ( ${sfiles} == 1 ) then
543
    foreach i ( $datfiles )
544
      ${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}
545
    end
546
  endif
547
endif
548
 
549
# ---------------------------------------------------------------------
550
# Prepare input file for caltra and run it
551
# ---------------------------------------------------------------------
552
 
553
# Write parameter file
554
\rm -f ${parfile}
555
touch ${parfile}
556
 
557
echo $inpfile                                              >> $parfile
558
echo $outfile                                              >> $parfile    
559
echo $startdate                                            >> $parfile
560
echo $enddate                                              >> $parfile
561
echo $idir                                                 >> $parfile
562
echo $numfiles                                             >> $parfile
563
foreach i ( $datfiles )
564
  echo $i                                                  >> $parfile
565
end
566
echo $timeinc                                              >> $parfile
567
echo $tstart                                               >> $parfile
568
echo $tend                                                 >> $parfile
569
echo $ntra                                                 >> $parfile
570
echo $ntim                                                 >> $parfile
571
echo $ncol                                                 >> $parfile
572
echo $ntrace                                               >> $parfile
573
cat ${tvfile}                                              >> $parfile
574
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1] | wc -l   >> $parfile
575
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]           >> $parfile
576
if ( $sfiles == 1 ) then
577
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1] | wc -l >> $parfile
578
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]         >> $parfile
579
else
580
  echo 0                                                   >> $parfile
581
endif
582
echo \"${timecheck}\"                                      >> $parfile
583
echo \"${intmode}\"                                        >> $parfile
584
echo ${radius}                                             >> $parfile # Bojan circle mode
5 michaesp 585
echo ${tropo_pv}                                           >> $parfile # Bojan clustering mode
586
echo ${tropo_th}                                           >> $parfile # Bojan clustering mode
3 michaesp 587
 
588
# Finish the preprocessor
589
echo 
590
echo '       *** END OF PREPROCESSOR TRACE ***              '
591
echo '========================================================='
592
echo
593
 
594
# Run  trace
595
${prog}
596
 
597
if ( "${status}" != "0" ) then
598
  echo "ERROR:  Program <trace> failed"
599
  exit 1
600
endif
601
 
602
# ---------------------------------------------------------------------
603
# Final tasks (make clean)
604
# ---------------------------------------------------------------------
605
 
606
finish:
607
 
608
if ( "${noclean}" == "false" ) then
609
  \rm -f ${parfile}
610
endif
611
 
612
exit 0 
613