Skip to content

Commit

Permalink
updates for CSVFile and MF6Output (#2082)
Browse files Browse the repository at this point in the history
* fix(tri2vor): remove invalid geometries from voronoi nodes

* remove zero length, one vertex (point), and two vertex (line) geometries from list of nodes

* linting

* update(CsvFile, MF6Output)

* update CsvFile to increment duplicate headers
* added human readable __repr__ to MF6Output class

---------

Co-authored-by: jlarsen <[email protected]>
  • Loading branch information
jdlarsen-UA and jlarsen-usgs authored Feb 2, 2024
1 parent cff4f23 commit f9a314b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
43 changes: 43 additions & 0 deletions autotest/test_obs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
import pytest
import flopy
from modflow_devtools.markers import requires_exe

from flopy.modflow import (
Expand All @@ -19,6 +20,7 @@
from flopy.utils.observationfile import Mf6Obs



@requires_exe("mf2005")
def test_hob_simple(function_tmpdir):
"""
Expand Down Expand Up @@ -444,3 +446,44 @@ def test_multilayerhob_pr_multiline():
hob = ModflowHob.load(StringIO(problem_hob), ml)

assert len(hob.obs_data) == 2, "pr, mlay... load error"


def test_duplicate_observation_names(function_tmpdir):
sim_ws = function_tmpdir
sim = flopy.mf6.MFSimulation(sim_ws=sim_ws)

tdis = flopy.mf6.ModflowTdis(sim)
ims = flopy.mf6.ModflowIms(sim)

gwf = flopy.mf6.ModflowGwf(sim)

nlay = 2
nrow = 10
ncol = 10
top = 10
botm = [0, -10]

dis = flopy.mf6.ModflowGwfdis(gwf, nlay=nlay, nrow=nrow, ncol=ncol, top=top,
botm=botm)
ic = flopy.mf6.ModflowGwfic(gwf, strt=top)
npf = flopy.mf6.ModflowGwfnpf(gwf, k=1, k33=1)
sto = flopy.mf6.ModflowGwfsto(gwf)
obs = flopy.mf6.ModflowUtlobs(
gwf,
continuous={"repeat_obs.csv": [
("obsdup", "HEAD", (0, 4, 4)),
("obsdup", "HEAD", (1, 4, 4))
]
}
)

spd = {0: [((1, 4, 4), -50.)]}
wel = flopy.mf6.ModflowGwfwel(gwf, stress_period_data=spd)
sim.write_simulation()
sim.run_simulation()

gwf = sim.get_model()
obs = gwf.obs.output.obs()
data = obs.get_data()
if len(data.dtype.names) != 3:
raise AssertionError("CsvFile not incrementing duplicate headings")
20 changes: 20 additions & 0 deletions flopy/mf6/utils/output_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ def get_layerfile_data(self, f=data, text=rectype):
setattr(self.__class__, rectype, get_layerfile_data)
self._methods.append(f"{rectype}()")

def __repr__(self):
"""
String representation of the MF6Output object
Returns
-------
s : str
human readable class representation
"""
name = self._obj.name
if isinstance(name, list):
name = name[0]
l = [
f"MF6Output Class for {name}",
f"Available output methods include:",
]
l += [f".{m}" for m in self.methods()]
s = "\n".join(l)
return s

def methods(self):
"""
Method that returns a list of available method calls
Expand Down
20 changes: 20 additions & 0 deletions flopy/utils/observationfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,33 @@ def __init__(
# read header line
line = self.file.readline()
self._header = line.rstrip().split(delimiter)
self.__fix_duplicate_headings()
self.floattype = "f8"
self.dtype = _build_dtype(self._header, self.floattype)

self.data = self.read_csv(
self.file, self.dtype, delimiter, deletechars, replace_space
)

def __fix_duplicate_headings(self):
"""
Method to increment duplicate observation names if they exist
"""
new_header = []
while self._header:
colname = self._header.pop(0)
cnt = 1
if colname in new_header:
cnt = 1
basename = colname
while colname in new_header:
colname = f"{basename}_{cnt}"
cnt += 1
new_header.append(colname)

self._header = new_header

@property
def obsnames(self):
"""
Expand Down

0 comments on commit f9a314b

Please sign in to comment.