Skip to content

Instantly share code, notes, and snippets.

@asjohnston-asf
Created October 29, 2025 19:32
Show Gist options
  • Select an option

  • Save asjohnston-asf/bcabaffdf0bbc61b3382fc7bd1f243ca to your computer and use it in GitHub Desktop.

Select an option

Save asjohnston-asf/bcabaffdf0bbc61b3382fc7bd1f243ca to your computer and use it in GitHub Desktop.
from osgeo import gdal, osr
from nisar.products.readers import open_product
# get the data bands
gcov = open_product('NISAR_L2_PR_GCOV_045_107_A_031_4005_DHDH_A_20240621T144514_20240621T144551_T00410_N_F_J_001.h5')
hhhh = gcov.getImageDataset(frequency='A', polarization='HHHH')
hvhv = gcov.getImageDataset(frequency='A', polarization='HVHV')
# compute your RGB pixel values here
red = hhhh[:,:]
green = hvhv[:,:]
blue = hhhh[:,:]
# create an RGB raster in memory
grid = gcov.getGeoGridParameters(frequency='A', polarization='HHHH')
driver = gdal.GetDriverByName('MEM')
raster = driver.Create('', grid.width, grid.length, 3, gdal.GDT_Float32)
geotransform = (grid.start_x, grid.spacing_x, 0, grid.start_y, 0, grid.spacing_y)
raster.SetGeoTransform(geotransform)
srs = osr.SpatialReference()
srs.ImportFromEPSG(grid.epsg)
raster.SetProjection(srs.ExportToWkt())
raster.GetRasterBand(1).WriteArray(red)
raster.GetRasterBand(2).WriteArray(green)
raster.GetRasterBand(3).WriteArray(blue)
# write RGB raster to disk as a cloud optimized geotiff
gdal.GetDriverByName('COG').CreateCopy('out.tiff', raster, options=['NUM_THREADS=ALL_CPUS', 'BIGTIFF=YES', 'RESAMPLING=NEAREST'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment