<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: global name is undefined in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197042#M15106</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You haven't defined "fd" anywhere in that function.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 Feb 2019 09:16:29 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2019-02-19T09:16:29Z</dc:date>
    <item>
      <title>global name is undefined</title>
      <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197041#M15105</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm trying to execute zonal statistics but I'm experiencing an error like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;C:\Python27\ArcGIS10.5\python.exe "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py"&lt;BR /&gt;Running against: 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]&lt;BR /&gt;Running python 32 bit&lt;BR /&gt;&amp;gt; v0.3 (dev_gg)&lt;BR /&gt;Starting at 20190219155051&lt;BR /&gt;Traceback (most recent call last):&lt;BR /&gt; File "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py", line 179, in &amp;lt;module&amp;gt;&lt;BR /&gt; zonal_statistics(*argv)&lt;BR /&gt; File "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py", line 48, in zonal_statistics&lt;BR /&gt; set_environment(workspace, fd.masked_layer)&lt;BR /&gt;NameError: global name 'fd' is not defined&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;This is my code:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;csv
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;datetime
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;os
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;sys
&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;time
&lt;SPAN style="color: #cc7832;"&gt;from &lt;/SPAN&gt;collections &lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;defaultdict

&lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;arcpy
&lt;SPAN style="color: #cc7832;"&gt;from &lt;/SPAN&gt;arcpy.sa &lt;SPAN style="color: #cc7832;"&gt;import &lt;/SPAN&gt;*

&lt;SPAN style="color: #cc7832;"&gt;def &lt;/SPAN&gt;&lt;SPAN style="color: #ffc66d;"&gt;zonal_statistics&lt;/SPAN&gt;(&lt;SPAN style="color: #808080;"&gt;*argv&lt;/SPAN&gt;):
    &lt;SPAN style="color: #629755; font-style: italic;"&gt;"""
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    Calculates the Zonal statistics usinf a raster grid ans a zone grid/vector and extracts the results to a feature class
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-weight: bold; font-style: italic;"&gt;:param&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt; values_path: Path to the values grid
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-weight: bold; font-style: italic;"&gt;:param&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt; zones_path: Path to the zone grid
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-weight: bold; font-style: italic;"&gt;:param&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt; 'out_feature_class': Feature class as target to append data
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-weight: bold; font-style: italic;"&gt;:param&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt; type of statistics. Can be 'sum', 'avg', 'min', or 'max'
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-weight: bold; font-style: italic;"&gt;:return&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;: adds statistics values to 'out_feature_class'
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    """
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; font-style: italic;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #cc7832;"&gt;print &lt;/SPAN&gt;(&lt;SPAN style="color: #a5c261;"&gt;"&amp;gt; v0.3 (dev_gg)"&lt;/SPAN&gt;)
    &lt;SPAN style="color: #808080;"&gt;# Jan 6, 2017:
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    # Added newly created RDD_B1_URB and NLI_B1_URB
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    # Limited Nightlights to Urban areas
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    # Limited Roads to outside urban areas (correlation with urban)
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    # Changed ABS to CON (Consumption)
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    # Calculates river reach zonal stats from a set of input layers as defined in 'csv_name'
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;""" The output layer is defined in the CSV file"""
&lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;    &lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;# Its here: C:\temp\FFR_Results\1_ZonalResults\ZonalResults.gdb\streams_results_ffr
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    start &lt;/SPAN&gt;= time.clock()
    __location__ = os.path.realpath(os.path.join(os.getcwd()&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;os.path.dirname(__file__)))

    TIMESTAMP = &lt;SPAN style="color: #8888c6;"&gt;int&lt;/SPAN&gt;(datetime.datetime.fromtimestamp(time.time()).strftime(&lt;SPAN style="color: #a5c261;"&gt;'%Y%m%d%H%M%S'&lt;/SPAN&gt;))
    &lt;SPAN style="color: #cc7832;"&gt;print &lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;"Starting at %s" &lt;/SPAN&gt;% (&lt;SPAN style="color: #8888c6;"&gt;str&lt;/SPAN&gt;(TIMESTAMP))

    workspaceFolder = os.path.join(&lt;SPAN style="color: #a5c261;"&gt;r"C:\temp\FFR_Results\1_ZonalResults"&lt;/SPAN&gt;&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;'Temp'&lt;/SPAN&gt;)
    make_sure_path_exists(workspaceFolder)

    workspace = createWorkspace(workspaceFolder&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;TIMESTAMP)

    &lt;SPAN style="color: #808080;"&gt;# Set environment
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    # csv_name = r"E:\Dropbox\Projects\FFR\Code\ZonalGridTransfer\FFR_India_input.csv"
&lt;/SPAN&gt;&lt;SPAN style="color: #808080;"&gt;    &lt;/SPAN&gt;csv_name = &lt;SPAN style="color: #a5c261;"&gt;r"E:\Dropbox\Projects\FFR\Code\ZonalGridTransfer\FFR_input.csv"
&lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #a5c261;"&gt;    &lt;/SPAN&gt;set_environment(workspace&lt;SPAN style="color: #cc7832;"&gt;, &lt;/SPAN&gt;fd.masked_layer)&lt;/PRE&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;P&gt;&lt;/P&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:50:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197041#M15105</guid>
      <dc:creator>ArdiNur_Armanto</dc:creator>
      <dc:date>2021-12-11T09:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: global name is undefined</title>
      <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197042#M15106</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You haven't defined "fd" anywhere in that function.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2019 09:16:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197042#M15106</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2019-02-19T09:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: global name is undefined</title>
      <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197043#M15107</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2019 09:27:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197043#M15107</guid>
      <dc:creator>ArdiNur_Armanto</dc:creator>
      <dc:date>2019-02-19T09:27:37Z</dc:date>
    </item>
    <item>
      <title>Re: global name is undefined</title>
      <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197044#M15108</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Feb 2019 10:15:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197044#M15108</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2019-02-19T10:15:45Z</dc:date>
    </item>
    <item>
      <title>Re: global name is undefined</title>
      <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197045#M15109</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Feb 2019 02:32:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197045#M15109</guid>
      <dc:creator>ArdiNur_Armanto</dc:creator>
      <dc:date>2019-02-20T02:32:52Z</dc:date>
    </item>
    <item>
      <title>Re: global name is undefined</title>
      <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197046#M15110</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A global var can be defined anywhere at the module level, i.e. in&amp;nbsp;the same script&amp;nbsp;file as your function and not inside a function&amp;nbsp;or class.&amp;nbsp; I recommend not using globals though.&amp;nbsp; Just define it inside your zonal function&amp;nbsp;or pass it in as an argument.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #cc7832; border: 0px; font-weight: inherit; font-size: 12px;"&gt;def &lt;/SPAN&gt;&lt;SPAN style="color: #ffc66d; border: 0px; font-weight: inherit; font-size: 12px;"&gt;zonal_statistics&lt;/SPAN&gt;(&lt;SPAN style="color: #808080; border: 0px; font-weight: inherit; font-size: 12px;"&gt;*argv&lt;/SPAN&gt;):
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #629755; border: 0px; font-weight: inherit; font-size: 12px;"&gt;"""
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; border: 0px; font-weight: inherit; font-size: 12px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Calculates the Zonal statistics etc...&lt;/SPAN&gt;&lt;SPAN style="color: #629755; border: 0px; font-weight: inherit; font-size: 12px;"&gt;
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; border: 0px; font-weight: inherit; font-size: 12px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; """
    &lt;SPAN style="color: #ccffff;"&gt;fd = "whatever fd is...?"&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #cc7832; border: 0px; font-weight: inherit; font-size: 12px;"&gt;def &lt;/SPAN&gt;&lt;SPAN style="color: #ffc66d; border: 0px; font-weight: inherit; font-size: 12px;"&gt;zonal_statistics&lt;/SPAN&gt;(fd, &lt;SPAN style="color: #808080; border: 0px; font-weight: inherit; font-size: 12px;"&gt;*argv&lt;/SPAN&gt;):
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="color: #629755; border: 0px; font-weight: inherit; font-size: 12px;"&gt;"""
&lt;/SPAN&gt;&lt;SPAN style="color: #629755; border: 0px; font-weight: inherit; font-size: 12px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Calculates the Zonal statistics etc...&lt;/SPAN&gt;&lt;SPAN style="color: #629755; border: 0px; font-weight: inherit; font-size: 12px;"&gt;
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;SPAN style="border: 0px; color: #629755; font-weight: inherit; font-size: 12px; "&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; """&lt;BR /&gt;&lt;BR /&gt;#etc...&lt;BR /&gt;&lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="color: #a9b7c6; border: 0px; font-weight: inherit; font-size: 12px;"&gt;fd = "whatever fd is...?"
&lt;/SPAN&gt;zonal_statistics(fd, rest of, your, args)&lt;/PRE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:50:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197046#M15110</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T09:50:48Z</dc:date>
    </item>
    <item>
      <title>Re: global name is undefined</title>
      <link>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197047#M15111</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Feb 2019 03:29:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/global-name-is-undefined/m-p/197047#M15111</guid>
      <dc:creator>ArdiNur_Armanto</dc:creator>
      <dc:date>2019-02-20T03:29:32Z</dc:date>
    </item>
  </channel>
</rss>

