ARTS  2.3.1285(git:92a29ea9-dirty)
m_xml.h
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 
27 #ifndef m_xml_h
28 #define m_xml_h
29 
30 #ifdef ENABLE_MPI
31 #include "mpi.h"
32 #endif
33 
34 #include "agenda_class.h"
35 #include "exceptions.h"
36 #include "workspace_ng.h"
37 #include "xml_io.h"
38 
39 /* Workspace method: Doxygen documentation will be auto-generated */
40 template <typename T>
41 void ReadXML( // WS Generic Output:
42  T& v,
43  // WS Generic Output Names:
44  const String& v_name,
45  // WS Generic Input:
46  const String& f,
47  // WS Generic Input Names:
48  const String& f_name _U_,
49  const Verbosity& verbosity) {
50  String filename = f;
51 
52  // Create default filename if empty
53  filename_xml(filename, v_name);
54 
55  xml_read_from_file(filename, v, verbosity);
56 }
57 
58 /* Workspace method: Doxygen documentation will be auto-generated */
59 template <typename T>
61  // WS Generic Output:
62  T& v,
63  // WS Generic Output Names:
64  const String& v_name,
65  // WS Generic Input:
66  const String& f,
67  // WS Generic Input Names:
68  const String& f_name,
69  const Verbosity& verbosity) {
70  ReadXML(v, v_name, f, f_name, verbosity);
71 }
72 
73 /* Workspace method: Doxygen documentation will be auto-generated */
74 template <typename T>
75 void ReadXMLIndexed( // WS Generic Output:
76  T& v,
77  // WS Generic Output Names:
78  const String& v_name,
79  // WS Input:
80  const Index& file_index,
81  // WS Generic Input:
82  const String& f,
83  const Index& digits,
84  // WS Generic Input Names:
85  const String& f_name _U_,
86  const String& digits_name _U_,
87  const Verbosity& verbosity) {
88  String filename = f;
89 
90  // Create default filename if empty
91  filename_xml_with_index(filename, file_index, v_name, digits);
92 
93  xml_read_from_file(filename, v, verbosity);
94 }
95 
96 /* Workspace method: Doxygen documentation will be auto-generated */
97 template <typename T>
99  // WS Generic Output:
100  T& v,
101  // WS Generic Output Names:
102  const String& v_name,
103  // WS Input:
104  const Index& file_index,
105  // WS Generic Input:
106  const String& f,
107  const Index& digits,
108  // WS Generic Input Names:
109  const String& f_name,
110  const String& digits_name,
111  const Verbosity& verbosity) {
113  v, v_name, file_index, f, digits, f_name, digits_name, verbosity);
114 }
115 
116 /* Workspace method: Doxygen documentation will be auto-generated */
117 template <typename T>
118 void WriteXML( //WS Input:
119  const String& file_format,
120  // WS Generic Input:
121  const T& v,
122  const String& f,
123  const Index& no_clobber,
124  // WS Generic Input Names:
125  const String& v_name,
126  const String& f_name _U_,
127  const String& no_clobber_name _U_,
128  const Verbosity& verbosity)
129 
130 {
131  // If MPI is enabled make sure only master process performs the write.
132 #ifdef ENABLE_MPI
133  int initialized;
134  MPI_Initialized(&initialized);
135  if (!initialized) {
136  MPI_Init(nullptr, nullptr);
137  }
138  int rank;
139  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
140  if (rank != 0) {
141  return;
142  }
143 #endif // ENABLE_MPI
144 
145  String filename = f;
146 
147  // Create default filename if empty
148  filename_xml(filename, v_name);
149 
150  const FileType ftype = string2filetype(file_format);
151 
152  String errmsg;
153 
154 #pragma omp critical(WriteXML_critical_region)
155  {
156  try {
157  xml_write_to_file(filename, v, ftype, no_clobber, verbosity);
158  } catch (const std::exception& e) {
159  errmsg = e.what();
160  }
161  }
162 
163  if (errmsg.length()) {
164  throw std::runtime_error(errmsg);
165  }
166 }
167 
168 /* Workspace method: Doxygen documentation will be auto-generated */
169 template <typename T>
171  //WS Input:
172  const String& file_format,
173  // WS Generic Input:
174  const T& v,
175  const String& f,
176  const Index& no_clobber,
177  // WS Generic Input Names:
178  const String& v_name,
179  const String& f_name,
180  const String& no_clobber_name,
181  const Verbosity& verbosity) {
182  WriteXML(file_format,
183  v,
184  f,
185  no_clobber,
186  v_name,
187  f_name,
188  no_clobber_name,
189  verbosity);
190 }
191 
192 /* Workspace method: Doxygen documentation will be auto-generated */
193 template <typename T>
194 void WriteXMLIndexed( //WS Input:
195  const String& file_format,
196  const Index& file_index,
197  // WS Generic Input:
198  const T& v,
199  const String& f,
200  const Index& digits,
201  // WS Generic Input Names:
202  const String& v_name,
203  const String& f_name,
204  const String& digits_name _U_,
205  const Verbosity& verbosity) {
206  String filename = f;
207 
208  // Create default filename if empty
209  filename_xml_with_index(filename, file_index, v_name, digits);
210 
211  WriteXML(file_format, v, filename, 0, v_name, f_name, "", verbosity);
212 }
213 
214 /* Workspace method: Doxygen documentation will be auto-generated */
215 template <typename T>
217  //WS Input:
218  const String& file_format,
219  const Index& file_index,
220  // WS Generic Input:
221  const T& v,
222  const String& f,
223  const Index& digits,
224  // WS Generic Input Names:
225  const String& v_name,
226  const String& f_name,
227  const String& digits_name,
228  const Verbosity& verbosity) {
229  WriteXMLIndexed(file_format,
230  file_index,
231  v,
232  f,
233  digits,
234  v_name,
235  f_name,
236  digits_name,
237  verbosity);
238 }
239 
240 #endif // m_xml_h
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
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:450
void ReadXMLIndexed(T &v, const String &v_name, const Index &file_index, const String &f, const Index &digits, const String &f_name, const String &digits_name, const Verbosity &verbosity)
WORKSPACE METHOD: ReadXMLIndexed.
Definition: m_xml.h:75
This file contains basic functions to handle XML data files.
FileType
Definition: xml_io.h:37
void WriteXMLIndexed(const String &file_format, const Index &file_index, const T &v, const String &f, const Index &digits, const String &v_name, const String &f_name, const String &digits_name, const Verbosity &verbosity)
WORKSPACE METHOD: WriteXMLIndexed.
Definition: m_xml.h:194
This file contains the Workspace class.
Declarations for agendas.
The declarations of all the exception classes.
void xml_read_from_file(const String &filename, T &type, const Verbosity &verbosity)
Reads data from XML file.
Definition: xml_io.cc:901
void ReadXML(T &v, const String &v_name, const String &f, const String &f_name, const Verbosity &verbosity)
WORKSPACE METHOD: ReadXML.
Definition: m_xml.h:41
void filename_xml(String &filename, const String &varname)
Gives the default filename for the XML formats.
Definition: xml_io.cc:434
Workspace class.
Definition: workspace_ng.h:40
#define _U_
Definition: config.h:183
void WriteXML(const String &file_format, const T &v, const String &f, const Index &no_clobber, const String &v_name, const String &f_name, const String &no_clobber_name, const Verbosity &verbosity)
WORKSPACE METHOD: WriteXML.
Definition: m_xml.h:118
void xml_write_to_file(const String &filename, const T &type, const FileType ftype, const Index no_clobber, const Verbosity &verbosity)
Write data to XML file.
Definition: xml_io.cc:972
FileType string2filetype(const String &file_format)
Definition: xml_io.h:79