Subversion Repositories lagranto.ecmwf

Rev

Rev 5 | 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} < 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
 
295
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
296
     set datfiles=$date
297
     if ( $td1 < $timeinc    ) set td=$td1
298
     if ( $td2 < $timeinc    ) set td=$td2
299
     if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
300
     goto label2      
301
  endif
302
 
303
end
304
 
305
# if no P/T-files are available for the specified time period, then $td is
306
# still undefined
307
if ( $td == "" ) then
308
  echo " ERROR: no data files available for the specified time period"
309
  exit 1
310
endif
311
 
312
# Everything is fine so far: proceed
313
label2:
314
 
315
# Check whether first date is ok - before or at needed dates
316
if ( $direction == "f" ) then
317
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
318
else
319
  set tdiff0 = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${date}`
320
endif
321
  if ( $tdiff0 < 0 ) then
322
  echo " ERROR: data files missing for the specified time period"
323
  exit 1
324
endif
325
 
326
# Calculate the number of further files
327
@ num = ( $timediff_hh + $td ) / $timeinc + 1
328
@ dum1 = ( $num - 1 ) * $timeinc
329
@ dum2 = $timediff_hh + $td
330
if ( $dum1 != $dum2 ) @ num ++
331
 
332
# Get a list of all needed files
333
set numfiles=$num
334
set sfiles=1
335
echo $datfiles
336
while ( $num > 1 )
337
  set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
338
 
339
  echo $date
340
 
341
  if ( ! -f ${charp}${date} ) then
342
    echo " ERROR: file with primary data is missing for $date"
343
    exit 1
344
  else if ( ! -f ${chars}${date} ) then
345
    set sfiles=0
346
    set datfiles=`echo $datfiles $date`
347
  else
348
    set datfiles=`echo $datfiles $date`
349
  endif
350
  @ num --
351
end
352
 
353
# Write some status information
354
echo "Primary file prefix            : ${charp}"
355
echo "Secondary file prefix          : ${chars}"
356
echo "Time increment for input files : ${timeinc}"
357
echo "# input files                  : ${numfiles}"
358
echo "${charp} files availability           : 1"  
359
echo "${chars} files availability           : ${sfiles}"     
360
echo "First input file               : $datfiles[1] " 
361
echo "Last input file                : $datfiles[$numfiles] " 
362
 
363
# ---------------------------------------------------------------------
364
# Handle vertical wind - scaling factor
365
# ---------------------------------------------------------------------
366
 
367
echo
368
echo '---- VERTICAL WIND SCALING ------------------------------'
369
echo
370
 
371
# Check whether OMEGA is avialbale on first file
372
set first=$datfiles[1]
373
set file="${charp}${first}"
374
set omflag=`${LAGRANTO}/goodies/getvars $file | grep " OMEGA " | wc -l`
375
if ( $omflag != 1 ) then
376
   echo " ERROR: variable OMEGA is not on file $file"
377
   exit 1
378
endif
379
 
380
# Get the maximum and minimum wind on 850 hPa and decide the <wfactor>
381
set wmin  = `${LAGRANTO}/goodies/getmima $file OMEGA P850 | awk '{print $1}'`
382
set wmax  = `${LAGRANTO}/goodies/getmima $file OMEGA P850 | awk '{print $2}'`
383
set wmin  = `echo $wmin | sed -e 's/\.//'`
384
set wmax  = `echo $wmax | sed -e 's/\.//'`
385
set wdiff = `echo $wmax - $wmin | bc`
386
 
387
if ( $wdiff > 400 ) then
388
    set wfactor=1.
389
else
390
    set wfactor=100.
391
endif
392
 
393
# Write status information
394
echo "Vertical scaling factor (wfactor)  : ${wfactor}"
395
 
396
# ---------------------------------------------------------------------
397
# Time step and output interval
398
# ---------------------------------------------------------------------
399
 
400
echo
401
echo '---- TIME STEPS -----------------------------------------'
402
echo
403
 
404
# Take the time step and output step from flag list ('nil', if not defined)
405
set timestep = ${flag_t}
406
set deltout  = ${flag_o}
407
 
408
# Calculate the time step
409
if ( $timestep == "nil" ) @ timestep = ( 60 * $timeinc ) / 12
410
 
411
# Take the output interval from time increment
412
if ( $deltout == "nil" ) @ deltout = 60 * ${timeinc}
413
 
414
# Check whether the timestep is an integer ratio of deltout
415
@ flag = $deltout / $timestep
416
@ flag = $deltout - ( $flag * $timestep )
417
if ( $flag != 0 ) then
418
  echo " ERROR: output time interval should be multiple of timestep"
419
  echo 
420
  echo "           $deltout min  : output time interval"
421
  echo "           $timestep min  : time step"
422
  exit 1
423
endif
424
 
425
# Calculate the start and the end time relative to the first datfile
426
if ( $direction == f ) then
427
  set tstart = `${LAGRANTO}/goodies/gettidiff $startdate $datfiles[1]`
428
  set tend   = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $enddate`
429
else
430
  set tstart = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $startdate`
431
  set tend   = `${LAGRANTO}/goodies/gettidiff $enddate $datfiles[1]`
432
endif
433
 
434
# Check whether tstart and tend are a multiple of the output time interval
435
if ( $tstart != 0 ) then
436
  if ( `echo $tstart | grep "\."` != "" ) then
437
    set dum=`echo $tstart | sed -e 's/[-0-9]*\.//'`
438
    @ flag = $dum / $deltout
439
    @ flag = $dum - ( $flag * $deltout )
440
    if ( $flag != 0 ) then
441
      echo " ERROR : the start time should be shifted relative to the first"
442
      echo "         datafile by a multiple of the output time interval"
443
      echo "         hint:  set the latter with the option -o"
444
      exit 1
445
    endif
446
  endif
447
endif
448
if ( `echo $tend | grep "\."` != "" ) then
449
  set dum=`echo $tend | sed -e 's/[-0-9]*\.//'`
450
  @ flag = $dum / $deltout
451
  @ flag = $dum - ( $flag * $deltout )
452
  if ( $flag != 0 ) then
453
    echo " ERROR : the end time should be shifted relative to the first"
454
    echo "         datafile by a multiple of the output time interval"
455
    echo "         hint:  set the latter with the option -o"
456
    exit 1
457
  endif
458
endif
459
 
460
# Write status information
461
echo "Trajectory calculation time step [min] : ${timestep}"
462
echo "Output time step [min]                 : ${deltout}"
463
if ( $direction == f ) then
464
  echo "Start time relative to first file      : $datfiles[1] + ${tstart} "
465
  echo "End time relative to first file        : $datfiles[$numfiles] - ${tend} "  
466
else
467
  echo "Start time relative to first file      : $datfiles[$numfiles] - ${tstart} "
468
  echo "End time relative to last file         : $datfiles[1] + ${tend} "
469
endif
470
 
471
# ---------------------------------------------------------------------
472
# Start file
473
# ---------------------------------------------------------------------
474
 
475
echo
476
echo '---- START FILE -----------------------------------------'
477
echo
478
 
479
# Check if start file is available
480
if ( ! -f ${startf} ) then
481
   echo " ERROR : start file ${startf} is missing"
482
   exit 1
483
endif
484
 
485
# Decide whether startfile has an explicit format specifier
486
set format = "0"
487
foreach app ( 1 2 3 4 5 6 7 8 9 )
488
  set flag = `echo ${startf} | grep ".${app}"`
489
  if ( "${flag}" != "" ) set format = "${app}"
490
end
491
 
492
# If format is 0, it might nevertheless be a hidden format 1
493
if ( "${format}" == "0" ) then
494
    set ncol = `awk "{print NF}" ${startf} | tail -1` 
495
    if ( "${ncol}" != "3" ) then
496
        set format = "1"
497
        echo " WARNING: ${startf} is a hidden trajectory file of format 1"
498
        echo "          it will be renamed: ${startf} -> ${startf}.1"
499
        echo
500
        ln -sf ${startf} ${startf}.1
501
        set startf = "${startf}.1"
502
    endif
503
endif
504
 
505
# Get the number of trajectories
506
if ( "${format}" == "0" ) then
507
   set ntra = `wc -l ${startf} | awk '{print $1}' `
508
   set ncol = 3
509
else
510
   set ntra = `${LAGRANTO}/goodies/trainfo.sh ${startf} ntra`
511
   set ncol = `${LAGRANTO}/goodies/trainfo.sh ${startf} ncol`
512
   set ntim = `${LAGRANTO}/goodies/trainfo.sh ${startf} ntim`
513
 
514
   if ( "${ntim}" != "1" ) then
515
      echo " ERROR: starting trajectory file must only have one time... Stop"
516
      exit 1
517
   endif
518
 
519
endif
520
 
521
# Write status information
522
echo "Start file                  : ${startf} "
523
if ( "${format}" == "0" ) then 
524
   echo "Format                      : (lon,lat,p) list"
525
else
526
   echo "Format                      : trajectory file (${format})"
527
endif
528
echo "# coordinates (lon,lat,lev) : ${ntra} "  
529
echo "# columns                   : ${ncol} "  
530
 
531
# ---------------------------------------------------------------------
532
# Prepare input file for caltra and run it
533
# ---------------------------------------------------------------------
534
 
535
# Set times relative to the reference date
536
if ( "${changet}" == "true" ) then
537
 echo
538
 echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
539
 echo   
540
 foreach i ( $datfiles )
541
   ${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}
542
 end
543
 if ( ${sfiles} == 1 ) then
544
   foreach i ( $datfiles )
545
     ${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}
546
   end
547
 endif
548
endif
549
 
550
# Split the reference date
551
set yyyy=`echo ${refdate}   | cut -c 1-4` 
552
set   mm=`echo ${refdate}   | cut -c 5-6` 
553
set   dd=`echo ${refdate}   | cut -c 7-8` 
554
set   hh=`echo ${refdate}   | cut -c 10-11` 
555
set  min=`echo ${refdate}00 | cut -c 12-13` 
556
 
557
# Get the total tiem range
558
if ( ${timediff_mm} != '' ) then
559
   @ timerange = 60 * ${timediff_hh} + ${timediff_mm}
560
else
561
   @ timerange = 60 * ${timediff_hh}
562
endif
563
 
564
# Write parameter file
565
\rm -f ${parfile}
566
touch ${parfile}
567
 
568
\echo $idir                   >> $parfile
569
echo $numfiles                >> $parfile
570
foreach i ( $datfiles )
571
  echo $i                     >> $parfile
572
end
573
echo $timeinc                 >> $parfile
574
echo $flag_p                  >> $parfile
575
echo \"${startf}\"            >> $parfile
576
echo ${ntra}                  >> $parfile
577
echo ${ncol}                  >> $parfile
578
echo \"${outfile}\"           >> $parfile
579
echo $timestep                >> $parfile
580
echo $tstart                  >> $parfile
581
echo $tend                    >> $parfile
582
echo $deltout                 >> $parfile
583
echo $flag_j                  >> $parfile
584
echo $wfactor                 >> $parfile
585
echo $yyyy                    >> $parfile
586
echo $mm                      >> $parfile
587
echo $dd                      >> $parfile
588
echo $hh                      >> $parfile
589
echo $min                     >> $parfile
590
echo $timerange               >> $parfile
591
echo \"${timecheck}\"         >> $parfile
592
 
593
# Finish the preprocessor
594
echo 
595
echo '       *** END OF PREPROCESSOR CALTRA ***              '
596
echo '========================================================='
597
echo
598
 
599
# Run caltra
600
${LAGRANTO}/caltra/caltra
601
 
602
if ( "${status}" != "0" ) then
603
  echo "ERROR:  Program <caltra> failed"
604
  exit 1
605
endif
606
 
607
# ----------------------------------------------------------
608
# Final tasks (make clean)
609
# ----------------------------------------------------------
610
 
611
finish:
612
 
613
if ( "${noclean}" == "false" ) then
614
  \rm -f ${parfile}
615
 endif
616
 
617
exit 0 
618