ARTS  2.2.66
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 
18 
20 // File description
22 
31 #include "config_global.h"
32 
33 #ifdef ENABLE_NETCDF
34 
35 #include <cstring>
36 
37 #include "arts.h"
38 #include "nc_io.h"
39 #include "nc_io_types.h"
40 
41 
42 //=== GasAbsLookup ==========================================================
43 
45 
51 void nca_read_from_file(const int ncid, GasAbsLookup& gal, const Verbosity&)
52 {
53  nca_get_data_ArrayOfArrayOfSpeciesTag(ncid, "species", gal.species, true);
54  if (!gal.species.nelem()) throw runtime_error("No species found in lookup table file!");
55 
56  nca_get_data_ArrayOfIndex(ncid, "nonlinear_species", gal.nonlinear_species, true);
57  nca_get_data_Vector(ncid, "f_grid", gal.f_grid, true);
58  nca_get_data_Vector(ncid, "p_grid", gal.p_grid, true);
59  nca_get_data_Matrix(ncid, "vmrs_ref", gal.vmrs_ref, true);
60  nca_get_data_Vector(ncid, "t_ref", gal.t_ref, true);
61  nca_get_data_Vector(ncid, "t_pert", gal.t_pert, true);
62  nca_get_data_Vector(ncid, "nls_pert", gal.nls_pert, true);
63  nca_get_data_Tensor4(ncid, "xsec", gal.xsec, true);
64 }
65 
66 
68 
74 void nca_write_to_file(const int ncid, const GasAbsLookup& gal, const Verbosity&)
75 {
76  int retval;
77 
78  int species_strings_varid;
79  int species_count_varid;
80 
81  ArrayOfIndex species_count(gal.species.nelem());
82  Index species_max_strlen = 0;
83  char* species_strings = NULL;
84 
85  if (gal.species.nelem())
86  {
87  long species_total_nelems = 0;
88  for (Index nspecies = 0; nspecies < gal.species.nelem(); nspecies++)
89  {
90  Index nspecies_nelem = gal.species[nspecies].nelem();
91  species_total_nelems += nspecies_nelem;
92  species_count[nspecies] = nspecies_nelem;
93 
94  for (ArrayOfSpeciesTag::const_iterator it = gal.species[nspecies].begin();
95  it != gal.species[nspecies].end(); it++)
96  if (it->Name().nelem() > species_max_strlen) species_max_strlen = it->Name().nelem();
97  }
98  species_max_strlen++;
99 
100  species_strings = new char[species_total_nelems*species_max_strlen];
101  memset(species_strings, 0, species_total_nelems*species_max_strlen);
102 
103  Index str_i = 0;
104  for (ArrayOfArrayOfSpeciesTag::const_iterator it1 = gal.species.begin();
105  it1 != gal.species.end(); it1++)
106  for (ArrayOfSpeciesTag::const_iterator it2 = it1->begin();
107  it2 != it1->end(); it2++)
108  {
109  memccpy(&species_strings[str_i], it2->Name().c_str(), 0, species_max_strlen);
110  str_i += species_max_strlen;
111  }
112 
113  species_count_varid = nca_def_ArrayOfIndex(ncid, "species_count", species_count);
114 
115  int species_strings_ncdims[2];
116  nca_def_dim(ncid, "species_strings_nelem", species_total_nelems, &species_strings_ncdims[0]);
117  nca_def_dim(ncid, "species_strings_length", species_max_strlen, &species_strings_ncdims[1]);
118  nca_def_var(ncid, "species_strings", NC_CHAR, 2, &species_strings_ncdims[0],
119  &species_strings_varid);
120  }
121  else {
122  throw runtime_error("Current lookup table contains no species!");
123  }
124 
125  // Define dimensions and variables
126  int nonlinear_species_varid = nca_def_ArrayOfIndex(ncid, "nonlinear_species",
127  gal.nonlinear_species);
128  int f_grid_varid = nca_def_Vector(ncid, "f_grid", gal.f_grid);
129  int p_grid_varid = nca_def_Vector(ncid, "p_grid", gal.p_grid);
130  int vmrs_ref_varid = nca_def_Matrix(ncid, "vmrs_ref", gal.vmrs_ref);
131  int t_ref_varid = nca_def_Vector(ncid, "t_ref", gal.t_ref);
132  int t_pert_varid = nca_def_Vector(ncid, "t_pert", gal.t_pert);
133  int nls_pert_varid = nca_def_Vector(ncid, "nls_pert", gal.nls_pert);
134  int xsec_varid = nca_def_Tensor4(ncid, "xsec", gal.xsec);
135 
136  if ((retval = nc_enddef(ncid))) nca_error(retval, "nc_enddef");
137 
138  // Write variables
139  nca_put_var_ArrayOfIndex(ncid, species_count_varid, species_count);
140  if (gal.species.nelem())
141  {
142  if ((retval = nc_put_var_text(ncid, species_strings_varid, species_strings)))
143  nca_error(retval, "nc_put_var");
144  }
145 
146  delete[] species_strings;
147 
148  nca_put_var_ArrayOfIndex(ncid, nonlinear_species_varid, gal.nonlinear_species);
149  nca_put_var_Vector(ncid, f_grid_varid, gal.f_grid);
150  nca_put_var_Vector(ncid, p_grid_varid, gal.p_grid);
151  nca_put_var_Matrix(ncid, vmrs_ref_varid, gal.vmrs_ref);
152  nca_put_var_Vector(ncid, t_ref_varid, gal.t_ref);
153  nca_put_var_Vector(ncid, t_pert_varid, gal.t_pert);
154  nca_put_var_Vector(ncid, nls_pert_varid, gal.nls_pert);
155  nca_put_var_Tensor4(ncid, xsec_varid, gal.xsec);
156 }
157 
158 
160 // Dummy funtion for groups for which
161 // IO function have not yet been implemented
163 
164 #define TMPL_NC_READ_WRITE_FILE_DUMMY(what) \
165  void nca_write_to_file(const int, const what&, const Verbosity&) \
166  { \
167  throw runtime_error("NetCDF support not yet implemented for this type!"); \
168  } \
169  void nca_read_from_file(const int, what&, const Verbosity&) \
170  { \
171  throw runtime_error("NetCDF support not yet implemented for this type!"); \
172  }
173 
175 
176 //==========================================================================
177 
178 // Undefine the macro to avoid it being used anywhere else
179 #undef TMPL_NC_READ_WRITE_FILE_DUMMY
180 
181 #endif /* ENABLE_NETCDF */
182 
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:539
GasAbsLookup::nonlinear_species
ArrayOfIndex nonlinear_species
The species tags with non-linear treatment.
Definition: gas_abs_lookup.h:165
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:454
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:629
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:558
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:46
Array< Index >
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:578
config_global.h
TMPL_NC_READ_WRITE_FILE_DUMMY
#define TMPL_NC_READ_WRITE_FILE_DUMMY(what)
Definition: nc_io_compound_types.cc:164
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:51
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:258
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:307
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:651
GasAbsLookup::t_ref
Vector t_ref
The reference temperature profile [K].
Definition: gas_abs_lookup.h:206
GasAbsLookup::f_grid
Vector f_grid
The frequency grid [Hz].
Definition: gas_abs_lookup.h:169
GasAbsLookup::p_grid
Vector p_grid
The pressure grid for the table [Pa].
Definition: gas_abs_lookup.h:183
Verbosity
Definition: messages.h:50
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:282
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:234
GasAbsLookup::nls_pert
Vector nls_pert
The vector of perturbations for the VMRs of the nonlinear species.
Definition: gas_abs_lookup.h:235
GasAbsLookup::xsec
Tensor4 xsec
Absorption cross sections.
Definition: gas_abs_lookup.h:269
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:216
GasAbsLookup::species
ArrayOfArrayOfSpeciesTag species
The species tags for which the table is valid.
Definition: gas_abs_lookup.h:156
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:74
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:197
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
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:521
GasAbsLookup::vmrs_ref
Matrix vmrs_ref
The reference VMR profiles.
Definition: gas_abs_lookup.h:201
GasAbsLookup::t_pert
Vector t_pert
The vector of temperature perturbations [K].
Definition: gas_abs_lookup.h:220
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.
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:480
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:607