Unsupported operand error

1097
7
Jump to solution
12-23-2013 04:45 AM
anTonialcaraz
Occasional Contributor II
Hello,

I'm getting an error in my script that I cannot quite understand (I'm pretty new to python).

Please see below code (simplified version) and error (image).

The error comes from the "Calculation of Parameter 5". I've tested the Conditional statement in raster calculator and it works fine.
I've actually used even more complex Conditional statements within the same script (I'm showing a simplified version here) with no problems at all.

The other thing I'm not sure about is how to use the "OutLocation" variable (Parameter 0) within the save Method for the output of Parameter 5 (CDF). In the Help they only mention the use of the full path for the output.
I've tried with this: CDF.save(OutLocation) + "CDF3" but there is obviously something wrong. Using ArcView 10.0

ERROR:

[ATTACH=CONFIG]30072[/ATTACH]

CODE:

import arcpy import string from arcpy import env arcpy.env.overwriteOutput=True from arcpy.sa import * arcpy.CheckOutExtension("Spatial")   # Set the output location and extent  OutLocation = arcpy.GetParameterAsText(0)  arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)   # Input strings, variables and mask  StageAge = str(arcpy.GetParameterAsText(1))  BSS = arcpy.Raster(arcpy.GetParameterAsText(2))  InputLatitude = arcpy.Raster(arcpy.GetParameterAsText(3))  InputZ = arcpy.GetParameterAsText(4)  InputCountrylines = arcpy.GetParameterAsText(5)  NPPEquation = str(arcpy.GetParameterAsText(6))  CDFEquation = str(arcpy.GetParameterAsText(7))  env.mask = InputZ  # Calculate latitude North/South, offshore distance and Z for CDF Equations; also set the "0" values from Offshoredistance and Bathymetry to "1"  LatNorth = Con((InputLatitude > 0),(InputLatitude)) LatSouth = Con((InputLatitude < 0),(InputLatitude)) Offshoredistancemin0 = EucDistance(InputCountrylines, "", 0.5, "") Offshoredistance = Con((Offshoredistancemin0 == 0),1,(Offshoredistancemin0)) InputZ = Con((InputZ == 0),1,(InputZ)) ZforCDF = Con((InputZ) >50, (InputZ))   # Calculation of Parameter 4 - Net Primary Productivity (NPP). Check the NPP Equation Name and follow the appropiate branch  if NPPEquation == "Min NPP":      rasterNorth = (-1.9875 * (LatNorth) + 194)     rasterSouth = (2.3377 * (LatSouth) + 200)      NPP = arcpy.MosaicToNewRaster_management([(rasterNorth), (rasterSouth)], OutLocation, (StageAge) + "_MinNPP", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST")  elif NPPEquation == "Max NPP":      rasterNorth = (-8.0488 * (LatNorth) + 840)     rasterSouth = (9.6104 * (LatSouth) + 840)      NPP = arcpy.MosaicToNewRaster_management([(rasterNorth), (rasterSouth)], OutLocation, (StageAge) + "_MaxNPP", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST")   # Calculation of Parameter 5 - Carbon Delivery Flux (CDF). Check the CDF Equation Name and follow the appropiate branch  if CDFEquation == "1. Suess 1980":      CDF = Con((ZforCDF >= 50) & (ZforCDF < 100), ((0.049 * (Power((NPP), 1.41)) + (NPP) / ((0.0238 * 50) + (0.212))) / 2) - ((((0.049 * (Power((NPP), 1.41)) +     (NPP) / ((0.0238 * 50) + (0.212))) / 2) - ((NPP) / ((0.0238 * 100) + (0.212)))) / 50)  * ((ZforCDF) - 50), (NPP) / ((0.0238 * (ZforCDF)) + (0.212)))      CDF.save(OutLocation)  elif CDFEquation == "2. Suess 1980 mod":      CDF = Con((ZforCDF >= 50) & (ZforCDF < 100),  (0.049 * (Power((NPP), 1.41))) - ((((0.049 * (Power((NPP),1.41))) - (27.1 * (Power((NPP) / 100,0.935)))) / 50) *     ((ZforCDF) - 50)), 27.1 * (Power((NPP) / (ZforCDF),0.935)))      CDF.save(OutLocation)




Many thanks for your help

Toni
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
anTonialcaraz
Occasional Contributor II
Hi,

Just in case anyone's interested I just found out where the problem was:

In the calculation of Parameter 5 (CDF) NPP is used. NPP is the output of Parameter 4 and the result of Mosaic to New Raster.
As far as I can see NPP is not quite recognised as a raster object within the Parameter 5 calculation (not really sure why).

So I added "arcpy.Raster" in front of the "arcpy.MosaicToNewRaster_management" like this:

NPP = arcpy.Raster(arcpy.MosaicToNewRaster_management([(rasterNorth), (rasterSouth)], OutLocation, (StageAge) + "_MinNPP", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST"))


(The other option is to just add Raster in front of (NPP) in the CDF calculation equation).

Now it works.

Thanks for your help anyway.

View solution in original post

0 Kudos
7 Replies
DouglasSands
Occasional Contributor II
The error means that somewhere in the code, you are combining a string with the output of a function. However I can't specifically see where you are doing this. You said the code provided is simplified?

Also you don't need to do this:

NPPEquation = str(arcpy.GetParameterAsText(6))


This:
NPPEquation = arcpy.GetParameterAsText(6)


Is the same. Can you identify what line is causing the error? Perhaps try hard-coding all of the parameters and see if this works to help find the source.
0 Kudos
anTonialcaraz
Occasional Contributor II
Hi Doug,

Thanks a lot for your comments, I'm taking note.

The code I'm showing is simplified in terms of not including all the "elif" statements (different NPP and CDF equations) so there is nothing "extra" that may cause the error in the rest of the code (I should think).

I'm getting the error in the line AFTER "if CDFEquation == "1. Suess 1980":", that is in the first Con calculation.
ZforCDF and NPP have been previously defined so I don't quite understand this error.

I'm not sure either how to use the OutLocation variable to define the output path and name of that Con calculation.

I'll hard code the variables and see what happen.

Thanks a lot.

Toni
0 Kudos
anTonialcaraz
Occasional Contributor II
Hi,

I've hard coded and simplified the script to facilitate readability.

I'm still getting the same kind of errors with the Con calculation. This calculation works in raster calculator. In fact, it works also as a separate script that I had been using within Model Builder. Consequently, I'm lost!!

Please see below images and code.

IMAGES:

Image "errorA": Apparently the error may seem to be related to the operand "/"

Image "errorB": If I change that "/" operand by "*" (just to try something) I get this error

Image "errorC": If I change that "/" operand by "+" the error goes to the next line until encounters the next "/" operand

[ATTACH=CONFIG]30093[/ATTACH]

[ATTACH=CONFIG]30094[/ATTACH]

[ATTACH=CONFIG]30095[/ATTACH]

CODE:

import arcpy
import string
from arcpy import env
arcpy.env.overwriteOutput=True
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")


# Inputs / Output

NPP = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL_MODEL\LFP_GLOBAL_Python\OUTPUT.gdb\NPP"
ZforCDF = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL_MODEL\LFP_GLOBAL_Python\OUTPUT.gdb\ZforCDF"


OutRasterlayer = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL_MODEL\LFP_GLOBAL_Python\OUTPUT.gdb\CDF_NEW"


# Calculate CDF

CDF = Con((ZforCDF >= 50) & (ZforCDF < 100), ((0.049 * (Power ((NPP), 1.41)) + (NPP) / ((0.0238 * 50) + (0.212))) / 2) - ((((0.049 * (Power ((NPP), 1.41)) +
    (NPP) / ((0.0238 * 50) + (0.212))) / 2) - ((NPP) / ((0.0238 * 100) + (0.212)))) / 50)  * ((ZforCDF) - 50), (NPP) / ((0.0238 * (ZforCDF)) + (0.212)))

CDF.save(OutRasterlayer)



Any help will be greatly appreciated

Thanks
0 Kudos
NeilAyres
MVP Alum
If NPP and ZforCDF are rasters on disk, shouldn't they be referenced to Raster objects before all that long calculation?
Like :
NPP = arcpy.Raster(r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL_MODEL\LFP_GLOBAL_Python\OUTPUT.gdb\NPP")

etc
0 Kudos
DouglasSands
Occasional Contributor II
If NPP and ZforCDF are rasters on disk, shouldn't they be referenced to Raster objects before all that long calculation?
Like :
NPP = arcpy.Raster(r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL_MODEL\LFP_GLOBAL_Python\OUTPUT.gdb\NPP")

etc


You beat me to it, but this definitely necessary. Otherwise NPP is a string and that would be why the unsupported operand error comes up with * and / operators. The reason that + works is that strings support +. For example

folder = 'C:\\'
file = 'a_file.txt'
full_path = folder + file


Because you use getParameterAsText(), they are being set to strings. You might be able to use arcpy.getParameter(#) instead. This would grab the parameter as an object and may work in your case.
0 Kudos
anTonialcaraz
Occasional Contributor II
Hi there,

Thanks a lot for your comments. Still pretty much lost though...

Please see below two versions of the same code. I'm just changing the calculation in "Calculation of Parameter 5".
CODE A works. CODE B does NOT. In CODE B I'm getting the "Operand Error". Any ideas?

CODE A:

import arcpy
import string
from arcpy import env
arcpy.env.overwriteOutput=True
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")


# Set the output location and extent

OutLocation = arcpy.GetParameterAsText(0)

arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)


# Input strings, variables and mask

StageAge = arcpy.GetParameterAsText(1)

BSS = arcpy.Raster(arcpy.GetParameterAsText(2))

InputLatitude = arcpy.Raster(arcpy.GetParameterAsText(3))

InputZ = arcpy.GetParameterAsText(4)

InputCountrylines = arcpy.GetParameterAsText(5)

NPPEquation = arcpy.GetParameterAsText(6)

CDFEquation = arcpy.GetParameterAsText(7)

env.mask = InputZ

# Calculate latitude North/South and offshore distance; also set the "0" values from Offshoredistance and Bathymetry to "1"

LatNorth = Con((InputLatitude > 0),(InputLatitude))
LatSouth = Con((InputLatitude < 0),(InputLatitude))
Offshoredistancemin0 = EucDistance(InputCountrylines, "", 0.5, "")
Offshoredistance = Con((Offshoredistancemin0 == 0),1,(Offshoredistancemin0))
InputZ1 = Con((InputZ == 0),1,(InputZ))
ZforCDF = Con((InputZ1) >50, (InputZ1))


# Calculation of Parameter 3 - Background Total Organic Carbon (TOC)

range1 = Con(((BSS) < 0.0378), (BSS))
range1phi = Con((range1), 6.11)

range2 = Con(((BSS) >= 0.0378) & ((BSS) <= 0.47), (BSS))
range2mm = 0.0593-(1.7714*(range2))+(18.7109*Power((range2),2))-(22.7289*Power((range2),3))
range2phi = -3.3219*Log10((range2mm))

range3 = Con(((BSS) > 0.47), (BSS))
range3mm = 0.8381 + (1.2188*(range3)) - (0.0007*Power((range3),2))
range3phi = -3.3219*Log10((range3mm))

GrainSizephi = arcpy.MosaicToNewRaster_management([(range1phi), (range2phi), (range3phi)], OutLocation, (StageAge) + "_Grain_Size_phi", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST")


# Calculation of Parameter 4 - Net Primary Productivity (NPP).

if NPPEquation == "Min NPP":

    rasterNorth = (-1.9875 * (LatNorth) + 194)
    rasterSouth = (2.3377 * (LatSouth) + 200)

    NPP = arcpy.MosaicToNewRaster_management([(rasterNorth), (rasterSouth)], OutLocation, (StageAge) + "_MinNPP", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST")


# Calculation of Parameter 5 - Carbon Delivery Flux (CDF).

if CDFEquation == "1. Suess 1980":

    CDF = (NPP * ZforCDF / 100) * 4

    CDF.save(r"Q:\LFP_GLOBAL_Python\OUTPUT.gdb\CDF")



CODE B:

import arcpy
import string
from arcpy import env
arcpy.env.overwriteOutput=True
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")


# Set the output location and extent

OutLocation = arcpy.GetParameterAsText(0)

arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)


# Input strings, variables and mask

StageAge = arcpy.GetParameterAsText(1)

BSS = arcpy.Raster(arcpy.GetParameterAsText(2))

InputLatitude = arcpy.Raster(arcpy.GetParameterAsText(3))

InputZ = arcpy.GetParameterAsText(4)

InputCountrylines = arcpy.GetParameterAsText(5)

NPPEquation = arcpy.GetParameterAsText(6)

CDFEquation = arcpy.GetParameterAsText(7)

env.mask = InputZ

# Calculate latitude North/South and offshore distance; also set the "0" values from Offshoredistance and Bathymetry to "1"

LatNorth = Con((InputLatitude > 0),(InputLatitude))
LatSouth = Con((InputLatitude < 0),(InputLatitude))
Offshoredistancemin0 = EucDistance(InputCountrylines, "", 0.5, "")
Offshoredistance = Con((Offshoredistancemin0 == 0),1,(Offshoredistancemin0))
InputZ1 = Con((InputZ == 0),1,(InputZ))
ZforCDF = Con((InputZ1) >50, (InputZ1))


# Calculation of Parameter 3 - Background Total Organic Carbon (TOC)

range1 = Con(((BSS) < 0.0378), (BSS))
range1phi = Con((range1), 6.11)

range2 = Con(((BSS) >= 0.0378) & ((BSS) <= 0.47), (BSS))
range2mm = 0.0593-(1.7714*(range2))+(18.7109*Power((range2),2))-(22.7289*Power((range2),3))
range2phi = -3.3219*Log10((range2mm))

range3 = Con(((BSS) > 0.47), (BSS))
range3mm = 0.8381 + (1.2188*(range3)) - (0.0007*Power((range3),2))
range3phi = -3.3219*Log10((range3mm))

GrainSizephi = arcpy.MosaicToNewRaster_management([(range1phi), (range2phi), (range3phi)], OutLocation, (StageAge) + "_Grain_Size_phi", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST")


# Calculation of Parameter 4 - Net Primary Productivity (NPP). Check the NPP Equation Name and follow the appropiate branch

if NPPEquation == "Min NPP":

    rasterNorth = (-1.9875 * (LatNorth) + 194)
    rasterSouth = (2.3377 * (LatSouth) + 200)

    NPP = arcpy.MosaicToNewRaster_management([(rasterNorth), (rasterSouth)], OutLocation, (StageAge) + "_MinNPP", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST")


# Calculation of Parameter 5 - Carbon Delivery Flux (CDF). Check the CDF Equation Name and follow the appropiate branch

if CDFEquation == "1. Suess 1980":

     CDF = Con((ZforCDF >= 50) & (ZforCDF < 100), NPP * 2, NPP * 4)

     CDF.save(r"Q:\LFP_GLOBAL_Python\OUTPUT.gdb\CDF")
0 Kudos
anTonialcaraz
Occasional Contributor II
Hi,

Just in case anyone's interested I just found out where the problem was:

In the calculation of Parameter 5 (CDF) NPP is used. NPP is the output of Parameter 4 and the result of Mosaic to New Raster.
As far as I can see NPP is not quite recognised as a raster object within the Parameter 5 calculation (not really sure why).

So I added "arcpy.Raster" in front of the "arcpy.MosaicToNewRaster_management" like this:

NPP = arcpy.Raster(arcpy.MosaicToNewRaster_management([(rasterNorth), (rasterSouth)], OutLocation, (StageAge) + "_MinNPP", "", "32_BIT_FLOAT", "", "1", "LAST", "FIRST"))


(The other option is to just add Raster in front of (NPP) in the CDF calculation equation).

Now it works.

Thanks for your help anyway.
0 Kudos