ARTS 2.5.9 (git: 825fa5f2)
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(Quantum::Number::toType(val));
133 }
134}
135
136void xml_find_and_open_input_file(std::shared_ptr<istream>& ifs,
137 const String& filename,
138 const Verbosity& verbosity) {
140
141 String xml_file = filename;
142 find_xml_file(xml_file, verbosity);
143 out2 << " Reading " << xml_file << '\n';
144
145 // Open input stream:
146 if (xml_file.substr(xml_file.length() - 3, 3) == ".gz")
147#ifdef ENABLE_ZLIB
148 {
149 ifs = std::shared_ptr<istream>(new igzstream());
151 *(static_cast<igzstream*>(ifs.get())), xml_file, verbosity);
152 }
153#else
154 {
155 throw runtime_error(
156 "This arts version was compiled without zlib support.\n"
157 "Thus zipped xml files cannot be read.");
158 }
159#endif /* ENABLE_ZLIB */
160 else {
161 ifs = shared_ptr<istream>(new ifstream());
163 *(static_cast<ifstream*>(ifs.get())), xml_file, verbosity);
164 }
165}
166
168// General XML functions (file header, start root tag, end root tag)
170
172
180 ostringstream os;
181 os << "XML data parse error: Error reading ";
182 tag.write_to_stream(os);
183 os << str_error << "\n"
184 << "Check syntax of XML file. A possible cause is that the file "
185 << "contains NaN or Inf values.\n";
186 throw runtime_error(os.str());
187}
188
190// Default file name
192
194
200void filename_xml(String& filename, const String& varname) {
201 if ("" == filename) {
202 extern const String out_basename;
203 filename = out_basename + "." + varname + ".xml";
204 }
205}
206
208
217 const Index& file_index,
218 const String& varname,
219 const Index& digits) {
220 if ("" == filename) {
221 extern const String out_basename;
222 ostringstream os;
223 os << out_basename << "." << varname << "." << std::setw((int)digits)
224 << std::setfill('0') << file_index << ".xml";
225 filename = os.str();
226 } else {
227 ostringstream os;
228 os << filename << "." << std::setw((int)digits) << std::setfill('0')
229 << file_index << ".xml";
230 filename = os.str();
231 }
232}
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:92
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
String name
Definition: xml_io_base.h:120
void write_to_stream(ostream &os)
Write XML tag.
Definition: xml_io_base.cc:322
Index nelem() const
Definition: mystring.h:189
#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:355
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
String out_basename
The basename for the report file and for all other output files.
Definition: messages.cc:42
#define CREATE_OUT2
Definition: messages.h:205
This file contains header information for the dealing with command line parameters.
Species::Tag SpeciesTag
Definition: species_tags.h:100
#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:216
void xml_data_parse_error(ArtsXMLTag &tag, String str_error)
Throws XML parser runtime error.
Definition: xml_io.cc:179
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:136
void filename_xml(String &filename, const String &varname)
Gives the default filename for the XML formats.
Definition: xml_io.cc:200
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:449
void xml_parse_error(const String &str_error)
Throws XML parser runtime error.
Definition: xml_io_base.cc:539