22from nctoolkit .api import open_data , open_thredds
33from nctoolkit .matchpoint import open_matchpoint
44import 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
734def 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 :
0 commit comments