ARTS  2.3.1285(git:92a29ea9-dirty)
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 
44 void nca_read_from_file(const int ncid, Matrix& m, const Verbosity&) {
45  Index nrows, ncols;
46  nrows = nc_get_dim(ncid, "nrows");
47  ncols = nc_get_dim(ncid, "ncols");
48 
49  m.resize(nrows, ncols);
50  nca_get_data_double(ncid, "Matrix", m.get_c_array());
51 }
52 
54 
58 void 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 
79 void nca_read_from_file(const int ncid, Tensor3& t, const Verbosity&) {
80  Index npages, nrows, ncols;
81  npages = nc_get_dim(ncid, "npages");
82  nrows = nc_get_dim(ncid, "nrows");
83  ncols = nc_get_dim(ncid, "ncols");
84 
85  t.resize(npages, nrows, ncols);
86  nca_get_data_double(ncid, "Tensor3", t.get_c_array());
87 }
88 
90 
94 void 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 
117 void nca_read_from_file(const int ncid, Tensor4& t, const Verbosity&) {
118  Index nbooks, npages, nrows, ncols;
119  nbooks = nc_get_dim(ncid, "nbooks");
120  npages = nc_get_dim(ncid, "npages");
121  nrows = nc_get_dim(ncid, "nrows");
122  ncols = nc_get_dim(ncid, "ncols");
123 
124  t.resize(nbooks, npages, nrows, ncols);
125  nca_get_data_double(ncid, "Tensor4", t.get_c_array());
126 }
127 
129 
133 void 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 
158 void nca_read_from_file(const int ncid, Tensor5& t, const Verbosity&) {
159  Index nshelves, nbooks, npages, nrows, ncols;
160  nshelves = nc_get_dim(ncid, "nshelves");
161  nbooks = nc_get_dim(ncid, "nbooks");
162  npages = nc_get_dim(ncid, "npages");
163  nrows = nc_get_dim(ncid, "nrows");
164  ncols = nc_get_dim(ncid, "ncols");
165 
166  t.resize(nshelves, nbooks, npages, nrows, ncols);
167  nca_get_data_double(ncid, "Tensor5", t.get_c_array());
168 }
169 
171 
175 void 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 
202 void nca_read_from_file(const int ncid, Vector& v, const Verbosity&) {
203  Index nelem;
204  nelem = nc_get_dim(ncid, "nelem");
205 
206  v.resize(nelem);
207  nca_get_data_double(ncid, "Vector", v.get_c_array());
208 }
209 
211 
215 void 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  throw runtime_error("NetCDF support not yet implemented for this type!"); \
235  } \
236  void nca_read_from_file(const int, what&, const Verbosity&) { \
237  throw runtime_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 */
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:50
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
void nca_write_to_file(const int ncid, const Matrix &m, const Verbosity &)
Writes a Matrix to a NetCDF file.
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:53
This file contains basic functions to handle NetCDF data files.
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:1743
void nca_read_from_file(const int ncid, Matrix &m, const Verbosity &)
Reads a Matrix from a NetCDF file.
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackI.cc:735
The Vector class.
Definition: matpackI.h:860
void nca_error(const int e, const String s)
Throws a runtime error for the given NetCDF error code.
Definition: nc_io.cc:622
The Tensor4 class.
Definition: matpackIV.h:421
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:60
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:47
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:147
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:63
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:432
const Numeric * get_c_array() const
Conversion to plain C-array, const-version.
Definition: matpackI.cc:272
The Tensor3 class.
Definition: matpackIII.h:339
The global header file for ARTS.
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:44
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:150
This file contains private function declarations and template instantiation to handle NetCDF data fil...
The Matrix class.
Definition: matpackI.h:1193
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackV.cc:675
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:664
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:144
void resize(Index n)
Resize function.
Definition: matpackI.cc:404
Index nelem(const Lines &l)
Number of lines.
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:57
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:359
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:56
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIII.cc:321
Index nc_get_dim(const int ncid, const String &name, const bool noerror)
Read a dimension from NetCDF file.
Definition: nc_io.cc:300
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:66
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:429
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1064
The Tensor5 class.
Definition: matpackV.h:506
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1056
const Numeric * get_c_array() const
Conversion to plain C-array.
Definition: matpackIV.cc:358