Creating geotiff for snap toolbox, how to include "spectral wavelength" metadata?

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

It seems that snap ignores this metadata.

Using ENVI classic, SNAP for some reason removes them when you convert the image to geotiff.

ENVI Classic --> File --> Edit Envi header --> and you can edit metadata.

Anyone found what is wrong with TOA Reflectance values?

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?

@marpet GDAL can save .aux.xml files next to a file to save the metadata item not supported by the format. It then knows how to read it back.

It allows for example to save a GeoTIFF to png, and when doing the inverse format translation, to get the original GeoTIFF with all its metadata.

See http://www.gdal.org/classGDALPamDataset.html#details for the implementation details.

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:

hollstei@*:  ~> gdalinfo ./S2_L2A.tif 
Driver: GTiff/GeoTIFF
Files: ./S2_L2A.tif
Size is 22, 22
Coordinate System is `'
Metadata:
  Name=L2A
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (    0.0,    0.0)
Lower Left  (    0.0,   22.0)
Upper Right (   22.0,    0.0)
Lower Right (   22.0,   22.0)
Center      (   11.0,   11.0)
Band 1 Block=22x18 Type=Int16, ColorInterp=Gray
  Description = B02
  Metadata:
    name=B02
    Spectral Bandwidth=0
    Spectral Wavelength=0
Band 2 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B03
  Metadata:
    name=B03
    Spectral Bandwidth=1
    Spectral Wavelength=1
Band 3 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B04
  Metadata:
    name=B04
    Spectral Bandwidth=2
    Spectral Wavelength=2
Band 4 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B05
  Metadata:
    name=B05
    Spectral Bandwidth=3
    Spectral Wavelength=3
Band 5 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B06
  Metadata:
    name=B06
    Spectral Bandwidth=4
    Spectral Wavelength=4
Band 6 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B07
  Metadata:
    name=B07
    Spectral Bandwidth=5
    Spectral Wavelength=5
Band 7 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B08
  Metadata:
    name=B08
    Spectral Bandwidth=6
    Spectral Wavelength=6
Band 8 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B8A
  Metadata:
    name=B8A
    Spectral Bandwidth=7
    Spectral Wavelength=7
Band 9 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B11
  Metadata:
    name=B11
    Spectral Bandwidth=8
    Spectral Wavelength=8
Band 10 Block=22x18 Type=Int16, ColorInterp=Undefined
  Description = B12
  Metadata:
    name=B12
    Spectral Bandwidth=9
    Spectral Wavelength=9

I hope this helps.

P.S. The upload for the file seems to be broken for this file, the image is also here:
https://www.dropbox.com/s/u5r30sfoz4mkcog/S2_L2A.tif?dl=0

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?

I can try this, any suggestions on how to do this? I have no experience with using SNAP from python.

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

No, it is not separate. It is in the tiff file.