Subversion Repositories lagranto.20cr

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