Skip to content

Commit 3668738

Browse files
committed
made data functions more transparent
1 parent 2d7ace0 commit 3668738

4 files changed

Lines changed: 39 additions & 34 deletions

File tree

cryogrid_pytools/data/from_earth_engine.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from functools import lru_cache
22

3-
import xarray as xr
43
import ee
5-
import wxee # noqa
64
import rioxarray # noqa
7-
5+
import wxee # noqa
6+
import xarray as xr
87
from loguru import logger
8+
99
from .utils import _decorator_dataarray_to_bbox
1010

1111

@@ -27,14 +27,16 @@ def gee_auth():
2727

2828

2929
@_decorator_dataarray_to_bbox
30-
def get_modis_albedo_500m(bbox_WSEN) -> xr.DataArray:
30+
def get_modis_albedo_500m(bbox_WSEN, res=500, **kwargs) -> xr.DataArray:
3131
"""
3232
Retrieve MODIS albedo data at 500m resolution for a given bounding box.
3333
3434
Parameters
3535
----------
3636
bbox_WSEN : list or tuple
3737
Bounding box in the format [west, south, east, north].
38+
res : int
39+
Resolution in meters. Default is 500m.
3840
3941
Returns
4042
-------
@@ -69,7 +71,7 @@ def get_modis_albedo_500m(bbox_WSEN) -> xr.DataArray:
6971

7072
# Convert the Earth Engine image to an xarray dataset
7173
ds = (
72-
image_10th_pct.wx.to_xarray(scale=500, region=rio, progress=False)
74+
image_10th_pct.wx.to_xarray(scale=res, region=rio, progress=False)
7375
.pipe(lambda x: x * 0.001) # Scale factor for albedo
7476
.isel(time=0, drop=True)["Albedo_BSA_shortwave_p10"]
7577
.rename("modis_albedo_BSA_shortwave")
@@ -98,14 +100,16 @@ def get_modis_albedo_500m(bbox_WSEN) -> xr.DataArray:
98100

99101

100102
@_decorator_dataarray_to_bbox
101-
def get_aster_ged_100m_v3(bbox_WSEN):
103+
def get_aster_ged_100m_v3(bbox_WSEN, res=100, **kwargs):
102104
"""
103105
Retrieve ASTER GED data at 100m resolution for a given bounding box.
104106
105107
Parameters
106108
----------
107109
bbox_WSEN : list or tuple
108110
Bounding box in the format [west, south, east, north].
111+
res : int
112+
Resolution in meters. Default is 100m.
109113
110114
Returns
111115
-------
@@ -132,7 +136,7 @@ def get_aster_ged_100m_v3(bbox_WSEN):
132136

133137
# Convert the Earth Engine image to an xarray dataset
134138
ds = (
135-
image_clipped.wx.to_xarray(scale=100, region=roi, progress=False)
139+
image_clipped.wx.to_xarray(scale=res, region=roi, progress=False)
136140
.isel(time=0, drop=True)
137141
.assign_attrs(period="2000-2008")
138142
)
@@ -141,7 +145,7 @@ def get_aster_ged_100m_v3(bbox_WSEN):
141145

142146

143147
@_decorator_dataarray_to_bbox
144-
def get_aster_ged_emmis_elev(bbox_WSEN):
148+
def get_aster_ged_emmis_elev(bbox_WSEN, res=100, **kwargs):
145149
"""
146150
Retrieve ASTER GED emissivity and elevation data for a given bounding box.
147151
@@ -161,7 +165,7 @@ def get_aster_ged_emmis_elev(bbox_WSEN):
161165
bbox_WSEN = tuple(bbox_WSEN)
162166

163167
# Retrieve ASTER GED data
164-
ds = get_aster_ged_100m_v3(bbox_WSEN)
168+
ds = get_aster_ged_100m_v3(bbox_WSEN, res=res)
165169

166170
# Process emissivity bands
167171
emissivity_bands = [f"emissivity_band{b:02d}" for b in range(10, 15)]

cryogrid_pytools/data/from_planetary_computer.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def search_stac_items_planetary_computer(collection, bbox, **kwargs) -> list:
5252

5353
@utils._decorator_dataarray_to_bbox
5454
def get_dem_copernicus30(
55-
bbox_WSEN: list, res_m: int = 30, epsg=32643, smoothing_iters=2, smoothing_size=3
55+
bbox_WSEN: list, res: int = 30, epsg=32643, smoothing_iters=2, smoothing_size=3
5656
) -> _xr.DataArray:
5757
"""
5858
Download DEM data from the STAC catalog (default is COP DEM Global 30m).
@@ -61,8 +61,8 @@ def get_dem_copernicus30(
6161
----------
6262
bbox_WSEN : list
6363
The bounding box of the area of interest in WSEN format.
64-
res_m : int
65-
The resolution of the DEM data in meters.
64+
res : int
65+
The resolution of the DEM data in EPSG units.
6666
epsg : int, optional
6767
The EPSG code of the projection of the DEM data. Default is
6868
EPSG:32643 (UTM 43N) for the Pamir region.
@@ -82,13 +82,8 @@ def get_dem_copernicus30(
8282

8383
check_epsg(epsg)
8484

85-
assert res_m >= 30, (
86-
"The resolution must be greater than 30m for the COP DEM Global 30m dataset."
87-
)
88-
res = res_m / 111_111 if epsg == 4326 else res_m
89-
9085
_logger.info(
91-
f"Fetching COP DEM Global data from Planetary Computer (cop-dem-glo-30 @ {res_m}m)"
86+
f"Fetching COP DEM Global data from Planetary Computer (cop-dem-glo-30 @ {res:.2g})"
9287
)
9388
items = search_stac_items_planetary_computer("cop-dem-glo-30", bbox_WSEN)
9489
da_dem = _stackstac.stack(
@@ -112,16 +107,16 @@ def get_dem_copernicus30(
112107

113108

114109
@utils._decorator_dataarray_to_bbox
115-
def get_esa_land_cover(bbox_WSEN: tuple, res_m: int = 30, epsg=32643) -> _xr.DataArray:
110+
def get_esa_land_cover(bbox_WSEN: tuple, res: int = 30, epsg=32643) -> _xr.DataArray:
116111
"""
117112
Get the ESA World Cover dataset on the target grid and resolution.
118113
119114
Parameters
120115
----------
121116
bbox_WSEN : tuple
122117
Bounding box in the format (West, South, East, North).
123-
res_m : int, optional
124-
Resolution in meters. Defaults to 30.
118+
res : int, optional
119+
Resolution in EPSG units. Defaults to 30 (meters for UTM).
125120
epsg : int, optional
126121
EPSG code for the coordinate reference system. Defaults to 32643.
127122
@@ -168,10 +163,10 @@ def get_land_cover_classes(item):
168163
check_epsg(epsg)
169164

170165
# get the units in the projection
171-
res = get_res_in_proj_units(res_m, epsg, min_res=10)
166+
res = get_res_in_proj_units(res, epsg, min_res=10)
172167

173168
_logger.info(
174-
f"Fetching ESA World Cover (v2.0) data from Planetary Computer (esa-worldcover @ {res_m}m)"
169+
f"Fetching ESA World Cover (v2.0) data from Planetary Computer (esa-worldcover @ {res}m)"
175170
)
176171
items = search_stac_items_planetary_computer(
177172
collection="esa-worldcover",
@@ -210,7 +205,7 @@ def get_land_cover_classes(item):
210205

211206

212207
@utils._decorator_dataarray_to_bbox
213-
def get_esri_land_cover(bbox_WSEN: tuple, res_m: int = 30, epsg: int = 32643):
208+
def get_esri_land_cover(bbox_WSEN: tuple, res: int = 30, epsg: int = 32643):
214209
from .utils import _long_string_processor
215210

216211
items = search_stac_items_planetary_computer(
@@ -222,7 +217,7 @@ def get_esri_land_cover(bbox_WSEN: tuple, res_m: int = 30, epsg: int = 32643):
222217
da = _stackstac.stack(
223218
items,
224219
epsg=epsg,
225-
resolution=res_m,
220+
resolution=res,
226221
bounds_latlon=bbox_WSEN,
227222
).isel(time=0, band=0)
228223

@@ -269,7 +264,7 @@ def get_sentinel2_data(
269264
bbox_WSEN: tuple,
270265
years=range(2018, 2025),
271266
assets=["SCL"],
272-
res_m: int = 30,
267+
res: int = 30,
273268
epsg=32643,
274269
max_cloud_cover=5,
275270
) -> _xr.DataArray:
@@ -284,8 +279,8 @@ def get_sentinel2_data(
284279
Range of years to fetch data for. Defaults to range(2018, 2025).
285280
assets : list, optional
286281
List of assets to fetch. Defaults to ['SCL'].
287-
res_m : int, optional
288-
Resolution in meters. Defaults to 30.
282+
res : int, optional
283+
Resolution in EPSG units. Defaults to 30 (meters for UTM).
289284
epsg : int, optional
290285
EPSG code for the coordinate reference system. Defaults to 32643.
291286
max_cloud_cover : int, optional
@@ -299,11 +294,9 @@ def get_sentinel2_data(
299294
from ..utils import drop_coords_without_dim
300295
from .utils import (
301296
check_epsg,
302-
get_res_in_proj_units,
303297
)
304298

305299
check_epsg(epsg)
306-
res = get_res_in_proj_units(res_m, epsg, min_res=10)
307300

308301
da_list = []
309302
for year in years:
@@ -348,7 +341,7 @@ def get_sentinel2_data(
348341

349342
@utils._decorator_dataarray_to_bbox
350343
def get_snow_melt_doy(
351-
bbox_WSEN: tuple, years=range(2018, 2025), res_m: int = 30, epsg=32643
344+
bbox_WSEN: tuple, years=range(2018, 2025), res: int = 30, epsg=32643
352345
) -> _xr.DataArray:
353346
"""
354347
Calculate the snow melt day of year (DOY) from Sentinel-2 SCL data for a given bounding box and years.
@@ -359,7 +352,7 @@ def get_snow_melt_doy(
359352
Bounding box coordinates in the format (West, South, East, North).
360353
years : range, optional
361354
Range of years to consider. Defaults to range(2018, 2025).
362-
res_m : int, optional
355+
res : int, optional
363356
Spatial resolution in meters. Defaults to 30.
364357
epsg : int, optional
365358
EPSG code for the coordinate reference system. Defaults to 32643.
@@ -371,7 +364,7 @@ def get_snow_melt_doy(
371364
"""
372365

373366
da = get_sentinel2_data(
374-
bbox_WSEN, years=years, res_m=res_m, epsg=epsg, max_cloud_cover=10
367+
bbox_WSEN, years=years, res=res, epsg=epsg, max_cloud_cover=10
375368
)
376369

377370
_logger.info("Calculating snow melt day of year (DOY) from Sentinel-2 SCL data")

cryogrid_pytools/data/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ def _decorator_dataarray_to_bbox(func):
2626
The wrapped function with the additional functionality of handling
2727
DataArray and reprojecting the output.
2828
"""
29+
from functools import wraps
30+
31+
import numpy as np
2932

3033
@_cached
34+
@wraps(func)
3135
def wrapper(*args, **kwargs):
3236
if len(args) >= 1:
3337
bbox_or_target = args[0]
@@ -38,6 +42,10 @@ def wrapper(*args, **kwargs):
3842
if isinstance(bbox_or_target, _xr.DataArray):
3943
da = bbox_or_target
4044
bbox = da.rv.get_bbox_latlon()
45+
res = np.abs(da.rio.resolution()).mean()
46+
epsg = da.rio.crs.to_epsg()
47+
kwargs.update(res=res, epsg=epsg)
48+
4149
out = func(bbox, *args, **kwargs)
4250
out = out.rio.reproject_match(da)
4351

cryogrid_pytools/outputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def read_OUT_regridded_files(
234234
"Depths are the same for all gridcells. Setting depth as the dimension."
235235
)
236236
ds = ds.swap_dims(level="depth")
237-
ds = ds.reset_coords("elevation")
237+
ds = ds.reset_coords("elevation").astype("float32")
238238

239239
return ds
240240

0 commit comments

Comments
 (0)