ARTS 2.5.11 (git: 725533f0)
xml_io.cc
Go to the documentation of this file.
1
2// File description
4
13#include "xml_io.h"
14#include "arts.h"
15#include "bifstream.h"
16#include "bofstream.h"
17#include "file.h"
18#include "parameters.h"
19
20
22// ArtsXMLTag implementation
24
25void ArtsXMLTag::add_attribute(const String& aname, const std::vector<QuantumNumberType>& value) {
26 ostringstream v;
27
28 if(value.size() == 0)
29 v << "";
30 else {
31 for(size_t i=0; i<value.size()-1; i++)
32 v << value[i] << ' ';
33 v << value.back();
34 }
35
36 add_attribute(aname, v.str());
37}
38
39void ArtsXMLTag::add_attribute(const String& aname, const ArrayOfSpecies& value, const bool self, const bool bath) {
40 ostringstream v;
41
42 if(self)
44 for(Index i=Index(self); i<value.nelem()-Index(bath); i++)
45 v << ' ' << Species::toShortName(value[i]);
46 if(bath) {
48 }
49
50 add_attribute(aname, v.str());
51}
52
54 String attribute_value;
55
56 get_attribute_value(aname, attribute_value);
57 value = SpeciesTag(attribute_value);
58}
59
60void ArtsXMLTag::get_attribute_value(const String& aname, ArrayOfSpecies& value, bool& self, bool& bath) {
61 value.resize(0);
62 self=false;
63 bath=false;
64
65 String attribute_value;
66 istringstream strstr("");
67
68 get_attribute_value(aname, attribute_value);
69 if (attribute_value.nelem() == 0) return;
70
71 strstr.str(attribute_value);
72 String val;
73
74 while(not strstr.eof()) {
75 strstr >> val;
76 if (strstr.fail()) {
77 xml_parse_error("Error while parsing value of " + aname + " from <" + name +
78 ">");
79 }
80
82 value.push_back(Species::Species::FINAL);
83 self = true;
84 }
85 else if(val == LineShape::bath_broadening) {
86 value.push_back(Species::Species::Bath);
87 bath = true;
88 }
89 else {
90 Species::Species x = Species::fromShortName(val);
91 ARTS_USER_ERROR_IF(not good_enum(x), "Species: ", val, " cannot be understood")
92 value.push_back(x);
93 }
94 }
95}
96
97void ArtsXMLTag::get_attribute_value(const String& aname, std::vector<QuantumNumberType>& value) {
98 value.resize(0);
99
100 String attribute_value;
101 istringstream strstr("");
102
103 get_attribute_value(aname, attribute_value);
104 if (attribute_value.nelem() == 0) return;
105
106 strstr.str(attribute_value);
107 String val;
108
109 while(not strstr.eof()) {
110 strstr >> val;
111 if (strstr.fail()) {
112 xml_parse_error("Error while parsing value of " + aname + " from <" + name +
113 ">");
114 }
115 value.push_back(Quantum::Number::toType(val));
116 }
117}
118
119void xml_find_and_open_input_file(std::shared_ptr<istream>& ifs,
120 const String& filename,
121 const Verbosity& verbosity) {
123
124 String xml_file = filename;
125 find_xml_file(xml_file, verbosity);
126 out2 << " Reading " << xml_file << '\n';
127
128 // Open input stream:
129 if (xml_file.substr(xml_file.length() - 3, 3) == ".gz")
130#ifdef ENABLE_ZLIB
131 {
132 ifs = std::shared_ptr<istream>(new igzstream());
134 *(static_cast<igzstream*>(ifs.get())), xml_file, verbosity);
135 }
136#else
137 {
138 throw runtime_error(
139 "This arts version was compiled without zlib support.\n"
140 "Thus zipped xml files cannot be read.");
141 }
142#endif /* ENABLE_ZLIB */
143 else {
144 ifs = shared_ptr<istream>(new ifstream());
146 *(static_cast<ifstream*>(ifs.get())), xml_file, verbosity);
147 }
148}
149
151// General XML functions (file header, start root tag, end root tag)
153
155
163 ostringstream os;
164 os << "XML data parse error: Error reading ";
165 tag.write_to_stream(os);
166 os << str_error << "\n"
167 << "Check syntax of XML file. A possible cause is that the file "
168 << "contains NaN or Inf values.\n";
169 throw runtime_error(os.str());
170}
171
173// Default file name
175
177
183void filename_xml(String& filename, const String& varname) {
184 if ("" == filename) {
185 extern const String out_basename;
186 filename = out_basename + "." + varname + ".xml";
187 }
188}
189
191
200 const Index& file_index,
201 const String& varname,
202 const Index& digits) {
203 if ("" == filename) {
204 extern const String out_basename;
205 ostringstream os;
206 os << out_basename << "." << varname << "." << std::setw((int)digits)
207 << std::setfill('0') << file_index << ".xml";
208 filename = os.str();
209 } else {
210 ostringstream os;
211 os << filename << "." << std::setw((int)digits) << std::setfill('0')
212 << file_index << ".xml";
213 filename = os.str();
214 }
215}
The global header file for ARTS.
This file contains the class declaration of bifstream.
This file contains the class declaration of bofstream.
Index nelem() const ARTS_NOEXCEPT
Definition array.h:75
The ARTS XML tag class.
Definition xml_io.h:28
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:25
void get_attribute_value(const String &aname, SpeciesTag &value)
Returns value of attribute as type SpeciesTag.
Definition xml_io.cc:53
String name
void write_to_stream(ostream &os)
Write XML tag.
Index nelem() const
Definition mystring.h:172
#define ARTS_USER_ERROR_IF(condition,...)
Definition debug.h:137
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:338
This file contains basic functions to handle ASCII files.
String out_basename
The basename for the report file and for all other output files.
Definition messages.cc:25
#define CREATE_OUT2
Definition messages.h:188
constexpr std::string_view bath_broadening
Name for bath broadening in printing and reading user input.
constexpr std::string_view self_broadening
Name for self broadening in printing and reading user input.
This file contains header information for the dealing with command line parameters.
Species::Tag SpeciesTag
#define v
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:199
void xml_data_parse_error(ArtsXMLTag &tag, String str_error)
Throws XML parser runtime error.
Definition xml_io.cc:162
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:119
void filename_xml(String &filename, const String &varname)
Gives the default filename for the XML formats.
Definition xml_io.cc:183
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.
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.