Changing Raster format and name with elif

572
7
04-15-2014 09:22 AM
GiovanniOcchiali
New Contributor
Hello everyone,

my name is Giovanni and I am relatively new to ArcGIS and Python coding. I have a problem I haven't been able to solve: I have a large amount of .tif raster which I need to transform in .bil while at the same time changing their name. Both the input and output name have their own logic: input is equal to "australia_arcYYYYMMDD.tif" where YYYY is the year, MM the month and DD the day, output required is "rain_YYYYDDD.bil" where DDD is the Julian date, so that anyhow there will be a one-to-one relationship between the two.
I was trying something along the line of:
import arcpy 
arcpy.env.workspace = "e:/Pyton/prova/Tif" 
outLoc = ("e:/Pyton/prova/bil")
outDirectory = outLoc
datasetList = arcpy.ListDatasets("*", "Raster") 
for d in datasetList:
 tif = str(d)
 elif tif == "australia_arc20000101*":
  outRast = "rain_2000001.bil"
  outD = outDirectory + "/" + outRast
  arcpy.CopyRaster_management(tif,outD)
 elif tif == "australia_arc20000102*":
  outRast = "rain_2000002.bil"
  outD = outDirectory + "/" + outRast
  arcpy.CopyRaster_management(tif,outD)
 else:
  outRast = "rain_2000001.bil"
  outD = outDirectory + "/" + outRast
  arcpy.CopyRaster_management(tif,outD)


or otherwise
import arcpy 
arcpy.env.workspace = "E:/Pyton/Prova/Tif" 
outLoc = ("e:/Pyton/prova/bil")
outDirectory = outLoc
datasetList = arcpy.ListDatasets("*", "Raster") 
for d in datasetList:
 tif = str(d)
elif d in datasetlist == "australia_arc20000101*":
 outRast = "rain_2000001.bil"
 outD = outDirectory + "/" + outRast
 arcpy.CopyRaster_management(tif,outD)
elif d in datasetlist == "australia_arc20000102*":
 outRast = "rain_2000002.bil"
 outD = outDirectory + "/" + outRast
 arcpy.CopyRaster_management(tif,outD)
else:
 outRast = "rain_2000001.bil"
 outD = outDirectory + "/" + outRast
 arcpy.CopyRaster_management(tif,outD)


but neither of the two seems to work. As I've said I have little knowledge of the program (I'm a social scientist), so there might be some rookie error. The idea would be to write a long elif statement (365 elif + 1 else) once I got the proper way of doing it, is definitely not the most polished way of obtaining the result but the file are relatively small so that converting a year takes around ten minute. If anybody has any suggestion I am sure it will prove of invaluable help.
Thanks in advance

Giovanni
Tags (2)
0 Kudos
7 Replies
IanMurray
Frequent Contributor
You cannot have just start with an elif statement.  You must start with if statement, then have elif statement for subsequent queries, until you end with an else statement.

Btw your idea of that many elif statements is crazy.  There is an easier way to do this.



import arcpy
arcpy.env.workspace = "e:/Pyton/prova/Tif"
outLoc = ("e:/Pyton/prova/bil")
outDirectory = outLoc
datasetList = arcpy.ListDatasets("*", "Raster")
for d in datasetList:
              tif = str(d)
d = d.split("_")
              d = d[-1]
              outRast = "rain_" + d + ".bil"
              outD = outDirectory + "/" + outRast
0 Kudos
IanMurray
Frequent Contributor
You cannot have just start with an elif statement.  You must start with if statement, then have elif statement for subsequent queries, until you end with an else statement.

Btw your idea of that many elif statements is crazy.  There is an easier way to do this.



import arcpy
arcpy.env.workspace = "e:/Pyton/prova/Tif"
outLoc = ("e:/Pyton/prova/bil")
outDirectory = outLoc
datasetList = arcpy.ListDatasets("*", "Raster")
#Looping through all rasters
for d in datasetList:
              # storing input raster name
              tif = str(d)

d = d.split("_")
              d = d[-1]
              print d
              outRast = "rain_" + d + ".bil"
              outD = outDirectory + "/" + outRast
              arcpy.CopyRaster_management(tif,outD)
0 Kudos
IanMurray
Frequent Contributor
You cannot have just start with an elif statement. You must start with if statement, then have elif statement for subsequent queries, until you end with an else statement.

This would be much simpler if your output could be YYYYMMDD same as your input raster.  All you would have to do is take the ending from your input file, store is as string and concatenate it onto the end of your output raster. 

Using that many elif statements just isn't a smart way to go about this.
0 Kudos
JoshuaChisholm
Occasional Contributor III
Hello Giovanni,

You're off to a good start. I made a few changes:
import datetime
import arcpy

def getDateFromFile(f):
    return f.split('_arc')[1][:8] #gets the first 8 characters after "_arc"

def getJulian(d):
    dt = datetime.datetime.strptime(d, '%Y%m%d')
    return str(dt.timetuple().tm_yday).zfill(3)


arcpy.env.workspace = "e:/Pyton/prova/Tif" 
outDirectory = "e:/Pyton/prova/bil" #should not need the blackets
datasetList = arcpy.ListDatasets("*", "Raster") 
for tif in datasetList:
    date=getDateFromFile(tif) #calls on function defined above
    year=date[:4] #gets first 4 characters of date
    outRast = "rain_"+year+getJulian(date)+".bil"
    outBil = outDirectory + "/" + outRast
    print "Copying "+tif+" to "+outRast+"..."
    arcpy.CopyRaster_management(tif,outBil)


For the future, you must start an if statement with "if", not "elif".
Let me know if it works for you or if you have any questions about it. Good luck!
~Josh
0 Kudos
IanMurray
Frequent Contributor
Hello Giovanni,

You're off to a good start. I made a few changes:
import datetime
import arcpy

def getDateFromFile(f):
    return f.split('_arc')[1][:8] #gets the first 8 characters after "_arc"

def getJulian(d):
    dt = datetime.datetime.strptime(d, '%Y%m%d')
    return str(dt.timetuple().tm_yday).zfill(3)


arcpy.env.workspace = "e:/Pyton/prova/Tif" 
outDirectory = "e:/Pyton/prova/bil" #should not need the blackets
datasetList = arcpy.ListDatasets("*", "Raster") 
for tif in datasetList:
    date=getDateFromFile(tif) #calls on function defined above
    year=date[:4] #gets first 4 characters of date
    outRast = "rain_"+year+getJulian(date)+".bil"
    outBil = outDirectory + "/" + outRast
    print "Copying "+tif+" to "+outRast+"..."
    arcpy.CopyRaster_management(tif,outBil)


For the future, you must start an if statement with "if", not "elif".
Let me know if it works for you or if you have any questions about it. Good luck!
~Josh


Thanks Josh, I wasn't familiar enough with datetime to want to try to write that out for him.

I was in the middle of trying to do some painful if, elif statements to calculate Julian dates from the YYYYMMDD from those file names.  Especially when you have to consider leapyears, its a big pain.
0 Kudos
JoshuaChisholm
Occasional Contributor III
Thanks for the shout out Ian (and up vote). I'm still learning the datetime module. I find it surprisingly challenging. Trying to code Julian day yourself would be very very hard indeed.
0 Kudos
GiovanniOcchiali
New Contributor
Thank you very much to the both of you (especially Josh), you made my life so much easier!
Never heard of datetime before, but I'm just making my acquaintance with Python for now!
Have a good Easter!
Best

Giovanni
0 Kudos