Calculate list of raster from two different subfolder using ArcPy

4087
29
Jump to solution
08-17-2016 04:02 AM
ShouvikJha
Occasional Contributor III

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (3)
0 Kudos
29 Replies
ShouvikJha
Occasional Contributor III

xander_bakker‌. Thank you for your suggestion. here-with I have tried to give you a glimpse  what actually i want to do.  i am looking for your kind cooperation. Thank you  

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 Vfraction sqrt((Raster * Min Rater value)) / (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 for Vfraction for different month 
 ras_num = ras_name[:3]
 out_name_Vfraction = os.path.join(ws_out_Vfraction, 'r{0}_Vfraction.TIF'.format(ras_num))
 ras_Vfraction.save(out_name_vfraction)

# calculate AET raster(multiplication of monthly dict value with same month Vfraction raster(e.g. 001_Vfraction * 179.36 (value taken from 001_dict)) 
 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 same month dct value (e.g. JANUARY dct :001)
 # and store it to ws_out_AET folder name of 001_AET to 012_AET
      ras_AET = 001_Vfraction * 179.36 (001_dct)

# After that i want to execute this equation to calculate monthly step WScalar
# And save it to ws_out_Wscalar folder name of e.g for JANUARY: 001_WScalar‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍, For FEBRUARY: 002_WSCALAR .....012_WSCALAR
  ras_wscalar = (0.5 + 0.5 * (001_AET / 179.36))(179.36 value taken from 001_dict))


‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
XanderBakker
Esri Esteemed Contributor

I more or less understand what you are trying to do, but if you could provide the latest working code, so I can include the new things? That would take me less time than correct the script you posted. 

0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌. Thanks  Upto Vfraction (Line no: 28) whatever i had   already i shared with you in my last reply but next stage i don't understanding  how to execute those function into code.  Below i have attached the reference raster which i am working. Please find it 

0 Kudos
XanderBakker
Esri Esteemed Contributor
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'

    # dictionary with values per month
    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}

    # 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_num = ras_name[:3]

        ras_mean = arcpy.Raster(os.path.join(ws_in_mean, ras_name))
        print "ras_mean", ras_name

        # calculate Vfraction sqrt((Raster * Min Rater value)) / (Max raster value - Min Raster Value )
        ras_Vfraction = sqrt((ras_mean - ras_mean.minimum) / (ras_mean.maximum - ras_mean.minimum))

        # save raster for Vfraction for different month
        out_name_Vfraction = os.path.join(ws_out_Vfraction, 'r{0}_Vfraction.TIF'.format(ras_num))
        ras_Vfraction.save(out_name_Vfraction)
        
        # Next multiplication of monthly Vfraction raster with same month dct value (e.g. JANUARY dct :001)
        month_value = dct[ras_num]
        ras_AET = ras_Vfraction * month_value

        # and store it to ws_out_AET folder name of 001_AET to 012_AET
        out_name_AET = os.path.join(ws_out_AET, 'r{0}_AET.TIF'.format(ras_num))
        ras_AET.save(out_name_AET)

        # After that i want to execute this equation to calculate monthly step WScalar
        ras_wscalar = 0.5 + 0.5 * ras_AET / month_value

        # And save it to ws_out_Wscalar folder name of e.g for JANUARY: 001_WScalar, For FEBRUARY: 002_WSCALAR .....012_WSCALAR
        out_name_wscalar = os.path.join(ws_out_Wscalar, 'r{0}_WSCALAR.TIF'.format(ras_num))
        ras_wscalar.save(out_name_wscalar)

if __name__ == '__main__':
    main()

Once again, I did not test the code, but it should be something like this.

ShouvikJha
Occasional Contributor III

Xander Bakker‌. Thank you very much. Code given by you working absolutely correct. Many many thank you for your kind cooperation. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

You're welcome. I'm glad works!

0 Kudos
ShouvikJha
Occasional Contributor III

Xander Bakker‌. I am trying to bit modify the above code, I am trying to multiplication month wise value with FPAR raster (line no 36), but code generating only December month's output from  both calculation. (Line no 27 and 36)

I have made one code, take reference from earlier code , Below is my working code .

Problem with the code is, this code generating only December month's output (r012_FPAR, r012_APAR, ). rest of month its not generating any output. I have tried to find out the solution, but i could not able to find out. 

def main():
 import arcpy
 import os
 from arcpy.sa import *
 arcpy.env.overwriteOutput = True
 # Checkout extension
 arcpy.CheckOutExtension("Spatial")
 arcpy.env.overwriteOutput = True
# avoid using - in folders in combination with raster calculations!
 ws_in_mean = r'D:\MODIS-NDVI-2012\MASKED-NDVI-2012A'
 ws_out_fpar = r'D:\MODIS-NDVI-2012\FPARC'
 ws_out_apar = r'D:\MODIS-NDVI-2012\APARC'
# dictionary with values per month
 dct = {'001': 17,'002': 19, '003': 23, '004': 28,
        '005': 31,'006': 27, '007': 22, '008': 18,
        '009': 16,'010': 21, '011': 17, '012': 18}
# 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 ) + 0.01
 ras_fpar = (((ras_mean - ras_mean.minimum) * (0.95 - 0.01)) / (ras_mean.maximum - ras_mean.minimum)) + 0.01

# save raster
 ras_num = ras_name[:3]
 out_name_fpar = os.path.join(ws_out_fpar, 'r{0}_FPAR.TIF'.format(ras_num))
 ras_fpar.save(out_name_fpar)

#Calculate APAR
 month_value = dct[ras_num]
 ras_apar = ras_fpar * month_value

# save APAR raster
 out_name_apar = os.path.join(ws_out_apar, 'r{0}_APAR.TIF'.format(ras_num))
 ras_apar.save(out_name_apar)

if __name__ == '__main__':
 main()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
XanderBakker
Esri Esteemed Contributor

There can be several reason for this. One being that the list "lst_ras_mean" only contains the raster for the month december, but since you print the content of this list, I assume that that is not the case. The other possible (most likely) reason is your indentation. In this specific case you only produce the last raster in the list, which means that the save action is not within the loop started on line 26. Since the code you pasted does not have valid indentation (it will not run) could you please revise the way you paste your code. Please do not paste the code in the thread, then select that text and apply the syntax highlighting, since that will corrupt your indentation. Please follow the steps exactly as described here:  Posting code with Syntax Highlighting on GeoNet 

If the code is structured like this, it should produce the output for each month (line 42-44) are include in the loop. 

def main():
    import arcpy
    import os
    from arcpy.sa import *
    arcpy.env.overwriteOutput = True

    # Checkout extension
    arcpy.CheckOutExtension("Spatial")
    arcpy.env.overwriteOutput = True

    # avoid using - in folders in combination with raster calculations!
    ws_in_mean = r'D:\MODIS-NDVI-2012\MASKED-NDVI-2012A'
    ws_out_fpar = r'D:\MODIS-NDVI-2012\FPARC'
    ws_out_apar = r'D:\MODIS-NDVI-2012\APARC'

    # dictionary with values per month
    dct = {'001': 17,'002': 19, '003': 23, '004': 28,
        '005': 31,'006': 27, '007': 22, '008': 18,
        '009': 16,'010': 21, '011': 17, '012': 18}

    # 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 ) + 0.01
        ras_fpar = (((ras_mean - ras_mean.minimum) * (0.95 - 0.01)) / (ras_mean.maximum - ras_mean.minimum)) + 0.01

        # save raster
        ras_num = ras_name[:3]
        out_name_fpar = os.path.join(ws_out_fpar, 'r{0}_FPAR.TIF'.format(ras_num))
        ras_fpar.save(out_name_fpar)

        #Calculate APAR
        month_value = dct[ras_num]
        ras_apar = ras_fpar * month_value

        # save APAR raster
        out_name_apar = os.path.join(ws_out_apar, 'r{0}_APAR.TIF'.format(ras_num))
        ras_apar.save(out_name_apar)


if __name__ == '__main__':
    main()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
ShouvikJha
Occasional Contributor III

Xander Bakker‌. Thank you very much. Code working perfectly. 

Regarding indentation issue with my code ,  i had chosen  Python syntax but i don't know why still  indentation issue exist.

I  am sorry for the inconvenience caused. Hope next time that issue will not be persist.  

I will follow the step suggested by you. Once again thank you.       

0 Kudos
XanderBakker
Esri Esteemed Contributor

You're welcome. Yeah, it would be great if the code would be posted with valid indentation, because in this case, it just wasn't clear if the indentation was the cause of the issue. 

0 Kudos