ARTS 2.5.11 (git: 725533f0)
nc_io_basic_types.cc
Go to the documentation of this file.
1
2// File description
4
13#include "arts.h"
14
15#ifdef ENABLE_NETCDF
16
17#include "nc_io.h"
18#include "nc_io_types.h"
19
20//=== Matrix ==========================================================
21
23
27void nca_read_from_file(const int ncid, Matrix& m, const Verbosity&) {
28 Index nrows, ncols;
29 nrows = nca_get_dim(ncid, "nrows");
30 ncols = nca_get_dim(ncid, "ncols");
31
32 m.resize(nrows, ncols);
33 nca_get_data(ncid, "Matrix", m.unsafe_data_handle());
34}
35
37
41void nca_write_to_file(const int ncid, const Matrix& m, const Verbosity&) {
42 int retval;
43 int ncdims[2], varid;
44 if ((retval = nc_def_dim(ncid, "nrows", m.nrows(), &ncdims[0])))
45 nca_error(retval, "nc_def_dim");
46 if ((retval = nc_def_dim(ncid, "ncols", m.ncols(), &ncdims[1])))
47 nca_error(retval, "nc_def_dim");
48 if ((retval = nc_def_var(ncid, "Matrix", NC_DOUBLE, 2, &ncdims[0], &varid)))
49 nca_error(retval, "nc_def_var");
50 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
51 if ((retval = nc_put_var_double(ncid, varid, m.unsafe_data_handle())))
52 nca_error(retval, "nc_put_var");
53}
54
55//=== Tensor3 ==========================================================
56
58
62void nca_read_from_file(const int ncid, Tensor3& t, const Verbosity&) {
63 Index npages, nrows, ncols;
64 npages = nca_get_dim(ncid, "npages");
65 nrows = nca_get_dim(ncid, "nrows");
66 ncols = nca_get_dim(ncid, "ncols");
67
68 t.resize(npages, nrows, ncols);
69 nca_get_data(ncid, "Tensor3", t.unsafe_data_handle());
70}
71
73
77void nca_write_to_file(const int ncid, const Tensor3& t, const Verbosity&) {
78 int retval;
79 int ncdims[3], varid;
80 if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[0])))
81 nca_error(retval, "nc_def_dim");
82 if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[1])))
83 nca_error(retval, "nc_def_dim");
84 if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[2])))
85 nca_error(retval, "nc_def_dim");
86 if ((retval = nc_def_var(ncid, "Tensor3", NC_DOUBLE, 3, &ncdims[0], &varid)))
87 nca_error(retval, "nc_def_var");
88 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
89 if ((retval = nc_put_var_double(ncid, varid, t.unsafe_data_handle())))
90 nca_error(retval, "nc_put_var");
91}
92
93//=== Tensor4 ==========================================================
94
96
100void nca_read_from_file(const int ncid, Tensor4& t, const Verbosity&) {
101 Index nbooks, npages, nrows, ncols;
102 nbooks = nca_get_dim(ncid, "nbooks");
103 npages = nca_get_dim(ncid, "npages");
104 nrows = nca_get_dim(ncid, "nrows");
105 ncols = nca_get_dim(ncid, "ncols");
106
107 t.resize(nbooks, npages, nrows, ncols);
108 nca_get_data(ncid, "Tensor4", t.unsafe_data_handle());
109}
110
112
116void nca_write_to_file(const int ncid, const Tensor4& t, const Verbosity&) {
117 int retval;
118 int ncdims[4], varid;
119 if ((retval = nc_def_dim(ncid, "nbooks", t.nbooks(), &ncdims[0])))
120 nca_error(retval, "nc_def_dim");
121 if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[1])))
122 nca_error(retval, "nc_def_dim");
123 if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[2])))
124 nca_error(retval, "nc_def_dim");
125 if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[3])))
126 nca_error(retval, "nc_def_dim");
127 if ((retval = nc_def_var(ncid, "Tensor4", NC_DOUBLE, 4, &ncdims[0], &varid)))
128 nca_error(retval, "nc_def_var");
129 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
130 if ((retval = nc_put_var_double(ncid, varid, t.unsafe_data_handle())))
131 nca_error(retval, "nc_put_var");
132}
133
134//=== Tensor5 ==========================================================
135
137
141void nca_read_from_file(const int ncid, Tensor5& t, const Verbosity&) {
142 Index nshelves, nbooks, npages, nrows, ncols;
143 nshelves = nca_get_dim(ncid, "nshelves");
144 nbooks = nca_get_dim(ncid, "nbooks");
145 npages = nca_get_dim(ncid, "npages");
146 nrows = nca_get_dim(ncid, "nrows");
147 ncols = nca_get_dim(ncid, "ncols");
148
149 t.resize(nshelves, nbooks, npages, nrows, ncols);
150 nca_get_data(ncid, "Tensor5", t.unsafe_data_handle());
151}
152
154
158void nca_write_to_file(const int ncid, const Tensor5& t, const Verbosity&) {
159 int retval;
160 int ncdims[5], varid;
161 if ((retval = nc_def_dim(ncid, "nshelves", t.nshelves(), &ncdims[0])))
162 nca_error(retval, "nc_def_dim");
163 if ((retval = nc_def_dim(ncid, "nbooks", t.nbooks(), &ncdims[1])))
164 nca_error(retval, "nc_def_dim");
165 if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[2])))
166 nca_error(retval, "nc_def_dim");
167 if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[3])))
168 nca_error(retval, "nc_def_dim");
169 if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[4])))
170 nca_error(retval, "nc_def_dim");
171 if ((retval = nc_def_var(ncid, "Tensor5", NC_DOUBLE, 5, &ncdims[0], &varid)))
172 nca_error(retval, "nc_def_var");
173 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
174 if ((retval = nc_put_var_double(ncid, varid, t.unsafe_data_handle())))
175 nca_error(retval, "nc_put_var");
176}
177
178//=== Vector ==========================================================
179
181
185void nca_read_from_file(const int ncid, Vector& v, const Verbosity&) {
186 Index nelem;
187 nelem = nca_get_dim(ncid, "nelem");
188
189 v.resize(nelem);
190 nca_get_data(ncid, "Vector", v.unsafe_data_handle());
191}
192
194
198void nca_write_to_file(const int ncid, const Vector& v, const Verbosity&) {
199 int retval;
200 int ncdim, varid;
201 if ((retval = nc_def_dim(ncid, "nelem", v.nelem(), &ncdim)))
202 nca_error(retval, "nc_def_dim");
203 if ((retval = nc_def_var(ncid, "Vector", NC_DOUBLE, 1, &ncdim, &varid)))
204 nca_error(retval, "nc_def_var");
205 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
206 if ((retval = nc_put_var_double(ncid, varid, v.unsafe_data_handle())))
207 nca_error(retval, "nc_put_var");
208}
209
211// Dummy funtion for groups for which
212// IO function have not yet been implemented
214
215#define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
216 void nca_write_to_file(const int, const what &, const Verbosity &) { \
217 ARTS_USER_ERROR("NetCDF support not yet implemented for this type!"); \
218 } \
219 void nca_read_from_file(const int, what &, const Verbosity &) { \
220 ARTS_USER_ERROR("NetCDF support not yet implemented for this type!"); \
221 }
222
223//==========================================================================
224
225// Undefine the macro to avoid it being used anywhere else
226#undef TMPL_NC_READ_WRITE_FILE_DUMMY
227
228#endif /* ENABLE_NETCDF */
The global header file for ARTS.
Index nca_get_dim(const int ncid, const String &name, const bool noerror)
Read a dimension from NetCDF file.
Definition nc_io.cc:279
void nca_error(const int e, const std::string_view s)
Throws a runtime error for the given NetCDF error code.
Definition nc_io.cc:626
void nca_get_data(const int ncid, const String &name, int *data)
Read variable of type int from NetCDF file.
Definition nc_io.cc:306
This file contains basic functions to handle NetCDF data files.
void nca_read_from_file(const int ncid, Matrix &m, const Verbosity &)
Reads a Matrix from a NetCDF file.
void nca_write_to_file(const int ncid, const Matrix &m, const Verbosity &)
Writes a Matrix to a NetCDF file.
This file contains private function declarations and template instantiation to handle NetCDF data fil...
#define v