Skip to content

Commit 16bfc13

Browse files
committed
added examples on creating new DataArray objects using Tim's to_xda routine to loading_llc_compact_binary_files tutorial
1 parent 9d6e843 commit 16bfc13

2 files changed

Lines changed: 493 additions & 186 deletions

File tree

Tutorials_as_Jupyter_Notebooks/ECCO_v4_Loading_LLC_compact_binary_files.ipynb

Lines changed: 373 additions & 135 deletions
Large diffs are not rendered by default.

Tutorials_as_Python_Files/ECCO_v4_Loading_LLC_compact_binary_files.py

Lines changed: 120 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
## tell Python where to find it. For example, if your ecco_v4_py
2020
## files are in /Users/ifenty/ECCOv4-py/ecco_v4_py, then use:
2121
import sys
22-
sys.path.append('/Users/ifenty/ECCOv4-py')
22+
23+
sys.path.append('/Users/ifenty/git_repos/my_forks/ECCOv4-py')
2324
import ecco_v4_py as ecco
2425

2526
import matplotlib.pyplot as plt
@@ -32,15 +33,15 @@
3233
# *read_llc_to_tiles* reads a llc compact format binary file and converts to a numpy ndarray of dimension:
3334
# [N_recs, N_z, N_tiles, llc, llc]
3435
#
35-
# For ECCOv4 our convenction is:
36+
# For ECCOv4 our convention is:
3637
# ```
37-
# 'N_recs' = number of time cords
38+
# 'N_recs' = number of time levels
3839
# 'N_z' = number of depth levels
3940
# 'N_tiles' = 13
4041
# 'llc' = 90
4142
# ```
4243
#
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).
4445
#
4546
# There are several other options which you can learn about using the 'help' command:
4647

@@ -64,7 +65,7 @@
6465

6566
# ## Example 1: Load a 2D llc 'compact' binary file
6667
#
67-
# First load the bathymetry map
68+
# The file 'bathy_eccollc_90x50_min2pts.bin' contains the 2D array of bathymetry for the model.
6869

6970
# In[3]:
7071

@@ -73,55 +74,58 @@
7374
input_file = 'bathy_eccollc_90x50_min2pts.bin'
7475

7576

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+
7679
# In[4]:
7780

7881

7982
bathy = ecco.read_llc_to_tiles(input_dir, input_file)
8083

8184

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
8388

8489
# In[5]:
8590

8691

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);
9094

9195

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.
9399

94100
# In[6]:
95101

96102

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')
100105

101-
# ## Load ecco-grid information to make a fancier lat-lon plot
102106

103107
# In[7]:
104108

105109

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);
108112

109113

110114
# In[8]:
111115

112116

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);
115119

116120

117121
# ## Convert the ndarray into a DataArray
118122
#
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:
120124

121125
# In[9]:
122126

123127

124-
tile = range(1,14)
128+
tile = range(13)
125129
i = range(90)
126130
j = range(90)
127131

@@ -132,7 +136,7 @@
132136
# Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields
133137
bathy_DA = xr.DataArray(bathy,coords={'tile': tile,
134138
'j': j,
135-
'i': i},dims=['tile','j','i'])
139+
'i': i}, dims=['tile','j','i'])
136140

137141

138142
# In[11]:
@@ -141,105 +145,133 @@
141145
print(bathy_DA.dims)
142146

143147

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+
144164
# ## Example 2: Load a 3D 'compact' llc binary file with 3rd dimension = Time
145165
#
146166
#
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].
148168

149-
# In[12]:
169+
# In[14]:
150170

151171

152172
input_file = 'runoff-2d-Fekete-1deg-mon-V4-SMOOTH.bin'
153173

154174

155175
# specify the length of the n_recs dimension, nl, as 12. By default, nk = 1
156176

157-
# In[13]:
177+
# In[15]:
158178

159179

160180
runoff = ecco.read_llc_to_tiles(input_dir, input_file, nl=12)
161181

162182

163-
# In[14]:
183+
# Shape is n_recs (12), n_z (1), n_tiles (13), llc (90), llc (90)
184+
185+
# In[16]:
164186

165187

166188
print(runoff.shape)
167189

168190

169191
# ### Plot the November runoff climatology
170192

171-
# In[15]:
193+
# In[17]:
172194

173195

174196
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);
176198

177199

178200
# ## Convert the ndarray into a DataArray
179201
#
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
181205

182-
# In[16]:
206+
# In[18]:
183207

184208

185209
tile = range(1,14)
186210
i = range(90)
187211
j = range(90)
188-
month = range(12)
212+
time = range(12) # months
189213
k = [0];
190214

191215

192-
# In[17]:
216+
# In[19]:
193217

194218

195219
# 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,
197221
'k': k,
198222
'tile': tile,
199223
'j': j,
200-
'i': i},dims=['month','k','tile','j','i'])
224+
'i': i}, dims=['time','k','tile','j','i'])
201225

202226

203-
# In[18]:
227+
# In[20]:
204228

205229

206-
runoff_DA.dims
230+
print(runoff_DA.dims)
231+
print(runoff_DA.shape)
207232

208233

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]:
210239

211240

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)
213245

214246

215247
# ## Example 3: Load a 3D 'compact' llc binary file with 3rd dimension = Depth
216248
#
217249
#
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]
219251

220-
# In[20]:
252+
# In[22]:
221253

222254

223255
input_file = 'total_kapredi_r009bit11.bin'
224256

225257

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.
227259

228-
# In[21]:
260+
# In[23]:
229261

230262

231263
kapredi = ecco.read_llc_to_tiles(input_dir, input_file, nk=50)
232264

233265

234-
# In[22]:
266+
# In[24]:
235267

236268

237269
print(kapredi.shape)
238270

239271

240272
# ### Plot log10 of the parameter at the 10th depth level (105m)
241273

242-
# In[23]:
274+
# In[25]:
243275

244276

245277
plt.figure(figsize=(15,6));
@@ -248,9 +280,11 @@
248280

249281
# ## Convert the ndarray into a DataArray
250282
#
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
252286

253-
# In[24]:
287+
# In[26]:
254288

255289

256290
tile = range(1,14)
@@ -259,7 +293,7 @@
259293
k = range(50)
260294

261295

262-
# In[25]:
296+
# In[27]:
263297

264298

265299
# Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields
@@ -269,18 +303,53 @@
269303
'i': i},dims=['k','tile','j','i'])
270304

271305

272-
# In[26]:
306+
# In[28]:
273307

274308

275-
kapredi_DA.dims
309+
print(kapredi_DA.dims)
310+
print(kapredi_DA.shape)
276311

277312

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+
279319

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)
280324

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)
282333

283334

284335
# ## Parting thoughts
285336
#
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

Comments
 (0)