ARTS  2.3.1285(git:92a29ea9-dirty)
m_raw.cc
Go to the documentation of this file.
1 /* Copyright (C) 2020
2  * Richard Larsson <larsson@mps.mpg.de>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2, or (at your option) any
7  * later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17  * USA. */
18 
28 #include "artstime.h"
29 #include "raw.h"
30 
31 
33  const Vector& cold,
34  const Vector& atm,
35  const Vector& hot,
36  const Numeric& cold_temp,
37  const Numeric& hot_temp,
38  const Index& calib,
39  const Verbosity&)
40 {
41  if(cold.nelem() not_eq atm.nelem() or atm.nelem() not_eq hot.nelem()) {
42  throw std::runtime_error("Length of vectors must be correct");
43  }
44 
45  y.resize(atm.nelem());
46  if (calib) {
47  for (Index i=0; i<y.nelem(); i++) {
48  y[i] = calibration(cold[i], atm[i], hot[i], cold_temp, hot_temp);
49  }
50  } else {
51  for (Index i=0; i<y.nelem(); i++) {
52  y[i] = systemtemp(cold[i], hot[i], cold_temp, hot_temp);
53  }
54  }
55 }
56 
57 
59  ArrayOfTime& time_grid,
60  ArrayOfMatrix& covmat_sepsbatch,
61  ArrayOfIndex& counts,
62  const String& time_step,
63  const Index& disregard_first,
64  const Index& disregard_last,
65  const Verbosity&)
66 {
67  // Size of problem
68  const Index n=time_grid.nelem();
69  if (time_grid.nelem() not_eq n) {
70  throw std::runtime_error("Time vector length must match input data length");
71  }
72 
73  // Time is not decreasing
74  if (not std::is_sorted(time_grid.cbegin(), time_grid.cend()))
75  throw std::runtime_error("Time vector cannot decrease");
76 
77  // Find the limits of the range
78  auto lims = time_steps(time_grid, time_step);
79 
80  // Output variables
81  ArrayOfVector ybatch_out;
82  ArrayOfTime time_grid_out;
83 
84  if (lims.front() == n) {
85  ybatch_out.resize(0);
86  time_grid_out.resize(0);
87  covmat_sepsbatch.resize(0);
88  counts.resize(0);
89  } else {
90 
91  // Frequency grids
92  const Index k = ybatch[0].nelem();
93  if (not std::all_of(ybatch.cbegin(), ybatch.cend(), [k](auto& x){return x.nelem() == k;})) {
94  throw std::runtime_error("Bad frequency grid size in input data; expects all equal");
95  }
96 
97  // Allocate output
98  const Index m = lims.nelem() - 1;
99  if (m < 0)
100  throw std::runtime_error("Must include last if time step covers all of the range");
101  ybatch_out = ArrayOfVector(m, Vector(k));
102  time_grid_out = ArrayOfTime(m);
103  covmat_sepsbatch = ArrayOfMatrix(m, Matrix(k, k));
104  counts.resize(m);
105 
106  // Fill output
107  #pragma omp parallel for if (not arts_omp_in_parallel()) schedule(guided)
108  for (Index i=0; i<m; i++) {
109  counts[i] = lims[i+1] - lims[i];
110  time_grid_out[i] = mean_time(time_grid, lims[i], lims[i+1]);
111  linalg::avg(ybatch_out[i], ybatch, lims[i], lims[i+1]);
112  linalg::cov(covmat_sepsbatch[i], ybatch_out[i], ybatch, lims[i], lims[i+1]);
113  }
114  }
115 
116  if (disregard_first) {
117  counts.erase(counts.begin());
118  ybatch_out.erase(ybatch_out.begin());
119  time_grid_out.erase(time_grid_out.begin());
120  covmat_sepsbatch.erase(covmat_sepsbatch.begin());
121  }
122 
123  if (disregard_last) {
124  counts.pop_back();
125  ybatch_out.pop_back();
126  time_grid_out.pop_back();
127  covmat_sepsbatch.pop_back();
128  }
129 
130  ybatch = ybatch_out;
131  time_grid = time_grid_out;
132 }
133 
134 
136  ArrayOfVector& ybatch,
137  const ArrayOfIndex& range,
138  const Vector& trop_temp,
139  const Numeric& targ_temp,
140  const Verbosity&)
141 {
142  // Size of problem
143  const Index n=ybatch.nelem();
144 
145  const Index m=n?ybatch[0].nelem():0;
146  if (std::any_of(ybatch.begin(), ybatch.end(), [m](auto& y){return y.nelem() not_eq m;})) {
147  throw std::runtime_error("Bad input size, all of ybatch must match itself");
148  } else if (trop_temp.nelem() not_eq n) {
149  throw std::runtime_error("Bad input size, trop_temp must match ybatch");
150  }
151 
152  // This algorithm stores partial transmission and median and tropospheric temperature in the correction terms
153  ybatch_corr = ArrayOfVector(n, Vector(3));
154 
155  // Compute tropospheric correction
156  for (Index i=0; i<n; i++) {
157  ybatch_corr[i][2] = trop_temp[i];
158  ybatch_corr[i][0] = linalg::median(ybatch[i], range);
159  ybatch_corr[i][1] = std::exp(- std::log((ybatch_corr[i][2] - ybatch_corr[i][0]) / (ybatch_corr[i][2] - targ_temp)));
160  }
161 
162  // Apply correction
163  for (Index i=0; i<n; i++) {
164  ybatch[i] *= ybatch_corr[i][1];
165  ybatch[i] += ybatch_corr[i][2] * (1 - ybatch_corr[i][1]);
166  }
167 }
168 
169 
171  const ArrayOfVector& ybatch_corr,
172  const Verbosity&)
173 {
174  // Size of problem
175  const Index n=ybatch.nelem();
176  if ((std::any_of(ybatch_corr.begin(), ybatch_corr.end(), [](auto& corr){return corr.nelem() not_eq 3;})) or ybatch_corr.nelem() not_eq n) {
177  throw std::runtime_error("Bad input size, all of ybatch_corr must match ybatch and have three elements each");
178  }
179 
180  // Apply inverse of correction
181  for (Index i=0; i<n; i++) {
182  ybatch[i] -= ybatch_corr[i][2] * (1 - ybatch_corr[i][1]);
183  ybatch[i] /= ybatch_corr[i][1];
184  }
185 }
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
void ybatchTroposphericCorrectionNaiveMedianInverse(ArrayOfVector &ybatch, const ArrayOfVector &ybatch_corr, const Verbosity &)
WORKSPACE METHOD: ybatchTroposphericCorrectionNaiveMedianInverse.
Definition: m_raw.cc:170
Index nelem() const
Number of elements.
Definition: array.h:195
void avg(VectorView y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the average of the ranged ys.
Definition: raw.cc:32
The Vector class.
Definition: matpackI.h:860
invlib::Vector< ArtsVector > Vector
invlib wrapper type for ARTS vectors.
Definition: oem.h:32
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
Array< Vector > ArrayOfVector
An array of vectors.
Definition: array.h:58
void cov(MatrixView cov, const Vector &y, const ArrayOfVector &ys, const Index start=0, const Index end=-1)
Compute the covariance matrix of the ranged ys.
Definition: raw.cc:72
void yColdAtmHot(Vector &y, const Vector &cold, const Vector &atm, const Vector &hot, const Numeric &cold_temp, const Numeric &hot_temp, const Index &calib, const Verbosity &)
WORKSPACE METHOD: yColdAtmHot.
Definition: m_raw.cc:32
bool is_sorted(ConstVectorView x)
Checks if a vector is sorted in ascending order.
Definition: logic.cc:199
Stuff related to generating y-data from raw data.
Time mean_time(const ArrayOfTime &ts, Index s, Index e)
Computes the average time in a list.
Definition: artstime.cc:135
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
void ybatchTroposphericCorrectionNaiveMedianForward(ArrayOfVector &ybatch_corr, ArrayOfVector &ybatch, const ArrayOfIndex &range, const Vector &trop_temp, const Numeric &targ_temp, const Verbosity &)
WORKSPACE METHOD: ybatchTroposphericCorrectionNaiveMedianForward.
Definition: m_raw.cc:135
Array< Matrix > ArrayOfMatrix
An array of matrices.
Definition: array.h:66
constexpr Numeric calibration(Numeric pc, Numeric pa, Numeric ph, Numeric tc, Numeric th) noexcept
Computes the linear calibration formula.
Definition: raw.h:44
Array< Time > ArrayOfTime
List of times.
Definition: artstime.h:81
Stuff related to time in ARTS.
void resize(Index n)
Resize function.
Definition: matpackI.cc:404
constexpr Numeric systemtemp(Numeric pc, Numeric ph, Numeric tc, Numeric th) noexcept
Computes the linear receiver temperature formula.
Definition: raw.h:59
ArrayOfIndex time_steps(const ArrayOfTime &times, const String &step)
Finds the index matching demands in a list of times.
Definition: artstime.cc:60
invlib::Matrix< ArtsMatrix > Matrix
invlib wrapper type for ARTS matrices.
Definition: oem.h:34
void ybatchTimeAveraging(ArrayOfVector &ybatch, ArrayOfTime &time_grid, ArrayOfMatrix &covmat_sepsbatch, ArrayOfIndex &counts, const String &time_step, const Index &disregard_first, const Index &disregard_last, const Verbosity &)
WORKSPACE METHOD: ybatchTimeAveraging.
Definition: m_raw.cc:58
Numeric median(const ConstVectorView v, const ArrayOfIndex &pos=ArrayOfIndex{})
Get the median of the vector in the range.
Definition: raw.cc:90