Skip to content

Commit

Permalink
Merge pull request #209 from cgahr/pad
Browse files Browse the repository at this point in the history
fix #202
  • Loading branch information
roxyboy committed Jul 3, 2024
2 parents 83b0a75 + de84162 commit 60e37a4
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
7 changes: 5 additions & 2 deletions xrft/padding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Functions to pad and unpad a N-dimensional regular grid
"""

import numpy as np
from xarray.core.utils import either_dict_or_kwargs

Expand Down Expand Up @@ -311,7 +312,9 @@ def _pad_coordinates_callback(vector, iaxis_pad_width, iaxis, kwargs):
vector[:n_start] = (
vmin - n_start * spacing + np.linspace(0, spacing * (n_start - 1), n_start)
)
vector[-n_end:] = vmax + spacing + np.linspace(0, spacing * (n_end - 1), n_end)
vector[len(vector) - n_end :] = (
vmax + spacing + np.linspace(0, spacing * (n_end - 1), n_end)
)
return vector


Expand Down Expand Up @@ -438,6 +441,6 @@ def _pad_width_to_slice(pad_width, size):
A slice object for removing the padded elements of the coordinate
array.
"""
if type(pad_width) == int:
if isinstance(pad_width, int):
pad_width = (pad_width, pad_width)
return slice(pad_width[0], size - pad_width[1])
4 changes: 4 additions & 0 deletions xrft/tests/test_padding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Unit tests for padding functions
"""

import pytest
import numpy as np
import xarray as xr
Expand Down Expand Up @@ -208,11 +209,14 @@ def test_unpad_pop_pad_width_attributes(sample_da_2d, pad_width_arg):
(
{"x": 4, "y": 3},
{"x": 4},
{"x": 0},
{"y": 3},
{"x": 4, "y": 0},
{"x": (4, 3), "y": 3},
{"x": (4, 3), "y": (5, 3)},
{"x": (4, 3)},
{"y": (5, 3)},
{"x": (0, 3), "y": (5, 0)},
),
)
def test_unpad_ifft_fft_pad_round_trip(sample_da_2d, pad_width):
Expand Down
1 change: 1 addition & 0 deletions xrft/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Test utility functions of xrft
"""

import pytest
import numpy as np
import pandas as pd
Expand Down
1 change: 1 addition & 0 deletions xrft/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Utility functions for xrft
"""

import numpy as np

from .xrft import _diff_coord
Expand Down
2 changes: 1 addition & 1 deletion xrft/xrft.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _diff_coord(coord):
return np.diff(coord)
elif pd.api.types.is_datetime64_dtype(v0):
diff = np.diff(coord).astype("timedelta64[ns]").astype("f8")
diff = (diff/1e9) #convert back to seconds
diff = diff / 1e9 # convert back to seconds
return diff
else:
return np.diff(coord)
Expand Down

0 comments on commit 60e37a4

Please sign in to comment.