Commit 8dd79369 authored by PIOLLE's avatar PIOLLE
Browse files

fixed padding

parent d2b6e027
Pipeline #2697 failed with stages
in 0 seconds
......@@ -1055,10 +1055,25 @@ class Dataset(ABC):
"""
if coordname not in ['lat', 'lon', 'time', 'z']:
logging.warning(
"{} is not a geolocation corrdinate".format(coordname)
"{} is not a geolocation coordinate".format(coordname)
)
return self.get_coord(coordname)
def get_geocoord_by_axis(self, axis: str) -> 'Field':
"""Returns the geolocation coordinate field for the given axis.
Possible coordinate field names are 'lat', 'lon', 'time', 'z'.
"""
if axis not in ['X', 'Y', 'T', 'Z']:
logging.warning(
"{} is not a geolocation coordinate".format(axis)
)
for coordname in self.geocoordnames:
if self.get_coord(coordname).attrs['axis'] == axis:
return self.get_coord(coordname)
raise ValueError('No coordinate found for axis: {}'.format(axis))
@property
def geodims(self) -> MutableMapping[str, int]:
"""Mapping from geolocation dimension names to lengths.
......@@ -1481,7 +1496,15 @@ class Dataset(ABC):
subset = self.dataset[fields]
else:
subset = self.dataset[fields][index]
if padding:
# negative indices may give strange results with xarray
corrected_index = index.copy()
for k, v in corrected_index.items():
if isinstance(v, slice) and v.start < 0:
corrected_index[k] = slice(0, v.stop, v.step)
subset = self.dataset[fields][corrected_index]
else:
subset = self.dataset[fields][index]
if not deep and padding:
raise ValueError(
......
......@@ -614,12 +614,12 @@ class Field(object):
else:
pad_edges.append([0, 0])
masked_values = subset.to_masked_array()
res = numpy.pad(
subset, pad_edges, 'constant',
constant_values=numpy.nan
masked_values, pad_edges, 'constant',
constant_values=masked_values.fill_value
)
if isinstance(subset, numpy.ma.MaskedArray):
res = numpy.ma.fix_invalid(res, copy=False)
res = numpy.ma.masked_equal(res, masked_values.fill_value, copy=False)
return res
......
......@@ -5,7 +5,7 @@ import xarray as xr
from cerbere.feature.grid import CylindricalGrid
from .test_xarray_feature import TestFeature
from .test_feature import TestFeature
class TestCylindricalGridFeature(TestFeature):
......
......@@ -7,7 +7,7 @@ import xarray as xr
from cerbere.feature.section import Section
from .test_xarray_feature import TestFeature
from .test_feature import TestFeature
class TestSectionFeature(TestFeature):
......
......@@ -9,7 +9,7 @@ import xarray as xr
from cerbere.feature.trajectory import Trajectory
from .test_xarray_feature import TestFeature
from .test_feature import TestFeature
class TestTrajectoryFeature(TestFeature):
......@@ -53,3 +53,49 @@ class TestTrajectoryFeature(TestFeature):
)
self.assertEqual(len(subset), 1)
print(subset[0])
def test_extract_subset(self):
dst = self.define_base_feature()
subset = dst.extract(
index={'time': slice(10, 20)}, deep=True
)
self.assertTrue('myvar' in subset.fieldnames)
self.assertSequenceEqual(
subset.get_values('myvar').shape, (10,)
)
self.assertIsNot(
subset.get_field('myvar').to_dataarray(),
dst.get_field('myvar').to_dataarray()
)
def test_extract_subset_padding(self):
dst = self.define_base_feature()
subset = dst.extract(
index={'time': slice(70, 90)}, padding=True, deep=True
)
self.assertTrue('myvar' in subset.fieldnames)
self.assertSequenceEqual(
subset.get_values('myvar').shape, (20,)
)
self.assertIsNot(
subset.get_field('myvar').to_dataarray(),
dst.get_field('myvar').to_dataarray()
)
self.assertEqual(subset.get_values('myvar').count(), 10)
self.assertEqual(subset.get_values('myvar').size, 20)
def test_extract_subset_padding_offlimit(self):
dst = self.define_base_feature()
subset = dst.extract(
index={'time': slice(-10, 10)}, padding=True, deep=True
)
self.assertTrue('myvar' in subset.fieldnames)
self.assertSequenceEqual(
subset.get_values('myvar').shape, (20,)
)
self.assertIsNot(
subset.get_field('myvar').to_dataarray(),
dst.get_field('myvar').to_dataarray()
)
self.assertEqual(subset.get_values('myvar').count(), 10)
self.assertEqual(subset.get_values('myvar').size, 20)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment