Subversion Repositories lagranto.20cr

Rev

Details | Last modification | View Log | RSS feed

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