Subversion Repositories lagranto.ocean

Rev

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