Как конвертировать данные Zarr в GeoTIFF?

#python-xarray #google-earth-engine #geotiff #rasterio #zarr

Вопрос:

Я хочу загрузить данные прогноза HRRR в движок Google Earth, поэтому я думаю, что мне нужно преобразовать их в GeoTIFF.

напр.

 import xarray as xr
import s3fs
fs = s3fs.S3FileSystem(anon=True)

urls = ["s3://hrrrzarr/sfc/20210915/20210915_12z_fcst.zarr/surface/APCP_acc_fcst",
        "s3://hrrrzarr/sfc/20210915/20210915_12z_fcst.zarr/surface/APCP_acc_fcst/surface"]
ds = xr.open_mfdataset([s3fs.S3Map(url, s3=fs) for url in urls], engine="zarr")

# Now convert to geotiff, e.g. ds.to_geotiff().save("example.tif")
 

Ответ №1:

Используйте rasterio и rioxarray, чтобы закончить этот пример:

 
# Open zarr data in xarray
import xarray as xr
import s3fs
fs = s3fs.S3FileSystem(anon=True)

urls = ["s3://hrrrzarr/sfc/20210915/20210915_12z_fcst.zarr/surface/APCP_acc_fcst",
        "s3://hrrrzarr/sfc/20210915/20210915_12z_fcst.zarr/surface/APCP_acc_fcst/surface"]
ds = xr.open_mfdataset([s3fs.S3Map(url, s3=fs) for url in urls], engine="zarr")

# Assign CRS projection using rioxarray
import rioxarray
from pyproj import CRS

ds = ds.rename(projection_x_coordinate="x", projection_y_coordinate="y")
crs = CRS.from_cf({"grid_mapping_name":"lambert_conformal_conic", "longitude_of_central_meridian":-97.5,
                             "latitude_of_projection_origin": 38.5,
                             "standard_parallel":38.5})
ds = ds.rio.write_crs(crs, inplace=True)
ds["APCP_acc_fcst"] = ds["APCP_acc_fcst"].astype("float64") # Original data uses short floats which cause an error
ds = ds.rio.reproject("EPSG:4326") # Arbitrarily picked a projection since GEE seems to want projections w/ official codes

# Save as GeoTiff
ds["APCP_acc_fcst"].rio.to_raster("example.tif")