Subversion Repositories lagranto.ecmwf

Rev

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