@@ -162,67 +162,47 @@ def get_spatial_region_of_interest(
162162 max_x_and_y_index_height = x_and_y_index_at_center + half_image_size_pixels_height
163163
164164 # Check whether the requested region of interest steps outside of the available data:
165- suggested_reduction_of_image_size_pixels_width = (
166- max (
167- (- min_x_and_y_index_width .min () if (min_x_and_y_index_width < 0 ).any () else 0 ),
168- (min_x_and_y_index_width .x_index_at_center - len (data_array .x_geostationary )),
169- )
170- * 2
165+ # Need to know how much to pad the outputs, so can do that here
166+ min_width_padding = max (- min_x_and_y_index_width .x_index_at_center , 0 )
167+ max_width_padding = max (
168+ max_x_and_y_index_width .x_index_at_center - len (data_array .x_geostationary ), 0
171169 )
172- suggested_reduction_of_image_size_pixels_height = (
173- max (
174- (- max_x_and_y_index_height .min () if (max_x_and_y_index_height < 0 ).any () else 0 ),
175- (max_x_and_y_index_height .y_index_at_center - len (data_array .y_geostationary )),
176- )
177- * 2
170+ min_height_padding = max (- min_x_and_y_index_height .y_index_at_center , 0 )
171+ max_height_padding = max (
172+ max_x_and_y_index_height .y_index_at_center - len (data_array .y_geostationary ), 0
178173 )
179- # If the requested region does step outside the available data then raise an exception
180- # with a helpful message:
181- if (
182- suggested_reduction_of_image_size_pixels_width > 0
183- or suggested_reduction_of_image_size_pixels_height > 0
184- ):
185- new_suggested_image_size_pixels_width = (
186- self ._rectangle .size_pixels_width - suggested_reduction_of_image_size_pixels_width
187- )
188- new_suggested_image_size_pixels_height = (
189- self ._rectangle .size_pixels_height - suggested_reduction_of_image_size_pixels_height
190- )
191- raise RuntimeError (
192- "Requested region of interest of satellite data steps outside of the available"
193- " geographical extent of the Zarr data. The requested region of interest extends"
194- f" from pixel indicies"
195- f" x={ min_x_and_y_index_width .x_index_at_center } to"
196- f" x={ min_x_and_y_index_width .x_index_at_center } ,"
197- f" y={ min_x_and_y_index_width .y_index_at_center } to"
198- f" y={ min_x_and_y_index_width .y_index_at_center } ."
199- f" In the Zarr data, len(x)={ len (data_array .x_geostationary )} ,"
200- f" len(y)={ len (data_array .y_geostationary )} ."
201- f" Try reducing image_size_pixels_height from { self ._rectangle .size_pixels_height } "
202- f" to { new_suggested_image_size_pixels_height } pixels."
203- f" Try reducing image_size_pixels_width from { self ._rectangle .size_pixels_width } "
204- f" to { new_suggested_image_size_pixels_width } pixels."
205- f" { self .history_length = } ; { self .forecast_length = } ; { x_center_osgb = } ;"
206- f" { y_center_osgb = } ; { x_center_geostationary = } ; { y_center_geostationary = } ;"
207- f" { min (data_array .x_geostationary .values )= } ;"
208- f" { max (data_array .x_geostationary .values )= } ;"
209- f" { min (data_array .y_geostationary .values )= } ;"
210- f" { max (data_array .y_geostationary .values )= } ;\n "
211- f" { data_array = } "
212- )
213174
214175 # Select the geographical region of interest.
215176 # Note that isel is *exclusive* of the end of the slice.
216177 # e.g. isel(x=slice(0, 3)) will return the first, second, and third values.
217178 data_array = data_array .isel (
218179 x_geostationary = slice (
219- min_x_and_y_index_width .x_index_at_center , max_x_and_y_index_width .x_index_at_center
180+ max (min_x_and_y_index_width .x_index_at_center , 0 ),
181+ min (max_x_and_y_index_width .x_index_at_center , len (data_array .x_geostationary )),
220182 ),
221183 y_geostationary = slice (
222- min_x_and_y_index_height .y_index_at_center ,
223- max_x_and_y_index_height .y_index_at_center ,
184+ max ( min_x_and_y_index_height .y_index_at_center , 0 ) ,
185+ min ( max_x_and_y_index_height .y_index_at_center , len ( data_array . y_geostationary )) ,
224186 ),
225187 )
188+
189+ # isel is wrong if padding before, so add padding after to be the correct size
190+ # Get the difference for each direction and use that
191+ if (
192+ min_height_padding > 0
193+ or min_width_padding > 0
194+ or max_height_padding > 0
195+ or max_width_padding > 0
196+ ):
197+ data_array = data_array .pad (
198+ pad_width = {
199+ "x_geostationary" : (min_width_padding , max_width_padding ),
200+ "y_geostationary" : (min_height_padding , max_height_padding ),
201+ },
202+ mode = "constant" ,
203+ constant_values = 0 ,
204+ )
205+
226206 return data_array
227207
228208 def get_example (self , location : SpaceTimeLocation ) -> xr .Dataset :
0 commit comments