ARTS  2.2.66
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 
18 
20 // File description
22 
31 #include "arts.h"
32 
33 #ifdef ENABLE_NETCDF
34 
35 #include "nc_io.h"
36 #include "nc_io_types.h"
37 
38 
39 //=== Matrix ==========================================================
40 
42 
46 void nca_read_from_file(const int ncid, Matrix& m, const Verbosity&)
47 {
48  Index nrows, ncols;
49  nrows = nc_get_dim(ncid, "nrows");
50  ncols = nc_get_dim(ncid, "ncols");
51 
52  m.resize(nrows, ncols);
53  nca_get_data_double(ncid, "Matrix", m.get_c_array());
54 }
55 
56 
58 
62 void nca_write_to_file(const int ncid, const Matrix& m, const Verbosity&)
63 {
64  int retval;
65  int ncdims[2], varid;
66  if ((retval = nc_def_dim(ncid, "nrows", m.nrows(), &ncdims[0])))
67  nca_error(retval, "nc_def_dim");
68  if ((retval = nc_def_dim(ncid, "ncols", m.ncols(), &ncdims[1])))
69  nca_error(retval, "nc_def_dim");
70  if ((retval = nc_def_var(ncid, "Matrix", NC_DOUBLE, 2, &ncdims[0], &varid)))
71  nca_error(retval, "nc_def_var");
72  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
73  if ((retval = nc_put_var_double(ncid, varid, m.get_c_array())))
74  nca_error(retval, "nc_put_var");
75 }
76 
77 
78 //=== Tensor3 ==========================================================
79 
81 
85 void nca_read_from_file(const int ncid, Tensor3& t, const Verbosity&)
86 {
87  Index npages, nrows, ncols;
88  npages = nc_get_dim(ncid, "npages");
89  nrows = nc_get_dim(ncid, "nrows");
90  ncols = nc_get_dim(ncid, "ncols");
91 
92  t.resize(npages, nrows, ncols);
93  nca_get_data_double(ncid, "Tensor3", t.get_c_array());
94 }
95 
96 
98 
102 void nca_write_to_file(const int ncid, const Tensor3& t, const Verbosity&)
103 {
104  int retval;
105  int ncdims[3], varid;
106  if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[0])))
107  nca_error(retval, "nc_def_dim");
108  if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[1])))
109  nca_error(retval, "nc_def_dim");
110  if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[2])))
111  nca_error(retval, "nc_def_dim");
112  if ((retval = nc_def_var(ncid, "Tensor3", NC_DOUBLE, 3, &ncdims[0], &varid)))
113  nca_error(retval, "nc_def_var");
114  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
115  if ((retval = nc_put_var_double(ncid, varid, t.get_c_array())))
116  nca_error(retval, "nc_put_var");
117 }
118 
119 
120 //=== Tensor4 ==========================================================
121 
123 
127 void nca_read_from_file(const int ncid, Tensor4& t, const Verbosity&)
128 {
129  Index nbooks, npages, nrows, ncols;
130  nbooks = nc_get_dim(ncid, "nbooks");
131  npages = nc_get_dim(ncid, "npages");
132  nrows = nc_get_dim(ncid, "nrows");
133  ncols = nc_get_dim(ncid, "ncols");
134 
135  t.resize(nbooks, npages, nrows, ncols);
136  nca_get_data_double(ncid, "Tensor4", t.get_c_array());
137 }
138 
139 
141 
145 void nca_write_to_file(const int ncid, const Tensor4& t, const Verbosity&)
146 {
147  int retval;
148  int ncdims[4], varid;
149  if ((retval = nc_def_dim(ncid, "nbooks", t.nbooks(), &ncdims[0])))
150  nca_error(retval, "nc_def_dim");
151  if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[1])))
152  nca_error(retval, "nc_def_dim");
153  if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[2])))
154  nca_error(retval, "nc_def_dim");
155  if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[3])))
156  nca_error(retval, "nc_def_dim");
157  if ((retval = nc_def_var(ncid, "Tensor4", NC_DOUBLE, 4, &ncdims[0], &varid)))
158  nca_error(retval, "nc_def_var");
159  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
160  if ((retval = nc_put_var_double(ncid, varid, t.get_c_array())))
161  nca_error(retval, "nc_put_var");
162 }
163 
164 
165 //=== Tensor5 ==========================================================
166 
168 
172 void nca_read_from_file(const int ncid, Tensor5& t, const Verbosity&)
173 {
174  Index nshelves, nbooks, npages, nrows, ncols;
175  nshelves = nc_get_dim(ncid, "nshelves");
176  nbooks = nc_get_dim(ncid, "nbooks");
177  npages = nc_get_dim(ncid, "npages");
178  nrows = nc_get_dim(ncid, "nrows");
179  ncols = nc_get_dim(ncid, "ncols");
180 
181  t.resize(nshelves, nbooks, npages, nrows, ncols);
182  nca_get_data_double(ncid, "Tensor5", t.get_c_array());
183 }
184 
185 
187 
191 void nca_write_to_file(const int ncid, const Tensor5& t, const Verbosity&)
192 {
193  int retval;
194  int ncdims[5], varid;
195  if ((retval = nc_def_dim(ncid, "nshelves", t.nshelves(), &ncdims[0])))
196  nca_error(retval, "nc_def_dim");
197  if ((retval = nc_def_dim(ncid, "nbooks", t.nbooks(), &ncdims[1])))
198  nca_error(retval, "nc_def_dim");
199  if ((retval = nc_def_dim(ncid, "npages", t.npages(), &ncdims[2])))
200  nca_error(retval, "nc_def_dim");
201  if ((retval = nc_def_dim(ncid, "nrows", t.nrows(), &ncdims[3])))
202  nca_error(retval, "nc_def_dim");
203  if ((retval = nc_def_dim(ncid, "ncols", t.ncols(), &ncdims[4])))
204  nca_error(retval, "nc_def_dim");
205  if ((retval = nc_def_var(ncid, "Tensor5", NC_DOUBLE, 5, &ncdims[0], &varid)))
206  nca_error(retval, "nc_def_var");
207  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
208  if ((retval = nc_put_var_double(ncid, varid, t.get_c_array())))
209  nca_error(retval, "nc_put_var");
210 }
211 
212 
213 //=== Vector ==========================================================
214 
216 
220 void nca_read_from_file(const int ncid, Vector& v, const Verbosity&)
221 {
222  Index nelem;
223  nelem = nc_get_dim(ncid, "nelem");
224 
225  v.resize(nelem);
226  nca_get_data_double(ncid, "Vector", v.get_c_array());
227 }
228 
229 
231 
235 void nca_write_to_file(const int ncid, const Vector& v, const Verbosity&)
236 {
237  int retval;
238  int ncdim, varid;
239  if ((retval = nc_def_dim(ncid, "nelem", v.nelem(), &ncdim)))
240  nca_error(retval, "nc_def_dim");
241  if ((retval = nc_def_var(ncid, "Vector", NC_DOUBLE, 1, &ncdim, &varid)))
242  nca_error(retval, "nc_def_var");
243  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
244  if ((retval = nc_put_var_double(ncid, varid, v.get_c_array())))
245  nca_error(retval, "nc_put_var");
246 }
247 
248 
250 // Dummy funtion for groups for which
251 // IO function have not yet been implemented
253 
254 #define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
255  void nca_write_to_file(const int, const what&, const Verbosity&) \
256  { \
257  throw runtime_error("NetCDF support not yet implemented for this type!"); \
258  } \
259  void nca_read_from_file(const int, what&, const Verbosity&) \
260  { \
261  throw runtime_error("NetCDF support not yet implemented for this type!"); \
262  }
263 
264 //==========================================================================
265 
266 // Undefine the macro to avoid it being used anywhere else
267 #undef TMPL_NC_READ_WRITE_FILE_DUMMY
268 
269 #endif /* ENABLE_NETCDF */
270 
Matrix
The Matrix class.
Definition: matpackI.h:788
Tensor4::resize
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1403
ConstTensor5View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:41
nca_get_data_double
void nca_get_data_double(const int ncid, const String &name, Numeric *data)
Read variable of type double from NetCDF file.
Definition: nc_io.cc:397
Tensor3
The Tensor3 class.
Definition: matpackIII.h:348
ConstTensor5View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:59
nc_get_dim
Index nc_get_dim(const int ncid, const String &name, const bool noerror)
Read a dimension from NetCDF file.
Definition: nc_io.cc:334
nc_io.h
This file contains basic functions to handle NetCDF data files.
Vector::resize
void resize(Index n)
Resize function.
Definition: matpackI.cc:798
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:832
nca_write_to_file
void nca_write_to_file(const int ncid, const Matrix &m, const Verbosity &)
Writes a Matrix to a NetCDF file.
Definition: nc_io_basic_types.cc:62
Tensor4
The Tensor4 class.
Definition: matpackIV.h:383
nc_io_types.h
This file contains private function declarations and template instantiation to handle NetCDF data fil...
Tensor3::resize
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:862
ConstTensor5View::npages
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:47
ConstTensor3View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:143
Tensor5::resize
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:2430
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:838
Tensor5View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackV.cc:778
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:180
ConstTensor4View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:81
nca_read_from_file
void nca_read_from_file(const int ncid, Matrix &m, const Verbosity &)
Reads a Matrix from a NetCDF file.
Definition: nc_io_basic_types.cc:46
Verbosity
Definition: messages.h:50
ConstTensor4View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:69
ConstTensor4View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:63
Matrix::resize
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1580
ConstTensor3View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:146
Tensor5
The Tensor5 class.
Definition: matpackV.h:451
ConstTensor4View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:75
ConstTensor5View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:53
ConstTensor5View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:35
ConstTensor3View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:149
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
MatrixView::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackI.cc:1214
Tensor3View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIII.cc:455
Vector
The Vector class.
Definition: matpackI.h:556
nca_error
void nca_error(const int e, const String s)
Throws a runtime error for the given NetCDF error code.
Definition: nc_io.cc:671
Tensor4View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIV.cc:402
VectorView::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackI.cc:542
arts.h
The global header file for ARTS.