Well ID and other comments in stress period data in flopy

374 Views Asked by At

I would like to assign a commented out Well ID number to my well file, (also do the same for ghb cells) but I cannot find anything on how to do so.

I wrote something to create my own ghb file but if I try to load it back into my flopy mf class, and later write it out my other packages with mf.write_input() the comments do not stay and it gets overwritten.

I know in mf.wrtie_input() I can specify what packages to write out, and if I take away the ghb file I made earlier (or well file) then the original file does not get written over which is good.

But I would like to know if there is a way to straight up add comments to the stress_period_data for each package so I can keep it all contained in the flopy class.

Thanks

2

There are 2 best solutions below

2
On

Like in this example, you can extend the default dtype to include extra attributes that the MfList instance will carry to the write:

well_dtype = [('k', '<i8'), ('i', '<i8'), ('j', '<i8'),('flux', '<f4'), ('wel_id', object)]
stress_period_data = np.zeros((3), dtype=well_dtype)
wel = flopy.modflow.ModflowWel(m, stress_period_data=stress_period_data, dtype=well_dtype)

I'm not sure of an easy way to load an existing wel package with extra attributes - just FYI

1
On

The only way I know to carry remarks over from an existing package is to open the file in read mode, create a pandas DataFrame with the column data, and build a new package out of it. Here is an example:

import os
import pandas as pd
import flopy.modflow as fpm
from collections import OrderedDict


pak_nam = 'drn'
mf_version = 'mfnwt'

# the model from which the DRN package will be copied
inmod = fpm.Modflow.load('10kTDS.nam',
                     model_ws=r'..\10kTDS',
                     version=mf_version, 
                     load_only=['drn'], 
                     check=False)

# the model where the new DRN package will be attached
mf = fpm.Modflow.load('ss2010.nam',
                      model_ws=os.path.join('..', 'ss2010'),
                      version=mf_version,
                      load_only=['dis', 'bas6'],
                      check=False)

# read the contents of the DRN package
with open(inmod.drn.fn_path, 'r') as f:
    lines = f.readlines()

# create pandas DataFrame
data = []
for line in lines[3:]:
    pieces = line.strip().split('#')
    t = pieces[0].strip().split()
    remark = pieces[-1]
    if t[0] == '-1':
        break
    else:
        data.append([int(t[0]),
                     int(t[1]),
                     int(t[2]),
                     float(t[3]),
                     float(t[4]),
                     '# ' + remark.strip()])
pak_df = pd.DataFrame(data,
                      columns=['k', 'i', 'j', 'alt_va', 'cond', 'remark'])
pak_df.loc[:, ['k', 'i', 'j']] -= 1

# specify data format
formats = OrderedDict([('k', '{:>10d}'.format),
                       ('i', '{:>10d}'.format),
                       ('j', '{:>10d}'.format),
                       ('alt_va', '{:>.2F}'.format),
                       ('cond', '{:>15.6E}'.format),
                       ('remark', '{>:50}'.format)])

# create new stress period data: for numpy record array use DataFrame.to_records()
pak_spd = {0: pak_df[list(formats.keys())].to_records(index=False)}

# attach DRN package to new model
pak = fpm.ModflowDrn(mf,
                     stress_period_data=pak_spd,
                     ipakcb=53,
                     options=['NOPRINT'],
                     filenames=os.path.join('..', 'ss2010', 'ss2010.{}'.format(pak_nam)),
                     dtype=pak_spd[0].dtype)

pak.write_file(check=False)