I'm trying to optimize my code a bit because I'm tired of having intermediate files. I use rasterio to reproject grib files in the following manner:
def reproject_raster(in_path, out_path):
# reproject raster to project crs
with rio.open(in_path) as src:
src_crs = src.crs
transform, width, height = calculate_default_transform(src_crs, crs, src.width, src.height, *src.bounds)
kwargs = src.meta.copy()
kwargs.update({
'crs': crs,
'transform': transform,
'width': width,
'height': height})
with rio.open(out_path, 'w', **kwargs) as dst:
for i in range(1, src.count + 1):
reproject(
source=rio.band(src, i),
destination=rio.band(dst, i),
src_transform=src.transform,
src_crs=src.crs,
dst_transform=transform,
dst_crs=crs,
resampling=Resampling.nearest)
return(out_path)
Does anyone know how to do this in-memory so that the function would return something like a rasterio <class 'rasterio.io.DatasetReader'> type instead of saving it to a file?