Code Monkey home page Code Monkey logo

python-flopy-parser's Introduction

DOI Build Status

Table of Content

flopyparser

Converts a zip with MODFLOW input files to a zip containing Flopy script in different formats. This Flopy (Python) script can generate the intitial MODFLOW input files.

workflow

It should work for all packages of MODFLOW, MT3D, and SEAWAT. For a complete list, see the Packages with default values and the load supported packages on the Flopy website.

The flopyparser is continously tested with several benchmark modflow models. This ensures that the generated python scripts contain valid code and produce the same heads and flow as the benchmark input files.

The software is available under MIT license. The author has absolutely no convidense in that the software is correct and is not responsible for the content and consequences of malicious scripts. I you find it useful, please consider donating to charity (be creative in choosing which one) and send me a note (or create and close an issue). Thanks! The author is not affiliated with the modflow family nor with Flopy. This converter/generator uses the Flopy load function. Any errors/mistakes in the Flopy load functions propagate silently in to the generated script.

Use cases

  • You are coming from a different modeling environment and want to start using Flopy
  • Clean up your flopy script/notebook
  • Add a description (and default value) to your parameters
  • Check someone else's MODFLOW input files / Flopy script
  • Check homework assignments
  • Start from scratch by adding your own packages

Highlights

  • Returns .ipynb, py, tex, html, markdown, and rst file of your MODFLOW input files
  • Consistent and clean markup is used
  • All the parameters are defined explicitely
  • A description is loaded and interpreted from the flopy package directly. The same description as in the docs (modflowpy.github.io/flopydoc/) is used.
  • Makes use of smart broadcasting to reduce the size of the arrays printed to the script.

Install

Enter in the terminal,

$ pip install https://github.com/bdestombe/python-flopy-parser/archive/master.zip

The $-sign should be omitted, and only refers to that the command is to be entered in the bash-commandline. The flopyparser package added to system's $PATH and is reachable from any directory. Check if everything works by typing in any directory,

$ flopyparser --help

Uninstall with,

$ pip uninstall flopyparser

Example usage from the commandline:

Using zipfiles from the commandline

Try this first,

$ flopyparser --outbytesfile output.zip --inbytesfile input.zip --logfile log.txt

input.zip is a zip-file that contains MODFLOW input files and a single .nam file. Its content is processed and written to output.zip. Some logging is written to log.txt.

Using pipes from the commandline

Might be of interest when using flopyparser as webservice.

$ openssl base64 -in input.zip -out input.zip.b64
$ flopyparser --outbytesfile output.zip --inbase64file input.zip.b64

Here, in the first line input.zip is encoded to base64 and is used in the second line as input file for flopyparser.

$ flopyparser --outbytesfile output.zip --inbase64file - < input.zip.b64

The content of input.zip.b64 is streamed/piped to flopyparser

$ openssl base64 -in input.zip | flopyparser --outbytesfile output.zip --inbase64file -

The same as what is done previously, however input.zip is encoded and instead of writing it to a file, it is passed as stdin to the inbase64file argument of flopyparser.

$ openssl base64 -in input.zip | flopyparser --outbase64file utput.zip --inbase64file - --logfile -

The log file is printed to stdout.

You cannot send both outbase64file and logfile to stdout. They will be mixed and the resulting output file is not readable.

Example usage in Python

Using zipfiles in Python

from flopyparser.flopyparser import process

inbytesfn = 'input.zip'      # Dont forget the b flag when opening the file
outbytesfn = 'output.zip'.   # Dont forget the b flag when opening the file
logfn = 'log.txt'

with open(inbytesfn, 'rb') as inbytesfh, \
        open(outbytesfn, 'wb') as outbytesfh, \
        open(logfn, 'w') as logfh:
    process(inbytesfile=inbytesfile, 
            outbytesfile=outbytesfile, 
            logfile=logfile)

Using as a function in Python

This example loads a name-file and overwrites the dis and bas6 package with the default parameter values. If dis and bas6 were not loaded with the name file, they are added. Extra options are now accessible, such as print_descr for printing the parameter description (bool), width for the desired line width of the produced script (int, number of characters), use_yapf to use Google's package to format the produced code (bool, conversion becomes slow).

from flopyparser.model import Model

mp = Model(load_nam='path_to_namfile.nam', add_pack=['dis', 'bas6'])
fn = 'path_to_jupyter_notebook.ipynb'

mp.write_script_model2string(fn=fn,
                             print_descr=True,
                             width=99,
                             use_yapf=True)

python-flopy-parser's People

Contributors

bdestombe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

python-flopy-parser's Issues

Testing a repository with modflow input files

Note to self:

  • Goal is to share a model in a github repository
  • Sharing the modflow input files is better (reproducible) than sharing just Python code. Ensures all the code is packed. Independent of flopy.
  • But lacks the code descriptions, easy edits, easy access to plot functions that Python has
  • Therefore create a repository that only contains input files (and a dictionary with parameter comments). Github actions creates the python files that are nice, documented and sharable. Using flopymetascript via tox to also be usable on PC.
  • Every change to the repository results in a diff of the input files, and a diff created by the Github actions that changes the Python files.
  • Test exec(python script) to ensure the code is runable, stop at writing input files, do not actually run inputfiles. Flopymetascript tests should ensure the further functioning of the generated python script.
  • sharing the repository is enough for someone to run, understand, see history, make changes

Bringing this into flopy

Hi @bdestombe, in recent development talks there is some interest in a flopy facility to convert MODFLOW/etc input files to equivalent Python. One motivation, besides those you list in the readme, is programmatic access to example models. A first step in this direction is to define the models in Python, to avoid the need to package input files with flopy.

This project seems like a great starting point. Would it be OK to build on your work here? The idea being to generalize it and add support for MF6.

If you'd like to contribute that would be very welcome, but certainly no obligation/expectation. In any case, if this does make it into flopy, the authors/citation could be updated to reflect your efforts here. Thanks in advance.

@langevin-usgs

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.