Subversion Repositories lagranto.um

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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