Skip to content

Commit

Permalink
fix(MODFLOW 6 SFR Package input): write length and time conversions i…
Browse files Browse the repository at this point in the history
…nstead of previous unit_conversion (deprecated in MODFLOW 6.4.2)
  • Loading branch information
aleaf committed Sep 24, 2024
1 parent b2233e6 commit 16ca960
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
16 changes: 10 additions & 6 deletions sfrmaker/mf5to6.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Mf6SFR:
'BUDGET FILEOUT model.sfr.cbc',
'STAGE FILEOUT model.sfr.stage.bin']
An appropriate unit_conversion is written by default.
Appropriate unit conversion options are written by default.
See MODFLOW-6 documentation for other options.
By default None.
Expand Down Expand Up @@ -64,7 +64,8 @@ class Mf6SFR:
cols = ['rno', 'cellid', 'k', 'i', 'j', 'rlen', 'rwid', 'rgrd',
'rtp', 'rbth', 'rhk', 'man', 'ncon', 'ustrf', 'ndv', 'idomain', 'line_id']
def __init__(self, ModflowSfr2=None, SFRData=None,
period_data=None, idomain=None,
period_data=None, idomain=None,
length_conversion=1.0, time_conversion=86400,
options=None, auxiliary_line_numbers=True):

# instantiate with SFRData instance instead of ModflowSfr2 instance
Expand Down Expand Up @@ -94,7 +95,8 @@ def __init__(self, ModflowSfr2=None, SFRData=None,
self.ModflowSfr2.reach_data.sort(order=['iseg', 'ireach'])

self.structured = self.ModflowSfr2.parent.structured
self.unit_conversion = ModflowSfr2.const
self.length_conversion = length_conversion
self.time_conversion = time_conversion
self.nreaches = len(ModflowSfr2.reach_data)
self.nper = ModflowSfr2.nper

Expand Down Expand Up @@ -151,8 +153,10 @@ def options_block(self, options):
if options is not None:
for opt in options:
options_block += ' {}\n'.format(opt)
if 'unit_conversion' not in options_block:
options_block += ' unit_conversion {}\n'.format(self.unit_conversion)
if 'length_conversion' not in options_block:
options_block += ' length_conversion {}\n'.format(self.length_conversion)
if 'time_conversion' not in options_block:
options_block += ' time_conversion {}\n'.format(self.time_conversion)
if 'auxiliary' not in options_block and self.auxiliary_line_numbers:
options_block += ' auxiliary line_id\n'
options_block += 'END Options\n'
Expand Down Expand Up @@ -278,7 +282,7 @@ def write_file(self, filename=None, outpath='', options=None,
'BUDGET FILEOUT model.sfr.cbc',
'STAGE FILEOUT model.sfr.stage.bin']
An appropriate unit_conversion is written by default.
Appropriate unit conversion options are written by default.
See MODFLOW-6 documentation for other options.
By default None.
external_files_path : str, optional
Expand Down
18 changes: 13 additions & 5 deletions sfrmaker/sfrdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def __init__(self, reach_data=None,
self._period_data = None
self._observations = None
self._observations_filename = None
self.length_conversion = self.len_const[self._lenuni]
self.time_conversion = self.time_const[self._itmuni]

# convert any modflow6 kwargs to modflow5
kwargs = {SFRData.mf5names[k] if k in SFRData.mf6names else k:
Expand Down Expand Up @@ -701,15 +703,18 @@ def create_modflow_sfr2(self, model=None, const=None,
# self._ModflowSfr2.parent = model
return self._ModflowSfr2

def create_mf6sfr(self, model=None, unit_conversion=None,
def create_mf6sfr(self, model=None,
length_conversion=None, time_conversion=None,
stage_filerecord=None,
budget_filerecord=None,
flopy_rno_input_is_zero_based=True,
**kwargs
):

if unit_conversion is None:
unit_conversion = self.const
if length_conversion is None:
length_conversion = self.length_conversion
if time_conversion is None:
time_conversion = self.time_conversion
if stage_filerecord is None:
stage_filerecord = '{}.sfr.stage.bin'.format(self.package_name)
if budget_filerecord is None:
Expand Down Expand Up @@ -741,7 +746,8 @@ def create_mf6sfr(self, model=None, unit_conversion=None,

# update the ModflowSfr2 instance in case the SFR dataset has changed
self.create_modflow_sfr2()
sfr6 = Mf6SFR(self.modflow_sfr2)
sfr6 = Mf6SFR(self.modflow_sfr2, length_conversion=length_conversion,
time_conversion=time_conversion)

# package data
# An error occurred when storing data "packagedata" in a recarray.
Expand Down Expand Up @@ -796,9 +802,11 @@ def create_mf6sfr(self, model=None, unit_conversion=None,
# "Use sfrdata.write_package(version='mf6') instead.")
pass

mf6sfr = mf6.ModflowGwfsfr(model=m, unit_conversion=unit_conversion,
mf6sfr = mf6.ModflowGwfsfr(model=m,
stage_filerecord=stage_filerecord,
budget_filerecord=budget_filerecord,
length_conversion=length_conversion,
time_conversion=time_conversion,
nreaches=len(self.reach_data),
packagedata=packagedata,
connectiondata=connectiondata,
Expand Down
9 changes: 6 additions & 3 deletions sfrmaker/test/test_mf5to6.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def mf6sfr_instance_SFRdata(shellmound_sfrdata):
'STAGE FILEOUT sr.stage.bin',
],
['auxiliary line_id',
'unit_conversion 1.0']
'length_conversion 1.0',
'time_conversion 86400']
),
)
def test_init(options, shellmound_ModflowSfr2):
Expand All @@ -40,8 +41,10 @@ def test_init(options, shellmound_ModflowSfr2):
if options is not None:
expected_keys = expected_keys[:1] + [' '.join(item.split()[:-1]) if len(item.split()) > 1 else item
for item in options]
if 'unit_conversion' not in expected_keys:
expected_keys.append('unit_conversion')
if 'length_conversion' not in expected_keys:
expected_keys.append('length_conversion')
if 'time_conversion' not in expected_keys:
expected_keys.append('time_conversion')
if 'auxiliary' not in expected_keys:
expected_keys.append('auxiliary')
expected_keys.append('END')
Expand Down
16 changes: 13 additions & 3 deletions sfrmaker/test/test_sfr_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ def test_ibound_representation_of_idomain(shellmound_sfrdata, shellmound_model):
def test_write_mf6_package(shellmound_sfrdata, mf6sfr, outdir):
sfr_package_file = os.path.join(outdir, 'test.package_file.sfr')
shellmound_sfrdata.write_package(filename=sfr_package_file, version='mf6')
with open(sfr_package_file) as src:
alltext = src.read()
assert 'unit_conversion' not in alltext, "deprecated in MODFLOW 6.4.2"
with open(sfr_package_file) as src:
for line in src:
if 'budget' in line.lower() or 'stage' in line.lower():
Expand All @@ -214,9 +217,16 @@ def test_write_mf6_package(shellmound_sfrdata, mf6sfr, outdir):
assert path == ''
assert fname.replace('.cbc', '').replace('.stage.bin', '') == \
os.path.split(sfr_package_file)[1]
elif 'unit_conversion' in line.lower():
_, conversion = line.strip().split()
assert np.allclose(float(conversion), shellmound_sfrdata.const)
elif 'length_conversion' in line.lower():

_, length_conversion = line.strip().split()
assert np.allclose(float(length_conversion),
shellmound_sfrdata.length_conversion)
elif 'time_conversion' in line.lower():

_, time_conversion = line.strip().split()
assert np.allclose(float(time_conversion),
shellmound_sfrdata.time_conversion)
if 'end options' in line.lower():
break

Expand Down

0 comments on commit 16ca960

Please sign in to comment.