calculate mean of selected raster chosen from multiple raster using ArcPy

5323
52
Jump to solution
08-24-2016 11:15 AM
ShouvikJha
Occasional Contributor III

I am trying to  calculate mean of selected raster chosen from multiple raster. I have multiple raster file in different month folder, i am trying to select only two raster (e.g For JANUARY : 001_Max_Temper.tif, 001_Min_Temper.tif) from different month folder (JANUARY, FEBRUARY....DECEMBER) and calculate the mean of those selected raster and save it as on same month folder (e.g output name, JANUARY: 001_Mean_Temp). 

I have written a code to do this task but i am getting error massage while i am running this code. Below i have attached my code.  

import arcpy, os, calendar
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
arcpy.env.parallelProcessingFactor = "100%"
topWorkspace = r'D:\SWAT-WEATHER-DATA2'
arcpy.env.workspace = topWorkspace

# Get dict of months and month-number (i.e. January = 001, March = 003 etc.)
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} # Get dict of months and month number (i.e. January = 001, March = 003 etc.)

# Step through list of all folders
for folderPath in arcpy.ListWorkspaces():
    baseName = os.path.basename(folderPath).upper()
    if baseName in months: # Test that subfolder is a month name
        monthNumber = months[baseName] # Get month-number for use in output filename
        arcpy.env.workspace = folderPath
        rasterList = arcpy.ListRasters('*Max_Temper.tif', '*Min_Temper.tif')
        # Execute CellStatistics
        outCellStatistics = CellStatistics(rasterList, "MEAN", "NODATA")
        #Save the output
        outRasterName = outCellStatistics, "Mean_Temp_{}.tif".format(monthNumber)
        outCellStatistics.save(outRasterName)
        

#print done
print 'done'
Tags (2)
0 Kudos
52 Replies
ShouvikJha
Occasional Contributor III

Xander Bakker‌. This one is very useful step to verify the length of rasterList before calculating mean. Thank you 

0 Kudos
ShouvikJha
Occasional Contributor III

xander_bakker‌ Thank you. code is working fine. Once again thank you for your kind cooperation.  

0 Kudos
DanPatterson_Retired
MVP Emeritus

perhaps you could mark Xander's response correct so there is some closure to this thread

ShouvikJha
Occasional Contributor III

Dan_Patterson‌ . I already marked it correct. Thank you 

0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌. I just tried to modify above code for another calculation, But error occurring. Please cooperate me. Below i shown the modified code along with error massage, 

Message File Name Line Position
Traceback
 <module> D:\Arc-GIS-Python-Script\T_SCALAR.py 27
 Minus C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Functions.py 3840
 swapper C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Utils.py 47
 wrapper C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\sa\Functions.py 3837
TypeError: expected a raster or layer name‍‍‍‍‍‍‍

import arcpy, os, calendar
from arcpy.sa import *
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
arcpy.env.parallelProcessingFactor = "100%"
topWorkspace = r'D:\SWAT-WEATHER-DATA2'
ws_out = r'D:\SWAT-WEATHER-DATA2\TScalar' ### Output folder!!!
arcpy.env.workspace = topWorkspace
# Get dict of months and month-number (i.e. January = 001, March = 003 etc.)
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} # Get dict of months and month number (i.e. January = 001, March = 003 etc.)

# Step through list of all folders
for folderPath in arcpy.ListWorkspaces():
 baseName = os.path.basename(folderPath).upper()
 if baseName in months: # Test that subfolder is a month name
 monthNumber = months[baseName] # Get month-number for use in output filename
 arcpy.env.workspace = folderPath
# Raster1 take Mean_Temp raster from all month folder(e.g for january: Mean_temp_001.tif) and Raster 2 is constant month of october raster
 rasterList1 = arcpy.ListRasters(r'Mean_Temp*.tif')
 rasterList2 = arcpy.Raster(r'D:\SWAT-WEATHER-DATA2\OCTOBER\Mean_Temp_010.tif')

 ### combine list raster into equation

 #TScalar1 = 1.1814 / (1 + EXP(0.2 * (rasterList2 - 10- rasterList1)))
 TScalar1 = 1.1814 / ((1 + Exp(0.2 * (rasterList2 - 10 - rasterList1))))
 
 #TScalar2 = 1/(1+EXP(0.3*(- rasterList2 - 10 + rasterList1)))
 TScalar2 = 1 / (1 + Exp(0.3 * (- rasterList2 - 10 - rasterList1)))

# Multiply TScalar1 and TScalar2
 TScalar = (TScalar1 * TScalar2)

#Save the output
 outRasterName = os.path.join(ws_out, "T_SCALAR_{}.tif".format(monthNumber))
 TScalar.save(outRasterName)
 print outRasterName

#print done
print 'done'‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
XanderBakker
Esri Esteemed Contributor

On line 26 and 29 you are trying to do a calculation on a list. This is not possible. You should perform the calculation using Raster objects.

What exactly are you trying to do? Can you add a brief explanation?

0 Kudos
ShouvikJha
Occasional Contributor III

I want to calculate two different peramater using Monthly mean Temp. data using that equation. That Monthly mean Temp data stored in all monthly folder based on different month, for raster1 i want to take mean temp. raster from different month (e.g. JANAURAY : Mean_Temp_001, FEBRUARY: Mean_Temp_002 so on) and raster2 i want to keep constant the raster map of Month of October data (Mean_Temp_010). 

Later stage, i want to multiply TScalar1 and TScalar2 and save it TScalar folder 

0 Kudos
XanderBakker
Esri Esteemed Contributor

If I understand it correctly, you would need to do something like this:

import arcpy, os, calendar
from arcpy.sa import *
from arcpy import env
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
arcpy.env.parallelProcessingFactor = "100%"
topWorkspace = r'D:\SWAT-WEATHER-DATA2'
ws_out = r'D:\SWAT-WEATHER-DATA2\TScalar' ### Output folder!!!
arcpy.env.workspace = topWorkspace

# Get dict of months and month-number (i.e. January = 001, March = 003 etc.)
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} 

# Step through list of all folders
for folderPath in arcpy.ListWorkspaces():
    baseName = os.path.basename(folderPath).upper()
    if baseName in months: # Test that subfolder is a month name
        monthNumber = months[baseName] # Get month-number for use in output filename
        arcpy.env.workspace = folderPath

        # Raster1 take Mean_Temp raster from all month folder(e.g for january: Mean_temp_001.tif) and Raster 2 is constant month of october raster
        rasterList1 = arcpy.ListRasters(r'Mean_Temp*.tif')
        ras_oct = arcpy.Raster(r'D:\SWAT-WEATHER-DATA2\OCTOBER\Mean_Temp_010.tif')

        for ras_name in rasterList1:
            ras_mean = arcpy.Raster(os.path.join(folderPath, ras_name))

            # TScalar1 = 1.1814 / (1 + EXP(0.2 * (rasterList2 - 10 - rasterList1)))
            ras_scal1 = 1.1814 / ((1 + Exp(0.2 * (ras_oct - 10 - ras_mean))))

            # TScalar2 = 1/(1+EXP(0.3*(- rasterList2 - 10 + rasterList1)))
            ras_scal2 = 1 / (1 + Exp(0.3 * (-1 * ras_oct - 10 - ras_mean)))

            # Multiply TScalar1 and TScalar2
            ras_scal = (ras_scal1 * ras_scal2)

            # Save the output
            outRasterName = os.path.join(ws_out, "T_SCALAR_{}.tif".format(monthNumber))
            print outRasterName
            ras_scal.save(outRasterName)
            

#print done
print 'done'

For every month it will do the calculations. However, not sure if this will be correct for the month of October, when the mean raster is the same as the constant raster.

ShouvikJha
Occasional Contributor III

xander_bakker‌. Thank you very much for your kind support. Code working perfectly. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Glad to hear that. What about the month October? Does the result make sense?