AnsweredAssumed Answered

Take Minimum value of multiple raster using ArcPy

Question asked by india123 on Jul 12, 2018
Latest reply on Jul 17, 2018 by india123

Hi all, 

 

I have monthly raster for different parameters in three different folders. I am trying to take the minimum value of raster from each raster in two different parameters and solve the below equation. For that, I have written a code, but the code is producing an error. 

It is giving an error in line no 35. 

Sample data attached below 

 

Code: 

def main():
    import arcpy
    import os
    arcpy.env.overwriteOutput = True
    # Checkout extension
    arcpy.CheckOutExtension("Spatial")

    ws_in_APAR = r'E:\APAR'

    ws_in_TSCALAR = r'E:\T_SCALAR'

    ws_in_WSCALAR = r'E:\W_SCALAR'

    ws_out_GPP = r'E:\GPP_1982'


    # list "mean" rasters (r001_APAR so on)
    arcpy.env.workspace = ws_in_APAR
    lst_ras_APAR = arcpy.ListRasters()
    print "lst_ras_APAR", lst_ras_APAR

    # list "mean" rasters (r001_TSCALAR so on)
    arcpy.env.workspace = ws_in_TSCALAR
    lst_ras_TSCALAR = arcpy.ListRasters()
    print "lst_ras_TSCALAR", lst_ras_TSCALAR

    # list "mean" rasters (r001_WSCALAR so on)
    arcpy.env.workspace = ws_in_WSCALAR
    lst_ras_WSCALAR = arcpy.ListRasters()
    print "lst_ras_WSCALAR", lst_ras_WSCALAR




    for ras_name in lst_ras_APAR, lst_ras_TSCALAR, lst_ras_WSCALAR   :
        ras_APAR = arcpy.Raster(os.path.join(ws_in_APAR, ras_APAR))
        ras_TSCALAR = arcpy.Raster(os.path.join(ws_in_TSCALAR, ras_TSCALAR))
        ras_WSCALAR = arcpy.Raster(os.path.join(ws_in_WSCALAR, ras_WSCALAR))



        # calculate (ras_TSCALAR.minimum wil take the minimum value of perticular raster)
        ras_GPP = (ras_APAR * 1.48) * (ras_TSCALAR.minimum * ras_WSCALAR.minimum)

        # save raster
        ras_num = ras_name[:3]
        out_name_GPP = os.path.join(ws_out_GPP, 'r{0}_GPP'.format(ras_num))
        ras_GPP.save(out_name_GPP)



if __name__ == '__main__':
    main()

 

Error message:

Traceback (most recent call last):
  File "E:\GPP.py", line 64, in <module>
    main()
  File "E:\GPP.py", line 47, in main
    ras_APAR = arcpy.Raster(os.path.join(ws_in_APAR, ras_APAR))
UnboundLocalError: local variable 'ras_APAR' referenced before assignment

Attachments

Outcomes