Subversion Repositories lagranto.20cr

Rev

Details | Last modification | View Log | RSS feed

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