|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# # ECCOv4 Loading llc binary files in the 'compact' format |
| 5 | +# |
| 6 | +# This section demonstrates loading ECCO binary files written in the so-called llc 'compact' format. Compact binary files have a non-intuitive layout of the 13 lat-lon-cap tiles required by the MITgcm. This tutorial demonstrates using the 'read_llc_to_tiles' routine to read and re-organize the llc compact binary files into a more familiar 13-tile layout. |
| 7 | + |
| 8 | +# ## Objectives |
| 9 | +# |
| 10 | +# By the end of the tutorial you will be able to read llc compact binary files of any dimension, plot them, and convert them into DataArrays. |
| 11 | + |
| 12 | +# In[1]: |
| 13 | + |
| 14 | + |
| 15 | +## Import the ecco_v4_py library into Python |
| 16 | +## ========================================= |
| 17 | + |
| 18 | +## -- If ecco_v4_py is not installed in your local Python library, |
| 19 | +## tell Python where to find it. For example, if your ecco_v4_py |
| 20 | +## files are in /Users/ifenty/ECCOv4-py/ecco_v4_py, then use: |
| 21 | +import sys |
| 22 | +sys.path.append('/Users/ifenty/ECCOv4-py') |
| 23 | +import ecco_v4_py as ecco |
| 24 | + |
| 25 | +import matplotlib.pyplot as plt |
| 26 | +import numpy as np |
| 27 | +import xarray as xr |
| 28 | + |
| 29 | + |
| 30 | +# ## The *read_llc_to_tiles* subroutine |
| 31 | +# |
| 32 | +# *read_llc_to_tiles* reads a llc compact format binary file and converts to a numpy ndarray of dimension: |
| 33 | +# [N_recs, N_z, N_tiles, llc, llc] |
| 34 | +# |
| 35 | +# For ECCOv4 our convenction is: |
| 36 | +# ``` |
| 37 | +# 'N_recs' = number of time cords |
| 38 | +# 'N_z' = number of depth levels |
| 39 | +# 'N_tiles' = 13 |
| 40 | +# 'llc' = 90 |
| 41 | +# ``` |
| 42 | +# |
| 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 | +# |
| 45 | +# There are several other options which you can learn about using the 'help' command: |
| 46 | + |
| 47 | +# In[2]: |
| 48 | + |
| 49 | + |
| 50 | +help(ecco.read_llc_to_tiles) |
| 51 | + |
| 52 | + |
| 53 | +# ## Related routines |
| 54 | +# |
| 55 | +# Two related routines which you might find useful: |
| 56 | +# |
| 57 | +# 1. *read_llc_to_compact*: Loads an MITgcm binary file in the 'compact' format of the |
| 58 | +# lat-lon-cap (LLC) grids and preserves its original dimension |
| 59 | +# |
| 60 | +# 2. *read_llc_to_faces* : Loads an MITgcm binary file in the 'compact' format of the |
| 61 | +# lat-lon-cap (LLC) grids and converts it to the '5 faces' dictionary. |
| 62 | +# |
| 63 | +# For the remainder of the tutorial we will only use *read_llc_to_tiles*. |
| 64 | + |
| 65 | +# ## Example 1: Load a 2D llc 'compact' binary file |
| 66 | +# |
| 67 | +# First load the bathymetry map |
| 68 | + |
| 69 | +# In[3]: |
| 70 | + |
| 71 | + |
| 72 | +input_dir = '/Users/ifenty/tmp/input_init/' |
| 73 | +input_file = 'bathy_eccollc_90x50_min2pts.bin' |
| 74 | + |
| 75 | + |
| 76 | +# In[4]: |
| 77 | + |
| 78 | + |
| 79 | +bathy = ecco.read_llc_to_tiles(input_dir, input_file) |
| 80 | + |
| 81 | + |
| 82 | +# *bathy* is a float64 numpy array of dimension [13, 90, 90] |
| 83 | + |
| 84 | +# In[5]: |
| 85 | + |
| 86 | + |
| 87 | +print(bathy.shape) |
| 88 | +print(type(bathy)) |
| 89 | +print(type(bathy[0,0,0])) |
| 90 | + |
| 91 | + |
| 92 | +# ### Plot the 13 tiles |
| 93 | + |
| 94 | +# In[6]: |
| 95 | + |
| 96 | + |
| 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 | + |
| 100 | + |
| 101 | +# ## Load ecco-grid information to make a fancier lat-lon plot |
| 102 | + |
| 103 | +# In[7]: |
| 104 | + |
| 105 | + |
| 106 | +ecco_grid_dir = '/Users/ifenty/tmp/nctiles_grid/' |
| 107 | +ecco_grid = ecco.load_ecco_grid_nc(input_dir, 'ECCO-GRID.nc') |
| 108 | + |
| 109 | + |
| 110 | +# In[8]: |
| 111 | + |
| 112 | + |
| 113 | +plt.figure(figsize=(15,6)); |
| 114 | +ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, bathy, show_colorbar=True); |
| 115 | + |
| 116 | + |
| 117 | +# ## Convert the ndarray into a DataArray |
| 118 | +# |
| 119 | +# Converting the ndarray to a DataArray can be useful for broadcasting operations. |
| 120 | + |
| 121 | +# In[9]: |
| 122 | + |
| 123 | + |
| 124 | +tile = range(1,14) |
| 125 | +i = range(90) |
| 126 | +j = range(90) |
| 127 | + |
| 128 | + |
| 129 | +# In[10]: |
| 130 | + |
| 131 | + |
| 132 | +# Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields |
| 133 | +bathy_DA = xr.DataArray(bathy,coords={'tile': tile, |
| 134 | + 'j': j, |
| 135 | + 'i': i},dims=['tile','j','i']) |
| 136 | + |
| 137 | + |
| 138 | +# In[11]: |
| 139 | + |
| 140 | + |
| 141 | +print(bathy_DA.dims) |
| 142 | + |
| 143 | + |
| 144 | +# ## Example 2: Load a 3D 'compact' llc binary file with 3rd dimension = Time |
| 145 | +# |
| 146 | +# |
| 147 | +# Runoff is a 12 month climatology, dimensions of [time, j, i] |
| 148 | + |
| 149 | +# In[12]: |
| 150 | + |
| 151 | + |
| 152 | +input_file = 'runoff-2d-Fekete-1deg-mon-V4-SMOOTH.bin' |
| 153 | + |
| 154 | + |
| 155 | +# specify the length of the n_recs dimension, nl, as 12. By default, nk = 1 |
| 156 | + |
| 157 | +# In[13]: |
| 158 | + |
| 159 | + |
| 160 | +runoff = ecco.read_llc_to_tiles(input_dir, input_file, nl=12) |
| 161 | + |
| 162 | + |
| 163 | +# In[14]: |
| 164 | + |
| 165 | + |
| 166 | +print(runoff.shape) |
| 167 | + |
| 168 | + |
| 169 | +# ### Plot the November runoff climatology |
| 170 | + |
| 171 | +# In[15]: |
| 172 | + |
| 173 | + |
| 174 | +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'); |
| 176 | + |
| 177 | + |
| 178 | +# ## Convert the ndarray into a DataArray |
| 179 | +# |
| 180 | +# Converting the ndarray to a DataArray can be useful for broadcasting operations. |
| 181 | + |
| 182 | +# In[16]: |
| 183 | + |
| 184 | + |
| 185 | +tile = range(1,14) |
| 186 | +i = range(90) |
| 187 | +j = range(90) |
| 188 | +month = range(12) |
| 189 | +k = [0]; |
| 190 | + |
| 191 | + |
| 192 | +# In[17]: |
| 193 | + |
| 194 | + |
| 195 | +# Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields |
| 196 | +runoff_DA = xr.DataArray(runoff,coords={'month': month, |
| 197 | + 'k': k, |
| 198 | + 'tile': tile, |
| 199 | + 'j': j, |
| 200 | + 'i': i},dims=['month','k','tile','j','i']) |
| 201 | + |
| 202 | + |
| 203 | +# In[18]: |
| 204 | + |
| 205 | + |
| 206 | +runoff_DA.dims |
| 207 | + |
| 208 | + |
| 209 | +# In[19]: |
| 210 | + |
| 211 | + |
| 212 | +runoff_DA.shape |
| 213 | + |
| 214 | + |
| 215 | +# ## Example 3: Load a 3D 'compact' llc binary file with 3rd dimension = Depth |
| 216 | +# |
| 217 | +# |
| 218 | +# 'totak_kapredi' is a 50 depth level field of the adjusted GM redi parameter, dimensions of [depth, j, i] |
| 219 | + |
| 220 | +# In[20]: |
| 221 | + |
| 222 | + |
| 223 | +input_file = 'total_kapredi_r009bit11.bin' |
| 224 | + |
| 225 | + |
| 226 | +# specify the number of depth levels as 50. |
| 227 | + |
| 228 | +# In[21]: |
| 229 | + |
| 230 | + |
| 231 | +kapredi = ecco.read_llc_to_tiles(input_dir, input_file, nk=50) |
| 232 | + |
| 233 | + |
| 234 | +# In[22]: |
| 235 | + |
| 236 | + |
| 237 | +print(kapredi.shape) |
| 238 | + |
| 239 | + |
| 240 | +# ### Plot log10 of the parameter at the 10th depth level (105m) |
| 241 | + |
| 242 | +# In[23]: |
| 243 | + |
| 244 | + |
| 245 | +plt.figure(figsize=(15,6)); |
| 246 | +ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, np.log10(kapredi[10,:]), cmin=2,cmax=4,show_colorbar=True); |
| 247 | + |
| 248 | + |
| 249 | +# ## Convert the ndarray into a DataArray |
| 250 | +# |
| 251 | +# Converting the ndarray to a DataArray can be useful for broadcasting operations. |
| 252 | + |
| 253 | +# In[24]: |
| 254 | + |
| 255 | + |
| 256 | +tile = range(1,14) |
| 257 | +i = range(90) |
| 258 | +j = range(90) |
| 259 | +k = range(50) |
| 260 | + |
| 261 | + |
| 262 | +# In[25]: |
| 263 | + |
| 264 | + |
| 265 | +# Convert numpy array to an xarray DataArray with matching dimensions as the monthly mean fields |
| 266 | +kapredi_DA = xr.DataArray(kapredi,coords={'k': k, |
| 267 | + 'tile': tile, |
| 268 | + 'j': j, |
| 269 | + 'i': i},dims=['k','tile','j','i']) |
| 270 | + |
| 271 | + |
| 272 | +# In[26]: |
| 273 | + |
| 274 | + |
| 275 | +kapredi_DA.dims |
| 276 | + |
| 277 | + |
| 278 | +# In[27]: |
| 279 | + |
| 280 | + |
| 281 | +kapredi_DA.shape |
| 282 | + |
| 283 | + |
| 284 | +# ## Parting thoughts |
| 285 | +# |
| 286 | +# *read_llc_to_tiles* can also be used to read ECCO '*.data'* generated when re-running the ECCO model. |
0 commit comments