예제 #1
0
def test_plot_tiles(get_test_array_2d, vdict, is_xda):
    """Run through various options and make sure nothing is broken"""

    test_arr = get_test_array_2d
    test_arr = test_arr if is_xda else test_arr.values
    ecco.plot_tiles(test_arr, **vdict)
    plt.close()
예제 #2
0
def test_plot_tiles(vdict):
    """Run through various options and make sure nothing is broken"""

    # read in array
    nparr = get_test_array(is_xda=False)
    xda = get_test_array(is_xda=True)

    # run plotting routine
    for arr in [nparr, xda]:
        ecco.plot_tiles(arr, **vdict)
        plt.close()
예제 #3
0
def test_plot_tiles_array(get_test_array_2d, is_xda):
    """a crude test to make sure the array being created
    matches the original"""

    arr_expected = get_test_array_2d
    arr_expected = arr_expected if is_xda else arr_expected.values
    _, arr_test = ecco.plot_tiles(arr_expected)
    assert np.allclose(np.nansum(arr_test), float(np.nansum(arr_expected)))
    plt.close()
예제 #4
0
def test_plot_tiles_array():
    """a crude test to make sure the array being created
    matches the original"""

    nparr = get_test_array(is_xda=False)
    xda = get_test_array(is_xda=True)

    for arr_expected in [nparr, xda]:
        _, arr_test = ecco.plot_tiles(arr_expected)
        assert np.nansum(arr_test) == float(np.nansum(arr_expected))
        plt.close()
예제 #5
0
ones = xr.ones_like(ds_ecco.YC)
dome_maskC = ones.where(ds_ecco.YC>=lat, 0)

# plt.figure(figsize=(12,5))
# ecco.plot_proj_to_latlon_grid(dome_maskC.XC,dome_maskC.YC,dome_maskC,\
#                               projection_type='robin',\
#                               cmap='hot',
#                               user_lon_0=0,\
#                               show_colorbar=True, show_grid_labels=True, show_grid_lines=True)

maskC = ds_ecco.maskC
maskW = ds_ecco.maskW
maskS = ds_ecco.maskS

plt.figure(figsize=(12,5))
ecco.plot_tiles(dome_maskC+maskC.isel(k=0), cmap='viridis')
plt.show()


lat_maskW = grid.diff(dome_maskC,'X',boundary='fill')
lat_maskS = grid.diff(dome_maskC,'Y',boundary='fill')

atl_maskW = ecco.get_basin_mask(basin_name='atlExt', mask=maskW.isel(k=0))
atl_maskS = ecco.get_basin_mask(basin_name='atlExt', mask=maskS.isel(k=0))

plt.figure(figsize=(12,5))
ecco.plot_proj_to_latlon_grid(ds_ecco.XC, ds_ecco.YC, atl_maskW, projection_type='robin', cmap='viridis', user_lon_0=0)
plt.show()

mvt = ecco.calc_meridional_vol_trsp(ds_ecco,lat_vals=lat, basin_name='atlExt')
mht = ecco.calc_meridional_heat_trsp(ds_ecco,lat_vals=lat, basin_name='atlExt')
# Mask masks and add them to ecco_ds

maskC = ecco_ds.hFacC.where(ecco_ds.hFacC > 0, np.nan)
maskW = ecco_ds.hFacW.where(ecco_ds.hFacW > 0, np.nan)
maskS = ecco_ds.hFacS.where(ecco_ds.hFacS > 0, np.nan)

maskW.name = 'maskW'
maskS.name = 'maskS'
maskC.name = 'maskC'
ecco_ds = xr.merge((ecco_ds, maskW, maskC, maskS))

# In[9]:

plt.figure(figsize=(12, 6))
ecco.plot_tiles(dome_maskC + maskC.isel(k=0), cmap='viridis')
plt.show()

# Recall that for tiles 7-12, the y-dimension actually runs East-West.
# Therefore, we want to compute a finite difference in the x-dimension on these tiles to get the latitude band at 26$^\circ$N.
# For tiles 1-5, we clearly want to difference in the y-dimension.
# Things get more complicated on tile 6.

# Here we make the [xgcm Grid object](https://xgcm.readthedocs.io/en/latest/api.html#grid) which allows us to compute finite differences in simple one liners.
# This object understands how each of the tiles on the LLC grid connect, because we provide that information.
# To see under the hood, checkout the utility function [get_llc_grid](https://github.com/ECCO-GROUP/ECCOv4-py/blob/master/ecco_v4_py/ecco_utils.py) where these connections are defined.

# In[10]:

ecco_ds
예제 #7
0
# In[4]:


bathy = ecco.read_llc_to_tiles(input_dir, input_file)


# *bathy* is a numpy float32 array with dimension [13, 90, 90]

# ### Plot the 13 tiles bathymetry data 

# 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]:
예제 #8
0
def interploate_one3d(infile, outfile, varnm, unitnm):
    # ## Load the Grid Data

    # In[2]:

    fdir = '/Volumes/data01/forum/user/owang/junk/nonblank/'
    fname = 'XC.data'
    # in compact format
    XC = ecco.load_llc_compact(fdir, fname)
    # in 13 tiles format
    XC_t = ecco.load_llc_compact_to_tiles(fdir, fname, less_output=True)

    fname = 'YC.data'
    YC = ecco.load_llc_compact(fdir, fname)
    YC_t = ecco.load_llc_compact_to_tiles(fdir, fname, less_output=True)

    fname = 'maskCtrlC.data'
    maskc = ecco.load_llc_compact(fdir, fname, nk=50)
    maskc_t = ecco.load_llc_compact_to_tiles(fdir,
                                             fname,
                                             less_output=True,
                                             nk=50)

    # This is a 1D array.
    RC = np.squeeze(-1 * ecco.load_binary_array(fdir, 'RC.data', 1, 50))

    # In[3]:

    fname = infile
    fdir = '/Volumes/data01/forum/user/owang/junk/h8i_i48/diags/STATE/'

    # as compact
    data = ecco.load_llc_compact(fdir, fname, nk=50)
    # as tiles
    data_t = ecco.llc_compact_to_tiles(data, less_output=True)

    # In[4]:

    #   print(data_t.shape)
    #   print(data.shape)

    # # Plot level 11 of the data

    # In[5]:

    ecco.plot_tiles(data_t[:, 10, :],
                    layout='latlon',
                    rotate_to_latlon=True,
                    cmin=-.2,
                    cmax=.2)

    # # Resample to nearest latlon point

    # In[6]:

    #help(ecco.resample_to_latlon)

    # In[7]:

    new_grid_delta_deg = 0.25

    new_grid_min_lat = -90 + new_grid_delta_deg
    new_grid_max_lat = 90 - new_grid_delta_deg
    new_grid_min_lon = -180
    new_grid_max_lon = 180 - new_grid_delta_deg

    radius_of_influence = 120000  #m

    # In[8]:

    RC.shape

    # In[9]:

    for num, name in np.ndenumerate(RC):
        print num, name
        print type(num)

    # In[10]:

    data_latlon_projection = []

    for num, name in np.ndenumerate(RC):
        print(num[0])

        # create mask
        masktmp = 1. - maskc[num, :]
        datamsked = ma.array(data[num, :], mask=masktmp)

        new_grid_lon, new_grid_lat, tmp = ecco.resample_to_latlon(
            XC,
            YC,
            datamsked,
            new_grid_min_lat,
            new_grid_max_lat,
            new_grid_delta_deg,
            new_grid_min_lon,
            new_grid_max_lon,
            new_grid_delta_deg,
            nprocs_user=1,
            mapping_method='nearest_neighbor',
            radius_of_influence=radius_of_influence)

        print(tmp.shape)
        tmp = np.expand_dims(tmp, 0)
        if num[0] == 0:
            data_latlon_projection = tmp
        else:
            data_latlon_projection = np.append(data_latlon_projection, tmp, 0)

        print data_latlon_projection.shape

    # In[11]:

    lons_1d = new_grid_lon[0, :]
    lats_1d = new_grid_lat[:, 0]

    # In[12]:

    nz, ny, nx = np.shape(data_latlon_projection)
    print(lons_1d[0])
    print(lons_1d[-1])

    print(nx, ny, nz)

    # In[13]:

    plt.imshow(data_latlon_projection[0, :],
               origin='lower',
               vmin=0,
               vmax=.2,
               cmap='jet')

    # In[14]:

    plt.imshow(data_latlon_projection[25, :],
               origin='lower',
               vmin=0,
               vmax=.2,
               cmap='jet')

    # In[16]:

    foo = xr.DataArray(data_latlon_projection,
                       coords=[RC, lats_1d, lons_1d],
                       dims=['depth', 'latitude', 'longitude'])
    foo.isel(depth=1).plot()

    # In[17]:

    foo.attrs['xmin'] = np.min(new_grid_lon)
    foo.attrs['ymax'] = np.max(new_grid_lat)
    foo.attrs['spacing'] = new_grid_delta_deg
    foo.attrs['no_data'] = -9999.0
    foo.attrs['proj4'] = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
    foo.attrs['Projection'] = 'Lat Lon EPSG:4326'
    foo.attrs['Insitution'] = 'JPL'
    foo.attrs['original_filename'] = fname
    foo.attrs['author'] = 'Ou Wang'
    foo.attrs['product_version'] = '0.1'
    foo.attrs['nx'] = nx
    foo.attrs['ny'] = ny
    foo.attrs['nz'] = nz
    foo.attrs['units'] = unitnm
    foo.attrs['Conventions'] = 'CF-1.6'
    foo.attrs[
        'Metadata_Conventions'] = 'CF-1.6, Unidata Dataset Discovery v1.0, GDS v2.0'
    foo.attrs['cdm_data_type'] = 'Grid'
    foo.attrs['grid_method'] = 'bin average neighbor'
    foo.attrs['radius_of_influence'] = radius_of_influence
    foo.attrs['depth_units'] = 'meters'
    foo.name = varnm

    # In[18]:

    #foo

    # In[19]:

    foo.to_netcdf('/Volumes/data01/forum/user/owang/junk/output/' + varnm +
                  '/' + outfile + '.nc')
예제 #9
0
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')
# optional arguments:
#   cbar       - show the colorbar
#   cmin, cmax - color range min and max
#   fsize      - figure size in inches

# pull out surface salinity
tmp_plt = ecco_ds.SALT.isel(time=3, k=0)

# mask to NaN where hFacC is == 0
# syntax is actually "keep where hFacC is not equal to zero"
tmp_plt = tmp_plt.where(ecco_ds.hFacC.isel(k=0) != 0, np.nan)
tmp_plt

# In[19]:

ecco.plot_tiles(tmp_plt, cmin=32, cmax=35.5, show_colorbar=True)

# use `suptitle` (super title) to make a title over subplots.
plt.suptitle('SSS ' + str(ecco_ds.time[3].values)[0:7])

# #### lat-lon layout
#
# Another option of `plot_tiles` is to show tiles 7-12 rotated and lined up tiles 0-5
#
# > **Note:** *Rotation of tiles 7-13 is only for **plotting**.  These arrays are not rotated using this routine.  We'll show to how actually rotate these tiles in a later tutorial.*

# In[20]:

# optional arguments:
#   cbar       - show the colorbar
#   cmin, cmax - color range min and max
# ## Plotting fields from all 13 tiles simultaneously
#
# ### Plotting all 13 tiles with `plot_tiles`
#
# #### lat-lon-cap layout
# The custom `plot_tiles` routines in the ecco_v4_py package plot all 13 tiles of a field.  By default the routine will plot all of the tiles in the lat-lon-cap layout. The optional argument *cbar* tells the routine to add a colobar.  The optional arguments *cmin* and *cmax* specify the min and max values for the colorbar.

# In[12]:

tiles = v4.SSH.sel(time=1)

# optional arguments:
#   cbar       - show the colorbar
#   cmin, cmax - color range min and max
#   fsize      - figure size in inches
ecco.plot_tiles(tiles, cbar=True, cmin=-1.5, cmax=1.5, fsize=13)

# use `suptitle` (super title) to make a title over subplots.
plt.suptitle('SSH (m) ' + str(v4.tim[0].values)[0:7])

# #### lat-lon layout
#
# Another option of `plot_tiles` is to show tiles 8-13 rotated and lined up tiles 1-6.
#
# > **Note:** *Rotation of tiles 8-13 is only for **plotting**.  These arrays are not rotated using this routine.  We'll show to how actually rotate these tiles in the next tutorial.*

# In[13]:

tiles = v4.SSH.sel(time=1)

# new optional argument:
grid_dir = ECCO_dir + 'nctiles_grid/'

## load the grid
grid = xr.open_dataset(grid_dir + 'ECCOv4r3_grid.nc')
grid


# Let's plot two of the model grid parameter fields ``hFacC`` (tracer cell thickness factor) and ``rA`` (grid cell surface area)
# 
# First we plot ``hFac``:

# In[5]:


ecco.plot_tiles(grid.hFacC.sel(k=0), show_colorbar=True, cmap='gray');


# In[6]:


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]:
예제 #13
0
## 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'
ssh_global_mean.plot()
예제 #14
0
YC_da = YC_da.astype(array_precision)
effective_radius = effective_radius.astype(array_precision)

model_grid = xr.merge((XC_da, YC_da, effective_grid_radius_da))
model_grid.attrs['name'] = 'ECCO_llc90'
model_grid.attrs['type'] = 'llc'

model_grid = add_metadata_to_model_grid(model_grid)
encoding = make_model_grid_encodings(model_grid, netcdf_fill_value)

print(model_grid)
model_grid.to_netcdf(str(grid_output_dir) + '/ECCO_llc90_demo.nc',\
                     encoding=encoding)

#%%
ecco.plot_tiles(model_grid.XC)
ecco.plot_tiles(model_grid.effective_grid_radius, show_colorbar=True)

#%% GRID 4: fixed lat-lon-cap-90

llc270_dir = Path(source_dir / 'llc270')

tile_files = list(llc270_dir.glob('tile*mitgrid*'))
print(tile_files)

ny = [270 * 3, 270 * 3, 90 * 3, 90 * 3, 90 * 3]
nx = [90 * 3, 90 * 3, 90 * 3, 270 * 3, 270 * 3]

js = np.arange(270, dtype=np.int16)
ts = np.arange(13, dtype=np.int16)
output_dir = Path('/home/ifenty/tmp/time-invariant_20210317/')
output_dir.mkdir(parents=True, exist_ok=True)

#%%
plt.close('all')

ecco_grid = xr.open_dataset(ecco_grid_dir / 'GRID_GEOMETRY_ECCO_V4r4_native_llc0090.nc')

#%%
xx_diffkr = ecco.read_llc_to_tiles(mixing_dir, 'xx_diffkr.effective.0000000129.data', nk=50)
xx_diffkr_DA = xr.ones_like(ecco_grid.hFacC)
xx_diffkr_DA.name = 'DIFFKR'
xx_diffkr_DA.attrs = dict()
xx_diffkr_DA.values = xx_diffkr
xx_diffkr_DA = xx_diffkr_DA.where(ecco_grid.hFacC > 0)
ecco.plot_tiles(xx_diffkr_DA.isel(k=10), layout='latlon',rotate_to_latlon=True, show_colorbar=True,
                show_tile_labels=(False))
print(xx_diffkr_DA )


#%%
xx_kapgm = ecco.read_llc_to_tiles(mixing_dir, 'xx_kapgm.effective.0000000129.data', nk=50)
xx_kapgm_DA = xr.ones_like(ecco_grid.hFacC)
xx_kapgm_DA.name = 'KAPGM'
xx_kapgm_DA.attrs = dict()
xx_kapgm_DA.values = xx_kapgm
xx_kapgm_DA = xx_kapgm_DA.where(ecco_grid.hFacC > 0)
ecco.plot_tiles(xx_kapgm_DA.isel(k=10), layout='latlon',rotate_to_latlon=True, show_colorbar=True, show_tile_labels=(False),cmap='jet')
print(xx_kapgm_DA)

#%%
xx_kapredi = ecco.read_llc_to_tiles(mixing_dir, 'xx_kapredi.effective.0000000129.data', nk=50)
                          'OCEAN_3D_MIXING_COEFFS_ECCO_V4r4_native_llc0090.nc')
mix_ll = xr.open_dataset(dataset_dir /
                         'OCEAN_3D_MIXING_COEFFS_ECCO_V4r4_latlon_0p50deg.nc')

#%%
kl = 5
tmp = mix_llc['KAPGM'].isel(k=kl)
cmap = copy.copy(cmocean.cm.dense)

cmap.set_bad(color='dimgray')

cmin = tmp.min()
cmax = tmp.max()
plt.close('all')
tb = ecco.plot_tiles(tmp, cmap=cmap, fig_num=1, \
                     show_tile_labels=False,\
                     cmin=cmin, cmax=cmax)

#%%
shortname_tmp = mix_llc.metadata_link.split('ShortName=')[1]
print(shortname_tmp)

tb_filename = shortname_tmp + '.jpg'

fig_ref = tb[0]
fig_ref.set_size_inches(thumbnail_width_to_height_ratio * thumbnail_height,
                        thumbnail_height)

fname = dataset_dir / tb_filename
#plt.suptitle(f'ECCO V4r4 Gent-McWilliams diffusivity coefficient at {np.round(mix_llc.Z.isel(k=kl).values)}m')
예제 #17
0
# There are several ways of doing the above calculations.  Since this is the first tutorial with actual calcuations, we'll present a few different approaches for getting to the same answer.
#
# #### Part 1: $A_{\text{global ocean}}$
#
# Let's start on the simplest quantity, the global ocean surface area $A_{\text{global ocean}}$.  Our calculation uses $SSH$ which is a 'c' point variable.  The surface area of tracer grid cells is provided by the model grid parameter $RAC$.  $RAC$ is a two-dimensional field that is defined over all model grid points, including land.  To calculate the total ocean surface area we therefore need to ignore the area contributions from land. The wet/dry mask at 'c' points is provided by the $land_c$ variable: 1 for wet cells, 0 for dry cells.  Because this wet/dry mask varies with depth, $land_c$ is three-dimensional.
#
# To illustrate the depth dependence of $land_c$ here are the wet/dry masks for the uppermost grid cell (centered at z=5m) and the 35th grid cell in teh vertical (centered at z=1634 m):

# In[3]:

# select the uppermost grid cell, k = 1
k_level = 1
ecco.plot_tiles(v4.land_c.sel(k=k_level),
                layout='latlon',
                rotate_to_latlon=True,
                user_cmap='ocean',
                cmin=0,
                cmax=1.05,
                tile_labels=False,
                cbar=True)

# select out the model depth at k=1, round the number and convert to string.
z = np.array_str((np.round(v4.RC.sel(k=k_level).values)))

plt.suptitle('Wet/dry mask for k = ' + str(k_level) + ', z = ' + z + 'm')

# In[4]:

# select the 35th grid cell, k = 35
k_level = 35
ecco.plot_tiles(v4.land_c.sel(k=k_level),
                layout='latlon',
예제 #18
0

# In[10]:


# the resulting ocean_mask variable is a 2D DataArray because we only loaded 1 vertical level of the model grid
print(type(ocean_mask))
print((ocean_mask.dims))


# In[11]:


plt.figure(figsize=(12,5), dpi= 90)

ecco.plot_tiles(ocean_mask.isel(k=0),layout='latlon', rotate_to_latlon=True)

# select out the model depth at k=1, round the number and convert to string.
z = str((np.round(ecco_monthly_ds.Z.values[0])))
plt.suptitle('Wet (1) /dry (0) mask for k=' + str(0) + ',   z=' + z + 'm');


# To calculate $A_{\text{global ocean}}$ we must apply the surface wet/dry mask to $rA$.  

# In[12]:


# Method 1: the array index method, []
#           select land_c at k index 0
total_ocean_area = np.sum(ecco_monthly_ds.rA*ocean_mask[0,:])
예제 #19
0
    vars_to_load=['oceTAUX', 'oceTAUY'],
    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)

# In[21]:

tmp = ecco_ds.oceTAUX.isel(time=0)
tmp_masked = np.where(ecco_ds.maskC.isel(k=0) == 1, tmp, np.nan)
ecco.plot_tiles(tmp_masked)

# We can sees the expected positive zonal wind stress in tiles 0-5 because the x-y coordinates of tiles 0-5 happen to approximately line up with the meridians and parallels of the lat-lon grid.  Importantly,  for tiles 7-12 wind stress in the +x direction corresponds to mainly wind stress in the SOUTH direction.  To see the zonal wind stress in tiles 7-12 one has to plot ``oceTAUY`` and recognize that for those tiles positive values correspond with wind stress in the tile's +y direction, which is approximately east-west.  To wit,

# In[22]:

tmp = ecco_ds.oceTAUY.isel(time=0)
tmp_masked = np.where(ecco_ds.maskC.isel(k=0) == 1, tmp, np.nan)
ecco.plot_tiles(tmp_masked)

# Great, but if you want have a normal recognizable map of zonal and meridional wind stress, we need to determine the zonal and meridional components of these vectors.

# ### Vector rotation
#
# For vector rotation we leverage the very useful XGCM package (https://xgcm.readthedocs.io/en/latest/).
#
예제 #20
0
def create_thumbnails(dataset_base_dir,\
                      product_type,\
                      grouping_to_process,\
                      thumbnail_height,\
                      thumbnail_width_to_height_ratio,\
                      output_dir):

    if not output_dir.exists():
        try:
            output_dir.mkdir(parents=True, exist_ok=True)
        except:
            print('cannot make %s ' % output_dir)

    # grouping
    print('dataset_base_dir ', dataset_base_dir)
    groupings = list(dataset_base_dir.glob('*/'))

    #pprint(groupings)
    print('\n... found groupings ')
    for grouping_i, grouping in enumerate(groupings):
        print(grouping_i, grouping.name)

    if grouping_to_process != 'all':
        grouping_to_process = int(grouping_to_process)

        groupings = [groupings[grouping_to_process]]

    plt.close('all')
    for grouping_i, grouping in enumerate(groupings):
        print('\n --- grouping to process', grouping.name)

        glob_str = grouping.name + "*ECCO*.nc"

        # find list of files
        files_in_grouping = np.sort(list(grouping.glob(glob_str)))

        print('processing ', grouping.name)
        print('\n')

        tmp_ds = xr.open_dataset(files_in_grouping[0])

        shortname_tmp = tmp_ds.metadata_link.split('ShortName=')[1]
        print(shortname_tmp)

        tb_filename = shortname_tmp + '.jpg'

        data_vars = list(tmp_ds.data_vars)
        print(data_vars)

        which_data_var = 0
        which_k_level = 0

        data_var = data_vars[0]

        if 'FRESH_FLUX' in shortname_tmp:
            data_var = 'oceFWflx'
        elif 'HEAT_FLUX' in shortname_tmp:
            data_var = 'oceQnet'
        elif 'BOLUS_STREAMFUNCTION' in shortname_tmp:
            which_k_level = 1
        elif 'SALT_PLUME' in shortname_tmp:
            which_k_level = 1
        elif 'DENS_STRAT' in shortname_tmp:
            data_var = 'RHOAnoma'

        tmp_ds_dims = set(tmp_ds.dims)
        print('tmp_ds dims ', tmp_ds_dims)

        if product_type == 'native':

            dims_3D = {'k', 'k_u', 'k_l'}
            dims_2D = {'i', 'i_g', 'j', 'j_g'}

            if len(dims_3D.intersection(tmp_ds_dims)) > 0:
                ds_dim = 3

            elif len(dims_2D.intersection(tmp_ds_dims)) > 0:
                ds_dim = 2

        elif product_type == 'latlon':
            dims_3D = {'Z'}
            dims_2D = {'latitude', 'longitude'}

            if len(dims_3D.intersection(tmp_ds_dims)) > 0:
                ds_dim = 3

            elif len(dims_2D.intersection(tmp_ds_dims)) > 0:
                ds_dim = 2

        print('data_var dimension ', ds_dim)
        fname = output_dir / tb_filename

        print(data_var, shortname_tmp)
        if ds_dim == 3:
            tmp = tmp_ds[data_var][0, which_k_level, :]

        elif ds_dim == 2:
            tmp = tmp_ds[data_var][0, :]

        print('data_var dimension ', tmp.shape)

        cmin = np.nanmin(tmp)
        cmax = np.nanmax(tmp)
        print('color lims')
        print(shortname_tmp, cmin, cmax)

        # default
        cmap = copy.copy(plt.get_cmap('jet'))
        if product_type == 'native':
            cmap.set_bad(color='dimgray')

        if ('STRESS' in shortname_tmp) or \
           ('FLUX' in shortname_tmp) or\
           ('VEL' in shortname_tmp) or\
           ('TEND' in shortname_tmp) or\
           ('BOLUS' in shortname_tmp):

            fac = 0.8
            if ('FRESH_FLUX' in shortname_tmp) or\
                ('MOMENTUM_TEND' in shortname_tmp):
                fac = 0.25

            cmin, cmax = even_cax(cmin, cmax, fac)
            print(cmin, cmax)

            cmap = copy.copy(cmocean.cm.balance)  #plt.get_cmap('bwr'))
            if product_type == 'native':
                cmap.set_bad(color='dimgray')

        elif ('TEMP_SALINITY' in shortname_tmp):
            cmap = copy.copy(cmocean.cm.thermal)
            if product_type == 'native':
                cmap.set_bad(color='dimgray')

        elif ('MIXED_LAYER' in shortname_tmp):

            cmap = copy.copy(cmocean.cm.deep)

            if product_type == 'native':
                cmap.set_bad(color='dimgray')
            cmin = 0
            cmax = 250

        elif ('SEA_ICE_CONC' in shortname_tmp):

            cmap = copy.copy(cmocean.cm.ice)
            if product_type == 'native':
                cmap.set_bad(color='dimgray')
            cmin = 0
            cmax = 1

        elif 'DENS_STRAT' in shortname_tmp:
            cmap = copy.copy(cmocean.cm.dense)
            if product_type == 'native':
                cmap.set_bad(color='dimgray')

        if product_type == 'native':

            tb = ecco.plot_tiles(tmp, cmap=cmap, fig_num=grouping_i, \
                                 show_tile_labels=False,\
                                     cmin=cmin, cmax=cmax)

            fig_ref = tb[0]
        elif product_type == 'latlon':
            fig_ref = plt.figure(num=grouping_i, figsize=[8, 8])
            ax = plt.axes(projection=ccrs.Robinson(central_longitude=-66.5))
            ecco.plot_global(tmp_ds[data_var].longitude,\
                             tmp_ds[data_var].latitude,\
                             tmp,\
                             4326, cmin, cmax, ax,\
                             show_grid_labels=False, cmap=cmap)

        fig_ref.set_size_inches(
            thumbnail_width_to_height_ratio * thumbnail_height,
            thumbnail_height)

        print(fname)
        plt.savefig(fname,
                    dpi=100,
                    facecolor='w',
                    bbox_inches='tight',
                    pad_inches=0.05)
    pott_model_grid_ann_xy[k, :] = data_model_projection

    orig_data = s_clim_1995_2017_ann_DA[k, :].values

    data_model_projection = ec.transform_to_target_grid(source_indices_within_target_radius_i, \
                                                        num_source_indices_within_target_radius_i, \
                                                        nearest_source_index_to_target_index_i, \
                                                        orig_data, model_grid.XC.shape)

    s_model_grid_ann_xy[k, :] = data_model_projection

#%%
if model_grid_type == 'llc':
    plt.close('all')
    ecco.plot_tiles(pott_model_grid_xy[0,9,:], \
                    layout='latlon', rotate_to_latlon=True, show_tile_labels=False)

    plt.close('all')
    ecco.plot_tiles(pott_model_grid_xy[0,9,:], \
                    layout='latlon', rotate_to_latlon=True, show_tile_labels=False)

#%% save intermediate solution to disk
# WOA mapped to model grid in x and y but not depth

pott_ann_model_grid_xy_DA = xr.DataArray(pott_model_grid_ann_xy,
                                         dims=['Z', 'tile', 'j', 'i'])
pott_ann_model_grid_xy_DA = pott_ann_model_grid_xy_DA.assign_coords(
    {'j': model_grid.j})
pott_ann_model_grid_xy_DA = pott_ann_model_grid_xy_DA.assign_coords(
    {'i': model_grid.i})
pott_ann_model_grid_xy_DA = pott_ann_model_grid_xy_DA.assign_coords(
    

    mg5 = []

    return XG_t, YG_t, RAZ_t

#%%
if __name__ == '__main__':

    llc = 90
    grid_dir = '/Volumes/ECCO_BASE/ECCO_v4r3/grid_llc90'

    XG, YG, RAZ = extract_G_grid_fields_from_mitgrid_files_as_tiles(llc, grid_dir)
    
    #%%
    ds = xr.Dataset({'XG': (['tile','j_g','i_g'], XG),
                     'YG': (['tile','j_g','i_g'], YG),
                     'RAZ': (['tile','j_g','i_g'], RAZ)}, 
                    coords={'tile': range(1,14),
                            'j_g': (('j_g',), range(1,92), {'axis':'Y',}),
                            'i_g': (('i_g',), range(1,92), {'axis':'X',})})
    

    ds.to_netcdf(grid_dir + '/' + 'G_grid_fields_as_tiles.nc')

    
    #%%
    sys.path.append('/Users/ifenty/git_repo_mine/ECCOv4-py/')
    import ecco_v4_py as ecco
    ecco.plot_tiles(ds.YG)