def data_from_image(im, classes, mask, limits=[0., 1.]): """ This function extracts the data from a image, based on the classes and a mask. Once the data is masked, a look-up-table is used to set values to the classes. """ import time N = int(basic.nonan(classes).max() + 1) # zero is a class... classes[mask] = np.nan classLimits = np.linspace(np.min(limits), np.max(limits), N) cmax = 2**np.round(np.log2(im.max())) imSingle = im[:, :, 0] * (cmax**0) + im[:, :, 1] * ( cmax**1) + im[:, :, 2] * (cmax**2.) data = np.zeros(imSingle.shape) t0 = 0 for k in xrange(N - 1): #for each class do a linear interpolation if np.any(classes == k): data[classes == k] = basic.rescale(imSingle[classes == k], classLimits[k:k + 2], quiet=True) if basic.progresstime(t0): basic.progress(k, N) t0 = time.time() data[mask] = np.nan return data
def point_solve_dem(dem, i12s, cohs, h2phs, dem_std, multilook, samples=1000, stepsize=None, figures=False, unwrapped=False): """point_solve_dem(dem, i12s, cohs, dem_std, multilook, samples=1000, stepsize=None): Solves a single point. dem=scalar: dem height for the point i12s=vector: observed subtrrefpha, filtphase,unwrap values for point cohs= vector: observed coherence values for point dem_std= scalar: estimated dem standard dev. for point multilook= scalar: interferogram multilooking samples=1000: scalar to be used to constract search space. stepsize=scalar: height sensitivity to use instead of samples to constract search space. search space = linspace (dem-3*dem_std , dem+3*dem_std , samples) OR search space = r_[dem-3*dem_std : dem+3*dem_std : stepSize] """ import numpy as np import os os.sys.path.append('/d0/bosmanoglu/projectLocker/pythonUnwrap/trunk') import insar import scipy from scipy import special if stepsize is None: ss = linspace(dem-3*dem_std , dem+3*dem_std , samples) #stepsize else: ss = np.r_[dem-3*dem_std : dem+3*dem_std : stepsize] #convert ss to phase sp=np.dot(np.atleast_2d(ss).T, np.atleast_2d(h2phs)) #step phase # offset the domain with phase value if any(iscomplex(i12s)): sp=sp-np.tile(np.angle(i12s), (sp.shape[0],1)) else: sp=sp-np.tile(i12s, (sp.shape[0],1)); #pdf of dem pdem=insar.dem.maximum_likelihood.pdf_dem(dem, ss, dem_std); #pdf based on InSAR data if figures: pint=insar.coh2pdfML( cohs, multilook, domain=sp.T ) for k in xrange(len(pint)): plot(ss, pint[k,:]) pint=basic.nonan(pint, True).prod(0) plot(ss, pdem, 'k--') plot(ss, pdem*pint/nanmax(pdem*pint), 'k-.') plot(ss, pint/nanmax(pint), 'k:') print('i12s/cohs/h2phs') print(i12s) print(cohs) print(h2phs) else: pint=insar.coh2pdfML( cohs, multilook, domain=sp.T ).prod(0) #joint pdf jp=pdem*pint return ss[jp.argmax()]
def onclick(event): P.figure(fh.number) P.clf() #ax = P.gca() #inv = ax.transData.inverted() #A=inv.transform((event.x, event.y)) #A[1]=np.int(np.round((1-A[1])*array2d.shape[1])) #A[0]=np.int(np.round((A[0])*array2d.shape[0])) try: y = np.round(event.xdata) except: return x = np.round(event.ydata) #ARRAY MAPPING IS first axis y(rows) and second axis is cols (x) if all(np.isnan(array3d[x, y, :])): #if there are no points to plot (all nan) then return return #Plot second scatter data. if array3d2 is not None: if isinstance(array3d2, list): if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) markers = ['*', '+', 's', 'd', 'x', 'v', '<', '>', '^'] m = 0 for arr in array3d2: print("%d, %d, %d" % (x, y, m)) P.scatter(xScat, arr[x, y, :], marker=markers[m]) idx = ~(np.isnan(arr[x, y, :]) | np.isnan(array3d[x, y, :])) #c=insar.crosscorrelate(basic.nonan(w[idx]*arr[x, y,idx]),basic.nonan(w[idx]*array3d[x, y,idx])) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w[idx] * arr[x, y, idx]), basic.nonan(w[idx] * array3d[x, y, idx])) P.annotate(str("r2[%s]: %0.2f" % (markers[m], r_value)), (0, 0.9 - m * 0.05), xycoords='axes fraction') m = m + 1 else: if xerror3d2 is None: xerr = None else: xerr = xerror3d2[x, y, :] if yerror3d2 is None: yerr = None else: yerr = yerror3d2[x, y, :] P.errorbar(xScat, array3d2[x, y, :], xerr=xerr, yerr=yerr, marker='*', fmt='o') #Plot function result as scatter data. p = None if fn is not None: if fn == 'linear_amplitude_annual': dataMask = ~np.isnan(array3d[x, y, :]) p0 = np.array([1, 0, 0, basic.nonan(array3d[x, y, :]).mean()]) fitfun = lambda p: (p[0] + p[1] * xScat[dataMask] / 365. ) * np.cos(2 * np.pi * xScat[dataMask] / 365. + p[2]) + p[3] xScat2 = np.linspace(xScat.min(), xScat.max()) fitfun2 = lambda p: (p[0] + p[1] * xScat2 / 365.) * np.cos( 2 * np.pi * xScat2 / 365. + p[2]) + p[3] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) errfun = lambda p: basic.nonan(w * array3d[x, y, :]) - w[ dataMask] * fitfun(p) #p=scipy.optimize.fmin_powell(errfun, p0) p = scipy.optimize.leastsq(errfun, p0) p = p[0] P.scatter(xScat[dataMask], fitfun(p), marker='^') sortedxy = np.squeeze(np.dstack([xScat2, fitfun2(p)])) sortedxy = sortedxy[sortedxy[:, 0].argsort(), :] P.plot(sortedxy[:, 0], sortedxy[:, 1]) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w * array3d[x, y, :]), w[dataMask] * fitfun(p)) P.annotate( str("a0:%0.2f\na1:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], p[3], r_value**2.)), (0.8, 0.8), xycoords='axes fraction') elif fn == 'quadratic_amplitude_annual': dataMask = ~np.isnan(array3d[x, y, :]) p0 = np.array( [1, 0, 0, 0, basic.nonan(array3d[x, y, :]).mean()]) fitfun = lambda p: (p[0] + p[1] * xScat[dataMask] / 365. + p[ 2] * (xScat[dataMask] / 365.)**2.) * np.cos( 2 * np.pi * xScat[dataMask] / 365. + p[3]) + p[4] xScat2 = np.linspace(xScat.min(), xScat.max()) fitfun2 = lambda p: (p[0] + p[1] * xScat2 / 365. + p[2] * ( xScat2 / 365.)**2.) * np.cos(2 * np.pi * xScat2 / 365. + p[ 3]) + p[4] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) errfun = lambda p: basic.nonan(w * array3d[x, y, :]) - w[ dataMask] * fitfun(p) #p=scipy.optimize.fmin_powell(errfun, p0) p = scipy.optimize.leastsq(errfun, p0) p = p[0] P.scatter(xScat[dataMask], fitfun(p), marker='^') sortedxy = np.squeeze(np.dstack([xScat2, fitfun2(p)])) sortedxy = sortedxy[sortedxy[:, 0].argsort(), :] P.plot(sortedxy[:, 0], sortedxy[:, 1]) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w * array3d[x, y, :]), w[dataMask] * fitfun(p)) P.annotate(str( "a0:%0.2f\na1:%0.2f\na2:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], p[3], p[4], r_value**2.)), (0.8, 0.8), xycoords='axes fraction') elif fn == 'annual': dataMask = ~np.isnan(array3d[x, y, :]) p0 = np.array([1, 1, basic.nonan(array3d[x, y, :]).mean()]) fitfun = lambda p: p[0] * np.cos(2 * np.pi * xScat[dataMask] / 365. + p[1]) + p[2] xScat2 = np.linspace(xScat.min(), xScat.max()) fitfun2 = lambda p: p[0] * np.cos(2 * np.pi * xScat2 / 365. + p[1]) + p[2] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w = np.ones(array3d[x, y, :].shape) else: w = basic.rescale(1. / yerror3d[x, y, :], [1, 2]) errfun = lambda p: basic.nonan(w * array3d[x, y, :]) - w[ dataMask] * fitfun(p) #p=scipy.optimize.fmin_powell(errfun, p0) p = scipy.optimize.leastsq(errfun, p0) p = p[0] P.scatter(xScat[dataMask], fitfun(p), marker='^') sortedxy = np.squeeze(np.dstack([xScat2, fitfun2(p)])) sortedxy = sortedxy[sortedxy[:, 0].argsort(), :] P.plot(sortedxy[:, 0], sortedxy[:, 1]) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( basic.nonan(w * array3d[x, y, :]), w[dataMask] * fitfun(p)) P.annotate(str("amp:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], r_value**2.)), (0.8, 0.8), xycoords='axes fraction') else: p = None P.scatter(xScat, fn(xScat), marker='^') #convert axis to date... if dateaxis: try: P.figure(fh.number).axes[0].xaxis_date(tz=None) P.figure(fh.number).autofmt_xdate() except: pass #change x y to xMap, yMap if yMap is not None: xM = ya * x + yb else: xM = x if xMap is not None: yM = xa * (y) + xb else: yM = y #x and y are flipped in the try/except block above. So Flip again. #if p is not None: # P.title("x,y,[]: " + str(yM) + ", " + str(xM) + ', ' + str(p) ) #else: P.title("x,y,z,z.std: " + str(yM) + ", " + str(xM) + ', ' + str(array2d[x, y]) + ', ' + str(np.std(basic.nonan(array3d[x, y, :])))) # rotate and align the tick labels so they look better #P.figure(fh.number).autofmt_xdate() # use a more precise date string for the x axis locations in the # toolbar #P.gca().fmt_xdata = mdates.DateFormatter('%Y-%m-%d') if xerror3d is None: xerr = None else: xerr = xerror3d[x, y, :] if yerror3d is None: yerr = None else: yerr = yerror3d[x, y, :] if modelError: yerr = yerror3d[x, y, :] yerr[dataMask] = errfun(p) P.errorbar(xScat, array3d[x, y, :], xerr=xerr, yerr=yerr, fmt='ro') if ylimScat is not None: P.ylim(ylimScat)
def histogram_matching(inputArr, histogramArr=None, bins=100, zpdf=None, zbins=None): """histogram_matching(inputArr, histogramArr=None, bins=100, zpdf=None, zbins=None) """ if (histogramArr is None) and (zpdf is None): print('Error: histogramArr or zpdf has to be specified') return if (bins <= 1) and (zbins is None): print('Skipping histogram matching: bins<=1') return inputArr if len(zbins) <= 1: print('Skipping histogram matching: len(zbins)<=1') return inputArr dS = inputArr.shape lenS = inputArr.size s = basic.nonan(inputArr.ravel()) #Limit matching to majority of the pixels.. We don't want a long trail. sm = s.mean() ss = s.std() sbins = P.np.linspace(sm - 3 * ss, sm + 3 * ss, bins + 1) spdf, sbins = P.np.histogram(s, sbins) spdf = spdf / P.np.double(sum(spdf)) sk = P.np.cumsum(spdf) #spdf * P.np.triu(P.np.ones(dS)) #CDF #Histogram to be matched if zpdf is None: dZ = histogramArr.shape lenZ = histogramArr.size z = basic.nonan(histogramArr.ravel()) zm = z.mean() zs = z.std() zbins = P.np.linspace(zm - 3 * zs, zm + 3 * zs, bins + 1) zpdf, zbins = P.np.histogram(z, zbins) else: #make zpdf match the length of bins zpdf = np.interp(sbins, np.linspace(zbins[0], zbins[-1], zpdf.shape[0]), zpdf) zbins = sbins #zbins no longer needed?. zpdf = zpdf / P.np.double(sum(zpdf)) zk = P.np.cumsum(zpdf) #G(z), CDF #create the image p_prev = 0 z0 = P.np.empty(dS) z0[:] = P.np.nan for q in xrange(0, bins): for p in xrange(p_prev, bins): if zk[p] >= sk[q]: #print ['replacing from ', sbins[q], ' to ', sbins[q+1] , ' with ', zbins[p]] p_prev = p + 1 q_last = q #z0[ P.np.ma.mask_or(inputArr>sbins[q], inputArr<sbins[q+1]) ] = zbins[p]; if q == 0: z0[inputArr < sbins[q + 1]] = zbins[p] else: z0[((inputArr >= sbins[q]).astype(P.np.int) * (inputArr < sbins[q + 1]).astype(P.np.int)).astype( P.np.bool)] = zbins[p] #print ['replacing ', ((inputArr>sbins[q]).astype(P.np.int) * (inputArr<sbins[q+1]).astype(P.np.int)).sum(), ' pixels']; break #inner for #print('q %f p %f zk %f sk %f' %(q,p,zk[p], sk[q])) z0[inputArr >= sbins[q_last]] = zbins[p] return z0
def onclick(event): P.figure(fh.number); P.clf(); #ax = P.gca() #inv = ax.transData.inverted() #A=inv.transform((event.x, event.y)) #A[1]=np.int(np.round((1-A[1])*array2d.shape[1])) #A[0]=np.int(np.round((A[0])*array2d.shape[0])) try: y=np.round(event.xdata); except: return x=np.round(event.ydata); #ARRAY MAPPING IS first axis y(rows) and second axis is cols (x) if all(np.isnan(array3d[x, y,:])): #if there are no points to plot (all nan) then return return #Plot second scatter data. if array3d2 is not None: if isinstance(array3d2, list): if yerror3d is None: w=np.ones(array3d[x, y,:].shape); else: w=basic.rescale(1./yerror3d[x,y,:], [1,2]) markers=['*','+','s','d','x','v','<','>','^'] m=0; for arr in array3d2: print ("%d, %d, %d" % (x,y,m)) P.scatter(xScat, arr[x, y,:], marker=markers[m]); idx=~( np.isnan(arr[x, y,:]) | np.isnan(array3d[x, y,:])) #c=insar.crosscorrelate(basic.nonan(w[idx]*arr[x, y,idx]),basic.nonan(w[idx]*array3d[x, y,idx])) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(basic.nonan(w[idx]*arr[x, y,idx]), basic.nonan(w[idx]*array3d[x, y,idx])) P.annotate(str("r2[%s]: %0.2f" % (markers[m],r_value)), (0,0.9-m*0.05), xycoords='axes fraction') m=m+1; else: if xerror3d2 is None: xerr=None; else: xerr=xerror3d2[x,y,:] if yerror3d2 is None: yerr=None; else: yerr=yerror3d2[x, y,:] P.errorbar(xScat,array3d2[x, y,:], xerr=xerr, yerr=yerr, marker='*', fmt='o'); #Plot function result as scatter data. p=None if fn is not None: if fn=='linear_amplitude_annual': dataMask=~np.isnan(array3d[x, y,:]) p0=np.array([1,0,0,basic.nonan(array3d[x, y,:]).mean() ]) fitfun=lambda p: (p[0]+p[1]*xScat[dataMask]/365. )* np.cos(2*np.pi*xScat[dataMask]/365.+p[2]) + p[3] xScat2=np.linspace(xScat.min(),xScat.max()) fitfun2=lambda p: (p[0]+p[1]*xScat2/365.) * np.cos(2*np.pi*xScat2/365.+p[2]) + p[3] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w=np.ones(array3d[x, y,:].shape); else: w=basic.rescale(1./yerror3d[x,y,:], [1,2]) errfun=lambda p: basic.nonan(w*array3d[x, y,:])-w[dataMask]*fitfun(p); #p=scipy.optimize.fmin_powell(errfun, p0) p=scipy.optimize.leastsq(errfun, p0); p=p[0]; P.scatter(xScat[dataMask], fitfun(p), marker='^'); sortedxy= np.squeeze(np.dstack([xScat2, fitfun2(p)])); sortedxy=sortedxy[sortedxy[:,0].argsort(),:] P.plot(sortedxy[:,0], sortedxy[:,1]); slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(basic.nonan(w*array3d[x, y,:]),w[dataMask]*fitfun(p)) P.annotate(str("a0:%0.2f\na1:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], p[3], r_value**2.)), (0.8,0.8), xycoords='axes fraction') elif fn=='quadratic_amplitude_annual': dataMask=~np.isnan(array3d[x, y,:]) p0=np.array([1,0,0,0,basic.nonan(array3d[x, y,:]).mean() ]) fitfun=lambda p: (p[0]+p[1]*xScat[dataMask]/365.+p[2]*(xScat[dataMask]/365.)**2. )* np.cos(2*np.pi*xScat[dataMask]/365.+p[3]) + p[4] xScat2=np.linspace(xScat.min(),xScat.max()) fitfun2=lambda p: (p[0]+p[1]*xScat2/365.+p[2]*(xScat2/365.)**2.) * np.cos(2*np.pi*xScat2/365.+p[3]) + p[4] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w=np.ones(array3d[x, y,:].shape); else: w=basic.rescale(1./yerror3d[x,y,:], [1,2]) errfun=lambda p: basic.nonan(w*array3d[x, y,:])-w[dataMask]*fitfun(p); #p=scipy.optimize.fmin_powell(errfun, p0) p=scipy.optimize.leastsq(errfun, p0); p=p[0]; P.scatter(xScat[dataMask], fitfun(p), marker='^'); sortedxy= np.squeeze(np.dstack([xScat2, fitfun2(p)])); sortedxy=sortedxy[sortedxy[:,0].argsort(),:] P.plot(sortedxy[:,0], sortedxy[:,1]); slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(basic.nonan(w*array3d[x, y,:]),w[dataMask]*fitfun(p)) P.annotate(str("a0:%0.2f\na1:%0.2f\na2:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], p[3], p[4], r_value**2.)), (0.8,0.8), xycoords='axes fraction') elif fn=='annual': dataMask=~np.isnan(array3d[x, y,:]) p0=np.array([1,1,basic.nonan(array3d[x, y,:]).mean() ]) fitfun=lambda p: p[0]* np.cos(2*np.pi*xScat[dataMask]/365.+p[1]) + p[2] xScat2=np.linspace(xScat.min(),xScat.max()) fitfun2=lambda p: p[0]* np.cos(2*np.pi*xScat2/365.+p[1]) + p[2] #errfun=lambda p: sum(abs(basic.nonan(array3d[x, y,:])-fitfun(p))); if yerror3d is None: w=np.ones(array3d[x, y,:].shape); else: w=basic.rescale(1./yerror3d[x,y,:], [1,2]) errfun=lambda p: basic.nonan(w*array3d[x, y,:])-w[dataMask]*fitfun(p); #p=scipy.optimize.fmin_powell(errfun, p0) p=scipy.optimize.leastsq(errfun, p0); p=p[0]; P.scatter(xScat[dataMask], fitfun(p), marker='^'); sortedxy= np.squeeze(np.dstack([xScat2, fitfun2(p)])); sortedxy=sortedxy[sortedxy[:,0].argsort(),:] P.plot(sortedxy[:,0], sortedxy[:,1]); slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(basic.nonan(w*array3d[x, y,:]),w[dataMask]*fitfun(p)) P.annotate(str("amp:%0.2f\npha:%0.2f\nbias:%0.2f\nr2:%0.2f" % (p[0], p[1], p[2], r_value**2.)), (0.8,0.8), xycoords='axes fraction') else: p=None P.scatter(xScat, fn(xScat), marker='^'); #convert axis to date... if dateaxis: try: P.figure(fh.number).axes[0].xaxis_date(tz=None) P.figure(fh.number).autofmt_xdate() except: pass #change x y to xMap, yMap if yMap is not None: xM=ya*x+yb; else: xM=x; if xMap is not None: yM=xa*(y)+xb; else: yM=y; #x and y are flipped in the try/except block above. So Flip again. #if p is not None: # P.title("x,y,[]: " + str(yM) + ", " + str(xM) + ', ' + str(p) ) #else: P.title("x,y,z,z.std: " + str(yM) + ", " + str(xM) + ', ' + str(array2d[x,y]) +', ' + str(np.std(basic.nonan(array3d[x, y,:]))) ) # rotate and align the tick labels so they look better #P.figure(fh.number).autofmt_xdate() # use a more precise date string for the x axis locations in the # toolbar #P.gca().fmt_xdata = mdates.DateFormatter('%Y-%m-%d') if xerror3d is None: xerr=None; else: xerr=xerror3d[x,y,:] if yerror3d is None: yerr=None; else: yerr=yerror3d[x, y,:] if modelError: yerr=yerror3d[x, y,:] yerr[dataMask]=errfun(p) P.errorbar(xScat,array3d[x, y,:], xerr=xerr, yerr=yerr, fmt='ro'); if ylimScat is not None: P.ylim(ylimScat);
def histogram_matching(inputArr, histogramArr=None, bins=100, zpdf=None, zbins=None): """histogram_matching(inputArr, histogramArr=None, bins=100, zpdf=None, zbins=None) """ if (histogramArr is None) and (zpdf is None): print('Error: histogramArr or zpdf has to be specified') return if (bins<=1) and (zbins is None): print('Skipping histogram matching: bins<=1') return inputArr if len(zbins)<=1: print('Skipping histogram matching: len(zbins)<=1') return inputArr dS=inputArr.shape lenS=inputArr.size s=basic.nonan(inputArr.ravel()) #Limit matching to majority of the pixels.. We don't want a long trail. sm=s.mean() ss=s.std() sbins=P.np.linspace(sm-3*ss,sm+3*ss,bins+1); spdf, sbins=P.np.histogram(s, sbins) spdf=spdf/P.np.double(sum(spdf)) sk= P.np.cumsum(spdf) #spdf * P.np.triu(P.np.ones(dS)) #CDF #Histogram to be matched if zpdf is None: dZ=histogramArr.shape lenZ=histogramArr.size z=basic.nonan(histogramArr.ravel()) zm=z.mean() zs=z.std() zbins=P.np.linspace(zm-3*zs,zm+3*zs,bins+1); zpdf, zbins=P.np.histogram(z, zbins) else: #make zpdf match the length of bins zpdf=np.interp(sbins, np.linspace(zbins[0], zbins[-1], zpdf.shape[0]), zpdf); zbins=sbins #zbins no longer needed?. zpdf=zpdf/P.np.double(sum(zpdf)) zk= P.np.cumsum(zpdf) #G(z), CDF #create the image p_prev=0 z0=P.np.empty(dS) z0[:]=P.np.nan for q in xrange(0,bins): for p in xrange(p_prev,bins): if zk[p] >= sk[q]: #print ['replacing from ', sbins[q], ' to ', sbins[q+1] , ' with ', zbins[p]] p_prev=p+1 q_last=q #z0[ P.np.ma.mask_or(inputArr>sbins[q], inputArr<sbins[q+1]) ] = zbins[p]; if q==0: z0[ inputArr<sbins[q+1] ] = zbins[p]; else: z0[ ((inputArr>=sbins[q]).astype(P.np.int) * (inputArr<sbins[q+1]).astype(P.np.int)).astype(P.np.bool) ] = zbins[p]; #print ['replacing ', ((inputArr>sbins[q]).astype(P.np.int) * (inputArr<sbins[q+1]).astype(P.np.int)).sum(), ' pixels']; break #inner for #print('q %f p %f zk %f sk %f' %(q,p,zk[p], sk[q])) z0[inputArr>=sbins[q_last]]=zbins[p] return z0
def point_solve_dem(dem, i12s, cohs, h2phs, dem_std, multilook, samples=1000, stepsize=None, figures=False, unwrapped=False): """point_solve_dem(dem, i12s, cohs, dem_std, multilook, samples=1000, stepsize=None): Solves a single point. dem=scalar: dem height for the point i12s=vector: observed subtrrefpha, filtphase,unwrap values for point cohs= vector: observed coherence values for point dem_std= scalar: estimated dem standard dev. for point multilook= scalar: interferogram multilooking samples=1000: scalar to be used to constract search space. stepsize=scalar: height sensitivity to use instead of samples to constract search space. search space = linspace (dem-3*dem_std , dem+3*dem_std , samples) OR search space = r_[dem-3*dem_std : dem+3*dem_std : stepSize] """ import numpy as np import os os.sys.path.append('/d0/bosmanoglu/projectLocker/pythonUnwrap/trunk') import insar import scipy from scipy import special if stepsize is None: ss = linspace(dem - 3 * dem_std, dem + 3 * dem_std, samples) #stepsize else: ss = np.r_[dem - 3 * dem_std:dem + 3 * dem_std:stepsize] #convert ss to phase sp = np.dot(np.atleast_2d(ss).T, np.atleast_2d(h2phs)) #step phase # offset the domain with phase value if any(iscomplex(i12s)): sp = sp - np.tile(np.angle(i12s), (sp.shape[0], 1)) else: sp = sp - np.tile(i12s, (sp.shape[0], 1)) #pdf of dem pdem = insar.dem.maximum_likelihood.pdf_dem(dem, ss, dem_std) #pdf based on InSAR data if figures: pint = insar.coh2pdfML(cohs, multilook, domain=sp.T) for k in xrange(len(pint)): plot(ss, pint[k, :]) pint = basic.nonan(pint, True).prod(0) plot(ss, pdem, 'k--') plot(ss, pdem * pint / nanmax(pdem * pint), 'k-.') plot(ss, pint / nanmax(pint), 'k:') print('i12s/cohs/h2phs') print(i12s) print(cohs) print(h2phs) else: pint = insar.coh2pdfML(cohs, multilook, domain=sp.T).prod(0) #joint pdf jp = pdem * pint return ss[jp.argmax()]