Skip to content

Commit

Permalink
adding updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsockol committed Jul 18, 2024
1 parent add4b4d commit 03cedfe
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
24 changes: 16 additions & 8 deletions act/utils/data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,8 +1412,14 @@ def calculate_percentages(ds, fields, time=None, time_slice=None, threshold=None
return percentages


def convert_2d_to_1d(ds, parse=None, variables=None, keep_name_if_one=False,
use_dim_value_in_name=False, dim_labels=None):
def convert_2d_to_1d(
ds,
parse=None,
variables=None,
keep_name_if_one=False,
use_dim_value_in_name=False,
dim_labels=None,
):
"""
Function to convert a single 2D variable into multiple 1D
variables using the second dimension in the new variable name.
Expand Down Expand Up @@ -1457,7 +1463,7 @@ def convert_2d_to_1d(ds, parse=None, variables=None, keep_name_if_one=False,
"""
# If no parse dimension name given assume it is the one not equal to 'time'
if parse is None:
parse = (list(set(list(ds.dims)) - set(['time'])))[0]
parse = (list(set(list(ds.dims)) - {'time'}))[0]

new_ds = ds.copy()

Expand All @@ -1467,7 +1473,7 @@ def convert_2d_to_1d(ds, parse=None, variables=None, keep_name_if_one=False,
if variables is None:
variables = list(new_ds.variables)

if dim_labels is not None and isinstance(dim_labels, (str, )):
if dim_labels is not None and isinstance(dim_labels, (str,)):
dim_labels = [dim_labels]

# Check if we want to keep the names the same if the second dimension
Expand All @@ -1482,9 +1488,9 @@ def convert_2d_to_1d(ds, parse=None, variables=None, keep_name_if_one=False,
continue
# Check if the parse dimension is in the dimension tuple
if parse in new_ds[var].dims:
if len((new_ds[parse])) >= num_dims:
if len(new_ds[parse]) >= num_dims:
for i in range(0, new_ds.sizes[parse]):
if (dim_labels is not None):
if dim_labels is not None:
new_var_name = '_'.join([var, dim_labels[i]])
elif use_dim_value_in_name:
level = str(parse_values[i]) + ds[parse].attrs['units']
Expand All @@ -1497,10 +1503,12 @@ def convert_2d_to_1d(ds, parse=None, variables=None, keep_name_if_one=False,
try:
ancillary_variables = new_ds[new_var_name].attrs['ancillary_variables']
current_qc_var_name = ds.qcfilter.check_for_ancillary_qc(
var, add_if_missing=False)
var, add_if_missing=False
)
if current_qc_var_name is not None:
ancillary_variables = ancillary_variables.replace(
current_qc_var_name, 'qc_' + new_var_name)
current_qc_var_name, 'qc_' + new_var_name
)
new_ds[new_var_name].attrs['ancillary_variables'] = ancillary_variables
except KeyError:
pass
Expand Down
10 changes: 4 additions & 6 deletions tests/utils/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@ def test_calculate_percentages():
def test_convert_2d_to_1d():
# Create a sample dataset
data = np.array([[1, 2], [3, 4], [5, 6]])
ds = xr.Dataset({
'var': (('time', 'level'), data)
}, coords={'time': [0, 1, 2], 'level': [10, 20]})
ds = xr.Dataset(
{'var': (('time', 'level'), data)}, coords={'time': [0, 1, 2], 'level': [10, 20]}
)
ds['level'].attrs['units'] = 'm'

# Run the function
Expand Down Expand Up @@ -597,9 +597,7 @@ def test_convert_2d_to_1d():

# Create a sample dataset
data = np.array([[1], [3], [5]])
ds = xr.Dataset({
'var': (('time', 'level'), data)
}, coords={'time': [0, 1, 2], 'level': [10]})
ds = xr.Dataset({'var': (('time', 'level'), data)}, coords={'time': [0, 1, 2], 'level': [10]})

# Run the function with keep_name_if_one=True
result = convert_2d_to_1d(ds, parse='level', keep_name_if_one=True)
Expand Down

0 comments on commit 03cedfe

Please sign in to comment.