Subversion Repositories lagranto.um

Rev

Rev 3 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 michaesp 1
#!/bin/csh
2
 
3
# ---------------------------------------------------------------------
4
# Usage, parameter settings
5
# ---------------------------------------------------------------------
6
 
15 michaesp 7
# Set Lagranto 
8
set LAGRANTO = ${LAGRANTOBASE}.${MODEL}/
9
 
3 michaesp 10
# Write usage information
11
if ( (${#argv} == 0) | (${#argv} < 2) ) then
12
  echo 
13
  echo "--------------------------------------------------------------"
14
  echo " Usage: density.sh lslfile outfile [options]"
15
  echo 
16
  echo "        lslfile      : Input trajectory file "
17
  echo "        outfile      : Output netCDF file"
18
  echo
19
  echo " Optional flags                                               "
20
  echo
21
  echo "        -radius val        : filter radius (in km, default 300 km)  "
22
  echo "        -interp val unit   : interpolation (in h or km)"
23
  echo "        -time val          : time step (default all)"  
24
  echo "        -field name        : field for gridding"
25
  echo "        -create            : force creation of a new file"
26
  echo
27
  echo " Output grid:"
28
  echo  
29
  echo "        Default output grid has parameters: nlon   =  360"
30
  echo "                                            nlat   =  180"
31
  echo "                                            lonmin = -180"
32
  echo "                                            latmin =  -90"
33
  echo "                                            dlon   =    1"
34
  echo "                                            dlat   =    1"
35
  echo 
36
  echo "        The grid can be changed with the options: "
37
  echo 
38
  echo "               -latlon   nlon,nlat,lonmin,latmin,dlon,dlat" 
39
  echo "               -rotated  clon,clat,nlonlat,dlonlat"
40
  echo "--------------------------------------------------------------"
41
  exit 0
42
endif
43
 
44
# Write title
45
echo 
46
echo '========================================================='
47
echo '       *** START OF PREPROCESSOR DENSITY ***              '
48
echo
49
 
50
# ---------------------------------------------------------------------
51
# Handle input parameters
52
# ---------------------------------------------------------------------
53
 
54
# Get input parameters
55
set inpfile = $1
56
set outfile = $2
57
 
58
# Set default values
59
set radius     = 100
60
set unit       = 'km'
61
set grid       = "360 180 -180. -90. 1. 1."
62
set mode       = 'keep'
63
set tratime    = 'all'
64
set param      = 0
65
set gridtype   = 'latlon'
66
set sel_file   = 'nil'
67
set sel_format = 'nil'
68
set field      = 'nil'    
69
set crefile    = 0
70
 
71
# Handle optional arguments
72
while ( $#argv > 0 )
73
 
74
  switch ( $argv[1] )
75
 
76
   case -radius
77
     set radius = $argv[2]
78
     set unit   = $argv[3]
79
     echo "Flag '-radius'     -> ${radius} ${unit} (user defined)"
80
     shift;
81
     shift;
82
   breaksw
83
 
84
   case -time
85
     set tratime=$argv[2]
86
     echo "Flag '-tratime'    -> ${tratime} (user defined)"
87
     shift;
88
   breaksw
89
 
90
   case -interp
91
     set param=$argv[2]
92
 
93
     if ( "$argv[3]" == "h"   ) set mode = "time"
94
     if ( "$argv[3]" == "km"  ) set mode = "space"
95
     if ( "$argv[3]" == "deg" ) set mode = "grid"
96
 
97
     echo "Flag '-interp'      -> ${mode} ${param} (user defined)"
98
     shift;
99
     shift;
100
   breaksw
101
 
102
   case -create
103
     set crefile = 1
104
     echo "Flag '-create'      -> true (user defined)"
105
   breaksw
106
 
107
   case -latlon
108
     set gridtype = 'latlon'
109
     if ( "$argv[2]" == "dynamic" ) then
110
	set grid = "0 0 0 0 0 0"
111
	echo "Flag '-latlon'       -> dynamic  (user defined)"
112
	shift;
113
     else
114
        set nlon   = $argv[2]
115
        set nlat   = $argv[3]
116
        set lonmin = $argv[4]
117
        set latmin = $argv[5]
118
        set dlon   = $argv[6]
119
        set dlat   = $argv[7]
120
        set grid   = "${nlon} ${nlat} ${lonmin} ${latmin} ${dlon} ${dlat}"
121
        echo "Flag '-latlon      ->  ${grid}  (user defined)"
122
        shift;
123
        shift;
124
        shift;
125
        shift;
126
        shift;
127
        shift;
128
     endif
129
   breaksw
130
 
131
   case -rotated
132
     set gridtype = 'rotated'
133
     set clon     =  $argv[2]
134
     set clat     =  $argv[3]
135
     set nlonlat = $argv[4]
136
     set dlonlat = $argv[5]
137
     set grid   = "${clon} ${clat} ${nlonlat} ${dlonlat}"
138
     echo "Flag '-rotated     ->  ${clon}, ${clat}, ${nlonlat}, ${dlonlat}  (user defined)"
139
     shift;
140
     shift;
141
     shift;
142
     shift;
143
    breaksw
144
 
145
    case -index
146
       set sel_file   = $argv[2]
147
       set sel_format = 'index'
148
       echo "Flag '-index'     -> ${sel_file} (user defined)"
149
       shift;
150
   breaksw
151
 
152
   case -boolean
153
       set sel_file   = $argv[2]
154
       set sel_format = 'boolean'
155
       echo "Flag '-boolean'   -> ${sel_file} (user defined)"
156
       shift;
157
   breaksw
158
 
159
   case -field
160
       set field   = $argv[2]
161
       echo "Flag '-field'   -> ${field} (user defined)"
162
       shift;
163
   breaksw
164
 
165
  endsw
166
 
167
  shift;
168
 
169
end
170
 
171
# ---------------------------------------------------------------------
172
# Do some checks and preparation, then run the program
173
# ---------------------------------------------------------------------
174
 
175
# Rename field <time> to <TIME> to avoid conflict with time coordinate
176
# on the netCDF file
177
if ( "${field}" == "time" ) set field = "TIME"    
178
 
179
# Determine the time step 
180
if ( "${tratime}" == "all" ) then
181
    set step = 0
182
else
183
    set timelist = (`${LAGRANTO}/bin/trainfo.sh $inpfile times`) 
184
    set step     = 0
185
    set found    = 0
186
    foreach val ( ${timelist} )
187
       @ step = ${step} + 1
188
       if ( "${tratime}" == "${val}" ) then
189
          set found   = ${step}
190
       endif
191
     end
192
     if ( ${found} == 0 ) then
193
       echo "Invalid time ${tratime} for gridding"
194
       echo "${timelist}"
195
       exit 1
196
      endif
197
      set step = ${found}
198
endif
199
 
200
# Check consistency of arguments
201
if ( ( "${mode}" == "time" ) & ( ${step} != 0 ) ) then
202
    echo " ERROR: Options 'interp -time' and 'step' incompatible' "
203
    exit 1
204
endif
205
if ( ( "${mode}" == "space" ) & ( ${step} != 0 ) ) then
206
    echo " ERROR: Options 'interp -space' and 'step' incompatible' "
207
    exit 1
208
endif
209
 
210
# Get trajectory info
211
set ntra   = (`${LAGRANTO}/bin/trainfo.sh $inpfile ntra`)
212
set ntime  = (`${LAGRANTO}/bin/trainfo.sh $inpfile ntim`)
213
set nfield = (`${LAGRANTO}/bin/trainfo.sh $inpfile ncol`)
214
 
215
# Check whether selection file is available
216
if ( "${sel_file}" != "nil" ) then
217
   if ( ! -f ${sel_file} ) then
218
     echo " ERROR: selection file ${sel_file} is missing... Stop"
219
     exit 1
220
   endif
221
endif
222
 
223
# Check whether output file exists - set the <crefile> flag
224
if ( "${crefile}" == "0" ) then
225
   if ( ! -f ${outfile} ) then
226
       set crefile = 1
227
       echo
228
       echo "${outfile} will be created... "
229
   else
230
       echo
231
       echo "${outfile} will be modified ..."
232
   endif
233
else
234
   echo
235
   echo "${outfile} will be created... "
236
endif
237
 
238
# Chewck whether the variable exists - set the <crevar> flag
239
if ( "${crefile}" == "0" ) then    
240
    set varlist = ` ${LAGRANTO}/goodies/getvars ${outfile}` 
241
    set crevar  = 1
242
    foreach var ( ${varlist} )
243
       if ( "${var}" == "${field}" ) set crevar = 0
244
    end
245
else
246
   set crevar = 1
247
endif
248
 
249
# Prepare parameter file and run program
250
\rm -f density.param
251
touch density.param
252
echo ${inpfile}                 >> density.param
253
echo ${outfile}                 >> density.param
254
echo \"${field}\"               >> density.param
255
echo ${ntime} ${nfield} ${ntra} >> density.param
256
echo ${gridtype}                >> density.param
257
echo ${grid}                    >> density.param
258
echo ${radius} ${unit}          >> density.param
259
echo ${mode}                    >> density.param
260
echo ${param}                   >> density.param
261
echo ${step}                    >> density.param
262
echo \"${sel_file}\"            >> density.param
263
echo \"${sel_format}\"          >> density.param
264
echo ${crefile}                 >> density.param
265
echo ${crevar}                  >> density.param
266
 
267
# Write status info
268
echo 
269
echo '       *** END OF PREPROCESSOR DENSITY ***              '
270
echo '========================================================='
271
echo
272
 
273
# Run density
274
${LAGRANTO}/density/density
275
 
276
# Make clean
277
\rm -f density.param
278
 
279
exit 0
280
 
281
 
282
 
283