Subversion Repositories lagranto.um

Rev

Rev 15 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15 Rev 16
1
#!/bin/csh
1
#!/bin/csh
2
 
2
 
3
# Set Lagranto 
3
# Set Lagranto 
4
set LAGRANTO = ${LAGRANTOBASE}.${MODEL}/
4
set LAGRANTO = ${LAGRANTOBASE}.${MODEL}/
5
 
5
 
6
# ---------------------------------------------------------------------
6
# ---------------------------------------------------------------------
7
# Usage, parameter settings
7
# Usage, parameter settings
8
# ---------------------------------------------------------------------
8
# ---------------------------------------------------------------------
9
 
9
 
10
# Write usage information
10
# Write usage information
11
if ( (${#argv} == 0) | (${#argv} < 2) ) then
11
if ( (${#argv} == 0) | (${#argv} < 2) ) then
12
  echo 
12
  echo 
13
  echo "--------------------------------------------------------------"
13
  echo "--------------------------------------------------------------"
14
  echo " Usage: trace.sh inpfile outfile [flags]        "
14
  echo " Usage: trace.sh inpfile outfile [flags]        "
15
  echo 
15
  echo 
16
  echo "        inpfile   : Input trajectory file                     "
16
  echo "        inpfile   : Input trajectory file                     "
17
  echo "        outfile   : Output trajectory file                    "
17
  echo "        outfile   : Output trajectory file                    "
18
  echo "        [ flags ] : Optional list of flags                    "
18
  echo "        [ flags ] : Optional list of flags                    "
19
  echo
19
  echo
20
  echo " Optional flags                                               "
20
  echo " Optional flags                                               "
21
  echo
21
  echo
22
  echo "        -i hours     : Time increment for input files in hours"
22
  echo "        -i hours     : Time increment for input files in hours"
23
  echo "        -v filename  : Name of tracing file                   "
23
  echo "        -v filename  : Name of tracing file                   "
24
  echo "        -f var scale : Trace field <var> with scaling <scale> "
24
  echo "        -f var scale : Trace field <var> with scaling <scale> "
25
  echo "        -changet     : change times on P,S files before calculation" 
25
  echo "        -changet     : change times on P,S files before calculation" 
26
  echo "        -noclean     : do not remove temporary files"
26
  echo "        -noclean     : do not remove temporary files"
27
  echo 
27
  echo 
28
  echo " Tracing variables must be available on file <tracevars>, if not"
28
  echo " Tracing variables must be available on file <tracevars>, if not"
29
  echo " provided with "-f" option. The format of the <tracevars> file is"
29
  echo " provided with "-f" option. The format of the <tracevars> file is"
30
  echo  
30
  echo  
31
  echo "           varname   scale    compflag[0|1]   prefix[P|S]     "
31
  echo "           varname   scale    compflag[0|1]   prefix[P|S]     "
32
  echo
32
  echo
33
  echo " The tracing variables have the following form                "
33
  echo " The tracing variables have the following form                "
34
  echo 
34
  echo 
35
  echo "             varname[:shift] with <shift> as + 100 km[lat]    "
35
  echo "             varname[:shift] with <shift> as + 100 km[lat]    "
36
  echo "                                             - 100 km[lon]    "
36
  echo "                                             - 100 km[lon]    "
37
  echo "                                             + 2 dlon         "
37
  echo "                                             + 2 dlon         "
38
  echo "                                             - 2 dlat         "
38
  echo "                                             - 2 dlat         "
39
  echo "                                             + 50 hPa         "
39
  echo "                                             + 50 hPa         "
40
  echo "                                             - 1 dp           "
40
  echo "                                             - 1 dp           "
41
  echo "                                             + 6 h            "
41
  echo "                                             + 6 h            "
42
  echo "                                             - 180 min        "
42
  echo "                                             - 180 min        "
43
  echo
43
  echo
44
  echo " The following fields can be computed 'online':               "
44
  echo " The following fields can be computed 'online':               "
45
  echo
45
  echo
46
  echo " TH, RHO, RH, THE, LHR, DUDX, DVDX, DTDX, DUDY, DVDY, DTDY,    " 
46
  echo " TH, RHO, RH, THE, LHR, DUDX, DVDX, DTDX, DUDY, DVDY, DTDY,    " 
47
  echo " DUDP, DVDP, DTDP, DTHDP, NSQ, RELVORT, ABSVORT, DIV, DTHDX,"
47
  echo " DUDP, DVDP, DTDP, DTHDP, NSQ, RELVORT, ABSVORT, DIV, DTHDX,"
48
  echo " DTHDY, PV, RI, TI"
48
  echo " DTHDY, PV, RI, TI"
49
  echo "--------------------------------------------------------------"
49
  echo "--------------------------------------------------------------"
50
  exit 0
50
  exit 0
51
endif
51
endif
52
 
52
 
53
# Write title
53
# Write title
54
echo 
54
echo 
55
echo '========================================================='
55
echo '========================================================='
56
echo '       *** START OF PREPROCESSOR TRACE ***              '
56
echo '       *** START OF PREPROCESSOR TRACE ***              '
57
echo
57
echo
58
 
58
 
59
# Get the arguments
59
# Get the arguments
60
set inpfile   = $1
60
set inpfile   = $1
61
set outfile   = $2
61
set outfile   = $2
62
 
62
 
63
# Set base directories (run+prog)
63
# Set base directories (run+prog)
64
set cdfdir=${PWD}
64
set cdfdir=${PWD}
65
set tradir=${PWD}
65
set tradir=${PWD}
66
 
66
 
67
# Set program paths and filenames 
67
# Set program paths and filenames 
68
set parfile = ${tradir}/trace.param 
68
set parfile = ${tradir}/trace.param 
69
set prog    = ${LAGRANTO}/trace/trace
69
set prog    = ${LAGRANTO}/trace/trace
70
 
70
 
71
# Set the prefix of the primary and secondary data files
71
# Set the prefix of the primary and secondary data files
72
set charp = 'P'
72
set charp = 'P'
73
set chars = 'S'
73
set chars = 'S'
74
 
74
 
75
echo '---- DIRECTORIES AND PROGRAMS ---------------------------'
75
echo '---- DIRECTORIES AND PROGRAMS ---------------------------'
76
echo    
76
echo    
77
echo "CDF directory         : ${cdfdir}"
77
echo "CDF directory         : ${cdfdir}"
78
echo "TRA directory         : ${tradir}"
78
echo "TRA directory         : ${tradir}"
79
echo "PROGRAM TRACE         : ${prog}"
79
echo "PROGRAM TRACE         : ${prog}"
80
echo "PARAMETER file        : ${parfile}"
80
echo "PARAMETER file        : ${parfile}"
81
echo
81
echo
82
 
82
 
83
# ---------------------------------------------------------------------
83
# ---------------------------------------------------------------------
84
# Set optional flags
84
# Set optional flags
85
# ---------------------------------------------------------------------
85
# ---------------------------------------------------------------------
86
 
86
 
87
echo '---- OPTIONAL FLAGS -------------------------------------'
87
echo '---- OPTIONAL FLAGS -------------------------------------'
88
echo
88
echo
89
 
89
 
90
# Set some default values ("nil" must be set according to input files)
90
# Set some default values ("nil" must be set according to input files)
91
set flag_i     = "nil"
91
set flag_i     = "nil"
92
set flag_v     = "tracevars"
92
set flag_v     = "tracevars"
93
set flag_f     = "nil"
93
set flag_f     = "nil"
94
set tvfile     = 'tracevars'
94
set tvfile     = 'tracevars'
95
set changet    = 'false'
95
set changet    = 'false'
96
set noclean    = 'false'
96
set noclean    = 'false'
97
set timecheck  = 'yes' 
97
set timecheck  = 'no' 
98
 
98
 
99
# Set flag for consitency
99
# Set flag for consitency
100
set isok = 1
100
set isok = 1
101
 
101
 
102
while ( $#argv > 0 )
102
while ( $#argv > 0 )
103
 
103
 
104
  switch ( $argv[1] )
104
  switch ( $argv[1] )
105
 
105
 
106
   case -i
106
   case -i
107
     set flag_i=$argv[2]
107
     set flag_i=$argv[2]
108
     echo "Flag '-i'     -> ${flag_i} (user defined)"
108
     echo "Flag '-i'     -> ${flag_i} (user defined)"
109
     shift;
109
     shift;
110
   breaksw
110
   breaksw
111
 
111
 
112
   case -v
112
   case -v
113
     set flag_v="-v"
113
     set flag_v="-v"
114
     set tvfile=$argv[2]
114
     set tvfile=$argv[2]
115
     echo "Flag '-v'     -> ${tvfile} (user defined)"
115
     echo "Flag '-v'     -> ${tvfile} (user defined)"
116
     shift;
116
     shift;
117
     if ( $isok == 2 ) set isok = 0
117
     if ( $isok == 2 ) set isok = 0
118
     if ( $isok == 1 ) set isok = 2
118
     if ( $isok == 1 ) set isok = 2
119
   breaksw
119
   breaksw
120
 
120
 
121
   case -f
121
   case -f
122
     set flag_f="-f"
122
     set flag_f="-f"
123
     set tvfile="tracevars.tmp"
123
     set tvfile="tracevars.tmp"
124
     shift;
124
     shift;
125
     set tvar="$argv[1]"
125
     set tvar="$argv[1]"
126
     shift;
126
     shift;
127
     set tscale="$argv[1]"
127
     set tscale="$argv[1]"
128
     echo "Flag '-f'     -> ${tvar} ${tscale} (user defined)"
128
     echo "Flag '-f'     -> ${tvar} ${tscale} (user defined)"
129
     if ( $isok == 2 ) set isok = 0
129
     if ( $isok == 2 ) set isok = 0
130
     if ( $isok == 1 ) set isok = 2
130
     if ( $isok == 1 ) set isok = 2
131
   breaksw
131
   breaksw
132
 
132
 
133
   case -changet
133
   case -changet
134
     set changet = 'true'
134
     set changet = 'true'
135
     echo "changet       -> true (user defined)"
135
     echo "changet       -> true (user defined)"
136
   breaksw
136
   breaksw
137
 
137
 
138
   case -noclean
138
   case -noclean
139
     set noclean = 'true'
139
     set noclean = 'true'
140
     echo "noclean       -> true (user defined)"
140
     echo "noclean       -> true (user defined)"
141
   breaksw
141
   breaksw
142
 
142
 
143
   case -notimecheck
143
   case -notimecheck
144
     set timecheck = 'no'
144
     set timecheck = 'no'
145
     echo "timecheck               -> no (user defined)"
145
     echo "timecheck               -> no (user defined)"
146
   breaksw
146
   breaksw
147
 
147
 
148
  endsw
148
  endsw
149
 
149
 
150
  shift;
150
  shift;
151
 
151
 
152
end
152
end
153
 
153
 
154
# No change of times necessary if no check requested
154
# No change of times necessary if no check requested
155
if ( "${timecheck}" == "no" ) then
155
if ( "${timecheck}" == "no" ) then
156
   set  changet = 'false'
156
   set  changet = 'false'
157
endif
157
endif
158
 
158
 
159
# Check consitency of arguments
159
# Check consitency of arguments
160
if ( $isok == 0 ) then
160
if ( $isok == 0 ) then
161
    echo
161
    echo
162
    echo " ERROR: Use either option '-v' or '-f', but not both..."
162
    echo " ERROR: Use either option '-v' or '-f', but not both..."
163
    exit 1
163
    exit 1
164
endif
164
endif
165
 
165
 
166
# ---------------------------------------------------------------------
166
# ---------------------------------------------------------------------
167
# Handle the input trajectory file
167
# Handle the input trajectory file
168
# ---------------------------------------------------------------------
168
# ---------------------------------------------------------------------
169
 
169
 
170
echo
170
echo
171
echo '---- TIME RANGE -----------------------------------------'
171
echo '---- TIME RANGE -----------------------------------------'
172
echo
172
echo
173
 
173
 
174
# Check whether the input file can be found
174
# Check whether the input file can be found
175
if ( ! -f ${inpfile} ) then
175
if ( ! -f ${inpfile} ) then
176
    echo " ERROR : Input file ${inpfile} is missing"
176
    echo " ERROR : Input file ${inpfile} is missing"
177
    exit 1
177
    exit 1
178
endif
178
endif
179
 
179
 
180
# Get the start, end and reference date for the tracing
180
# Get the start, end and reference date for the tracing
181
set startdate = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} startdate` 
181
set startdate = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} startdate` 
182
set enddate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} enddate` 
182
set enddate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} enddate` 
183
set refdate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} refdate` 
183
set refdate   = `${LAGRANTO}/goodies/trainfo.sh ${inpfile} refdate` 
184
set ntra      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
184
set ntra      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntra`
185
set ntim      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
185
set ntim      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ntim`
186
set ncol      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
186
set ncol      =  `${LAGRANTO}/goodies/trainfo.sh ${inpfile} ncol`
187
 
187
 
188
# Check format of start and end date - must be the same
188
# Check format of start and end date - must be the same
189
set ns=`echo $startdate | sed -e 's/_[0-9]*//' | wc -c`
189
set ns=`echo $startdate | sed -e 's/_[0-9]*//' | wc -c`
190
set ne=`echo $enddate   | sed -e 's/_[0-9]*//' | wc -c`
190
set ne=`echo $enddate   | sed -e 's/_[0-9]*//' | wc -c`
191
if ( $ns != $ne ) then
191
if ( $ns != $ne ) then
192
  echo " ERROR: start and end date must be in the same format ***"
192
  echo " ERROR: start and end date must be in the same format ***"
193
  exit 1
193
  exit 1
194
endif
194
endif
195
if ( $ns != 9 ) then
195
if ( $ns != 9 ) then
196
  echo " ERROR: Date format must be yyyymmdd ***"
196
  echo " ERROR: Date format must be yyyymmdd ***"
197
  exit 1
197
  exit 1
198
endif
198
endif
199
set ns=`echo $startdate | sed -e 's/[0-9]*_//' | wc -c`
199
set ns=`echo $startdate | sed -e 's/[0-9]*_//' | wc -c`
200
set ne=`echo $enddate   | sed -e 's/[0-9]*_//' | wc -c`
200
set ne=`echo $enddate   | sed -e 's/[0-9]*_//' | wc -c`
201
if ( $ns != $ne ) then
201
if ( $ns != $ne ) then
202
  echo " ERROR: start and end date must be in the same format ***"
202
  echo " ERROR: start and end date must be in the same format ***"
203
  exit 1
203
  exit 1
204
endif
204
endif
205
if ( ( $ns != 5 ) & ( $ns != 3 ) ) then
205
if ( ( $ns != 5 ) & ( $ns != 3 ) ) then
206
  echo " ERROR: Time format must be hh(mm) ***"
206
  echo " ERROR: Time format must be hh(mm) ***"
207
  exit 1
207
  exit 1
208
endif
208
endif
209
 
209
 
210
# Split the start and end date into <yymmdd_hh and mm>
210
# Split the start and end date into <yymmdd_hh and mm>
211
set startdate_ymdh = `echo $startdate | cut -c 1-11`
211
set startdate_ymdh = `echo $startdate | cut -c 1-11`
212
set startdate_min  = `echo $startdate | cut -c 12-13`
212
set startdate_min  = `echo $startdate | cut -c 12-13`
213
if ( $startdate_min == "" ) set startdate_min = 00
213
if ( $startdate_min == "" ) set startdate_min = 00
214
 
214
 
215
set enddate_ymdh = `echo $enddate | cut -c 1-11`
215
set enddate_ymdh = `echo $enddate | cut -c 1-11`
216
set enddate_min  = `echo $enddate | cut -c 12-13`
216
set enddate_min  = `echo $enddate | cut -c 12-13`
217
if ( $enddate_min == "" ) set enddate_min = 00
217
if ( $enddate_min == "" ) set enddate_min = 00
218
 
218
 
219
# Get the time difference between <start_ymdh> and <end_ymdh> date
219
# Get the time difference between <start_ymdh> and <end_ymdh> date
220
# Decide whether trajectoriesare forward or backward
220
# Decide whether trajectoriesare forward or backward
221
set timediff_hh = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${startdate_ymdh}`
221
set timediff_hh = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh} ${startdate_ymdh}`
222
 
222
 
223
if ( $timediff_hh == 0 ) then
223
if ( $timediff_hh == 0 ) then
224
  if ( $enddate_min > $startdate_min ) then
224
  if ( $enddate_min > $startdate_min ) then
225
    set direction = f
225
    set direction = f
226
    set idir      = 1
226
    set idir      = 1
227
  else
227
  else
228
    set direction = b
228
    set direction = b
229
    set idir      = -1
229
    set idir      = -1
230
  endif
230
  endif
231
else if ( $timediff_hh > 0 ) then
231
else if ( $timediff_hh > 0 ) then
232
  set direction = f
232
  set direction = f
233
  set idir      = 1
233
  set idir      = 1
234
else
234
else
235
  set direction = b
235
  set direction = b
236
  set idir      = -1
236
  set idir      = -1
237
  @ timediff_hh = $idir * $timediff_hh
237
  @ timediff_hh = $idir * $timediff_hh
238
endif
238
endif
239
 
239
 
240
# Get also minutes for time difference, if <start_min> or <end_min> != 0
240
# Get also minutes for time difference, if <start_min> or <end_min> != 0
241
set timediff_mm=
241
set timediff_mm=
242
 
242
 
243
if ( $startdate_min != 00 || $enddate_min != 00 ) then
243
if ( $startdate_min != 00 || $enddate_min != 00 ) then
244
  @ min = ( $enddate_min - $startdate_min )
244
  @ min = ( $enddate_min - $startdate_min )
245
  if ( $min == 0 ) then
245
  if ( $min == 0 ) then
246
    set timediff_mm=
246
    set timediff_mm=
247
  else if ( $min > 0 ) then
247
  else if ( $min > 0 ) then
248
    if ( $idir == 1 ) then
248
    if ( $idir == 1 ) then
249
      set timediff_mm=$min
249
      set timediff_mm=$min
250
    else
250
    else
251
      @ timediff_hh --
251
      @ timediff_hh --
252
      @ timediff_mm = 60 - $min
252
      @ timediff_mm = 60 - $min
253
    endif
253
    endif
254
  else
254
  else
255
    if ( $idir == 1 ) then
255
    if ( $idir == 1 ) then
256
      @ timediff_hh --
256
      @ timediff_hh --
257
      @ timediff_mm = 60 + $min
257
      @ timediff_mm = 60 + $min
258
    else
258
    else
259
      @ timediff_mm = 0 - $min
259
      @ timediff_mm = 0 - $min
260
    endif
260
    endif
261
  endif
261
  endif
262
else
262
else
263
  set timediff_mm = 0
263
  set timediff_mm = 0
264
endif
264
endif
265
 
265
 
266
# Write status information
266
# Write status information
267
echo "Time range      : ${startdate} -> ${enddate}"
267
echo "Time range      : ${startdate} -> ${enddate}"
268
if ( ${timediff_mm} != "" ) then
268
if ( ${timediff_mm} != "" ) then
269
   echo "Time difference : ${timediff_hh} h ${timediff_mm} min"
269
   echo "Time difference : ${timediff_hh} h ${timediff_mm} min"
270
else
270
else
271
   echo "Time difference : ${timediff_hh} h"
271
   echo "Time difference : ${timediff_hh} h"
272
endif
272
endif
273
echo "Direction       : ${direction} (${idir})"
273
echo "Direction       : ${direction} (${idir})"
274
 
274
 
275
# ---------------------------------------------------------------------
275
# ---------------------------------------------------------------------
276
# Check availability of input data 
276
# Check availability of input data 
277
# ---------------------------------------------------------------------
277
# ---------------------------------------------------------------------
278
 
278
 
279
echo
279
echo
280
echo '---- INPUT FILES ----------------------------------------'
280
echo '---- INPUT FILES ----------------------------------------'
281
echo
281
echo
282
 
282
 
283
# Take the time increment from flag list ('nil', if not defined)
283
# Take the time increment from flag list ('nil', if not defined)
284
set timeinc = ${flag_i}
284
set timeinc = ${flag_i}
285
 
285
 
286
# Find a first data file (if possible corresponding to start/end date
286
# Find a first data file (if possible corresponding to start/end date
287
# If starttime is not a data time, take the first file in the direectory
287
# If starttime is not a data time, take the first file in the direectory
288
if ( $direction == "f" ) then
288
if ( $direction == "f" ) then
289
  set file=${charp}${startdate_ymdh}
289
  set file=${charp}${startdate_ymdh}
290
else
290
else
291
  set file=${charp}${enddate_ymdh}
291
  set file=${charp}${enddate_ymdh}
292
endif
292
endif
293
if ( ! -f $file ) then
293
if ( ! -f $file ) then
294
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
294
  set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
295
endif
295
endif
296
 
296
 
297
# Determine timeinc (the time difference in hours between two data file)
297
# Determine timeinc (the time difference in hours between two data file)
298
# if not already defined with option -i
298
# if not already defined with option -i
299
if ( ${timeinc} == "nil" ) then
299
if ( ${timeinc} == "nil" ) then
300
  set date1=`echo $file | cut -c 2-`
300
  set date1=`echo $file | cut -c 2-`
301
  set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
301
  set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
302
  @ n ++
302
  @ n ++
303
  set date2=`\ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-`
303
  set date2=`\ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-`
304
  set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
304
  set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
305
endif
305
endif
306
if ( $timeinc == 0 ) then
306
if ( $timeinc == 0 ) then
307
    echo " ERROR: cannot set the time increment between input files ***"
307
    echo " ERROR: cannot set the time increment between input files ***"
308
    exit 1
308
    exit 1
309
endif
309
endif
310
 
310
 
311
# Decide whether the time increment is smaller than 1 h; if so set the hires
311
# Decide whether the time increment is smaller than 1 h; if so set the hires
312
# flag and convert the timeincement into fractional hour
312
# flag and convert the timeincement into fractional hour
313
set flag = `echo ${timeinc} | grep -n '\.'`
313
set flag = `echo ${timeinc} | grep -n '\.'`
314
set hires = 0
314
set hires = 0
315
if ( "${flag}" != "" ) then
315
if ( "${flag}" != "" ) then
316
  echo "Input files availability < 1 hour ($timeinc): switch to hires"
316
  echo "Input files availability < 1 hour ($timeinc): switch to hires"
317
  echo "mode. All input files must be of form: P{yyyymmdd_hhmm}"
317
  echo "mode. All input files must be of form: P{yyyymmdd_hhmm}"
318
  set hires = 1
318
  set hires = 1
319
  set timeinc_min = `echo "(${timeinc} * 100 + 0.5)/1" | bc`
319
  set timeinc_min = `echo "(${timeinc} * 100 + 0.5)/1" | bc`
320
  set timeinc     = `echo "scale=8; ${timeinc} * 100 / 60" | bc`
320
  set timeinc     = `echo "scale=8; ${timeinc} * 100 / 60" | bc`
321
  goto hires
321
  goto hires
322
endif
322
endif
323
 
323
 
324
## Search the first file to use: We step through all P files and see whether they are
324
## Search the first file to use: We step through all P files and see whether they are
325
# good P files. Let's first do the test for the first data file found. If it's ok, we 
325
# good P files. Let's first do the test for the first data file found. If it's ok, we 
326
# take it; if not, we step through all P files and find the good one  
326
# take it; if not, we step through all P files and find the good one  
327
set flag=0
327
set flag=0
328
set td=
328
set td=
329
 
329
 
330
set date = `echo $file | cut -c 2-12`
330
set date = `echo $file | cut -c 2-12`
331
set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
331
set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
332
set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
332
set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
333
 
333
 
334
if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
334
if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
335
   set datfiles=$date
335
   set datfiles=$date
336
   if ( $td1 < $timeinc    ) set td=$td1
336
   if ( $td1 < $timeinc    ) set td=$td1
337
   if ( $td2 < $timeinc    ) set td=$td2
337
   if ( $td2 < $timeinc    ) set td=$td2
338
   if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
338
   if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
339
   goto label2      
339
   goto label2      
340
endif
340
endif
341
 
341
 
342
foreach i ( ${charp}????????_?? )
342
foreach i ( ${charp}????????_?? )
343
 
343
 
344
  set date = `echo $i | cut -c 2-12`
344
  set date = `echo $i | cut -c 2-12`
345
  set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
345
  set td1  = `${LAGRANTO}/goodies/gettidiff ${startdate_ymdh} ${date}`
346
  set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
346
  set td2  = `${LAGRANTO}/goodies/gettidiff ${enddate_ymdh}   ${date}`
347
 
347
 
348
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
348
  if (( $td1 < $timeinc || $td2 < $timeinc ) && ( $td1 >= 0 || $td2 >= 0 )) then
349
      set datfiles=$date
349
      set datfiles=$date
350
      if ( $td1 < $timeinc    ) set td=$td1
350
      if ( $td1 < $timeinc    ) set td=$td1
351
      if ( $td2 < $timeinc    ) set td=$td2
351
      if ( $td2 < $timeinc    ) set td=$td2
352
      if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
352
      if ( ( $startdate_min > 0 ) || ( $enddate_min > 0 ) ) @ td ++
353
      goto label2
353
      goto label2
354
  endif
354
  endif
355
 
355
 
356
end
356
end
357
 
357
 
358
if ( $td == "" ) then
358
if ( $td == "" ) then
359
  echo " ERROR: no data files available for the specified time period"
359
  echo " ERROR: no data files available for the specified time period"
360
  exit 1
360
  exit 1
361
endif
361
endif
362
 
362
 
363
# If the input files come with time increment < 1 hour, handle it in this
363
# If the input files come with time increment < 1 hour, handle it in this
364
# special section. The aim is not to 'interfere' with the well established
364
# special section. The aim is not to 'interfere' with the well established
365
# code handling the standard case with time increment >= 1 hour. In this mode
365
# code handling the standard case with time increment >= 1 hour. In this mode
366
# we require that the start and end date are available as P files: no start is
366
# we require that the start and end date are available as P files: no start is
367
# allowed between the model output files
367
# allowed between the model output files
368
hires:
368
hires:
369
 
369
 
370
set flag=0
370
set flag=0
371
set td=0
371
set td=0
372
 
372
 
373
if ( $direction == "f" ) then
373
if ( $direction == "f" ) then
374
  set file=${charp}${startdate}
374
  set file=${charp}${startdate}
375
else 
375
else 
376
  set file=${charp}${enddate}
376
  set file=${charp}${enddate}
377
endif
377
endif
378
 
378
 
379
if ( -f $file ) then
379
if ( -f $file ) then
380
  set date     = `echo ${file} | cut -c 2-14`
380
  set date     = `echo ${file} | cut -c 2-14`
381
  set datfiles = $date
381
  set datfiles = $date
382
  set td       = 0
382
  set td       = 0
383
  goto label2
383
  goto label2
384
else
384
else
385
  echo
385
  echo
386
  echo " ERROR: no data files available for the specified time period"
386
  echo " ERROR: no data files available for the specified time period"
387
  exit 1
387
  exit 1
388
endif
388
endif
389
 
389
 
390
# Everything is fine so far: proceed
390
# Everything is fine so far: proceed
391
label2:
391
label2:
392
 
392
 
393
# Calculate the number of further files; handle hires (timeinc<1 h) and
393
# Calculate the number of further files; handle hires (timeinc<1 h) and
394
# standard mode (timeinc >= 1 h) separately
394
# standard mode (timeinc >= 1 h) separately
395
if ( ${hires} == 0 ) then
395
if ( ${hires} == 0 ) then
396
 @ num = ( $timediff_hh + $td ) / $timeinc + 1
396
 @ num = ( $timediff_hh + $td ) / $timeinc + 1
397
 @ dum1 = ( $num - 1 ) * $timeinc
397
 @ dum1 = ( $num - 1 ) * $timeinc
398
 @ dum2 = $timediff_hh + $td
398
 @ dum2 = $timediff_hh + $td
399
 if ( $dum1 != $dum2 ) @ num ++
399
 if ( $dum1 != $dum2 ) @ num ++
400
else
400
else
401
 set num = `echo "( ${timediff_hh}*60 + ${timediff_mm} ) / ( ${timeinc_min} ) + 1" | bc`
401
 set num = `echo "( ${timediff_hh}*60 + ${timediff_mm} ) / ( ${timeinc_min} ) + 1" | bc`
402
endif 
402
endif 
403
 
403
 
404
# Get a list of all needed files
404
# Get a list of all needed files
405
set numfiles=$num
405
set numfiles=$num
406
set sfiles=1
406
set sfiles=1
407
echo $datfiles
407
echo $datfiles
408
while ( $num > 1 )
408
while ( $num > 1 )
409
  if ( $hires == 0 ) then
409
  if ( $hires == 0 ) then
410
     set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
410
     set date=`${LAGRANTO}/goodies/newtime $date $timeinc`
411
  else
411
  else
412
     set date=`${LAGRANTO}/goodies/newtime $date $timeinc_min`
412
     set date=`${LAGRANTO}/goodies/newtime $date $timeinc_min`
413
  endif
413
  endif
414
  if ( ! -f ${charp}${date} ) then
414
  if ( ! -f ${charp}${date} ) then
415
    echo " ERROR: file with primary data is missing for $date"
415
    echo " ERROR: file with primary data is missing for $date"
416
    exit 1
416
    exit 1
417
  else if ( ! -f ${chars}${date} ) then
417
  else if ( ! -f ${chars}${date} ) then
418
    set sfiles=0
418
    set sfiles=0
419
    set datfiles=`echo $datfiles $date`
419
    set datfiles=`echo $datfiles $date`
420
  else
420
  else
421
    set datfiles=`echo $datfiles $date`
421
    set datfiles=`echo $datfiles $date`
422
  endif
422
  endif
423
  @ num --
423
  @ num --
424
end
424
end
425
 
425
 
426
# Calculate the start and the end time relative to the first datfile
426
# Calculate the start and the end time relative to the first datfile
427
if ( $direction == f ) then
427
if ( $direction == f ) then
428
  set tstart = `${LAGRANTO}/goodies/gettidiff $startdate $datfiles[1]`
428
  set tstart = `${LAGRANTO}/goodies/gettidiff $startdate $datfiles[1]`
429
  set tend   = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $enddate`
429
  set tend   = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $enddate`
430
else
430
else
431
  set tstart = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $startdate`
431
  set tstart = `${LAGRANTO}/goodies/gettidiff $datfiles[$numfiles] $startdate`
432
  set tend   = `${LAGRANTO}/goodies/gettidiff $enddate $datfiles[1]`
432
  set tend   = `${LAGRANTO}/goodies/gettidiff $enddate $datfiles[1]`
433
endif
433
endif
434
 
434
 
435
 
435
 
436
# Write some status information
436
# Write some status information
437
echo "Primary file prefix               : ${charp}"
437
echo "Primary file prefix               : ${charp}"
438
echo "Secondary file prefix             : ${chars}"
438
echo "Secondary file prefix             : ${chars}"
439
if ( ${hires} == 0 ) then
439
if ( ${hires} == 0 ) then
440
  echo "Time increment for input files    : ${timeinc} h"
440
  echo "Time increment for input files    : ${timeinc} h"
441
else
441
else
442
  echo "Time increment for input files    : ${timeinc_min} min (0$timeinc h) "
442
  echo "Time increment for input files    : ${timeinc_min} min (0$timeinc h) "
443
endif
443
endif
444
echo "# input files                     : ${numfiles}"
444
echo "# input files                     : ${numfiles}"
445
echo "First input file                  : $datfiles[1] " 
445
echo "First input file                  : $datfiles[1] " 
446
echo "Last input file                   : $datfiles[$numfiles] " 
446
echo "Last input file                   : $datfiles[$numfiles] " 
447
echo "${charp} files availability              : 1"  
447
echo "${charp} files availability              : 1"  
448
echo "${chars} files availability              : ${sfiles}"     
448
echo "${chars} files availability              : ${sfiles}"     
449
if ( $direction == f ) then
449
if ( $direction == f ) then
450
echo "Start time relative to first file : $datfiles[1] + ${tstart} "
450
echo "Start time relative to first file : $datfiles[1] + ${tstart} "
451
echo "End time relative to last file    : $datfiles[$numfiles] - ${tend} "  
451
echo "End time relative to last file    : $datfiles[$numfiles] - ${tend} "  
452
else
452
else
453
echo "Start time relative to last file  : $datfiles[$numfiles] - ${tstart} "
453
echo "Start time relative to last file  : $datfiles[$numfiles] - ${tstart} "
454
echo "End time relative to first file   : $datfiles[1] + ${tend} "
454
echo "End time relative to first file   : $datfiles[1] + ${tend} "
455
endif
455
endif
456
 
456
 
457
echo
457
echo
458
echo $datfiles
458
echo $datfiles
459
 
459
 
460
# ---------------------------------------------------------------------
460
# ---------------------------------------------------------------------
461
# Check availability of input data 
461
# Check availability of input data 
462
# ---------------------------------------------------------------------
462
# ---------------------------------------------------------------------
463
 
463
 
464
echo
464
echo
465
echo '---- TRACEVAR FILE --------------------------------------'
465
echo '---- TRACEVAR FILE --------------------------------------'
466
echo    
466
echo    
467
 
467
 
468
# If "-f" option is used, create a temporary tracevar file
468
# If "-f" option is used, create a temporary tracevar file
469
if ( "${flag_f}" == "-f" ) then
469
if ( "${flag_f}" == "-f" ) then
470
 
470
 
471
#   Preset values for <compfl> and <tprefix>
471
#   Preset values for <compfl> and <tprefix>
472
    set tcompfl=1
472
    set tcompfl=1
473
    set tprefix='P'
473
    set tprefix='P'
474
 
474
 
475
#   Split the variable name into its vectorial components, if necessary
475
#   Split the variable name into its vectorial components, if necessary
476
    set tvar0 = `echo ${tvar} | awk -F. '{print $1}' `
476
    set tvar0 = `echo ${tvar} | awk -F. '{print $1}' `
477
    set tvar1 = `echo ${tvar} | awk -F. '{print $2}' `
477
    set tvar1 = `echo ${tvar} | awk -F. '{print $2}' `
478
     
478
     
479
#   Check availability on P file - only for first vector component
479
#   Check availability on P file - only for first vector component
480
    foreach var ( `${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]` )
480
    foreach var ( `${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]` )
481
       if ( "${var}" == "${tvar0}" ) then
481
       if ( "${var}" == "${tvar0}" ) then
482
          set tcompfl=0
482
          set tcompfl=0
483
              set tprefix="P"
483
              set tprefix="P"
484
       endif
484
       endif
485
    end
485
    end
486
 
486
 
487
#   Check availability on S file 
487
#   Check availability on S file 
488
    if ( ${sfiles} == 1 ) then
488
    if ( ${sfiles} == 1 ) then
489
       foreach var ( `${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]` )
489
       foreach var ( `${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]` )
490
         if ( "${var}" == "${tvar0}" ) then
490
         if ( "${var}" == "${tvar0}" ) then
491
            set tcompfl=0
491
            set tcompfl=0
492
                set tprefix="S"
492
                set tprefix="S"
493
         endif
493
         endif
494
       end
494
       end
495
    endif
495
    endif
496
 
496
 
497
#   Write the temporary <tracevars> file
497
#   Write the temporary <tracevars> file
498
    echo "${tvar} ${tscale} ${tcompfl} ${tprefix}" >! ${tvfile}
498
    echo "${tvar} ${tscale} ${tcompfl} ${tprefix}" >! ${tvfile}
499
    echo "Temporary tracervar file <${tvfile}> created"
499
    echo "Temporary tracervar file <${tvfile}> created"
500
    echo
500
    echo
501
 
501
 
502
endif
502
endif
503
 
503
 
504
# Check if tracevars-file exists
504
# Check if tracevars-file exists
505
if ( ! -f $tvfile ) then
505
if ( ! -f $tvfile ) then
506
  echo  " ERROR:  file $tvfile was not found ***"
506
  echo  " ERROR:  file $tvfile was not found ***"
507
  exit 1
507
  exit 1
508
endif
508
endif
509
 
509
 
510
# check if the variables contained in the tracevars-file are available in the
510
# check if the variables contained in the tracevars-file are available in the
511
# data file and check also if there are no empty lines in the tracevars-file
511
# data file and check also if there are no empty lines in the tracevars-file
512
 
512
 
513
set nlines = `cat $tvfile | wc -l`
513
set nlines = `cat $tvfile | wc -l`
514
set vars   = `cat $tvfile | awk '{print $1}'`
514
set vars   = `cat $tvfile | awk '{print $1}'`
515
set nvars  = `echo $vars | wc -w`
515
set nvars  = `echo $vars | wc -w`
516
if ( $nlines != $nvars ) then
516
if ( $nlines != $nvars ) then
517
  echo " ERROR: tracevars-files must not contain empty lines ***"
517
  echo " ERROR: tracevars-files must not contain empty lines ***"
518
  exit 1
518
  exit 1
519
endif
519
endif
520
set calf=`cat $tvfile | awk '{print $3}'`
520
set calf=`cat $tvfile | awk '{print $3}'`
521
set tfil=`cat $tvfile | awk '{print $4}'`
521
set tfil=`cat $tvfile | awk '{print $4}'`
522
 
522
 
523
# Write some status information
523
# Write some status information
524
cat ${tvfile}
524
cat ${tvfile}
525
echo
525
echo
526
echo "# Number of tracing variables : ${nlines}"
526
echo "# Number of tracing variables : ${nlines}"
527
echo "Fields are read from following files : ${tfil}"
527
echo "Fields are read from following files : ${tfil}"
528
 
528
 
529
# Loop over all variables - check availability
529
# Loop over all variables - check availability
530
foreach v ( $vars )
530
foreach v ( $vars )
531
  if ( $calf[1] == 0 ) then
531
  if ( $calf[1] == 0 ) then
532
    set v0 = `echo $v | awk 'BEGIN {FS = ":"}; {print $1}'`
532
    set v0 = `echo $v | awk 'BEGIN {FS = ":"}; {print $1}'`
533
    set flag=`${LAGRANTO}/goodies/getvars $tfil[1]$datfiles[1] | grep " $v0 " | wc -l`
533
    set flag=`${LAGRANTO}/goodies/getvars $tfil[1]$datfiles[1] | grep " $v0 " | wc -l`
534
    set iscomment=`echo $v0 | cut -c 1` 
534
    set iscomment=`echo $v0 | cut -c 1` 
535
    if ( "${iscomment}" != "#" ) then 
535
    if ( "${iscomment}" != "#" ) then 
536
         if ( $flag == 0 ) then
536
         if ( $flag == 0 ) then
537
           echo " ERROR: variable $v listed in $tvfile is not on the $tfil[1]-files ***"
537
           echo " ERROR: variable $v listed in $tvfile is not on the $tfil[1]-files ***"
538
           exit 1
538
           exit 1
539
         endif
539
         endif
540
    endif
540
    endif
541
  endif
541
  endif
542
  shift calf
542
  shift calf
543
  shift tfil
543
  shift tfil
544
end
544
end
545
set ntrace=${nlines} 
545
set ntrace=${nlines} 
546
 
546
 
547
# ---------------------------------------------------------------------
547
# ---------------------------------------------------------------------
548
# Prepare input file for trace and run it
548
# Prepare input file for trace and run it
549
# ---------------------------------------------------------------------
549
# ---------------------------------------------------------------------
550
 
550
 
551
# Set times relative to the reference date
551
# Set times relative to the reference date
552
if ( "${changet}" == "true" ) then
552
if ( "${changet}" == "true" ) then
553
  echo
553
  echo
554
  echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
554
  echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
555
  echo   
555
  echo   
556
  foreach i ( $datfiles )
556
  foreach i ( $datfiles )
557
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}
557
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${charp}${i}
558
  end
558
  end
559
  if ( ${sfiles} == 1 ) then
559
  if ( ${sfiles} == 1 ) then
560
    foreach i ( $datfiles )
560
    foreach i ( $datfiles )
561
      ${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}
561
      ${LAGRANTO}/goodies/changet.sh ${refdate} ${chars}${i}
562
    end
562
    end
563
  endif
563
  endif
564
endif
564
endif
565
 
565
 
566
# ---------------------------------------------------------------------
566
# ---------------------------------------------------------------------
567
# Prepare input file for trace and run it
567
# Prepare input file for trace and run it
568
# ---------------------------------------------------------------------
568
# ---------------------------------------------------------------------
569
 
569
 
570
# Write parameter file
570
# Write parameter file
571
\rm -f ${parfile}
571
\rm -f ${parfile}
572
touch ${parfile}
572
touch ${parfile}
573
 
573
 
574
echo $inpfile                                              >> $parfile
574
echo $inpfile                                              >> $parfile
575
echo $outfile                                              >> $parfile    
575
echo $outfile                                              >> $parfile    
576
echo $startdate                                            >> $parfile
576
echo $startdate                                            >> $parfile
577
echo $enddate                                              >> $parfile
577
echo $enddate                                              >> $parfile
578
echo $idir                                                 >> $parfile
578
echo $idir                                                 >> $parfile
579
echo $numfiles                                             >> $parfile
579
echo $numfiles                                             >> $parfile
580
foreach i ( $datfiles )
580
foreach i ( $datfiles )
581
  echo $i                                                  >> $parfile
581
  echo $i                                                  >> $parfile
582
end
582
end
583
echo $timeinc                                              >> $parfile
583
echo $timeinc                                              >> $parfile
584
echo $tstart                                               >> $parfile
584
echo $tstart                                               >> $parfile
585
echo $tend                                                 >> $parfile
585
echo $tend                                                 >> $parfile
586
echo $ntra                                                 >> $parfile
586
echo $ntra                                                 >> $parfile
587
echo $ntim                                                 >> $parfile
587
echo $ntim                                                 >> $parfile
588
echo $ncol                                                 >> $parfile
588
echo $ncol                                                 >> $parfile
589
echo $ntrace                                               >> $parfile
589
echo $ntrace                                               >> $parfile
590
cat ${tvfile}                                              >> $parfile
590
cat ${tvfile}                                              >> $parfile
591
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1] | wc -l   >> $parfile
591
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1] | wc -l   >> $parfile
592
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]           >> $parfile
592
${LAGRANTO}/goodies/getvars ${charp}$datfiles[1]           >> $parfile
593
if ( $sfiles == 1 ) then
593
if ( $sfiles == 1 ) then
594
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1] | wc -l >> $parfile
594
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1] | wc -l >> $parfile
595
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]         >> $parfile
595
  ${LAGRANTO}/goodies/getvars ${chars}$datfiles[1]         >> $parfile
596
else
596
else
597
  echo 0                                                   >> $parfile
597
  echo 0                                                   >> $parfile
598
endif
598
endif
599
echo \"${timecheck}\"                                      >> $parfile
599
echo \"${timecheck}\"                                      >> $parfile
600
 
600
 
601
# Finish the preprocessor
601
# Finish the preprocessor
602
echo 
602
echo 
603
echo '       *** END OF PREPROCESSOR TRACE ***              '
603
echo '       *** END OF PREPROCESSOR TRACE ***              '
604
echo '========================================================='
604
echo '========================================================='
605
echo
605
echo
606
 
606
 
607
# Run  trace
607
# Run  trace
608
${prog}
608
${prog}
609
 
609
 
610
if ( "${status}" != "0" ) then
610
if ( "${status}" != "0" ) then
611
  echo "ERROR:  Program <trace> failed"
611
  echo "ERROR:  Program <trace> failed"
612
  exit 1
612
  exit 1
613
endif
613
endif
614
 
614
 
615
# ---------------------------------------------------------------------
615
# ---------------------------------------------------------------------
616
# Final tasks (make clean)
616
# Final tasks (make clean)
617
# ---------------------------------------------------------------------
617
# ---------------------------------------------------------------------
618
 
618
 
619
finish:
619
finish:
620
 
620
 
621
if ( "${noclean}" == "false" ) then
621
if ( "${noclean}" == "false" ) then
622
  \rm -f ${parfile}
622
  \rm -f ${parfile}
623
endif
623
endif
624
 
624
 
625
exit 0 
625
exit 0 
626
 
626