0

From an stack of geotiff (time-series of NDVI) in a file like 'NDVI_TS.tif' I want to get individual band names. For example: 'Band 086: 20190803T004719'. I can see that when I load the stack into QGIS for example. I need to trace back the dates in the name, in the example above would be 2019-08-03. However, I can't find a way to access it from Python. I can access the bands by index but that doesn't help me finding from which date they are from.

from osgeo import gdal

NDVI = gdal.Open('NDVI_TS.tif', gdal.GA_ReadOnly) 
#for example I can get data from band 86 as and array with:
band86 = NDVI.GetRasterBand(86).ReadAsArray()

I feel there should be some easy solution for this but failed to find.

Jonathan
  • 133
  • 1
  • 15

1 Answers1

2

I don't know if this is efficient, but is the way I solve this (only tested with small images):

bands = {NDVI.GetRasterBand(i).GetDescription(): i for i in range(1, NDVI.RasterCount + 1)}

You get a dictionary with the band name (the one you see in QGIS) and their respective index.

{ ...,
 'Band 086: 20190803T004719': 86,
 ...
}

You can create a small function to parse the band name and get the dates as you want (didn't test it properly):

import datetime
import re

def string2date(string):
   date_string = re.match('.* (.*)T', string).group(1)
   return datetime.datetime.strptime(date_string, '%Y%m%d').strftime('%Y-%m-%d')

And then you can apply to the previous dict:

bands = {string2date(NDVI.GetRasterBand(i).GetDescription()): i for i in range(1, NDVI.RasterCount + 1)}

Hope it helps

Álvaro
  • 98
  • 1
  • 6