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. And I want the resulted images in folder C.

The following code actually executes successfully but it doesn't write or save the resulted images or files in folder C.

Here is the code (courtesy BERA)

import os, arcpy
from collections import defaultdict as dd

baseFolder = r'D:\data' # containing two sub-folders
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))

for partname, pathlist in d.items():
    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='D\combined_data', raster_dataset_name_with_extension=partname+'.tif', number_of_bands=1)

1 Answer 1

2

UPDATE

The below code should achieve what you're trying to do. However as with all things programming there are many ways to go about doing this. Circling back to your original post, it looks like you are missing the colon ":" in your output file path. You put 'D\combined_data' instead of 'D:\combined_data' If that was an error in your original script, that would cause it to not be output as well. Either way, fixing the error in your code, or using the sample below, should achieve what you're after. In the snippet below, I tried to use your paths as you showed them. I tested this on my machine with two sample rasters that I named exactly as the ones you showed a raster was successfully output.

Try this:

import arcpy,os

baseFolder =r"D:\data"
subfolders = [ f.path for f in os.scandir(baseFolder) if f.is_dir() ]


#set directories
Dir_a = subfolders[0]
Dir_b = subfolders[1]
OutputLocation = r"D:\combined_data"

#set workpace and get list of all rasters in Dir_a
arcpy.env.workspace = Dir_a
rasters_a = [os.path.join(Dir_a,f) for f in arcpy.ListRasters()]

#set workpace and get list of all rasters in Dir_b
arcpy.env.workspace = Dir_b
rasters_b = [os.path.join(Dir_b,f) for f in arcpy.ListRasters()]


ToCombine = {}
for raster in rasters_a:
  rasterkey = raster.split('.')[1]
  paths = [raster, [f for f in rasters_b if f.split('.')[1] == rasterkey][0]]
  ToCombine[rasterkey] = paths



for key, rasterPaths in ToCombine.items():
  arcpy.MosaicToNewRaster_management(rasterPaths, OutputLocation, raster_dataset_name_with_extension=key+".tif", number_of_bands=1)
4
  • Actually I need to combine images based on the part of the name like A2017177 which is one dot from the left. This part of the name is actually the date of the acquisition of the image. This particular task can be done by this line ` d[file.split('.')[1]].append(os.path.join(root, file))`. Only problem in the code is that it doesn't write or save the output image in the output location Jan 14, 2021 at 14:47
  • Asif please see my updated answer.
    – John
    Jan 14, 2021 at 21:22
  • Thanks, this worked but I had to modify your code a little because os.scandir() was throwing error. This is what I was looking for. Thank you so much. Jan 15, 2021 at 7:03
  • You're welcome Asif! Always glad to help!
    – John
    Jan 15, 2021 at 18:17

Your Answer

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

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