<?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: Run raster calculation on multiple lists of rasters in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243214#M18899</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The Raster Calculator is designed for use as a tool or in ModelBuilder.&lt;BR /&gt;You'd do well to do this task in Python map algebra instead.&lt;BR /&gt;&lt;BR /&gt;For example, use Raster() to cast them to type Raster so the map algebra will work.&lt;BR /&gt;&lt;BR /&gt;Code:&lt;BR /&gt;&lt;BR /&gt;from arcpy.sa import * &lt;BR /&gt;arcpy.workspace = path&amp;nbsp; # direct where the input and output is&lt;BR /&gt;&lt;BR /&gt;# list inputs&lt;BR /&gt;demlist = arcpy.ListRasters('DEM_ext*')&lt;BR /&gt;minlist = arcpy.ListRasters('ZonalMin*')&lt;BR /&gt;maxlist = arcpy.ListRasters('ZonalMax*')&lt;BR /&gt;&lt;BR /&gt;# Starting from the first raster of each type and iterate through to the sixth [0] - [5]&lt;BR /&gt;# Start at index [0]&amp;nbsp;&amp;nbsp; &lt;BR /&gt;for k in range(6): # 0,1,2... 5&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outrast = 100.0 * ((Raster(demlist&lt;K&gt;) - Raster(minlist&lt;K&gt;)) / \&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (Raster(maxlist&lt;K&gt;) - Raster(minlist&lt;K&gt;))&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outrastName = 'Rel_elv%s' % k # grid names should be &amp;lt; 13 chars&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outrast.save(outrastName)&lt;BR /&gt;&lt;BR /&gt;&lt;/K&gt;&lt;/K&gt;&lt;/K&gt;&lt;/K&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Curtis,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you very much for the reply!&amp;nbsp; One question though, I see that you are using the parameter substitution on the Rel_elv output.&amp;nbsp; I am still pretty to new to python so I am just wondering what that does?&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This worked perfectly! Thanks again!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Caleb&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 30 Aug 2012 22:39:35 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2012-08-30T22:39:35Z</dc:date>
    <item>
      <title>Run raster calculation on multiple lists of rasters</title>
      <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243212#M18897</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;So I'm having a little trouble here...This is part of a larger script and I can get everything to work up until this point.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have an equation I need to throw in the Raster Calculator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;((DEM - MIN) / (MAX - MIN) * 100)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I need to perform this equation 6 times (I have 6 DEMs, 6 Mins, and 6 Maxes numbered in ascending order in the gdb).&amp;nbsp; What I was hoping to do was set up an equation where I can do something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# List 3 raster types for input for raster calculator demlist = arcpy.ListRasters('DEM_ext*') minlist = arcpy.ListRasters('ZonalMin*') maxlist = arcpy.ListRasters('ZonalMax*')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;From this list I would then like to use this equation in the raster calculator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# Starting from the first raster of each type and iterate through to the sixth [0] - [5]&amp;nbsp; # Start at index [0]&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = ((demlist[0] - minlist[0]) / (maxlist[0] - minlist[0])* 100)&amp;nbsp; # Then go to [1] and so on... outrast = outpath + os.sep + 'Relative_elv' + str(index)&amp;nbsp; # index here is a variable to represent a raster in the list RasterCalculator(expression, outrast)&amp;nbsp; # index += 1 &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would like this to iterate through and perform this same calculation on all 6 rasters (DEM, MIN, MAX).&amp;nbsp; I have been trying to figure out a way but I am at a loss as to how to do this? What would be a good way to initiate a loop to go through and perform this calculation?&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is what each list of rasters looks like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]17386[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 19:24:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243212#M18897</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2012-08-30T19:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: Run raster calculation on multiple lists of rasters</title>
      <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243213#M18898</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The Raster Calculator is designed for use as a tool or in ModelBuilder.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You'd do well to do this task in Python map algebra instead.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For example, use Raster() to cast them to type Raster so the map algebra will work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;from arcpy.sa import *&amp;nbsp; arcpy.workspace = path&amp;nbsp; # direct where the input and output is&amp;nbsp; # list inputs demlist = arcpy.ListRasters('DEM_ext*') minlist = arcpy.ListRasters('ZonalMin*') maxlist = arcpy.ListRasters('ZonalMax*')&amp;nbsp; # Starting from the first raster of each type and iterate through to the sixth [0] - [5] # Start at index [0]&amp;nbsp;&amp;nbsp;&amp;nbsp; for k in range(6): # 0,1,2... 5 &amp;nbsp;&amp;nbsp;&amp;nbsp; outrast = 100.0 * ((Raster(demlist&lt;K&gt;) - Raster(minlist&lt;K&gt;)) / \ &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (Raster(maxlist&lt;K&gt;) - Raster(minlist&lt;K&gt;))&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; outrastName = 'Rel_elv%s' % k # grid names should be &amp;lt; 13 chars &amp;nbsp;&amp;nbsp;&amp;nbsp; outrast.save(outrastName)&lt;/K&gt;&lt;/K&gt;&lt;/K&gt;&lt;/K&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 21:44:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243213#M18898</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-08-30T21:44:03Z</dc:date>
    </item>
    <item>
      <title>Re: Run raster calculation on multiple lists of rasters</title>
      <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243214#M18899</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;The Raster Calculator is designed for use as a tool or in ModelBuilder.&lt;BR /&gt;You'd do well to do this task in Python map algebra instead.&lt;BR /&gt;&lt;BR /&gt;For example, use Raster() to cast them to type Raster so the map algebra will work.&lt;BR /&gt;&lt;BR /&gt;Code:&lt;BR /&gt;&lt;BR /&gt;from arcpy.sa import * &lt;BR /&gt;arcpy.workspace = path&amp;nbsp; # direct where the input and output is&lt;BR /&gt;&lt;BR /&gt;# list inputs&lt;BR /&gt;demlist = arcpy.ListRasters('DEM_ext*')&lt;BR /&gt;minlist = arcpy.ListRasters('ZonalMin*')&lt;BR /&gt;maxlist = arcpy.ListRasters('ZonalMax*')&lt;BR /&gt;&lt;BR /&gt;# Starting from the first raster of each type and iterate through to the sixth [0] - [5]&lt;BR /&gt;# Start at index [0]&amp;nbsp;&amp;nbsp; &lt;BR /&gt;for k in range(6): # 0,1,2... 5&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outrast = 100.0 * ((Raster(demlist&lt;K&gt;) - Raster(minlist&lt;K&gt;)) / \&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (Raster(maxlist&lt;K&gt;) - Raster(minlist&lt;K&gt;))&amp;nbsp;&amp;nbsp; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outrastName = 'Rel_elv%s' % k # grid names should be &amp;lt; 13 chars&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; outrast.save(outrastName)&lt;BR /&gt;&lt;BR /&gt;&lt;/K&gt;&lt;/K&gt;&lt;/K&gt;&lt;/K&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Curtis,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you very much for the reply!&amp;nbsp; One question though, I see that you are using the parameter substitution on the Rel_elv output.&amp;nbsp; I am still pretty to new to python so I am just wondering what that does?&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This worked perfectly! Thanks again!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Caleb&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 22:39:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243214#M18899</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2012-08-30T22:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: Run raster calculation on multiple lists of rasters</title>
      <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243215#M18900</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Caleb and Curtis,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am following the expression/syntax of this thread. I have successfully ran the script. I just have some questions. One is, how can I save my output file that the filename looks like &lt;STRONG&gt;file_001 (PET_001....to PET_037)&lt;/STRONG&gt; and not file_00. My filename always starts with file_00. Is there a way to concatenate the file naming to: &lt;STRONG&gt;file_037&lt;/STRONG&gt; and not file_0037. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Another, how can I save it as .tif. Is this as simple as this: &lt;STRONG&gt;'Re'_elv%s' %k + ".tif"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hope my query is clear.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks very much,&lt;/P&gt;&lt;P&gt;-Leo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Aug 2014 09:03:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243215#M18900</guid>
      <dc:creator>Leo_KrisPalao</dc:creator>
      <dc:date>2014-08-29T09:03:03Z</dc:date>
    </item>
    <item>
      <title>Re: Run raster calculation on multiple lists of rasters</title>
      <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243216#M18901</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Yes, you can use python string formatting to have full control over leading zeros, AND the way to save a tif is to give a folder path (or have the current workspace be a folder, and use a .tif extension on the filename. The there are some variations on TIFF format (compression, tiling etc) that can be controlled by settings in the geoprocessing environment (10.0 and later).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can use the &lt;A href="https://docs.python.org/2/library/string.html"&gt;modern&lt;/A&gt; ".format()" string formatting instead of the &lt;A href="https://docs.python.org/2/library/stdtypes.html#string-formatting"&gt;old &lt;/A&gt;(but still supported C-like "%" formatting I used above. &lt;A href="http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format"&gt;Which is best?&lt;/A&gt; I think the % syntax was going to be deprecated but many programmers just like it so it unlikely to go away - it's still there in Python 3.4. However, I am liking the new form the more I use it, it's a little more powerful and I think it's easier to read (and therefore debug).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/STRONG&gt; "%03d" % 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; color: #3334ca;"&gt;'003'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/STRONG&gt; "{0:03d}".format(3)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier; color: #3334ca;"&gt;'003'&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here it is in my code sample.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14093297463311194 jive_text_macro" jivemacro_uid="_14093297463311194" modifiedtitle="true"&gt;
&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;outrastName = 'Rel_elv{03d}.tif".format(k) # Python 2.6+ &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;outrastName = 'Rel_elv%03d.tif" % k # deprecated - but still works&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;outrast.save(outrastName)&lt;/SPAN&gt;&lt;/P&gt;
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 29 Aug 2014 16:37:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243216#M18901</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-08-29T16:37:22Z</dc:date>
    </item>
    <item>
      <title>Re: Run raster calculation on multiple lists of rasters</title>
      <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243217#M18902</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Curtis,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your suggestion. It worked okay. I am reading some of the linked in your reply about string operation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-Leo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 05:04:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243217#M18902</guid>
      <dc:creator>Leo_KrisPalao</dc:creator>
      <dc:date>2014-09-11T05:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: Run raster calculation on multiple lists of rasters</title>
      <link>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243218#M18903</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Or even ...&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; n = 3&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt; str(n).zfill(3)&lt;/P&gt;&lt;P&gt;'003'&lt;/P&gt;&lt;P&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Neil&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 06:25:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/run-raster-calculation-on-multiple-lists-of/m-p/243218#M18903</guid>
      <dc:creator>NeilAyres</dc:creator>
      <dc:date>2014-09-11T06:25:13Z</dc:date>
    </item>
  </channel>
</rss>

