Subversion Repositories lagranto.wrf

Rev

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