def loadZarrRun(filename, chunks='auto'): dsWC = xr.open_zarr(filename, chunks=chunks) ds = xr.open_dataset('../data/model/jet_his.nc') # For some reason I have to correct the grid metrics that aren't being output with this CROCO run. dsWC['s_rho'] = ('s_rho', ds.s_rho) dsWC['s_w'] = ('s_w', ds.s_w) dsWC['Cs_r'] = ds.Cs_r dsWC['Cs_w'] = ds.Cs_w dsWC['h'] = (('eta_rho', 'xi_rho'), 4000 + 0 * dsWC.hbl.isel(time=0)) dsWC['pm'] = (('eta_rho', 'xi_rho'), 1 / 500 + 0 * dsWC.hbl.isel(time=0)) dsWC['pn'] = (('eta_rho', 'xi_rho'), 1 / 500 + 0 * dsWC.hbl.isel(time=0)) dsWC['pm_u'] = (('eta_rho', 'xi_u'), 1 / 500 + 0 * dsWC.u.isel(time=0, s_rho=0)) dsWC['pn_v'] = (('eta_v', 'xi_rho'), 1 / 500 + 0 * dsWC.v.isel(time=0, s_rho=0)) #dsWC = dsWC.rename(time='ocean_time') dsWC, grid = xroms.roms_dataset(dsWC, Vtransform=1) dsWC['rho'] = dsWC.temp dsWC = dsWC.swap_dims({'time': 'ocean_time'}) dsWC['temp'] = (-(1000 + dsWC.rho) / (1000 + dsWC.rho.isel(ocean_time=1, s_rho=-1).mean()) + 1) / 2e-4 + 20 return dsWC, grid
def loadNewRun(filename, chunks): # note here that filename should be the directory of history files files = sorted(glob(filename + 'jet_his.*.nc')) dsWC = xr.open_mfdataset(files, combine='nested', concat_dim='time', data_vars='minimal', chunks=chunks) ds = xr.open_dataset('../data/model/jet_his.nc') # For some reason I have to correct the grid metrics that aren't being output with this CROCO run. dsWC['s_rho'] = ('s_rho', ds.s_rho) dsWC['s_w'] = ('s_w', ds.s_w) dsWC['Cs_r'] = ds.Cs_r dsWC['Cs_w'] = ds.Cs_w dsWC['h'] = (('eta_rho', 'xi_rho'), 4000 + 0 * dsWC.hbl.isel(time=0)) dsWC['pm'] = (('eta_rho', 'xi_rho'), 1 / 500 + 0 * dsWC.hbl.isel(time=0)) dsWC['pn'] = (('eta_rho', 'xi_rho'), 1 / 500 + 0 * dsWC.hbl.isel(time=0)) dsWC['pm_u'] = (('eta_rho', 'xi_u'), 1 / 500 + 0 * dsWC.u.isel(time=0, s_rho=0)) dsWC['pn_v'] = (('eta_v', 'xi_rho'), 1 / 500 + 0 * dsWC.v.isel(time=0, s_rho=0)) #dsWC['f'] = dsNC.f ## XXX THIS IS A HACK THAT WON"T ALWAYS WORK #dsWC = dsWC.rename(time='ocean_time') dsWC, grid = xroms.roms_dataset(dsWC, Vtransform=1) dsWC['rho'] = dsWC.temp dsWC = dsWC.swap_dims({'time': 'ocean_time'}) dsWC['temp'] = (-(1000 + dsWC.rho) / (1000 + dsWC.rho.isel(ocean_time=1, s_rho=-1).mean()) + 1) / 2e-4 + 20 return dsWC, grid
def __init__(self, ds): self.ds = ds # if ds wasn't read in with an xroms load function, it probably doesn't have a grid object if "grid" not in ds.attrs: self.ds, grid = xroms.roms_dataset(self.ds) self.grid = grid
def loadOldRun(filename, chunks): ds = xr.open_dataset(filename, chunks=chunks) ds['zeta'] = 0 * ds.hbl # didn't save zeta...oops. ds = ds.rename(time='ocean_time') ds, grid = xroms.roms_dataset(ds) ds['temp'] = (-(1000 + ds.rho) / (1000 + ds.rho.isel(ocean_time=0, s_rho=-1).mean()) + 1) / 2e-4 + 20 return ds, grid
def __init__(self, ds): self.ds = ds # extra for getting coordinates but changes variables self._ds = ds.copy(deep=True) # if ds wasn't read in with an xroms load function, it probably doesn't have a grid object if "grid" not in ds.attrs: self.ds, grid = xroms.roms_dataset(self.ds) self.grid = grid
"""Test utilities in xroms""" import cartopy import numpy as np import xarray as xr import xroms grid1 = xr.open_dataset("xroms/tests/input/grid.nc") # ds = xroms.open_netcdf('xroms/tests/input/ocean_his_0001.nc') ds = xr.open_dataset("xroms/tests/input/ocean_his_0001.nc") # combine the two: ds = ds.merge(grid1, overwrite_vars=True, compat="override") ds, grid = xroms.roms_dataset(ds) # # missing psi grid in variables # ds = ds.assign_coords({'lon_psi': ds.lon_psi, 'lat_psi': ds.lat_psi}) xl, yl, N = 14, 9, 3 z_rho = np.array([-97.5025, -50.05, -2.5975]) # eta_rho=0, xi_rho=0 dz = np.array([33.3, 33.3, 33.3]) # eta_rho=0, xi_rho=0 dx = 14168.78734979 # when eta_rho=0 dy = 18467.47219268 # when eta_rho=0 u = np.linspace(0, 1.2, xl - 1) v = np.linspace(-0.1, 0.1, yl - 1)[:, np.newaxis] temp = np.linspace(15, 20, N) salt = np.linspace(25, 15, N) zeta = np.linspace(-0.1, 0.1, xl) g = 9.81 rho0 = 1025
from xhistogram.xarray import histogram import xarray as xr import xroms import h5netcdf from glob import glob #Need to subset the netcdf files because xroms will crash if I open them all. years = np.arange(1994, 2017) for y in years: paths = glob('/d1/shared/TXLA_ROMS/output_20yr_obc/%i/ocean_his_00*.nc' % y) print('Loading Data') ds = xroms.open_mfnetcdf(paths, chunks={'ocean_time': 1}) ds, grid = xroms.roms_dataset(ds, Vtransform=None) print('Data Loaded') #This will yield a box in the SUNRISE area of interest: see histogram_demonstration for examples. etaslice = slice(30, 150) xislice = slice(270, 405) # dsdx = grid.derivative(ds.salt, 'X') # dsdy = grid.derivative(ds.salt, 'Y') dsdx = grid.interp(grid.diff(ds.salt, 'X'), 'X', boundary='extend') / ds.dx dsdy = grid.interp(grid.diff(ds.salt, 'Y'), 'Y', boundary='extend') / ds.dy dsdx.attrs = '' dsdy.attrs = '' dsdx_slice = dsdx.isel( eta_rho=etaslice, xi_rho=xislice,
from pytest import approx import numpy as np import xroms from xroms import ll2xy, xy2ll # A = xroms.roms_dataset('../examples/ocean_avg_0014.nc') A = xroms.roms_dataset('/home/bjorn/python/xroms/examples/ocean_avg_0014.nc') A = xroms.roms_dataset('examples/ocean_avg_0014.nc') def test_xy2ll2xy(): """grid to geography to grid should be identity""" x, y = 100, 110 lon, lat = xy2ll(A, x, y) x1, y1 = ll2xy(A, lon, lat) assert (x1 == approx(x)) assert (y1 == approx(y)) def test_ll2xy2ll(): """geography to grid to geography should be identity""" lon, lat = 5, 60 x, y = ll2xy(A, lon, lat) lon1, lat1 = xy2ll(A, x, y) print(float(lon1), float(lat1)) assert (lon1 == approx(lon, abs=1e-4)) assert (lat1 == approx(lat)) def test_ll2xy_edge(): """Testing edge cases, literally"""