IODA Bundle
GsiSatBiasReader.cpp
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2021 UCAR
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #include "GsiSatBiasReader.h"
9 
10 #include <string>
11 #include <vector>
12 #include <fstream>
13 
14 #include <Eigen/Dense>
15 
16 /// Read info for one channel from the GSI text file
17 /// Returns sensor (instrument + satellite) name and channel index
18 void readOneChannel(std::ifstream & infile, std::string & sensor, size_t & channel) {
19  float tlap, tsum;
20  std::size_t ntlapupdate;
21  float par;
22  infile >> sensor;
23  infile >> channel;
24  infile >> tlap;
25  infile >> tsum;
26  infile >> ntlapupdate;
27  for (size_t ii = 0; ii < gsi_npredictors; ++ii) {
28  infile >> par;
29  }
30 }
31 
32 /// Finds all sensors in the file (returned in \p sensors) and number of channels
33 /// for all the sensors (returned in \p nchannels)
34 void findSensorsChannels(const std::string & filename, std::vector<std::string> & sensors,
35  std::vector<int> & nchannels) {
36  std::ifstream infile(filename);
37 
38  std::size_t ich; // sequential number
39  std::string sensor; // sensor/instrument/satellite
40  std::size_t channel; // channel number
41 
42  if (infile.is_open())
43  {
44  /// Read the first entry, save first sensor
45  infile >> ich;
46  readOneChannel(infile, sensor, channel);
47  sensors.push_back(sensor);
48  size_t nsensors = 0;
49  nchannels.push_back(1);
50 
51  while (infile >> ich)
52  {
53  readOneChannel(infile, sensor, channel);
54  if (sensor == sensors[nsensors]) {
55  // still processing the same sensor, just update the number of channels
56  nchannels[nsensors]++;
57  } else {
58  // new sensor: move on
59  sensors.push_back(sensor);
60  nsensors++;
61  nchannels.push_back(1);
62  }
63  }
64  infile.close();
65  }
66 }
67 
68 //---------------------------------------------------------------------------------------
69 void readObsBiasCoefficients(const std::string & filename, const std::string & sensor,
70  std::vector<int> & channels, Eigen::ArrayXXf & coeffs ) {
71  std::ifstream infile(filename);
72 
73  std::size_t ich; // sequential number
74  std::string nusis; // sensor/instrument/satellite
75  std::size_t nuchan; // channel number
76  float tlap, tsum;
77  std::size_t ntlapupdate;
78 
79  if (infile.is_open())
80  {
81  float par;
82  size_t jchan = 0;
83  while (infile >> ich)
84  {
85  infile >> nusis;
86  infile >> nuchan;
87  infile >> tlap;
88  infile >> tsum;
89  infile >> ntlapupdate;
90  if (nusis == sensor) {
91  /// it's the sensor we're interested in; read in coefficients and channel
92  /// indices
93  for (size_t jpred = 0; jpred < gsi_npredictors; ++jpred) {
94  infile >> par;
95  coeffs(jpred, jchan) = par;
96  channels[jchan] = nuchan;
97  }
98  jchan++;
99  } else {
100  /// not interested in this channel; passing
101  for (size_t jpred = 0; jpred < gsi_npredictors; ++jpred) {
102  infile >> par;
103  }
104  }
105  }
106  infile.close();
107  }
108 }
109 
110 //---------------------------------------------------------------------------------------
111 void readObsBiasCoeffErrors(const std::string & filename, const std::string & sensor,
112  std::vector<int> & channels, Eigen::ArrayXXf & errs,
113  Eigen::ArrayXf & nobs) {
114  std::ifstream infile(filename);
115 
116  std::size_t ich; // sequential number
117  std::string nusis; // sensor/instrument/satellite
118  std::size_t nuchan; // channel number
119 
120  if (infile.is_open())
121  {
122  float par;
123  size_t jchan = 0;
124  while (infile >> ich)
125  {
126  infile >> nusis;
127  infile >> nuchan;
128  infile >> par;
129  if (nusis == sensor) {
130  nobs(jchan) = par;
131  /// it's the sensor we're interested in; read in coefficients and channel
132  /// indices
133  for (size_t jpred = 0; jpred < gsi_npredictors; ++jpred) {
134  infile >> par;
135  errs(jpred, jchan) = par;
136  channels[jchan] = nuchan;
137  }
138  jchan++;
139  } else {
140  /// not interested in this channel; passing
141  for (size_t jpred = 0; jpred < gsi_npredictors; ++jpred) {
142  infile >> par;
143  }
144  }
145  }
146  infile.close();
147  }
148 }
void readOneChannel(std::ifstream &infile, std::string &sensor, size_t &channel)
void readObsBiasCoefficients(const std::string &filename, const std::string &sensor, std::vector< int > &channels, Eigen::ArrayXXf &coeffs)
void findSensorsChannels(const std::string &filename, std::vector< std::string > &sensors, std::vector< int > &nchannels)
void readObsBiasCoeffErrors(const std::string &filename, const std::string &sensor, std::vector< int > &channels, Eigen::ArrayXXf &errs, Eigen::ArrayXf &nobs)
constexpr size_t gsi_npredictors
Number of predictors in GSI satbias file.