Subversion Repositories lagranto.ecmwf

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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