Skip to content

Instantly share code, notes, and snippets.

@adamjstewart
Created December 6, 2023 15:49
Show Gist options
  • Save adamjstewart/7f5324a6a339c20e778f39536402fb4a to your computer and use it in GitHub Desktop.
Save adamjstewart/7f5324a6a339c20e778f39536402fb4a to your computer and use it in GitHub Desktop.
Convert PRISMA imagery from HDF5 to GeoTIFF
import glob
import os
import h5py
import numpy as np
import rasterio as rio
hdf5_dir = "hdf5"
gtif_dir = "gtif"
for file in glob.iglob(os.path.join(hdf5_dir, "*.he5")):
print(file)
with h5py.File(file) as src:
epsg = src.attrs["Epsg_Code"]
west = min(src.attrs["Product_LLcorner_easting"], src.attrs["Product_ULcorner_easting"])
south = min(src.attrs["Product_LLcorner_northing"], src.attrs["Product_LRcorner_northing"])
east = max(src.attrs["Product_LRcorner_easting"], src.attrs["Product_URcorner_easting"])
north = max(src.attrs["Product_ULcorner_northing"], src.attrs["Product_URcorner_northing"])
vnir = src["HDFEOS/SWATHS/PRS_L2D_HCO/Data Fields/VNIR_Cube"][()]
swir = src["HDFEOS/SWATHS/PRS_L2D_HCO/Data Fields/SWIR_Cube"][()]
data = np.concatenate([vnir, swir], axis=1)
data = np.transpose(data, [1, 0, 2])
count, height, width = data.shape
profile = {
"fp": file.replace(hdf5_dir, gtif_dir).replace(".he5", ".tif"),
"mode": "w",
"driver": "GTiff",
"width": width,
"height": height,
"count": count,
"crs": rio.crs.CRS.from_epsg(epsg),
"transform": rio.transform.from_bounds(west, south, east, north, width, height),
"dtype": data.dtype,
}
with rio.open(**profile) as dst:
for i in range(count):
dst.write(data[i], i + 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment