@@ -137,6 +137,68 @@ def resample_cube_spatial(
137137 data = data , projection = target_crs , resolution = target_resolution , method = method
138138 )
139139
140+ def align_arrays (a , b , res ):
141+ desc = 1
142+ if len (a ) == 1 and len (b ) == 1 :
143+ return a
144+ elif len (a ) == 1 and len (b ) > 1 :
145+ if b [0 ] > b [1 ]:
146+ desc = - 1
147+ elif len (a ) > len (b ):
148+ b0 = np .absolute (b [0 ] - a ).argmin ()
149+ blen = len (b )
150+ close = np .isclose (b , a [b0 : b0 + blen ], atol = res * 0.99 )
151+ if close .all ():
152+ return a [b0 : b0 + blen ]
153+ else :
154+ raise Exception ("Coordinates could not be aligned! " )
155+ elif len (a ) == len (b ):
156+ if b [0 ] > b [1 ]:
157+ if a [0 ] < a [1 ]:
158+ a = np .flip (a )
159+ else :
160+ if a [0 ] > a [1 ]:
161+ a = np .flip (a )
162+ close = np .isclose (b , a , atol = res * 0.99 )
163+ if close .all ():
164+ return a
165+ else :
166+ raise Exception ("Coordinates could not be aligned! " )
167+ else :
168+ if b [0 ] > b [1 ]:
169+ desc = - 1
170+ if a [0 ] < a [1 ]:
171+ a = np .flip (a )
172+ else :
173+ if a [0 ] > a [1 ]:
174+ a = np .flip (a )
175+ a0 = np .absolute (b - a [0 ]).argmin ()
176+ alen = len (a )
177+ close = np .isclose (a , b [a0 : a0 + alen ], atol = res * 0.99 )
178+ if not close .all ():
179+ raise Exception ("Coordinates could not be aligned! " )
180+
181+ new_b = np .arange (b [0 ], a [0 ], res * desc )
182+ new_b = np .append (new_b , a )
183+ new_b = np .append (new_b , np .flip (np .arange (b [- 1 ], a [- 1 ], - res * desc )))
184+ if len (b ) != len (new_b ):
185+ raise Exception ("Coordinates could not be aligned! " )
186+ return new_b
187+
188+ x_data = resampled_data [resampled_data .openeo .x_dim [0 ]].values
189+ x_target = target [target .openeo .x_dim [0 ]].values
190+ if not (len (x_data ) == len (x_target ) and (x_data == x_target ).all ()):
191+ resampled_data [resampled_data .openeo .x_dim [0 ]] = align_arrays (
192+ x_target , x_data , target_resolution
193+ )
194+
195+ y_data = resampled_data [resampled_data .openeo .y_dim [0 ]].values
196+ y_target = target [target .openeo .y_dim [0 ]].values
197+ if not (len (y_data ) == len (y_target ) and (y_data == y_target ).all ()):
198+ resampled_data [resampled_data .openeo .y_dim [0 ]] = align_arrays (
199+ y_target , y_data , target_resolution
200+ )
201+
140202 return resampled_data
141203
142204
0 commit comments