ARTS  2.2.66
nc_io_array_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 //=== ArrayOfMatrix ==========================================================
39 
41 
45 void nca_read_from_file(const int ncid, ArrayOfMatrix& aom, const Verbosity&)
46 {
47  Index nelem;
48  nelem = nc_get_dim(ncid, "nelem");
49 
50  long* vnrows = new long[nelem];
51  long* vncols = new long[nelem];
52  aom.resize(nelem);
53  nca_get_data_long(ncid, "Matrix_nrows", vnrows);
54  nca_get_data_long(ncid, "Matrix_ncols", vncols);
55  size_t pos = 0;
56  for (Index i = 0; i < nelem; i++)
57  {
58  aom[i].resize(vnrows[i], vncols[i]);
59  nca_get_dataa_double(ncid, "ArrayOfMatrix", pos, vnrows[i] * vncols[i],
60  aom[i].get_c_array());
61  pos += vnrows[i] * vncols[i];
62  }
63 
64  delete[] vnrows;
65  delete[] vncols;
66 }
67 
68 
70 
74 void nca_write_to_file(const int ncid, const ArrayOfMatrix& aom, const Verbosity&)
75 {
76  int retval;
77  int ncdim, varid_nrows, varid_ncols;
78  int ncdim_total, varid;
79  long nelem_total = 0;
80  long* vncols = new long[aom.nelem()];
81  long* vnrows = new long[aom.nelem()];
82  for (Index i = 0; i < aom.nelem(); i++)
83  {
84  vnrows[i] = aom[i].nrows();
85  vncols[i] = aom[i].ncols();
86  nelem_total += vnrows[i] * vncols[i];
87  }
88 
89  if ((retval = nc_def_dim(ncid, "nelem", aom.nelem(), &ncdim)))
90  nca_error(retval, "nc_def_dim");
91  if ((retval = nc_def_dim(ncid, "nelem_total", nelem_total, &ncdim_total)))
92  nca_error(retval, "nc_def_dim");
93 
94  if ((retval = nc_def_var(ncid, "Matrix_nrows", NC_LONG, 1,
95  &ncdim, &varid_nrows)))
96  nca_error(retval, "nc_def_var");
97  if ((retval = nc_def_var(ncid, "Matrix_ncols", NC_LONG, 1,
98  &ncdim, &varid_ncols)))
99  nca_error(retval, "nc_def_var");
100  if ((retval = nc_def_var(ncid, "ArrayOfMatrix", NC_DOUBLE, 1,
101  &ncdim_total, &varid)))
102  nca_error(retval, "nc_def_var");
103 
104  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
105 
106  if ((retval = nc_put_var_long(ncid, varid_nrows, vnrows)))
107  nca_error(retval, "nc_put_var");
108  if ((retval = nc_put_var_long(ncid, varid_ncols, vncols)))
109  nca_error(retval, "nc_put_var");
110 
111  size_t pos = 0;
112  for (Index i = 0; i < aom.nelem(); i++)
113  {
114  size_t count = aom[i].nrows() * aom[i].ncols();
115  if ((retval = nc_put_vara_double(ncid, varid, &pos, &count,
116  aom[i].get_c_array())))
117  nca_error(retval, "nc_put_var");
118  pos += count;
119  }
120 
121  delete[] vnrows;
122  delete[] vncols;
123 }
124 
125 
126 //=== ArrayOfVector ==========================================================
127 
129 
133 void nca_read_from_file(const int ncid, ArrayOfVector& aov, const Verbosity&)
134 {
135  Index nelem;
136  nelem = nc_get_dim(ncid, "nelem");
137 
138  long* vnelem = new long[nelem];
139  aov.resize(nelem);
140  nca_get_data_long(ncid, "Vector_nelem", vnelem);
141  size_t pos = 0;
142  for (Index i = 0; i < nelem; i++)
143  {
144  aov[i].resize(vnelem[i]);
145  nca_get_dataa_double(ncid, "ArrayOfVector", pos, vnelem[i],
146  aov[i].get_c_array());
147  pos += vnelem[i];
148  }
149 
150  delete[] vnelem;
151 }
152 
153 
155 
159 void nca_write_to_file(const int ncid, const ArrayOfVector& aov, const Verbosity&)
160 {
161  int retval;
162  int ncdim, varid_nelem;
163  int ncdim_total, varid;
164  long nelem_total = 0;
165  long* velems = new long[aov.nelem()];
166  for (Index i = 0; i < aov.nelem(); i++)
167  {
168  velems[i] = aov[i].nelem();
169  nelem_total += velems[i];
170  }
171 
172  if ((retval = nc_def_dim(ncid, "nelem", aov.nelem(), &ncdim)))
173  nca_error(retval, "nc_def_dim");
174  if ((retval = nc_def_dim(ncid, "nelem_total", nelem_total, &ncdim_total)))
175  nca_error(retval, "nc_def_dim");
176 
177  if ((retval = nc_def_var(ncid, "Vector_nelem", NC_LONG, 1,
178  &ncdim, &varid_nelem)))
179  nca_error(retval, "nc_def_var");
180  if ((retval = nc_def_var(ncid, "ArrayOfVector", NC_DOUBLE, 1,
181  &ncdim_total, &varid)))
182  nca_error(retval, "nc_def_var");
183 
184  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
185 
186  if ((retval = nc_put_var_long(ncid, varid_nelem, velems)))
187  nca_error(retval, "nc_put_var");
188 
189  size_t pos = 0;
190  for (Index i = 0; i < aov.nelem(); i++)
191  {
192  size_t count = aov[i].nelem();
193  if ((retval = nc_put_vara_double(ncid, varid, &pos, &count,
194  aov[i].get_c_array())))
195  nca_error(retval, "nc_put_var");
196  pos += count;
197  }
198 
199  delete[] velems;
200 }
201 
202 
204 // Dummy funtion for groups for which
205 // IO function have not yet been implemented
207 
208 #define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
209  void nca_write_to_file(const int, const what&, const Verbosity&) \
210  { \
211  throw runtime_error("NetCDF support not yet implemented for this type!"); \
212  } \
213  void nca_read_from_file(const int, what&, const Verbosity&) \
214  { \
215  throw runtime_error("NetCDF support not yet implemented for this type!"); \
216  }
217 
218 //==========================================================================
219 
220 // Undefine the macro to avoid it being used anywhere else
221 #undef TMPL_NC_READ_WRITE_FILE_DUMMY
222 
223 #endif /* ENABLE_NETCDF */
224 
225 
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.
nca_write_to_file
void nca_write_to_file(const int ncid, const ArrayOfMatrix &aom, const Verbosity &)
Writes an ArrayOfMatrix to a NetCDF file.
Definition: nc_io_array_types.cc:74
nc_io_types.h
This file contains private function declarations and template instantiation to handle NetCDF data fil...
nca_get_dataa_double
void nca_get_dataa_double(const int ncid, const String &name, size_t start, size_t count, Numeric *data)
Read variable of type array of double from NetCDF file.
Definition: nc_io.cc:415
Array< Matrix >
Verbosity
Definition: messages.h:50
nca_get_data_long
void nca_get_data_long(const int ncid, const String &name, long *data)
Read variable of type long from NetCDF file.
Definition: nc_io.cc:379
nca_read_from_file
void nca_read_from_file(const int ncid, ArrayOfMatrix &aom, const Verbosity &)
Reads an ArrayOfMatrix from a NetCDF file.
Definition: nc_io_array_types.cc:45
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
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
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:176
arts.h
The global header file for ARTS.