def test_quickgrid(): """test quick grid.""" dat2 = [[1, 1], [2, 1.3333333333333333]] x = np.array([1, 2, 1]) y = np.array([1, 1, 2]) z = np.array([1, 1, 2]) dat = dataprep.quickgrid(x, y, z, 1) np.testing.assert_array_equal(dat, dat2)
def tests(): """ Tests to debug """ app = QtWidgets.QApplication(sys.argv) ttt = PTime() aaa = dp.GroupProj('Input Projection') ttt.since_last_call() pdb.set_trace() points = np.random.rand(1000000, 2) values = dp.func(points[:, 0], points[:, 1]) dat = dp.quickgrid(points[:, 0], points[:, 1], values, .001, numits=-1) plt.imshow(dat) plt.colorbar() plt.show()
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 get_modis(ifile): """ Gets MODIS data Parameters ---------- ifile : str filename to import Returns ------- dat : PyGMI raster Data dataset imported """ dat = [] ifile = ifile[:] dataset = gdal.Open(ifile, gdal.GA_ReadOnly) subdata = dataset.GetSubDatasets() latentry = [i for i in subdata if 'Latitude' in i[1]] subdata.pop(subdata.index(latentry[0])) dataset = None dataset = gdal.Open(latentry[0][0], gdal.GA_ReadOnly) rtmp = dataset.GetRasterBand(1) lats = rtmp.ReadAsArray() latsdim = ((lats.max() - lats.min()) / (lats.shape[0] - 1)) / 2 lonentry = [i for i in subdata if 'Longitude' in i[1]] subdata.pop(subdata.index(lonentry[0])) dataset = None dataset = gdal.Open(lonentry[0][0], gdal.GA_ReadOnly) rtmp = dataset.GetRasterBand(1) lons = rtmp.ReadAsArray() lonsdim = ((lons.max() - lons.min()) / (lons.shape[1] - 1)) / 2 lonsdim = latsdim tlx = lons.min() - abs(lonsdim / 2) tly = lats.max() + abs(latsdim / 2) cols = int((lons.max() - lons.min()) / lonsdim) + 1 rows = int((lats.max() - lats.min()) / latsdim) + 1 newx2, newy2 = np.mgrid[0:rows, 0:cols] newx2 = newx2 * lonsdim + tlx newy2 = tlx - newy2 * latsdim tmp = [] for i in subdata: if 'HDF4_EOS:EOS_SWATH' in i[0]: tmp.append(i) subdata = tmp i = -1 for ifile2, bandid2 in subdata: dataset = None dataset = gdal.Open(ifile2, gdal.GA_ReadOnly) rtmp2 = dataset.ReadAsArray() if rtmp2.shape[-1] == min(rtmp2.shape) and rtmp2.ndim == 3: rtmp2 = np.transpose(rtmp2, (2, 0, 1)) nbands = 1 if rtmp2.ndim == 3: nbands = rtmp2.shape[0] for i2 in range(nbands): rtmp = dataset.GetRasterBand(i2 + 1) bandid = rtmp.GetDescription() nval = rtmp.GetNoDataValue() i += 1 dat.append(Data()) if rtmp2.ndim == 3: dat[i].data = rtmp2[i2] else: dat[i].data = rtmp2 newx = lons[dat[i].data != nval] newy = lats[dat[i].data != nval] newz = dat[i].data[dat[i].data != nval] if newx.size == 0: dat[i].data = np.zeros((rows, cols)) + nval else: tmp = quickgrid(newx, newy, newz, latsdim) mask = np.ma.getmaskarray(tmp) gdat = tmp.data dat[i].data = np.ma.masked_invalid(gdat[::-1]) dat[i].data.mask = mask[::-1] if dat[i].data.dtype.kind == 'i': if nval is None: nval = 999999 nval = int(nval) elif dat[i].data.dtype.kind == 'u': if nval is None: nval = 0 nval = int(nval) else: if nval is None: nval = 1e+20 nval = float(nval) dat[i].data = np.ma.masked_invalid(dat[i].data) dat[i].data.mask = (np.ma.getmaskarray(dat[i].data) | (dat[i].data == nval)) if dat[i].data.mask.size == 1: dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) + np.ma.getmaskarray(dat[i].data)) dat[i].dataid = bandid2 + ' ' + bandid dat[i].nullvalue = nval dat[i].xdim = abs(lonsdim) dat[i].ydim = abs(latsdim) rows, cols = dat[i].data.shape xmin = tlx ymax = tly ymin = ymax - rows * dat[i].ydim xmax = xmin + cols * dat[i].xdim dat[i].extent = [xmin, xmax, ymin, ymax] srs = osr.SpatialReference() srs.ImportFromWkt(dataset.GetProjection()) srs.AutoIdentifyEPSG() dat[i].wkt = srs.ExportToWkt() dataset = None return dat
def get_modis(ifile): """ Gets MODIS data Parameters ---------- ifile : str filename to import Returns ------- dat : PyGMI raster Data dataset imported """ dat = [] ifile = ifile[:] dataset = gdal.Open(ifile, gdal.GA_ReadOnly) subdata = dataset.GetSubDatasets() latentry = [i for i in subdata if 'Latitude' in i[1]] subdata.pop(subdata.index(latentry[0])) dataset = gdal.Open(latentry[0][0], gdal.GA_ReadOnly) rtmp = dataset.GetRasterBand(1) lats = rtmp.ReadAsArray() latsdim = ((lats.max()-lats.min())/(lats.shape[0]-1))/2 lonentry = [i for i in subdata if 'Longitude' in i[1]] subdata.pop(subdata.index(lonentry[0])) dataset = gdal.Open(lonentry[0][0], gdal.GA_ReadOnly) rtmp = dataset.GetRasterBand(1) lons = rtmp.ReadAsArray() lonsdim = ((lons.max()-lons.min())/(lons.shape[1]-1))/2 lonsdim = latsdim tlx = lons.min()-abs(lonsdim/2) tly = lats.max()+abs(latsdim/2) cols = int((lons.max()-lons.min())/lonsdim)+1 rows = int((lats.max()-lats.min())/latsdim)+1 newx2, newy2 = np.mgrid[0:rows, 0:cols] newx2 = newx2*lonsdim + tlx newy2 = tlx - newy2*latsdim tmp = [] for i in subdata: if 'HDF4_EOS:EOS_SWATH' in i[0]: tmp.append(i) subdata = tmp i = -1 for ifile2, bandid2 in subdata: dataset = gdal.Open(ifile2, gdal.GA_ReadOnly) gtr = dataset.GetGeoTransform() rtmp2 = dataset.ReadAsArray() if rtmp2.shape[-1] == min(rtmp2.shape) and rtmp2.ndim == 3: rtmp2 = np.transpose(rtmp2, (2, 0, 1)) nbands = 1 if rtmp2.ndim == 3: nbands = rtmp2.shape[0] for i2 in range(nbands): rtmp = dataset.GetRasterBand(i2+1) bandid = rtmp.GetDescription() nval = rtmp.GetNoDataValue() i += 1 dat.append(Data()) if rtmp2.ndim == 3: dat[i].data = rtmp2[i2] else: dat[i].data = rtmp2 newx = lons[dat[i].data != nval] newy = lats[dat[i].data != nval] newz = dat[i].data[dat[i].data != nval] if newx.size == 0: dat[i].data = np.zeros((rows, cols)) + nval else: tmp = quickgrid(newx, newy, newz, latsdim) mask = np.ma.getmaskarray(tmp) gdat = tmp.data dat[i].data = np.ma.masked_invalid(gdat[::-1]) dat[i].data.mask = mask[::-1] if dat[i].data.dtype.kind == 'i': if nval is None: nval = 999999 nval = int(nval) elif dat[i].data.dtype.kind == 'u': if nval is None: nval = 0 nval = int(nval) else: if nval is None: nval = 1e+20 nval = float(nval) dat[i].data = np.ma.masked_invalid(dat[i].data) dat[i].data.mask = np.ma.getmaskarray(dat[i].data) | (dat[i].data == nval) if dat[i].data.mask.size == 1: dat[i].data.mask = (np.ma.make_mask_none(dat[i].data.shape) + np.ma.getmaskarray(dat[i].data)) dat[i].nrofbands = dataset.RasterCount dat[i].tlx = tlx dat[i].tly = tly dat[i].dataid = bandid2+' '+bandid dat[i].nullvalue = nval dat[i].rows = dat[i].data.shape[0] dat[i].cols = dat[i].data.shape[1] dat[i].xdim = abs(lonsdim) dat[i].ydim = abs(latsdim) dat[i].gtr = gtr srs = osr.SpatialReference() srs.ImportFromWkt(dataset.GetProjection()) srs.AutoIdentifyEPSG() dat[i].wkt = srs.ExportToWkt() return dat