Commit 4fad655b authored by Jeff Piollé's avatar Jeff Piollé

fixes

parent db7d0a45
......@@ -158,7 +158,7 @@ class AbstractFeature(cerbere.feature.feature.Feature):
for field, values in content.items():
if field not in GEOCOORDINATES:
if isinstance(values, Field):
data_vars[values.name] = values.xarray()
data_vars[values.name] = values.to_dataarray()
elif isinstance(values, (xr.DataArray, numpy.ndarray)):
data_vars[field] = (('lon', 'lat',), values)
else:
......@@ -656,7 +656,7 @@ class AbstractFeature(cerbere.feature.feature.Feature):
* a :class:`xarray.DataArray`
"""
if isinstance(values, Field):
xrdata = values.xarray()
xrdata = values.to_dataarray()
elif isinstance(values, xr.DataArray):
xrdata = values
......
......@@ -185,7 +185,7 @@ class Field(newfield.Field):
"""
return Field(data=xrdata)
def xarray(self):
def to_dataarray(self):
"""Return the field values a xarray DataArray"""
if self.mapper is None:
return self.array
......
......@@ -258,7 +258,7 @@ class Dataset(ABC):
if isinstance(self._url, list) and mode in [WRITE_NEW, READ_WRITE]:
raise ValueError("A list of files is read only")
self.open(**kwargs)
self._open(**kwargs)
elif isinstance(dataset, xr.Dataset):
self.dataset = dataset
......@@ -307,16 +307,16 @@ class Dataset(ABC):
for var in data.keys():
if isinstance(data[var], Field):
data[var] = to_dict(data[var].xarray)
data[var] = to_dict(data[var].to_dataarray)
if 'coords' in data.keys():
for var, value in data['coords'].items():
if isinstance(value, Field):
data[var] = to_dict(value.xarray)
data[var] = to_dict(value.to_dataarray)
if 'data_vars' in data.keys():
for var, value in data['data_vars'].items():
if isinstance(value, Field):
data[var] = to_dict(value.xarray)
data[var] = to_dict(value.to_dataarray)
# create a dataset
self.dataset = xr.Dataset.from_dict(data)
......@@ -359,7 +359,7 @@ class Dataset(ABC):
raise NotImplementedError
@classmethod
def exists(cls, url):
def exists(cls, url: str) -> bool:
"""tests if `url` is an existing resource"""
try:
result = urlparse(url)
......@@ -509,7 +509,7 @@ class Dataset(ABC):
else:
return xr.Dataset()
def open(self, **kwargs):
def _open(self, **kwargs):
if self.is_opened():
logging.warning("A file is already opened : {}".format(
self._url)
......@@ -653,35 +653,26 @@ class Dataset(ABC):
return tuple(self.get_field_dims(fieldname).values())
@property
def _varnames(self):
"""Returns the names of all the fields of the dataset.
Returns:
list<string>: list of field names
def _varnames(self) -> List[str]:
"""List names of all the fields (including coordinates) of the dataset.
"""
if isinstance(self.dataset, Dataset):
return self.dataset._varnames
return list(self.dataset.variables.keys())
@property
def coordnames(self):
"""Returns the names of the coordinate fields of the dataset.
Returns:
list<string>: list of field names
"""
def coordnames(self) -> List[str]:
"""List of names of the coordinate fields of the dataset."""
if isinstance(self.dataset, Dataset):
return self.dataset._coordnames
return list(self.dataset.coords.keys())
@property
def geocoords(self):
"""
Return the geolocation fields
"""
def geocoords(self) -> List['xarray.DataArray']:
"""List of geolocation coordinates (as DataArray)"""
if isinstance(self.dataset, Dataset):
return self.dataset.geocoords
return self.dataset.geocoords
return [
_ for _ in self.dataset.coords if _ in GEOCOORDINATES
]
......@@ -736,7 +727,7 @@ class Dataset(ABC):
.format(field.name)
)
self.dataset = self.dataset.assign(
{field.name: field.xarray}
{field.name: field.to_dataarray}
)
field._attach_dataset(self)
......
This diff is collapsed.
......@@ -46,9 +46,9 @@ class GHRSSTNCDataset(NCDataset):
self.__collection_id = None
def open(self, **kwargs):
def _open(self, **kwargs):
# url needs to be opened first in order to guess default datamodel
super().open(**kwargs)
super()._open(**kwargs)
if self._mode != 'w':
......
......@@ -82,7 +82,7 @@ class Feature(Dataset):
* a :class:`xarray.DataArray`
"""
if isinstance(values, Field):
xrdata = values.xarray()
xrdata = values.to_dataarray()
elif isinstance(values, xr.DataArray):
xrdata = values
......
......@@ -278,8 +278,8 @@ class TestXArrayDataset(unittest.TestCase):
subset.get_values('test_var').shape, (360, 160)
)
self.assertIsNot(
subset.get_field('test_var').xarray,
dst.get_field('test_var').xarray
subset.get_field('test_var').to_dataarray,
dst.get_field('test_var').to_dataarray
)
def test_extract_as_view(self):
......@@ -290,8 +290,8 @@ class TestXArrayDataset(unittest.TestCase):
subset.get_values('test_var').shape, (360, 160)
)
self.assertIs(
subset.get_field('test_var').xarray,
dst.get_field('test_var').xarray
subset.get_field('test_var').to_dataarray,
dst.get_field('test_var').to_dataarray
)
def test_extract_rename(self):
......@@ -302,8 +302,8 @@ class TestXArrayDataset(unittest.TestCase):
subset.get_values('new_test_var').shape, (360, 160)
)
self.assertIsNot(
subset.get_field('new_test_var').xarray,
dst.get_field('test_var').xarray
subset.get_field('new_test_var').to_dataarray,
dst.get_field('test_var').to_dataarray
)
def test_extract_subset(self):
......@@ -316,8 +316,8 @@ class TestXArrayDataset(unittest.TestCase):
subset.get_values('test_var').shape, (5, 10)
)
self.assertIsNot(
subset.get_field('test_var').xarray,
dst.get_field('test_var').xarray
subset.get_field('test_var').to_dataarray,
dst.get_field('test_var').to_dataarray
)
def test_extract_subset_padding(self):
......@@ -332,8 +332,8 @@ class TestXArrayDataset(unittest.TestCase):
subset.get_values('test_var').shape, (10, 10)
)
self.assertIsNot(
subset.get_field('test_var').xarray,
dst.get_field('test_var').xarray
subset.get_field('test_var').to_dataarray,
dst.get_field('test_var').to_dataarray
)
self.assertEqual(subset.get_values('test_var').count(), 50)
self.assertEqual(subset.get_values('test_var').size, 100)
\ No newline at end of file
......@@ -25,6 +25,20 @@ class TestField(unittest.TestCase):
data = np.ma.zeros((100, 200))
return Field(data, name='test', dims=('x', 'y'))
def test_create_field_from_numpy2(self):
data = np.ma.zeros((100, 200))
field = Field(data=(), name='test')
print("TEST2", field)
print("TOTO")
self.assertIsInstance(field, Field)
def test_create_field_from_numpy2(self):
data = np.ma.zeros((100, 200))
field = Field(data=xr.DataArray(data), name='test')
print("TEST2", field)
print("TOTO")
self.assertIsInstance(field, Field)
def test_create_field(self):
field = self.create_field_from_numpy()
self.assertIsInstance(field, Field)
......@@ -32,7 +46,7 @@ class TestField(unittest.TestCase):
def test_get_xarray(self):
field = self.create_field_from_numpy()
xarr = field.xarray
xarr = field.to_dataarray
self.assertIsInstance(xarr, xr.DataArray)
print(xarr.dims)
self.assertTrue('x' in xarr.dims)
......@@ -86,4 +100,11 @@ class TestField(unittest.TestCase):
self.assertIsInstance(values, np.ma.MaskedArray)
self.assertEqual(values.shape, (20, 5))
self.assertEqual(values.count(), 50)
self.assertEqual(values[10:, :].count(), 0)
\ No newline at end of file
self.assertEqual(values[10:, :].count(), 0)
def test_apply_func(self):
field1 = self.create_field_from_numpy()
field2 = self.create_field_from_numpy()
np.ma.multiply(field1.get_values(), field2.get_values())
fsum = Field.compute(np.ma.multiply, field1, field2)
print("SUM ", fsum)
\ No newline at end of file
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