Pansharpening changes background value from 0 to 1, but only sometimes?

574
0
03-10-2020 08:09 AM
Labels (2)
EmmaTompkins
New Contributor

I have a python script that takes a folder full of multispectral worldview tifs and pansharpens them with their matching panchromatic band.

Both these datasets have a background value of 0 but after I pansharpen it certain dates will have a background value of 1, which I do not want. I need the value to stay zero for other processes later on.

Basically I am using the Gram-Schmidt method to pansharpen R, G B, NIR2 and then pansharpen the other four bands (RedEdge, Yellow, Coastal blue, NIR1) and then creating a composite to combine the two  pansharpened outputs back into one raster. I do not want to use a mosaic dataset as these outputs are used later outside of ArcGIS. 

Here is the code I use:

def pansharpening(multispec_list, pan_list):

    # Pan sharpen each image individually
    if len(multispec_list) != len(pan_list):
        sys.exit("UNEVEN NUMBER OF PAN AND MULTI BANDS")

    pansharp_list = []
    for i in multispec_list:
        # find the unique ID in the multispectral tif and match it to the pan band

        multi = i

        pan = multi.replace("M2AS", "P2AS")

        if pan not in pan_list:
            sys.exit("Missing matching pan bands to " + i)

        # if the ID does not exist in the pan list the variable will be empty after that loop

        pansharp_rgbn2 = os.path.splitext(multi)[0] + "_pansharpen_rgbn2.tif"
        pansharp_recbyn1 = os.path.splitext(multi)[0] + "_pansharpen_recbyn1.tif"
        comp_out = os.path.splitext(multi)[0] + "_pansharpen_allbands_0.tif"
        comp_out_nodata = os.path.splitext(multi)[0] + "_pansharpen_allbands.tif"

        # pansharpen R, G, B, NIR2 and CoastalB, RE, Y, NIR1 bands

        print("pansharpening " + multi + " with " + pan + "and WorldView-3 inputs" )
        arcpy.CreatePansharpenedRasterDataset_management(multi, 5, 3, 2, 8, pansharp_rgbn2, pan, "Gram-Schmidt", "", "", "", "", "WorldView-3")
        print("pansharpened R,G,B,NIR2")
        arcpy.CreatePansharpenedRasterDataset_management(multi, 6, 4, 1, 7, pansharp_recbyn1, pan, "Gram-Schmidt", "", "", "", "", "WorldView-3")
        print("pansharpened CB,Y,RE,NIR1")

        # create a composite with all 8 bands in the correct WV3 order
        coastal = pansharp_recbyn1 + "\Band_3"
        blue = pansharp_rgbn2 + "\Band_3"
        green = pansharp_rgbn2 + "\Band_2"
        yellow = pansharp_recbyn1 + "\Band_2"
        red = pansharp_rgbn2 + "\Band_1"
        rededge = pansharp_recbyn1 + "\Band_1"
        nir1 = pansharp_recbyn1 + "\Band_4"
        nir2 = pansharp_rgbn2 + "\Band_4"

        print("creating composite")
        arcpy.CompositeBands_management ([coastal,blue,green,yellow,red,rededge,nir1,nir2], comp_out)

        # set nodata to 0
        print("setting nodata to 0")
        arcpy.CopyRaster_management(comp_out, comp_out_nodata, "", "", "0", "NONE", "NONE", "16_BIT_UNSIGNED", "NONE", "NONE")
        pansharp_list.append(comp_out_nodata)

        arcpy.Delete_management(pansharp_rgbn2)
        arcpy.Delete_management(pansharp_recbyn1)
        arcpy.Delete_management(comp_out)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I can take the raster and reclassify the 0's to 1's later, but I want to avoid this from happening in the first place. As I'm not sure how to check in python if only the background value is 1s or 0s besides bringing it into ArcGIS Pro and visually looking at the background. (checking the whole raster for 1's in python may not work as I would expect there to be a value of 1 somewhere in the tif).

Any idea why this is happening?

0 Kudos
0 Replies