Subversion Repositories lagranto.ecmwf

Rev

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