Exemplo n.º 1
0
def read_nemo_oneatatime(fn_nemo_data, fn_nemo_domain, obs, landmask, chunks):
    print("analyse_ssh_hourly: a")

    file_list = glob.glob(fn_nemo_data)

    file = file_list[0]
    nemo = coast.NEMO(file, fn_nemo_domain, chunks=chunks).dataset

    ind2D = gu.nearest_indices_2D(nemo.longitude,
                                  nemo.latitude,
                                  obs.longitude,
                                  obs.latitude,
                                  mask=landmask)
    print("analyse_ssh_hourly: b")
    nemo_list = []

    for ff in range(0, len(file_list)):
        file = file_list[ff]
        print(file)
        nemo = coast.NEMO(file, fn_nemo_domain, chunks=chunks).dataset
        nemo = nemo['ssh']
        nemo_ext = nemo.isel(x_dim=ind2D[0], y_dim=ind2D[1]).load()
        nemo_ext = nemo_ext.swap_dims({'dim_0': 'port'})
        nemo_list.append(nemo_ext)

    print("analyse_ssh_hourly: c")

    nemo = xr.merge(nemo_list)

    print('d')

    return nemo
Exemplo n.º 2
0
def read_data_for_regridding_nemo(fn_nemo_data, fn_nemo_domain):
    ''' For reading NEMO data into the right dataset format for XESMF.
    Ouput from this function must be an xarray.Dataset or xarray.DataArray 
    object. Longitude and latitude variables must be named 'lon' and 'lat' 
    respectively. If data is on rectilinear grid, there should also be 
    dimensions named 'lon' and 'lat'. f using, "mask" should be 1s over the 
    ocean and 0 over the land.
    '''
    import coast

    # Use coast to create NEMO object and extract dataset. Only keep variables
    # of interest.
    ds = coast.NEMO(fn_nemo_data,
                    fn_nemo_domain,
                    multiple=True,
                    chunks={'season': 1})
    ds = ds.dataset[['ssh', 'temperature', 'salinity']]

    # Rename longitude and latitude for XESMF
    ds = ds.rename({'longitude': 'lon', 'latitude': 'lat'})

    # Create a landmask and place into dataset using y_dim and x_dim dimensions
    # x_dim and y_dim are dimensions from the NEMO dataset object
    domain = xr.open_dataset(fn_nemo_domain, chunks={})
    ds['mask'] = (['y_dim', 'x_dim'], domain.top_level[0].values.astype(bool))

    return ds
def read_harmonics_model_nemo(fn_nemo_harmonics, fn_nemo_domain, constituents):
    '''
    For reading and formatting model data. Modify or replace as necessary.
    If output from this fucntion is correct then the validation script will
    continue to work. Data output should be in the form of a single xarray
    Dataset object. This Dataset must have the minimum form and variable names:
    
    Dimensions:           (constituent: 2, x_dim: 369, y_dim: 297)
    Coordinates:
        latitude          (y_dim, x_dim) float32 ...
        longitude         (y_dim, x_dim) float32 ...
    Data variables:
        amplitude         (constituent, y_dim, x_dim) float64 0.7484 0.7524 ... 0.0
        phase             (constituent, y_dim, x_dim) float64 -1.8 -1.8 ... -0.0
        constituent_name  (constituent) <U3 'M2' 'S2'
        landmask          (y_dim, x_dim) bool False False False ... True True True
        
    Here, x_dim and y_dim are the longitude and latitude axes respectively. The
    default script determines the landmask from bathymetry. If not available,
    set this variable to all False (tell it there is no land -- a Lie!!).
    '''
    # Read in nemo harmonics using COAsT
    nemo = coast.NEMO(fn_nemo_harmonics, fn_nemo_domain)
    nemo = nemo.harmonics_combine(['M2', 'S2'])

    # Convert to amplitude and phase.
    nemo.harmonics_convert()

    # Define landmask as being everywhere that the depth is 0 (or "shallower")
    landmask = nemo.bathy_metry <= 0
    landmask = landmask.squeeze()
    nemo.dataset['landmask'] = landmask

    return nemo.dataset
Exemplo n.º 4
0
def read_nemo_ssh(fn_nemo_data, fn_nemo_domain, chunks):
    print("analyse_ssh_hourly: Reading NEMO data")
    nemo = coast.NEMO(fn_nemo_data,
                      fn_nemo_domain,
                      multiple=True,
                      chunks=chunks).dataset
    print("analyse_ssh_hourly: Done")
    return nemo[['ssh', 'time_instant']]
 def read_nemo_data(self, fn_nemo_data, fn_nemo_domain, chunks):
     nemo = coast.NEMO(fn_nemo_data,
                       fn_nemo_domain,
                       multiple=True,
                       chunks={}).dataset
     nemo = nemo['ssh']
     print("analyse_monthly_ssh: NEMO data read")
     return nemo
Exemplo n.º 6
0
def read_data_input_nemo(fn_nemo_data, fn_nemo_domain):
    ''' For reading multiple NEMO data files to use for analysis. Uses COAsT
    to create the xarray dataset.'''
    data = coast.NEMO(fn_nemo_data,
                      fn_nemo_domain,
                      multiple=True,
                      chunks='auto').dataset
    data = data[['temperature', 'ssh', 'salinity']]
    return data
def read_model_nemo(fn_nemo_data,
                    fn_nemo_domain,
                    depth_levels='all',
                    variables_to_extract='all'):
    ''' Routine for reading NEMO model data using COAsT.
    This should return numpy arrays of longitude and latitude. This can be done
    manually or by reading data from another file and extracting lists of 
    locations'''

    # Read NEMO data into a COAsT object (correct format)
    model_data = coast.NEMO(fn_nemo_data,
                            fn_nemo_domain,
                            grid_ref='t-grid',
                            multiple=True,
                            chunks={'time_counter': 1})

    # Extract the xarray dataset and desired variables
    model_data = model_data.dataset

    # If dataset has a depth dimension and user has specified depth levels
    if depth_levels != 'all' and 'z_dim' in model_data.dims:
        model_data = model_data.isel(z_dim=depth_levels)

    # If only specified variables are wanted
    if variables_to_extract != 'all':
        model_data = model_data[variables_to_extract]

    # Create a landmask and place into dataset
    # Here I create a landmask from the top_level variable in the domain file.
    # This should be named 'landmask'.
    domain = xr.open_dataset(fn_nemo_domain, chunks={})
    model_data['landmask'] = (['y_dim', 'x_dim'],
                              ~domain.top_level[0].values.astype(bool))

    # If no mask needed, set to None (uncomment below)
    # model_data['landmask'] = None
    return model_data
Exemplo n.º 8
0
'''

# Begin by importing coast and other packages
import coast
import datetime
import numpy as np
import matplotlib.pyplot as plt

# And by defining some file paths
fn_nemo_dat = './example_files/COAsT_example_NEMO_data.nc'
fn_nemo_dom = './example_files/COAsT_example_NEMO_domain.nc'
fn_tidegauge = './example_files/tide_gauges/lowestoft-p024-uk-bodc'
fn_tidegauge_mult = './example_files/tide_gauges/l*'

# We need to load in a NEMO object for doing NEMO things.
nemo = coast.NEMO(fn_nemo_dat, fn_nemo_dom, grid_ref='t-grid')

# And now we can load in our ALTIMETRY data. By default, TIDEGAUGE is set up
# to read in GESLA ASCII files. However, if no path is supplied, then the
# object's dataset will be initialised as None. Custom data can then be loaded
# if desired, as long as it follows the data formatting for TIDEGAUGE. Here
# we load data between two specified dates:
date0 = datetime.datetime(2007, 1, 10)
date1 = datetime.datetime(2007, 1, 16)
tidegauge = coast.TIDEGAUGE(fn_tidegauge, date_start=date0, date_end=date1)

# Before comparing our observations to the model, we will interpolate a model
# variable to the same time and geographical space as the tidegauge. This is
# done using the obs_operator() method:
tidegauge.obs_operator(nemo, mod_var_name='ssh', time_interp='nearest')
Exemplo n.º 9
0
import coast
import numpy as np
import xarray as xr
import dask
import matplotlib.pyplot as plt
import matplotlib.colors as colors  # colormap fiddling

#################################################
#%%  Loading  data
#################################################

dir_nam = "/projectsa/COAsT/NEMO_example_data/MO_INDIA/"
fil_nam = "ind_1d_cat_20180101_20180105_25hourm_grid_T.nc"
dom_nam = "domain_cfg_wcssp.nc"


sci_t = coast.NEMO(dir_nam + fil_nam, \
        dir_nam + dom_nam, grid_ref='t-grid', multiple=False)

#%% Plot
fig = plt.figure()

plt.pcolormesh(sci_t.dataset.longitude, sci_t.dataset.latitude,
               sci_t.dataset.temperature.isel(t_dim=0).isel(z_dim=0))
plt.xlabel('longitude')
plt.ylabel('latitude')
plt.title('WCSSP India SST')
plt.colorbar()
plt.show()
fig.savefig('WCSSP_India_SST.png', dpi=120)
import matplotlib.colors as colors  # colormap fiddling

#################################################
#%%  Loading  data
#################################################

config = 'AMM15'
dir_nam = "/projectsa/NEMO/gmaya/2013p2/"
fil_nam = "20130415_25hourm_grid_T.nc"
dom_nam = "/projectsa/NEMO/gmaya/AMM15_GRID/amm15.mesh_mask.cs3x.nc"

chunks = {"x": 10, "y": 10, "time_counter": 10}

sci_t = coast.NEMO(dir_nam + fil_nam,
                   dom_nam,
                   grid_ref='t-grid',
                   multiple=False,
                   chunks=chunks)

# create an empty w-grid object, to store stratification
sci_w = coast.NEMO(fn_domain=dom_nam, grid_ref='w-grid', chunks=chunks)

print('* Loaded ', config, ' data')

#################################################
#%% subset of data and domain ##
#################################################
# Pick out a North Sea subdomain
print('* Extract North Sea subdomain')
ind_sci = sci_t.subset_indices([51, -4], [62, 15])
sci_nwes_t = sci_t.isel(y_dim=ind_sci[0],
try:
    config = 'AMM60'
    dir_AMM60 = "/projectsa/COAsT/NEMO_example_data/AMM60/"
    fil_nam_AMM60 = "AMM60_1d_20100704_20100708_grid_T.nc"
    mon = 'July'
    #mon = 'Feb'

    if mon == 'July':
        fil_names_AMM60 = "AMM60_1d_201007*_grid_T.nc"
    elif mon == 'Feb':
        fil_names_AMM60 = "AMM60_1d_201002*_grid_T.nc"

    chunks = {"x": 10, "y": 10, "time_counter": 10}
    sci_t = coast.NEMO(dir_AMM60 + fil_names_AMM60,
                       dir_AMM60 + "mesh_mask.nc",
                       grid_ref='t-grid',
                       multiple=True,
                       chunks=chunks)

    # create an empty w-grid object, to store stratification
    sci_w = coast.NEMO(fn_domain=dir_AMM60 + "mesh_mask.nc", grid_ref='w-grid')

# OR load in AMM7 example data
except:
    config = 'AMM7'
    dn_files = "./example_files/"

    if not os.path.isdir(dn_files):
        print(
            "please go download the examples file from https://linkedsystems.uk/erddap/files/COAsT_example_files/"
        )
Exemplo n.º 12
0
import sys
sys.path.append('/home/users/dbyrne/code/COAsT/')
import coast
import coast.general_utils as gu
import xarray as xr
import numpy as np

fn_nemo_data = "/gws/nopw/j04/jmmp_collab/CO9_AMM15/outputs/hourly/*.nc"
fn_nemo_domain = "/gws/nopw/j04/jmmp_collab/CO9_AMM15/inputs/CO7_EXACT_CFG_FILE.nc"
fn_tideanal = "/gws/nopw/j04/jmmp_collab/CO9_AMM15/obs/tideanal.edited.nc"
fn_out = "/gws/nopw/j04/jmmp_collab/CO9_AMM15/analysis/tideanal_extracted.nc"

nemo = coast.NEMO(fn_nemo_data, fn_nemo_domain, multiple=True, chunks='auto').dataset
tana = xr.open_dataset(fn_tideanal)
mask = nemo.bottom_level==0

ind2D = gu.nearest_indices_2D(nemo.longitude, nemo.latitude, 
                              tana.longitude, tana.latitude, mask)

nemo = nemo.isel(x_dim = ind2D[0], y_dim = ind2D[1])
nemo.compute()
nemo = nemo.swap_dims({'dim_0':'port'})
nemo = nemo.swap_dims({'t_dim':'time'})

nemo.to_netcdf(fn_out)
Exemplo n.º 13
0
import matplotlib.pyplot as plt
#import matplotlib.colors as colors # colormap fiddling

#################################################
#%%  Loading and initialising methods ##
#################################################
    
dir_nam = "/projectsa/anchor/NEMO/AMM60/"
fil_nam = "AMM60_1h_20100818_20100822_NorthSea.nc"
dom_nam = "/projectsa/FASTNEt/jelt/AMM60/mesh_mask.nc"

dir_nam = '/projectsa/NEMO/jelt/AMM60_ARCHER_DUMP/AMM60smago/EXP_NSea/OUTPUT/'
fil_nam = 'AMM60_1h_20120204_20120208_NorthSea.nc'

chunks = {"x":10, "y":10, "time_counter":10}
sci_w = coast.NEMO(dir_nam + fil_nam, 
                 dom_nam, grid_ref='w-grid', multiple=True, chunks=chunks)

sci_w.dataset = sci_w.dataset.swap_dims({'depthw':'z_dim'})
#################################################
#%% subset of data and domain ##
#################################################
# Pick out a North Sea subdomain
ind_sci = sci_w.subset_indices([51,-4], [60,15])
sci_nwes_w = sci_w.isel(y_dim=ind_sci[0], x_dim=ind_sci[1]) #nwes = northwest europe shelf

#%% Compute a diffusion from w-vel
Kz = (sci_nwes_w.dataset.wo * sci_nwes_w.dataset.e3_0).sum(dim='z_dim').mean(dim='t_dim')

# plot map      
lon =  sci_nwes_w.dataset.longitude.squeeze()
lat =  sci_nwes_w.dataset.latitude.squeeze()
Exemplo n.º 14
0
 def read_nemo_ssh(self, fn_nemo_data, fn_nemo_domain, chunks):
     nemo = coast.NEMO(fn_nemo_data,
                       fn_nemo_domain,
                       multiple=True,
                       chunks=chunks).dataset
     return nemo['ssh']
    def __init__(self,
                 fn_nemo_data,
                 fn_nemo_domain,
                 fn_en4,
                 fn_out,
                 surface_def=2,
                 bottom_def=10,
                 regional_masks=[],
                 region_names=[],
                 nemo_chunks={'time_counter': 50},
                 bathymetry=None):

        print('0', flush=True)

        nemo = coast.NEMO(fn_nemo_data,
                          fn_nemo_domain,
                          multiple=True,
                          chunks=nemo_chunks)
        nemo_mask = nemo.dataset.bottom_level == 0
        nemo.dataset = nemo.dataset.rename({'t_dim': 'time'})
        if bathymetry is not None:
            nemo.dataset = nemo.dataset[[
                'votemper_top', 'vosaline_top', 'votemper_bot', 'vosaline_bot'
            ]]
        else:
            nemo.dataset = nemo.dataset[['votemper_top', 'vosaline_top']]

        print('a', flush=True)

        en4 = coast.PROFILE()
        en4.read_EN4(fn_en4, multiple=True)

        # Get obs in box
        lonmax = np.nanmax(nemo.dataset['longitude'])
        lonmin = np.nanmin(nemo.dataset['longitude'])
        latmax = np.nanmax(nemo.dataset['latitude'])
        latmin = np.nanmin(nemo.dataset['latitude'])
        ind = coast.general_utils.subset_indices_lonlat_box(
            en4.dataset['longitude'], en4.dataset['latitude'], lonmin, lonmax,
            latmin, latmax)[0]
        en4 = en4.isel(profile=ind)
        print('b', flush=True)

        # Get obs time slice
        n_nemo_time = nemo.dataset.dims['time']
        nemo.dataset.time.load()
        en4.dataset.time.load()
        nemo_time = pd.to_datetime(nemo.dataset.time.values)
        en4_time = pd.to_datetime(en4.dataset.time.values)
        time_max = max(nemo_time) + timedelta(hours=1)
        time_min = min(nemo_time) - timedelta(hours=1)
        time_ind0 = en4_time <= time_max
        time_ind1 = en4_time >= time_min
        time_ind = np.logical_and(time_ind0, time_ind1)
        en4 = en4.isel(profile=time_ind)

        # Get model indices
        en4_time = pd.to_datetime(en4.dataset.time.values)
        ind2D = gu.nearest_indices_2D(nemo.dataset.longitude,
                                      nemo.dataset.latitude,
                                      en4.dataset.longitude,
                                      en4.dataset.latitude,
                                      mask=nemo_mask)

        print('c', flush=True)

        # Estimate EN4 SST as mean of top levels
        surface_ind = en4.dataset.depth <= surface_def

        sst_en4 = en4.dataset.potential_temperature.where(surface_ind, np.nan)
        sss_en4 = en4.dataset.practical_salinity.where(surface_ind, np.nan)

        sst_en4 = sst_en4.mean(dim="z_dim", skipna=True).load()
        sss_en4 = sss_en4.mean(dim="z_dim", skipna=True).load()

        print('d', flush=True)

        # Bottom values
        if bathymetry is not None:
            bathy_pts = bathymetry.isel(
                x_dim=ind2D[0], y_dim=ind2D[1]).swap_dims({'dim_0': 'profile'})
            bottom_ind = en4.dataset.depth >= (bathy_pts - bottom_def)

            sbt_en4 = en4.dataset.potential_temperature.where(
                bottom_ind, np.nan)
            sbs_en4 = en4.dataset.practical_salinity.where(bottom_ind, np.nan)

            sbt_en4 = sbt_en4.mean(dim="z_dim", skipna=True).load()
            sbs_en4 = sbs_en4.mean(dim="z_dim", skipna=True).load()

        print('e', flush=True)

        # For every EN4 profile, determine the nearest model time index
        # If more than t_crit away from nearest, then discard it
        n_prof = en4.dataset.dims['profile']

        sst_e = np.zeros(n_prof) * np.nan
        sss_e = np.zeros(n_prof) * np.nan
        sst_ae = np.zeros(n_prof) * np.nan
        sss_ae = np.zeros(n_prof) * np.nan
        crps_tem_2 = np.zeros(n_prof) * np.nan
        crps_sal_2 = np.zeros(n_prof) * np.nan
        crps_tem_4 = np.zeros(n_prof) * np.nan
        crps_sal_4 = np.zeros(n_prof) * np.nan
        crps_tem_6 = np.zeros(n_prof) * np.nan
        crps_sal_6 = np.zeros(n_prof) * np.nan

        sbt_e = np.zeros(n_prof) * np.nan
        sbs_e = np.zeros(n_prof) * np.nan
        sbt_e = np.zeros(n_prof) * np.nan
        sbs_e = np.zeros(n_prof) * np.nan

        # CRPS

        x_dim_len = nemo.dataset.dims['x_dim']
        y_dim_len = nemo.dataset.dims['y_dim']

        n_r = nemo.dataset.dims['y_dim']
        n_c = nemo.dataset.dims['x_dim']
        regional_masks = regional_masks.copy()
        region_names = region_names.copy()
        regional_masks.append(np.ones((n_r, n_c)))
        region_names.append('whole_domain')
        n_regions = len(regional_masks)
        n_season = 5

        print('Starting analysis')

        for tii in range(0, n_nemo_time):

            print(nemo_time[tii], flush=True)

            time_diff = np.abs(nemo_time[tii] -
                               en4_time).astype('timedelta64[m]')
            use_ind = np.where(time_diff.astype(int) < 30)[0]
            n_use = len(use_ind)

            if n_use > 0:

                tmp = nemo.isel(time=tii).dataset
                tmp.load()
                x_tmp = ind2D[0][use_ind]
                y_tmp = ind2D[1][use_ind]

                x_tmp = xr.where(x_tmp < x_dim_len - 7, x_tmp, np.nan)
                y_tmp = xr.where(y_tmp < y_dim_len - 7, y_tmp, np.nan)

                x_tmp = xr.where(x_tmp > 7, x_tmp, np.nan)
                y_tmp = xr.where(y_tmp > 7, y_tmp, np.nan)

                shared_mask = np.logical_or(np.isnan(x_tmp), np.isnan(y_tmp))
                shared_mask = np.where(~shared_mask)

                x_tmp = x_tmp[shared_mask].astype(int)
                y_tmp = y_tmp[shared_mask].astype(int)
                use_ind = use_ind[shared_mask].astype(int)

                n_use = len(use_ind)
                if n_use < 1:
                    continue

                tmp_pts = tmp.isel(x_dim=x_tmp, y_dim=y_tmp)
                sst_en4_tmp = sst_en4.values[use_ind]
                sss_en4_tmp = sss_en4.values[use_ind]
                sst_e[use_ind] = tmp_pts.votemper_top.values - sst_en4_tmp
                sss_e[use_ind] = tmp_pts.vosaline_top.values - sss_en4_tmp

                if bathymetry is not None:
                    sbt_en4_tmp = sbt_en4.values[use_ind]
                    sbs_en4_tmp = sbs_en4.values[use_ind]
                    sbt_e[use_ind] = tmp_pts.votemper_bot.values - sbt_en4_tmp
                    sbs_e[use_ind] = tmp_pts.vosaline_bot.values - sbs_en4_tmp

                nh_x = [
                    np.arange(x_tmp[ii] - 2, x_tmp[ii] + 3)
                    for ii in range(0, n_use)
                ]
                nh_y = [
                    np.arange(y_tmp[ii] - 2, y_tmp[ii] + 3)
                    for ii in range(0, n_use)
                ]
                nh = [
                    tmp.isel(x_dim=nh_x[ii], y_dim=nh_y[ii])
                    for ii in range(0, n_use)
                ]
                crps_tem_tmp = [
                    cu.crps_empirical(nh[ii].votemper_top.values.flatten(),
                                      sst_en4_tmp[ii])
                    for ii in range(0, n_use)
                ]
                crps_sal_tmp = [
                    cu.crps_empirical(nh[ii].vosaline_top.values.flatten(),
                                      sss_en4_tmp[ii])
                    for ii in range(0, n_use)
                ]
                crps_tem_2[use_ind] = crps_tem_tmp
                crps_sal_2[use_ind] = crps_sal_tmp

                nh_x = [
                    np.arange(x_tmp[ii] - 4, x_tmp[ii] + 5)
                    for ii in range(0, n_use)
                ]
                nh_y = [
                    np.arange(y_tmp[ii] - 4, y_tmp[ii] + 5)
                    for ii in range(0, n_use)
                ]
                nh = [
                    tmp.isel(x_dim=nh_x[ii], y_dim=nh_y[ii])
                    for ii in range(0, n_use)
                ]
                crps_tem_tmp = [
                    cu.crps_empirical(nh[ii].votemper_top.values.flatten(),
                                      sst_en4_tmp[ii])
                    for ii in range(0, n_use)
                ]
                crps_sal_tmp = [
                    cu.crps_empirical(nh[ii].vosaline_top.values.flatten(),
                                      sss_en4_tmp[ii])
                    for ii in range(0, n_use)
                ]
                crps_tem_4[use_ind] = crps_tem_tmp
                crps_sal_4[use_ind] = crps_sal_tmp

                nh_x = [
                    np.arange(x_tmp[ii] - 6, x_tmp[ii] + 7)
                    for ii in range(0, n_use)
                ]
                nh_y = [
                    np.arange(y_tmp[ii] - 6, y_tmp[ii] + 7)
                    for ii in range(0, n_use)
                ]
                nh = [
                    tmp.isel(x_dim=nh_x[ii], y_dim=nh_y[ii])
                    for ii in range(0, n_use)
                ]
                crps_tem_tmp = [
                    cu.crps_empirical(nh[ii].votemper_top.values.flatten(),
                                      sst_en4_tmp[ii])
                    for ii in range(0, n_use)
                ]
                crps_sal_tmp = [
                    cu.crps_empirical(nh[ii].vosaline_top.values.flatten(),
                                      sss_en4_tmp[ii])
                    for ii in range(0, n_use)
                ]
                crps_tem_6[use_ind] = crps_tem_tmp
                crps_sal_6[use_ind] = crps_sal_tmp

        print('Profile analysis done', flush=True)
        sst_ae = np.abs(sst_e)
        sss_ae = np.abs(sss_e)
        sbt_ae = np.abs(sbt_e)
        sbs_ae = np.abs(sbs_e)
        # Put everything into xarray dataset
        en4_season = get_season_index(sst_en4.time.values)

        # Regional Means
        reg_array = np.zeros((n_regions, n_season)) * np.nan
        is_in_region = [mm[ind2D[1], ind2D[0]] for mm in regional_masks]
        is_in_region = np.array(is_in_region, dtype=bool)

        ds = xr.Dataset(coords=dict(longitude=("profile",
                                               sst_en4.longitude.values),
                                    latitude=("profile",
                                              sst_en4.latitude.values),
                                    time=("profile", sst_en4.time.values),
                                    season_ind=("profile", en4_season)),
                        data_vars=dict(obs_sst=('profile', sst_en4.values),
                                       obs_sss=('profile', sss_en4.values),
                                       sst_err=("profile", sst_e),
                                       sss_err=("profile", sss_e),
                                       sst_abs_err=("profile", sst_ae),
                                       sss_abs_err=("profile", sss_ae),
                                       sst_crps2=("profile", crps_tem_2),
                                       sss_crps2=("profile", crps_sal_2),
                                       sst_crps4=("profile", crps_tem_4),
                                       sss_crps4=("profile", crps_sal_4),
                                       sst_crps6=("profile", crps_tem_6),
                                       sss_crps6=("profile", crps_sal_6)))

        season_names = ['All', 'DJF', 'MAM', 'JJA', 'SON']
        ds = ds.chunk({'profile': 10000})

        ds_mean = xr.Dataset(
            coords=dict(longitude=("profile", sst_en4.longitude.values),
                        latitude=("profile", sst_en4.latitude.values),
                        time=("profile", sst_en4.time.values),
                        season_ind=("profile", en4_season),
                        region_names=('region', region_names),
                        season=('season', season_names)),
            data_vars=dict(sst_me=(["region", "season"], reg_array.copy()),
                           sss_me=(["region", "season"], reg_array.copy()),
                           sst_mae=(["region", "season"], reg_array.copy()),
                           sss_mae=(["region", "season"], reg_array.copy()),
                           sst_crps2_mean=(["region",
                                            "season"], reg_array.copy()),
                           sss_crps2_mean=(["region",
                                            "season"], reg_array.copy()),
                           sst_crps4_mean=(["region",
                                            "season"], reg_array.copy()),
                           sss_crps4_mean=(["region",
                                            "season"], reg_array.copy()),
                           sst_crps6_mean=(["region",
                                            "season"], reg_array.copy()),
                           sss_crps6_mean=(["region",
                                            "season"], reg_array.copy())))

        if bathymetry is not None:
            ds_mean['sbt_me'] = (['region', 'season'], reg_array.copy())
            ds_mean['sbs_me'] = (['region', 'season'], reg_array.copy())
            ds_mean['sbt_mae'] = (['region', 'season'], reg_array.copy())
            ds_mean['sbs_mae'] = (['region', 'season'], reg_array.copy())

            ds['obs_sbt'] = (['profile'], sbt_en4.values)
            ds['obs_sbs'] = (['profile'], sbs_en4.values)
            ds['sbt_err'] = (['profile'], sbt_e)
            ds['sbs_err'] = (['profile'], sbs_e)
            ds['sbt_abs_err'] = (['profile'], sbt_ae)
            ds['sbs_abs_err'] = (['profile'], sbs_ae)

        for reg in range(0, n_regions):
            reg_ind = np.where(is_in_region[reg].astype(bool))[0]
            ds_reg = ds.isel(profile=reg_ind)
            ds_reg_group = ds_reg.groupby('time.season')
            ds_reg_mean = ds_reg_group.mean(skipna=True).compute()

            ds_mean['sst_me'][reg, 1:] = ds_reg_mean.sst_err.values
            ds_mean['sss_me'][reg, 1:] = ds_reg_mean.sss_err.values
            ds_mean['sst_mae'][reg, 1:] = ds_reg_mean.sst_abs_err.values
            ds_mean['sss_mae'][reg, 1:] = ds_reg_mean.sss_abs_err.values
            ds_mean['sst_crps2_mean'][reg, 1:] = ds_reg_mean.sst_crps2.values
            ds_mean['sss_crps2_mean'][reg, 1:] = ds_reg_mean.sss_crps2.values
            ds_mean['sst_crps4_mean'][reg, 1:] = ds_reg_mean.sst_crps4.values
            ds_mean['sss_crps4_mean'][reg, 1:] = ds_reg_mean.sss_crps4.values
            ds_mean['sst_crps6_mean'][reg, 1:] = ds_reg_mean.sst_crps6.values
            ds_mean['sss_crps6_mean'][reg, 1:] = ds_reg_mean.sss_crps6.values

            if bathymetry is not None:
                ds_mean['sbt_me'][reg, 1:] = ds_reg_mean.sbt_err.values
                ds_mean['sbs_me'][reg, 1:] = ds_reg_mean.sbs_err.values
                ds_mean['sbt_mae'][reg, 1:] = ds_reg_mean.sbt_abs_err.values
                ds_mean['sbs_mae'][reg, 1:] = ds_reg_mean.sbs_abs_err.values

            ds_reg_mean = ds_reg.mean(dim='profile', skipna=True).compute()
            ds_mean['sst_me'][reg, 0] = ds_reg_mean.sst_err.values
            ds_mean['sss_me'][reg, 0] = ds_reg_mean.sss_err.values
            ds_mean['sst_mae'][reg, 0] = ds_reg_mean.sst_abs_err.values
            ds_mean['sss_mae'][reg, 0] = ds_reg_mean.sss_abs_err.values
            ds_mean['sst_crps2_mean'][reg, 0] = ds_reg_mean.sst_crps2.values
            ds_mean['sss_crps2_mean'][reg, 0] = ds_reg_mean.sss_crps2.values
            ds_mean['sst_crps4_mean'][reg, 0] = ds_reg_mean.sst_crps4.values
            ds_mean['sss_crps4_mean'][reg, 0] = ds_reg_mean.sss_crps4.values
            ds_mean['sst_crps6_mean'][reg, 0] = ds_reg_mean.sst_crps6.values
            ds_mean['sss_crps6_mean'][reg, 0] = ds_reg_mean.sss_crps6.values

            if bathymetry is not None:
                ds_mean['sbt_me'][reg, 0] = ds_reg_mean.sbt_err.values
                ds_mean['sbs_me'][reg, 0] = ds_reg_mean.sbs_err.values
                ds_mean['sbt_mae'][reg, 0] = ds_reg_mean.sbt_abs_err.values
                ds_mean['sbs_mae'][reg, 0] = ds_reg_mean.sbs_abs_err.values

        ds_out = xr.merge((ds, ds_mean))
        ds_out['is_in_region'] = (['region', 'profile'], is_in_region)

        # Write to file
        write_ds_to_file(ds_out, fn_out)
Exemplo n.º 16
0
import numpy as np
import xarray as xr
import dask
import matplotlib.pyplot as plt
import matplotlib.colors as colors  # colormap fiddling

#################################################
#%%  Loading  data
#################################################

dir_nam = "/projectsa/accord/GCOMS1k/OUTPUTS/BLZE12_02/2015/"
fil_nam = "BLZE12_1h_20151101_20151130_grid_T.nc"
dom_nam = "/projectsa/accord/GCOMS1k/INPUTS/BLZE12_C1/domain_cfg.nc"


sci_t = coast.NEMO(dir_nam + fil_nam, \
        dom_nam, grid_ref='t-grid', multiple=False)

sci_u = coast.NEMO(dir_nam + fil_nam.replace('grid_T','grid_U'), \
        dom_nam, grid_ref='u-grid', multiple=False)

sci_v = coast.NEMO(dir_nam + fil_nam.replace('grid_T','grid_V'), \
        dom_nam, grid_ref='v-grid', multiple=False)

# create an empty w-grid object, to store stratification
sci_w = coast.NEMO(fn_domain=dom_nam, grid_ref='w-grid')

#%% Plot
plt.pcolormesh(sci_t.dataset.ssh.isel(t_dim=0))
plt.show()