-1

I have two folders containing 1800 files each. I need to merge or combine them based on their names e.g., file MOD10A2.A2017177.h24v05.006.2017187224801.tif in folder A should combine/merge with the file MYD10A2.A2017177.h24v05.006.2017191190921.tif in folder B and file MOD10A2.A2017188.h24v05.006.2017183445431.tif in folder A should combine/merge with the file MYD10A2.A2017188.h24v05.006.2017134567541.tif in folder B and so on.

0

1 Answer 1

1

I dont know which merge tool you want to use. For the pairing of rasters you can use os.walk to traverse folders and defaultdict(list) to match the rasters by name. I assume the name match is always one dot from the left in the filename.

import os, arcpy
from collections import defaultdict as dd

baseFolder = r'/home/bera/Desktop/tempgis/folder' #Change
d = dd(list)

for root, folders, files in os.walk(baseFolder):
    for file in files:
        d[file.split('.')[1]].append(os.path.join(root, file))

#d is now for example:

#defaultdict(list,
#            {'A2017177': ['/home/bera/Desktop/tempgis/folder/folder2/MYD10A2.A2017177.h24v05.006.2017191190921.tif', '/home/bera/Desktop/tempgis/folder/folder1/MOD10A2.A2017177.h23v05.006.2017187224801.tif']})

for partname, pathlist in d.items(): #d.iteritems() if you have ArcMap (python 2)
    #print(partname)
    #print(pathlist)
    if len(pathlist)==2:
        fullPaths = [os.path.join(root, f) for f in pathlist]
        if all(os.path.isfile(p) and p.endswith('.tif') for p in fullPaths):
            arcpy.MosaicToNewRaster_management(input_rasters=';'.join(fullPaths), output_location='SOMEFOLDER_OR_FGDB', raster_dataset_name_with_extension=partname+'.tif', number_of_bands=1) #Adjust this line
1
  • Thanks, code executed successfully but it is not writing anything in the output location. Commented Jan 12, 2021 at 17:22

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