def resample_to_latlon_and_save_to_nc(dir_data,dir_grid,dir_output,\ var_to_load,years_to_load, k_sub = [0], \ min_lat=-90, max_lat=90,min_lon=0, max_lon=360, dx = 0.25, dy = 0.25,\ mapping_method='nearest_neighbor',fill_value=np.nan): import ecco_v4_py as ecco import numpy as np import xarray as xr ds_grid = ecco.load_ecco_grid_nc(dir_grid) mask_C = ds_grid.hFacC.isel(k=k_sub) mask_C = mask_C.where(mask_C > 0, np.nan) # resample n_lon = len(np.arange(min_lon + dx, max_lon - dx, dx)) + 1 n_lat = len(np.arange(min_lat + dy, max_lat - dy, dy)) + 1 #%% load data for year in list(years_to_load): ds_data = ecco.load_ecco_var_from_years_nc(dir_data + '/' + str(year), var_to_load=var_to_load, years_to_load=[year], k_subset=[0]) n_time = len(ds_data.time) sst = np.full([n_time, n_lat, n_lon], np.nan) for in_time in range(0, n_time): print(year, in_time + 1) lon,lat,sst[in_time,:,:] = ecco.resample_to_latlon(ds_data.XC,ds_data.YC,ds_data.THETA.isel(time=in_time, k=0)*mask_C,\ min_lat+dy, max_lat-dy, dy,\ min_lon+dx, max_lon-dx, dx,\ mapping_method=mapping_method,fill_value=fill_value) ds_data_save = xr.Dataset({}) ds_data_save.coords['lon'] = lon[0, :] ds_data_save.coords['lat'] = lat[:, 0] ds_data_save.coords['time'] = ds_data.time ds_data_save[var_to_load] = (('time', 'lat', 'lon'), sst) # name ds_data_save.lon['long_name'] = 'longitude (degree)' ds_data_save.lat['long_name'] = 'latitude (degree)' ds_data_save[var_to_load]['long_name'] = var_to_load # output file_output = dir_output + var_to_load + str(year) + '.nc' print('Saving ' + file_output + '...') ds_data_save.to_netcdf(path=file_output) print('Finish ' + file_output + '.')
# Define main directory base_dir = '/home/username/ECCOv4-release' # Define ECCO version ecco_version = 'v4r3' # Define a high-level directory for ECCO fields ECCO_dir = base_dir + '/Release3_alt' # > **Note**: Change `base_dir` to your own directory path. # In[7]: # Load the model grid grid_dir = ECCO_dir + '/nctiles_grid/' ecco_grid = ecco.load_ecco_grid_nc(grid_dir, 'ECCOv4r3_grid.nc') # ### Volume # Calculate the volume of each grid cell. This is used when converting advective and diffusive flux convergences and calculating volume-weighted averages. # In[8]: # 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'
import warnings warnings.filterwarnings('ignore') 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
# else: # leap = False # else: # leap = True # else: # leap = False # return leap dir_ecco = '/Volumes/ECCO/ecco.jpl.nasa.gov/drive/files/Version4/Release4/' dir_monthly = dir_ecco + 'nctiles_monthly/' dir_grid = dir_ecco + '/nctiles_grid/' dir_daily = dir_ecco + 'nctiles_daily/' dir_output = '/Volumes/ECCO/ecco.jpl.nasa.gov/drive/files/Version4/Release4/interp_daily/SST/chinasea/' ds_grid = ecco.load_ecco_grid_nc(dir_grid, 'ECCOv4r3_grid.nc') mask_C = ds_grid.hFacC.isel(k=0) mask_C = mask_C.where(mask_C > 0, np.nan) # resample dx = 0.25 dy = 0.25 min_lat = 0 max_lat = 50 min_lon = 100 max_lon = 140 n_lon = len(np.arange(min_lon + dx, max_lon - dx, dx)) + 1 n_lat = len(np.arange(min_lat + dy, max_lat - dy, dy)) + 1 #%% load data for year in range(1992, 2018):
# In[5]: # Use plot_tiles to make a quick plot of the 13 tiles. See the tutorial on plotting for more examples. ecco.plot_tiles(bathy, layout='latlon',rotate_to_latlon=True,show_tile_labels=False, show_colorbar=True); # ## Load ecco-grid information to make a fancier lat-lon plot # # 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. # In[6]: ecco_grid_dir = '/Users/ifenty/tmp/nctiles_grid/' ecco_grid = ecco.load_ecco_grid_nc(input_dir, 'ECCO-GRID.nc') # In[7]: plt.figure(figsize=(15,6)); ecco.plot_proj_to_latlon_grid(ecco_grid.XC, ecco_grid.YC, bathy, show_colorbar=True, user_lon_0=-66); # In[8]: plt.figure(figsize=(12,8)); 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);
# In[4]: ## Set top-level file directory for the ECCO NetCDF files ## ================================================================= # base_dir = '/home/username/' 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 = 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)`.
# base_dir = '/home/username/' base_dir = '/home/ifenty/ECCOv4-release' ## define a high-level directory for ECCO fields ECCO_dir = base_dir + '/Release3_alt' # Now load daily and monthly mean versions of SSH and THETA # 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]:
import numpy as np import xarray as xr import sys import matplotlib.pyplot as plt import ecco_v4_py as ecco ## define a high-level directory for ECCO fields 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') # grid2 = ecco.load_ecco_grid_nc(grid_dir, 'ECCOv4r3_grid.nc') ## load the grid grid = xr.open_dataset(dir_grid + '/ECCOv4r3_grid.nc') # plot hFac ecco.plot_tiles(grid.hFacC.sel(k=0), show_colorbar=True, cmap='gray', figsize=(10, 9)) ecco.plot_tiles(grid.rA, show_colorbar=True) ## Method 2: Loading the model grid parameters using load_ecco_grid_nc grid_subset = ecco.load_ecco_grid_nc(dir_grid, 'ECCOv4r3_grid.nc', tiles_to_load='all', k=0) ecco.plot_tiles(grid_subset.hFacC.sel(k=0), show_colorbar=True, cmap='gray')
ecco.plot_tiles(grid.rA, show_colorbar=True); 'Model grid cell surface area [m^2]' # ### Method 2: Loading the model grid parameters using ``load_ecco_grid_nc`` # # A more advanced routine, ``load_ecco_grid_nc``, allows you to load only a subset of tiles and vertical levels. If no optional parameters are given, the entire grid object is loaded, just like ``open_dataset`` # In[7]: grid_dir = ECCO_dir + 'nctiles_grid' grid = ecco.load_ecco_grid_nc(grid_dir, 'ECCOv4r3_grid.nc') grid # Alternatively we can load just a subset of tiles and vertical levels. # In[8]: grid_subset = ecco.load_ecco_grid_nc(grid_dir, 'ECCOv4r3_grid.nc', tiles_to_load = [1, 10, 12], k_subset=[0,1,2,3]) grid_subset # notice that ``grid_subset`` only has 3 tiles (9,10, 11) and 4 depth levels (0, 1, 2, 3), as expected. # # Let's plot ``hFacC`` and ``rA`` again