#!/usr/bin/env python3


import os
import argparse
import logging

import isce
import isceobj
from components.stdproc.stdproc import crossmul
from iscesys.ImageUtil.ImageUtil import ImageUtil as IU


def createParser():

    '''
    Command Line Parser.
    '''
    parser = argparse.ArgumentParser( description='Generate offset field between two Sentinel swaths')
    parser.add_argument('-m', '--master', type=str, dest='master', required=True,
            help='Master image')
    parser.add_argument('-s', '--slave', type=str, dest='slave', required=True,
            help='Slave image')
    parser.add_argument('-o', '--outdir', type=str, dest='prefix', default='crossmul',
            help='Prefix of output int and amp files')
    parser.add_argument('-a', '--alks', type=int, dest='azlooks', default=1,
            help='Azimuth looks')
    parser.add_argument('-r', '--rlks', type=int, dest='rglooks', default=1,
            help='Range looks')

    return parser

def cmdLineParse(iargs = None):
    parser = createParser()
    return parser.parse_args(args=iargs)

def run(imageSlc1, imageSlc2, resampName, azLooks, rgLooks):
    objSlc1 = isceobj.createSlcImage()
    #right now imageSlc1 and 2 are just text files, need to open them as image

    IU.copyAttributes(imageSlc1, objSlc1)
    objSlc1.setAccessMode('read')
    objSlc1.createImage()

    objSlc2 = isceobj.createSlcImage()
    IU.copyAttributes(imageSlc2, objSlc2)
    objSlc2.setAccessMode('read')
    objSlc2.createImage()

    slcWidth = imageSlc1.getWidth()
    intWidth = int(slcWidth / rgLooks)

    lines = min(imageSlc1.getLength(), imageSlc2.getLength())

    resampAmp = resampName + '.amp'
    resampInt = resampName + '.int'

    objInt = isceobj.createIntImage()
    objInt.setFilename(resampInt)
    objInt.setWidth(intWidth)
    imageInt = isceobj.createIntImage()
    IU.copyAttributes(objInt, imageInt)
    objInt.setAccessMode('write')
    objInt.createImage()

    objAmp = isceobj.createAmpImage()
    objAmp.setFilename(resampAmp)
    objAmp.setWidth(intWidth)
    imageAmp = isceobj.createAmpImage()
    IU.copyAttributes(objAmp, imageAmp)
    objAmp.setAccessMode('write')
    objAmp.createImage()

    objCrossmul = crossmul.createcrossmul()
    objCrossmul.width = slcWidth
    objCrossmul.length = lines
    objCrossmul.LooksDown = azLooks
    objCrossmul.LooksAcross = rgLooks

    objCrossmul.crossmul(objSlc1, objSlc2, objInt, objAmp)

    for obj in [objInt, objAmp, objSlc1, objSlc2]:
        obj.finalizeImage()

    return imageInt, imageAmp


def main(iargs=None):
    inps = cmdLineParse(iargs)

    img1 = isceobj.createImage()
    img1.load(inps.master + '.xml')

    img2 = isceobj.createImage()
    img2.load(inps.slave + '.xml')

    os.makedirs(os.path.dirname(inps.prefix), exist_ok=True)

    run(img1, img2, inps.prefix, inps.azlooks, inps.rglooks)

if __name__ == '__main__':

    main()
    '''
    Main driver.
    '''

[((19, 13, 19, 102), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((38, 14, 38, 38), 'isceobj.createSlcImage', 'isceobj.createSlcImage', ({}, {}), '()', False, 'import isceobj\n'), ((41, 4, 41, 41), 'iscesys.ImageUtil.ImageUtil.ImageUtil.copyAttributes', 'IU.copyAttributes', ({(41, 22, 41, 31): 'imageSlc1', (41, 33, 41, 40): 'objSlc1'}, {}), '(imageSlc1, objSlc1)', True, 'from iscesys.ImageUtil.ImageUtil import ImageUtil as IU\n'), ((45, 14, 45, 38), 'isceobj.createSlcImage', 'isceobj.createSlcImage', ({}, {}), '()', False, 'import isceobj\n'), ((46, 4, 46, 41), 'iscesys.ImageUtil.ImageUtil.ImageUtil.copyAttributes', 'IU.copyAttributes', ({(46, 22, 46, 31): 'imageSlc2', (46, 33, 46, 40): 'objSlc2'}, {}), '(imageSlc2, objSlc2)', True, 'from iscesys.ImageUtil.ImageUtil import ImageUtil as IU\n'), ((58, 13, 58, 37), 'isceobj.createIntImage', 'isceobj.createIntImage', ({}, {}), '()', False, 'import isceobj\n'), ((61, 15, 61, 39), 'isceobj.createIntImage', 'isceobj.createIntImage', ({}, {}), '()', False, 'import isceobj\n'), ((62, 4, 62, 39), 'iscesys.ImageUtil.ImageUtil.ImageUtil.copyAttributes', 'IU.copyAttributes', ({(62, 22, 62, 28): 'objInt', (62, 30, 62, 38): 'imageInt'}, {}), '(objInt, imageInt)', True, 'from iscesys.ImageUtil.ImageUtil import ImageUtil as IU\n'), ((66, 13, 66, 37), 'isceobj.createAmpImage', 'isceobj.createAmpImage', ({}, {}), '()', False, 'import isceobj\n'), ((69, 15, 69, 39), 'isceobj.createAmpImage', 'isceobj.createAmpImage', ({}, {}), '()', False, 'import isceobj\n'), ((70, 4, 70, 39), 'iscesys.ImageUtil.ImageUtil.ImageUtil.copyAttributes', 'IU.copyAttributes', ({(70, 22, 70, 28): 'objAmp', (70, 30, 70, 38): 'imageAmp'}, {}), '(objAmp, imageAmp)', True, 'from iscesys.ImageUtil.ImageUtil import ImageUtil as IU\n'), ((74, 18, 74, 43), 'components.stdproc.stdproc.crossmul.createcrossmul', 'crossmul.createcrossmul', ({}, {}), '()', False, 'from components.stdproc.stdproc import crossmul\n'), ((91, 11, 91, 32), 'isceobj.createImage', 'isceobj.createImage', ({}, {}), '()', False, 'import isceobj\n'), ((94, 11, 94, 32), 'isceobj.createImage', 'isceobj.createImage', ({}, {}), '()', False, 'import isceobj\n'), ((97, 16, 97, 44), 'os.path.dirname', 'os.path.dirname', ({(97, 32, 97, 43): 'inps.prefix'}, {}), '(inps.prefix)', False, 'import os\n')]