Beispiel #1
0
def multilook(infile,
              outname=None,
              alks=5,
              rlks=15,
              multilook_tool="isce",
              no_data=None):
    '''
    Take looks.
    '''

    if multilook_tool == "gdal":

        from osgeo import gdal

        print("multi looking using gdal ...")
        if outname is None:
            spl = os.path.splitext(infile)
            ext = '.{0}alks_{1}rlks'.format(alks, rlks)
            outname = spl[0] + ext + spl[1]

        print(infile)
        ds = gdal.Open(infile + ".vrt", gdal.GA_ReadOnly)

        xSize = ds.RasterXSize
        ySize = ds.RasterYSize

        outXSize = xSize / int(rlks)
        outYSize = ySize / int(alks)

        if no_data:
            gdalTranslateOpts = gdal.TranslateOptions(format="ENVI",
                                                      width=outXSize,
                                                      height=outYSize,
                                                      noData=no_data)
        else:
            gdalTranslateOpts = gdal.TranslateOptions(format="ENVI",
                                                      width=outXSize,
                                                      height=outYSize)

        gdal.Translate(outname, ds, options=gdalTranslateOpts)
        ds = None

        ds = gdal.Open(outname, gdal.GA_ReadOnly)
        gdal.Translate(outname + ".vrt",
                       ds,
                       options=gdal.TranslateOptions(format="VRT"))
        ds = None

    else:
        from mroipac.looks.Looks import Looks

        print('Multilooking {0} ...'.format(infile))

        inimg = isceobj.createImage()
        inimg.load(infile + '.xml')

        if outname is None:
            spl = os.path.splitext(inimg.filename)
            ext = '.{0}alks_{1}rlks'.format(alks, rlks)
            outname = spl[0] + ext + spl[1]

        lkObj = Looks()
        lkObj.setDownLooks(alks)
        lkObj.setAcrossLooks(rlks)
        lkObj.setInputImage(inimg)
        lkObj.setOutputFilename(outname)
        lkObj.looks()

    return outname
Beispiel #2
0
def runMultilook(in_dir,
                 out_dir,
                 alks,
                 rlks,
                 in_ext='.rdr',
                 out_ext='.rdr',
                 method='gdal',
                 fbase_list=[
                     'hgt', 'incLocal', 'lat', 'lon', 'los', 'shadowMask',
                     'waterMask'
                 ]):
    """
    Multilook geometry files.
    """
    from iscesys.Parsers.FileParserFactory import createFileParser
    from mroipac.looks.Looks import Looks

    msg = 'generate multilooked geometry files with alks={} and rlks={}'.format(
        alks, rlks)
    if method == 'isce':
        msg += ' using mroipac.looks.Looks() ...'
    else:
        msg += ' using gdal.Translate() ...'
    print('-' * 50 + '\n' + msg)

    # create 'geom_reference' directory
    os.makedirs(out_dir, exist_ok=True)

    # multilook files one by one
    for fbase in fbase_list:
        in_file = os.path.join(in_dir, '{}{}'.format(fbase, in_ext))
        out_file = os.path.join(out_dir, '{}{}'.format(fbase, out_ext))

        if all(os.path.isfile(in_file + ext) for ext in ['', '.vrt', '.xml']):
            print('multilook {}'.format(in_file))

            # option 1 - Looks module (isce)
            if method == 'isce':
                xmlProp = createFileParser('xml').parse(in_file + '.xml')[0]
                if ('image_type' in xmlProp
                        and xmlProp['image_type'] == 'dem'):
                    inImage = isceobj.createDemImage()
                else:
                    inImage = isceobj.createImage()

                inImage.load(in_file + '.xml')
                inImage.filename = in_file

                lkObj = Looks()
                lkObj.setDownLooks(alks)
                lkObj.setAcrossLooks(rlks)
                lkObj.setInputImage(inImage)
                lkObj.setOutputFilename(out_file)
                lkObj.looks()

            # option 2 - gdal_translate (gdal)
            elif method == 'gdal':
                ds = gdal.Open(in_file, gdal.GA_ReadOnly)
                in_wid = ds.RasterXSize
                in_len = ds.RasterYSize

                out_wid = int(in_wid / rlks)
                out_len = int(in_len / alks)
                src_wid = out_wid * rlks
                src_len = out_len * alks

                options_str = '-of ENVI -a_nodata 0 -outsize {ox} {oy} -srcwin 0 0 {sx} {sy} '.format(
                    ox=out_wid, oy=out_len, sx=src_wid, sy=src_len)
                gdal.Translate(out_file, ds, options=options_str)

                # generate ISCE .xml file
                if not os.path.isfile(out_file + '.xml'):
                    cmd = 'gdal2isce_xml.py -i {}.vrt'.format(out_file)
                    print(cmd)
                    os.system(cmd)

            else:
                raise ValueError(
                    'un-supported multilook method: {}'.format(method))

            # copy the full resolution xml/vrt file from ./merged/geom_reference to ./geom_reference
            # to facilitate the number of looks extraction
            # the file path inside .xml file is not, but should, updated
            if in_file != out_file + '.full':
                shutil.copy(in_file + '.xml', out_file + '.full.xml')
                shutil.copy(in_file + '.vrt', out_file + '.full.vrt')

    return out_dir
Beispiel #3
0
def runLook(self):
    '''take looks
    '''
    catalog = isceobj.Catalog.createCatalog(self._insar.procDoc.name)
    self.updateParamemetersFromUser()

    #referenceTrack = self._insar.loadTrack(reference=True)
    #secondaryTrack = self._insar.loadTrack(reference=False)
    wbdFile = os.path.abspath(self._insar.wbd)

    insarDir = 'insar'
    os.makedirs(insarDir, exist_ok=True)
    os.chdir(insarDir)

    amp = isceobj.createImage()
    amp.load(self._insar.amplitude + '.xml')
    width = amp.width
    length = amp.length
    width2 = int(width / self._insar.numberRangeLooks2)
    length2 = int(length / self._insar.numberAzimuthLooks2)

    if not ((self._insar.numberRangeLooks2 == 1) and
            (self._insar.numberAzimuthLooks2 == 1)):
        #take looks
        look(self._insar.differentialInterferogram,
             self._insar.multilookDifferentialInterferogram, width,
             self._insar.numberRangeLooks2, self._insar.numberAzimuthLooks2, 4,
             0, 1)
        look(self._insar.amplitude, self._insar.multilookAmplitude, width,
             self._insar.numberRangeLooks2, self._insar.numberAzimuthLooks2, 4,
             1, 1)
        look(self._insar.latitude, self._insar.multilookLatitude, width,
             self._insar.numberRangeLooks2, self._insar.numberAzimuthLooks2, 3,
             0, 1)
        look(self._insar.longitude, self._insar.multilookLongitude, width,
             self._insar.numberRangeLooks2, self._insar.numberAzimuthLooks2, 3,
             0, 1)
        look(self._insar.height, self._insar.multilookHeight, width,
             self._insar.numberRangeLooks2, self._insar.numberAzimuthLooks2, 3,
             0, 1)
        #creat xml
        create_xml(self._insar.multilookDifferentialInterferogram, width2,
                   length2, 'int')
        create_xml(self._insar.multilookAmplitude, width2, length2, 'amp')
        create_xml(self._insar.multilookLatitude, width2, length2, 'double')
        create_xml(self._insar.multilookLongitude, width2, length2, 'double')
        create_xml(self._insar.multilookHeight, width2, length2, 'double')
        #los has two bands, use look program in isce instead
        #cmd = "looks.py -i {} -o {} -r {} -a {}".format(self._insar.los, self._insar.multilookLos, self._insar.numberRangeLooks2, self._insar.numberAzimuthLooks2)
        #runCmd(cmd)

        #replace the above system call with function call
        from mroipac.looks.Looks import Looks
        from isceobj.Image import createImage
        inImage = createImage()
        inImage.load(self._insar.los + '.xml')

        lkObj = Looks()
        lkObj.setDownLooks(self._insar.numberAzimuthLooks2)
        lkObj.setAcrossLooks(self._insar.numberRangeLooks2)
        lkObj.setInputImage(inImage)
        lkObj.setOutputFilename(self._insar.multilookLos)
        lkObj.looks()

        #water body
        #this looking operation has no problems where there is only water and land, but there is also possible no-data area
        #look(self._insar.wbdOut, self._insar.multilookWbdOut, width, self._insar.numberRangeLooks2, self._insar.numberAzimuthLooks2, 0, 0, 1)
        #create_xml(self._insar.multilookWbdOut, width2, length2, 'byte')
        #use waterBodyRadar instead to avoid the problems of no-data pixels in water body
        waterBodyRadar(self._insar.multilookLatitude,
                       self._insar.multilookLongitude, wbdFile,
                       self._insar.multilookWbdOut)

    os.chdir('../')

    catalog.printToLog(logger, "runLook")
    self._insar.procDoc.addAllFromCatalog(catalog)