Skip to content

Commit

Permalink
Merge pull request #87 from equinor/loggername
Browse files Browse the repository at this point in the history
Use named loggers from Pythons logging module
  • Loading branch information
berland authored Jan 14, 2020
2 parents 87b4355 + 8cfea66 commit 163e3e2
Show file tree
Hide file tree
Showing 30 changed files with 210 additions and 166 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ output-format=colorized
ignore=_version.py,__init__.py,setup.py,versioneer.py,jobs.py

[BASIC]
good-names=df
good-names=df,logger

[FORMAT]
max-line-length=88
Expand Down
12 changes: 8 additions & 4 deletions ecl2df/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import pandas as pd


logging.basicConfig()
logger = logging.getLogger(__name__)


def parse_ecl_month(eclmonth):
"""Translate Eclipse month strings to integer months"""
eclmonth2num = {
Expand Down Expand Up @@ -47,15 +51,15 @@ def merge_zones(df, zonedict, zoneheader="ZONE", kname="K1"):
assert isinstance(kname, str)
assert isinstance(df, pd.DataFrame)
if not zonedict:
logging.warning("Can't merge in empty zone information")
logger.warning("Can't merge in empty zone information")
return df
if zoneheader in df:
logging.error(
logger.error(
"Column name %s already exists, refusing to merge in any more", zoneheader
)
return df
if kname not in df:
logging.error("Can't merge on non-existing column %s", kname)
logger.error("Can't merge on non-existing column %s", kname)
return df
zone_df = pd.DataFrame.from_dict(zonedict, orient="index", columns=[zoneheader])
zone_df.index.name = "K"
Expand Down Expand Up @@ -104,7 +108,7 @@ def stack_on_colnames(dframe, sep="@", stackcolname="DATE", inplace=True):
dframe = pd.DataFrame(dframe)
tuplecolumns = list(map(lambda x: tuple(x.split(sep)), dframe.columns))
if max(map(len, tuplecolumns)) < 2:
logging.info("No columns to stack")
logger.info("No columns to stack")
return dframe
dframe.columns = pd.MultiIndex.from_tuples(
tuplecolumns, names=["dummy", stackcolname]
Expand Down
22 changes: 12 additions & 10 deletions ecl2df/compdat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from .common import parse_ecl_month, merge_zones
from .grid import merge_initvectors

logging.basicConfig()
logger = logging.getLogger(__name__)

# Sunbeam terms:
COMPDATKEYS = [
Expand Down Expand Up @@ -109,7 +111,7 @@ def sunbeam2rmsterm(reckey):

def deck2compdatsegsdfs(deck, start_date=None):
"""Deprecated function name"""
logging.warning("Deprecated method name: deck2compdatsegsdfs(), use deck2dfs()")
logger.warning("Deprecated method name: deck2compdatsegsdfs(), use deck2dfs()")
return deck2dfs(deck, start_date)


Expand Down Expand Up @@ -141,17 +143,17 @@ def deck2dfs(deck, start_date=None, unroll=True):
month = rec["MONTH"][0]
year = rec["YEAR"][0]
date = datetime.date(year=year, month=parse_ecl_month(month), day=day)
logging.info("Parsing at date %s", str(date))
logger.info("Parsing at date %s", str(date))
elif kword.name == "TSTEP":
if not date:
logging.critical("Can't use TSTEP when there is no start_date")
logger.critical("Can't use TSTEP when there is no start_date")
return {}
for rec in kword:
steplist = rec[0]
# Assuming not LAB units, then the unit is days.
days = sum(steplist)
date += datetime.timedelta(days=days)
logging.info(
logger.info(
"Advancing %s days to %s through TSTEP", str(days), str(date)
)
elif kword.name == "COMPDAT":
Expand Down Expand Up @@ -210,7 +212,7 @@ def deck2dfs(deck, start_date=None, unroll=True):
rec_data["SEGMENT_MD"] = rec_data["SEGMENT_LENGTH"]
welsegsrecords.append(rec_data)
elif kword.name == "TSTEP":
logging.warning("Possible premature stop at first TSTEP")
logger.warning("Possible premature stop at first TSTEP")
break

compdat_df = pd.DataFrame(compdatrecords)
Expand Down Expand Up @@ -303,7 +305,7 @@ def unrolldf(dframe, start_column="K1", end_column="K2"):
if dframe.empty:
return dframe
if start_column not in dframe and end_column not in dframe:
logging.warning(
logger.warning(
"Cannot unroll on non-existing columns %s and %s", start_column, end_column
)
return dframe
Expand Down Expand Up @@ -350,7 +352,7 @@ def fill_parser(parser):
def main():
"""Entry-point for module, for command line utility
"""
logging.warning("compdat2csv is deprecated, use 'ecl2csv compdat <args>' instead")
logger.warning("compdat2csv is deprecated, use 'ecl2csv compdat <args>' instead")
parser = argparse.ArgumentParser()
parser = fill_parser(parser)
args = parser.parse_args()
Expand All @@ -360,13 +362,13 @@ def main():
def compdat2df_main(args):
"""Entry-point for module, for command line utility"""
if args.verbose:
logging.basicConfig(level=logging.INFO)
logger.setLevel(logging.INFO)
eclfiles = EclFiles(args.DATAFILE)
if eclfiles:
deck = eclfiles.get_ecldeck()
compdat_df = df(eclfiles, initvectors=args.initvectors)
if compdat_df.empty:
logging.warning("Empty COMPDAT data being written to disk!")
logger.warning("Empty COMPDAT data being written to disk!")
compdat_df.to_csv(args.output, index=False)
print("Wrote to " + args.output)

Expand All @@ -390,7 +392,7 @@ def df(eclfiles, initvectors=None):

zonemap = eclfiles.get_zonemap()
if zonemap:
logging.info("Merging zonemap into compdat")
logger.info("Merging zonemap into compdat")
compdat_df = merge_zones(compdat_df, zonemap)

return compdat_df
8 changes: 5 additions & 3 deletions ecl2df/ecl2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ def get_parser():
"fipreports",
help=("Extract FIPxxxxx REPORT REGION data from Eclipse PRT output file."),
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description=("Extract FIPxxxxx REPORT REGION data from PRT file. "
"This parses currently in-place, outflows to wells and regions, and material "
"balance errors"),
description=(
"Extract FIPxxxxx REPORT REGION data from PRT file. "
"This parses currently in-place, outflows to wells and regions, and "
"material balance errors"
),
)
fipreports.fill_parser(fipreports_parser)
fipreports_parser.set_defaults(func=fipreports.fipreports_main)
Expand Down
19 changes: 11 additions & 8 deletions ecl2df/eclfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from ecl.grid import EclGrid
from ecl.summary import EclSum

logging.basicConfig()
logger = logging.getLogger(__name__)

# Default parse option to Sunbeam for a very permissive parsing
SUNBEAM_RECOVERY = [
("PARSE_UNKNOWN_KEYWORD", sunbeam.action.ignore),
Expand Down Expand Up @@ -83,7 +86,7 @@ def get_ecldeck(self):
deckfile = self._eclbase + ".DATA"
else:
deckfile = self._eclbase # Will be any filename
logging.info("Parsing deck file %s...", deckfile)
logger.info("Parsing deck file %s...", deckfile)
deck = sunbeam.deck.parse(deckfile, recovery=SUNBEAM_RECOVERY)
self._deck = deck
return self._deck
Expand Down Expand Up @@ -111,7 +114,7 @@ def get_egrid(self):
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), egridfilename
)
logging.info("Opening grid data from EGRID file: %s", egridfilename)
logger.info("Opening grid data from EGRID file: %s", egridfilename)
self._egrid = EclGrid(egridfilename)
return self._egrid

Expand All @@ -125,7 +128,7 @@ def get_egridfile(self):
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), egridfilename
)
logging.info("Opening data vectors from EGRID file: %s", egridfilename)
logger.info("Opening data vectors from EGRID file: %s", egridfilename)
self._egridfile = EclFile(egridfilename)
return self._egridfile

Expand All @@ -145,7 +148,7 @@ def get_eclsum(self, include_restart=True):
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), smryfilename
)
logging.info("Opening UNSMRY file: %s", smryfilename)
logger.info("Opening UNSMRY file: %s", smryfilename)
self._eclsum = EclSum(smryfilename, include_restart=include_restart)
return self._eclsum

Expand All @@ -157,7 +160,7 @@ def get_initfile(self):
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), initfilename
)
logging.info("Opening INIT file: %s", initfilename)
logger.info("Opening INIT file: %s", initfilename)
self._initfile = EclFile(initfilename)
return self._initfile

Expand All @@ -169,7 +172,7 @@ def get_rftfile(self):
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), rftfilename
)
logging.info("Opening RFT file: %s", rftfilename)
logger.info("Opening RFT file: %s", rftfilename)
self._rftfile = EclFile(rftfilename)
return self._rftfile

Expand All @@ -181,7 +184,7 @@ def get_rstfile(self):
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), rstfilename
)
logging.info("Opening RST file: %s", rstfilename)
logger.info("Opening RST file: %s", rstfilename)
self._rstfile = EclFile(rstfilename)
return self._rstfile

Expand Down Expand Up @@ -230,7 +233,7 @@ def get_zonemap(self, filename=None):
if filename_defaulted:
# No warnings when the default filename is not there.
return {}
logging.warning("Zonefile %s not found, ignoring", fullpath)
logger.warning("Zonefile %s not found, ignoring", fullpath)
return {}

zonelines = open(fullpath).readlines()
Expand Down
19 changes: 11 additions & 8 deletions ecl2df/equil.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
from ecl2df import inferdims
from .eclfiles import EclFiles

logging.basicConfig()
logger = logging.getLogger(__name__)


def deck2equildf(deck):
"""Deprecated function name"""
logging.warning("Deprecated function name, deck2equildf")
logger.warning("Deprecated function name, deck2equildf")
return deck2df(deck)


Expand Down Expand Up @@ -49,18 +52,18 @@ def deck2df(deck, ntequl=None):
"""
if "EQLDIMS" not in deck:
if not isinstance(deck, str):
logging.critical(
logger.critical(
"Will not be able to guess NTEQUL from a parsed deck without EQLDIMS."
)
logging.critical(
logger.critical(
(
"Only data for the first EQUIL will be returned. "
"Instead, supply string to deck2df()"
)
)
ntequl = 1
if not ntequl:
logging.warning("EQLDIMS+NTEQUL or ntequl not supplied. Will be guessed")
logger.warning("EQLDIMS+NTEQUL or ntequl not supplied. Will be guessed")
ntequl_estimate = inferdims.guess_dim(deck, "EQLDIMS", 0)
augmented_strdeck = inferdims.inject_dimcount(
deck, "EQLDIMS", 0, ntequl_estimate
Expand Down Expand Up @@ -138,7 +141,7 @@ def deck2df(deck, ntequl=None):
rowlist = [x[0] for x in rec]
if len(rowlist) > len(columnnames):
rowlist = rowlist[: len(columnnames)]
logging.warning(
logger.warning(
"Something wrong with columnnames " + "or EQUIL-data, data is chopped!"
)
records.append(rowlist)
Expand Down Expand Up @@ -170,7 +173,7 @@ def fill_parser(parser):
def main():
"""Entry-point for module, for command line utility
"""
logging.warning("equil2csv is deprecated, use 'ecl2csv equil <args>' instead")
logger.warning("equil2csv is deprecated, use 'ecl2csv equil <args>' instead")
parser = argparse.ArgumentParser()
parser = fill_parser(parser)
args = parser.parse_args()
Expand All @@ -180,13 +183,13 @@ def main():
def equil2df_main(args):
"""Read from disk and write CSV back to disk"""
if args.verbose:
logging.basicConfig(level=logging.INFO)
logger.setLevel(logging.INFO)
eclfiles = EclFiles(args.DATAFILE)
if eclfiles:
deck = eclfiles.get_ecldeck()
equil_df = deck2df(deck)
if equil_df.empty:
logging.warning("Empty EQUIL-data being written to disk!")
logger.warning("Empty EQUIL-data being written to disk!")
equil_df.to_csv(args.output, index=False)
print("Wrote to " + args.output)

Expand Down
12 changes: 8 additions & 4 deletions ecl2df/faults.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@

from .eclfiles import EclFiles

logging.basicConfig()
logger = logging.getLogger(__name__)


RECORD_COLUMNS = ["NAME", "IX1", "IX2", "IY1", "IY2", "IZ1", "IZ2", "FACE"]
COLUMNS = ["NAME", "I", "J", "K", "FACE"]
ALLOWED_FACES = ["X", "Y", "Z", "I", "J", "K", "X-", "Y-", "Z-", "I-", "J-", "K-"]


def deck2faultsdf(deck):
"""Deprecated function name"""
logging.warning("Deprecated function name deck2faultsdf")
logger.warning("Deprecated function name deck2faultsdf")
return deck2df(deck)


Expand Down Expand Up @@ -68,7 +72,7 @@ def fill_parser(parser):
def main():
"""Entry-point for module, for command line utility
"""
logging.warning("faults2csv is deprecated, use 'ecl2csv faults <args>' instead")
logger.warning("faults2csv is deprecated, use 'ecl2csv faults <args>' instead")
parser = argparse.ArgumentParser()
parser = fill_parser(parser)
args = parser.parse_args()
Expand All @@ -78,13 +82,13 @@ def main():
def faults2df_main(args):
"""Read from disk and write CSV back to disk"""
if args.verbose:
logging.basicConfig(level=logging.INFO)
logger.setLevel(logging.INFO)
eclfiles = EclFiles(args.DATAFILE)
if eclfiles:
deck = eclfiles.get_ecldeck()
faults_df = deck2df(deck)
if faults_df.empty:
logging.warning("Empty FAULT data being written to disk!")
logger.warning("Empty FAULT data being written to disk!")
faults_df.to_csv(args.output, index=False)
print("Wrote to " + args.output)

Expand Down
Loading

0 comments on commit 163e3e2

Please sign in to comment.