Mosaic raster files in a loop from all sub-directories in a directory

4562
5
Jump to solution
06-24-2013 01:58 PM
ZiaAhmed
New Contributor III
Mosaic raster files in a loop from all sub-directories in a directory
I am trying to mosaic all tif files from a directory, it works fine for the files for a particular directory using following python code. I like to run this code in a loop to process data  for all other months (~/year/month/date). Structure of my input dir, sub directories like this:
L:/MODIS_NDVI/TIF_files/2001/ January /Date_01
~  /Date_02
L:/MODIS_NDVI/TIF_files/2001/ February /Date_01
~  /Date_02
Help will be appreciated.
Zia

# Mosaic:  Date 01, January, 2001
try:
    import arcpy
    arcpy.env.workspace =  r"L:/MODIS_NDVI/TIF_files/2001/ January /Date_01"
    rasterList = arcpy.ListRasters("*","TIF")
    outLoc = "H:\\ MODIS_NDVI \\MOSAIC_Files \\2001\\January\\"+ raster[9:-16]
    arcpy.MosaicToNewRaster_management(rasterList, outLoc + ".tif", "", "16_BIT_SIGNED", "", "1", "LAST", "FIRST")
except:
    print "Mosiac failed."
    print arcpy.GetMessages()

#  Mosaic : Date 02, January, 2001
try:
    import arcpy
    arcpy.env.workspace =  r"L:/MODIS_NDVI/TIF_files/2001/ January /Date_02"
    rasterList = arcpy.ListRasters("*","TIF")
    outLoc = "H:\\ MODIS_NDVI \\MOSAIC_Files \\2001\\January\\"+ raster[9:-16]
    arcpy.MosaicToNewRaster_management(rasterList, outLoc + ".tif", "", "16_BIT_SIGNED", "", "1", "LAST", "FIRST")
except:
    print "Mosiac failed."
    print arcpy.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor
Glad I could help.

If you get a chance, could you click the checkmark in the upper right of the post that answered your question.

This will give me credit for it.

Thanks,

R_

View solution in original post

0 Kudos
5 Replies
RhettZufelt
MVP Frequent Contributor
Would probably read the years, months, and dates into their own list.


#  Mosaic : Date 02, January, 2001

import arcpy, os

years = []
months = []    ##  either populate these manually or read in
dates = []

try:
    
    for year in years:
       for month in months:
          for date in dates:
             workspace = arcpy.env.workspace = r"L:\MODIS_NDVI\TIF_files" + os.sep + year + os.sep + month + os.sep + date
             rasterList = arcpy.ListRasters("*","TIF")
             
             outLoc = workspace + os.sep + "mosaic.tif"
             arcpy.MosaicToNewRaster_management(rasterList, outLoc, "", "16_BIT_SIGNED", "", "1", "LAST", "FIRST")
except:
    print "Mosiac failed."
    print arcpy.GetMessages()



Have not tried this, nor am I anywhere that I can but should make a new mosaic.tif file in each date folder with all the tiff files from that folder mosaiced.

R_
0 Kudos
ZiaAhmed
New Contributor III
Thank you so much. After slight modification, following  code works fine when all files have organized  according to month (sub-folder) and dates (sub-sub-folder) in year directory.
If all files are one folder (for example - year_2001 folder), is it possible to apply python function in a loop - that will  mosaic files for a individual date (julian date - like 2001001- for January 1, 2001) one after another?   My study location is covered by two MODIS sheets (h25v06 and h26V06).  File names,  for example are 2001001.h25v06.tif, 2001001.h26v06, 2001017,h25v06, 2001017,h26v06 ......
Thanks again for helping me.
ZIa


import arcpy, os

months = ['01_Jan','02_Feb']   
dates = ['Date_01','Date_02']
Fn=''
for month in months:
    for date in dates:
        try:
            workspace = arcpy.env.workspace = r"M:\BD_MODIS_NDVI\PROJ_TIF\Test" + os.sep + month + os.sep + date+ os.sep
            rasterList = arcpy.ListRasters("*","TIF")
            for raster in rasterList:
                Fn= raster[0:7] # file names
            arcpy.MosaicToNewRaster_management(rasterList,workspace, Fn+".tif","", "16_BIT_SIGNED", "", "1", "LAST", "FIRST")
        except:
            print "Mosiac failed."
            print arcpy.GetMessages()


0 Kudos
RhettZufelt
MVP Frequent Contributor
Could try something like this:


#  Mosaic : Date 02, January, 2001

import arcpy, os

rasts = []
try:
     workspace = arcpy.env.workspace = r"L:/MODIS_NDVI/TIF_files/2001"
     rasterList = arcpy.ListRasters("*","TIF")
     for raster in rasterList:
         rasts.append(raster.split(".")[0])    ## grabs the filename before first "." which is J year and appends to the list "rasts"
         del raster
     rastSet = set(rasts)            ## make a set so unique (no duplicates) - result of one entry for each julian year
     for r in rastSet:                  ##iterate through set
         newList = []
         for raster in rasterList:         ## iterate through list of rasters again, and those that start with our value for r are appended to NewList.
             if raster.startswith(r):
                 newList.append(raster)   ## use this newList as you raster list for mosaic tool
         want to do the mosaic at this indentation level so it only does it for r = singlevalue
         del raster
         del newList

             

except:
    print "Mosiac failed."
    print arcpy.GetMessages()




That should get you going,

R_
0 Kudos
ZiaAhmed
New Contributor III
Thank you so much. It mosaic files accordingly. I appreciate your help. It save lot of my time. Here is the final code:


# Mosaic.py
# Created on: 2013-03-02 17:51:39.00000
# Description:
## ---------------------------------------------------------------------------
import arcpy, os
Fn=''
rasts = []
try:
     workspace = arcpy.env.workspace = r"M:\BD_MODIS_NDVI\PROJ_TIF\Test"
     rasterList = arcpy.ListRasters("*","TIF")
     for raster in rasterList:
         ## grabs the filename before first "." which is J year and appends to the list "rasts"
         rasts.append(raster.split(".")[0])
         del raster
     ## make a set so unique (no duplicates) - result of one entry for each julian year
     rastSet = set(rasts)
     ##iterate through set
     for r in rastSet:  
         newList = []
         ## iterate through list of rasters again, and those that start with our value for r are appended to NewList.
         for raster in rasterList:       
             if raster.startswith(r):
                 newList.append(raster)
                 for raster in newList:
                    Fn= raster[0:7] # file names
                 arcpy.MosaicToNewRaster_management(newList,workspace, Fn+".tif","", "16_BIT_SIGNED", "", "1", "LAST", "FIRST")
                 ## use this newList as you raster list for mosaic tool
         # want to do the mosaic at this indentation level so it only does it for r = singlevalue
         del raster
         del newList
            
except:
    print "Mosiac failed."
    print arcpy.GetMessages()



Could try something like this:


#  Mosaic : Date 02, January, 2001

import arcpy, os

rasts = []
try:
     workspace = arcpy.env.workspace = r"L:/MODIS_NDVI/TIF_files/2001"
     rasterList = arcpy.ListRasters("*","TIF")
     for raster in rasterList:
         rasts.append(raster.split(".")[0])    ## grabs the filename before first "." which is J year and appends to the list "rasts"
         del raster
     rastSet = set(rasts)            ## make a set so unique (no duplicates) - result of one entry for each julian year
     for r in rastSet:                  ##iterate through set
         newList = []
         for raster in rasterList:         ## iterate through list of rasters again, and those that start with our value for r are appended to NewList.
             if raster.startswith(r):
                 newList.append(raster)   ## use this newList as you raster list for mosaic tool
         want to do the mosaic at this indentation level so it only does it for r = singlevalue
         del raster
         del newList

             

except:
    print "Mosiac failed."
    print arcpy.GetMessages()




That should get you going,

R_
0 Kudos
RhettZufelt
MVP Frequent Contributor
Glad I could help.

If you get a chance, could you click the checkmark in the upper right of the post that answered your question.

This will give me credit for it.

Thanks,

R_
0 Kudos