Skip to content

Commit

Permalink
_encode_dataarray to _encode_time_dataarray
Browse files Browse the repository at this point in the history
  • Loading branch information
ctuguinay committed Apr 17, 2024
1 parent 900ae7d commit 2e3d0ee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions echopype/tests/utils/test_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import dask
import warnings

from echopype.utils.coding import _get_auto_chunk, set_netcdf_encodings, _encode_dataarray, DEFAULT_TIME_ENCODING
from echopype.utils.coding import _get_auto_chunk, set_netcdf_encodings, _encode_time_dataarray, DEFAULT_TIME_ENCODING

@pytest.mark.parametrize(
"chunk",
Expand Down Expand Up @@ -72,7 +72,7 @@ def test_set_netcdf_encodings():
assert encoding["var3"]["zlib"] is False

@pytest.mark.unit
def test_encode_dataarray_on_nanosecond_resolution_encoding():
def test_encode_time_dataarray_on_nanosecond_resolution_encoding():
"""Test to ensure that the expected warning / lack of warnings comes up."""
# Create an array with a multiple datetime64 elements
datetime_array = np.array(
Expand All @@ -89,15 +89,15 @@ def test_encode_dataarray_on_nanosecond_resolution_encoding():
# between time differences in 2023 and 1970
with warnings.catch_warnings():
warnings.simplefilter("error")
decoded_datetime_array = _encode_dataarray(
decoded_datetime_array = _encode_time_dataarray(
datetime_array,
)

# Check if datetime_array and decoded_datetime_array are equal
assert np.array_equal(datetime_array, decoded_datetime_array), "Arrays are not equal"

@pytest.mark.unit
def test_encode_dataarray_on_encoded_time_data():
def test_encode_time_dataarray_on_encoded_time_data():
"""Test to ensure that the array equality and expected error hold."""
# Create an array with a multiple datetime64 elements
datetime_array = np.array(
Expand All @@ -121,16 +121,16 @@ def test_encode_dataarray_on_encoded_time_data():
# Check that no warning is raised
with warnings.catch_warnings():
warnings.simplefilter("error")
decoded_datetime_array = _encode_dataarray(
decoded_datetime_array = _encode_time_dataarray(
encoded_datetime_array
)

# Check if datetime_array and decoded_datetime_array are equal
assert np.array_equal(datetime_array, decoded_datetime_array), "Arrays are not equal"

# Check to see if returns empty array
assert np.array_equal(np.empty(0), _encode_dataarray(np.empty(0)))
assert np.array_equal(np.empty(0), _encode_time_dataarray(np.empty(0)))

# Check to see if value error is raised when we pass in an encoded float datetime array
with pytest.raises(ValueError, match="Encoded time data array must be of type ```np.int64```."):
_encode_dataarray(encoded_datetime_array.astype(np.float64))
_encode_time_dataarray(encoded_datetime_array.astype(np.float64))
4 changes: 2 additions & 2 deletions echopype/utils/coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def sanitize_dtypes(ds: xr.Dataset) -> xr.Dataset:
return ds


def _encode_dataarray(da):
def _encode_time_dataarray(da):
"""Encodes and decode datetime64 array similar to writing to file"""
if da.size == 0:
return da
Expand Down Expand Up @@ -132,7 +132,7 @@ def set_time_encodings(ds: xr.Dataset) -> xr.Dataset:
# Examples: ping_time, ping_time_2, time1, time2
if bool(search(r"_time|^time[\d]+$", var)):
new_ds[var] = xr.apply_ufunc(
_encode_dataarray,
_encode_time_dataarray,
da,
keep_attrs=True,
)
Expand Down

0 comments on commit 2e3d0ee

Please sign in to comment.