Skip to content

Commit 154d350

Browse files
cleaned up some tests and some lint, and few issues AI identified.
1 parent 1474b1d commit 154d350

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

gridded/depth.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os
1+
22
import warnings
33

44
import numpy as np
@@ -8,11 +8,7 @@
88
from gridded.utilities import (
99
can_create_class,
1010
get_dataset,
11-
merge_var_search_dicts,
1211
parse_filename_dataset_args,
13-
search_dataset_for_any_long_name,
14-
search_dataset_for_variables_by_longname,
15-
search_dataset_for_variables_by_varname,
1612
search_netcdf_vars,
1713
)
1814

@@ -258,7 +254,8 @@ def interpolation_alphas(
258254
indices = np.ma.MaskedArray(data=idxs, mask=np.zeros((len(idxs)), dtype=bool))
259255

260256
alphas = np.ma.MaskedArray(
261-
data=np.empty((len(points)), dtype=np.float64) * np.nan, mask=np.zeros((len(points)), dtype=bool)
257+
data=np.full((len(points),), np.nan, dtype=np.float64),
258+
mask=np.zeros((len(points)), dtype=bool)
262259
)
263260

264261
# set above surface and below seafloor alphas to allow future filtering
@@ -295,7 +292,7 @@ def interpolation_alphas(
295292

296293
alphas[within_layer] = (depths[within_layer] - L0) / (L1 - L0)
297294

298-
if any(np.isnan(alphas)):
295+
if np.isnan(alphas).any():
299296
raise ValueError("Some alphas are still unmasked and NaN. Please file a bug report")
300297

301298
return indices, alphas
@@ -491,7 +488,9 @@ def from_netCDF(
491488
time = Time.constant_time()
492489
else:
493490
time = Time.from_netCDF(
494-
dataset=zeta_var._grp, # zeta_var should be a netCDF4.Variable, so its _grp attribute should be the dataset it belongs to
491+
# zeta_var should be a netCDF4.Variable,
492+
# so its _grp attribute should be the dataset it belongs to
493+
dataset=zeta_var._grp,
495494
datavar=zeta_var,
496495
origin=time_origin,
497496
displacement=displacement,
@@ -675,13 +674,18 @@ def interpolation_alphas(
675674
# if data_shape[0] == self.num_layers:
676675
# raise NotImplementedError('Interpolation of data on depth layers not supported yet')
677676

678-
depth_profiles = self.get_depth_profile(points, time, data_shape=data_shape, _hash=_hash, extrapolate=extrapolate)
677+
depth_profiles = self.get_depth_profile(points,
678+
time,
679+
data_shape=data_shape,
680+
_hash=_hash,
681+
extrapolate=extrapolate)
679682

680683
indices = np.ma.MaskedArray(
681684
data=-np.ones((len(points)), dtype=np.int64) * 1000, mask=np.zeros((len(points)), dtype=bool)
682685
)
683686
alphas = np.ma.MaskedArray(
684-
data=np.empty((len(points)), dtype=np.float64) * np.nan, mask=np.zeros((len(points)), dtype=bool)
687+
data=np.full((len(points)), np.nan, dtype=np.float64),
688+
mask=np.zeros((len(points)), dtype=bool)
685689
)
686690

687691
# use np.digitize to bin the depths into the layers.
@@ -779,10 +783,16 @@ def _apply_boundary_conditions(
779783
alphas.mask = np.logical_or(alphas.mask, exclusion_mask)
780784
return indices, alphas, oob_mask
781785

782-
783-
def get_s_coordinate(self, points, time, data_shape=None, _hash=None, **kwargs):
786+
def get_s_coordinate(self,
787+
points,
788+
time,
789+
data_shape=None,
790+
_hash=None,
791+
**kwargs):
784792
"""
785-
Given an array of points and a time, returns the S-Coordinate values of the depth layers at those points and time.
793+
Given an array of points and a time, returns the S-Coordinate values
794+
of the depth layers at those points and time.
795+
786796
:param points: array of points to interpolate to
787797
:type points: numpy array of shape (n, 3)
788798
@@ -794,7 +804,8 @@ def get_s_coordinate(self, points, time, data_shape=None, _hash=None, **kwargs):
794804
index on the sigma layers or levels.
795805
:type data_shape: tuple of int
796806
797-
:return: numpy array of shape (n, num_w_levels) of n s-coordinate depth_profiles. 0 reference is mean sea surface.
807+
:return: numpy array of shape (n, num_w_levels) of n s-coordinate
808+
depth_profiles. 0 reference is mean sea surface.
798809
"""
799810
raise NotImplementedError("get_s_coordinate not implemented for S_Depth, required in subclasses")
800811

@@ -812,7 +823,8 @@ def get_depth_profile(self, points, time, data_shape=None, _hash=None, **kwargs)
812823
index on the sigma layers or levels.
813824
:type data_shape: tuple of int
814825
815-
:return: numpy array of shape (n, num_w_levels) of n depth_profiles, referenced to the surface (i.e. surface is 0, seafloor is negative)
826+
:return: numpy array of shape (n, num_w_levels) of n depth_profiles,
827+
referenced to the surface (i.e. surface is 0, seafloor is negative)
816828
"""
817829
z = self.zeta.at(points, time, unmask=False, _hash=_hash, **kwargs)
818830
return self.get_s_coordinate(points, time, data_shape=data_shape, _hash=_hash, **kwargs) + z

gridded/tests/test_depth.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,14 +531,20 @@ def get_database_nc():
531531
"""
532532
L_depth_file = os.path.join(TEST_DATA, "test_L_Depth.nc")
533533

534-
ncfile = nc.Dataset(L_depth_file)
535-
ds = gridded.Dataset(L_depth_file)
534+
# ncfile = nc.Dataset(L_depth_file)
535+
# ds = gridded.Dataset(L_depth_file)
536+
ds = gridded.Dataset.from_netCDF(L_depth_file)
536537
depth = gridded.depth.Depth.from_netCDF(filename=L_depth_file)
537-
time = gridded.time.Time.from_netCDF(filename=L_depth_file, datavar=ncfile["u"])
538+
# what's going on here? We shouldn't have to separately get time?
539+
# after a dataset is loaded?
540+
# but even if so, why not let Time.from_netCDF load the variable?
541+
# time = gridded.time.Time.from_netCDF(filename=L_depth_file, datavar=ncfile["u"])
542+
time = gridded.time.Time.from_netCDF(filename=L_depth_file, varname="time")
538543

539544
return time, depth, ds
540545

541546

547+
542548
class Test_L_Depth:
543549
def test_construction(self, get_l_depth):
544550

0 commit comments

Comments
 (0)