|
19 | 19 | ## tell Python where to find it. For example, if your ecco_v4_py |
20 | 20 | ## files are in /Users/ifenty/ECCOv4-py/ecco_v4_py, then use: |
21 | 21 | import sys |
22 | | -sys.path.append('/Users/ifenty/ECCOv4-py') |
| 22 | + |
| 23 | +sys.path.append('/Users/ifenty/git_repos/my_forks/ECCOv4-py') |
23 | 24 | import ecco_v4_py as ecco |
24 | 25 |
|
25 | 26 | import matplotlib.pyplot as plt |
|
32 | 33 | # *read_llc_to_tiles* reads a llc compact format binary file and converts to a numpy ndarray of dimension: |
33 | 34 | # [N_recs, N_z, N_tiles, llc, llc] |
34 | 35 | # |
35 | | -# For ECCOv4 our convenction is: |
| 36 | +# For ECCOv4 our convention is: |
36 | 37 | # ``` |
37 | | -# 'N_recs' = number of time cords |
| 38 | +# 'N_recs' = number of time levels |
38 | 39 | # 'N_z' = number of depth levels |
39 | 40 | # 'N_tiles' = 13 |
40 | 41 | # 'llc' = 90 |
41 | 42 | # ``` |
42 | 43 | # |
43 | | -# By default the routine will try to load a single 2D slice of a llc90 compact binary file: (N_rec = 1, N_z =1, N_tiles = 13, and llc=90). |
| 44 | +# By default the routine will try to load a single 2D slice of a llc90 compact binary file: (N_recs = 1, N_z =1, N_tiles = 13, and llc=90). |
44 | 45 | # |
45 | 46 | # There are several other options which you can learn about using the 'help' command: |
46 | 47 |
|
|
64 | 65 |
|
65 | 66 | # ## Example 1: Load a 2D llc 'compact' binary file |
66 | 67 | # |
67 | | -# First load the bathymetry map |
| 68 | +# The file 'bathy_eccollc_90x50_min2pts.bin' contains the 2D array of bathymetry for the model. |
68 | 69 |
|
69 | 70 | # In[3]: |
70 | 71 |
|
|
73 | 74 | input_file = 'bathy_eccollc_90x50_min2pts.bin' |
74 | 75 |
|
75 | 76 |
|
| 77 | +# *read_llc_to_tiles* actually runs several other subroutines: *load_binary_array* which does the lower level reading of the binary file, *llc_compact_to_faces* which converts the array to 5 'faces', and finally *llc_faces_to_tiles* which extracts the 13 tiles from the 5 faces: |
| 78 | + |
76 | 79 | # In[4]: |
77 | 80 |
|
78 | 81 |
|
79 | 82 | bathy = ecco.read_llc_to_tiles(input_dir, input_file) |
80 | 83 |
|
81 | 84 |
|
82 | | -# *bathy* is a float64 numpy array of dimension [13, 90, 90] |
| 85 | +# *bathy* is a numpy float32 array with dimension [13, 90, 90] |
| 86 | + |
| 87 | +# ### Plot the 13 tiles bathymetry data |
83 | 88 |
|
84 | 89 | # In[5]: |
85 | 90 |
|
86 | 91 |
|
87 | | -print(bathy.shape) |
88 | | -print(type(bathy)) |
89 | | -print(type(bathy[0,0,0])) |
| 92 | +# Use plot_tiles to make a quick plot of the 13 tiles. See the tutorial on plotting for more examples. |
| 93 | +ecco.plot_tiles(bathy, layout='latlon',rotate_to_latlon=True,show_tile_labels=False, show_colorbar=True); |
90 | 94 |
|
91 | 95 |
|
92 | | -# ### Plot the 13 tiles |
| 96 | +# ## Load ecco-grid information to make a fancier lat-lon plot |
| 97 | +# |
| 98 | +# With the longitudes (XC) and latitudes (YC) and the 13 tile ndarray we can plot the field in different geographic projections. See the tutorial on plotting for more examples. |
93 | 99 |
|
94 | 100 | # In[6]: |
95 | 101 |
|
96 | 102 |
|
97 | | -# Use plot_tiles to make a quick plot of the 13 tiles. See the tutorial on plotting for more examples. |
98 | | -ecco.plot_tiles(bathy, layout='latlon',rotate_to_latlon=True,show_tile_labels=False); |
99 | | - |
| 103 | +ecco_grid_dir = '/Users/ifenty/tmp/nctiles_grid/' |
| 104 | +ecco_grid = ecco.load_ecco_grid_nc(input_dir, 'ECCO-GRID.nc') |
100 | 105 |
|
101 | | -# ## Load ecco-grid information to make a fancier lat-lon plot |
102 | 106 |
|
103 | 107 | # In[7]: |
104 | 108 |
|
105 | 109 |
|
106 | | -ecco_grid_dir = '/Users/ifenty/tmp/nctiles_grid/' |
107 | | -ecco_grid = ecco.load_ecco_grid_nc(input_dir, 'ECCO-GRID.nc') |
| 110 | +plt.figure(figsize=(15,6)); |
| 111 | +ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, bathy, show_colorbar=True, user_lon_0=-66); |
108 | 112 |
|
109 | 113 |
|
110 | 114 | # In[8]: |
111 | 115 |
|
112 | 116 |
|
113 | | -plt.figure(figsize=(15,6)); |
114 | | -ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, bathy, show_colorbar=True); |
| 117 | +plt.figure(figsize=(12,8)); |
| 118 | +ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, bathy, show_colorbar=True, projection_type='stereo', lat_lim =-45, less_output=True,dx=.25,dy=.25); |
115 | 119 |
|
116 | 120 |
|
117 | 121 | # ## Convert the ndarray into a DataArray |
118 | 122 | # |
119 | | -# Converting the ndarray to a DataArray can be useful for broadcasting operations. |
| 123 | +# Converting the ndarray to a DataArray can be useful for broadcasting operations. One approach is to create the DataArray manually by specifying the names and values of the new dimension coordinates: |
120 | 124 |
|
121 | 125 | # In[9]: |
122 | 126 |
|
123 | 127 |
|
124 | | -tile = range(1,14) |
| 128 | +tile = range(13) |
125 | 129 | i = range(90) |
126 | 130 | j = range(90) |
127 | 131 |
|
|
132 | 136 | # Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields |
133 | 137 | bathy_DA = xr.DataArray(bathy,coords={'tile': tile, |
134 | 138 | 'j': j, |
135 | | - 'i': i},dims=['tile','j','i']) |
| 139 | + 'i': i}, dims=['tile','j','i']) |
136 | 140 |
|
137 | 141 |
|
138 | 142 | # In[11]: |
|
141 | 145 | print(bathy_DA.dims) |
142 | 146 |
|
143 | 147 |
|
| 148 | +# Another approach is to use the routine *llc_tiles_to_xda*. *llc_tiles_to_xda* uses |
| 149 | + |
| 150 | +# In[12]: |
| 151 | + |
| 152 | + |
| 153 | +bathy.shape |
| 154 | + |
| 155 | + |
| 156 | +# In[13]: |
| 157 | + |
| 158 | + |
| 159 | +bathy_DA2 = ecco.llc_tiles_to_xda(bathy, var_type='c',grid_da=ecco_grid.XC) |
| 160 | +print(bathy_DA2.dims) |
| 161 | +print(bathy_DA2.coords) |
| 162 | + |
| 163 | + |
144 | 164 | # ## Example 2: Load a 3D 'compact' llc binary file with 3rd dimension = Time |
145 | 165 | # |
146 | 166 | # |
147 | | -# Runoff is a 12 month climatology, dimensions of [time, j, i] |
| 167 | +# The file 'runoff-2d-Fekete-1deg-mon-V4-SMOOTH.bin' contains the 12 month climatology of river runoff, dimensions of [time, j, i]. |
148 | 168 |
|
149 | | -# In[12]: |
| 169 | +# In[14]: |
150 | 170 |
|
151 | 171 |
|
152 | 172 | input_file = 'runoff-2d-Fekete-1deg-mon-V4-SMOOTH.bin' |
153 | 173 |
|
154 | 174 |
|
155 | 175 | # specify the length of the n_recs dimension, nl, as 12. By default, nk = 1 |
156 | 176 |
|
157 | | -# In[13]: |
| 177 | +# In[15]: |
158 | 178 |
|
159 | 179 |
|
160 | 180 | runoff = ecco.read_llc_to_tiles(input_dir, input_file, nl=12) |
161 | 181 |
|
162 | 182 |
|
163 | | -# In[14]: |
| 183 | +# Shape is n_recs (12), n_z (1), n_tiles (13), llc (90), llc (90) |
| 184 | + |
| 185 | +# In[16]: |
164 | 186 |
|
165 | 187 |
|
166 | 188 | print(runoff.shape) |
167 | 189 |
|
168 | 190 |
|
169 | 191 | # ### Plot the November runoff climatology |
170 | 192 |
|
171 | | -# In[15]: |
| 193 | +# In[17]: |
172 | 194 |
|
173 | 195 |
|
174 | 196 | plt.figure(figsize=(15,6)); |
175 | | -ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, runoff[10,:], cmin=0,cmax=1e-7, show_colorbar=True, cmap='bwr'); |
| 197 | +ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, runoff[10,:], cmin=0,cmax=1e-7, show_colorbar=True, cmap='bwr',user_lon_0=-66); |
176 | 198 |
|
177 | 199 |
|
178 | 200 | # ## Convert the ndarray into a DataArray |
179 | 201 | # |
180 | | -# Converting the ndarray to a DataArray can be useful for broadcasting operations. |
| 202 | +# Converting the ndarray to a DataArray can be useful for broadcasting operations. Two methods: 'manual' and using *llc_tiles_to_xda*. |
| 203 | +# |
| 204 | +# ### Method 1: Manual |
181 | 205 |
|
182 | | -# In[16]: |
| 206 | +# In[18]: |
183 | 207 |
|
184 | 208 |
|
185 | 209 | tile = range(1,14) |
186 | 210 | i = range(90) |
187 | 211 | j = range(90) |
188 | | -month = range(12) |
| 212 | +time = range(12) # months |
189 | 213 | k = [0]; |
190 | 214 |
|
191 | 215 |
|
192 | | -# In[17]: |
| 216 | +# In[19]: |
193 | 217 |
|
194 | 218 |
|
195 | 219 | # Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields |
196 | | -runoff_DA = xr.DataArray(runoff,coords={'month': month, |
| 220 | +runoff_DA = xr.DataArray(runoff,coords={'time': time, |
197 | 221 | 'k': k, |
198 | 222 | 'tile': tile, |
199 | 223 | 'j': j, |
200 | | - 'i': i},dims=['month','k','tile','j','i']) |
| 224 | + 'i': i}, dims=['time','k','tile','j','i']) |
201 | 225 |
|
202 | 226 |
|
203 | | -# In[18]: |
| 227 | +# In[20]: |
204 | 228 |
|
205 | 229 |
|
206 | | -runoff_DA.dims |
| 230 | +print(runoff_DA.dims) |
| 231 | +print(runoff_DA.shape) |
207 | 232 |
|
208 | 233 |
|
209 | | -# In[19]: |
| 234 | +# ### Method 2: *llc_tiles_to_xda* |
| 235 | +# |
| 236 | +# runoff is a 3D array with the 3rd dimension being time and therefore we need to pass *llc_tiles_to_xda* a similarly-dimensioned DataArray or tell the subroutine that the 3rd dimension is depth. The 'ecco_grid' DataSet doesn't have any similary-dimensioned DataArrays (no DataArrays with a time dimension). Therefore we will tell the routine that the new 4th dimension should be time: |
| 237 | + |
| 238 | +# In[21]: |
210 | 239 |
|
211 | 240 |
|
212 | | -runoff_DA.shape |
| 241 | +##### specify that the 5th dimension should be time |
| 242 | +runoff_DA2 = ecco.llc_tiles_to_xda(runoff, var_type='c',dim4='depth', dim5='time') |
| 243 | +print(runoff_DA2.dims) |
| 244 | +print(runoff_DA2.coords) |
213 | 245 |
|
214 | 246 |
|
215 | 247 | # ## Example 3: Load a 3D 'compact' llc binary file with 3rd dimension = Depth |
216 | 248 | # |
217 | 249 | # |
218 | | -# 'totak_kapredi' is a 50 depth level field of the adjusted GM redi parameter, dimensions of [depth, j, i] |
| 250 | +# The file 'total_kapredi_r009bit11.bin' is a 50 depth level array of the adjusted GM redi parameter (first guess + adjustments), dimensions of [depth, j, i] |
219 | 251 |
|
220 | | -# In[20]: |
| 252 | +# In[22]: |
221 | 253 |
|
222 | 254 |
|
223 | 255 | input_file = 'total_kapredi_r009bit11.bin' |
224 | 256 |
|
225 | 257 |
|
226 | | -# specify the number of depth levels as 50. |
| 258 | +# specify the number of depth levels as 50. n_recs defaults to 1 and is dropped by default. |
227 | 259 |
|
228 | | -# In[21]: |
| 260 | +# In[23]: |
229 | 261 |
|
230 | 262 |
|
231 | 263 | kapredi = ecco.read_llc_to_tiles(input_dir, input_file, nk=50) |
232 | 264 |
|
233 | 265 |
|
234 | | -# In[22]: |
| 266 | +# In[24]: |
235 | 267 |
|
236 | 268 |
|
237 | 269 | print(kapredi.shape) |
238 | 270 |
|
239 | 271 |
|
240 | 272 | # ### Plot log10 of the parameter at the 10th depth level (105m) |
241 | 273 |
|
242 | | -# In[23]: |
| 274 | +# In[25]: |
243 | 275 |
|
244 | 276 |
|
245 | 277 | plt.figure(figsize=(15,6)); |
|
248 | 280 |
|
249 | 281 | # ## Convert the ndarray into a DataArray |
250 | 282 | # |
251 | | -# Converting the ndarray to a DataArray can be useful for broadcasting operations. |
| 283 | +# Converting the ndarray to a DataArray can be useful for broadcasting operations. Two methods: 'manual' and using *llc_tiles_to_xda*. |
| 284 | +# |
| 285 | +# ### Method 1: Manual |
252 | 286 |
|
253 | | -# In[24]: |
| 287 | +# In[26]: |
254 | 288 |
|
255 | 289 |
|
256 | 290 | tile = range(1,14) |
|
259 | 293 | k = range(50) |
260 | 294 |
|
261 | 295 |
|
262 | | -# In[25]: |
| 296 | +# In[27]: |
263 | 297 |
|
264 | 298 |
|
265 | 299 | # Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields |
|
269 | 303 | 'i': i},dims=['k','tile','j','i']) |
270 | 304 |
|
271 | 305 |
|
272 | | -# In[26]: |
| 306 | +# In[28]: |
273 | 307 |
|
274 | 308 |
|
275 | | -kapredi_DA.dims |
| 309 | +print(kapredi_DA.dims) |
| 310 | +print(kapredi_DA.shape) |
276 | 311 |
|
277 | 312 |
|
278 | | -# In[27]: |
| 313 | +# ### Method 2: *llc_tiles_to_xda* |
| 314 | +# |
| 315 | +# kapredi is a 4D array (depth, tile, j, i) and therefore we need to pass *llc_tiles_to_xda* a similarly-dimensioned DataArray or tell the subroutine that the 4th dimension is depth |
| 316 | + |
| 317 | +# In[29]: |
| 318 | + |
279 | 319 |
|
| 320 | +# use similarly-dimensioned DataArray: hFacC |
| 321 | +kapredi_DA2 = ecco.llc_tiles_to_xda(kapredi, var_type='c',grid_da=ecco_grid.hFacC) |
| 322 | +print(kapredi_DA2.dims) |
| 323 | +print(kapredi_DA2.coords) |
280 | 324 |
|
281 | | -kapredi_DA.shape |
| 325 | + |
| 326 | +# In[30]: |
| 327 | + |
| 328 | + |
| 329 | +# specify that the 4th dimension should be depth |
| 330 | +kapredi_DA3 = ecco.llc_tiles_to_xda(kapredi, var_type='c', dim4='depth') |
| 331 | +print(kapredi_DA3.dims) |
| 332 | +print(kapredi_DA3.coords) |
282 | 333 |
|
283 | 334 |
|
284 | 335 | # ## Parting thoughts |
285 | 336 | # |
286 | | -# *read_llc_to_tiles* can also be used to read ECCO '*.data'* generated when re-running the ECCO model. |
| 337 | +# 1. *read_llc_to_tiles* can also be used to read ECCO '*.data'* generated when re-running the ECCO model. |
| 338 | +# |
| 339 | +# 1. Converting from numpy ndarrays or xarray DataArrays in the 'tile' format to a 5-faces format or compact format can be made with routines like: *llc_tiles_to_compact* and *llc_tiles_to_faces* |
| 340 | +# |
| 341 | +# ### *llc_tiles_to_faces* |
| 342 | + |
| 343 | +# In[31]: |
| 344 | + |
| 345 | + |
| 346 | +help(ecco.llc_tiles_to_faces) |
| 347 | + |
| 348 | + |
| 349 | +# ### *llc_tiles_to_compact* |
| 350 | + |
| 351 | +# In[32]: |
| 352 | + |
| 353 | + |
| 354 | +help(ecco.llc_tiles_to_compact) |
| 355 | + |
0 commit comments