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