Skip to content

geospace-code/nc4fortran

Repository files navigation

Object-oriented Fortran NetCDF4 interface

DOI ci ci_windows ci_fpm

Simple, robust, thin, object-oriented NetCDF4 polymorphic read/write interface. For HDF5 see h5fortran. Designed for easy use as a CMake "ExternalProject" using static or shared linking. Uses Fortran 2008 submodule for clean template structure. nc4fortran abstracts away the messy parts of NetCDF4 so that you can read/write various types/ranks of data with a single command. In distinction from other high-level NetCDF4 interfaces, nc4fortran works to deduplicate code, using polymorphism wherever feasible, with an extensive test suite.

Polymorphic API with read/write for types int32, int64, real32, real64 with rank:

  • scalar (0-D)
  • 1-D .. 7-D

Also:

  • read/write character variables.
  • read/write character, int, float, double attributes

Datatypes are coerced as per standard Fortran rules. For example, reading a float NetCDF4 variable into an integer Fortran variable: 42.3 => 42

Tested on systems with NetCDF4 including:

  • MacOS
  • Linux
  • Windows

See API for usage.

Build

Requirements:

  • modern Fortran compiler: examples: GCC ≥ 7 or Intel oneAPI ≥ 2021
  • NetCDF4 Fortran library
    • Mac / Homebrew: brew install gcc netcdf
    • Linux: apt install gfortran libnetcdf-dev libnetcdff-dev
    • Windows Subsystem for Linux: apt install gfortran libnetcdf-dev libnetcdff-dev
    • Windows Cygwin libnetcdf-fortran-devel

Note that some precompiled NetCDF4 libraries include C / C++ without Fortran.

Build this NetCDF OO Fortran interface. The library libnc4fortran.a is built, link it into your program as usual.

CMake

cmake -B build
cmake --build build

# optional
ctest --test-dir build

To specify a particular NetCDF library, use

cmake -DNetCDF_ROOT=/path/to/netcdff -B build

or set environment variable NetCDF_ROOT=/path/to/netcdff

To use nc4fortran as a CMake ExternalProject do like:

include(FetchContent)

FetchContent_Declare(nc4fortran_proj
GIT_REPOSITORY https://github.com/geospace-code/nc4fortran.git
)

FetchContent_MakeAvailable(nc4fortran_proj)

# ------------------------------------------------------
# whatever your program is
add_executable(myProj main.f90)
target_link_libraries(myProj nc4fortran::nc4fortran)

Fortran Package Manager (fpm)

fpm build
fpm test
fpm install

Acknowledgements

nc4fortran was funded in part by NASA NNH19ZDA001N-HDEE grant 80NSSC20K0176.