I have a folder with filenames bio1_36.tif, bio1_37.tif, bio2_36_tif, bio2_37.tif, and so on. There are 19 bio variables and 36 and 37 at the end refer to the tile numbers. My goal is to mosaic the two tiles for each variable, clip to the size of a polygon layer called “Katanga” and convert the final output to ascii format. I’ve been using the code below however this will only generate the correct output if the two tiles for the same bio variable are specified in the code or these two files are the only input in the workspace folder.
How can I do the above procedure in a recursive manner for each bio variable, i.e. setting up a loop?
This way I don’t have to create separate folders for each bio variable or specify them in the code.
I am new to Python.
Below is the code I’ve been using (note: "test" folder only contains one bio variable with the two tiles):
import arcpy
from arcpy import env
from arcpy.sa import*
import time
start_time = time.clock()
env.workspace = "G:/Eck_health_disease/spatial data/WorldClimate data/test"
arcpy.env.overwriteOutput = True
ImgList = arcpy.ListRasters()
print "Processing Mosaic"
print "Number of tiles to be Mosaicked:" + str(len(ImgList))
arcpy.MosaicToNewRaster_management(ImgList, env.workspace, "bio1_mos.tif", pixel_type="32_BIT_FLOAT", cellsize="", number_of_bands="1", mosaic_method="", mosaic_colormap_mode="MATCH")
Katanga_poly = "G:/Eck_health_disease/spatial data/COD_adm_shp/DRC_Katanga.shp"
arcpy.Clip_management("bio1_mos.tif","21.7447528839 -13.4556760785 30.7780862172 -4.99734274513", "bio1_mos_Kat.tif", Katanga_poly, "#", "ClippingGeometry", "MAINTAIN_EXTENT")
arcpy.RasterToASCII_conversion("bio1_mos_Kat.tif", "bio1_mos_Kat.asc")
print "Task Completed!"
print time.clock() - start_time, "seconds"
Any help would be appreciated. Thank you!
Solved! Go to Solution.
I've updated my code to search all the bioX rasters for each iteration.
You can iterate your variable numbers:
import arcpy
from arcpy import env
from arcpy.sa import*
import time
start_time = time.clock()
env.workspace = "G:/Eck_health_disease/spatial data/WorldClimate data/test"
arcpy.env.overwriteOutput = True
print "Processing Mosaic"
print "Number of tiles to be Mosaicked:" + str(len(ImgList))
Katanga_poly = r"G:\Eck_health_disease\spatial data\COD_adm_shp\DRC_Katanga.shp"
for i in range(1,19):
ImgList = arcpy.ListRasters("bio"+str(i)+"*")
arcpy.MosaicToNewRaster_management(ImgList, env.workspace, "bio"+str(i)+"_mos.tif", pixel_type="32_BIT_FLOAT", cellsize="", number_of_bands="1", mosaic_method="", mosaic_colormap_mode="MATCH")
arcpy.Clip_management("bio"+str(i)+"_mos.tif","21.7447528839 -13.4556760785 30.7780862172 -4.99734274513", "bio"+str(i)+"_mos_Kat.tif", Katanga_poly, "#", "ClippingGeometry", "MAINTAIN_EXTENT")
arcpy.RasterToASCII_conversion("bio"+str(i)+"_mos_Kat.tif", "bio"+str(i)+"_mos_Kat.asc")
print "Task Completed!"
print time.clock() - start_time, "seconds"
Hello,
Thank you very much for the suggestion. However, this code generate only
one mosaic file named *bio1_mos.tif *instead of two when I tried on a test
folder which contained bio1_36.tif, bio1_37.tif, bio2_36_tif,
bio2_37.tif files.
And, all of these files were used in the mosaicking. What I want is to
mosaic only bio1_36.tif, bio1_37.tif and create an output named *bio1_mos.tif.
*Then, mosaic bio2_36_tif, bio2_37.tif and generate an output file named
bio2_mos.tif and do this for all 19 variables.
Thank you for your help.
Regards,
Kemal
I've updated my code to search all the bioX rasters for each iteration.
Thank you! That does the trick. Can you tell me what "*" does in the updated line "bio"+str(i)+"*" ?
Also, range (1,19) goes from 1 to 18 so for 19 variables it needs to be (1,20). I figured that out when I was working
with a test folder with four files.
Thanks again! It saved a lot of time.
The "*" is a wildcard character that basically means any character after the "bio" string, so for this example the ListRasters function will be filtered to all datasets starting with "bio" and the selected parameter number e.g. all "bio1" rasters.
Thanks for the explanation. Now, I can use it in other situations. Best,
Kemal
Hello again,
I've also been working on the code below. Maybe you can help me with this
if it makes sense. I could get it to generate the bio1_mos and bio2_mos tif
files but the processing of mosic to new raster is giving error and the
files are empty. Please note that "test" folder contains bio1_36 and
bio2_36.tif files. test2 file contains bio1_37 and bio2_37.tif files.
Thank you!
import arcpy, os, sys
from arcpy import env
from arcpy.sa import *
import time
start_time = time.clock()
this will overwrite output. Important in testing codes
arcpy.env.overwriteOutput = True
activate the spatial analyst extention of ArcGIS
arcpy.CheckOutExtension("Spatial")
path = "G:/Eck_health_disease/spatial data/WorldClimate data/test"
bio36 =
f=[]
for fname in bio36:
with open(os.path.join(path, fname), 'r') as fr:
f.append(fr.readlines())
path = "G:/Eck_health_disease/spatial data/WorldClimate data/test2"
bio37 =
f=[]
for fname in bio37:
with open(os.path.join(path, fname), 'r') as fr:
f.append(fr.readlines())
newList=[]
mosaic =[]
basename=[]
##counter = 1
for i in range(len(bio36)):
for j in range(len(bio37)):
try:
workspace = arcpy.env.workspace =
"G:/Eck_health_disease/spatial data/WorldClimate data/bio_mos"
basename = os.path.basename(bio36).split("_")[0]
r1 = arcpy.sa.Raster(i)
r2 = arcpy.sa.Raster(os.path.join(bio37, basename +
"_37.tif"))
newList = [bio36, bio37]
list = ";".join(newList)
rasterList = arcpy.ListRasters(NewList)
Fn = raster[0:1]
outname = basename + "_mos.tif"
mosaic =
arcpy.MosaicToNewRaster_management(list,workspace,outname,"","32_BIT_FLOAT",
"", "1", "","MATCH")
mosaic.save(os.path.join(workspace,outname))
except:
print "Mosaic failed."
print arcpy.GetMessages()
print "Task Completed!"
print time.clock() - start_time, "seconds"