(im_ms, im_pan, cloud_mask)
return im_ms_ps
Pansharpens a multispectral image, using the panchromatic band and a cloud mask.
    A PCA is applied to the image, then the 1st PC is replaced, after histogram 
    matching with the panchromatic band. Note that it is essential to match the
    histrograms of the 1st PC and the panchromatic band before replacing and 
    inverting the PCA.

    KV WRL 2018

    Arguments:
    -----------
    im_ms: np.array
        Multispectral image to pansharpen (3D)
    im_pan: np.array
        Panchromatic band (2D)
    cloud_mask: np.array
        2D cloud mask with True where cloud pixels are

    Returns:
    -----------
    im_ms_ps: np.ndarray
        Pansharpened multispectral image (3D)
Pansharpens a multispectral image, using the panchromatic band and a cloud mask.
    A PCA is applied to the image, then the 1st PC is replaced, after histogram 
    matching with the panchromatic band. Note that it is essential to match the
    histrograms of the 1st PC and the panchromatic band before replacing and 
    inverting the PCA.
Pansharpens a multispectral image using the panchromatic band and a cloud mask . A PCA is applied to the image then the 1st PC is replaced after histogram matching with the panchromatic band . Note that it is essential to match the histrograms of the 1st PC and the panchromatic band before replacing and inverting the PCA .
def pansharpen(im_ms, im_pan, cloud_mask):
    """
    Pansharpens a multispectral image, using the panchromatic band and a cloud mask.
    A PCA is applied to the image, then the 1st PC is replaced, after histogram 
    matching with the panchromatic band. Note that it is essential to match the
    histrograms of the 1st PC and the panchromatic band before replacing and 
    inverting the PCA.

    KV WRL 2018

    Arguments:
    -----------
    im_ms: np.array
        Multispectral image to pansharpen (3D)
    im_pan: np.array
        Panchromatic band (2D)
    cloud_mask: np.array
        2D cloud mask with True where cloud pixels are

    Returns:
    -----------
    im_ms_ps: np.ndarray
        Pansharpened multispectral image (3D)
        
    """

    # reshape image into vector and apply cloud mask
    vec = im_ms.reshape(im_ms.shape[0] * im_ms.shape[1], im_ms.shape[2])
    vec_mask = cloud_mask.reshape(im_ms.shape[0] * im_ms.shape[1])
    vec = vec[~vec_mask, :]
    # apply PCA to multispectral bands
    pca = decomposition.PCA()
    vec_pcs = pca.fit_transform(vec)

    # replace 1st PC with pan band (after matching histograms)
    vec_pan = im_pan.reshape(im_pan.shape[0] * im_pan.shape[1])
    vec_pan = vec_pan[~vec_mask]
    vec_pcs[:,0] = hist_match(vec_pan, vec_pcs[:,0])
    vec_ms_ps = pca.inverse_transform(vec_pcs)

    # reshape vector into image
    vec_ms_ps_full = np.ones((len(vec_mask), im_ms.shape[2])) * np.nan
    vec_ms_ps_full[~vec_mask,:] = vec_ms_ps
    im_ms_ps = vec_ms_ps_full.reshape(im_ms.shape[0], im_ms.shape[1], im_ms.shape[2])

    return im_ms_ps
def pansharpen ( im_ms , im_pan , cloud_mask ) : # reshape image into vector and apply cloud mask vec = im_ms . reshape ( im_ms . shape [ 0 ] * im_ms . shape [ 1 ] , im_ms . shape [ 2 ] ) vec_mask = cloud_mask . reshape ( im_ms . shape [ 0 ] * im_ms . shape [ 1 ] ) vec = vec [ ~ vec_mask , : ] # apply PCA to multispectral bands pca = decomposition . PCA ( ) vec_pcs = pca . fit_transform ( vec ) # replace 1st PC with pan band (after matching histograms) vec_pan = im_pan . reshape ( im_pan . shape [ 0 ] * im_pan . shape [ 1 ] ) vec_pan = vec_pan [ ~ vec_mask ] vec_pcs [ : , 0 ] = hist_match ( vec_pan , vec_pcs [ : , 0 ] ) vec_ms_ps = pca . inverse_transform ( vec_pcs ) # reshape vector into image vec_ms_ps_full = np . ones ( ( len ( vec_mask ) , im_ms . shape [ 2 ] ) ) * np . nan vec_ms_ps_full [ ~ vec_mask , : ] = vec_ms_ps im_ms_ps = vec_ms_ps_full . reshape ( im_ms . shape [ 0 ] , im_ms . shape [ 1 ] , im_ms . shape [ 2 ] ) return im_ms_ps