Skip to content

Commit e8c5198

Browse files
committed
Try to set extent if none provided - xarray style
1 parent 38b525e commit e8c5198

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

uxarray/core/dataarray.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,24 @@ def to_raster(
380380
if not isinstance(ax, GeoAxes):
381381
raise TypeError("`ax` must be an instance of cartopy.mpl.geoaxes.GeoAxes")
382382

383+
auto_extent_applied = False
384+
if pixel_mapping is None:
385+
xlim, ylim = ax.get_xlim(), ax.get_ylim()
386+
if np.allclose(xlim, (0.0, 1.0)) and np.allclose(ylim, (0.0, 1.0)):
387+
try:
388+
import cartopy.crs as ccrs
389+
390+
lon = self.uxgrid.node_lon.values
391+
lat = self.uxgrid.node_lat.values
392+
lon_min, lon_max = float(np.nanmin(lon)), float(np.nanmax(lon))
393+
lat_min, lat_max = float(np.nanmin(lat)), float(np.nanmax(lat))
394+
ax.set_extent(
395+
(lon_min, lon_max, lat_min, lat_max), crs=ccrs.PlateCarree()
396+
)
397+
auto_extent_applied = True
398+
except Exception:
399+
pass
400+
383401
pixel_ratio_set = pixel_ratio is not None
384402
if not pixel_ratio_set:
385403
pixel_ratio = 1.0
@@ -403,6 +421,12 @@ def to_raster(
403421
+ input_ax_attrs._value_comparison_message(pm_ax_attrs)
404422
)
405423
pixel_mapping = np.asarray(pixel_mapping, dtype=INT_DTYPE)
424+
elif auto_extent_applied:
425+
warn(
426+
"Axes extent was default; auto-setting from grid lon/lat bounds for rasterization. "
427+
"Set the extent explicitly to control this.",
428+
stacklevel=2,
429+
)
406430

407431
raster, pixel_mapping_np = _nearest_neighbor_resample(
408432
data,

0 commit comments

Comments
 (0)