Subversion Repositories pvinversion.ecmwf

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
# Set some variables and paths
5
# -------------------------------------------------------------------
6
 
7
# Jump to specified step
8
set step="help"
9
set param1= 
10
set param2=
11
set param3=
12
 
13
if ( $#argv == 1 ) then
14
  set step=$1
15
endif
16
if ( $#argv == 2 ) then
17
  set step=$1
18
  set param1=$2
19
endif
20
if ( $#argv == 3 ) then
21
  set step=$1
22
  set param1=$2
23
  set param2=$3
24
endif
25
if ( $#argv == 4 ) then
26
  set step=$1
27
  set param1=$2
28
  set param2=$3
29
  set param3=$4
30
endif
31
 
32
set file1=${param1}
33
set file2=${param2}
34
 
35
# Set base directory for programmes
36
set bdir=${DYN_TOOLS}/inversion/
37
 
38
# Set name of the parameter file, coastline file and sample directory
39
set parafile="${PWD}/inversion.param"
40
set coastfile="${bdir}/prep/coastline.dat"
41
set sampledir="/net/bio/atmosdyn/erainterim/cdf/2006/01/"
42
 
43
# ---------------------------------------------------------------------------
44
# Installation, help and sample
45
# ---------------------------------------------------------------------------
46
 
47
# Get parameter file, if not yet available
48
if ( ! -f ${parafile} ) then
49
       cp ${bdir}/inversion.param . 
50
endif
51
 
52
# Extract parameters from parameter file
53
set progex=${bdir}/inversion.perl
54
set    date=`${progex} ${parafile} date           | awk '{ print $2}'`
55
set nofiter=`${progex} ${parafile} n_of_iteration | awk '{ print $2}'`
56
set    save=`${progex} ${parafile} save_iteration | awk '{ print $2}'`
57
set    idir=`${progex} ${parafile} inp_dir        | awk '{ print $2}'`
58
set    rdir=`${progex} ${parafile} run_dir        | awk '{ print $2}'`
59
set    odir=`${progex} ${parafile} out_dir        | awk '{ print $2}'`
60
 
61
# Create the needed directories
62
if ( ${step} == "inst" ) then  
63
  if ( ! -d ${idir} ) mkdir ${idir}
64
  if ( ! -d ${rdir} ) mkdir ${rdir}
65
  if ( ! -d ${odir} ) mkdir ${odir}
66
endif
67
 
68
# Create the needed directories
69
if ( ${step} == "help" ) then 
70
  echo 
71
  echo "Installation"
72
  echo "   inst:   Creates the input, run and output directory; copy template parameter file"
73
  echo 
74
  echo "Sample case study"
75
  echo "   sample: Copy all files for a sample case study"
76
  echo 
77
  echo "Preparing input files [prep]"
78
  echo "   prep0:  Calculate S file with PV and TH [ P -> S ]"
79
  echo "   prep1:  Interpolate onto height levels [ P,Z -> H ]"
80
  echo "   prep2:  Rotate into local cartesian coordinate system [ H -> R ]"
81
  echo "   prep3:  Add TH,PV,NSQ and RHO to the data file [ -> R ]"
82
  echo "   prep4:  Define modified and anomaly PV field and boundary values [ -> R ]"
83
  echo "   prep5:  Reduce the domain size and split the input files [ R -> ORG,MOD,ANO,REF]" 
84
  echo "   prep6:  Add the reference profile [MOD -> REF]"
85
  echo "   prep7:  Add coastlines to REF file [-> REF]"
86
  echo "   prep8:  Move the files to the run directory [ORG,MOD,ANO,REF -> ]"
87
  echo
88
  echo "Perform the PV inversion [pvin]"
89
  echo "   pvin1:  Add NSQ, TH, RHO, and PV [ -> MOD]"
90
  echo "   pvin2:  Change Ertel's PV anomaly into one of quasi-geostrophic PV [MOD -> ANO]"
91
  echo "   pvin3:  Inversion of quasi-geostrophic PV anomaly [ANO -> ANO]"
92
  echo "   pvin4:  Subtract anomaly from MOD file [MOD-ANO -> MOD]"
93
  echo "   pvin5:  Keep iterative steps if save flag is set"
94
  echo
95
  echo "Postprocessing [post]"
96
  echo "   post1:  Copy needed files from input and run directory [P,MOD -> ]" 
97
  echo "   post2:  Rotate from quasi-cartesian coordinate frame to lat/lon system [ MOD -> GEO ]"
98
  echo "   post3:  Bring modified fields back to P file [ P+GEO -> P ]" 
99
  echo "   post4:  Calculate S file with PV and TH [ P -> S ]"
100
  echo "   post5:  Make clean"
101
  echo "   post6:  Calculate difference to original P and S file"
102
  echo
103
  echo "Diagnostic Tools"
104
  echo "   diag1:  Check the consitency of the boundary conditions"
105
  echo "   diag2:  Calculate the quasi-geostrophic PV (diag2 [ORG|MOD|ANO])"
106
  echo "   diag3:  Get the difference between two files (diag3 [ORG|MOD|ANO] [ORG|MOD|ANO])"
107
  echo "   diag4:  Calculate geopotential with hydrostatic equation (diag4 [P|ORG|MOD|ANO])"
108
  echo "   diag5:  Q vector analysis; geostrophic wind balance [ORG|MOD|ANO] [GEO|DIV_UV|QVEC|DIV_Q]"
109
  echo
110
  echo "Modifying Pv anomalies"
111
  echo "   mod1:  Stretching and amplitude changes for anomalies [MOD]" 
112
endif
113
 
114
 
115
# Copy sample files (if specified)
116
if ( ${step} == "sample" ) then 
117
  \cp ${sampledir}/P20060116_18 ${idir}
118
  \cp ${sampledir}/eraint_cst   ${idir}
119
  \cp ${sampledir}/Z20060116_18 ${idir}
120
  \cp ${sampledir}/pressure_cst ${idir}
121
endif
122
 
123
# ---------------------------------------------------------------------------
124
# Preparatory steps
125
# ---------------------------------------------------------------------------
126
 
127
# Change to data directory
128
cd ${idir}
129
 
130
# Step 0: Calculate S file with PV and TH (not in prep mode)
131
if ( ${step} == "prep0" ) then
132
    \rm -f S${date}
133
    p2s P${date} TH PV    
134
endif
135
 
136
# Step 1: Interpolate onto height levels
137
if ( ${step} == "prep1" | ${step} == "prep" ) then
138
  echo "P${date}"                        >! fort.10
139
  echo "Z${date}"                        >> fort.10
140
  echo "H${date}"                        >> fort.10
141
  ${bdir}/inversion.perl ${parafile} p2z >> fort.10
142
  echo "U        U       P${date}"       >> fort.10
143
  echo "V        V       P${date}"       >> fort.10
144
  echo "T        T       P${date}"       >> fort.10
145
  echo "Q        Q       P${date}"       >> fort.10
146
  ${bdir}/prep/p2z
147
  \rm -f fort.10 
148
endif
149
 
150
# Step 2: Rotate into local cartesian coordinate system 
151
if ( ${step} == "prep2" | ${step} == "prep" ) then
152
  echo "H${date}"                                >! fort.10
153
  echo "R${date}"                                >> fort.10 
154
  ${bdir}/inversion.perl ${parafile} rotate_grid >> fort.10
155
  echo "5"                                       >> fort.10
156
  echo "ORO"                                     >> fort.10              
157
  echo "U.V"                                     >> fort.10
158
  echo "P"                                       >> fort.10
159
  echo "T"                                       >> fort.10
160
  echo "Q"                                       >> fort.10
161
  ${bdir}/prep/rotate_grid
162
  \rm -f fort.10
163
endif
164
 
165
# Step 3: Add TH,PV,NSQ and RHO to the data file
166
if ( ${step} == "prep3" | ${step} == "prep" ) then
167
  echo "TH"         >! fort.10
168
  echo "R${date}"   >> fort.10
169
  echo "R${date}"   >> fort.10
170
  echo "5       "   >> fort.10
171
  ${bdir}/prep/z2s
172
  echo "PV"         >! fort.10
173
  echo "R${date}"   >> fort.10
174
  echo "R${date}"   >> fort.10
175
  echo "5       "   >> fort.10
176
  ${bdir}/prep/z2s
177
  echo "NSQ"        >! fort.10
178
  echo "R${date}"   >> fort.10
179
  echo "R${date}"   >> fort.10
180
  echo "5       "   >> fort.10
181
  ${bdir}/prep/z2s
182
  echo "RHO"        >! fort.10
183
  echo "R${date}"   >> fort.10
184
  echo "R${date}"   >> fort.10
185
  echo "5       "   >> fort.10
186
  ${bdir}/prep/z2s
187
  \rm -f fort.10
188
endif
189
 
190
# Step 4: Set the modified PV field and boundary values
191
if ( ${step} == "prep4" | ${step} == "prep" ) then
192
  echo "R${date}"                                >! fort.10
193
  ${bdir}/inversion.perl ${parafile} def_anomaly >> fort.10
194
  ${bdir}/prep/def_anomaly
195
  \rm -f fort.10 
196
endif
197
 
198
# Step 5: Reduce the domain size and split the input files
199
if ( ${step} == "prep5" | ${step} == "prep" ) then
200
  echo "PV        PV        R${date}    ORG_${date}" >! fort.10
201
  echo "U         U         R${date}    ORG_${date}" >> fort.10
202
  echo "V         V         R${date}    ORG_${date}" >> fort.10
203
  echo "TH        TH        R${date}    ORG_${date}" >> fort.10
204
  echo "Q         Q         R${date}    ORG_${date}" >> fort.10
205
  echo "P         P         R${date}    ORG_${date}" >> fort.10
206
  echo "T         T         R${date}    ORG_${date}" >> fort.10
207
  echo "PV_FILT   PV_AIM    R${date}    MOD_${date}" >> fort.10
208
  echo "U         U         R${date}    MOD_${date}" >> fort.10
209
  echo "V         V         R${date}    MOD_${date}" >> fort.10
210
  echo "Q         Q         R${date}    MOD_${date}" >> fort.10
211
  echo "P         P         R${date}    MOD_${date}" >> fort.10
212
  echo "T         T         R${date}    MOD_${date}" >> fort.10
213
  echo "NSQ       NSQ       R${date}    MOD_${date}" >> fort.10
214
  echo "RHO       RHO       R${date}    MOD_${date}" >> fort.10
215
  echo "TH        TH        R${date}    MOD_${date}" >> fort.10
216
  echo "PV_ANOM   PV        R${date}    ANO_${date}" >> fort.10
217
  echo "TH_ANOM   TH        R${date}    ANO_${date}" >> fort.10
218
  echo "UU_ANOM   U         R${date}    ANO_${date}" >> fort.10
219
  echo "VV_ANOM   V         R${date}    ANO_${date}" >> fort.10
220
  echo "ORO       ORO       R${date}    REF_${date}" >> fort.10
221
  echo "X         X         R${date}    REF_${date}" >> fort.10
222
  echo "Y         Y         R${date}    REF_${date}" >> fort.10
223
  echo "LAT       LAT       R${date}    REF_${date}" >> fort.10
224
  echo "LON       LON       R${date}    REF_${date}" >> fort.10
225
  echo "CORIOL    CORIOL    R${date}    REF_${date}" >> fort.10
226
  ${bdir}/prep/cutnetcdf
227
  \rm -f fort.10 
228
endif
229
 
230
# Step 6: Add the reference profile 
231
if ( ${step} == "prep6" | ${step} == "prep" ) then
232
  \rm -f fort.10 
233
  echo "MOD_${date}"                       >! fort.10
234
  echo "REF_${date}"                       >> fort.10
235
  ${bdir}/inversion.perl ${parafile} ref   >> fort.10
236
  ${bdir}/prep/ref_profile
237
  \rm -f fort.10 
238
endif
239
 
240
# Step 7: Add coastlines to REF file
241
if ( ${step} == "prep7" | ${step} == "prep" ) then
242
  \rm -f fort.10 
243
  echo \"REF_${date}\"                         >! fort.10
244
  echo \"${coastfile}\"                        >> fort.10
245
  ${bdir}/inversion.perl ${parafile} coastline >> fort.10
246
  ${bdir}/prep/coastline
247
  \rm -f fort.10 
248
endif
249
 
250
# Step 8: Move the files to the run directory
251
if ( ${step} == "prep8" | ${step} == "prep" ) then
252
  \mv MOD_${date} MOD_${date}_cst ${rdir}
253
  \mv ORG_${date} ORG_${date}_cst ${rdir}
254
  \mv ANO_${date} ANO_${date}_cst ${rdir}
255
  \mv REF_${date} REF_${date}_cst ${rdir}
256
  \rm -f fort.10
257
endif
258
 
259
# ---------------------------------------------------------------------------
260
# Inversion
261
# ---------------------------------------------------------------------------
262
 
263
# Change to data directory
264
cd ${rdir}
265
 
266
# Start loop
267
set count=0
268
loop:
269
 
270
# Step 1: Add NSQ, TH, RHO, and PV to MOD file, take grid from REF file
271
if ( ${step} == "pvin1" | ${step} == "pvin" ) then
272
  echo "NSQ"           >! fort.10
273
  echo "MOD_${date}"   >> fort.10
274
  echo "REF_${date}"   >> fort.10
275
  echo "5          "   >> fort.10
276
  ${bdir}/pvin/z2s
277
  echo "RHO"           >! fort.10
278
  echo "MOD_${date}"   >> fort.10
279
  echo "REF_${date}"   >> fort.10
280
  echo "5          "   >> fort.10
281
  ${bdir}/pvin/z2s
282
  echo "TH"            >! fort.10
283
  echo "MOD_${date}"   >> fort.10
284
  echo "REF_${date}"   >> fort.10
285
  echo "5          "   >> fort.10
286
  ${bdir}/pvin/z2s
287
  echo "PV"            >! fort.10
288
  echo "MOD_${date}"   >> fort.10
289
  echo "REF_${date}"   >> fort.10
290
  echo "5          "   >> fort.10
291
  ${bdir}/pvin/z2s
292
  \rm -f fort.10
293
endif
294
 
295
# Step 2: Change Ertel's PV anomaly into an anomaly of quasi-geostrophic PV
296
if ( ${step} == "pvin2" | ${step} == "pvin" ) then
297
  echo "MOD_${date}"   >! fort.10
298
  echo "REF_${date}"   >> fort.10
299
  echo "ANO_${date}"   >> fort.10
300
  ${bdir}/pvin/pv_to_qgpv
301
  \rm -f fort.10 
302
endif
303
 
304
# Step 3: Inversion of quasi-geostrophic PV anomaly with Neumann boundary 
305
if ( ${step} == "pvin3" | ${step} == "pvin" ) then
306
  echo "ANO_${date}"   >! fort.10
307
  echo "REF_${date}"   >> fort.10
308
  ${bdir}/pvin/inv_cart
309
  \rm -f fort.10 
310
endif
311
 
312
# Step 4: Prepare the output of the inversion for next iteration step
313
if ( ${step} == "pvin4" | ${step} == "pvin" ) then
314
  \rm -f fort.10 
315
  echo "MOD_${date}"                                >! fort.10
316
  echo "ANO_${date}"                                >> fort.10
317
  ${bdir}/inversion.perl ${parafile} prep_iteration >> fort.10  
318
  ${bdir}/pvin/prep_iteration
319
  \rm -f fort.10 
320
endif
321
 
322
# Step 5: Keep iterative steps if save flag is set
323
if ( ${step} == "pvin5" | ${step} == "pvin" ) then
324
  if ( "${save}" == "yes" ) then
325
    set pre=''
326
    if ( ${count} < 10 ) then
327
      set pre='0'
328
    endif
329
    \cp MOD_${date} MOD_${date}_${pre}${count}
330
    \cp ANO_${date} ANO_${date}_${pre}${count}
331
  endif
332
endif
333
 
334
# End loop for iterations
335
if ( ${step} == "pvin" ) then
336
  @ count = ${count} + 1
337
  if ( ${count} < ${nofiter} ) goto loop
338
endif
339
 
340
# ---------------------------------------------------------------------------
341
# Postprocessing
342
# ---------------------------------------------------------------------------
343
 
344
# Change to output directory
345
cd ${odir}
346
 
347
# Step 1: Copy needed files from input and run directory
348
if ( ${step} == "post1" | ${step} == "post" ) then
349
    ln -sf ${rdir}/MOD_${date}       .
350
    ln -sf ${rdir}/MOD_${date}_cst   .
351
    ln -sf ${rdir}/ORG_${date}       .
352
    ln -sf ${rdir}/ORG_${date}_cst   .
353
    \cp ${idir}/P${date}             .
354
    set cfn = `getcfn P${date}`
355
    \cp ${idir}/${cfn}               .
356
endif
357
 
358
# Step 2:  Rotate from quasi-cartesian coordinate frame to lat/lon system
359
if ( ${step} == "post2" | ${step} == "post" ) then
360
    echo "MOD_${date}"                                >! fort.10
361
    echo "GEO.MOD_${date}"                            >> fort.10   
362
    ${bdir}/inversion.perl ${parafile} rotate_lalo    >> fort.10
363
    echo "3"                                          >> fort.10
364
    echo "T"                                          >> fort.10     
365
    echo "U.V"                                        >> fort.10
366
    echo "P"                                          >> fort.10
367
    ${bdir}/post/rotate_lalo
368
    \rm -f fort.10 
369
    echo "ORG_${date}"                                >! fort.10
370
    echo "GEO.ORG_${date}"                            >> fort.10   
371
    ${bdir}/inversion.perl ${parafile} rotate_lalo    >> fort.10
372
    echo "3"                                          >> fort.10
373
    echo "T"                                          >> fort.10     
374
    echo "U.V"                                        >> fort.10
375
    echo "P"                                          >> fort.10
376
    ${bdir}/post/rotate_lalo
377
    \rm -f fort.10 
378
endif
379
 
380
# Step 3: Bring modified fields back to P file
381
if ( ${step} == "post3" | ${step} == "post" ) then
382
    \rm -f fort.10 
383
    echo "P${date}"                                   >! fort.10
384
    echo "GEO.MOD_${date}"                            >> fort.10 
385
    echo "GEO.ORG_${date}"                            >> fort.10  
386
    ${bdir}/inversion.perl ${parafile} add2p          >> fort.10
387
    ${bdir}/post/add2p
388
    \rm -f fort.10 
389
endif
390
 
391
# Step 4: Calculate S file with PV and TH
392
if ( ${step} == "post4" | ${step} == "post" ) then
393
    \rm -f S${date}
394
    p2s P${date} TH PV    
395
endif
396
 
397
# Step 5: Make clean
398
if ( ${step} == "post5" | ${step} == "post" ) then
399
    \rm -f MOD_${date} MOD_${date}_cst
400
    \rm -f GEO_${date} GEO_${date}_cst
401
endif
402
 
403
# Step 6: Get difference of original and modified P,S files
404
if ( ${step} == "post6" | ${step} == "post" ) then
405
   \rm -f levs
406
   echo "950." >! levs
407
   echo "925." >> levs
408
   echo "900." >> levs
409
   echo "875." >> levs
410
   echo "850." >> levs
411
   echo "825." >> levs
412
   echo "800." >> levs
413
   echo "775." >> levs
414
   echo "750." >> levs
415
   echo "725." >> levs
416
   echo "700." >> levs
417
   echo "675." >> levs
418
   echo "650." >> levs
419
   echo "625." >> levs
420
   echo "600." >> levs
421
   echo "575." >> levs
422
   echo "550." >> levs
423
   echo "525." >> levs
424
   echo "500." >> levs
425
   echo "475." >> levs
426
   echo "450." >> levs
427
   echo "425." >> levs
428
   echo "400." >> levs
429
   echo "375." >> levs
430
   echo "350." >> levs
431
   echo "325." >> levs
432
   echo "300." >> levs
433
   echo "275." >> levs
434
   echo "250." >> levs
435
   echo "225." >> levs
436
   echo "200." >> levs
437
   echo "175," >> levs
438
   echo "150." >> levs
439
   echo "125." >> levs
440
   echo "100." >> levs
441
   echo " 75." >> levs
442
   echo " 50." >> levs
443
 
444
   \rm -f vars
445
   echo "U P"  >! vars
446
   echo "V P"  >> vars
447
   echo "T P"  >> vars
448
   echo "Z P"  >> vars
449
   echo "TH S" >> vars
450
   echo "PV S" >> vars
451
 
452
   nput2p ${date} pr_cst levs vars
453
   ncks -A -v PS P${date} L${date}
454
   \mv -f L${date} L${date}.1
455
 
456
   \cp levs vars ${idir}/
457
   cd ${idir}
458
   nput2p ${date} pr_cst levs vars
459
   ncks -A -v PS P${date} L${date}
460
   \rm levs
461
   \rm vars
462
   \mv L${date} ${odir}/L${date}.0
463
   cd ${odir} 
464
 
465
   ncdiff L${date}.1 L${date}.0 L${date}
466
   \rm -f L${date}.1
467
   \rm -f L${date}.0
468
 
469
endif
470
 
471
 
472
# ---------------------------------------------------------------------------
473
# Diagnotic Tools
474
# ---------------------------------------------------------------------------
475
 
476
# Step 1: Check the consistency of the boundary conditions (diag1)
477
if ( ${step} == "diag1"  ) then
478
    cd ${rdir}
479
    \rm -f fort.10 
480
    echo "ANO_${date}"  >! fort.10
481
    echo "REF_${date}"  >> fort.10   
482
    ${bdir}/diag/check_boundcon
483
    \rm -f fort.10 
484
endif
485
 
486
# Step 2: Calculate the quasi-geostrophic PV (diag2 [ORG|MOD|ANO]
487
if ( ${step} == "diag2"  ) then
488
    cd ${rdir}
489
    \rm -f fort.10 
490
    echo "${file1}_${date}"  >! fort.10
491
    echo "REF_${date}"       >> fort.10  
492
    ${bdir}/diag/calc_qgpv
493
    \rm -f fort.10 
494
endif
495
 
496
# Step 3: Get difference between two files (diag3 [ORG|MOD|ANO] - [ORG|MOD|ANO])
497
if ( ${step} == "diag3"  ) then
498
    cd ${rdir}
499
    \rm -f fort.10 
500
    echo "${file1}_${date}"  >! fort.10
501
    echo "${file2}_${date}"  >> fort.10  
502
    echo "DIA_${date}"       >> fort.10  
503
    ${bdir}/diag/difference
504
    \rm -f fort.10 
505
endif
506
 
507
# Step 4: Calculate geopotential with hydrostatic equation (diag4)
508
if ( ${step} == "diag4"  ) then
509
    cd ${odir}
510
    \rm -f fort.10 
511
    echo "P${date}"  >! fort.10
512
    echo "P${date}"  >> fort.10  
513
    ${bdir}/diag/hydrostatic
514
    \rm -f fort.10 
515
endif
516
 
517
# Step 5:  Q vector analysis / geostrophic balance check
518
if ( ${step} == "diag5"  ) then
519
    cd ${rdir}
520
    \rm -f fort.10
521
    echo "${param3}"         >! fort.10
522
    echo "${file1}"          >> fort.10
523
    echo "${file2}"          >> fort.10
524
    echo 1                   >> fort.10
525
    ${bdir}/diag/qvec_analysis
526
    \rm -f fort.10
527
endif
528
 
529
 
530
# ---------------------------------------------------------------------------
531
# Modifying tools
532
# ---------------------------------------------------------------------------
533
 
534
if ( ${step} == "mod1"  ) then
535
    cd ${idir}
536
    \rm -f fort.10 
537
    set commandstr=$2
538
    echo "R${date}"          >! fort.10  
539
    echo \"${commandstr}\"   >> fort.10    
540
    ${bdir}/spec/modify_anomaly
541
    \rm -f fort.10 
542
endif
543
 
544
 
545
 
546
 
547
 
548
 
549
 
550