@@ -155,16 +155,17 @@ def _compute_coherence(numerator, denominator):
155155 return coherence
156156
157157
158- def crop (data : xr .Dataset | xr .DataArray , geom : sg .Polygon ) -> xr .Dataset :
158+ def crop (data : xr .Dataset | xr .DataArray , geom : sg .Polygon | tuple ) -> xr .Dataset :
159159 """Crop a radar image or stack of radar images to the bounding box of a polygon.
160160
161161 Parameters
162162 ----------
163163 data: xr.Dataset | xr.DataArray
164164 The dataset or data array to be cropped in azimuth and range
165- geom: sg.Polygon
165+ geom: sg.Polygon | tuple
166166 shapely.geometry.Polygon in radar coordinates of the area that should be
167- kept, in [azimuth, range] format
167+ kept, in [azimuth, range] format, OR a tuple of the bounding box of the crop in
168+ (min_azimuth, min_range, max_azimuth, max_range) format
168169
169170 Returns
170171 -------
@@ -174,12 +175,31 @@ def crop(data: xr.Dataset | xr.DataArray, geom: sg.Polygon) -> xr.Dataset:
174175 Raises
175176 ------
176177 ValueError
177- If the azimuth or range coordinate does not exist in `data`
178+ - If the azimuth or range coordinate does not exist in `data`
179+ - If `geom` is not `shapely.geometry.Polygon` or `tuple`
180+
181+ AssertionError
182+ - When a tuple is passed to `geom` that falls in one of three categories:
183+ - The tuple does not have exactly 4 entries
184+ - The minimum azimuth coordinate is larger than or equal
185+ to the maximum azimuth coordinate
186+ - The minimum range coordinate is larger than or equal
187+ to the maximum range coordinate
178188 """
179189 if not {"azimuth" , "range" }.issubset (data .dims ):
180190 raise ValueError ("The data must have azimuth and range dimensions." )
181191
182- bounding_box = geom .bounds # returns (min_az, min_r, max_az, max_r)
192+ if isinstance (geom , sg .Polygon ):
193+ bounding_box = geom .bounds # returns (min_az, min_r, max_az, max_r)
194+ elif isinstance (geom , tuple ):
195+ assert len (geom ) == 4 , f"geom as tuple must have four entries, got { len (geom )} "
196+ assert geom [0 ] < geom [2 ], f"geom Azimuth wrong: min { geom [0 ]} >= max { geom [2 ]} "
197+ assert geom [1 ] < geom [3 ], f"geom Range wrong: min { geom [1 ]} >= max { geom [3 ]} "
198+ bounding_box = geom
199+ else :
200+ raise ValueError (
201+ f"geom must be tuple or shapely.geometry.Polygon, is { type (geom )} ."
202+ )
183203 data = data .sel (
184204 azimuth = range (int (bounding_box [0 ]), int (np .ceil (bounding_box [2 ]))+ 1 ),
185205 range = range (int (bounding_box [1 ]), int (np .ceil (bounding_box [3 ]))+ 1 )
0 commit comments