ARTS  2.3.1285(git:92a29ea9-dirty)
gridded_fields.h
Go to the documentation of this file.
1 /* Copyright (C) 2008-2012 Oliver Lemke <olemke@core-dump.info>
2 
3  This program is free software; you can redistribute it and/or modify it
4  under the terms of the GNU General Public License as published by the
5  Free Software Foundation; either version 2, or (at your option) any
6  later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16  USA.
17 */
18 
33 #ifndef gridded_fields_h
34 #define gridded_fields_h
35 
36 #include <stdexcept>
37 #include "array.h"
38 #include "matpackVI.h"
39 #include "mystring.h"
40 
43 
45 
46 #define CHECK_ERROR_BOILERPLATE \
47  "size mismatch between grids and data.\n" \
48  "Note that a grid is allowed to be empty, but in the\n" \
49  "data that dimension must have exactly one element.\n"
50 
52 class GriddedField {
53  private:
60 
61  protected:
63 
68  : dim(0),
69  mname(),
70  mgridtypes(),
71  mgridnames(),
72  mstringgrids(),
73  mnumericgrids() { /* Nothing to do here */
74  }
75 
77 
86  GriddedField(const Index d, const String& s)
87  : dim(d),
88  mname(s),
89  mgridtypes(d, GRID_TYPE_NUMERIC),
90  mgridnames(d),
91  mstringgrids(d),
92  mnumericgrids(d) { /* Nothing to do here */
93  }
94 
95  public:
97 
98  Index get_dim() const { return dim; }
99 
100  void copy_grids(const GriddedField& gf);
101 
103 
109  const String& get_grid_name(Index i) const { return mgridnames[i]; }
110 
112 
119  Index ret = 0;
120  assert(i < dim);
121  switch (mgridtypes[i]) {
122  case GRID_TYPE_NUMERIC:
123  ret = mnumericgrids[i].nelem();
124  break;
125  case GRID_TYPE_STRING:
126  ret = mstringgrids[i].nelem();
127  break;
128  }
129 
130  return ret;
131  }
132 
134 
140  GridType get_grid_type(Index i) const { return mgridtypes[i]; }
141 
142  const Vector& get_numeric_grid(Index i) const;
143 
145 
146  const ArrayOfString& get_string_grid(Index i) const;
147 
149 
151 
152  const String& get_name() const { return mname; }
153 
154  void set_grid(Index i, const Vector& g);
155 
156  void set_grid(Index i, const ArrayOfString& g);
157 
159 
165  void set_grid_name(Index i, const String& s) {
166  assert(i < dim);
167  mgridnames[i] = s;
168  }
169 
171 
172  void set_name(const String& s) { mname = s; }
173 
175 
182  virtual bool checksize() const = 0;
183 
185 
188  virtual void checksize_strict() const = 0;
189 
191  virtual ~GriddedField() {}
192 
193  friend std::ostream& operator<<(std::ostream& os, const GriddedField& gf);
194 };
195 
196 class GriddedField1 final : public GriddedField {
197  public:
201 
202  GriddedField1(const String& s) : GriddedField(1, s) {}
203 
204  bool checksize() const final {
205  return (!get_grid_size(0) && data.nelem() == 1) ||
206  data.nelem() == get_grid_size(0);
207  }
208 
209  void checksize_strict() const final {
210  if (!checksize()) {
212  os << "GriddedField1 ";
213  if (get_name().length()) os << "(" << get_name() << ") ";
215  os << "Grid";
216  if (get_grid_name(0).nelem()) os << " (" << get_grid_name(0) << ")";
217  os << " = " << get_grid_size(0) << "\n";
218  os << "Data";
219  os << " = " << data.nelem();
220  throw std::runtime_error(os.str());
221  }
222  }
223 
225 
226  void resize(const GriddedField1& gf) { data.resize(gf.get_grid_size(0)); }
227 
229 
230  void resize(Index n) { data.resize(n); }
231 
232  friend std::ostream& operator<<(std::ostream& os, const GriddedField1& gf);
233 
235 };
236 
237 class GriddedField2 final : public GriddedField {
238  public:
242 
243  GriddedField2(const String& s) : GriddedField(2, s) {}
244 
245  bool checksize() const final {
246  return ((!get_grid_size(1) && data.ncols() == 1) ||
247  data.ncols() == get_grid_size(1)) &&
248  ((!get_grid_size(0) && data.nrows() == 1) ||
249  data.nrows() == get_grid_size(0));
250  }
251 
252  void checksize_strict() const final {
253  if (!checksize()) {
255  os << "GriddedField2 ";
256  if (get_name().length()) os << "(" << get_name() << ") ";
258  for (Index i = 0; i < 2; i++) {
259  os << "Grid " << i;
260  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
261  os << " = " << get_grid_size(i) << "\n";
262  }
263  os << "Data";
264  os << " = " << data.nrows() << ", " << data.ncols();
265  throw std::runtime_error(os.str());
266  }
267  }
268 
270 
271  void resize(const GriddedField2& gf) {
272  data.resize(gf.get_grid_size(0), gf.get_grid_size(1));
273  }
274 
276 
277  void resize(Index r, Index c) { data.resize(r, c); }
278 
279  friend std::ostream& operator<<(std::ostream& os, const GriddedField2& gf);
280 
282 };
283 
284 class GriddedField3 final : public GriddedField {
285  public:
289 
290  GriddedField3(const String& s) : GriddedField(3, s) {}
291 
293  data = n;
294 
295  return *this;
296  }
297 
298  bool checksize() const final {
299  return ((!get_grid_size(2) && data.ncols() == 1) ||
300  data.ncols() == get_grid_size(2)) &&
301  ((!get_grid_size(1) && data.nrows() == 1) ||
302  data.nrows() == get_grid_size(1)) &&
303  ((!get_grid_size(0) && data.npages() == 1) ||
304  data.npages() == get_grid_size(0));
305  }
306 
307  void checksize_strict() const final {
308  if (!checksize()) {
310  os << "GriddedField3 ";
311  if (get_name().length()) os << "(" << get_name() << ") ";
313  for (Index i = 0; i < 3; i++) {
314  os << "Grid " << i;
315  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
316  os << " = " << get_grid_size(i) << "\n";
317  }
318  os << "Data";
319  os << " = " << data.npages() << ", " << data.nrows() << ", "
320  << data.ncols();
321  throw std::runtime_error(os.str());
322  }
323  }
324 
326 
327  void resize(const GriddedField3& gf) {
328  data.resize(gf.get_grid_size(0), gf.get_grid_size(1), gf.get_grid_size(2));
329  }
330 
332 
333  void resize(Index p, Index r, Index c) { data.resize(p, r, c); }
334 
335  friend std::ostream& operator<<(std::ostream& os, const GriddedField3& gf);
336 
338 };
339 
340 class GriddedField4 final : public GriddedField {
341  public:
345 
346  GriddedField4(const String& s) : GriddedField(4, s) {}
347 
348  bool checksize() const final {
349  return ((!get_grid_size(3) && data.ncols() == 1) ||
350  data.ncols() == get_grid_size(3)) &&
351  ((!get_grid_size(2) && data.nrows() == 1) ||
352  data.nrows() == get_grid_size(2)) &&
353  ((!get_grid_size(1) && data.npages() == 1) ||
354  data.npages() == get_grid_size(1)) &&
355  ((!get_grid_size(0) && data.nbooks() == 1) ||
356  data.nbooks() == get_grid_size(0));
357  }
358 
359  void checksize_strict() const final {
360  if (!checksize()) {
362  os << "GriddedField4 ";
363  if (get_name().length()) os << "(" << get_name() << ") ";
365  for (Index i = 0; i < 4; i++) {
366  os << "Grid " << i;
367  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
368  os << " = " << get_grid_size(i) << "\n";
369  }
370  os << "Data";
371  os << " = " << data.nbooks() << ", " << data.npages() << ", ";
372  os << data.nrows() << ", " << data.ncols();
373  throw std::runtime_error(os.str());
374  }
375  }
376 
378 
379  void resize(const GriddedField4& gf) {
380  data.resize(gf.get_grid_size(0),
381  gf.get_grid_size(1),
382  gf.get_grid_size(2),
383  gf.get_grid_size(3));
384  }
385 
387 
388  void resize(Index b, Index p, Index r, Index c) { data.resize(b, p, r, c); }
389 
390  friend std::ostream& operator<<(std::ostream& os, const GriddedField4& gf);
391 
393 };
394 
395 class GriddedField5 final : public GriddedField {
396  public:
400 
401  GriddedField5(const String& s) : GriddedField(5, s) {}
402 
403  bool checksize() const final {
404  return ((!get_grid_size(4) && data.ncols() == 1) ||
405  data.ncols() == get_grid_size(4)) &&
406  ((!get_grid_size(3) && data.nrows() == 1) ||
407  data.nrows() == get_grid_size(3)) &&
408  ((!get_grid_size(2) && data.npages() == 1) ||
409  data.npages() == get_grid_size(2)) &&
410  ((!get_grid_size(1) && data.nbooks() == 1) ||
411  data.nbooks() == get_grid_size(1)) &&
412  ((!get_grid_size(0) && data.nshelves() == 1) ||
413  data.nshelves() == get_grid_size(0));
414  }
415 
416  void checksize_strict() const final {
417  if (!checksize()) {
419  os << "GriddedField5 ";
420  if (get_name().length()) os << "(" << get_name() << ") ";
422  for (Index i = 0; i < 5; i++) {
423  os << "Grid " << i;
424  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
425  os << " = " << get_grid_size(i) << "\n";
426  }
427  os << "Data";
428  os << " = " << data.nshelves() << ", " << data.nbooks() << ", ";
429  os << data.npages() << ", " << data.nrows() << ", " << data.ncols();
430  throw std::runtime_error(os.str());
431  }
432  }
433 
435 
436  void resize(const GriddedField5& gf) {
437  data.resize(gf.get_grid_size(0),
438  gf.get_grid_size(1),
439  gf.get_grid_size(2),
440  gf.get_grid_size(3),
441  gf.get_grid_size(4));
442  }
443 
445 
446  void resize(Index s, Index b, Index p, Index r, Index c) {
447  data.resize(s, b, p, r, c);
448  }
449 
450  friend std::ostream& operator<<(std::ostream& os, const GriddedField5& gf);
451 
453 };
454 
455 class GriddedField6 final : public GriddedField {
456  public:
460 
461  GriddedField6(const String& s) : GriddedField(6, s) {}
462 
463  bool checksize() const final {
464  return ((!get_grid_size(5) && data.ncols() == 1) ||
465  data.ncols() == get_grid_size(5)) &&
466  ((!get_grid_size(4) && data.nrows() == 1) ||
467  data.nrows() == get_grid_size(4)) &&
468  ((!get_grid_size(3) && data.npages() == 1) ||
469  data.npages() == get_grid_size(3)) &&
470  ((!get_grid_size(2) && data.nbooks() == 1) ||
471  data.nbooks() == get_grid_size(2)) &&
472  ((!get_grid_size(1) && data.nshelves() == 1) ||
473  data.nshelves() == get_grid_size(1)) &&
474  ((!get_grid_size(0) && data.nvitrines() == 1) ||
475  data.nvitrines() == get_grid_size(0));
476  }
477 
478  void checksize_strict() const final {
479  if (!checksize()) {
481  os << "GriddedField6 ";
482  if (get_name().length()) os << "(" << get_name() << ") ";
484  for (Index i = 0; i < 5; i++) {
485  os << "Grid " << i;
486  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
487  os << " = " << get_grid_size(i) << "\n";
488  }
489  os << "Data";
490  os << " = " << data.nvitrines() << data.nshelves() << ", "
491  << data.nbooks() << ", ";
492  os << data.npages() << ", " << data.nrows() << ", " << data.ncols();
493  throw std::runtime_error(os.str());
494  }
495  }
496 
498 
499  void resize(const GriddedField6& gf) {
500  data.resize(gf.get_grid_size(0),
501  gf.get_grid_size(1),
502  gf.get_grid_size(2),
503  gf.get_grid_size(3),
504  gf.get_grid_size(4),
505  gf.get_grid_size(5));
506  }
507 
509 
510  void resize(Index v, Index s, Index b, Index p, Index r, Index c) {
511  data.resize(v, s, b, p, r, c);
512  }
513 
514  friend std::ostream& operator<<(std::ostream& os, const GriddedField6& gf);
515 
517 };
518 
519 /********** Output operators **********/
520 
521 std::ostream& operator<<(std::ostream& os, const GriddedField& gf);
522 std::ostream& operator<<(std::ostream& os, const GriddedField1& gf);
523 std::ostream& operator<<(std::ostream& os, const GriddedField2& gf);
524 std::ostream& operator<<(std::ostream& os, const GriddedField3& gf);
525 std::ostream& operator<<(std::ostream& os, const GriddedField4& gf);
526 std::ostream& operator<<(std::ostream& os, const GriddedField5& gf);
527 std::ostream& operator<<(std::ostream& os, const GriddedField6& gf);
528 
529 /************ Array types *************/
530 
539 
540 #undef CHECK_ERROR_BOILERPLATE
541 
542 #endif
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
GriddedField5()
Construct an empty GriddedField5.
void checksize_strict() const final
Strict consistency check.
Index nelem() const
Number of elements.
Definition: array.h:195
virtual bool checksize() const =0
Consistency check.
Array< GriddedField3 > ArrayOfGriddedField3
void resize(const GriddedField1 &gf)
Make this GriddedField1 the same size as the given one.
GriddedField(const Index d, const String &s)
Construct a GriddedField.
Array< GriddedField2 > ArrayOfGriddedField2
#define CHECK_ERROR_BOILERPLATE
bool checksize() const final
Consistency check.
The Vector class.
Definition: matpackI.h:860
const ArrayOfString & get_string_grid(Index i) const
Get a string grid.
virtual void checksize_strict() const =0
Strict consistency check.
void resize(const GriddedField6 &gf)
Make this GriddedField6 the same size as the given one.
The Tensor4 class.
Definition: matpackIV.h:421
GriddedField1()
Construct an empty GriddedField1.
GriddedField()
Construct an empty GriddedField.
GridType get_grid_type(Index i) const
Get grid type.
void checksize_strict() const final
Strict consistency check.
void checksize_strict() const final
Strict consistency check.
Array< GriddedField4 > ArrayOfGriddedField4
ArrayOfString mgridnames
void resize(const GriddedField4 &gf)
Make this GriddedField4 the same size as the given one.
G0 G2 FVC Y DV Numeric Numeric Numeric Zeeman LowerQuantumNumbers void * data
bool checksize() const final
Consistency check.
void checksize_strict() const final
Strict consistency check.
Array< GriddedField1 > ArrayOfGriddedField1
bool checksize() const final
Consistency check.
void copy_grids(const GriddedField &gf)
Copy grids.
This file contains the definition of Array.
GriddedField6()
Construct an empty GriddedField6.
const String & get_name() const
Get the name of this gridded field.
Index get_dim() const
Get the dimension of this gridded field.
GriddedField2()
Construct an empty GriddedField2.
The Tensor3 class.
Definition: matpackIII.h:339
GriddedField5(const String &s)
Construct an empty GriddedField5 with the given name.
void checksize_strict() const final
Strict consistency check.
void resize(Index r, Index c)
Resize the data matrix.
GriddedField3(const String &s)
Construct an empty GriddedField3 with the given name.
Array< GridType > ArrayOfGridType
GriddedField3()
Construct an empty GriddedField3.
ArrayOfVector mnumericgrids
GriddedField4()
Construct an empty GriddedField4.
void resize(Index v, Index s, Index b, Index p, Index r, Index c)
Resize the data tensor.
void set_grid(Index i, const Vector &g)
Set a numeric grid.
Index get_grid_size(Index i) const
Get the size of a grid.
GriddedField3 & operator=(Numeric n)
Array< ArrayOfString > mstringgrids
bool checksize() const final
Consistency check.
void resize(const GriddedField2 &gf)
Make this GriddedField2 the same size as the given one.
const Vector & get_numeric_grid(Index i) const
Get a numeric grid.
GridType
void resize(Index b, Index p, Index r, Index c)
Resize the data tensor.
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
The Matrix class.
Definition: matpackI.h:1193
GriddedField6(const String &s)
Construct an empty GriddedField6 with the given name.
friend std::ostream & operator<<(std::ostream &os, const GriddedField &gf)
GriddedField2(const String &s)
Construct an empty GriddedField2 with the given name.
GriddedField4(const String &s)
Construct an empty GriddedField4 with the given name.
basic_ostringstream< char, string_char_traits< char >, alloc > ostringstream
Definition: sstream.h:204
void resize(Index p, Index r, Index c)
Resize the data tensor.
void resize(Index n)
Resize the data vector.
const String & get_grid_name(Index i) const
Get grid name.
void checksize_strict() const final
Strict consistency check.
The Tensor6 class.
Definition: matpackVI.h:1088
void resize(Index s, Index b, Index p, Index r, Index c)
Resize the data tensor.
void set_name(const String &s)
Set name of this gridded field.
Index nelem(const Lines &l)
Number of lines.
GriddedField1(const String &s)
Construct an empty GriddedField1 with the given name.
void set_grid_name(Index i, const String &s)
Set grid name.
void resize(const GriddedField5 &gf)
Make this GriddedField5 the same size as the given one.
Array< GriddedField5 > ArrayOfGriddedField5
bool checksize() const final
Consistency check.
Array< Array< GriddedField3 > > ArrayOfArrayOfGriddedField3
void resize(const GriddedField3 &gf)
Make this GriddedField3 the same size as the given one.
virtual ~GriddedField()
GriddedField virtual destructor.
bool checksize() const final
Consistency check.
Array< GridType > mgridtypes
Array< Array< GriddedField1 > > ArrayOfArrayOfGriddedField1
Array< Array< GriddedField2 > > ArrayOfArrayOfGriddedField2
The Tensor5 class.
Definition: matpackV.h:506
This file contains the definition of String, the ARTS string class.