Skip to content
Daniel Wagner edited this page Jul 3, 2023 · 4 revisions

PyFVS

Introduction

PyFVS provides an interface to the FVS shared library in a Pythonic manner. From Python the full FVS API is available (similar to the R). Additionally, all FVS common data and arrays can be accessed at run-time. PyFVS utilizes the F2PY toolset for wrapping the FVS code in a Python C extension module.

Development and maintenance for this API is currently unsupported and has been tagged as archival. This code base can be restored by creating a new branch based off the archive/pyFVS tag.

 git checkout -b NEW_BRANCH_NAME archive/pyFVS 

Usage Example

"""
Demonstrates basic PyFVS functionality with mixed API and direct access.
"""
import numpy
import pyfvspnc as fvs

#array to store cycle summary variables
cycle_sum= numpy.zeros(20,dtype='I4')

cmd = '--keywordfile=foo.key'
fvs.fvssetcmdline(cmd)
for year in range(2010,2120,10):
  fvs.fvssetstoppointcodes(6,year)
  fvs.fvs()

  #get summary variables for the current growth cycle
  fvs.fvssummary(cycle_sum)
  
  #get tree variables directly from the FVS arrays
  #PyFVS returns Numpy Arrays
  tn = fvs.contrl.itrn #current number of tree records
  tpa = fvs.arrays.prob[:tn]
  dbh = fvs.arrays.dbh[:tn]
  baa = sum(dbh*dbh*0.005454154*tpa)

  print 'Year: {:<d} BAA: {:<.1f}'.format(year,baa)

Building PyFVS

Building the PyFVS extension is integrated with the Open-FVS CMake build system. Follow the instructions provided elsewhere in the wiki for building FVS. Note that currently the PyFVS branch has a modified CMakeLists.txt configuration file. See the associated readme_cmake.txt file for a description of usage and options.

The PyFVS build step is enabled by adding -DWITH_PYMOD=ON to the CMake configuration command.

PyFVS is known to build on 32 bit Windows XP using MinGW GCC version 4.7.0. It has also been built successfully on 64 bit UBuntu 13.10 with GCC 4.8.

Other compilers (Visual C, Intel) and platforms should work, but will likely require some modification to the CMakeLists.txt file for F2PY to function correctly. Any modifications to support other compilers would be a welcome contribution.

Support for SCons and Distutils has been dropped.

Build Requirements

Python

PyFVS is known to work with Python version 2.7 on Windows XP. However, it will likely work with any version and distribution supporting the Numpy Python library. PyFVS uses F2PY, which is currently included with Numpy. The build should work with any of the mainstream Python distributions, Python.org, Python(x,y), ArcGIS, etc.

PyFVS requires a 2.7+ Python installation to be available in the PATH environment variable at configuration time.

Check your version

python -V

F2PY

The F2PY Fortran interface generator is used to automate wrapping FVS subroutines and data objects into a Python CAPI extension. F2PY is provided within the Numpy library. Numpy is under the umbrella of SciPy, a suite of Python libraries for scientific and mathematical applications.

Install Numpy

Numpy is available from several sources:

Useful F2PY references