Hi Python Users,
I want to automate my filenaming in my python script. This is what I am doing.
I am calculating a bunch of rasters using the implementation of raster calculator / map algebra in python. I want to save my output in this way:
PET_001.tif
.
.
.
.
PET_037.tif
here is the script that I am running:
clim = r'path\to\rasters'
arcpy.env.workspace = clim
clim_rasters = arcpy.ListRasters()
for raster in clim_rasters:
tmax_ras = arcpy.ListRasters("Tmax*")
tmin_ras = arcpy.ListRasters("Tmin*")
ra_ras = arcpy.ListRasters("RA*")
PET5 = 0.0023 * Raster(ra_ras) * ((Raster(tmax_ras) + Raster(tmin_ras) / 2.00) + 17.8) * ((Raster(tmax_ras) + Raster(tmin_ras))**0.5)
I need to save my output in such a way described above. Help please.
Thanks,
-Leo
Solved! Go to Solution.
or you could have some fun... then don't forget to save your raster
(ie Raster.save(outName)
''' FileNamesPaddedZeros padding filenames with zeros ''' filename = "c:/temp/MyfileName.tif" basename, extension = filename.split(".") for i in range(40): padWith = "%03i." % outName = basename + "/" + padWith + extension print outName
Have a look at the CreateUniqueName function:
or you could have some fun... then don't forget to save your raster
(ie Raster.save(outName)
''' FileNamesPaddedZeros padding filenames with zeros ''' filename = "c:/temp/MyfileName.tif" basename, extension = filename.split(".") for i in range(40): padWith = "%03i." % outName = basename + "/" + padWith + extension print outName
Hi Dan and Anthony,
Thanks for your reply.
By the way Dan, I tried to copy your code and execute it. If my declared variable in filename is "PET_.tif", it only prints "PET_%03i.tif" and repeat it 40 times.
The padWith variable which is "%03i." % is throwing a syntax error. It is because of the last % character. If I remove the last % character it executes but print statement is like this:
PET_%03i.tif"
.
.
.
(40 times)
.
.
PET_%03i.tif"
Thanks for any help.
I am a just a python newbie and I am learning it through self study and of course through asking in the forum.
-Leo
You have to give the whole path to the tiff's filename, not the variable name
I answered this for you on another post this morning
Run raster calculation on multiple lists of rasters
but I think we have another issue here - your code is assuming the ListRasters will only return one raster. Are you sure this is the case?
How are your data structured? Are these netCDF rasters that have rasters like "Tmax01" inside each raster?
Hi Curtis and Dan,
Thanks for your suggestions.
By the way, Curtis, I changed the code and and followed the code in your other post. The files are numbered in sequence. Tmax_001, Tmax_002....Tmax_037
I think I have to read more about these strings and filenaming techniques in Python.
Thanks,
-Leo
The powerful string manipulation capabilities in Python are one of the things that make it so awesome.
These arcpy methods are very helpful with file naming as well, especially for temporary files: CreateScratchName, CreateUniqueName, ValidateTableName.
This site is a good resource for string formatting.