Select to view content in your preferred language

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

7500
48
03-21-2019 12:12 AM
LochlanJones
Occasional Contributor

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
Emerging Contributor

yes sir

0 Kudos
ravitejaSETTIPALLI
Emerging Contributor

Lochlan Jones‌ could you look intho this?

0 Kudos
JohannesBierer
Frequent Contributor

After line 13 you could insert:

prefix1 = prefix1.replace("O", "Y")

Python String replace() Method - Tutorialspoint 
0 Kudos
ravitejaSETTIPALLI
Emerging Contributor

thanks i will work on this

0 Kudos
ravitejaSETTIPALLI
Emerging Contributor

sir this is my scenario,so that i want to chane/replace the  files in terra folder as MOD60_terra,   MOD61_terra,  ,MOD62_terra...... o and files in aqua folder  as MOD60_aqua,    MOD61_aqua,     MOD62_aqua.......

0 Kudos
JohannesBierer
Frequent Contributor

Don't forget to backup before ...

This code can be used to rename your raster data to MOD60, MOD61. Hopefully. Without terra and aqua, don't know why you want this?

import arcpy, os, re

workspace = r"yourpath"

walk = arcpy.da.Walk(workspace, topdown=True, datatype="RasterDataset")

for dirpath, dirnames, filenames in walk:
    
    for filename in filenames:

        rasterE = os.path.join(dirpath, filename)
        
        filename1 = re.sub("04_3K.A20190", "", filename)
        
        filename2 = filename1[:5]
        print filename2

        rasterRename = os.path.join(dirpath, filename2)

        arcpy.Rename_management(rasterE, rasterRename)
ravitejaSETTIPALLI
Emerging Contributor

thanks sir,finally its working, but thing is now how can i replace the file name  MOD by MYD (or) MYD by MOD to calculate mean.

0 Kudos
ravitejaSETTIPALLI
Emerging Contributor
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:\final3\AQUA'
... outws = r'D:\final3\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)

in previously i used this code to calculate the mean ,now what are the changes i have to make to calculate to calculate mean,could you please look into this.

0 Kudos
RazaKhan
New Contributor
>>> import arcpy
... from arcpy.sa import*
... arcpy.CheckOutExtension ("Spatial")
... u'CheckedOut'
... arcpy.env.overwriteOutput = True
... arcpy.env.workspace = r'I:\Test_Delete\AQUA_D'
... inws = arcpy.env.workspace
... inws1 = r'I:\Test_Delete\AQUA_N'
... outws = r'I:\Test_Delete\OUTPUT'
... rasters = arcpy.ListRasters()
... for r in rasters:    
...     prefix = os.path.splitext(r)[0]
...     suffix = os.path.splitext(r)[1]
...     prefix1 = prefix.rstrip("Aqua_D")
...     raster2 = os.path.join(inws1, prefix1 + "Aqua_N" + 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)

I'm using these codes to generate Cell-Statistics, as everyone using it here.
I have data in four different folders,
but right now I'm using two folders just for the testing but I couldn't get the result.
I'm a beginner level of Python user. So, Help me out with this code.

I'm getting this kind of error again and again:
... 
I:\Test_Delete\AQUA_N\clip1_A2002185Aqua_N.tif
I:\Test_Delete\OUTPUT\clip1_A2002185mean.tif
Runtime error 
Traceback (most recent call last):
 File "<string>", line 19, in <module>
RuntimeError: ERROR 000732: Input Raster: Dataset I:\Test_Delete\AQUA_N\clip1_A2002185Aqua_N.tif does not exist or is not supported

I have tried everything but still coudn't find the result.
0 Kudos