Subversion Repositories lagranto.20cr

Rev

Details | Last modification | View Log | RSS feed

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