ARTS  1.0.222
m_io.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000, 2001 Stefan Buehler <sbuehler@uni-bremen.de>
2  Patrick Eriksson <patrick@rss.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 
19 
20 
22 // File description
24 
40 // External declarations
43 
44 #include <math.h>
45 #include "arts.h"
46 #include "atm_funcs.h"
47 #include "file.h"
48 #include "math_funcs.h"
49 #include "messages.h"
50 #include "auto_md.h"
51 #include "make_array.h"
52 
53 #include "complex.h"
54 
55 
56 //**************************************************************************
57 //
58 // 1. Overall ARTS functions
59 //
60 //**************************************************************************
61 
68 void Echo(const String& message,
69  const Index& output_level)
70 {
71  ostringstream os;
72  os << message << '\n';
73 
74  switch (output_level)
75  {
76  case 0: out0 << os.str (); break;
77  case 1: out1 << os.str (); break;
78  case 2: out2 << os.str (); break;
79  case 3: out3 << os.str (); break;
80  default:
81  throw runtime_error ("Output level must have value from 0-3");
82  }
83 
84 }
85 
86 
93 void Exit()
94 {
95  out1 << " Forced exit.\n";
96  exit(0);
97 }
98 
99 
100 
107 extern const Numeric DEG2RAD;
108 
109 void Test( )
110 {
111  /*
112  // This function can be used to test stuff.
113 
114  const Numeric f = 100e9;
115  const Numeric t_ground=300;
116  const Numeric th=30;
117  const String pol = "v";
118 
119  const Numeric theta = 1 - 300 / t_ground;
120  const Numeric e0 = 77.66 - 103.3 * theta;
121  const Numeric e1 = 0.0671 * e0;
122  const Numeric f1 = 20.2 + 146.4 * theta + 316 * theta * theta;
123  const Numeric e2 = 3.52;
124  const Numeric f2 = 39.8 * f1;
125  //
126  const Numeric n1 = 1;
127 
128  const Numeric sintheta = sin( DEG2RAD*th );
129  const Numeric costheta = sqrt( 1 - sintheta*sintheta );
130 
131  const Complex ifGHz( 0.0, f/1e9 );
132 
133  const Complex eps = e2 + (e1-e2) / (1.0-ifGHz/f2) +
134  (e0-e1) / (1.0-ifGHz/f1);
135  const Complex n2 = sqrt( eps );
136  Complex a,b;
137 
138  if( pol == "v" )
139  {
140  a = n2 * costheta;
141  b = n1 * cos( asin( n1 * sintheta / n2.real() ) );
142  }
143  else if( pol == "h" )
144  {
145  a = n1 * costheta;
146  b = n2 * cos( asin( n1 * sintheta / n2.real() ) );
147  }
148  else
149  throw runtime_error(
150  "The keyword argument *pol* must be \"v\" or \"h\"." );
151 
152  // Power reflection coefficient
153  const Numeric r = pow( abs( ( a - b ) / ( a + b ) ), 2.0 );
154 
155  Numeric e = 1 - r;
156 
157  cout << "r = " << r << "\n";
158  cout << "e = " << e << "\n";
159  */
160 }
161 
162 
163 
164 
165 //**************************************************************************
166 //
167 // 2. ASCII file functions
168 //
169 //**************************************************************************
170 
172 // File workspace methods (sorted after workspace variable)
174 
175 // The ASCII functions are created by Stefan Buehler
176 
177 
178 //=== Index ============================================================
179 
180 // This function shall be modified to handle Index
182  const Index& v,
183  const String& v_name,
184  const String& f )
185 {
186  String filename = f;
187 
188  // Create default filename if empty
189  filename_ascii( filename, v_name );
190 
191  // Store the value in an ArrayOfMatrix and write to file
192  ArrayOfMatrix am(1);
193  am[0].resize( 1, 1 );
194  am[0] = static_cast<Numeric>(v); // Matpack can set all elements like this.
195  write_array_of_matrix_to_file(filename,am);
196 }
197 
198 
199 // This function shall be modified to handle Index
201  Index& v,
202  const String& v_name,
203  const String& f )
204 {
205  String filename = f;
206 
207  // Create default filename if empty
208  filename_ascii( filename, v_name );
209 
210  // Read the value as ArrayOfMatrix and move to Numeric
211  ArrayOfMatrix am;
212  read_array_of_matrix_from_file(am,filename);
213  if ( (am.nelem()!=1) || (am[0].nrows()!=1) || (am[0].ncols()!=1) )
214  {
215  ostringstream os;
216  os << "The file " << filename << " contains not a single value.";
217  throw runtime_error(os.str());
218  }
219 
220  Numeric a = am[0](0,0);
221 
222  if ( (a-floor(a)) != 0 )
223  throw runtime_error("The value in the file is not an integer.");
224  if ( a < 0 )
225  throw runtime_error("The value in the file is negative.");
226 
227  v = (int) a;
228 }
229 
230 
231 
232 //=== NUMERIC ==========================================================
233 
235  const Numeric& v,
236  const String& v_name,
237  const String& f )
238 {
239  String filename = f;
240 
241  // Create default filename if empty
242  filename_ascii( filename, v_name );
243 
244  // Store the value in an ArrayOfMatrix and write to file
245  ArrayOfMatrix am(1);
246  am[0].resize( 1, 1 );
247  am[0] = v; // Matpack can set all elements like this.
248  write_array_of_matrix_to_file(filename,am);
249 }
250 
251 
253  Numeric& v,
254  const String& v_name,
255  const String& f )
256 {
257  String filename = f;
258 
259  // Create default filename if empty
260  filename_ascii( filename, v_name );
261 
262  // Read the value as ArrayOfMatrix and move to Numeric
263  ArrayOfMatrix am;
264  read_array_of_matrix_from_file(am,filename);
265  if ( (am.nelem()!=1) || (am[0].nrows()!=1) || (am[0].ncols()!=1) )
266  {
267  ostringstream os;
268  os << "The file " << filename << " contains not a single numeric value";
269  throw runtime_error(os.str());
270  }
271 
272  v = am[0](0,0);
273 }
274 
275 
276 
277 //=== Vector ==========================================================
278 
279 void VectorWriteAscii(// WS Output:
280  const Vector& v,
281  // WS Variable Names:
282  const String& v_name,
283  // Control Parameters:
284  const String& f)
285 {
286  String filename = f;
287 
288  // Create default filename if empty
289  filename_ascii( filename, v_name );
290 
291  // Convert the vector to a 1-column matrix:
292  Matrix m(v);
293  // The static_cast to const Vector here is necessary to suppress a
294  // warning message from the compiler about different possible
295  // conversion paths.
296 
297  // Convert the matrix to an array of matrix:
298  MakeArray<Matrix> am(m);
299  // MakeArray is the special kind of array with explicit
300  // initialization. Here, the generated array has only 1 element,
301  // which is initialized from Matrix m.
302 
303  // Write the array of matrix to the file.
304  write_array_of_matrix_to_file(filename,am);
305 }
306 
307 
308 
309 void VectorReadAscii(// WS Generic Output:
310  Vector& v,
311  // WS Generic Output Names:
312  const String& v_name,
313  // Control Parameters:
314  const String& f)
315 {
316  String filename = f;
317 
318  // Create default filename if empty
319  filename_ascii( filename, v_name );
320 
321  // Read an array of matrix from the file:
322  ArrayOfMatrix am;
323  read_array_of_matrix_from_file(am,filename);
324 
325  // Convert the array of matrix to a matrix.
326  if ( 1 != am.nelem() )
327  throw runtime_error("You tried to convert an array of matrix to a matrix,\n"
328  "but the dimension of the array is not 1.");
329  Matrix m(am[0]);
330 
331  // Check that this really is a 1-column matrix:
332  if ( 1 != m.ncols() )
333  throw runtime_error("You tried to convert a matrix to a vector,\n"
334  "but it has more than one column.");
335 
336  // Convert the 1-column matrix to a vector:
337  v.resize(m.nrows());
338  v = m(Range(joker),0);
339  // (The m(Range(joker),0) picks out first column of m, = operator copies
340  // to v.)
341 }
342 
343 
344 
345 //=== Matrix ==========================================================
346 
347 void MatrixWriteAscii(// WS Generic Input:
348  const Matrix& m,
349  // WS Generic Input Names:
350  const String& m_name,
351  // Control Parameters:
352  const String& f)
353 {
354  String filename = f;
355 
356  // Create default filename if empty
357  filename_ascii( filename, m_name );
358 
359  // Convert the matrix to an array of matrix:
360  MakeArray<Matrix> am(m);
361  // MakeArray is the special kind of array with explicit
362  // initialization. Here, the generated array has only 1 element,
363  // which is initialized from Matrix m.
364 
365  // Write the array of matrix to the file.
366  write_array_of_matrix_to_file(filename,am);
367 }
368 
369 
370 
371 void MatrixReadAscii(// WS Generic Output:
372  Matrix& m,
373  // WS Generic Output Names:
374  const String& m_name,
375  // Control Parameters:
376  const String& f)
377 {
378  String filename = f;
379 
380  // Create default filename if empty
381  filename_ascii( filename, m_name );
382 
383  // Read the array of matrix from the file:
384  ArrayOfMatrix am;
385  read_array_of_matrix_from_file(am,filename);
386 
387  // cout << "am.nelem(): " << am.nelem() << "\n";
388 
389  // Convert the array of matrix to a matrix.
390  if ( 1 != am.nelem() )
391  throw runtime_error("You tried to convert an array of matrix to a matrix,\n"
392  "but the dimension of the array is not 1.");
393 
394  m.resize( am[0].nrows(), am[0].ncols() );
395  m = am[0];
396 }
397 
398 
399 
400 //=== ArrayOfIndex =====================================================
401 
402 // This function shall be modified to handle ArrayOfIndex
404  const ArrayOfIndex& v,
405  const String& v_name,
406  const String& f )
407 {
408  String filename = f;
409 
410  // Create default filename if empty
411  filename_ascii( filename, v_name );
412 
413  // Store the value in an ArrayOfMatrix and write to file
414  const Index n = v.nelem();
415  ArrayOfMatrix am(1);
416  am[0].resize(n,1);
417  for ( Index i=0; i<n; i++ )
418  am[0](i,0) = static_cast<Numeric>(v[i]);
419  write_array_of_matrix_to_file(filename,am);
420 }
421 
422 
423 // This function shall be modified to handle Index
425  ArrayOfIndex& v,
426  const String& v_name,
427  const String& f )
428 {
429  // FIXME: This function is crap. Put the whole ASCII file stuff
430  // should be changed in the future, so I leave it for now.
431 
432  String filename = f;
433 
434  // Create default filename if empty
435  filename_ascii( filename, v_name );
436 
437  // Read the value as ArrayOfMatrix
438  ArrayOfMatrix am;
439  read_array_of_matrix_from_file(am,filename);
440  if ( (am.nelem()!=1) )
441  {
442  ostringstream os;
443  os << "The file " << filename << " contains more than one vector.";
444  throw runtime_error(os.str());
445  }
446 
447  Matrix m(am[0]); // Create Matrix and initialize from
448  // first element of am.
449 
450  // Check that this really is a 1-column matrix:
451  if ( 1 != m.ncols() )
452  throw runtime_error("You tried to convert a matrix to a vector,\n"
453  "but it has more than one column.");
454 
455  // Convert the 1-column matrix to a vector:
456  Vector x(m(Range(joker),0));
457  // (The m(Range(joker),0) picks out first column of m. Matpack can
458  // initialize the new vector x from that.
459 
460  const Index n = x.nelem();
461  v.resize(n);
462  for ( Index i=0; i<n; i++ )
463  {
464  if ( (x[i]-floor(x[i])) != 0 )
465  throw runtime_error("A value in the file is not an integer.");
466  if ( x[i] < 0 )
467  throw runtime_error("A value in the file is negative.");
468  v[i] = (Index) x[i];
469  }
470 }
471 
472 
473 
474 //=== ArrayOfVector ====================================================
475 
476 void ArrayOfVectorWriteAscii(// WS Output:
477  const ArrayOfVector& av,
478  // WS Variable Names:
479  const String& av_name,
480  // Control Parameters:
481  const String& f)
482 {
483  String filename = f;
484 
485  // Create default filename if empty
486  filename_ascii( filename, av_name );
487 
488  // Convert the array of vector to an array of matrix:
489  ArrayOfMatrix am(av.nelem());
490  for (Index i=0; i<av.nelem(); ++i)
491  {
492  // to_matrix(am[i],av[i]);
493  am[i].resize( av[i].nelem(), 1 );
494  am[i] = av[i]; // Matpack can copy the content of a
495  // Vector to a 1-column Matrix.
496  }
497 
498  // Write the array of matrix to the file.
499  write_array_of_matrix_to_file(filename,am);
500 }
501 
502 
503 
504 void ArrayOfVectorReadAscii(// WS Generic Output:
505  ArrayOfVector& av,
506  // WS Generic Output Names:
507  const String& av_name,
508  // Control Parameters:
509  const String& f)
510 {
511  String filename = f;
512 
513  // Create default filename if empty
514  filename_ascii( filename, av_name );
515 
516  // Read an array of matrix from the file:
517  ArrayOfMatrix am;
518  read_array_of_matrix_from_file(am,filename);
519 
520  // Convert the array of matrix to an array of vector.
521  av.resize(am.nelem());
522  for (Index i=0; i<am.nelem(); ++i)
523  {
524  // Check that this really is a 1-column matrix:
525  if ( 1 != am[i].ncols() )
526  throw runtime_error("You tried to convert a matrix to a vector,\n"
527  "but it has more than one column.");
528 
529  // Convert the 1-column matrix to a vector:
530  av[i].resize(am[i].nrows());
531  av[i] = am[i](Range(joker),0);
532  // (The am[i](Range(joker),0) picks out first column of am[i], = operator copies
533  // to v.)
534  }
535 }
536 
537 
538 
539 //=== ArrayOfMatrix ====================================================
540 
541 void ArrayOfMatrixWriteAscii(// WS Generic Input:
542  const ArrayOfMatrix& am,
543  // WS Generic Input Names:
544  const String& am_name,
545  // Control Parameters:
546  const String& f)
547 {
548  String filename = f;
549 
550  // Create default filename if empty
551  filename_ascii( filename, am_name );
552 
553  // Write the array of matrix to the file.
554  write_array_of_matrix_to_file(filename,am);
555 }
556 
557 
558 
559 void ArrayOfMatrixReadAscii(// WS Generic Output:
560  ArrayOfMatrix& am,
561  // WS Generic Output Names:
562  const String& am_name,
563  // Control Parameters:
564  const String& f)
565 {
566  String filename = f;
567 
568  // Create default filename if empty
569  filename_ascii( filename, am_name );
570 
571  // Read the array of matrix from the file:
572  read_array_of_matrix_from_file(am,filename);
573 }
574 
575 
576 
577 //=== STRING ===============================================================
578 
579 void StringWriteAscii( // WS Generic Input:
580  const String& s,
581  // WS Generic Input Names:
582  const String& s_name,
583  // Control Parameters:
584  const String& f)
585 {
586  String filename = f;
587 
588  // Create default filename if empty
589  filename_ascii( filename, s_name );
590 
591  // Convert the String to an array of String:
592  ArrayOfString as(1);
593  as[0] = s;
594 
595  // Write the array of matrix to the file.
596  write_array_of_String_to_file(filename,as);
597 }
598 
599 
600 
601 void StringReadAscii( // WS Generic Output:
602  String& s,
603  // WS Generic Output Names:
604  const String& s_name,
605  // Control Parameters:
606  const String& f)
607 {
608  String filename = f;
609 
610  // Create default filename if empty
611  filename_ascii( filename, s_name );
612 
613  // Read the array of matrix from the file:
614  ArrayOfString as;
615  read_array_of_String_from_file(as,filename);
616 
617  // Convert the array of String to a String.
618  if ( 1 != as.nelem() )
619  throw runtime_error("You tried to convert an array of String to a String,\n"
620  "but the dimension of the array is not 1.");
621 
622  s = as[0];
623 }
624 
625 
626 
627 //=== ArrayOfString ====================================================
628 
629 void ArrayOfStringWriteAscii( // WS Generic Input:
630  const ArrayOfString& as,
631  // WS Generic Input Names:
632  const String& as_name,
633  // Control Parameters:
634  const String& f)
635 {
636  String filename = f;
637 
638  // Create default filename if empty
639  filename_ascii( filename, as_name );
640 
641  // Write the array of matrix to the file.
642  write_array_of_String_to_file(filename,as);
643 }
644 
645 
646 
647 void ArrayOfStringReadAscii( // WS Generic Output:
648  ArrayOfString& as,
649  // WS Generic Output Names:
650  const String& as_name,
651  // Control Parameters:
652  const String& f)
653 {
654  String filename = f;
655 
656  // Create default filename if empty
657  filename_ascii( filename, as_name );
658 
659  // Read the array of matrix from the file:
660  read_array_of_String_from_file(as,filename);
661 }
662 
663 
664 //=== tgs ====================================================
665 
666 void TagGroupsSpeciesWriteAscii( // WS Generic Input:
667  const TagGroups& tgs,
668  // WS Generic Input Names:
669  const String& tgs_name,
670  // Control Parameters:
671  const String& f)
672 {
673  String filename = f;
674 
675  // Create default filename if empty
676  filename_ascii( filename, tgs_name );
677 
678  // Write the tag groups to the file.
679  write_tag_groups_species_to_file(filename,tgs);
680 }
681 
682 
683 
684 //**************************************************************************
685 //
686 // 3. Creation by workspace method keywords
687 //
688 //**************************************************************************
689 
690 //=== Index ============================================================
691 
698 void IndexSet(// WS Generic Output:
699  Index& x,
700  // WS Generic Output Names:
701  const String& x_name,
702  // Control Parameters:
703  const Index& value)
704 {
705  x = value;
706  out3 << " Setting " << x_name << " to " << value << ".\n";
707 }
708 
709 
710 
711 //=== NUMERIC ==========================================================
712 
719 void NumericSet(// WS Generic Output:
720  Numeric& x,
721  // WS Generic Output Names:
722  const String& x_name,
723  // Control Parameters:
724  const Numeric& value)
725 {
726  x = value;
727  out3 << " Setting " << x_name << " to " << value << ".\n";
728 }
729 
730 
738  // WS Generic Output:
739  Numeric& x,
740  // WS Generic Output Names:
741  const String& x_name,
742  // Control Parameters:
743  const Vector& v,
744  const String& v_name )
745 {
746  x = v[0];
747  out3 << " Setting " << x_name << " to the first value of " << v_name << ".\n";
748 }
749 
750 
751 
759  // WS Generic Output:
760  Numeric& x,
761  // WS Generic Output Names:
762  const String& x_name,
763  // Control Parameters:
764  const Vector& v,
765  const String& v_name )
766 {
767  x = v[v.nelem()-1];
768  out3 << " Setting " << x_name << " to the last value of " << v_name << ".\n";
769 }
770 
771 
772 
773 //=== Vector ==========================================================
774 
781 void VectorSet( Vector& x,
782  const String& x_name,
783  const Index& n,
784  const Numeric& value )
785 {
786  x.resize(n);
787  x = value; // Matpack can set all elements like this.
788  out2 << " Creating " << x_name << " as a constant vector\n";
789  out3 << " length : " << n << "\n";
790  out3 << " value : " << value << "\n";
791 }
792 
793 
794 
802  Vector& x,
803  const String& x_name,
804  const Vector& z,
805  const String& /* z_name */,
806  const Numeric& value )
807 {
808  const Index n = z.nelem();
809  x.resize(n);
810  x = value; // Matpack can set all elements like this.
811  out2 << " Creating " << x_name << " as a constant vector\n";
812  out3 << " length : " << n << "\n";
813  out3 << " value : " << value << "\n";
814 }
815 
816 
817 
825  const String& x_name,
826  const Numeric& start,
827  const Numeric& stop,
828  const Numeric& step )
829 {
830  linspace(x,start,stop,step);
831  out2 << " Creating " << x_name << " as linearly spaced vector\n";
832  out3 << " length: " << x.nelem() << "\n";
833  out3 << " first value: " << x[0] << "\n";
834  if ( x.nelem() > 1 )
835  {
836  out3 << " step size: " << x[1]-x[0] << "\n";
837  out3 << " last value: " << x[x.nelem()-1] << "\n";
838  }
839 }
840 
841 
842 
850  const String& x_name,
851  const Numeric& start,
852  const Numeric& stop,
853  const Index& n )
854 {
855  if ( n<2 ) // changed by TKS since for calculations with laboratory data (original n<2)
856  throw runtime_error("The number of points must be > 1.");
857  nlinspace(x,start,stop,n);
858  out2 << " Creating " << x_name << " as linearly spaced vector\n";
859  out3 << " length: " << n << "\n";
860  out3 << " first value: " << x[0] << "\n";
861  if ( x.nelem() > 1 )
862  {
863  out3 << " step size: " << x[1]-x[0] << "\n";
864  out3 << " last value: " << x[x.nelem()-1] << "\n";
865  }
866 }
867 
868 
869 
871  // WS Generic Output:
872  Vector& p,
873  // WS Generic Output Names:
874  const String& /* p_name */,
875  // WS Input:
876  const Vector& p_abs,
877  const Vector& z_abs,
878  // Control Parameters:
879  const Numeric& delta_z,
880  const Numeric& p_start,
881  const Numeric& p_stop)
882 
883 {
884  Vector p_lim(2), z_lim(2);
885  p_lim[0] = p_start;
886  p_lim[1] = p_stop;
887 
888  interpp(z_lim,p_abs,z_abs,p_lim);
889 
890  Vector z;
891  linspace(z,z_lim[0],z_lim[1],delta_z);
892  p.resize( z.nelem());
893  z2p(p, z_abs, p_abs, z);
894 }
895 
896 
897 
905  const String& x_name,
906  const Numeric& start,
907  const Numeric& stop,
908  const Index& n )
909 {
910  if ( n<2 )
911  throw runtime_error("The number of points must be > 1.");
912  if ( (start<=0) || (stop<=0) )
913  throw runtime_error("Only positive numbers are allowed.");
914 
915  x.resize(n);
916  x = nlogspace(start,stop,n);
917  out2 << " Creating " << x_name << " as logarithmically spaced vector\n";
918  out3 << " length: " << n << "\n";
919  out3 << " first value: " << x[0] << "\n";
920  if ( x.nelem() > 1 )
921  out3 << " last value: " << x[x.nelem()-1] << "\n";
922 }
923 
924 
925 
933  Vector& y2,
934  const String& name_y2,
935  const Vector& y1,
936  const String& name_y1 )
937 {
938  out2 << " " << name_y2 << " = " << name_y1 << "\n";
939  y2.resize( y1.nelem() );
940  y2 = y1; // Matpack can copy the contents of
941  // vectors like this. The dimensions
942  // must be the same!
943 }
944 
945 
953  Vector& v,
954  const String& /* v_name */,
955  const Matrix& m,
956  const String& /* m_name */,
957  const String& orientation,
958  const Index& index )
959 {
960  if (orientation == String ("col"))
961  {
962  if (index < m.ncols ())
963  {
964  v.resize (m.nrows ());
965  v = m (joker, index);
966  }
967  else
968  throw runtime_error ("Index out of column bounds");
969  }
970  else if (orientation == String ("row"))
971  {
972  if (index < m.nrows ())
973  {
974  v.resize (m.ncols ());
975  v = m (index, joker);
976  }
977  else
978  throw runtime_error ("Index out of row bounds");
979  }
980  else
981  throw runtime_error ("Orientation must be either \"row\" or \"col\"");
982 }
983 
984 
992  Vector& y2,
993  const String& name_y2,
994  const Vector& y1,
995  const String& name_y1 )
996 {
997  out2 << " Flips " << name_y2 << " to create " << name_y1 << "\n";
998 
999  Index n = y1.nelem();
1000 
1001  Vector dum( n );
1002  for ( Index i=0; i<n; i++ )
1003  dum[n-1-i] = y1[i];
1004 
1005  y2.resize( n );
1006  y2 = dum; // Matpack can copy the contents of
1007  // vectors like this. The dimensions
1008  // must be the same!
1009 }
1010 
1011 
1012 
1020  Vector& y,
1021  const String& y_name,
1022  const Vector& f,
1023  const String& /* f_name */,
1024  const Numeric& t )
1025 {
1026  if ( t > 0 )
1027  {
1028  y.resize( f.nelem() );
1029  planck( y, f, t );
1030  out2<<" Setting " << y_name << " to blackbody radiation for "<<t<<" K.\n";
1031  }
1032  else
1033  throw runtime_error("The temperature must be > 0.");
1034 }
1035 
1036 
1037 
1045  Vector& out,
1046  const String& out_name,
1047  const Vector& in,
1048  const String& in_name )
1049 {
1050  out2<<" " << out_name << " = log10( " << in_name << " )\n";
1051 
1052  out.resize( in.nelem() );
1053  transform( out, log10, in ); // out = log10(in)
1054 }
1055 
1056 
1057 
1065  Vector& out,
1066  const String& out_name,
1067  const Vector& in,
1068  const String& in_name,
1069  const Numeric& value )
1070 {
1071  out2<<" " << out_name << " = " << in_name << " + " << value << "\n";
1072 
1073  // Note that in and out can be the same vector
1074  if (&out==&in)
1075  {
1076  // Out and in are the same. Just add the scalar value.
1077  out += value; // With Matpack you can add a scalar
1078  // to all elements of a vector like
1079  // this.
1080  }
1081  else
1082  {
1083  // Out and in are different. We first have to copy in to out,
1084  // then add the scalar value.
1085 
1086  out.resize( in.nelem() );
1087  out = in; // Matpack can copy the contents of
1088  // vectors like this. The dimensions
1089  // must be the same!
1090  out += value;
1091  }
1092 }
1093 
1094 
1095 
1103  Vector& out,
1104  const String& out_name,
1105  const Vector& in,
1106  const String& in_name,
1107  const Numeric& value )
1108 {
1109  out2<<" " << out_name << " = " << in_name << " * " << value << "\n";
1110 
1111  // Note that in and out can be the same vector
1112  if (&out==&in)
1113  {
1114  // Out and in are the same. Just multiply by the scalar value.
1115  out *= value; // With Matpack you can add a scalar
1116  // to all elements of a vector like
1117  // this.
1118  }
1119  else
1120  {
1121  // Out and in are different. We first have to copy in to out,
1122  // then multiply by the scalar value.
1123 
1124  out.resize( in.nelem() );
1125  out = in; // Matpack can copy the contents of
1126  // vectors like this. The dimensions
1127  // must be the same!
1128  out *= value;
1129  }
1130 }
1131 
1143 void VectorMatrixMultiply(// WS Generic Output:
1144  Vector& y,
1145  // WS Generic Output Names:
1146  const String& /* y_name */,
1147  // WS Generic Input:
1148  const Matrix& M,
1149  const Vector& x,
1150  // WS Generic Input Names:
1151  const String& M_name,
1152  const String& x_name)
1153 {
1154  // Check that dimensions are right, x must match columns of M:
1155  check_length_ncol( x, x_name, M, M_name );
1156 
1157  // Temporary for the result:
1158  Vector dummy( M.nrows() );
1159 
1160  mult( dummy, M, x );
1161 
1162  // Copy result to y:
1163 
1164  y.resize( dummy.nelem() );
1165 
1166  y = dummy;
1167 }
1168 
1169 
1170 //=== Matrix ==========================================================
1171 
1178 void MatrixSet( Matrix& x,
1179  const String& x_name,
1180  const Index& nrows,
1181  const Index& ncols,
1182  const Numeric& value )
1183 {
1184  x.resize( nrows, ncols );
1185  x = value; // Matpack can set all elements like this.
1186  out2 << " Creating " << x_name << " as a constant matrix\n";
1187  out3 << " nrows : " << nrows << "\n";
1188  out3 << " ncols : " << ncols << "\n";
1189  out3 << " value : " << value << "\n";
1190 }
1191 
1192 
1193 
1195  Matrix& y2,
1196  const String& name_y2,
1197  const Matrix& y1,
1198  const String& name_y1 )
1199 {
1200  out2 << " " << name_y2 << " = " << name_y1 << "\n";
1201  y2.resize( y1.nrows(), y1.ncols() );
1202  y2 = y1; // Matpack can copy the contents of
1203  // matrices like this. The dimensions
1204  // must be the same!
1205 }
1206 
1207 
1208 
1210  Matrix& m,
1211  const String& name_m,
1212  const Vector& y,
1213  const String& name_y,
1214  const Index& n )
1215 {
1216  out2 << " Creates" << name_m << " by copying " << name_y << n << "times.\n";
1217  m.resize( y.nelem(), n );
1218  for ( Index i=0; i<n; ++i )
1219  m(Range(joker),i) = y; // Copy content of vector y to this
1220  // column of Matrix m.
1221 }
1222 
1223 
1224 
1232  Matrix& out,
1233  const String& out_name,
1234  const Matrix& in,
1235  const String& in_name,
1236  const Numeric& value )
1237 {
1238  out2<<" " << out_name << " = " << in_name << " * " << value << "\n";
1239 
1240  // Note that in and out can be the same matrix
1241  if (&out==&in)
1242  {
1243  // Out and in are the same. Just multiply by the scalar value.
1244  out *= value; // With Matpack you can multiply a scalar
1245  // to all elements of a matrix like
1246  // this.
1247  }
1248  else
1249  {
1250  // Out and in are different. We first have to copy in to out,
1251  // then multiply by the scalar value.
1252 
1253  out.resize( in.nrows(), in.ncols() );
1254  out = in; // Matpack can copy the contents of
1255  // matrices like this. The dimensions
1256  // must be the same!
1257  out *= value;
1258  }
1259 }
1260 
1261 
1262 
1270  Matrix& x,
1271  const String& x_name,
1272  const Index& nrows,
1273  const Numeric& value )
1274 {
1275  x.resize( nrows, nrows );
1276  for ( Index i=0; i<Index(nrows); i++ )
1277  x(i,i) = value;
1278 
1279  out2 << " Creating " << x_name << " as a diagonal matrix\n";
1280  out3 << " nrows : " << nrows << "\n";
1281  out3 << " value : " << value << "\n";
1282 }
1283 
1295 void MatrixMatrixMultiply(// WS Generic Output:
1296  Matrix& Y,
1297  // WS Generic Output Names:
1298  const String& /* Y_name */,
1299  // WS Generic Input:
1300  const Matrix& M,
1301  const Matrix& X,
1302  // WS Generic Input Names:
1303  const String& M_name,
1304  const String& X_name)
1305 {
1306  // Check that dimensions are right, M.ncols() must match X.nrows():
1307  check_ncol_nrow( M, M_name, X, X_name );
1308 
1309  // Temporary for the result:
1310  Matrix dummy( M.nrows(), X.ncols() );
1311 
1312  mult( dummy, M, X );
1313 
1314  // Copy result to Y:
1315 
1316  Y.resize( dummy.nrows(), dummy.ncols() );
1317 
1318  Y = dummy;
1319 }
1320 
1321 void ArrayOfMatrixMatrixMultiply(// WS Generic Output:
1322  ArrayOfMatrix& Y,
1323  // WS Generic Output Names:
1324  const String& /* Y_name */,
1325  // WS Generic Input:
1326  const Matrix& M,
1327  const ArrayOfMatrix& X,
1328  // WS Generic Input Names:
1329  const String& M_name,
1330  const String& X_name)
1331 {
1332  // Check that dimensions are right, M.ncols() must match X.nrows():
1333  // check_ncol_nrow( M, M_name, X, X_name );
1334 
1335  // Temporary for the result:
1336  ArrayOfMatrix dummy( X.nelem() );
1337 
1338  // Loop over the Matrices
1339  for ( Index i=0; i<X.nelem(); i++ )
1340  {
1341  // Check that dimensions are right, M.ncols() must match X.nrows():
1342  check_ncol_nrow( M, M_name, X[i], X_name );
1343 
1344  // Temporary for the result:
1345  Matrix dummymat( M.nrows(), X[i].ncols() );
1346 
1347  mult( dummymat, M, X[i] );
1348 
1349  // Copy result to Y:
1350 
1351  dummy[i].resize( dummymat.nrows(), dummymat.ncols() );
1352 
1353  dummy[i] = dummymat;
1354  }
1355 
1356  Y.resize( dummy.nelem() );
1357  for (Index i = 0; i < dummy.nelem(); i++)
1358  {
1359  Y[i].resize( dummy[i].nrows(), dummy[i].ncols() );
1360  Y[i] = dummy[i];
1361  }
1362 }
1363 
1364 
1376 void MatrixMatrixAdd(// WS Generic Output:
1377  Matrix& Y,
1378  // WS Generic Output Names:
1379  const String& /* Y_name */,
1380  // WS Generic Input:
1381  const Matrix& M,
1382  const Matrix& X,
1383  // WS Generic Input Names:
1384  const String& M_name,
1385  const String& X_name)
1386 {
1387  Index m = M.nrows();
1388  Index n = M.ncols();
1389 
1390  // Check that dimensions are right
1391  if ( n != X.ncols() || m != X.nrows() )
1392  {
1393  ostringstream os;
1394  os << "The size of the two matrices must be identical. \n"
1395  << "Size of " << M_name << " is " << M.nrows() << " x " << M.ncols()
1396  << "\n"
1397  << "Size of " << X_name << " is " << X.nrows() << " x " << X.ncols();
1398  throw runtime_error( os.str() );
1399  }
1400 
1401  // Temporary for the result:
1402  Matrix dummy( m, n );
1403  Index i,j;
1404 
1405  for ( i=0; i<m; i++ )
1406  {
1407  for ( j=0; j<n; j++ )
1408  dummy(i,j) = M(i,j) + X(i,j);
1409  }
1410 
1411  // Copy result to Y:
1412  Y.resize( m, n );
1413 
1414  Y = dummy;
1415 }
1416 
1417 
1418 
1419 
1420 //=== STRING ===============================================================
1421 
1428 void StringSet( String& s,
1429  const String& s_name,
1430  const String& s2 )
1431 {
1432  s = s2;
1433  out3 << " Setting " << s_name << " to " << s2 << "\n";
1434 }
1435 
1436 
1437 
1438 //=== ArrayOfSTRING ========================================================
1439 
1447  ArrayOfString& sa,
1448  const String& sa_name,
1449  const ArrayOfString& sa2 )
1450 {
1451  sa.resize(sa2.nelem());
1452  sa = sa2; // Arrays can be copied like this.
1453  out3 << " Setting " << sa_name << "\n";
1454 }
1455 
check_ncol_nrow
void check_ncol_nrow(const Matrix &A, const String &A_name, const Matrix &B, const String &B_name)
Checks that the number of columns of the first matrix is the same as the number of rows of the second...
Definition: math_funcs.cc:622
Matrix
The Matrix class.
Definition: matpackI.h:544
ArrayOfMatrixReadAscii
void ArrayOfMatrixReadAscii(ArrayOfMatrix &am, const String &am_name, const String &f)
Definition: m_io.cc:559
out2
Out2 out2
Level 2 output stream.
Definition: messages.cc:54
transform
void transform(VectorView y, double(&my_func)(double), ConstVectorView x)
A generic transform function for vectors, which can be used to implement mathematical functions opera...
Definition: matpackI.h:2405
NumericCopyFirstOfVector
void NumericCopyFirstOfVector(Numeric &x, const String &x_name, const Vector &v, const String &v_name)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:737
auto_md.h
IndexWriteAscii
void IndexWriteAscii(const Index &v, const String &v_name, const String &f)
Definition: m_io.cc:181
VectorNLinSpace
void VectorNLinSpace(Vector &x, const String &x_name, const Numeric &start, const Numeric &stop, const Index &n)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:849
VectorCopy
void VectorCopy(Vector &y2, const String &name_y2, const Vector &y1, const String &name_y1)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:932
write_array_of_matrix_to_file
void write_array_of_matrix_to_file(const String &filename, const ArrayOfMatrix &am)
A helper function that writes an array of matrix to a file.
Definition: file.cc:322
atm_funcs.h
This file contains declerations of functions releated to atmospheric physics or geometry.
check_length_ncol
void check_length_ncol(const Vector &x, const String &x_name, const Matrix &A, const String &A_name)
Checkss that the length of a vector and the number of columns of a matrix match.
Definition: math_funcs.cc:592
MatrixCopy
void MatrixCopy(Matrix &y2, const String &name_y2, const Matrix &y1, const String &name_y1)
Definition: m_io.cc:1194
Echo
void Echo(const String &message, const Index &output_level)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:68
TagGroupsSpeciesWriteAscii
void TagGroupsSpeciesWriteAscii(const TagGroups &tgs, const String &tgs_name, const String &f)
Definition: m_io.cc:666
MatrixWriteAscii
void MatrixWriteAscii(const Matrix &m, const String &m_name, const String &f)
Definition: m_io.cc:347
NumericSet
void NumericSet(Numeric &x, const String &x_name, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:719
VectorPlanck
void VectorPlanck(Vector &y, const String &y_name, const Vector &f, const String &, const Numeric &t)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1019
ArrayOfStringWriteAscii
void ArrayOfStringWriteAscii(const ArrayOfString &as, const String &as_name, const String &f)
Definition: m_io.cc:629
out0
Out0 out0
Level 0 output stream.
Definition: messages.cc:50
ArrayOfIndexReadAscii
void ArrayOfIndexReadAscii(ArrayOfIndex &v, const String &v_name, const String &f)
Definition: m_io.cc:424
VectorScale
void VectorScale(Vector &out, const String &out_name, const Vector &in, const String &in_name, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1102
Vector::resize
void resize(Index n)
Resize function.
Definition: matpackI.h:1467
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.h:1491
Exit
void Exit()
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:93
joker
Joker joker
Define the global joker objekt.
Definition: constants.cc:233
mult
void mult(VectorView y, const ConstMatrixView &M, const ConstVectorView &x)
Matrix Vector multiplication.
Definition: matpackI.h:2291
MatrixFillWithVector
void MatrixFillWithVector(Matrix &m, const String &name_m, const Vector &y, const String &name_y, const Index &n)
Definition: m_io.cc:1209
VectorSetLengthFromVector
void VectorSetLengthFromVector(Vector &x, const String &x_name, const Vector &z, const String &, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:801
ArrayOfStringSet
void ArrayOfStringSet(ArrayOfString &sa, const String &sa_name, const ArrayOfString &sa2)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1446
nlogspace
void nlogspace(Vector &x, const Numeric start, const Numeric stop, const Index n)
Logarithmically spaced vector with specified length.
Definition: math_funcs.cc:198
complex.h
A class implementing complex numbers for ARTS.
Array
This can be used to make arrays out of anything.
Definition: array.h:48
MatrixMatrixAdd
void MatrixMatrixAdd(Matrix &Y, const String &, const Matrix &M, const Matrix &X, const String &M_name, const String &X_name)
Compute Y = M+X.
Definition: m_io.cc:1376
MatrixReadAscii
void MatrixReadAscii(Matrix &m, const String &m_name, const String &f)
Definition: m_io.cc:371
VectorReadAscii
void VectorReadAscii(Vector &v, const String &v_name, const String &f)
Definition: m_io.cc:309
messages.h
Declarations having to do with the four output streams.
planck
void planck(MatrixView B, ConstVectorView f, ConstVectorView t)
Calculates a blackbody radiation (the Planck function) matrix.
Definition: atm_funcs.cc:75
ArrayOfIndexWriteAscii
void ArrayOfIndexWriteAscii(const ArrayOfIndex &v, const String &v_name, const String &f)
Definition: m_io.cc:403
VectorSet
void VectorSet(Vector &x, const String &x_name, const Index &n, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:781
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.h:1497
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:61
out1
Out1 out1
Level 1 output stream.
Definition: messages.cc:52
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: arts.h:147
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.h:937
MatrixDiagonal
void MatrixDiagonal(Matrix &x, const String &x_name, const Index &nrows, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1269
VectorNLogSpace
void VectorNLogSpace(Vector &x, const String &x_name, const Numeric &start, const Numeric &stop, const Index &n)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:904
MatrixScale
void MatrixScale(Matrix &out, const String &out_name, const Matrix &in, const String &in_name, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1231
Test
void Test()
Definition: m_io.cc:109
ArrayOfVectorWriteAscii
void ArrayOfVectorWriteAscii(const ArrayOfVector &av, const String &av_name, const String &f)
Definition: m_io.cc:476
make_array.h
Implements the class MakeArray, which is a derived class of Array, allowing explicit initialization.
math_funcs.h
Contains declerations of basic mathematical and vector/matrix functions.
VectorLinSpace
void VectorLinSpace(Vector &x, const String &x_name, const Numeric &start, const Numeric &stop, const Numeric &step)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:824
write_array_of_String_to_file
void write_array_of_String_to_file(const String &filename, const ArrayOfString &as)
A help function that writes an array of String to a file.
Definition: file.cc:497
ArrayOfMatrixMatrixMultiply
void ArrayOfMatrixMatrixMultiply(ArrayOfMatrix &Y, const String &, const Matrix &M, const ArrayOfMatrix &X, const String &M_name, const String &X_name)
Definition: m_io.cc:1321
VectorWriteAscii
void VectorWriteAscii(const Vector &v, const String &v_name, const String &f)
Definition: m_io.cc:279
VectorMatrixMultiply
void VectorMatrixMultiply(Vector &y, const String &, const Matrix &M, const Vector &x, const String &M_name, const String &x_name)
Compute y = M*x.
Definition: m_io.cc:1143
Matrix::resize
void resize(Index r, Index c)
Resize function.
Definition: matpackI.h:2237
read_array_of_String_from_file
void read_array_of_String_from_file(ArrayOfString &as, const String &filename)
A help function to read an array of String from a file.
Definition: file.cc:582
StringSet
void StringSet(String &s, const String &s_name, const String &s2)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1428
nlinspace
void nlinspace(Vector &x, const Numeric start, const Numeric stop, const Index n)
Linearly spaced vector with specified length.
Definition: math_funcs.cc:168
ArrayOfMatrixWriteAscii
void ArrayOfMatrixWriteAscii(const ArrayOfMatrix &am, const String &am_name, const String &f)
Definition: m_io.cc:541
VectorCalcLog10
void VectorCalcLog10(Vector &out, const String &out_name, const Vector &in, const String &in_name)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1044
VectorPressuresForLinAltitudes
void VectorPressuresForLinAltitudes(Vector &p, const String &, const Vector &p_abs, const Vector &z_abs, const Numeric &delta_z, const Numeric &p_start, const Numeric &p_stop)
Definition: m_io.cc:870
NumericWriteAscii
void NumericWriteAscii(const Numeric &v, const String &v_name, const String &f)
Definition: m_io.cc:234
Range
The range class.
Definition: matpackI.h:139
NumericCopyLastOfVector
void NumericCopyLastOfVector(Numeric &x, const String &x_name, const Vector &v, const String &v_name)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:758
out3
Out3 out3
Level 3 output stream.
Definition: messages.cc:56
VectorFlip
void VectorFlip(Vector &y2, const String &name_y2, const Vector &y1, const String &name_y1)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:991
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: arts.h:153
IndexReadAscii
void IndexReadAscii(Index &v, const String &v_name, const String &f)
Definition: m_io.cc:200
VectorAdd
void VectorAdd(Vector &out, const String &out_name, const Vector &in, const String &in_name, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1064
ArrayOfStringReadAscii
void ArrayOfStringReadAscii(ArrayOfString &as, const String &as_name, const String &f)
Definition: m_io.cc:647
read_array_of_matrix_from_file
void read_array_of_matrix_from_file(ArrayOfMatrix &am, const String &filename)
A helper function that reads an array of matrix from a file.
Definition: file.cc:425
filename_ascii
void filename_ascii(String &filename, const String &varname)
Gives the default file name for the ASCII formats.
Definition: file.cc:64
DEG2RAD
const Numeric DEG2RAD
See the the online help (arts -d FUNCTION_NAME)
VectorCopyFromMatrix
void VectorCopyFromMatrix(Vector &v, const String &, const Matrix &m, const String &, const String &orientation, const Index &index)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:952
MatrixSet
void MatrixSet(Matrix &x, const String &x_name, const Index &nrows, const Index &ncols, const Numeric &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:1178
interpp
void interpp(VectorView x, ConstVectorView p0, ConstVectorView x0, ConstVectorView p)
Interpolates a vertical profile at a new set of pressures.
Definition: atm_funcs.cc:658
file.h
This file contains basic functions to handle ASCII and binary (HDF) data files.
MakeArray
Explicit construction of Arrays.
Definition: make_array.h:52
MatrixMatrixMultiply
void MatrixMatrixMultiply(Matrix &Y, const String &, const Matrix &M, const Matrix &X, const String &M_name, const String &X_name)
Compute Y = M*X.
Definition: m_io.cc:1295
StringReadAscii
void StringReadAscii(String &s, const String &s_name, const String &f)
Definition: m_io.cc:601
linspace
void linspace(Vector &x, const Numeric start, const Numeric stop, const Numeric step)
Linearly spaced vector with specified spacing.
Definition: math_funcs.cc:139
IndexSet
void IndexSet(Index &x, const String &x_name, const Index &value)
See the the online help (arts -d FUNCTION_NAME)
Definition: m_io.cc:698
Vector
The Vector class.
Definition: matpackI.h:389
StringWriteAscii
void StringWriteAscii(const String &s, const String &s_name, const String &f)
Definition: m_io.cc:579
write_tag_groups_species_to_file
void write_tag_groups_species_to_file(const String &filename, const TagGroups &tgs)
A help function that writes an array of String to a file.
Definition: file.cc:656
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:115
z2p
void z2p(VectorView p, ConstVectorView z0, ConstVectorView p0, ConstVectorView z)
Converts an altitude vector to pressures.
Definition: atm_funcs.cc:621
NumericReadAscii
void NumericReadAscii(Numeric &v, const String &v_name, const String &f)
Definition: m_io.cc:252
arts.h
The global header file for ARTS.
ArrayOfVectorReadAscii
void ArrayOfVectorReadAscii(ArrayOfVector &av, const String &av_name, const String &f)
Definition: m_io.cc:504