2019-01-16 19:40:08 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#Author: Heresh Fattahi
|
|
|
|
|
extractCommonValidRegion: check stack/*.xml file existance
Otherwise, if 'stack' folder exists but is empty, the script will return ERROR as below:
```
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack already exists.
Replacing master with existing stack.
updating the valid overlap region of:
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack
******************
swath: 1
Traceback (most recent call last):
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 109, in <module>
main()
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 95, in main
topMaster = ut.loadProduct(os.path.join(inps.master , 'IW{0}.xml'.format(swath)))
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/s1a_isce_utils.py", line 26, in loadProduct
obj = pm.loadProduct(xmlname)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/ProductManager.py", line 25, in loadProduct
self.load(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/Configurable.py", line 1407, in load
tmpProp, tmpFact, tmpMisc = FP.parse(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Parsers/XmlParser.py", line 41, in parse
root = ET.parse(filename)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 1197, in parse
tree.parse(source, parser)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 587, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/net/kraken/nobak/zyunjun/TonopahSenAT64/stack/IW1.xml'
```
2020-05-22 05:11:18 +00:00
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import numpy as np
|
2021-01-06 19:07:58 +00:00
|
|
|
from osgeo import gdal
|
2019-01-16 19:40:08 +00:00
|
|
|
import isce
|
|
|
|
import isceobj
|
|
|
|
from isceobj.Sensor.TOPS import createTOPSSwathSLCProduct
|
|
|
|
from mroipac.correlation.correlation import Correlation
|
|
|
|
import s1a_isce_utils as ut
|
extractCommonValidRegion: check stack/*.xml file existance
Otherwise, if 'stack' folder exists but is empty, the script will return ERROR as below:
```
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack already exists.
Replacing master with existing stack.
updating the valid overlap region of:
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack
******************
swath: 1
Traceback (most recent call last):
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 109, in <module>
main()
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 95, in main
topMaster = ut.loadProduct(os.path.join(inps.master , 'IW{0}.xml'.format(swath)))
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/s1a_isce_utils.py", line 26, in loadProduct
obj = pm.loadProduct(xmlname)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/ProductManager.py", line 25, in loadProduct
self.load(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/Configurable.py", line 1407, in load
tmpProp, tmpFact, tmpMisc = FP.parse(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Parsers/XmlParser.py", line 41, in parse
root = ET.parse(filename)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 1197, in parse
tree.parse(source, parser)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 587, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/net/kraken/nobak/zyunjun/TonopahSenAT64/stack/IW1.xml'
```
2020-05-22 05:11:18 +00:00
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
def createParser():
|
|
|
|
parser = argparse.ArgumentParser( description='Extract valid overlap region for the stack')
|
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
parser.add_argument('-m', '--reference', dest='reference', type=str, required=True,
|
|
|
|
help='Directory with reference acquisition')
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
parser.add_argument('-s', '--secondary', dest='secondary', type=str, required=True,
|
|
|
|
help='Directory with secondary acquisition')
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
def cmdLineParse(iargs = None):
|
|
|
|
parser = createParser()
|
|
|
|
return parser.parse_args(args=iargs)
|
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
def updateValidRegion(topReference, secondaryPath, swath):
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
#secondarySwathList = ut.getSwathList(secondary)
|
|
|
|
#swathList = list(sorted(set(referenceSwathList+secondarySwathList)))
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
#for swath in swathList:
|
|
|
|
#IWstr = 'IW{0}'.format(swath)
|
|
|
|
####Load relevant products
|
2020-07-02 19:40:49 +00:00
|
|
|
#topReference = ut.loadProduct(os.path.join(inps.reference , 'IW{0}.xml'.format(swath)))
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2020-10-24 16:31:48 +00:00
|
|
|
print(secondaryPath)
|
2020-07-02 19:40:49 +00:00
|
|
|
topCoreg = ut.loadProduct(os.path.join(secondaryPath , 'IW{0}.xml'.format(swath)))
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
topIfg = ut.coregSwathSLCProduct()
|
|
|
|
topIfg.configure()
|
|
|
|
|
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
minReference = topReference.bursts[0].burstNumber
|
|
|
|
maxReference = topReference.bursts[-1].burstNumber
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
minSecondary = topCoreg.bursts[0].burstNumber
|
|
|
|
maxSecondary = topCoreg.bursts[-1].burstNumber
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
minBurst = max(minSecondary, minReference)
|
|
|
|
maxBurst = min(maxSecondary, maxReference)
|
|
|
|
print ('minSecondary,maxSecondary',minSecondary, maxSecondary)
|
|
|
|
print ('minReference,maxReference',minReference, maxReference)
|
2019-01-16 19:40:08 +00:00
|
|
|
print ('minBurst, maxBurst: ', minBurst, maxBurst)
|
|
|
|
|
|
|
|
for ii in range(minBurst, maxBurst + 1):
|
|
|
|
|
|
|
|
####Process the top bursts
|
2020-07-02 19:40:49 +00:00
|
|
|
reference = topReference.bursts[ii-minReference]
|
|
|
|
secondary = topCoreg.bursts[ii-minSecondary]
|
|
|
|
ut.adjustCommonValidRegion(reference,secondary)
|
|
|
|
#topReference.bursts[ii-minReference].firstValidLine = reference.firstValidLine
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
return topReference
|
2019-01-16 19:40:08 +00:00
|
|
|
|
2020-10-24 16:31:48 +00:00
|
|
|
def dropSecondarysWithDifferentNumberOfBursts(secondaryList, reference, swathList):
|
|
|
|
'''Drop secondary acquisitions that have different number of bursts
|
|
|
|
than the reference acquisition.
|
|
|
|
'''
|
|
|
|
print('checking the number of bursts in coreg_secondarys against the one in reference')
|
|
|
|
secondaryList2Drop = []
|
|
|
|
for swath in swathList:
|
|
|
|
prodReference = ut.loadProduct(os.path.join(reference, 'IW{0}.xml'.format(swath)))
|
|
|
|
numBursts = len(prodReference.bursts)
|
|
|
|
|
|
|
|
for secondary in secondaryList:
|
|
|
|
prodSecondary = ut.loadProduct(os.path.join(secondary, 'IW{0}.xml'.format(swath)))
|
|
|
|
if len(prodSecondary.bursts) != numBursts:
|
|
|
|
msg = 'WARNING: {} has different number of bursts ({}) than the reference {} ({}) for swath {}'.format(
|
|
|
|
os.path.basename(secondary), len(prodSecondary.bursts),
|
|
|
|
os.path.basename(reference), numBursts, swath)
|
|
|
|
msg += ' --> exclude it for common region calculation'
|
|
|
|
print(msg)
|
|
|
|
secondaryList2Drop.append(secondary)
|
|
|
|
|
|
|
|
secondaryList2Drop = list(sorted(set(secondaryList2Drop)))
|
|
|
|
if len(secondaryList2Drop) == 0:
|
|
|
|
print('all secondary images have the same number of bursts as the reference')
|
|
|
|
|
|
|
|
secondaryList = list(sorted(set(secondaryList) - set(secondaryList2Drop)))
|
|
|
|
|
|
|
|
return secondaryList
|
|
|
|
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
def main(iargs=None):
|
|
|
|
'''extract common valid overlap region for the stack.
|
|
|
|
'''
|
|
|
|
inps=cmdLineParse(iargs)
|
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
stackDir = os.path.join(os.path.dirname(inps.reference),'stack')
|
2019-01-16 19:40:08 +00:00
|
|
|
if not os.path.exists(stackDir):
|
|
|
|
print('creating ', stackDir)
|
|
|
|
os.makedirs(stackDir)
|
extractCommonValidRegion: check stack/*.xml file existance
Otherwise, if 'stack' folder exists but is empty, the script will return ERROR as below:
```
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack already exists.
Replacing master with existing stack.
updating the valid overlap region of:
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack
******************
swath: 1
Traceback (most recent call last):
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 109, in <module>
main()
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 95, in main
topMaster = ut.loadProduct(os.path.join(inps.master , 'IW{0}.xml'.format(swath)))
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/s1a_isce_utils.py", line 26, in loadProduct
obj = pm.loadProduct(xmlname)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/ProductManager.py", line 25, in loadProduct
self.load(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/Configurable.py", line 1407, in load
tmpProp, tmpFact, tmpMisc = FP.parse(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Parsers/XmlParser.py", line 41, in parse
root = ET.parse(filename)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 1197, in parse
tree.parse(source, parser)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 587, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/net/kraken/nobak/zyunjun/TonopahSenAT64/stack/IW1.xml'
```
2020-05-22 05:11:18 +00:00
|
|
|
elif len(glob.glob(os.path.join(stackDir, '*.xml'))) > 0:
|
2019-01-16 19:40:08 +00:00
|
|
|
print(stackDir , ' already exists.')
|
2020-07-02 19:40:49 +00:00
|
|
|
print('Replacing reference with existing stack.')
|
|
|
|
inps.reference = stackDir
|
2019-01-16 19:40:08 +00:00
|
|
|
print('updating the valid overlap region of:')
|
|
|
|
print(stackDir)
|
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
referenceSwathList = ut.getSwathList(inps.reference)
|
|
|
|
secondaryList = glob.glob(os.path.join(inps.secondary,'2*'))
|
|
|
|
secondarySwathList = ut.getSwathList(secondaryList[0]) # assuming all secondarys have the same swaths
|
|
|
|
swathList = list(sorted(set(referenceSwathList+secondarySwathList)))
|
2020-10-24 16:31:48 +00:00
|
|
|
# discard secondarys with different number of bursts than the reference
|
|
|
|
secondaryList = dropSecondarysWithDifferentNumberOfBursts(secondaryList, inps.reference, swathList)
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
for swath in swathList:
|
|
|
|
print('******************')
|
|
|
|
print('swath: ', swath)
|
|
|
|
####Load relevant products
|
2020-07-02 19:40:49 +00:00
|
|
|
topReference = ut.loadProduct(os.path.join(inps.reference , 'IW{0}.xml'.format(swath)))
|
|
|
|
#print('reference.firstValidLine: ', topReference.bursts[4].firstValidLine)
|
|
|
|
for secondary in secondaryList:
|
|
|
|
topReference = updateValidRegion(topReference, secondary, swath)
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
print('writing ', os.path.join(stackDir , 'IW{0}.xml'.format(swath)))
|
2020-07-02 19:40:49 +00:00
|
|
|
ut.saveProduct(topReference, os.path.join(stackDir , 'IW{0}.xml'.format(swath)))
|
2020-04-13 19:40:32 +00:00
|
|
|
os.makedirs(os.path.join(stackDir ,'IW{0}'.format(swath)), exist_ok=True)
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
'''
|
|
|
|
Main driver.
|
|
|
|
'''
|
|
|
|
main()
|
|
|
|
|
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
#swathList = ut.getSwathList(reference)
|
2019-01-16 19:40:08 +00:00
|
|
|
#swathList[2]
|
|
|
|
#frames = []
|
|
|
|
#for swath in swathList:
|
2020-07-02 19:40:49 +00:00
|
|
|
# ifg = ut.loadProduct(os.path.join(inps.reference , 'IW{0}.xml'.format(swath)))
|
2019-01-16 19:40:08 +00:00
|
|
|
|
|
|
|
# if inps.isaligned:
|
|
|
|
# reference = ifg.reference
|
|
|
|
# else:
|
|
|
|
# reference = ifg
|
|
|
|
|
|
|
|
# minBurst = ifg.bursts[0].burstNumber
|
|
|
|
# maxBurst = ifg.bursts[-1].burstNumber
|
|
|
|
|
|
|
|
# if minBurst==maxBurst:
|
|
|
|
# print('Skipping processing of swath {0}'.format(swath))
|
|
|
|
# continue
|
|
|
|
|
|
|
|
# frames.append(ifg)
|
|
|
|
|
|
|
|
|
|
|
|
#swaths = [Swath(x) for x in frame]
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2020-07-02 19:40:49 +00:00
|
|
|
slcPath = '/home/hfattahi/PROCESSDIR/MexicoCity_Test/TestStack_offsets/reference'
|
2019-01-16 19:40:08 +00:00
|
|
|
swath = ut.loadProduct(os.path.join(slcPath , 'IW{0}.xml'.format(2)))
|
|
|
|
|
|
|
|
tref = swath.sensingStart
|
|
|
|
rref = swath.bursts[0].startingRange
|
extractCommonValidRegion: check stack/*.xml file existance
Otherwise, if 'stack' folder exists but is empty, the script will return ERROR as below:
```
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack already exists.
Replacing master with existing stack.
updating the valid overlap region of:
/net/kraken/nobak/zyunjun/TonopahSenAT64/stack
******************
swath: 1
Traceback (most recent call last):
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 109, in <module>
main()
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/extractCommonValidRegion.py", line 95, in main
topMaster = ut.loadProduct(os.path.join(inps.master , 'IW{0}.xml'.format(swath)))
File "/home/zyunjun/tools/isce2/contrib/stack/topsStack/s1a_isce_utils.py", line 26, in loadProduct
obj = pm.loadProduct(xmlname)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/ProductManager.py", line 25, in loadProduct
self.load(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Component/Configurable.py", line 1407, in load
tmpProp, tmpFact, tmpMisc = FP.parse(filename)
File "/net/kraken/home1/geomod/apps/rhel7/isce2-github/isce/components/iscesys/Parsers/XmlParser.py", line 41, in parse
root = ET.parse(filename)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 1197, in parse
tree.parse(source, parser)
File "/net/kraken/home1/geomod/apps/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 587, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/net/kraken/nobak/zyunjun/TonopahSenAT64/stack/IW1.xml'
```
2020-05-22 05:11:18 +00:00
|
|
|
dt = swath.bursts[0].azimuthTimeInterval
|
2019-01-16 19:40:08 +00:00
|
|
|
dr = swath.bursts[0].rangePixelSize
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print (slcPath)
|
|
|
|
for ind, burst in enumerate(swath.bursts):
|
|
|
|
|
|
|
|
xoff = np.int(np.round( (burst.startingRange - rref)/dr))
|
|
|
|
yoff = np.int(np.round( (burst.sensingStart - tref).total_seconds() / dt))
|
|
|
|
tyoff = int(burst.firstValidLine)
|
|
|
|
txoff = int(burst.firstValidSample)
|
|
|
|
wysize = int(burst.numValidLines)
|
|
|
|
wxsize = int(burst.numValidSamples)
|
|
|
|
fyoff = int(yoff + burst.firstValidLine)
|
|
|
|
fxoff = int(xoff + burst.firstValidSample)
|
|
|
|
|
|
|
|
#print(xoff, fxoff)
|
|
|
|
print(yoff, fyoff)
|
|
|
|
'''
|