ARTS  2.4.0(git:4fb77825)
nc_io_compound_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 "config.h"
31 
32 #ifdef ENABLE_NETCDF
33 
34 #include <cstring>
35 
36 #include "arts.h"
37 #include "nc_io.h"
38 #include "nc_io_types.h"
39 
40 //=== GasAbsLookup ==========================================================
41 
43 
49 void nca_read_from_file(const int ncid, GasAbsLookup& gal, const Verbosity&) {
50  nca_get_data_ArrayOfArrayOfSpeciesTag(ncid, "species", gal.species, true);
51  if (!gal.species.nelem())
52  throw runtime_error("No species found in lookup table file!");
53 
55  ncid, "nonlinear_species", gal.nonlinear_species, true);
56  nca_get_data_Vector(ncid, "f_grid", gal.f_grid, true);
57  nca_get_data_Vector(ncid, "p_grid", gal.p_grid, true);
58  nca_get_data_Matrix(ncid, "vmrs_ref", gal.vmrs_ref, true);
59  nca_get_data_Vector(ncid, "t_ref", gal.t_ref, true);
60  nca_get_data_Vector(ncid, "t_pert", gal.t_pert, true);
61  nca_get_data_Vector(ncid, "nls_pert", gal.nls_pert, true);
62  nca_get_data_Tensor4(ncid, "xsec", gal.xsec, true);
63 }
64 
66 
72 void nca_write_to_file(const int ncid,
73  const GasAbsLookup& gal,
74  const Verbosity&) {
75  int retval;
76 
77  int species_strings_varid;
78  int species_count_varid;
79 
80  ArrayOfIndex species_count(gal.species.nelem());
81  Index species_max_strlen = 0;
82  char* species_strings = NULL;
83 
84  if (gal.species.nelem()) {
85  long species_total_nelems = 0;
86  for (Index nspecies = 0; nspecies < gal.species.nelem(); nspecies++) {
87  Index nspecies_nelem = gal.species[nspecies].nelem();
88  species_total_nelems += nspecies_nelem;
89  species_count[nspecies] = nspecies_nelem;
90 
91  for (ArrayOfSpeciesTag::const_iterator it = gal.species[nspecies].begin();
92  it != gal.species[nspecies].end();
93  it++)
94  if (it->Name().nelem() > species_max_strlen)
95  species_max_strlen = it->Name().nelem();
96  }
97  species_max_strlen++;
98 
99  species_strings = new char[species_total_nelems * species_max_strlen];
100  memset(species_strings, 0, species_total_nelems * species_max_strlen);
101 
102  Index str_i = 0;
103  for (ArrayOfArrayOfSpeciesTag::const_iterator it1 = gal.species.begin();
104  it1 != gal.species.end();
105  it1++)
106  for (ArrayOfSpeciesTag::const_iterator it2 = it1->begin();
107  it2 != it1->end();
108  it2++) {
109  memccpy(&species_strings[str_i],
110  it2->Name().c_str(),
111  0,
112  species_max_strlen);
113  str_i += species_max_strlen;
114  }
115 
116  species_count_varid =
117  nca_def_ArrayOfIndex(ncid, "species_count", species_count);
118 
119  int species_strings_ncdims[2];
120  nca_def_dim(ncid,
121  "species_strings_nelem",
122  species_total_nelems,
123  &species_strings_ncdims[0]);
124  nca_def_dim(ncid,
125  "species_strings_length",
126  species_max_strlen,
127  &species_strings_ncdims[1]);
128  nca_def_var(ncid,
129  "species_strings",
130  NC_CHAR,
131  2,
132  &species_strings_ncdims[0],
133  &species_strings_varid);
134  } else {
135  throw runtime_error("Current lookup table contains no species!");
136  }
137 
138  // Define dimensions and variables
139  int nonlinear_species_varid =
140  nca_def_ArrayOfIndex(ncid, "nonlinear_species", gal.nonlinear_species);
141  int f_grid_varid = nca_def_Vector(ncid, "f_grid", gal.f_grid);
142  int p_grid_varid = nca_def_Vector(ncid, "p_grid", gal.p_grid);
143  int vmrs_ref_varid = nca_def_Matrix(ncid, "vmrs_ref", gal.vmrs_ref);
144  int t_ref_varid = nca_def_Vector(ncid, "t_ref", gal.t_ref);
145  int t_pert_varid = nca_def_Vector(ncid, "t_pert", gal.t_pert);
146  int nls_pert_varid = nca_def_Vector(ncid, "nls_pert", gal.nls_pert);
147  int xsec_varid = nca_def_Tensor4(ncid, "xsec", gal.xsec);
148 
149  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
150 
151  // Write variables
152  nca_put_var_ArrayOfIndex(ncid, species_count_varid, species_count);
153  if (gal.species.nelem()) {
154  if ((retval =
155  nc_put_var_text(ncid, species_strings_varid, species_strings)))
156  nca_error(retval, "nc_put_var");
157  }
158 
159  delete[] species_strings;
160 
162  ncid, nonlinear_species_varid, gal.nonlinear_species);
163  nca_put_var_Vector(ncid, f_grid_varid, gal.f_grid);
164  nca_put_var_Vector(ncid, p_grid_varid, gal.p_grid);
165  nca_put_var_Matrix(ncid, vmrs_ref_varid, gal.vmrs_ref);
166  nca_put_var_Vector(ncid, t_ref_varid, gal.t_ref);
167  nca_put_var_Vector(ncid, t_pert_varid, gal.t_pert);
168  nca_put_var_Vector(ncid, nls_pert_varid, gal.nls_pert);
169  nca_put_var_Tensor4(ncid, xsec_varid, gal.xsec);
170 }
171 
173 // Dummy funtion for groups for which
174 // IO function have not yet been implemented
176 
177 #define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
178  void nca_write_to_file(const int, const what&, const Verbosity&) { \
179  throw runtime_error("NetCDF support not yet implemented for this type!"); \
180  } \
181  void nca_read_from_file(const int, what&, const Verbosity&) { \
182  throw runtime_error("NetCDF support not yet implemented for this type!"); \
183  }
184 
186 
187 //==========================================================================
188 
189 // Undefine the macro to avoid it being used anywhere else
190 #undef TMPL_NC_READ_WRITE_FILE_DUMMY
191 
192 #endif /* ENABLE_NETCDF */
nca_get_data_Matrix
void nca_get_data_Matrix(const int ncid, const String &name, Matrix &m, const bool noerror)
Read variable of type Matrix from NetCDF file.
Definition: nc_io.cc:497
GasAbsLookup::nonlinear_species
ArrayOfIndex nonlinear_species
The species tags with non-linear treatment.
Definition: gas_abs_lookup.h:205
nca_get_data_ArrayOfIndex
void nca_get_data_ArrayOfIndex(const int ncid, const String &name, ArrayOfIndex &aoi, const bool noerror)
Read variable of type ArrayOfIndex from NetCDF file.
Definition: nc_io.cc:413
nc_io.h
This file contains basic functions to handle NetCDF data files.
nca_put_var_Matrix
bool nca_put_var_Matrix(const int ncid, const int varid, const Matrix &m)
Write variable of type Matrix to NetCDF file.
Definition: nc_io.cc:585
nca_get_data_Tensor4
void nca_get_data_Tensor4(const int ncid, const String &name, Tensor4 &t, const bool noerror)
Read variable of type Tensor4 from NetCDF file.
Definition: nc_io.cc:517
nc_io_types.h
This file contains private function declarations and template instantiation to handle NetCDF data fil...
Agenda
The Agenda class.
Definition: agenda_class.h:44
GasAbsLookup
An absorption lookup table.
Definition: gas_abs_lookup.h:45
Array
This can be used to make arrays out of anything.
Definition: array.h:108
nca_put_var_ArrayOfIndex
bool nca_put_var_ArrayOfIndex(const int ncid, const int varid, const ArrayOfIndex &a)
Write variable of type ArrayOfIndex to NetCDF file.
Definition: nc_io.cc:539
TMPL_NC_READ_WRITE_FILE_DUMMY
#define TMPL_NC_READ_WRITE_FILE_DUMMY(what)
Definition: nc_io_compound_types.cc:177
nca_read_from_file
void nca_read_from_file(const int ncid, GasAbsLookup &gal, const Verbosity &)
Reads a GasAbsLookup table from a NetCDF file.
Definition: nc_io_compound_types.cc:49
nca_def_Vector
int nca_def_Vector(const int ncid, const String &name, const Vector &v)
Define NetCDF dimensions and variable for a Vector.
Definition: nc_io.cc:236
nca_def_Tensor4
int nca_def_Tensor4(const int ncid, const String &name, const Tensor4 &t)
Define NetCDF dimensions and variable for a Tensor4.
Definition: nc_io.cc:277
nca_put_var_Tensor4
bool nca_put_var_Tensor4(const int ncid, const int varid, const Tensor4 &t)
Write variable of type Tensor4 to NetCDF file.
Definition: nc_io.cc:604
GasAbsLookup::t_ref
Vector t_ref
The reference temperature profile [K].
Definition: gas_abs_lookup.h:246
GasAbsLookup::f_grid
Vector f_grid
The frequency grid [Hz].
Definition: gas_abs_lookup.h:209
GasAbsLookup::p_grid
Vector p_grid
The pressure grid for the table [Pa].
Definition: gas_abs_lookup.h:223
Verbosity
Definition: messages.h:49
nca_def_Matrix
int nca_def_Matrix(const int ncid, const String &name, const Matrix &m)
Define NetCDF dimensions and variable for a Matrix.
Definition: nc_io.cc:256
nca_def_ArrayOfIndex
int nca_def_ArrayOfIndex(const int ncid, const String &name, const ArrayOfIndex &a)
Define NetCDF dimensions and variable for an ArrayOfIndex.
Definition: nc_io.cc:214
GasAbsLookup::nls_pert
Vector nls_pert
The vector of perturbations for the VMRs of the nonlinear species.
Definition: gas_abs_lookup.h:275
GasAbsLookup::xsec
Tensor4 xsec
Absorption cross sections.
Definition: gas_abs_lookup.h:309
nca_def_var
void nca_def_var(const int ncid, const String &name, const nc_type type, const int ndims, const int *dims, int *varid)
Define NetCDF variable.
Definition: nc_io.cc:194
GasAbsLookup::species
ArrayOfArrayOfSpeciesTag species
The species tags for which the table is valid.
Definition: gas_abs_lookup.h:196
nca_write_to_file
void nca_write_to_file(const int ncid, const GasAbsLookup &gal, const Verbosity &)
Writes a GasAbsLookup table to a NetCDF file.
Definition: nc_io_compound_types.cc:72
config.h
nca_def_dim
void nca_def_dim(const int ncid, const String &name, const Index nelem, int *ncdim)
Define NetCDF dimension.
Definition: nc_io.cc:174
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
nca_get_data_Vector
void nca_get_data_Vector(const int ncid, const String &name, Vector &v, const bool noerror)
Read variable of type Vector from NetCDF file.
Definition: nc_io.cc:478
GasAbsLookup::vmrs_ref
Matrix vmrs_ref
The reference VMR profiles.
Definition: gas_abs_lookup.h:241
GasAbsLookup::t_pert
Vector t_pert
The vector of temperature perturbations [K].
Definition: gas_abs_lookup.h:260
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.
nca_get_data_ArrayOfArrayOfSpeciesTag
void nca_get_data_ArrayOfArrayOfSpeciesTag(const int ncid, const String &name, ArrayOfArrayOfSpeciesTag &aast, const bool noerror)
Read variable of type ArrayOfArrayOfSpeciesTag from NetCDF file.
Definition: nc_io.cc:438
nca_put_var_Vector
bool nca_put_var_Vector(const int ncid, const int varid, const Vector &v)
Write variable of type Vector to NetCDF file.
Definition: nc_io.cc:566