1
+ # %%
2
+ import sys
3
+
4
+ # set system path to find local modules
5
+ sys .path .append (".." )
6
+
7
+ import numpy as np
8
+ import pandas as pd
9
+ import matplotlib .pyplot as plt
10
+
11
+ from src import io , var , utils , fourier , physics
12
+ from wrappers import interface
13
+ from vis import plotter , cart_plot
14
+
15
+ from IPython import get_ipython
16
+
17
+ ipython = get_ipython ()
18
+
19
+ if ipython is not None :
20
+ ipython .run_line_magic ("load_ext" , "autoreload" )
21
+ else :
22
+ print (ipython )
23
+
24
+ def autoreload ():
25
+ if ipython is not None :
26
+ ipython .run_line_magic ("autoreload" , "2" )
27
+
28
+ from sys import exit
29
+
30
+ if __name__ != "__main__" :
31
+ exit (0 )
32
+ # %%
33
+ autoreload ()
34
+ from inputs .icon_regional_run import params
35
+
36
+ if params .self_test ():
37
+ params .print ()
38
+
39
+ print (params .path_compact_topo )
40
+
41
+ grid = var .grid ()
42
+
43
+ # read grid
44
+ reader = io .ncdata (padding = params .padding , padding_tol = (60 - params .padding ))
45
+
46
+ # writer object
47
+ writer = io .nc_writer (params )
48
+
49
+ reader .read_dat (params .path_compact_grid , grid )
50
+
51
+ clat_rad = np .copy (grid .clat )
52
+ clon_rad = np .copy (grid .clon )
53
+
54
+ grid .apply_f (utils .rad2deg )
55
+
56
+ n_cells = grid .clat .size
57
+
58
+ for c_idx in range (n_cells )[:1 ]:
59
+ c_idx = 90
60
+ print (c_idx )
61
+
62
+ topo = var .topo_cell ()
63
+ lat_verts = grid .clat_vertices [c_idx ]
64
+ lon_verts = grid .clon_vertices [c_idx ]
65
+
66
+ lat_extent = [lat_verts .min () - 1.0 ,lat_verts .min () - 1.0 ,lat_verts .max () + 1.0 ]
67
+ lon_extent = [lon_verts .min () - 1.0 ,lon_verts .min () - 1.0 ,lon_verts .max () + 1.0 ]
68
+ # we only keep the topography that is inside this lat-lon extent.
69
+ lat_verts = np .array (lat_extent )
70
+ lon_verts = np .array (lon_extent )
71
+
72
+ params .lat_extent = lat_extent
73
+ params .lon_extent = lon_extent
74
+
75
+ # read topography
76
+ if not params .enable_merit :
77
+ reader .read_dat (params .fn_topo , topo )
78
+ reader .read_topo (topo , topo , lon_verts , lat_verts )
79
+ else :
80
+ reader .read_merit_topo (topo , params )
81
+ topo .topo [np .where (topo .topo < - 500.0 )] = - 500.0
82
+
83
+ topo .gen_mgrids ()
84
+ # %%
85
+ clon = np .array ([grid .clon [c_idx ]])
86
+ clat = np .array ([grid .clat [c_idx ]])
87
+ clon_vertices = np .array ([grid .clon_vertices [c_idx ]])
88
+ clat_vertices = np .array ([grid .clat_vertices [c_idx ]])
89
+
90
+ ncells = 1
91
+ nv = clon_vertices [0 ].size
92
+ # -- create the triangles
93
+ clon_vertices = np .where (clon_vertices < - 180.0 , clon_vertices + 360.0 , clon_vertices )
94
+ clon_vertices = np .where (clon_vertices > 180.0 , clon_vertices - 360.0 , clon_vertices )
95
+
96
+ triangles = np .zeros ((ncells , nv , 2 ), np .float32 )
97
+
98
+ for i in range (0 , ncells , 1 ):
99
+ triangles [i , :, 0 ] = np .array (clon_vertices [i , :])
100
+ triangles [i , :, 1 ] = np .array (clat_vertices [i , :])
101
+
102
+ print ("--> triangles done" )
103
+
104
+ cart_plot .lat_lon_icon (topo , triangles , ncells = ncells , clon = clon , clat = clat )
105
+
106
+
107
+ # %%
108
+ tri_idx = 0
109
+ # initialise cell object
110
+ cell = var .topo_cell ()
111
+
112
+ simplex_lon = triangles [tri_idx , :, 0 ]
113
+ simplex_lat = triangles [tri_idx , :, 1 ]
114
+
115
+ utils .get_lat_lon_segments (
116
+ simplex_lat , simplex_lon , cell , topo , rect = params .rect
117
+ )
118
+
119
+ topo_orig = np .copy (cell .topo )
120
+
121
+ if params .dfft_first_guess :
122
+ nhi = len (cell .lon )
123
+ nhj = len (cell .lat )
124
+
125
+ first_guess = interface .get_pmf (nhi , nhj , params .U , params .V )
126
+ fobj_tri = fourier .f_trans (nhi , nhj )
127
+
128
+ #######################################################
129
+ # do fourier...
130
+
131
+ if not params .dfft_first_guess :
132
+ freqs , uw_pmf_freqs , dat_2D_fg0 = first_guess .sappx (cell , lmbda = 0.0 )
133
+
134
+ #######################################################
135
+ # do fourier using DFFT
136
+
137
+ if params .dfft_first_guess :
138
+ ampls , uw_pmf_freqs , dat_2D_fg0 , kls = first_guess .dfft (cell )
139
+ freqs = np .copy (ampls )
140
+
141
+ print ("uw_pmf_freqs_sum:" , uw_pmf_freqs .sum ())
142
+
143
+ fq_cpy = np .copy (freqs )
144
+
145
+ indices = []
146
+ max_ampls = []
147
+
148
+ for ii in range (params .n_modes ):
149
+ max_idx = np .unravel_index (fq_cpy .argmax (), fq_cpy .shape )
150
+ indices .append (max_idx )
151
+ max_ampls .append (fq_cpy [max_idx ])
152
+ max_val = fq_cpy [max_idx ]
153
+ fq_cpy [max_idx ] = 0.0
154
+
155
+ utils .get_lat_lon_segments (
156
+ simplex_lat , simplex_lon , cell , topo , rect = False
157
+ )
158
+
159
+ k_idxs = [pair [1 ] for pair in indices ]
160
+ l_idxs = [pair [0 ] for pair in indices ]
161
+
162
+ second_guess = interface .get_pmf (nhi , nhj , params .U , params .V )
163
+
164
+ if params .dfft_first_guess :
165
+ second_guess .fobj .set_kls (
166
+ k_idxs , l_idxs , recompute_nhij = True , components = "real"
167
+ )
168
+ else :
169
+ second_guess .fobj .set_kls (k_idxs , l_idxs , recompute_nhij = False )
170
+
171
+ freqs , uw , dat_2D_sg0 = second_guess .sappx (cell , lmbda = 1e-1 , updt_analysis = True )
172
+
173
+ cell .topo = topo_orig
174
+
175
+ writer .output (tri_idx , clat_rad [tri_idx ], clon_rad [tri_idx ], cell .analysis )
176
+
177
+ cell .uw = uw
178
+
179
+ if params .plot :
180
+ fs = (15 , 9.0 )
181
+ v_extent = [dat_2D_sg0 .min (), dat_2D_sg0 .max ()]
182
+
183
+ fig , axs = plt .subplots (2 , 2 , figsize = fs )
184
+
185
+ fig_obj = plotter .fig_obj (
186
+ fig , second_guess .fobj .nhar_i , second_guess .fobj .nhar_j
187
+ )
188
+ axs [0 , 0 ] = fig_obj .phys_panel (
189
+ axs [0 , 0 ],
190
+ dat_2D_sg0 ,
191
+ title = "T%i: Reconstruction" % tri_idx ,
192
+ xlabel = "longitude [km]" ,
193
+ ylabel = "latitude [km]" ,
194
+ extent = [cell .lon .min (), cell .lon .max (), cell .lat .min (), cell .lat .max ()],
195
+ v_extent = v_extent ,
196
+ )
197
+
198
+ axs [0 , 1 ] = fig_obj .phys_panel (
199
+ axs [0 , 1 ],
200
+ cell .topo * cell .mask ,
201
+ title = "T%i: Reconstruction" % tri_idx ,
202
+ xlabel = "longitude [km]" ,
203
+ ylabel = "latitude [km]" ,
204
+ extent = [cell .lon .min (), cell .lon .max (), cell .lat .min (), cell .lat .max ()],
205
+ v_extent = v_extent ,
206
+ )
207
+
208
+ if params .dfft_first_guess :
209
+ axs [1 , 0 ] = fig_obj .fft_freq_panel (
210
+ axs [1 , 0 ], freqs , kls [0 ], kls [1 ], typ = "real"
211
+ )
212
+ axs [1 , 1 ] = fig_obj .fft_freq_panel (
213
+ axs [1 , 1 ], uw , kls [0 ], kls [1 ], title = "PMF spectrum" , typ = "real"
214
+ )
215
+ else :
216
+ axs [1 , 0 ] = fig_obj .freq_panel (axs [1 , 0 ], freqs )
217
+ axs [1 , 1 ] = fig_obj .freq_panel (axs [1 , 1 ], uw , title = "PMF spectrum" )
218
+
219
+ plt .tight_layout ()
220
+ plt .savefig ("%sT%i.pdf" % (params .path_output , tri_idx ))
221
+ plt .show ()
222
+ # %%
0 commit comments