All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
acolm.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2012, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /** \file acolm.h
24  \brief The \ref o2scl_acol::acol_manager class header
25 */
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <iostream>
31 #include <string>
32 #include <vector>
33 #include <fnmatch.h>
34 #include <o2scl/misc.h>
35 #include <o2scl/cli.h>
36 #include <o2scl/fit_nonlin.h>
37 #include <o2scl/table_units.h>
38 #include <o2scl/table3d.h>
39 #include <o2scl/format_float.h>
40 #include <o2scl/hdf_file.h>
41 #include <o2scl/hdf_io.h>
42 #include <o2scl/lib_settings.h>
43 
44 #ifdef O2SCL_READLINE
45 #include <o2scl/cli_readline.h>
46 #else
47 #include <o2scl/cli.h>
48 #endif
49 
50 /// A namespace for objects associated with the command-line utility 'acol'
51 namespace o2scl_acol {
52 
53  /** \brief The driver for 'acol' command-line utility
54  \nothing
55 
56  \comment
57  There was some concern about the confusion b/w the commands
58  "get row by index" (get-row) and "get row by function"
59  (find-row) and "select rows by function" (select-rows),
60  but it might be ok.
61  \endcomment
62 
63  \todo Rework so that the error handler isn't called prematurely.
64  Ensure table::make_cols() and table::calc() don't call the error
65  handler.
66 
67  \todo Ensure add() copies constants and interpolation type,
68  and units.
69 
70  \future Stack-like operations (push, pop, swap, stack-list, etc.)
71 
72  \future Use get_input() in comm_create?
73 
74  \future Add functionality to ensure that three digit exponents
75  are still handled gracefully (do this by creating a new boolean
76  setting which, if true, always makes three spaces for
77  exponents?)
78 
79  \future Fix insert and insert_full so that it automatically
80  renames columns
81 
82  \future Allow "insert" commands to be restrictive, avoiding
83  extrapolation
84 
85  \future Replace ~ with $HOME in filenames (this might be best
86  done inside the \ref o2scl::cli class). (Some progress made.
87  Function cli::expand_tilde() is written but not yet
88  implemented.)
89 
90  \future New 3d commands: extract, transpose, contours, find_grid_x,
91  find_grid_y, create-3d, etc.
92 
93  \hline
94  */
95  class acol_manager {
96 
97 #ifndef DOXYGEN_INTERNAL
98 
99  protected:
100 
101 #ifdef DOXYGEN
102  /// A pointer to the table
103  table_units<> *tabp;
104 #else
106 #endif
107 
108 #ifdef DOXYGEN
109  /// The number formatter for html output
110  format_float ffl;
111 #else
113 #endif
114 
115 #ifdef DOXYGEN
116  /// Pointer to the three dimensional table
117  table3d *t3p;
118 #else
120 #endif
121 
122  /// Convert units object (initialized by constructor to global object)
124 
125  /// \name Parameters modifiable by the user
126  //@{
127  /// The output precision (default 6)
128  int prec;
129 
130  /// The verbosity level (default 1)
131  int verbose;
132 
133  /// True if we should make the output into neat columns (default true)
134  bool pretty;
135 
136  /// True if we should output column names
137  bool names_out;
138 
139  /// The name of the table
140  std::string table_name;
141 
142  /// Filename for units command
143  std::string unit_fname;
144 
145  /// Default arguments from environment
146  std::string def_args;
147 
148  /// The number of columns requested by the user
150 
151  /// True for scientific output mode
153  //@}
154 
155  /// \name The parameter objects
156  //@{
157  o2scl::cli::parameter_string p_table_name;
158  o2scl::cli::parameter_string p_unit_fname;
159  o2scl::cli::parameter_string p_def_args;
160  o2scl::cli::parameter_int p_verbose;
163  o2scl::cli::parameter_bool p_scientific;
165  o2scl::cli::parameter_bool p_names_out;
166  //@}
167 
168  /// Number of columns in screen
169  int ncols;
170 
171  /// If true, the \table is 3d
172  bool threed;
173 
174  /// Dummy cli object for cli::cli_gets()
175 #ifdef DOXYGEN
176  cli *cl;
177 #else
178  o2scl::cli *cl;
179 #endif
180 
181 #endif
182 
183  public:
184 
185  acol_manager();
186 
187  virtual ~acol_manager() {}
188 
189  /** \brief True if we should run interactive mode after parsing
190  the command-line
191  */
193 
194  /// The environment variable to read from
195  std::string env_var_name;
196 
197  /// String parameters
198  std::map<std::string,std::string *> str_params;
199 
200  /// Integer parameters
201  std::map<std::string,int *> int_params;
202 
203  protected:
204 
205  /// Ensure \c col is unique from entries in \c cnames
206  int make_unique_name(std::string &col, std::vector<std::string> &cnames);
207 
208  public:
209 
210  /** \brief Main run function
211 
212  Process command-line options using cli object, interface with
213  the operating system via getenv(), instantiate and call the
214  acol_manager object.
215  */
216  virtual int run(int argv, char *argc[]);
217 
218  /// Create the cli object (with readline support if available)
219  virtual int setup_cli();
220 
221  /// Add the options to the cli object
222  virtual int setup_options();
223 
224  /// Add the help text to the cli object
225  virtual int setup_help();
226 
227  /// Add the parameters for 'set' to the cli object
228  virtual int setup_parameters();
229 
230  /// Assign a constant
231  virtual int comm_assign(std::vector<std::string> &sv, bool itive_com);
232 
233  /// Compute a scalar value
234  virtual int comm_calc(std::vector<std::string> &sv, bool itive_com);
235 
236  /// Create a table from a column of equally spaced values
237  virtual int comm_create(std::vector<std::string> &sv, bool itive_com);
238 
239  /// Delete a column
240  virtual int comm_delete_col(std::vector<std::string> &sv, bool itive_com);
241 
242  /// Delete rows specified by a function
243  virtual int comm_delete_rows(std::vector<std::string> &sv, bool itive_com);
244 
245  /// Create a column which is the derivative of another
246  virtual int comm_deriv(std::vector<std::string> &sv, bool itive_com);
247 
248  /// Create a column which is the second derivative of another
249  virtual int comm_deriv2(std::vector<std::string> &sv, bool itive_com);
250 
251  /// Parameters for iterate_func()
252  typedef struct {
253  std::string tname;
255  bool found;
256  std::string type;
257  int verbose;
258  } iter_parms;
259 
260  /// HDF object iteration function
261  static herr_t iterate_func(hid_t loc, const char *name,
262  const H5L_info_t *inf, void *op_data);
263 
264  /// HDF object iteration function
265  static herr_t filelist_func(hid_t loc, const char *name,
266  const H5L_info_t *inf, void *op_data);
267 
268  /// Read a file
269  virtual int comm_read(std::vector<std::string> &sv, bool itive_com);
270 
271  /// Read a file and list the O2scl objects
272  virtual int comm_filelist(std::vector<std::string> &sv, bool itive_com);
273 
274  /// Find a row from a function
275  virtual int comm_find_row(std::vector<std::string> &sv, bool itive_com);
276 
277  /// Create a column from a function
278  virtual int comm_function(std::vector<std::string> &sv, bool itive_com);
279 
280  /// Read a generic data file
281  virtual int comm_generic(std::vector<std::string> &sv, bool itive_com);
282 
283  /// Print out an entire row
284  virtual int comm_get_row(std::vector<std::string> &sv, bool itive_com);
285 
286  /// Fit two columns to a function
287  virtual int comm_fit(std::vector<std::string> &sv, bool itive_com);
288 
289  /// Create an html file
290  virtual int comm_html(std::vector<std::string> &sv, bool itive_com);
291 
292  /// Insert a column from an external table using interpolation
293  virtual int comm_insert(std::vector<std::string> &sv, bool itive_com);
294 
295  /// Insert an external table using interpolation
296  virtual int comm_insert_full(std::vector<std::string> &sv, bool itive_com);
297 
298  /// Create a column which is the integral of another
299  virtual int comm_integ(std::vector<std::string> &sv, bool itive_com);
300 
301  /// Toggle interactive mode
302  virtual int comm_interactive(std::vector<std::string> &sv, bool itive_com);
303 
304  /// Output to a file in internal format
305  virtual int comm_internal(std::vector<std::string> &sv, bool itive_com);
306 
307  /// Create an html file
308  virtual int comm_interp(std::vector<std::string> &sv, bool itive_com);
309 
310  /// List columns in table 'tp' named 'tname' assuming screen size 'ncol'
311  virtual int comm_list(std::vector<std::string> &sv, bool itive_com);
312 
313  /// Compute the maximum value of a colum
314  virtual int comm_max(std::vector<std::string> &sv, bool itive_com);
315 
316  /// Compute the minimum value of a colum
317  virtual int comm_min(std::vector<std::string> &sv, bool itive_com);
318 
319  /// Add a column for line numbers
320  virtual int comm_index(std::vector<std::string> &sv, bool itive_com);
321 
322  /// Output to screen or file
323  virtual int comm_output(std::vector<std::string> &sv, bool itive_com);
324 
325  /// Get or set the current interpolation type
326  virtual int comm_interp_type(std::vector<std::string> &sv, bool itive_com);
327 
328  /// Preview the table
329  virtual int comm_preview(std::vector<std::string> &sv, bool itive_com);
330 
331  /// Add two table3d objects
332  virtual int comm_add(std::vector<std::string> &sv, bool itive_com);
333 
334  /// Rename a column
335  virtual int comm_rename(std::vector<std::string> &sv, bool itive_com);
336 
337  /// Select several columns for a new table
338  virtual int comm_select(std::vector<std::string> &sv, bool itive_com);
339 
340  /// Select several rows for a new table
341  virtual int comm_select_rows(std::vector<std::string> &sv, bool itive_com);
342 
343  /// Post-processing for setting a value
344  virtual int comm_set(std::vector<std::string> &sv, bool itive_com);
345 
346  /// Set an individual data point at a specified row and column
347  virtual int comm_set_data(std::vector<std::string> &sv, bool itive_com);
348 
349  /// Set units of a column
350  virtual int comm_set_unit(std::vector<std::string> &sv, bool itive_com);
351 
352  /// Set units of a column
353  virtual int comm_show_units(std::vector<std::string> &sv, bool itive_com);
354 
355  /// Get units of a column
356  virtual int comm_get_unit(std::vector<std::string> &sv, bool itive_com);
357 
358  /// Convert units of a column
359  virtual int comm_convert_unit(std::vector<std::string> &sv,
360  bool itive_com);
361 
362  /// Sort the table by a column
363  virtual int comm_sort(std::vector<std::string> &sv, bool itive_com);
364 
365  /// Get column stats
366  virtual int comm_stats(std::vector<std::string> &sv, bool itive_com);
367 
368  /// Print version
369  virtual int comm_version(std::vector<std::string> &sv, bool itive_com);
370 
371  /// Get a conversion factor
372  virtual int comm_get_conv(std::vector<std::string> &sv, bool itive_com);
373 
374  /// Set screen witdth
375  int set_swidth(int ncol) {
376  ncols=ncol;
377  return 0;
378  }
379 
380  protected:
381 
382  /** \brief Separate string into words delimited by whitespace
383  (for comm_fit())
384  */
385  int separate(std::string str, std::vector<std::string> &sv);
386 
387  /// An internal command for prompting the user for command arguments
388  int get_input(std::vector<std::string> &sv,
389  std::vector<std::string> &directions,
390  std::vector<std::string> &in, std::string comm_name,
391  bool itive_com);
392 
393  /// An internal command for prompting the user for one command argument
394  int get_input_one(std::vector<std::string> &sv, std::string directions,
395  std::string &in, std::string comm_name,
396  bool itive_com);
397  };
398 
399 }
virtual int comm_function(std::vector< std::string > &sv, bool itive_com)
Create a column from a function.
virtual int comm_sort(std::vector< std::string > &sv, bool itive_com)
Sort the table by a column.
Parameters for iterate_func()
Definition: acolm.h:252
virtual int comm_internal(std::vector< std::string > &sv, bool itive_com)
Output to a file in internal format.
virtual int comm_integ(std::vector< std::string > &sv, bool itive_com)
Create a column which is the integral of another.
std::string table_name
The name of the table.
Definition: acolm.h:140
The driver for 'acol' command-line utility.
Definition: acolm.h:95
table_units * tabp
A pointer to the table.
Definition: acolm.h:103
virtual int comm_delete_col(std::vector< std::string > &sv, bool itive_com)
Delete a column.
String parameter for o2scl::cli.
Definition: cli.h:257
virtual int comm_preview(std::vector< std::string > &sv, bool itive_com)
Preview the table.
virtual int comm_get_conv(std::vector< std::string > &sv, bool itive_com)
Get a conversion factor.
std::map< std::string, std::string * > str_params
String parameters.
Definition: acolm.h:198
virtual int comm_calc(std::vector< std::string > &sv, bool itive_com)
Compute a scalar value.
virtual int comm_index(std::vector< std::string > &sv, bool itive_com)
Add a column for line numbers.
Convert units.
Definition: convert_units.h:88
virtual int comm_stats(std::vector< std::string > &sv, bool itive_com)
Get column stats.
virtual int comm_insert_full(std::vector< std::string > &sv, bool itive_com)
Insert an external table using interpolation.
int user_ncols
The number of columns requested by the user.
Definition: acolm.h:149
virtual int setup_options()
Add the options to the cli object.
virtual int comm_filelist(std::vector< std::string > &sv, bool itive_com)
Read a file and list the O2scl objects.
format_float ffl
The number formatter for html output.
Definition: acolm.h:110
int ncols
Number of columns in screen.
Definition: acolm.h:169
virtual int comm_find_row(std::vector< std::string > &sv, bool itive_com)
Find a row from a function.
virtual int comm_version(std::vector< std::string > &sv, bool itive_com)
Print version.
virtual int comm_interp(std::vector< std::string > &sv, bool itive_com)
Create an html file.
int prec
The output precision (default 6)
Definition: acolm.h:128
static herr_t filelist_func(hid_t loc, const char *name, const H5L_info_t *inf, void *op_data)
HDF object iteration function.
virtual int comm_interactive(std::vector< std::string > &sv, bool itive_com)
Toggle interactive mode.
virtual int comm_set(std::vector< std::string > &sv, bool itive_com)
Post-processing for setting a value.
Configurable command-line interface.
Definition: cli.h:234
cli * cl
Dummy cli object for cli::cli_gets()
Definition: acolm.h:176
bool post_interactive
True if we should run interactive mode after parsing the command-line.
Definition: acolm.h:192
std::string env_var_name
The environment variable to read from.
Definition: acolm.h:195
virtual int comm_insert(std::vector< std::string > &sv, bool itive_com)
Insert a column from an external table using interpolation.
int get_input_one(std::vector< std::string > &sv, std::string directions, std::string &in, std::string comm_name, bool itive_com)
An internal command for prompting the user for one command argument.
virtual int comm_select(std::vector< std::string > &sv, bool itive_com)
Select several columns for a new table.
static herr_t iterate_func(hid_t loc, const char *name, const H5L_info_t *inf, void *op_data)
HDF object iteration function.
virtual int comm_read(std::vector< std::string > &sv, bool itive_com)
Read a file.
virtual int comm_min(std::vector< std::string > &sv, bool itive_com)
Compute the minimum value of a colum.
std::map< std::string, int * > int_params
Integer parameters.
Definition: acolm.h:201
virtual int comm_select_rows(std::vector< std::string > &sv, bool itive_com)
Select several rows for a new table.
int set_swidth(int ncol)
Set screen witdth.
Definition: acolm.h:375
bool names_out
True if we should output column names.
Definition: acolm.h:137
virtual int comm_get_row(std::vector< std::string > &sv, bool itive_com)
Print out an entire row.
table3d * t3p
Pointer to the three dimensional table.
Definition: acolm.h:117
int verbose
The verbosity level (default 1)
Definition: acolm.h:131
bool threed
If true, the table table is 3d.
Definition: acolm.h:172
Format a floating point number into a Latex or HTML string.
Definition: format_float.h:113
int separate(std::string str, std::vector< std::string > &sv)
Separate string into words delimited by whitespace (for comm_fit())
String parameter for o2scl::cli.
Definition: cli.h:280
virtual int comm_show_units(std::vector< std::string > &sv, bool itive_com)
Set units of a column.
virtual int comm_deriv(std::vector< std::string > &sv, bool itive_com)
Create a column which is the derivative of another.
bool scientific
True for scientific output mode.
Definition: acolm.h:152
int get_input(std::vector< std::string > &sv, std::vector< std::string > &directions, std::vector< std::string > &in, std::string comm_name, bool itive_com)
An internal command for prompting the user for command arguments.
o2scl::convert_units & cng
Convert units object (initialized by constructor to global object)
Definition: acolm.h:123
virtual int comm_list(std::vector< std::string > &sv, bool itive_com)
List columns in table 'tp' named 'tname' assuming screen size 'ncol'.
virtual int setup_help()
Add the help text to the cli object.
virtual int comm_generic(std::vector< std::string > &sv, bool itive_com)
Read a generic data file.
Integer parameter for o2scl::cli.
Definition: cli.h:326
virtual int comm_output(std::vector< std::string > &sv, bool itive_com)
Output to screen or file.
virtual int run(int argv, char *argc[])
Main run function.
virtual int comm_deriv2(std::vector< std::string > &sv, bool itive_com)
Create a column which is the second derivative of another.
Data table table class with units.
Definition: table_units.h:37
virtual int setup_cli()
Create the cli object (with readline support if available)
virtual int comm_add(std::vector< std::string > &sv, bool itive_com)
Add two table3d objects.
virtual int comm_html(std::vector< std::string > &sv, bool itive_com)
Create an html file.
virtual int comm_interp_type(std::vector< std::string > &sv, bool itive_com)
Get or set the current interpolation type.
A data structure containing many slices of two-dimensional data points defined on a grid...
Definition: table3d.h:76
int make_unique_name(std::string &col, std::vector< std::string > &cnames)
Ensure col is unique from entries in cnames.
std::string def_args
Default arguments from environment.
Definition: acolm.h:146
virtual int comm_assign(std::vector< std::string > &sv, bool itive_com)
Assign a constant.
virtual int comm_convert_unit(std::vector< std::string > &sv, bool itive_com)
Convert units of a column.
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl...
Definition: hdf_file.h:93
virtual int comm_fit(std::vector< std::string > &sv, bool itive_com)
Fit two columns to a function.
virtual int comm_rename(std::vector< std::string > &sv, bool itive_com)
Rename a column.
std::string unit_fname
Filename for units command.
Definition: acolm.h:143
virtual int setup_parameters()
Add the parameters for 'set' to the cli object.
bool pretty
True if we should make the output into neat columns (default true)
Definition: acolm.h:134
virtual int comm_max(std::vector< std::string > &sv, bool itive_com)
Compute the maximum value of a colum.
virtual int comm_get_unit(std::vector< std::string > &sv, bool itive_com)
Get units of a column.
virtual int comm_delete_rows(std::vector< std::string > &sv, bool itive_com)
Delete rows specified by a function.
virtual int comm_create(std::vector< std::string > &sv, bool itive_com)
Create a table from a column of equally spaced values.
virtual int comm_set_data(std::vector< std::string > &sv, bool itive_com)
Set an individual data point at a specified row and column.
virtual int comm_set_unit(std::vector< std::string > &sv, bool itive_com)
Set units of a column.

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..