def test_main_subparsers(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir.join(".TMP-satfunc.csv") sys.argv = ["ecl2csv", "satfunc", DATAFILE, "-o", str(tmpcsvfile)] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty tmpcsvfile2 = tmpdir.join(".TMP-satfunc-swof.csv") print(tmpcsvfile2) sys.argv = [ "ecl2csv", "satfunc", DATAFILE, "--keywords", "SWOF", "--output", str(tmpcsvfile2), ] ecl2csv.main() assert os.path.exists(str(tmpcsvfile2)) disk_df = pd.read_csv(str(tmpcsvfile2)) assert set(disk_df["KEYWORD"].unique()) == {"SWOF"}
def test_emptytree_commandlinetool(tmpdir, mocker, caplog): tmpdir.chdir() Path("EMPTY.DATA").write_text("") mocker.patch("sys.argv", ["ecl2csv", "gruptree", "--prettyprint", "EMPTY.DATA"]) ecl2csv.main() assert "No tree data to prettyprint" in caplog.text
def test_main_subparsers(tmpdir, mocker): """Test command line interface""" tmpcsvfile = tmpdir / ".TMP-rft.csv" mocker.patch("sys.argv", ["ecl2csv", "rft", DATAFILE, "-o", str(tmpcsvfile)]) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty tmpcsvfile = tmpdir / ".TMP-rft2.csv" # Test with RFT file as argument: sys.argv = [ "ecl2cvsv", "rft", "-v", DATAFILE.replace(".DATA", ".RFT"), "-o", str(tmpcsvfile), ] ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_sgof_satnuminferrer(tmpdir, mocker): """Test inferring of SATNUMS in SGOF strings""" sgofstr = """ SGOF 0 0 1 1 1 1 0 0 / 0 0 1 1 0.5 0.5 0.5 0.5 1 1 0 0 / 0 0 1 0 0.1 0.1 0.1 0.1 1 1 0 0 / """ tmpdir.chdir() assert inferdims.guess_dim(sgofstr, "TABDIMS", 0) == 3 sgofdf = satfunc.df(sgofstr) assert "SATNUM" in sgofdf assert len(sgofdf["SATNUM"].unique()) == 3 assert len(sgofdf) == 8 inc = satfunc.df2ecl(sgofdf) df_from_inc = satfunc.df(inc) pd.testing.assert_frame_equal(sgofdf, df_from_inc) # Write to file and try to parse it with command line: sgoffile = "__sgof_tmp.txt" Path(sgoffile).write_text(sgofstr) mocker.patch( "sys.argv", ["ecl2csv", "satfunc", "-v", sgoffile, "-o", sgoffile + ".csv"]) ecl2csv.main() parsed_sgof = pd.read_csv(sgoffile + ".csv") assert len(parsed_sgof["SATNUM"].unique()) == 3
def test_main(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir.join(".TMP-eclgrid.csv") sys.argv = [ "eclgrid2csv", DATAFILE, "-o", str(tmpcsvfile), "--init", "PORO" ] grid.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty os.remove(str(tmpcsvfile)) # Do again with also restarts, and using subparsers: sys.argv = [ "ecl2csv", "grid", DATAFILE, "-o", str(tmpcsvfile), "--rstdate", "first", "--init", "PORO", ] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty os.remove(str(tmpcsvfile)) # Do again with also restarts: sys.argv = [ "eclgrid2csv", DATAFILE, "-o", str(tmpcsvfile), "--rstdate", "2001-02-01", "--init", "PORO", ] grid.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty os.remove(str(tmpcsvfile)) # Test with constants dropping sys.argv = [ "eclgrid2csv", DATAFILE, "-o", str(tmpcsvfile), "--dropconstants" ] grid.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) # That PVTNUM is constant is a particular feature # of the test dataset. assert "PVTNUM" not in disk_df assert not disk_df.empty
def test_main_subparsers(tmpdir, mocker): """Test command line interface""" tmpcsvfile = tmpdir.join("satfunc.csv") mocker.patch("sys.argv", ["ecl2csv", "satfunc", DATAFILE, "-o", str(tmpcsvfile)]) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty tmpcsvfile2 = tmpdir.join(".TMP-satfunc-swof.csv") print(tmpcsvfile2) mocker.patch( "sys.argv", [ "ecl2csv", "satfunc", DATAFILE, "--keywords", "SWOF", "--output", str(tmpcsvfile2), ], ) ecl2csv.main() assert Path(tmpcsvfile2).is_file() disk_df = pd.read_csv(str(tmpcsvfile2)) assert set(disk_df["KEYWORD"].unique()) == {"SWOF"}
def test_main_subparser(tmpdir, mocker): """Test command line interface""" tmpdir.chdir() tmpcsvfile = "equil.csv" mocker.patch("sys.argv", ["ecl2csv", "equil", "-v", DATAFILE, "-o", tmpcsvfile]) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(tmpcsvfile) assert not disk_df.empty # Test the reverse operation: mocker.patch( "sys.argv", ["csv2ecl", "equil", "-v", "--output", "equil.inc", tmpcsvfile]) csv2ecl.main() # NB: cvs2ecl does not output the phase configuration! phases = "WATER\nGAS\nOIL\n\n" ph_equil_inc = Path("phasesequil.inc") ph_equil_inc.write_text(phases + Path("equil.inc").read_text()) pd.testing.assert_frame_equal(equil.df(ph_equil_inc.read_text()), disk_df) # Test via stdout: result = subprocess.run( ["csv2ecl", "equil", "--output", "-", tmpcsvfile], stdout=subprocess.PIPE, ) pd.testing.assert_frame_equal( equil.df(phases + result.stdout.decode()), disk_df, check_like=True, )
def test_main(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir / "trans.csv" sys.argv = ["ecl2csv", "trans", "-v", DATAFILE, "-o", str(tmpcsvfile)] ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_main(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir.join(".TMP-trans.csv") sys.argv = ["ecl2csv", "trans", "-v", DATAFILE, "-o", str(tmpcsvfile)] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_main_subparser(tmpdir): """Test command line interface with subparsers""" tmpcsvfile = tmpdir.join(".TMP-faultsdf.csv") sys.argv = ["ecl2csv", "faults", DATAFILE, "-o", str(tmpcsvfile)] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_main_subparsers(tmpdir, mocker): """Test command line interface""" tmpcsvfile = tmpdir / ".TMP-wcondf.csv" mocker.patch("sys.argv", ["ecl2csv", "wcon", DATAFILE, "-o", str(tmpcsvfile)]) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_main_subparser(tmp_path, mocker): """Test command line interface""" tmpcsvfile = tmp_path / "gruptree.csv" mocker.patch("sys.argv", ["ecl2csv", "gruptree", "-v", REEK, "-o", str(tmpcsvfile)]) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_main_subparsers(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir.join(".TMP-compdat.csv") sys.argv = ["ecl2csv", "compdat", DATAFILE, "-o", str(tmpcsvfile)] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert "ZONE" in disk_df assert not disk_df.empty
def test_main_subparser(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir / "sum.csv" sys.argv = ["ecl2csv", "summary", DATAFILE, "-o", str(tmpcsvfile)] ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty assert "FOPT" in disk_df
def test_main(tmp_path, mocker): """Test command line interface""" tmpcsvfile = tmp_path / "trans.csv" mocker.patch("sys.argv", ["ecl2csv", "trans", "-v", EIGHTCELLS, "-o", str(tmpcsvfile)]) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_main_subparsers(): """Test command line interface""" tmpcsvfile = ".TMP-satfunc.csv" sys.argv = ["ecl2csv", "satfunc", DATAFILE, "-o", tmpcsvfile] ecl2csv.main() assert os.path.exists(tmpcsvfile) disk_df = pd.read_csv(tmpcsvfile) assert not disk_df.empty os.remove(tmpcsvfile)
def test_main_debugmode(tmpdir, mocker): """Test debug mode""" tmpdir.chdir() mocker.patch( "sys.argv", ["ecl2csv", "rft", "--debug", DATAFILE, "-o", "indebugmode.csv"]) ecl2csv.main() # Extra files emitted in debug mode: assert not pd.read_csv("con.csv").empty assert Path("seg.csv").exists() # too simple example data, no segments. assert Path("icd.csv").exists() # too simple example data, no ICD
def test_main(tmpdir, mocker): """Test command line interface""" tmpcsvfile = tmpdir.join("nnc.csv") mocker.patch("sys.argv", ["ecl2csv", "nnc", "-v", DATAFILE, "-o", str(tmpcsvfile)]) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty assert "I1" in disk_df assert "TRAN" in disk_df
def test_cmdline(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir.join(".TMP-fipreports.csv") sys.argv = [ "ecl2csv", "fipreports", "-v", DATAFILE, "--output", str(tmpcsvfile) ] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(tmpcsvfile) assert "FIPNAME" in disk_df assert "STOIIP_OIL" in disk_df assert not disk_df.empty
def test_cmdline(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir / "TMP-fipreports.csv" sys.argv = [ "ecl2csv", "fipreports", "-v", DATAFILE, "--output", str(tmpcsvfile) ] ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(tmpcsvfile) assert "FIPNAME" in disk_df assert "STOIIP_OIL" in disk_df assert not disk_df.empty
def test_ecl2csv_summary(tmp_path, mocker): """Test that the command line utility ecl2csv is installed and works with summary data""" tmpcsvfile = tmp_path / "sum.csv" mocker.patch( "sys.argv", [ "ecl2csv", "summary", "-v", REEK, "-o", str(tmpcsvfile), "--start_date", "2002-01-02", "--end_date", "2003-01-02", ], ) ecl2csv.main() disk_df = pd.read_csv(tmpcsvfile) assert len(disk_df) == 97 # Includes timestamps assert str(disk_df["DATE"].values[0]) == "2002-01-02 00:00:00" assert str(disk_df["DATE"].values[-1]) == "2003-01-02 00:00:00" tmpcsvfile = tmp_path / "sum.csv" mocker.patch( "sys.argv", [ "ecl2csv", "summary", REEK, "-o", str(tmpcsvfile), "--time_index", "daily", "--start_date", "2002-01-02", "--end_date", "2003-01-02", ], ) ecl2csv.main() disk_df = pd.read_csv(tmpcsvfile) assert len(disk_df) == 366 # Pandas' csv export writes datetime64 as pure date # when there are no clock-times involved: assert str(disk_df["DATE"].values[0]) == "2002-01-02" assert str(disk_df["DATE"].values[-1]) == "2003-01-02"
def test_rsvd_via_file(tmpdir): """Test that we can reparse RSVD with unknown TABDIMS from a file using the command line utility""" tmpdir.chdir() deckstr = """ RSVD 10 100 30 400 / 50 100 60 1000 /""" rsvd_df = equil.df(deckstr) with open("rsvd.inc", "w") as filehandle: filehandle.write(deckstr) sys.argv = ["ecl2csv", "equil", "-v", "rsvd.inc", "-o", "rsvd.csv"] ecl2csv.main() rsvd_df_fromcsv = pd.read_csv("rsvd.csv") pd.testing.assert_frame_equal(rsvd_df, rsvd_df_fromcsv)
def test_paramsupport_explicitfile(tmp_path, mocker): """Test explicit naming of parameters file from command line. This is a little bit tricky because the parameter file is assumed to be relative to the DATA file, not to working directory unless it is absolute.""" tmpcsvfile = tmp_path / "smrywithrandomparams.txt" randomparamfile = tmp_path / "fooparams.txt" randomparamfile.write_text("FOO barrbarr\nCOM 1234", encoding="ascii") mocker.patch( "sys.argv", [ "ecl2csv", "summary", "--verbose", EIGHTCELLS, "-o", str(tmpcsvfile), "--paramfile", str(randomparamfile), # Absolute filepath ], ) ecl2csv.main() assert pd.read_csv(tmpcsvfile)["FOO"].unique() == ["barrbarr"] assert pd.read_csv(tmpcsvfile)["COM"].unique() == [1234] # If we now change to tmp_path and give a relative filename to the parameter file,' # it will not be found: os.chdir(tmp_path) mocker.patch( "sys.argv", [ "ecl2csv", "summary", "--verbose", EIGHTCELLS, "-o", "smry_noparams.csv", "--paramfile", Path(randomparamfile).name, # A relative filepath ], ) ecl2csv.main() assert "FOO" not in pd.read_csv("smry_noparams.csv")
def test_main_arrow(tmp_path, mocker): """Check that we can export grid in arrow format""" mocker.patch( "sys.argv", ["ecl2csv", "grid", "--arrow", EIGHTCELLS, "-o", str(tmp_path / "grid.arrow")], ) ecl2csv.main() # Obtain the CSV version for comparison: mocker.patch( "sys.argv", ["ecl2csv", "grid", EIGHTCELLS, "-o", str(tmp_path / "grid.csv")] ) ecl2csv.main() # Read from disk and verify similarity disk_frame_arrow = pyarrow.feather.read_table(tmp_path / "grid.arrow").to_pandas() disk_frame_csv = pd.read_csv(tmp_path / "grid.csv") pd.testing.assert_frame_equal(disk_frame_arrow, disk_frame_csv, check_dtype=False)
def test_main(tmpdir): """Test command line interface""" tmpcsvfile = str(tmpdir.join("pvt.csv")) sys.argv = ["ecl2csv", "pvt", "-v", DATAFILE, "-o", tmpcsvfile] ecl2csv.main() assert os.path.exists(tmpcsvfile) disk_df = pd.read_csv(tmpcsvfile) assert "PVTNUM" in disk_df assert "KEYWORD" in disk_df assert not disk_df.empty # Write back to include file: incfile = str(tmpdir.join("pvt.inc")) sys.argv = ["csv2ecl", "pvt", "-v", str(tmpcsvfile), "-o", incfile] csv2ecl.main() # Reparse the include file on disk back to dataframe # and check dataframe equality assert os.path.exists(incfile) disk_inc_df = pvt.df(open(incfile).read()) pd.testing.assert_frame_equal(disk_df, disk_inc_df)
def test_main(tmp_path, mocker): """Test command line interface""" tmpcsvfile = tmp_path / "eclgrid.csv" mocker.patch( "sys.argv", [ "ecl2csv", "grid", EIGHTCELLS, "-o", str(tmpcsvfile), "--rstdates", "first", "--vectors", "PORO", ], ) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty Path(tmpcsvfile).unlink() # Do again with also restarts and multiple vectors: mocker.patch( "sys.argv", [ "ecl2csv", "grid", "--verbose", EIGHTCELLS, "-o", str(tmpcsvfile), "--rstdates", "2000-01-01", "--vectors", "PORO", "PERMX", ], ) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty assert "PORO" in disk_df assert "PERMX" in disk_df Path(tmpcsvfile).unlink() # Test with constants dropping mocker.patch( "sys.argv", ["ecl2csv", "grid", REEK, "-o", str(tmpcsvfile), "--dropconstants"] ) ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) # That PVTNUM is constant is a particular feature # of the test dataset. assert "PVTNUM" not in disk_df assert not disk_df.empty
def test_main_subparsers(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir.join(".TMP-rft.csv") sys.argv = ["ecl2csv", "rft", DATAFILE, "-o", str(tmpcsvfile)] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty tmpcsvfile = tmpdir.join(".TMP-rft2.csv") # Test with RFT file as argument: sys.argv = [ "ecl2cvsv", "rft", DATAFILE.replace(".DATA", ".RFT"), "-o", str(tmpcsvfile), ] ecl2csv.main() assert os.path.exists(str(tmpcsvfile)) disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty
def test_sgof_satnuminferrer(tmpdir): """Test inferring of SATNUMS in SGOF strings""" sgofstr = """ SGOF 0 0 1 1 1 1 0 0 / 0 0 1 1 0.5 0.5 0.5 0.5 1 1 0 0 / 0 0 1 0 0.1 0.1 0.1 0.1 1 1 0 0 / """ tmpdir.chdir() assert inferdims.guess_dim(sgofstr, "TABDIMS", 0) == 3 sgofdf = satfunc.deck2df(sgofstr) assert "SATNUM" in sgofdf assert len(sgofdf["SATNUM"].unique()) == 3 assert len(sgofdf) == 8 # This illustrates how we cannot do it, CRITICAL # logging errors will be displayed: sgofdf = satfunc.deck2df(EclFiles.str2deck(sgofstr)) assert len(sgofdf["SATNUM"].unique()) == 1 # Write to file and try to parse it with command line: sgoffile = "__sgof_tmp.txt" with open(sgoffile, "w") as sgof_f: sgof_f.write(sgofstr) sys.argv = ["ecl2csv", "satfunc", sgoffile, "-o", sgoffile + ".csv"] ecl2csv.main() parsed_sgof = pd.read_csv(sgoffile + ".csv") assert len(parsed_sgof["SATNUM"].unique()) == 3
def test_paramsupport(tmpdir): """Test that we can merge in parameters.txt""" tmpcsvfile = tmpdir.join(".TMP-sum.csv") eclfiles = EclFiles(DATAFILE) parameterstxt = os.path.join(eclfiles.get_path(), "parameters.txt") if os.path.exists(parameterstxt): os.remove(parameterstxt) with open(parameterstxt, "w") as pfile: pfile.write("FOO 1\nBAR 3") sys.argv = ["ecl2csv", "summary", DATAFILE, "-o", str(tmpcsvfile), "-p"] ecl2csv.main() disk_df = pd.read_csv(tmpcsvfile) assert "FOPT" in disk_df assert "FOO" in disk_df assert "BAR" in disk_df assert disk_df["BAR"].unique()[0] == 3 os.remove(parameterstxt) os.remove(str(tmpcsvfile)) parametersyml = os.path.join(eclfiles.get_path(), "parameters.yml") if os.path.exists(parametersyml): os.remove(parametersyml) with open(parametersyml, "w") as pfile: pfile.write(yaml.dump({"FOO": 1, "BAR": 3})) sys.argv = ["ecl2csv", "summary", DATAFILE, "-o", str(tmpcsvfile), "-p"] ecl2csv.main() disk_df = pd.read_csv(str(tmpcsvfile)) assert "FOPT" in disk_df assert "FOO" in disk_df assert len(disk_df["FOO"].unique()) == 1 assert disk_df["FOO"].unique()[0] == 1 assert "BAR" in disk_df assert len(disk_df["BAR"].unique()) == 1 assert disk_df["BAR"].unique()[0] == 3 os.remove(parametersyml)
def test_main(tmpdir): """Test command line interface""" tmpcsvfile = tmpdir / "eclgrid.csv" sys.argv = [ "ecl2csv", "grid", DATAFILE, "-o", str(tmpcsvfile), "--rstdates", "first", "--vectors", "PORO", ] ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty Path(tmpcsvfile).unlink() # Do again with also restarts and multiple vectors: sys.argv = [ "ecl2csv", "grid", "--verbose", DATAFILE, "-o", str(tmpcsvfile), "--rstdates", "2001-02-01", "--vectors", "PORO", "PERMX", ] ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) assert not disk_df.empty assert "PORO" in disk_df assert "PERMX" in disk_df Path(tmpcsvfile).unlink() # Test with constants dropping sys.argv = [ "ecl2csv", "grid", DATAFILE, "-o", str(tmpcsvfile), "--dropconstants" ] ecl2csv.main() assert Path(tmpcsvfile).is_file() disk_df = pd.read_csv(str(tmpcsvfile)) # That PVTNUM is constant is a particular feature # of the test dataset. assert "PVTNUM" not in disk_df assert not disk_df.empty