Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion autotest/t007_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def export_netcdf(m):
fnc.write()
except Exception as e:
raise Exception(
'ncdf export fail for namfile {0}:\n{1} '.format(namfile, str(e)))
'ncdf export fail for namfile {0}:\n{1} '.format(m.name, str(e)))
try:
nc = netCDF4.Dataset(fnc_name, 'r')
except Exception as e:
Expand Down Expand Up @@ -118,6 +118,44 @@ def export_shapefile(namfile):
fnc_name, s.numRecords)
return


def export_shapefile_modelgrid_override(namfile):
try:
import shapefile as shp
except:
return

from flopy.discretization import StructuredGrid

print(namfile)
m = flopy.modflow.Modflow.load(namfile, model_ws=pth, verbose=False)
mg0 = m.modelgrid
modelgrid = StructuredGrid(mg0.delc * 0.3048, mg0.delr * 0.3048,
mg0.top, mg0.botm, mg0.idomain, mg0.lenuni,
mg0.epsg, mg0.proj4, xoff=mg0.xoffset,
yoff=mg0.yoffset, angrot=mg0.angrot)

assert m, 'Could not load namefile {}'.format(namfile)
assert isinstance(m, flopy.modflow.Modflow)
fnc_name = os.path.join(spth, m.name + '.shp')

try:
fnc = m.export(fnc_name, modelgrid=modelgrid)
#fnc2 = m.export(fnc_name, package_names=None)
#fnc3 = m.export(fnc_name, package_names=['DIS'])


except Exception as e:
raise Exception(
'shapefile export fail for namfile {0}:\n{1} '.format(namfile,
str(e)))
try:
s = shp.Reader(fnc_name)
except Exception as e:
raise Exception(
' shapefile import fail for {0}:{1}'.format(fnc_name, str(e)))


def test_freyberg_export():
from flopy.discretization import StructuredGrid
namfile = 'freyberg.nam'
Expand Down Expand Up @@ -1133,6 +1171,12 @@ def test_shapefile():
return


def test_shapefile_export_modelgrid_override():
for namfile in namfiles[0:2]:
yield export_shapefile_modelgrid_override, namfile
return


def test_netcdf():
for namfile in namfiles:
yield export_mf2005_netcdf, namfile
Expand Down
6 changes: 3 additions & 3 deletions autotest/t064_test_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_init_time(self):
"""test model and package init time(s)."""
mfp = TestModflowPerformance()
target = 0.3 # seconds
assert mfp.init_time < target, "model init took {:.2fs}, should take {:.1f}s".format(mfp.init_time, target)
assert mfp.init_time < target, "model init took {:.2f}s, should take {:.1f}s".format(mfp.init_time, target)
print('setting up model took {:.2f}s'.format(mfp.init_time))

def test_0_write_time(self):
Expand All @@ -73,7 +73,7 @@ def test_0_write_time(self):
t0 = time.time()
mfp.m.write_input()
t1 = time.time() - t0
assert t1 < target, "model write took {:.2fs}, should take {:.1f}s".format(t1, target)
assert t1 < target, "model write took {:.2f}s, should take {:.1f}s".format(t1, target)
print('writing input took {:.2f}s'.format(t1))

def test_9_load_time(self):
Expand All @@ -85,7 +85,7 @@ def test_9_load_time(self):
m = fm.Modflow.load('{}.nam'.format(mfp.modelname),
model_ws=mfp.model_ws, check=False)
t1 = time.time() - t0
assert t1 < target, "model load took {:.2fs}, should take {:.1f}s".format(t1, target)
assert t1 < target, "model load took {:.2f}s, should take {:.1f}s".format(t1, target)
print('loading the model took {:.2f}s'.format(t1))

@classmethod
Expand Down
329 changes: 209 additions & 120 deletions examples/Notebooks/flopy3_shapefile_export.ipynb

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions flopy/export/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ class NetCdf(object):
(default 'down')
verbose : if True, stdout is verbose. If str, then a log file
is written to the verbose file
forgive: what to do if a duplicate variable name is being created. If
forgive : what to do if a duplicate variable name is being created. If
True, then the newly requested var is skipped. If False, then
an exception is raised.
**kwargs : keyword arguments
modelgrid : flopy.discretization.Grid instance
user supplied model grid which will be used in lieu of the model
object modelgrid for netcdf production

Notes
-----
Expand All @@ -136,7 +140,7 @@ class NetCdf(object):

def __init__(self, output_filename, model, time_values=None,
z_positive='up', verbose=None, prj=None, logger=None,
forgive=False):
forgive=False, **kwargs):

assert output_filename.lower().endswith(".nc")
if verbose is None:
Expand All @@ -156,6 +160,8 @@ def __init__(self, output_filename, model, time_values=None,

self.model = model
self.model_grid = model.modelgrid
if "modelgrid" in kwargs:
self.model_grid = kwargs.pop("modelgrid")
self.model_time = model.modeltime
if prj is not None:
self.model_grid.proj4 = prj
Expand Down
18 changes: 15 additions & 3 deletions flopy/export/shapefile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ def model_attributes_to_shapefile(filename, ml, package_names=None,
Additional 2D arrays to add as attributes to the shapefile.
(default is None)

**kwargs : keyword arguments
modelgrid : fp.modflow.Grid object
if modelgrid is supplied, user supplied modelgrid is used in lieu
of the modelgrid attached to the modflow model object
epsg : int
epsg projection information
prj : str
user supplied prj file

Returns
-------
Expand All @@ -297,7 +305,11 @@ def model_attributes_to_shapefile(filename, ml, package_names=None,
else:
package_names = [pak.name[0] for pak in ml.packagelist]

grid = ml.modelgrid
if "modelgrid" in kwargs:
grid = kwargs.pop("modelgrid")
else:
grid = ml.modelgrid

if grid.grid_type == 'USG-Unstructured':
raise Exception('Flopy does not support exporting to shapefile from '
'and MODFLOW-USG unstructured grid.')
Expand Down Expand Up @@ -393,10 +405,10 @@ def model_attributes_to_shapefile(filename, ml, package_names=None,
array_dict[name] = arr

# write data arrays to a shapefile
write_grid_shapefile2(filename, ml.modelgrid, array_dict)
write_grid_shapefile2(filename, grid, array_dict)
epsg = kwargs.get('epsg', None)
prj = kwargs.get('prj', None)
write_prj(filename, ml.modelgrid, epsg, prj)
write_prj(filename, grid, epsg, prj)


def shape_attr_name(name, length=6, keep_layer=False):
Expand Down
Loading