ARTS  2.3.1285(git:92a29ea9-dirty)
m_telsem.cc
Go to the documentation of this file.
1 /* Copyright (C) 2017
2  Oliver Lemke <olemke@core-dump.info>
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 */
19 
26 #include "file.h"
27 #include "geodetic.h"
28 #include "matpackI.h"
29 #include "mystring.h"
30 #include "rte.h"
31 #include "telsem.h"
32 
33 extern Numeric EARTH_RADIUS;
34 extern Numeric DEG2RAD;
35 
36 /* Workspace method: Doxygen documentation will be auto-generated */
38  const Numeric &lat,
39  const Numeric &lon,
40  const Numeric &theta,
41  const Vector &f,
42  const TelsemAtlas &atlas,
43  const Numeric &d_max,
44  const Verbosity &) {
45  chk_if_in_range("Latitude input to TELSEM2", lat, -90.0, 90.0);
46  chk_if_in_range("Longitude input to TELSEM2", lon, 0.0, 360.0);
47 
48  Index cellnumber = atlas.calc_cellnum(lat, lon);
49  // Check if cell is in atlas.
50  if (!atlas.contains(cellnumber)) {
51  if (d_max <= 0.0) {
52  throw std::runtime_error(
53  "Given coordinates are not contained in "
54  " TELSEM atlas. To enable nearest neighbor"
55  "interpolation set *d_max* to a positive "
56  "value.");
57  } else {
58  cellnumber = atlas.calc_cellnum_nearest_neighbor(lat, lon);
59  Numeric lat_nn, lon_nn;
60  std::tie(lat_nn, lon_nn) = atlas.get_coordinates(cellnumber);
61  Numeric d = sphdist(lat, lon, lat_nn, lon_nn);
62  if (d > d_max) {
63  std::ostringstream out{};
64  out << "Distance of nearest neighbor exceeds provided limit (";
65  out << d << " > " << d_max << ").";
66  throw std::runtime_error(out.str());
67  }
68  }
69  }
70 
71  Index class1 = atlas.get_class1(cellnumber);
72  Index class2 = atlas.get_class2(cellnumber);
73  Vector emis_v = atlas.get_emis_v(cellnumber);
74  Vector emis_h = atlas.get_emis_h(cellnumber);
75 
76  emis.resize(f.nelem(), 2);
77  for (Index i = 0; i < f.nelem(); ++i) {
78  std::tie(emis(i, 0), emis(i, 1)) =
79  atlas.emis_interp(theta, f[i] * 1e-9, class1, class2, emis_v, emis_h);
80  }
81 }
82 
83 /* Workspace method: Doxygen documentation will be auto-generated */
84 void telsemSurfaceTypeLandSea(Index &surface_type,
85  const Index &atmosphere_dim,
86  const Vector &lat_grid,
87  const Vector &lat_true,
88  const Vector &lon_true,
89  const Vector &rtp_pos,
90  const TelsemAtlas &atlas,
91  const Verbosity &) {
92  // Checks
93  chk_latlon_true(atmosphere_dim, lat_grid, lat_true, lon_true);
94 
95  Numeric lat, lon;
97  lat, lon, atmosphere_dim, lat_grid, lat_true, lon_true, rtp_pos);
98  chk_if_in_range("Latitude input to TELSEM2", lat, -90.0, 90.0);
99  chk_if_in_range("Longitude input to TELSEM2", lon, 0.0, 360.0);
100 
101  Index cellnumber = atlas.calc_cellnum(lat, lon);
102  if (atlas.contains(cellnumber)) {
103  surface_type = 1;
104  } else {
105  surface_type = 0;
106  }
107 }
108 
109 /* Workspace method: Doxygen documentation will be auto-generated */
111  const Numeric &lat,
112  const Numeric &lon,
113  const TelsemAtlas &atlas,
114  const Verbosity &) {
115  chk_if_in_range("Latitude input to TELSEM2", lat, -90.0, 90.0);
116  chk_if_in_range("Longitude input to TELSEM2", lon, 0.0, 360.0);
117 
118  Index cellnumber = atlas.calc_cellnum(lat, lon);
119  if (atlas.contains(cellnumber)) {
120  emis = atlas[cellnumber];
121  } else {
122  emis.resize(0);
123  }
124 }
125 
126 /* Workspace method: Doxygen documentation will be auto-generated */
128  const String &directory,
129  const Index &month,
130  const String &filename_pattern,
131  const Verbosity &verbosity) {
132  CREATE_OUT2;
133  const Index imonth = filename_pattern.find("@MM@");
134  if (imonth < 0) {
135  ostringstream os;
136  os << "Substring '@MM@' not found in filename_pattern for" << std::endl
137  << "month number replacement: " << filename_pattern;
138  }
139 
140  std::ifstream is;
141 
142  ostringstream month_ss;
143  if (month < 10) {
144  month_ss << 0;
145  }
146  month_ss << month;
147 
148  String this_filename = filename_pattern;
149  this_filename.replace(imonth, 4, month_ss.str());
150  this_filename = directory + '/' + this_filename;
151 
152  out2 << "Reading TELSEM atlas: " << this_filename << '\n';
153  open_input_file(is, this_filename);
154  atlas.read(is);
155  atlas.set_month(month);
156 
157  String corr_filename = directory + '/' + "correlations";
158  out2 << "Reading correlations: " << corr_filename << '\n';
159  std::ifstream corr_is;
160  open_input_file(corr_is, corr_filename);
161  Tensor3 correlation(10, 7, 7);
162  String s;
163  for (Index i = 0; i < 10; i++) {
164  std::getline(corr_is, s);
165  for (Index j = 0; j < 7; j++) {
166  for (Index k = 0; k < 7; k++) {
167  corr_is >> correlation(i, j, k);
168  if (corr_is.fail())
169  throw std::runtime_error("Error reading correlation.");
170  }
171  std::getline(corr_is, s);
172  }
173  }
174  atlas.set_correl(correlation);
175 }
176 
177 /* Workspace method: Doxygen documentation will be auto-generated */
179  const String &directory,
180  const String &filename_pattern,
181  const Verbosity &verbosity) {
182  CREATE_OUT2;
183  const Index imonth = filename_pattern.find("@MM@");
184  if (imonth < 0) {
185  ostringstream os;
186  os << "Substring '@MM@' not found in filename_pattern for" << std::endl
187  << "month number replacement: " << filename_pattern;
188  }
189 
190  telsem_atlases.resize(12);
191  for (Index i = 1; i <= 12; i++) {
192  std::ifstream is;
193  ostringstream month;
194  if (i < 10) month << 0;
195  month << i;
196  String this_filename = filename_pattern;
197  this_filename.replace(imonth, 4, month.str());
198  this_filename = directory + '/' + this_filename;
199 
200  out2 << "Reading TELSEM atlas: " << this_filename << '\n';
201  open_input_file(is, this_filename);
202  telsem_atlases[i - 1].read(is);
203  telsem_atlases[i - 1].set_month(i);
204  }
205 
206  std::ifstream is;
207  String corr_filename = directory + '/' + "correlations";
208  out2 << "Reading correlations: " << corr_filename << '\n';
209  open_input_file(is, corr_filename);
210  Tensor3 correlation(10, 7, 7);
211  String s;
212  for (Index i = 0; i < 10; i++) {
213  std::getline(is, s);
214  for (Index j = 0; j < 7; j++) {
215  for (Index k = 0; k < 7; k++) {
216  is >> correlation(i, j, k);
217  if (is.fail()) throw std::runtime_error("Error reading correlation.");
218  }
219  std::getline(is, s);
220  }
221  }
222 
223  for (Index i = 0; i < 12; i++) {
224  telsem_atlases[i].set_correl(correlation);
225  }
226 }
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.
std::pair< Numeric, Numeric > get_coordinates(Index cellnum) const
Definition: telsem.cc:229
Numeric EARTH_RADIUS
void set_month(Index m)
Definition: telsem.h:72
Vector get_emis_h(Index cellnum) const
Definition: telsem.h:157
Index calc_cellnum(Numeric lat, Numeric lon) const
Definition: telsem.cc:142
void read(std::istream &is)
Definition: telsem.cc:46
The Vector class.
Definition: matpackI.h:860
void telsem_atlasesReadAscii(ArrayOfTelsemAtlas &telsem_atlases, const String &directory, const String &filename_pattern, const Verbosity &verbosity)
WORKSPACE METHOD: telsem_atlasesReadAscii.
Definition: m_telsem.cc:178
bool contains(Index cellnumber) const
Definition: telsem.h:83
void telsem_atlasReadAscii(TelsemAtlas &atlas, const String &directory, const Index &month, const String &filename_pattern, const Verbosity &verbosity)
WORKSPACE METHOD: telsem_atlasReadAscii.
Definition: m_telsem.cc:127
This file contains basic functions to handle ASCII files.
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
Index get_class2(Index cellnumber) const
Definition: telsem.h:118
void telsemStandalone(Matrix &emis, const Numeric &lat, const Numeric &lon, const Numeric &theta, const Vector &f, const TelsemAtlas &atlas, const Numeric &d_max, const Verbosity &)
WORKSPACE METHOD: telsemStandalone.
Definition: m_telsem.cc:37
Index get_class1(Index cellnumber) const
Definition: telsem.h:100
The Tensor3 class.
Definition: matpackIII.h:339
Vector get_emis_v(Index i) const
Definition: telsem.h:135
void pos2true_latlon(Numeric &lat, Numeric &lon, const Index &atmosphere_dim, ConstVectorView lat_grid, ConstVectorView lat_true, ConstVectorView lon_true, ConstVectorView pos)
Determines the true alt and lon for an "ARTS position".
Definition: rte.cc:2314
_CS_string_type str() const
Definition: sstream.h:491
void chk_latlon_true(const Index &atmosphere_dim, ConstVectorView lat_grid, ConstVectorView lat_true, ConstVectorView lon_true)
chk_latlon_true
Index calc_cellnum_nearest_neighbor(Numeric lat, Numeric lon) const
Definition: telsem.cc:174
Numeric sphdist(const Numeric &lat1, const Numeric &lon1, const Numeric &lat2, const Numeric &lon2)
sphdist
Definition: geodetic.cc:1205
Numeric DEG2RAD
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
The Matrix class.
Definition: matpackI.h:1193
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
Implementation of Matrix, Vector, and such stuff.
basic_ostringstream< char, string_char_traits< char >, alloc > ostringstream
Definition: sstream.h:204
This can be used to make arrays out of anything.
Definition: array.h:40
void open_input_file(ifstream &file, const String &name)
Open a file for reading.
Definition: file.cc:147
void chk_if_in_range(const String &x_name, const Index &x, const Index &x_low, const Index &x_high)
chk_if_in_range
Definition: check_input.cc:89
void telsemAtlasLookup(Vector &emis, const Numeric &lat, const Numeric &lon, const TelsemAtlas &atlas, const Verbosity &)
WORKSPACE METHOD: telsemAtlasLookup.
Definition: m_telsem.cc:110
void resize(Index n)
Resize function.
Definition: matpackI.cc:404
A telsem atlas.
Definition: telsem.h:57
#define CREATE_OUT2
Definition: messages.h:206
void set_correl(const Tensor3 &t)
Definition: telsem.h:76
void telsemSurfaceTypeLandSea(Index &surface_type, const Index &atmosphere_dim, const Vector &lat_grid, const Vector &lat_true, const Vector &lon_true, const Vector &rtp_pos, const TelsemAtlas &atlas, const Verbosity &)
WORKSPACE METHOD: telsemSurfaceTypeLandSea.
Definition: m_telsem.cc:84
This file contains the definition of String, the ARTS string class.
Declaration of functions in rte.cc.
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1056