Skip to content

Commit 570ca16

Browse files
committed
doc: update api
1 parent 98c29d7 commit 570ca16

File tree

1 file changed

+98
-29
lines changed

1 file changed

+98
-29
lines changed

API.md

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,134 @@
1-
# nc4fortran Usage
1+
# nc4fortran API
2+
3+
This document provides a listing of nc4fortran `public` scoped user-facing procedures and methods with a summary of their parameters.
24

35
All examples assume:
46

57
```fortran
68
use nc4fortran, only: netcdf_file
7-
type(netcdf_file) :: hf
8-
```
99
10-
* gzip compression may be applied for rank ≥ 2 arrays by setting `comp_lvl` to a value between 1 and 9.
11-
Shuffle filter is automatically applied for better compression
12-
* string attributes may be applied to any variable at time of writing or later.
10+
type(netcdf_file) :: h
11+
```
1312

14-
Check NetCDF4 library version:
13+
Query NetCDF4 library version:
1514

1615
```fortran
17-
use nc4fortran, only: nc4version
16+
use nc4fortran, only : nc4version
1817
print *, nc4version()
1918
```
2019

21-
Create new NetCDF file, with variable "value1"
20+
## Open NetCDF4 file reference
21+
22+
More than one NetCDF4 file can be open in a program, by declaring unique file handle (variable) like:
23+
24+
```fortran
25+
type(netcdf_file) :: h1, h2, h3
26+
```
27+
28+
```fortran
29+
call h%open(filename, action, comp_lvl)
30+
!! Opens hdf5 file
31+
32+
character(*), intent(in) :: filename
33+
character(*), intent(in), optional :: action !< r, w, rw
34+
integer, intent(in), optional :: comp_lvl !< 0: no compression. 1-9: ZLIB compression, higher is more compressior
35+
```
36+
37+
## Close NetCDF4 file reference
38+
39+
```fortran
40+
call h%close()
41+
!! This must be called on each open file to flush buffers to disk
42+
!! data loss can occur if program terminates before this procedure
43+
```
44+
45+
To avoid memory leaks or corrupted files, always "close" files before STOPping the Fortran program.
46+
47+
## Flush data to disk while file is open
48+
49+
```fortran
50+
call h%flush()
51+
```
52+
53+
## Disk variable (dataset) inquiry
54+
55+
To allocate variables before reading data, inquire about dataset characteristics with these procedures.
2256

2357
```fortran
24-
call hf%open('test.nc', action='w')
58+
rank = h%ndim(dataset_name)
59+
60+
character(*), intent(in) :: dataset_name
61+
```
62+
63+
Get disk dataset shape (1D vector)
2564

26-
call hf%write('value1', 123.)
65+
```fortran
66+
call h%shape(dataset_name, dims)
2767
28-
call hf%close()
68+
character(*), intent(in) :: dataset_name
69+
integer(HSIZE_T), intent(out), allocatable :: dims(:)
2970
```
3071

31-
Check if variable exists
72+
Does dataset "dname" exist in this HDF5 file?
3273

3374
```fortran
34-
logical :: exists
75+
tf = h%exist(dname)
3576
36-
exists = hf%exist('fooname')
77+
character(*), intent(in) :: dname
3778
```
3879

39-
Add/append variable "value1" to existing NetCDF file "test.nc"
80+
Is dataset "dname" contiguous on disk?
81+
82+
```fortran
83+
tf = h%is_contig(dname)
84+
85+
character(*), intent(in) :: dname
86+
```
4087

41-
* if file `test.nc` exists, add a variable to it
42-
* if file `test.nc` does not exist, create it and add a variable to it.
88+
These are more advanced inquiries into the memory layout of the dataset, for advanced users:
4389

4490
```fortran
45-
call hf%open('test.nc', action='rw')
91+
call h%chunks(dname, chunk_size)
4692
47-
call hf%write('value1', 123.)
93+
character(*), intent(in) :: dname
94+
integer, intent(out) :: chunk_size(:)
95+
```
96+
97+
## file write operations
4898

49-
call hf%close()
99+
```fortran
100+
call h%write(dname,value, istart, iend, stride, chunk_size)
101+
!! write 0d..7d dataset
102+
character(*), intent(in) :: dname
103+
class(*), intent(in) :: value(:) !< array to write
104+
integer, intent(in), optional :: chunk_size(rank(value))
105+
integer, intent(in), optional, dimension(:) :: istart, iend, stride !< array slicing
50106
```
51107

52-
Read scalar, 3-D array of unknown size
108+
Write dataset attribute (e.g. units or instrument)
53109

54110
```fortran
55-
call ncf%open('test.nc', action='r')
111+
call h%writeattr(dname, attr, attrval)
112+
113+
character(*), intent(in) :: dname, attr !< dataset name, attribute name
114+
class(*), intent(in) :: attrval(:) !< character, real, integer
115+
```
56116

57-
integer, allocatable :: dims(:)
58-
real, allocatable :: A(:,:,:)
117+
## file read operations
59118

60-
call ncf%shape('foo', dims)
61-
allocate(A(dims(1), dims(2), dims(3)))
62-
call ncf%read('foo', A)
119+
Read data from disk to memory
63120

64-
call ncf%close()
121+
```fortran
122+
call h%read(dname, value, istart, iend, stride)
123+
character(*), intent(in) :: dname
124+
class(*), intent(out) :: value(:) !< read array to this ALLOCATED variable
125+
integer, intent(in), optional, dimension(:) :: istart, iend, stride !< array slicing
126+
```
127+
128+
Read dataset attribute into memory
129+
130+
```fortran
131+
call h%readattr(dname, attr, attrval)
132+
character(*), intent(in) :: dname, attr !< dataset name, attribute name
133+
class(*), intent(out) :: attrval(:) !< character, real, integer
65134
```

0 commit comments

Comments
 (0)