<?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 Loop through raster names in two folders, perform Con (Spatial Analyst tool) if the dates match in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1209884#M65491</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I cannot perform the Con tool over all the raster images with same date in two folders. What I am trying to achieve is producing an image where the pixel values of 1 are converted to 0 and the pixel values of 0 are replaced with the pixel values of another image using the images with the same dates.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the script I have worked upon so far.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os
from arcpy.sa import *

# Set the current workspace
location_a = "D:/datasets/regression_interpolation_rasters/airtemp_snowmask_regression_rasters"
location_b = "D:/datasets/regression_interpolation_rasters/regression_rasters"
location = "D:/datasets/regression_interpolation_rasters/snowmask_rasters"

# get the names of rasters
arcpy.env.workspace = location_a
snow_raster = arcpy.ListRasters("", "TIF")
for snow_ras in snow_raster:
    snow = Raster(snow_ras)
    #print(type(snow))
    snow_name = snow_ras.split("_")
    snow_values = snow_name[2]
    #print(snow_values) 
    
# get the names of rasters 
arcpy.env.workspace = location_b
airtemp_raster = arcpy.ListRasters("", "TIF")
#print(airtemp_raster)

for airtemp_ras in airtemp_raster:
    airtemp = Raster(airtemp_ras)
    airtemp_name = airtemp_ras.split("_")
    airtemp_values = airtemp_name[0]
    #print(airtemp_values)
    if airtemp_values == snow_values:
        output_raster = Con(snow_raster==1, 0, airtemp_raster)  
        output = os.path.join(location,airtemp_ras.split("_")[0] +'_airtemp'+ '.tif')
        output_raster.save(output)
            #print("yes")
    else:
        print("action cannot take place")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;All it is showing the else statement even though the dates match.The placement of the dates vary but the format of dates are similar.These are the images in location_b&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VasudhaChaturvedi_1-1662474912616.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50498i176CE5246A1F02A5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VasudhaChaturvedi_1-1662474912616.png" alt="VasudhaChaturvedi_1-1662474912616.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;These are the images in location_a&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VasudhaChaturvedi_2-1662474932205.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50499i4EC05A3201B1B95B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VasudhaChaturvedi_2-1662474932205.png" alt="VasudhaChaturvedi_2-1662474932205.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 06 Sep 2022 21:26:22 GMT</pubDate>
    <dc:creator>VasudhaChaturvedi</dc:creator>
    <dc:date>2022-09-06T21:26:22Z</dc:date>
    <item>
      <title>Loop through raster names in two folders, perform Con (Spatial Analyst tool) if the dates match</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1209884#M65491</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I cannot perform the Con tool over all the raster images with same date in two folders. What I am trying to achieve is producing an image where the pixel values of 1 are converted to 0 and the pixel values of 0 are replaced with the pixel values of another image using the images with the same dates.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the script I have worked upon so far.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os
from arcpy.sa import *

# Set the current workspace
location_a = "D:/datasets/regression_interpolation_rasters/airtemp_snowmask_regression_rasters"
location_b = "D:/datasets/regression_interpolation_rasters/regression_rasters"
location = "D:/datasets/regression_interpolation_rasters/snowmask_rasters"

# get the names of rasters
arcpy.env.workspace = location_a
snow_raster = arcpy.ListRasters("", "TIF")
for snow_ras in snow_raster:
    snow = Raster(snow_ras)
    #print(type(snow))
    snow_name = snow_ras.split("_")
    snow_values = snow_name[2]
    #print(snow_values) 
    
# get the names of rasters 
arcpy.env.workspace = location_b
airtemp_raster = arcpy.ListRasters("", "TIF")
#print(airtemp_raster)

for airtemp_ras in airtemp_raster:
    airtemp = Raster(airtemp_ras)
    airtemp_name = airtemp_ras.split("_")
    airtemp_values = airtemp_name[0]
    #print(airtemp_values)
    if airtemp_values == snow_values:
        output_raster = Con(snow_raster==1, 0, airtemp_raster)  
        output = os.path.join(location,airtemp_ras.split("_")[0] +'_airtemp'+ '.tif')
        output_raster.save(output)
            #print("yes")
    else:
        print("action cannot take place")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;All it is showing the else statement even though the dates match.The placement of the dates vary but the format of dates are similar.These are the images in location_b&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VasudhaChaturvedi_1-1662474912616.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50498i176CE5246A1F02A5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VasudhaChaturvedi_1-1662474912616.png" alt="VasudhaChaturvedi_1-1662474912616.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;These are the images in location_a&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VasudhaChaturvedi_2-1662474932205.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50499i4EC05A3201B1B95B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VasudhaChaturvedi_2-1662474932205.png" alt="VasudhaChaturvedi_2-1662474932205.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2022 21:26:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1209884#M65491</guid>
      <dc:creator>VasudhaChaturvedi</dc:creator>
      <dc:date>2022-09-06T21:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through raster names in two folders, perform Con (Spatial Analyst tool) if the dates match</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1209914#M65494</link>
      <description>&lt;P&gt;Your 'snow_val' in your conditional is the last item assigned to it in the first loop, so its not iterating as intended.&amp;nbsp; You need to update your loops to nest the&lt;/P&gt;&lt;PRE&gt;for airtemp_ras in airtemp_raster:&lt;/PRE&gt;&lt;P&gt;logic so that checks the current snow_val to each air_temp item.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2022 15:25:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1209914#M65494</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-09-06T15:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through raster names in two folders, perform Con (Spatial Analyst tool) if the dates match</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1209917#M65495</link>
      <description>&lt;UL&gt;&lt;LI&gt;You don't save your snow_values anywhere, so snow_values is just the date part of the last raster in location_a, which is why the comparison in line 28 returns False.&lt;/LI&gt;&lt;LI&gt;You have location_a and location_b the wrong way around (or you mislabeled your screenshots).&lt;/LI&gt;&lt;LI&gt;Your variable names are really misleading.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Here's what you want to do (untested):&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# build a dictionary {date: raster} for the snow rasters
# I'm going by your screenshots: snow rasters in location_b
snow_raster_dict = dict()
arcpy.env.workspace = location_b
snow_raster_list = arcpy.ListRasters("", "TIF")
for snow_name in snow_raster_list:
    snow_raster = Raster(snow_name)
    #print(type(snow_raster))
    snow_name_parts = snow_name.split("_")
    snow_date = snow_name_parts[2]
    snow_raster_dict[snow_date] = snow_raster
#print(snow_raster_dict)    

# get the names of airtemp rasters 
# again, going by your screenshots -&amp;gt; location_a
arcpy.env.workspace = location_a
airtemp_raster_list = arcpy.ListRasters("", "TIF")
#print(airtemp_raster_list)

for airtemp_name in airtemp_raster_list:
    airtemp_raster = Raster(airtemp_name)
    airtemp_name_parts = airtemp_name.split("_")
    airtemp_date = airtemp_name_parts[0]
    # try to get the corresponding snow raster
    try:
        snow_raster = snow_raster_dict[airtemp_date]
        # do the calculation
        output_raster = Con(snow_raster==1, 0, airtemp_raster)  
        output = os.path.join(location,airtemp_name.split("_")[0] +'_airtemp'+ '.tif')
        output_raster.save(output)
        #print(f"created new airtemp raster for {airtemp_date}")
    # catch KeyErrors (date not found in snow_raster_dict)
    except KeyError:
        print(f"no snow raster found for {airtemp_date}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2022 15:27:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1209917#M65495</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-09-06T15:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through raster names in two folders, perform Con (Spatial Analyst tool) if the dates match</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1210229#M65504</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I corrected the labeling of the raster images in the screenshot. The screenshot labelling was wrong, which has been corrected now&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2022 07:05:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1210229#M65504</guid>
      <dc:creator>VasudhaChaturvedi</dc:creator>
      <dc:date>2022-09-07T07:05:47Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through raster names in two folders, perform Con (Spatial Analyst tool) if the dates match</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1210233#M65505</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;&amp;nbsp;for your suggestion. I updated the code as per your suggestion, the images whose dates match are being stored but somehow all the images have the same pixel value of 1 replaced by 0 which is incorrect.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example this is one of the output stored where the pixel value of 1 is replaced with pixel value 0, and pixel value of 0 is replaced with the pixel value of another raster.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VasudhaChaturvedi_0-1662534896210.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50595iAE89EB4B24A3EF04/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VasudhaChaturvedi_0-1662534896210.png" alt="VasudhaChaturvedi_0-1662534896210.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;But the binary image with 1(grey color) and 0 (black color) pixel values looks like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VasudhaChaturvedi_1-1662534993193.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50596iE84CD7A6CDE1EF16/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VasudhaChaturvedi_1-1662534993193.png" alt="VasudhaChaturvedi_1-1662534993193.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;And the image with which the pixel values of 0 in binary image will be replaced is:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="VasudhaChaturvedi_2-1662535097814.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/50597i326DB3D0AA87A94B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="VasudhaChaturvedi_2-1662535097814.png" alt="VasudhaChaturvedi_2-1662535097814.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Sep 2022 07:20:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1210233#M65505</guid>
      <dc:creator>VasudhaChaturvedi</dc:creator>
      <dc:date>2022-09-07T07:20:13Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through raster names in two folders, perform Con (Spatial Analyst tool) if the dates match</title>
      <link>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1210332#M65506</link>
      <description>&lt;P&gt;Your code actually worked&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341"&gt;@JohannesLindner&lt;/a&gt;. Thank you for your help. It was just a minor mistake that was causing a problem. Instead of single = I used double == signs for the try statement. So the final code to solve my query is here.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy, os
from arcpy.sa import *
from arcpy import env

# Set the current workspace
location_a = "D:/datasets/planetscope/binary_snow_images_converted"
location_b = "D:/datasets/regression_interpolation_rasters/linear_regression_interpolation_rasters_without_snowmask"
location = "D:/datasets/regression_interpolation_rasters/regression_rasters_after_snowmask"
arcpy.env.extent = "D:/datasets/DEM ALTA_VALTELLINA LOMBARDY REGION/clip_dtm.tif"
arcpy.env.snapRaster = "D:/datasets/DEM ALTA_VALTELLINA LOMBARDY REGION/clip_dtm.tif"
arcpy.env.cellSize = "D:/datasets/regression_interpolation_rasters/linear_regression_interpolation_rasters_without_snowmask/2017-10-01_airtemp.tif"

# get the names of rasters
snow_raster_dict = dict()
all_snow_rasters = []
snow_raster_list = arcpy.ListRasters("", "TIF")

for snow_name in snow_raster_list:
    snow_raster = Raster(snow_name)
    snow_name_parts = snow_name.split("_")
    snow_date = snow_name_parts[2]
    snow_raster_dict[snow_date] = snow_raster
    
# get the names of rasters 
arcpy.env.workspace = location_b
airtemp_raster_list = arcpy.ListRasters("", "TIF")

for airtemp_name in airtemp_raster_list:
    airtemp_raster = Raster(airtemp_name)
    airtemp_name_parts = airtemp_name.split("_")
    airtemp_date = airtemp_name_parts[0]

    try:
        snow_raster = snow_raster_dict[airtemp_date]
        output_raster = Con(snow_raster==1, 0, airtemp_raster)  
        output = os.path.join(location,airtemp_name.split("_")[0] +'_airtemp'+ '.tif')
        output_raster.save(output)
    except KeyError:
        print(f"no snow raster found for {airtemp_date}")&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 07 Sep 2022 13:57:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/loop-through-raster-names-in-two-folders-perform/m-p/1210332#M65506</guid>
      <dc:creator>VasudhaChaturvedi</dc:creator>
      <dc:date>2022-09-07T13:57:48Z</dc:date>
    </item>
  </channel>
</rss>

