MeteoIODoc 20241221.207bde49
libcodes.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2012 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef LIBCODES_H
20#define LIBCODES_H
21
22#include <meteoio/IOInterface.h>
23
24#include <eccodes.h>
25#include <vector>
26#include <memory>
27
28#include <iostream>
29
30namespace mio {
40 namespace codes {
42
43 // ------------------------- POINTER HANDLING -------------------------
45 void operator()(codes_handle *h) const { codes_handle_delete(h); }
46 };
47
48 struct IndexDeleter {
49 void operator()(codes_index *i) const { codes_index_delete(i); }
50 };
51
52 using CodesHandlePtr = std::unique_ptr<codes_handle, HandleDeleter>;
53 using CodesIndexPtr = std::unique_ptr<codes_index, IndexDeleter>;
54
55 CodesHandlePtr makeUnique(codes_handle *h);
56 CodesIndexPtr makeUnique(codes_index *i);
57
58 // ------------------------- FILE AND MESSAGE HANDLING -------------------------
59 CodesIndexPtr indexFile(const std::string &filename, const std::vector<std::string> &index_keys, bool verbose);
60
61 // ------------------------- GETTERS -------------------------
62 template <typename T> std::vector<CodesHandlePtr> getMessages(CodesIndexPtr &index, const std::string &param_key, const T &paramID, const std::string &level_key, const std::string &levelType);
63 std::vector<CodesHandlePtr> getMessages(const std::string &filename, ProductKind product = PRODUCT_GRIB);
64 std::vector<CodesHandlePtr> getMessages(FILE* in_file, ProductKind product = PRODUCT_GRIB);
66
67 Date getMessageDateGrib(CodesHandlePtr &h, const double &tz_in);
68 std::string getSubsetPrefix(const size_t &subsetNumber);
69 Date getMessageDateBUFR(CodesHandlePtr &h, const size_t& subsetNumber, const double &tz_in = 0);
70
71 std::map<std::string, double> getGridParameters(CodesHandlePtr &h_unique);
72 void getGriddedValues(CodesHandlePtr &h, std::vector<double> &values, std::map<std::string, double> &gridParams);
73 std::map<std::string, double> getGriddedValues(CodesHandlePtr &h, std::vector<double> &values);
74
75 void getNearestValues_grib(CodesHandlePtr &h, const std::vector<double> &in_lats, const std::vector<double> &in_lons, std::vector<double> &out_lats, std::vector<double> &out_lons,
76 std::vector<double> &distances, std::vector<double> &values, std::vector<int> &indexes);
77
78 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, double &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
79 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, long &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
80 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, int &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
81 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, std::string &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
82 template <typename T> bool getParameter(CodesHandlePtr &h, const std::vector<std::string> &paramNames, T &param_value, const size_t& subset_number = -1);
83
84 // ------------------------- SETTERS -------------------------
85 void setMissingValue(CodesHandlePtr &message, double missingValue);
86
87 bool selectParameter(codes_index *raw, const std::string &param_key, const std::string &paramId);
88 bool selectParameter(codes_index *raw, const std::string &param_key, const double &paramId);
89 bool selectParameter(codes_index *raw, const std::string &param_key, const long &paramId);
90
91 // ------------------------- WRITE -------------------------
92 void writeToFile(CodesHandlePtr &h, const std::string &filename);
93 CodesHandlePtr createBUFRMessageFromSample(long num_subsets, const std::map<MeteoParam, size_t> &multi_param_occurences, const std::set<std::string> &available_params,
94 const std::vector<MeteoParam> &POSSIBLE_MULTIPLE_PARAMETERS, const bool &write_cryos_station=false, const long& num_cryo_heights=0);
95 void setTime(CodesHandlePtr& ibufr, const Date& date, const std::string& subset_prefix);
96 void setTypicalTime(CodesHandlePtr& ibufr, const Date& date);
97 bool setParameter(CodesHandlePtr& ibufr, const std::string& parameterName, const double& parameterValue);
98 bool setParameter(CodesHandlePtr& ibufr, const std::string& parameterName, const long& parameterValue);
99 bool setParameter(CodesHandlePtr &ibufr, const std::string &parameterName, const std::vector<long> &parameterValues);
100 bool setParameter(CodesHandlePtr& ibufr, const std::string& parameterName, const std::string& parameterValue);
102
103 // ------------------------- CONSTANTS -------------------------
104 extern const std::string BUFR_HEIGHT_KEY;
105 extern const std::map<std::string, std::string> BUFR_PARAMETER;
106 extern const std::map<std::string, std::string> BUFR_PARAMETER_ALT;
107 extern const std::vector<int> FLAG_TO_EPSG;
108 extern const long SNOW_SURFACE_QUALIFIER;
109 extern const std::map<std::string, long> GRIB_DEFAULT_PARAM_TABLE;
110 extern const std::map<std::string, std::string> GRIB_DEFAULT_LEVELTYPE_TABLE;
111 extern const std::map<std::string, long> GRIB_DEFAULT_LEVELNO_TABLE;
112
113 // ------------------------- TEMPLATE FUNCTIONS -------------------------
114 // definition of the template functions
115 // -1 to indicate that the subset number is not used
116 template <typename T> bool getParameter(CodesHandlePtr &h, const std::vector<std::string> &paramNames, T &param_value, const size_t &subset_number) {
117 std::string subset_prefix = getSubsetPrefix(subset_number);
118 T tmp = param_value;
119 for (const auto &paramName : paramNames) {
120 getParameter(h, subset_prefix + paramName, param_value, IOUtils::nothrow);
121 if (param_value != tmp) {
122 return true;
123 }
124 }
125 return false;
126 }
127
128 template <typename T>
129 std::vector<CodesHandlePtr> getMessages(CodesIndexPtr &index, const std::string &param_key, const T &paramID, const std::string &level_key, const std::string &levelType) {
130 codes_index *raw = index.get();
131 if (!selectParameter(raw, param_key, paramID)) {
132 return {};
133 };
134
135 CODES_CHECK(codes_index_select_string(raw, level_key.c_str(), levelType.c_str()), 0);
136
137 codes_handle *h = nullptr;
138 int ret;
139 std::vector<CodesHandlePtr> handles;
140 while ((h = codes_handle_new_from_index(raw, &ret)) != nullptr) {
141 if (!h)
142 throw IOException("Unable to create grib handle from index", AT);
143 if (ret != 0 && ret != CODES_END_OF_INDEX) {
144 throw IOException("Error reading message: Errno " + std::to_string(ret), AT);
145 }
146#ifdef DEBUG
147 size_t len = 500;
148 char name[len] = {'\0'};
149 std::cerr << "Found message ";
150 CODES_CHECK(codes_get_string(h, "name", name, &len), 0);
151 std::cerr << "With name " << name << "\n";
152 CODES_CHECK(codes_get_string(h, "shortName", name, &len), 0);
153 std::cerr << "With shortName " << name << "\n";
154 if (levelType != 1) {
155 long level;
156 CODES_CHECK(codes_get_long(h, "level", &level), 0);
157 std::cerr << "With level " << level << "\n";
158 }
159#endif
160 handles.push_back(makeUnique(h));
161 }
162 return handles;
163 }
164 };
165} // namespace mio
166#endif // LIBCODES_H
#define AT
Definition: IOExceptions.h:28
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
The basic exception class adjusted for the needs of SLF software.
Definition: IOExceptions.h:40
Parameters
this enum provides indexed access to meteorological fields
Definition: MeteoData.h:145
This namespace handles all the low level manipulation of GRIB and BUFR files with ecCodes.
@ raw
Definition: IOUtils.h:55
ThrowOptions
Definition: IOUtils.h:74
@ nothrow
Definition: IOUtils.h:74
@ dothrow
Definition: IOUtils.h:74
void packMessage(CodesHandlePtr &m)
Definition: libcodes.cc:685
std::string getSubsetPrefix(const size_t &subsetNumber)
Definition: libcodes.cc:457
void getNearestValues_grib(CodesHandlePtr &h, const std::vector< double > &in_lats, const std::vector< double > &in_lons, std::vector< double > &out_lats, std::vector< double > &out_lons, std::vector< double > &distances, std::vector< double > &values, std::vector< int > &indexes)
Definition: libcodes.cc:432
const long SNOW_SURFACE_QUALIFIER
Definition: libcodes.cc:94
const std::map< std::string, std::string > GRIB_DEFAULT_LEVELTYPE_TABLE
Definition: libcodes.cc:162
const std::map< std::string, std::string > BUFR_PARAMETER
Definition: libcodes.cc:59
void setTime(CodesHandlePtr &ibufr, const Date &date, const std::string &subset_prefix)
Definition: libcodes.cc:637
CodesHandlePtr makeUnique(codes_handle *h)
Definition: libcodes.cc:194
std::unique_ptr< codes_index, IndexDeleter > CodesIndexPtr
Definition: libcodes.h:53
bool selectParameter(codes_index *raw, const std::string &param_key, const std::string &paramId)
Definition: libcodes.cc:494
Date getMessageDateGrib(CodesHandlePtr &h, const double &tz_in)
Definition: libcodes.cc:350
void unpackMessage(CodesHandlePtr &m)
Definition: libcodes.cc:444
const std::string BUFR_HEIGHT_KEY
Definition: libcodes.cc:57
std::map< std::string, double > getGriddedValues(CodesHandlePtr &h, std::vector< double > &values)
Definition: libcodes.cc:407
CodesHandlePtr createBUFRMessageFromSample(long num_subsets, const std::map< MeteoParam, size_t > &multi_param_occurences, const std::set< std::string > &available_params, const std::vector< MeteoParam > &POSSIBLE_MULTIPLE_PARAMETERS, const bool &write_cryos_station, const long &num_cryo_heights)
Definition: libcodes.cc:606
bool setParameter(CodesHandlePtr &ibufr, const std::string &parameterName, const double &parameterValue)
Definition: libcodes.cc:659
std::map< std::string, double > getGridParameters(CodesHandlePtr &h_unique)
Definition: libcodes.cc:363
std::vector< CodesHandlePtr > getMessages(const std::string &filename, ProductKind product)
Definition: libcodes.cc:315
const std::map< std::string, std::string > BUFR_PARAMETER_ALT
Definition: libcodes.cc:88
void setMissingValue(CodesHandlePtr &message, double missingValue)
Definition: libcodes.cc:491
CodesIndexPtr indexFile(const std::string &filename, const std::vector< std::string > &index_keys, bool verbose)
Definition: libcodes.cc:207
bool getParameter(CodesHandlePtr &h, const std::string &parameterName, double &parameterValue, const IOUtils::ThrowOptions &throwError)
Definition: libcodes.cc:250
Date getMessageDateBUFR(CodesHandlePtr &h, const size_t &subsetNumber, const double &tz_in)
Definition: libcodes.cc:465
void setTypicalTime(CodesHandlePtr &ibufr, const Date &date)
Definition: libcodes.cc:648
std::unique_ptr< codes_handle, HandleDeleter > CodesHandlePtr
Definition: libcodes.h:52
const std::vector< int > FLAG_TO_EPSG
Definition: libcodes.cc:91
const std::map< std::string, long > GRIB_DEFAULT_LEVELNO_TABLE
Definition: libcodes.cc:189
const std::map< std::string, long > GRIB_DEFAULT_PARAM_TABLE
Definition: libcodes.cc:157
void writeToFile(CodesHandlePtr &h, const std::string &filename)
Definition: libcodes.cc:499
Definition: Config.cc:31
const std::vector< MeteoParam > POSSIBLE_MULTIPLE_PARAMETERS
Definition: BUFRIO.cc:173
Definition: libcodes.h:44
void operator()(codes_handle *h) const
Definition: libcodes.h:45
Definition: libcodes.h:48
void operator()(codes_index *i) const
Definition: libcodes.h:49