Hi Brad,Thanks for the explanation. That helps a lot. I got both field calculations to work perfectly using the following in the code block:import datetime
def Calc(strDate):
lstDate = map(int, strDate.split("/")) # create list of date integers D(D), M(M), YYYY
objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1]) # create date object
intDay = int(objDate.strftime("%j")) # integer day of the year
for i in range(1, 366, 16):
if intDay >= i and intDay < i+16:
return i
return 0
import datetime
def Calc(strDate, strImageDate):
lstDate = map(int, strDate.split("/")) # create list of date integers D(D), M(M), YYYY
objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1]) # create date object
objImage = "00" + strImageDate
strTif = r"J:\GEOG683\HEB_NDVI_GEO" + r"\Montana_Canada_NDVI_A" + objDate.strftime("%Y") + "D" + objImage[-3:] + ".tif"
return strTif
Just was hoping you could clarify a couple more things. First in the Expression I put either Calc(!Date_Time!) or Calc(!Date_Time!,!ImgDate!) ....do I put this expression because it means the code block is calculating using values from the fields Date_Time and ImgDate?Unfortunately, I am still having another issue that I can't resolve. I tried all weekend to resolve it, but couldn't figure it out. My issue is when I export the above codeblocks to a python script outside of model builder that allows the user to input the name of the field (i.e. a field name other than ImgDate) and dates interval (i.e value other then 16) using sys.argv it doesn't work. It seems the code within the code block string doesn't recognize these variables from outside the code block expression. Specifically its not recognizing the variable assigned to DatesInterval inside the code block. Here is the orignial python code exported from Modelbuilder that works great as long as the field is ImgDate and Date range is 16.gp.CalculateField_management(InputFC, "ImgDate", "Calc(!Date_Time!)", "PYTHON", "import datetime\\ndef Calc(strDate):\\n lstDate = map(int, strDate.split(\"/\")) # create list of date integers D(D), M(M), YYYY\\n objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1]) # create date object\\n intDay = int(objDate.strftime(\"%j\")) # integer day of the year\\n for i in range(1, 366, 16):\\n if intDay >= i and intDay < i+16:\\n return i\\n return 0")
##print "Calculated ImgDate"
However, when I attmpet to alter this code with the variables for the field name (FieldName1) and variable for the image dates (DatesInterval) the code doesn't seem to pass the DatesInterval assignment within the codeblock string. I tried to assign it within the code block but it still says ExecuteError: ERROR 000539: Error running expression: Calc("5/29/2006") <type 'exceptions.UnboundLocalError'>: local variable 'DatesInterval' referenced before assignmentgp.CalculateField_management(InputFC, FieldName1, "Calc(!Date_Time!)", "PYTHON", "import datetime\\ndef Calc(strDate):\\n DatesInterval = +DatesInterval\\n lstDate = map(int, strDate.split(\"/\")) # create list of date integers D(D), M(M), YYYY\\n objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1]) # create date object\\n intDay = int(objDate.strftime(\"%j\")) # integer day of the year\\n for i in range(1, 366, DatesInterval):\\n if intDay >= i and intDay < i+DatesInterval:\\n return i\\n return 0")
print "Calculated ImgDate"
Do you mind showing me how to pass a python variable/argument from outside the codeblock. I've attached the entire code of python if that helps. Thank you so much for your detailed input. Your effort and help is greatly appreciated.