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

6248
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‌ you are a hero! When I ran everything from the 'for r in raster' line, it worked. Here is the final code for my scenario:

>>> import arcpy, os
>>> from arcpy.sa import*
>>> arcpy.CheckOutExtension ("Spatial")
u'CheckedOut'
>>> 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_Clips_scale\output'
>>>
>>> raster = arcpy.ListRasters()
>>> for r in raster:
... prefix = os.path.splitext(r)[0]
... suffix = os.path.splitext(r)[1]
... prefix1 = prefix.rstrip("ndvi")
... raster2 = os.path.join(inws1, prefix1 + "combine" + suffix)
... print raster2
... outraster = os.path.join(outws, prefix1 + "multiply" + suffix)
... print outraster
... mycalc = arcpy.sa.Raster(r) * arcpy.sa.Raster(raster2)
... mycalc.save(outraster)

Thanks for your patience and assistance with this Lochlan Jones‌ - I finally can multiply my iterating rasters, and I learnt a lot along the way. I hope this thread can help others as much as it helped me!

ravitejaSETTIPALLI
New Contributor II

could you please recheck the final code and post that code,it was working not for me....

0 Kudos
ravitejaSETTIPALLI
New Contributor II

@Lochlan Jones    i too have same challenge but i have to calculate the mean for wo sets of corresponding rasters(each set conatains 136 files), named 'AQUA' (e.g. 'MOD04_3K.A2019060.mosaic.061.2019318071023.pssgmc_000501390776.Optical_Depth_Land_And_Ocean-Optical_Depth_Land_And_Ocean.tif') and 'TERRA'  (e.g.'MYD04_3K.A2019060.mosaic.061.2019318064730.pssgmc_000501390767.Optical_Depth_Land_And_Ocean-Optical_Depth_Land_And_Ocean.tif').

but iam facing this Runtime error
Runtime error 
Traceback (most recent call last):
File "<string>", line 7, in <module>
RuntimeError: ERROR 000732: Input Raster: Dataset D:\FINAL\TERRA\MYD04_3K.A2019060.mosaic.061.2019318064730.pssgmc_000501390767.Optical_Depth_Land_And_Ocean-Optical_DepthLand_And_Ocean.tif.tif does not exist or is not supported

0 Kudos
LochlanJones
New Contributor III

Hi Raviteja, assuming you copied and pasted the error, I think the problem will involve the fact that '.tif.tif' is doubled up at the end of the file it is trying to retrieve from the input dataset. This suggests to me there could be a problem with your formatting or use of splittext as it is taking the full file name with .tif at the end and then adding another .tif. Could you post your code exactly as you entered it?

0 Kudos
ravitejaSETTIPALLI
New Contributor II

0 Kudos
ravitejaSETTIPALLI
New Contributor II

This is my exact scenario,if possible please look into that trouble shoot me ,thanks in advance 

0 Kudos
LochlanJones
New Contributor III

The clue to your problem is in the light grey text towards the bottom. You will notice that those file names do not match with the files you’re trying to retrieve.

You need to make sure the ‘raster2 = ‘ command pieces together a name that matches your input data. It is making the wrong name and then it can’t find that file because of it so you get the error.

It’s also possible to file names might be too long but give it a go and it might be fine. I can take a closer look at this later on as well. Let me know if that makes sense

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

0 Kudos
ravitejaSETTIPALLI
New Contributor II

Thanks for reply sir,tomo I will try once again as per your inputs and will post my result .

0 Kudos
ravitejaSETTIPALLI
New Contributor II

SIR I TRIED AS YOU SAID,BUT AGAIN IT WAS SHOWING THE BELOW ERROR,PLEASE MAKE A LOOK INTO THAT SIT

0 Kudos
DanPatterson_Retired
MVP Emeritus

can you not shorten your file names substantially removing punctuation etc.

sometimes you can get away with it, but in general, it is asking for trouble

Recommendations for Limitations on Image Filenaming for managing image collections and image databas... 

Do not use more than one period in a file name, and limit the portion after the period to three characters (often referred to as the file "extension.").