ARTS  2.4.0(git:4fb77825)
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 */
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
94 
95  Numeric lat, lon;
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 }
Matrix
The Matrix class.
Definition: matpackI.h:1193
TelsemAtlas::set_correl
void set_correl(const Tensor3 &t)
Definition: telsem.h:76
ARTS::Var::atmosphere_dim
Index atmosphere_dim(Workspace &ws) noexcept
Definition: autoarts.h:2510
TelsemAtlas::calc_cellnum
Index calc_cellnum(Numeric lat, Numeric lon) const
Definition: telsem.cc:142
ARTS::Var::lat
Numeric lat(Workspace &ws) noexcept
Definition: autoarts.h:3933
DEG2RAD
Numeric DEG2RAD
ARTS::Var::surface_type
Index surface_type(Workspace &ws) noexcept
Definition: autoarts.h:6886
Tensor3
The Tensor3 class.
Definition: matpackIII.h:339
ARTS::Var::telsem_atlases
ArrayOfTelsemAtlas telsem_atlases(Workspace &ws) noexcept
Definition: autoarts.h:7004
ARTS::Var::lat_grid
Vector lat_grid(Workspace &ws) noexcept
Definition: autoarts.h:3962
TelsemAtlas::read
void read(std::istream &is)
Definition: telsem.cc:46
ARTS::Var::verbosity
Verbosity verbosity(Workspace &ws) noexcept
Definition: autoarts.h:7112
sphdist
Numeric sphdist(const Numeric &lat1, const Numeric &lon1, const Numeric &lat2, const Numeric &lon2)
sphdist
Definition: geodetic.cc:1205
TelsemAtlas::get_class2
Index get_class2(Index cellnumber) const
Definition: telsem.h:118
Vector::resize
void resize(Index n)
Resize function.
Definition: matpackI.cc:404
TelsemAtlas::contains
bool contains(Index cellnumber) const
Definition: telsem.h:83
TelsemAtlas
A telsem atlas.
Definition: telsem.h:57
open_input_file
void open_input_file(ifstream &file, const String &name)
Open a file for reading.
Definition: file.cc:147
telsemStandalone
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
CREATE_OUT2
#define CREATE_OUT2
Definition: messages.h:206
telsemSurfaceTypeLandSea
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
ARTS::Var::lon
Numeric lon(Workspace &ws) noexcept
Definition: autoarts.h:4064
ARTS::Var::lon_true
Vector lon_true(Workspace &ws) noexcept
Definition: autoarts.h:4113
TelsemAtlas::get_emis_v
Vector get_emis_v(Index i) const
Definition: telsem.h:135
matpackI.h
Implementation of Matrix, Vector, and such stuff.
Array
This can be used to make arrays out of anything.
Definition: array.h:108
EARTH_RADIUS
Numeric EARTH_RADIUS
telsem_atlasReadAscii
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
TelsemAtlas::get_class1
Index get_class1(Index cellnumber) const
Definition: telsem.h:100
my_basic_string< char >
telsemAtlasLookup
void telsemAtlasLookup(Vector &emis, const Numeric &lat, const Numeric &lon, const TelsemAtlas &atlas, const Verbosity &)
WORKSPACE METHOD: telsemAtlasLookup.
Definition: m_telsem.cc:110
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:51
chk_latlon_true
void chk_latlon_true(const Index &atmosphere_dim, ConstVectorView lat_grid, ConstVectorView lat_true, ConstVectorView lon_true)
chk_latlon_true
Definition: check_input.cc:1652
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Verbosity
Definition: messages.h:49
geodetic.h
TelsemAtlas::emis_interp
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
ARTS::Var::lat_true
Vector lat_true(Workspace &ws) noexcept
Definition: autoarts.h:3985
TelsemAtlas::calc_cellnum_nearest_neighbor
Index calc_cellnum_nearest_neighbor(Numeric lat, Numeric lon) const
Definition: telsem.cc:174
Matrix::resize
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1056
pos2true_latlon
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
rte.h
Declaration of functions in rte.cc.
file.h
This file contains basic functions to handle ASCII files.
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
chk_if_in_range
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
TelsemAtlas::get_coordinates
std::pair< Numeric, Numeric > get_coordinates(Index cellnum) const
Definition: telsem.cc:229
telsem_atlasesReadAscii
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
Vector
The Vector class.
Definition: matpackI.h:860
TelsemAtlas::get_emis_h
Vector get_emis_h(Index cellnum) const
Definition: telsem.h:157
telsem.h
This file contains the definition of the TELSEM atlas format.
mystring.h
This file contains the definition of String, the ARTS string class.
ARTS::Var::rtp_pos
Vector rtp_pos(Workspace &ws) noexcept
Definition: autoarts.h:5774
TelsemAtlas::set_month
void set_month(Index m)
Definition: telsem.h:72