forked from hmchristensen/MUMIP-cg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coarsen_icon_manyt.ncl
1782 lines (1325 loc) · 64 KB
/
coarsen_icon_manyt.ncl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; coarsen_icon_manyt.ncl
;================================================================================
; Copyright 2021 Hannah M. Christensen
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
; http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;================================================================================
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/esmf/ESMF_regridding.ncl"
load "func_read_hires.ncl"
load "func_coords_in.ncl"
load "func_reduce_area.ncl"
load "add_to_file.ncl"
load "func_advection.ncl"
load "func_advtend.ncl"
load "func_geostrophic.ncl"
load "func_thetal.ncl"
begin
;===================================;
; check input information from CLI ;
;===================================;
if (.not. isdefined("datein") .or. \
.not. isdefined("hourin")) then
print("usage: ncl coarsen_icon_manyt.ncl datein=yyyymmdd hourin=i")
exit()
end if
if ((hourin .ne. 0) .and. (hourin .ne. 1) .and. (hourin .ne. 2) .and. (hourin .ne. 3) \
.and. (hourin .ne. 4) .and. (hourin .ne. 5) .and. (hourin .ne. 6) \
.and. (hourin .ne. 7) ) then
print("error: hourin must be integer between 0 and 7")
exit()
end if
;=======================================================
; User defined variables
;===================================;
; define coarsening resolution ;
;===================================;
; define coarse graining resolution / degrees
CG_resol = "0.2"
; define expected temporal resolution files in hours
t_step_in = 3
;===================================;
; define temporal interpolation ;
;===================================;
t_step_out = t_step_in*60*60 ; in seconds. Ensure integer.
flag_interp_time = False ; for DEPHY, we will not perform interpolation
;===================================;
; define optional smoothing ;
;===================================;
flag_smooth = True
;====================================;
; define optional land interpolation ;
; - sets land points to Nan ;
; and interp from neighbouring sea ;
;====================================;
flag_land = True
;====================================;
; define whether to use CG high-res ;
; input fields for mixing ratios ;
; as opposed to estimating from q ;
;====================================;
flag_accurate_r = False
;====================================;
; define whether to use CG high-res ;
; input fields for theta_l ;
; as opposed to estimating ;
;====================================;
flag_accurate_thetal = False
;===================================;
; select subset data for testing ;
;===================================;
;; cascade domain:
region = "IO"
set_lat_min = -35.0
set_lat_max = 5.0
set_lon_min = 51.0
set_lon_max = 95.0
flag_subset = True
;===================================;
; define code version ;
;===================================;
version = "v2.0"
;===================================;
; define paths to input variables ;
;===================================;
generic_path = "/home/b/b381215/work/CG/"
;; timestep_path = (/ 20160811 , 20160811 , 20160811 , 20160811 , 20160811 , 20160811 , 20160811 , 20160811/) ; time in file name
;; timestep_idx = (/ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 /) ; time idx in file
timestep_path = (/ datein /)
timestep_idx = (/ hourin /)
time_ref = todouble(2016080100) ; all times ref to this time
filename = "nwp_R2B10_lkm1007_"
n_time_in = dimsizes(timestep_path)
;===================================;
; define paths to output files ;
;===================================;
path_out = "/home/b/b381215/work/CG/SCM_in/" ; Output directory
;; file_out_tmp = (/filename, \
;; CG_resol,"_",\
;; tostring(timestep_path(0)),".",tostring(t_step_in*timestep_idx(0)),"-",\
;; tostring(timestep_path(n_time_in-1)),".",tostring(t_step_in*timestep_idx(n_time_in-1)),\
;; ".nc"/) ; Output file name
file_out_tmp = (/"mumip_icon2.5_",region,"_",CG_resol,"_",\
tostring(timestep_path),".",tostring_with_format(t_step_in*timestep_idx, "%0.2i"),"_",version,".nc"/)
file_out = str_concat(file_out_tmp)
delete([/file_out_tmp/])
;================================================;
; Read in input co-ordinates (high-res) ;
;================================================;
filetype = "atm_3d"
variable = "pres"
coords_in = func_coords_in(generic_path,timestep_path,timestep_idx,time_ref,filename,filetype,variable,CG_resol,region)
lon_in = coords_in[0]
lat_in = coords_in[1]
lev_in_tmp = coords_in[2] ; level index
time_in = coords_in[3]
n_lon_in = dimsizes(lon_in)
n_lat_in = dimsizes(lat_in)
n_lev_in = dimsizes(lev_in_tmp)
;==============================
print("read in static height, orography, and lsm")
data_path_matr = (/generic_path,CG_resol,"/",region,"/dyamond_R2B10_lkm1007_",CG_resol,"_vgrid_",region,".nc"/)
data_path = str_concat(data_path_matr)
; Input variable dimensions: (time, latitude, longitude) ;
in_file = addfile(data_path,"r")
;; GRIB changes
;;lev_data_tmp = in_file->HHL(0,:,:,:)
;;height_data_tmp = lev_data_tmp
;;lsm_data_tmp = in_file->FR_LAND(0,:,:)
;;zorog_data_tmp = in_file->HSURF(0,:,:)
;;z0_data_tmp = in_file->Z0(0,:,:)
;;=======
lev_data_tmp = in_file->zg(0,:,:,:)
height_data_tmp = lev_data_tmp
lsm_data_tmp = in_file->lsm(0,:,:)
zorog_data_tmp = in_file->zg_2(0,:,:)
z0_data_tmp = in_file->sr(0,:,:)
; select a height profile characteristic of a sea point
; HMC! if we move to consider land points, will need to edit this
do j=0,n_lon_in-1
do i=0,n_lat_in-1
if (lsm_data_tmp(i,j).eq.0).and.(zorog_data_tmp(i,j).eq.0) then
; use me for height coordinate
levh_in_tmp = lev_data_tmp(:,i,j)
break_me = True
break
print(j)
end if
end do
if break_me then
break
end if
print(i)
end do
delete_VarAtts(levh_in_tmp,(/"lat","lon"/))
n_levh_in_tmp = dimsizes(levh_in_tmp)
; compute full levels from half levels: https://www.dwd.de/DWD/forschung/nwv/fepub/icon_database_main.pdf p115
levf_in_tmp = 0.5*(levh_in_tmp(0:n_levh_in_tmp-2)+levh_in_tmp(1:n_levh_in_tmp-1))
n_levf_in_tmp = dimsizes(levf_in_tmp)
height_data_tmpf = 0.5*(height_data_tmp(0:n_levh_in_tmp-2,:,:)+height_data_tmp(1:n_levh_in_tmp-1,:,:))
; just select required levels (height file generic - all 91 levels)
levh_in = levh_in_tmp(n_levh_in_tmp-(n_lev_in+1):n_levh_in_tmp-1)
levf_in = levf_in_tmp(n_levf_in_tmp-n_lev_in:n_levf_in_tmp-1)
height_data_in = height_data_tmpf(n_levf_in_tmp-n_lev_in:n_levf_in_tmp-1,:,:)
; follow dephy protocol for vertical coordinate
levh_in!0 = "lev"
levh_in&lev = levh_in
levh_in@units = "m"
levh_in@long_name = "altitude"
levf_in!0 = "lev"
levf_in&lev = levf_in
levf_in@units = "m"
levf_in@long_name = "altitude"
delete([/lev_data_tmp,levh_in_tmp,levf_in_tmp,n_levh_in_tmp,n_levf_in_tmp,lev_in_tmp,i,j,break_me/])
; height variable (same as lev, but spatially varying)
height_data_in!0 = "lev"
height_data_in&lev = levf_in
height_data_in@units = "m"
height_data_in@long_name = "altitude"
height_data_in!1 = "lat"
height_data_in&lat = lat_in
height_data_in!2 = "lon"
height_data_in&lon = lon_in
;================================================;
; Erase land if required ;
;================================================;
height_data_in@_FillValue = default_fillvalue("float")
lsm_data_tmp@_FillValue = default_fillvalue("float")
zorog_data_tmp@_FillValue = default_fillvalue("float")
z0_data_tmp@_FillValue = default_fillvalue("float")
if (flag_land) then
; any point where zorog ~= 0 exactly has some land
tol = 10^(-12)
land2d = zorog_data_tmp+lsm_data_tmp
land3d = conform(height_data_in,land2d,(/1,2/))
lsm_data_out = where(land2d.ge.tol,lsm_data_tmp@_FillValue ,lsm_data_tmp)
height_data_out1 = where(land3d.ge.tol,height_data_in@_FillValue,height_data_in)
zorog_data_out = where(land2d.ge.tol,zorog_data_tmp@_FillValue,zorog_data_tmp)
z0_data_out = where(land2d.ge.tol,z0_data_tmp@_FillValue ,z0_data_tmp)
copy_VarCoords(lsm_data_tmp,lsm_data_out)
copy_VarAtts( lsm_data_tmp,lsm_data_out)
copy_VarCoords(height_data_in,height_data_out1)
copy_VarAtts( height_data_in,height_data_out1)
copy_VarCoords(zorog_data_tmp,zorog_data_out)
copy_VarAtts( zorog_data_tmp,zorog_data_out)
copy_VarCoords(z0_data_tmp,z0_data_out)
copy_VarAtts( z0_data_tmp,z0_data_out)
;======================
; use poisson grid fill to smoothly fill in missing values
is_cyclic = False ; not cyclic data
guess_type = 1 ; start with zonal means
nscan = 200 ; no. iterations
eps = 1.e-6 ; tolerance
relc = 0.6 ; relaxation const
opt = 0 ; dummy
poisson_grid_fill(lsm_data_out ,is_cyclic,guess_type,nscan,eps,relc,opt)
poisson_grid_fill(height_data_out1,is_cyclic,guess_type,nscan,eps,relc,opt)
poisson_grid_fill(zorog_data_out ,is_cyclic,guess_type,nscan,eps,relc,opt)
poisson_grid_fill(z0_data_out ,is_cyclic,guess_type,nscan,eps,relc,opt)
;= create new variable to indicate where we have interpolated
land_mask = where(land2d.ge.tol,lsm_data_tmp@_FillValue ,lsm_data_tmp)
copy_VarCoords(lsm_data_tmp,land_mask)
copy_VarAtts( lsm_data_tmp,land_mask)
land_mask@long_name = "Land erased if Fill Value"
else
lsm_data_out = lsm_data_tmp
height_data_out1 = height_data_in
zorog_data_out = zorog_data_tmp
z0_data_out = z0_data_tmp
land_mask = lsm_data_out
land_mask@long_name = "Land erased if Fill Value"
end if
delete([/lsm_data_tmp,height_data_in,zorog_data_tmp,z0_data_tmp,land3d/])
;================================================;
; Define output co-ordinates (low-res) ;
;================================================;
; we will perform no horizontal regridding, but we do subset the data
; we will perform NO VERTICAL INTERPOLATION but leave on native grid.
flag_interp_lev = False
lat_out = lat_in({set_lat_min:set_lat_max})
lon_out = lon_in({set_lon_min:set_lon_max})
n_lon_out = dimsizes(lon_out)
n_lat_out = dimsizes(lat_out)
; lev out = full levels input variables
if (.not.flag_interp_lev) then
lev_out = levf_in
n_lev_out = n_lev_in
height_data_out = height_data_out1
else
print("Error! assuming no vertical interpolation required")
end if
if (.not.flag_interp_time) then
time_out = time_in
n_time_out = n_time_in
else
print("Error! assuming no temporal interpolation required")
end if
;;===============
;; dummy arrays for output.
array_4d_out = new((/n_time_out,n_lev_out,n_lat_out,n_lon_out/),float)
array_4d_out!0 = "time"
array_4d_out!1 = "lev"
array_4d_out!2 = "lat"
array_4d_out!3 = "lon"
array_4d_out&time = time_out
array_4d_out&lev = lev_out
array_4d_out&lat = lat_out
array_4d_out&lon = lon_out
array_4dh_out = new((/n_time_out,n_lev_out+1,n_lat_out,n_lon_out/),float)
array_4dh_out!0 = "time"
array_4dh_out!1 = "lev"
array_4dh_out!2 = "lat"
array_4dh_out!3 = "lon"
array_4dh_out&time = time_out
array_4dh_out&lev = levh_in
array_4dh_out&lat = lat_out
array_4dh_out&lon = lon_out
array_3d_out = new((/n_time_out,n_lat_out,n_lon_out/),float)
array_3d_out!0 = "time"
array_3d_out!1 = "lat"
array_3d_out!2 = "lon"
array_3d_out&time = time_out
array_3d_out&lat = lat_out
array_3d_out&lon = lon_out
;=================================================================;
; SET UP SAVING DATA TO NCL FILE ;
print("=========================================")
print(" ** OPEN NCL FILE AND DEFINE CO-ORDS ** ")
print("=========================================")
;===================================================================
; Define dimensions of variables
;
system("/bin/rm -f " + path_out + file_out) ; remove if exists
fout = addfile (path_out + file_out, "c") ; open output file - create
; fout = addfile (path_out + file_out, "w") ; open output file - read and write
;===================================================================
; explicitly declare file definition mode. Improve efficiency.
setfileoption(fout,"DefineMode",True)
; ;===================================================================
; ; create global attributes of the file
;
; fAtt = True ; assign file attributes
; fAtt@title = "SCM input file derived from DYAMOND summer ICON 2.5 km simulation"
; fAtt@Conventions = "None"
; fAtt@creation_date = systemfunc ("date")
; fileattdef( fout, fAtt ) ; copy file attributes
;
;===================================================================
; predefine the coordinate variables and their dimensionality
; Note: to get an UNLIMITED record dimension, we set the dimensionality
; to -1 (or the actual size) and set the dimension name to True.
dimNames = (/"time", "lat", "lon", "lev"/)
dimSizes = (/ -1 , n_lat_out, n_lon_out, n_lev_out/)
dimUnlim = (/ True , False, False, False/)
filedimdef(fout,dimNames,dimSizes,dimUnlim)
;===================================================================
; start writing to file
add_to_file(fout,time_out ,"time")
add_to_file(fout,lev_out ,"lev")
add_to_file(fout,lat_out ,"lat")
add_to_file(fout,lon_out ,"lon")
add_to_file(fout,height_data_out ,"height_t")
add_to_file(fout,lsm_data_out ,"lsm")
add_to_file(fout,zorog_data_out ,"zorog")
add_to_file(fout,z0_data_out ,"z0")
add_to_file(fout,land_mask ,"land_erase_flag")
; add_to_file(fout,date , "date")
; add_to_file(fout,second , "second")
;=============================================================
delete([/file_out,path_out/])
delete([/dimNames,dimSizes,dimUnlim/])
;==================================================================================
; param 1. pressure
print("pressure calculation")
filetype = "atm_3d"
variable_file = "pres" ; name of file
; variable_name = "P" ; name in file
variable_name = "pfull"
; initialise: time, lev, lat, lon
pres_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
pres_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
pres_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(pres_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/pres_tmp/])
end do
; vertical coordinate. already on full levels
; Erase land if required ===========================;
pres_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(pres_data_out,land2d,(/2,3/))
pres_data_out_lf = where(land4d.ge.tol,pres_data_out@_FillValue,pres_data_out)
copy_VarCoords(pres_data_out,pres_data_out_lf)
copy_VarAtts( pres_data_out,pres_data_out_lf)
poisson_grid_fill(pres_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/pres_data_out/])
pres_data_out = pres_data_out_lf
delete([/pres_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
pres_data_out_sm = smth9_Wrap(pres_data_out,0.5,0.25,False)
delete([/pres_data_out/])
pres_data_out = pres_data_out_sm
delete([/pres_data_out_sm/])
end if
delete_VarAtts(pres_data_out,(/"param","time","height"/))
add_to_file(fout,pres_data_out,"pressure_t")
delete([/variable_file,variable_name,pres_data_out/])
;==================================================================================
; param 2. temperature
print("temperature calculation")
filetype = "atm_3d"
variable_file = "t" ; name of file
; variable_name = "T" ; name in file
variable_name = "ta" ; name in file
; initialise: time, lev, lat, lon
t_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
t_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
t_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(t_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/t_tmp/])
end do
; vertical coordinate. already on full levels
; Erase land if required ===========================;
t_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(t_data_out,land2d,(/2,3/))
t_data_out_lf = where(land4d.ge.tol,t_data_out@_FillValue,t_data_out)
copy_VarCoords(t_data_out,t_data_out_lf)
copy_VarAtts( t_data_out,t_data_out_lf)
poisson_grid_fill(t_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/t_data_out/])
t_data_out = t_data_out_lf
delete([/t_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
t_data_out_sm = smth9_Wrap(t_data_out,0.5,0.25,False)
delete([/t_data_out/])
t_data_out = t_data_out_sm
delete([/t_data_out_sm/])
end if
delete_VarAtts(t_data_out,(/"param","standard_name","time","height"/))
add_to_file(fout,t_data_out,"temp_t")
delete([/variable_file,variable_name,t_data_out/])
;==================================================================================
; param 3. theta
print("theta calculation")
filetype = "atm_3d"
variable_file = "theta" ; name of file
variable_name = "theta" ; name in file
; initialise: time, lev, lat, lon
theta_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
theta_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
theta_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(theta_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/theta_tmp/])
end do
; vertical coordinate. already on full levels
; Erase land if required ===========================;
theta_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(theta_data_out,land2d,(/2,3/))
theta_data_out_lf = where(land4d.ge.tol,theta_data_out@_FillValue,theta_data_out)
copy_VarCoords(theta_data_out,theta_data_out_lf)
copy_VarAtts( theta_data_out,theta_data_out_lf)
poisson_grid_fill(theta_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/theta_data_out/])
theta_data_out = theta_data_out_lf
delete([/theta_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
theta_data_out_sm = smth9_Wrap(theta_data_out,0.5,0.25,False)
delete([/theta_data_out/])
theta_data_out = theta_data_out_sm
delete([/theta_data_out_sm/])
end if
delete_VarAtts(theta_data_out,(/"time","height"/))
theta_data_out@long_name="Potential temperature"
theta_data_out@units="K"
add_to_file(fout,theta_data_out,"theta_t")
delete([/variable_file,variable_name,theta_data_out/])
;==================================================================================
; param 3b. thetal
if (flag_accurate_thetal) then
print("thetal calculation")
filetype = "atm_3d"
variable_file = "thetal" ; name of file
variable_name = "thetal" ; name in file
; initialise: time, lev, lat, lon
thetal_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
thetal_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
thetal_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(thetal_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/thetal_tmp/])
end do
; vertical coordinate. already on full levels
; Erase land if required ===========================;
thetal_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(thetal_data_out,land2d,(/2,3/))
thetal_data_out_lf = where(land4d.ge.tol,thetal_data_out@_FillValue,thetal_data_out)
copy_VarCoords(thetal_data_out,thetal_data_out_lf)
copy_VarAtts( thetal_data_out,thetal_data_out_lf)
poisson_grid_fill(thetal_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/thetal_data_out/])
thetal_data_out = thetal_data_out_lf
delete([/thetal_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
thetal_data_out_sm = smth9_Wrap(thetal_data_out,0.5,0.25,False)
delete([/thetal_data_out/])
thetal_data_out = thetal_data_out_sm
delete([/thetal_data_out_sm/])
end if
delete_VarAtts(thetal_data_out,(/"time","height"/))
thetal_data_out@long_name="Potential temperature"
thetal_data_out@units="K"
add_to_file(fout,thetal_data_out,"thetal_t")
delete([/variable_file,variable_name,thetal_data_out/])
end if ; flag_accurate_thetal
;==================================================================================
; param 4. vertical velocity
print("vertical velocity calculation")
filetype = "atm_3d"
variable_file = "w" ; name of file
;variable_name = "W" ; name in file
variable_name = "wa" ; name in file
; initialise: time, lev, lat, lon
wh_data_out = array_4dh_out
; loop over vertical half levels
do lev_count = 0,n_lev_in
w_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
wh_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(w_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/w_tmp/])
end do
; vertical coordinate. on half levels. Interpolate to full levelsS
w_data_out=0.5*(wh_data_out(:,0:n_lev_in-1,:,:)+wh_data_out(:,1:n_lev_in,:,:))
copy_VarCoords(array_4d_out,w_data_out)
w_data_out@long_name="Vertical Velocity (w)"
w_data_out@units="m s-1"
; Erase land if required ===========================;
w_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(w_data_out,land2d,(/2,3/))
w_data_out_lf = where(land4d.ge.tol,w_data_out@_FillValue,w_data_out)
copy_VarCoords(w_data_out,w_data_out_lf)
copy_VarAtts( w_data_out,w_data_out_lf)
poisson_grid_fill(w_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/w_data_out/])
w_data_out = w_data_out_lf
delete([/w_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
w_data_out_sm = smth9_Wrap(w_data_out,0.5,0.25,False)
delete([/w_data_out/])
w_data_out = w_data_out_sm
delete([/w_data_out_sm/])
end if
add_to_file(fout,w_data_out,"w")
delete([/variable_file,variable_name,wh_data_out,w_data_out/])
;==================================================================================
; param 4b. vertical pressure velocity
print("HMC to do! omega = vertical pressure velocity calculation")
; now this parameter we need to compute from the ICON data.
; but the problem is we need to compute it from high resolution data
; before coarse graining for a more accurate estimate
; need high resolution w, pressure and t data
; the NCL function w_to_omega makes the hydrostatic approximation which is likely not good enough in our case
; so if this is needed, we'll have to compute it from definition of omega:
; omega = Dp/Dt = partial dp/dt + u.grad p (last term in 3D)
;==================================================================================
; param 5. horizontal winds
print("U wind calculation")
filetype = "atm_3d"
variable_file = "u" ; name of file
;variable_name = "U" ; name in file
variable_name = "ua" ; name in file
; initialise: time, lev, lat, lon
u_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
u_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
u_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(u_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/u_tmp/])
end do
; vertical coordinate. already on full levels
; Erase land if required ===========================;
u_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(u_data_out,land2d,(/2,3/))
u_data_out_lf = where(land4d.ge.tol,u_data_out@_FillValue,u_data_out)
copy_VarCoords(u_data_out,u_data_out_lf)
copy_VarAtts( u_data_out,u_data_out_lf)
poisson_grid_fill(u_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/u_data_out/])
u_data_out = u_data_out_lf
delete([/u_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
u_data_out_sm = smth9_Wrap(u_data_out,0.5,0.25,False)
delete([/u_data_out/])
u_data_out = u_data_out_sm
delete([/u_data_out_sm/])
end if
u_data_out@long_name = "Zonal wind"
delete_VarAtts(u_data_out,(/"param","standard_name","time","height"/))
add_to_file(fout,u_data_out,"u_t")
delete([/variable_file,variable_name,u_data_out/])
;=====
print("V wind calculation")
filetype = "atm_3d"
variable_file = "v" ; name of file
; variable_name = "V" ; name in file
variable_name = "va" ; name in file
; initialise: time, lev, lat, lon
v_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
v_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
v_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(v_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/v_tmp/])
end do
; vertical coordinate. already on full levels
; Erase land if required ===========================;
v_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(v_data_out,land2d,(/2,3/))
v_data_out_lf = where(land4d.ge.tol,v_data_out@_FillValue,v_data_out)
copy_VarCoords(v_data_out,v_data_out_lf)
copy_VarAtts( v_data_out,v_data_out_lf)
poisson_grid_fill(v_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/v_data_out/])
v_data_out = v_data_out_lf
delete([/v_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
v_data_out_sm = smth9_Wrap(v_data_out,0.5,0.25,False)
delete([/v_data_out/])
v_data_out = v_data_out_sm
delete([/v_data_out_sm/])
end if
v_data_out@long_name="Meridional wind"
delete_VarAtts(v_data_out,(/"param","standard_name","time","height"/))
add_to_file(fout,v_data_out,"v_t")
delete([/variable_file,variable_name,v_data_out/])
;==================================================================================
; param 6. moisture
print("q/qi/ql calculation")
; q: specific humidity = mass of water vapour per unit mass of moist air (dry+vapour+liquid+ice)
; r: water vapour mixing ratio = mass of water vapour per unit mass of dry air
; we can read in specific humidity (QV), specific cloud water content (QC_DIA) and specific cloud ice content (QI_DIA)
; must calculate qt, rv, rl, ri and rt
;==========================
print("qv calculation")
filetype = "atm_3d"
variable_file = "qv" ; name of file
;variable_name = "QV" ; name in file
variable_name = "hus" ; name in file
; initialise: time, lev, lat, lon
qv_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
qv_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
qv_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(qv_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/qv_tmp/])
end do
; Erase land if required ===========================;
qv_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(qv_data_out,land2d,(/2,3/))
qv_data_out_lf = where(land4d.ge.tol,qv_data_out@_FillValue,qv_data_out)
copy_VarCoords(qv_data_out,qv_data_out_lf)
copy_VarAtts( qv_data_out,qv_data_out_lf)
poisson_grid_fill(qv_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/qv_data_out/])
qv_data_out = qv_data_out_lf
delete([/qv_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
qv_data_out_sm = smth9_Wrap(qv_data_out,0.5,0.25,False)
delete([/qv_data_out/])
qv_data_out = qv_data_out_sm
delete([/qv_data_out_sm/])
end if
delete_VarAtts(qv_data_out,(/"param","standard_name","time","height"/))
delete([/variable_file,variable_name/])
;==========================
print("ql calculation")
filetype = "atm_3d"
variable_file = "tot_qc_dia" ; name of file
; variable_name = "QC_DIA" ; name in file
variable_name = "param212.1.0" ; name in file
; initialise: time, lev, lat, lon
ql_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
ql_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
ql_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(ql_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/ql_tmp/])
end do
; Erase land if required ===========================;
ql_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(ql_data_out,land2d,(/2,3/))
ql_data_out_lf = where(land4d.ge.tol,ql_data_out@_FillValue,ql_data_out)
copy_VarCoords(ql_data_out,ql_data_out_lf)
copy_VarAtts( ql_data_out,ql_data_out_lf)
poisson_grid_fill(ql_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/ql_data_out/])
ql_data_out = ql_data_out_lf
delete([/ql_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
ql_data_out_sm = smth9_Wrap(ql_data_out,0.5,0.25,False)
delete([/ql_data_out/])
ql_data_out = ql_data_out_sm
delete([/ql_data_out_sm/])
end if
ql_data_out@long_name="Liquid water content"
delete_VarAtts(ql_data_out,(/"param","time","height"/))
delete([/variable_file,variable_name/])
;==========================
print("qi calculation")
filetype = "atm_3d"
variable_file = "tot_qi_dia" ; name of file
;variable_name = "QI_DIA" ; name in file
variable_name = "param213.1.0" ; name in file
; initialise: time, lev, lat, lon
qi_data_out = array_4d_out
; loop over vertical levels
do lev_count = 0,n_lev_in-1
qi_tmp = func_read_hires(generic_path,timestep_path,timestep_idx,time_in,filename,filetype,variable_file,variable_name,CG_resol,region,lev_count)
qi_data_out(time|:,lev|lev_count,lat|:,lon|:) = func_reduce_area(qi_tmp,flag_subset,set_lat_min,set_lat_max,set_lon_min,set_lon_max,3)
delete([/qi_tmp/])
end do
; Erase land if required ===========================;
qi_data_out@_FillValue = default_fillvalue("float")
if (flag_land) then
land4d = conform(qi_data_out,land2d,(/2,3/))
qi_data_out_lf = where(land4d.ge.tol,qi_data_out@_FillValue,qi_data_out)
copy_VarCoords(qi_data_out,qi_data_out_lf)
copy_VarAtts( qi_data_out,qi_data_out_lf)
poisson_grid_fill(qi_data_out_lf,is_cyclic,guess_type,nscan,eps,relc,opt)
delete([/qi_data_out/])
qi_data_out = qi_data_out_lf
delete([/qi_data_out_lf,land4d/])
end if
; smooth data to remove unresolved features
if flag_smooth then
qi_data_out_sm = smth9_Wrap(qi_data_out,0.5,0.25,False)
delete([/qi_data_out/])
qi_data_out = qi_data_out_sm
delete([/qi_data_out_sm/])
end if
qi_data_out@long_name="Ice water content"
delete_VarAtts(qi_data_out,(/"param","time","height"/))
delete([/variable_file,variable_name/])
;==========================
print("qt calculation")
qt_data_out = qv_data_out+ql_data_out+qi_data_out
copy_VarCoords(qv_data_out,qt_data_out)
qt_data_out@long_name="Total water content"
qt_data_out@units="kg kg-1"
; vertical coordinate. already on full levels
;==========================
if (flag_accurate_r) then
print("rv calculation")
filetype = "atm_3d"