ARTS 2.5.11 (git: 725533f0)
xml_io_general_types.cc
Go to the documentation of this file.
1
2// File description
4
13#include "arts.h"
14#include "xml_io_base.h"
15#include "double_imanip.h"
16
18// Overloaded functions for reading/writing data from/to XML stream
20
21//=== Index ==================================================================
22
24
29void xml_read_from_stream(istream& is_xml,
30 Index& index,
31 bifstream* pbifs,
32 const Verbosity& verbosity) {
33 XMLTag tag(verbosity);
34
35 tag.read_from_stream(is_xml);
36 tag.check_name("Index");
37
38 if (pbifs) {
39 *pbifs >> index;
40 if (pbifs->fail()) {
41 xml_data_parse_error(tag, "");
42 }
43 } else {
44 is_xml >> index;
45 if (is_xml.fail()) {
46 xml_data_parse_error(tag, "");
47 }
48 }
49
50 tag.read_from_stream(is_xml);
51 tag.check_name("/Index");
52}
53
55
61void xml_write_to_stream(ostream& os_xml,
62 const Index& index,
63 bofstream* pbofs,
64 const String& name,
65 const Verbosity& verbosity) {
66 XMLTag open_tag(verbosity);
67 XMLTag close_tag(verbosity);
68
69 open_tag.set_name("Index");
70 if (name.length()) open_tag.add_attribute("name", name);
71
72 open_tag.write_to_stream(os_xml);
73
74 if (pbofs)
75 *pbofs << index;
76 else
77 os_xml << index;
78
79 close_tag.set_name("/Index");
80 close_tag.write_to_stream(os_xml);
81 os_xml << '\n';
82}
83
84//=== Matrix ==========================================================
85
87
92void xml_read_from_stream(istream& is_xml,
93 Matrix& matrix,
94 bifstream* pbifs,
95 const Verbosity& verbosity) {
96 XMLTag tag(verbosity);
97 Index nrows, ncols;
98
99 tag.read_from_stream(is_xml);
100 tag.check_name("Matrix");
101
102 tag.get_attribute_value("nrows", nrows);
103 tag.get_attribute_value("ncols", ncols);
104 matrix.resize(nrows, ncols);
105
106 if (pbifs) {
107 pbifs->readDoubleArray(matrix.data_handle(), nrows * ncols);
108 } else {
109 for (Index r = 0; r < nrows; r++) {
110 for (Index c = 0; c < ncols; c++) {
111 is_xml >> double_imanip() >> matrix(r, c);
112 if (is_xml.fail()) {
113 ostringstream os;
114 os << " near "
115 << "\n Row : " << r << "\n Column: " << c;
116 xml_data_parse_error(tag, os.str());
117 }
118 }
119 }
120 }
121
122 tag.read_from_stream(is_xml);
123 tag.check_name("/Matrix");
124}
125
127
133void xml_write_to_stream(ostream& os_xml,
134 const Matrix& matrix,
135 bofstream* pbofs,
136 const String& name,
137 const Verbosity& verbosity) {
138 XMLTag open_tag(verbosity);
139 XMLTag close_tag(verbosity);
140
141 open_tag.set_name("Matrix");
142 if (name.length()) open_tag.add_attribute("name", name);
143 open_tag.add_attribute("nrows", matrix.nrows());
144 open_tag.add_attribute("ncols", matrix.ncols());
145
146 open_tag.write_to_stream(os_xml);
147 os_xml << '\n';
148
150
151 // Write the elements:
152 for (Index r = 0; r < matrix.nrows(); ++r) {
153 if (pbofs)
154 *pbofs << matrix(r, 0);
155 else
156 os_xml << matrix(r, 0);
157
158 for (Index c = 1; c < matrix.ncols(); ++c) {
159 if (pbofs)
160 *pbofs << matrix(r, c);
161 else
162 os_xml << " " << matrix(r, c);
163 }
164
165 if (!pbofs) os_xml << '\n';
166 }
167
168 close_tag.set_name("/Matrix");
169 close_tag.write_to_stream(os_xml);
170
171 os_xml << '\n';
172}
173
174//=== Numeric =========================================================
175
177
182void xml_read_from_stream(istream& is_xml,
183 Numeric& numeric,
184 bifstream* pbifs,
185 const Verbosity& verbosity) {
186 XMLTag tag(verbosity);
187
188 tag.read_from_stream(is_xml);
189 tag.check_name("Numeric");
190
191 if (pbifs) {
192 *pbifs >> numeric;
193 if (pbifs->fail()) {
194 xml_data_parse_error(tag, "");
195 }
196 } else {
197 is_xml >> double_imanip() >> numeric;
198 if (is_xml.fail()) {
199 xml_data_parse_error(tag, "");
200 }
201 }
202
203 tag.read_from_stream(is_xml);
204 tag.check_name("/Numeric");
205}
206
208
214void xml_write_to_stream(ostream& os_xml,
215 const Numeric& numeric,
216 bofstream* pbofs,
217 const String& name,
218 const Verbosity& verbosity) {
219 XMLTag open_tag(verbosity);
220 XMLTag close_tag(verbosity);
221
222 open_tag.set_name("Numeric");
223 if (name.length()) open_tag.add_attribute("name", name);
224
225 open_tag.write_to_stream(os_xml);
226
228
229 if (pbofs)
230 *pbofs << numeric;
231 else
232 os_xml << numeric;
233
234 close_tag.set_name("/Numeric");
235 close_tag.write_to_stream(os_xml);
236 os_xml << '\n';
237}
238
239//=== Sparse ====================================================
240
242
247void xml_read_from_stream(istream& is_xml,
248 Sparse& sparse,
249 bifstream* pbifs,
250 const Verbosity& verbosity) {
251 XMLTag tag(verbosity);
252 Index nrows, ncols, nnz;
253
254 tag.read_from_stream(is_xml);
255 tag.check_name("Sparse");
256
257 tag.get_attribute_value("nrows", nrows);
258 tag.get_attribute_value("ncols", ncols);
259 sparse.resize(nrows, ncols);
260
261 tag.read_from_stream(is_xml);
262 tag.check_name("RowIndex");
263 tag.get_attribute_value("nelem", nnz);
264
265 ArrayOfIndex rowind(nnz), colind(nnz);
266 Vector data(nnz);
267
268 for (Index i = 0; i < nnz; i++) {
269 if (pbifs) {
270 *pbifs >> rowind[i];
271 if (pbifs->fail()) {
272 ostringstream os;
273 os << " near "
274 << "\n Row index: " << i;
275 xml_data_parse_error(tag, os.str());
276 }
277 } else {
278 is_xml >> rowind[i];
279 if (is_xml.fail()) {
280 ostringstream os;
281 os << " near "
282 << "\n Row index: " << i;
283 xml_data_parse_error(tag, os.str());
284 }
285 }
286 }
287 tag.read_from_stream(is_xml);
288 tag.check_name("/RowIndex");
289
290 tag.read_from_stream(is_xml);
291 tag.check_name("ColIndex");
292
293 for (Index i = 0; i < nnz; i++) {
294 if (pbifs) {
295 *pbifs >> colind[i];
296 if (pbifs->fail()) {
297 ostringstream os;
298 os << " near "
299 << "\n Column index: " << i;
300 xml_data_parse_error(tag, os.str());
301 }
302 } else {
303 is_xml >> colind[i];
304 if (is_xml.fail()) {
305 ostringstream os;
306 os << " near "
307 << "\n Column index: " << i;
308 xml_data_parse_error(tag, os.str());
309 }
310 }
311 }
312 tag.read_from_stream(is_xml);
313 tag.check_name("/ColIndex");
314
315 tag.read_from_stream(is_xml);
316 tag.check_name("SparseData");
317
318 if (pbifs) {
319 pbifs->readDoubleArray(data.data_handle(), nnz);
320 } else {
321 for (Index i = 0; i < nnz; i++) {
322 is_xml >> double_imanip() >> data[i];
323 if (is_xml.fail()) {
324 ostringstream os;
325 os << " near "
326 << "\n Data element: " << i;
327 xml_data_parse_error(tag, os.str());
328 }
329 }
330 }
331 tag.read_from_stream(is_xml);
332 tag.check_name("/SparseData");
333
334 tag.read_from_stream(is_xml);
335 tag.check_name("/Sparse");
336
337 sparse.insert_elements(nnz, rowind, colind, data);
338}
339
341
347void xml_write_to_stream(ostream& os_xml,
348 const Sparse& sparse,
349 bofstream* pbofs,
350 const String& name,
351 const Verbosity& verbosity) {
352 XMLTag sparse_tag(verbosity);
353 XMLTag row_tag(verbosity);
354 XMLTag col_tag(verbosity);
355 XMLTag data_tag(verbosity);
356 XMLTag close_tag(verbosity);
357
358 sparse_tag.set_name("Sparse");
359 if (name.length()) sparse_tag.add_attribute("name", name);
360 sparse_tag.add_attribute("nrows", sparse.nrows());
361 sparse_tag.add_attribute("ncols", sparse.ncols());
362 //sparse_tag.add_attribute ("nnz", sparse.nnz());
363 row_tag.set_name("RowIndex");
364 row_tag.add_attribute("nelem", sparse.nnz());
365 col_tag.set_name("ColIndex");
366 col_tag.add_attribute("nelem", sparse.nnz());
367 data_tag.set_name("SparseData");
368 data_tag.add_attribute("nelem", sparse.nnz());
369
370 sparse_tag.write_to_stream(os_xml);
371 os_xml << '\n';
372
373 row_tag.write_to_stream(os_xml);
374 os_xml << '\n';
375
376 ArrayOfIndex rowind(sparse.nnz()), colind(sparse.nnz());
377 Vector data(sparse.nnz());
378 sparse.list_elements(data, rowind, colind);
379
380 // Write row indices.
381
382 for (Index i = 0; i < sparse.nnz(); i++) {
383 if (pbofs)
384 //FIXME: It should be the longer lines
385 *pbofs << rowind[i];
386 else
387 os_xml << rowind[i] << '\n';
388 }
389
390 close_tag.set_name("/RowIndex");
391 close_tag.write_to_stream(os_xml);
392 os_xml << '\n';
393
394 col_tag.write_to_stream(os_xml);
395 os_xml << '\n';
396
397 // Write column indices.
398
399 for (Index i = 0; i < sparse.nnz(); i++) {
400 if (pbofs)
401 //FIXME: It should be the longer lines
402 *pbofs << colind[i];
403 else
404 os_xml << colind[i] << '\n';
405 }
406
407 close_tag.set_name("/ColIndex");
408 close_tag.write_to_stream(os_xml);
409 os_xml << '\n';
410
411 data_tag.write_to_stream(os_xml);
412 os_xml << '\n';
414
415 // Write data.
416
417 for (Index i = 0; i < sparse.nnz(); i++) {
418 if (pbofs)
419 *pbofs << data[i];
420 else
421 os_xml << data[i] << ' ';
422 }
423 os_xml << '\n';
424 close_tag.set_name("/SparseData");
425 close_tag.write_to_stream(os_xml);
426 os_xml << '\n';
427
428 close_tag.set_name("/Sparse");
429 close_tag.write_to_stream(os_xml);
430
431 os_xml << '\n';
432}
433
434//=== String ===========================================================
435
437
441/* param pbifs Pointer to binary input stream. NULL in case of ASCII file.
442 Ignored because strings are always stored in ASCII format. */
443void xml_read_from_stream(istream& is_xml,
444 String& str,
445 bifstream* /* pbifs */,
446 const Verbosity& verbosity) {
447 XMLTag tag(verbosity);
448 char dummy;
449
450 tag.read_from_stream(is_xml);
451 tag.check_name("String");
452
453 // Skip whitespaces
454 bool string_starts_with_quotes = true;
455 do {
456 is_xml >> dummy;
457 switch (dummy) {
458 case ' ':
459 case '\"':
460 case '\n':
461 case '\r':
462 case '\t':
463 break;
464 default:
465 string_starts_with_quotes = false;
466 }
467 } while (is_xml.good() && dummy != '"' && string_starts_with_quotes);
468
469 // Throw exception if first char after whitespaces is not a quote
470 if (!string_starts_with_quotes) {
471 xml_parse_error("String must begin with \"");
472 }
473
474 //catch case where string is empty. CPD 29/8/05
475 dummy = (char)is_xml.peek();
476 if (dummy == '"') {
477 str = "";
478 } else {
479 stringbuf strbuf;
480
481 is_xml.get(strbuf, '"');
482 if (is_xml.fail()) {
483 xml_parse_error("String must end with \"");
484 }
485 str = strbuf.str();
486 }
487
488 // Ignore quote
489 is_xml >> dummy;
490
491 tag.read_from_stream(is_xml);
492 tag.check_name("/String");
493}
494
496
501/* param pbofs Pointer to binary file stream. NULL for ASCII output.
502 Ignored because strings are always in ASCII format. */
503void xml_write_to_stream(ostream& os_xml,
504 const String& str,
505 bofstream* /* pbofs */,
506 const String& name,
507 const Verbosity& verbosity) {
508 XMLTag open_tag(verbosity);
509 XMLTag close_tag(verbosity);
510
511 open_tag.set_name("String");
512 if (name.length()) open_tag.add_attribute("name", name);
513
514 open_tag.write_to_stream(os_xml);
515
516 os_xml << '\"' << str << '\"';
517
518 close_tag.set_name("/String");
519 close_tag.write_to_stream(os_xml);
520 os_xml << '\n';
521}
522
523//=== Tensor3 ================================================================
524
526
531void xml_read_from_stream(istream& is_xml,
532 Tensor3& tensor,
533 bifstream* pbifs,
534 const Verbosity& verbosity) {
535 XMLTag tag(verbosity);
536 Index npages, nrows, ncols;
537
538 tag.read_from_stream(is_xml);
539 tag.check_name("Tensor3");
540
541 tag.get_attribute_value("npages", npages);
542 tag.get_attribute_value("nrows", nrows);
543 tag.get_attribute_value("ncols", ncols);
544 tensor.resize(npages, nrows, ncols);
545
546 if (pbifs) {
547 pbifs->readDoubleArray(tensor.data_handle(), npages * nrows * ncols);
548 } else {
549 for (Index p = 0; p < npages; p++) {
550 for (Index r = 0; r < nrows; r++) {
551 for (Index c = 0; c < ncols; c++) {
552 is_xml >> double_imanip() >> tensor(p, r, c);
553 if (is_xml.fail()) {
554 ostringstream os;
555 os << " near "
556 << "\n Page : " << p << "\n Row : " << r
557 << "\n Column: " << c;
558 xml_data_parse_error(tag, os.str());
559 }
560 }
561 }
562 }
563 }
564
565 tag.read_from_stream(is_xml);
566 tag.check_name("/Tensor3");
567}
568
570
576void xml_write_to_stream(ostream& os_xml,
577 const Tensor3& tensor,
578 bofstream* pbofs,
579 const String& name,
580 const Verbosity& verbosity) {
581 XMLTag open_tag(verbosity);
582 XMLTag close_tag(verbosity);
583
584 open_tag.set_name("Tensor3");
585 if (name.length()) open_tag.add_attribute("name", name);
586 open_tag.add_attribute("npages", tensor.npages());
587 open_tag.add_attribute("nrows", tensor.nrows());
588 open_tag.add_attribute("ncols", tensor.ncols());
589
590 open_tag.write_to_stream(os_xml);
591 os_xml << '\n';
592
594
595 // Write the elements:
596 for (Index p = 0; p < tensor.npages(); ++p) {
597 for (Index r = 0; r < tensor.nrows(); ++r) {
598 if (pbofs)
599 *pbofs << tensor(p, r, 0);
600 else
601 os_xml << tensor(p, r, 0);
602 for (Index c = 1; c < tensor.ncols(); ++c) {
603 if (pbofs)
604 *pbofs << tensor(p, r, c);
605 else
606 os_xml << " " << tensor(p, r, c);
607 }
608 if (!pbofs) os_xml << '\n';
609 }
610 }
611
612 close_tag.set_name("/Tensor3");
613 close_tag.write_to_stream(os_xml);
614
615 os_xml << '\n';
616}
617
618//=== Tensor4 =========================================================
619
621
626void xml_read_from_stream(istream& is_xml,
627 Tensor4& tensor,
628 bifstream* pbifs,
629 const Verbosity& verbosity) {
630 XMLTag tag(verbosity);
631 Index nbooks, npages, nrows, ncols;
632
633 tag.read_from_stream(is_xml);
634 tag.check_name("Tensor4");
635
636 tag.get_attribute_value("nbooks", nbooks);
637 tag.get_attribute_value("npages", npages);
638 tag.get_attribute_value("nrows", nrows);
639 tag.get_attribute_value("ncols", ncols);
640 tensor = Tensor4(nbooks, npages, nrows, ncols);
641
642 if (pbifs) {
643 pbifs->readDoubleArray(tensor.data_handle(),
644 nbooks * npages * nrows * ncols);
645 } else {
646 for (Index b = 0; b < nbooks; b++) {
647 for (Index p = 0; p < npages; p++) {
648 for (Index r = 0; r < nrows; r++) {
649 for (Index c = 0; c < ncols; c++) {
650 is_xml >> double_imanip() >> tensor(b, p, r, c);
651 if (is_xml.fail()) {
652 ostringstream os;
653 os << " near "
654 << "\n Book : " << b << "\n Page : " << p
655 << "\n Row : " << r << "\n Column: " << c;
656 xml_data_parse_error(tag, os.str());
657 }
658 }
659 }
660 }
661 }
662 }
663
664 tag.read_from_stream(is_xml);
665 tag.check_name("/Tensor4");
666}
667
669
675void xml_write_to_stream(ostream& os_xml,
676 const Tensor4& tensor,
677 bofstream* pbofs,
678 const String& name,
679 const Verbosity& verbosity) {
680 XMLTag open_tag(verbosity);
681 XMLTag close_tag(verbosity);
682
683 open_tag.set_name("Tensor4");
684 if (name.length()) open_tag.add_attribute("name", name);
685 open_tag.add_attribute("nbooks", tensor.nbooks());
686 open_tag.add_attribute("npages", tensor.npages());
687 open_tag.add_attribute("nrows", tensor.nrows());
688 open_tag.add_attribute("ncols", tensor.ncols());
689
690 open_tag.write_to_stream(os_xml);
691 os_xml << '\n';
692
694
695 // Write the elements:
696 for (Index b = 0; b < tensor.nbooks(); ++b) {
697 for (Index p = 0; p < tensor.npages(); ++p) {
698 for (Index r = 0; r < tensor.nrows(); ++r) {
699 if (pbofs)
700 *pbofs << tensor(b, p, r, 0);
701 else
702 os_xml << tensor(b, p, r, 0);
703 for (Index c = 1; c < tensor.ncols(); ++c) {
704 if (pbofs)
705 *pbofs << tensor(b, p, r, c);
706 else
707 os_xml << " " << tensor(b, p, r, c);
708 }
709 if (!pbofs) os_xml << '\n';
710 }
711 }
712 }
713
714 close_tag.set_name("/Tensor4");
715 close_tag.write_to_stream(os_xml);
716
717 os_xml << '\n';
718}
719
720//=== Tensor5 =========================================================
721
723
728void xml_read_from_stream(istream& is_xml,
729 Tensor5& tensor,
730 bifstream* pbifs,
731 const Verbosity& verbosity) {
732 XMLTag tag(verbosity);
733 Index nshelves, nbooks, npages, nrows, ncols;
734
735 tag.read_from_stream(is_xml);
736 tag.check_name("Tensor5");
737
738 tag.get_attribute_value("nshelves", nshelves);
739 tag.get_attribute_value("nbooks", nbooks);
740 tag.get_attribute_value("npages", npages);
741 tag.get_attribute_value("nrows", nrows);
742 tag.get_attribute_value("ncols", ncols);
743 tensor.resize(nshelves, nbooks, npages, nrows, ncols);
744
745 if (pbifs) {
746 pbifs->readDoubleArray(tensor.data_handle(),
747 nshelves * nbooks * npages * nrows * ncols);
748 } else {
749 for (Index s = 0; s < nshelves; s++) {
750 for (Index b = 0; b < nbooks; b++) {
751 for (Index p = 0; p < npages; p++) {
752 for (Index r = 0; r < nrows; r++) {
753 for (Index c = 0; c < ncols; c++) {
754 is_xml >> double_imanip() >> tensor(s, b, p, r, c);
755 if (is_xml.fail()) {
756 ostringstream os;
757 os << " near "
758 << "\n Shelf : " << s << "\n Book : " << b
759 << "\n Page : " << p << "\n Row : " << r
760 << "\n Column: " << c;
761 xml_data_parse_error(tag, os.str());
762 }
763 }
764 }
765 }
766 }
767 }
768 }
769
770 tag.read_from_stream(is_xml);
771 tag.check_name("/Tensor5");
772}
773
775
781void xml_write_to_stream(ostream& os_xml,
782 const Tensor5& tensor,
783 bofstream* pbofs,
784 const String& name,
785 const Verbosity& verbosity) {
786 XMLTag open_tag(verbosity);
787 XMLTag close_tag(verbosity);
788
789 open_tag.set_name("Tensor5");
790 if (name.length()) open_tag.add_attribute("name", name);
791 open_tag.add_attribute("nshelves", tensor.nshelves());
792 open_tag.add_attribute("nbooks", tensor.nbooks());
793 open_tag.add_attribute("npages", tensor.npages());
794 open_tag.add_attribute("nrows", tensor.nrows());
795 open_tag.add_attribute("ncols", tensor.ncols());
796
797 open_tag.write_to_stream(os_xml);
798 os_xml << '\n';
799
801
802 // Write the elements:
803 for (Index s = 0; s < tensor.nshelves(); ++s) {
804 for (Index b = 0; b < tensor.nbooks(); ++b) {
805 for (Index p = 0; p < tensor.npages(); ++p) {
806 for (Index r = 0; r < tensor.nrows(); ++r) {
807 if (pbofs)
808 *pbofs << tensor(s, b, p, r, 0);
809 else
810 os_xml << tensor(s, b, p, r, 0);
811 for (Index c = 1; c < tensor.ncols(); ++c) {
812 if (pbofs)
813 *pbofs << tensor(s, b, p, r, c);
814 else
815 os_xml << " " << tensor(s, b, p, r, c);
816 }
817 if (!pbofs) os_xml << '\n';
818 }
819 }
820 }
821 }
822
823 close_tag.set_name("/Tensor5");
824 close_tag.write_to_stream(os_xml);
825
826 os_xml << '\n';
827}
828
829//=== Tensor6 =========================================================
830
832
837void xml_read_from_stream(istream& is_xml,
838 Tensor6& tensor,
839 bifstream* pbifs,
840 const Verbosity& verbosity) {
841 XMLTag tag(verbosity);
842 Index nvitrines, nshelves, nbooks, npages, nrows, ncols;
843
844 tag.read_from_stream(is_xml);
845 tag.check_name("Tensor6");
846
847 tag.get_attribute_value("nvitrines", nvitrines);
848 tag.get_attribute_value("nshelves", nshelves);
849 tag.get_attribute_value("nbooks", nbooks);
850 tag.get_attribute_value("npages", npages);
851 tag.get_attribute_value("nrows", nrows);
852 tag.get_attribute_value("ncols", ncols);
853 tensor.resize(nvitrines, nshelves, nbooks, npages, nrows, ncols);
854
855 if (pbifs) {
856 pbifs->readDoubleArray(
857 tensor.data_handle(),
858 nvitrines * nshelves * nbooks * npages * nrows * ncols);
859 } else {
860 for (Index v = 0; v < nvitrines; v++) {
861 for (Index s = 0; s < nshelves; s++) {
862 for (Index b = 0; b < nbooks; b++) {
863 for (Index p = 0; p < npages; p++) {
864 for (Index r = 0; r < nrows; r++) {
865 for (Index c = 0; c < ncols; c++) {
866 is_xml >> double_imanip() >> tensor(v, s, b, p, r, c);
867 if (is_xml.fail()) {
868 ostringstream os;
869 os << " near "
870 << "\n Vitrine: " << v << "\n Shelf : " << s
871 << "\n Book : " << b << "\n Page : " << p
872 << "\n Row : " << r << "\n Column : " << c;
873 xml_data_parse_error(tag, os.str());
874 }
875 }
876 }
877 }
878 }
879 }
880 }
881 }
882
883 tag.read_from_stream(is_xml);
884 tag.check_name("/Tensor6");
885}
886
888
894void xml_write_to_stream(ostream& os_xml,
895 const Tensor6& tensor,
896 bofstream* pbofs,
897 const String& name,
898 const Verbosity& verbosity) {
899 XMLTag open_tag(verbosity);
900 XMLTag close_tag(verbosity);
901
902 open_tag.set_name("Tensor6");
903 if (name.length()) open_tag.add_attribute("name", name);
904 open_tag.add_attribute("nvitrines", tensor.nvitrines());
905 open_tag.add_attribute("nshelves", tensor.nshelves());
906 open_tag.add_attribute("nbooks", tensor.nbooks());
907 open_tag.add_attribute("npages", tensor.npages());
908 open_tag.add_attribute("nrows", tensor.nrows());
909 open_tag.add_attribute("ncols", tensor.ncols());
910
911 open_tag.write_to_stream(os_xml);
912 os_xml << '\n';
913
915
916 // Write the elements:
917 for (Index v = 0; v < tensor.nvitrines(); ++v) {
918 for (Index s = 0; s < tensor.nshelves(); ++s) {
919 for (Index b = 0; b < tensor.nbooks(); ++b) {
920 for (Index p = 0; p < tensor.npages(); ++p) {
921 for (Index r = 0; r < tensor.nrows(); ++r) {
922 if (pbofs)
923 *pbofs << tensor(v, s, b, p, r, 0);
924 else
925 os_xml << tensor(v, s, b, p, r, 0);
926 for (Index c = 1; c < tensor.ncols(); ++c) {
927 if (pbofs)
928 *pbofs << tensor(v, s, b, p, r, c);
929 else
930 os_xml << " " << tensor(v, s, b, p, r, c);
931 }
932 if (!pbofs) os_xml << '\n';
933 }
934 }
935 }
936 }
937 }
938
939 close_tag.set_name("/Tensor6");
940 close_tag.write_to_stream(os_xml);
941
942 os_xml << '\n';
943}
944
945//=== Tensor7 =========================================================
946
948
953void xml_read_from_stream(istream& is_xml,
954 Tensor7& tensor,
955 bifstream* pbifs,
956 const Verbosity& verbosity) {
957 XMLTag tag(verbosity);
958 Index nlibraries, nvitrines, nshelves, nbooks, npages, nrows, ncols;
959
960 tag.read_from_stream(is_xml);
961 tag.check_name("Tensor7");
962
963 tag.get_attribute_value("nlibraries", nlibraries);
964 tag.get_attribute_value("nvitrines", nvitrines);
965 tag.get_attribute_value("nshelves", nshelves);
966 tag.get_attribute_value("nbooks", nbooks);
967 tag.get_attribute_value("npages", npages);
968 tag.get_attribute_value("nrows", nrows);
969 tag.get_attribute_value("ncols", ncols);
970 tensor.resize(nlibraries, nvitrines, nshelves, nbooks, npages, nrows, ncols);
971
972 if (pbifs) {
973 pbifs->readDoubleArray(
974 tensor.data_handle(),
975 nlibraries * nvitrines * nshelves * nbooks * npages * nrows * ncols);
976 } else {
977 for (Index l = 0; l < nlibraries; l++) {
978 for (Index v = 0; v < nvitrines; v++) {
979 for (Index s = 0; s < nshelves; s++) {
980 for (Index b = 0; b < nbooks; b++) {
981 for (Index p = 0; p < npages; p++) {
982 for (Index r = 0; r < nrows; r++) {
983 for (Index c = 0; c < ncols; c++) {
984 is_xml >> double_imanip() >> tensor(l, v, s, b, p, r, c);
985 if (is_xml.fail()) {
986 ostringstream os;
987 os << " near "
988 << "\n Library: " << l << "\n Vitrine: " << v
989 << "\n Shelf : " << s << "\n Book : " << b
990 << "\n Page : " << p << "\n Row : " << r
991 << "\n Column : " << c;
992 xml_data_parse_error(tag, os.str());
993 }
994 }
995 }
996 }
997 }
998 }
999 }
1000 }
1001 }
1002
1003 tag.read_from_stream(is_xml);
1004 tag.check_name("/Tensor7");
1005}
1006
1008
1014void xml_write_to_stream(ostream& os_xml,
1015 const Tensor7& tensor,
1016 bofstream* pbofs,
1017 const String& name,
1018 const Verbosity& verbosity) {
1019 XMLTag open_tag(verbosity);
1020 XMLTag close_tag(verbosity);
1021
1022 open_tag.set_name("Tensor7");
1023 if (name.length()) open_tag.add_attribute("name", name);
1024 open_tag.add_attribute("nlibraries", tensor.nlibraries());
1025 open_tag.add_attribute("nvitrines", tensor.nvitrines());
1026 open_tag.add_attribute("nshelves", tensor.nshelves());
1027 open_tag.add_attribute("nbooks", tensor.nbooks());
1028 open_tag.add_attribute("npages", tensor.npages());
1029 open_tag.add_attribute("nrows", tensor.nrows());
1030 open_tag.add_attribute("ncols", tensor.ncols());
1031
1032 open_tag.write_to_stream(os_xml);
1033 os_xml << '\n';
1034
1036
1037 // Write the elements:
1038 for (Index l = 0; l < tensor.nlibraries(); ++l) {
1039 for (Index v = 0; v < tensor.nvitrines(); ++v) {
1040 for (Index s = 0; s < tensor.nshelves(); ++s) {
1041 for (Index b = 0; b < tensor.nbooks(); ++b) {
1042 for (Index p = 0; p < tensor.npages(); ++p) {
1043 for (Index r = 0; r < tensor.nrows(); ++r) {
1044 if (pbofs)
1045 *pbofs << tensor(l, v, s, b, p, r, 0);
1046 else
1047 os_xml << tensor(l, v, s, b, p, r, 0);
1048 for (Index c = 1; c < tensor.ncols(); ++c) {
1049 if (pbofs)
1050 *pbofs << tensor(l, v, s, b, p, r, c);
1051 else
1052 os_xml << " " << tensor(l, v, s, b, p, r, c);
1053 }
1054 if (!pbofs) os_xml << '\n';
1055 }
1056 }
1057 }
1058 }
1059 }
1060 }
1061
1062 close_tag.set_name("/Tensor7");
1063 close_tag.write_to_stream(os_xml);
1064
1065 os_xml << '\n';
1066}
1067
1068//=== Vector ==========================================================
1069
1071
1077void xml_parse_from_stream(istream& is_xml,
1078 Vector& vector,
1079 bifstream* pbifs,
1080 XMLTag& tag,
1081 const Verbosity&) {
1082 Index nelem;
1083
1084 tag.get_attribute_value("nelem", nelem);
1085 vector.resize(nelem);
1086
1087 if (pbifs) {
1088 pbifs->readDoubleArray(vector.data_handle(), vector.nelem());
1089 } else {
1090 for (Index n = 0; n < nelem; n++) {
1091 is_xml >> double_imanip() >> vector[n];
1092 if (is_xml.fail()) {
1093 ostringstream os;
1094 os << " near "
1095 << "\n Element: " << n;
1096 xml_data_parse_error(tag, os.str());
1097 }
1098 }
1099 }
1100}
1101
1103
1108void xml_read_from_stream(istream& is_xml,
1109 Vector& vector,
1110 bifstream* pbifs,
1111 const Verbosity& verbosity) {
1112 XMLTag tag(verbosity);
1113
1114 tag.read_from_stream(is_xml);
1115 tag.check_name("Vector");
1116
1117 xml_parse_from_stream(is_xml, vector, pbifs, tag, verbosity);
1118
1119 tag.read_from_stream(is_xml);
1120 tag.check_name("/Vector");
1121}
1122
1124
1130void xml_write_to_stream(ostream& os_xml,
1131 const Vector& vector,
1132 bofstream* pbofs,
1133 const String& name,
1134 const Verbosity& verbosity) {
1135 XMLTag open_tag(verbosity);
1136 XMLTag close_tag(verbosity);
1137 Index n = vector.nelem();
1138 ostringstream v;
1139
1140 // Convert nelem to string
1141 v << n;
1142
1143 open_tag.set_name("Vector");
1144 if (name.length()) open_tag.add_attribute("name", name);
1145 open_tag.add_attribute("nelem", v.str());
1146
1147 open_tag.write_to_stream(os_xml);
1148 os_xml << '\n';
1149
1151
1152 for (Index i = 0; i < n; ++i)
1153 if (pbofs)
1154 *pbofs << vector[i];
1155 else
1156 os_xml << vector[i] << '\n';
1157
1158 close_tag.set_name("/Vector");
1159 close_tag.write_to_stream(os_xml);
1160
1161 os_xml << '\n';
1162}
1163
1165// Dummy funtion for groups for which
1166// IO function have not yet been implemented
The global header file for ARTS.
The ARTS XML tag class.
Definition xml_io_base.h:53
void add_attribute(const String &aname, String value)
void write_to_stream(ostream &os)
Write XML tag.
void check_name(const String &expected_name)
Check tag name.
void read_from_stream(istream &is)
Reads next XML tag.
void get_attribute_value(const String &aname, String &value)
void set_name(const String &new_name)
Definition xml_io_base.h:61
Binary output file stream class.
Definition bifstream.h:26
void readDoubleArray(double *d, unsigned long size)
Definition binio.cc:184
Binary output file stream class.
Definition bofstream.h:25
Input manipulator class for doubles to enable nan and inf parsing.
Fast double input stream with support for parsing nan and inf.
#define v
#define c
#define b
void xml_data_parse_error(ArtsXMLTag &tag, String str_error)
Throws XML parser runtime error.
Definition xml_io.cc:162
void xml_set_stream_precision(ostream &os)
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
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.
void xml_read_from_stream(istream &is_xml, Index &index, bifstream *pbifs, const Verbosity &verbosity)
Reads Index from XML input stream.
void xml_parse_from_stream(istream &is_xml, Vector &vector, bifstream *pbifs, XMLTag &tag, const Verbosity &)
Parses Vector from XML input stream.