Bases: DataObjectSupportingAggregation
Represents a Multidimensional Aggregation in HydroShare
Source code in hsclient\hydroshare.py
class NetCDFAggregation(DataObjectSupportingAggregation):
"""Represents a Multidimensional Aggregation in HydroShare"""
@classmethod
def create(cls, base_aggr):
return super().create(aggr_cls=cls, base_aggr=base_aggr)
def as_data_object(self, agg_path: str) -> 'xarray.Dataset':
"""
Loads the Multidimensional aggregation to a xarray Dataset object
:param agg_path: the path to the Multidimensional aggregation
:return: the Multidimensional aggregation as a xarray Dataset object
"""
if xarray is None:
raise Exception("xarray package was not found")
return self._get_data_object(agg_path=agg_path, func=xarray.open_dataset)
def save_data_object(self, resource: 'Resource', agg_path: str, as_new_aggr: bool = False,
destination_path: str = "") -> 'Aggregation':
"""
Saves the xarray Dataset object to the Multidimensional aggregation
:param resource: the resource containing the aggregation
:param agg_path: the path to the Multidimensional aggregation
:param as_new_aggr: Defaults False, set to True to create a new Multidimensional aggregation
:param destination_path: the destination path in Hydroshare to save the new aggregation
:return: the updated or new Multidimensional aggregation
"""
self._validate_aggregation_for_update(resource, AggregationType.MultidimensionalAggregation)
file_path = self._validate_aggregation_path(agg_path, for_save_data=True)
self._data_object.to_netcdf(file_path, format="NETCDF4")
aggr_main_file_path = self.main_file_path
if not as_new_aggr:
# cache some of the metadata fields of the original aggregation to update the metadata of the
# updated aggregation
keywords = self.metadata.subjects
additional_meta = self.metadata.additional_metadata
# upload the updated aggregation files
self._update_aggregation(resource, file_path)
# retrieve the updated aggregation
aggr = resource.aggregation(file__path=aggr_main_file_path)
# update metadata
for kw in keywords:
if kw not in aggr.metadata.subjects:
aggr.metadata.subjects.append(kw)
aggr.metadata.additional_metadata = additional_meta
aggr.save()
else:
# creating a new aggregation
resource.file_upload(file_path, destination_path=destination_path)
# retrieve the new aggregation
agg_path = urljoin(destination_path, os.path.basename(aggr_main_file_path))
aggr = resource.aggregation(file__path=agg_path)
aggr._data_object = None
return aggr
as_data_object(agg_path)
Loads the Multidimensional aggregation to a xarray Dataset object :param agg_path: the path to the Multidimensional aggregation :return: the Multidimensional aggregation as a xarray Dataset object
Source code in hsclient\hydroshare.py
def as_data_object(self, agg_path: str) -> 'xarray.Dataset':
"""
Loads the Multidimensional aggregation to a xarray Dataset object
:param agg_path: the path to the Multidimensional aggregation
:return: the Multidimensional aggregation as a xarray Dataset object
"""
if xarray is None:
raise Exception("xarray package was not found")
return self._get_data_object(agg_path=agg_path, func=xarray.open_dataset)
save_data_object(resource, agg_path, as_new_aggr=False, destination_path='')
Saves the xarray Dataset object to the Multidimensional aggregation :param resource: the resource containing the aggregation :param agg_path: the path to the Multidimensional aggregation :param as_new_aggr: Defaults False, set to True to create a new Multidimensional aggregation :param destination_path: the destination path in Hydroshare to save the new aggregation :return: the updated or new Multidimensional aggregation
Source code in hsclient\hydroshare.py
def save_data_object(self, resource: 'Resource', agg_path: str, as_new_aggr: bool = False,
destination_path: str = "") -> 'Aggregation':
"""
Saves the xarray Dataset object to the Multidimensional aggregation
:param resource: the resource containing the aggregation
:param agg_path: the path to the Multidimensional aggregation
:param as_new_aggr: Defaults False, set to True to create a new Multidimensional aggregation
:param destination_path: the destination path in Hydroshare to save the new aggregation
:return: the updated or new Multidimensional aggregation
"""
self._validate_aggregation_for_update(resource, AggregationType.MultidimensionalAggregation)
file_path = self._validate_aggregation_path(agg_path, for_save_data=True)
self._data_object.to_netcdf(file_path, format="NETCDF4")
aggr_main_file_path = self.main_file_path
if not as_new_aggr:
# cache some of the metadata fields of the original aggregation to update the metadata of the
# updated aggregation
keywords = self.metadata.subjects
additional_meta = self.metadata.additional_metadata
# upload the updated aggregation files
self._update_aggregation(resource, file_path)
# retrieve the updated aggregation
aggr = resource.aggregation(file__path=aggr_main_file_path)
# update metadata
for kw in keywords:
if kw not in aggr.metadata.subjects:
aggr.metadata.subjects.append(kw)
aggr.metadata.additional_metadata = additional_meta
aggr.save()
else:
# creating a new aggregation
resource.file_upload(file_path, destination_path=destination_path)
# retrieve the new aggregation
agg_path = urljoin(destination_path, os.path.basename(aggr_main_file_path))
aggr = resource.aggregation(file__path=agg_path)
aggr._data_object = None
return aggr