Skip to content

Commit

Permalink
⛈️ itemsize
Browse files Browse the repository at this point in the history
;
  • Loading branch information
Jason Leaver committed Jun 19, 2024
1 parent 64dec55 commit 2e1e3d0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
33 changes: 7 additions & 26 deletions src/nzthermo/_core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ cdef T[:] dispatch(
T[:] out

N, Z = temperature.shape[0], pressure.shape[1]
out = np.empty((N,), dtype=np.float64 if sizeof(double) == pressure.itemsize else np.float32)
out = np.empty((N,), dtype=np.dtype(f"f{pressure.itemsize}"))

with nogil:
if BROADCAST is mode:
Expand Down Expand Up @@ -534,9 +534,6 @@ cdef void parcel_profile_with_lcl_1d(
moist_lapse_1d(pt[stop + 1:], pressure[stop:], lcl.pressure, lcl.temperature)





cdef T[:, :, :] parcel_profile_with_lcl_2d(
T[:, :] pressure,
T[:, :] temperature,
Expand All @@ -548,13 +545,8 @@ cdef T[:, :, :] parcel_profile_with_lcl_2d(
T[:, :, :] out

N, Z = temperature.shape[0], pressure.shape[1] + 1
out = np.full(
(4, N, Z),
fill_value=NaN,
dtype=np.float64 if sizeof(double) == pressure.itemsize else np.float32
)
# cdef long[:] indcies = np.argmax(np.isnan(pressure), axis=1)
# print(indcies)
out = np.full((4, N, Z), fill_value=NaN, dtype=np.dtype(f"f{pressure.itemsize}"))

with nogil, parallel():
if BROADCAST is mode:
for i in prange(N, schedule='dynamic'):
Expand All @@ -569,9 +561,6 @@ cdef T[:, :, :] parcel_profile_with_lcl_2d(
)
else: # MATRIX
for i in prange(N, schedule='dynamic'):



parcel_profile_with_lcl_1d(
out[0, i, :],
out[1, i, :],
Expand All @@ -581,12 +570,7 @@ cdef T[:, :, :] parcel_profile_with_lcl_2d(
temperature[i, :],
dewpoint[i, :],
)

print(
'nanvalues',
np.argmin(~np.isnan(pressure), axis=1).any()
)


return out


Expand All @@ -606,15 +590,13 @@ def parcel_profile_with_lcl(np.ndarray pressure, np.ndarray temperature, np.ndar
temperature.astype(np.float64),
dewpoint.astype(np.float64),
mode,

)
else:
out[...] = parcel_profile_with_lcl_2d[float](
pressure.astype(np.float32),
temperature.astype(np.float32),
dewpoint.astype(np.float32),
mode,

)

return out[0], out[1], out[2], out[3]
Expand All @@ -633,7 +615,7 @@ cdef T[:] _interpolate_nz(
T[:] out

N, Z = x.shape[0], xp.shape[0]
out = np.empty(N, dtype=np.float32 if sizeof(float) == x.itemsize else np.float64)
out = np.empty(N, dtype=np.dtype(f"f{x.itemsize}"))
with nogil, parallel():
for n in prange(N, schedule='runtime'):
out[n] = C.interpolate_1d(x[n], &xp[0], &fp[n, 0], Z)
Expand Down Expand Up @@ -700,7 +682,6 @@ def interpolate_nz(
array([296.63569648, 296.79664494, 296.74736566, 297.07070398, 297.54936596]),
array([295.07855875, 294.79437914, 295.27081714, 295.4858194, 296.31665617])
)
"""
dtype = __x.dtype
cdef np.ndarray xp = np.asarray(__xp, dtype=dtype)
Expand All @@ -713,7 +694,7 @@ def interpolate_nz(
else:
out[i] = _interpolate_nz[float](__x, xp, fp[i], log_x)

if interp_nan:
if interp_nan:
mask = np.isnan(out[i])
out[i, mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask), __x[~mask])

Expand Down Expand Up @@ -770,7 +751,7 @@ def intersect(

if increasing is False and direction == 'increasing':
increasing = True

out = np.empty((2, a.shape[0]), x.dtype)
if x.dtype == np.float64:
out[...] = intersect_2d[double](x, a, b, mode, log_x, increasing, bottom)
Expand Down
12 changes: 8 additions & 4 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,8 +1194,8 @@ def test_lfc_metpy_regression(which) -> None:
# ............................................................................................... #
# nzthermo.core.most_unstable_parcel
# ............................................................................................... #
@pytest.mark.mu
@pytest.mark.cape
@pytest.mark.mu_cape
@pytest.mark.regression
@pytest.mark.parametrize("depth", [30000.0])
def test_most_unstable_parcel_index(depth) -> None:
Expand All @@ -1213,9 +1213,10 @@ def test_most_unstable_parcel_index(depth) -> None:
)


@pytest.mark.mu
@pytest.mark.cape
@pytest.mark.mu_cape
@pytest.mark.regression
@pytest.mark.most_unstable_cape
@pytest.mark.parametrize("depth", [30000.0])
def test_most_unstable_parcel(depth) -> None:
p, t, td, idx = most_unstable_parcel(
Expand Down Expand Up @@ -1276,6 +1277,7 @@ def test_most_unstable_parcel(depth) -> None:
t_ = t_.m[m]
td_ = td_.m[m]
mu_profile_ = mu_profile_.m[m]

if np.isnan(t_[0]):
continue

Expand Down Expand Up @@ -1332,8 +1334,10 @@ def test_cape_cin_metpy_regression(which_lfc, which_el) -> None:
# ............................................................................................... #
# nzthermo.core.most_unstable_cape_cin
# ............................................................................................... #
@pytest.mark.most_unstable_cape_cin
@pytest.mark.mu
@pytest.mark.cape_cin
@pytest.mark.regression
@pytest.mark.most_unstable_cape_cin
@pytest.mark.parametrize(
"depth",
[30000.0],
Expand Down Expand Up @@ -1362,4 +1366,4 @@ def test_most_unstable_cape_cin_metpy_regression(depth) -> None:
Td[i] * K,
depth=depth * Pa,
)
assert_allclose(CAPE[i], CAPE_.m, atol=1000)
assert_allclose(CAPE[i], CAPE_.m, atol=900)

0 comments on commit 2e1e3d0

Please sign in to comment.