0

I have two lists and I would like to find a way to select them based on names.

In list one I have a series of tif files:

list1=['LT50300281984137PAC00_sr_band1.tif',
,'LT50300281984137PAC00_sr_band2.tif'  
'LT50300281984137PAC00_sr_band3.tif','LT50300281994260XXX03_sr_band1.tif',
'LT50300281994260XXX03_sr_band2.tif',
'LT50300281994260XXX03_sr_band3.tif']

in list two I have two files:

list2=[LT50300281984137PAC00_mask.tif,LT50300281994260XXX03_mask.tif]

I want to exectue a process (which I won't ask how to do here) for files in list one which start with LT50300281984137PAC00 to the file in list 2 which starts the same way, and the same for the files which start with LT50300281994260XXX03.

I think it will go something like:

#select the files based on names
for i in list1:
    if i[0:20] in list2:
#this code executes the process I want ONLY if the naming matches
        for b in list1:
            arcpy.gp.ExtractByMask_sa(b,list2,outraster)
     else:
          print('Done')

So basically if the file names in list 1 match those in list 2 I want to execute my process otherwise print done

Edit:

New code being tried is:

import arcpy

arcpy.env.workspace=r'F:\Sheyenne\Atmospherically Corrected Landsat\hank_practice'

list1 = arcpy.ListRasters("*band*")

list2 = arcpy.ListRasters("*_nocloud.tif")  

for raster in list1:
  rasterName = raster
  rasterMask = "" #initialize your variable to be populated in the inner loop
  for mask in list2:
    if mask.startswith(rasterName):
      rasterMask = mask
      break #break out of the inner loop - we have a match
  if rasterMask != "": #just to make sure we have something
    #generic naming
    outraster = rasterName.replace(".tif", "_masked.tif")
    arcpy.gp.ExtractByMask_sa(rasterName,rasterMask,outraster)
2
  • Have you thought about using python's startwith() in a loop? Like if blah.startswith(anotherstring) add to a sub list to be processed. Info: tutorialspoint.com/python/string_startswith.htm
    – Branco
    Commented Aug 11, 2015 at 16:18
  • I have not, but it looks like exactly what I want to do!
    – spotter
    Commented Aug 11, 2015 at 16:19

3 Answers 3

1

I thought about this and you might have luck with a loop over your primarily list and an inner loop looking for its mask.

How about trying something like this:

for raster in list1:
  rasterName = raster
  rasterMask = "" #initialize your variable to be populated in the inner loop
  for mask in list2:
    if mask.startswith(rasterName.split("_")[0]):
      rasterMask = mask
      break #break out of the inner loop - we have a match
  if rasterMask != "": #just to make sure we have something
    #generic naming
    outraster = rasterName.replace(".tif", "_masked.tif")
    arcpy.gp.ExtractByMask_sa(rasterName,rasterMask,outraster)

Edit... I thought on the outraster part, you can just replace part of the input's name. Typically, I take the extension and replace it with "_somethingToKnowItIsAProduct" and add the extension back in. So, the .replace(".tif", "_masked.tif) would mark your output like LT50300281984137PAC00_sr_band1_masked.tif.

7
  • one question, where should outraster be placed? I put it after rasterMask=mask but it didn't seem to work
    – spotter
    Commented Aug 11, 2015 at 16:32
  • @spotter see the edit.
    – Branco
    Commented Aug 11, 2015 at 16:36
  • thats how I do it as well, the code runs with no errors, but nothing executes either. I think this is close to where I want to be though so I will keep chipping away at it. Thank you!
    – spotter
    Commented Aug 11, 2015 at 16:39
  • Where are you running the program? You can add messages in to see what may or may not be happening. I am assuming you're running ArcMap 9.3 so the arcpy call should be right.
    – Branco
    Commented Aug 11, 2015 at 16:51
  • I am using ArcMap 10.3 but I actually run arcpy out of the Anaconda environment. when I print things out it appears that rasterMask is not populated in the code. I added an edit so you can see exactly what I am running my code as now.
    – spotter
    Commented Aug 11, 2015 at 16:54
0

Comparing string is pretty straight forward in python. You can take a look at: https://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs

an easy way to get a list of filenames is the listdir command.

If you only want to compare part of strings you can use a wildcard * or the startswith function http://www.tutorialspoint.com/python/string_startswith.htm

some example code:

import os

pathname1 = "path1"
pathname2 = "path2"
uniquecharacters   = 5  # defines how many characters at the end of the string are unique. 

matches = 0 
for item1 in os.listdir(pathname1):    
    for item2 in os.listdir(pathname2):
        print item1 
        print item2
        if item1.startswith(item2[:-uniquecharacters]):
            print "we have a match"
            matches = matches + 1 

print "we have %s match(es)" %( matches )    

hope this helps

0
0

Branco's answer was so helpful! However, in my case, I changed the code a little bit especially in startswith part because only the middle part of the raster name and mask name would match. I hope I could contribute something to stackexchange!

for mask in masklist:
    if mask.startswith(rasterName.split("_")[3], 17, 26) == True:
        rasterMask = mask
        break

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.