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