Exporting Model from Model Builder

154
3
08-09-2011 06:06 AM
PinoHegi
New Contributor
Hello

I'm trying to export a Field Calculator Function which is running fine in Model Builder to Python in order to use it further within a script.

But when I try to run it within the script I get the following error message:

<class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: calculate_angles(84.3969887378542) <type 'exceptions.TypeError'>: unsupported operand type(s) for -: 'int' and 'unicode'
Failed to execute (CalculateField).

Does anybody know how to solve this error?
I tryed mapping all the datatypes to float (or double) within the function without success.
And as arcpy.AddMessage is not working within the python function, debugging the script is really annoying.

Thanks for any help!
0 Kudos
3 Replies
BruceNielsen
Regular Contributor
Models exported to Python scripts are well known for their "issues". If you post the code (within a code block), it would help.
0 Kudos
PinoHegi
New Contributor
sure:

import arcpy
import math
import numpy
from arcpy import env
from numpy import *

pointshape = arcpy.GetParameterAsText (0)     # Eingabe des Punkte-Shapefiles
raster = arcpy.GetParameterAsText (1)         # Eingabe der Rasterdatei (DHM/DEM)
solar_azimuth = arcpy.GetParameterAsText (2)  # Eingabe des Azimuts der Sonne
solar_height = arcpy.GetParameterAsText (3) # Eingabe des Höhenwinkels der Sonne
tilt = arcpy.GetParameterAsText (4)           # Neigung der Solarmodule
output_raster = arcpy.GetParameterAsText (5)


def calculate_angles(ET_ANGLE):
    #Calculating IN-Vector

    phi_in = math.radians(180 - solar_azimuth)
    theta_in  = math.radians(90 - solar_height)

    r = 1
    x = -(r * math.sin(theta_in) * math.cos(phi_in))
    y = -(r * math.sin(theta_in) * math.sin(phi_in))
    z = -(r * math.cos(theta_in))


    VSonneIn = numpy.matrix([, , ])


    # Calculating Module-Vector

    phi_modul = math.radians(180 - ET_ANGLE)
    theta_modul = math.radians(tilt)

    # r = 1 from above
    u = r * math.sin(theta_modul) * math.cos(phi_modul)
    v = r * math.sin(theta_modul) * math.sin(phi_modul)
    w = r * math.cos(theta_modul)

    # Defining Transformation-Matrix

    MReflexion = numpy.matrix([[1-2*u*u, -2*u*v, -2*u*w],
                           [-2*u*v, 1-2*v*v, -2*v*w],
                           [-2*u*w, -2*v*w, 1-2*w*w]])

    VSonneOut = MReflexion * VSonneIn

    # Calculating OUT-Angles

    theta_out = math.degrees(math.acos(VSonneOut[2]/(math.sqrt(VSonneOut[0]*VSonneOut[0]+VSonneOut[1]*VSonneOut[1]+VSonneOut[2]*VSonneOut[2]))))

#    VERT1 = (90 - theta_out)
#    return VERT1


    if (VSonneOut[0] > 0):
        phi_out = math.degrees(math.atan(VSonneOut[1]/VSonneOut[0]))
    elif (VSonneOut[0] == 0):
        phi_out = math.degrees(numpy.sign(VSonneOut[1])*(math.pi/2))
    elif (VSonneOut[1] >= 0):
     phi_out = math.degrees(math.atan(VSonneOut[1]/VSonneOut[0])+math.pi)
    else:
     phi_out = math.degrees(math.atan(VSonneOut[1]/VSonneOut[0])-math.pi)

    AZIMUTH1 = (180 - phi_out)
    return AZIMUTH1



the expression is:

calculate_angles(float(!ET_ANGLE!))
0 Kudos
ShitijMehta
Esri Regular Contributor
Check this blog for considerations when exporting a model to a Python script.
0 Kudos