Subversion Repositories lagranto.icon

Rev

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
# Link cdf input files
572
foreach date ( $datfiles )
573
  if ( -f ${cdfdir}/${charp}${date} ) then
574
     if ( ! -f  ${charp}${date} ) then
575
        ln -s  ${cdfdir}/${charp}${date}  ${charp}${date}
576
        echo "Linking  ${date} "
577
     else
578
        echo "Existing ${date} "
579
     endif
580
  endif
581
  if ( -f ${cdfdir}/${chars}${date} ) then
582
     if ( ! -f  ${chars}${date} ) then
583
        ln -sf  ${cdfdir}/${chars}${date}  ${chars}${date}
584
     endif
585
  endif
586
end
587
 
588
# Link LMCONSTANTS file
589
if ( -f ${cdfdir}/LMCONSTANTS ) then
590
   ln -s  ${cdfdir}/LMCONSTANTS  LMCONSTANTS
591
else
592
   echo " ERROR: LMCONSTANTS file is missing... Stop"
593
   exit 1
594
endif
595
 
596
# Change times on netCDF files relative to reference date
597
if ( "${changet}" == "true" ) then
598
 echo
599
 foreach i ( $datfiles )
600
   ${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}
601
 end
602
 if ( ${sfiles} == 1 ) then
603
   echo
604
   foreach i ( $datfiles )
605
     ${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}
606
   end
607
 endif
608
endif
609
 
610
# Get the constants file for each file
611
set first = 1
612
foreach date ( $datfiles )
613
  set cstname = `ncdump -h ${charp}${date} | grep constants_file_name`
614
  set cstname = `echo \'${cstname}\' |   awk -F\" '{print $2}'`
615
  if ( -f ${cdfdir}/${cstname} ) then
616
    if ( ! -f ${cstname} ) then
617
       ln -s  ${cdfdir}/${cstname}  ${cstname}
618
       echo "Linking  constants file ${cstname}"
619
       set first = 0
620
    else if ( "${first}" == "1" ) then
621
       echo "Existing constants file ${cstname}"
622
       set first = 0
623
    endif
624
  endif
625
end
626
 
627
# Copy the tracevars file
628
if ( "${flag_tracevars}" == "file" ) then
629
   if ( -f ${tradir}/${tracefile} ) then
630
      \cp ${tradir}/${tracefile} .
631
   endif
632
endif
633
 
634
# Copy the start file/criterion
635
if ( "${flag_startf}" == "criterion" ) then
636
   \rm -f startf.criterion
637
   echo ${startf} > startf.criterion
638
   set startf =  "startf.criterion"
639
else if ( "${flag_startf}" == "file" ) then
640
   if ( -f ${tradir}/${startf} ) then
641
     \cp ${tradir}/${startf} .
642
   endif
643
else if ( "${flag_startf}" == "point" ) then
644
   \rm -f startf
645
   echo ${startf}  > startf
646
   set startf      =  "startf"
647
   set flag_startf = "file"
648
endif 
649
 
650
# Copy the select file/criterion
651
if ( "${flag_select}" == "criterion" ) then
652
   \rm -f select.criterion
653
   echo ${select} > select.criterion
654
   set select = "select.criterion"
655
else
656
   if ( -f ${tradir}/${select} ) then
657
     \cp ${tradir}/${select} .
658
   endif
659
endif 
660
 
661
 
662
# ---------------------------------------------------------------------
663
# Prepare the run script
664
# ---------------------------------------------------------------------
665
 
666
echo
667
echo '---- PREPARE RUN SCRIPT ---------------------------------'
668
echo
669
 
670
# Remove runscript if it already exists
671
if ( -f ${runscript}.sh ) then
672
   \rm -f ${runscript}.sh
673
endif 
674
 
675
# Write header
676
echo '#\!/bin/csh'                    >! ${runscript}.sh
677
echo "#"                              >> ${runscript}.sh
678
echo "#----- Calling command"         >> ${runscript}.sh
679
echo "#"                              >> ${runscript}.sh
680
echo "# lagranto ${lagranto_call} "   >> ${runscript}.sh
681
echo "#"                              >> ${runscript}.sh 
682
echo "#----- Output file    "         >> ${runscript}.sh
683
echo "#"                              >> ${runscript}.sh
684
echo "# ${outfile}                "   >> ${runscript}.sh
685
echo "#"                              >> ${runscript}.sh 
686
 
687
# Decide whether <create_startf> has to be run
688
if ( "${flag_startf}" == "criterion" ) then
689
  echo "#------ Run <create_startf>"                                                                                       >> ${runscript}.sh
690
  echo "#"                                                                                                                 >> ${runscript}.sh
691
  echo ${LAGRANTO}/bin/create_startf.sh ${startdate} startf \"`cat startf.criterion`\" -t ${tracefile} -regionf ${regionf} >> ${runscript}.sh 
692
  echo "#"                                                                                                                 >> ${runscript}.sh
693
  set startf = "startf"
694
endif
695
 
696
# Check whether startfile is ready
697
cat >> ${runscript}.sh <<EOF 
698
#------ Abort if no startf is available
699
#
700
if ( ! -f ${startf} ) then
701
  echo " ERROR: no start file available .... Stop"
702
  exit 1
703
endif
704
EOF
705
 
706
# Decide wich intermediate format is used (needed for efficient communication between different programs)
707
set iform1 = ""
708
set iform2 = ""
709
if ( "${tracefile}" != ""    ) set iform1 = ".4"
710
if ( "${select}"    != "nil" ) set iform2 = ".4"
711
 
712
# Remove all trajectory files which might already be there
713
cat >> ${runscript}.sh <<EOF 
714
#
715
#------ Remove existing trajectory files
716
#
717
if ( -f ${outfile}${iform1} ) then
718
  \rm -f ${outfile}${iform1}
719
endif
720
if ( -f ${outfile}${iform2} ) then
721
  \rm -f ${outfile}${iform2}
722
endif
723
if ( -f ${outfile} ) then
724
  \rm -f ${outfile}
725
endif
726
 
727
EOF
728
 
729
# Write call to <caltra> into runscript
730
echo "#"                                                                                      >> ${runscript}.sh
731
echo "#------ Run <caltra>"                                                                   >> ${runscript}.sh
732
echo "#"                                                                                      >> ${runscript}.sh
733
echo ${LAGRANTO}/bin/caltra.sh ${startdate} ${enddate} startf ${outfile}${iform1} ${jumpflag} >> ${runscript}.sh  
734
 
735
# Check whether caltra was successful
736
cat >> ${runscript}.sh <<EOF 
737
#
738
#------ Abort if caltra was not successful
739
#
740
if ( ! -f ${outfile}${iform1} ) then
741
  echo " ERROR: caltra failed .... Stop"
742
  exit 1
743
endif
744
EOF
745
 
746
#  Decide whether <trace> has to be run
747
if ( "${tracefile}" != "" ) then
748
  echo "#"                                                                                    >> ${runscript}.sh
749
  echo "#------ Run <trace>"                                                                  >> ${runscript}.sh
750
  echo "#"                                                                                    >> ${runscript}.sh
751
  echo ${LAGRANTO}/bin/trace.sh ${outfile}${iform1} ${outfile}${iform2} -v ${tracefile}       >> ${runscript}.sh 
752
 
753
endif
754
 
755
# Check whether trace was successful
756
cat >> ${runscript}.sh <<EOF 
757
#
758
#------ Abort if trace was not successful
759
#
760
if ( ! -f ${outfile}${iform2} ) then
761
  echo " ERROR: trace failed .... Stop"
762
  exit 1
763
endif
764
EOF
765
 
766
#  Decide whether <select> has to be run
767
if ( "${select}" != "nil" ) then                        
768
  echo "#"                                                                                               >> ${runscript}.sh
769
  echo "#------ Run <select>"                                                                            >> ${runscript}.sh
770
  echo "#"                                                                                               >> ${runscript}.sh
771
  echo ${LAGRANTO}/bin/select.sh ${outfile}${iform2} ${outfile}  \"`cat ${select}`\" -regionf ${regionf} >> ${runscript}.sh 
772
  echo "#"                                                                                               >> ${runscript}.sh
773
endif
774
 
775
# Show runscript on screen
776
more ${runscript}.sh
777
 
778
# Change runscript to executable
779
chmod u+x ${runscript}.sh
780
 
781
# Decide whether to stop at this place (only preparation for Lagranto run)
782
if ( "${prepflag}" == "prep" ) then
783
  echo
784
  echo '---- READY TO RUN ----- ---------------------------------'
785
  echo
786
  echo " Would you like to change to the rundirectory [y/n] "
787
 
788
  set id = $<
789
  if ( ( "${id}" == "y" ) | ( "${id}" == "Y" ) ) then
790
     cd ${rundir}
791
     xterm &
792
  endif
793
 
794
  exit 0
795
 
796
endif  
797
 
798
# Define environment variable for the case
799
echo
800
echo '---- REMEMBER MOST RECENT CALL --------------------------'
801
echo
802
cd ${calldir}
803
if ( -f lagranto.param ) \rm lagranto.param
804
echo "${rundir}"  >! lagranto.param
805
echo "${outfile}" >> lagranto.param
806
cd ${rundir}
807
echo
808
echo " run directory + output file -> lagranto.param"
809
 
810
# ---------------------------------------------------------------------
811
# Run lagranto
812
# ---------------------------------------------------------------------
813
 
814
echo
815
echo '---- RUN LAGRANTO ---------------------------------------'
816
echo
817
 
818
if ( "${logfile}" != "/dev/stdout" ) then
819
    echo "log goes to ${logfile}"
820
endif
821
 
822
# Remove logfile
823
if ( "${logfile}" != "/dev/stdout" ) then
824
    \rm -f    ${logfile}
825
endif
826
 
827
# Run it
828
${rundir}/${runscript}.sh > ${logfile}
829
 
830
# Check for errors in logfile
831
if ( "${logfile}" != "/dev/stdout" ) then
832
   grep ERROR ${logfile} 
833
endif
834
 
835
# ---------------------------------------------------------------------
836
# Make clean and finish lagranto run
837
# ---------------------------------------------------------------------
838
 
839
# Remove unneeded files
840
\rm -f fort.9
841
 
842
# Remove files if requested
843
if ( "${keepflag}" != "keep" ) then
844
 
845
   echo 
846
   echo '---- MAKING CLEAN ---------------------------------------'
847
   echo
848
 
849
   cd ${rundir}
850
 
851
 
852
   set runscript = "${runscript}.sh"
853
 
854
   foreach file ( `\ls -L1` )
855
 
856
      set id = "0"
857
 
858
      if ( "${file}" == "${outfile}"   ) set id="1" 
859
      if ( "${file}" == "${runscript}" ) set id="1" 
860
      if ( "${file}" == "${logfile}"   ) set id="1" 
861
 
862
      if ( "${id}" == "0" ) \rm -f ${file}      
863
 
864
   end
865
 
866
endif
867
 
868
# Everything is fine!
869
echo
870
echo '             *** END OF LAGRANTO ***             '
871
echo '========================================================='
872
echo
873
 
874
exit 0 
875
 
876