https://github.com/geospace-code/nc4fortran
Object-oriented Fortran NetCDF4 interface
https://github.com/geospace-code/nc4fortran
netcdf4 object-oriented-fortran
Last synced: 4 months ago
JSON representation
Object-oriented Fortran NetCDF4 interface
- Host: GitHub
- URL: https://github.com/geospace-code/nc4fortran
- Owner: geospace-code
- License: mit
- Created: 2019-12-23T19:38:38.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-08-27T01:03:53.000Z (10 months ago)
- Last Synced: 2025-01-11T16:38:44.903Z (5 months ago)
- Topics: netcdf4, object-oriented-fortran
- Language: CMake
- Homepage:
- Size: 464 KB
- Stars: 41
- Watchers: 4
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Citation: CITATION.cff
- Codemeta: codemeta.json
Awesome Lists containing this project
README
# Object-oriented Fortran NetCDF4 interface
[](https://zenodo.org/badge/latestdoi/229812591)
[](https://github.com/geospace-code/nc4fortran/actions/workflows/ci.yml)
[](https://github.com/geospace-code/nc4fortran/actions/workflows/ci_windows.yml)
[](https://github.com/geospace-code/nc4fortran/actions/workflows/ci_fpm.yml)Simple, robust, thin, object-oriented NetCDF4 polymorphic read/write interface.
For HDF5 see [h5fortran](https://github.com/geospace-code/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-DAlso:
* read/write **character** variables.
* read/write character, int, float, double attributesDatatypes are coerced as per standard Fortran rules.
For example, reading a float NetCDF4 variable into an integer Fortran variable: 42.3 => 42Tested on systems with NetCDF4 including:
* MacOS
* Linux
* WindowsSee [API](./API.md) 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
```sh
cmake -B build
cmake --build build# optional
ctest --test-dir build
```To specify a particular NetCDF library, use
```sh
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:
```cmake
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)
```sh
fpm build
fpm test
fpm install
```## Acknowledgements
nc4fortran was funded in part by NASA NNH19ZDA001N-HDEE grant 80NSSC20K0176.