1

I have 732 temperature rasters (366 min and 366 max temp) that I want to manipulate depending on if the max temperature was greater than a specified and constant crop category temperature.

import os
import arcpy
import sys

if arcpy.CheckExtension("Spatial") == "Available":
    arcpy.AddMessage("Checking out Spatial Analyst")
    arcpy.CheckOutExtension("Spatial")
else:
    arcpy.AddError("Unable to get spatial analyst extension")
    sys.exit(0)
'''
HU formula if Max Temp > Max Crop Temp HU formula if Max Temp < Max Crop Temp
1   85  (85-(MaxTemp - 85) + MinTemp)/2) - 45   ((MaxTemp + MinTemp)/2) - 45
2   75  (75-(MaxTemp - 75) + MinTemp)/2) - 40   ((MaxTemp + MinTemp)/2) - 40
3   75  (75-(MaxTemp - 75) + MinTemp)/2) - 45   ((MaxTemp + MinTemp)/2) - 45
4   80  (80-(MaxTemp - 80) + MinTemp)/2) - 50   ((MaxTemp + MinTemp)/2) - 50
5   95  (95-(MaxTemp - 95) + MinTemp)/2) - 50   ((MaxTemp + MinTemp)/2) - 50
6   90  (90-(MaxTemp - 90) + MinTemp)/2) - 50   ((MaxTemp + MinTemp)/2) - 50
7   90  (90-(MaxTemp - 90) + MinTemp)/2) - 60   ((MaxTemp + MinTemp)/2) - 60
8   80  (80-(MaxTemp - 80) + MinTemp)/2) - 65   ((MaxTemp + MinTemp)/2) - 65
9   95  (95-(MaxTemp - 95) + MinTemp)/2) - 65   ((MaxTemp + MinTemp)/2) - 65
'''
DIR = os.path.dirname(__file__)
xlsFilePath = os.path.join(DIR, '..', 'resources', 'xls')
INPUT_NORMAL_HEATUNIT_RAST_MAX_FOLDER = os.path.join(DIR, '..', 'resources',      'input', 'max')
INPUT_NORMAL_HEATUNIT_RAST_MIN_FOLDER = os.path.join(DIR, '..', 'resources', 'input', 'min')
OUTPUT = os.path.join(DIR, '..', 'resources', 'output')

for day in range(0, 367):
    maxNormName = 'Nor_Max_{0}.tif'.format(str(day).zfill(3))
    minNormName = 'Nor_Min_{0}.tif'.format(str(day).zfill(3))
    maxNormFile = os.path.join(INPUT_NORMAL_HEATUNIT_RAST_MAX_FOLDER, maxNormName)
     minNormFile = os.path.join(INPUT_NORMAL_HEATUNIT_RAST_MIN_FOLDER, minNormName)
    categories = [(maxNormFile, minNormFile, 85, 45, str(1)), (maxNormFile, minNormFile, 75, 40, str(2)),
              (maxNormFile, minNormFile, 75, 45, str(3)), (maxNormFile, minNormFile, 80, 50, str(4)),
              (maxNormFile, minNormFile, 95, 50, str(5)), (maxNormFile, minNormFile, 90, 50, str(6)),
              (maxNormFile, minNormFile, 90, 60, str(7)),
              (maxNormFile, minNormFile, 80, 65, str(8)),
              (maxNormFile, minNormFile, 95, 65, str(9))]

for category in categories:
    print "Con(\"%{0}%\" > {2}, ((({2}-(\"%{0}%\"-{2}))+\"%{1}%\")/2)-{3}, ((\"%{0}%\" + \"%{1}%\")/2)-{3})".format(
        *category)

    output = 'Nor_HU{0}_{1}.tif'.format(category[4], str(day).zfill(3))
    outputLocation = os.path.join(OUTPUT, output)

    arcpy.gp.RasterCalculator_sa(
        "Con(\"%{0}%\" > {2}, ((({2}-(\"%{0}%\"-{2}))+\"%{1}%\")/2)-{3}, ((\"%{0}%\" + \"%{1}%\")/2)-{3})".format(
            *category),
        outputLocation)

When I run this, I get a type error pointing to the last line where I am using a con statement in raster calculator. My console output is:

Con("%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\max\Nor_Max_000.tif%" > 85, (((85-("%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\max\Nor_Max_000.tif%"-85))+"%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\min\Nor_Min_000.tif%")/2)-45, (("%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\max\Nor_Max_000.tif%" + "%C:\Users\uma.bhandaram\Desktop\Peak\peakseason\normalCalc..\resources\input\min\Nor_Min_000.tif%")/2)-45)

This looks right to me but clearly it's not. Where am I going wrong?

  • 1
    I'd try with a very simple section of that formula (maybe just ((MaxTemp + MinTemp)/2) - 65) and see if that works. – Paul Mar 21 '17 at 19:12
1

You did not specify if you run your script in ArcMap, Model Builder or as a standalone python script (?), which may cause your problem.

In this situation I guess you are attempting to use it in a standalone script, so it's not really a matter of type error. I mean it is a type error, because python understands your variables maxNormFile, minNormFile as strings (which they are - representing paths of files) not as rasters in "ArcMap way".

Please check out the info: http://desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/how-raster-calculator-works.htm

which states:

The Raster Calculator tool is not intended to be used in scripting environments and is not available in the standard Spatial Analyst ArcPy module.

Raster Calculator can be used in Model Builder. But exporting script from Model Builder and rewriting and using as a standalone script won't work.

Instead you should explore the topic of Map Algebra, which can be used in standalone script. Please search the Esri doc pages.

0

Without knowing the type error, i would suggest the following:

  • Catch the exception and post more infos.
  • Copy the data to c:\temp as this will shorten the paths and will make errors more visible, as it prevents [...] in the path
  • Try to set the values to a floating point number, by adding a .0 so that 85 (integer or string (?)) becomes 85.0 (float)
  • Please see gis.stackexchange.com/tour and edit your question to include some context. Short, one-liner answers are generally not what we are going for in this community. – MaryBeth Mar 21 '17 at 20:16
  • Type error is specified in the question title: TypeError: unsupported operand type(s) for -: 'str' and 'int' – d324223 Mar 21 '17 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.