MeteoIODoc 20240919.fc1eb87b
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
27#include <iostream>
28
29namespace mio {
39 namespace codes {
41
42 // ------------------------- POINTER HANDLING -------------------------
44 void operator()(codes_handle *h) const { codes_handle_delete(h); }
45 };
46
47 struct IndexDeleter {
48 void operator()(codes_index *i) const { codes_index_delete(i); }
49 };
50
51 using CodesHandlePtr = std::unique_ptr<codes_handle, HandleDeleter>;
52 using CodesIndexPtr = std::unique_ptr<codes_index, IndexDeleter>;
53
54 CodesHandlePtr makeUnique(codes_handle *h);
55 CodesIndexPtr makeUnique(codes_index *i);
56
57 // ------------------------- FILE AND MESSAGE HANDLING -------------------------
58 CodesIndexPtr indexFile(const std::string &filename, const std::vector<std::string> &index_keys, bool verbose);
59
60 // ------------------------- GETTERS -------------------------
61 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);
62 std::vector<CodesHandlePtr> getMessages(const std::string &filename, ProductKind product = PRODUCT_GRIB);
63 std::vector<CodesHandlePtr> getMessages(FILE* in_file, ProductKind product = PRODUCT_GRIB);
65
66 Date getMessageDateGrib(CodesHandlePtr &h, const double &tz_in);
67 std::string getSubsetPrefix(const size_t &subsetNumber);
68 Date getMessageDateBUFR(CodesHandlePtr &h, const size_t& subsetNumber, const double &tz_in = 0);
69
70 std::map<std::string, double> getGridParameters(CodesHandlePtr &h_unique);
71 void getGriddedValues(CodesHandlePtr &h, std::vector<double> &values, std::map<std::string, double> &gridParams);
72 std::map<std::string, double> getGriddedValues(CodesHandlePtr &h, std::vector<double> &values);
73
74 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,
75 std::vector<double> &distances, std::vector<double> &values, std::vector<int> &indexes);
76
77 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, double &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
78 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, long &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
79 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, int &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
80 bool getParameter(CodesHandlePtr &h, const std::string &parameterName, std::string &param_value, const IOUtils::ThrowOptions& throwError = IOUtils::dothrow);
81 template <typename T> bool getParameter(CodesHandlePtr &h, const std::vector<std::string> &paramNames, T &param_value, const size_t& subset_number = -1);
82
83 // ------------------------- SETTERS -------------------------
84 void setMissingValue(CodesHandlePtr &message, double missingValue);
85
86 bool selectParameter(codes_index *raw, const std::string &param_key, const std::string &paramId);
87 bool selectParameter(codes_index *raw, const std::string &param_key, const double &paramId);
88 bool selectParameter(codes_index *raw, const std::string &param_key, const long &paramId);
89
90 // ------------------------- WRITE -------------------------
91 void writeToFile(CodesHandlePtr &h, const std::string &filename);
92 CodesHandlePtr createBUFRMessageFromSample(long num_subsets, const std::map<MeteoParam, size_t> &multi_param_occurences, const std::set<std::string> &available_params,
93 const std::vector<MeteoParam> &POSSIBLE_MULTIPLE_PARAMETERS, const bool &write_cryos_station=false, const long& num_cryo_heights=0);
94 void setTime(CodesHandlePtr& ibufr, const Date& date, const std::string& subset_prefix);
95 void setTypicalTime(CodesHandlePtr& ibufr, const Date& date);
96 bool setParameter(CodesHandlePtr& ibufr, const std::string& parameterName, const double& parameterValue);
97 bool setParameter(CodesHandlePtr& ibufr, const std::string& parameterName, const long& parameterValue);
98 bool setParameter(CodesHandlePtr &ibufr, const std::string &parameterName, const std::vector<long> &parameterValues);
99 bool setParameter(CodesHandlePtr& ibufr, const std::string& parameterName, const std::string& parameterValue);
101
102 // ------------------------- CONSTANTS -------------------------
103 extern const std::string BUFR_HEIGHT_KEY;
104 extern const std::map<std::string, std::string> BUFR_PARAMETER;
105 extern const std::map<std::string, std::string> BUFR_PARAMETER_ALT;
106 extern const std::vector<int> FLAG_TO_EPSG;
107 extern const long SNOW_SURFACE_QUALIFIER;
108 extern const std::map<std::string, long> GRIB_DEFAULT_PARAM_TABLE;
109 extern const std::map<std::string, std::string> GRIB_DEFAULT_LEVELTYPE_TABLE;
110 extern const std::map<std::string, long> GRIB_DEFAULT_LEVELNO_TABLE;
111
112 // ------------------------- TEMPLATE FUNCTIONS -------------------------
113 // definition of the template functions
114 // -1 to indicate that the subset number is not used
115 template <typename T> bool getParameter(CodesHandlePtr &h, const std::vector<std::string> &paramNames, T &param_value, const size_t &subset_number) {
116 std::string subset_prefix = getSubsetPrefix(subset_number);
117 T tmp = param_value;
118 for (const auto &paramName : paramNames) {
119 getParameter(h, subset_prefix + paramName, param_value, IOUtils::nothrow);
120 if (param_value != tmp) {
121 return true;
122 }
123 }
124 return false;
125 }
126
127 template <typename T>
128 std::vector<CodesHandlePtr> getMessages(CodesIndexPtr &index, const std::string &param_key, const T &paramID, const std::string &level_key, const std::string &levelType) {
129 codes_index *raw = index.get();
130 if (!selectParameter(raw, param_key, paramID)) {
131 return {};
132 };
133
134 CODES_CHECK(codes_index_select_string(raw, level_key.c_str(), levelType.c_str()), 0);
135
136 codes_handle *h = nullptr;
137 int ret;
138 std::vector<CodesHandlePtr> handles;
139 while ((h = codes_handle_new_from_index(raw, &ret)) != nullptr) {
140 if (!h)
141 throw IOException("Unable to create grib handle from index", AT);
142 if (ret != 0 && ret != CODES_END_OF_INDEX) {
143 throw IOException("Error reading message: Errno " + std::to_string(ret), AT);
144 }
145#ifdef DEBUG
146 size_t len = 500;
147 char name[len] = {'\0'};
148 std::cerr << "Found message ";
149 CODES_CHECK(codes_get_string(h, "name", name, &len), 0);
150 std::cerr << "With name " << name << "\n";
151 CODES_CHECK(codes_get_string(h, "shortName", name, &len), 0);
152 std::cerr << "With shortName " << name << "\n";
153 if (levelType != 1) {
154 long level;
155 CODES_CHECK(codes_get_long(h, "level", &level), 0);
156 std::cerr << "With level " << level << "\n";
157 }
158#endif
159 handles.push_back(makeUnique(h));
160 }
161 return handles;
162 }
163 };
164} // namespace mio
165#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:677
std::string getSubsetPrefix(const size_t &subsetNumber)
Definition: libcodes.cc:456
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:431
const long SNOW_SURFACE_QUALIFIER
Definition: libcodes.cc:92
const std::map< std::string, std::string > GRIB_DEFAULT_LEVELTYPE_TABLE
Definition: libcodes.cc:161
const std::map< std::string, std::string > BUFR_PARAMETER
Definition: libcodes.cc:57
void setTime(CodesHandlePtr &ibufr, const Date &date, const std::string &subset_prefix)
Definition: libcodes.cc:635
CodesHandlePtr makeUnique(codes_handle *h)
Definition: libcodes.cc:193
std::unique_ptr< codes_index, IndexDeleter > CodesIndexPtr
Definition: libcodes.h:52
bool selectParameter(codes_index *raw, const std::string &param_key, const std::string &paramId)
Definition: libcodes.cc:493
Date getMessageDateGrib(CodesHandlePtr &h, const double &tz_in)
Definition: libcodes.cc:349
void unpackMessage(CodesHandlePtr &m)
Definition: libcodes.cc:443
const std::string BUFR_HEIGHT_KEY
Definition: libcodes.cc:55
std::map< std::string, double > getGriddedValues(CodesHandlePtr &h, std::vector< double > &values)
Definition: libcodes.cc:406
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:604
bool setParameter(CodesHandlePtr &ibufr, const std::string &parameterName, const double &parameterValue)
Definition: libcodes.cc:657
std::map< std::string, double > getGridParameters(CodesHandlePtr &h_unique)
Definition: libcodes.cc:362
std::vector< CodesHandlePtr > getMessages(const std::string &filename, ProductKind product)
Definition: libcodes.cc:314
const std::map< std::string, std::string > BUFR_PARAMETER_ALT
Definition: libcodes.cc:86
void setMissingValue(CodesHandlePtr &message, double missingValue)
Definition: libcodes.cc:490
CodesIndexPtr indexFile(const std::string &filename, const std::vector< std::string > &index_keys, bool verbose)
Definition: libcodes.cc:206
bool getParameter(CodesHandlePtr &h, const std::string &parameterName, double &parameterValue, const IOUtils::ThrowOptions &throwError)
Definition: libcodes.cc:249
Date getMessageDateBUFR(CodesHandlePtr &h, const size_t &subsetNumber, const double &tz_in)
Definition: libcodes.cc:464
void setTypicalTime(CodesHandlePtr &ibufr, const Date &date)
Definition: libcodes.cc:646
std::unique_ptr< codes_handle, HandleDeleter > CodesHandlePtr
Definition: libcodes.h:51
const std::vector< int > FLAG_TO_EPSG
Definition: libcodes.cc:89
const std::map< std::string, long > GRIB_DEFAULT_LEVELNO_TABLE
Definition: libcodes.cc:188
const std::map< std::string, long > GRIB_DEFAULT_PARAM_TABLE
Definition: libcodes.cc:156
void writeToFile(CodesHandlePtr &h, const std::string &filename)
Definition: libcodes.cc:498
Definition: Config.cc:31
const std::vector< MeteoParam > POSSIBLE_MULTIPLE_PARAMETERS
Definition: BUFRIO.cc:178
Definition: libcodes.h:43
void operator()(codes_handle *h) const
Definition: libcodes.h:44
Definition: libcodes.h:47
void operator()(codes_index *i) const
Definition: libcodes.h:48