@@ -17,6 +17,9 @@ def _interpolate_value_boundaries_z1d(values, z, dz, threshold, out):
1717 if (n % 2 == 0 and values [k , i , j ] >= threshold ) or (
1818 n % 2 == 1 and values [k , i , j ] <= threshold
1919 ): # exceeding (n even) or falling below threshold (n odd)
20+ if values [k , i , j ] == values [k - 1 , i , j ]:
21+ # edge case: subsequent values equal threshold result in ZeroDivisionError
22+ continue
2023 if not np .isnan (values [k - 1 , i , j ]):
2124 # interpolate z coord of threshold
2225 out [n , i , j ] = z [k - 1 ] + (threshold - values [k - 1 , i , j ]) * (
@@ -42,6 +45,9 @@ def _interpolate_value_boundaries_z3d(values, z, dz, threshold, out):
4245 if (n % 2 == 0 and values [k , i , j ] >= threshold ) or (
4346 n % 2 == 1 and values [k , i , j ] <= threshold
4447 ): # exceeding (n even) or falling below threshold (n odd)
48+ if values [k , i , j ] == values [k - 1 , i , j ]:
49+ # edge case: subsequent values equal threshold result in ZeroDivisionError
50+ continue
4551 if not np .isnan (values [k - 1 , i , j ]):
4652 # interpolate z coord of threshold
4753 out [n , i , j ] = z [k - 1 , i , j ] + (
@@ -69,6 +75,10 @@ def _interpolate_value_boundaries_z3ddz1d(values, z, dz, threshold, out):
6975 if (n % 2 == 0 and values [k , i , j ] >= threshold ) or (
7076 n % 2 == 1 and values [k , i , j ] <= threshold
7177 ): # exceeding (n even) or falling below threshold (n odd)
78+ # if (values[k, i, j] == threshold) and (values[k - 1, i, j] == threshold):
79+ if values [k , i , j ] == values [k - 1 , i , j ]:
80+ # edge case: subsequent values equal threshold result in ZeroDivisionError
81+ continue
7282 if not np .isnan (values [k - 1 , i , j ]):
7383 # interpolate z coord of threshold
7484 out [n , i , j ] = z [k - 1 , i , j ] + (
@@ -111,18 +121,25 @@ def interpolate_value_boundaries(values, z, threshold):
111121
112122 values = values .load ()
113123 out = xr .full_like (values , np .nan )
124+ # if z.dz is a scalar, conform to z
125+ # TODO: broadcast dz to z, so _interpolate_value_boundaries_z3ddz1d is superfluous?
126+ if len (z .dz .dims ) == 0 :
127+ dz = np .ones_like (z ) * z .dz .values
128+ else :
129+ dz = z .dz .values
130+
114131 if len (z .dims ) == 1 :
115132 _interpolate_value_boundaries_z1d (
116- values .values , z .values , z . dz . values , threshold , out .values
133+ values .values , z .values , dz , threshold , out .values
117134 )
118135 else :
119- if len (z .dz .dims ) = = 1 :
136+ if len (z .dz .dims ) < = 1 :
120137 _interpolate_value_boundaries_z3ddz1d (
121- values .values , z .values , z . dz . values , threshold , out .values
138+ values .values , z .values , dz , threshold , out .values
122139 )
123140 else :
124141 _interpolate_value_boundaries_z3d (
125- values .values , z .values , z . dz . values , threshold , out .values
142+ values .values , z .values , dz , threshold , out .values
126143 )
127144 out = out .rename ({"layer" : "boundary" })
128145 out = out .dropna (dim = "boundary" , how = "all" )
0 commit comments