Subversion Repositories lagranto.ecmwf

Rev

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