I have two different folder (workspace1 and workspace2) with Rasters data with the two different parameters. Whatever raster in workspace1 (input raster name e.g 001_mean, 002_mean, 003_mean, 004_mean ….012_mean ), I would like to execute below equation
((Raster – Min Rater value) * (0.95 – 0.01)) / (Max raster value – Min Raster Value )
and save it into outputfolder 1 name of output (e.g. 001_FPAR, 002_FPAR, 003_FPAR, 004_FPAR ….012_FPAR)
After calculating workspace1, I want to take different raster file from workspace 2 (raster name , 001_SR, 002_SR, 003_SR, 004_SR……012_SR) and multiply it with each raster file with calculated raster from workspace 1 and save it into outputfolder 2 as name of 001_APAR, 002_APAR, 003_APAR, 004_APAR ……012_APAR.
I have attached below the list of input raster.
I have written the code below but its giving error
import arcpy
arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *
workspace1 = glob.glob(r"D:\MODIS-NDVI-2012\MASKED-NDVI-2012A")
workspace2 = glob.glob(r"D:\MODIS-NDVI-2012\MASKED")
outFolder1 = r"D:\MODIS-NDVI-2012\FPAR"
outFolder2 = r"D:\MODIS-NDVI-2012\APAR"
list = arcpy.ListRasters()
for inRaster in list:
localRaster = Raster(inRaster)
for r in workspace1
NDVIMINResult = arcpy.GetRasterProperties_management(inRaster1, "MINIMUM")
NDVIMAXResult = arcpy.GetRasterProperties_management(inRaster1, "MAXIMUM")
NDVIMin = float(NDVIMINResult.getOutput(0))
NDVIMax = float(NDVIMAXResult.getOutput(0))
outminus = Minus(inRaster1, NDVIMin)
outminus1 = Minus(0.95-0.01)
outmultiply = Multiply(outminus,outminus1)
outminus2 = Minus(NDVIMax, NDVIMin)
outdevide = Devide(outmultiply,outminus2)
outdevide.save = (outdevide)
for r in workspace2
outmultiply1 = Multiply (outdevide,Raster)
outmultiply1.save (outmultiply1)
Solved! Go to Solution.
Thank you Dan_Patterson.There was small correction needed, i have solved the issue. Thank you very much
Xander Bakker. Thank you very much. Finally i have solved the issue. Now the code is running fine without any issues. There was small correction on line 42, Corrected line is, after the rster name sr command did not include. Once again thank you very much.
ras_sr = arcpy.Raster(os.path.join(ws_in_sr, ras_name_sr))
Xander Bakker I want to do small modification on above code, following instruction, Instead of multiplication (Line no 43) FPAR * sr raster, i want to do multiplication FPAR * (any numaric value e.g 20) and output raster pass it to APAR folder. meanwhile upto FPAR i want to keep the program same, only APAR stage i want to modify by multiply Numeric digit (any numaric value e.g 20) with FPAR instead of SR raster.
line 43 contains this code:
ras_apar = ras_sr * ras_fpar
but you can change it to:
ras_apar = ras_sr * 20
and it will still work.
Is that want you want? It will still generate the same output apar filename, so if you don't want to loose the other results, you should change the output filename on line 46
xander_bakker. Thank you. Yes exactly same i was looking for which you mentioned .The modified code is working perfectly. Once again thank you very much for the cooperation.
You're welcome. I'm glad it works!
Xander Bakker. I would like to do small modification on above code.
Instead of Multiplication sr raster with FPAR (Line no 43). I want to multiply some specific value with monthly FPAR raster. And keep as it same.
JANUARY | FEBRUARY | MARCH | APRIL | MAY | JUNE | JULY | AUGUST | SEPTEMBER | OCTOBER | NOVEMBER | DECEMBER |
179.36 | 194.95 | 239.44 | 289.05 | 318.66 | 272.35 | 227.13 | 182.76 | 168.53 | 210.87 | 176.31 | 182.41 |
(Line no 43: ras_apar = ras_sr * ras_fpar)
Than i want to execute this equation to calculate one more parameter (e.g for JANUARY : 0.5 + 0.5 * (001_APAR/179.36) , for FEBRUARY: 0.5 + 0.5*(002_APAR/194.95 (For JANUARY)), and save it to workspace (D:\MODIS-NDVI-2012\ws_out_wscaler).
Not a complete solution, but below an example of how you can implement it. The idea is to create a dictionary (lines 5 to 7). You can extract the value for each month using the numbers that represent the month and multiply that value directly with the raster. The solution to the second question is shown starting at line 27 and uses the same concept.
def main():
# you should create a dictionary that allows you to get the values you need
# in this case the key is the month indicator
dct = {'001': 179.36, '002': 194.95, '003': 239.44, '004': 289.05,
'005': 318.66, '006': 272.35, '007': 227.13, '008': 182.76,
'009': 168.53, '010': 210.87, '011': 176.31, '012': 182.41}
# Instead of Multiplication sr raster with FPAR (Line no 43).
# I want to multiply some specific value with monthly FPAR raster
ras_name = '007_SR'
ras_num = ras_name[:3]
# extract the value based on the month numbers (ras_num)
multiply_val = dct[ras_num]
print ras_name, ras_num, multiply_val
# multiply the value with the raster, and save the raster
# Than I want to execute this equation to calculate one more parameter
# (e.g for JANUARY : 0.5 + 0.5 * (001_APAR/179.36) ,
# for FEBRUARY: 0.5 + 0.5*(002_APAR/194.95 (For JANUARY)),
# and save it to workspace (D:\MODIS-NDVI-2012\ws_out_wscaler)
ws_out_wscaler = r'D:\MODIS-NDVI-2012\ws_out_wscaler'
ras_name = '005_APAR'
ras_num = ras_name[:3]
divide_val = dct[ras_num]
ras_wscalar = 0.5 + 0.5 * ras_apar / divide_val
out_name_wscalar = os.path.join(ws_out_wscalar, 'r{0}_WSCALAR'.format(ras_num))
ras_wscalar.save(out_name_wscalar)
if __name__ == '__main__':
main()
Xander Bakker. Thank you. I have forget to mentioned one more step, I am extremely sorry for the inconvenience.
Here with i am giving clear picture of the code, kind consider it
def main():
import arcpy
import os
arcpy.env.overwriteOutput = True
# Checkout extension
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
ws_in_mean = r'D:\MODIS-NDVI-2012\MASKED-NDVI-2012A'
ws_out_Vfraction= r'D:\MODIS-NDVI-2012\VEGETATION_Fraction'
ws_out_AET= r'D:\MODIS-NDVI-2012\ACTUAL_ET'
ws_out_Wscalar = r'D:\MODIS-NDVI-2012\W_SCALAR'
# list "mean" rasters
arcpy.env.workspace = ws_in_mean
lst_ras_mean = arcpy.ListRasters()
print "lst_ras_mean", lst_ras_mean
for ras_name in lst_ras_mean:
ras_mean = arcpy.Raster(os.path.join(ws_in_mean, ras_name))
print "ras_mean", ras_name
# calculate ((Raster * Min Rater value) * (0.95 * 0.01)) / (Max raster value * Min Raster Value )
ras_Vfraction = sqrt((ras_mean - ras_mean.minimum) / (ras_mean.maximum - ras_mean.minimum))
ras_Vfraction.save(out_name_Vfraction)
# save raster
ras_num = ras_name[:3]
out_name_fpar = os.path.join(ws_out_fpar, 'r{0}_Vfraction.TIF'.format(ras_num))
ras_fpar.save(out_name_vfraction)
# calculate Wscalar raster
dct = {'001': 179.36, '002': 194.95, '003': 239.44, '004': 289.05,
'005': 318.66, '006': 272.35, '007': 227.13, '008': 182.76,
'009': 168.53, '010': 210.87, '011': 176.31, '012': 182.41}
# Next multiplication of monthly Vfraction raster with monthwise dct value
# and store it to ws_out_AET folder name of 001_AET to 012_AET
ras_AET = 001_Vfraction * 179.36 (001)
# After that i want to execute this equation to calculate monthly step WScalar
# And save it to ws_out_Wscalar folder name of 001_WScalar
ras_wscalar = 0.5 + 0.5 * 001_AET / 001_dct Value (179.36)
The code is a little hard to interpret, because it does not have correct indentation. Furthermore on line 22 you save a raster with a name stored in the variable "out_name_Vfraction" and this variable does not exist. When you post code, enter the advanced editor (upper right corner when you add a reply):
Expand the toolbar:
In the expanded toolbar, click more and Syntax highlighter:
Select the language (Python):
Paste you code:
Click OK in the lower part of the screen
And the code will appear with the correct format:
Perhaps it is better if you paste the latest working code and explain the additional steps you want to include and I post back the new code.