-1

I am trying to multiply different raster from different subfolder. and during calculation of raster trying to give a msak of raster. i have written a code but its giving error. here is my code along with error mrssage

Error Message

lst_ras_APAR [u'r001_APAR.TIF', u'r002_APAR.TIF', u'r003_APAR.TIF', u'r004_APAR.TIF', u'r005_APAR.TIF', u'r006_APAR.TIF', u'r007_APAR.TIF', u'r008_APAR.TIF', u'r009_APAR.TIF', u'r010_APAR.TIF', u'r011_APAR.TIF', u'r012_APAR.TIF']
lst_ras_TScalar [u'r001_TSCALAR.TIF', u'r002_TSCALAR.TIF', u'r003_TSCALAR.TIF', u'r004_TSCALAR.TIF', u'r005_TSCALAR.TIF', u'r006_TSCALAR.TIF', u'r007_TSCALAR.TIF', u'r008_TSCALAR.TIF', u'r009_TSCALAR.TIF', u'r010_TSCALAR.TIF', u'r011_TSCALAR.TIF', u'r012_TSCALAR.TIF', u'T_SCALAR_001.tif', u'T_SCALAR_002.tif', u'T_SCALAR_003.tif', u'T_SCALAR_004.tif', u'T_SCALAR_005.tif', u'T_SCALAR_006.tif', u'T_SCALAR_007.tif', u'T_SCALAR_008.tif', u'T_SCALAR_009.tif', u'T_SCALAR_010.tif', u'T_SCALAR_011.tif', u'T_SCALAR_012.tif']
lst_ras_WScalar [u'r001_WSCALAR.TIF', u'r002_WSCALAR.TIF', u'r003_WSCALAR.TIF', u'r004_WSCALAR.TIF', u'r005_WSCALAR.TIF', u'r006_WSCALAR.TIF', u'r007_WSCALAR.TIF', u'r008_WSCALAR.TIF', u'r009_WSCALAR.TIF', u'r010_WSCALAR.TIF', u'r011_WSCALAR.TIF', u'r012_WSCALAR.TIF']
Traceback (most recent call last):
 File "<module2>", line 59, in <module>
 File "<module2>", line 32, in main
UnboundLocalError: local variable 'ras_apar' referenced before assignment

**

def main():
 import arcpy
 import os
 arcpy.env.overwriteOutput = True
 # Checkout extension
 arcpy.CheckOutExtension("Spatial")
 arcpy.env.overwriteOutput = True
# Workspace Folder
 ws_in_apar = r'D:\MODIS-NDVI-2012\APAR'
 ws_in_tscalar = r'D:\MODIS-NDVI-2012\TScalar'
 ws_in_wscalar = r'D:\MODIS-NDVI-2012\WScalar'
 ws_out_NPP = r'D:\MODIS-NDVI-2012\NPP'

# list "apar" rasters (e.g r001_APAR, r002_APAR so on  )
 arcpy.env.workspace = ws_in_apar
 lst_ras_APAR = arcpy.ListRasters()
 print "lst_ras_APAR", lst_ras_APAR

# list "tscalar" rasters (e.g r001_TSCALAR, r002_TSCALAR so on )
 arcpy.env.workspace = ws_in_tscalar
 lst_ras_TScalar = arcpy.ListRasters()
 print "lst_ras_TScalar", lst_ras_TScalar

 # list "wscalar" rasters (e.g r001_WSCALAR, r002_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:
 ras_APAR = arcpy.Raster(os.path.join(ws_in_apar, ras_APAR))
 print "ras_apar", ras_apar

for ras_TSCALAR in lst_ras_TSCALAR:
 ras_TSCALAR = arcpy.Raster(os.path.join(ws_in_tscalar, ras_TSCALAR))
 print "ras_TSCALAR", ras_TSCALAR

 for ras_WSCALAR in lst_ras_WSCALAR:
 ras_WSCALAR = arcpy.Raster(os.path.join(ws_in_wscalar, ras_WSCALAR))
 print "ras_WSCALAR", ras_WSCALAR

# calculate 
 ras_NPP = (ras_APAR * ras_TSCALAR * ras_WSCALAR * 0.98)

#Process rectricted within mask file : Process will occur only on location that fall within the mask
 #else it will be assigned to nodata inthe output
 mask="D:\MOD-REF\NDVI\CROP-L-OCT-15.img"
 NPP_Mask = ExtractByMask(ras_NPP, mask)

 # save raster
 ras_num = ras_name[:3]
 out_name_NPP = os.path.join(ws_out_NPP, 'r{0}_NPP.TIF'.format(ras_num))
 NPP_Mask.save(out_name_NPP)

if __name__ == '__main__':
 main()

Updated Code below , Error still existing.

def main():

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

        # avoid using - in folders in combination with raster calculations!
        ws_in_apar = r'D:\MODIS-NDVI-2012\APAR'
        ws_in_tscalar = r'D:\MODIS-NDVI-2012\TScalar'
        ws_in_wscalar = r'D:\MODIS-NDVI-2012\WScalar'
        ws_out_NPP = r'D:\MODIS-NDVI-2012\NPP'

        # list "apar" rasters (e.g r001_APAR)
        arcpy.env.workspace = ws_in_apar
        lst_ras_APAR = arcpy.ListRasters()
        print "lst_ras_APAR", lst_ras_APAR

        # list "tscalar" rasters (e.g r012_TSCALAR)
        arcpy.env.workspace = ws_in_tscalar
        lst_ras_TScalar = arcpy.ListRasters()
        print "lst_ras_TScalar", lst_ras_TScalar


        # list "wscalar" rasters (e.g r001_WSCALAR)
        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:
            ras_APAR = arcpy.Raster(os.path.join(ws_in_apar, ras_APAR))
            print "ras_APAR", ras_APAR

        for ras_TSCALAR in lst_ras_TSCALAR:
            ras_TSCALAR = arcpy.Raster(os.path.join(ws_in_tscalar, ras_TSCALAR))
            print "ras_TSCALAR", ras_TSCALAR


        for ras_WSCALAR in lst_ras_WSCALAR:
            ras_WSCALAR = arcpy.Raster(os.path.join(ws_in_wscalar, ras_WSCALAR))
            print "ras_WSCALAR", ras_WSCALAR

            # calculate 
            ras_NPP = (ras_APAR * ras_TSCALAR * ras_WSCALAR * 0.98)

       #Process rectricted within mask file : Process will occur only on location that fall within the mask
       #else it will be assigned to nodata inthe output
            mask="D:\MOD-REF\NDVI\CROP-L-OCT-15.img"
            NPP_Mask = ExtractByMask(ras_NPP, mask)


            # save raster
            ras_num = ras_name[:3]
            out_name_NPP = os.path.join(ws_out_NPP, 'r{0}_NPP.TIF'.format(ras_num))
            NPP_Mask.save(out_name_NPP)

    if __name__ == '__main__':
        main()
1

Python is case-sensitive for everything but paths. ras_APAR is not the same as ras_apar. Change ras_apar (this variable hasn't been instantiated) to its upper-case version and it will work.

  • I have changed it it to ras_apar, but still same error – Shouvik Jha Aug 26 '16 at 12:45
  • i replaced the line given you you. But still facing same error – Shouvik Jha Aug 26 '16 at 12:51
  • You should replace ras_apar with ras_APAR, not the opposite.. Could you please update your code? – GISGe Aug 26 '16 at 12:54
  • I have updated. Please look at the code. – Shouvik Jha Aug 26 '16 at 12:56
  • 1
    Sorry but I'm lost. I don't even know which line raises the error. I can fix obvious code errors but this site is not meant to provide in-depth code-reviewing. I suggest you to add print statements here and there to check your variables' value an help you locate the error. – GISGe Aug 26 '16 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.