ARTS 2.5.0 (git: 9ee3ac6c)
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);
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);
122
123 for (Index r = 0; r < nrows; r++) {
124 for (Index c = 0; c < ncols; c++) {
125 if (pbifs) {
126 *pbifs >> matrix(r, c);
127 if (pbifs->fail()) {
128 ostringstream os;
129 os << " near "
130 << "\n Row : " << r << "\n Column: " << c;
131 xml_data_parse_error(tag, os.str());
132 }
133 } else {
134 is_xml >> double_imanip() >> matrix(r, c);
135 if (is_xml.fail()) {
136 ostringstream os;
137 os << " near "
138 << "\n Row : " << r << "\n Column: " << c;
139 xml_data_parse_error(tag, os.str());
140 }
141 }
142 }
143 }
144
145 tag.read_from_stream(is_xml);
146 tag.check_name("/Matrix");
147}
148
150
156void xml_write_to_stream(ostream& os_xml,
157 const Matrix& matrix,
158 bofstream* pbofs,
159 const String& name,
160 const Verbosity& verbosity) {
161 XMLTag open_tag(verbosity);
162 XMLTag close_tag(verbosity);
163
164 open_tag.set_name("Matrix");
165 if (name.length()) open_tag.add_attribute("name", name);
166 open_tag.add_attribute("nrows", matrix.nrows());
167 open_tag.add_attribute("ncols", matrix.ncols());
168
169 open_tag.write_to_stream(os_xml);
170 os_xml << '\n';
171
173
174 // Write the elements:
175 for (Index r = 0; r < matrix.nrows(); ++r) {
176 if (pbofs)
177 *pbofs << matrix(r, 0);
178 else
179 os_xml << matrix(r, 0);
180
181 for (Index c = 1; c < matrix.ncols(); ++c) {
182 if (pbofs)
183 *pbofs << matrix(r, c);
184 else
185 os_xml << " " << matrix(r, c);
186 }
187
188 if (!pbofs) os_xml << '\n';
189 }
190
191 close_tag.set_name("/Matrix");
192 close_tag.write_to_stream(os_xml);
193
194 os_xml << '\n';
195}
196
197//=== Numeric =========================================================
198
200
205void xml_read_from_stream(istream& is_xml,
206 Numeric& numeric,
207 bifstream* pbifs,
208 const Verbosity& verbosity) {
209 XMLTag tag(verbosity);
210
211 tag.read_from_stream(is_xml);
212 tag.check_name("Numeric");
213
214 if (pbifs) {
215 *pbifs >> numeric;
216 if (pbifs->fail()) {
217 xml_data_parse_error(tag, "");
218 }
219 } else {
220 is_xml >> double_imanip() >> numeric;
221 if (is_xml.fail()) {
222 xml_data_parse_error(tag, "");
223 }
224 }
225
226 tag.read_from_stream(is_xml);
227 tag.check_name("/Numeric");
228}
229
231
237void xml_write_to_stream(ostream& os_xml,
238 const Numeric& numeric,
239 bofstream* pbofs,
240 const String& name,
241 const Verbosity& verbosity) {
242 XMLTag open_tag(verbosity);
243 XMLTag close_tag(verbosity);
244
245 open_tag.set_name("Numeric");
246 if (name.length()) open_tag.add_attribute("name", name);
247
248 open_tag.write_to_stream(os_xml);
249
251
252 if (pbofs)
253 *pbofs << numeric;
254 else
255 os_xml << numeric;
256
257 close_tag.set_name("/Numeric");
258 close_tag.write_to_stream(os_xml);
259 os_xml << '\n';
260}
261
262//=== Sparse ====================================================
263
265
270void xml_read_from_stream(istream& is_xml,
271 Sparse& sparse,
272 bifstream* pbifs,
273 const Verbosity& verbosity) {
274 XMLTag tag(verbosity);
275 Index nrows, ncols, nnz;
276
277 tag.read_from_stream(is_xml);
278 tag.check_name("Sparse");
279
280 tag.get_attribute_value("nrows", nrows);
281 tag.get_attribute_value("ncols", ncols);
282 sparse.resize(nrows, ncols);
283
284 tag.read_from_stream(is_xml);
285 tag.check_name("RowIndex");
286 tag.get_attribute_value("nelem", nnz);
287
288 ArrayOfIndex rowind(nnz), colind(nnz);
289 Vector data(nnz);
290
291 for (Index i = 0; i < nnz; i++) {
292 if (pbifs) {
293 *pbifs >> rowind[i];
294 if (pbifs->fail()) {
295 ostringstream os;
296 os << " near "
297 << "\n Row index: " << i;
298 xml_data_parse_error(tag, os.str());
299 }
300 } else {
301 is_xml >> rowind[i];
302 if (is_xml.fail()) {
303 ostringstream os;
304 os << " near "
305 << "\n Row index: " << i;
306 xml_data_parse_error(tag, os.str());
307 }
308 }
309 }
310 tag.read_from_stream(is_xml);
311 tag.check_name("/RowIndex");
312
313 tag.read_from_stream(is_xml);
314 tag.check_name("ColIndex");
315
316 for (Index i = 0; i < nnz; i++) {
317 if (pbifs) {
318 *pbifs >> colind[i];
319 if (pbifs->fail()) {
320 ostringstream os;
321 os << " near "
322 << "\n Column index: " << i;
323 xml_data_parse_error(tag, os.str());
324 }
325 } else {
326 is_xml >> colind[i];
327 if (is_xml.fail()) {
328 ostringstream os;
329 os << " near "
330 << "\n Column index: " << i;
331 xml_data_parse_error(tag, os.str());
332 }
333 }
334 }
335 tag.read_from_stream(is_xml);
336 tag.check_name("/ColIndex");
337
338 tag.read_from_stream(is_xml);
339 tag.check_name("SparseData");
340
341 for (Index i = 0; i < nnz; i++) {
342 if (pbifs) {
343 *pbifs >> data[i];
344 if (pbifs->fail()) {
345 ostringstream os;
346 os << " near "
347 << "\n Data element: " << i;
348 xml_data_parse_error(tag, os.str());
349 }
350 } else {
351 is_xml >> double_imanip() >> data[i];
352 if (is_xml.fail()) {
353 ostringstream os;
354 os << " near "
355 << "\n Data element: " << i;
356 xml_data_parse_error(tag, os.str());
357 }
358 }
359 }
360 tag.read_from_stream(is_xml);
361 tag.check_name("/SparseData");
362
363 tag.read_from_stream(is_xml);
364 tag.check_name("/Sparse");
365
366 sparse.insert_elements(nnz, rowind, colind, data);
367}
368
370
376void xml_write_to_stream(ostream& os_xml,
377 const Sparse& sparse,
378 bofstream* pbofs,
379 const String& name,
380 const Verbosity& verbosity) {
381 XMLTag sparse_tag(verbosity);
382 XMLTag row_tag(verbosity);
383 XMLTag col_tag(verbosity);
384 XMLTag data_tag(verbosity);
385 XMLTag close_tag(verbosity);
386
387 sparse_tag.set_name("Sparse");
388 if (name.length()) sparse_tag.add_attribute("name", name);
389 sparse_tag.add_attribute("nrows", sparse.nrows());
390 sparse_tag.add_attribute("ncols", sparse.ncols());
391 //sparse_tag.add_attribute ("nnz", sparse.nnz());
392 row_tag.set_name("RowIndex");
393 row_tag.add_attribute("nelem", sparse.nnz());
394 col_tag.set_name("ColIndex");
395 col_tag.add_attribute("nelem", sparse.nnz());
396 data_tag.set_name("SparseData");
397 data_tag.add_attribute("nelem", sparse.nnz());
398
399 sparse_tag.write_to_stream(os_xml);
400 os_xml << '\n';
401
402 row_tag.write_to_stream(os_xml);
403 os_xml << '\n';
404
405 ArrayOfIndex rowind(sparse.nnz()), colind(sparse.nnz());
406 Vector data(sparse.nnz());
407 sparse.list_elements(data, rowind, colind);
408
409 // Write row indices.
410
411 for (Index i = 0; i < sparse.nnz(); i++) {
412 if (pbofs)
413 //FIXME: It should be the longer lines
414 *pbofs << rowind[i];
415 else
416 os_xml << rowind[i] << '\n';
417 }
418
419 close_tag.set_name("/RowIndex");
420 close_tag.write_to_stream(os_xml);
421 os_xml << '\n';
422
423 col_tag.write_to_stream(os_xml);
424 os_xml << '\n';
425
426 // Write column indices.
427
428 for (Index i = 0; i < sparse.nnz(); i++) {
429 if (pbofs)
430 //FIXME: It should be the longer lines
431 *pbofs << colind[i];
432 else
433 os_xml << colind[i] << '\n';
434 }
435
436 close_tag.set_name("/ColIndex");
437 close_tag.write_to_stream(os_xml);
438 os_xml << '\n';
439
440 data_tag.write_to_stream(os_xml);
441 os_xml << '\n';
443
444 // Write data.
445
446 for (Index i = 0; i < sparse.nnz(); i++) {
447 if (pbofs)
448 *pbofs << data[i];
449 else
450 os_xml << data[i] << ' ';
451 }
452 os_xml << '\n';
453 close_tag.set_name("/SparseData");
454 close_tag.write_to_stream(os_xml);
455 os_xml << '\n';
456
457 close_tag.set_name("/Sparse");
458 close_tag.write_to_stream(os_xml);
459
460 os_xml << '\n';
461}
462
463//=== String ===========================================================
464
466
470/* param pbifs Pointer to binary input stream. NULL in case of ASCII file.
471 Ignored because strings are always stored in ASCII format. */
472void xml_read_from_stream(istream& is_xml,
473 String& str,
474 bifstream* /* pbifs */,
475 const Verbosity& verbosity) {
476 XMLTag tag(verbosity);
477 char dummy;
478
479 tag.read_from_stream(is_xml);
480 tag.check_name("String");
481
482 // Skip whitespaces
483 bool string_starts_with_quotes = true;
484 do {
485 is_xml >> dummy;
486 switch (dummy) {
487 case ' ':
488 case '\"':
489 case '\n':
490 case '\r':
491 case '\t':
492 break;
493 default:
494 string_starts_with_quotes = false;
495 }
496 } while (is_xml.good() && dummy != '"' && string_starts_with_quotes);
497
498 // Throw exception if first char after whitespaces is not a quote
499 if (!string_starts_with_quotes) {
500 xml_parse_error("String must begin with \"");
501 }
502
503 //catch case where string is empty. CPD 29/8/05
504 dummy = (char)is_xml.peek();
505 if (dummy == '"') {
506 str = "";
507 } else {
508 stringbuf strbuf;
509
510 is_xml.get(strbuf, '"');
511 if (is_xml.fail()) {
512 xml_parse_error("String must end with \"");
513 }
514 str = strbuf.str();
515 }
516
517 // Ignore quote
518 is_xml >> dummy;
519
520 tag.read_from_stream(is_xml);
521 tag.check_name("/String");
522}
523
525
530/* param pbofs Pointer to binary file stream. NULL for ASCII output.
531 Ignored because strings are always in ASCII format. */
532void xml_write_to_stream(ostream& os_xml,
533 const String& str,
534 bofstream* /* pbofs */,
535 const String& name,
536 const Verbosity& verbosity) {
537 XMLTag open_tag(verbosity);
538 XMLTag close_tag(verbosity);
539
540 open_tag.set_name("String");
541 if (name.length()) open_tag.add_attribute("name", name);
542
543 open_tag.write_to_stream(os_xml);
544
545 os_xml << '\"' << str << '\"';
546
547 close_tag.set_name("/String");
548 close_tag.write_to_stream(os_xml);
549 os_xml << '\n';
550}
551
552//=== Tensor3 ================================================================
553
555
560void xml_read_from_stream(istream& is_xml,
561 Tensor3& tensor,
562 bifstream* pbifs,
563 const Verbosity& verbosity) {
564 XMLTag tag(verbosity);
566
567 tag.read_from_stream(is_xml);
568 tag.check_name("Tensor3");
569
570 tag.get_attribute_value("npages", npages);
571 tag.get_attribute_value("nrows", nrows);
572 tag.get_attribute_value("ncols", ncols);
573 tensor.resize(npages, nrows, ncols);
574
575 for (Index p = 0; p < npages; p++) {
576 for (Index r = 0; r < nrows; r++) {
577 for (Index c = 0; c < ncols; c++) {
578 if (pbifs) {
579 *pbifs >> tensor(p, r, c);
580 if (pbifs->fail()) {
581 ostringstream os;
582 os << " near "
583 << "\n Page : " << p << "\n Row : " << r
584 << "\n Column: " << c;
585 xml_data_parse_error(tag, os.str());
586 }
587 } else {
588 is_xml >> double_imanip() >> tensor(p, r, c);
589 if (is_xml.fail()) {
590 ostringstream os;
591 os << " near "
592 << "\n Page : " << p << "\n Row : " << r
593 << "\n Column: " << c;
594 xml_data_parse_error(tag, os.str());
595 }
596 }
597 }
598 }
599 }
600
601 tag.read_from_stream(is_xml);
602 tag.check_name("/Tensor3");
603}
604
606
612void xml_write_to_stream(ostream& os_xml,
613 const Tensor3& tensor,
614 bofstream* pbofs,
615 const String& name,
616 const Verbosity& verbosity) {
617 XMLTag open_tag(verbosity);
618 XMLTag close_tag(verbosity);
619
620 open_tag.set_name("Tensor3");
621 if (name.length()) open_tag.add_attribute("name", name);
622 open_tag.add_attribute("npages", tensor.npages());
623 open_tag.add_attribute("nrows", tensor.nrows());
624 open_tag.add_attribute("ncols", tensor.ncols());
625
626 open_tag.write_to_stream(os_xml);
627 os_xml << '\n';
628
630
631 // Write the elements:
632 for (Index p = 0; p < tensor.npages(); ++p) {
633 for (Index r = 0; r < tensor.nrows(); ++r) {
634 if (pbofs)
635 *pbofs << tensor(p, r, 0);
636 else
637 os_xml << tensor(p, r, 0);
638 for (Index c = 1; c < tensor.ncols(); ++c) {
639 if (pbofs)
640 *pbofs << tensor(p, r, c);
641 else
642 os_xml << " " << tensor(p, r, c);
643 }
644 if (!pbofs) os_xml << '\n';
645 }
646 }
647
648 close_tag.set_name("/Tensor3");
649 close_tag.write_to_stream(os_xml);
650
651 os_xml << '\n';
652}
653
654//=== Tensor4 =========================================================
655
657
662void xml_read_from_stream(istream& is_xml,
663 Tensor4& tensor,
664 bifstream* pbifs,
665 const Verbosity& verbosity) {
666 XMLTag tag(verbosity);
668
669 tag.read_from_stream(is_xml);
670 tag.check_name("Tensor4");
671
672 tag.get_attribute_value("nbooks", nbooks);
673 tag.get_attribute_value("npages", npages);
674 tag.get_attribute_value("nrows", nrows);
675 tag.get_attribute_value("ncols", ncols);
676 tensor.resize(nbooks, npages, nrows, ncols);
677
678 for (Index b = 0; b < nbooks; b++) {
679 for (Index p = 0; p < npages; p++) {
680 for (Index r = 0; r < nrows; r++) {
681 for (Index c = 0; c < ncols; c++) {
682 if (pbifs) {
683 *pbifs >> tensor(b, p, r, c);
684 if (pbifs->fail()) {
685 ostringstream os;
686 os << " near "
687 << "\n Book : " << b << "\n Page : " << p
688 << "\n Row : " << r << "\n Column: " << c;
689 xml_data_parse_error(tag, os.str());
690 }
691 } else {
692 is_xml >> double_imanip() >> tensor(b, p, r, c);
693 if (is_xml.fail()) {
694 ostringstream os;
695 os << " near "
696 << "\n Book : " << b << "\n Page : " << p
697 << "\n Row : " << r << "\n Column: " << c;
698 xml_data_parse_error(tag, os.str());
699 }
700 }
701 }
702 }
703 }
704 }
705
706 tag.read_from_stream(is_xml);
707 tag.check_name("/Tensor4");
708}
709
711
717void xml_write_to_stream(ostream& os_xml,
718 const Tensor4& tensor,
719 bofstream* pbofs,
720 const String& name,
721 const Verbosity& verbosity) {
722 XMLTag open_tag(verbosity);
723 XMLTag close_tag(verbosity);
724
725 open_tag.set_name("Tensor4");
726 if (name.length()) open_tag.add_attribute("name", name);
727 open_tag.add_attribute("nbooks", tensor.nbooks());
728 open_tag.add_attribute("npages", tensor.npages());
729 open_tag.add_attribute("nrows", tensor.nrows());
730 open_tag.add_attribute("ncols", tensor.ncols());
731
732 open_tag.write_to_stream(os_xml);
733 os_xml << '\n';
734
736
737 // Write the elements:
738 for (Index b = 0; b < tensor.nbooks(); ++b) {
739 for (Index p = 0; p < tensor.npages(); ++p) {
740 for (Index r = 0; r < tensor.nrows(); ++r) {
741 if (pbofs)
742 *pbofs << tensor(b, p, r, 0);
743 else
744 os_xml << tensor(b, p, r, 0);
745 for (Index c = 1; c < tensor.ncols(); ++c) {
746 if (pbofs)
747 *pbofs << tensor(b, p, r, c);
748 else
749 os_xml << " " << tensor(b, p, r, c);
750 }
751 if (!pbofs) os_xml << '\n';
752 }
753 }
754 }
755
756 close_tag.set_name("/Tensor4");
757 close_tag.write_to_stream(os_xml);
758
759 os_xml << '\n';
760}
761
762//=== Tensor5 =========================================================
763
765
770void xml_read_from_stream(istream& is_xml,
771 Tensor5& tensor,
772 bifstream* pbifs,
773 const Verbosity& verbosity) {
774 XMLTag tag(verbosity);
776
777 tag.read_from_stream(is_xml);
778 tag.check_name("Tensor5");
779
780 tag.get_attribute_value("nshelves", nshelves);
781 tag.get_attribute_value("nbooks", nbooks);
782 tag.get_attribute_value("npages", npages);
783 tag.get_attribute_value("nrows", nrows);
784 tag.get_attribute_value("ncols", ncols);
786
787 for (Index s = 0; s < nshelves; s++) {
788 for (Index b = 0; b < nbooks; b++) {
789 for (Index p = 0; p < npages; p++) {
790 for (Index r = 0; r < nrows; r++) {
791 for (Index c = 0; c < ncols; c++) {
792 if (pbifs) {
793 *pbifs >> tensor(s, b, p, r, c);
794 if (pbifs->fail()) {
795 ostringstream os;
796 os << " near "
797 << "\n Shelf : " << s << "\n Book : " << b
798 << "\n Page : " << p << "\n Row : " << r
799 << "\n Column: " << c;
800 xml_data_parse_error(tag, os.str());
801 }
802 } else {
803 is_xml >> double_imanip() >> tensor(s, b, p, r, c);
804 if (is_xml.fail()) {
805 ostringstream os;
806 os << " near "
807 << "\n Shelf : " << s << "\n Book : " << b
808 << "\n Page : " << p << "\n Row : " << r
809 << "\n Column: " << c;
810 xml_data_parse_error(tag, os.str());
811 }
812 }
813 }
814 }
815 }
816 }
817 }
818
819 tag.read_from_stream(is_xml);
820 tag.check_name("/Tensor5");
821}
822
824
830void xml_write_to_stream(ostream& os_xml,
831 const Tensor5& tensor,
832 bofstream* pbofs,
833 const String& name,
834 const Verbosity& verbosity) {
835 XMLTag open_tag(verbosity);
836 XMLTag close_tag(verbosity);
837
838 open_tag.set_name("Tensor5");
839 if (name.length()) open_tag.add_attribute("name", name);
840 open_tag.add_attribute("nshelves", tensor.nshelves());
841 open_tag.add_attribute("nbooks", tensor.nbooks());
842 open_tag.add_attribute("npages", tensor.npages());
843 open_tag.add_attribute("nrows", tensor.nrows());
844 open_tag.add_attribute("ncols", tensor.ncols());
845
846 open_tag.write_to_stream(os_xml);
847 os_xml << '\n';
848
850
851 // Write the elements:
852 for (Index s = 0; s < tensor.nshelves(); ++s) {
853 for (Index b = 0; b < tensor.nbooks(); ++b) {
854 for (Index p = 0; p < tensor.npages(); ++p) {
855 for (Index r = 0; r < tensor.nrows(); ++r) {
856 if (pbofs)
857 *pbofs << tensor(s, b, p, r, 0);
858 else
859 os_xml << tensor(s, b, p, r, 0);
860 for (Index c = 1; c < tensor.ncols(); ++c) {
861 if (pbofs)
862 *pbofs << tensor(s, b, p, r, c);
863 else
864 os_xml << " " << tensor(s, b, p, r, c);
865 }
866 if (!pbofs) os_xml << '\n';
867 }
868 }
869 }
870 }
871
872 close_tag.set_name("/Tensor5");
873 close_tag.write_to_stream(os_xml);
874
875 os_xml << '\n';
876}
877
878//=== Tensor6 =========================================================
879
881
886void xml_read_from_stream(istream& is_xml,
887 Tensor6& tensor,
888 bifstream* pbifs,
889 const Verbosity& verbosity) {
890 XMLTag tag(verbosity);
892
893 tag.read_from_stream(is_xml);
894 tag.check_name("Tensor6");
895
896 tag.get_attribute_value("nvitrines", nvitrines);
897 tag.get_attribute_value("nshelves", nshelves);
898 tag.get_attribute_value("nbooks", nbooks);
899 tag.get_attribute_value("npages", npages);
900 tag.get_attribute_value("nrows", nrows);
901 tag.get_attribute_value("ncols", ncols);
903
904 for (Index v = 0; v < nvitrines; v++) {
905 for (Index s = 0; s < nshelves; s++) {
906 for (Index b = 0; b < nbooks; b++) {
907 for (Index p = 0; p < npages; p++) {
908 for (Index r = 0; r < nrows; r++) {
909 for (Index c = 0; c < ncols; c++) {
910 if (pbifs) {
911 *pbifs >> tensor(v, s, b, p, r, c);
912 if (pbifs->fail()) {
913 ostringstream os;
914 os << " near "
915 << "\n Vitrine: " << v << "\n Shelf : " << s
916 << "\n Book : " << b << "\n Page : " << p
917 << "\n Row : " << r << "\n Column : " << c;
918 xml_data_parse_error(tag, os.str());
919 }
920 } else {
921 is_xml >> double_imanip() >> tensor(v, s, b, p, r, c);
922 if (is_xml.fail()) {
923 ostringstream os;
924 os << " near "
925 << "\n Vitrine: " << v << "\n Shelf : " << s
926 << "\n Book : " << b << "\n Page : " << p
927 << "\n Row : " << r << "\n Column : " << c;
928 xml_data_parse_error(tag, os.str());
929 }
930 }
931 }
932 }
933 }
934 }
935 }
936 }
937
938 tag.read_from_stream(is_xml);
939 tag.check_name("/Tensor6");
940}
941
943
949void xml_write_to_stream(ostream& os_xml,
950 const Tensor6& tensor,
951 bofstream* pbofs,
952 const String& name,
953 const Verbosity& verbosity) {
954 XMLTag open_tag(verbosity);
955 XMLTag close_tag(verbosity);
956
957 open_tag.set_name("Tensor6");
958 if (name.length()) open_tag.add_attribute("name", name);
959 open_tag.add_attribute("nvitrines", tensor.nvitrines());
960 open_tag.add_attribute("nshelves", tensor.nshelves());
961 open_tag.add_attribute("nbooks", tensor.nbooks());
962 open_tag.add_attribute("npages", tensor.npages());
963 open_tag.add_attribute("nrows", tensor.nrows());
964 open_tag.add_attribute("ncols", tensor.ncols());
965
966 open_tag.write_to_stream(os_xml);
967 os_xml << '\n';
968
970
971 // Write the elements:
972 for (Index v = 0; v < tensor.nvitrines(); ++v) {
973 for (Index s = 0; s < tensor.nshelves(); ++s) {
974 for (Index b = 0; b < tensor.nbooks(); ++b) {
975 for (Index p = 0; p < tensor.npages(); ++p) {
976 for (Index r = 0; r < tensor.nrows(); ++r) {
977 if (pbofs)
978 *pbofs << tensor(v, s, b, p, r, 0);
979 else
980 os_xml << tensor(v, s, b, p, r, 0);
981 for (Index c = 1; c < tensor.ncols(); ++c) {
982 if (pbofs)
983 *pbofs << tensor(v, s, b, p, r, c);
984 else
985 os_xml << " " << tensor(v, s, b, p, r, c);
986 }
987 if (!pbofs) os_xml << '\n';
988 }
989 }
990 }
991 }
992 }
993
994 close_tag.set_name("/Tensor6");
995 close_tag.write_to_stream(os_xml);
996
997 os_xml << '\n';
998}
999
1000//=== Tensor7 =========================================================
1001
1003
1008void xml_read_from_stream(istream& is_xml,
1009 Tensor7& tensor,
1010 bifstream* pbifs,
1011 const Verbosity& verbosity) {
1012 XMLTag tag(verbosity);
1013 Index nlibraries, nvitrines, nshelves, nbooks, npages, nrows, ncols;
1014
1015 tag.read_from_stream(is_xml);
1016 tag.check_name("Tensor7");
1017
1018 tag.get_attribute_value("nlibraries", nlibraries);
1019 tag.get_attribute_value("nvitrines", nvitrines);
1020 tag.get_attribute_value("nshelves", nshelves);
1021 tag.get_attribute_value("nbooks", nbooks);
1022 tag.get_attribute_value("npages", npages);
1023 tag.get_attribute_value("nrows", nrows);
1024 tag.get_attribute_value("ncols", ncols);
1025 tensor.resize(nlibraries, nvitrines, nshelves, nbooks, npages, nrows, ncols);
1026
1027 for (Index l = 0; l < nlibraries; l++) {
1028 for (Index v = 0; v < nvitrines; v++) {
1029 for (Index s = 0; s < nshelves; s++) {
1030 for (Index b = 0; b < nbooks; b++) {
1031 for (Index p = 0; p < npages; p++) {
1032 for (Index r = 0; r < nrows; r++) {
1033 for (Index c = 0; c < ncols; c++) {
1034 if (pbifs) {
1035 *pbifs >> tensor(l, v, s, b, p, r, c);
1036 if (pbifs->fail()) {
1037 ostringstream os;
1038 os << " near "
1039 << "\n Library: " << l << "\n Vitrine: " << v
1040 << "\n Shelf : " << s << "\n Book : " << b
1041 << "\n Page : " << p << "\n Row : " << r
1042 << "\n Column : " << c;
1043 xml_data_parse_error(tag, os.str());
1044 }
1045 } else {
1046 is_xml >> double_imanip() >> tensor(l, v, s, b, p, r, c);
1047 if (is_xml.fail()) {
1048 ostringstream os;
1049 os << " near "
1050 << "\n Library: " << l << "\n Vitrine: " << v
1051 << "\n Shelf : " << s << "\n Book : " << b
1052 << "\n Page : " << p << "\n Row : " << r
1053 << "\n Column : " << c;
1054 xml_data_parse_error(tag, os.str());
1055 }
1056 }
1057 }
1058 }
1059 }
1060 }
1061 }
1062 }
1063 }
1064
1065 tag.read_from_stream(is_xml);
1066 tag.check_name("/Tensor7");
1067}
1068
1070
1076void xml_write_to_stream(ostream& os_xml,
1077 const Tensor7& tensor,
1078 bofstream* pbofs,
1079 const String& name,
1080 const Verbosity& verbosity) {
1081 XMLTag open_tag(verbosity);
1082 XMLTag close_tag(verbosity);
1083
1084 open_tag.set_name("Tensor7");
1085 if (name.length()) open_tag.add_attribute("name", name);
1086 open_tag.add_attribute("nlibraries", tensor.nlibraries());
1087 open_tag.add_attribute("nvitrines", tensor.nvitrines());
1088 open_tag.add_attribute("nshelves", tensor.nshelves());
1089 open_tag.add_attribute("nbooks", tensor.nbooks());
1090 open_tag.add_attribute("npages", tensor.npages());
1091 open_tag.add_attribute("nrows", tensor.nrows());
1092 open_tag.add_attribute("ncols", tensor.ncols());
1093
1094 open_tag.write_to_stream(os_xml);
1095 os_xml << '\n';
1096
1098
1099 // Write the elements:
1100 for (Index l = 0; l < tensor.nlibraries(); ++l) {
1101 for (Index v = 0; v < tensor.nvitrines(); ++v) {
1102 for (Index s = 0; s < tensor.nshelves(); ++s) {
1103 for (Index b = 0; b < tensor.nbooks(); ++b) {
1104 for (Index p = 0; p < tensor.npages(); ++p) {
1105 for (Index r = 0; r < tensor.nrows(); ++r) {
1106 if (pbofs)
1107 *pbofs << tensor(l, v, s, b, p, r, 0);
1108 else
1109 os_xml << tensor(l, v, s, b, p, r, 0);
1110 for (Index c = 1; c < tensor.ncols(); ++c) {
1111 if (pbofs)
1112 *pbofs << tensor(l, v, s, b, p, r, c);
1113 else
1114 os_xml << " " << tensor(l, v, s, b, p, r, c);
1115 }
1116 if (!pbofs) os_xml << '\n';
1117 }
1118 }
1119 }
1120 }
1121 }
1122 }
1123
1124 close_tag.set_name("/Tensor7");
1125 close_tag.write_to_stream(os_xml);
1126
1127 os_xml << '\n';
1128}
1129
1130//=== Vector ==========================================================
1131
1133
1139void xml_parse_from_stream(istream& is_xml,
1140 Vector& vector,
1141 bifstream* pbifs,
1142 XMLTag& tag,
1143 const Verbosity&) {
1144 Index nelem;
1145
1146 tag.get_attribute_value("nelem", nelem);
1147 vector.resize(nelem);
1148
1149 for (Index n = 0; n < nelem; n++) {
1150 if (pbifs) {
1151 *pbifs >> vector[n];
1152 if (pbifs->fail()) {
1153 ostringstream os;
1154 os << " near "
1155 << "\n Element: " << n;
1156 xml_data_parse_error(tag, os.str());
1157 }
1158 } else {
1159 is_xml >> double_imanip() >> vector[n];
1160 if (is_xml.fail()) {
1161 ostringstream os;
1162 os << " near "
1163 << "\n Element: " << n;
1164 xml_data_parse_error(tag, os.str());
1165 }
1166 }
1167 }
1168}
1169
1171
1176void xml_read_from_stream(istream& is_xml,
1177 Vector& vector,
1178 bifstream* pbifs,
1179 const Verbosity& verbosity) {
1180 XMLTag tag(verbosity);
1181
1182 tag.read_from_stream(is_xml);
1183 tag.check_name("Vector");
1184
1185 xml_parse_from_stream(is_xml, vector, pbifs, tag, verbosity);
1186
1187 tag.read_from_stream(is_xml);
1188 tag.check_name("/Vector");
1189}
1190
1192
1198void xml_write_to_stream(ostream& os_xml,
1199 const Vector& vector,
1200 bofstream* pbofs,
1201 const String& name,
1202 const Verbosity& verbosity) {
1203 XMLTag open_tag(verbosity);
1204 XMLTag close_tag(verbosity);
1205 Index n = vector.nelem();
1206 ostringstream v;
1207
1208 // Convert nelem to string
1209 v << n;
1210
1211 open_tag.set_name("Vector");
1212 if (name.length()) open_tag.add_attribute("name", name);
1213 open_tag.add_attribute("nelem", v.str());
1214
1215 open_tag.write_to_stream(os_xml);
1216 os_xml << '\n';
1217
1219
1220 for (Index i = 0; i < n; ++i)
1221 if (pbofs)
1222 *pbofs << vector[i];
1223 else
1224 os_xml << vector[i] << '\n';
1225
1226 close_tag.set_name("/Vector");
1227 close_tag.write_to_stream(os_xml);
1228
1229 os_xml << '\n';
1230}
1231
1233// Dummy funtion for groups for which
1234// IO function have not yet been implemented
The global header file for ARTS.
Index nrows
Index nbooks
Index npages
void * data
Index nvitrines
Index nshelves
type upp char * str
Index ncols
Index nrows() const ARTS_NOEXCEPT
Returns the number of rows.
Definition: matpackI.cc:433
Index ncols() const ARTS_NOEXCEPT
Returns the number of columns.
Definition: matpackI.cc:436
Index npages() const
Returns the number of pages.
Definition: matpackIII.h:144
Index nrows() const
Returns the number of rows.
Definition: matpackIII.h:147
Index ncols() const
Returns the number of columns.
Definition: matpackIII.h:150
Index npages() const
Returns the number of pages.
Definition: matpackIV.cc:58
Index nbooks() const
Returns the number of books.
Definition: matpackIV.cc:55
Index ncols() const
Returns the number of columns.
Definition: matpackIV.cc:64
Index nrows() const
Returns the number of rows.
Definition: matpackIV.cc:61
Index ncols() const
Returns the number of columns.
Definition: matpackV.cc:56
Index nshelves() const
Returns the number of shelves.
Definition: matpackV.cc:44
Index npages() const
Returns the number of pages.
Definition: matpackV.cc:50
Index nrows() const
Returns the number of rows.
Definition: matpackV.cc:53
Index nbooks() const
Returns the number of books.
Definition: matpackV.cc:47
Index ncols() const
Returns the number of columns.
Definition: matpackVI.cc:57
Index npages() const
Returns the number of pages.
Definition: matpackVI.cc:51
Index nrows() const
Returns the number of rows.
Definition: matpackVI.cc:54
Index nshelves() const
Returns the number of shelves.
Definition: matpackVI.cc:45
Index nbooks() const
Returns the number of books.
Definition: matpackVI.cc:48
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVI.cc:42
Index nshelves() const
Returns the number of shelves.
Definition: matpackVII.cc:48
Index nrows() const
Returns the number of rows.
Definition: matpackVII.cc:57
Index nvitrines() const
Returns the number of vitrines.
Definition: matpackVII.cc:45
Index npages() const
Returns the number of pages.
Definition: matpackVII.cc:54
Index ncols() const
Returns the number of columns.
Definition: matpackVII.cc:60
Index nlibraries() const
Returns the number of libraries.
Definition: matpackVII.cc:42
Index nbooks() const
Returns the number of books.
Definition: matpackVII.cc:51
Index nelem() const ARTS_NOEXCEPT
Returns the number of elements.
Definition: matpackI.cc:48
The Matrix class.
Definition: matpackI.h:1225
void resize(Index r, Index c)
Resize function.
Definition: matpackI.cc:1056
The Sparse class.
Definition: matpackII.h:67
Index nnz() const
Returns the number of nonzero elements.
Definition: matpackII.cc:72
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:270
Index nrows() const
Returns the number of rows.
Definition: matpackII.cc:66
Index ncols() const
Returns the number of columns.
Definition: matpackII.cc:69
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
The Tensor3 class.
Definition: matpackIII.h:339
void resize(Index p, Index r, Index c)
Resize function.
Definition: matpackIII.cc:658
The Tensor4 class.
Definition: matpackIV.h:421
void resize(Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackIV.cc:1058
The Tensor5 class.
Definition: matpackV.h:506
void resize(Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackV.cc:1741
The Tensor6 class.
Definition: matpackVI.h:1088
void resize(Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVI.cc:2171
The Tensor7 class.
Definition: matpackVII.h:2382
void resize(Index l, Index v, Index s, Index b, Index p, Index r, Index c)
Resize function.
Definition: matpackVII.cc:5482
The Vector class.
Definition: matpackI.h:876
void resize(Index n)
Resize function.
Definition: matpackI.cc:408
The ARTS XML tag class.
Definition: xml_io_base.h:68
void add_attribute(const String &aname, const String &value)
Adds a String attribute to tag.
Definition: xml_io_base.cc:60
void write_to_stream(ostream &os)
Write XML tag.
Definition: xml_io_base.cc:300
void check_name(const String &expected_name)
Check tag name.
Definition: xml_io_base.cc:48
void read_from_stream(istream &is)
Reads next XML tag.
Definition: xml_io_base.cc:179
void get_attribute_value(const String &aname, String &value)
Returns value of attribute as String.
Definition: xml_io_base.cc:124
void set_name(const String &new_name)
Definition: xml_io_base.h:76
Binary output file stream class.
Definition: bifstream.h:42
Binary output file stream class.
Definition: bofstream.h:42
Input manipulator class for doubles to enable nan and inf parsing.
Definition: double_imanip.h:64
IO manipulation classes for parsing nan and inf.
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
NUMERIC Numeric
The type to use for all floating point numbers.
Definition: matpack.h:33
Index nelem(const Lines &l)
Number of lines.
Matrix matrix(const std::array< Numeric, 196 > data)
Create the square matrix from the static data.
Definition: igrf13.cc:206
constexpr Numeric l(const Index p0, const Index n, const Numeric x, const SortedVectorType &xi, const Index j, const std::pair< Numeric, Numeric > cycle={ -180, 180}) noexcept
#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:197
void xml_set_stream_precision(ostream &os)
Definition: xml_io_base.cc:683
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:517
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.