Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Using Python function in ArcPy script?

$
0
0

I have an script to transform DN into Reflectance of each band of several images.

The unique variable of the equation, apart of the Band itself, is the Divisor:

  • (B1 – 1) / 508
  • (B2 – 1) / 254
  • (B3 – 1) / 363
  • (B4 – 1) / 423

I wrote an script, copying the same code four times.

I know it can be improv it, using a function instead of repeating it, but I do not figure out how to do it.

Any idea?

import os, arcpy, glob
from arcpy import  env, sa
check = arcpy.CheckOutExtension("Spatial")
print check

arcpy.env.workspace = r'F:Mosaic 2000'
ws = env.workspace
wsf= r'F:Mosaic 2000Reflectance'
rasters = glob.glob(os.path.join(ws,'*.tif'))
for raster in rasters:
try:
    '''bands1 = os.path.basename(raster) + 'Band_1'
    Refl1 = arcpy.sa.Int(arcpy.sa.Int(arcpy.sa.Int((arcpy.sa.Raster(bands1))- 1) /  0.0508))
    outname = os.path.basename(raster)[15:23]+'_B1.tif'
    Refl1.save(os.path.join(wsf,outname))
    print outname
except Exception as e:
    arcpy.AddError(e.message)

try:
    bands2 = os.path.basename(raster) + 'Band_2'
    Refl2 = arcpy.sa.Int(arcpy.sa.Int(arcpy.sa.Int((arcpy.sa.Raster(bands2))- 1) / 0.0254))
    outname = os.path.basename(raster)[15:23]+'_B2.tif'
    Refl2.save(os.path.join(wsf,outname))
    print outname
except Exception as e:
    arcpy.AddError(e.message)'''

try:
    bands3 = os.path.basename(raster) + 'Band_3'
    Refl3 = arcpy.sa.Int(arcpy.sa.Int(arcpy.sa.Int((arcpy.sa.Raster(bands3))- 1) / 0.0363))
    outname = os.path.basename(raster)[15:23]+'_B3.tif'
    Refl3.save(os.path.join(wsf,outname))
    print outname
except Exception as e:
    arcpy.AddError(e.message)

try:
    bands4 = os.path.basename(raster) + 'Band_4'
    Refl4 = arcpy.sa.Int(arcpy.sa.Int(arcpy.sa.Int((arcpy.sa.Raster(bands4))- 1) / 0.0423))
    outname = os.path.basename(raster)[15:23]+'_B4.tif'
    Refl4.save(os.path.join(wsf,outname))
    print outname
except Exception as e:
    arcpy.AddError(e.message)

Viewing all articles
Browse latest Browse all 767

Trending Articles