Subversion Repositories lagranto.um

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
5
# ---------------------------------------------------------------------
6
 
7
# Write usage information
8
if ( (${#argv} == 0) ) then
9
  echo 
10
  echo "-----------------------------------------------------------------------------------"
11
  echo "Usage: "
12
  echo 
13
  echo "  1. lagranto {caseid[.label]} startdate enddate {startf} {select} [flags] "
14
  echo
15
  echo "         caseid     : case     (name of case directories; label appears in output directoy)"
16
  echo "                      local    (everything is run in local directory)"
17
  echo "                      interim  (take wind fields from Era-Interim)"
18
  echo "                      analysis (take wind fields from ECMWF operational analysis)"
19
  echo "                      forecast (take wind fields from ECMWF operational forecast)" 
20
  echo
21
  echo "         startdate  : start date in format yyyymmdd_hh(mm)"
22
  echo 
23
  echo "         enddate    : end date in format yyyymmdd_hh(mm)"
24
  echo  
25
  echo "         startf     : [ file | criterion ] with starting coordinates (lon,lat,p)"
26
  echo
27
  echo "         select     : [ file | criterion ] for trajectory selection (set 'nil' for none)"
28
  echo
29
  echo "         flags      : -o     outfile   (name of output trajectory file) "
30
  echo "                      -j               (jumping flag for crossing lower boundary)"
31
  echo "                      -v     filename  (name of tracing file)"
32
  echo "                      -r     filename  (name of region file)"
33
  echo "                      -noclean         (do not clean trajectory directory after run)"
34
  echo "                      -prep            (prepare run script, but do not run lagranto)"
35
  echo "                      -log             (logfile is written to screen instead of logfile)"
36
  echo "                      -changet         (change times on netCDF files realtive to startdate)"
37
  echo
38
  echo "  2. lagranto [ -open | -remove ] caseid[.label] "
39
  echo
40
  echo "        -open   : change to the run directory [open a new Xterm window]"
41
  echo "        -remove : remove the whole run directory"
42
  echo
43
  echo "Examples:"
44
  echo 
45
  echo "-----------------------------------------------------------------------------------"
46
  exit 0
47
endif
48
 
49
# Check whether LAGRANTO was set with setenv
50
set test = `setenv | grep LAGRANTO` 
51
if ( "${test}" == "" ) then
52
   echo "---------------------------------------------------------------------"
53
   echo "Please set environmental variable <LAGRANTO> to your source directory"
54
   echo 'csh > setenv LAGRANTO ${TO_YOUR_LAGRANTO_INSTALLATION}'
55
   echo "---------------------------------------------------------------------"
56
   exit 1
57
endif
58
 
59
# ---------------------------------------------------------------------
60
# Set some general parameters
61
# ---------------------------------------------------------------------
62
 
63
# Set the prefix of the primary and secondary data files
64
set charp = 'P'
65
set chars = 'S'
66
 
67
# Set the name of the run script
68
set runscript = "runscript"
69
 
70
# Remember the calling directory
71
set calldir = $PWD
72
 
73
# ---------------------------------------------------------------------
74
# Parameter handling
75
# ---------------------------------------------------------------------
76
 
77
# Write title
78
echo
79
echo '========================================================='
80
echo '             *** START OF LAGRANTO ***             '
81
echo
82
 
83
# Save the complete argument list
84
set lagranto_call = "$*"
85
 
86
echo '---- HANDLE ARGUMENTS -----------------------------------'
87
echo
88
 
89
# Get fixed arguments
90
if (${#argv} == 1) then
91
  set mode = '-open'
92
  set caseid    = "$1"
93
  shift
94
 
95
else if (${#argv} == 2) then
96
  set mode      = "$1"
97
  set caseid    = "$2"
98
  shift
99
  shift
100
 
101
else
102
  set caseid    = "$1"
103
  set startdate = "$2"
104
  set enddate   = "$3"
105
  set startf    = "$4"
106
  set select    = "$5"
107
  set mode      = "runlagranto"
108
  shift
109
  shift
110
  shift
111
  shift
112
  shift
113
 
114
endif
115
 
116
# Split <caseid> into base and appendix 
117
set casebase = `(echo $caseid | awk -F "." '{print $1}')`   
118
set appendix = `(echo $caseid | awk -F "." '{print $2}')`   
119
if ( "${appendix}" != "" ) then
120
  set appendix = ".${appendix}"
121
endif
122
 
123
# Set the directories (depending on <caseid>)
124
if ( "${casebase}" == "local" ) then
125
  set cdfdir = $PWD
126
  set tradir = $PWD
127
 
128
else if ( "${casebase}" == "interim" ) then
129
  set cdfdir = /net/dansgaard/atmosdyn/erainterim/cdf/
130
  set tradir = $PWD
131
 
132
else if ( "${casebase}" == "analysis" ) then
133
  set cdfdir = 'TO_BE_SPECIFIED'
134
  set tradir = $PWD
135
  echo "ERROR: caseid (analysis) not yet implemented"
136
  exit 1  
137
 
138
else if ( "${casebase}" == "forecast" ) then
139
  set cdfdir = 'TO_BE_SPECIFIED'
140
  set tradir = $PWD
141
  echo "ERROR: caseid (forecast) not yet implemented"
142
  exit 1  
143
 
144
else
145
  set cdfdir = ${HOME}/cdf/${casebase}
146
  set tradir = ${HOME}/tra/${casebase}
147
endif
148
 
149
# Change to the run directory if requested
150
if ( "${mode}" == "-open" ) then
151
   set count = 0   
152
   foreach folder ( ${tradir}/ntr_*_${caseid}_* )
153
     @ count = ${count} + 1
154
     echo "[${count}] ${folder}"
155
   end
156
   echo
157
   echo "Please select directory [0...${count}]"
158
   set id = $<
159
   set count = 0   
160
   foreach folder ( ${tradir}/ntr_*_${caseid}_* )
161
     @ count = ${count} + 1
162
     if ( "${count}" == "${id}" ) then 
163
        cd ${folder}
164
        xterm &
165
     endif
166
   end
167
   exit 0
168
endif
169
 
170
# Remove the whole run directory if requested
171
if ( "${mode}" == "-remove" ) then
172
   set count = 0   
173
   foreach folder ( ${tradir}/ntr_*_${caseid}_* )
174
     @ count = ${count} + 1
175
     echo "[${count}] ${folder}"
176
   end
177
   echo
178
   echo "Please select directory [0...${count}, all]"
179
   set id = $<
180
   set count = 0   
181
   foreach folder ( ${tradir}/ntr_*_${caseid}_* )
182
     @ count = ${count} + 1
183
     if ( ( "${count}" == "${id}" ) | ( "${id}" == "all" ) ) then 
184
        \rm -r ${folder}
185
     endif
186
   end
187
   exit 0
188
endif
189
 
190
# Show the trajectory file
191
if ( "${mode}" == "-show" ) then
192
   set count = 0   
193
   foreach folder ( ${tradir}/ntr_*_${caseid}_* )
194
     @ count = ${count} + 1
195
     echo "[${count}] ${folder}"
196
   end
197
   echo
198
   echo "Please select directory [0...${count}]"
199
   set id = $<
200
   set count = 0   
201
   foreach folder ( ${tradir}/ntr_*_${caseid}_* )
202
     @ count = ${count} + 1
203
     if ( "${count}" == "${id}" ) then 
204
        cd ${folder}
205
        set filename = ` sed -ne '9,9p' runscript.sh | cut -c 2-` 
206
	${LAGRANTO}/goodies/trainfo.sh ${filename} list | less
207
     endif
208
   end
209
   exit 0
210
endif
211
 
212
# Decide whether <select> is a file or an explicit criterion
213
set flag_select = 'file'
214
set test = `echo ${select} | grep ':' | wc -c`
215
if ( "${test}" != "0" ) then 
216
  set flag_select = 'criterion'
217
endif
218
 
219
# Decide whether <startf> is a file or an explicit criterion
220
set flag_startf = 'file'
221
 
222
set test = `echo ${startf} | grep ' ' | wc -c`
223
if ( "${test}" != "0" ) then 
224
  set flag_startf = 'point'
225
endif
226
 
227
set test = `echo ${startf} | grep '@' | wc -c`
228
if ( "${test}" != "0" ) then 
229
  set flag_startf = 'criterion'
230
endif
231
 
232
 
233
# Write some status information
234
echo " Case ID       : ${caseid}"
235
echo " Start date    : ${startdate}"
236
echo " End date      : ${enddate}"
237
 
238
if ( "${flag_startf}" == "criterion" ) then
239
    echo " Start file    : ${startf} [criterion] -> startf.criterion [file]"
240
else if ( "${flag_startf}" == "file" ) then
241
    echo " Start points  : ${startf} [file]"
242
else if ( "${flag_startf}" == "point" ) then
243
    echo " Start points  : ${startf} [point] -> startf [file]"
244
endif
245
 
246
if ( "${flag_select}" == "criterion" ) then
247
    echo " Selection     : ${select} [criterion] -> selectf.criterion [file]"
248
else
249
    echo " Selection     : ${select} [file]"
250
endif    
251
 
252
echo
253
 
254
# Set defaults for flags
255
set jumpflag   = ""
256
set tracefile  = "tracevars"
257
set outfile    = "lsl_${startdate}"
258
set keepflag   = ""
259
set prepflag   = ""
260
set logfile    = "${runscript}.logfile"
261
set regionf    = "regionf"
262
set changet    = "false"
263
 
264
# Handle flags
265
while ( $#argv > 0 )
266
 
267
  switch ( $argv[1] )
268
 
269
  echo $argv[1]
270
 
271
   case -j
272
     set jumpflag = "-j"
273
     echo " Flag '-j [jump]'             -> True "
274
     echo
275
   breaksw
276
 
277
   case -prep
278
     set prepflag = "prep"
279
     echo " Flag '-prep [prepare only]'  -> ${prepflag} "
280
     echo
281
   breaksw
282
 
283
   case -noclean
284
    set keepflag = "keep"
285
     echo " Flag '-noclean [no cleaning]' -> ${keepflag} "
286
     echo
287
   breaksw
288
 
289
   case -o
290
     set outfile =  $argv[2]
291
     echo " Flag '-o [output]'            -> ${outfile} "
292
     echo
293
   breaksw
294
 
295
   case -v
296
     set tracefile = $argv[2]
297
     echo " Flag '-v [tracevars]'         -> ${tracefile} "
298
     echo
299
     shift
300
   breaksw
301
 
302
   case -r
303
     set regionfile = $argv[2]
304
     echo " Flag '-r [regionfile]'        -> ${regionfile} "
305
     echo
306
     shift
307
   breaksw
308
 
309
   case -log
310
     set logfile = "/dev/stdout"
311
     echo " Flag '-v [log]'               -> ${logfile} "
312
   breaksw
313
 
314
   case -changet
315
     set changet = "true"
316
     echo " Flag '-changet [change times]'-> True "
317
     echo
318
   breaksw
319
 
320
  endsw
321
 
322
  shift;
323
 
324
end
325
 
326
# Decide whether a tracevars file is given or not
327
if ( -f ${tradir}/${tracefile} ) then
328
   set flag_tracevars = 'file'
329
else
330
   set flag_tracevars = 'nil'
331
endif
332
 
333
echo
334
if ( "${flag_tracevars}" == "file" ) then
335
    echo " Trace Vars    : ${tracefile} [file]"
336
else
337
    echo " Trace Vars    : [no fields are traced]"
338
endif    
339
echo
340
 
341
# Set the name of the run directory (forward/backward distinction)
342
set dir   = 'f'
343
set range = `${LAGRANTO}/bin/gettidiff.sh ${enddate} ${startdate}`
344
if ( ${range} < 0 ) then
345
  set dir   = 'b'
346
  set range = `${LAGRANTO}/bin/gettidiff.sh ${startdate} ${enddate}`
347
endif
348
set rundir = ${tradir}/ntr_${startdate}_${dir}${range}_${caseid}
349
 
350
if ( "${flag_startf}" == "criterion" ) then
351
  set rundir = "${rundir}_startf"
352
else if ( "${flag_startf}" == "point" ) then
353
  set rundir = "${rundir}_startf"
354
else if ( "${flag_startf}" == "file" ) then
355
  set rundir = "${rundir}_${startf}"
356
endif
357
 
358
if (  "${flag_select}" != "file" ) then
359
  set rundir = "${rundir}_selectf"
360
else
361
 set rundir = "${rundir}_${select}"
362
endif
363
 
364
# Write directory names
365
echo '---- DIRECTORIES ----------------------------------------'
366
echo
367
echo " CDF = ${cdfdir}"
368
echo " TRA = ${tradir}"
369
echo " RUN = ${rundir}"
370
 
371
# Check whether CDF and TRA directories exist
372
if ( ! -d ${cdfdir} ) then
373
    echo 'Cannot find CDF directory ... Stop'
374
    exit 1
375
endif
376
if ( ! -d ${tradir} ) then
377
    echo 'Cannot find TRA directory ... Stop'
378
    exit 1
379
endif
380
 
381
# Create the run directory
382
if ( ! -d ${rundir} ) then
383
   mkdir ${rundir}
384
endif 
385
if ( ! -d ${rundir} ) then
386
    echo 'Cannot create RUN directory ... Stop'
387
    exit 1
388
endif 
389
 
390
# ---------------------------------------------------------------------
391
# Handle the time specifier - startdate, enddate
392
# ---------------------------------------------------------------------
393
 
394
echo
395
echo '---- TIME RANGE -----------------------------------------'
396
echo
397
 
398
# Check format of start and end date - must be the same
399
set ns=`echo $startdate | sed -e 's/_[0-9]*//' | wc -c`
400
set ne=`echo $enddate   | sed -e 's/_[0-9]*//' | wc -c`
401
if ( $ns != $ne ) then
402
  echo " ERROR: start and end date must be in the same format ***"
403
  exit 1
404
endif
405
if ( $ns != 9 ) then
406
  echo " ERROR: Date format must be yyyymmdd ***"
407
  exit 1
408
endif
409
set ns=`echo $startdate | sed -e 's/[0-9]*_//' | wc -c`
410
set ne=`echo $enddate   | sed -e 's/[0-9]*_//' | wc -c`
411
if ( $ns != $ne ) then
412
  echo " ERROR: start and end date must be in the same format ***"
413
  exit 1
414
endif
415
if ( ( $ns != 5 ) & ( $ns != 3 ) ) then
416
  echo " ERROR: Time format must be hh(mm) ***"
417
  exit 1
418
endif
419
 
420
# Split the start and end date into <yymmdd_hh and mm>
421
set startdate_ymdh = `echo $startdate | cut -c 1-11`
422
set startdate_min  = `echo $startdate | cut -c 12-13`
423
if ( $startdate_min == "" ) set startdate_min = 00
424
 
425
set enddate_ymdh = `echo $enddate | cut -c 1-11`
426
set enddate_min  = `echo $enddate | cut -c 12-13`
427
if ( $enddate_min == "" ) set enddate_min = 00
428
 
429
# Get the time difference between <start_ymdh> and <end_ymdh> date
430
# Decide whether trajectoriesare forward or backward
431
set timediff_hh = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${startdate_ymdh}`
432
 
433
if ( $timediff_hh == 0 ) then
434
  if ( $enddate_min > $startdate_min ) then
435
    set direction = f
436
    set idir      = 1
437
  else
438
    set direction = b
439
    set idir      = -1
440
  endif
441
else if ( $timediff_hh > 0 ) then
442
  set direction = f
443
  set idir      = 1
444
else
445
  set direction = b
446
  set idir      = -1
447
  @ timediff_hh = $idir * $timediff_hh
448
endif
449
 
450
# Get also minutes for time difference, if <start_min> or <end_min> != 0
451
set timediff_mm=
452
 
453
if ( $startdate_min != 00 || $enddate_min != 00 ) then
454
  @ min = ( $enddate_min - $startdate_min )
455
  if ( $min == 0 ) then
456
    set timediff_mm=
457
  else if ( $min > 0 ) then
458
    if ( $idir == 1 ) then
459
      set timediff_mm=$min
460
    else
461
      @ timediff_hh --
462
      @ timediff_mm = 60 - $min
463
    endif
464
  else
465
    if ( $idir == 1 ) then
466
      @ timediff_hh --
467
      @ timediff_mm = 60 + $min
468
    else
469
      @ timediff_mm = 0 - $min
470
    endif
471
  endif
472
endif
473
 
474
# Set the reference date equal to the startdate
475
set refdate=${startdate}
476
 
477
# Write status information
478
echo "Time range      : ${startdate} -> ${enddate}"
479
if ( ${timediff_mm} !=  "" ) then
480
   echo "Time difference : ${timediff_hh} h ${timediff_mm} min"
481
else
482
   echo "Time difference : ${timediff_hh} h"
483
endif
484
echo "Direction       : ${direction} (${idir})"
485
echo "Reference date  : ${refdate}"
486
 
487
# ---------------------------------------------------------------------
488
# Decide which files are needed and check availability
489
# ---------------------------------------------------------------------
490
 
491
echo
492
echo '---- INPUT FILES ----------------------------------------'
493
echo
494
 
495
# Change to input data (cdf) directory
496
cd ${cdfdir}
497
echo
498
 
499
# Take the time increment from flag list ('nil', if not defined)
500
set timeinc = 'nil'
501
 
502
# Find a first data file (if possible corresponding to start/end date
503
# If starttime is not a data time, take the first file in the direectory
504
if ( $direction == "f" ) then
505
  set file=${charp}${startdate_ymdh}
506
else
507
  set file=${charp}${enddate_ymdh}
508
endif
509
if ( ! -f $file ) then
510
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
511
endif
512
 
513
# Determine timeinc (the time difference in hours between two data file)
514
# if not already defined with option -i
515
if ( ${timeinc} == "nil" ) then
516
  set date1=`echo $file | cut -c 2-12`
517
  set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
518
  @ n ++
519
  set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-12`
520
  set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
521
endif
522
if ( $timeinc == 0 ) then
523
    echo " ERROR: cannot set the time increment between input files ***"
524
    exit 1
525
endif
526
 
527
# Search the first file to use
528
set flag=0
529
set td=
530
foreach i ( ${charp}????????_?? )
531
 
532
  set date = `echo $i | cut -c 2-12`
533
  set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
534
  set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
535
 
536
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
537
      set datfiles=$date
538
      if ( $td1 < $timeinc    ) set td=$td1
539
      if ( $td2 < $timeinc    ) set td=$td2
540
      if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++ 
541
      goto label2
542
  endif
543
 
544
end
545
 
546
# if no P/T-files are available for the specified time period, then $td is
547
# still undefined
548
if ( $td == "" ) then
549
  echo " ERROR: no data files available for the specified time period"
550
  exit 1
551
endif
552
 
553
# Everything is fine so far: proceed
554
label2:
555
 
556
# Calculate the number of further files
557
@ num = ( $timediff_hh + $td ) / $timeinc + 1
558
@ dum1 = ( $num - 1 ) * $timeinc
559
@ dum2 = $timediff_hh + $td
560
if ( $dum1 != $dum2 ) @ num ++
561
 
562
# Get a list of all needed files
563
set numfiles=$num
564
set sfiles=1
565
while ( $num > 1 )
566
  set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
567
  if ( ! -f ${charp}${date} ) then
568
    echo " ERROR: file with primary data is missing for $date"
569
    exit 1
570
  else if ( ! -f ${chars}${date} ) then
571
    set sfiles=0
572
    set datfiles=`echo $datfiles $date`
573
  else
574
    set datfiles=`echo $datfiles $date`
575
  endif
576
  @ num --
577
end
578
 
579
# Write some status information
580
echo "Primary file prefix            : ${charp}"
581
echo "Secondary file prefix          : ${chars}"
582
echo "Time increment for input files : ${timeinc}"
583
echo "# input files                  : ${numfiles}"
584
echo "${charp} files availability           : 1"  
585
echo "${chars} files availability           : ${sfiles}"     
586
echo "First input file               : $datfiles[1] " 
587
echo "Last input file                : $datfiles[$numfiles] " 
588
 
589
 
590
# ---------------------------------------------------------------------
591
# Prepare run directory - copy all needed files
592
# ---------------------------------------------------------------------
593
 
594
echo
595
echo '---- PREPARE RUN DIRECTORY ------------------------------'
596
echo
597
 
598
# Change to run directory
599
cd ${rundir}
600
echo
601
 
602
 
603
# Link cdf input files
604
foreach date ( $datfiles )
605
  if ( -f ${cdfdir}/${charp}${date} ) then
606
     if ( ! -f  ${charp}${date} ) then
607
        ln -s  ${cdfdir}/${charp}${date}  ${charp}${date}
608
        echo "Linking  ${date} "
609
     else
610
        echo "Existing ${date} "
611
     endif
612
  endif
613
  if ( -f ${cdfdir}/${chars}${date} ) then
614
     if ( ! -f  ${chars}${date} ) then
615
        ln -sf  ${cdfdir}/${chars}${date}  ${chars}${date}
616
     endif
617
  endif
618
end
619
 
620
# Change times on netCDF files relative to reference date
621
if ( "${changet}" == "true" ) then
622
 echo
623
 foreach i ( $datfiles )
624
   ${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}
625
 end
626
 if ( ${sfiles} == 1 ) then
627
   echo
628
   foreach i ( $datfiles )
629
     ${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}
630
   end
631
 endif
632
endif
633
 
634
# Get the constants file for each file
635
set first = 1
636
foreach date ( $datfiles )
637
  set cstname = `ncdump -h ${charp}${date} | grep constants_file_name`
638
  set cstname = `echo \'${cstname}\' |   awk -F\" '{print $2}'`
639
  if ( -f ${cdfdir}/${cstname} ) then
640
    if ( ! -f ${cstname} ) then
641
       ln -s  ${cdfdir}/${cstname}  ${cstname}
642
       echo "Linking  constants file ${cstname}"
643
       set first = 0
644
    else if ( "${first}" == "1" ) then
645
       echo "Existing constants file ${cstname}"
646
       set first = 0
647
    endif
648
  endif
649
end
650
 
651
# Copy the tracevars file
652
if ( "${flag_tracevars}" == "file" ) then
653
   if ( -f ${tradir}/${tracefile} ) then
654
      \cp ${tradir}/${tracefile} .
655
   endif
656
endif
657
 
658
# Copy the start file/criterion
659
if ( "${flag_startf}" == "criterion" ) then
660
   \rm -f startf.criterion
661
   echo ${startf} > startf.criterion
662
   set startf =  "startf.criterion"
663
else if ( "${flag_startf}" == "file" ) then
664
   if ( -f ${tradir}/${startf} ) then
665
     \cp ${tradir}/${startf} .
666
   endif
667
else if ( "${flag_startf}" == "point" ) then
668
   \rm -f startf
669
   echo ${startf}  > startf
670
   set startf      =  "startf"
671
   set flag_startf = "file"
672
endif 
673
 
674
# Copy the select file/criterion
675
if ( "${flag_select}" == "criterion" ) then
676
   \rm -f select.criterion
677
   echo ${select} > select.criterion
678
   set select = "select.criterion"
679
else
680
   if ( -f ${tradir}/${select} ) then
681
     \cp ${tradir}/${select} .
682
   endif
683
endif 
684
 
685
 
686
# ---------------------------------------------------------------------
687
# Prepare the run script
688
# ---------------------------------------------------------------------
689
 
690
echo
691
echo '---- PREPARE RUN SCRIPT ---------------------------------'
692
echo
693
 
694
# Remove runscript if it already exists
695
if ( -f ${runscript}.sh ) then
696
   \rm -f ${runscript}.sh
697
endif 
698
 
699
# Write header
700
echo '#\!/bin/csh'                    >! ${runscript}.sh
701
echo "#"                              >> ${runscript}.sh
702
echo "#----- Calling command"         >> ${runscript}.sh
703
echo "#"                              >> ${runscript}.sh
704
echo "# lagranto ${lagranto_call} "   >> ${runscript}.sh
705
echo "#"                              >> ${runscript}.sh 
706
echo "#----- Output file    "         >> ${runscript}.sh
707
echo "#"                              >> ${runscript}.sh
708
echo "# ${outfile}                "   >> ${runscript}.sh
709
echo "#"                              >> ${runscript}.sh 
710
 
711
# Decide whether <create_startf> has to be run
712
if ( "${flag_startf}" == "criterion" ) then
713
  echo "#------ Run <create_startf>"                                                                                       >> ${runscript}.sh
714
  echo "#"                                                                                                                 >> ${runscript}.sh
715
  echo ${LAGRANTO}/bin/create_startf.sh ${startdate} startf \"`cat startf.criterion`\" -t ${tracefile} -regionf ${regionf} >> ${runscript}.sh 
716
  echo "#"                                                                                                                 >> ${runscript}.sh
717
  set startf = "startf"
718
endif
719
 
720
# Check whether startfile is ready
721
cat >> ${runscript}.sh <<EOF 
722
#------ Abort if no startf is available
723
#
724
if ( ! -f ${startf} ) then
725
  echo " ERROR: no start file available .... Stop"
726
  exit 1
727
endif
728
EOF
729
 
730
# Decide wich intermediate format is used (needed for efficient communication between different programs)
731
set iform1 = ""
732
set iform2 = ""
733
if ( "${tracefile}" != ""    ) set iform1 = ".4"
734
if ( "${select}"    != "nil" ) set iform2 = ".4"
735
 
736
# Remove all trajectory files which might already be there
737
cat >> ${runscript}.sh <<EOF 
738
#
739
#------ Remove existing trajectory files
740
#
741
if ( -f ${outfile}${iform1} ) then
742
  \rm -f ${outfile}${iform1}
743
endif
744
if ( -f ${outfile}${iform2} ) then
745
  \rm -f ${outfile}${iform2}
746
endif
747
if ( -f ${outfile} ) then
748
  \rm -f ${outfile}
749
endif
750
 
751
EOF
752
 
753
# Write call to <caltra> into runscript
754
echo "#"                                                                                      >> ${runscript}.sh
755
echo "#------ Run <caltra>"                                                                   >> ${runscript}.sh
756
echo "#"                                                                                      >> ${runscript}.sh
757
echo ${LAGRANTO}/bin/caltra.sh ${startdate} ${enddate} startf ${outfile}${iform1} ${jumpflag} >> ${runscript}.sh  
758
 
759
# Check whether caltra was successful
760
cat >> ${runscript}.sh <<EOF 
761
#
762
#------ Abort if caltra was not successful
763
#
764
if ( ! -f ${outfile}${iform1} ) then
765
  echo " ERROR: caltra failed .... Stop"
766
  exit 1
767
endif
768
EOF
769
 
770
#  Decide whether <trace> has to be run
771
if ( "${tracefile}" != "" ) then
772
  echo "#"                                                                                    >> ${runscript}.sh
773
  echo "#------ Run <trace>"                                                                  >> ${runscript}.sh
774
  echo "#"                                                                                    >> ${runscript}.sh
775
  echo ${LAGRANTO}/bin/trace.sh ${outfile}${iform1} ${outfile}${iform2} -v ${tracefile}       >> ${runscript}.sh 
776
 
777
endif
778
 
779
# Check whether trace was successful
780
cat >> ${runscript}.sh <<EOF 
781
#
782
#------ Abort if trace was not successful
783
#
784
if ( ! -f ${outfile}${iform2} ) then
785
  echo " ERROR: trace failed .... Stop"
786
  exit 1
787
endif
788
EOF
789
 
790
#  Decide whether <select> has to be run
791
if ( "${select}" != "nil" ) then                        
792
  echo "#"                                                                                               >> ${runscript}.sh
793
  echo "#------ Run <select>"                                                                            >> ${runscript}.sh
794
  echo "#"                                                                                               >> ${runscript}.sh
795
  echo ${LAGRANTO}/bin/select.sh ${outfile}${iform2} ${outfile}  \"`cat ${select}`\" -regionf ${regionf} >> ${runscript}.sh 
796
  echo "#"                                                                                               >> ${runscript}.sh
797
endif
798
 
799
# Show runscript on screen
800
more ${runscript}.sh
801
 
802
# Change runscript to executable
803
chmod u+x ${runscript}.sh
804
 
805
# Decide whether to stop at this place (only preparation for Lagranto run)
806
if ( "${prepflag}" == "prep" ) then
807
  echo
808
  echo '---- READY TO RUN ----- ---------------------------------'
809
  echo
810
  echo " Would you like to change to the rundirectory [y/n] "
811
 
812
  set id = $<
813
  if ( ( "${id}" == "y" ) | ( "${id}" == "Y" ) ) then
814
     cd ${rundir}
815
     xterm &
816
  endif
817
 
818
  exit 0
819
 
820
endif  
821
 
822
# Define environment variable for the case
823
echo
824
echo '---- REMEMBER MOST RECENT CALL --------------------------'
825
echo
826
cd ${calldir}
827
if ( -f lagranto.param ) \rm lagranto.param
828
echo "${rundir}"  >! lagranto.param
829
echo "${outfile}" >> lagranto.param
830
cd ${rundir}
831
echo
832
echo " run directory + output file -> lagranto.param"
833
 
834
# ---------------------------------------------------------------------
835
# Run lagranto
836
# ---------------------------------------------------------------------
837
 
838
echo
839
echo '---- RUN LAGRANTO ---------------------------------------'
840
echo
841
 
842
if ( "${logfile}" != "/dev/stdout" ) then
843
    echo "log goes to ${logfile}"
844
endif
845
 
846
# Remove logfile
847
if ( "${logfile}" != "/dev/stdout" ) then
848
    \rm -f    ${logfile}
849
endif
850
 
851
# Run it
852
${rundir}/${runscript}.sh > ${logfile}
853
 
854
# Check for errors in logfile
855
if ( "${logfile}" != "/dev/stdout" ) then
856
   grep ERROR ${logfile} 
857
endif
858
 
859
# ---------------------------------------------------------------------
860
# Make clean and finish lagranto run
861
# ---------------------------------------------------------------------
862
 
863
# Remove unneeded files
864
\rm -f fort.9
865
 
866
# Remove files if requested
867
if ( "${keepflag}" != "keep" ) then
868
 
869
   echo 
870
   echo '---- MAKING CLEAN ---------------------------------------'
871
   echo
872
 
873
   cd ${rundir}
874
 
875
 
876
   set runscript = "${runscript}.sh"
877
 
878
   foreach file ( `\ls -L1` )
879
 
880
      set id = "0"
881
 
882
      if ( "${file}" == "${outfile}"   ) set id="1" 
883
      if ( "${file}" == "${runscript}" ) set id="1" 
884
      if ( "${file}" == "${logfile}"   ) set id="1" 
885
 
886
      if ( "${id}" == "0" ) \rm -f ${file}      
887
 
888
   end
889
 
890
endif
891
 
892
# Everything is fine!
893
echo
894
echo '             *** END OF LAGRANTO ***             '
895
echo '========================================================='
896
echo
897
 
898
exit 0 
899
 
900