ARTS  2.0.49
nc_io_array_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 //=== ArrayOfMatrix ==========================================================
39 
41 
45 void
46 nc_read_from_file (const int ncid,
47  ArrayOfMatrix& aom)
48 {
49  Index nelem;
50  nelem = nc_get_dim (ncid, "nelem");
51 
52  long *vnrows = new long[nelem];
53  long *vncols = new long[nelem];
54  aom.resize (nelem);
55  nc_get_data_long (ncid, "Matrix_nrows", vnrows);
56  nc_get_data_long (ncid, "Matrix_ncols", vncols);
57  size_t pos = 0;
58  for (Index i = 0; i < nelem; i++)
59  {
60  aom[i].resize (vnrows[i], vncols[i]);
61  nc_get_dataa_double (ncid, "ArrayOfMatrix", pos, vnrows[i] * vncols[i],
62  aom[i].get_c_array());
63  pos += vnrows[i] * vncols[i];
64  }
65 
66  delete [] vnrows;
67  delete [] vncols;
68 }
69 
70 
72 
76 void
77 nc_write_to_file (const int ncid,
78  const ArrayOfMatrix& aom)
79 {
80  int retval;
81  int ncdim, varid_nrows, varid_ncols;
82  int ncdim_total, varid;
83  long nelem_total = 0;
84  long *vncols = new long[aom.nelem()];
85  long *vnrows = new long[aom.nelem()];
86  for (Index i = 0; i < aom.nelem(); i++)
87  {
88  vnrows[i] = aom[i].nrows();
89  vncols[i] = aom[i].ncols();
90  nelem_total += vnrows[i] * vncols[i];
91  }
92 
93  if ((retval = nc_def_dim (ncid, "nelem", aom.nelem(), &ncdim)))
94  ncerror (retval, "nc_def_dim");
95  if ((retval = nc_def_dim (ncid, "nelem_total", nelem_total, &ncdim_total)))
96  ncerror (retval, "nc_def_dim");
97 
98  if ((retval = nc_def_var (ncid, "Matrix_nrows", NC_LONG, 1,
99  &ncdim, &varid_nrows)))
100  ncerror (retval, "nc_def_var");
101  if ((retval = nc_def_var (ncid, "Matrix_ncols", NC_LONG, 1,
102  &ncdim, &varid_ncols)))
103  ncerror (retval, "nc_def_var");
104  if ((retval = nc_def_var (ncid, "ArrayOfMatrix", NC_DOUBLE, 1,
105  &ncdim_total, &varid)))
106  ncerror (retval, "nc_def_var");
107 
108  if ((retval = nc_enddef (ncid))) ncerror (retval, "nc_enddef");
109 
110  if ((retval = nc_put_var_long (ncid, varid_nrows, vnrows)))
111  ncerror (retval, "nc_put_var");
112  if ((retval = nc_put_var_long (ncid, varid_ncols, vncols)))
113  ncerror (retval, "nc_put_var");
114 
115  size_t pos = 0;
116  for (Index i = 0; i < aom.nelem(); i++)
117  {
118  size_t count = aom[i].nrows() * aom[i].ncols();
119  if ((retval = nc_put_vara_double (ncid, varid, &pos, &count,
120  aom[i].get_c_array())))
121  ncerror (retval, "nc_put_var");
122  pos += count;
123  }
124 
125  delete [] vnrows;
126  delete [] vncols;
127 }
128 
129 
130 //=== ArrayOfVector ==========================================================
131 
133 
137 void
138 nc_read_from_file (const int ncid,
139  ArrayOfVector& aov)
140 {
141  Index nelem;
142  nelem = nc_get_dim (ncid, "nelem");
143 
144  long *vnelem = new long[nelem];
145  aov.resize (nelem);
146  nc_get_data_long (ncid, "Vector_nelem", vnelem);
147  size_t pos = 0;
148  for (Index i = 0; i < nelem; i++)
149  {
150  aov[i].resize (vnelem[i]);
151  nc_get_dataa_double (ncid, "ArrayOfVector", pos, vnelem[i],
152  aov[i].get_c_array());
153  pos += vnelem[i];
154  }
155 
156  delete [] vnelem;
157 }
158 
159 
161 
165 void
166 nc_write_to_file (const int ncid,
167  const ArrayOfVector& aov)
168 {
169  int retval;
170  int ncdim, varid_nelem;
171  int ncdim_total, varid;
172  long nelem_total = 0;
173  long *velems = new long[aov.nelem()];
174  for (Index i = 0; i < aov.nelem(); i++)
175  {
176  velems[i] = aov[i].nelem();
177  nelem_total += velems[i];
178  }
179 
180  if ((retval = nc_def_dim (ncid, "nelem", aov.nelem(), &ncdim)))
181  ncerror (retval, "nc_def_dim");
182  if ((retval = nc_def_dim (ncid, "nelem_total", nelem_total, &ncdim_total)))
183  ncerror (retval, "nc_def_dim");
184 
185  if ((retval = nc_def_var (ncid, "Vector_nelem", NC_LONG, 1,
186  &ncdim, &varid_nelem)))
187  ncerror (retval, "nc_def_var");
188  if ((retval = nc_def_var (ncid, "ArrayOfVector", NC_DOUBLE, 1,
189  &ncdim_total, &varid)))
190  ncerror (retval, "nc_def_var");
191 
192  if ((retval = nc_enddef (ncid))) ncerror (retval, "nc_enddef");
193 
194  if ((retval = nc_put_var_long (ncid, varid_nelem, velems)))
195  ncerror (retval, "nc_put_var");
196 
197  size_t pos = 0;
198  for (Index i = 0; i < aov.nelem(); i++)
199  {
200  size_t count = aov[i].nelem();
201  if ((retval = nc_put_vara_double (ncid, varid, &pos, &count,
202  aov[i].get_c_array())))
203  ncerror (retval, "nc_put_var");
204  pos += count;
205  }
206 
207  delete [] velems;
208 }
209 
210 
212 // Dummy funtion for groups for which
213 // IO function have not yet been implemented
215 
216 #define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
217  void nc_write_to_file (const int, const what&) \
218  { \
219  throw runtime_error ("NetCDF support not yet implemented for this type!"); \
220  } \
221  void nc_read_from_file (const int, what&) \
222  { \
223  throw runtime_error ("NetCDF support not yet implemented for this type!"); \
224  }
225 
226 //=== Array Types ==========================================================
227 
228 TMPL_NC_READ_WRITE_FILE_DUMMY( Array<IsotopeRecord> )
229 TMPL_NC_READ_WRITE_FILE_DUMMY( Array<SpeciesRecord> )
230 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfArrayOfArrayOfGridPos )
231 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfGriddedField1 )
232 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfGriddedField3 )
233 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfGridPos )
234 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfArrayOfGridPos )
235 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfIndex )
236 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfLineRecord )
237 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfMatrix )
238 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfSpeciesTag )
239 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfTensor3 )
240 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfArrayOfTensor6 )
241 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfGriddedField1 )
242 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfGriddedField2 )
243 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfGriddedField3 )
244 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfGriddedField4 )
245 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfGridPos )
246 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfIndex )
247 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfLineRecord )
248 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfLineshapeSpec )
249 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfPpath )
250 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfRetrievalQuantity )
251 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfSingleScatteringData )
252 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfSpeciesTag )
253 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfString )
254 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfSparse )
255 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfTensor3 )
256 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfTensor4 )
257 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfTensor6 )
258 TMPL_NC_READ_WRITE_FILE_DUMMY( ArrayOfTensor7 )
259 
260 //==========================================================================
261 
262 // Undefine the macro to avoid it being used anywhere else
263 #undef TMPL_NC_READ_WRITE_FILE_DUMMY
264 
265 #endif /* ENABLE_NETCDF */
266 
267 
nc_io.h
This file contains basic functions to handle NetCDF data files.
nc_io_types.h
This file contains private function declarations and template instantiation to handle NetCDF data fil...
Array
This can be used to make arrays out of anything.
Definition: array.h:103
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:172
arts.h
The global header file for ARTS.