Subversion Repositories lagranto.um

Rev

Rev 7 | Details | Compare with Previous | Last modification | View Log | RSS feed

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