ARTS  2.2.66
agenda_record.cc
Go to the documentation of this file.
1 /* Copyright (C) 2002-2012 Stefan Buehler <sbuehler@ltu.se>
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 
26 #include <map>
27 #include <iostream>
28 #include "messages.h"
29 #include "agenda_record.h"
30 #include "wsv_aux.h"
31 #include "workspace_ng.h"
32 
33 namespace global_data {
35 map<String, Index> AgendaMap;
36 
37 extern const Array<AgRecord> agenda_data;
38 extern const ArrayOfString wsv_group_names;
39 }
40 
42 
52 AgRecord::AgRecord(const char name[],
53  const char description[],
54  const MakeArray<String>& output,
55  const MakeArray<String>& input) : mname(name),
56  mdescription(description),
57  moutput(0),
58  minput(0)
59 {
60  // We must check that this agenda exists in the workspace
61 
62  // Find returns end() if the name is not found in the map. If
63  // this assertion fails, it means that we are trying to set the
64  // lookup data for an agenda that has not been defined in
65  // wsv_data (i.e., in workspace.cc). First make a record entry
66  // in workspace.cc for your agenda. If you have done so and
67  // still get an assertion failure, then check that you spelled
68  // the name in exactly the same way in both places.
69  assert(Workspace::WsvMap.end() !=
70  Workspace::WsvMap.find(mname));
71 
72  moutput.resize(output.nelem());
73  for (Index j = 0; j < output.nelem(); ++j)
74  {
75  moutput[j] = get_wsv_id(output[j]);
76  if (moutput[j] == -1)
77  {
78  ostringstream os;
79 
80  os << "Unknown output WSV " << output[j] << " in WSM " << mname;
81  throw runtime_error(os.str());
82  }
83  }
84 
85  minput.resize(input.nelem());
86  for (Index j = 0; j < input.nelem(); ++j)
87  {
88  minput[j] = get_wsv_id(input[j]);
89  if (minput[j] == -1)
90  {
91  ostringstream os;
92 
93  os << "Unknown input WSV " << input[j] << " in WSM " << mname;
94  throw runtime_error(os.str());
95  }
96  }
97 
98 }
99 
101 {
104 
105  for (Index i = 0; i < agenda_data.nelem(); ++i)
106  {
107  AgendaMap[agenda_data[i].Name()] = i;
108  }
109 }
110 
112 
123 {
124  // Make external data visible
127 
128  Index i, j, k;
129 
130  k = 0;
131 
132  // Check, that each agenda from agenda_data occurs in wsv_data:
133  for (i = 0; i < agenda_data.nelem(); ++i)
134  {
135  // cout << "Checking wsv_data for " << agenda_data[i].Name() << ".\n";
136 
137  // Find returns end() if the name is not found in the map. If
138  // this assertion fails, it means that wsv_data does not contain
139  // this agenda.
140  // You have to add a record for your agenda in both files:
141  // workspace.cc and agendas.cc. The name has to be spelled
142  // exactly the same in both places.
143  // Uncomment the cout statement above and recompile to see which
144  // agenda causes the trouble.
145  assert(Workspace::WsvMap.end() !=
146  Workspace::WsvMap.find(agenda_data[i].Name()));
147  }
148 
149  // Check, that each agenda from wsv_data occurs in agenda_data:
150  for (j = 0; j < Workspace::wsv_data.nelem(); ++j)
151  {
152  // Is this an agenda WSV?
153  if (get_wsv_group_id("Agenda") == Workspace::wsv_data[j].Group())
154  {
155  // cout << "Checking agenda_data for " << Workspace::wsv_data[j].Name() << ".\n";
156 
157  // Find returns end() if the name is not found in the map. If
158  // this assertion fails, it means that agenda_data does not contain
159  // this agenda.
160  // You have to add a record for your agenda in both files:
161  // workspace.cc and agendas.cc. The name has to be spelled
162  // exactly the same in both places.
163  // Uncomment the cout statement above and recompile to see which
164  // agenda causes the trouble.
165  assert(AgendaMap.end() !=
166  AgendaMap.find(Workspace::wsv_data[j].Name()));
167 
168  // Counts the number of agenda WSVs in Workspace::wsv_data:
169  ++k;
170  }
171  }
172 
173  // As a last check we make sure that both lists contain the same
174  // number of agendas:
175  assert(i == k);
176 
177  return true;
178 }
179 
181 
188 ostream& operator<<(ostream& os, const AgRecord& agr)
189 {
190  bool first;
191 
192  os << "\n*-------------------------------------------------------------------*\n"
193  << "Workspace variable = " << agr.Name()
194  << "\n---------------------------------------------------------------------\n"
195  << "\n" << agr.Description() << "\n"
196  << "\n---------------------------------------------------------------------\n";
197 
198  os << "Group = Agenda\n";
199 
200  // Output:
201  first = true;
202  os << "Output = ";
203  for (Index i = 0; i < agr.Out().nelem(); ++i)
204  {
205  if (first) first = false;
206  else os << ", ";
207 
208  os << Workspace::wsv_data[agr.Out()[i]].Name();
209  }
210  os << "\n";
211 
212  // Input:
213  first = true;
214  os << "Input = ";
215  for (Index i = 0; i < agr.In().nelem(); ++i)
216  {
217  if (first) first = false;
218  else os << ", ";
219 
220  os << Workspace::wsv_data[agr.In()[i]].Name();
221  }
222 
223  os << "\n*-------------------------------------------------------------------*\n";
224 
225  return os;
226 }
227 
229 
238 ostream& operator<<(ostream& os, const WsvRecord& wr)
239 {
241 
242  // We need a special treatment for the case that the WSV is an agenda.
243 
244  if (get_wsv_group_id("Agenda") != wr.Group())
245  {
246  // No agenda.
247 
248  os << "\n*-------------------------------------------------------------------*\n"
249  << "Workspace variable = " << wr.Name()
250  << "\n---------------------------------------------------------------------\n"
251  << "\n" << wr.Description() << "\n"
252  << "\n---------------------------------------------------------------------\n"
253  << "Group = " << wsv_group_names[wr.Group()]
254  << "\n*-------------------------------------------------------------------*\n";
255  }
256  else
257  {
258  // Agenda.
259 
261 
262  // AgendaMap is constant here and should never be changed
264 
265  map<String, Index>::const_iterator j =
266  AgendaMap.find(wr.Name());
267 
268  // Just for added safety, check that we really found something:
269  assert(j != AgendaMap.end());
270 
271  cout << agenda_data[j->second] << "\n";
272  }
273 
274  return os;
275 }
276 
278 
282 void write_agenda_wrapper_header(ofstream& ofs,
283  const AgRecord& agr)
284 {
286 
287  // Wrapper function
288  ofs << "void " << agr.Name() << "Execute(\n";
289 
290  // Wrapper function Workspace parameters
291  ofs << " // Workspace\n";
292  ofs << " Workspace& ws,\n";
293  // Wrapper function output parameters
294  const ArrayOfIndex& ago = agr.Out();
295  ofs << " // Output\n";
296  for (ArrayOfIndex::const_iterator j = ago.begin(); j != ago.end(); j++)
297  {
298  ofs << " ";
299  ofs << wsv_group_names[Workspace::wsv_data[*j].Group()] << "& ";
300  ofs << Workspace::wsv_data[*j].Name() << ",\n";
301  }
302 
303  // Wrapper function input parameters
304  const ArrayOfIndex& agi = agr.In();
305  ofs << " // Input\n";
306  for (ArrayOfIndex::const_iterator j = agi.begin(); j != agi.end(); j++)
307  {
308  // Ignore Input parameters that are also output
309  ArrayOfIndex::const_iterator it = ago.begin();
310  while (it != ago.end() && *it != *j)
311  it++;
312 
313  if (it == ago.end())
314  {
315  String group_name = wsv_group_names[Workspace::wsv_data[*j].Group()];
316 
317  ofs << " const ";
318  ofs << group_name;
319 
320  // Don't pass by reference for elementary types
321  if (group_name != "Index" && group_name != "Numeric")
322  {
323  ofs << "&";
324  }
325  ofs << " " << Workspace::wsv_data[*j].Name() << ",\n";
326  }
327  }
328 
329  // Wrapper function agenda and silent parameters
330  ofs << " // Wrapper Input\n";
331  ofs << " const Agenda& input_agenda)";
332 }
333 
agenda_record.h
Declarations for AgRecord, storing lookup information for one agenda.
Workspace::wsv_data
static Array< WsvRecord > wsv_data
Definition: workspace_ng.h:64
AgRecord::minput
ArrayOfIndex minput
Workspace Input.
Definition: agenda_record.h:89
AgRecord::AgRecord
AgRecord()
Default constructor.
Definition: agenda_record.h:48
get_wsv_id
Index get_wsv_id(const String &name)
Get index of WSV.
Definition: workspace.cc:4998
DEBUG_ONLY
#define DEBUG_ONLY(...)
Definition: debug.h:37
global_data::AgendaMap
map< String, Index > AgendaMap
The map associated with agenda_data.
Definition: agenda_record.cc:35
Array
This can be used to make arrays out of anything.
Definition: array.h:107
WsvRecord::Group
Index Group() const
The wsv group to which this variable belongs.
Definition: wsv_aux.h:107
messages.h
Declarations having to do with the four output streams.
write_agenda_wrapper_header
void write_agenda_wrapper_header(ofstream &ofs, const AgRecord &agr)
Write a agenda wrapper header.
Definition: agenda_record.cc:282
AgRecord
Lookup information for one agenda.
Definition: agenda_record.h:44
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:64
AgRecord::moutput
ArrayOfIndex moutput
Workspace Output.
Definition: agenda_record.h:86
WsvRecord::Name
const String & Name() const
Name of this workspace variable.
Definition: wsv_aux.h:103
AgRecord::Name
const String & Name() const
Definition: agenda_record.h:62
AgRecord::mname
String mname
The name of this agenda.
Definition: agenda_record.h:80
check_agenda_data
bool check_agenda_data()
Check that agendas.cc and workspace.cc are consistent.
Definition: agenda_record.cc:122
AgRecord::In
const ArrayOfIndex & In() const
Definition: agenda_record.h:65
define_agenda_map
void define_agenda_map()
Definition: agenda_record.cc:100
WsvRecord
This class contains all static information for one workspace variable.
Definition: wsv_aux.h:54
workspace_ng.h
This file contains the declaration and partly the implementation of the workspace class.
WsvRecord::Description
const String & Description() const
A text describing this workspace variable.
Definition: wsv_aux.h:105
global_data
Definition: agenda_record.cc:33
Workspace::WsvMap
static map< String, Index > WsvMap
Definition: workspace_ng.h:67
global_data::agenda_data
const Array< AgRecord > agenda_data
The lookup information for the agendas.
Definition: agendas.cc:41
operator<<
ostream & operator<<(ostream &os, const AgRecord &agr)
Output operator for AgRecord.
Definition: agenda_record.cc:188
MakeArray
Explicit construction of Arrays.
Definition: make_array.h:52
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
AgRecord::Description
const String & Description() const
Definition: agenda_record.h:63
global_data::wsv_group_names
const ArrayOfString wsv_group_names
The names associated with Wsv groups as Strings.
Definition: global_data.h:94
get_wsv_group_id
Index get_wsv_group_id(const String &name)
Returns the id of the given group.
Definition: groups.cc:205
AgRecord::Out
const ArrayOfIndex & Out() const
Definition: agenda_record.h:64
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:176
wsv_aux.h
Auxiliary header stuff related to workspace variable groups.