import matplotlib.pyplot as plt import xarray as xr import ecco_v4_py as ecco dir_ecco = '/Volumes/ECCO/ecco.jpl.nasa.gov/drive/files/Version4/Release4/' dir_monthly = dir_ecco + '/nctiles_monthly/' dir_daily = dir_ecco + '/nctiles_daily/' dir_grid = dir_ecco + '/nctiles_grid/' fname_grid = 'ECCOv4r3_grid.nc' ## read grid ds_grid = ecco.load_ecco_grid_nc(dir_grid,fname_grid, dask_chunk=True) vars_ecco = ecco.recursive_load_ecco_var_from_years_nc(dir_monthly,\ vars_to_load=['ADVx_TH', 'ADVy_TH',\ 'DFxE_TH', 'DFyE_TH',\ 'ADVx_SLT','ADVy_SLT',\ 'DFxE_SLT','DFyE_SLT',\ 'UVELMASS','VVELMASS'],\ years_to_load='all',\ dask_chunk=True) ds_ecco = xr.merge([vars_ecco,ds_grid]) del(ds_grid,vars_ecco) lat = 26 ds_ecco['maskC'] = ds_ecco.hFacC ds_ecco['maskW'] = ds_ecco.hFacW ds_ecco['maskS'] = ds_ecco.hFacS grid = ecco.get_llc_grid(ds_ecco) rapid_maskW, rapid_maskS = ecco.vector_calc.get_latitude_masks(lat_val=lat, yc=ds_ecco.YC, grid=grid) rapid_maskC = ecco.scalar_calc.get_latitude_mask(lat_val=lat, yc=ds_ecco.YC, grid=grid)
# Now that we have all 288 month records of SSH. # ## Loading one or more years of more than variable using ``recursive_load_ecco_var_from_years_nc`` # # With ``recursive_load_ecco_var_from_years_nc`` one can specify one or more variables to load, one or more years to load, while also requesting only a subset of tiles and vertical levels. # # Let's demonstrate by first loading all of the information for monthly-averaged ``SSH`` and ``THETA`` fields for the years 2010, 2011, and 2012. We'll then load SSH and OBP for *all* of the years and tile 6. Finally, we'll load all of the monthly-mean variables for the year 2010. # # ### Loading SSH and THETA for 2010-2012 # In[10]: nctiles_monthly_dir = ECCO_dir + 'nctiles_monthly/' SSH_THETA_2010_2012 = ecco.recursive_load_ecco_var_from_years_nc( nctiles_monthly_dir, vars_to_load=['SSH', 'THETA'], years_to_load=[2010, 2011, 2012]).load() # In[11]: SSH_THETA_2010_2012 # We see three years (36 months) of data and 13 tiles and 50 vertical levels. # ### Loading SSH and OBP for all years and tile 6 # Now let's demonstrate how the ``recursive_load_ecco_var_from_years_nc`` routine enable us to load all of the years of output for multiple variables. The trick is to specify the directory that contains all of the variables. The routine will recursively search all subdirectories for these fields. Note, this only works if the subdirectories are of the same temporal period (monthly mean, daily mean, or snapshots). # In[12]: SSH_OBP_2010_tile_6 = ecco.recursive_load_ecco_var_from_years_nc( nctiles_monthly_dir,
## define a high-level directory for ECCO fields dir_base = '/Volumes/ECCO/ecco.jpl.nasa.gov/drive/files/Version4' dir_ECCO = dir_base + '/Release4' if hostName[-1] == '3': dir_ECCO = '/Volumes/15394457571/ecco_eg' dir_grid = dir_ECCO + '/nctiles_grid' dir_daily = dir_ECCO + '/nctiles_daily/' dir_monthly = dir_ECCO + '/nctiles_monthly/' ## load the grid # grid = xr.open_dataset(dir_grid + '/ECCOv4r3_grid.nc') grid = ecco.load_ecco_grid_nc(dir_grid, 'ECCOv4r3_grid.nc') ecco_ds = ecco.recursive_load_ecco_var_from_years_nc(dir_monthly,\ vars_to_load=['ADVx_TH', 'ADVy_TH', \ 'DFxE_TH', 'DFyE_TH'],\ years_to_load='all',\ dask_chunk=True) ecco_ds = xr.merge([ecco_ds, grid]) grid = ecco.get_llc_grid(ecco_ds) rapid_mask_W, rapid_mask_S = ecco.vector_calc.get_latitude_masks(lat_val=26, yc=ecco_ds.YC, grid=grid) rapid_mask_C = ecco.scalar_calc.get_latitude_mask(lat_val=26, yc=ecco_ds.YC, grid=grid) lat = 26 ones = xr.ones_like(ecco_ds.YC) dome_mask_C = ones.where(ecco_ds.YC >= lat, 0)
## define a high-level directory for ECCO fields ECCO_dir = base_dir + '/Release3_alt' # In[5]: ## Load the model grid grid_dir = ECCO_dir + '/nctiles_grid/' ecco_grid = ecco.load_ecco_grid_nc(grid_dir) ## Load one year of 2D daily data, SSH, SST, and SSS data_dir = ECCO_dir + '/nctiles_monthly' ecco_vars = ecco.recursive_load_ecco_var_from_years_nc( data_dir, vars_to_load=['ADVx_TH', 'ADVy_TH', 'DFxE_TH', 'DFyE_TH'], years_to_load=range(2008, 2014)) ## Merge the ecco_grid with the ecco_vars to make the ecco_ds ecco_ds = xr.merge((ecco_grid, ecco_vars)) # ## Grab latitude band: 26$^\circ$N array as an example # # Here we want to grab the transport values which along the band closest represented in the model to 26$^\circ$N. # In a latitude longitude grid this could simply be done by, e.g. `U.sel(lat=26)`. # However, the LLC grid is slightly more complicated. # Luckily, the functionality enabled by the [xgcm Grid object](https://xgcm.readthedocs.io/en/latest/api.html#grid) makes this relatively easy. # Note that this subsection can be performed with the with the ecco_v4_py modules [vector_calc](https://github.com/ECCO-GROUP/ECCOv4-py/blob/master/ecco_v4_py/vector_calc.py) and [scalar_calc](https://github.com/ECCO-GROUP/ECCOv4-py/blob/master/ecco_v4_py/scalar_calc.py) as follows: # # ``` # from ecco_v4_py import vector_calc, scalar_calc
warnings.filterwarnings('ignore') import ecco_v4_py as ecco dir_base = '/Volumes/ECCO/ecco.jpl.nasa.gov/drive/files/Version4' dir_ECCO = dir_base + '/Release4/' dir_grid = dir_ECCO + '/nctiles_grid/' ## load the grid # grid = xr.open_dataset(dir_grid + '/ECCOv4r3_grid.nc') grid = ecco.load_ecco_grid_nc(dir_grid, 'ECCOv4r3_grid.nc') dir_day_mean = dir_ECCO + '/nctiles_daily/' ecco_vars = \ ecco.recursive_load_ecco_var_from_years_nc(dir_day_mean, \ vars_to_load=['SSH'], \ years_to_load=2010,\ dask_chunk=True) ecco_ds = xr.merge([grid, ecco_vars]) # ssh_arr = ecco_ds.SSH.values # # fig = plt.figure(figsize=(8,6.5)) # plt.imshow(ssh_arr[0,2,:,:],origin='lower') # fig = plt.figure(figsize=(8,6.5)) # plt.imshow(ecco_ds.SSH[0,2,:,:],origin='lower') # # ssh_masked = ecco_ds.SSH.where(grid.hFacC > 0, np.nan) # ssh_masked[0,2,:,:,0].plot(cmap='jet') # # xc = ecco_ds.XC
base_dir = '/Users/ifenty/ECCOv4-release' ## define a high-level directory for ECCO fields ECCO_dir = base_dir + '/Release3_alt' # In[5]: ## Load the model grid grid_dir = ECCO_dir + '/nctiles_grid/' ecco_grid = xr.open_dataset(grid_dir + 'ECCOv4r3_grid.nc') ## Load one year of 2D daily data, SSH, SST, and SSS day_mean_dir = ECCO_dir + '/nctiles_monthly/' ecco_vars = ecco.recursive_load_ecco_var_from_years_nc( day_mean_dir, vars_to_load=['SSH', 'THETA', 'SALT'], years_to_load=2010) ## Merge the ecco_grid with the ecco_vars to make the ecco_ds ecco_ds = xr.merge((ecco_grid, ecco_vars)).load() # In[6]: ecco_ds # ### Plotting a single tile with ``imshow`` # # First we'll plot the average SSH for the first month (Jan 1992) on tiles 3, 7, and 8 using the basic `imshow` routine from *pyplot*. We are plotting these three different tiles to show that these lat-lon-cap tiles all have a different orientation in $x$ and $y$. # # > **Note:** *The **origin='lower'** argument to `imshow` is required to make the $y$ origin at the bottom of the plot.* # # #### Tile 2 (Northeast Atlantic)
# In[4]: ## Load the model grid grid_dir= ECCO_dir + '/nctiles_grid/' ecco_grid = ecco.load_ecco_grid_nc(grid_dir, 'ECCOv4r3_grid.nc', k_subset=[0]) # In[5]: ## Load 2D DAILY data, SSH, SST, and SSS data_dir= ECCO_dir + '/nctiles_daily' ecco_daily_vars = ecco.recursive_load_ecco_var_from_years_nc(data_dir, vars_to_load=['SSH','THETA'], years_to_load=range(1993,2018)).load() ## Merge the ecco_grid with the ecco_vars to make the ecco_ds ecco_daily_ds = xr.merge((ecco_grid , ecco_daily_vars)) # In[7]: ## Load 2D MONTHLY data, SSH, SST, and SSS data_dir= ECCO_dir + '/nctiles_monthly' ecco_monthly_vars = ecco.recursive_load_ecco_var_from_years_nc(data_dir, vars_to_load=['SSH','THETA'], years_to_load=range(1993,2018), k_subset=[0]).load() ## Merge the ecco_grid with the ecco_vars to make the ecco_ds ecco_monthly_ds = xr.merge((ecco_grid , ecco_monthly_vars))
dir_daily = dir_ecco + '/nctiles_daily/' dir_grid = dir_ecco + '/nctiles_grid/' fname_grid = 'ECCOv4r3_grid.nc' ## read grid ds_grid = ecco.load_ecco_grid_nc(dir_grid,fname_grid, dask_chunk=True) year_start = 1992 year_end = 2017 # load one extra year worth of snapshots ds_ecco_monthly_snaps = \ ecco.recursive_load_ecco_var_from_years_nc(dir_monthly_snapshots,\ vars_to_load=['ETAN'],\ years_to_load='all',\ dask_chunk=True) date_last_record = \ ecco.extract_yyyy_mm_dd_hh_mm_ss_from_datetime64(ds_ecco_monthly_snaps.time[-1].values) # load monthly mean data ds_ecco_monthly_mean = \ ecco.recursive_load_ecco_var_from_years_nc(dir_monthly,\ vars_to_load=['ETAN',\ 'oceFWflx',\ 'UVELMASS',\ 'VVELMASS',\ 'WVELMASS'],\ years_to_load='all',\ dask_chunk=True)
## Load the model grid grid_dir = ECCO_dir / 'nctiles_grid/' # In[5]: ## Load the model grid ecco_grid = ecco.load_ecco_grid_nc(grid_dir, 'ECCO-GRID.nc') # In[6]: ## Load one year of 2D daily data, SSH, SST, and SSS day_mean_dir = ECCO_dir / 'nctiles_monthly/' ecco_vars = ecco.recursive_load_ecco_var_from_years_nc(day_mean_dir, vars_to_load=['SSH'], years_to_load=2000, less_output=True) ecco_ds = [] ## Merge the ecco_grid with the ecco_vars to make the ecco_ds ecco_ds = xr.merge((ecco_grid, ecco_vars)).load() pprint(ecco_ds.data_vars) # ### Plotting the dataset # # Plotting the ECCOv4 fields was covered in an earlier tutorial. Before demonstrating interpolation, we will first plot one of our SSH fields. Take a closer look at the arguments of ``plot_proj_to_latlon_grid``, ``dx=2`` and ``dy=2``. # In[7]:
print('saving to ', new_filename_1) theta_dataset.to_netcdf(path=new_filename_1) print('finished saving') # *It's really that simple!* # # ## Saving a new custom ``Dataset`` to NetCDF # # # Now let's create a new custom `Dataset` that with *THETA*, *SSH* and model grid parameter variables for a few tiles and depth level 10. # In[6]: data_dir = ECCO_dir + '/nctiles_monthly/' SSH_THETA_201003 = ecco.recursive_load_ecco_var_from_years_nc( data_dir, ['SSH', 'THETA'], tiles_to_load=[0, 1, 2], years_to_load=2010) grid_dir = ECCO_dir + '/nctiles_grid/' grid = ecco.load_ecco_grid_nc(grid_dir) grid.close() custom_dataset = xr.merge([SSH_THETA_201003, grid]) # and now we can easily save it: # In[7]: new_filename_2 = './test_output_2.nc' print('saving to ', new_filename_2) custom_dataset.to_netcdf(path=new_filename_2) custom_dataset.close() print('finished saving')
## define a high-level directory for ECCO fields ECCO_dir = base_dir + '/Release3_alt' # In[4]: ## Load the model grid grid_dir= ECCO_dir + '/nctiles_grid/' ecco_grid = ecco.load_ecco_grid_nc(grid_dir) ## Load one year of SSH and OBP day_mean_dir= ECCO_dir + '/nctiles_monthly/' ecco_vars = ecco.recursive_load_ecco_var_from_years_nc(day_mean_dir, vars_to_load=['SSH','OBP'], years_to_load=2010, dask_chunk=False) ## Merge the ecco_grid with the ecco_vars to make the ecco_ds ecco_ds = xr.merge((ecco_grid , ecco_vars)) # In[5]: ecco_ds.data_vars # In[6]: ecco_ds.coords
# load a single tile, monthly average THETA for March 2010 for model tile 2 dir_theta = dir_ECCO + '/nctiles_monthly/THETA/2010/' fname = 'THETA_2010_03.nc' dataset_theta = xr.open_dataset(dir_theta+fname).load() fname_save = './test_output.nc' print('saving to ',fname_save) dataset_theta.to_netcdf(path=fname_save) print('finished saving') # Saving a new custom Dataset to NetCDF dataset_ecco.to_netcdf(path=fname_save) # Saving the results of calculations dir_ETAN = dir_ECCO + '/nctiles_monthly/ETAN/' dataset_ETAN = xr.open_dataset(dir_ETAN + '/1992/ETAN_1992_01.nc') SSH_THETA_201003 = \ ecco.recursive_load_ecco_var_from_years_nc(dir_ECCO+'/nctiles_monthly/', \ ['SSH', 'THETA'], \ tiles_to_load = [0,1,2], years_to_load = 2010) SSH_sq = SSH_THETA_201003.SSH * SSH_THETA_201003.SSH SSH_sq.name = 'SSH^2' SSH_sq.attrs['long_name'] = 'Square of Surface Height Anomaly' SSH_sq.attrs['units'] = 'm^2' SSH_sq.to_netcdf(path='./test.nc')
plt.title('SSH (m)') # Method 2, using load_ecco_var_from_years_nc dataset_ETAN = ecco.load_ecco_var_from_years_nc(dir_ETAN,'ETAN',years_to_load=1992).load() dataset_ETAN.attrs = [] dataset_ETAN # Method 3, using dask ## Example 1a: Load two years monthly-mean ECCO fields into memory without Dask dir_mon_mean = dir_ECCO + '/nctiles_monthly/' import time t_0 = time.time() large_subset_no_dask = \ ecco.recursive_load_ecco_var_from_years_nc(dir_mon_mean, \ vars_to_load= ['THETA','ETAN'], \ years_to_load=[2010, 2011], \ less_output=True) delta_t_2_yrs_no_dask = time.time() - t_0 print(delta_t_2_yrs_no_dask) ## Example 1b: Load two years of monthly-mean ECCO fields into memory using Dask t_0 = time.time() large_subset_with_dask = \ ecco.recursive_load_ecco_var_from_years_nc(dir_mon_mean, \ vars_to_load= ['THETA','ETAN'], \ years_to_load=[2010, 2011]) delta_t_2_yrs_with_dask = time.time() - t_0 print(delta_t_2_yrs_with_dask) ## Example 2: Load two years of monthly-mean ECCO fields into memory with Dask t_0 = time.time() large_subset_with_dask1 = \
## Load the model" grid grid_dir = ECCO_dir + '/nctiles_grid/' ecco_grid = ecco.load_ecco_grid_nc(grid_dir, 'ECCOv4r3_grid.nc') # ### Load 2D MONTHLY $\eta$ snapshots # In[6]: data_dir = ECCO_dir + '/nctiles_monthly_snapshots' year_start = 1993 year_end = 2017 # load one extra year worth of snapshots ecco_monthly_snaps = ecco.recursive_load_ecco_var_from_years_nc( data_dir, vars_to_load=['ETAN'], years_to_load=range(year_start, year_end + 1)).load() num_months = len(ecco_monthly_snaps.time.values) # drop the last 11 months so that we have one snapshot at the beginning and end of each month within the # range t1993/1/1 to 2015/1/1) ecco_monthly_snaps = ecco_monthly_snaps.isel( time=np.arange(0, num_months - 11)) # In[7]: # 1993-01 (beginning of first month) to 2015-01-01 (end of last month, 2014-12) print(ecco_monthly_snaps.ETAN.time.isel(time=[0, -1]).values) # In[8]:
import socket hostName = socket.gethostname() ## define a high-level directory for ECCO fields dir_base = '/Volumes/ECCO/ecco.jpl.nasa.gov/drive/files/Version4' dir_ECCO = dir_base +'/Release4' if hostName[-1] == '3': dir_ECCO = '/Volumes/15394457571/ecco_eg' dir_grid = dir_ECCO + '/nctiles_grid' dir_daily = dir_ECCO + '/nctiles_daily/' ## load the grid # grid = xr.open_dataset(dir_grid + '/ECCOv4r3_grid.nc') grid = ecco.load_ecco_grid_nc(dir_grid, 'ECCOv4r3_grid.nc') ecco_ds = ecco.recursive_load_ecco_var_from_years_nc(dir_daily,\ vars_to_load=['SSH'],\ years_to_load=1992, dask_chunk=True) ecco_ds = xr.merge([grid,ecco_ds]) # Area_global ocean_mask = np.ceil(ecco_ds.hFacC) ocean_mask = ocean_mask.where(ocean_mask==1, np.nan) # plt.figure(figsize=(12,9),dpi=90) ax = ecco.plot_tiles(ocean_mask.sel(k=0),layout='latlon', rotate_to_latlon=True,fig_size=6) plt.suptitle('test') area_sum = np.nansum(ocean_mask.isel(k=0)*ecco_ds.rA) ssh_global_mean = (ecco_ds.SSH*ecco_ds.rA*ocean_mask.isel(k=0)).sum(dim=['i','j','tile'])/area_sum ssh_global_mean = ssh_global_mean - ssh_global_mean.mean(dim='time') ssh_global_mean.['units']='m'
# Volume (m^3) vol = (ecco_grid.rA * ecco_grid.drF * ecco_grid.hFacC).transpose( 'tile', 'k', 'j', 'i') # ### Load monthly snapshots # In[9]: data_dir = ECCO_dir + '/nctiles_monthly_snapshots' year_start = 1993 year_end = 2017 # Load one extra year worth of snapshots ecco_monthly_snaps = ecco.recursive_load_ecco_var_from_years_nc( data_dir, vars_to_load=['ETAN', 'THETA'], years_to_load=range(year_start, year_end + 1)) num_months = len(ecco_monthly_snaps.time.values) # Drop the last 11 months so that we have one snapshot at the beginning and end of each month within the # range 1993/1/1 to 2015/1/1 ecco_monthly_snaps = ecco_monthly_snaps.isel( time=np.arange(0, num_months - 11)) # In[10]: # 1993-01 (beginning of first month) to 2015-01-01 (end of last month, 2014-12) print(ecco_monthly_snaps.ETAN.time.isel(time=[0, -1]).values) # In[11]:
## define a high-level directory for ECCO fields ECCO_dir = base_dir + '/Release3_alt' # In[5]: ## Load the model grid grid_dir= ECCO_dir + '/nctiles_grid/' ecco_grid = ecco.load_ecco_grid_nc(grid_dir) ## Load one year of 2D daily data, SSH, SST, and SSS data_dir= ECCO_dir + '/nctiles_monthly' ecco_vars = ecco.recursive_load_ecco_var_from_years_nc(data_dir, vars_to_load=['ADVx_TH', 'ADVy_TH', 'UVELMASS', 'VVELMASS'], years_to_load = range(2008,2014)) ## Merge the ecco_grid with the ecco_vars to make the ecco_ds ds = xr.merge((ecco_grid , ecco_vars)) # ## Define the OSNAP lines # # We define the OSNAP lines roughly by point pairs in `[longitude, latitude]` space. # The lines are then computed via the function `ecco_v4_py.get_section_line_masks` as the great circle arc between these two points. # # See below for similar MATLAB functions # In[6]: pt1_east = [-44, 60]