Example #1
0
def main():
    parser = argparse.ArgumentParser(description="Upgrade AVIRIS-C radiances")
    parser.add_argument('infile', type=str, metavar='INPUT')
    parser.add_argument('outfile', type=str, metavar='OUTPUT')
    parser.add_argument('--scaling', '-s', action='store')
    parser.add_argument('--verbose', '-v', action='store_true')
    args = parser.parse_args()

    hdrfile = find_header(args.infile)
    hdr = envi.read_envi_header(hdrfile)
    hdr['data type'] = '4'
    hdr['byte order'] = '0'
    if hdr['interleave'] != 'bip':
        raise ValueError('I expected BIP interleave')
    hdr['interleave'] = 'bil'
    hdr['data ignore value'] = '-9999'
    envi.write_envi_header(args.outfile + '.hdr', hdr)
    lines = int(hdr['lines'])
    samples = int(hdr['samples'])
    bands = int(hdr['bands'])
    frame = samples * bands
    if args.verbose:
        print('Lines: %i  Samples: %i  Bands: %i\n' % (lines, samples, bands))

    if args.scaling is None:
        scaling = np.ones(bands, dtype=s.float32)
    else:
        scaling = s.loadtxt(args.scaling)

    prefix = os.path.split(args.infile)[-1][:3]
    if prefix in [
            'f95', 'f96', 'f97', 'f98', 'f99', 'f00', 'f01', 'f02', 'f03',
            'f04', 'f05'
    ]:
        gains = s.r_[50.0 * np.ones(160), 100.0 * np.ones(64)]
    elif prefix in [
            'f06', 'f07', 'f08', 'f09', 'f10', 'f11', 'f12', 'f13', 'f14',
            'f15', 'f16', 'f17', 'f18', 'f19', 'f20', 'f21'
    ]:
        gains = s.r_[300.0 * np.ones(110), 600.0 * np.ones(50),
                     1200.0 * np.ones(64)]
    else:
        raise ValueError('Unrecognized year prefix "%s"' % prefix)

    with open(args.infile, 'rb') as fin:
        with open(args.outfile, 'wb') as fout:
            for line in range(lines):
                X = np.fromfile(fin, dtype=s.int16, count=frame)
                X.byteswap(True)
                X = X.flatten()
                bad = X < -49
                X = np.array(X, dtype=s.float32)
                X = np.array(X.reshape((samples, bands)))
                X = scaling * X / gains
                X = X.flatten()
                X[bad] = -9999.0
                X = X.reshape((samples, bands))
                X = X.T.flatten()  # convert from BIP to BIL
                X = np.array(X, dtype=s.float32)
                X.tofile(fout)
Example #2
0
 def load_header(self, path):
     from spectral.io import envi
     basename = os.path.basename(path)
     dest = os.path.join(tempdir, basename)
     if not os.path.exists(dest):
         self.download_file(path, dest)
     return envi.read_envi_header(dest)
    def hsiNan_fill(self, imgHdrName):
        """
        This function can be used to fill in the nans based on the other data in the image.

        :param imgHdrName: location of the HDR file associated with the envi image of choice
        :return:
        """
        imgName = imgHdrName.replace('.hdr', '.img')
        header = envi.read_envi_header(imgHdrName)
        'Read in the background image'
        crImg = envi.open(imgHdrName, imgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape

        arrCrImg = crCube.reshape((rows * cols, bands))
        'Fill the NaNs in the columns'
        arrCrImg =  generalUtilities().fill_nan(arrCrImg)
        'Fill the NaNs in the rows'
        arrCrImgCrop = arrCrImg[:, 4:244]
        arrCrImgCrop = generalUtilities().fill_nan(arrCrImgCrop.T)
        arrCrImg[:, 4:244] = arrCrImgCrop.T
        'Reshape to image size'
        crCube_nr = arrCrImg.reshape((rows, cols, bands))

        'Save the background image'
        outFileName1 = imgName.replace('.img', '_CRnR.hdr')
        envi.save_image(outFileName1, crCube_nr, dtype='single',
                        force=True, interleave='bil', metadata=header)

        return outFileName1
Example #4
0
def load_S3_image(image_path):
    """
    load S3 image downloaded with SNAP, applies conversion to TOA and compute ratios

    :param image_path: path to .data/ dir
    :return: 3D np.array with the image bands sorted in BAND_NAMES_S3_RATIOS order
    """
    inputs = [os.path.join(image_path,"Oa%s_reflectance.hdr"%b) for b in BAND_NAMES_S3]

    images = []
    for ip in inputs:
        metadata = envi.read_envi_header(ip)
        gain = np.array([float(m) for m in metadata['data gain values']])
        offset = np.array([float(m) for m in metadata['data offset values']])
        img = np.float64(envi.open(ip)[:,:,:])
        img *= gain
        img += offset
        img/= np.pi
        images.append(img)

    S3ratio1 = (images[7]) / (images[3])
    S3ratio2 = (images[10]) / (images[3])
    images.append(S3ratio1)
    images.append(S3ratio2)
    images = np.concatenate(images,axis=2)

    return images
    def crism_fillNanRows(self, imgName):
        """
        This function fill the empty of NaN rows using the neighbors.

        :param imgName: Name/address of the image to be smoothed
        :return:
        """

        hdrName = imgName.replace('.img', '.hdr')
        header = envi.read_envi_header(hdrName)
        'Read in the background image'
        crImg = envi.open(hdrName, imgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape

        arrCrImg = crCube.reshape((rows * cols, bands))
        arrCrImgCrop = arrCrImg[:, self.strtBand:self.stopBand]
        'Fill the NaNs in the columns'
        arrCrImgCrop = generalUtilities().fill_nan(arrCrImgCrop)
        'Fill the NaNs in the rows'
        arrCrImgCrop = generalUtilities().fill_nan(arrCrImgCrop.T)
        arrCrImg[:, self.strtBand:self.stopBand] = arrCrImgCrop.T
        'Reshape to image size'
        crCube_nr = arrCrImg.reshape((rows, cols, bands))

        'Save the background image'
        outFileName1 = imgName.replace('_MS_CR.img', '_MS_CRnR.hdr')
        envi.save_image(outFileName1,
                        crCube_nr,
                        dtype='single',
                        force=True,
                        interleave='bil',
                        metadata=header)

        return outFileName1.replace('.hdr', '.img')
Example #6
0
def image_obj(hdr, img):
    "create a object of the image corresponding to certain header"
    head = envi.read_envi_header(hdr)
    param = envi.gen_params(head)
    param.filename = img  # spectral data file corresponding to .hdr file

    if 'bbl' in head:
        head['bbl'] = [float(b) for b in head['bbl']]
        print(len(head['bbl']))
        good_bands = np.nonzero(
            head['bbl'])[0]  # get good bands only for computation
        bad_bands = np.nonzero(np.array(head['bbl']) == 0)[0]
        print("calculating target signature...")
        t_mean = target_sign(png_img, img_data_obj, channel, good_bands)

    interleave = head['interleave']
    if (interleave == 'bip' or interleave == 'BIP'):
        print("it is a bip")
        from spectral.io.bipfile import BipFile
        img_obj = BipFile(param, head)

    if (interleave == 'bil' or interleave == 'BIL'):
        print("It is a bil file")
        from spectral.io.bilfile import BilFile
        img_obj = BilFile(param, head)

    return img_obj
    def crism_CreateModeledImage(self, abImgName, saveImg=0, targetFolder=''):
        """
        @function name      :crism_CreateModeledImage
        @description        :This function will be used to created modeled image from the constituents generated
                            using the UMass Pipeline
        ----------------------------------------------------------------------------------------------------------------
        INPUTS
        ----------------------------------------------------------------------------------------------------------------
        :param self:
        :param abImgName: Name/address of the absoprtion image generated by the UMass Pipeline
        :param saveImg: Flag to decide if the modeled image is to be saved. The image is saved if the flag is not 0.
                        (default = 0)
        ----------------------------------------------------------------------------------------------------------------
        OUTPUTS
        ----------------------------------------------------------------------------------------------------------------
        :return: msImg: The image modeled by the UMass pipeline
        """

        'The absorption image header is'
        abHdrName = abImgName.replace('.img', '.hdr')
        'Read in the background image'
        abImg = envi.open(abHdrName, abImgName)
        abCube = abImg.load()
        abHeader = envi.read_envi_header(abHdrName)

        'The background image name is'
        bgImgName = abImgName.replace('_AB.img', '_Bg.img')
        bgHdrName = bgImgName.replace('.img', '.hdr')
        bgImg = envi.open(bgHdrName, bgImgName)
        bgCube = bgImg.load()

        'The modeled image is'
        msCube = abCube * bgCube

        if (saveImg != 0):
            finalLoc = abImgName.rfind('/')
            imgName = abImgName[(finalLoc + 1):]
            imgName = imgName.replace('_AB.img', '_MS.hdr')
            if targetFolder:
                if not os.path.isdir(targetFolder):
                    os.makedirs(targetFolder)
                imgName = targetFolder + imgName
            else:
                localFolder = os.getcwd() + '/out'
                if not os.path.isdir(localFolder):
                    os.makedirs(localFolder)
                imgName = localFolder + imgName

            'Save the image'
            envi.save_image(imgName,
                            msCube,
                            dtype=np.float32,
                            force=True,
                            interleave='bil',
                            metadata=abHeader)
            return imgName.replace('.hdr', '.img')
        else:
            str = 'Completed'
            return str
Example #8
0
    def add_files(self, hdr_file, dat_file):
        self.idx_dta = QSettings.ENVIITEMS
        self.envi_img = envi.open(hdr_file, dat_file)

        wavenum = envi.read_envi_header(hdr_file)['wavelength']
        self.bands = np.array([float(i) for i in wavenum])

        self.maxidx = len(self.idx_dta)
Example #9
0
    def convert(self):
        with open(self.hdr, 'r+') as hdr:
            assert (
                any('byte order' in line for line in hdr)
            ), "Byte order information missing from header file.\nDepending on what operating system the header file was CREATED on,\nyou can manually insert\n'byte order = 0' (Windows, Linux) or 'byte order = 1' (MacOS)\ninto the header file:\n{}".format(
                self.hdr)
            #bo_found = any('byte order' in line for line in hdr)
            #if not bo_found:
            #    self.add_byteorder()

        #try:
        envi_img = envi.open(self.hdr, self.dat)
        #except envi.MissingEnviHeaderParameter("byte order")

        img_data = envi_img.load()
        xspacing = np.asarray(envi.read_envi_header(self.hdr)['pixel size'],
                              dtype=float)[0]
        yspacing = np.asarray(envi.read_envi_header(self.hdr)['pixel size'],
                              dtype=float)[1]
        wavenum = np.asarray(envi.read_envi_header(self.hdr)['wavelength'],
                             dtype=float)

        rows = np.shape(img_data)[0]
        columns = np.shape(img_data)[1]

        for i in range(rows):
            for j in range(columns):
                spectrum = np.column_stack((wavenum, img_data[i,
                                                              j, :].flatten()))
                #self.print_message(self.message, 'Saving spectrum {} of {}.'.format(loading.progress['value']+1, rows*columns))
                x = i * xspacing
                y = j * yspacing
                fname = 'X{} Y{}.CSV'.format(str(np.round(x, 6)),
                                             str(np.round(y, 6)))
                np.savetxt((self.targetdir + '/' + fname),
                           spectrum,
                           delimiter=',')
Example #10
0
def main():
    # Read in header file and get list of wavelengths and fwhm
    hdr_path = sys.argv[1]
    output_path = sys.argv[2]
    hdr = envi.read_envi_header(hdr_path)
    wl = hdr["wavelength"]
    fwhm = hdr["fwhm"]

    # Need to offset fwhm if its length is not the same as the wavelengths' length.  This is a known bug in
    # the AVIRIS-NG data.
    fwhm_offset = 0 if len(wl) == len(fwhm) else 23
    wl_arr = []
    for i in range(len(wl)):
        wl_arr.append([i, wl[i], fwhm[i + fwhm_offset]])

    np.savetxt(output_path, np.array(wl_arr, dtype=np.float32))
Example #11
0
def readENVIHeader(fName):
    """
    Reads envi header

    Parameters
    ----------
    fName : String, Path to .hdr file
    
    Returns
    -------
    centers : Band-centers
	fwhm : full-width half-maxes
    """
    hdr = envi.read_envi_header(fName)
    centers = np.array(hdr['wavelength'], dtype=np.float)
    fwhm = np.array(hdr['fwhm'], dtype=np.float)
    return centers, fwhm
Example #12
0
def load_ENVI_spec_lib(file_name):
    """
    Load a ENVI .sli file.

    Parameters:
        file_name: `path string`
            The complete path to the library file to load.

    Returns: `numpy array`
        A (n x p) HSI cube.

        head: `dictionary`
            Starting at version 0.13.1, the ENVI file header

    """
    sli = envi.open(file_name)
    head = envi.read_envi_header(file_name)
    return sli.spectra, head
Example #13
0
def load_ENVI_spec_lib(file_name):
    """
    Load a ENVI .sli file.

    Parameters:
        file_name: `path string`
            The complete path to the library file to load.

    Returns: `numpy array`
        A (n x p) HSI cube.

        head: `dictionary`
            Starting at version 0.13.1, the ENVI file header

    """
    sli = envi.open(file_name)
    head = envi.read_envi_header(file_name)
    return sli.spectra, head
Example #14
0
def load_ENVI_file(file_name):
    """
    Load the data and the header from an ENVI file.
    It use the SPy (spectral) library. At 'file_name' give the envi header file name.

    Parameters:
        file_name: `path string`
            The complete path to the file to load. Use the header file name.

    Returns: `tuple`
        data: `numpy array`
            A (m x n x p) HSI cube.

        head: `dictionary`
            Starting at version 0.13.1, the ENVI file header
     """
    img = envi.open(file_name)
    head = envi.read_envi_header(file_name)
    return np.array(img.load()), head
Example #15
0
def load_ENVI_file(file_name):
    """
    Load the data and the header from an ENVI file.
    It use the SPy (spectral) library. At 'file_name' give the envi header file name.

    Parameters:
        file_name: `path string`
            The complete path to the file to load. Use the header file name.

    Returns: `tuple`
        data: `numpy array`
            A (m x n x p) HSI cube.

        head: `dictionary`
            Starting at version 0.13.1, the ENVI file header
     """
    img = envi.open(file_name)
    head = envi.read_envi_header(file_name)
    return np.array(img.load()), head
def image_obj(hdr, img):
    "create a object of the image corresponding to certain header"

    head = envi.read_envi_header(hdr)
    param = envi.gen_params(head)
    param.filename = img  # spectral data file corresponding to .hdr file

    interleave = head['interleave']
    if (interleave == 'bip' or interleave == 'BIP'):
        print("it is a bip")
        from spectral.io.bipfile import BipFile
        img_obj = BipFile(param, head)

    if (interleave == 'bil' or interleave == 'BIL'):
        print("It is a bil file")
        from spectral.io.bilfile import BilFile
        img_obj = BilFile(param, head)

    return img_obj
Example #17
0
 def load_mica_library(self, train):
     if self.mica_library is None:
         # sliName = './examples/domains/data/CRISM_data_summPar_1/UMass_redMICA_CR_enhanced.sli'
         # sliHdrName = './examples/domains/data/CRISM_data_summPar_1/UMass_redMICA_CR_enhanced.sli.hdr'
         # subset = (4,244)
         sliName = './examples/domains/data/CRISM_data_summPar_1/yukiMicaNum.sli'
         sliHdrName = './examples/domains/data/CRISM_data_summPar_1/yukiMicaNum.hdr'
         sliHdr = envi.read_envi_header(sliHdrName)
         endMem_Name = sliHdr['spectra names']
         not_hematite = np.array([(name != 'HEMATITE')
                                  for name in endMem_Name])
         self.mica_names = list(np.array(endMem_Name)[not_hematite])
         subset = (0, 240)
         micaSLI = envi.open(sliHdrName, sliName)
         mica_dataRed = micaSLI.spectra[not_hematite, :]
         mica_dataRed = self.fnScaleMICAEM(
             mica_dataRed[:, subset[0]:subset[1]]).astype('float32')
         mica_dataRed = self.zero_one_x_ind(mica_dataRed)
         # mica_dataRed -= 0.5
         self.mica_library = train.m.to_gpu(torch.from_numpy(mica_dataRed))
Example #18
0
    def fnCRISM_createCRImg(self, imgName, bgFileName):
        """
        This function can be used to create a continuum removed version of the TER images

        :param imgName: the simple fileName
        :param bgFileName: the associated continuum file
        ----------------------------------------------------------------------------------------------------------------
        OUTPUT
        ----------------------------------------------------------------------------------------------------------------
        :return: outFileName: The name of the file with the convex background
        """

        'The image name is'
        imgHdrName = imgName.replace(".IMG", ".HDR")
        imgHdrName = imgHdrName.replace(".img", ".hdr")
        'Now load the image'
        img = envi.open(imgHdrName, imgName)
        cube = img.load()
        [rows, cols, bands] = img.shape

        'The background image is'
        bgHdrName = bgFileName.replace(".img", ".hdr")
        bgHdrName = bgHdrName.replace(".IMG", ".HDR")
        'Now load the image'
        bgImg = envi.open(bgHdrName, bgFileName)
        header = envi.read_envi_header(bgHdrName)
        bgCube = bgImg.load()

        cube_cr = np.empty((rows, cols, bands), dtype=np.float32)
        cube_cr[:] = np.nan
        cube_cr[:, :, self.strtBand:self.stopBand] = \
            (cube[:, :, self.strtBand:self.stopBand]) / (bgCube[:, :, self.strtBand:self.stopBand])
        outFileName = bgHdrName.replace('_Bg.hdr', '_CR.hdr')
        envi.save_image(outFileName,
                        cube_cr,
                        dtype='single',
                        force=True,
                        interleave='bil',
                        metadata=header)

        return outFileName.replace('.hdr', '.img')
    def hsiSpatialSmoothing(self, crImgName, filtersize=5):
        """
        This function can be used to perform a spatial smoothing on an Hyperspectral image and save it.

        :param crImgName: the name of the file we want to smooth
        :param filtersize: the filter size
        :return: the name of the saved smoothed image
        """
        crHdrName = crImgName.replace('.img', '.hdr')
        header = envi.read_envi_header(crHdrName)
        'Read in the background image'
        crImg = envi.open(crHdrName, crImgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape
        t1 = crImgName.rfind('/')
        temp = crImgName[(t1+1):]
        if ((temp.find('FRT') != -1) or (temp.find('FRS') != -1)):
            strtCol = 29
            stopCol = -7
        else:
            if ((temp.find('HRL') != -1) or (temp.find('HRS') != -1)):
                strtCol = 15
                stopCol = -4

        crCube = crCube[:, strtCol:stopCol, 4:244]

        'Initialize matrix to nans'
        crCube_smoothed = np.empty((rows, cols, bands), dtype=float)
        crCube_smoothed[:] = np.nan

        for ii in range(240):
            bandImg = np.squeeze(crCube[:, :, ii])
            bandImg_smooth = ndimage.uniform_filter(bandImg, size=filtersize)
            crCube_smoothed[:, strtCol:stopCol, ii + 4] = bandImg_smooth

        outFileName = crImgName.replace('.img', ('_smoothed' + str(filtersize) + '.hdr'))
        envi.save_image(outFileName, crCube_smoothed, dtype=np.float32, force=True,
                        interleave='bil', metadata=header)

        return outFileName
    def crismImgSmooth(self, imgName, filterSize=5):
        """
        This function smooths each band of the hyperspectral image band by band

        :param imgName: Name of the image to be smoothed
        :param filterSize: The kernel size of the boxcar(uniform) filter
        :return:
        """
        imgHdrName = imgName.replace('.img', '.hdr')
        header = envi.read_envi_header(imgHdrName)

        'Read in the background image'
        crImg = envi.open(imgHdrName, imgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape

        'Initialize matrix to nans'
        crCube_smoothed = np.empty((rows, cols, bands), dtype=float)
        crCube_smoothed[:] = np.nan

        for ii in range(self.strtBand, self.stopBand, 1):
            bandImg = np.squeeze(crCube[self.strtRow:(rows + self.stopRow),
                                        self.strtCol:(cols + self.stopCol),
                                        ii])
            bandImg_smooth = ndimage.uniform_filter(bandImg, size=filterSize)
            crCube_smoothed[self.strtRow:(rows + self.stopRow),
                            self.strtCol:(cols + self.stopCol),
                            ii] = bandImg_smooth

        outFileName = imgName.replace(
            '_CRnR.img', ('_smoothed' + str(filterSize) + '_CR.hdr'))
        envi.save_image(outFileName,
                        crCube_smoothed,
                        dtype=np.float32,
                        force=True,
                        interleave='bil',
                        metadata=header)

        return outFileName.replace('.hdr', '.img')
    def hsiFlip(self, imgName):
        """
        This function can be used to flip the image upside down. This is often required in the case of CRISM images as
        the data is the pds is arranged in the reverse order

        :param imgName: The address of the image to be flipped
        ----------------------------------------------------------------------------------------------------------------
        OUTPUT
        ----------------------------------------------------------------------------------------------------------------
        :return: outFileName: The name of the file with the convex background
        """

        imgHdrName = imgName.replace(".img", ".hdr")
        'Now load the image'
        img = envi.open(imgHdrName, imgName)
        header = envi.read_envi_header(imgHdrName)
        cube = img.load()
        [_, _, bands] = img.shape

        'Get the wavelength information'
        wvl = header['wavelength']
        wvl = np.asarray(wvl, dtype=np.float32)

        'Flip the image and the wavelengths'
        cube_flip = np.flip(cube, axis=2)
        wvl = np.flip(wvl, axis=0)
        header['wavelength'] = wvl

        if header['default bands']:
            defaultBands = np.asarray(header['default bands'], dtype=np.int)
            defaultBands = bands - defaultBands
            header['default bands'] = defaultBands

        'Save the flipped data'
        outFileName = imgName.replace(".img", "_flip.hdr")
        envi.save_image(outFileName, cube_flip, dtype='single', force=True, interleave='bil', metadata=header)

        return outFileName
import gdal
from gdalconst import *
import ogr
import numpy as np
import spectral.io.envi as envi
import pdb

indir = '/Volumes/DGE/CAO/caodata/Scratch/dknapp/Belize_stuff/ortho/'
outfile = 'test_best_mosaic'
testing = infiles[0]
timg = envi.read_envi_header(indir+testing+'.hdr')
metadata = timg.copy()
## metadata['bands'] = '%i' % sum(const.use_out)
## metadata['interleave'] = 'bil'
## metadata['wavelength'] = ['%7.2f'%w for w in wl[const.use_out]]
## metadata['fwhm'] = ['%7.2f'%w for w in fwhm[const.use_out]]
## metadata['data type'] = '4'

infiles = ['CASI_2017_04_11_140827_rgb_ort', 'CASI_2017_04_11_141408_rgb_ort', 
           'CASI_2017_04_11_142021_rgb_ort', 'CASI_2017_04_11_142709_rgb_ort', 
           'CASI_2017_04_11_143903_rgb_ort', 'CASI_2017_04_11_144519_rgb_ort', 
           'CASI_2017_04_11_144847_rgb_ort', 'CASI_2017_04_11_145350_rgb_ort', 
           'CASI_2017_04_11_145830_rgb_ort', 'CASI_2017_04_11_150358_rgb_ort', 
           'CASI_2017_04_11_150953_rgb_ort', 'CASI_2017_04_11_151613_rgb_ort', 
           'CASI_2017_04_11_152207_rgb_ort', 'CASI_2017_04_11_152805_rgb_ort', 
           'CASI_2017_04_11_153422_rgb_ort', 'CASI_2017_04_11_154033_rgb_ort', 
           'CASI_2017_04_11_154653_rgb_ort', 'CASI_2017_04_11_155337_rgb_ort', 
           'CASI_2017_04_11_155934_rgb_ort', 'CASI_2017_04_11_160605_rgb_ort', 
           'CASI_2017_04_11_161251_rgb_ort', 'CASI_2017_04_11_162024_rgb_ort', 
           'CASI_2017_04_11_162644_rgb_ort', 'CASI_2017_04_11_163318_rgb_ort', 
           'CASI_2017_04_11_163937_rgb_ort', 'CASI_2017_04_11_164602_rgb_ort', 
import spectral.io.envi as envi
from scipy import interpolate
from spectral.io.envi import SpectralLibrary as sli
import os
import matplotlib.pyplot as plt

from generalUtilities import generalUtilities

'----------------------------------------------------------------------------------------------------------------------'
baseFolder = '/Volume2/arunFiles/python_HSITools/relabProcessing/Mixtures/Spectra/'
'Extracting CR MICA Spectrum'
sliName = '/Volume1/data/CRISM/arun/CR_MICA_Spectrum_full_v6.sli'
sliHdrName = '/Volume1/data/CRISM/arun/CR_MICA_Spectrum_full_v6.sli.hdr'

'Read in the header'
sliHdr = envi.read_envi_header(sliHdrName)
wavelength = np.asarray(sliHdr['wavelength'], dtype='single')
wavelength = wavelength[4:244]
wavelength[239] = 2.6
'Create empty variables to hold the data'
relabSpectra = []
relabSpectra_CR = []
relabSpectra_Bg = []
sample_name = []

for r, d, f in os.walk(baseFolder):
    for file in f:
        'Image files will have the end ".txt"'
        if file.find('.txt') != -1:
            print(file)
            fileName = os.path.join(r, file)
Example #24
0
 def __init__(self, fname):
     self.f_handle = envi.open(fname)
     self.spectra = self.f_handle.spectra
     self.head = envi.read_envi_header(fname)
     self.wvl = [float(x) for x in self.head['wavelength']]
     self.spectra_names = self.head['spectra names']
Example #25
0
    def fnCRISM_contRem(self, imgName, filter=False, filterSize=11):
        """
        This function can be used to create continuum images for images from the WUSTL pipeline

        ----------------------------------------------------------------------------------------------------------------
        INPUT
        ----------------------------------------------------------------------------------------------------------------
        :param imgName: The location of the image
        :param filter: Choose whether to use some spectral smoothing (default=false)
        :param filterSize: Filter size if needed (default=11)
        :param strtBand: Where does the user wish to start the continuum (default=0)
        :param numBands: The number of bands to be included (default=240)

        ----------------------------------------------------------------------------------------------------------------
        OUTPUT
        ----------------------------------------------------------------------------------------------------------------

        :return: : outFileName: The name of the file with the convex background
        """

        'The image name is'
        imgHdrName = imgName.replace(".img", ".hdr")
        imgHdrName = imgHdrName.replace(".IMG", ".HDR")
        'Now load the image'
        img = envi.open(imgHdrName, imgName)
        header = envi.read_envi_header(imgHdrName)
        cube = img.load()
        [rows, cols, bands] = img.shape

        'Get the wavelength information'
        wvl = header['wavelength']
        wvl = wvl[self.strtBand:self.stopBand]
        wvl = np.asarray(wvl, dtype='single')

        'Create a matrix to hold the background'
        cube_bg = np.empty((rows, cols, bands), dtype=np.float32)
        cube_bg[:] = np.nan

        'For each pixel find the continuum'
        for ii in tqdm(range(rows)):
            for jj in range(cols):
                'The spectrum is'
                spectrum = np.squeeze(cube[ii, jj,
                                           self.strtBand:self.stopBand])
                if filter:
                    spectrum = filter1d(spectrum, filterSize)

                'Check if it has nans'
                flag = np.isnan(spectrum).any()

                'if not nan find the continuum'
                if not flag:
                    points = np.asarray(np.vstack((wvl, spectrum)))
                    points = points.T

                    'Find the continuum'
                    ycnt = np.asarray(generalUtilities().convex_hull(points))

                    'Place this continnum in the folder'
                    cube_bg[ii, jj, self.strtBand:self.stopBand] = ycnt

        'Save the background image'
        if filter:
            outFileName = imgName.replace(
                '.img', ('_spFilt' + str(filterSize) + '_Bg.hdr'))
            outFileName = outFileName.replace(
                '.IMG', ('_spFilt' + str(filterSize) + '_Bg.HDR'))
        else:
            outFileName = imgName.replace('.img', '_Bg.hdr')
            outFileName = outFileName.replace('.IMG', '_Bg.HDR')

        envi.save_image(outFileName,
                        cube_bg,
                        dtype='single',
                        force=True,
                        interleave='bil',
                        metadata=header)

        return outFileName.replace('.hdr', '.img')
Example #26
0
 def load_header(self, path):
     basename = os.path.basename(path)
     dest = os.path.join(tempdir, basename)
     if not os.path.exists(dest):
         self.download(path, dest)
     return envi.read_envi_header(dest)
Example #27
0
'And Yukis corrected image'
yukiImgName = imgFolder + 'HRL0000B8C2_07_IF183L_TRR3_atcr_sabcondv4_1_Lib11123_1_4_5_l1_gadmm_a_v2_ca_ice_b_nr.img'
yukiHdrName = yukiImgName.replace('.img', '.hdr')

'Read the original image'
oriImg = envi.open(oriHdrName, oriImgName)
oriCube = oriImg.load()
oriCube = np.asarray(oriCube[:, :, 4:244])

'Read Yukis corrected image'
yukiImg = envi.open(yukiHdrName, yukiImgName)
yukiCube = yukiImg.load()
yukiCube = np.asarray(yukiCube[:, :, 4:244])

'Get the wavelengths'
header = envi.read_envi_header(yukiHdrName)
wavelength = np.asarray(header['wavelength'], dtype=np.float32)
wavelength = wavelength[4:244]

'Extract the ROI'
row = 408
col = 310
doubletOri = oriCube[(row - 2):(row + 2), (col - 2):(col + 2), :]
doubletYuki = yukiCube[(row - 2):(row + 2), (col - 2):(col + 2), :]

'Initialize the average spectra'
avgSpectraOri = np.zeros((240, ))
avgSpectraYuki = np.zeros((240, ))

avgSpectraYuki_ip = np.mean(np.squeeze(np.mean(doubletYuki, 0)), 0)
Example #28
0
    def create_Maps4CRISMImages_Cosine(self, model, mica_data, imgName, endMem_Name='', scaleFlag=True,
                                           scaleLevel=0.2):
        """
        @function name      :create_Maps4CRISMImages_Cosine
        @description        :This function accepts the discriminator model, a search library-a set of spectra to find
        similarities for and the address of a CRISM image and then creates maps that illustrates the simlarities
        between the spectra in the search library and the spectra in the CRISM Image. The function assumes that both the
        search library and image have the continnum removed using the same strategy as the spectra used to train the
        model.
        ----------------------------------------------------------------------------------------------------------------
        INPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) model        : The model which recreates the representaion of the
                          GAN-discriminator
        2) mica_data    : The library which contains the string of spectra we wish to
                          find [continuum removed]. This object is expected to be
                          of size [nSamples X nBands X 1]
        3) imgName      : The address of the CRISM Image. [continuum removed]
        4) endMem_name  : Names associated with each entry in the search library
                          (has to have length = nSamples)
        5) filetype     : The file type can be 'FRT' or 'HRL'. Default value is
                          'FRT' as this has a larger frame
        ----------------------------------------------------------------------------------------------------------------
        OUTPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) mapImag      : a 3D object which the same number of rows or columns as the CRISM imagel
                          and bands equal to the number of spectra in the search library
        """

        'Get the predictions at output layer for the MICA DATA'
        mica_dataPreds_l2 = np.asarray(model.predict(mica_data))

        'Read the image and header'
        hdrName = imgName.replace('.img', '.hdr')
        img = envi.open(hdrName, imgName)
        str1 = os.getcwd()
        header = envi.read_envi_header(str1 + ('/modelHeader.hdr'))
        cubeOrig = img.load()
        [rowOrig, colOrig, _] = cubeOrig.shape

        if (self.stopCol == 0):
            self.stopCol = colOrig

        if (self.stopRow == 0):
            self.stopRow = rowOrig


        cube = cubeOrig[self.strtRow:self.stopRow, self.strtCol:self.stopCol, self.strtBand:self.stopBand]
        cube[cube >=1] = 1

        [rows, cols, bands] = cube.shape
        'Convert to array and fill nan'
        arrImg = cube.reshape((rows * cols, bands))
        arrImg = generalUtilities().fill_nan(arrImg)

        'Scale the image spectra to a specific band depth if needed'
        if scaleFlag:
            arrImg = hsiUtilities().scaleSpectra(arrImg, scaleMin=scaleLevel)

        'Get model Prediction for this image'
        arrImg = arrImg.reshape((arrImg.shape[0], arrImg.shape[1], 1))
        imgPreds = np.asarray(model.predict(arrImg))
        'Find the cosine distance between the exemplars and the data'
        dist = np.squeeze(cosineDist(mica_dataPreds_l2, imgPreds))

        'Initialize output variable'
        simMap = np.zeros((rowOrig, colOrig, mica_data.shape[0]))
        'Reshape and form the maps for each endmember'
        for em in range(mica_dataPreds_l2.shape[0]):
            distMap = np.squeeze(dist[em, :])
            distMap = distMap.reshape((rows, cols))
            simMap[self.strtRow:self.stopRow, self.strtCol:self.stopCol, em] = distMap

        'Save this map in the same folder as the input image'
        'Change the header to hold band-names of users choice'

        if (len(endMem_Name) == mica_data.shape[0]):
            header_final = header
            try:
                header_final['band names'] = endMem_Name
                header_final['samples'] = cols
                header_final['lines'] = rows
                #header_final['offset'] = header['offset']
                #header_final['file type'] = header['file type']
                #header_final['data type'] = header['data type']
                #header_final['byte order'] = header['byte order']
                header_final['bands'] = len(endMem_Name)

            except:
                pass
        else:
            header_final = header
            try:
                header_final['wavelength'] = ''
                header_final['fwhm'] = ''
                header_final['bbl'] = ''
                header_final['bands'] = mica_data.shape[0]
            except:
                pass

        'Save the MAPS'
        mapName = imgName.replace('.img', (self.op_postFix + '.hdr'))
        envi.save_image(mapName, simMap, dtype=np.float32, force=True,
                        interleave='bil', metadata=header_final)

        return mapName.replace('.hdr', '.img')
Example #29
0
    os.makedirs(out_dir)

filenames = [
    't1.11302.1935.1000m.hdr', 't1.11302.1935.geo.hdr',
    't1.11302.1935.mod35.hdr', 't1.11302.1935.mod35qa.hdr',
    't1.11302.1935.met.hdr'
]

metadatas = {}

for filename in filenames:
    in_file = os.path.join(in_dir, filename)
    out_file = os.path.join(out_dir, filename)
    print('hdr file: {}'.format(in_file))

    metadata = envi.read_envi_header(in_file)
    interleave = metadata.pop('interleave')
    metadata.pop('description')
    file_type = filename.split('.')[-2]
    metadatas[file_type] = {'metadata': metadata, 'interleave': interleave}

    data = envi.open(in_file).asarray()
    import numpy as np
    import matplotlib.pyplot as plt
    data = np.array(data)
    print(data.shape)
    for i in range(36):
        _data = data[:, :, i]
        print(type(_data))
        _data[_data == -1] = np.nan
        print('i = {} min = {} max = {}'.format(i, np.nanmin(_data),
Example #30
0
    def create_Maps4CRISMImages_BestGuess(self, imgName, threshLevel=0.707, highThreshBands=''):
        """
        @function name      :create_Maps4CRISMImages_Best Guess
        @description        :This function accepts similiarity map as input and thresholds the different mineral
        similarity maps  and thresholds out the values below a specific similarity
        ----------------------------------------------------------------------------------------------------------------
        INPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) imgName:              Address of the similarity maps for a specific set of minerals of interest.
        2) threshLevel:          The threshold below which the similarites are inconclusive.
        3) highThreshBands:      Minerals for which the simlairty must be much higher (due to rareness of mineral or
        extreme similarity to other endmembers).
        ----------------------------------------------------------------------------------------------------------------
        OUTPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) mapName:              Address of the Hyperspectral image which contains the best guess image.
        """
        'Open the similarity map of interest'
        hdrName = imgName.replace('.img', '.hdr')
        img = envi.open(hdrName, imgName)
        header = envi.read_envi_header(hdrName)
        cube = img.load()
        [rows, cols, bands] = cube.shape

        'Apply higher thresholds for bands where these higher thresholds are important'
        if highThreshBands:
            highThreshLevel = 1 - (0.5*(1 - threshLevel))
            temp = (cube[:, :, highThreshBands])
            temp[temp < highThreshLevel] = 0
            cube[:, :, highThreshBands] = temp

        'Apply the lower threshold to all other bands'
        cube[cube < threshLevel] = 0

        'Read the mask and header'
        maskName = imgName.replace(self.op_postFix, '_mask')
        maskHdr = maskName.replace('.img', '.hdr')
        mask = envi.open(maskHdr, maskName)
        mask = mask.load()
        mask = np.squeeze(mask)

        'Intitialize new products'
        cube_BestGuess = np.zeros(cube.shape)
        cube_Identification = np.zeros(cube.shape)

        'For each pixel find which mineral has highest score'
        bestGuess = np.argmax(cube, axis=2)
        'Now modify the cosine distance map to only hold best guess'
        for em in range(bands):
            temp = np.squeeze(cube[:, :, em])
            'Set all except best guess to 0'
            temp[bestGuess != em] = 0
            cube_BestGuess[:, :, em] = np.multiply(temp, mask)
            # cube_BestGuess[:,:, em] = temp

        'Save best guess cube'
        bestGuessName = imgName.replace('_Cosine', '_BestGuess')
        bestGuessName = bestGuessName.replace('.img', '.hdr')
        envi.save_image(bestGuessName, cube_BestGuess, dtype=np.float32, force=True, interleave='bil', metadata=header)

        return bestGuessName.replace('.hdr', '.img')
Example #31
0
import os

if __name__ == "__main__":
    srcFolder = '/Volume1/data/CRISM/jezeroCoRegistration/Registered Images/FRT00005C5E_trail/'

    for r, d, f in os.walk(srcFolder):
        for file in f:
            if file.find('_warp.dat') != -1:
                if file.find('.enp') == -1:
                    'The image name is'
                    imgName = os.path.join(r, file)
                    hdrName = imgName.replace('.dat', '.hdr')

                    'Read in and load image'
                    img = envi.open(hdrName, imgName)
                    cube = np.asarray(img.load())
                    cube[cube >= 1] = 0

                    'Read in the header'
                    hdr = envi.read_envi_header(hdrName)
                    hdr['data ignore value'] = 0

                    'Write this updated file'
                    outName = imgName.replace('.dat', '_zf.hdr')
                    envi.save_image(outName,
                                    cube,
                                    dtype=np.float32,
                                    force=True,
                                    interleave='bil',
                                    metadata=hdr)
Example #32
0
    def create_Maps4CRISMImages_CompBestGuess(self, img55Name, img33Name='', img11Name=''):
        """
        @function name      :create_Maps4CRISMImages_CompBestGuess
        @description        :This function works with best guess maps with different smoothing levels. If there is a
        conflict of the mineral identified at different levels, we will revert to identification at the lowest smoothing
        levels (this will ensure that small deposits are not missed). On the other hand if the mineral identified at all
        the levels are the same the will use the highest score across the different smoothing levels.
        ----------------------------------------------------------------------------------------------------------------
        INPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) img55Name:              Address of best guess image at a 5X5 level smoothing
        2) img33Name:              Address of best guess image at a 5X5 level smoothing
        3) img11Name:              Address of best guess image at a 5X5 level smoothing
        ----------------------------------------------------------------------------------------------------------------
        OUTPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) outFileName:              Address of the Hyperspectral image which contains the mask image.
        """

        'If names follow the scheme automatically extract them'
        if not img11Name:
            img11Name = img55Name.replace('_smoothed5', '_smoothed1')

        if not img33Name:
            img33Name = img55Name.replace('_smoothed5', '_smoothed3')

        'Read in the best guess image - pixel level'
        hdr11Name = img11Name.replace('.img', '.hdr')
        img_11 = envi.open(hdr11Name, img11Name)
        cube_11 = img_11.load()
        [rows, cols, bands] = cube_11.shape
        header = envi.read_envi_header(hdr11Name)

        'Read in the best guess image - 3X3 level'
        hdr33Name = img33Name.replace('.img', '.hdr')
        img_33 = envi.open(hdr33Name, img33Name)
        cube_33 = img_33.load()


        'Read in the best guess image - 5X5 level'
        hdr55Name = img55Name.replace('.img', '.hdr')
        img_55 = envi.open(hdr55Name, img55Name)
        cube_55 = img_55.load()

        'Get the corresponding image from the various images'
        cube_maxComposite = np.zeros((rows, cols, bands))
        cube_maxArg = np.zeros((rows, cols, bands))
        for ii in range(bands):
            temp = np.zeros((rows, cols, 3))
            temp[:, :, 0] = np.squeeze(cube_11[:, :, ii])
            temp[:, :, 1] = np.squeeze(cube_33[:, :, ii])
            temp[:, :, 2] = np.squeeze(cube_55[:, :, ii])

            tempMax = np.max(temp, 2)
            tempArg = np.argmax(temp, 2)
            cube_maxComposite[:, :, ii] = tempMax
            cube_maxArg[:, :, ii] = tempArg

        'For each pixel find which mineral has highest score'
        bestGuess = np.argmax(cube_maxComposite, axis=2)
        'Now modify the cosine distance map to only hold best guess'
        for em in range(bands):
            temp = np.squeeze(cube_maxComposite[:, :, em])
            temp1 = np.squeeze(cube_maxArg[:, :, em])
            'Set all except best guess to 0'
            temp[bestGuess != em] = 0
            temp1[bestGuess != em] = 0
            cube_maxComposite[:, :, em] = temp
            cube_maxArg[:, :, em] = temp1

        'File Name for the best guess image'
        outFileName = img11Name.replace('_BestGuess', '_compBestGuess')
        outFileName = outFileName.replace('_smoothed1', '_')
        outFileName = outFileName.replace('.img', '.hdr')
        'File Name for the image which contains the smoothing level which leads to the identification'
        out1FileName = img11Name.replace('_BestGuess', '_compBestGuessArg')
        out1FileName = out1FileName.replace('_smoothed1', '_')
        out1FileName = out1FileName.replace('.img', '.hdr')

        envi.save_image(outFileName, cube_maxComposite, dtype=np.float32, force=True,
                        interleave='bil', metadata=header)
        envi.save_image(out1FileName, cube_maxArg, dtype=np.float32, force=True,
                        interleave='bil', metadata=header)

        return outFileName.replace('.hdr', '.img')