I have a folder of raster .tif.
files. I want to iterate through all the rasters in this folder and divide them by a specific raster. I then want to take these outputs and write them to new rasters.
Here is what I am trying:
d = ['Blue', 'Red', 'Green']
test = 'test.tif'
folder = Path('Path/to/My/Folder')
for f in folder.glob("*.tif"):
if any(color in f.name for color in d):
new_raster = f/test
What I am doing here is this: In my folder, I have many raster .tif
files. However, I only actually want to use them in my loop code if their file names contain "Blue", "Red" or "Green". So I would want to use the files Blue_Raster.tif
, Red_Raster.tif
, and Green_Raster.tif
in my loop, but exclude a Purple_Raster.tif
, etc. So then FOR EACH of these rasters, I want to divide them by my selected raster test.tif
. This produces FOR EACH of my rasters (just for Blue, Red, and Green) a new_raster
, which I want to write to an output raster, titled either Blue_Output_raster.tif
, Red_Output_raster.tif
, or Green_Output_raster.tif
. However, I am not sure how to actually write these rasters. Would this just involve a simple use of the writeraster()
function in gdal
? I am not sure if there are actually many more steps involved or if I even used the raster division here correctly. I am open to using rasterio
or geocube
as well if that is simpler.