ARTS  2.2.66
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 "matpackVI.h"
38 #include "array.h"
39 #include "mystring.h"
40 
42 enum GridType {
45 };
46 
48 
49 #define CHECK_ERROR_BOILERPLATE \
50  "size mismatch between grids and data.\n" \
51  "Note that a grid is allowed to be empty, but in the\n" \
52  "data that dimension must have exactly one element.\n"
53 
55 class GriddedField {
56 private:
63 
64 protected:
66 
70  GriddedField() : dim(0),
71  mname(),
72  mgridtypes(),
73  mgridnames(),
74  mstringgrids(),
75  mnumericgrids()
76  { /* Nothing to do here */ }
77 
79 
88  GriddedField(const Index d, const String& s) : dim(d),
89  mname(s),
90  mgridtypes(d, GRID_TYPE_NUMERIC),
91  mgridnames(d),
92  mstringgrids(d),
93  mnumericgrids(d)
94  { /* Nothing to do here */ }
95 
96 
97 public:
99 
100  Index get_dim() const { return dim; }
101 
102  void copy_grids(const GriddedField& gf);
103 
105 
111  const String& get_grid_name(Index i) const { return mgridnames[i]; }
112 
114 
121  {
122  Index ret = 0;
123  assert(i < dim);
124  switch (mgridtypes[i])
125  {
126  case GRID_TYPE_NUMERIC:
127  ret = mnumericgrids[i].nelem(); break;
128  case GRID_TYPE_STRING: ret = mstringgrids[i].nelem(); break;
129  }
130 
131  return ret;
132  }
133 
135 
141  GridType get_grid_type(Index i) const { return mgridtypes[i]; }
142 
144 
146 
147  const ArrayOfString& get_string_grid(Index i) const;
148 
150 
152 
153  const String& get_name() const { return mname; }
154 
155  void set_grid(Index i, const Vector& g);
156 
157  void set_grid(Index i, const ArrayOfString& g);
158 
160 
166  void set_grid_name(Index i, const String& s)
167  {
168  assert(i < dim);
169  mgridnames[i] = s;
170  }
171 
173 
174  void set_name(const String& s) { mname = s; }
175 
177 
184  virtual bool checksize() const = 0;
185 
187 
190  virtual void checksize_strict() const = 0;
191 
193  virtual ~GriddedField() {}
194 
195  friend std::ostream& operator<<(std::ostream& os, const GriddedField& gf);
196 };
197 
198 
199 class GriddedField1 : public GriddedField {
200 public:
204 
205  GriddedField1(const String& s) : GriddedField(1, s) {}
206 
207  virtual bool checksize() const
208  {
209  return (!get_grid_size(0) && data.nelem() == 1)
210  || data.nelem() == get_grid_size(0);
211  }
212 
213  virtual void checksize_strict() const
214  {
215  if (!checksize())
216  {
217  std::ostringstream os;
218  os << "GriddedField1 ";
219  if (get_name().length()) os << "(" << get_name() << ") ";
221  os << "Grid";
222  if (get_grid_name(0).nelem()) os << " (" << get_grid_name(0) << ")";
223  os << " = " << get_grid_size(0) << "\n";
224  os << "Data";
225  os << " = " << data.nelem();
226  throw std::runtime_error(os.str());
227  }
228  }
229 
231 
232  void resize(const GriddedField1& gf)
233  {
234  data.resize(gf.get_grid_size(0));
235  }
236 
238 
239  void resize(Index n)
240  {
241  data.resize(n);
242  }
243 
244  friend std::ostream& operator<<(std::ostream& os, const GriddedField1& gf);
245 
247 };
248 
249 
250 class GriddedField2 : public GriddedField {
251 public:
255 
256  GriddedField2(const String& s) : GriddedField(2, s) {}
257 
258  virtual bool checksize() const
259  {
260  return ((!get_grid_size(1) && data.ncols() == 1)
261  || data.ncols() == get_grid_size(1))
262  && ((!get_grid_size(0) && data.nrows() == 1)
263  || data.nrows() == get_grid_size(0));
264  }
265 
266  virtual void checksize_strict() const
267  {
268  if (!checksize())
269  {
270  std::ostringstream os;
271  os << "GriddedField2 ";
272  if (get_name().length()) os << "(" << get_name() << ") ";
274  for (Index i = 0; i < 2; i++)
275  {
276  os << "Grid " << i;
277  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
278  os << " = " << get_grid_size(i) << "\n";
279  }
280  os << "Data";
281  os << " = " << data.nrows() << ", " << data.ncols();
282  throw std::runtime_error(os.str());
283  }
284  }
285 
287 
288  void resize(const GriddedField2& gf)
289  {
290  data.resize(gf.get_grid_size(0),
291  gf.get_grid_size(1));
292  }
293 
295 
296  void resize(Index r, Index c)
297  {
298  data.resize(r, c);
299  }
300 
301  friend std::ostream& operator<<(std::ostream& os, const GriddedField2& gf);
302 
304 };
305 
306 
307 class GriddedField3 : public GriddedField {
308 public:
312 
313  GriddedField3(const String& s) : GriddedField(3, s) {}
314 
316  {
317  data = n;
318 
319  return *this;
320  }
321 
322  virtual bool checksize() const
323  {
324  return ((!get_grid_size(2) && data.ncols() == 1)
325  || data.ncols() == get_grid_size(2))
326  && ((!get_grid_size(1) && data.nrows() == 1)
327  || data.nrows() == get_grid_size(1))
328  && ((!get_grid_size(0) && data.npages() == 1)
329  || data.npages() == get_grid_size(0));
330  }
331 
332  virtual void checksize_strict() const
333  {
334  if (!checksize())
335  {
336  std::ostringstream os;
337  os << "GriddedField3 ";
338  if (get_name().length()) os << "(" << get_name() << ") ";
340  for (Index i = 0; i < 3; i++)
341  {
342  os << "Grid " << i;
343  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
344  os << " = " << get_grid_size(i) << "\n";
345  }
346  os << "Data";
347  os << " = " << data.npages() << ", " << data.nrows() << ", " << data.ncols();
348  throw std::runtime_error(os.str());
349  }
350  }
351 
353 
354  void resize(const GriddedField3& gf)
355  {
356  data.resize(gf.get_grid_size(0),
357  gf.get_grid_size(1),
358  gf.get_grid_size(2));
359  }
360 
362 
363  void resize(Index p, Index r, Index c)
364  {
365  data.resize(p, r, c);
366  }
367 
368  friend std::ostream& operator<<(std::ostream& os, const GriddedField3& gf);
369 
371 };
372 
373 
374 class GriddedField4 : public GriddedField {
375 public:
379 
380  GriddedField4(const String& s) : GriddedField(4, s) {}
381 
382  virtual bool checksize() const
383  {
384  return ((!get_grid_size(3) && data.ncols() == 1)
385  || data.ncols() == get_grid_size(3))
386  && ((!get_grid_size(2) && data.nrows() == 1)
387  || data.nrows() == get_grid_size(2))
388  && ((!get_grid_size(1) && data.npages() == 1)
389  || data.npages() == get_grid_size(1))
390  && ((!get_grid_size(0) && data.nbooks() == 1)
391  || data.nbooks() == get_grid_size(0));
392  }
393 
394  virtual void checksize_strict() const
395  {
396  if (!checksize())
397  {
398  std::ostringstream os;
399  os << "GriddedField4 ";
400  if (get_name().length()) os << "(" << get_name() << ") ";
402  for (Index i = 0; i < 4; i++)
403  {
404  os << "Grid " << i;
405  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
406  os << " = " << get_grid_size(i) << "\n";
407  }
408  os << "Data";
409  os << " = " << data.nbooks() << ", " << data.npages() << ", ";
410  os << data.nrows() << ", " << data.ncols();
411  throw std::runtime_error(os.str());
412  }
413  }
414 
416 
417  void resize(const GriddedField4& gf)
418  {
419  data.resize(gf.get_grid_size(0),
420  gf.get_grid_size(1),
421  gf.get_grid_size(2),
422  gf.get_grid_size(3));
423  }
424 
426 
427  void resize(Index b, Index p, Index r, Index c)
428  {
429  data.resize(b, p, r, c);
430  }
431 
432  friend std::ostream& operator<<(std::ostream& os, const GriddedField4& gf);
433 
435 };
436 
437 
438 class GriddedField5 : public GriddedField {
439 public:
443 
444  GriddedField5(const String& s) : GriddedField(5, s) {}
445 
446  virtual bool checksize() const
447  {
448  return ((!get_grid_size(4) && data.ncols() == 1)
449  || data.ncols() == get_grid_size(4))
450  && ((!get_grid_size(3) && data.nrows() == 1)
451  || data.nrows() == get_grid_size(3))
452  && ((!get_grid_size(2) && data.npages() == 1)
453  || data.npages() == get_grid_size(2))
454  && ((!get_grid_size(1) && data.nbooks() == 1)
455  || data.nbooks() == get_grid_size(1))
456  && ((!get_grid_size(0) && data.nshelves() == 1)
457  || data.nshelves() == get_grid_size(0));
458  }
459 
460  virtual void checksize_strict() const
461  {
462  if (!checksize())
463  {
464  std::ostringstream os;
465  os << "GriddedField5 ";
466  if (get_name().length()) os << "(" << get_name() << ") ";
468  for (Index i = 0; i < 5; i++)
469  {
470  os << "Grid " << i;
471  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
472  os << " = " << get_grid_size(i) << "\n";
473  }
474  os << "Data";
475  os << " = " << data.nshelves() << ", " << data.nbooks() << ", ";
476  os << data.npages() << ", " << data.nrows() << ", " << data.ncols();
477  throw std::runtime_error(os.str());
478  }
479  }
480 
482 
483  void resize(const GriddedField5& gf)
484  {
485  data.resize(gf.get_grid_size(0),
486  gf.get_grid_size(1),
487  gf.get_grid_size(2),
488  gf.get_grid_size(3),
489  gf.get_grid_size(4));
490  }
491 
493 
494  void resize(Index s, Index b, Index p, Index r, Index c)
495  {
496  data.resize(s, b, p, r, c);
497  }
498 
499  friend std::ostream& operator<<(std::ostream& os, const GriddedField5& gf);
500 
502 };
503 
504 
505 class GriddedField6 : public GriddedField {
506 public:
510 
511  GriddedField6(const String& s) : GriddedField(6, s) {}
512 
513  virtual bool checksize() const
514  {
515  return ((!get_grid_size(5) && data.ncols() == 1)
516  || data.ncols() == get_grid_size(5))
517  && ((!get_grid_size(4) && data.nrows() == 1)
518  || data.nrows() == get_grid_size(4))
519  && ((!get_grid_size(3) && data.npages() == 1)
520  || data.npages() == get_grid_size(3))
521  && ((!get_grid_size(2) && data.nbooks() == 1)
522  || data.nbooks() == get_grid_size(2))
523  && ((!get_grid_size(1) && data.nshelves() == 1)
524  || data.nshelves() == get_grid_size(1))
525  && ((!get_grid_size(0) && data.nvitrines() == 1)
526  || data.nvitrines() == get_grid_size(0));
527  }
528 
529  virtual void checksize_strict() const
530  {
531  if (!checksize())
532  {
533  std::ostringstream os;
534  os << "GriddedField6 ";
535  if (get_name().length()) os << "(" << get_name() << ") ";
537  for (Index i = 0; i < 5; i++)
538  {
539  os << "Grid " << i;
540  if (get_grid_name(i).nelem()) os << " (" << get_grid_name(i) << ")";
541  os << " = " << get_grid_size(i) << "\n";
542  }
543  os << "Data";
544  os << " = " << data.nvitrines() << data.nshelves() << ", " << data.nbooks() << ", ";
545  os << data.npages() << ", " << data.nrows() << ", " << data.ncols();
546  throw std::runtime_error(os.str());
547  }
548  }
549 
551 
552  void resize(const GriddedField6& gf)
553  {
554  data.resize(gf.get_grid_size(0),
555  gf.get_grid_size(1),
556  gf.get_grid_size(2),
557  gf.get_grid_size(3),
558  gf.get_grid_size(4),
559  gf.get_grid_size(5));
560  }
561 
563 
564  void resize(Index v, Index s, Index b, Index p, Index r, Index c)
565  {
566  data.resize(v, s, b, p, r, c);
567  }
568 
569  friend std::ostream& operator<<(std::ostream& os, const GriddedField6& gf);
570 
572 };
573 
574 
575 
576 /********** Output operators **********/
577 
578 std::ostream& operator<<(std::ostream& os, const GriddedField& gf);
579 std::ostream& operator<<(std::ostream& os, const GriddedField1& gf);
580 std::ostream& operator<<(std::ostream& os, const GriddedField2& gf);
581 std::ostream& operator<<(std::ostream& os, const GriddedField3& gf);
582 std::ostream& operator<<(std::ostream& os, const GriddedField4& gf);
583 std::ostream& operator<<(std::ostream& os, const GriddedField5& gf);
584 std::ostream& operator<<(std::ostream& os, const GriddedField6& gf);
585 
586 /************ Array types *************/
587 
595 
596 #undef CHECK_ERROR_BOILERPLATE
597 
598 #endif
599 
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
GriddedField5()
Construct an empty GriddedField5.
The VectorView class.
Definition: matpackI.h:372
Index nelem() const
Number of elements.
Definition: array.h:176
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.
virtual void checksize_strict() const
Strict consistency check.
Array< GriddedField2 > ArrayOfGriddedField2
#define CHECK_ERROR_BOILERPLATE
ConstVectorView get_numeric_grid(Index i) const
Get a numeric grid.
The Vector class.
Definition: matpackI.h:556
const ArrayOfString & get_string_grid(Index i) const
Get a string grid.
virtual void checksize_strict() const =0
Strict consistency check.
virtual void checksize_strict() const
Strict consistency check.
void resize(const GriddedField6 &gf)
Make this GriddedField6 the same size as the given one.
The Tensor4 class.
Definition: matpackIV.h:383
virtual bool checksize() const
Consistency check.
GriddedField1()
Construct an empty GriddedField1.
GriddedField()
Construct an empty GriddedField.
GridType get_grid_type(Index i) const
Get grid type.
virtual bool checksize() const
Consistency check.
Array< Array< GriddedField2 > > ArrayOfArrayOfGriddedField2
Array< GriddedField4 > ArrayOfGriddedField4
ArrayOfString mgridnames
void resize(const GriddedField4 &gf)
Make this GriddedField4 the same size as the given one.
Array< GriddedField1 > ArrayOfGriddedField1
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.
virtual bool checksize() const
Consistency check.
The Tensor3 class.
Definition: matpackIII.h:348
virtual bool checksize() const
Consistency check.
GriddedField5(const String &s)
Construct an empty GriddedField5 with the given name.
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
void resize(const GriddedField2 &gf)
Make this GriddedField2 the same size as the given one.
virtual void checksize_strict() const
Strict consistency check.
virtual void checksize_strict() const
Strict consistency check.
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:29
The Matrix class.
Definition: matpackI.h:788
GriddedField6(const String &s)
Construct an empty GriddedField6 with the given name.
friend std::ostream & operator<<(std::ostream &os, const GriddedField &gf)
Array< Array< GriddedField3 > > ArrayOfArrayOfGriddedField3
virtual bool checksize() const
Consistency check.
GriddedField2(const String &s)
Construct an empty GriddedField2 with the given name.
GriddedField4(const String &s)
Construct an empty GriddedField4 with the given name.
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.
The Tensor6 class.
Definition: matpackVI.h:950
void resize(Index s, Index b, Index p, Index r, Index c)
Resize the data tensor.
A constant view of a Vector.
Definition: matpackI.h:292
void set_name(const String &s)
Set name of this gridded field.
GriddedField1(const String &s)
Construct an empty GriddedField1 with the given name.
void set_grid_name(Index i, const String &s)
Set grid name.
Array< Array< GriddedField1 > > ArrayOfArrayOfGriddedField1
virtual void checksize_strict() const
Strict consistency check.
void resize(const GriddedField5 &gf)
Make this GriddedField5 the same size as the given one.
virtual void checksize_strict() const
Strict consistency check.
virtual bool checksize() const
Consistency check.
void resize(const GriddedField3 &gf)
Make this GriddedField3 the same size as the given one.
virtual ~GriddedField()
GriddedField virtual destructor.
Array< GridType > mgridtypes
The Tensor5 class.
Definition: matpackV.h:451
This file contains the definition of String, the ARTS string class.