Dear All,
I am trying to make a composite band of six raster files. I have do this for +40 scenes of Landsat 8 processed data. Each scene folder contains about +12 tif files, I want create multi-tif file with following bands using Arc-python batch mode:
LC81360432015069LGN00_toa_band2.tif | # Blue | |
LC81360432015069LGN00_toa_band3.tif" | # Green | |
LC81360432015069LGN00_toa_band4.tif" | # Red | |
LC81360432015069LGN00_toa_band5.tif" | # NIR | |
LC81360432015069LGN00_toa_band6.tif | # SWIR 1 | |
LC81360432015069LGN00_toa_band7.tif" | # SWIR 2 |
The following python script works fine when the folder contains only above six files. For this, I have to reorganize all 40 folders accordingly, I do not want to do this. This there any way to create a file list only with above six files? Help will be appreciated. Thanks
Zia
import arcpy, os
arcpy.env.workspace = r'E:\Landsat8\Process_data_2015\'
# list all folders in a directory
folders = arcpy.ListWorkspaces()
for folder in folders:
arcpy.env.workspace = folder
rasters = arcpy.ListRasters("*.tif")
name = os.path.join(rasters[1].split("_")[0] + ".tif")
arcpy.CompositeBands_management(rasters, name)
print "Processing complete"
Solved! Go to Solution.
Unfortunately you did not answer my question. It is important to know which file names you wish to exclude in order to know how to exclude them. Let's suppose you have the following rasters in a folder (see the list "rasters" in the code below, ending on band1.tif until band12.tif). You can use list comprehension to filter out the ones you don't need:
rasters = ['LC81360432015069LGN00_toa_band1.tif', 'LC81360432015069LGN00_toa_band2.tif', 'LC81360432015069LGN00_toa_band3.tif', 'LC81360432015069LGN00_toa_band4.tif', 'LC81360432015069LGN00_toa_band5.tif', 'LC81360432015069LGN00_toa_band6.tif', 'LC81360432015069LGN00_toa_band7.tif', 'LC81360432015069LGN00_toa_band8.tif', 'LC81360432015069LGN00_toa_band9.tif', 'LC81360432015069LGN00_toa_band10.tif', 'LC81360432015069LGN00_toa_band11.tif', 'LC81360432015069LGN00_toa_band12.tif'] chk4 = ['band{0}.tif'.format(i) for i in range(2, 8)] rasters = [a for a in rasters if a[-9:] in chk4] print rasters
This will yield:
['LC81360432015069LGN00_toa_band2.tif', 'LC81360432015069LGN00_toa_band3.tif', 'LC81360432015069LGN00_toa_band4.tif', 'LC81360432015069LGN00_toa_band5.tif', 'LC81360432015069LGN00_toa_band6.tif', 'LC81360432015069LGN00_toa_band7.tif']
... only the bands 2 until 7. This will only work if the assumption made before is true...
Kind regards, Xander
This would require removing the unwanted rasters from the list "rasters". Should be easy to do if you can exclude/include by a part of the name. Do you a list of a sample folder that has 12+ tifs?
Yes. I just want to list from LC81360432015069LGN00_toa_band2.tif to LC81360432015069LGN00_toa_band7.tif from each folder.
Thanks
Zia
Unfortunately you did not answer my question. It is important to know which file names you wish to exclude in order to know how to exclude them. Let's suppose you have the following rasters in a folder (see the list "rasters" in the code below, ending on band1.tif until band12.tif). You can use list comprehension to filter out the ones you don't need:
rasters = ['LC81360432015069LGN00_toa_band1.tif', 'LC81360432015069LGN00_toa_band2.tif', 'LC81360432015069LGN00_toa_band3.tif', 'LC81360432015069LGN00_toa_band4.tif', 'LC81360432015069LGN00_toa_band5.tif', 'LC81360432015069LGN00_toa_band6.tif', 'LC81360432015069LGN00_toa_band7.tif', 'LC81360432015069LGN00_toa_band8.tif', 'LC81360432015069LGN00_toa_band9.tif', 'LC81360432015069LGN00_toa_band10.tif', 'LC81360432015069LGN00_toa_band11.tif', 'LC81360432015069LGN00_toa_band12.tif'] chk4 = ['band{0}.tif'.format(i) for i in range(2, 8)] rasters = [a for a in rasters if a[-9:] in chk4] print rasters
This will yield:
['LC81360432015069LGN00_toa_band2.tif', 'LC81360432015069LGN00_toa_band3.tif', 'LC81360432015069LGN00_toa_band4.tif', 'LC81360432015069LGN00_toa_band5.tif', 'LC81360432015069LGN00_toa_band6.tif', 'LC81360432015069LGN00_toa_band7.tif']
... only the bands 2 until 7. This will only work if the assumption made before is true...
Kind regards, Xander
Thanks Xander. It works fine if I work with only one folder that contains only 12 tif files as you mentioned in your code. But in my case, I have to work with 42 folders. Each folders contains following tif files and file names is different. I just I want list file *_toa_band2.tif, *_toa_band3.tif, *_toa_band4.tif,*_toa_band5.tif, *_toa_band6.tif, and *_toa_band7.tif from each folder.
Thanks again
Zia
For example:
Folder name:
LC81360432015085-SC20150730095903
LC81360442015085-SC20150730095903
LC81360452015085-SC20150730095903
Files in a folder (LC81360432015085-SC20150730095903)
Would something like this help...
import sys, os rasters = ['LC81360432015069LGN00_toa_band1.tif', 'LC81360432015069LGN00_toa_band2.tif', 'LC81360432015069LGN00_toa_band3.tif', 'LC81360432015069LGN00_toa_band4.tif', 'LC81360432015069LGN00_toa_band5.tif', 'LC81360432015069LGN00_toa_band6.tif', 'LC81360432015069LGN00_toa_band7.tif', 'LC81360432015069LGN00_toa_band8.tif', 'LC81360432015069LGN00_toa_band9.tif', 'LC81360432015069LGN00_toa_band10.tif', 'LC81360432015069LGN00_toa_band11.tif', 'LC81360432015069LGN00_toa_band12.tif'] bandsrequired = [2, 3, 4, 5, 6, 7] for rastif in rasters: # get rid of the .tif part ras = rastif.split(".")[0] # get the last bit band?? band = ras.split("_")[2] bandNum = int(band.lstrip("band")) if bandNum in bandsrequired: # do something here print ras
Thanks. I have modified your codes. Now it works for all folders.
I appreciate your help.
Thanks again
Zia
#-------------------------------------
import arcpy, os
arcpy.env.workspace = r'E:\LandsatTest'
# list all folders in a directory
folders = arcpy.ListWorkspaces()
for folder in folders:
arcpy.env.workspace = folder
rasters = arcpy.ListRasters("*_toa_band*.tif")
chk4 = ['band{0}.tif'.format(i) for i in range(2, 8)]
rasters = [a for a in rasters if a[-9:] in chk4]
name = os.path.join(rasters[1].split("_")[0] + ".tif")
print(rasters)
You could put the line creating the list called "chk4" outside the loop. It will be the same for each iteration.
Could you mark the post that answered the question as the "correct answer" to close the thead?
Thanks