ARTS  2.3.1285(git:92a29ea9-dirty)
test_telsem.cc
Go to the documentation of this file.
1 /* Copyright (C) 2018
2  Simon Pfreundschuh <simon.pfreundschuh@chalmers.se>
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 
27 #include <fstream>
28 #include <iostream>
29 #include <memory>
30 #include <string>
31 
32 #include "arts.h"
33 #include "telsem.h"
34 
49 Numeric test_telsem_interpolate(std::string atlas_file,
50  std::string result_path,
51  Numeric resolution,
52  Numeric theta,
53  Vector frequencies) {
54  TelsemAtlas atlas(atlas_file);
55 
56  Index n_freqs = frequencies.nelem();
57  std::vector<std::unique_ptr<std::ifstream>> results_h, results_v;
58  results_h.reserve(n_freqs);
59  results_v.reserve(n_freqs);
60 
61  for (Index i = 0; i < n_freqs; ++i) {
62  std::string filename_h = result_path;
63  std::string filename_v = result_path;
64  filename_h += "/emisH_IND_MULT" + std::to_string(i + 1) + ".txt";
65  filename_v += "/emisV_IND_MULT" + std::to_string(i + 1) + ".txt";
66 
67  results_h.push_back(std::unique_ptr<std::ifstream>(
68  new std::ifstream(filename_h, std::ifstream::in)));
69  results_v.push_back(std::unique_ptr<std::ifstream>(
70  new std::ifstream(filename_v, std::ifstream::in)));
71  }
72 
73  Index n_lat = static_cast<Index>(180.0 / resolution);
74  Index n_lon = static_cast<Index>(360.0 / resolution);
75 
76  Numeric error = 0.0;
77 
78  for (Index i = n_lat - 1; i >= 0; --i) {
79  for (Index j = 0; j < n_lon; ++j) {
80  // An offset of half of the latitude resolution is added to avoid hitting
81  // the boundary between to cells.
82 
83  Numeric lat = 0.125 + resolution / 2.0 - 90.0 +
84  static_cast<Numeric>(i) * resolution;
85  Numeric lon =
86  0.125 + resolution / 2.0 + static_cast<Numeric>(j) * resolution;
87 
88  Index cellnumber = atlas.calc_cellnum(lat, lon);
89 
90  Vector emis_h(3), emis_h_interp(n_freqs), emis_h_interp_ref(n_freqs),
91  emis_v(3), emis_v_interp(n_freqs), emis_v_interp_ref(n_freqs);
92 
93  // Read reference emissivities.
94  for (Index k = 0; k < n_freqs; ++k) {
95  (*results_h[k]) >> emis_h_interp_ref[k];
96  (*results_v[k]) >> emis_v_interp_ref[k];
97  }
98 
99  emis_h_interp = 0.0;
100  emis_v_interp = 0.0;
101 
102  // Read emissivities from atlas.
103  if (atlas.contains(cellnumber)) {
104  emis_h = atlas.get_emis_h(cellnumber);
105  emis_v = atlas.get_emis_v(cellnumber);
106 
107  Index class1 = atlas.get_class1(cellnumber);
108  Index class2 = atlas.get_class2(cellnumber);
109 
110  for (Index k = 0; k < n_freqs; ++k) {
111  std::tie(emis_v_interp[k], emis_h_interp[k]) = atlas.emis_interp(
112  theta, frequencies[k], class1, class2, emis_v, emis_h);
113  }
114  }
115 
116  for (Index k = 0; k < n_freqs; ++k) {
117  error =
118  std::max(error, std::fabs(emis_h_interp[k] - emis_h_interp_ref[k]));
119  error =
120  std::max(error, std::fabs(emis_v_interp[k] - emis_v_interp_ref[k]));
121  }
122  }
123  }
124  return error;
125 }
126 
140  String result_path,
141  Numeric resolution) {
142  TelsemAtlas atlas(atlas_file);
143 
144  std::vector<std::unique_ptr<std::ifstream>> results_h, results_v;
145  results_h.reserve(3);
146  results_v.reserve(3);
147 
148  for (Index i = 0; i < 3; ++i) {
149  std::string filename_h = result_path;
150  std::string filename_v = result_path;
151  filename_h += "/emisH" + std::to_string(i + 1) + ".txt";
152  filename_v += "/emisV" + std::to_string(i + 1) + ".txt";
153 
154  results_h.push_back(std::unique_ptr<std::ifstream>(
155  new std::ifstream(filename_h, std::ifstream::in)));
156  results_v.push_back(std::unique_ptr<std::ifstream>(
157  new std::ifstream(filename_v, std::ifstream::in)));
158  }
159 
160  Index n_lat = static_cast<Index>(180.0 / resolution);
161  Index n_lon = static_cast<Index>(360.0 / resolution);
162 
163  Numeric error = 0.0;
164 
165  for (Index i = n_lat - 1; i >= 0; --i) {
166  for (Index j = 0; j < n_lon; ++j) {
167  // An offset of half of the latitude resolution is added to avoid hitting
168  // the boundary between to cells.
169 
170  Numeric lat = 0.125 + resolution / 2.0 - 90.0 +
171  static_cast<Numeric>(i) * resolution;
172  Numeric lon =
173  0.125 + resolution / 2.0 + static_cast<Numeric>(j) * resolution;
174 
175  Index cellnumber = atlas.calc_cellnum(lat, lon);
176 
177  Vector emis_h(3), emis_h_ref(3), emis_v(3), emis_v_ref(3);
178 
179  // Read reference emissivities.
180  for (Index k = 0; k < 3; ++k) {
181  (*results_h[k]) >> emis_h_ref[k];
182  (*results_v[k]) >> emis_v_ref[k];
183  }
184 
185  emis_h = 0.0;
186  emis_v = 0.0;
187 
188  // Read emissivities from atlas.
189  if (atlas.contains(cellnumber)) {
190  emis_h = atlas.get_emis_h(cellnumber);
191  emis_v = atlas.get_emis_v(cellnumber);
192  }
193 
194  for (Index k = 0; k < 3; ++k) {
195  error = std::max(error, std::fabs(emis_h[k] - emis_h_ref[k]));
196  error = std::max(error, std::fabs(emis_v[k] - emis_v_ref[k]));
197  }
198  }
199  }
200  return error;
201 }
202 
203 int main(int argc, const char** argv) {
204  if (argc != 4) {
205  std::cout
206  << "\nThis test uses the test results of the TELSEM2 fortran\n"
207  "module to test the ARTS TELSEM interface. \n\n Usage:\n"
208  "./test_telsem <atlas_path> <results_folder> <resolution> \n"
209  "where:\n - atlas_path: Path to the TELSEM2 atlas to load \n"
210  " - results_folder: The folder containing the result files \n"
211  "\t of the TELSEM2 tests.\n - resolution: The resolution used"
212  "to generate the lat/lon map.\n\n Note that for the interpolation"
213  "test the frequencies must match.\n\n";
214  return 0;
215  }
216 
217  String atlas_file = argv[1];
218  String result_path = argv[2];
219  Numeric resolution = std::stoi(argv[3]);
220 
221  std::cout << "Atlas file: " << atlas_file << std::endl;
222  std::cout << "Result path: " << result_path << std::endl;
223 
224  // Reading of emissivities.
225 
226  Numeric error = test_telsem_read(atlas_file, result_path, resolution);
227  std::cout << "Maximum error reading emissivities: " << error
228  << std::endl;
229 
230  // Interpolation of emissivities.
231 
232  Vector frequencies = {6.0, 25.0, 31.4, 60.0, 190.0};
233  Numeric theta = 15.0; // Incidence angle
234  error = test_telsem_interpolate(
235  atlas_file, result_path, resolution, theta, frequencies);
236  std::cout << "Maximum error interpolating emissivities: " << error
237  << std::endl;
238  return 0;
239 }
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
This file contains the definition of the TELSEM atlas format.
Vector get_emis_h(Index cellnum) const
Definition: telsem.h:157
Index calc_cellnum(Numeric lat, Numeric lon) const
Definition: telsem.cc:142
The Vector class.
Definition: matpackI.h:860
bool contains(Index cellnumber) const
Definition: telsem.h:83
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
Index get_class2(Index cellnumber) const
Definition: telsem.h:118
Numeric test_telsem_read(String atlas_file, String result_path, Numeric resolution)
Test reading of TELSEM emissivities.
Definition: test_telsem.cc:139
Index get_class1(Index cellnumber) const
Definition: telsem.h:100
The global header file for ARTS.
Vector get_emis_v(Index i) const
Definition: telsem.h:135
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
std::pair< Numeric, Numeric > emis_interp(Numeric theta, Numeric freq, Index class1, Index class2, const ConstVectorView &ev, const ConstVectorView &eh) const
Definition: telsem.cc:291
A telsem atlas.
Definition: telsem.h:57
#define max(a, b)
Numeric test_telsem_interpolate(std::string atlas_file, std::string result_path, Numeric resolution, Numeric theta, Vector frequencies)
Test reading of TELSEM emissivity interpolation.
Definition: test_telsem.cc:49
int main(int argc, const char **argv)
Definition: test_telsem.cc:203