Subversion Repositories lagranto.wrf

Rev

Rev 11 | Rev 23 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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