ARTS 2.5.0 (git: 9ee3ac6c)
xml_io.cc
Go to the documentation of this file.
1/* Copyright (C) 2002-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 "xml_io.h"
31#include "arts.h"
32#include "bifstream.h"
33#include "bofstream.h"
34#include "file.h"
35#include "parameters.h"
36
37
39// ArtsXMLTag implementation
41
42void ArtsXMLTag::add_attribute(const String& aname, const std::vector<QuantumNumberType>& value) {
43 ostringstream v;
44
45 if(value.size() == 0)
46 v << "";
47 else {
48 for(size_t i=0; i<value.size()-1; i++)
49 v << value[i] << ' ';
50 v << value.back();
51 }
52
53 add_attribute(aname, v.str());
54}
55
56void ArtsXMLTag::add_attribute(const String& aname, const ArrayOfSpecies& value, const bool self, const bool bath) {
57 ostringstream v;
58
59 if(self)
60 v << LineShape::self_broadening;
61 for(Index i=Index(self); i<value.nelem()-Index(bath); i++)
62 v << ' ' << Species::toShortName(value[i]);
63 if(bath) {
64 v << ' ' << LineShape::bath_broadening;
65 }
66
67 add_attribute(aname, v.str());
68}
69
71 String attribute_value;
72
73 get_attribute_value(aname, attribute_value);
74 value = SpeciesTag(attribute_value);
75}
76
77void ArtsXMLTag::get_attribute_value(const String& aname, ArrayOfSpecies& value, bool& self, bool& bath) {
78 value.resize(0);
79 self=false;
80 bath=false;
81
82 String attribute_value;
83 istringstream strstr("");
84
85 get_attribute_value(aname, attribute_value);
86 if (attribute_value.nelem() == 0) return;
87
88 strstr.str(attribute_value);
89 String val;
90
91 while(not strstr.eof()) {
92 strstr >> val;
93 if (strstr.fail()) {
94 xml_parse_error("Error while parsing value of " + aname + " from <" + name +
95 ">");
96 }
97
98 if(val == LineShape::self_broadening) {
99 value.push_back(Species::Species::FINAL);
100 self = true;
101 }
102 else if(val == LineShape::bath_broadening) {
103 value.push_back(Species::Species::Bath);
104 bath = true;
105 }
106 else {
107 Species::Species x = Species::fromShortName(val);
108 ARTS_USER_ERROR_IF(not good_enum(x), "Species: ", val, " cannot be understood")
109 value.push_back(x);
110 }
111 }
112}
113
114void ArtsXMLTag::get_attribute_value(const String& aname, std::vector<QuantumNumberType>& value) {
115 value.resize(0);
116
117 String attribute_value;
118 istringstream strstr("");
119
120 get_attribute_value(aname, attribute_value);
121 if (attribute_value.nelem() == 0) return;
122
123 strstr.str(attribute_value);
124 String val;
125
126 while(not strstr.eof()) {
127 strstr >> val;
128 if (strstr.fail()) {
129 xml_parse_error("Error while parsing value of " + aname + " from <" + name +
130 ">");
131 }
132 value.push_back(string2quantumnumbertype(val));
133 }
134}
135
137 String attribute_value;
138 istringstream strstr("");
139
140 get_attribute_value(aname, attribute_value);
141
142 strstr.str(attribute_value);
143 String key;
144 Rational r;
145
146 strstr >> key;
147 while (strstr) {
148 strstr >> r;
149 value.Set(key, r);
150 strstr >> key;
151 }
152}
153
154void xml_find_and_open_input_file(std::shared_ptr<istream>& ifs,
155 const String& filename,
156 const Verbosity& verbosity) {
158
159 String xml_file = filename;
160 find_xml_file(xml_file, verbosity);
161 out2 << " Reading " << xml_file << '\n';
162
163 // Open input stream:
164 if (xml_file.substr(xml_file.length() - 3, 3) == ".gz")
165#ifdef ENABLE_ZLIB
166 {
167 ifs = std::shared_ptr<istream>(new igzstream());
169 *(std::static_pointer_cast<igzstream>(ifs)), xml_file, verbosity);
170 }
171#else
172 {
173 throw runtime_error(
174 "This arts version was compiled without zlib support.\n"
175 "Thus zipped xml files cannot be read.");
176 }
177#endif /* ENABLE_ZLIB */
178 else {
179 ifs = shared_ptr<istream>(new ifstream());
181 *(std::static_pointer_cast<ifstream>(ifs)), xml_file, verbosity);
182 }
183}
184
186// General XML functions (file header, start root tag, end root tag)
188
190
198 ostringstream os;
199 os << "XML data parse error: Error reading ";
200 tag.write_to_stream(os);
201 os << str_error << "\n"
202 << "Check syntax of XML file. A possible cause is that the file "
203 << "contains NaN or Inf values.\n";
204 throw runtime_error(os.str());
205}
206
208// Default file name
210
212
218void filename_xml(String& filename, const String& varname) {
219 if ("" == filename) {
220 extern const String out_basename;
221 filename = out_basename + "." + varname + ".xml";
222 }
223}
224
226
235 const Index& file_index,
236 const String& varname,
237 const Index& digits) {
238 if ("" == filename) {
239 extern const String out_basename;
240 ostringstream os;
241 os << out_basename << "." << varname << "." << std::setw((int)digits)
242 << std::setfill('0') << file_index << ".xml";
243 filename = os.str();
244 } else {
245 ostringstream os;
246 os << filename << "." << std::setw((int)digits) << std::setfill('0')
247 << file_index << ".xml";
248 filename = os.str();
249 }
250}
The global header file for ARTS.
String out_basename
The basename for the report file and for all other output files.
Definition: messages.cc:42
This file contains the class declaration of bifstream.
This file contains the class declaration of bofstream.
Index nelem() const ARTS_NOEXCEPT
Number of elements.
Definition: array.h:195
The ARTS XML tag class.
Definition: xml_io.h:45
void add_attribute(const String &aname, const std::vector< QuantumNumberType > &value)
Adds value of attribute as type std::vector<QuantumNumberType> to tag.
Definition: xml_io.cc:42
void get_attribute_value(const String &aname, SpeciesTag &value)
Returns value of attribute as type SpeciesTag.
Definition: xml_io.cc:70
Container class for Quantum Numbers.
Definition: quantum.h:112
constexpr void Set(Index qn, Rational r)
Set quantum number at position.
Definition: quantum.h:199
Implements rational numbers to work with other ARTS types.
Definition: rational.h:52
String name
Definition: xml_io_base.h:118
void write_to_stream(ostream &os)
Write XML tag.
Definition: xml_io_base.cc:300
Index nelem() const
Number of elements.
Definition: mystring.h:253
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:134
constexpr bool good_enum(EnumType x) noexcept
Checks if the enum number is good.
Definition: enums.h:21
void find_xml_file(String &filename, const Verbosity &verbosity)
Find an xml file.
Definition: file.cc:414
This file contains basic functions to handle ASCII files.
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
#define CREATE_OUT2
Definition: messages.h:206
This file contains header information for the dealing with command line parameters.
#define v
Species::Tag SpeciesTag
Definition: species_tags.h:99
void filename_xml_with_index(String &filename, const Index &file_index, const String &varname, const Index &digits)
Gives the default filename, with file index, for the XML formats.
Definition: xml_io.cc:234
void xml_data_parse_error(ArtsXMLTag &tag, String str_error)
Throws XML parser runtime error.
Definition: xml_io.cc:197
void xml_find_and_open_input_file(std::shared_ptr< istream > &ifs, const String &filename, const Verbosity &verbosity)
Open plain or zipped xml file.
Definition: xml_io.cc:154
void filename_xml(String &filename, const String &varname)
Gives the default filename for the XML formats.
Definition: xml_io.cc:218
This file contains basic functions to handle XML data files.
void xml_open_input_file(ifstream &ifs, const String &name, const Verbosity &verbosity)
Open file for XML input.
Definition: xml_io_base.cc:427
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:517