Subversion Repositories lagranto.icon

Rev

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