1

I've created a multiband raster of individual rasters of temperature at different times over a period of two days using rasterio in the following way:

with rasterio.open('test.tif', 'w', **meta) as dst:
    for id, layer in enumerate(file_list, start=1):
        with rasterio.open(layer) as src1:
            dst.write_band(id, src1.read(1))

Using gdalinfo to evaluate test.tif reveals lackluster metadata for the bands.

Band 1 Block=69x3 Type=Float64, ColorInterp=Gray
Band 2 Block=69x3 Type=Float64, ColorInterp=Undefined
Band 3 Block=69x3 Type=Float64, ColorInterp=Undefined
Band 4 Block=69x3 Type=Float64, ColorInterp=Undefined

I've used this for adding a description to each band, but I have not found a way to add datetime to the metadata.

I've looked at this answer, but it only changes the description and doesn't add a new tag. Additionally, I would have to run it as a subprocess in python, and I'd prefer to do this entirely within python.

I've also looked at this answer, but using the tag TIFFTAG_DATETIME with gdal's ds.SetMetadata() is not working. It doesn't throw an error but when I view the metadata using gdalinfo or qgis nothing shows up.

Does anybody know how to add a datetime property to the metadata of a multiband raster using python?

3
  • Trying to sort out what gdalinfo or other metadata viewers will pull for GeoTIFFs can be a world of pain. For a "data cube" such as yours are you able to write data out to a netCDF file? That format will have far richer metadata support baked in. – Charlie Parr May 7 at 19:58
  • I could look into that if you think that's easier to edit the metadata! – JWB May 7 at 20:00
  • In my opinion it is easier - you'll have to judge for yourself as to if it suits your purposes but here is a link to get started reading GeoTIFFs: xarray.pydata.org/en/stable/user-guide/io.html – Charlie Parr May 7 at 20:04
1

You can use ds.update_tags(band, tag=value). See the docs for additional info.

Here's an example with an existing dataset (opened in r+ mode).

import rasterio as rio

with rio.open('test.tif', 'r+') as ds:
    for i in range(1, ds.count+1):
        # Here I add a non-standard tag called "DATE_TIME"
        # this can be anything, like "MY_CUSTOM_BAND_DATE_TIME_TAG"
        # Or you could use "TIFFTAG_DATETIME" if you want. Up to you...
        ds.update_tags(i, DATE_TIME='2021-05-08T14:30')

enter image description here

Your code would look something like:

with rasterio.open('test.tif', 'w', **meta) as dst:
    for id, layer in enumerate(file_list, start=1):
        with rasterio.open(layer) as src1:
            dst.write_band(id, src1.read(1))
            dst.update_tags(id, DATE_TIME='2021-05-08T14:30')
1
  • This worked, thank you!! – JWB May 10 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.