def main():
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()

    if rank == 0:

        procfile = sys.argv[1].strip()
        para_list = ut.readProc(procfile)
        ice_model_file = para_list.get("ice_model").strip()  ### load model
        ice_model_col = int(para_list.get("ice_model_col").strip())
        grid_size = float(
            para_list.get("grid_size").strip())  ### grid size in m
        r = float(para_list.get("r").strip())  ### density
        v = float(para_list.get("v").strip())  ### Poisson's ratio
        E = int(para_list.get("E").strip())  ### Young's modulus
        E_unit = para_list.get("E_unit").strip()
        if E_unit == 'MPa':
            E = E * (10**6)
        elif E_unit == 'GPa':
            E = E * (10**9)
        else:
            print "Unit of Young's modulus should be either MPa or GPa!\n"
            exit(1)
        print "Young's modulus is: ", E
        angle_az = float(para_list.get("angle_az").strip())
        angle_inc = float(para_list.get("angle_inc").strip())

        ice_model_data = np.fromfile(ice_model_file, dtype=np.float32)
        ice_model_lin = ice_model_data.size / ice_model_col
        dis_model_v = np.zeros((ice_model_data.size), dtype=np.float32)
        #dis_model_e = np.zeros((ice_model_data.size),dtype=np.float32).reshape(ice_model_lin,ice_model_col)
        #dis_model_n = np.zeros((ice_model_data.size),dtype=np.float32).reshape(ice_model_lin,ice_model_col)
        g = 9.81

        vec_col = np.linspace(0, ice_model_col - 1, ice_model_col)
        vec_lin = np.linspace(0, ice_model_lin - 1, ice_model_lin)
        x1, y1 = np.meshgrid(vec_col, vec_lin)
        x2, y2 = x1, y1

        x1 = x1.reshape(x1.size)
        y1 = y1.reshape(y1.size)
        x2 = x2.reshape(ice_model_data.size)[np.abs(ice_model_data) > 0.00001]
        y2 = y2.reshape(ice_model_data.size)[np.abs(ice_model_data) > 0.00001]
        ice_model_data = ice_model_data[np.abs(ice_model_data) > 0.00001]

        const_v = grid_size**2 * r * g * (1 - v**2) / E / np.pi
        const_r = grid_size**2 * r * g * (1 + v) * (1 - 2 * v) / E / 2 / np.pi
        print "2 constants are: ", const_v, " and ", const_r

        dis_model_v_batch = np.array_split(dis_model_v, comm.size)
        dis_model_v = None
        dis_model_e_batch = dis_model_v_batch
        dis_model_n_batch = dis_model_v_batch
        x1_batch = np.array_split(x1, size)
        y1_batch = np.array_split(y1, size)

    else:
        ice_model_data = None
        dis_model_v_batch = None
        dis_model_e_batch = None
        dis_model_n_batch = None
        x2 = None
        y2 = None
        const_v = None
        const_r = None
        x1_batch = None
        y1_batch = None
        grid_size = None

    const_v = comm.bcast(const_v, root=0)
    const_r = comm.bcast(const_r, root=0)
    x2 = comm.bcast(x2, root=0)
    y2 = comm.bcast(y2, root=0)
    grid_size = comm.bcast(grid_size, root=0)
    ice_model_data = comm.bcast(ice_model_data, root=0)
    dis_model_v_batch = comm.scatter(dis_model_v_batch, root=0)
    dis_model_e_batch = comm.scatter(dis_model_e_batch, root=0)
    dis_model_n_batch = comm.scatter(dis_model_n_batch, root=0)
    x1_batch = comm.scatter(x1_batch, root=0)
    y1_batch = comm.scatter(y1_batch, root=0)

    N_pix = len(x1_batch)
    print N_pix, " data received!\n"
    print "Shape of dis_model_v_batch is: ", dis_model_v_batch.shape

    for ni in range(N_pix):
        line1 = y1_batch[ni]
        col1 = x1_batch[ni]
        dis_y = line1 - y2
        dis_x = col1 - x2
        distance = np.power(np.power(dis_y, 2) + np.power(dis_x, 2), 0.5)
        distance[distance < 1] = 10000000.
        sin_ang = dis_x / distance
        cos_ang = -1 * dis_y / distance
        distance = distance * grid_size
        dis_model_v_batch[ni] = np.sum(const_v * (1 / distance) *
                                       ice_model_data)
        dis_model_e_batch[ni] = np.sum(const_r * (1 / distance) *
                                       ice_model_data * sin_ang)
        dis_model_e_batch[ni] = np.sum(const_r * (1 / distance) *
                                       ice_model_data * cos_ang)

    dis_model_v_batch = comm.gather(dis_model_v_batch, root=0)
    dis_model_e_batch = comm.gather(dis_model_e_batch, root=0)
    dis_model_n_batch = comm.gather(dis_model_n_batch, root=0)

    if rank == 0:
        dis_model_v = dis_model_v_batch[0]
        dis_model_e = dis_model_e_batch[0]
        dis_model_n = dis_model_n_batch[0]
        for ni in range(size - 1):
            dis_model_v = np.hstack((dis_model_v, dis_model_v_batch[ni + 1]))
            dis_model_e = np.hstack((dis_model_e, dis_model_e_batch[ni + 1]))
            dis_model_n = np.hstack((dis_model_n, dis_model_n_batch[ni + 1]))

        dis_model_los = dis_model_v * np.cos(angle_inc/180*np.pi) - \
        dis_model_e * np.sin(angle_inc/180*np.pi) * np.cos(angle_az/180*np.pi) + \
        dis_model_n * np.sin(angle_inc/180*np.pi) * np.sin(angle_az/180*np.pi)

        dis_model_los.astype('float32').tofile(
            'elastic_half_space_load_los_model')
def main():
    para_list = ut.readProc(sys.argv[1])
    interf_folder = para_list.get("interf").strip()
    pattern = para_list.get("pattern").strip()
    maskfile = para_list.get("mask").strip()
    th_d = para_list.get("th_d")
    if isinstance(th_d, str):
        th_d = [th_d]
    for ni in range(len(th_d)):
        th_d[ni] = th_d[ni].strip()
    th_c = para_list.get("th_c").strip()
    iteration = len(th_d)
    method = para_list.get("method").strip()
    solution = para_list.get("solution").strip()
    plane_type = para_list.get("plane_type").strip()
    APS_type = para_list.get("APS_type").strip()
    demfile = para_list.get("dem").strip()
    step = para_list.get("step")
    if isinstance(step, str):
        step = [step]
    for ni in range(len(step)):
        step[ni] = step[ni].strip()
    if para_list.get("software")=="RSMAS":
        int_list = glob.glob(interf_folder+"/PO*")
    else:
        int_list = glob.glob(interf_folder+"/int_*")
    
    N_line = 0
    for f in int_list:
        print "f is: ", f, "\n" 
        intfile = glob.glob(f+"/*"+pattern)[0]
        intfile_rsc = intfile + ".rsc"
        para_list1 = ut.readRsc(intfile_rsc)
        temp_line = int(para_list1.get("FILE_LENGTH").strip())
        if N_line < temp_line:
            N_line = temp_line
    print "maskfile is: ",bool(maskfile=='1'), "\n"
    if maskfile == '1':
        print "The maximum line is: ", N_line, "\n"
    else:
        Joblist = interf_folder + "/run_remove_ramp_aps"
        for ni in range(iteration):
            if ni < 2:
                method = "0"
            else:
                method = "1"
            k = 1
            IN = open(Joblist, 'w')
            for f in int_list:
                intfile = glob.glob(f+"/*"+pattern)[0]
                corfile = intfile[:-3]+"cor"
                if ni>5:
                    APS_type == "2"
                if ni % 2 == 0:
                    temp_str = "/nethome/wlzhao/python_code/remove_aps_wrap.py " + intfile + " " + corfile + " " + maskfile + " " + demfile + \
                               " " + plane_type + " " + "0" + " " + th_d[ni] + " " + th_c + " " + method + " " + \
                               solution + " " + str(ni+1) + " " + step[ni]
                else:
                    temp_str = "/nethome/wlzhao/python_code/remove_aps_wrap.py " + intfile + " " + corfile + " " + maskfile + " " + demfile + \
                               " " + "0" + " " + APS_type + " " + th_d[ni] + " " + th_c + " " + method + " " + \
                               solution + " " + str(2) + " " + step[ni]
                IN.write(temp_str)
                if k<len(int_list):
                    IN.write("\n")
                k = k+1
            IN.close() 

            WT = "0:30"
            temp_str = os.getenv("INT_SCR").strip() + "/createBatch_8.pl --workdir " + interf_folder + " --infile " + Joblist + " walltime=" + WT
            temp_list = [os.getenv("INT_SCR").strip()+"/createBatch_8.pl","--workdir",interf_folder,"--infile",Joblist,"walltime="+WT]
            sys.stderr.write(temp_str+"\n")
            output = subprocess.Popen(temp_list).wait()
            if output == 0:
                print "Iteration ", str(ni+1), " has been done!\n"
            else:
                print "There is error happened. Check code again!\n"
                exit(1)
            
        print "All the jobs are done! Good luck!\n"
        exit(0)
Example #3
0
'''
Created on Sun Aug 17 18:14:13 2014

code used for layered elastic model of surface loading

Wenliang Zhao
'''

import os, sys, re, glob, subprocess, math, time
import Rsmas_Utilib as ut
import ElasticSAR as es
import numpy as np
from osgeo import gdal
import time

para_list = ut.readProc(sys.argv[1])
processdir = para_list.get("processdir").strip()
disp_dir = processdir + "/InSAR"
line_insar = int(para_list.get("line_insar").strip())
col_insar = int(para_list.get("col_insar").strip())
left_lon_insar = float(para_list.get("left_lon_insar").strip())
top_lat_insar = float(para_list.get("top_lat_insar").strip())
res_lon_insar = float(para_list.get("res_lon_insar").strip())
res_lat_insar = float(para_list.get("res_lat_insar").strip())
lookAng_insar = float(para_list.get("lookAng").strip())
azimuth_insar = float(para_list.get("azimuth").strip())
utmZone = para_list.get("utmZone").strip()
res_x_utm = int(para_list.get("spacing").strip())
res_y_utm = res_x_utm
box = para_list.get("box")
method = para_list.get("method").strip()
def main():
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()
    
    if rank == 0:

        procfile = sys.argv[1].strip()
        para_list = ut.readProc(procfile)
        ice_model_file = para_list.get("ice_model").strip()    ### load model
        ice_model_col = int(para_list.get("ice_model_col").strip())
        grid_size = float(para_list.get("grid_size").strip())  ### grid size in m
        r = float(para_list.get("r").strip())     ### density
        v = float(para_list.get("v").strip())     ### Poisson's ratio
        E = int(para_list.get("E").strip())       ### Young's modulus
        E_unit = para_list.get("E_unit").strip()
        if E_unit == 'MPa':
            E = E * (10 ** 6)
        elif E_unit == 'GPa':
            E = E * (10 ** 9)
        else:
            print "Unit of Young's modulus should be either MPa or GPa!\n"
            exit(1)
        print "Young's modulus is: ", E
        angle_az = float(para_list.get("angle_az").strip())
        angle_inc = float(para_list.get("angle_inc").strip())

        ice_model_data = np.fromfile(ice_model_file,dtype=np.float32)
        ice_model_lin = ice_model_data.size / ice_model_col
        dis_model_v = np.zeros((ice_model_data.size),dtype=np.float32)
        #dis_model_e = np.zeros((ice_model_data.size),dtype=np.float32).reshape(ice_model_lin,ice_model_col)
        #dis_model_n = np.zeros((ice_model_data.size),dtype=np.float32).reshape(ice_model_lin,ice_model_col)
        g = 9.81

        vec_col = np.linspace(0,ice_model_col-1, ice_model_col)
        vec_lin = np.linspace(0,ice_model_lin-1, ice_model_lin)
        x1, y1 = np.meshgrid(vec_col, vec_lin)
        x2, y2 = x1, y1

        x1 = x1.reshape(x1.size)
        y1 = y1.reshape(y1.size)
        x2 = x2.reshape(ice_model_data.size)[np.abs(ice_model_data)>0.00001]
        y2 = y2.reshape(ice_model_data.size)[np.abs(ice_model_data)>0.00001]
        ice_model_data = ice_model_data[np.abs(ice_model_data)>0.00001]

        const_v = grid_size ** 2 * r * g * (1-v**2) / E / np.pi
        const_r = grid_size ** 2 * r * g * (1+v) * (1-2*v) / E / 2 / np.pi
        print "2 constants are: ", const_v, " and ", const_r

        dis_model_v_batch = np.array_split(dis_model_v,comm.size)
        dis_model_v = None
        dis_model_e_batch = dis_model_v_batch
        dis_model_n_batch = dis_model_v_batch
        x1_batch = np.array_split(x1,size)
        y1_batch = np.array_split(y1,size)
    
    else:
        ice_model_data = None
        dis_model_v_batch = None
        dis_model_e_batch = None
        dis_model_n_batch = None
        x2 = None
        y2 = None
        const_v = None
        const_r = None
        x1_batch = None
        y1_batch = None
        grid_size = None
    
    const_v = comm.bcast(const_v, root=0)
    const_r = comm.bcast(const_r, root=0)
    x2 = comm.bcast(x2, root=0)
    y2 = comm.bcast(y2, root=0)
    grid_size = comm.bcast(grid_size, root=0)
    ice_model_data = comm.bcast(ice_model_data, root=0)
    dis_model_v_batch = comm.scatter(dis_model_v_batch, root=0)
    dis_model_e_batch = comm.scatter(dis_model_e_batch, root=0)
    dis_model_n_batch = comm.scatter(dis_model_n_batch, root=0)
    x1_batch = comm.scatter(x1_batch, root=0)
    y1_batch = comm.scatter(y1_batch, root=0)
    
    N_pix = len(x1_batch)
    print N_pix, " data received!\n"
    print "Shape of dis_model_v_batch is: ", dis_model_v_batch.shape
    
    
    
    for ni in range(N_pix):
        line1 = y1_batch[ni]
        col1 = x1_batch[ni]
        dis_y = line1 - y2
        dis_x = col1 - x2
        distance = np.power(np.power(dis_y,2) + np.power(dis_x,2), 0.5)
        distance[distance<1] = 10000000.
        sin_ang = dis_x / distance
        cos_ang = -1 * dis_y / distance
        distance = distance * grid_size
        dis_model_v_batch[ni] = np.sum(const_v * (1 / distance) * ice_model_data)
        dis_model_e_batch[ni] = np.sum(const_r * (1 /distance) * ice_model_data * sin_ang)
        dis_model_e_batch[ni] = np.sum(const_r * (1 /distance) * ice_model_data * cos_ang)
    
    
    dis_model_v_batch = comm.gather(dis_model_v_batch, root=0)
    dis_model_e_batch = comm.gather(dis_model_e_batch, root=0)
    dis_model_n_batch = comm.gather(dis_model_n_batch, root=0)
    
    if rank == 0:
        dis_model_v = dis_model_v_batch[0]
        dis_model_e = dis_model_e_batch[0]
        dis_model_n = dis_model_n_batch[0]
        for ni in range(size-1):
            dis_model_v = np.hstack((dis_model_v, dis_model_v_batch[ni+1]))
            dis_model_e = np.hstack((dis_model_e, dis_model_e_batch[ni+1]))
            dis_model_n = np.hstack((dis_model_n, dis_model_n_batch[ni+1]))
         
        dis_model_los = dis_model_v * np.cos(angle_inc/180*np.pi) - \
        dis_model_e * np.sin(angle_inc/180*np.pi) * np.cos(angle_az/180*np.pi) + \
        dis_model_n * np.sin(angle_inc/180*np.pi) * np.sin(angle_az/180*np.pi)
        
        dis_model_los.astype('float32').tofile('elastic_half_space_load_los_model')
import os
import re
import sys
import Rsmas_Utilib as ut
import glob
import subprocess

if len(sys.argv) != 2:
    print '''
    Usage: sparse_snaphu.py *.template    
    template must include path to interferogram
    
'''
    exit(0)

para_list = ut.readProc(sys.argv[1])
int_str = para_list.get("unwrap_str")
#snaphu_def = os.getenv("INT_SCR").strip() + "/snaphu.default"
#para_snaphu1 = ut.readProc(snaphu_def)
if not para_list.get("method"):
    method = "RSMAS"
else:
    method = para_list.get("method").strip()
if method == "RSMAS":
    processdir = os.getenv("PROCESSDIR")
    #WDIR = processdir
    processdir = processdir + "/" + sys.argv[1].partition('.')[0].strip()
    folder_list = glob.glob(processdir + "/PO*")
elif method == "NSBAS":
    processdir = para_list.get("processdir").strip()
    #WDIR = processdir
def main():
    para_list = ut.readProc(sys.argv[1])
    interf_folder = para_list.get("interf").strip()
    pattern = para_list.get("pattern").strip()
    maskfile = para_list.get("mask").strip()
    th_d = para_list.get("th_d")
    if isinstance(th_d, str):
        th_d = [th_d]
    for ni in range(len(th_d)):
        th_d[ni] = th_d[ni].strip()
    th_c = para_list.get("th_c").strip()
    iteration = len(th_d)
    method = para_list.get("method").strip()
    solution = para_list.get("solution").strip()
    plane_type = para_list.get("plane_type").strip()
    APS_type = para_list.get("APS_type").strip()
    demfile = para_list.get("dem").strip()
    step = para_list.get("step")
    if isinstance(step, str):
        step = [step]
    for ni in range(len(step)):
        step[ni] = step[ni].strip()
    if para_list.get("software") == "RSMAS":
        int_list = glob.glob(interf_folder + "/PO*")
    else:
        int_list = glob.glob(interf_folder + "/int_*")

    N_line = 0
    for f in int_list:
        print "f is: ", f, "\n"
        intfile = glob.glob(f + "/*" + pattern)[0]
        intfile_rsc = intfile + ".rsc"
        para_list1 = ut.readRsc(intfile_rsc)
        temp_line = int(para_list1.get("FILE_LENGTH").strip())
        if N_line < temp_line:
            N_line = temp_line
    print "maskfile is: ", bool(maskfile == '1'), "\n"
    if maskfile == '1':
        print "The maximum line is: ", N_line, "\n"
    else:
        Joblist = interf_folder + "/run_remove_ramp_aps"
        for ni in range(iteration):
            if ni < 2:
                method = "0"
            else:
                method = "1"
            k = 1
            IN = open(Joblist, 'w')
            for f in int_list:
                intfile = glob.glob(f + "/*" + pattern)[0]
                corfile = intfile[:-3] + "cor"
                if ni > 5:
                    APS_type == "2"
                if ni % 2 == 0:
                    temp_str = "/nethome/wlzhao/python_code/remove_aps_wrap.py " + intfile + " " + corfile + " " + maskfile + " " + demfile + \
                               " " + plane_type + " " + "0" + " " + th_d[ni] + " " + th_c + " " + method + " " + \
                               solution + " " + str(ni+1) + " " + step[ni]
                else:
                    temp_str = "/nethome/wlzhao/python_code/remove_aps_wrap.py " + intfile + " " + corfile + " " + maskfile + " " + demfile + \
                               " " + "0" + " " + APS_type + " " + th_d[ni] + " " + th_c + " " + method + " " + \
                               solution + " " + str(2) + " " + step[ni]
                IN.write(temp_str)
                if k < len(int_list):
                    IN.write("\n")
                k = k + 1
            IN.close()

            WT = "0:30"
            temp_str = os.getenv("INT_SCR").strip(
            ) + "/createBatch_8.pl --workdir " + interf_folder + " --infile " + Joblist + " walltime=" + WT
            temp_list = [
                os.getenv("INT_SCR").strip() + "/createBatch_8.pl",
                "--workdir", interf_folder, "--infile", Joblist,
                "walltime=" + WT
            ]
            sys.stderr.write(temp_str + "\n")
            output = subprocess.Popen(temp_list).wait()
            if output == 0:
                print "Iteration ", str(ni + 1), " has been done!\n"
            else:
                print "There is error happened. Check code again!\n"
                exit(1)

        print "All the jobs are done! Good luck!\n"
        exit(0)
import os
import re
import sys
import Rsmas_Utilib as ut
import glob
import subprocess

if len(sys.argv) != 2:
    print '''
    Usage: sparse_snaphu.py *.template    
    template must include path to interferogram
    
'''
    exit(0)
    
para_list = ut.readProc(sys.argv[1])
int_str = para_list.get("unwrap_str")
#snaphu_def = os.getenv("INT_SCR").strip() + "/snaphu.default"
#para_snaphu1 = ut.readProc(snaphu_def)
if not para_list.get("method"):
    method = "RSMAS"
else:
    method = para_list.get("method").strip()
if method == "RSMAS":
    processdir = os.getenv("PROCESSDIR")
    #WDIR = processdir
    processdir = processdir + "/" + sys.argv[1].partition('.')[0].strip()
    folder_list = glob.glob(processdir+"/PO*")
elif method == "NSBAS":
    processdir = para_list.get("processdir").strip()
    #WDIR = processdir
Example #8
0
import glob


if len(sys.argv) != 2:
    print '''
    Usage: sparse_snaphu.py int_folder  
    template must include path to interferogram
    
'''
    exit(0)

int_folder = sys.argv[1].strip()
os.system("cd " + int_folder)
procfile = int_folder + "/sparse_para"
procfile1 = int_folder + "/snaphu_para"
para_list = ut.readProc(procfile)
para_list1 = ut.readProc(procfile1)

if para_list.get("interferogram"):
    intfile = para_list.get("interferogram").strip() 
else:
    print "Must have path to interferogram!\n"
    exit(0)   

if para_list.get("coherence"):
    corfile = para_list.get("coherence").strip()
else:
    print "Must have path to coherence!\n"
    exit(0)

Example #9
0
def main():
    
    global th_d, l_M, w_M, method, line1, col1, step
            
    
    intfile = sys.argv[1].strip()
    corfile = sys.argv[2].strip()
    maskfile = sys.argv[3].strip()
    demfile = sys.argv[4].strip()
    plane_type = int(sys.argv[5].strip())
    APS_type = int(sys.argv[6].strip())
    th_d = float(sys.argv[7].strip())
    th_c = float(sys.argv[8].strip())
    method = int(sys.argv[9].strip())
    solution = int(sys.argv[10].strip())
    iteration = int(sys.argv[11].strip())
    step = int(sys.argv[12].strip())
    print "All the parameters obtained!\n"
    print "Iteration is: ", str(iteration),"\n"
    ### read in mask file
    print "Reading maskfile\n"
    [phase_M, rscContent_M,l_M,w_M] = ut.read_float(maskfile)

    ### read in intfile corfile and demfile
    print "Reading interferogram and coherence!\n"
    print "intfile is: ", intfile
    N_looks = re.findall('\d+rlks',intfile)[0].strip()
    if len(N_looks) == 5:
        newName = intfile[:-9] + "ramp_aps_" + intfile[-9:]
    elif len(N_looks) == 6:
        newName = intfile[:-10] + "ramp_aps_" + intfile[-10:]
    if iteration != 1:
        intfile = newName
    
 
    [amplitude_int, phase_int, rscContent_int,l_int_temp,w_int] = ut.readInt(intfile)
    if l_M>l_int_temp:
        temp_array = np.zeros((l_M-l_int_temp,w_M),dtype=np.float)
    	phase_int = np.vstack((phase_int,temp_array))
    print "Coherence file is: ", corfile, "\n"
    [amplitude_cor, phase_cor, rscContent_cor,l_cor,w_cor] = ut.read_bifloat(corfile)
    print "Coherence size is: ", phase_cor.shape, "\n"
    if l_M>l_cor:
        temp_array = np.zeros((l_M-l_cor,w_M),dtype=np.float)
        phase_cor = np.vstack((phase_cor,temp_array))
    if l_M<l_cor:
        phase_cor = phase_cor[:l_M,:]
    #[phase_dem, rscContent_dem,l_dem,w_dem] = ut.read_float(demfile)
    [phase_dem, rscContent_dem,l_dem,w_dem] = ut.readDEM(demfile)
    if l_M>l_dem:
        temp_array = np.zeros((l_M-l_dem,w_M),dtype=np.float)
        phase_dem = np.vstack((phase_dem,temp_array))
      
    phase_temp = np.copy(phase_int)
    folder = os.path.dirname(intfile)
    os.chdir(folder)
    temp_dir = os.getcwd()
    print "Current dir is: ", temp_dir,"\n"
    temp_rsc = "temp.int.rsc"
    rscContent_int["FILE_LENGTH"] = str(l_M)
    IN = open(temp_rsc, 'w')
    for k,v in rscContent_int.iteritems():
        IN.write(k.ljust(41)+v+"\n")
    IN.close()

    temp_list = ["cp","-f",intfile+".rsc", "temp.int.rsc"]
    print temp_list, "\n"
    output = subprocess.Popen(temp_list).wait()
    temp_list = ["SWfilter_casc",intfile,corfile,"temp.int",str(w_M),str(l_int_temp),"0.01"]
    print temp_list, "\n"
    output = subprocess.Popen(temp_list).wait()
    if output == 0:
        print "success!\n"
    else:
        print "failed!\n"
        exit(0)
    intfile = "temp.int"
    [amplitude_int, phase_int, rscContent_int,l_int,w_int] = ut.readInt(intfile)
    if l_M>l_int:
        temp_array = np.zeros((l_M-l_int,w_M),dtype=np.float)
        phase_int = np.vstack((phase_int,temp_array))    
    if l_M<l_int:
        phase_int = phase_int[:l_M,:]

    ### mask phase and reshape files
    inData = np.copy(phase_int)
    phase_int = None
    inData[np.isnan(phase_M)] = np.nan
    inData[phase_cor<th_c] = np.nan
    inData[inData==0] = np.nan
    inData = inData.reshape(l_M,w_M)
    #phase_cor = None
    phase_dem = phase_dem.reshape(l_M,w_M)



    ### differentiation in range and azimuth
    line1 = l_M - step 
    col1 = w_M - step
    temp_x = np.linspace(0, w_M, w_M)
    temp_y = np.linspace(0, l_M, l_M)
    ran, azi = np.meshgrid(temp_x,temp_y)

    data1 = inData[:,step:] - inData[:,:(-1*step)]
    data11 = data1[:,step:] - data1[:,:(-1*step)]
    data2 = inData[step:,:] - inData[:(-1*step),:]
    data_r = np.vstack((data1.reshape(l_M*col1,1),data2.reshape(line1*w_M,1)))
    data_r[np.abs(data_r)>th_d] = np.nan
    length_r = data_r.size

    ran1 = ran[:,step:] - ran[:,:(-1*step)]
    ran2 = ran[step:,:] - ran[:(-1*step),:]
    range_t = ran.reshape(l_M*w_M,1)

    azi1 = azi[:,step:] - azi[:,:(-1*step)]
    azi2 = azi[step:,:] - azi[:(-1*step),:]
    azimuth_t = azi.reshape(l_M*w_M,1)
    azi2_sum = azi[step:,:] + azi[:(-1*step),:]

    phase_dem = phase_dem.astype('float')
    phase_dem[np.abs(phase_dem)>10000] = np.nan
    phase_dem[np.abs(phase_dem)<0.001] = np.nan
    dem1 = phase_dem[:,step:] - phase_dem[:,:(-1*step)]
    dem1[np.abs(dem1)>800] = np.nan
    #dem11 = dem1[:,step1:] - dem1[:,:(-1*step1)]
    #dem11[np.abs(dem11)>800] = np.nan
    #data11[np.isnan(dem11)] = np.nan
    #data11[np.abs(data11)>th_d] = np.nan
    #temp = data11 / dem11
    #temp[np.isinf(temp)] = np.nan
    #temp[np.abs(temp)>0.1] = np.nan
    #rate2 = np.mean(temp[~np.isnan(temp)])
    #data11 = None
    #dem11 = None
    #temp = None
    dem2 = phase_dem[step:,:] - phase_dem[:(-1*step),:]
    dem2[np.abs(dem2)>800] = np.nan    

    height = phase_dem.reshape(l_M*w_M,1)
    dem1_sum = phase_dem[:,step:] + phase_dem[:,:(-1*step)]
    dem2_sum = phase_dem[step:,:] + phase_dem[:(-1*step),:];
    dem_r = np.vstack((dem1.reshape(l_M*col1,1),dem2.reshape(line1*w_M,1)))
    mask1 = np.ones((length_r,1))
    mask1[np.isnan(data_r)] = np.nan
    mask1[np.isnan(dem_r)] = np.nan
    data_r[np.isnan(mask1)] = np.nan

    
    #### initial original design matrix
    if plane_type == 1:
        POINTS_ori = np.copy(range_t)
        if APS_type == 1:
            POINTS_ori = np.hstack((POINTS_ori,height))
        elif APS_type == 2:
            POINTS_ori = np.hstack((POINTS_ori,height))
            POINTS_ori = np.hstack((POINTS_ori,np.power(height,2)))
    elif plane_type == 2:
        POINTS_ori = np.copy(range_t)
        POINTS_ori = np.hstack((POINTS_ori,azimuth_t))
        if APS_type == 1:
            POINTS_ori = np.hstack((POINTS_ori,height))
        elif APS_type == 2:
            POINTS_ori = np.hstack((POINTS_ori,height))
            POINTS_ori = np.hstack((POINTS_ori,np.power(height,2)))
    elif plane_type == 3:
        POINTS_ori = np.copy(range_t)
        POINTS_ori = np.hstack((POINTS_ori,azimuth_t))
        POINTS_ori = np.hstack((POINTS_ori,np.power(azimuth_t,2)))
        if APS_type == 1:
            POINTS_ori = np.hstack((POINTS_ori,height))
        elif APS_type == 2:
            POINTS_ori = np.hstack((POINTS_ori,height))
            POINTS_ori = np.hstack((POINTS_ori,np.power(height,2)))
    else:
        if APS_type == 1:
            POINTS_ori = np.copy(height)
        elif APS_type == 2:
            POINTS_ori = np.copy(height)
            POINTS_ori = np.hstack((POINTS_ori,np.power(height,2)))

    azimuth_t = None
    range_t = None
    ran = None
    azi = None
    height = None

    ### initial design matrix
    if solution == 1:
        if plane_type == 1:
            POINTS = ran1.reshape((l_M*col1,1))
            data_r_temp = np.copy(data_r_temp[:ran1.size])
            mask1_temp = np.copy(mask1[:ran1.size])
            if APS_type == 1:
                POINTS = np.hstack((POINTS,dem1.reshape(l_M*col1,1)))
            elif APS_type == 2:
                POINTS = np.hstack((POINTS,dem1.reshape(l_M*col1,1)))
                POINTS = np.stack((POINTS,(dem1*dem1_sum).reshape(l_M*col1,1)))
            plane,APS,par = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,solution)
        elif plane_type == 2:
            POINTS = np.copy(ran1.reshape((l_M*col1,1)))
            data_r_temp = np.copy(data_r[:ran1.size])
            mask1_temp = np.copy(mask1[:ran1.size])
            if APS_type == 1:
                POINTS = np.hstack((POINTS,dem1.reshape(l_M*col1,1)))
            elif APS_type == 2:
                POINTS = np.hstack((POINTS,dem1.reshape(l_M*col1,1)))
                POINTS = np.hstack((POINTS,(dem1*dem1_sum).reshape(l_M*col1,1)))
            if POINTS[0,:].size > 2:
                plane1,APS,par1 = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)
            else:
                if method == 0:
                    par1 = np.mean(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                else:
                    par1 = np.median(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                plane1 = POINTS_ori[:,0] * par1
                APS = np.array([])
            if POINTS[0,:].size > 2:
                APS = APS.reshape(l_M,w_l)
                TT2 = APS[step:,:] - APS[:(-1*step),:]
                data_r_temp = data_r[ran1.size:] - TT2.reshape(line1*w_M,1)
                data_r_temp[np.abs(data_r_temp)>th_d] = np.nan
                if method == 0:
                    par2 = np.mean(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                else:
                    par2 = np.median(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                plane2 = POINTS_ori[:,1] * par2
                par = np.array([[par1[0]],[par2],[par1[1:]]])
            else:
                data_r_temp = np.copy(data_r[ran1.size:])
                if method == 0:
                    par2 = np.mean(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                else:
                    par2 = np.median(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
            plane2 = POINTS_ori[:,1] * par2
            par = np.array([[par1],[par2]])
            plane = (plane1 + plane2).reshape(l_M,w_M)

        elif plane_type == 3:
            POINTS = np.copy(ran1.reshape(l_M*col1,1))
            data_r_temp = np.copy(data_r)
            data_r_temp = np.copy(data_r_temp[:ran1.size])
            mask1_temp = np.copy(mask1[:ran1.size])
            if APS_type == 1:
                POINTS = np.hstack((POINTS,dem1.reshape(l_M,col1)))
            elif APS_type == 2:
                POINTS = np.hstack((POINTS,dem1.reshape(l_M,col1)))
                POINTS = np.hstack((POINTS,(dem1*dem1_sum).reshape(l_M,col1)))
            plane_type = 4
            plane1,APS1,par1 = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)
        
            POINTS = np.copy(azi2.reshape(line1*w_M,1))
            POINTS = np.hstack((POINTS,(azi2*azi2_sum).reshape(line1*w_M,1)))
            data_r_temp = np.copy(data_r[ran1.size:])
            mask1_temp = np.copy(mask1[ran1.size:])
            if APS_type == 1:
                POINTS = np.hstack((POINTS,dem2.reshape(line1*w_M,1)))
            elif APS_type == 2:
                POINTS = np.hstack((POINTS,dem2.reshape(line1*w_M,1)))
                POINTS = np.hstack((POINTS,(dem2*dem2_sum).reshape(line1*w_M,1)))

            plane2,APS2,par2 = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)
    
            if APS_type == 1:
                par = np.array([[par1[0]],[par2[0]],[par2[1]],[(par1[1]+par2[2])/2]])
                plane = (plane1 + plane2).reshape(l_M,w_M)
                APS = ((APS1 + APS2) / 2).reshape(l_M,w_M)
            elif APS_type == 2:
                par = np.array([[par1[0]],[par2[0] ],[par2[1]],[(par1[1]+par2[2])/2],[(par1[2]+par2[3])/2]])
                plane = (plane1 + plane2).reshape(l_M,w_M)
                APS = ((APS1 + APS2) / 2).reshape(l_M,w_M)
            else:
                par = np.array([[par1],[par2]])
                plane = (plane1 + plane2).reshape(l_M,w_M)
                APS = np.array([])
         
    elif solution == 2:
        if plane_type == 1:
            data_r_temp = np.copy(data_r)
            mask1_temp = np.copy(mask1)
            if APS_type == 1:
                POINTS = np.vstack((dem1.reshape(l_M*col1,1),dem2.reshape(line1*w_M,1)))
            elif APS_type == 2:
                POINTS = np.vstack((dem1.reshape(l_M*col1,1),dem2.reshape(line1*w_M,1)))
                POINTS = np.hstack((POINTS,np.vstack(((dem1*dem1_sum).reshape(l_M*col1,1),(dem2*dem2_sum).reshape(line1*w_M,1)))))

            if APS_type > 0:
                plane,APS,par = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)
                plane = plane.reshape(l_M,w_M)
            else:
                if method == 0:
                    par = np.mean(data_r[:ran1.size]/step)
                    plane = POINTS_ori[:,0] * par
                    APS = np.array([])
                else:
                    par = np.median(data_r[:ran1.size]/step)
                    plane = (POINTS_ori[:,0] * par).reshape(l_M,w_M)
                    APS = np.array([])

        elif plane_type == 2:
            data_r_temp = np.copy(data_r)
            mask1_temp = np.copy(mask1)
            if APS_type == 1:
                POINTS = np.vstack((dem1.reshape(l_M*col1,1),dem2.reshape(line1*w_M,1)))
            elif APS_type == 2:
                POINTS = np.vstack((dem1.reshape(l_M*col1,1),dem2.reshape(line1*w_M,1)))
                POINTS = np.hstack((POINTS,np.vstack(((dem1*dem1_sum).reshape(l_M*col1,1),(dem2*dem2_sum).reshape(line1*w_M,1)))))
            if APS_type > 0:
                print "size of POINTS: ", POINTS.size, ",data", data_r_temp.size,",mask", mask1_temp.size, ",plane_type",plane_type,",APS_type",APS_type,",solution",solution,"\n" 
                plane1,APS,par1 = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)
            else:
                data_r_temp = data_r_temp[:ran1.size]
                if method == 0:
                    par1 = np.mean(data_r_temp[~np.isnan(data_r_temp)]/step)
                else:
                    par1 = np.median(data_r_temp[~np.isnan(data_r_temp)]/step)
                print "par1 is: ", par1, "\n"
                plane1 = POINTS_ori[:,0] * par1
                APS = np.array([])

            if APS_type > 0:
                APS = APS.reshape(l_M,w_M)
                TT2 = APS[step:,:] - APS[:(-1*step),:]
                data_r_temp = data_r[ran1.size:] - TT2.reshape(line1*w_M,1)
                data_r_temp[np.abs(data_r_temp)>th_d] = np.nan    
                if method == 0:
                    par2 = np.mean(data_r_temp[~np.isnan(data_r_temp)]/step)
                else:
                    par2 = np.median(data_r_temp[~np.isnan(data_r_temp)]/step)
                plane2 = POINTS_ori[:,1] * par2
                print "term1 is: ", par1[0],", term2 is: ", par2, ", term3 is: ", par1[1:],"\n"
                par = np.array([[par1[0]],[par2],[par1[1:]]])
            else:
                data_r_temp = np.copy(data_r[ran1.size:]) 
                if method == 0:
                    par2 = np.mean(data_r_temp[~np.isnan(data_r_temp)]/step)
                else:
                    par2 = np.median(data_r_temp[~np.isnan(data_r_temp)]/step)
                print "par2 is: ", par2,"\n"
                plane2 = POINTS_ori[:,1] * par2
                par = np.array([[par1],[par2]])
            plane = plane1 + plane2

        elif plane_type == 3:
            data_r_temp = np.copy(data_r)
            mask1_temp = np.copy(mask1)
            if APS_type == 1:
                POINTS = np.copy(dem1.reshape(l_M,col1))
            elif APS_type == 2:
                POINTS = np.copy(dem1.reshape(l_M,col1))
                POINTS = np.hstack((POINTS,(dem1*dem1_sum).reshape(l_M,col1)))
        
            if APS_type > 0:
                plane1,APS,par1 = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)
                APS = APS.reshape(l_M,w_M)
                TT2 = APS[1:,:] - APS[0:-1,:]
                data_r_temp = np.copy(data_r[ran1.size:])
                POINTS = (azi2*azi2_sum).reshape(line1*w_M,1)
                APS_type = 0
                plane2,APS2,par2 = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)
                par = np.array[[par1[0]],[par2],[par1[1]]]
            else:
                data_r_temp = np.copy(data_r[:ran1.size])
                if method == 0:
                    par1 = np.mean(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                else:
                    par1 = np.median(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                plane1 = POINTS_ori[:,0] * par1            
                APS = np.array([])
                data_r_temp = np.copy(data_r[ran1.size:])
                if method == 0:
                    par2 = np.mean(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                else:
                    par2 = np.median(data_r_temp[np.invert(np.isnan(data_r_temp))]/step)
                plane2 = POINTS_ori[:,0] * par2
                par = np.array[[par1],[par2]]
  
            plane = (plane1 + plane2).reshape(l_M,w_M)
 
        elif plane_type == 0:
            data_r_temp = np.copy(data_r)
            mask1_temp = np.copy(mask1)
            if APS_type == 1:
                POINTS = np.vstack((dem1.reshape(l_M*col1,1),dem2.reshape(line1*w_M,1)))
            elif APS_type == 2:
                POINTS = np.vstack((dem1.reshape(l_M*col1,1),dem2.reshape(line1*w_M,1)))
                POINTS = np.hstack((POINTS,np.vstack(((dem1*dem1_sum).reshape(l_M*col1,1),(dem2*dem2_sum).reshape(line1*w_M,1)))))
            plane,APS,par = inverse_mat(POINTS,data_r_temp,mask1_temp,POINTS_ori,plane_type,APS_type,solution)


    ##### finalize the results
    TT = np.zeros((l_M,w_M))
    if plane.size:
        plane = plane.reshape(l_M,w_M)
        TT = TT + plane
    if APS.size:
        APS = APS.reshape(l_M,w_M)
        TT = TT + APS
    ramp_file = "temp_ramp"+N_looks+".unw"
    if os.path.isfile(ramp_file):
        [amplitude_ramp, phase_ramp, rscContent_ramp,l_ramp,w_ramp] = ut.read_bifloat(ramp_file)
        phase_ramp = phase_ramp + TT[:l_int_temp,:]
        temp_array = np.zeros((l_ramp*2,w_ramp),dtype=np.float32)
        temp_array[::2] = amplitude_ramp
        temp_array[1::2] = phase_ramp
        temp_array.astype('float32').tofile(ramp_file)
        temp_array = None 
    else:
        amplitude_ramp = amplitude_int
        phase_ramp = np.copy(TT[:l_int_temp,:])
        temp_array = np.zeros((l_int_temp*2,w_int),dtype=np.float32)
        temp_array[::2] = amplitude_ramp
        temp_array[1::2] = phase_ramp
        temp_array.astype('float32').tofile(ramp_file)
        temp_array = None
 
    TT = np.fmod(TT,2*np.pi)
    TT[TT<(-1*np.pi)] = TT[TT<(-1*np.pi)] + np.pi*2
    TT[TT>np.pi] = TT[TT>np.pi] - np.pi*2
    phase_temp[np.where(phase_temp==0)] = np.nan
    phase = np.fmod(phase_temp-TT,2*np.pi)
    phase[phase<(-1*np.pi)] = phase[phase<(-1*np.pi)] + np.pi*2
    phase[phase>np.pi] = phase[phase>np.pi] - np.pi*2
    phase = phase[:l_int_temp,:]
    phase[np.isnan(phase)] = 0
    #im = plt.imshow(phase,cmap=plt.cm.jet, vmin=-3.14, vmax=3.14)
    #plt.show()
    if l_M>l_int_temp:
        phase_cor = phase_cor[:l_int_temp,:]
        temp_array = np.zeros((l_int_temp*2,w_int))
        temp_array[::2,:] = amplitude_int
        print "Size of phase_cor is: ", phase_cor.shape, ", Size of temp_array is: ", temp_array.shape, "\n"
        temp_array[1::2,:]   = np.copy(phase_cor[:l_int_temp,:]) 
        rscContent_cor["FILE_LENGTH"] = str(l_int_temp)
        corfile_rsc = corfile + ".rsc"
        os.system("rm -f "+corfile_rsc)
        IN = open(corfile_rsc, 'w')   
        for k,v in rscContent_cor.iteritems():
            IN.write(k.ljust(41)+v+"\n")
        IN.close()
        os.system("rm -f "+corfile)
        temp_array.astype('float32').tofile(corfile)

    elif l_M<l_int_temp:
        temp = np.zeros((l_int_temp-l_M,w_int),np.float32)
        phase_cor = np.vstack((phase_cor,temp))
        temp_array = np.zeros((l_int_temp*2,w_int))
        temp_array[::2,:] = amplitude_int
        print "Size of phase_cor is: ", phase_cor.shape, ", Size of temp_array is: ", temp_array.shape, "\n"
        temp_array[1::2,:]   = np.copy(phase_cor[:l_int_temp,:])
        rscContent_cor["FILE_LENGTH"] = str(l_int_temp)
        corfile_rsc = corfile + ".rsc"
        os.system("rm -f "+corfile_rsc)
        IN = open(corfile_rsc, 'w')
        for k,v in rscContent_cor.iteritems():
            IN.write(k.ljust(41)+v+"\n")
        IN.close()
        os.system("rm -f "+corfile)
        temp_array.astype('float32').tofile(corfile)


    temp_array = amplitude_int * np.exp(1j*phase)
    print "newName is: ", newName, "\n" 
    temp_array.astype('complex64').tofile(newName)
    newName_rsc = newName + ".rsc"
    intfile_rsc = intfile + ".rsc"
    ramp_file_rsc = ramp_file + ".rsc"
    os.system("cp -f "+intfile_rsc+" "+newName_rsc)
    os.system("cp -f "+intfile_rsc+" "+ramp_file_rsc)
    temp_dir = os.getcwd()
    print "Current dir is: ", temp_dir,"\n"
    temp_str = "rm -f temp.int*"
    print temp_str,"\n"
    os.system(temp_str)
'''
Created on Sun Aug 17 18:14:13 2014

code used for layered elastic model of surface loading

Wenliang Zhao
'''

import os,sys,re,glob,subprocess,math,time
import Rsmas_Utilib as ut
import ElasticSAR as es
import numpy as np
from osgeo import gdal
import time

para_list = ut.readProc(sys.argv[1])
processdir = para_list.get("processdir").strip()
disp_dir = processdir + "/InSAR"
line_insar = int(para_list.get("line_insar").strip())
col_insar = int(para_list.get("col_insar").strip())
left_lon_insar = float(para_list.get("left_lon_insar").strip())
top_lat_insar = float(para_list.get("top_lat_insar").strip())
res_lon_insar = float(para_list.get("res_lon_insar").strip())
res_lat_insar = float(para_list.get("res_lat_insar").strip())
lookAng_insar = float(para_list.get("lookAng").strip())
azimuth_insar = float(para_list.get("azimuth").strip())
utmZone = para_list.get("utmZone").strip()
res_x_utm = int(para_list.get("spacing").strip())
res_y_utm = res_x_utm
box = para_list.get("box")
method = para_list.get("method").strip()