ARTS 2.5.4 (git: 31ce4f0e)
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
49void 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
72void 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 */
The global header file for ARTS.
The Agenda class.
Definition: agenda_class.h:69
Index nelem() const ARTS_NOEXCEPT
Definition: array.h:92
An absorption lookup table.
Vector p_grid
The pressure grid for the table [Pa].
Tensor4 xsec
Absorption cross sections.
Vector t_pert
The vector of temperature perturbations [K].
Vector f_grid
The frequency grid [Hz].
Matrix vmrs_ref
The reference VMR profiles.
Vector nls_pert
The vector of perturbations for the VMRs of the nonlinear species.
Vector t_ref
The reference temperature profile [K].
ArrayOfIndex nonlinear_species
The species tags with non-linear treatment.
ArrayOfArrayOfSpeciesTag species
The species tags for which the table is valid.
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
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
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
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
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
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
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
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
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
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
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
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
void nca_def_dim(const int ncid, const String &name, const Index nelem, int *ncdim)
Define NetCDF dimension.
Definition: nc_io.cc:174
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
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
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
void nca_error(const int e, const String s)
Throws a runtime error for the given NetCDF error code.
Definition: nc_io.cc:622
This file contains basic functions to handle NetCDF data files.
void nca_read_from_file(const int ncid, GasAbsLookup &gal, const Verbosity &)
Reads a GasAbsLookup table from a NetCDF file.
#define TMPL_NC_READ_WRITE_FILE_DUMMY(what)
void nca_write_to_file(const int ncid, const GasAbsLookup &gal, const Verbosity &)
Writes a GasAbsLookup table to a NetCDF file.
This file contains private function declarations and template instantiation to handle NetCDF data fil...