Skip to content

Commit

Permalink
FIX: os independent tmpdir and paths
Browse files Browse the repository at this point in the history
Change from hardcoded "/tmp" to tempfile functions
and also make paths based on pathlib functions
  • Loading branch information
jcrivenaes committed Jun 17, 2021
1 parent b12e6e6 commit c56eced
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 40 deletions.
13 changes: 8 additions & 5 deletions examples/grid3d_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
Crop a 3D grid.
"""

from os.path import join as ojn
import tempfile
import pathlib

import xtgeo

EXPATH1 = "../../xtgeo-testdata/3dgrids/reek"
EXPATH1 = pathlib.Path("../../xtgeo-testdata/3dgrids/reek")

GRIDFILEROOT = ojn(EXPATH1, "REEK")
GRIDFILEROOT = EXPATH1 / "REEK"

INITPROPS = ["PORO", "PERMX"]

TMPDIR = pathlib.Path(tempfile.gettempdir())


def cropper():
"""Do a cropping of a 3D grid"""
Expand Down Expand Up @@ -48,12 +51,12 @@ def cropper():
tmpgrd = grd.copy()
tmpgrd.crop(ncr, nrr, (1, nlay), props="all")
# save to disk as ROFF files
tmpgrd.to_file("/tmp/grid" + fname + ".roff")
tmpgrd.to_file(TMPDIR / ("grid" + fname + ".roff"))
for prop in tmpgrd.props:
print("{} for {} .. {}".format(prop.name, ncr, nrr))
fname2 = prop.name + fname + ".roff"
fname2 = fname2.lower()
prop.to_file(ojn("/tmp/", fname2))
prop.to_file(TMPDIR / fname2)


if __name__ == "__main__":
Expand Down
12 changes: 7 additions & 5 deletions examples/grid3d_get_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
"""

from os.path import join as ojn
import pathlib
import tempfile
import xtgeo

EXPATH = "../../xtgeo-testdata/3dgrids/reek"
EXPATH = pathlib.Path("../../xtgeo-testdata/3dgrids/reek")

GRIDFILEROOT = ojn(EXPATH, "REEK")
GRIDFILEROOT = EXPATH / "REEK"
TMPDIR = pathlib.Path(tempfile.gettempdir())

INITS = ["PORO", "PERMX"]
RESTARTS = ["PRESSURE", "SWAT", "SOIL"]
Expand All @@ -31,7 +33,7 @@ def extractdf():
"""Extract dataframe from Eclipse case"""

# gete dataframe from the grid only
grd = xtgeo.grid_from_file(GRIDFILEROOT + ".EGRID")
grd = xtgeo.grid_from_file(GRIDFILEROOT.with_suffix(".EGRID"))
dataframe = grd.dataframe() # will not have any grid props
print(dataframe)

Expand Down Expand Up @@ -60,7 +62,7 @@ def extractdf():

print(dataframe)

dataframe.to_csv(ojn("/tmp", "reek_sim.csv"))
dataframe.to_csv(TMPDIR / "reek_sim.csv")


if __name__ == "__main__":
Expand Down
13 changes: 6 additions & 7 deletions examples/grid3d_print_init_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
Print a CSV from all INIT vectors
"""

from os.path import join as ojn
import pathlib
import tempfile
import numpy as np
import xtgeo

EXPATH1 = "../../xtgeo-testdata/3dgrids/reek"
GRIDFILEROOT = ojn(EXPATH1, "REEK")
EXPATH = pathlib.Path("../../xtgeo-testdata/3dgrids/reek")

# EXPATH1 = '/scratch/troll_fmu/rnyb/10_troll_r003/realization-0/iter' +\
# '-0/eclipse/model'
# GRIDFILEROOT = ojn(EXPATH1, 'ECLIPSE')
GRIDFILEROOT = EXPATH / "REEK"
TMPDIR = pathlib.Path(tempfile.gettempdir())

INITPROPS = "all" # will look for all vectors that looks "gridvalid"

Expand All @@ -29,7 +28,7 @@ def all_init_as_csv():
dfr = dfr.iloc[:, ~np.isclose(0, dfr.var())]
print(dfr.head())
print("Write to file...")
dfr.to_csv(ojn("/tmp", "mycsvdump.csv"), index=False)
dfr.to_csv(TMPDIR / "mycsvdump.csv", index=False)


if __name__ == "__main__":
Expand Down
33 changes: 18 additions & 15 deletions examples/surface_slice_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
JRIV
"""

from os.path import join, basename
import pathlib
import tempfile

import xtgeo

DEBUG = False

EXPATH1 = "../../xtgeo-testdata/cubes/etc/"
EXPATH2 = "../../xtgeo-testdata/surfaces/etc"
EXPATH1 = pathlib.Path("../../xtgeo-testdata/cubes/etc/")
EXPATH2 = pathlib.Path("../../xtgeo-testdata/surfaces/etc")

TMPDIR = pathlib.Path(tempfile.gettempdir())


def slice_a_cube_with_surface():
"""Slice a seismic cube with a surface on OW dat/map format"""

cubefile = join(EXPATH1, "ib_test_cube2.segy")
surfacefile = join(EXPATH2, "h1.dat")
cubefile = EXPATH1 / "ib_test_cube2.segy"
surfacefile = EXPATH2 / "h1.dat"

mycube = xtgeo.cube_from_file(cubefile)

Expand All @@ -34,14 +37,14 @@ def slice_a_cube_with_surface():
mysurf.slice_cube(mycube, sampling="trilinear")

# export result
mysurf.to_file(join("/tmp", "slice.dat"), fformat="ijxyz")
mysurf.to_file(TMPDIR / "slice.dat", fformat="ijxyz")


def attribute_around_surface_symmetric():
"""Get atttribute around a surface (symmetric)"""

cubefile = join(EXPATH1, "ib_test_cube2.segy")
surfacefile = join(EXPATH2, "h1.dat")
cubefile = EXPATH1 / "ib_test_cube2.segy"
surfacefile = EXPATH2 / "h1.dat"

mycube = xtgeo.cube_from_file(cubefile)

Expand All @@ -54,15 +57,15 @@ def attribute_around_surface_symmetric():
)
for attr in myattrs.keys():
myattrs[attr].to_file(
join("/tmp", "myfile_symmetric_" + attr + ".dat"), fformat="ijxyz"
TMPDIR / ("myfile_symmetric_" + attr + ".dat"), fformat="ijxyz"
)


def attribute_around_surface_asymmetric():
"""Get attribute around a surface (asymmetric)"""

cubefile = join(EXPATH1, "ib_test_cube2.segy")
surfacefile = join(EXPATH2, "h1.dat")
cubefile = EXPATH1 / "ib_test_cube2.segy"
surfacefile = EXPATH2 / "h1.dat"

above = 10
below = 20
Expand Down Expand Up @@ -92,14 +95,14 @@ def attribute_around_surface_asymmetric():
myattrs[attr].describe()

myattrs[attr].to_file(
join("/tmp", "myfile_asymmetric_" + attr + ".dat"), fformat="ijxyz"
TMPDIR / ("myfile_asymmetric_" + attr + ".dat"), fformat="ijxyz"
)


def attribute_around_constant_cube_slices():
"""Get attribute around a constant cube slices"""

cubefile = join(EXPATH1, "ib_test_cube2.segy")
cubefile = EXPATH1 / "ib_test_cube2.segy"

level1 = 1010
level2 = 1100
Expand All @@ -125,7 +128,7 @@ def attribute_around_constant_cube_slices():
myattrs[attr].describe()

myattrs[attr].to_file(
join("/tmp", "myfile_constlevels_" + attr + ".dat"), fformat="ijxyz"
TMPDIR / ("myfile_constlevels_" + attr + ".dat"), fformat="ijxyz"
)


Expand All @@ -136,4 +139,4 @@ def attribute_around_constant_cube_slices():
attribute_around_surface_asymmetric()
attribute_around_constant_cube_slices()

print("Running example OK: {}".format(basename(__file__)))
print(f"Running example OK: {pathlib.Path(__file__).name}")
19 changes: 11 additions & 8 deletions examples/surface_slice_grid3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@
JRIV
"""
import os
from os.path import join, basename
import pathlib
import tempfile

import xtgeo

TMPDIR = pathlib.Path(tempfile.gettempdir())


def slice_a_grid():
"""Slice a 3D grid property with maps (looping)"""

expath1 = "../../xtgeo-testdata/3dgrids/reek"
expath2 = "../../xtgeo-testdata/surfaces/reek/1"
expath1 = pathlib.Path("../../xtgeo-testdata/3dgrids/reek")
expath2 = pathlib.Path("../../xtgeo-testdata/surfaces/reek/1")

gridfileroot = join(expath1, "REEK")
surfacefile = join(expath2, "midreek_rota.gri")
gridfileroot = expath1 / "REEK"
surfacefile = expath2 / "midreek_rota.gri"

initprops = ["PORO", "PERMX"]

Expand All @@ -44,9 +47,9 @@ def slice_a_grid():
sconst.slice_grid3d(grd, prp)

fname = "{}_{}.gri".format(prp.name, myslice)
sconst.to_file(join("/tmp", fname))
sconst.to_file(TMPDIR / fname)

fname = "{}_{}.png".format(prp.name, myslice)
fname = TMPDIR / ("{}_{}.png".format(prp.name, myslice))

if "SKIP_PLOT" not in os.environ:
sconst.quickplot(filename=fname)
Expand All @@ -55,4 +58,4 @@ def slice_a_grid():
if __name__ == "__main__":
slice_a_grid()

print("Running example OK: {}".format(basename(__file__)))
print(f"Running example OK: {pathlib.Path(__file__).name}")

0 comments on commit c56eced

Please sign in to comment.