def reconstruct_igrams_from_timeseries(h5timeseries,h5igrams): ######################################################## dateList = h5timeseries['timeseries'].keys() dateIndex={} for ni in range(len(dateList)): dateIndex[dateList[ni]]=ni dset = h5timeseries['timeseries'].get(h5timeseries['timeseries'].keys()[0]) nrows,ncols=shape(dset) timeseries = zeros((len(h5timeseries['timeseries'].keys()),shape(dset)[0]*shape(dset)[1]),float32) for date in dateList: dset = h5timeseries['timeseries'].get(date) d = dset[0:dset.shape[0],0:dset.shape[1]] timeseries[dateIndex[date]][:]=d.flatten(0) del d lt,numpixels=shape(timeseries) A,B = ut.design_matrix(h5igrams) lam = float(h5timeseries['timeseries'].attrs['WAVELENGTH']) range2phase=-4*pi/lam timeseries=range2phase*timeseries p=-1*ones([A.shape[0],1]) Ap=hstack((p,A)) estData=dot(Ap,timeseries) return estData,nrows,ncols
def reconstruct_igrams_from_timeseries(h5timeseries, h5igrams): ######################################################## dateList = h5timeseries['timeseries'].keys() dateIndex = {} for ni in range(len(dateList)): dateIndex[dateList[ni]] = ni dset = h5timeseries['timeseries'].get(h5timeseries['timeseries'].keys()[0]) nrows, ncols = shape(dset) timeseries = zeros((len( h5timeseries['timeseries'].keys()), shape(dset)[0] * shape(dset)[1]), float32) for date in dateList: dset = h5timeseries['timeseries'].get(date) d = dset[0:dset.shape[0], 0:dset.shape[1]] timeseries[dateIndex[date]][:] = d.flatten(0) del d lt, numpixels = shape(timeseries) A, B = ut.design_matrix(h5igrams) lam = float(h5timeseries['timeseries'].attrs['WAVELENGTH']) range2phase = -4 * pi / lam timeseries = range2phase * timeseries p = -1 * ones([A.shape[0], 1]) Ap = hstack((p, A)) estData = dot(Ap, timeseries) return estData, nrows, ncols
def main(argv): try: file = argv[0] except: Usage() sys.exit(1) h5file = h5py.File(file) ifgramList = h5file['interferograms'].keys() sx = int(h5file['interferograms'][ifgramList[0]].attrs['WIDTH']) sy = int(h5file['interferograms'][ifgramList[0]].attrs['FILE_LENGTH']) curls, Triangles, C = ut.get_triangles(h5file) A, B = ut.design_matrix(h5file) ligram, lv = np.shape(B) lcurls = np.shape(curls)[0] print 'Number of all triangles: ' + str(lcurls) print 'Number of interferograms: ' + str(ligram) # print curls curlfile = 'curls.h5' if not os.path.isfile(curlfile): ut.generate_curls(curlfile, h5file, Triangles, curls) thr = 0.50 curls = np.array(curls) n1 = curls[:, 0] n2 = curls[:, 1] n3 = curls[:, 2] numPixels = sy * sx print 'reading interferograms...' data = np.zeros((ligram, numPixels), np.float32) for ni in range(ligram): dset = h5file['interferograms'][ifgramList[ni]].get(ifgramList[ni]) d = dset[0:dset.shape[0], 0:dset.shape[1]] data[ni] = d.flatten(1) print np.shape(data) print 'reading curls ...' h5curl = h5py.File(curlfile) curlList = h5curl['interferograms'].keys() curlData = np.zeros((lcurls, numPixels), np.float32) for ni in range(lcurls): dset = h5curl['interferograms'][curlList[ni]].get(curlList[ni]) d = dset[0:dset.shape[0], 0:dset.shape[1]] curlData[ni] = d.flatten(1) pi = np.pi EstUnwrap = np.zeros((ligram, numPixels), np.float32) try: maskFile = argv[1] h5Mask = h5py.File(maskFile) dset = h5Mask['mask'].get('mask') Mask = dset[0:dset.shape[0], 0:dset.shape[1]] except: dset = h5file['mask'].get('mask') Mask = dset[0:dset.shape[0], 0:dset.shape[1]] Mask = Mask.flatten(1) for ni in range(numPixels): #dU = np.zeros([ligram,1]) #print np.shape(dU) #print np.shape(data[:,ni]) if Mask[ni] == 1: dU = data[:, ni] # nan_ndx = dataPoint == 0. unwCurl = np.array(curlData[:, ni]) # print unwCurl ind = np.abs(unwCurl) >= thr N1 = n1[ind] N2 = n2[ind] N3 = n3[ind] indC = np.abs(unwCurl) < thr Nc1 = n1[indC] Nc2 = n2[indC] Nc3 = n3[indC] N = np.hstack([N1, N2, N3]) UniN = np.unique(N) Nc = np.hstack([Nc1, Nc2, Nc3]) UniNc = np.unique(Nc) inter = list(set(UniNc) & set(UniN)) # intersetion UniNc = list(UniNc) for x in inter: UniNc.remove(x) D = np.zeros([len(UniNc), ligram]) for i in range(len(UniNc)): D[i, UniNc[i]] = 1 AAA = np.vstack([-2 * pi * C, D]) # AAA1=np.hstack([AAA,np.zeros([AAA.shape[0],lv])]) # AAA2=np.hstack([-2*pi*np.eye(ligram),B]) # AAAA=np.vstack([AAA1,AAA2]) AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)]) # print '************************' # print np.linalg.matrix_rank(C) # print np.linalg.matrix_rank(AAA) # print np.linalg.matrix_rank(AAAA) # print '************************' # LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0]))# + list(dU) # ind=np.isnan(AAA) # M1=pinv(AAA) # M=np.dot(M1,LLL) # EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi ########## # with Tikhonov regularization: AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)]) LLL = list(np.dot(C, dU)) + list(np.zeros( np.shape(UniNc)[0])) + list(np.zeros(ligram)) ind = np.isnan(AAAA) M1 = pinv(AAAA) M = np.dot(M1, LLL) EstUnwrap[:, ni] = np.round(M[0:ligram]) * 2.0 * np.pi # print M[0:ligram] # print np.round(M[0:ligram]) else: EstUnwrap[:, ni] = np.zeros([ligram]) if not np.remainder(ni, 10000): print 'Processing point: %7d of %7d ' % (ni, numPixels) dataCor = data + EstUnwrap unwCorFile = file.replace('.h5', '') + '_unwCor.h5' h5unwCor = h5py.File(unwCorFile, 'w') gg = h5unwCor.create_group('interferograms') for i in range(ligram): group = gg.create_group(ifgramList[i]) # dset = group.create_dataset(ifgramList[i], data=np.reshape(dataCor[i,:],[sy,sx]), compression='gzip') dset = group.create_dataset(ifgramList[i], data=np.reshape(dataCor[i, :], [sx, sy]).T, compression='gzip') for key, value in h5file['interferograms'][ ifgramList[i]].attrs.iteritems(): group.attrs[key] = value MASK = h5file['mask'].get('mask') gm = h5unwCor.create_group('mask') dset = gm.create_dataset('mask', data=MASK, compression='gzip') try: Coh = h5file['meanCoherence'].get('meanCoherence') gc = h5unwCor.create_group('meanCoherence') dset = gc.create_dataset('meanCoherence', data=Coh, compression='gzip') except: print '' h5unwCor.close() h5file.close() h5curl.close()
def main(argv): try: file = argv[0] except: Usage() sys.exit(1) h5file = h5py.File(file) ifgramList = h5file["interferograms"].keys() sx = int(h5file["interferograms"][ifgramList[0]].attrs["WIDTH"]) sy = int(h5file["interferograms"][ifgramList[0]].attrs["FILE_LENGTH"]) curls, Triangles, C = ut.get_triangles(h5file) A, B = ut.design_matrix(h5file) ligram, lv = np.shape(B) lcurls = np.shape(curls)[0] print "Number of all triangles: " + str(lcurls) print "Number of interferograms: " + str(ligram) # print curls curlfile = "curls.h5" if not os.path.isfile(curlfile): ut.generate_curls(curlfile, h5file, Triangles, curls) thr = 0.50 curls = np.array(curls) n1 = curls[:, 0] n2 = curls[:, 1] n3 = curls[:, 2] numPixels = sy * sx print "reading interferograms..." data = np.zeros((ligram, numPixels), np.float32) for ni in range(ligram): dset = h5file["interferograms"][ifgramList[ni]].get(ifgramList[ni]) d = dset[0 : dset.shape[0], 0 : dset.shape[1]] data[ni] = d.flatten(1) print np.shape(data) print "reading curls ..." h5curl = h5py.File(curlfile) curlList = h5curl["interferograms"].keys() curlData = np.zeros((lcurls, numPixels), np.float32) for ni in range(lcurls): dset = h5curl["interferograms"][curlList[ni]].get(curlList[ni]) d = dset[0 : dset.shape[0], 0 : dset.shape[1]] curlData[ni] = d.flatten(1) pi = np.pi EstUnwrap = np.zeros((ligram, numPixels), np.float32) try: maskFile = argv[1] h5Mask = h5py.File(maskFile) dset = h5Mask["mask"].get("mask") Mask = dset[0 : dset.shape[0], 0 : dset.shape[1]] except: dset = h5file["mask"].get("mask") Mask = dset[0 : dset.shape[0], 0 : dset.shape[1]] Mask = Mask.flatten(1) for ni in range(numPixels): # dU = np.zeros([ligram,1]) # print np.shape(dU) # print np.shape(data[:,ni]) if Mask[ni] == 1: dU = data[:, ni] # nan_ndx = dataPoint == 0. unwCurl = np.array(curlData[:, ni]) # print unwCurl ind = np.abs(unwCurl) >= thr N1 = n1[ind] N2 = n2[ind] N3 = n3[ind] indC = np.abs(unwCurl) < thr Nc1 = n1[indC] Nc2 = n2[indC] Nc3 = n3[indC] N = np.hstack([N1, N2, N3]) UniN = np.unique(N) Nc = np.hstack([Nc1, Nc2, Nc3]) UniNc = np.unique(Nc) inter = list(set(UniNc) & set(UniN)) # intersetion UniNc = list(UniNc) for x in inter: UniNc.remove(x) D = np.zeros([len(UniNc), ligram]) for i in range(len(UniNc)): D[i, UniNc[i]] = 1 AAA = np.vstack([-2 * pi * C, D]) # AAA1=np.hstack([AAA,np.zeros([AAA.shape[0],lv])]) # AAA2=np.hstack([-2*pi*np.eye(ligram),B]) # AAAA=np.vstack([AAA1,AAA2]) AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)]) # print '************************' # print np.linalg.matrix_rank(C) # print np.linalg.matrix_rank(AAA) # print np.linalg.matrix_rank(AAAA) # print '************************' # LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0]))# + list(dU) # ind=np.isnan(AAA) # M1=pinv(AAA) # M=np.dot(M1,LLL) # EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi ########## # with Tikhonov regularization: AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)]) LLL = list(np.dot(C, dU)) + list(np.zeros(np.shape(UniNc)[0])) + list(np.zeros(ligram)) ind = np.isnan(AAAA) M1 = pinv(AAAA) M = np.dot(M1, LLL) EstUnwrap[:, ni] = np.round(M[0:ligram]) * 2.0 * np.pi # print M[0:ligram] # print np.round(M[0:ligram]) else: EstUnwrap[:, ni] = np.zeros([ligram]) if not np.remainder(ni, 10000): print "Processing point: %7d of %7d " % (ni, numPixels) dataCor = data + EstUnwrap unwCorFile = file.replace(".h5", "") + "_unwCor.h5" h5unwCor = h5py.File(unwCorFile, "w") gg = h5unwCor.create_group("interferograms") for i in range(ligram): group = gg.create_group(ifgramList[i]) # dset = group.create_dataset(ifgramList[i], data=np.reshape(dataCor[i,:],[sy,sx]), compression='gzip') dset = group.create_dataset(ifgramList[i], data=np.reshape(dataCor[i, :], [sx, sy]).T, compression="gzip") for key, value in h5file["interferograms"][ifgramList[i]].attrs.iteritems(): group.attrs[key] = value MASK = h5file["mask"].get("mask") gm = h5unwCor.create_group("mask") dset = gm.create_dataset("mask", data=MASK, compression="gzip") try: Coh = h5file["meanCoherence"].get("meanCoherence") gc = h5unwCor.create_group("meanCoherence") dset = gc.create_dataset("meanCoherence", data=Coh, compression="gzip") except: print "" h5unwCor.close() h5file.close() h5curl.close()
def main(argv): ##### Inputs try: ifgram_file = argv[0] timeseries_file = argv[1] except: usage() sys.exit(1) try: outfile = argv[2] except: outfile = 'reconstructed_' + ifgram_file atr = readfile.read_attribute(timeseries_file) length = int(atr['FILE_LENGTH']) width = int(atr['WIDTH']) ##### Read time-series file print('loading timeseries ...') h5ts = h5py.File(timeseries_file, 'r') date_list = sorted(h5ts['timeseries'].keys()) date_num = len(date_list) timeseries = np.zeros((date_num, length * width)) print('number of acquisitions: ' + str(date_num)) prog_bar = ptime.progress_bar(maxValue=date_num) for i in range(date_num): date = date_list[i] d = h5ts['timeseries'].get(date)[:] timeseries[i, :] = d.flatten(0) prog_bar.update(i + 1, suffix=date) prog_bar.close() h5ts.close() del d range2phase = -4 * np.pi / float(atr['WAVELENGTH']) timeseries = range2phase * timeseries ##### Estimate interferograms from timeseries print( 'estimating interferograms from timeseries using design matrix from input interferograms' ) A, B = ut.design_matrix(ifgram_file) p = -1 * np.ones([A.shape[0], 1]) Ap = np.hstack((p, A)) estData = np.dot(Ap, timeseries) del timeseries ##### Write interferograms file print('writing >>> ' + outfile) h5 = h5py.File(ifgram_file, 'r') ifgram_list = sorted(h5['interferograms'].keys()) ifgram_num = len(ifgram_list) date12_list = ptime.list_ifgram2date12(ifgram_list) h5out = h5py.File(outfile, 'w') group = h5out.create_group('interferograms') print('number of interferograms: ' + str(ifgram_num)) prog_bar = ptime.progress_bar(maxValue=ifgram_num) for i in range(ifgram_num): ifgram = ifgram_list[i] data = np.reshape(estData[i, :], (length, width)) gg = group.create_group(ifgram) dset = gg.create_dataset(ifgram, data=data, compression='gzip') for key, value in h5['interferograms'][ifgram].attrs.items(): gg.attrs[key] = value prog_bar.update(i + 1, suffix=date12_list[i]) prog_bar.close() h5.close() h5out.close() print('Done.') return outfile