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 0import 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 strTifJust 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?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 assignment
gp.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.
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?
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.
try:
# Process: Add Field containing Image Date...
gp.AddField_management(InputFC, FieldName1, "TEXT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
print "Added Field " + FieldName1
gp.addmessage("Added Field " + FieldName1)
# Process: Add Field to hold sampled value...
gp.AddField_management(InputFC, FieldName2, "FLOAT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
print "Added Sample Value Field"
gp.addmessage("Added Field " + FieldName2)
# Process: Add Field to hold Raster Path...
gp.AddField_management(InputFC, FieldName3, "TEXT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
print " Added Field containing Raster Path"
gp.addmessage("Added Field " + FieldName3)
objFeatures = gp.UpdateCursor(InputFC):
objFeature = objFeatures.Next()
while objFeature:
# Calculate Image Date
# DatesInterval = +DatesInterval
lstDate = map(int, strDate.split(\"/\"))
objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1])
intDay = int(objDate.strftime(\"%j\"))
intInterval = 0
for i in range(1, 366, DatesInterval):
if intDay >= i and intDay < i+DatesInterval:
intInterval = i
break
objFeature.SetValue(FieldName1, intInterval)
# Calculate Raster Path
objImage = \"00\" + strImageDate
strTif = RasterFolder + RasterPrefix + objDate.strftime(\"%Y\") + DatePrefix + objImage[-3:] + \".tif\"
objFeature.SetValue(FieldName3, strTif)
objFeatures.UpdateRow(objFeature)
objFeature = objFeatures.Next()
del objFeature
del objFeatures
print "Calculated ImgDate"
print "Calculated NDVI Raster Path"
gp.addmessage("Calculated Raster Path in " + FieldName3)
# Process: Sample Rasters Listed in Fields...
gp.ArcGISRasterSamplerSampleRastersInFields_GeoEco(InputFC, FieldName3, FieldName2, "", "NEAREST", "", "", "")
...etc....
objFeatures = gp.UpdateCursor(InputFC):- which I think I resolved by removing the colon at the end.
lstDate = map(int, strDate.split(\"/\")) intDay = int(objDate.strftime(\"%j\")) objImage = \"00\" + strImageDate strTif = RasterFolder + RasterPrefix + objDate.strftime(\"%Y\") + DatePrefix + objImage[-3:] + \".tif\"The error in each line seems to occur after the first backslash in each line. I understand backslash can be line continuation characters, but I don't understand which ones I should be removing or why the code lines include these at all.
lstDate = map(int, strDate.split("/"))
intDay = int(objDate.strftime("%j"))
objImage = "00" + strImageDate
strTif = RasterFolder + RasterPrefix + objDate.strftime("%Y") + DatePrefix + objImage[-3:] + ".tif"
try:
# Process: Add Field containing Image Date...
gp.AddField_management(InputFC, FieldName1, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
print "Added Field " + FieldName1
gp.addmessage("Added Field " + FieldName1)
# Process: Add Field to hold sampled value...
gp.AddField_management(InputFC, FieldName2, "FLOAT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
print "Added Sample Value Field"
gp.addmessage("Added Field " + FieldName2)
# Process: Add Field to hold Raster Path...
gp.AddField_management(InputFC, FieldName3, "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
print " Added Field containing Raster Path"
gp.addmessage("Added Field " + FieldName3)
objFeatures = gp.UpdateCursor(InputFC)
objFeature = objFeatures.Next()
while objFeature:
# Calculate Image Date
# DatesInterval = +DatesInterval
strDate = objFeature.GetValue("Date_Time")
lstDate = map(int, strDate.split("/"))
objDate = datetime.date(lstDate[2], lstDate[0], lstDate[1])
intDay = int(objDate.strftime("%j"))
intInterval = 0
for i in range(1, 366, DatesInterval):
if intDay >= i and intDay < i+DatesInterval:
intInterval = i
break
objFeature.SetValue(FieldName1, str(intInterval))
# Calculate Raster Path
objImage = "00" + str(intInterval)
strTif = RasterFolder + RasterPrefix + objDate.strftime("%Y") + DatePrefix + objImage[-3:] + ".tif"
objFeature.SetValue(FieldName3, strTif)
objFeatures.UpdateRow(objFeature)
objFeature = objFeatures.Next()
del objFeature
del objFeatures