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

6227
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
ravitejaSETTIPALLI
New Contributor II

Dan Patterson Lochlan Jones shortened my names, but still iam facing same issue

0 Kudos
JohannesBierer
Occasional Contributor III

Don't know if it could be the reason, you are using spaces in your paths: ...\final 2\... . What if you use ...\final_2\... ?

0 Kudos
ravitejaSETTIPALLI
New Contributor II

i tried by removing spaces,but not working sir

0 Kudos
JohannesBierer
Occasional Contributor III
JohannesBierer
Occasional Contributor III

It can't work because you use different prefix. MOD60_ is not the same as MYD60_. So you could rename MYD60_ to MOD60_ etc. and it should find the rasterdata? Or use replace() in you script?

ravitejaSETTIPALLI
New Contributor II
>>> import arcpy, os
... from arcpy.sa import*
... arcpy.CheckOutExtension ("Spatial")
... u'CheckedOut'
... arcpy.env.overwriteOutput = True
... arcpy.env.workspace = r'D:\final2\TERRA'
... inws = arcpy.env.workspace
... inws1 = r'D:\final2\AQUA'
... outws = r'D:\final2\OUTPUT'
... rasters = arcpy.ListRasters()
... for r in rasters:    
...     prefix = os.path.splitext(r)[0]
...     suffix = os.path.splitext(r)[1]
...     prefix1 = prefix.rstrip("terra")
...     raster2 = os.path.join(inws1, prefix1 + "aqua" + suffix)
...     print raster2
...     outraster = os.path.join(outws, prefix1 + "mean" + suffix)
...     print outraster
...     outCellStatistics = CellStatistics([arcpy.sa.Raster(r), arcpy.sa.Raster(raster2)], "MEAN")
...     outCellStatistics.save(outraster)
...     
D:\final2\AQUA\MYD60_aqua.tif
D:\final2\OUTPUT\MYD60_mean.tif
D:\final2\AQUA\MYD61_aqua.tif
D:\final2\OUTPUT\MYD61_mean.tif
D:\final2\AQUA\MYD62_aqua.tif
D:\final2\OUTPUT\MYD62_mean.tif

thanks alot,it got finally worked.thanks you so much

0 Kudos
ravitejaSETTIPALLI
New Contributor II

but i have 200 files in each folder,so that i cant rename the each file manully,in earlier message you had told that use the replace(),in my script,could you teach me how to use the replace() syntax in my script.

0 Kudos
LochlanJones
New Contributor III

Not sure how to rename batches of files with python, but there is a free software worth looking into called file renamer that I’ve used in the past. But it’s possible someone on here might be able to help out with the renaming. I think windows powershell can do simple stuff like replacing specified text with some other text which is probably what you want assuming you want to change MYD to MOD. If you use Windows, look up something along the line so if ‘powershell file renaming replace’ and you’ll find plenty of helpful pages.

ravitejaSETTIPALLI
New Contributor II

>>> import arcpy, os
... from arcpy.sa import*
... arcpy.CheckOutExtension ("Spatial")
... u'CheckedOut'
... arcpy.env.overwriteOutput = True
... arcpy.env.workspace = r'D:\final2\TERRA'
... inws = arcpy.env.workspace
... inws1 = r'D:\final2\AQUA'
... outws = r'D:\final2\output'
... rasters = arcpy.ListRasters()
... for r in rasters:
...     prefix = os.path.splitext(r)[0]
...     suffix = os.path.splitext(r)[1]
...     prefix1 = prefix.rstrip("terra")
...     raster2 = os.path.join(inws, prefix1 + "aqua" + suffix)
...     print raster2
...     outraster = os.path.join(outws, prefix1 + "mean" + suffix)
...     print outraster
...     outCellStatistics = CellStatistics([arcpy.sa.Raster(r), arcpy.sa.Raster(raster2)], "MEAN", "NODATA")
...     outCellStatistics.save(outraster)
...     
D:\final2\TERRA\MOD60_aqua.tif
D:\final2\output\MOD60_mean.tif
Runtime error 
Traceback (most recent call last):
  File "<string>", line 19, in <module>
RuntimeError: ERROR 000732: Input Raster: Dataset D:\final2\TERRA\MOD60_aqua.tif does not exist or is not supported
0 Kudos
DanPatterson_Retired
MVP Emeritus

have you done it manually with one raster?

0 Kudos