Skip to content

Commit

Permalink
make str group flattening into a public function that can handle zero…
Browse files Browse the repository at this point in the history
… whitespace and add tests
  • Loading branch information
danielfromearth committed Jul 2, 2024
1 parent 45a4075 commit 57985a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
22 changes: 13 additions & 9 deletions concatenator/attribute_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def regroup_coordinate_attribute(attribute_string: str) -> str:
Examples
--------
>>> coord_att = "__Time_and_Position__time __Time_and_Position__instrument_fov_latitude __Time_and_Position__instrument_fov_longitude"
>>> _flatten_coordinate_attribute(coord_att)
>>> flatten_string_with_groups(coord_att)
Time_and_Position/time Time_and_Position/instrument_fov_latitude Time_and_Position/instrument_fov_longitude
Parameters
Expand Down Expand Up @@ -58,40 +58,44 @@ def flatten_coordinate_attribute_paths(
if "coordinates" in var.ncattrs():
coord_att = var.getncattr("coordinates")

new_coord_att = _flatten_coordinate_attribute(coord_att)
new_coord_att = flatten_string_with_groups(coord_att)

dataset.variables[variable_name].setncattr("coordinates", new_coord_att)


def _flatten_coordinate_attribute(attribute_string: str) -> str:
"""Converts attributes that specify group membership via "/" to use new group delimiter, even for the root level.
def flatten_string_with_groups(str_with_groups: str) -> str:
"""Determine separator and flatten string specifying group membership via "/".
Applies to variable paths or attributes, even for the root level.
Examples
--------
>>> coord_att = "Time_and_Position/time Time_and_Position/instrument_fov_latitude Time_and_Position/instrument_fov_longitude"
>>> _flatten_coordinate_attribute(coord_att)
>>> flatten_string_with_groups(coord_att)
__Time_and_Position__time __Time_and_Position__instrument_fov_latitude __Time_and_Position__instrument_fov_longitude
Parameters
----------
attribute_string : str
str_with_groups : str
Returns
-------
str
"""
# Use the separator that's in the attribute string only if all separators in the string are the same.
# Otherwise, we will use our own default separator.
whitespaces = re.findall(r"\s+", attribute_string)
if len(set(whitespaces)) <= 1:
whitespaces = re.findall(r"\s+", str_with_groups)
if len(set(whitespaces)) == 0:
new_sep = ""
elif len(set(whitespaces)) == 1:
new_sep = whitespaces[0]
else:
new_sep = concatenator.coord_delim

# A new string is constructed.
return new_sep.join(
f'{concatenator.group_delim}{c.replace("/", concatenator.group_delim)}'
for c in attribute_string.split() # split on any whitespace
for c in str_with_groups.split() # split on any whitespace
)


Expand Down
14 changes: 11 additions & 3 deletions tests/unit/test_attribute_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@

import concatenator
from concatenator.attribute_handling import (
_flatten_coordinate_attribute,
flatten_string_with_groups,
regroup_coordinate_attribute,
)


def test_coordinate_attribute_flattening():
# Case with groups present and double spaces.
assert (
_flatten_coordinate_attribute(
flatten_string_with_groups(
"Time_and_Position/time Time_and_Position/instrument_fov_latitude Time_and_Position/instrument_fov_longitude"
)
== "__Time_and_Position__time __Time_and_Position__instrument_fov_latitude __Time_and_Position__instrument_fov_longitude"
)

# Case with NO groups present and single spaces.
assert (
_flatten_coordinate_attribute(
flatten_string_with_groups(
"time longitude latitude ozone_profile_pressure ozone_profile_altitude"
)
== "__time __longitude __latitude __ozone_profile_pressure __ozone_profile_altitude"
)


def test_variable_path_flattening():
# Case with group present.
assert flatten_string_with_groups("geolocation/time") == "__geolocation__time"

# Case with NO groups present.
assert flatten_string_with_groups("time") == "__time"


def test_coordinate_attribute_regrouping():
# Case with groups present and double spaces.
assert (
Expand Down

0 comments on commit 57985a3

Please sign in to comment.