Skip to content

Commit

Permalink
fix(sfr): remove sensitivity to SFR reach_data (ds2) rec array order. (
Browse files Browse the repository at this point in the history
…#637)

* making sfr reachdata (ds2) write insensitive to data field order

currently uses dtype.descr which can fail is struct array fields change
order.

* happend upon etvr not evtr in evt package

* Adding test that exposes issue with passing disordered reach_data arrays

* tidy-up
  • Loading branch information
briochh authored and langevin-usgs committed Aug 28, 2019
1 parent 842c89b commit 41dd80d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
29 changes: 26 additions & 3 deletions autotest/t009_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ def test_ds_6d_6e_disordered():
sfr = m.get_package("SFR")
sfr2 = m2.get_package("SFR")


if len(sfr.graph) != len(sfr2.graph):
raise AssertionError

Expand All @@ -416,6 +415,28 @@ def test_ds_6d_6e_disordered():
raise AssertionError


def test_disordered_reachdata_fields():
path = os.path.join("..", "examples", "data", "hydmod_test")
wpath = os.path.join(".", "temp", "t009_disorderfields")
m = flopy.modflow.Modflow.load("test1tr2.nam",
model_ws=path)
sfr = m.get_package("SFR")
orig_reach_data = sfr.reach_data
# build shuffled rec array
shuffled_fields = list(set(orig_reach_data.dtype.names))
data = []
names = []
formats = []
for field in shuffled_fields:
data.append(orig_reach_data[field])
names.append(field)
formats.append(orig_reach_data.dtype[field].str)
reach_data = np.rec.fromarrays(data, names=names, formats=formats)
m.sfr.reach_data = reach_data
m.change_model_ws(wpath)
m.write_input()


def test_transient_example():
path = os.path.join('temp', 't009')
gpth = os.path.join('..', 'examples', 'data', 'mf2005_test', 'testsfr2.*')
Expand Down Expand Up @@ -488,14 +509,16 @@ def test_sfr_plot():
#assert True
pass


if __name__ == '__main__':
# test_sfr()
# test_ds_6d_6e_disordered()
test_disordered_reachdata_fields()
# test_sfr_renumbering()
# test_example()
# test_export()
#test_transient_example()
#test_sfr_plot()
# test_transient_example()
# mtest_sfr_plot()
# test_assign_layers()
# test_SfrFile()
# test_const()
Expand Down
6 changes: 3 additions & 3 deletions flopy/modflow/mfsfr2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def _write_reach_data(self, f_sfr):
for idx in ['k', 'i', 'j', 'node']:
if (idx in columns):
d[idx] += 1
d = d[columns]
d = d[columns] # data columns sorted
formats = _fmt_string(d)[:-1] + '\n'
for rec in d:
f_sfr.write(formats.format(*rec))
Expand Down Expand Up @@ -2805,8 +2805,8 @@ def _get_item2_names(nstrm, reachinput, isfropt, structured=False):

def _fmt_string(array, float_format='{!s}'):
fmt_string = ''
for field in array.dtype.descr:
vtype = field[1][1].lower()
for field in array.dtype.names: # data already sorted
vtype = array.dtype[field].str[1].lower()
if vtype == 'v':
continue
if vtype == 'i':
Expand Down

0 comments on commit 41dd80d

Please sign in to comment.