Skip to content

Commit e7c738a

Browse files
committed
improve depth direction handling
1 parent 5b5ef66 commit e7c738a

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

nctoolkit/mp_adders.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,33 @@
22
from nctoolkit.api import open_data, open_thredds
33
from nctoolkit.matchpoint import open_matchpoint
44
import nctoolkit.api as api
5+
import xarray as xr
6+
7+
def is_z_down(ff):
8+
import netCDF4 as nc
9+
try:
10+
ds = open_data(ff, checks = False)
11+
var = ds.variables[0]
12+
ds = xr.open_dataset(ff)
13+
for x in ds[var].metpy.coordinates("longitude"):
14+
lon_name = x.name
15+
for x in ds[var].metpy.coordinates("latitude"):
16+
lat_name = x.name
17+
for x in ds[var].metpy.coordinates("time"):
18+
time_name = x.name
19+
coords = [x for x in list(ds.coords) if x not in [lon_name, lat_name, time_name]]
20+
ds = nc.Dataset(ff)
21+
if len(coords) == 1:
22+
23+
z = ds.variables[coords[0]]
24+
if hasattr(z, 'positive'):
25+
if z.positive == 'down':
26+
return True
27+
else:
28+
return False
29+
raise ValueError("Could not determine if z-axis is down from the provided file.")
30+
except:
31+
raise ValueError("Could not determine if z-axis is down from the provided file.")
532

633

734
def match_points(
@@ -67,7 +94,7 @@ def match_points(
6794
return mp.values
6895

6996

70-
def add_data(self, x=None, variables=None, depths=None, nan=None, top=False, quiet = False, **kwargs):
97+
def add_data(self, x=None, variables=None, depths=None, nan=None, top=False, quiet = False, direction_positive = None, **kwargs):
7198
"""
7299
Add dataset for matching
73100
@@ -91,6 +118,15 @@ def add_data(self, x=None, variables=None, depths=None, nan=None, top=False, qui
91118
Set to True to suppress output
92119
93120
"""
121+
122+
invert = False
123+
if direction_positive is not None:
124+
# must be down or up
125+
if direction_positive not in ["up", "down"]:
126+
raise ValueError("direction_positive must be True or False")
127+
if direction_positive != "down":
128+
invert = True
129+
94130
thredds = False
95131
try:
96132
if len(x.history) == 0:
@@ -134,6 +170,11 @@ def add_data(self, x=None, variables=None, depths=None, nan=None, top=False, qui
134170
"Unable to derive depths from the dataset! Please provide them."
135171
)
136172

173+
try:
174+
if is_z_down(self.depths[0]) is False:
175+
invert = True
176+
except:
177+
pass
137178
if depths is None:
138179
if self.points is not None:
139180
if "depth" in self.points:
@@ -157,6 +198,7 @@ def add_data(self, x=None, variables=None, depths=None, nan=None, top=False, qui
157198
self.data = open_data(x, checks=False)
158199
# ds_vars = open_data(self.data[0], checks = False)
159200

201+
160202
if variables is None:
161203
if quiet is False:
162204
print("All variables will be used")
@@ -221,6 +263,10 @@ def add_data(self, x=None, variables=None, depths=None, nan=None, top=False, qui
221263
else:
222264
self.data = open_data(x, checks=False)
223265

266+
if invert:
267+
self.depths.invert_levels()
268+
self.data.invert_levels()
269+
224270
self.variables = variables
225271

226272
if len(kwargs) > 0:

nctoolkit/mp_matchers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def match_points(
1212
regrid="bil",
1313
max_extrap=5,
1414
quiet = False,
15+
direction_positive = None,
1516
**kwargs,
1617
):
1718
"""
@@ -48,6 +49,11 @@ def match_points(
4849
Maximum distance for extrapolation. Defaults to 5.
4950
quiet: bool
5051
Set to True to suppress output
52+
direction_positive: str
53+
Set to "down" if the netcdf data is ordered from shallower to deeper depths only.
54+
Set to "up" if the netcdf data is ordered from deeper to shallower depths only.
55+
Default is None, which will parse it from the files
56+
Most netCDF files are ordered from shallower to deeper depths.
5157
kwargs: kwargs
5258
Additional arguments to send to assign
5359
@@ -87,7 +93,7 @@ def match_points(
8793
if x in df.columns:
8894
self.points_temporal = True
8995

90-
mp.add_data(x=ds, depths=depths, variables=variables, top=top, nan=nan, quiet = quiet, **kwargs)
96+
mp.add_data(x=ds, depths=depths, variables=variables, top=top, nan=nan, quiet = quiet, direction_positive = direction_positive, **kwargs)
9197
mp.add_points(df, quiet = quiet)
9298
mp.matchup(tmean=tmean, regrid=regrid, max_extrap=max_extrap, quiet = quiet)
9399
return mp.values

0 commit comments

Comments
 (0)