Files

236 lines
11 KiB
Python

#! /usr/bin/env python
#################################################################
### This program is part of PyINT v2.1 ###
### Copy Right (c): 2017-2019, Yunmeng Cao ###
### Author: Yunmeng Cao ###
### Contact : ymcmrs@gmail.com ###
#################################################################
import numpy as np
import os
import sys
import argparse
import subprocess
from pyint import _utils as ut
def cmdLineParse():
parser = argparse.ArgumentParser(description='Coregister SM mode SLC to a reference SLC image using GAMMA.',\
formatter_class=argparse.RawTextHelpFormatter,\
epilog=INTRODUCTION+'\n'+EXAMPLE)
parser.add_argument('projectName', help='Name of project.')
parser.add_argument('mdate', help='date of the slave SLC image')
parser.add_argument('sdate', help='date of the slave SLC image')
parser.add_argument('lon_min', help='the min lon of cut image')
parser.add_argument('lon_max', help='the max lon of cut image')
parser.add_argument('lat_min', help='the min lat of cut image')
parser.add_argument('lat_max', help='the min lat of cut image')
parser.add_argument('flag', help="the cut image flag (1: do; 0:don't do)")
inps = parser.parse_args()
return inps
INTRODUCTION = '''
-------------------------------------------------------------------
create_files for psokinv software running file by generated by Gamma
'''
EXAMPLE = """Usage:
create_psokinv.py projectName mdata sdata lon_min lon_max lat_min lat_max flag
create_psokinv.py PacayaT163TsxHhA 20241203 20241215 80 81 39 30 1
-------------------------------------------------------------------
"""
def main(argv):
inps = cmdLineParse()
projectName = inps.projectName
Sdate = inps.sdate
Mdate = inps.mdate
lon_max = inps.lon_max
lon_min = inps.lon_min
lat_min = inps.lat_min
lat_max = inps.lat_max
flag = inps.flag
scratchDir = os.getenv('SCRATCHDIR')
templateDir = os.getenv('TEMPLATEDIR')
templateFile = templateDir + "/" + projectName + ".template"
templateDict=ut.update_template(templateFile)
rlks = templateDict['range_looks']
azlks = templateDict['azimuth_looks']
Smdate = templateDict['masterDate']
demDir = scratchDir + '/' + projectName + '/DEM'
slcDir = scratchDir + '/' + projectName + "/SLC"
rslcDir = scratchDir + '/' + projectName + "/RSLC"
ifgramDir = scratchDir + '/' + projectName + "/ifgrams"
workDir = ifgramDir + '/' + Mdate + '-' + Sdate
MslcDir = rslcDir + '/' + Mdate
SslcDir = rslcDir + '/' + Sdate
offpar = SslcDir + '/' + Smdate + '_' + Sdate + '.off'
slcpar = MslcDir + '/' + Smdate + '.rslc.par'
dempar = demDir + '/' + Smdate + '_' + rlks + 'rlks.utm.dem.par'
dem = demDir + '/' + Smdate + '_' + rlks + 'rlks.utm.dem'
unwpar = demDir + '/' + Mdate + '_' + rlks + 'rlks.utm.dem.par'
unw = workDir + '/geo_' + Mdate + '-' + Sdate + "_" + rlks+"rlks.diff_filt.unw"
los = workDir + '/geo_' + Mdate + '-' + Sdate + "_" + rlks+"rlks.dispmap"
cor = workDir + '/geo_' + Mdate + '-' + Sdate + "_" + rlks+"rlks.diff_filt.cor"
os.chdir(workDir)
call_str = "look_vector " + slcpar+ " " + offpar + " " + dempar + " " + dem + ' lv_theta lv_phi'
os.system(call_str)
call_str = "grep 'corner_lat:' " + dempar + " | awk '{print $2}' "
North=subprocess.getstatusoutput(call_str)[1]
call_str = "grep 'corner_lon:' " + dempar + " | awk '{print $2}' "
West=subprocess.getstatusoutput(call_str)[1]
call_str = "grep 'post_lat:' " + dempar + " | awk '{print $2}' "
posty=subprocess.getstatusoutput(call_str)[1]
call_str = "grep 'post_lon:' " + dempar + " | awk '{print $2}' "
postx=subprocess.getstatusoutput(call_str)[1]
call_str = "grep 'width:' " + dempar + " | awk '{print $2}' "
width=subprocess.getstatusoutput(call_str)[1]
call_str = "grep 'nlines:' " + dempar + " | awk '{print $2}' "
length=subprocess.getstatusoutput(call_str)[1]
South=str(round(float(North)+(float(length)-1)*float(posty),7))
East=str(round(float(West)+(float(width)-1)*float(postx),7))
call_str = "swap_bytes lv_theta lv_theta.phase_swap 4 >dinsar.log "
os.system(call_str)
call_str = "gmt xyz2grd lv_theta.phase_swap -Glv_theta.grd -Ddegree/degree/cm/1/0/=/= -R" + West + "/" + East + "/" +South + "/" +North + " -I" + postx + " -ZTLf -N0"
os.system(call_str)
call_str = "gmt grdmath 90 lv_theta.grd 3.1415926 DIV 180 MUL SUB = lv_theta_final.grd"
os.system(call_str)
if flag == '0':
call_str = "gmt grdsample lv_theta_final.grd -Glv_theta.grd -I3c"
else:
call_str = "gmt grdsample lv_theta_final.grd -R" + lon_min + "/" + lon_max + "/" + lat_min + "/" + lat_max + " -Glv_theta.grd -I3c"
os.system(call_str)
call_str = "gmt grd2xyz lv_theta.grd -ZTLf -N0 >" +Mdate + '-' + Sdate + '.inc'
os.system(call_str)
call_str = "swap_bytes lv_phi lv_phi.phase_swap 4 >dinsar.log "
os.system(call_str)
call_str = "gmt xyz2grd lv_phi.phase_swap -Glv_phi.grd -Ddegree/degree/cm/1/0/=/= -R" + West + "/" + East + "/" +South + "/" +North + " -I" + postx + " -ZTLf -N0"
os.system(call_str)
call_str = "gmt grdmath -90 lv_phi.grd 3.1415926 DIV 180 MUL SUB = lv_phi_final.grd"
os.system(call_str)
if flag == '0':
call_str = "gmt grdsample lv_phi_final.grd -Glv_phi.grd -I3c"
else:
call_str = "gmt grdsample lv_phi_final.grd -R" + lon_min + "/" + lon_max + "/" + lat_min + "/" + lat_max + " -Glv_phi.grd -I3c"
os.system(call_str)
call_str = "gmt grd2xyz lv_phi.grd -ZTLf -N0 >" +Mdate + '-' + Sdate + '.azi'
os.system(call_str)
call_str = "swap_bytes " + unw+ " unw.phase_swap 4 >dinsar.log "
os.system(call_str)
call_str = "gmt xyz2grd unw.phase_swap -Gunw_f.grd -Ddegree/degree/cm/1/0/=/= -R" + West + "/" + East + "/" +South + "/" +North + " -I" + postx +" -ZTLf -N0"
os.system(call_str)
call_str = "swap_bytes " + los + " los.swap 4 >dinsar.log "
os.system(call_str)
call_str = "gmt xyz2grd los.swap -Glos_f.grd -Ddegree/degree/cm/1/0/=/= -R" + West + "/" + East + "/" +South + "/" +North + " -I" + postx +" -ZTLf -N0"
os.system(call_str)
call_str = "swap_bytes " + cor + " cor.swap 4 >dinsar.log "
os.system(call_str)
call_str = "gmt xyz2grd cor.swap -Gcor_f.grd -Ddegree/degree/cm/1/0/=/= -R" + West + "/" + East + "/" +South + "/" +North + " -I" + postx +" -ZTLf -N0"
os.system(call_str)
if flag == '0':
call_str = "gmt grdsample unw_f.grd -Gunw.grd -I3c"
else:
call_str = "gmt grdsample unw_f.grd -R" + lon_min + "/" + lon_max + "/" + lat_min + "/" + lat_max + " -Gunw.grd -I3c"
os.system(call_str)
call_str = "gmt grdmath 0.0556 unw_f.grd 3.1415926 DIV -4 DIV MUL = los.grd"
os.system(call_str)
call_str = "gmt grd2xyz unw.grd -ZTLf -N0 > " + Mdate + '-' + Sdate + '.unw'
os.system(call_str)
call_str = "swap_bytes " + dem + " dem.phase_swap 4 >dinsar.log "
os.system(call_str)
#call_str = "xyz2grd dem.phase_swap -Gdem_f.grd -Ddegree/degree/cm/1/0/=/= -R" + West + "/" + East + "/" +South + "/" +North + " -I0.001" + " -ZTLf -N0"
call_str = "gmt xyz2grd dem.phase_swap -Gdem_f.grd -Ddegree/degree/cm/1/0/=/= -R" + West + "/" + East + "/" +South + "/" +North + " -I" + postx + " -ZTLf -N0"
os.system(call_str)
if flag == '0':
call_str = "gmt grdsample dem_f.grd -Gdem.grd -I3c"
else:
call_str = "gmt grdsample dem_f.grd -R" + lon_min + "/" + lon_max + "/" + lat_min + "/" + lat_max + " -Gdem.grd -I3c"
os.system(call_str)
call_str = "gmt grd2xyz dem.grd -ZTLf -N0 >" + Mdate + '-' + Sdate + '.utm.dem'
os.system(call_str)
if flag == '0':
call_str = "gmt grdsample los_f.grd -Glos1.grd -I3c"
else:
call_str = "gmt grdsample los_f.grd -R" + lon_min + "/" + lon_max + "/" + lat_min + "/" + lat_max + " -Glos1.grd -I3c"
os.system(call_str)
call_str = "gmt grd2xyz los1.grd -ZTLf -N0 >" + Mdate + '-' + Sdate + '.utm.los'
os.system(call_str)
if flag == '0':
call_str = "gmt grdsample cor_f.grd -Gcor.grd -I3c"
else:
call_str = "gmt grdsample cor_f.grd -R" + lon_min + "/" + lon_max + "/" + lat_min + "/" + lat_max + " -Gcor.grd -I3c"
os.system(call_str)
call_str = "gmt grd2xyz cor.grd -ZTLf -N0 >" + Mdate + '-' + Sdate + '.utm.cor'
os.system(call_str)
call_str = "gmt grdinfo lv_theta.grd -C | awk '{print $10}' "
Width=subprocess.getstatusoutput(call_str)[1]
call_str = "gmt grdinfo lv_theta.grd -C | awk '{print $11}' "
line=subprocess.getstatusoutput(call_str)[1]
call_str = "gmt grdinfo lv_theta.grd -C | awk '{print $2}' "
West=subprocess.getstatusoutput(call_str)[1]
call_str = "gmt grdinfo lv_theta.grd -C | awk '{print $5}' "
North=subprocess.getstatusoutput(call_str)[1]
ymax=str(int(round(float(line)-1 ,7)))
xmax=str(int(round(float(Width)-1 ,7)))
output=workDir + '/' + Mdate + '-' + Sdate + '.unw.rsc'
if os.path.exists(output) is True:
os.remove(output)
fopen=open(output,'a+')
fopen.write('WIDTH ' + Width + '\n')
fopen.write('FILE_LENGTH ' + line + '\n')
fopen.write('XMIN 0' + '\n')
fopen.write('XMAX ' + xmax + '\n')
fopen.write('YMIN 0' + '\n')
fopen.write('YMAX ' + ymax + '\n')
fopen.write('RLOOKS 1' + '\n')
fopen.write('ALOOKS 1' + '\n')
fopen.write('X_FIRST ' + West + '\n')
fopen.write('X_STEP 8.33333333E-04' + '\n')
fopen.write('X_UNIT degrees' + '\n')
fopen.write('Y_FIRST ' + North + '\n')
fopen.write('Y_STEP -8.33333333E-04' + '\n')
fopen.write('Y_UNIT degrees' + '\n')
fopen.write('WAVELENGTH 0.0555041577' + width + '\n')
#fopen.write('WAVELENGTH 0.236' + width + '\n')
fopen.write('MOLDE insar')
fopen.close()
call_str = "swap_bytes " + Mdate + '-' + Sdate + '.unw' + " geo_" + Mdate + '-' + Sdate + "_dem_flat.unw 4 >dinsar.log "
os.system(call_str)
call_str = "swap_bytes " + Mdate + '-' + Sdate + '.utm.dem' + " geo_" + Mdate + '-' + Sdate + "_dem_flat.utm.dem 4 >dinsar.log "
os.system(call_str)
call_str = "cp -f " + Mdate + '-' + Sdate + ".unw.rsc " + Mdate + '-' + Sdate +".inc.rsc"
os.system(call_str)
call_str = "cp -f " + Mdate + '-' + Sdate + ".unw.rsc " + Mdate + '-' + Sdate +".azi.rsc"
os.system(call_str)
call_str = "cp -f " + Mdate + '-' + Sdate + ".unw.rsc " + " geo_" + Mdate + '-' + Sdate + "_dem_flat.unw.rsc"
os.system(call_str)
call_str = "cp -f " + Mdate + '-' + Sdate + ".unw.rsc " + " geo_" + Mdate + '-' + Sdate + "_dem_flat.utm.dem.rsc"
os.system(call_str)
call_str = "rm -rf swap*grd *phase_swap dinsar.log"
os.system(call_str)
print("convert gamma products to psokinv is done!")
sys.exit(1)
if __name__ == '__main__':
main(sys.argv[:])