ARTS  1.0.222
make_auto_md_h.cc
Go to the documentation of this file.
1 /* Copyright (C) 2000, 2001 Stefan Buehler <sbuehler@uni-bremen.de>
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 
68 #include "arts.h"
69 #include "token.h"
70 #include "array.h"
71 #include "file.h"
72 #include "auto_wsv.h"
73 #include "methods.h"
74 #include "wsv_aux.h"
75 
76 /* Adds commas and indentation to parameter lists. */
77 void align(ofstream& ofs, bool& is_first_parameter, const String& indent)
78 {
79  // Add comma and line break, if not first element:
80  if (is_first_parameter)
81  is_first_parameter = false;
82  else
83  {
84  ofs << ",\n";
85  // Make proper indentation:
86  ofs << indent;
87  }
88 }
89 
90 int main()
91 {
92  try
93  {
94  // Make the global data visible:
95  extern Array<MdRecord> md_data;
96  extern const ArrayOfString wsv_group_names;
97  extern const Array<WsvRecord> wsv_data;
98 
99  // Initialize method data.
100  define_md_data();
101 
102  // Initialize the wsv group name array:
104 
105  // Initialize wsv data.
106  define_wsv_data();
107 
108 
109  const Index n_md = md_data.nelem();
110  const Index n_wsv = wsv_data.nelem();
111 
112  // For safety, check if n_wsv and N_WSV have the same value. If not,
113  // then the file wsv.h is not up to date.
114  if (N_WSV != n_wsv)
115  {
116  cout << "The file wsv.h is not up to date!\n";
117  cout << "(N_WSV = " << N_WSV << ", n_wsv = " << n_wsv << ")\n";
118  cout << "Make wsv.h first. Check if Makefile is correct.\n";
119  return 1;
120  }
121 
122  // Write auto_md.h:
123  // -----------
124  ofstream ofs;
125  open_output_file(ofs,"auto_md.h");
126 
127  ofs << "// This file was generated automatically by make_auto_md_h.cc.\n";
128  ofs << "// DO NOT EDIT !\n";
129  ofs << "// Generated: "
130  << __DATE__ << ", "
131  << __TIME__ << "\n\n";
132 
133  ofs << "#ifndef auto_md_h\n";
134  ofs << "#define auto_md_h\n\n";
135 
136  ofs << "#include \"matpackI.h\"\n"
137  << "#include \"los.h\"\n"
138  << "#include \"absorption.h\"\n"
139  << "#include \"auto_wsv.h\"\n"
140  << "#include \"parser.h\"\n"
141  << "\n";
142 
143  ofs << "// This is only used for a consistency check. You can get the\n"
144  << "// number of workspace variables from wsv_data.nelem().\n"
145  << "#define N_MD " << n_md << "\n\n";
146 
147  ofs << "enum MdHandle{\n";
148  for (Index i=0; i<n_md-1; ++i)
149  {
150  ofs << " " << md_data[i].Name() << "_,\n";
151  }
152  ofs << " " << md_data[n_md-1].Name() << "_\n";
153  ofs << "};\n\n";
154 
155  // Add all the method function declarations
156  ofs << "// Method function declarations:\n\n";
157  for (Index i=0; i<n_md; ++i)
158  {
159 
160  // This is needed to flag the first function parameter, which
161  // needs no line break before being written:
162  bool is_first_parameter = true;
163 
164  // The String indent is needed to achieve the correct
165  // indentation of the functin parameters:
166  String indent(md_data[i].Name().nelem()+6,' ');
167 
168  // There are four lists of parameters that we have to
169  // write.
170  ArrayOfIndex vo=md_data[i].Output(); // Output
171  ArrayOfIndex vi=md_data[i].Input(); // Input
172  ArrayOfIndex vgo=md_data[i].GOutput(); // Generic Output
173  ArrayOfIndex vgi=md_data[i].GInput(); // Generic Input
174  // vo and vi contain handles of workspace variables,
175  // vgo and vgi handles of workspace variable groups.
176 
177  // Check, if some workspace variables are in both the
178  // input and the output list, and erase those from the input
179  // list:
180  for (ArrayOfIndex::const_iterator j=vo.begin(); j<vo.end(); ++j)
181  for (ArrayOfIndex::iterator k=vi.begin(); k<vi.end(); ++k)
182  {
183  if ( *j == *k )
184  {
185  // erase_vector_element(vi,k);
186  k = vi.erase(k) - 1;
187  // We need the -1 here, otherwise due to the
188  // following increment we would miss the element
189  // behind the erased one, which is now at the
190  // position of the erased one.
191  }
192  }
193 
194  // There used to be a similar block here for the generic
195  // input/output variables. However, this was a mistake. For
196  // example, if a method has a vector as generic input and a
197  // vector as generic output, this does not mean that it is
198  // the same vector!
199 
200 
201  // Start with the name of the method:
202  ofs << "void " << md_data[i].Name() << "(";
203 
204  // Write the Output workspace variables:
205  {
206  // Flag first parameter of this sort:
207  bool is_first_of_these = true;
208 
209  for (Index j=0; j<vo.nelem(); ++j)
210  {
211  // Add comma and line break, if not first element:
212  align(ofs,is_first_parameter,indent);
213 
214  // Add comment if this is the first of this sort
215  if (is_first_of_these)
216  {
217  ofs << "// WS Output:\n";
218  ofs << indent;
219  is_first_of_these = false;
220  }
221 
222  ofs << wsv_group_names[wsv_data[vo[j]].Group()] << "&";
223  }
224  }
225 
226  // Write the Generic output workspace variables:
227  {
228  // Flag first parameter of this sort:
229  bool is_first_of_these = true;
230 
231  for (Index j=0; j<vgo.nelem(); ++j)
232  {
233  // Add comma and line break, if not first element:
234  align(ofs,is_first_parameter,indent);
235 
236  // Add comment if this is the first of this sort
237  if (is_first_of_these)
238  {
239  ofs << "// WS Generic Output:\n";
240  ofs << indent;
241  is_first_of_these = false;
242  }
243 
244  ofs << wsv_group_names[md_data[i].GOutput()[j]] << "&";
245  }
246  }
247 
248  // Write the Generic output workspace variable names:
249  {
250  // Flag first parameter of this sort:
251  bool is_first_of_these = true;
252 
253  for (Index j=0; j<vgo.nelem(); ++j)
254  {
255  // Add comma and line break, if not first element:
256  align(ofs,is_first_parameter,indent);
257 
258  // Add comment if this is the first of this sort
259  if (is_first_of_these)
260  {
261  ofs << "// WS Generic Output Names:\n";
262  ofs << indent;
263  is_first_of_these = false;
264  }
265 
266  ofs << "const String&";
267  }
268  }
269 
270  // Write the Input workspace variables:
271  {
272  // Flag first parameter of this sort.
273  bool is_first_of_these = true;
274 
275  for (Index j=0; j<vi.nelem(); ++j)
276  {
277  // Add comma and line break, if not first element:
278  align(ofs,is_first_parameter,indent);
279 
280  // Add type if this is the first of this sort.
281  if (is_first_of_these)
282  {
283  ofs << "// WS Input:\n";
284  ofs << indent;
285  is_first_of_these = false;
286  }
287 
288  ofs << "const "
289  << wsv_group_names[wsv_data[vi[j]].Group()] << "&";
290  }
291  }
292 
293  // Write the Generic input workspace variables:
294  {
295  // Flag first parameter of this sort.
296  bool is_first_of_these = true;
297 
298  for (Index j=0; j<vgi.nelem(); ++j)
299  {
300  // Add comma and line break, if not first element:
301  align(ofs,is_first_parameter,indent);
302 
303  // Add type if this is the first of this sort.
304  if (is_first_of_these)
305  {
306  ofs << "// WS Generic Input:\n";
307  ofs << indent;
308  is_first_of_these = false;
309  }
310 
311  ofs << "const "
312  << wsv_group_names[md_data[i].GInput()[j]] << "&";
313  }
314  }
315 
316  // Write the Generic input workspace variable names:
317  {
318  // Flag first parameter of this sort:
319  bool is_first_of_these = true;
320 
321  for (Index j=0; j<vgi.nelem(); ++j)
322  {
323  // Add comma and line break, if not first element:
324  align(ofs,is_first_parameter,indent);
325 
326  // Add comment if this is the first of this sort
327  if (is_first_of_these)
328  {
329  ofs << "// WS Generic Input Names:\n";
330  ofs << indent;
331  is_first_of_these = false;
332  }
333 
334  ofs << "const String&";
335  }
336  }
337 
338  // Write the control parameters:
339  {
340  // Flag first parameter of this sort.
341  bool is_first_of_these = true;
342 
343  // Number of keyword parameters.
344  Index n_mr = md_data[i].Keywords().nelem();
345 
346  for (Index j=0; j!=n_mr; ++j)
347  {
348  // Add comma and line break, if not first element:
349  align(ofs,is_first_parameter,indent);
350 
351  // Add type if this is the first of this sort.
352  if (is_first_of_these)
353  {
354  ofs << "// Control Parameters:\n";
355  ofs << indent;
356  is_first_of_these = false;
357  }
358 
359  extern String TokValTypeName[];
360  ofs << "const " << TokValTypeName[md_data[i].Types()[j]] << "& "
361  << md_data[i].Keywords()[j];
362  }
363  }
364 
365  ofs << ");\n\n";
366  }
367 
368  // Add all the get-away function declarations:
369  ofs << "// Get-away function declarations:\n\n";
370  for (Index i=0; i<n_md; ++i)
371  ofs << "void " << md_data[i].Name()
372  << "_g(WorkSpace& ws, const MRecord& mr);\n";
373 
374  ofs << "\n";
375 
376  ofs << "\n#endif // auto_md_h\n";
377 
378  // Close auto_md.h.
379  ofs.close();
380 
381  }
382  catch (exception x)
383  {
384  cout << "Something went wrong. Message text:\n";
385  cout << x.what() << '\n';
386  return 1;
387  }
388 
389  return 0;
390 }
define_wsv_data
void define_wsv_data()
Define the lookup data for the workspace variables.
Definition: workspace.cc:44
main
int main()
Definition: make_auto_md_h.cc:90
wsv_data
const Array< WsvRecord > wsv_data
Definition: workspace.cc:42
array.h
This file contains the definition of Array.
Array
This can be used to make arrays out of anything.
Definition: array.h:48
define_md_data
void define_md_data()
Define the lookup data for the workspace methods.
Definition: methods.cc:42
my_basic_string< char >
align
void align(ofstream &ofs, bool &is_first_parameter, const String &indent)
Definition: make_auto_md_h.cc:77
wsv_group_names
ArrayOfString wsv_group_names
Definition: groups.cc:36
define_wsv_group_names
void define_wsv_group_names()
Define the array of workspace variable group names.
Definition: groups.cc:51
open_output_file
void open_output_file(ofstream &file, const String &name)
Open a file for writing.
Definition: file.cc:91
md_data
Array< MdRecord > md_data
The lookup information for the workspace methods.
Definition: globals_2.cc:53
N_WSV
#define N_WSV
Definition: auto_wsv.h:19
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: arts.h:153
TokValTypeName
String TokValTypeName[7]
The name of the type associated with the different tokens.
Definition: token.cc:26
file.h
This file contains basic functions to handle ASCII and binary (HDF) data files.
token.h
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:115
methods.h
Declaration of the class MdRecord.
wsv_aux.h
Auxiliary header stuff related to workspace variable groups.
auto_wsv.h
Declares the enum type that acts as a handle for workspace variables. Also declares the workspace its...
arts.h
The global header file for ARTS.