Skip to content

Commit

Permalink
Merge pull request #101 from ocefpaf/numpy20
Browse files Browse the repository at this point in the history
Fix for numpy 2.0...
  • Loading branch information
ocefpaf authored Jul 31, 2024
2 parents 86ffec5 + 10772d8 commit a7954cf
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
python-version: [ "3.10", "3.11", "3.12" ]
os: [ubuntu-latest]
fail-fast: false

Expand Down
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ repos:
- id: blackdoc

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.3.5
rev: v0.5.5
hooks:
- id: ruff

- repo: https://github.com/psf/black
rev: 24.3.0
rev: 24.4.2
hooks:
- id: black
language_version: python3

- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
rev: v2.3.0
hooks:
- id: codespell
args:
Expand All @@ -51,6 +51,6 @@ repos:
- id: add-trailing-comma

- repo: https://github.com/tox-dev/pyproject-fmt
rev: "1.7.0"
rev: "2.2.0"
hooks:
- id: pyproject-fmt
14 changes: 7 additions & 7 deletions oceans/RPSstuff.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def h2hms(hours):
Examples
--------
>>> h2hms(12.51)
(12.0, 30.0, 36.0)
(np.float64(12.0), np.float64(30.0), np.float64(36.0))
"""
hour = np.floor(hours)
Expand All @@ -33,7 +33,7 @@ def hms2h(h, m=None, s=None):
12.51
>>> # Or,
>>> hms2h(123036)
12.51
np.float64(12.51)
"""
if not m and not s:
Expand All @@ -55,7 +55,7 @@ def ms2hms(millisecs):
Examples
--------
>>> ms2hms(1e3 * 60)
(0.0, 1.0, 0.0)
(np.float64(0.0), np.float64(1.0), np.float64(0.0))
"""
sec = np.round(millisecs / 1000)
Expand Down Expand Up @@ -233,7 +233,7 @@ def s2hms(secs):
Examples
--------
>>> s2hms(3600 + 60 + 1)
(1.0, 1.0, 1)
(np.float64(1.0), np.float64(1.0), np.int64(1))
"""
hr = np.floor(secs / 3600)
Expand Down Expand Up @@ -509,7 +509,7 @@ def fixcoast(coast):
"""

ind = coast == -99999.0
coast[ind] = np.NaN
coast[ind] = np.nan

ind = np.where(np.isnan(coast[:, 0]))[0]
dind = np.diff(ind)
Expand All @@ -518,9 +518,9 @@ def fixcoast(coast):
coast = np.delete(coast, ind[idup], axis=0)

if not np.isnan(coast[0, 0]):
coast = np.insert(coast, 0, np.NaN, axis=0)
coast = np.insert(coast, 0, np.nan, axis=0)

if not np.isnan(coast[-1, -1]):
coast = np.append(coast, np.c_[np.NaN, np.NaN], axis=0)
coast = np.append(coast, np.c_[np.nan, np.nan], axis=0)

return coast
4 changes: 2 additions & 2 deletions oceans/colormaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ def load_cmap(fname):
}

# Data colormaps.
for fname in glob("%s/*.dat" % cmap_path):
for fname in glob(f"{cmap_path}/*.dat"):
cmap = os.path.basename(fname).split(".")[0]
data = load_cmap(fname)
arrays.update({cmap: data})

cm = Bunch()
for key, value in arrays.items():
cm.update({key: cmat2cmpl(value)})
cm.update({"%s_r" % key: cmat2cmpl(value, reverse=True)})
cm.update({f"{key}_r": cmat2cmpl(value, reverse=True)})


def demo():
Expand Down
2 changes: 1 addition & 1 deletion oceans/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _woa_variable(variable):


def _woa_url(variable, time_period, resolution):
base = "https://data.nodc.noaa.gov/thredds/dodsC"
base = "https://www.ncei.noaa.gov/thredds-ocean/dodsC"

v = _woa_variable(variable)

Expand Down
18 changes: 9 additions & 9 deletions oceans/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def smoo2(A, hei, wid, kind="hann", badflag=-9999, beta=14):
# Checking window type and dimensions
kinds = ["hann", "hamming", "blackman", "bartlett", "kaiser"]
if kind not in kinds:
raise ValueError("Invalid window type requested: %s" % kind)
raise ValueError(f"Invalid window type requested: {kind}")

if (np.mod(hei, 2) == 0) or (np.mod(wid, 2) == 0):
raise ValueError("Window dimensions must be odd")
Expand All @@ -205,7 +205,7 @@ def smoo2(A, hei, wid, kind="hann", badflag=-9999, beta=14):
A = np.asanyarray(A)
Fnan = np.isnan(A)
imax, jmax = A.shape
As = np.NaN * np.ones((imax, jmax))
As = np.nan * np.ones((imax, jmax))

for i in range(imax):
for j in range(jmax):
Expand Down Expand Up @@ -257,7 +257,7 @@ def smoo2(A, hei, wid, kind="hann", badflag=-9999, beta=14):
a = Ac * wdwc
As[i, j] = a.sum() / wdwc.sum()
# Assigning NaN to the positions holding NaNs in the original array.
As[Fnan] = np.NaN
As[Fnan] = np.nan

return As

Expand Down Expand Up @@ -302,7 +302,7 @@ def weim(x, N, kind="hann", badflag=-9999, beta=14):
# Checking window type and dimensions.
kinds = ["hann", "hamming", "blackman", "bartlett", "kaiser"]
if kind not in kinds:
raise ValueError("Invalid window type requested: %s" % kind)
raise ValueError(f"Invalid window type requested: {kind}")

if np.mod(N, 2) == 0:
raise ValueError("Window size must be odd")
Expand All @@ -329,7 +329,7 @@ def weim(x, N, kind="hann", badflag=-9999, beta=14):
ln = (N - 1) / 2
lx = x.size
lf = lx - ln
xs = np.NaN * np.ones(lx)
xs = np.nan * np.ones(lx)

# Eliminating bad data from mean computation.
fbad = x == badflag
Expand Down Expand Up @@ -459,7 +459,7 @@ def medfilt1(x, L=3):
msg = "Input sequence has to be 1d: ndim = {}".format
raise ValueError(msg(xin.ndim))

xout = np.zeros_like(xin) + np.NaN
xout = np.zeros_like(xin) + np.nan

Lwing = (L - 1) // 2

Expand Down Expand Up @@ -538,7 +538,7 @@ def md_trenberth(x):
>>> filtered = md_trenberth(x)
>>> fig, ax = plt.subplots()
>>> (l1,) = ax.plot(t, x, label="original")
>>> pad = [np.NaN] * 5
>>> pad = [np.nan] * 5
>>> (l2,) = ax.plot(t, np.r_[pad, filtered, pad], label="filtered")
>>> legend = ax.legend()
Expand Down Expand Up @@ -598,9 +598,9 @@ def pl33tn(x, dt=1.0, T=33.0, mode="valid", t=None):
>>> filtered_33d3 = pl33tn(x, dt=4.0, T=72.0) # 3 day filter
>>> fig, ax = plt.subplots()
>>> (l1,) = ax.plot(t, x, label="original")
>>> pad = [np.NaN] * 8
>>> pad = [np.nan] * 8
>>> (l2,) = ax.plot(t, np.r_[pad, filtered_33, pad], label="33 hours")
>>> pad = [np.NaN] * 17
>>> pad = [np.nan] * 17
>>> (l3,) = ax.plot(t, np.r_[pad, filtered_33d3, pad], label="3 days")
>>> legend = ax.legend()
Expand Down
15 changes: 8 additions & 7 deletions oceans/ocfis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gsw
import numpy as np
import numpy.ma as ma
import pandas as pd


def spdir2uv(spd, ang, deg=False):
Expand Down Expand Up @@ -583,7 +584,7 @@ def bin_dates(self, freq, tz=None):
>>> from pandas import Series, date_range
>>> n = 24 * 30
>>> sig = np.random.rand(n) + 2 * np.cos(2 * np.pi * np.arange(n))
>>> dates = date_range(start="1/1/2000", periods=n, freq="H")
>>> dates = date_range(start="1/1/2000", periods=n, freq="h")
>>> series = Series(data=sig, index=dates)
>>> new_series = bin_dates(series, freq="D", tz=None)
Expand All @@ -593,7 +594,7 @@ def bin_dates(self, freq, tz=None):
new_index = date_range(start=self.index[0], end=self.index[-1], freq=freq, tz=tz)
new_series = self.groupby(new_index.asof).mean()
# Averages at the center.
secs = new_index.freq.delta.total_seconds()
secs = pd.Timedelta(new_index.freq).total_seconds()
new_series.index = new_series.index.values + int(secs // 2)
return new_series

Expand Down Expand Up @@ -625,7 +626,7 @@ def series_spline(self):

def despike(self, n=3, recursive=False):
"""
Replace spikes with np.NaN.
Replace spikes with np.nan.
Removing spikes that are >= n * std.
default n = 3.
Expand All @@ -638,12 +639,12 @@ def despike(self, n=3, recursive=False):
)

removed = np.count_nonzero(outliers)
result[outliers] = np.NaN
result[outliers] = np.nan

counter = 0
if recursive:
while outliers.any():
result[outliers] = np.NaN
result[outliers] = np.nan
base = np.abs(result - np.nanmean(result))
outliers = base >= n * np.nanstd(result)
counter += 1
Expand All @@ -658,7 +659,7 @@ def pol2cart(theta, radius, units="deg"):
Examples
--------
>>> pol2cart(0, 1, units="deg")
(1.0, 0.0)
(np.float64(1.0), np.float64(0.0))
"""
if units in ["deg", "degs"]:
Expand Down Expand Up @@ -781,7 +782,7 @@ def get_profile(x, y, f, xi, yi, mode="nearest", order=3):
return map_coordinates(f, coords, mode=mode, order=order)


def strip_mask(arr, fill_value=np.NaN):
def strip_mask(arr, fill_value=np.nan):
"""
Take a masked array and return its data(filled) + mask.
Expand Down
2 changes: 1 addition & 1 deletion oceans/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def level_colormap(levels, cmap=None):
cdict = {"red": tuple(R), "green": tuple(G), "blue": tuple(B)}

return matplotlib.colors.LinearSegmentedColormap(
"%s_levels" % cmap.name,
f"{cmap.name}_levels",
cdict,
256,
)
Expand Down
4 changes: 2 additions & 2 deletions oceans/sw_extras/gamma_GP_from_SP_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def gamma_GP_from_SP_pt(SP, pt, p, lon, lat):
gamma_Pac = gamma_G_pacific(SP, pt)
gamma_Ind = gamma_G_indian(SP, pt)
gamma_SOce = gamma_G_southern_ocean(SP, pt, p)
# gamma_Arc = np.zeros_like(SP) * np.NaN
# gamma_Arc = np.zeros_like(SP) * np.nan

# Definition of the Indian part.
io_lon = np.array(
Expand Down Expand Up @@ -590,7 +590,7 @@ def gamma_GP_from_SP_pt(SP, pt, p, lon, lat):
gamma_GP = w_so * gamma_SOce + (1.0 - w_so) * gamma_middle

# Set NaN in the arctic region.
gamma_GP[lat > 66.0] = np.NaN
gamma_GP[lat > 66.0] = np.nan

# De-normalization.
gamma_GP = 20.0 * gamma_GP - 20
Expand Down
8 changes: 4 additions & 4 deletions oceans/sw_extras/sw_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def cor_beta(lat):
--------
>>> import oceans.sw_extras.sw_extras as swe
>>> swe.cor_beta(0)
2.2891225867210798e-11
np.float64(2.2891225867210798e-11)
References
----------
Expand Down Expand Up @@ -358,7 +358,7 @@ def inertial_period(lat):
>>> import oceans.sw_extras.sw_extras as swe
>>> lat = 30.0
>>> swe.inertial_period(lat) / 3600
23.93484986278565
np.float64(23.93484986278565)
"""
lat = np.asanyarray(lat)
Expand Down Expand Up @@ -427,7 +427,7 @@ def visc(s, t, p):
--------
>>> import oceans.sw_extras.sw_extras as swe
>>> swe.visc(40.0, 40.0, 1000.0)
8.200192496633804e-07
np.float64(8.200192496633804e-07)
Modifications: Original 1998/01/19 - Ayal Anis 1998
Expand Down Expand Up @@ -466,7 +466,7 @@ def tcond(s, t, p):
--------
>>> import oceans.sw_extras.sw_extras as swe
>>> swe.tcond(35, 20, 0)
0.5972445569999999
np.float64(0.5972445569999999)
References
----------
Expand Down
6 changes: 3 additions & 3 deletions oceans/sw_extras/waves.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def __init__(self, h, T=None, L=None, thetao=None, Ho=None, lat=None):
self.Ks = np.sqrt(1 / (1 + self.G) / np.tanh(self.k * self.h))

if thetao is None:
self.theta = np.NaN
self.Kr = np.NaN
self.theta = np.nan
self.Kr = np.nan
if thetao is not None:
self.theta = np.rad2deg(
np.asin(self.C / self.Co * np.sin(np.deg2rad(self.thetao))),
Expand All @@ -155,6 +155,6 @@ def __init__(self, h, T=None, L=None, thetao=None, Ho=None, lat=None):
)

if Ho is None:
self.H = np.NaN
self.H = np.nan
if Ho is not None:
self.H = self.Ho * self.Ks * self.Kr
Loading

0 comments on commit a7954cf

Please sign in to comment.