Skip to content

Commit

Permalink
Extend eva to support legacy ConMon time/vert data (JCSDA-internal#169)
Browse files Browse the repository at this point in the history
* Ref JCSDA-internal#167

Add support for batch processing on DataType/dtype.

* Ref JCSDA-internal#167

Fix datatype use with station data.

* Ref JCSDA-internal#167

Add update for conmon vert plots.

* Ref JCSDA-internal#167

Fix typo.
  • Loading branch information
EdwardSafford-NOAA authored Jan 2, 2024
1 parent bece9d2 commit b14e19d
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 61 deletions.
31 changes: 26 additions & 5 deletions src/eva/data/data_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def add_variable_to_collection(self, collection_name, group_name, variable_name,
# ----------------------------------------------------------------------------------------------

def get_variable_data_array(self, collection_name, group_name, variable_name,
channels=None, levels=None):
channels=None, levels=None, datatypes=None):

"""
Retrieve a specific variable (as a DataArray) from a collection.
Expand All @@ -181,18 +181,21 @@ def get_variable_data_array(self, collection_name, group_name, variable_name,
variable_name (str): Name of the variable.
channels (int or list[int]): Indices of channels to select (optional).
levels (int or list[int]): Indices of levels to select (optional).
datatypes (str or list[str]): Indices of data types to select (optional).
Returns:
DataArray: The selected variable as an xarray DataArray.
Raises:
ValueError: If channels are provided but the 'Channel' dimension is missing.
ValueError: If channels, levels, or datatypes are provided but the
corresponding 'Channel', 'Level', or 'DataType' dimension
is missing.
"""

group_variable_name = group_name + '::' + variable_name
data_array = self._collections[collection_name][group_variable_name]

if channels is None and levels is None:
if channels is None and levels is None and datatypes is None:
return data_array

if channels is not None:
Expand All @@ -201,6 +204,7 @@ def get_variable_data_array(self, collection_name, group_name, variable_name,
if 'Channel' not in list(self._collections[collection_name].dims):
self.logger.abort(f'In get_variable_data_array channels is provided but ' +
f'Channel is not a dimension in Dataset')

# Make sure it is a list
channels_sel = []
channels_sel.append(channels)
Expand Down Expand Up @@ -229,10 +233,26 @@ def get_variable_data_array(self, collection_name, group_name, variable_name,
self.logger.abort('In get_variable_data_array levels is neither none ' +
'nor a list of integers')

elif datatypes is not None:
if isinstance(datatypes, str) or not any(not isinstance(dt, str) for dt in datatypes):
# DataType must be a dimension if it will be used for selection
if 'DataType' not in list(self._collections[collection_name].dims):
self.logger.abort(f'In get_variable_data_array levels is provided but ' +
f'DataType is not a dimension in Dataset')
datatypes_sel = []
datatypes_sel.append(datatypes)

# Create a new DataArray with the requested datatypes
data_array_dtypes = data_array.sel(DataType=datatypes_sel)
return data_array_dtypes
else:
self.logger.abort('In get_variable_data_array datatype is neither none ' +
'nor a list of strings')

# ----------------------------------------------------------------------------------------------

def get_variable_data(self, collection_name, group_name, variable_name,
channels=None, levels=None):
channels=None, levels=None, datatypes=None):

"""
Retrieve the data of a specific variable from a collection.
Expand All @@ -243,13 +263,14 @@ def get_variable_data(self, collection_name, group_name, variable_name,
variable_name (str): Name of the variable.
channels (int or list[int]): Indices of channels to select (optional).
levels (int or list[int]): Indices of levels to select (optional).
datatypes (str or list[str]): Indices of data types to select (optional).
Returns:
ndarray: The selected variable data as a NumPy array.
"""

variable_array = self.get_variable_data_array(collection_name, group_name, variable_name,
channels, levels)
channels, levels, datatypes)

# Extract the actual data array
variable_data = variable_array.data
Expand Down
Loading

0 comments on commit b14e19d

Please sign in to comment.