ARTS 2.5.10 (git: 2f1c442c)
nc_io_basic_types.cc
Go to the documentation of this file.
1/* Copyright (C) 2003-2012 Oliver Lemke <olemke@core-dump.info>
2
3 This program is free software; you can redistribute it and/or modify it
4 under the terms of the GNU General Public License as published by the
5 Free Software Foundation; either version 2, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 USA. */
17
19// File description
21
30#include "arts.h"
31
32#ifdef ENABLE_NETCDF
33
34#include "nc_io.h"
35#include "nc_io_types.h"
36
37//=== Matrix ==========================================================
38
40
44void nca_read_from_file(const int ncid, Matrix& m, const Verbosity&) {
45 Index nrows, ncols;
46 nrows = nca_get_dim(ncid, "nrows");
47 ncols = nca_get_dim(ncid, "ncols");
48
49 m.resize(nrows, ncols);
50 nca_get_data(ncid, "Matrix", m.get_c_array());
51}
52
54
58void nca_write_to_file(const int ncid, const Matrix& m, const Verbosity&) {
59 int retval;
60 int ncdims[2], varid;
61 if ((retval = nc_def_dim(ncid, "nrows", m.nrows(), &ncdims[0])))
62 nca_error(retval, "nc_def_dim");
63 if ((retval = nc_def_dim(ncid, "ncols", m.ncols(), &ncdims[1])))
64 nca_error(retval, "nc_def_dim");
65 if ((retval = nc_def_var(ncid, "Matrix", NC_DOUBLE, 2, &ncdims[0], &varid)))
66 nca_error(retval, "nc_def_var");
67 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
68 if ((retval = nc_put_var_double(ncid, varid, m.get_c_array())))
69 nca_error(retval, "nc_put_var");
70}
71
72//=== Tensor3 ==========================================================
73
75
79void nca_read_from_file(const int ncid, Tensor3& t, const Verbosity&) {
80 Index npages, nrows, ncols;
81 npages = nca_get_dim(ncid, "npages");
82 nrows = nca_get_dim(ncid, "nrows");
83 ncols = nca_get_dim(ncid, "ncols");
84
85 t.resize(npages, nrows, ncols);
86 nca_get_data(ncid, "Tensor3", t.get_c_array());
87}
88
90
94void nca_write_to_file(const int ncid, const Tensor3& t, const Verbosity&) {
95 int retval;
96 int ncdims[3], varid;
97 if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[0])))
98 nca_error(retval, "nc_def_dim");
99 if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[1])))
100 nca_error(retval, "nc_def_dim");
101 if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[2])))
102 nca_error(retval, "nc_def_dim");
103 if ((retval = nc_def_var(ncid, "Tensor3", NC_DOUBLE, 3, &ncdims[0], &varid)))
104 nca_error(retval, "nc_def_var");
105 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
106 if ((retval = nc_put_var_double(ncid, varid, t.get_c_array())))
107 nca_error(retval, "nc_put_var");
108}
109
110//=== Tensor4 ==========================================================
111
113
117void nca_read_from_file(const int ncid, Tensor4& t, const Verbosity&) {
118 Index nbooks, npages, nrows, ncols;
119 nbooks = nca_get_dim(ncid, "nbooks");
120 npages = nca_get_dim(ncid, "npages");
121 nrows = nca_get_dim(ncid, "nrows");
122 ncols = nca_get_dim(ncid, "ncols");
123
124 t.resize(nbooks, npages, nrows, ncols);
125 nca_get_data(ncid, "Tensor4", t.get_c_array());
126}
127
129
133void nca_write_to_file(const int ncid, const Tensor4& t, const Verbosity&) {
134 int retval;
135 int ncdims[4], varid;
136 if ((retval = nc_def_dim(ncid, "nbooks", t.nbooks(), &ncdims[0])))
137 nca_error(retval, "nc_def_dim");
138 if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[1])))
139 nca_error(retval, "nc_def_dim");
140 if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[2])))
141 nca_error(retval, "nc_def_dim");
142 if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[3])))
143 nca_error(retval, "nc_def_dim");
144 if ((retval = nc_def_var(ncid, "Tensor4", NC_DOUBLE, 4, &ncdims[0], &varid)))
145 nca_error(retval, "nc_def_var");
146 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
147 if ((retval = nc_put_var_double(ncid, varid, t.get_c_array())))
148 nca_error(retval, "nc_put_var");
149}
150
151//=== Tensor5 ==========================================================
152
154
158void nca_read_from_file(const int ncid, Tensor5& t, const Verbosity&) {
159 Index nshelves, nbooks, npages, nrows, ncols;
160 nshelves = nca_get_dim(ncid, "nshelves");
161 nbooks = nca_get_dim(ncid, "nbooks");
162 npages = nca_get_dim(ncid, "npages");
163 nrows = nca_get_dim(ncid, "nrows");
164 ncols = nca_get_dim(ncid, "ncols");
165
166 t.resize(nshelves, nbooks, npages, nrows, ncols);
167 nca_get_data(ncid, "Tensor5", t.get_c_array());
168}
169
171
175void nca_write_to_file(const int ncid, const Tensor5& t, const Verbosity&) {
176 int retval;
177 int ncdims[5], varid;
178 if ((retval = nc_def_dim(ncid, "nshelves", t.nshelves(), &ncdims[0])))
179 nca_error(retval, "nc_def_dim");
180 if ((retval = nc_def_dim(ncid, "nbooks", t.nbooks(), &ncdims[1])))
181 nca_error(retval, "nc_def_dim");
182 if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[2])))
183 nca_error(retval, "nc_def_dim");
184 if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[3])))
185 nca_error(retval, "nc_def_dim");
186 if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[4])))
187 nca_error(retval, "nc_def_dim");
188 if ((retval = nc_def_var(ncid, "Tensor5", NC_DOUBLE, 5, &ncdims[0], &varid)))
189 nca_error(retval, "nc_def_var");
190 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
191 if ((retval = nc_put_var_double(ncid, varid, t.get_c_array())))
192 nca_error(retval, "nc_put_var");
193}
194
195//=== Vector ==========================================================
196
198
202void nca_read_from_file(const int ncid, Vector& v, const Verbosity&) {
203 Index nelem;
204 nelem = nca_get_dim(ncid, "nelem");
205
206 v.resize(nelem);
207 nca_get_data(ncid, "Vector", v.get_c_array());
208}
209
211
215void nca_write_to_file(const int ncid, const Vector& v, const Verbosity&) {
216 int retval;
217 int ncdim, varid;
218 if ((retval = nc_def_dim(ncid, "nelem", v.nelem(), &ncdim)))
219 nca_error(retval, "nc_def_dim");
220 if ((retval = nc_def_var(ncid, "Vector", NC_DOUBLE, 1, &ncdim, &varid)))
221 nca_error(retval, "nc_def_var");
222 if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
223 if ((retval = nc_put_var_double(ncid, varid, v.get_c_array())))
224 nca_error(retval, "nc_put_var");
225}
226
228// Dummy funtion for groups for which
229// IO function have not yet been implemented
231
232#define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
233 void nca_write_to_file(const int, const what &, const Verbosity &) { \
234 ARTS_USER_ERROR("NetCDF support not yet implemented for this type!"); \
235 } \
236 void nca_read_from_file(const int, what &, const Verbosity &) { \
237 ARTS_USER_ERROR("NetCDF support not yet implemented for this type!"); \
238 }
239
240//==========================================================================
241
242// Undefine the macro to avoid it being used anywhere else
243#undef TMPL_NC_READ_WRITE_FILE_DUMMY
244
245#endif /* ENABLE_NETCDF */
The global header file for ARTS.
Numeric * get_c_array() const noexcept
Definition: matpackI.h:1157
Index nrows() const noexcept
Definition: matpackI.h:1079
Index ncols() const noexcept
Definition: matpackI.h:1080
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:145
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:148
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:151
Index ncols() const noexcept
Definition: matpackIV.h:146
Index nrows() const noexcept
Definition: matpackIV.h:145
Index nbooks() const noexcept
Definition: matpackIV.h:143
Index npages() const noexcept
Definition: matpackIV.h:144
Index nrows() const noexcept
Definition: matpackV.h:157
Index ncols() const noexcept
Definition: matpackV.h:158
Index npages() const noexcept
Definition: matpackV.h:156
Index nbooks() const noexcept
Definition: matpackV.h:155
Index nshelves() const noexcept
Definition: matpackV.h:154
The Matrix class.
Definition: matpackI.h:1285
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1011
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIII.cc:315
The Tensor3 class.
Definition: matpackIII.h:352
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:661
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIV.cc:342
The Tensor4 class.
Definition: matpackIV.h:435
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1054
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackV.cc:662
The Tensor5 class.
Definition: matpackV.h:524
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:1741
The Vector class.
Definition: matpackI.h:910
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Index nca_get_dim(const int ncid, const String &name, const bool noerror)
Read a dimension from NetCDF file.
Definition: nc_io.cc:296
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:643
void nca_get_data(const int ncid, const String &name, int *data)
Read variable of type int from NetCDF file.
Definition: nc_io.cc:323
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