Skip to content

Commit bdd3916

Browse files
committed
add examples
1 parent 7052602 commit bdd3916

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

Examples/CMakeLists.txt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(nc4fortranExample
3+
LANGUAGES C CXX Fortran)
4+
enable_testing()
5+
6+
find_package(nc4fortran CONFIG)
7+
8+
if(nc4fortran_FOUND)
9+
include(${nc4fortran_DIR}/nc4fortranTargets.cmake)
10+
else()
11+
include(FetchContent)
12+
FetchContent_Declare(nc4fortran_proj
13+
GIT_REPOSITORY https://github.com/geospace-code/nc4fortran.git
14+
GIT_TAG v1.0.2)
15+
FetchContent_MakeAvailable(nc4fortran_proj)
16+
endif()
17+
18+
if(NOT NCDFOK)
19+
message(FATAL_ERROR "NetCDF4 is not working on your system, so nc4fortran cannot work.")
20+
endif()
21+
22+
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
23+
24+
# --- Fortran interface for examples
25+
add_library(fortran_interface fortran_interface.f90)
26+
target_link_libraries(fortran_interface PRIVATE nc4fortran::nc4fortran)
27+
28+
# --- example 1
29+
add_executable(example1 example1.f90)
30+
target_link_libraries(example1 nc4fortran::nc4fortran)
31+
add_test(NAME nc4fortran:Example1 COMMAND $<TARGET_FILE:example1>
32+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
33+
34+
# --- example 2
35+
add_executable(example2 example2.f90)
36+
target_link_libraries(example2 nc4fortran::nc4fortran)
37+
add_test(NAME nc4fortran:Example2 COMMAND $<TARGET_FILE:example2>
38+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})

Examples/Readme.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# nc4ortran Example project
2+
3+
It's easiest to use CMake with nc4fortran, since HDF5 consists of many library files and headers.
4+
From the nc4fortran/ directory, build the examples (or change to the Examples/ directory):
5+
6+
```sh
7+
cmake -B Examples/build -S Examples
8+
cmake --build Examples/build
9+
```
10+
11+
which creates examples under the Examples/build direcotry
12+
13+
If you installed nc4fortran previously, you can use that version by:
14+
15+
```sh
16+
cmake -B Examples/build -S Examples -Dnc4fortran_DIR=~/libs/nc4fortran/lib/cmake/nc4fortran
17+
```
18+
19+
## Examples
20+
21+
Example 1 and 2 show the object-oriented interface of nc4fortran, which may offer faster performance if more than one variable is being read or written.

Examples/example1.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
program example1
2+
3+
use nc4fortran, only : netcdf_file
4+
implicit none (type, external)
5+
6+
type(netcdf_file) :: h
7+
character(:), allocatable :: filename
8+
integer :: i32
9+
10+
filename = 'nc4fortran_example1.nc'
11+
12+
call h%initialize(filename, status='replace')
13+
14+
call h%write( 'x', 123)
15+
16+
call h%read('x', i32)
17+
call h%finalize()
18+
19+
if (i32 /= 123) error stop 'incorrect value read'
20+
21+
print *, 'OK: example 1'
22+
23+
end program

Examples/example2.f90

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
program example2
2+
3+
use nc4fortran, only : netcdf_file
4+
implicit none (type, external)
5+
6+
character(:), allocatable :: filename
7+
integer :: i32
8+
9+
type(netcdf_file) :: h
10+
11+
filename = 'nc4fortran_example2.nc'
12+
13+
call h%initialize(filename, status='replace')
14+
call h%write('x', 123)
15+
call h%finalize()
16+
17+
call h%initialize(filename, status='old', action='r')
18+
call h%read('x', i32)
19+
if (i32 /= 123) error stop 'incorrect value read'
20+
21+
print *, 'OK: example 2'
22+
23+
end program

Examples/fortran_interface.f90

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module fortran_interface
2+
!! filename(256) and var_name(64) are arbitrary sizes.
3+
!! ensure calling program buffers are equally sized
4+
use, intrinsic :: iso_c_binding, only : C_INT32_T, C_CHAR, C_NULL_CHAR
5+
use nc4fortran, only : netcdf_file
6+
7+
implicit none (type, external)
8+
9+
contains
10+
11+
12+
subroutine write_int32(filename, var_name, i32) bind(C)
13+
character(kind=C_CHAR) :: filename(256)
14+
character(kind=C_CHAR) :: var_name(64)
15+
integer(C_INT32_T), intent(in) :: i32
16+
type(netcdf_file) :: h
17+
18+
call h%initialize(cstr2fstr(filename), status='replace')
19+
call h%write(cstr2fstr(var_name), i32)
20+
call h%finalize()
21+
22+
end subroutine write_int32
23+
24+
25+
subroutine read_int32(filename, var_name, i32) bind(C)
26+
character(kind=C_CHAR) :: filename(256)
27+
character(kind=C_CHAR) :: var_name(64)
28+
integer(C_INT32_T), intent(out) :: i32
29+
type(netcdf_file) :: h
30+
31+
call h%initialize(cstr2fstr(filename), status='old', action='r')
32+
call h%read(cstr2fstr(var_name), i32)
33+
call h%finalize()
34+
35+
end subroutine read_int32
36+
37+
38+
function cstr2fstr(c_str) result(f_str)
39+
40+
character(kind=C_CHAR), intent(in) :: c_str(:)
41+
character(len=size(c_str)) :: buf
42+
character(:), allocatable :: f_str
43+
integer :: i
44+
45+
buf = ""
46+
!! clean variable, will get extra garbled text otherwise, maybe blank but non-trimmable character
47+
48+
do i = 1, size(c_str)
49+
if (c_str(i) == C_NULL_CHAR) exit
50+
buf(i:i) = c_str(i)
51+
enddo
52+
53+
f_str = trim(buf)
54+
55+
end function cstr2fstr
56+
57+
end module fortran_interface

0 commit comments

Comments
 (0)