ARTS 2.5.9 (git: 825fa5f2)
xml_io_general_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
19// File description
21
30#include "arts.h"
31#include "xml_io_base.h"
32#include "double_imanip.h"
33
35// Overloaded functions for reading/writing data from/to XML stream
37
38//=== Index ==================================================================
39
41
46void xml_read_from_stream(istream& is_xml,
47 Index& index,
48 bifstream* pbifs,
49 const Verbosity& verbosity) {
50 XMLTag tag(verbosity);
51
52 tag.read_from_stream(is_xml);
53 tag.check_name("Index");
54
55 if (pbifs) {
56 *pbifs >> index;
57 if (pbifs->fail()) {
58 xml_data_parse_error(tag, "");
59 }
60 } else {
61 is_xml >> index;
62 if (is_xml.fail()) {
63 xml_data_parse_error(tag, "");
64 }
65 }
66
67 tag.read_from_stream(is_xml);
68 tag.check_name("/Index");
69}
70
72
78void xml_write_to_stream(ostream& os_xml,
79 const Index& index,
80 bofstream* pbofs,
81 const String& name,
82 const Verbosity& verbosity) {
83 XMLTag open_tag(verbosity);
84 XMLTag close_tag(verbosity);
85
86 open_tag.set_name("Index");
87 if (name.length()) open_tag.add_attribute("name", name);
88
89 open_tag.write_to_stream(os_xml);
90
91 if (pbofs)
92 *pbofs << index;
93 else
94 os_xml << index;
95
96 close_tag.set_name("/Index");
97 close_tag.write_to_stream(os_xml);
98 os_xml << '\n';
99}
100
101//=== Matrix ==========================================================
102
104
109void xml_read_from_stream(istream& is_xml,
110 Matrix& matrix,
111 bifstream* pbifs,
112 const Verbosity& verbosity) {
113 XMLTag tag(verbosity);
114 Index nrows, ncols;
115
116 tag.read_from_stream(is_xml);
117 tag.check_name("Matrix");
118
119 tag.get_attribute_value("nrows", nrows);
120 tag.get_attribute_value("ncols", ncols);
121 matrix.resize(nrows, ncols);
122
123 if (pbifs) {
124 pbifs->readDoubleArray(matrix.get_c_array(), nrows * ncols);
125 } else {
126 for (Index r = 0; r < nrows; r++) {
127 for (Index c = 0; c < ncols; c++) {
128 is_xml >> double_imanip() >> matrix(r, c);
129 if (is_xml.fail()) {
130 ostringstream os;
131 os << " near "
132 << "\n Row : " << r << "\n Column: " << c;
133 xml_data_parse_error(tag, os.str());
134 }
135 }
136 }
137 }
138
139 tag.read_from_stream(is_xml);
140 tag.check_name("/Matrix");
141}
142
144
150void xml_write_to_stream(ostream& os_xml,
151 const Matrix& matrix,
152 bofstream* pbofs,
153 const String& name,
154 const Verbosity& verbosity) {
155 XMLTag open_tag(verbosity);
156 XMLTag close_tag(verbosity);
157
158 open_tag.set_name("Matrix");
159 if (name.length()) open_tag.add_attribute("name", name);
160 open_tag.add_attribute("nrows", matrix.nrows());
161 open_tag.add_attribute("ncols", matrix.ncols());
162
163 open_tag.write_to_stream(os_xml);
164 os_xml << '\n';
165
167
168 // Write the elements:
169 for (Index r = 0; r < matrix.nrows(); ++r) {
170 if (pbofs)
171 *pbofs << matrix(r, 0);
172 else
173 os_xml << matrix(r, 0);
174
175 for (Index c = 1; c < matrix.ncols(); ++c) {
176 if (pbofs)
177 *pbofs << matrix(r, c);
178 else
179 os_xml << " " << matrix(r, c);
180 }
181
182 if (!pbofs) os_xml << '\n';
183 }
184
185 close_tag.set_name("/Matrix");
186 close_tag.write_to_stream(os_xml);
187
188 os_xml << '\n';
189}
190
191//=== Numeric =========================================================
192
194
199void xml_read_from_stream(istream& is_xml,
200 Numeric& numeric,
201 bifstream* pbifs,
202 const Verbosity& verbosity) {
203 XMLTag tag(verbosity);
204
205 tag.read_from_stream(is_xml);
206 tag.check_name("Numeric");
207
208 if (pbifs) {
209 *pbifs >> numeric;
210 if (pbifs->fail()) {
211 xml_data_parse_error(tag, "");
212 }
213 } else {
214 is_xml >> double_imanip() >> numeric;
215 if (is_xml.fail()) {
216 xml_data_parse_error(tag, "");
217 }
218 }
219
220 tag.read_from_stream(is_xml);
221 tag.check_name("/Numeric");
222}
223
225
231void xml_write_to_stream(ostream& os_xml,
232 const Numeric& numeric,
233 bofstream* pbofs,
234 const String& name,
235 const Verbosity& verbosity) {
236 XMLTag open_tag(verbosity);
237 XMLTag close_tag(verbosity);
238
239 open_tag.set_name("Numeric");
240 if (name.length()) open_tag.add_attribute("name", name);
241
242 open_tag.write_to_stream(os_xml);
243
245
246 if (pbofs)
247 *pbofs << numeric;
248 else
249 os_xml << numeric;
250
251 close_tag.set_name("/Numeric");
252 close_tag.write_to_stream(os_xml);
253 os_xml << '\n';
254}
255
256//=== Sparse ====================================================
257
259
264void xml_read_from_stream(istream& is_xml,
265 Sparse& sparse,
266 bifstream* pbifs,
267 const Verbosity& verbosity) {
268 XMLTag tag(verbosity);
269 Index nrows, ncols, nnz;
270
271 tag.read_from_stream(is_xml);
272 tag.check_name("Sparse");
273
274 tag.get_attribute_value("nrows", nrows);
275 tag.get_attribute_value("ncols", ncols);
276 sparse.resize(nrows, ncols);
277
278 tag.read_from_stream(is_xml);
279 tag.check_name("RowIndex");
280 tag.get_attribute_value("nelem", nnz);
281
282 ArrayOfIndex rowind(nnz), colind(nnz);
283 Vector data(nnz);
284
285 for (Index i = 0; i < nnz; i++) {
286 if (pbifs) {
287 *pbifs >> rowind[i];
288 if (pbifs->fail()) {
289 ostringstream os;
290 os << " near "
291 << "\n Row index: " << i;
292 xml_data_parse_error(tag, os.str());
293 }
294 } else {
295 is_xml >> rowind[i];
296 if (is_xml.fail()) {
297 ostringstream os;
298 os << " near "
299 << "\n Row index: " << i;
300 xml_data_parse_error(tag, os.str());
301 }
302 }
303 }
304 tag.read_from_stream(is_xml);
305 tag.check_name("/RowIndex");
306
307 tag.read_from_stream(is_xml);
308 tag.check_name("ColIndex");
309
310 for (Index i = 0; i < nnz; i++) {
311 if (pbifs) {
312 *pbifs >> colind[i];
313 if (pbifs->fail()) {
314 ostringstream os;
315 os << " near "
316 << "\n Column index: " << i;
317 xml_data_parse_error(tag, os.str());
318 }
319 } else {
320 is_xml >> colind[i];
321 if (is_xml.fail()) {
322 ostringstream os;
323 os << " near "
324 << "\n Column index: " << i;
325 xml_data_parse_error(tag, os.str());
326 }
327 }
328 }
329 tag.read_from_stream(is_xml);
330 tag.check_name("/ColIndex");
331
332 tag.read_from_stream(is_xml);
333 tag.check_name("SparseData");
334
335 if (pbifs) {
336 pbifs->readDoubleArray(data.get_c_array(), nnz);
337 } else {
338 for (Index i = 0; i < nnz; i++) {
339 is_xml >> double_imanip() >> data[i];
340 if (is_xml.fail()) {
341 ostringstream os;
342 os << " near "
343 << "\n Data element: " << i;
344 xml_data_parse_error(tag, os.str());
345 }
346 }
347 }
348 tag.read_from_stream(is_xml);
349 tag.check_name("/SparseData");
350
351 tag.read_from_stream(is_xml);
352 tag.check_name("/Sparse");
353
354 sparse.insert_elements(nnz, rowind, colind, data);
355}
356
358
364void xml_write_to_stream(ostream& os_xml,
365 const Sparse& sparse,
366 bofstream* pbofs,
367 const String& name,
368 const Verbosity& verbosity) {
369 XMLTag sparse_tag(verbosity);
370 XMLTag row_tag(verbosity);
371 XMLTag col_tag(verbosity);
372 XMLTag data_tag(verbosity);
373 XMLTag close_tag(verbosity);
374
375 sparse_tag.set_name("Sparse");
376 if (name.length()) sparse_tag.add_attribute("name", name);
377 sparse_tag.add_attribute("nrows", sparse.nrows());
378 sparse_tag.add_attribute("ncols", sparse.ncols());
379 //sparse_tag.add_attribute ("nnz", sparse.nnz());
380 row_tag.set_name("RowIndex");
381 row_tag.add_attribute("nelem", sparse.nnz());
382 col_tag.set_name("ColIndex");
383 col_tag.add_attribute("nelem", sparse.nnz());
384 data_tag.set_name("SparseData");
385 data_tag.add_attribute("nelem", sparse.nnz());
386
387 sparse_tag.write_to_stream(os_xml);
388 os_xml << '\n';
389
390 row_tag.write_to_stream(os_xml);
391 os_xml << '\n';
392
393 ArrayOfIndex rowind(sparse.nnz()), colind(sparse.nnz());
394 Vector data(sparse.nnz());
395 sparse.list_elements(data, rowind, colind);
396
397 // Write row indices.
398
399 for (Index i = 0; i < sparse.nnz(); i++) {
400 if (pbofs)
401 //FIXME: It should be the longer lines
402 *pbofs << rowind[i];
403 else
404 os_xml << rowind[i] << '\n';
405 }
406
407 close_tag.set_name("/RowIndex");
408 close_tag.write_to_stream(os_xml);
409 os_xml << '\n';
410
411 col_tag.write_to_stream(os_xml);
412 os_xml << '\n';
413
414 // Write column indices.
415
416 for (Index i = 0; i < sparse.nnz(); i++) {
417 if (pbofs)
418 //FIXME: It should be the longer lines
419 *pbofs << colind[i];
420 else
421 os_xml << colind[i] << '\n';
422 }
423
424 close_tag.set_name("/ColIndex");
425 close_tag.write_to_stream(os_xml);
426 os_xml << '\n';
427
428 data_tag.write_to_stream(os_xml);
429 os_xml << '\n';
431
432 // Write data.
433
434 for (Index i = 0; i < sparse.nnz(); i++) {
435 if (pbofs)
436 *pbofs << data[i];
437 else
438 os_xml << data[i] << ' ';
439 }
440 os_xml << '\n';
441 close_tag.set_name("/SparseData");
442 close_tag.write_to_stream(os_xml);
443 os_xml << '\n';
444
445 close_tag.set_name("/Sparse");
446 close_tag.write_to_stream(os_xml);
447
448 os_xml << '\n';
449}
450
451//=== String ===========================================================
452
454
458/* param pbifs Pointer to binary input stream. NULL in case of ASCII file.
459 Ignored because strings are always stored in ASCII format. */
460void xml_read_from_stream(istream& is_xml,
461 String& str,
462 bifstream* /* pbifs */,
463 const Verbosity& verbosity) {
464 XMLTag tag(verbosity);
465 char dummy;
466
467 tag.read_from_stream(is_xml);
468 tag.check_name("String");
469
470 // Skip whitespaces
471 bool string_starts_with_quotes = true;
472 do {
473 is_xml >> dummy;
474 switch (dummy) {
475 case ' ':
476 case '\"':
477 case '\n':
478 case '\r':
479 case '\t':
480 break;
481 default:
482 string_starts_with_quotes = false;
483 }
484 } while (is_xml.good() && dummy != '"' && string_starts_with_quotes);
485
486 // Throw exception if first char after whitespaces is not a quote
487 if (!string_starts_with_quotes) {
488 xml_parse_error("String must begin with \"");
489 }
490
491 //catch case where string is empty. CPD 29/8/05
492 dummy = (char)is_xml.peek();
493 if (dummy == '"') {
494 str = "";
495 } else {
496 stringbuf strbuf;
497
498 is_xml.get(strbuf, '"');
499 if (is_xml.fail()) {
500 xml_parse_error("String must end with \"");
501 }
502 str = strbuf.str();
503 }
504
505 // Ignore quote
506 is_xml >> dummy;
507
508 tag.read_from_stream(is_xml);
509 tag.check_name("/String");
510}
511
513
518/* param pbofs Pointer to binary file stream. NULL for ASCII output.
519 Ignored because strings are always in ASCII format. */
520void xml_write_to_stream(ostream& os_xml,
521 const String& str,
522 bofstream* /* pbofs */,
523 const String& name,
524 const Verbosity& verbosity) {
525 XMLTag open_tag(verbosity);
526 XMLTag close_tag(verbosity);
527
528 open_tag.set_name("String");
529 if (name.length()) open_tag.add_attribute("name", name);
530
531 open_tag.write_to_stream(os_xml);
532
533 os_xml << '\"' << str << '\"';
534
535 close_tag.set_name("/String");
536 close_tag.write_to_stream(os_xml);
537 os_xml << '\n';
538}
539
540//=== Tensor3 ================================================================
541
543
548void xml_read_from_stream(istream& is_xml,
549 Tensor3& tensor,
550 bifstream* pbifs,
551 const Verbosity& verbosity) {
552 XMLTag tag(verbosity);
553 Index npages, nrows, ncols;
554
555 tag.read_from_stream(is_xml);
556 tag.check_name("Tensor3");
557
558 tag.get_attribute_value("npages", npages);
559 tag.get_attribute_value("nrows", nrows);
560 tag.get_attribute_value("ncols", ncols);
561 tensor.resize(npages, nrows, ncols);
562
563 if (pbifs) {
564 pbifs->readDoubleArray(tensor.get_c_array(), npages * nrows * ncols);
565 } else {
566 for (Index p = 0; p < npages; p++) {
567 for (Index r = 0; r < nrows; r++) {
568 for (Index c = 0; c < ncols; c++) {
569 is_xml >> double_imanip() >> tensor(p, r, c);
570 if (is_xml.fail()) {
571 ostringstream os;
572 os << " near "
573 << "\n Page : " << p << "\n Row : " << r
574 << "\n Column: " << c;
575 xml_data_parse_error(tag, os.str());
576 }
577 }
578 }
579 }
580 }
581
582 tag.read_from_stream(is_xml);
583 tag.check_name("/Tensor3");
584}
585
587
593void xml_write_to_stream(ostream& os_xml,
594 const Tensor3& tensor,
595 bofstream* pbofs,
596 const String& name,
597 const Verbosity& verbosity) {
598 XMLTag open_tag(verbosity);
599 XMLTag close_tag(verbosity);
600
601 open_tag.set_name("Tensor3");
602 if (name.length()) open_tag.add_attribute("name", name);
603 open_tag.add_attribute("npages", tensor.npages());
604 open_tag.add_attribute("nrows", tensor.nrows());
605 open_tag.add_attribute("ncols", tensor.ncols());
606
607 open_tag.write_to_stream(os_xml);
608 os_xml << '\n';
609
611
612 // Write the elements:
613 for (Index p = 0; p < tensor.npages(); ++p) {
614 for (Index r = 0; r < tensor.nrows(); ++r) {
615 if (pbofs)
616 *pbofs << tensor(p, r, 0);
617 else
618 os_xml << tensor(p, r, 0);
619 for (Index c = 1; c < tensor.ncols(); ++c) {
620 if (pbofs)
621 *pbofs << tensor(p, r, c);
622 else
623 os_xml << " " << tensor(p, r, c);
624 }
625 if (!pbofs) os_xml << '\n';
626 }
627 }
628
629 close_tag.set_name("/Tensor3");
630 close_tag.write_to_stream(os_xml);
631
632 os_xml << '\n';
633}
634
635//=== Tensor4 =========================================================
636
638
643void xml_read_from_stream(istream& is_xml,
644 Tensor4& tensor,
645 bifstream* pbifs,
646 const Verbosity& verbosity) {
647 XMLTag tag(verbosity);
648 Index nbooks, npages, nrows, ncols;
649
650 tag.read_from_stream(is_xml);
651 tag.check_name("Tensor4");
652
653 tag.get_attribute_value("nbooks", nbooks);
654 tag.get_attribute_value("npages", npages);
655 tag.get_attribute_value("nrows", nrows);
656 tag.get_attribute_value("ncols", ncols);
657 tensor = Tensor4(nbooks, npages, nrows, ncols);
658
659 if (pbifs) {
660 pbifs->readDoubleArray(tensor.get_c_array(),
661 nbooks * npages * nrows * ncols);
662 } else {
663 for (Index b = 0; b < nbooks; b++) {
664 for (Index p = 0; p < npages; p++) {
665 for (Index r = 0; r < nrows; r++) {
666 for (Index c = 0; c < ncols; c++) {
667 is_xml >> double_imanip() >> tensor(b, p, r, c);
668 if (is_xml.fail()) {
669 ostringstream os;
670 os << " near "
671 << "\n Book : " << b << "\n Page : " << p
672 << "\n Row : " << r << "\n Column: " << c;
673 xml_data_parse_error(tag, os.str());
674 }
675 }
676 }
677 }
678 }
679 }
680
681 tag.read_from_stream(is_xml);
682 tag.check_name("/Tensor4");
683}
684
686
692void xml_write_to_stream(ostream& os_xml,
693 const Tensor4& tensor,
694 bofstream* pbofs,
695 const String& name,
696 const Verbosity& verbosity) {
697 XMLTag open_tag(verbosity);
698 XMLTag close_tag(verbosity);
699
700 open_tag.set_name("Tensor4");
701 if (name.length()) open_tag.add_attribute("name", name);
702 open_tag.add_attribute("nbooks", tensor.nbooks());
703 open_tag.add_attribute("npages", tensor.npages());
704 open_tag.add_attribute("nrows", tensor.nrows());
705 open_tag.add_attribute("ncols", tensor.ncols());
706
707 open_tag.write_to_stream(os_xml);
708 os_xml << '\n';
709
711
712 // Write the elements:
713 for (Index b = 0; b < tensor.nbooks(); ++b) {
714 for (Index p = 0; p < tensor.npages(); ++p) {
715 for (Index r = 0; r < tensor.nrows(); ++r) {
716 if (pbofs)
717 *pbofs << tensor(b, p, r, 0);
718 else
719 os_xml << tensor(b, p, r, 0);
720 for (Index c = 1; c < tensor.ncols(); ++c) {
721 if (pbofs)
722 *pbofs << tensor(b, p, r, c);
723 else
724 os_xml << " " << tensor(b, p, r, c);
725 }
726 if (!pbofs) os_xml << '\n';
727 }
728 }
729 }
730
731 close_tag.set_name("/Tensor4");
732 close_tag.write_to_stream(os_xml);
733
734 os_xml << '\n';
735}
736
737//=== Tensor5 =========================================================
738
740
745void xml_read_from_stream(istream& is_xml,
746 Tensor5& tensor,
747 bifstream* pbifs,
748 const Verbosity& verbosity) {
749 XMLTag tag(verbosity);
750 Index nshelves, nbooks, npages, nrows, ncols;
751
752 tag.read_from_stream(is_xml);
753 tag.check_name("Tensor5");
754
755 tag.get_attribute_value("nshelves", nshelves);
756 tag.get_attribute_value("nbooks", nbooks);
757 tag.get_attribute_value("npages", npages);
758 tag.get_attribute_value("nrows", nrows);
759 tag.get_attribute_value("ncols", ncols);
760 tensor.resize(nshelves, nbooks, npages, nrows, ncols);
761
762 if (pbifs) {
763 pbifs->readDoubleArray(tensor.get_c_array(),
764 nshelves * nbooks * npages * nrows * ncols);
765 } else {
766 for (Index s = 0; s < nshelves; s++) {
767 for (Index b = 0; b < nbooks; b++) {
768 for (Index p = 0; p < npages; p++) {
769 for (Index r = 0; r < nrows; r++) {
770 for (Index c = 0; c < ncols; c++) {
771 is_xml >> double_imanip() >> tensor(s, b, p, r, c);
772 if (is_xml.fail()) {
773 ostringstream os;
774 os << " near "
775 << "\n Shelf : " << s << "\n Book : " << b
776 << "\n Page : " << p << "\n Row : " << r
777 << "\n Column: " << c;
778 xml_data_parse_error(tag, os.str());
779 }
780 }
781 }
782 }
783 }
784 }
785 }
786
787 tag.read_from_stream(is_xml);
788 tag.check_name("/Tensor5");
789}
790
792
798void xml_write_to_stream(ostream& os_xml,
799 const Tensor5& tensor,
800 bofstream* pbofs,
801 const String& name,
802 const Verbosity& verbosity) {
803 XMLTag open_tag(verbosity);
804 XMLTag close_tag(verbosity);
805
806 open_tag.set_name("Tensor5");
807 if (name.length()) open_tag.add_attribute("name", name);
808 open_tag.add_attribute("nshelves", tensor.nshelves());
809 open_tag.add_attribute("nbooks", tensor.nbooks());
810 open_tag.add_attribute("npages", tensor.npages());
811 open_tag.add_attribute("nrows", tensor.nrows());
812 open_tag.add_attribute("ncols", tensor.ncols());
813
814 open_tag.write_to_stream(os_xml);
815 os_xml << '\n';
816
818
819 // Write the elements:
820 for (Index s = 0; s < tensor.nshelves(); ++s) {
821 for (Index b = 0; b < tensor.nbooks(); ++b) {
822 for (Index p = 0; p < tensor.npages(); ++p) {
823 for (Index r = 0; r < tensor.nrows(); ++r) {
824 if (pbofs)
825 *pbofs << tensor(s, b, p, r, 0);
826 else
827 os_xml << tensor(s, b, p, r, 0);
828 for (Index c = 1; c < tensor.ncols(); ++c) {
829 if (pbofs)
830 *pbofs << tensor(s, b, p, r, c);
831 else
832 os_xml << " " << tensor(s, b, p, r, c);
833 }
834 if (!pbofs) os_xml << '\n';
835 }
836 }
837 }
838 }
839
840 close_tag.set_name("/Tensor5");
841 close_tag.write_to_stream(os_xml);
842
843 os_xml << '\n';
844}
845
846//=== Tensor6 =========================================================
847
849
854void xml_read_from_stream(istream& is_xml,
855 Tensor6& tensor,
856 bifstream* pbifs,
857 const Verbosity& verbosity) {
858 XMLTag tag(verbosity);
859 Index nvitrines, nshelves, nbooks, npages, nrows, ncols;
860
861 tag.read_from_stream(is_xml);
862 tag.check_name("Tensor6");
863
864 tag.get_attribute_value("nvitrines", nvitrines);
865 tag.get_attribute_value("nshelves", nshelves);
866 tag.get_attribute_value("nbooks", nbooks);
867 tag.get_attribute_value("npages", npages);
868 tag.get_attribute_value("nrows", nrows);
869 tag.get_attribute_value("ncols", ncols);
870 tensor.resize(nvitrines, nshelves, nbooks, npages, nrows, ncols);
871
872 if (pbifs) {
873 pbifs->readDoubleArray(
874 tensor.get_c_array(),
875 nvitrines * nshelves * nbooks * npages * nrows * ncols);
876 } else {
877 for (Index v = 0; v < nvitrines; v++) {
878 for (Index s = 0; s < nshelves; s++) {
879 for (Index b = 0; b < nbooks; b++) {
880 for (Index p = 0; p < npages; p++) {
881 for (Index r = 0; r < nrows; r++) {
882 for (Index c = 0; c < ncols; c++) {
883 is_xml >> double_imanip() >> tensor(v, s, b, p, r, c);
884 if (is_xml.fail()) {
885 ostringstream os;
886 os << " near "
887 << "\n Vitrine: " << v << "\n Shelf : " << s
888 << "\n Book : " << b << "\n Page : " << p
889 << "\n Row : " << r << "\n Column : " << c;
890 xml_data_parse_error(tag, os.str());
891 }
892 }
893 }
894 }
895 }
896 }
897 }
898 }
899
900 tag.read_from_stream(is_xml);
901 tag.check_name("/Tensor6");
902}
903
905
911void xml_write_to_stream(ostream& os_xml,
912 const Tensor6& tensor,
913 bofstream* pbofs,
914 const String& name,
915 const Verbosity& verbosity) {
916 XMLTag open_tag(verbosity);
917 XMLTag close_tag(verbosity);
918
919 open_tag.set_name("Tensor6");
920 if (name.length()) open_tag.add_attribute("name", name);
921 open_tag.add_attribute("nvitrines", tensor.nvitrines());
922 open_tag.add_attribute("nshelves", tensor.nshelves());
923 open_tag.add_attribute("nbooks", tensor.nbooks());
924 open_tag.add_attribute("npages", tensor.npages());
925 open_tag.add_attribute("nrows", tensor.nrows());
926 open_tag.add_attribute("ncols", tensor.ncols());
927
928 open_tag.write_to_stream(os_xml);
929 os_xml << '\n';
930
932
933 // Write the elements:
934 for (Index v = 0; v < tensor.nvitrines(); ++v) {
935 for (Index s = 0; s < tensor.nshelves(); ++s) {
936 for (Index b = 0; b < tensor.nbooks(); ++b) {
937 for (Index p = 0; p < tensor.npages(); ++p) {
938 for (Index r = 0; r < tensor.nrows(); ++r) {
939 if (pbofs)
940 *pbofs << tensor(v, s, b, p, r, 0);
941 else
942 os_xml << tensor(v, s, b, p, r, 0);
943 for (Index c = 1; c < tensor.ncols(); ++c) {
944 if (pbofs)
945 *pbofs << tensor(v, s, b, p, r, c);
946 else
947 os_xml << " " << tensor(v, s, b, p, r, c);
948 }
949 if (!pbofs) os_xml << '\n';
950 }
951 }
952 }
953 }
954 }
955
956 close_tag.set_name("/Tensor6");
957 close_tag.write_to_stream(os_xml);
958
959 os_xml << '\n';
960}
961
962//=== Tensor7 =========================================================
963
965
970void xml_read_from_stream(istream& is_xml,
971 Tensor7& tensor,
972 bifstream* pbifs,
973 const Verbosity& verbosity) {
974 XMLTag tag(verbosity);
975 Index nlibraries, nvitrines, nshelves, nbooks, npages, nrows, ncols;
976
977 tag.read_from_stream(is_xml);
978 tag.check_name("Tensor7");
979
980 tag.get_attribute_value("nlibraries", nlibraries);
981 tag.get_attribute_value("nvitrines", nvitrines);
982 tag.get_attribute_value("nshelves", nshelves);
983 tag.get_attribute_value("nbooks", nbooks);
984 tag.get_attribute_value("npages", npages);
985 tag.get_attribute_value("nrows", nrows);
986 tag.get_attribute_value("ncols", ncols);
987 tensor.resize(nlibraries, nvitrines, nshelves, nbooks, npages, nrows, ncols);
988
989 if (pbifs) {
990 pbifs->readDoubleArray(
991 tensor.get_c_array(),
992 nlibraries * nvitrines * nshelves * nbooks * npages * nrows * ncols);
993 } else {
994 for (Index l = 0; l < nlibraries; l++) {
995 for (Index v = 0; v < nvitrines; v++) {
996 for (Index s = 0; s < nshelves; s++) {
997 for (Index b = 0; b < nbooks; b++) {
998 for (Index p = 0; p < npages; p++) {
999 for (Index r = 0; r < nrows; r++) {
1000 for (Index c = 0; c < ncols; c++) {
1001 is_xml >> double_imanip() >> tensor(l, v, s, b, p, r, c);
1002 if (is_xml.fail()) {
1003 ostringstream os;
1004 os << " near "
1005 << "\n Library: " << l << "\n Vitrine: " << v
1006 << "\n Shelf : " << s << "\n Book : " << b
1007 << "\n Page : " << p << "\n Row : " << r
1008 << "\n Column : " << c;
1009 xml_data_parse_error(tag, os.str());
1010 }
1011 }
1012 }
1013 }
1014 }
1015 }
1016 }
1017 }
1018 }
1019
1020 tag.read_from_stream(is_xml);
1021 tag.check_name("/Tensor7");
1022}
1023
1025
1031void xml_write_to_stream(ostream& os_xml,
1032 const Tensor7& tensor,
1033 bofstream* pbofs,
1034 const String& name,
1035 const Verbosity& verbosity) {
1036 XMLTag open_tag(verbosity);
1037 XMLTag close_tag(verbosity);
1038
1039 open_tag.set_name("Tensor7");
1040 if (name.length()) open_tag.add_attribute("name", name);
1041 open_tag.add_attribute("nlibraries", tensor.nlibraries());
1042 open_tag.add_attribute("nvitrines", tensor.nvitrines());
1043 open_tag.add_attribute("nshelves", tensor.nshelves());
1044 open_tag.add_attribute("nbooks", tensor.nbooks());
1045 open_tag.add_attribute("npages", tensor.npages());
1046 open_tag.add_attribute("nrows", tensor.nrows());
1047 open_tag.add_attribute("ncols", tensor.ncols());
1048
1049 open_tag.write_to_stream(os_xml);
1050 os_xml << '\n';
1051
1053
1054 // Write the elements:
1055 for (Index l = 0; l < tensor.nlibraries(); ++l) {
1056 for (Index v = 0; v < tensor.nvitrines(); ++v) {
1057 for (Index s = 0; s < tensor.nshelves(); ++s) {
1058 for (Index b = 0; b < tensor.nbooks(); ++b) {
1059 for (Index p = 0; p < tensor.npages(); ++p) {
1060 for (Index r = 0; r < tensor.nrows(); ++r) {
1061 if (pbofs)
1062 *pbofs << tensor(l, v, s, b, p, r, 0);
1063 else
1064 os_xml << tensor(l, v, s, b, p, r, 0);
1065 for (Index c = 1; c < tensor.ncols(); ++c) {
1066 if (pbofs)
1067 *pbofs << tensor(l, v, s, b, p, r, c);
1068 else
1069 os_xml << " " << tensor(l, v, s, b, p, r, c);
1070 }
1071 if (!pbofs) os_xml << '\n';
1072 }
1073 }
1074 }
1075 }
1076 }
1077 }
1078
1079 close_tag.set_name("/Tensor7");
1080 close_tag.write_to_stream(os_xml);
1081
1082 os_xml << '\n';
1083}
1084
1085//=== Vector ==========================================================
1086
1088
1094void xml_parse_from_stream(istream& is_xml,
1095 Vector& vector,
1096 bifstream* pbifs,
1097 XMLTag& tag,
1098 const Verbosity&) {
1099 Index nelem;
1100
1101 tag.get_attribute_value("nelem", nelem);
1102 vector.resize(nelem);
1103
1104 if (pbifs) {
1105 pbifs->readDoubleArray(vector.get_c_array(), vector.nelem());
1106 } else {
1107 for (Index n = 0; n < nelem; n++) {
1108 is_xml >> double_imanip() >> vector[n];
1109 if (is_xml.fail()) {
1110 ostringstream os;
1111 os << " near "
1112 << "\n Element: " << n;
1113 xml_data_parse_error(tag, os.str());
1114 }
1115 }
1116 }
1117}
1118
1120
1125void xml_read_from_stream(istream& is_xml,
1126 Vector& vector,
1127 bifstream* pbifs,
1128 const Verbosity& verbosity) {
1129 XMLTag tag(verbosity);
1130
1131 tag.read_from_stream(is_xml);
1132 tag.check_name("Vector");
1133
1134 xml_parse_from_stream(is_xml, vector, pbifs, tag, verbosity);
1135
1136 tag.read_from_stream(is_xml);
1137 tag.check_name("/Vector");
1138}
1139
1141
1147void xml_write_to_stream(ostream& os_xml,
1148 const Vector& vector,
1149 bofstream* pbofs,
1150 const String& name,
1151 const Verbosity& verbosity) {
1152 XMLTag open_tag(verbosity);
1153 XMLTag close_tag(verbosity);
1154 Index n = vector.nelem();
1155 ostringstream v;
1156
1157 // Convert nelem to string
1158 v << n;
1159
1160 open_tag.set_name("Vector");
1161 if (name.length()) open_tag.add_attribute("name", name);
1162 open_tag.add_attribute("nelem", v.str());
1163
1164 open_tag.write_to_stream(os_xml);
1165 os_xml << '\n';
1166
1168
1169 for (Index i = 0; i < n; ++i)
1170 if (pbofs)
1171 *pbofs << vector[i];
1172 else
1173 os_xml << vector[i] << '\n';
1174
1175 close_tag.set_name("/Vector");
1176 close_tag.write_to_stream(os_xml);
1177
1178 os_xml << '\n';
1179}
1180
1182// Dummy funtion for groups for which
1183// IO function have not yet been implemented
The global header file for ARTS.
Numeric * get_c_array() const noexcept
Definition: matpackI.h:1157
Index nrows() const noexcept
Definition: matpackI.h:1079
Index ncols() const noexcept
Definition: matpackI.h:1080
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:145
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:148
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:151
Index ncols() const noexcept
Definition: matpackIV.h:146
Index nrows() const noexcept
Definition: matpackIV.h:145
Index nbooks() const noexcept
Definition: matpackIV.h:143
Index npages() const noexcept
Definition: matpackIV.h:144
Index nrows() const noexcept
Definition: matpackV.h:157
Index ncols() const noexcept
Definition: matpackV.h:158
Index npages() const noexcept
Definition: matpackV.h:156
Index nbooks() const noexcept
Definition: matpackV.h:155
Index nshelves() const noexcept
Definition: matpackV.h:154
Index nbooks() const noexcept
Definition: matpackVI.h:160
Index nvitrines() const noexcept
Definition: matpackVI.h:158
Index ncols() const noexcept
Definition: matpackVI.h:163
Index npages() const noexcept
Definition: matpackVI.h:161
Index nshelves() const noexcept
Definition: matpackVI.h:159
Index nrows() const noexcept
Definition: matpackVI.h:162
Index ncols() const noexcept
Definition: matpackVII.h:164
Index npages() const noexcept
Definition: matpackVII.h:162
Index nrows() const noexcept
Definition: matpackVII.h:163
Index nlibraries() const noexcept
Definition: matpackVII.h:158
Index nvitrines() const noexcept
Definition: matpackVII.h:159
Index nshelves() const noexcept
Definition: matpackVII.h:160
Index nbooks() const noexcept
Definition: matpackVII.h:161
Numeric * get_c_array() const noexcept
Conversion to plain C-array, const-version.
Definition: matpackI.h:633
Index nelem() const noexcept
Returns the number of elements.
Definition: matpackI.h:547
The Matrix class.
Definition: matpackI.h:1285
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1011
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIII.cc:315
The Tensor3 class.
Definition: matpackIII.h:352
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:661
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackIV.cc:342
The Tensor4 class.
Definition: matpackIV.h:435
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackV.cc:662
The Tensor5 class.
Definition: matpackV.h:524
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:1741
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackVI.cc:1721
The Tensor6 class.
Definition: matpackVI.h:1105
void resize(Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVI.cc:2176
const Numeric * get_c_array() const ARTS_NOEXCEPT
Conversion to plain C-array.
Definition: matpackVII.cc:4999
The Tensor7 class.
Definition: matpackVII.h:2407
void resize(Index l, Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVII.cc:5488
The Vector class.
Definition: matpackI.h:910
void resize(Index n)
Resize function.
Definition: matpackI.cc:390
The ARTS XML tag class.
Definition: xml_io_base.h:70
void add_attribute(const String &aname, String value)
Definition: xml_io_base.cc:67
void write_to_stream(ostream &os)
Write XML tag.
Definition: xml_io_base.cc:322
void check_name(const String &expected_name)
Check tag name.
Definition: xml_io_base.cc:54
void read_from_stream(istream &is)
Reads next XML tag.
Definition: xml_io_base.cc:201
void get_attribute_value(const String &aname, String &value)
Definition: xml_io_base.cc:140
void set_name(const String &new_name)
Definition: xml_io_base.h:78
Binary output file stream class.
Definition: bifstream.h:43
void readDoubleArray(double *d, unsigned long size)
Definition: binio.cc:184
Binary output file stream class.
Definition: bofstream.h:42
Input manipulator class for doubles to enable nan and inf parsing.
Definition: double_imanip.h:42
Fast double input stream with support for parsing nan and inf.
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
The Sparse class.
Definition: matpackII.h:75
Index nnz() const
Returns the number of nonzero elements.
Definition: matpackII.cc:68
void resize(Index r, Index c)
Resize function.
Definition: matpackII.cc:361
void list_elements(Vector &values, ArrayOfIndex &row_indices, ArrayOfIndex &column_indices) const
List elements in matrix.
Definition: matpackII.cc:266
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:62
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:65
void insert_elements(Index nnz, const ArrayOfIndex &rowind, const ArrayOfIndex &colind, ConstVectorView data)
Insert vector of elements with given row and column indices.
Definition: matpackII.cc:337
#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:179
void xml_set_stream_precision(ostream &os)
Definition: xml_io_base.cc:705
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:539
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.