Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with self.assertWarnsRegex(RuntimeWarning,
'multiple matches'):
coords = 'time lat lon lev x y latitude longitude'.split()
ds.t2m.attrs.pop('coordinates', None)
for dim in 'xytz':
getattr(d, dim).update(coords)
for coord in set(coords).intersection(ds.coords):
ds.coords[coord].attrs.pop('axis', None)
getattr(d, func_name)(ds.t2m)
uname = uname or name
circ_name = circ_name or name
ds = psyd.open_dataset(os.path.join(bt.test_dir, 'test-t2m-u-v.nc'))
d = psyd.CFDecoder(ds)
check_ds(name)
ds.close()
ds = psyd.open_dataset(os.path.join(bt.test_dir, 'icon_test.nc'))
d = psyd.CFDecoder(ds)
check_ds(uname)
ds.close()
ds = psyd.open_dataset(
os.path.join(bt.test_dir, 'circumpolar_test.nc'))
d = psyd.CFDecoder(ds)
check_ds(circ_name)
ds.close()
self.assertIsNone(getattr(d, func_name)(ds.t2m_2d))
if six.PY3:
# Test whether the warning is raised if the decoder finds
# multiple dimensions
with self.assertWarnsRegex(RuntimeWarning,
'multiple matches'):
coords = 'time lat lon lev x y latitude longitude'.split()
ds.t2m.attrs.pop('coordinates', None)
for dim in 'xytz':
getattr(d, dim).update(coords)
for coord in set(coords).intersection(ds.coords):
ds.coords[coord].attrs.pop('axis', None)
getattr(d, func_name)(ds.t2m)
uname = uname or name
circ_name = circ_name or name
ds = psyd.open_dataset(os.path.join(bt.test_dir, 'test-t2m-u-v.nc'))
d = psyd.CFDecoder(ds)
check_ds(name)
ds.close()
ds = psyd.open_dataset(os.path.join(bt.test_dir, 'icon_test.nc'))
d = psyd.CFDecoder(ds)
check_ds(uname)
ds.close()
ds = psyd.open_dataset(
os.path.join(bt.test_dir, 'circumpolar_test.nc'))
d = psyd.CFDecoder(ds)
check_ds(circ_name)
ds.close()
def test_update_06_2variables(self):
"""test the change of the variable of a concatenated array"""
ds = psyd.open_dataset(bt.get_file('test-t2m-u-v.nc'))
arr = ds[['t2m', 'u']].to_array().isel(time=0, lev=0, lat=0)
arr.attrs['test'] = 4
self.assertNotIn('test', ds.t2m.attrs)
arr.name = 'something'
arr.psy.base = ds
self.assertEqual(dict(arr.psy.idims), {'time': 0, 'lev': 0, 'lat': 0,
'lon': slice(None)})
self.assertEqual(arr.coords['variable'].values.tolist(), ['t2m', 'u'])
# update to next time step
arr.psy.update(time=1, name=['u', 'v'])
self.assertEqual(arr.time, ds.time[1])
self.assertEqual(arr.coords['variable'].values.tolist(), ['u', 'v'])
self.assertEqual(arr.values.tolist(),
ds[['u', 'v']].to_array()[
:, 1, 0, 0, :].values.tolist())
self.assertEqual(dict(arr.psy.idims), {'time': 1, 'lev': 0, 'lat': 0,
def test_fldstd_01_lola(self):
from psyplot.project import Cdo
fname = bt.get_file('test-t2m-u-v.nc')
ds = psyd.open_dataset(fname)
psyd.rcParams['gridweights.use_cdo'] = True
std = ds.psy.t2m.psy.fldstd(keepdims=True).values
ref = Cdo().fldstd(input=fname, returnArray='t2m')
self.assertAlmostArrayEqual(std, ref)
# try it with the self defined gridweights
psyd.rcParams['gridweights.use_cdo'] = False
std = ds.psy.t2m.psy.fldstd(keepdims=True).values
self.assertAlmostArrayEqual(std, ref, rtol=1e-3)
ds.close()
def _test_engine(self, engine):
from importlib import import_module
fname = self.fname
ds = psyd.open_dataset(fname, engine=engine).load()
self.assertEqual(ds.psy.filename, fname)
store_mod, store = ds.psy.data_store
# try to load the dataset
mod = import_module(store_mod)
try:
ds2 = psyd.open_dataset(getattr(mod, store).open(fname))
except AttributeError:
ds2 = psyd.open_dataset(getattr(mod, store)(fname))
ds.close()
ds2.close()
ds.psy.filename = None
dumped_fname, dumped_store_mod, dumped_store = psyd.get_filename_ds(
ds, dump=True, engine=engine, paths=True)
self.assertTrue(dumped_fname)
self.assertTrue(osp.exists(dumped_fname),
msg='Missing %s' % fname)
def test_open_dataset(self):
fname = self.test_to_netcdf()
ref_ds = self._test_ds
ds = psyd.open_dataset(fname)
self.assertEqual(
pd.to_datetime(ds.time.values).tolist(),
pd.to_datetime(ref_ds.time.values).tolist())
def test_get_variable_by_axis_02(self):
"""Test the :meth:`CFDecoder.get_variable_by_axis` method with missing
coordinates, see https://github.com/psyplot/psyplot/pull/19"""
fname = os.path.join(bt.test_dir, 'icon_test.nc')
with psyd.open_dataset(fname) as ds:
ds['ncells'] = ('ncells', np.arange(ds.dims['ncells']))
decoder = psyd.CFDecoder(ds)
arr = ds.psy['t2m'].psy.isel(ncells=slice(3, 10))
del arr['clon']
xcoord = decoder.get_variable_by_axis(arr, 'x', arr.coords)
self.assertEqual(xcoord.name, 'clon')
self.assertEqual(list(xcoord.ncells), list(arr.ncells))
def test_idims(self):
"""Test the extraction of the slicers of the dimensions"""
ds = psyd.open_dataset(bt.get_file('test-t2m-u-v.nc'))
arr = ds.t2m[1:, 1]
arr.psy.init_accessor(base=ds)
dims = arr.psy.idims
for dim in ['time', 'lev', 'lat', 'lon']:
self.assertEqual(
psyd.safe_list(ds[dim][dims[dim]]),
psyd.safe_list(arr.coords[dim]),
msg="Slice %s for dimension %s is wrong!" % (dims[dim], dim))
# test with unknown dimensions
if xr_version[:2] >= (0, 9):
ds = ds.drop('time')
arr = ds.t2m[1:, 1]
arr.psy.init_accessor(base=ds)
if not six.PY2:
with self.assertWarnsRegex(RuntimeWarning, 'time'):
dims = arr.psy.idims
def test_get_variable_by_axis(self):
"""Test the :meth:`CFDecoder.get_variable_by_axis` method"""
ds = psyd.open_dataset(os.path.join(bt.test_dir,
'circumpolar_test.nc'))
decoder = psyd.CFDecoder(ds)
arr = ds.t2m
arr.attrs.pop('coordinates', None)
arr.encoding.pop('coordinates', None)
for c in ds.coords.values():
c.attrs.pop('axis', None)
for dim in ['x', 'y', 'z', 't']:
self.assertIsNone(decoder.get_variable_by_axis(arr, dim),
msg="Accidently found coordinate %s" % dim)
# test coordinates attribute
arr.attrs['coordinates'] = 'latitude longitude'
self.assertEqual(decoder.get_variable_by_axis(arr, 'x').name,
'longitude')
self.assertEqual(decoder.get_variable_by_axis(arr, 'y').name,
def test_get_decoder(self):
"""Test to get the right decoder"""
ds = psyd.open_dataset(bt.get_file('simple_triangular_grid_si0.nc'))
d = psyd.CFDecoder.get_decoder(ds, ds.Mesh2_fcvar)
self.assertIsInstance(d, psyd.UGridDecoder)
return ds, d