Commit a55986de authored by PIOLLE's avatar PIOLLE
Browse files

fixed append method

parent 55d9eadb
...@@ -1034,8 +1034,8 @@ class Dataset(ABC): ...@@ -1034,8 +1034,8 @@ class Dataset(ABC):
# probably not all of them... # probably not all of them...
warnings.warn( warnings.warn(
"Entering a special case where not xarray variable " "Entering a special case where not xarray variable "
"assignment was possible, probably because of fill " "assignment was possible, probably because of fill or duplicate"
"values in a coordinate. Field: {}".format(field.name)) " values in a coordinate. Field: {}".format(field.name))
self._std_dataset[field.name] = xr.DataArray( self._std_dataset[field.name] = xr.DataArray(
coords=[(d, self._std_dataset.coords[d]) for d in field.dims], coords=[(d, self._std_dataset.coords[d]) for d in field.dims],
data=field.get_values(), data=field.get_values(),
......
...@@ -206,14 +206,9 @@ class Feature(Dataset, ABC): ...@@ -206,14 +206,9 @@ class Feature(Dataset, ABC):
return dim_validity return dim_validity
def append(self, feature, prefix, fields=None): def append(self, feature, prefix=None, fields=None):
"""Append the fields from another feature """Append the fields from another feature
It is assumed the two features do not share any dimension. The appended
feature (child) is considered as ancillary fields and looses its
model properties. The feature model is the one of the receiving
(parent) feature.
Args: Args:
feature (AbstractFeature derived class): the feature to append. feature (AbstractFeature derived class): the feature to append.
...@@ -225,43 +220,15 @@ class Feature(Dataset, ABC): ...@@ -225,43 +220,15 @@ class Feature(Dataset, ABC):
are to be appended. By default, all fields of the feature are are to be appended. By default, all fields of the feature are
appended. appended.
""" """
child_fields = {} added_fields = fields
# create new fields if added_fields is None:
for fieldname in feature.get_geolocation_fields(): added_fields = feature.fieldnames
newname = ''.join([prefix, fieldname])
if newname in self.fieldnames:
logging.debug("Field already existing: {}".format(newname))
continue
childfield = feature.get_geolocation_field(fieldname)
if feature.get_geolocation_field(fieldname) is not None:
child_fields[newname] = childfield.clone()
# additional _ ensures no field and dimensions have the same
# name
dims = OrderedDict([('_'.join([prefix, k]), v)
for k, v in childfield.dimensions.items()])
child_fields[newname].dimensions = dims
child_fields[newname].variable.shortname = newname
child_fields[newname].set_values(childfield.get_values())
fieldlist = fields
if fieldlist is None:
fieldlist = feature.fieldnames
for fieldname in fieldlist:
newname = prefix + fieldname
childfield = feature.get_field(fieldname)
child_fields[newname] = childfield.clone()
# additional _ ensures no field and dimensions have the same
# name
dims = OrderedDict([('_'.join([prefix, k]), v)
for k, v in childfield.dimensions.items()])
child_fields[newname].dimensions = dims
child_fields[newname].variable.shortname = newname
child_fields[newname].set_values(
feature.get_values(fieldname)
)
# append fields # append fields
for fieldname, field in child_fields.items(): for fieldname in added_fields:
field = feature.get_field(fieldname).clone()
if prefix is not None:
field.rename('{}{}'.format(prefix, field.name))
self.add_field(field) self.add_field(field)
def get_values(self, *args, **kwargs): def get_values(self, *args, **kwargs):
......
...@@ -166,3 +166,15 @@ class TestFeature(unittest.TestCase): ...@@ -166,3 +166,15 @@ class TestFeature(unittest.TestCase):
ncf = NCDataset(dataset=TEST_FILE) ncf = NCDataset(dataset=TEST_FILE)
feat = self.get_feature(ncf) feat = self.get_feature(ncf)
self.assertRaises(ValueError, feat.get_field, 'unknown_field') self.assertRaises(ValueError, feat.get_field, 'unknown_field')
def test_append(self):
feat = self.get_feature(self.define_base_feature())
# another feature of same dimensions & coordinates
feat2 = self.get_feature(self.define_base_feature())
# append
feat.append(feat2, prefix="v2_")
for fieldname in feat2.fieldnames:
self.assertIn("v2_"+fieldname, feat.fieldnames)
np.allclose(feat.get_values(fieldname), feat2.get_values(fieldname))
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