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 and parameter handling
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 create_startf short
14
  echo
15
  exit 1
16
endif
17
 
18
# -----------------------------------------------------------------------------------------------------------------
19
# Handle input parameters
20
# -----------------------------------------------------------------------------------------------------------------
21
 
22
# Prefix of primary and secondary file; and set the filenames
23
set charp='P'
24
set chars='S'
25
 
26
# Write title
27
echo 
28
echo '========================================================='
29
echo '       *** START OF PREPROCESSOR CREATE_STARTF ***              '
30
echo
31
 
32
# Extract arguments and split reference date
33
set refdate   = $1
34
set ofile     = $2
35
set specifier = `echo $3` 
36
shift
37
shift
38
shift
39
 
40
# Check whether specifier is a file or whether it is explicitly written
41
set flag_criterion = 'file'
42
set test = `echo ${specifier} | grep '@' | wc -c`
43
if ( "${test}" != "0" ) then 
44
  set flag_criterion = 'criterion'
45
endif
46
 
47
# Get the criterion from the file
48
if ( "${flag_criterion}" == "file" ) then
49
    if ( -f ${specifier} ) then
50
       set filename  = ${specifier}
51
       set specifier = `cat ${specifier}`
52
    else
53
       echo " ERROR: cannot read criterion from file ${specifier}... Stop"
54
       exit 1
55
    endif
56
endif
57
 
58
echo "---- INPUT PARAMETERS ----------------------------------"
59
echo 
60
echo "Reference date        : ${refdate}"
61
if ( "${flag_criterion}" == "criterion" ) then 
62
   echo "Specifier             : ${specifier}"
63
else
64
   echo "Specifier             : ${specifier} [from file ${filename}]"
65
endif
66
echo "Output file           : ${ofile}"
67
echo
68
 
69
# Handle optional arguments
70
set tvfile    = 'tracevars'
71
set changet   = 'false'
72
set noclean   = 'false'
73
set regionf   = 'regionf'
74
set timecheck = 'no' 
75
 
76
while ( $#argv > 0 )
77
 
78
  switch ( $argv[1] )
79
 
80
   case -t
81
     set tvfile = $argv[2]
82
     echo "tvfile                -> ${tvfile} (user defined)"
83
     shift;
84
   breaksw
85
 
86
   case -changet
87
     set changet = 'true'
88
     echo "changet               -> true (user defined)"
89
   breaksw
90
 
91
   case -noclean
92
     set noclean = 'true'
93
     echo "noclean               -> true (user defined)"
94
   breaksw
95
 
96
   case -timecheck
97
     set timecheck = 'yes'
98
     echo "timecheck               -> yes (user defined)"
99
   breaksw
100
 
101
   case -regionf
102
     set regionf = $argv[2]
103
     echo "regionf                -> ${regionf} (user defined)"
104
     shift;
105
   breaksw
106
 
107
   endsw
108
 
109
   shift;
110
 
111
end
112
 
113
# No change of times necessary if no check requested
114
if ( "${timecheck}" == "no" ) then
115
   set  changet = 'false'
116
endif
117
 
118
# Split the reference date
119
set yyyy=`echo ${refdate}   | cut -c 1-4` 
120
set   mm=`echo ${refdate}   | cut -c 5-6` 
121
set   dd=`echo ${refdate}   | cut -c 7-8` 
122
set   hh=`echo ${refdate}   | cut -c 10-11` 
123
set  min=`echo ${refdate}00 | cut -c 12-13` 
124
 
125
# Set base directories (run+prog)
126
set tradir=${PWD}
127
 
128
# Set program paths and filenames 
129
set parfile=${tradir}/create_startf.param 
130
set crifile=${tradir}/create_startf.criterion
131
 
132
# Decide whether a tracing and selection is necessary
133
# If so, some intermediate file are writen (in Fortran binary)
134
set flag = `${LAGRANTO}/startf/create_startf.perl "${specifier}" | tail -1` 
135
if ( "${flag}" != "nil" ) then
136
    set format1 = ".3"
137
    set format2 = ".3"
138
else
139
    set format1 = ""
140
    set format2 = ""
141
endif
142
 
143
# Write status information
144
echo
145
echo '---- DIRECTORIES AND PROGRAMS ---------------------------'
146
echo    
147
echo "PROGRAM CREATE_STARTF : ${LAGRANTO}/startf/create_startf"
148
echo "PARAMETER file        : ${parfile}"
149
echo "CRITERION file        : ${crifile}"
150
echo "RUN directory         : ${tradir}"
151
echo
152
 
153
# -----------------------------------------------------------------------------------------------------------------
154
# Set the primary and scecondary data files (necessary for interpolation if intermediate reference date)
155
# -----------------------------------------------------------------------------------------------------------------
156
 
157
# Find a first data file (if possible corresponding to reference date)
158
# If exact date/time match is found, timeinc is set to 1 (any number would do)
159
set file=${charp}${yyyy}${mm}${dd}_${hh}
160
if ( ( -f ${file} ) && ( ${min} == 0 ) ) then
161
   set timeshift=0
162
   set date0=${yyyy}${mm}${dd}_${hh}
163
   set date1=${yyyy}${mm}${dd}_${hh}
164
   set pfile0=${file}
165
   set pfile1=${file}
166
   set timeinc=1 
167
   goto label3
168
endif
169
set file=${charp}${yyyy}${mm}${dd}_${hh}${min}
170
if ( -f ${file} ) then
171
   set timeshift=0
172
   set date0=${yyyy}${mm}${dd}_${hh}${min}
173
   set date1=${yyyy}${mm}${dd}_${hh}${min}
174
   set pfile0=${file}
175
   set pfile1=${file}
176
   set timeinc=1
177
   goto label3
178
endif
179
   set file=`ls ${charp}[0-9_]*[0-9] | head -1 | sed -e 's/@//'`
180
endif
181
 
182
# Determine time increment (in hours) between data files
183
set date1=`echo ${file} | cut -c 2-12`
184
set n=`ls ${charp}[0-9_]*[0-9] | grep -n $date1 | awk -F: '{print $1}'`
185
@ n ++
186
set date2=`ls ${charp}[0-9_]*[0-9] | head -$n | tail -1 | cut -c 2-12`
187
set timeinc=`${LAGRANTO}/goodies/gettidiff $date2 $date1`
188
endif
189
if ( $timeinc == 0 ) then
190
    echo " ERROR: cannot set the time increment between input files ***"
191
    exit 1
192
endif
193
 
194
# Search the first file to use
195
set timeshift=
196
foreach i ( ${charp}????????_??* )
197
 
198
  set date0 = `echo $i | cut -c 2-12`
199
  set td1  = `${LAGRANTO}/goodies/gettidiff ${yyyy}${mm}${dd}_${hh} ${date0}`
200
 
201
  if ( ( $td1 >= 0 ) && ( $td1 < $timeinc ) )  then
202
      set timeshift=$td1
203
      set pfile0=${charp}${date0}
204
      goto label2
205
  endif
206
 
207
end
208
 
209
# Check if no P files are available for the specified time period
210
if ( $timeshift == "" ) then
211
  echo " ERROR: no data files available for the specified reference date"
212
  exit 1
213
endif
214
 
215
# Everything is fine so far: proceed
216
label2:
217
 
218
# Set the next date and check its availability
219
if ( ( ${timeshift} != 0 ) || ( ${min} > 0 ) ) then
220
    set date1=`${LAGRANTO}/goodies/newtime $date0 $timeinc`
221
    if ( ! -f ${charp}${date1} ) then
222
       echo " ERROR: file with primary data is missing for $date"
223
       exit 1
224
    else
225
       set pfile1=${charp}${date1}
226
    endif
227
else
228
    set date1=${date0}
229
    set pfile1=${pfile0}
230
endif
231
 
232
# Set the final timeshift 
233
if ( ${min} != 00 ) then
234
    set timeshift=${timeshift}.${min}
235
endif
236
 
237
# Everything is fine!
238
label3:
239
 
240
# Set secondary files and check their availability
241
set sfile0=${chars}${date0}
242
set sfile1=${chars}${date1}
243
 
244
# Write status information
245
echo '---- DATA FILES -----------------------------------------'
246
echo    
247
echo "Primary files                         : ${pfile0}"
248
echo "                                      : ${pfile1}"
249
echo "Secondary files                       : ${sfile0}"
250
echo "                                      : ${sfile1}"
251
echo "Timeshift to first data file (hh.mm)  : ${timeshift}"
252
echo "Time increment of data files          : ${timeinc}"
253
echo
254
 
255
# --------------------------------------------------------------------------------------------------------------
256
# Create the start positions (without selection)
257
# -----------------------------------------------------------------------------------------------------------------
258
 
259
# Set times relative to the reference date
260
if ( "${changet}" == "true" ) then
261
  echo '---- CHANGE TIMES ON DATA FILES  ------------------------'
262
  echo   
263
  ${LAGRANTO}/goodies/changet.sh ${refdate} ${pfile0}
264
  ${LAGRANTO}/goodies/changet.sh ${refdate} ${pfile1}
265
  if ( -f  ${sfile0} ) then
266
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${sfile0}
267
  endif
268
  if ( -f  ${sfile1} ) then
269
    ${LAGRANTO}/goodies/changet.sh ${refdate} ${sfile1}
270
  endif
271
endif
272
 
273
# Write parameters to parameter file and create the starting positions
274
\rm -f ${parfile}
275
echo \"${pfile0}\" \"${pfile1}\"    >! ${parfile}
276
echo \"${sfile0}\" \"${sfile1}\"    >> ${parfile}
277
echo \"${ofile}${format1}\"         >> ${parfile}
278
echo \"${regionf}\"                 >> ${parfile}
279
echo ${yyyy}                        >> ${parfile}
280
echo ${mm}                          >> ${parfile}
281
echo ${dd}                          >> ${parfile}
282
echo ${hh}                          >> ${parfile}
283
echo ${min}                         >> ${parfile}
284
echo 00                             >> ${parfile}
285
echo ${timeshift}                   >> ${parfile}
286
echo ${timeinc}                     >> ${parfile}
287
 
288
# Analyse the specifier and append to parameter file
289
${LAGRANTO}/startf/create_startf.perl "${specifier}" >> ${parfile}
290
 
291
if ( "${status}" != "0" ) then
292
  echo "ERROR:  Preprocessor <create_startf> failed"
293
  exit 1
294
endif
295
 
296
# Write selection criterion to file
297
\rm -f ${crifile}
298
tail -1 ${parfile} >! ${crifile} 
299
 
300
# Write flag for no time check
301
echo \"${timecheck}\"               >> ${parfile}
302
 
303
# Write title
304
echo 
305
echo '       *** END OF PREPROCESSOR CREATE_STARTF ***'        
306
echo '========================================================='
307
echo
308
 
309
# Create the startf
310
cd ${tradir}
311
${LAGRANTO}/startf/create_startf
312
 
313
if ( "${status}" != "0" ) then
314
  echo "ERROR:  Program <create_startf> failed"
315
  exit 1
316
endif
317
 
318
# --------------------------------------------------------------------------------------------------------------
319
# Apply selection (first tracing then selection)
320
# --------------------------------------------------------------------------------------------------------------
321
 
322
# Stop if no tracing and selection is necessary
323
if ( "${flag}" == "nil" ) goto finish
324
 
325
# Tracing of extra variables
326
if ( -f  ${ofile}${format2} ) then
327
  \rm -f ${ofile}${format2}
328
endif
329
if ( "${timecheck}" == "no" ) then
330
  ${LAGRANTO}/trace/trace.sh ${ofile}${format1} ${ofile}${format2} -v ${tvfile} -notimecheck
331
else
332
  ${LAGRANTO}/trace/trace.sh ${ofile}${format1} ${ofile}${format2} -v ${tvfile}
333
endif
334
\rm -f ${ofile}${format1}
335
 
336
# Selection
337
if ( -f  ${ofile} ) then
338
  \rm -f ${ofile}
339
endif
340
${LAGRANTO}/select/select.sh ${ofile}${format2} ${ofile} `cat ${crifile}` 
341
\rm -f ${ofile}${format2}
342
 
343
# --------------------------------------------------------------------------------------------------------------
344
# Final tasks (make clean)
345
# --------------------------------------------------------------------------------------------------------------
346
 
347
finish:
348
 
349
echo $noclean
350
 
351
if ( "${noclean}" == "false" ) then
352
  \rm -f ${crifile}
353
  \rm -f ${parfile}
354
 endif
355
 
356
exit 0 
357
 
358
 
359