Subversion Repositories lagranto.icon

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