I need to multiply multiple rasters in a folder with their corresponding rasters in another folder.

6156
48
03-21-2019 12:12 AM
LochlanJones
New Contributor III

I have one folder (folder 1) with 200 or more rasters and another folder (folder 2) with the same number. The rasters in folder 1 are all named genus_species_01 (e.g. acan_rufo_01, lito_bico_01, and so on). The rasters in folder 3 are the same but end with _03 instead. Each raster needs to be multiplied with it's corresponding raster from the other folder and the output must retain the species name and be saved in another folder. I am completely inexperienced when it comes to Python but I believe it is the only way to achieve this task.

The link below describes a possible solution to my problem only I am not proficient enough at formatting code (or understanding it) to be able to adapt it to my problem.

python - How to use the math tool ("times") in ArcGIS modelbuilder to multiply two raster sets while... 

I understand what most steps do but in some cases I'm unsure of what I need to change. In particular some of it would need to be changed based on the fact my rasters are located in separate folders rather than all within the same geodatabase as it is in the linked solution. Another part I am unsure about is prefix = r.split("_")[0] + "_" . To my understanding, this will split the raster name at the '_' character in order to retrieve the identifying first part of the name. But as far as I know the [0] refers to the first file in the specified workspace. So if my folder has over 200 rasters would I need to include r.split("_")[0] through to [200]?

Another potential issue is that I need both the abbreviate genus and species so it can only split the name at the second '_' and not the first.

Is anyone able to offer some guidance?

 

48 Replies
AnnaKreij
New Contributor III

Lochlan Jones‌ and Johannes Bierer‌ - I am entirely grateful to you both! Lochlan, I cannot thank you enough for taking the time to spell out each step as you have above - it's incredibly helpful. You have saved my code and advanced my knowledge substantially! Thank you!  

0 Kudos
AnnaKreij
New Contributor III

Lochlan Jones‌, the code appears to work well for me up until the raster calc stage (and thanks to you I now follow what it is doing). Can you see why I get error 000732 on my calculation? The files exist and have the same filename, as far as I can see (see below), in the original folders (workspace and inws1) but the output folder (outws) is still empty since the error appears before I can execute mycalc.save(outraster)...

In terms of my poor file paths - I tried both changing my filepaths to forwarded slashes as suggested by Dan Patterson and dragging and dropping the file paths , both providing the error below. Is there any other way I can improve the file paths to make this work?

 

>>> import arcpy, os
... from arcpy.sa import*
... arcpy.CheckOutExtension ("Spatial")
... 
... arcpy.env.overwriteOutput = True
... arcpy.env.workspace = r'D:/1_USGS/6_2005/NDVI_Clips_scale/ndvi'
... inws = arcpy.env.workspace
... inws1 = r'D:/1_USGS/6_2005/NDVI_Clips_scale/clouds_null'
... outws = r'D:/1_USGS/6_2005/NDVI_outputs'
... 
... rasters = arcpy.ListRasters()
... 
... for r in rasters:
... prefix = os.path.splitext(r)[0]
... suffix = os.path.splitext(r)[1]
... prefix1 = prefix.rstrip("ndvi")
... raster2 = os.path.join(outws, prefix1 + "combine" + suffix)
... print raster2
... outraster = os.path.join(outws, prefix1 + "multiply" + suffix)
... print outraster
... 
D:/1_USGS/6_2005/NDVI_outputs\20050122_combine.tif
D:/1_USGS/6_2005/NDVI_outputs\20050122_multiply.tif
D:/1_USGS/6_2005/NDVI_outputs\20050207_combine.tif
D:/1_USGS/6_2005/NDVI_outputs\20050207_multiply.tif
D:/1_USGS/6_2005/NDVI_outputs\20050318_combine.tif
D:/1_USGS/6_2005/NDVI_outputs\20050318_multiply.tif
>>> mycalc = arcpy.sa.Raster(r) * arcpy.sa.Raster(raster2)
Runtime error 
Traceback (most recent call last):
 File "<string>", line 1, in <module>
RuntimeError: ERROR 000732: Input Raster: Dataset D:/1_USGS/6_2005/NDVI_outputs\20050318_combine.tif does not exist or is not supported
>>> 

File_names

0 Kudos
DanPatterson_Retired
MVP Emeritus

D:/1_USGS/6_2005/NDVI_outputs\20050318_combine.tif

that is the output folder? but the input tif is in another folder.  It is failing before you even get  chance to complete the multiplication

LochlanJones
New Contributor III

Hi Anna, your new problem is that the ‘raster2’ line has ‘outws’ in it instead of ‘inws’. You are telling it to look in your output folder for your second raster set (combined). Change it to inws and try it again.

Get Outlook for iOS<https://aka.ms/o0ukef>

AnnaKreij
New Contributor III

That worked!! Thank you both. I cannot even begin to say how much I appreciate your prompt help on this.

Lochlan Jones you should be a teacher! Really - thank you for all your help and willingness to share. The world of programming beginners needs more people like you! 

0 Kudos
AnnaKreij
New Contributor III

Now my only (and hopefully final!) issue is that I only recieve one output, which is the output of the last file (20050318_multiply), rather than three as I was expecting from this test.

0 Kudos
LochlanJones
New Contributor III

I believe your last issue may be you’re missing the last line of code ‘mycalc.save(outraster)’ and because of this it is overwriting each output with each run leaving you with only the output of the final multiplication. If you include this last line and run it it should save them all.

Get Outlook for iOS<https://aka.ms/o0ukef>

AnnaKreij
New Contributor III

Thanks Lochlan Jones‌. I tried that and it brought me one step closer, but I receive the following error: 

>>> for r in rasters:
... prefix = os.path.splitext(r)[0]
... suffix = os.path.splitext(r)[1]
... prefix1 = prefix.rstrip("ndvi")
... raster2 = os.path.join(inws1, prefix1 + "combine" + suffix)
... outraster = os.path.join(outws, prefix1 + "multiply" + suffix)
... print outraster
... mycalc = arcpy.sa.Raster(r) * arcpy.sa.Raster(raster2)
... mycalc.save(outraster)

Runtime error
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: 'function' object is not iterable

Would the be the raster calculation function that is not iterable?

If you don't have any more time to spend on this I understand - I've really appreciated all your help so far. 

0 Kudos
LochlanJones
New Contributor III

Are you running everything before the ‘for r in rasters:’ at the same time as everything after? Or are you running the first part and then the second part? I think some of it won’t work if it isn’t all run at the same time.

Get Outlook for iOS<https://aka.ms/o0ukef>

0 Kudos
LochlanJones
New Contributor III

Can you also post the full code that’s giving you this error? It can be easy to miss one tiny thing. A common cause of this error message is missing the parentheses after a function. In this case the problem could be the line ‘rasters = arcpy.ListRasters()’. If you don’t have the parentheses at the end of it or you don’t run it at the same time as the ‘for r in rasters’ line it won’t work.

Get Outlook for iOS<https://aka.ms/o0ukef>