|
| 1 | +# --- |
| 2 | +# jupyter: |
| 3 | +# jupytext: |
| 4 | +# notebook_metadata_filter: all |
| 5 | +# text_representation: |
| 6 | +# extension: .py |
| 7 | +# format_name: light |
| 8 | +# format_version: '1.5' |
| 9 | +# jupytext_version: 1.14.5 |
| 10 | +# kernelspec: |
| 11 | +# display_name: Python 3 (ipykernel) |
| 12 | +# language: python |
| 13 | +# name: python3 |
| 14 | +# metadata: |
| 15 | +# section: flopy |
| 16 | +# authors: |
| 17 | +# - name: Jeremy White |
| 18 | +# --- |
| 19 | + |
| 20 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 21 | +# # Formatting ASCII output arrays |
| 22 | +# |
| 23 | +# ### Configuring numeric arrays written by FloPy |
| 24 | + |
| 25 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 26 | +# load and run the Freyberg model |
| 27 | + |
| 28 | +import os |
| 29 | + |
| 30 | +# + pycharm={"name": "#%%\n"} |
| 31 | +import sys |
| 32 | +from tempfile import TemporaryDirectory |
| 33 | + |
| 34 | +import matplotlib as mpl |
| 35 | +import matplotlib.pyplot as plt |
| 36 | +import numpy as np |
| 37 | + |
| 38 | +import flopy |
| 39 | + |
| 40 | +# Set name of MODFLOW exe |
| 41 | +# assumes executable is in users path statement |
| 42 | +version = "mf2005" |
| 43 | +exe_name = "mf2005" |
| 44 | +mfexe = exe_name |
| 45 | + |
| 46 | +# Set the paths |
| 47 | +loadpth = os.path.join("..", "..", "examples", "data", "freyberg") |
| 48 | +temp_dir = TemporaryDirectory() |
| 49 | +modelpth = temp_dir.name |
| 50 | + |
| 51 | +# make sure modelpth directory exists |
| 52 | +if not os.path.isdir(modelpth): |
| 53 | + os.makedirs(modelpth) |
| 54 | + |
| 55 | +print(sys.version) |
| 56 | +print("numpy version: {}".format(np.__version__)) |
| 57 | +print("matplotlib version: {}".format(mpl.__version__)) |
| 58 | +print("flopy version: {}".format(flopy.__version__)) |
| 59 | + |
| 60 | +# + |
| 61 | +ml = flopy.modflow.Modflow.load( |
| 62 | + "freyberg.nam", model_ws=loadpth, exe_name=exe_name, version=version |
| 63 | +) |
| 64 | +ml.model_ws = modelpth |
| 65 | +ml.write_input() |
| 66 | +success, buff = ml.run_model(silent=True, report=True) |
| 67 | +if success: |
| 68 | + for line in buff: |
| 69 | + print(line) |
| 70 | +else: |
| 71 | + raise ValueError("Failed to run.") |
| 72 | + |
| 73 | +files = ["freyberg.hds", "freyberg.cbc"] |
| 74 | +for f in files: |
| 75 | + if os.path.isfile(os.path.join(modelpth, f)): |
| 76 | + msg = "Output file located: {}".format(f) |
| 77 | + print(msg) |
| 78 | + else: |
| 79 | + errmsg = "Error. Output file cannot be found: {}".format(f) |
| 80 | + print(errmsg) |
| 81 | + |
| 82 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 83 | +# Each ``Util2d`` instance now has a ```.format``` attribute, which is an ```ArrayFormat``` instance: |
| 84 | + |
| 85 | +# + pycharm={"name": "#%%\n"} |
| 86 | +print(ml.lpf.hk[0].format) |
| 87 | + |
| 88 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 89 | +# The ```ArrayFormat``` class exposes each of the attributes seen in the ```ArrayFormat.___str___()``` call. ```ArrayFormat``` also exposes ``.fortran``, ``.py`` and ``.numpy`` atrributes, which are the respective format descriptors: |
| 90 | + |
| 91 | +# + pycharm={"name": "#%%\n"} |
| 92 | +print(ml.dis.botm[0].format.fortran) |
| 93 | +print(ml.dis.botm[0].format.py) |
| 94 | +print(ml.dis.botm[0].format.numpy) |
| 95 | + |
| 96 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 97 | +# #### (re)-setting ```.format``` |
| 98 | +# |
| 99 | +# We can reset the format using a standard fortran type format descriptor |
| 100 | + |
| 101 | +# + pycharm={"name": "#%%\n"} |
| 102 | +ml.dis.botm[0].format.free = False |
| 103 | +ml.dis.botm[0].format.fortran = "(20f10.4)" |
| 104 | +print(ml.dis.botm[0].format.fortran) |
| 105 | +print(ml.dis.botm[0].format.py) |
| 106 | +print(ml.dis.botm[0].format.numpy) |
| 107 | +print(ml.dis.botm[0].format) |
| 108 | +# - |
| 109 | + |
| 110 | +ml.write_input() |
| 111 | +success, buff = ml.run_model(silent=True, report=True) |
| 112 | +if success: |
| 113 | + for line in buff: |
| 114 | + print(line) |
| 115 | +else: |
| 116 | + raise ValueError("Failed to run.") |
| 117 | + |
| 118 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 119 | +# Let's load the model we just wrote and check that the desired ```botm[0].format``` was used: |
| 120 | + |
| 121 | +# + pycharm={"name": "#%%\n"} |
| 122 | +ml1 = flopy.modflow.Modflow.load("freyberg.nam", model_ws=modelpth) |
| 123 | +print(ml1.dis.botm[0].format) |
| 124 | + |
| 125 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 126 | +# We can also reset individual format components (we can also generate some warnings): |
| 127 | + |
| 128 | +# + pycharm={"name": "#%%\n"} |
| 129 | +ml.dis.botm[0].format.width = 9 |
| 130 | +ml.dis.botm[0].format.decimal = 1 |
| 131 | +print(ml1.dis.botm[0].format) |
| 132 | + |
| 133 | +# + [markdown] pycharm={"name": "#%% md\n"} |
| 134 | +# We can also select ``free`` format. Note that setting to free format resets the format attributes to the default, max precision: |
| 135 | + |
| 136 | +# + pycharm={"name": "#%%\n"} |
| 137 | +ml.dis.botm[0].format.free = True |
| 138 | +print(ml1.dis.botm[0].format) |
| 139 | +# - |
| 140 | + |
| 141 | +ml.write_input() |
| 142 | +success, buff = ml.run_model(silent=True, report=True) |
| 143 | +if success: |
| 144 | + for line in buff: |
| 145 | + print(line) |
| 146 | +else: |
| 147 | + raise ValueError("Failed to run.") |
| 148 | + |
| 149 | +# + pycharm={"name": "#%%\n"} |
| 150 | +ml1 = flopy.modflow.Modflow.load("freyberg.nam", model_ws=modelpth) |
| 151 | +print(ml1.dis.botm[0].format) |
0 commit comments