def gdal_to_dat(dest, bandid='Data'): """ GDAL to Data format Parameters ---------- dest - GDAL format GDAL format bandid - str band identity """ dat = Data() gtr = dest.GetGeoTransform() rtmp = dest.GetRasterBand(1) dat.data = rtmp.ReadAsArray() nval = rtmp.GetNoDataValue() dat.data = np.ma.masked_equal(dat.data, nval) dat.data.set_fill_value(nval) dat.data = np.ma.fix_invalid(dat.data) dat.nrofbands = dest.RasterCount dat.tlx = gtr[0] dat.tly = gtr[3] dat.dataid = bandid dat.nullvalue = nval dat.rows = dest.RasterYSize dat.cols = dest.RasterXSize dat.xdim = abs(gtr[1]) dat.ydim = abs(gtr[5]) dat.wkt = dest.GetProjection() dat.gtr = gtr return dat
def gdal_to_dat(dest, bandid='Data'): """ GDAL to Data format Parameters ---------- dest - GDAL format GDAL format bandid - str band identity """ dat = Data() gtr = dest.GetGeoTransform() rtmp = dest.GetRasterBand(1) dat.data = rtmp.ReadAsArray() nval = rtmp.GetNoDataValue() dat.data = np.ma.masked_equal(dat.data, nval) dat.data.set_fill_value(nval) dat.data = np.ma.fix_invalid(dat.data) dat.nrofbands = dest.RasterCount dat.tlx = gtr[0] dat.tly = gtr[3] dat.dataid = bandid dat.nullvalue = nval dat.rows = dest.RasterYSize dat.cols = dest.RasterXSize dat.xdim = abs(gtr[1]) dat.ydim = abs(gtr[5]) dat.wkt = dest.GetProjection() dat.gtr = gtr return dat
def rtp(data, I_deg, D_deg): """ Reduction to the Pole """ datamedian = np.ma.median(data.data) ndat = data.data - datamedian ndat.data[ndat.mask] = 0 fftmod = np.fft.fft2(ndat) ny, nx = fftmod.shape nyqx = 1 / (2 * data.xdim) nyqy = 1 / (2 * data.ydim) kx = np.linspace(-nyqx, nyqx, nx) ky = np.linspace(-nyqy, nyqy, ny) KX, KY = np.meshgrid(kx, ky) I = np.deg2rad(I_deg) D = np.deg2rad(D_deg) alpha = np.arctan2(KY, KX) filt = 1 / (np.sin(I) + 1j * np.cos(I) * np.cos(D - alpha))**2 zrtp = np.fft.ifft2(fftmod * filt) zrtp = zrtp.real + datamedian # Create dataset dat = Data() dat.data = np.ma.masked_invalid(zrtp) dat.data.mask = np.ma.getmaskarray(data.data) dat.rows, dat.cols = zrtp.shape dat.nullvalue = data.data.fill_value dat.dataid = data.dataid dat.tlx = data.tlx dat.tly = data.tly dat.xdim = data.xdim dat.ydim = data.ydim return dat
def rtp(data, I_deg, D_deg): """ Reduction to the Pole """ datamedian = np.ma.median(data.data) ndat = data.data - datamedian ndat.data[ndat.mask] = 0 fftmod = np.fft.fft2(ndat) ny, nx = fftmod.shape nyqx = 1/(2*data.xdim) nyqy = 1/(2*data.ydim) kx = np.linspace(-nyqx, nyqx, nx) ky = np.linspace(-nyqy, nyqy, ny) KX, KY = np.meshgrid(kx, ky) I = np.deg2rad(I_deg) D = np.deg2rad(D_deg) alpha = np.arctan2(KY, KX) filt = 1/(np.sin(I)+1j*np.cos(I)*np.cos(D-alpha))**2 zrtp = np.fft.ifft2(fftmod*filt) zrtp = zrtp.real + datamedian # Create dataset dat = Data() dat.data = np.ma.masked_invalid(zrtp) dat.data.mask = np.ma.getmaskarray(data.data) dat.rows, dat.cols = zrtp.shape dat.nullvalue = data.data.fill_value dat.dataid = data.dataid dat.tlx = data.tlx dat.tly = data.tly dat.xdim = data.xdim dat.ydim = data.ydim return dat
def acceptall(self): """Accept.""" dxy = self.dsb_dxy.value() nullvalue = self.dsb_null.value() data = self.indata['Point'][0] newdat = [] for data in self.pbar.iter(self.indata['Point']): if data.dataid != self.dataid.currentText(): continue filt = (data.zdata != nullvalue) x = data.xdata[filt] y = data.ydata[filt] z = data.zdata[filt] for i in [x, y, z]: filt = np.logical_not(np.isnan(i)) x = x[filt] y = y[filt] z = z[filt] tmp = quickgrid(x, y, z, dxy, showtext=self.parent.showprocesslog) mask = np.ma.getmaskarray(tmp) gdat = tmp.data # Create dataset dat = Data() dat.data = np.ma.masked_invalid(gdat[::-1]) dat.data.mask = mask[::-1] dat.nullvalue = nullvalue dat.dataid = data.dataid dat.xdim = dxy dat.ydim = dxy dat.extent = [x.min(), x.max(), y.min(), y.max()] newdat.append(dat) self.outdata['Raster'] = newdat self.outdata['Point'] = self.indata['Point']
def gdal_to_dat(dest, bandid='Data'): """ GDAL to Data format. Parameters ---------- dest - GDAL format GDAL format bandid - str band identity """ dat = Data() gtr = dest.GetGeoTransform() nbands = dest.RasterCount if nbands == 1: rtmp = dest.GetRasterBand(1) dat.data = rtmp.ReadAsArray() else: dat.data = [] for i in range(nbands): rtmp = dest.GetRasterBand(i + 1) dat.data.append(rtmp.ReadAsArray()) dat.data = np.array(dat.data) dat.data = np.moveaxis(dat.data, 0, -1) nval = rtmp.GetNoDataValue() dat.data = np.ma.masked_equal(dat.data, nval) dat.data.set_fill_value(nval) dat.data = np.ma.fix_invalid(dat.data) dat.extent_from_gtr(gtr) dat.dataid = bandid dat.nullvalue = nval dat.wkt = dest.GetProjection() return dat
def acceptall(self): """ accept """ dxy = self.dsb_dxy.value() data = self.indata['Point'][0] newdat = [] for data in self.pbar.iter(self.indata['Point']): if data.dataid != self.dataid.currentText(): continue x = data.xdata y = data.ydata z = data.zdata for i in [x, y, z]: filt = np.logical_not(np.isnan(i)) x = x[filt] y = y[filt] z = z[filt] tmp = quickgrid(x, y, z, dxy, showtext=self.parent.showprocesslog) mask = np.ma.getmaskarray(tmp) gdat = tmp.data # Create dataset dat = Data() dat.data = np.ma.masked_invalid(gdat[::-1]) dat.data.mask = mask[::-1] dat.rows, dat.cols = gdat.shape dat.nullvalue = dat.data.fill_value dat.dataid = data.dataid dat.tlx = x.min() dat.tly = y.max() dat.xdim = dxy dat.ydim = dxy newdat.append(dat) self.outdata['Raster'] = newdat self.outdata['Point'] = self.indata['Point']
def tiltdepth(self, data): """ Calculate tilt depth """ self.pbar.setValue(0) self.pbar.setMaximum(4) # RTP inc = self.dsb_inc.value() dec = self.dsb_dec.value() zout = dataprep.rtp(data, inc, dec) # Tilt self.pbar.setValue(1) nr, nc = zout.data.shape dy, dx = np.gradient(zout.data) dxtot = np.sqrt(dx**2 + dy**2) dz = cooper.vertical(zout.data) t1 = np.arctan(dz / dxtot) self.pbar.setValue(2) # A negative number implies we are straddling 0 # Contour tilt x = zout.extent[0] + np.arange(nc) * zout.xdim + zout.xdim / 2 y = zout.extent[-1] - np.arange(nr) * zout.ydim - zout.ydim / 2 X, Y = np.meshgrid(x, y) Z = np.rad2deg(t1) self.X = X self.Y = Y self.Z = Z cnt0 = self.axes.contour(X, Y, Z, [0]) cnt45 = self.axes.contour(X, Y, Z, [45], alpha=0) cntm45 = self.axes.contour(X, Y, Z, [-45], alpha=0) self.pbar.setValue(3) gx0, gy0, cgrad0, cntid0 = vgrad(cnt0) gx45, gy45, _, _ = vgrad(cnt45) gxm45, gym45, _, _ = vgrad(cntm45) g0 = np.transpose([gx0, gy0]) self.pbar.setValue(4) dmin1 = [] dmin2 = [] for i, j in self.pbar.iter(g0): dmin1.append(distpc(gx45, gy45, i, j, 0)) dmin2.append(distpc(gxm45, gym45, i, j, 0)) dx1 = gx45[dmin1] - gx0 dy1 = gy45[dmin1] - gy0 dx2 = gxm45[dmin2] - gx0 dy2 = gym45[dmin2] - gy0 grad = np.arctan2(dy1, dx1) * 180 / pi grad[grad > 90] -= 180 grad[grad < -90] += 180 gtmp1 = np.abs(90 - np.abs(grad - cgrad0)) grad = np.arctan2(dy2, dx2) * 180 / pi grad[grad > 90] -= 180 grad[grad < -90] += 180 gtmp2 = np.abs(90 - np.abs(grad - cgrad0)) gtmp = np.logical_and(gtmp1 <= 10, gtmp2 <= 10) gx0 = gx0[gtmp] gy0 = gy0[gtmp] cntid0 = cntid0[gtmp] dx1 = dx1[gtmp] dy1 = dy1[gtmp] dx2 = dx2[gtmp] dy2 = dy2[gtmp] dist1 = np.sqrt(dx1**2 + dy1**2) dist2 = np.sqrt(dx2**2 + dy2**2) dist = np.min([dist1, dist2], 0) self.x0 = gx0 self.x1 = dx1 + gx0 self.x2 = dx2 + gx0 self.y0 = gy0 self.y1 = dy1 + gy0 self.y2 = dy2 + gy0 self.depths = np.transpose([gx0, gy0, cntid0.astype(int), dist]) tmp = dataprep.quickgrid(gx0, gy0, dist, data.xdim, showtext=self.showtext) mask = np.ma.getmaskarray(tmp) gdat = tmp.data dat = Data() dat.data = np.ma.masked_invalid(gdat[::-1]) dat.data.mask = mask[::-1] dat.nullvalue = dat.data.fill_value dat.dataid = data.dataid dat.xdim = data.xdim dat.ydim = data.xdim dat.extent = [gx0.min(), gx0.max(), gy0.min(), gy0.max()] self.outdata['Raster'] = [dat]
def grid(): """ First 2 columns must be x and y """ filename = r'C:\Work\Programming\pygmi\data\sue\filt_magdata.csv' ofile = r'C:\Work\Programming\pygmi\data\magdata.tif' srows = 0 dlim = None xcol = 0 ycol = 1 zcol = 2 dxy = 15 # This bit reads in the first line to see if it is a header pntfile = open(filename) ltmp = pntfile.readline() pntfile.close() ltmp = ltmp.lower() isheader = any(c.isalpha() for c in ltmp) # Check for comma delimiting if ',' in ltmp: dlim = ',' # Set skip rows if isheader: srows = 1 # Now read in data datatmp = np.genfromtxt(filename, unpack=True, delimiter=dlim, skip_header=srows, usemask=False) # Now we interpolate xdata = datatmp[xcol] ydata = datatmp[ycol] zdata = datatmp[zcol] points = datatmp[:2].T newxdata = np.arange(xdata.min(), xdata.max(), dxy) newydata = np.arange(ydata.min(), ydata.max(), dxy) newpoints = np.meshgrid(newxdata, newydata) newpoints = (newpoints[0].flatten(), newpoints[1].flatten()) grid = si.griddata(points, zdata, newpoints, method='cubic') grid.shape = (newydata.shape[0], newxdata.shape[0]) grid = grid[::-1] # export data odat = Data() odat.dataid = '' odat.tlx = newxdata.min() odat.tly = newydata.max() odat.xdim = dxy odat.ydim = dxy odat.nrofbands = 1 odat.nullvalue = 1e+20 odat.rows, odat.cols = grid.shape odat.data = np.ma.masked_invalid(grid) tmp = pio.ExportData(None) tmp.ifile = ofile # tmp.export_ascii_xyz([odat]) # tmp.export_gdal([odat], 'ENVI') tmp.export_gdal([odat], 'GTiff') # Plotting section # dataex = (newxdata.min(), newxdata.max(), newydata.min(), newydata.max()) # plt.imshow(grid, cmap = plt.cm.jet, extent=dataex, origin='upper') plt.tricontourf(xdata, ydata, zdata, 40, cmap=plt.cm.jet) # plt.plot(xdata, ydata, '.') plt.colorbar() plt.show() pdb.set_trace()