Select to view content in your preferred language

global name is undefined

1171
6
02-19-2019 12:56 AM
ArdiNur_Armanto
Emerging Contributor

I'm trying to execute zonal statistics but I'm experiencing an error like this:

C:\Python27\ArcGIS10.5\python.exe "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py"
Running against: 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
Running python 32 bit
> v0.3 (dev_gg)
Starting at 20190219155051
Traceback (most recent call last):
File "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py", line 179, in <module>
zonal_statistics(*argv)
File "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py", line 48, in zonal_statistics
set_environment(workspace, fd.masked_layer)
NameError: global name 'fd' is not defined

This is my code:

import csv
import datetime
import os
import sys
import time
from collections import defaultdict

import arcpy
from arcpy.sa import *

def zonal_statistics(*argv):
    """
    Calculates the Zonal statistics usinf a raster grid ans a zone grid/vector and extracts the results to a feature class
    :param values_path: Path to the values grid
    :param zones_path: Path to the zone grid
    :param 'out_feature_class': Feature class as target to append data
    :param type of statistics. Can be 'sum', 'avg', 'min', or 'max'
    :return: adds statistics values to 'out_feature_class'
    """

    print ("> v0.3 (dev_gg)")
    # Jan 6, 2017:
    # Added newly created RDD_B1_URB and NLI_B1_URB
    # Limited Nightlights to Urban areas
    # Limited Roads to outside urban areas (correlation with urban)
    # Changed ABS to CON (Consumption)

    # Calculates river reach zonal stats from a set of input layers as defined in 'csv_name'

    """ The output layer is defined in the CSV file"""
    # Its here: C:\temp\FFR_Results\1_ZonalResults\ZonalResults.gdb\streams_results_ffr

    start = time.clock()
    __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

    TIMESTAMP = int(datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S'))
    print "Starting at %s" % (str(TIMESTAMP))

    workspaceFolder = os.path.join(r"C:\temp\FFR_Results\1_ZonalResults", 'Temp')
    make_sure_path_exists(workspaceFolder)

    workspace = createWorkspace(workspaceFolder, TIMESTAMP)

    # Set environment
    # csv_name = r"E:\Dropbox\Projects\FFR\Code\ZonalGridTransfer\FFR_India_input.csv"
    csv_name = r"E:\Dropbox\Projects\FFR\Code\ZonalGridTransfer\FFR_input.csv"

    set_environment(workspace, fd.masked_layer)

Tags (2)
0 Kudos
6 Replies
Luke_Pinner
MVP Regular Contributor

You haven't defined "fd" anywhere in that function.

0 Kudos
ArdiNur_Armanto
Emerging Contributor

Previously, I have already define fd as global variable but it still give me an error. Actually this code is not mine and I'm only practising to execute this code and I'm still not figuring out what 'fd' means and mask_layer consider to which attribute. So do you know how to solve this problem?

0 Kudos
Luke_Pinner
MVP Regular Contributor

You can define fd in your function, or pass a previously defined fd in to the function as an argument, or define it as a global variable. 

ArdiNur_Armanto
Emerging Contributor

Can you help me to write the code about how to define fd as a global variable? Which line do I have to define fd in my code as I've shown above? I'm sorry because I'm the beginner user of Python so I'm still learning the programming.

0 Kudos
Luke_Pinner
MVP Regular Contributor

A global var can be defined anywhere at the module level, i.e. in the same script file as your function and not inside a function or class.  I recommend not using globals though.  Just define it inside your zonal function or pass it in as an argument.

def zonal_statistics(*argv):
    """
    Calculates the Zonal statistics etc...
    """
    fd = "whatever fd is...?"

def zonal_statistics(fd, *argv):
    """
    Calculates the Zonal statistics etc...
    """

#etc...
fd = "whatever fd is...?"
zonal_statistics(fd, rest of, your, args)
ArdiNur_Armanto
Emerging Contributor

Thanks for your kindly help. I'm still confused about how "mask_layer" could be added as an attribute to fd string object. Can you help me to explain it?

0 Kudos