Subversion Repositories lagranto.wrf

Rev

Rev 21 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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