IODA
DataFromSQL.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * (C) Copyright 2021 UCAR
4  *
5  * This software is licensed under the terms of the Apache Licence Version 2.0
6  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
7  */
8 /** @file DataFromSQL.h
9  * @brief implements ODC bindings
10 **/
11 
12 #include <cctype>
13 #include <iomanip>
14 #include <map>
15 #include <string>
16 #include <vector>
17 
18 #include "ioda/defs.h"
19 #include "ioda/ObsGroup.h"
20 #include "unsupported/Eigen/CXX11/Tensor"
21 
22 namespace ioda {
23 namespace Engines {
24 namespace ODC {
25 
26  // TODO(DJDavies2): Take these obsgroup and varno
27  // definitions and encapsulate these into the YAML
28  // file structure.
29  static constexpr int obsgroup_scatwind = 2;
30  static constexpr int obsgroup_aircraft = 4;
31  static constexpr int obsgroup_sonde = 5;
32  static constexpr int obsgroup_atovs = 7;
33  static constexpr int obsgroup_airs = 16;
34  static constexpr int obsgroup_gpsro = 18;
35  static constexpr int obsgroup_ssmis = 19;
36  static constexpr int obsgroup_iasi = 26;
37  static constexpr int obsgroup_seviriclr = 27;
38  static constexpr int obsgroup_geocloud = 28;
39  static constexpr int obsgroup_amsr = 29;
40  static constexpr int obsgroup_abiclr = 37;
41  static constexpr int obsgroup_atms = 38;
42  static constexpr int obsgroup_cris = 39;
43  static constexpr int obsgroup_surfacecloud = 42;
44  static constexpr int obsgroup_mwsfy3 = 44;
45  static constexpr int obsgroup_ahiclr = 51;
46  static constexpr int obsgroup_mwri = 55;
47  static constexpr int obsgroup_gmilow = 56;
48  static constexpr int obsgroup_gmihigh = 57;
49  static constexpr int obsgroup_hiras = 60;
50 
51  static constexpr int varno_dd = 111;
52  static constexpr int varno_rawbt = 119;
53  static constexpr int varno_bending_angle = 162;
54  static constexpr int varno_rawsca = 233;
55  static constexpr int varno_u_amb = 242;
56  static constexpr int varno_rawbt_hirs = 248;
57  static constexpr int varno_rawbt_amsu = 249;
58  static constexpr int varno_cloud_fraction_covered = 266;
59  static constexpr int varno_rawbt_amsr_89ghz = 267;
60  static constexpr int varno_rawbt_mwts = 274;
61  static constexpr int varno_rawbt_mwhs = 275;
62 
63  static constexpr int odb_type_int = 1;
64  static constexpr int odb_type_real = 2;
65  static constexpr int odb_type_string = 3;
66  static constexpr int odb_type_bitfield = 4;
67 
68  static constexpr float odb_missing_float = -2147483648.0f;
69  static constexpr int odb_missing_int = 2147483647;
70 
71 class DataFromSQL {
72 private:
73  std::vector<std::string> columns_;
74  std::vector<int> column_types_;
75  std::vector<int> varnos_;
76  std::vector<double> data_;
77  size_t number_of_rows_ = 0;
79  size_t number_of_varnos_ = 0;
80  int obsgroup_ = 0;
81  std::map<int, size_t> varnos_and_levels_;
82 
83  /// \brief Returns a count of the rows extracted by an sql
84  /// \param sql The SQL to check
85  static size_t countRows(const std::string& sql);
86 
87  /// \brief Returns the value for a particular row/column
88  /// \param row Get data for this row
89  /// \param column Get data for this column
90  double getData(size_t row, size_t column) const;
91 
92  /// \brief Returns the value for a particular row/column (as name)
93  /// \param row Get data for this row
94  /// \param column Get data for this column
95  double getData(size_t row, const std::string& column) const;
96 
97  /// \brief Set a value for a row,column element
98  /// \param row The row to set
99  /// \param column The column to set
100  /// \param val Set this value
101  void setData(size_t row, size_t column, double val);
102 
103  /// \brief Populate structure with data from an sql
104  /// \param sql The SQL string to generate the data for the structure
105  void setData(const std::string& sql);
106 
107  /// \brief Returns the number of rows for a particular varno
108  /// \param varno The varno to check
109  size_t numberOfRowsForVarno(int varno) const;
110 
111  /// \brief Returns true if a particular varno is present
112  /// \param varno The varno to check
113  bool hasVarno(int varno) const;
114 
115  /// \brief Returns the index of a specified column
116  /// \param column The column to check
117  int getColumnIndex(const std::string& column) const;
118 
119  /// \brief Returns the number of levels for each varno
120  size_t numberOfLevels(int varno) const;
121 
122  /// \brief Returns data for a (metadata) column
123  /// \param column Get data for this column
124  Eigen::ArrayXf getMetadataColumn(std::string const& column) const;
125 
126  /// \brief Returns data for a (metadata) column
127  /// \param column Get data for this column
128  Eigen::ArrayXi getMetadataColumnInt(std::string const& column) const;
129 
130  /// \brief Returns data for a (metadata) column
131  /// \param column Get data for this column
132  std::vector<std::string> getMetadataStringColumn(std::string const& column) const;
133 
134  /// \brief Returns data for a varno for a varno column
135  /// \param varno Get data for this varno
136  /// \param column Get data for this column
137  /// \param nchans Number of channels to store
138  /// \param nchans_actual Actual number of channels
139  Eigen::ArrayXf getVarnoColumn(const std::vector<int>& varnos, std::string const& column,
140  const int nchans, const int nchans_actual) const;
141 
142 public:
143  /// \brief Simple constructor
144  DataFromSQL();
145 
146  /// \brief Returns the number of "metadata" rows, i.e. hdr-type rows
147  size_t numberOfMetadataRows() const;
148 
149  /// \brief Returns the dimensions for the ODB
151 
152  /// \brief Populate structure with data from specified columns, file and varnos
153  /// \param columns List of columns to extract
154  /// \param filename Extract from this file
155  /// \param varnos List of varnos to extract
156  void select(const std::vector<std::string>& columns, const std::string& filename,
157  const std::vector<int>& varnos, const std::string& query);
158 
159  /// \brief Returns a vector of date strings
160  std::vector<std::string> getDates(std::string const& date_col, std::string const& time_col) const;
161 
162  /// \brief Returns a vector of columns
163  const std::vector<std::string>& getColumns() const;
164 
165  /// \brief Returns an ioda variable for a specified column
166  /// \param column Get data for this column
167  ioda::Variable getIodaVariable(std::string const& column, ioda::ObsGroup og,
169 
170  /// \brief Returns an ioda variable for a specified column
173 
174  /// \brief Returns the type of a specified column
175  /// \param column The column to check
176  int getColumnTypeByName(std::string const& column) const;
177 
178  /// \brief Returns the obsgroup number
179  int getObsgroup() const;
180 };
181 
182 } // namespace ODC
183 } // namespace Engines
184 } // namespace ioda
Interfaces for ioda::ObsGroup and related classes.
std::map< int, size_t > varnos_and_levels_
Definition: DataFromSQL.h:81
std::vector< std::string > getDates(std::string const &date_col, std::string const &time_col) const
Returns a vector of date strings.
NewDimensionScales_t getVertcos() const
Returns the dimensions for the ODB.
Definition: DataFromSQL.cpp:84
size_t numberOfRowsForVarno(int varno) const
Returns the number of rows for a particular varno.
Definition: DataFromSQL.cpp:35
int getObsgroup() const
Returns the obsgroup number.
Eigen::ArrayXf getMetadataColumn(std::string const &column) const
Returns data for a (metadata) column.
Eigen::ArrayXf getVarnoColumn(const std::vector< int > &varnos, std::string const &column, const int nchans, const int nchans_actual) const
Returns data for a varno for a varno column.
Eigen::ArrayXi getMetadataColumnInt(std::string const &column) const
Returns data for a (metadata) column.
bool hasVarno(int varno) const
Returns true if a particular varno is present.
Definition: DataFromSQL.cpp:47
size_t numberOfMetadataRows() const
Returns the number of "metadata" rows, i.e. hdr-type rows.
Definition: DataFromSQL.cpp:24
ioda::Variable getIodaObsvalue(int varno, ioda::ObsGroup og, ioda::VariableCreationParameters params) const
Returns an ioda variable for a specified column.
std::vector< double > data_
Definition: DataFromSQL.h:76
DataFromSQL()
Simple constructor.
Definition: DataFromSQL.cpp:22
void setData(size_t row, size_t column, double val)
Set a value for a row,column element.
Definition: DataFromSQL.cpp:61
std::vector< int > varnos_
Definition: DataFromSQL.h:75
std::vector< int > column_types_
Definition: DataFromSQL.h:74
ioda::Variable getIodaVariable(std::string const &column, ioda::ObsGroup og, ioda::VariableCreationParameters params) const
Returns an ioda variable for a specified column.
std::vector< std::string > getMetadataStringColumn(std::string const &column) const
Returns data for a (metadata) column.
int getColumnIndex(const std::string &column) const
Returns the index of a specified column.
Definition: DataFromSQL.cpp:26
size_t numberOfLevels(int varno) const
Returns the number of levels for each varno.
Definition: DataFromSQL.cpp:54
static size_t countRows(const std::string &sql)
Returns a count of the rows extracted by an sql.
double getData(size_t row, size_t column) const
Returns the value for a particular row/column.
const std::vector< std::string > & getColumns() const
Returns a vector of columns.
std::vector< std::string > columns_
Definition: DataFromSQL.h:73
int getColumnTypeByName(std::string const &column) const
Returns the type of a specified column.
Definition: DataFromSQL.cpp:80
void select(const std::vector< std::string > &columns, const std::string &filename, const std::vector< int > &varnos, const std::string &query)
Populate structure with data from specified columns, file and varnos.
An ObsGroup is a specialization of a ioda::Group. It provides convenience functions and guarantees th...
Definition: ObsGroup.h:32
Variables store data!
Definition: Variable.h:680
Common preprocessor definitions used throughout IODA.
static constexpr int obsgroup_ssmis
Definition: DataFromSQL.h:35
static constexpr int obsgroup_aircraft
Definition: DataFromSQL.h:30
static constexpr int obsgroup_gmilow
Definition: DataFromSQL.h:47
static constexpr int varno_rawsca
Definition: DataFromSQL.h:54
static constexpr int varno_bending_angle
Definition: DataFromSQL.h:53
static constexpr int odb_type_string
Definition: DataFromSQL.h:65
static constexpr int obsgroup_surfacecloud
Definition: DataFromSQL.h:43
static constexpr int obsgroup_abiclr
Definition: DataFromSQL.h:40
static constexpr int varno_rawbt_hirs
Definition: DataFromSQL.h:56
static constexpr int obsgroup_sonde
Definition: DataFromSQL.h:31
static constexpr int obsgroup_gpsro
Definition: DataFromSQL.h:34
static constexpr int obsgroup_iasi
Definition: DataFromSQL.h:36
static constexpr int varno_cloud_fraction_covered
Definition: DataFromSQL.h:58
static constexpr int odb_type_real
Definition: DataFromSQL.h:64
static constexpr int varno_rawbt_amsu
Definition: DataFromSQL.h:57
static constexpr int obsgroup_atms
Definition: DataFromSQL.h:41
static constexpr int obsgroup_atovs
Definition: DataFromSQL.h:32
static constexpr int obsgroup_airs
Definition: DataFromSQL.h:33
static constexpr int varno_rawbt_mwts
Definition: DataFromSQL.h:60
static constexpr int obsgroup_ahiclr
Definition: DataFromSQL.h:45
static constexpr int obsgroup_cris
Definition: DataFromSQL.h:42
static constexpr int odb_missing_int
Definition: DataFromSQL.h:69
static constexpr int obsgroup_seviriclr
Definition: DataFromSQL.h:37
static constexpr int obsgroup_mwsfy3
Definition: DataFromSQL.h:44
static constexpr float odb_missing_float
Definition: DataFromSQL.h:68
static constexpr int odb_type_int
Definition: DataFromSQL.h:63
static constexpr int obsgroup_amsr
Definition: DataFromSQL.h:39
static constexpr int varno_rawbt_mwhs
Definition: DataFromSQL.h:61
static constexpr int obsgroup_mwri
Definition: DataFromSQL.h:46
static constexpr int obsgroup_gmihigh
Definition: DataFromSQL.h:48
static constexpr int obsgroup_hiras
Definition: DataFromSQL.h:49
static constexpr int varno_u_amb
Definition: DataFromSQL.h:55
static constexpr int varno_rawbt_amsr_89ghz
Definition: DataFromSQL.h:59
static constexpr int obsgroup_geocloud
Definition: DataFromSQL.h:38
static constexpr int varno_dd
Definition: DataFromSQL.h:51
static constexpr int odb_type_bitfield
Definition: DataFromSQL.h:66
static constexpr int varno_rawbt
Definition: DataFromSQL.h:52
static constexpr int obsgroup_scatwind
Definition: DataFromSQL.h:29
std::vector< std::shared_ptr< NewDimensionScale_Base > > NewDimensionScales_t
Used to specify Variable creation-time properties.
Definition: Has_Variables.h:57