def __init__(self, Tfile, Sfile): self.T_ds=gdal.Open(Tfile, gdalconst.GA_ReadOnly) self.T_band=self.T_ds.GetRasterBand(1) self.S_ds=gdal.Open(Sfile, gdalconst.GA_ReadOnly) self.S_band=self.S_ds.GetRasterBand(1) self.Nx=self.T_band.XSize self.Ny=self.T_band.YSize self.KW=13 # pad the edges by this amount to avoid edge effects self.S_sub=im_subset(0, 0, self.S_band.XSize, self.S_band.YSize, self.S_ds, pad_val=0, Bands=[[1]]) self.T_sub=im_subset(0, 0, self.T_band.XSize, self.S_band.YSize, self.T_ds, pad_val=0, Bands=[[1]]) GT_S=self.S_sub.source.GetGeoTransform() self.GT_S=GT_S self.UL_S=np.array([GT_S[0], GT_S[3]]) LL_S=np.array([GT_S[0], GT_S[3]+self.S_sub.Nr*GT_S[5]]) UR_S=np.array([GT_S[0]+self.S_sub.Nc*GT_S[1], GT_S[3]]) GT_T=self.T_sub.source.GetGeoTransform() self.GT_T=GT_T self.UL_T=np.array([GT_T[0], GT_T[3]]) LL_T=np.array([GT_T[0], GT_T[3]+self.T_sub.Nr*GT_T[5]]) UR_T=np.array([GT_T[0]+self.T_sub.Nc*GT_T[1], GT_T[3]]) XR=[np.max([LL_S[0], LL_T[0]]), np.min([UR_S[0], UR_T[0]])] YR=[np.max([LL_S[1], LL_T[1]]), np.min([UR_S[1], UR_T[1]])] self.XR=XR self.YR=YR self.T_c0c1=np.array([max([0, (LL_T[0]-XR[0])/GT_T[1]]), min(self.T_sub.Nc, (XR[1]-LL_T[0])/GT_T[1])]).astype(int) self.T_r0r1=np.array([max([0 , (YR[1]-UR_T[1])/GT_T[5]]), min([self.T_sub.Nr, (LL_T[1]-YR[1])/GT_T[5]])]).astype(int)
def __init__(self, Tfile, Sfile): self.T_ds = gdal.Open(Tfile, gdalconst.GA_ReadOnly) self.T_band = self.T_ds.GetRasterBand(1) self.S_ds = gdal.Open(Sfile, gdalconst.GA_ReadOnly) self.S_band = self.S_ds.GetRasterBand(1) self.Nx = self.T_band.XSize self.Ny = self.T_band.YSize self.KW = 13 # pad the edges by this amount to avoid edge effects self.S_sub = im_subset(0, 0, self.S_band.XSize, self.S_band.YSize, self.S_ds, pad_val=0, Bands=[[1]]) self.T_sub = im_subset(0, 0, self.T_band.XSize, self.S_band.YSize, self.T_ds, pad_val=0, Bands=[[1]]) GT_S = self.S_sub.source.GetGeoTransform() self.GT_S = GT_S self.UL_S = np.array([GT_S[0], GT_S[3]]) LL_S = np.array([GT_S[0], GT_S[3] + self.S_sub.Nr * GT_S[5]]) UR_S = np.array([GT_S[0] + self.S_sub.Nc * GT_S[1], GT_S[3]]) GT_T = self.T_sub.source.GetGeoTransform() self.GT_T = GT_T self.UL_T = np.array([GT_T[0], GT_T[3]]) LL_T = np.array([GT_T[0], GT_T[3] + self.T_sub.Nr * GT_T[5]]) UR_T = np.array([GT_T[0] + self.T_sub.Nc * GT_T[1], GT_T[3]]) XR = [np.max([LL_S[0], LL_T[0]]), np.min([UR_S[0], UR_T[0]])] YR = [np.max([LL_S[1], LL_T[1]]), np.min([UR_S[1], UR_T[1]])] self.XR = XR self.YR = YR self.T_c0c1 = np.array([ max([0, (LL_T[0] - XR[0]) / GT_T[1]]), min(self.T_sub.Nc, (XR[1] - LL_T[0]) / GT_T[1]) ]).astype(int) self.T_r0r1 = np.array([ max([0, (YR[1] - UR_T[1]) / GT_T[5]]), min([self.T_sub.Nr, (LL_T[1] - YR[1]) / GT_T[5]]) ]).astype(int)
if args.erode_By is not None: N_erode=np.ceil(args.erode_By/dx) xg,yg=np.meshgrid(np.arange(0, N_erode)-N_erode/2, np.arange(0, N_erode)-N_erode/2) k_erode=(xg**2 + yg**2) <= N_erode/2. if args.facet_tol is not None: xxg, yyg=np.meshgrid(np.arange(-8., 9), np.arange(-8., 9.)) opening_kernel=(xxg**2+yyg**2 <= 25) closing_kernel=(xxg**2+yyg**2 <= 64) pad=np.max([1, np.int(2.*(w_smooth+N_erode)/dec)]); if w_error is not None: pad=np.max([1, np.int(2.*w_error+2.*(w_smooth+N_erode)/dec)]) stride=np.int(blocksize/dec) in_sub=im_subset(0, 0, nX, nY, ds, pad_val=0, Bands=[1]) last_time=time.time() for out_sub in im_subset(0, 0, nX_out, nY_out, outDs, pad_val=0, Bands=out_bands, stride=stride, pad=pad): in_sub.setBounds(out_sub.c0*dec, out_sub.r0*dec, out_sub.Nc*dec, out_sub.Nr*dec, update=True) z=in_sub.z[0,:,:] mask=np.ones_like(in_sub.z[0,:,:]) mask[np.isnan(in_sub.z[0,:,:])]=0 mask[in_sub.z[0,:,:]==noData]=0 out_temp=np.zeros([len(out_bands), stride, stride]) if np.all(mask.ravel()==0): out_temp=out_temp+np.NaN out_sub.z=out_temp out_sub.setBounds(out_sub.c0+pad, out_sub.r0+pad, out_sub.Nc-2*pad, out_sub.Nr-2*pad)
def __call__(self, t_pixels, s_nxy_i, dxy0_i, XYc_i, min_template_sigma=None): KW = self.KW # loop over pixel centers self.C = np.zeros([XYc_i.shape[0], 1]) - 1 self.sigma_template = (self.C).copy() self.dxy = np.zeros([XYc_i.shape[0], 2]) self.xyC = np.zeros([XYc_i.shape[0], 2]) blocksize = 8192 Ds2x = True # this means: downsample by 2 to calculate an initial offset xg0, yg0 = np.meshgrid(np.arange(0, self.T_band.XSize, blocksize), np.arange(0, self.T_band.YSize, blocksize)) s_x_bounds = np.c_[XYc_i[:, 0] + dxy0_i[:, 0] - s_nxy_i[:, 0] - KW - 1000, XYc_i[:, 0] + dxy0_i[:, 0] + s_nxy_i[:, 0] + KW + 1000] s_y_bounds = np.c_[XYc_i[:, 1] + dxy0_i[:, 1] - s_nxy_i[:, 1] - KW - 1000, XYc_i[:, 1] + dxy0_i[:, 1] + s_nxy_i[:, 1] + KW + 1000] for xgi, ygi in zip(xg0.ravel(), yg0.ravel()): print('xgi, ygi ', xgi, ygi) # loop over the sub-blocks these = np.logical_and( np.logical_and(XYc_i[:, 0] > xgi, XYc_i[:, 0] <= xgi + blocksize), np.logical_and(XYc_i[:, 1] > ygi, XYc_i[:, 1] <= ygi + blocksize)) if ~np.any(these): continue self.T_sub.setBounds(xgi - t_pixels, ygi - t_pixels, blocksize + 2 * t_pixels, blocksize + 2 * t_pixels, update=1) self.S_sub.setBounds( s_x_bounds[these, 0].min(), s_y_bounds[these, 0].min(), s_x_bounds[these, 1].max() - s_x_bounds[these, 0].min(), s_y_bounds[these, 1].max() - s_y_bounds[these, 0].min(), update=1) #print "reading size:(%f,%f)" % (s_x_bounds[these,1].max()-s_x_bounds[these,0].min(), s_y_bounds[these,1].max()-s_y_bounds[these,0].min()) these_ind = np.array(np.nonzero(these)).ravel() count = -1 TaskParams = [] for Xc, Yc, s_nx, s_ny, dx0, dy0 in zip(XYc_i[these, 0], XYc_i[these, 1], s_nxy_i[these, 0], s_nxy_i[these, 1], dxy0_i[these, 0], dxy0_i[these, 1]): count = count + 1 #print('count is ', count) t_xr = [Xc - (t_pixels / 2 - 1) - KW, Xc + (t_pixels / 2) + KW] t_yr = [Yc - (t_pixels / 2 - 1) - KW, Xc + (t_pixels / 2) + KW] s_xr = [Xc + dx0 - (s_nx / 2 - 1) - KW, Xc + (s_nx / 2) + KW] s_yr = [Yc + dy0 - (s_ny / 2 - 1) - KW, Yc + (s_ny / 2) + KW] # Read in the data for this block. Use the im_subset objects: # read nodata if we read past the image edges. # Read T T_buffer = im_subset(0, 0, self.T_band.XSize, self.S_band.YSize, self.T_sub, pad_val=0) T_buffer.setBounds(t_xr[0] - KW, t_yr[0] - KW, t_pixels + 2. * KW, t_pixels + 2. * KW, update=1) T_img = T_buffer.z[0, :, :] if np.mean(T_img == 0) > .1: # bail if > 10% 0, flag with C=-2 continue # Read S S_buffer = im_subset(0, 0, self.S_band.XSize, self.S_band.YSize, self.S_sub, pad_val=0) S_buffer.setBounds(s_xr[0] - KW, s_yr[0] - KW, s_nx + 2. * KW, s_ny + 2. * KW, update=1) S_img = S_buffer.z[0, :, :] if np.mean(S_img == 0) > .25: # bail if > 25% 0 continue # To do: Not all these parameters are necessary on the # other side! params = (count, t_pixels, KW, min_template_sigma, these_ind, Xc, Yc, s_nx, s_ny, dx0, dy0, T_img.copy(), S_img.copy()) TaskParams.append(params) # To do: num processes must be a param! pool = Pool(processes=16) Out = pool.map(run_one_block, TaskParams) for i in range(len(TaskParams)): params = TaskParams[i] #out = run_one_block(params) # single-threaded out = Out[i] count = params[0] # Need not have count == i. self.C[these_ind[count]] = out[0] self.xyC[these_ind[count]] = [out[1], out[2]] if out[3] >= 0: self.sigma_template[these_ind[count]] = out[3] self.dxy[these_ind[count], :] = [out[4], out[5]] return (self.xyC).copy(), (self.dxy).copy(), (self.C).copy(), ( self.sigma_template).copy()
xg, yg = np.meshgrid( np.arange(0, N_erode) - N_erode / 2, np.arange(0, N_erode) - N_erode / 2) k_erode = (xg**2 + yg**2) <= N_erode / 2. if args.facet_tol is not None: xxg, yyg = np.meshgrid(np.arange(-8., 9), np.arange(-8., 9.)) opening_kernel = (xxg**2 + yyg**2 <= 25) closing_kernel = (xxg**2 + yyg**2 <= 64) pad = np.max([1, np.int(2. * (w_smooth + N_erode) / dec)]) if w_error is not None: pad = np.max([1, np.int(2. * w_error + 2. * (w_smooth + N_erode) / dec)]) stride = np.int(blocksize / dec) in_sub = im_subset(0, 0, nX, nY, ds, pad_val=0, Bands=[1]) last_time = time.time() for out_sub in im_subset(0, 0, nX_out, nY_out, outDs, pad_val=0, Bands=out_bands, stride=stride, pad=pad): in_sub.setBounds(out_sub.c0 * dec, out_sub.r0 * dec, out_sub.Nc * dec,
def __call__(self, t_pixels, s_nxy_i, dxy0_i, XYc_i, min_template_sigma=None): KW=self.KW # loop over pixel centers self.C=np.zeros([XYc_i.shape[0], 1])-1 self.sigma_template=(self.C).copy() self.dxy=np.zeros([XYc_i.shape[0], 2]) self.xyC=np.zeros([XYc_i.shape[0], 2]) blocksize=8192 Ds2x=True # this means: downsample by 2 to calculate an initial offset xg0, yg0=np.meshgrid(np.arange(0, self.T_band.XSize, blocksize), np.arange(0, self.T_band.YSize, blocksize)) s_x_bounds=np.c_[XYc_i[:,0]+dxy0_i[:,0]-s_nxy_i[:,0]-KW-1000, XYc_i[:,0]+dxy0_i[:,0]+s_nxy_i[:,0]+KW+1000] s_y_bounds=np.c_[XYc_i[:,1]+dxy0_i[:,1]-s_nxy_i[:,1]-KW-1000, XYc_i[:,1]+dxy0_i[:,1]+s_nxy_i[:,1]+KW+1000] for xgi, ygi in zip(xg0.ravel(), yg0.ravel()): print('xgi, ygi ', xgi, ygi) # loop over the sub-blocks these=np.logical_and(np.logical_and(XYc_i[:,0] > xgi , XYc_i[:,0] <= xgi+blocksize), np.logical_and( XYc_i[:,1] > ygi , XYc_i[:,1] <= ygi+blocksize)) if ~np.any(these): continue self.T_sub.setBounds(xgi-t_pixels, ygi-t_pixels, blocksize+2*t_pixels, blocksize+2*t_pixels, update=1) self.S_sub.setBounds(s_x_bounds[these,0].min(), s_y_bounds[these,0].min(), s_x_bounds[these,1].max()-s_x_bounds[these,0].min(), s_y_bounds[these,1].max()-s_y_bounds[these,0].min(), update=1) #print "reading size:(%f,%f)" % (s_x_bounds[these,1].max()-s_x_bounds[these,0].min(), s_y_bounds[these,1].max()-s_y_bounds[these,0].min()) these_ind=np.array(np.nonzero(these)).ravel() count=-1 TaskParams = [] for Xc, Yc, s_nx, s_ny, dx0, dy0 in zip(XYc_i[these,0], XYc_i[these,1], s_nxy_i[these,0], s_nxy_i[these,1], dxy0_i[these,0], dxy0_i[these,1]): count=count+1 #print('count is ', count) t_xr=[Xc-(t_pixels/2-1)-KW, Xc+(t_pixels/2)+KW] t_yr=[Yc-(t_pixels/2-1)-KW, Xc+(t_pixels/2)+KW] s_xr=[Xc+dx0-(s_nx/2-1)-KW, Xc+(s_nx/2)+KW] s_yr=[Yc+dy0-(s_ny/2-1)-KW, Yc+(s_ny/2)+KW] # Read in the data for this block. Use the im_subset objects: # read nodata if we read past the image edges. # Read T T_buffer=im_subset(0, 0, self.T_band.XSize, self.S_band.YSize, self.T_sub, pad_val=0) T_buffer.setBounds(t_xr[0]-KW, t_yr[0]-KW, t_pixels+2.*KW, t_pixels+2.*KW, update=1) T_img=T_buffer.z[0,:,:] if np.mean(T_img==0)>.1: # bail if > 10% 0, flag with C=-2 continue # Read S S_buffer=im_subset(0, 0, self.S_band.XSize, self.S_band.YSize, self.S_sub, pad_val=0) S_buffer.setBounds(s_xr[0]-KW, s_yr[0]-KW, s_nx+2.*KW, s_ny+2.*KW, update=1) S_img=S_buffer.z [0,:,:] if np.mean(S_img==0)> .25: # bail if > 25% 0 continue # To do: Not all these parameters are necessary on the # other side! params = ( count, t_pixels, KW, min_template_sigma, these_ind, Xc, Yc, s_nx, s_ny, dx0, dy0, T_img.copy(), S_img.copy() ) TaskParams.append(params) # To do: num processes must be a param! pool = Pool(processes=16) Out = pool.map(run_one_block, TaskParams) for i in range(len(TaskParams)): params = TaskParams[i] #out = run_one_block(params) # single-threaded out = Out[i] count = params[0] # Need not have count == i. self.C[these_ind[count]]=out[0] self.xyC[these_ind[count]]=[out[1], out[2]] if out[3] >= 0: self.sigma_template[these_ind[count]]=out[3] self.dxy[these_ind[count],:]=[out[4], out[5]] return (self.xyC).copy(), (self.dxy).copy(), (self.C).copy(), (self.sigma_template).copy()