ARTS  2.0.49
nc_io_basic_types.cc
Go to the documentation of this file.
1 /* Copyright (C) 2003-2008 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
47 nc_read_from_file (const int ncid,
48  Matrix& m)
49 {
50  Index nrows, ncols;
51  nrows = nc_get_dim (ncid, "nrows");
52  ncols = nc_get_dim (ncid, "ncols");
53 
54  m.resize(nrows, ncols);
55  nc_get_data_double (ncid, "Matrix", m.get_c_array());
56 }
57 
58 
60 
64 void
65 nc_write_to_file (const int ncid,
66  const Matrix& m)
67 {
68  int retval;
69  int ncdims[2], varid;
70  if ((retval = nc_def_dim (ncid, "nrows", m.nrows(), &ncdims[0])))
71  ncerror (retval, "nc_def_dim");
72  if ((retval = nc_def_dim (ncid, "ncols", m.ncols(), &ncdims[1])))
73  ncerror (retval, "nc_def_dim");
74  if ((retval = nc_def_var (ncid, "Matrix", NC_DOUBLE, 2, &ncdims[0], &varid)))
75  ncerror (retval, "nc_def_var");
76  if ((retval = nc_enddef (ncid))) ncerror (retval, "nc_enddef");
77  if ((retval = nc_put_var_double (ncid, varid, m.get_c_array())))
78  ncerror (retval, "nc_put_var");
79 }
80 
81 //=== Tensor3 ==========================================================
82 
84 
88 void
89 nc_read_from_file (const int ncid,
90  Tensor3& t)
91 {
92  Index npages, nrows, ncols;
93  npages = nc_get_dim (ncid, "npages");
94  nrows = nc_get_dim (ncid, "nrows");
95  ncols = nc_get_dim (ncid, "ncols");
96 
97  t.resize(npages, nrows, ncols);
98  nc_get_data_double (ncid, "Tensor3", t.get_c_array());
99 }
100 
101 
103 
107 void
108 nc_write_to_file (const int ncid,
109  const Tensor3& t)
110 {
111  int retval;
112  int ncdims[3], varid;
113  if ((retval = nc_def_dim (ncid, "npages", t.npages(), &ncdims[0])))
114  ncerror (retval, "nc_def_dim");
115  if ((retval = nc_def_dim (ncid, "nrows", t.nrows(), &ncdims[1])))
116  ncerror (retval, "nc_def_dim");
117  if ((retval = nc_def_dim (ncid, "ncols", t.ncols(), &ncdims[2])))
118  ncerror (retval, "nc_def_dim");
119  if ((retval = nc_def_var (ncid, "Tensor3", NC_DOUBLE, 3, &ncdims[0], &varid)))
120  ncerror (retval, "nc_def_var");
121  if ((retval = nc_enddef (ncid))) ncerror (retval, "nc_enddef");
122  if ((retval = nc_put_var_double (ncid, varid, t.get_c_array())))
123  ncerror (retval, "nc_put_var");
124 }
125 
126 //=== Tensor4 ==========================================================
127 
129 
133 void
134 nc_read_from_file (const int ncid,
135  Tensor4& t)
136 {
137  Index nbooks, npages, nrows, ncols;
138  nbooks = nc_get_dim (ncid, "nbooks");
139  npages = nc_get_dim (ncid, "npages");
140  nrows = nc_get_dim (ncid, "nrows");
141  ncols = nc_get_dim (ncid, "ncols");
142 
143  t.resize(nbooks, npages, nrows, ncols);
144  nc_get_data_double (ncid, "Tensor4", t.get_c_array());
145 }
146 
147 
149 
153 void
154 nc_write_to_file (const int ncid,
155  const Tensor4& t)
156 {
157  int retval;
158  int ncdims[4], varid;
159  if ((retval = nc_def_dim (ncid, "nbooks", t.nbooks(), &ncdims[0])))
160  ncerror (retval, "nc_def_dim");
161  if ((retval = nc_def_dim (ncid, "npages", t.npages(), &ncdims[1])))
162  ncerror (retval, "nc_def_dim");
163  if ((retval = nc_def_dim (ncid, "nrows", t.nrows(), &ncdims[2])))
164  ncerror (retval, "nc_def_dim");
165  if ((retval = nc_def_dim (ncid, "ncols", t.ncols(), &ncdims[3])))
166  ncerror (retval, "nc_def_dim");
167  if ((retval = nc_def_var (ncid, "Tensor4", NC_DOUBLE, 4, &ncdims[0], &varid)))
168  ncerror (retval, "nc_def_var");
169  if ((retval = nc_enddef (ncid))) ncerror (retval, "nc_enddef");
170  if ((retval = nc_put_var_double (ncid, varid, t.get_c_array())))
171  ncerror (retval, "nc_put_var");
172 }
173 
174 //=== Tensor5 ==========================================================
175 
177 
181 void
182 nc_read_from_file (const int ncid,
183  Tensor5& t)
184 {
185  Index nshelves, nbooks, npages, nrows, ncols;
186  nshelves = nc_get_dim (ncid, "nshelves");
187  nbooks = nc_get_dim (ncid, "nbooks");
188  npages = nc_get_dim (ncid, "npages");
189  nrows = nc_get_dim (ncid, "nrows");
190  ncols = nc_get_dim (ncid, "ncols");
191 
192  t.resize(nshelves, nbooks, npages, nrows, ncols);
193  nc_get_data_double (ncid, "Tensor5", t.get_c_array());
194 }
195 
196 
198 
202 void
203 nc_write_to_file (const int ncid,
204  const Tensor5& t)
205 {
206  int retval;
207  int ncdims[5], varid;
208  if ((retval = nc_def_dim (ncid, "nshelves", t.nshelves(), &ncdims[0])))
209  ncerror (retval, "nc_def_dim");
210  if ((retval = nc_def_dim (ncid, "nbooks", t.nbooks(), &ncdims[1])))
211  ncerror (retval, "nc_def_dim");
212  if ((retval = nc_def_dim (ncid, "npages", t.npages(), &ncdims[2])))
213  ncerror (retval, "nc_def_dim");
214  if ((retval = nc_def_dim (ncid, "nrows", t.nrows(), &ncdims[3])))
215  ncerror (retval, "nc_def_dim");
216  if ((retval = nc_def_dim (ncid, "ncols", t.ncols(), &ncdims[4])))
217  ncerror (retval, "nc_def_dim");
218  if ((retval = nc_def_var (ncid, "Tensor5", NC_DOUBLE, 5, &ncdims[0], &varid)))
219  ncerror (retval, "nc_def_var");
220  if ((retval = nc_enddef (ncid))) ncerror (retval, "nc_enddef");
221  if ((retval = nc_put_var_double (ncid, varid, t.get_c_array())))
222  ncerror (retval, "nc_put_var");
223 }
224 
225 
226 //=== Vector ==========================================================
227 
229 
233 void
234 nc_read_from_file (const int ncid,
235  Vector& v)
236 {
237  Index nelem;
238  nelem = nc_get_dim (ncid, "nelem");
239 
240  v.resize(nelem);
241  nc_get_data_double (ncid, "Vector", v.get_c_array());
242 }
243 
244 
246 
250 void
251 nc_write_to_file (const int ncid,
252  const Vector& v)
253 {
254  int retval;
255  int ncdim, varid;
256  if ((retval = nc_def_dim (ncid, "nelem", v.nelem(), &ncdim)))
257  ncerror (retval, "nc_def_dim");
258  if ((retval = nc_def_var (ncid, "Vector", NC_DOUBLE, 1, &ncdim, &varid)))
259  ncerror (retval, "nc_def_var");
260  if ((retval = nc_enddef (ncid))) ncerror (retval, "nc_enddef");
261  if ((retval = nc_put_var_double (ncid, varid, v.get_c_array())))
262  ncerror (retval, "nc_put_var");
263 }
264 
266 // Dummy funtion for groups for which
267 // IO function have not yet been implemented
269 
270 #define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
271  void nc_write_to_file (const int, const what&) \
272  { \
273  throw runtime_error ("NetCDF support not yet implemented for this type!"); \
274  } \
275  void nc_read_from_file (const int, what&) \
276  { \
277  throw runtime_error ("NetCDF support not yet implemented for this type!"); \
278  }
279 
280 //=== Basic Types ==========================================================
281 
282 TMPL_NC_READ_WRITE_FILE_DUMMY( Index )
283 TMPL_NC_READ_WRITE_FILE_DUMMY( Numeric )
284 TMPL_NC_READ_WRITE_FILE_DUMMY( Sparse )
285 TMPL_NC_READ_WRITE_FILE_DUMMY( String )
286 TMPL_NC_READ_WRITE_FILE_DUMMY( Tensor6 )
287 TMPL_NC_READ_WRITE_FILE_DUMMY( Tensor7 )
288 TMPL_NC_READ_WRITE_FILE_DUMMY( Timer )
289 
290 //==========================================================================
291 
292 // Undefine the macro to avoid it being used anywhere else
293 #undef TMPL_NC_READ_WRITE_FILE_DUMMY
294 
295 #endif /* ENABLE_NETCDF */
296 
Matrix
The Matrix class.
Definition: matpackI.h:767
Timer
Definition: m_general.h:54
Tensor4::resize
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1404
ConstTensor5View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:38
Tensor3
The Tensor3 class.
Definition: matpackIII.h:340
ConstTensor5View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:56
nc_io.h
This file contains basic functions to handle NetCDF data files.
Sparse
The Sparse class.
Definition: matpackII.h:55
Vector::resize
void resize(Index n)
Resize function.
Definition: matpackI.cc:771
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:796
Tensor4
The Tensor4 class.
Definition: matpackIV.h:375
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:863
ConstTensor5View::npages
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:44
ConstTensor3View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:151
Tensor5::resize
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:2435
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:802
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:62
Tensor5View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackV.cc:775
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:175
ConstTensor4View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:78
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
ConstTensor4View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:66
ConstTensor4View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:60
Matrix::resize
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1549
ConstTensor3View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:154
Tensor5
The Tensor5 class.
Definition: matpackV.h:443
ConstTensor4View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:72
ConstTensor5View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:50
ConstTensor5View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:32
ConstTensor3View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:157
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Tensor6
The Tensor6 class.
Definition: matpackVI.h:937
MatrixView::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackI.cc:1178
Tensor3View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIII.cc:452
Vector
The Vector class.
Definition: matpackI.h:555
Tensor4View::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIV.cc:399
VectorView::get_c_array
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackI.cc:527
Tensor7
The Tensor7 class.
Definition: matpackVII.h:1912
arts.h
The global header file for ARTS.