Subversion Repositories lagranto.um

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