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 2000\Reflectance'
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)
7

You could try something like this:

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

env.workspace = r'F:\Mosaic 2000'
ws = env.workspace
wsf= r'F:Mosaic 2000\Reflectance'
rasters = glob.glob(os.path.join(ws,'*.tif'))
bands = [['band1', 0.0508], ['band2',0.0254],['band3', 0.0363],['band4', 0.0423]]

def transform(raster):
    for band in bands:
        num = band[0][4:]
        try:
            thisBand = os.path.basename(raster) + '\\band_' + num
            Refl = Int(Int(Int((Raster(thisBand))- 1) /  band[1]))
            outname = os.path.basename(raster)[15:23] + '_B' + num + '.tif'
            Refl.save(os.path.join(wsf,outname))
            print outname
        except Exception as e:
            arcpy.AddError(e.message)


for raster in rasters: 
    transform(raster)

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.