ARTS  2.4.0(git:4fb77825)
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 
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 //=== ArrayOfMatrix ==========================================================
38 
40 
44 void nca_read_from_file(const int ncid, ArrayOfMatrix& aom, const Verbosity&) {
45  Index nelem;
46  nelem = nc_get_dim(ncid, "nelem");
47 
48  long* vnrows = new long[nelem];
49  long* vncols = new long[nelem];
50  aom.resize(nelem);
51  nca_get_data_long(ncid, "Matrix_nrows", vnrows);
52  nca_get_data_long(ncid, "Matrix_ncols", vncols);
53  size_t pos = 0;
54  for (Index i = 0; i < nelem; i++) {
55  aom[i].resize(vnrows[i], vncols[i]);
57  "ArrayOfMatrix",
58  pos,
59  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 
69 
73 void nca_write_to_file(const int ncid,
74  const ArrayOfMatrix& aom,
75  const Verbosity&) {
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  vnrows[i] = aom[i].nrows();
84  vncols[i] = aom[i].ncols();
85  nelem_total += vnrows[i] * vncols[i];
86  }
87 
88  if ((retval = nc_def_dim(ncid, "nelem", aom.nelem(), &ncdim)))
89  nca_error(retval, "nc_def_dim");
90  if ((retval = nc_def_dim(ncid, "nelem_total", nelem_total, &ncdim_total)))
91  nca_error(retval, "nc_def_dim");
92 
93  if ((retval =
94  nc_def_var(ncid, "Matrix_nrows", NC_LONG, 1, &ncdim, &varid_nrows)))
95  nca_error(retval, "nc_def_var");
96  if ((retval =
97  nc_def_var(ncid, "Matrix_ncols", NC_LONG, 1, &ncdim, &varid_ncols)))
98  nca_error(retval, "nc_def_var");
99  if ((retval = nc_def_var(
100  ncid, "ArrayOfMatrix", NC_DOUBLE, 1, &ncdim_total, &varid)))
101  nca_error(retval, "nc_def_var");
102 
103  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
104 
105  if ((retval = nc_put_var_long(ncid, varid_nrows, vnrows)))
106  nca_error(retval, "nc_put_var");
107  if ((retval = nc_put_var_long(ncid, varid_ncols, vncols)))
108  nca_error(retval, "nc_put_var");
109 
110  size_t pos = 0;
111  for (Index i = 0; i < aom.nelem(); i++) {
112  size_t count = aom[i].nrows() * aom[i].ncols();
113  if ((retval = nc_put_vara_double(
114  ncid, varid, &pos, &count, aom[i].get_c_array())))
115  nca_error(retval, "nc_put_var");
116  pos += count;
117  }
118 
119  delete[] vnrows;
120  delete[] vncols;
121 }
122 
123 //=== ArrayOfVector ==========================================================
124 
126 
130 void nca_read_from_file(const int ncid, ArrayOfVector& aov, const Verbosity&) {
131  Index nelem;
132  nelem = nc_get_dim(ncid, "nelem");
133 
134  long* vnelem = new long[nelem];
135  aov.resize(nelem);
136  nca_get_data_long(ncid, "Vector_nelem", vnelem);
137  size_t pos = 0;
138  for (Index i = 0; i < nelem; i++) {
139  aov[i].resize(vnelem[i]);
141  ncid, "ArrayOfVector", pos, vnelem[i], aov[i].get_c_array());
142  pos += vnelem[i];
143  }
144 
145  delete[] vnelem;
146 }
147 
149 
153 void nca_write_to_file(const int ncid,
154  const ArrayOfVector& aov,
155  const Verbosity&) {
156  int retval;
157  int ncdim, varid_nelem;
158  int ncdim_total, varid;
159  long nelem_total = 0;
160  long* velems = new long[aov.nelem()];
161  for (Index i = 0; i < aov.nelem(); i++) {
162  velems[i] = aov[i].nelem();
163  nelem_total += velems[i];
164  }
165 
166  if ((retval = nc_def_dim(ncid, "nelem", aov.nelem(), &ncdim)))
167  nca_error(retval, "nc_def_dim");
168  if ((retval = nc_def_dim(ncid, "nelem_total", nelem_total, &ncdim_total)))
169  nca_error(retval, "nc_def_dim");
170 
171  if ((retval =
172  nc_def_var(ncid, "Vector_nelem", NC_LONG, 1, &ncdim, &varid_nelem)))
173  nca_error(retval, "nc_def_var");
174  if ((retval = nc_def_var(
175  ncid, "ArrayOfVector", NC_DOUBLE, 1, &ncdim_total, &varid)))
176  nca_error(retval, "nc_def_var");
177 
178  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
179 
180  if ((retval = nc_put_var_long(ncid, varid_nelem, velems)))
181  nca_error(retval, "nc_put_var");
182 
183  size_t pos = 0;
184  for (Index i = 0; i < aov.nelem(); i++) {
185  size_t count = aov[i].nelem();
186  if ((retval = nc_put_vara_double(
187  ncid, varid, &pos, &count, aov[i].get_c_array())))
188  nca_error(retval, "nc_put_var");
189  pos += count;
190  }
191 
192  delete[] velems;
193 }
194 
196 // Dummy funtion for groups for which
197 // IO function have not yet been implemented
199 
200 #define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
201  void nca_write_to_file(const int, const what&, const Verbosity&) { \
202  throw runtime_error("NetCDF support not yet implemented for this type!"); \
203  } \
204  void nca_read_from_file(const int, what&, const Verbosity&) { \
205  throw runtime_error("NetCDF support not yet implemented for this type!"); \
206  }
207 
208 //==========================================================================
209 
210 // Undefine the macro to avoid it being used anywhere else
211 #undef TMPL_NC_READ_WRITE_FILE_DUMMY
212 
213 #endif /* ENABLE_NETCDF */
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:300
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:73
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:375
Array
This can be used to make arrays out of anything.
Definition: array.h:108
Absorption::nelem
Index nelem(const Lines &l)
Number of lines.
Definition: absorptionlines.h:1820
Verbosity
Definition: messages.h:49
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:343
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:44
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
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:622
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:195
arts.h
The global header file for ARTS.