我知道从现有包中转移注释的唯一方法是以读取模式打开文件,使用列数据创建一个 pandas DataFrame,然后从中构建一个新包。这是一个例子:
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)