ARTS  2.0.49
xml_io_basic_types.cc
Go to the documentation of this file.
1 /* Copyright (C) 2003-2008 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 
20 // File description
22 
31 #include "arts.h"
32 #include "xml_io.h"
33 #include "xml_io_private.h"
34 #include "xml_io_types.h"
35 
36 
38 // Overloaded functions for reading/writing data from/to XML stream
40 
41 //=== Index ==================================================================
42 
44 
49 void
50 xml_read_from_stream (istream& is_xml,
51  Index& index,
52  bifstream *pbifs, const Verbosity& verbosity)
53 {
54  ArtsXMLTag tag(verbosity);
55 
56  tag.read_from_stream (is_xml);
57  tag.check_name ("Index");
58 
59  if (pbifs)
60  {
61  *pbifs >> index;
62  if (pbifs->fail ())
63  {
64  xml_data_parse_error (tag, "");
65  }
66  }
67  else
68  {
69  is_xml >> index;
70  if (is_xml.fail ())
71  {
72  xml_data_parse_error (tag, "");
73  }
74  }
75 
76  tag.read_from_stream (is_xml);
77  tag.check_name ("/Index");
78 }
79 
80 
82 
88 void
89 xml_write_to_stream (ostream& os_xml,
90  const Index& index,
91  bofstream *pbofs,
92  const String& name, const Verbosity& verbosity)
93 {
94  ArtsXMLTag open_tag(verbosity);
95  ArtsXMLTag close_tag(verbosity);
96 
97  open_tag.set_name ("Index");
98  if (name.length ())
99  open_tag.add_attribute ("name", name);
100 
101  open_tag.write_to_stream (os_xml);
102 
103  if (pbofs)
104  *pbofs << index;
105  else
106  os_xml << index;
107 
108  close_tag.set_name ("/Index");
109  close_tag.write_to_stream (os_xml);
110  os_xml << '\n';
111 }
112 
113 
114 //=== Matrix ==========================================================
115 
117 
122 void
123 xml_read_from_stream (istream& is_xml,
124  Matrix& matrix,
125  bifstream *pbifs, const Verbosity& verbosity)
126 {
127  ArtsXMLTag tag(verbosity);
128  Index nrows, ncols;
129 
130  tag.read_from_stream (is_xml);
131  tag.check_name ("Matrix");
132 
133  tag.get_attribute_value ("nrows", nrows);
134  tag.get_attribute_value ("ncols", ncols);
135  matrix.resize (nrows, ncols);
136 
137  for (Index r = 0; r < nrows; r++)
138  {
139  for (Index c = 0; c < ncols; c++)
140  {
141  if (pbifs)
142  {
143  *pbifs >> matrix (r, c);
144  if (pbifs->fail ())
145  {
146  ostringstream os;
147  os << " near "
148  << "\n Row : " << r
149  << "\n Column: " << c;
150  xml_data_parse_error (tag, os.str());
151  }
152  }
153  else
154  {
155  is_xml >> matrix (r, c);
156  if (is_xml.fail ())
157  {
158  ostringstream os;
159  os << " near "
160  << "\n Row : " << r
161  << "\n Column: " << c;
162  xml_data_parse_error (tag, os.str());
163  }
164  }
165  }
166  }
167 
168  tag.read_from_stream (is_xml);
169  tag.check_name ("/Matrix");
170 }
171 
172 
174 
180 void
181 xml_write_to_stream (ostream& os_xml,
182  const Matrix& matrix,
183  bofstream *pbofs,
184  const String& name, const Verbosity& verbosity)
185 {
186  ArtsXMLTag open_tag(verbosity);
187  ArtsXMLTag close_tag(verbosity);
188 
189  open_tag.set_name ("Matrix");
190  if (name.length ())
191  open_tag.add_attribute ("name", name);
192  open_tag.add_attribute ("nrows", matrix.nrows ());
193  open_tag.add_attribute ("ncols", matrix.ncols ());
194 
195  open_tag.write_to_stream (os_xml);
196  os_xml << '\n';
197 
198  xml_set_stream_precision (os_xml);
199 
200  // Write the elements:
201  for (Index r = 0; r < matrix.nrows (); ++r)
202  {
203  if (pbofs)
204  *pbofs << matrix (r, 0);
205  else
206  os_xml << matrix (r, 0);
207 
208  for (Index c = 1; c < matrix.ncols (); ++c)
209  {
210  if (pbofs)
211  *pbofs << matrix (r, c);
212  else
213  os_xml << " " << matrix (r, c);
214  }
215 
216  if (!pbofs)
217  os_xml << '\n';
218  }
219 
220  close_tag.set_name ("/Matrix");
221  close_tag.write_to_stream (os_xml);
222 
223  os_xml << '\n';
224 }
225 
226 
227 //=== Numeric =========================================================
228 
230 
235 void
236 xml_read_from_stream (istream& is_xml,
237  Numeric& numeric,
238  bifstream *pbifs, const Verbosity& verbosity)
239 {
240  ArtsXMLTag tag(verbosity);
241 
242  tag.read_from_stream (is_xml);
243  tag.check_name ("Numeric");
244 
245  if (pbifs)
246  {
247  *pbifs >> numeric;
248  if (pbifs->fail ())
249  {
250  xml_data_parse_error (tag, "");
251  }
252  }
253  else
254  {
255  is_xml >> numeric;
256  if (is_xml.fail ())
257  {
258  xml_data_parse_error (tag, "");
259  }
260  }
261 
262  tag.read_from_stream (is_xml);
263  tag.check_name ("/Numeric");
264 }
265 
266 
268 
274 void
275 xml_write_to_stream (ostream& os_xml,
276  const Numeric& numeric,
277  bofstream *pbofs,
278  const String& name, const Verbosity& verbosity)
279 {
280  ArtsXMLTag open_tag(verbosity);
281  ArtsXMLTag close_tag(verbosity);
282 
283  open_tag.set_name ("Numeric");
284  if (name.length ())
285  open_tag.add_attribute ("name", name);
286 
287  open_tag.write_to_stream (os_xml);
288 
289  xml_set_stream_precision (os_xml);
290 
291  if (pbofs)
292  *pbofs << numeric;
293  else
294  os_xml << numeric;
295 
296  close_tag.set_name ("/Numeric");
297  close_tag.write_to_stream (os_xml);
298  os_xml << '\n';
299 }
300 
301 
302 //=== Sparse ====================================================
303 
305 
310 void
311 xml_read_from_stream (istream& is_xml,
312  Sparse& sparse,
313  bifstream *pbifs, const Verbosity& verbosity)
314 {
315  ArtsXMLTag tag(verbosity);
316  Index nrows, ncols, nnz;
317 
318  tag.read_from_stream (is_xml);
319  tag.check_name ("Sparse");
320 
321  tag.get_attribute_value ("nrows", nrows);
322  tag.get_attribute_value ("ncols", ncols);
323  sparse.resize(nrows, ncols);
324 
325  tag.read_from_stream (is_xml);
326  tag.check_name ("RowIndex");
327  tag.get_attribute_value ("nelem", nnz);
328 
329  ArrayOfIndex rowind(nnz), colind(nnz);
330  Vector data(nnz);
331 
332  for( Index i=0; i<nnz; i++) {
333  if (pbifs) {
334  *pbifs >> rowind[i];
335  if (pbifs->fail ()) {
336  ostringstream os;
337  os << " near "
338  << "\n Row index: " << i;
339  xml_data_parse_error (tag, os.str());
340  }
341  } else {
342  is_xml >> rowind[i];
343  if (is_xml.fail ()) {
344  ostringstream os;
345  os << " near "
346  << "\n Row index: " << i;
347  xml_data_parse_error (tag, os.str());
348  }
349  }
350  }
351  tag.read_from_stream (is_xml);
352  tag.check_name ("/RowIndex");
353 
354  tag.read_from_stream (is_xml);
355  tag.check_name ("ColIndex");
356 
357  for( Index i=0; i<nnz; i++) {
358  if (pbifs) {
359  *pbifs >> colind[i];
360  if (pbifs->fail ()) {
361  ostringstream os;
362  os << " near "
363  << "\n Column index: " << i;
364  xml_data_parse_error (tag, os.str());
365  }
366  } else {
367  is_xml >> colind[i];
368  if (is_xml.fail ()) {
369  ostringstream os;
370  os << " near "
371  << "\n Column index: " << i;
372  xml_data_parse_error (tag, os.str());
373  }
374  }
375  }
376  tag.read_from_stream (is_xml);
377  tag.check_name ("/ColIndex");
378 
379  tag.read_from_stream (is_xml);
380  tag.check_name ("SparseData");
381 
382  for( Index i=0; i<nnz; i++) {
383  if (pbifs) {
384  *pbifs >> data[i];
385  if (pbifs->fail ()) {
386  ostringstream os;
387  os << " near "
388  << "\n Data element: " << i;
389  xml_data_parse_error (tag, os.str());
390  }
391  } else {
392  is_xml >> data[i];
393  if (is_xml.fail ()) {
394  ostringstream os;
395  os << " near "
396  << "\n Data element: " << i;
397  xml_data_parse_error (tag, os.str());
398  }
399  }
400  }
401  tag.read_from_stream (is_xml);
402  tag.check_name ("/SparseData");
403 
404  tag.read_from_stream (is_xml);
405  tag.check_name ("/Sparse");
406 
407  for( Index i=0; i<nnz; i++)
408  sparse.rw(rowind[i], colind[i]) = data[i];
409 }
410 
412 
418 void
419 xml_write_to_stream (ostream& os_xml,
420  const Sparse& sparse,
421  bofstream *pbofs,
422  const String& name, const Verbosity& verbosity)
423 {
424  ArtsXMLTag sparse_tag(verbosity);
425  ArtsXMLTag row_tag(verbosity);
426  ArtsXMLTag col_tag(verbosity);
427  ArtsXMLTag data_tag(verbosity);
428  ArtsXMLTag close_tag(verbosity);
429 
430  sparse_tag.set_name ("Sparse");
431  if (name.length ())
432  sparse_tag.add_attribute ("name", name);
433  sparse_tag.add_attribute ("nrows", sparse.nrows());
434  sparse_tag.add_attribute ("ncols", sparse.ncols());
435  //sparse_tag.add_attribute ("nnz", sparse.nnz());
436  row_tag.set_name ("RowIndex");
437  row_tag.add_attribute ("nelem", sparse.nnz());
438  col_tag.set_name ("ColIndex");
439  col_tag.add_attribute ("nelem", sparse.nnz());
440  data_tag.set_name ("SparseData");
441  data_tag.add_attribute ("nelem", sparse.nnz());
442 
443  sparse_tag.write_to_stream (os_xml);
444  os_xml << '\n';
445 
446  row_tag.write_to_stream (os_xml);
447  os_xml << '\n';
448  for( Index i=0; i<sparse.nnz(); i++) {
449  if (pbofs)
450  //FIXME: It should be the longer lines
451  *pbofs << (*sparse.rowind())[i];
452  else
453  os_xml << (*sparse.rowind())[i] << '\n';
454  }
455  close_tag.set_name("/RowIndex");
456  close_tag.write_to_stream( os_xml);
457  os_xml << '\n';
458 
459  col_tag.write_to_stream (os_xml);
460  os_xml << '\n';
461  for( size_t i=0; i<sparse.colptr()->size()-1; i++) {
462  for( Index j=0; j<(*sparse.colptr())[i+1]-(*sparse.colptr())[i]; j++) {
463  if (pbofs)
464  *pbofs << (Index)i;
465  else
466  os_xml << (Index)i << '\n';
467  }
468  }
469  close_tag.set_name ("/ColIndex");
470  close_tag.write_to_stream (os_xml);
471  os_xml << '\n';
472 
473  data_tag.write_to_stream (os_xml);
474  os_xml << '\n';
475  xml_set_stream_precision (os_xml);
476  for( Index i=0; i<sparse.nnz(); i++) {
477  if (pbofs)
478  *pbofs << (*sparse.data())[i];
479  else
480  os_xml << (*sparse.data())[i] << ' ';
481  }
482  os_xml << '\n';
483  close_tag.set_name ("/SparseData");
484  close_tag.write_to_stream (os_xml);
485  os_xml << '\n';
486 
487  close_tag.set_name("/Sparse");
488  close_tag.write_to_stream( os_xml);
489 
490  os_xml << '\n';
491 }
492 
493 
494 //=== String ===========================================================
495 
497 
501 /* param pbifs Pointer to binary input stream. NULL in case of ASCII file.
502  Ignored because strings are always stored in ASCII format. */
503 void
504 xml_read_from_stream (istream& is_xml,
505  String& str,
506  bifstream * /* pbifs */, const Verbosity& verbosity)
507 {
508  ArtsXMLTag tag(verbosity);
509  char dummy;
510 
511  tag.read_from_stream (is_xml);
512  tag.check_name ("String");
513 
514  // Skip whitespaces
515  bool string_starts_with_quotes = true;
516  do
517  {
518  is_xml >> dummy;
519  switch (dummy)
520  {
521  case ' ':
522  case '\"':
523  case '\n':
524  case '\r':
525  case '\t':
526  break;
527  default:
528  string_starts_with_quotes = false;
529  }
530  } while (is_xml.good () && dummy != '"' && string_starts_with_quotes);
531 
532  // Throw exception if first char after whitespaces is not a quote
533  if (!string_starts_with_quotes)
534  {
535  xml_parse_error ("String must begin with \"");
536  }
537 
538  //catch case where string is empty. CPD 29/8/05
539  dummy=(char)is_xml.peek();
540  if (dummy=='"')
541  {
542  str = "";
543  }
544  else
545  {
546  stringbuf strbuf;
547 
548  is_xml.get (strbuf, '"');
549  if (is_xml.fail ())
550  {
551  xml_parse_error ("String must end with \"");
552  }
553  str = strbuf.str ();
554  }
555 
556  // Ignore quote
557  is_xml >> dummy;
558 
559  tag.read_from_stream (is_xml);
560  tag.check_name ("/String");
561 }
562 
563 
565 
570 /* param pbofs Pointer to binary file stream. NULL for ASCII output.
571  Ignored because strings are always in ASCII format. */
572 void
573 xml_write_to_stream (ostream& os_xml,
574  const String& str,
575  bofstream * /* pbofs */,
576  const String& name, const Verbosity& verbosity)
577 {
578  ArtsXMLTag open_tag(verbosity);
579  ArtsXMLTag close_tag(verbosity);
580 
581  open_tag.set_name ("String");
582  if (name.length ())
583  open_tag.add_attribute ("name", name);
584 
585  open_tag.write_to_stream (os_xml);
586 
587  os_xml << '\"' << str << '\"';
588 
589  close_tag.set_name ("/String");
590  close_tag.write_to_stream (os_xml);
591  os_xml << '\n';
592 }
593 
594 
595 //=== Tensor3 ================================================================
596 
598 
603 void
604 xml_read_from_stream (istream& is_xml,
605  Tensor3& tensor,
606  bifstream *pbifs, const Verbosity& verbosity)
607 {
608  ArtsXMLTag tag(verbosity);
609  Index npages, nrows, ncols;
610 
611  tag.read_from_stream (is_xml);
612  tag.check_name ("Tensor3");
613 
614  tag.get_attribute_value ("npages", npages);
615  tag.get_attribute_value ("nrows", nrows);
616  tag.get_attribute_value ("ncols", ncols);
617  tensor.resize (npages, nrows, ncols);
618 
619  for (Index p = 0; p < npages; p++)
620  {
621  for (Index r = 0; r < nrows; r++)
622  {
623  for (Index c = 0; c < ncols; c++)
624  {
625  if (pbifs)
626  {
627  *pbifs >> tensor (p, r, c);
628  if (pbifs->fail ())
629  {
630  ostringstream os;
631  os << " near "
632  << "\n Page : " << p
633  << "\n Row : " << r
634  << "\n Column: " << c;
635  xml_data_parse_error (tag, os.str());
636  }
637  }
638  else
639  {
640  is_xml >> tensor (p, r, c);
641  if (is_xml.fail ())
642  {
643  ostringstream os;
644  os << " near "
645  << "\n Page : " << p
646  << "\n Row : " << r
647  << "\n Column: " << c;
648  xml_data_parse_error (tag, os.str());
649  }
650  }
651  }
652  }
653  }
654 
655  tag.read_from_stream (is_xml);
656  tag.check_name ("/Tensor3");
657 }
658 
659 
661 
667 void
668 xml_write_to_stream (ostream& os_xml,
669  const Tensor3& tensor,
670  bofstream *pbofs,
671  const String& name, const Verbosity& verbosity)
672 {
673  ArtsXMLTag open_tag(verbosity);
674  ArtsXMLTag close_tag(verbosity);
675 
676  open_tag.set_name ("Tensor3");
677  if (name.length ())
678  open_tag.add_attribute ("name", name);
679  open_tag.add_attribute ("npages", tensor.npages ());
680  open_tag.add_attribute ("nrows", tensor.nrows ());
681  open_tag.add_attribute ("ncols", tensor.ncols ());
682 
683  open_tag.write_to_stream (os_xml);
684  os_xml << '\n';
685 
686  xml_set_stream_precision (os_xml);
687 
688  // Write the elements:
689  for (Index p = 0; p < tensor.npages (); ++p)
690  {
691  for (Index r = 0; r < tensor.nrows (); ++r)
692  {
693  if (pbofs)
694  *pbofs << tensor (p, r, 0);
695  else
696  os_xml << tensor (p, r, 0);
697  for (Index c = 1; c < tensor.ncols (); ++c)
698  {
699  if (pbofs)
700  *pbofs << tensor (p, r, c);
701  else
702  os_xml << " " << tensor (p, r, c);
703  }
704  if (!pbofs)
705  os_xml << '\n';
706  }
707  }
708 
709  close_tag.set_name ("/Tensor3");
710  close_tag.write_to_stream (os_xml);
711 
712  os_xml << '\n';
713 }
714 
715 
716 //=== Tensor4 =========================================================
717 
719 
724 void
725 xml_read_from_stream (istream& is_xml,
726  Tensor4& tensor,
727  bifstream *pbifs, const Verbosity& verbosity)
728 {
729  ArtsXMLTag tag(verbosity);
730  Index nbooks, npages, nrows, ncols;
731 
732  tag.read_from_stream (is_xml);
733  tag.check_name ("Tensor4");
734 
735  tag.get_attribute_value ("nbooks", nbooks);
736  tag.get_attribute_value ("npages", npages);
737  tag.get_attribute_value ("nrows", nrows);
738  tag.get_attribute_value ("ncols", ncols);
739  tensor.resize (nbooks,npages, nrows, ncols);
740 
741  for (Index b = 0; b < nbooks; b++)
742  {
743  for (Index p = 0; p < npages; p++)
744  {
745  for (Index r = 0; r < nrows; r++)
746  {
747  for (Index c = 0; c < ncols; c++)
748  {
749  if (pbifs)
750  {
751  *pbifs >> tensor (b, p, r, c);
752  if (pbifs->fail ())
753  {
754  ostringstream os;
755  os << " near "
756  << "\n Book : " << b
757  << "\n Page : " << p
758  << "\n Row : " << r
759  << "\n Column: " << c;
760  xml_data_parse_error (tag, os.str());
761  }
762  }
763  else
764  {
765  is_xml >> tensor (b, p, r, c);
766  if (is_xml.fail ())
767  {
768  ostringstream os;
769  os << " near "
770  << "\n Book : " << b
771  << "\n Page : " << p
772  << "\n Row : " << r
773  << "\n Column: " << c;
774  xml_data_parse_error (tag, os.str());
775  }
776  }
777  }
778  }
779  }
780  }
781 
782  tag.read_from_stream (is_xml);
783  tag.check_name ("/Tensor4");
784 }
785 
786 
788 
794 void
795 xml_write_to_stream (ostream& os_xml,
796  const Tensor4& tensor,
797  bofstream *pbofs,
798  const String& name, const Verbosity& verbosity)
799 {
800  ArtsXMLTag open_tag(verbosity);
801  ArtsXMLTag close_tag(verbosity);
802 
803  open_tag.set_name ("Tensor4");
804  if (name.length ())
805  open_tag.add_attribute ("name", name);
806  open_tag.add_attribute ("nbooks", tensor.nbooks ());
807  open_tag.add_attribute ("npages", tensor.npages ());
808  open_tag.add_attribute ("nrows", tensor.nrows ());
809  open_tag.add_attribute ("ncols", tensor.ncols ());
810 
811  open_tag.write_to_stream (os_xml);
812  os_xml << '\n';
813 
814  xml_set_stream_precision (os_xml);
815 
816  // Write the elements:
817  for (Index b = 0; b < tensor.nbooks (); ++b)
818  {
819  for (Index p = 0; p < tensor.npages (); ++p)
820  {
821  for (Index r = 0; r < tensor.nrows (); ++r)
822  {
823  if (pbofs)
824  *pbofs << tensor (b, p, r, 0);
825  else
826  os_xml << tensor (b, p, r, 0);
827  for (Index c = 1; c < tensor.ncols (); ++c)
828  {
829  if (pbofs)
830  *pbofs << tensor (b, p, r, c);
831  else
832  os_xml << " " << tensor (b, p, r, c);
833  }
834  if (!pbofs)
835  os_xml << '\n';
836  }
837  }
838  }
839 
840  close_tag.set_name ("/Tensor4");
841  close_tag.write_to_stream (os_xml);
842 
843  os_xml << '\n';
844 }
845 
846 
847 //=== Tensor5 =========================================================
848 
850 
855 void
856 xml_read_from_stream (istream& is_xml,
857  Tensor5& tensor,
858  bifstream *pbifs, const Verbosity& verbosity)
859 {
860  ArtsXMLTag tag(verbosity);
861  Index nshelves, nbooks, npages, nrows, ncols;
862 
863  tag.read_from_stream (is_xml);
864  tag.check_name ("Tensor5");
865 
866  tag.get_attribute_value ("nshelves", nshelves);
867  tag.get_attribute_value ("nbooks", nbooks);
868  tag.get_attribute_value ("npages", npages);
869  tag.get_attribute_value ("nrows", nrows);
870  tag.get_attribute_value ("ncols", ncols);
871  tensor.resize (nshelves, nbooks,npages, nrows, ncols);
872 
873  for (Index s = 0; s < nshelves; s++)
874  {
875  for (Index b = 0; b < nbooks; b++)
876  {
877  for (Index p = 0; p < npages; p++)
878  {
879  for (Index r = 0; r < nrows; r++)
880  {
881  for (Index c = 0; c < ncols; c++)
882  {
883  if (pbifs)
884  {
885  *pbifs >> tensor (s, b, p, r, c);
886  if (pbifs->fail ())
887  {
888  ostringstream os;
889  os << " near "
890  << "\n Shelf : " << s
891  << "\n Book : " << b
892  << "\n Page : " << p
893  << "\n Row : " << r
894  << "\n Column: " << c;
895  xml_data_parse_error (tag, os.str());
896  }
897  }
898  else
899  {
900  is_xml >> tensor (s, b, p, r, c);
901  if (is_xml.fail ())
902  {
903  ostringstream os;
904  os << " near "
905  << "\n Shelf : " << s
906  << "\n Book : " << b
907  << "\n Page : " << p
908  << "\n Row : " << r
909  << "\n Column: " << c;
910  xml_data_parse_error (tag, os.str());
911  }
912  }
913  }
914  }
915  }
916  }
917  }
918 
919  tag.read_from_stream (is_xml);
920  tag.check_name ("/Tensor5");
921 }
922 
923 
925 
931 void
932 xml_write_to_stream (ostream& os_xml,
933  const Tensor5& tensor,
934  bofstream *pbofs,
935  const String& name, const Verbosity& verbosity)
936 {
937  ArtsXMLTag open_tag(verbosity);
938  ArtsXMLTag close_tag(verbosity);
939 
940  open_tag.set_name ("Tensor5");
941  if (name.length ())
942  open_tag.add_attribute ("name", name);
943  open_tag.add_attribute ("nshelves", tensor.nshelves ());
944  open_tag.add_attribute ("nbooks", tensor.nbooks ());
945  open_tag.add_attribute ("npages", tensor.npages ());
946  open_tag.add_attribute ("nrows", tensor.nrows ());
947  open_tag.add_attribute ("ncols", tensor.ncols ());
948 
949  open_tag.write_to_stream (os_xml);
950  os_xml << '\n';
951 
952  xml_set_stream_precision (os_xml);
953 
954  // Write the elements:
955  for (Index s = 0; s < tensor.nshelves (); ++s)
956  {
957  for (Index b = 0; b < tensor.nbooks (); ++b)
958  {
959  for (Index p = 0; p < tensor.npages (); ++p)
960  {
961  for (Index r = 0; r < tensor.nrows (); ++r)
962  {
963  if (pbofs)
964  *pbofs << tensor (s, b, p, r, 0);
965  else
966  os_xml << tensor (s, b, p, r, 0);
967  for (Index c = 1; c < tensor.ncols (); ++c)
968  {
969  if (pbofs)
970  *pbofs << tensor (s, b, p, r, c);
971  else
972  os_xml << " " << tensor (s, b, p, r, c);
973  }
974  if (!pbofs)
975  os_xml << '\n';
976  }
977  }
978  }
979  }
980 
981  close_tag.set_name ("/Tensor5");
982  close_tag.write_to_stream (os_xml);
983 
984  os_xml << '\n';
985 }
986 
987 
988 //=== Tensor6 =========================================================
989 
991 
996 void
997 xml_read_from_stream (istream& is_xml,
998  Tensor6& tensor,
999  bifstream *pbifs, const Verbosity& verbosity)
1000 {
1001  ArtsXMLTag tag(verbosity);
1002  Index nvitrines, nshelves, nbooks, npages, nrows, ncols;
1003 
1004  tag.read_from_stream (is_xml);
1005  tag.check_name ("Tensor6");
1006 
1007  tag.get_attribute_value ("nvitrines", nvitrines);
1008  tag.get_attribute_value ("nshelves", nshelves);
1009  tag.get_attribute_value ("nbooks", nbooks);
1010  tag.get_attribute_value ("npages", npages);
1011  tag.get_attribute_value ("nrows", nrows);
1012  tag.get_attribute_value ("ncols", ncols);
1013  tensor.resize (nvitrines, nshelves, nbooks,npages, nrows, ncols);
1014 
1015  for (Index v = 0; v < nvitrines; v++)
1016  {
1017  for (Index s = 0; s < nshelves; s++)
1018  {
1019  for (Index b = 0; b < nbooks; b++)
1020  {
1021  for (Index p = 0; p < npages; p++)
1022  {
1023  for (Index r = 0; r < nrows; r++)
1024  {
1025  for (Index c = 0; c < ncols; c++)
1026  {
1027  if (pbifs)
1028  {
1029  *pbifs >> tensor (v, s, b, p, r, c);
1030  if (pbifs->fail ())
1031  {
1032  ostringstream os;
1033  os << " near "
1034  << "\n Vitrine: " << v
1035  << "\n Shelf : " << s
1036  << "\n Book : " << b
1037  << "\n Page : " << p
1038  << "\n Row : " << r
1039  << "\n Column : " << c;
1040  xml_data_parse_error (tag, os.str());
1041  }
1042  }
1043  else
1044  {
1045  is_xml >> tensor (v, s, b, p, r, c);
1046  if (is_xml.fail ())
1047  {
1048  ostringstream os;
1049  os << " near "
1050  << "\n Vitrine: " << v
1051  << "\n Shelf : " << s
1052  << "\n Book : " << b
1053  << "\n Page : " << p
1054  << "\n Row : " << r
1055  << "\n Column : " << c;
1056  xml_data_parse_error (tag, os.str());
1057  }
1058  }
1059  }
1060  }
1061  }
1062  }
1063  }
1064  }
1065 
1066  tag.read_from_stream (is_xml);
1067  tag.check_name ("/Tensor6");
1068 }
1069 
1070 
1072 
1078 void
1079 xml_write_to_stream (ostream& os_xml,
1080  const Tensor6& tensor,
1081  bofstream *pbofs,
1082  const String& name, const Verbosity& verbosity)
1083 {
1084  ArtsXMLTag open_tag(verbosity);
1085  ArtsXMLTag close_tag(verbosity);
1086 
1087  open_tag.set_name ("Tensor6");
1088  if (name.length ())
1089  open_tag.add_attribute ("name", name);
1090  open_tag.add_attribute ("nvitrines", tensor.nvitrines ());
1091  open_tag.add_attribute ("nshelves", tensor.nshelves ());
1092  open_tag.add_attribute ("nbooks", tensor.nbooks ());
1093  open_tag.add_attribute ("npages", tensor.npages ());
1094  open_tag.add_attribute ("nrows", tensor.nrows ());
1095  open_tag.add_attribute ("ncols", tensor.ncols ());
1096 
1097  open_tag.write_to_stream (os_xml);
1098  os_xml << '\n';
1099 
1100  xml_set_stream_precision (os_xml);
1101 
1102  // Write the elements:
1103  for (Index v = 0; v < tensor.nvitrines (); ++v)
1104  {
1105  for (Index s = 0; s < tensor.nshelves (); ++s)
1106  {
1107  for (Index b = 0; b < tensor.nbooks (); ++b)
1108  {
1109  for (Index p = 0; p < tensor.npages (); ++p)
1110  {
1111  for (Index r = 0; r < tensor.nrows (); ++r)
1112  {
1113  if (pbofs)
1114  *pbofs << tensor (v, s, b, p, r, 0);
1115  else
1116  os_xml << tensor (v, s, b, p, r, 0);
1117  for (Index c = 1; c < tensor.ncols (); ++c)
1118  {
1119  if (pbofs)
1120  *pbofs << tensor (v, s, b, p, r, c);
1121  else
1122  os_xml << " " << tensor (v, s, b, p, r, c);
1123  }
1124  if (!pbofs)
1125  os_xml << '\n';
1126  }
1127  }
1128  }
1129  }
1130  }
1131 
1132  close_tag.set_name ("/Tensor6");
1133  close_tag.write_to_stream (os_xml);
1134 
1135  os_xml << '\n';
1136 }
1137 
1138 
1139 //=== Tensor7 =========================================================
1140 
1142 
1147 void
1148 xml_read_from_stream (istream& is_xml,
1149  Tensor7& tensor,
1150  bifstream *pbifs, const Verbosity& verbosity)
1151 {
1152  ArtsXMLTag tag(verbosity);
1153  Index nlibraries, nvitrines, nshelves, nbooks, npages, nrows, ncols;
1154 
1155  tag.read_from_stream (is_xml);
1156  tag.check_name ("Tensor7");
1157 
1158  tag.get_attribute_value ("nlibraries", nlibraries);
1159  tag.get_attribute_value ("nvitrines", nvitrines);
1160  tag.get_attribute_value ("nshelves", nshelves);
1161  tag.get_attribute_value ("nbooks", nbooks);
1162  tag.get_attribute_value ("npages", npages);
1163  tag.get_attribute_value ("nrows", nrows);
1164  tag.get_attribute_value ("ncols", ncols);
1165  tensor.resize (nlibraries, nvitrines, nshelves, nbooks,npages, nrows, ncols);
1166 
1167  for (Index l = 0; l < nlibraries; l++)
1168  {
1169  for (Index v = 0; v < nvitrines; v++)
1170  {
1171  for (Index s = 0; s < nshelves; s++)
1172  {
1173  for (Index b = 0; b < nbooks; b++)
1174  {
1175  for (Index p = 0; p < npages; p++)
1176  {
1177  for (Index r = 0; r < nrows; r++)
1178  {
1179  for (Index c = 0; c < ncols; c++)
1180  {
1181  if (pbifs)
1182  {
1183  *pbifs >> tensor (l, v, s, b, p, r, c);
1184  if (pbifs->fail ())
1185  {
1186  ostringstream os;
1187  os << " near "
1188  << "\n Library: " << l
1189  << "\n Vitrine: " << v
1190  << "\n Shelf : " << s
1191  << "\n Book : " << b
1192  << "\n Page : " << p
1193  << "\n Row : " << r
1194  << "\n Column : " << c;
1195  xml_data_parse_error (tag, os.str());
1196  }
1197  }
1198  else
1199  {
1200  is_xml >> tensor (l, v, s, b, p, r, c);
1201  if (is_xml.fail ())
1202  {
1203  ostringstream os;
1204  os << " near "
1205  << "\n Library: " << l
1206  << "\n Vitrine: " << v
1207  << "\n Shelf : " << s
1208  << "\n Book : " << b
1209  << "\n Page : " << p
1210  << "\n Row : " << r
1211  << "\n Column : " << c;
1212  xml_data_parse_error (tag, os.str());
1213  }
1214  }
1215  }
1216  }
1217  }
1218  }
1219  }
1220  }
1221  }
1222 
1223  tag.read_from_stream (is_xml);
1224  tag.check_name ("/Tensor7");
1225 }
1226 
1227 
1229 
1235 void
1236 xml_write_to_stream (ostream& os_xml,
1237  const Tensor7& tensor,
1238  bofstream *pbofs,
1239  const String& name, const Verbosity& verbosity)
1240 {
1241  ArtsXMLTag open_tag(verbosity);
1242  ArtsXMLTag close_tag(verbosity);
1243 
1244  open_tag.set_name ("Tensor7");
1245  if (name.length ())
1246  open_tag.add_attribute ("name", name);
1247  open_tag.add_attribute ("nlibraries", tensor.nlibraries ());
1248  open_tag.add_attribute ("nvitrines", tensor.nvitrines ());
1249  open_tag.add_attribute ("nshelves", tensor.nshelves ());
1250  open_tag.add_attribute ("nbooks", tensor.nbooks ());
1251  open_tag.add_attribute ("npages", tensor.npages ());
1252  open_tag.add_attribute ("nrows", tensor.nrows ());
1253  open_tag.add_attribute ("ncols", tensor.ncols ());
1254 
1255  open_tag.write_to_stream (os_xml);
1256  os_xml << '\n';
1257 
1258  xml_set_stream_precision (os_xml);
1259 
1260  // Write the elements:
1261  for (Index l = 0; l < tensor.nlibraries (); ++l)
1262  {
1263  for (Index v = 0; v < tensor.nvitrines (); ++v)
1264  {
1265  for (Index s = 0; s < tensor.nshelves (); ++s)
1266  {
1267  for (Index b = 0; b < tensor.nbooks (); ++b)
1268  {
1269  for (Index p = 0; p < tensor.npages (); ++p)
1270  {
1271  for (Index r = 0; r < tensor.nrows (); ++r)
1272  {
1273  if (pbofs)
1274  *pbofs << tensor (l, v, s, b, p, r, 0);
1275  else
1276  os_xml << tensor (l, v, s, b, p, r, 0);
1277  for (Index c = 1; c < tensor.ncols (); ++c)
1278  {
1279  if (pbofs)
1280  *pbofs << tensor (l, v, s, b, p, r, c);
1281  else
1282  os_xml << " " << tensor (l, v, s, b, p, r, c);
1283  }
1284  if (!pbofs)
1285  os_xml << '\n';
1286  }
1287  }
1288  }
1289  }
1290  }
1291  }
1292 
1293  close_tag.set_name ("/Tensor7");
1294  close_tag.write_to_stream (os_xml);
1295 
1296  os_xml << '\n';
1297 }
1298 
1299 
1300 //=== Vector ==========================================================
1301 
1303 
1309 void
1310 xml_parse_from_stream (istream& is_xml,
1311  Vector& vector,
1312  bifstream *pbifs,
1313  ArtsXMLTag& tag,
1314  const Verbosity&)
1315 {
1316  Index nelem;
1317 
1318  tag.get_attribute_value ("nelem", nelem);
1319  vector.resize (nelem);
1320 
1321  for (Index n = 0; n < nelem; n++)
1322  {
1323  if (pbifs)
1324  {
1325  *pbifs >> vector[n];
1326  if (pbifs->fail ())
1327  {
1328  ostringstream os;
1329  os << " near "
1330  << "\n Element: " << n;
1331  xml_data_parse_error (tag, os.str());
1332  }
1333  }
1334  else
1335  {
1336  is_xml >> vector[n];
1337  if (is_xml.fail ())
1338  {
1339  ostringstream os;
1340  os << " near "
1341  << "\n Element: " << n;
1342  xml_data_parse_error (tag, os.str());
1343  }
1344  }
1345  }
1346 }
1347 
1348 
1350 
1355 void
1356 xml_read_from_stream (istream& is_xml,
1357  Vector& vector,
1358  bifstream *pbifs, const Verbosity& verbosity)
1359 {
1360  ArtsXMLTag tag(verbosity);
1361 
1362  tag.read_from_stream (is_xml);
1363  tag.check_name ("Vector");
1364 
1365  xml_parse_from_stream (is_xml, vector, pbifs, tag, verbosity);
1366 
1367  tag.read_from_stream (is_xml);
1368  tag.check_name ("/Vector");
1369 }
1370 
1371 
1373 
1379 void
1380 xml_write_to_stream (ostream& os_xml,
1381  const Vector& vector,
1382  bofstream *pbofs,
1383  const String& name, const Verbosity& verbosity)
1384 {
1385  ArtsXMLTag open_tag(verbosity);
1386  ArtsXMLTag close_tag(verbosity);
1387  Index n = vector.nelem ();
1388  ostringstream v;
1389 
1390  // Convert nelem to string
1391  v << n;
1392 
1393  open_tag.set_name ("Vector");
1394  if (name.length ())
1395  open_tag.add_attribute ("name", name);
1396  open_tag.add_attribute ("nelem", v.str ());
1397 
1398  open_tag.write_to_stream (os_xml);
1399  os_xml << '\n';
1400 
1401  xml_set_stream_precision (os_xml);
1402 
1403  for (Index i=0; i<n; ++i)
1404  if (pbofs)
1405  *pbofs << vector[i];
1406  else
1407  os_xml << vector[i] << '\n';
1408 
1409  close_tag.set_name ("/Vector");
1410  close_tag.write_to_stream (os_xml);
1411 
1412  os_xml << '\n';
1413 }
1414 
1416 // Dummy funtion for groups for which
1417 // IO function have not yet been implemented
1419 
1420 // FIXME: These should be implemented, sooner or later...
1421 
1422 void
1424  Timer&,
1425  bifstream * /* pbifs */,
1426  const Verbosity&)
1427 {
1428  throw runtime_error("Method not implemented!");
1429 }
1430 
1431 void
1433  const Timer&,
1434  bofstream * /* pbofs */,
1435  const String& /* name */,
1436  const Verbosity&)
1437 {
1438  throw runtime_error("Method not implemented!");
1439 }
1440 
1441 
Matrix
The Matrix class.
Definition: matpackI.h:767
Timer
Definition: m_general.h:54
Tensor4::resize
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1404
ConstTensor7View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackVII.cc:43
ConstTensor6View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackVI.cc:37
ConstTensor5View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:38
ArtsXMLTag::check_name
void check_name(const String &expected_name)
Check tag name.
Definition: xml_io.cc:56
Tensor6::resize
void resize(Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVI.cc:2845
Sparse::colptr
const vector< Index > * colptr() const
Definition: matpackII.h:81
Sparse::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:59
Tensor3
The Tensor3 class.
Definition: matpackIII.h:340
ConstTensor5View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:56
ConstTensor6View::npages
Index npages() const
Returns the number of pages.
Definition: matpackVI.cc:49
ConstTensor7View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackVII.cc:67
Sparse
The Sparse class.
Definition: matpackII.h:55
ConstTensor6View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackVI.cc:55
Vector::resize
void resize(Index n)
Resize function.
Definition: matpackI.cc:771
Sparse::rowind
const vector< Index > * rowind() const
Definition: matpackII.h:80
ArtsXMLTag::get_attribute_value
void get_attribute_value(const String &aname, String &value)
Returns value of attribute as String.
Definition: xml_io.cc:140
ConstMatrixView::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackI.cc:796
xml_set_stream_precision
void xml_set_stream_precision(ostream &os)
Definition: xml_io.cc:818
Tensor4
The Tensor4 class.
Definition: matpackIV.h:375
ArtsXMLTag::write_to_stream
void write_to_stream(ostream &os)
Write XML tag.
Definition: xml_io.cc:333
Tensor3::resize
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:863
ConstTensor5View::npages
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:44
ConstTensor3View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:151
ConstTensor7View::nlibraries
Index nlibraries() const
Returns the number of libraries.
Definition: matpackVII.cc:31
xml_parse_from_stream
void xml_parse_from_stream(istream &is_xml, Vector &vector, bifstream *pbifs, ArtsXMLTag &tag, const Verbosity &)
Parses Vector from XML input stream.
Definition: xml_io_basic_types.cc:1310
Array< Index >
xml_io_private.h
This file contains private function declarations and template instantiation to handle XML data files.
ConstTensor6View::nvitrines
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVI.cc:31
Tensor7::resize
void resize(Index l, Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVII.cc:5341
Tensor5::resize
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:2435
Sparse::nnz
Index nnz() const
Returns the number of nonzero elements.
Definition: matpackII.cc:65
xml_read_from_stream
void xml_read_from_stream(istream &is_xml, Index &index, bifstream *pbifs, const Verbosity &verbosity)
Reads Index from XML input stream.
Definition: xml_io_basic_types.cc:50
ConstMatrixView::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackI.cc:802
ArtsXMLTag::read_from_stream
void read_from_stream(istream &is)
Reads next XML tag.
Definition: xml_io.cc:194
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:62
xml_parse_error
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
Definition: xml_io.cc:620
ConstVectorView::nelem
Index nelem() const
Returns the number of elements.
Definition: matpackI.cc:175
ConstTensor7View::nvitrines
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVII.cc:37
xml_io_types.h
This file contains private function declarations and template instantiation to handle XML data files.
ConstTensor4View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:78
ConstTensor7View::npages
Index npages() const
Returns the number of pages.
Definition: matpackVII.cc:55
Numeric
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Verbosity
Definition: messages.h:50
ArtsXMLTag::add_attribute
void add_attribute(const String &aname, const String &value)
Adds a String attribute to tag.
Definition: xml_io.cc:74
bifstream
Binary output file stream class.
Definition: bifstream.h:45
ConstTensor4View::npages
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:66
ConstTensor4View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:60
Matrix::resize
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1549
ConstTensor3View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:154
ConstTensor6View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackVI.cc:43
Tensor5
The Tensor5 class.
Definition: matpackV.h:443
ConstTensor4View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:72
Sparse::resize
void resize(Index r, Index c)
Resize function.
Definition: matpackII.cc:516
ConstTensor7View::nbooks
Index nbooks() const
Returns the number of books.
Definition: matpackVII.cc:49
ConstTensor5View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:50
xml_write_to_stream
void xml_write_to_stream(ostream &os_xml, const Index &index, bofstream *pbofs, const String &name, const Verbosity &verbosity)
Writes Index to XML output stream.
Definition: xml_io_basic_types.cc:89
Sparse::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:53
xml_data_parse_error
void xml_data_parse_error(ArtsXMLTag &tag, String str_error)
Throws XML parser runtime error.
Definition: xml_io.cc:638
ConstTensor5View::nshelves
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:32
ConstTensor3View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:157
Sparse::rw
Numeric & rw(Index r, Index c)
Read and write index operator.
Definition: matpackII.cc:90
ArtsXMLTag
The ARTS XML tag class.
Definition: xml_io_private.h:83
Sparse::data
const vector< Numeric > * data() const
Definition: matpackII.h:79
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
ConstTensor7View::nrows
Index nrows() const
Returns the number of rows.
Definition: matpackVII.cc:61
Tensor6
The Tensor6 class.
Definition: matpackVI.h:937
Vector
The Vector class.
Definition: matpackI.h:555
ArtsXMLTag::set_name
void set_name(const String &new_name)
Definition: xml_io_private.h:94
ConstTensor6View::ncols
Index ncols() const
Returns the number of columns.
Definition: matpackVI.cc:61
Tensor7
The Tensor7 class.
Definition: matpackVII.h:1912
arts.h
The global header file for ARTS.
xml_io.h
This file contains basic functions to handle XML data files.
bofstream
Binary output file stream class.
Definition: bofstream.h:45