I’m programmatically creating geotiff files using gdal from python and data based on S2 images. How can I include metadata such as “spectal wavelength” to be able to use the spectrum view?
What I did so far:
import gdal
driver = gdal.GetDriverByName('GTiff')
fgt = driver.Create("./S2_L2A.tif",shape[0]//10,shape[1]//10,shape[2],gdal.GDT_Int16)
for iband,band in enumerate(bands):
print(iband,band)
rb = fgt.GetRasterBand(iband + 1)
rb.WriteArray(data[::10,::10,iband]*10000)
rb.SetDescription(band)
rb.SetMetadataItem("Name",str(band))
rb.SetMetadataItem("name",str(band))
rb.SetMetadataItem("Spectral Wavelength",str(iband))
rb.SetMetadataItem("Spectral Bandwidth",str(iband))
rb.FlushCache()
del fgt
Thanks for your input, but this one is not related to ENVI, nor was it involved. The data comes from S2, but for the question it doesn’t matter much.
The question is how to include metadata using python and gdal, such that the spectrum view from SNAP works. Basically, it means that one could use SNAP for viewing a wider range of data.
The GeoTiff reader in SNAP does not consider metadata like this, because there is no standard defined.
However, when SNAP is writing a GeoTiff file it adds the content of a BEAM-DIMAP file as an attribute.
The tiff tag number for this metadata is 65000.
So you could create a template product in SNAP having the spectral wavelength and band width and save it as BEAM-DIMAP product. The content of the *.dim file can be added to the GeoTiff file.
But I’m interested in how gdal persists the data you have set in your code. Do you have a little example file?
This sounds like a way to acieve the goal, however I don’t know how to add the *.dim file to a GeoTiff. Could you share the details? If SNAP would introduce some way to add this metadata to the GeoTiff itself, this would be great. Attached to this post is a dummy sample image with some metadata as shown in the original post. The corresponding call of gdalinfo gives:
Unfortunately I can’t provide an example. I don’t know gdal and how it is used.
My Idea was just to add content of the template dim-file into the GeoTiff-file at the certain tag (65000).
And before adding the template, adapt it to the current file.
What about the idea to use SNAP from Python directly to create the GeoTiff files?
Hi @marpet, @hollstein, @jmalik
I have been trying to read the SNAP metadata (e.g. rel, abs orbits and slice no) from a geotiff in gdal. Curiously, If I open a geotiff in SNAP that was also output by SNAP, the abstracted metadata is still available(so its there somewhere!!) …however I cannot find this directly from gdal, e.g: gdalInfo(AllMDD=true) --setting all metadata to True. Iv also tried to find the metadata domain tag ‘65000’ gdalInfo(ListMDD=True)
Anyone made any progress with this?
My automatic piplines currently involve xml parsing trees (OK but not as neat as gdal).
On topic, gdal v>2.11 (i think) will read .safe format, but unfortunately leaves out some useful metadata (namely relative orbit and slice number). Theres no support for .dim i’m aware of.
Indeed we store the SNAP specific metadata in the Tiff tag 65000. It contains the xml content. The same as the dim file when you export the product to BEAM-DIMAP. But I don’t know how to retrieve with gdal or other software.
Isn’t NetCDF an alternative for you? There you would have easier access to metadata.
GeoTiff is, in my opinion, one of the worst data formats for EO data. Because of the lack of metadata support. There are only proprietary ways of doing it.
@marpet , yes i totally agree-specifically when it comes to metadata! Unfortunately its a requirement of the algorithms going to be using. Is the tag in an external file such as the aux.xml? I suspect not as I don’t have any in the current folder, but just in case i’m missing something…
Ryan