|
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. |
2 | 4 |
|
3 | 5 | All examples assume: |
4 | 6 |
|
5 | 7 | ```fortran |
6 | 8 | use nc4fortran, only: netcdf_file |
7 | | -type(netcdf_file) :: hf |
8 | | -``` |
9 | 9 |
|
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 | +``` |
13 | 12 |
|
14 | | -Check NetCDF4 library version: |
| 13 | +Query NetCDF4 library version: |
15 | 14 |
|
16 | 15 | ```fortran |
17 | | -use nc4fortran, only: nc4version |
| 16 | +use nc4fortran, only : nc4version |
18 | 17 | print *, nc4version() |
19 | 18 | ``` |
20 | 19 |
|
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. |
22 | 56 |
|
23 | 57 | ```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) |
25 | 64 |
|
26 | | -call hf%write('value1', 123.) |
| 65 | +```fortran |
| 66 | +call h%shape(dataset_name, dims) |
27 | 67 |
|
28 | | -call hf%close() |
| 68 | +character(*), intent(in) :: dataset_name |
| 69 | +integer(HSIZE_T), intent(out), allocatable :: dims(:) |
29 | 70 | ``` |
30 | 71 |
|
31 | | -Check if variable exists |
| 72 | +Does dataset "dname" exist in this HDF5 file? |
32 | 73 |
|
33 | 74 | ```fortran |
34 | | -logical :: exists |
| 75 | +tf = h%exist(dname) |
35 | 76 |
|
36 | | -exists = hf%exist('fooname') |
| 77 | +character(*), intent(in) :: dname |
37 | 78 | ``` |
38 | 79 |
|
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 | +``` |
40 | 87 |
|
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: |
43 | 89 |
|
44 | 90 | ```fortran |
45 | | -call hf%open('test.nc', action='rw') |
| 91 | +call h%chunks(dname, chunk_size) |
46 | 92 |
|
47 | | -call hf%write('value1', 123.) |
| 93 | +character(*), intent(in) :: dname |
| 94 | +integer, intent(out) :: chunk_size(:) |
| 95 | +``` |
| 96 | + |
| 97 | +## file write operations |
48 | 98 |
|
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 |
50 | 106 | ``` |
51 | 107 |
|
52 | | -Read scalar, 3-D array of unknown size |
| 108 | +Write dataset attribute (e.g. units or instrument) |
53 | 109 |
|
54 | 110 | ```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 | +``` |
56 | 116 |
|
57 | | -integer, allocatable :: dims(:) |
58 | | -real, allocatable :: A(:,:,:) |
| 117 | +## file read operations |
59 | 118 |
|
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 |
63 | 120 |
|
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 |
65 | 134 | ``` |
0 commit comments