Skip to content

Commit

Permalink
add clip to bbox argument with true as default
Browse files Browse the repository at this point in the history
  • Loading branch information
egagli committed Apr 15, 2024
1 parent f78a2a7 commit cb11d38
Showing 1 changed file with 41 additions and 20 deletions.
61 changes: 41 additions & 20 deletions easysnowdata/remote_sensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def get_seasonal_mountain_snow_mask(bbox_input,data_product='mountain_snow') ->
chunks=True,
mask_and_scale=True,
)
mountain_snow_da = mountain_snow_da.rio.clip_box(xmin, ymin, xmax, ymax, crs='EPSG:4326').squeeze()
mountain_snow_da = mountain_snow_da.rio.clip_box(xmin, ymin, xmax, ymax, crs='EPSG:4326').squeeze().astype('float32')

mountain_snow_da.attrs['class_info'] = class_dict

Expand Down Expand Up @@ -1652,11 +1652,12 @@ class MODIS_snow:


def __init__(
self,bbox_input, start_date="2000-01-01", end_date=today, data_product="MOD10A2", bands=None, resolution=None, crs=None
self,bbox_input, clip_to_bbox=True, start_date="2000-01-01", end_date=today, data_product="MOD10A2", bands=None, resolution=None, crs=None
):

self.bbox_input = bbox_input
self.bbox_gdf = convert_bbox_to_geodataframe(bbox_input)
self.clip_to_bbox = clip_to_bbox
self.start_date = start_date
self.end_date = end_date
self.data_product = data_product
Expand Down Expand Up @@ -1701,10 +1702,10 @@ def get_data(self):

load_params = {
"items": self.search.item_collection(),
"bbox": self.bbox_gdf.total_bounds,
"chunks": {"time": 1, "x": 512, "y": 512},
}

if self.clip_to_bbox:
load_params["bbox"] = self.bbox_gdf.total_bounds
if self.bands:
load_params["bands"] = self.bands
if self.crs:
Expand Down Expand Up @@ -1732,23 +1733,43 @@ def get_data(self):

xmin, ymin, xmax, ymax = self.bbox_gdf.total_bounds

modis_snow = xr.concat(
[
rxr.open_rasterio(file, variable="CGF_NDSI_Snow_Cover", chunks={})[
"CGF_NDSI_Snow_Cover"
]
.squeeze()
.rio.clip_box(xmin, ymin, xmax, ymax, crs="EPSG:4326")
.assign_coords(
time=pd.to_datetime(
rxr.open_rasterio(file, variable="CGF_NDSI_Snow_Cover", chunks={}).squeeze().attrs["RANGEBEGINNINGDATE"]
if self.clip_to_bbox:
modis_snow = xr.concat(
[
rxr.open_rasterio(file, variable="CGF_NDSI_Snow_Cover", chunks={})[
"CGF_NDSI_Snow_Cover"
]
.squeeze()
.rio.clip_box(xmin, ymin, xmax, ymax, crs="EPSG:4326")
.assign_coords(
time=pd.to_datetime(
rxr.open_rasterio(file, variable="CGF_NDSI_Snow_Cover", chunks={}).squeeze().attrs["RANGEBEGINNINGDATE"]
)
)
)
.drop_vars("band")
for file in files
],
dim="time",
)
.drop_vars("band")
for file in files
],
dim="time",
)

else:
modis_snow = xr.concat(
[
rxr.open_rasterio(file, variable="CGF_NDSI_Snow_Cover", chunks={})[
"CGF_NDSI_Snow_Cover"
]
.squeeze()
.assign_coords(
time=pd.to_datetime(
rxr.open_rasterio(file, variable="CGF_NDSI_Snow_Cover", chunks={}).squeeze().attrs["RANGEBEGINNINGDATE"]
)
)
.drop_vars("band")
for file in files
],
dim="time",
)


else:
raise ValueError("Data product not recognized. Please choose 'MOD10A1', 'MOD10A2', or 'MOD10A1F'.")
Expand Down

0 comments on commit cb11d38

Please sign in to comment.