def test_irapasc_export_and_import(): """Export Irap ASCII and binary and import again.""" logger.info("Export to Irap Classic and Binary") x = xtgeo.RegularSurface( ncol=120, nrow=100, xori=1000, yori=5000, xinc=40, yinc=20, values=np.random.rand(120, 100), ) assert_equal(x.ncol, 120) mean1 = x.values.mean() x.to_file("TMP/irap2_a.fgr", fformat="irap_ascii") x.to_file("TMP/irap2_b.gri", fformat="irap_binary") fsize = os.path.getsize("TMP/irap2_b.gri") logger.info(fsize) assert_equal(fsize, 48900) # import irap ascii y = xtgeo.RegularSurface() y.from_file("TMP/irap2_a.fgr", fformat="irap_ascii") mean2 = y.values.mean() assert_almostequal(mean1, mean2, 0.0001)
def test_get_randomline_frompolygon(xtgshow): """Test randomline with both bilinear and nearest sampling for surfaces.""" fence = xtgeo.Polygons(FENCE1) xs = xtgeo.RegularSurface(TESTSET1) # get the polygon fspec = fence.get_fence(distance=10, nextend=2, asnumpy=False) assert_almostequal(fspec.dataframe[fspec.dhname][4], 10, 1) fspec = fence.get_fence(distance=20, nextend=5, asnumpy=True) arr1 = xs.get_randomline(fspec) arr2 = xs.get_randomline(fspec, sampling="nearest") x = arr1[:, 0] y1 = arr1[:, 1] y2 = arr2[:, 1] assert y1.mean() == pytest.approx(1706.7514, abs=0.001) assert y2.mean() == pytest.approx(1706.6995, abs=0.001) if xtgshow: import matplotlib.pyplot as plt plt.figure() plt.plot(x, y1) plt.plot(x, y2) plt.gca().invert_yaxis() plt.show()
def test_fence(): """Test sampling a fence from a surface.""" myfence = np.array( [ [462174.6191406, 5930073.3461914, 721.711059], [462429.4677734, 5930418.2055664, 720.909423], [462654.6738281, 5930883.9331054, 712.587158], [462790.8710937, 5931501.4443359, 676.873901], [462791.5273437, 5932040.4306640, 659.938476], [462480.2958984, 5932846.7387695, 622.102172], [462226.7070312, 5933397.8632812, 628.067138], [462214.4921875, 5933753.4936523, 593.260864], [462161.5048828, 5934327.8398437, 611.253540], [462325.0673828, 5934688.7519531, 626.485107], [462399.0429687, 5934975.2934570, 640.868774], ] ) logger.debug("NP:") logger.debug(myfence) print(myfence) x = xtgeo.RegularSurface(TESTSET1) newfence = x.get_fence(myfence) logger.debug("updated NP:") logger.debug(newfence) print(newfence) assert_almostequal(newfence[1][2], 1720.9094, 0.01)
def test_irapasc_import1_engine_python(): """Import Reek Irap ascii using python read engine""" logger.info("Import and export...") xsurf = xtgeo.RegularSurface(TESTSET3, fformat="irap_ascii", engine="python") assert xsurf.ncol == 1264 assert xsurf.nrow == 2010 assert_almostequal(xsurf.values[11, 0], 1678.89733887, 0.001) assert_almostequal(xsurf.values[1263, 2009], 1893.75, 0.01)
def test_irapbin_import1(): """Import Reek Irap binary.""" logger.info("Import and export...") xsurf = xtgeo.RegularSurface(TESTSET2) assert xsurf.ncol == 1264 assert xsurf.nrow == 2010 assert_almostequal(xsurf.values[11, 0], 1678.89733887, 0.00001) assert_almostequal(xsurf.values[1263, 2009], 1893.75, 0.01) xsurf.describe()
def test_statistics(): """Find the mean etc measures of the surfaces.""" flist = [TESTSET1A, TESTSET1B] surfs = xtgeo.Surfaces(flist) res = surfs.statistics() res["mean"].to_file(join(TMPD, "surf_mean.gri")) res["std"].to_file(join(TMPD, "surf_std.gri")) assert_almostequal(res["mean"].values.mean(), 1720.5029, 0.0001) assert_almostequal(res["std"].values.min(), 3.7039, 0.0001)
def test_ijxyz_import4_ow_messy_dat(): """Import some IJ XYZ small set with YFLIP -1 from OW messy dat format.""" logger.info("Import and export...") xsurf = xtgeo.RegularSurface() xsurf.from_file(TESTSET4D, fformat="ijxyz") xsurf.describe() assert_almostequal(xsurf.values.mean(), 5037.5840, 0.001) assert xsurf.ncol == 51 assert xsurf.yflip == -1 assert xsurf.nactive == 2578 xsurf.to_file(os.path.join(TMPD, "ijxyz_set4d.gri"))
def test_ijxyz_import1(): """Import some IJ XYZ format, typical seismic.""" logger.info("Import and export...") xsurf = xtgeo.RegularSurface() xsurf.from_file(TESTSET4A, fformat="ijxyz") xsurf.describe() assert_almostequal(xsurf.xori, 600413.048444, 0.0001) assert_almostequal(xsurf.xinc, 25.0, 0.0001) assert xsurf.ncol == 280 assert xsurf.nrow == 1341 xsurf.to_file(os.path.join(TMPD, "ijxyz_set4a.gri"))
def test_ijxyz_import2(tmpdir): """Import some IJ XYZ small set with YFLIP -1.""" logger.info("Import and export...") xsurf = xtgeo.RegularSurface() xsurf.from_file(TESTSET4B, fformat="ijxyz") xsurf.describe() assert_almostequal(xsurf.values.mean(), 5037.5840, 0.001) assert xsurf.ncol == 51 assert xsurf.yflip == -1 assert xsurf.nactive == 2578 xsurf.to_file(join(tmpdir, "ijxyz_set4b.gri"))
def test_value_from_xy(): """ get Z value from XY point """ x = xtgeo.RegularSurface() x.from_file(TESTSET1, fformat="irap_binary") z = x.get_value_from_xy(point=(460181.036, 5933948.386)) assert_almostequal(z, 1625.11, 0.01) # outside value shall return None z = x.get_value_from_xy(point=(0.0, 7337128.076)) assert z is None
def test_smoothing(): """Smooth the the surface""" srf = xtgeo.RegularSurface() srf.from_file(TESTSET1, fformat="irap_binary") mean1 = srf.values.mean() assert_almostequal(mean1, 1698.65, 0.1) srf.smooth(iterations=1, width=5) mean2 = srf.values.mean() assert_almostequal(mean2, 1698.65, 0.3) # smoothed ~same mean assert mean1 != mean2 # but not exacly same
def test_swapaxes(): """Import Reek Irap binary and swap axes.""" xsurf = xtgeo.RegularSurface(TESTSET5) xsurf.describe() logger.info(xsurf.yflip) xsurf.to_file("TMP/notswapped.gri") val1 = xsurf.values.copy() xsurf.swapaxes() xsurf.describe() logger.info(xsurf.yflip) xsurf.to_file("TMP/swapped.gri") xsurf.swapaxes() val2 = xsurf.values.copy() xsurf.to_file("TMP/swapped_reswapped.gri") valdiff = val2 - val1 assert_almostequal(valdiff.mean(), 0.0, 0.00001) assert_almostequal(valdiff.std(), 0.0, 0.00001)
def test_minmax_rotated_map(): """Min and max of rotated map""" logger.info("Import and export...") x = xtgeo.RegularSurface() x.from_file(TESTSET1, fformat="irap_binary") assert_almostequal(x.xmin, 454637.6, 0.1) assert_almostequal(x.xmax, 468895.1, 0.1) assert_almostequal(x.ymin, 5925995.0, 0.1) assert_almostequal(x.ymax, 5939998.7, 0.1)
def test_surfaces_apply(): """Test apply function.""" base = xtgeo.RegularSurface(TESTSET1A) base.describe() base.values *= 0.0 bmean = base.values.mean() surfs = [base] for inum in range(1, 101): tmp = base.copy() tmp.values += float(inum) surfs.append(tmp) so = xtgeo.Surfaces(surfs) res = so.apply(np.nanmean) assert_almostequal(res.values.mean(), bmean + 50.0, 0.0001) res = so.apply(np.nanpercentile, 10, axis=0, interpolation="nearest") assert_almostequal(res.values.mean(), bmean + 10.0, 0.0001)
def test_get_surfaces_from_3dgrid(): """Create surfaces from a 3D grid.""" mygrid = xtgeo.Grid(TESTSETG1) surfs = xtgeo.Surfaces() surfs.from_grid3d(mygrid, rfactor=2) surfs.describe() assert_almostequal(surfs.surfaces[-1].values.mean(), 1742.28, 0.04) assert_almostequal(surfs.surfaces[-1].values.min(), 1589.58, 0.04) assert_almostequal(surfs.surfaces[-1].values.max(), 1977.29, 0.04) assert_almostequal(surfs.surfaces[0].values.mean(), 1697.02, 0.04) for srf in surfs.surfaces: srf.to_file(join(TMPD, srf.name + ".gri"))
def test_irapbin_io(): """Import and export Irap binary.""" logger.info("Import and export...") x = xtgeo.RegularSurface() x.from_file(TESTSET1, fformat="irap_binary") x.to_file("TMP/reek1_test.fgr", fformat="irap_ascii") logger.debug("NX is %s", x.ncol) assert_equal(x.ncol, 554) # get the 1D numpy v1d = x.get_zval() logger.info("Mean VALUES are: %s", np.nanmean(v1d)) zval = x.values # add value via numpy zval = zval + 300 # update x.values = zval assert_almostequal(x.values.mean(), 1998.648, 0.01) x.to_file("TMP/reek1_plus_300_a.fgr", fformat="irap_ascii") x.to_file("TMP/reek1_plus_300_b.gri", fformat="irap_binary") mfile = TESTSET1 # direct import y = xtgeo.RegularSurface(mfile) assert_equal(y.ncol, 554) # semidirect import cc = xtgeo.RegularSurface().from_file(mfile) assert_equal(cc.ncol, 554)
def test_irapbin_import_metadatafirst(): """Import Reek Irap binary, first with metadata only, then values.""" logger.info("Import and export...") nsurf = 10 sur = [] t1 = xtg.timer() for ix in range(nsurf): sur.append(xtgeo.RegularSurface(TESTSET2, values=False)) t2 = xtg.timer(t1) logger.info("Loading %s surfaces lazy took %s secs.", nsurf, t2) assert sur[nsurf - 1].ncol == 1264 t1 = xtg.timer() for ix in range(nsurf): sur[ix].load_values() t2 = xtg.timer(t1) logger.info("Loading %s surfaces actual values took %s secs.", nsurf, t2) assert sur[nsurf - 1].ncol == 1264 assert sur[nsurf - 1].nrow == 2010 assert_almostequal(sur[nsurf - 1].values[11, 0], 1678.89733887, 0.00001)
def test_dataframe_more(): """Get a pandas Dataframe object, more detailed testing""" xmap = xtgeo.RegularSurface(TESTSET1) xmap.describe() dfrc = xmap.dataframe(ijcolumns=True, order="C", activeonly=True) dfrf = xmap.dataframe(ijcolumns=True, order="F", activeonly=True) dfrc.to_csv(os.path.join(TMPD, "regsurf_df_c.csv")) dfrf.to_csv(os.path.join(TMPD, "regsurf_df_f.csv")) xmap.to_file(os.path.join(TMPD, "regsurf_df.ijxyz"), fformat="ijxyz") assert_almostequal(dfrc["X_UTME"][2], 465956.274, 0.01) assert_almostequal(dfrf["X_UTME"][2], 462679.773, 0.01) dfrcx = xmap.dataframe(ijcolumns=False, order="C", activeonly=True) dfrcx.to_csv(os.path.join(TMPD, "regsurf_df_noij_c.csv")) dfrcy = xmap.dataframe( ijcolumns=False, order="C", activeonly=False, fill_value=None ) dfrcy.to_csv(os.path.join(TMPD, "regsurf_df_noij_c_all.csv"))
def test_more_statistics(): """Find the mean etc measures of the surfaces.""" base = xtgeo.RegularSurface(TESTSET1A) base.values *= 0.0 bmean = base.values.mean() surfs = [] surfs.append(base) # this will get 101 constant maps ranging from 0 til 100 for inum in range(1, 101): tmp = base.copy() tmp.values += float(inum) surfs.append(tmp) so = xtgeo.Surfaces(surfs) res = so.statistics() # theoretical stdev: sum2 = 0.0 for inum in range(0, 101): sum2 += (float(inum) - 50.0) ** 2 stdev = math.sqrt(sum2 / 100.0) # total 101 samples, use N-1 assert_almostequal(res["mean"].values.mean(), bmean + 50.0, 0.0001) assert_almostequal(res["std"].values.mean(), stdev, 0.0001) small = xtgeo.RegularSurface() so2 = xtgeo.Surfaces() for inum in range(10): tmp = small.copy() tmp.values += 8.76543 so2.append([tmp]) res2 = so2.statistics(percentiles=[10, 50]) assert res2["p10"].values.mean() == pytest.approx(16.408287142, 0.001)
def test_fill(): """Fill the undefined values for the surface""" srf = xtgeo.RegularSurface() srf.from_file(TESTSET1, fformat="irap_binary") minv1 = srf.values.min() assert_almostequal(srf.values.mean(), 1698.648, 0.001) srf.fill() minv2 = srf.values.min() assert_almostequal(srf.values.mean(), 1705.201, 0.001) assert_almostequal(minv1, minv2, 0.000001) srf = xtgeo.RegularSurface() srf.from_file(TESTSET1, fformat="irap_binary") srf.fill(444) assert_almostequal(srf.values.mean(), 1342.10498, 0.001)
def test_get_xy_values1d(): """Get the XY coordinate values""" xmap = xtgeo.RegularSurface() xcv, _ycv = xmap.get_xy_values1d(activeonly=False, order="C") assert_almostequal(xcv[1], 0.0, 0.001) xcv, _ycv = xmap.get_xy_values1d(activeonly=False, order="F") assert_almostequal(xcv[1], 25.0, 0.001) xcv, _ycv = xmap.get_xy_values1d(activeonly=True, order="C") assert_almostequal(xcv[1], 0.0, 0.001) xcv, _ycv = xmap.get_xy_values1d(activeonly=True, order="F") assert_almostequal(xcv[1], 25.0, 0.001)
def test_get_xy_values(): """Get the XY coordinate values as 2D arrays""" xmap = xtgeo.RegularSurface() xcv, _ycv = xmap.get_xy_values(order="C") xxv = xcv.ravel(order="K") assert_almostequal(xxv[1], 0.0, 0.001) xcv, _ycv = xmap.get_xy_values(order="F") xxv = xcv.ravel(order="K") assert_almostequal(xxv[1], 25.0, 0.001) xcv, _ycv = xmap.get_xy_values(order="C", asmasked=True) xxv = xcv.ravel(order="K") assert_almostequal(xxv[1], 0.0, 0.001) xcv, _ycv = xmap.get_xy_values(order="F", asmasked=True) xxv = xcv.ravel(order="K") assert_almostequal(xxv[1], 25.0, 0.001)
def test_dataframe_simple(): """Get a pandas Dataframe object""" xmap = xtgeo.RegularSurface(TESTSET1) dfrc = xmap.dataframe(ijcolumns=True, order="C", activeonly=True) assert_almostequal(dfrc["X_UTME"][2], 465956.274, 0.01) xmap = xtgeo.surface_from_file(TESTSET2) dfrc = xmap.dataframe() assert_almostequal(dfrc["X_UTME"][2], 461582.562498, 0.01) xmap.coarsen(2) dfrc = xmap.dataframe() assert_almostequal(dfrc["X_UTME"][2], 461577.5575, 0.01)
def test_twosurfaces_oper(): """Test operations between two surface in more different ways""" surf1 = xtgeo.RegularSurface(TESTSET1) surf2 = xtgeo.RegularSurface(TESTSET1A) iso1 = surf2.copy() iso1.values -= surf1.values iso1mean = iso1.values.mean() assert_almostequal(iso1mean, 43.71, 0.01) iso2 = surf2.copy() iso2.subtract(surf1) iso2mean = iso2.values.mean() assert_almostequal(iso2mean, 43.71, 0.01) assert iso1.values.all() == iso2.values.all() iso3 = surf2 - surf1 assert iso1.values.all() == iso3.values.all() assert isinstance(iso3, xtgeo.RegularSurface) sum1 = surf2.copy() sum1.values += surf1.values assert_almostequal(sum1.values.mean(), 3441.0, 0.01) sum2 = surf2.copy() sum2.add(surf1) assert sum1.values.all() == sum2.values.all() sum3 = surf1 + surf2 assert sum1.values.all() == sum3.values.all() zrf2 = surf2.copy() zrf1 = surf1.copy() newzrf1 = surf1.copy() newzrf1.values = zrf2.values / zrf1.values assert newzrf1.values.mean() == pytest.approx(1.0257, abs=0.01)