I am looking to stack certain GeoTiffs according to todays, date, since this will run daily, and hence proper layers need to be stacked.
#get current date
from datetime import datetime
datetime.today().strftime('%Y%m%d')
from osgeo import gdal
outvrt = '/vsimem/stacked.vrt' #/vsimem is special in-memory virtual "directory"
outtif = 'C:\Program Files\GeoServer\data_dir\data\RSS_WH/stacked.tif'
import glob
tifs = glob.glob('C:\Program Files\GeoServer\data_dir\data\RSS_WH/*.tif')
outds = gdal.BuildVRT(outvrt, tifs, separate=True)
outds = gdal.Translate(outtif, outds)
As you can see, currently, the end of it is only *.tif, meaning all the tif files in the folder would be stacked together. However, I need to insert the current date into the pathway so that everything, that contains for instance 12082021 and stuff before and after it should be included (because layers differ based on prefix and suffix, and the date is the only reliable constant.
CodePudding user response:
This is a straightforward modification of the code you have.
First, make sure you're assigning the current date to a variable, rather than just printing it out:
today = datetime.today().strftime('%Y%m%d')
Then include this in the search string in your glob command (making sure to put an asterisk on either side, if you want matches wherever the date is in the filename):
tifs = glob.glob('C:\Program Files\GeoServer\data_dir\data\RSS_WH/*' today '*.tif')
or, equivalently:
tifs = glob.glob('C:\Program Files\GeoServer\data_dir\data\RSS_WH/*{}*.tif'.format(today))