Bsoft  2.1.0
Bernard's software package
options.h
Go to the documentation of this file.
1 
9 #include "Bstring.h"
10 //#include "rwimg.h"
11 #include "Vector3.h"
12 #include "View.h"
13 #include "Euler.h"
14 #include "symmetry.h"
15 #include "UnitCell.h"
16 
17 // Declaration of global variables
18 extern int verbose; // Level of output to the screen
19 
26 class Boption {
27 public:
31 private:
32  int err;
33 public:
34  Boption(char* arg, char* arg1, const char* use[]) : next(NULL), err(0) {
35 // cout << arg << tab << arg1 << endl;
36  Bstring usetag;
37  for ( int n=0; use[n] != NULL; n++ ) {
38  usetag = use[n];
39 // cout << usetag << endl;
40  if ( usetag[0] == '-' ) {
41  if ( usetag.find(arg, 0) == 0 ) {
42  if ( tag.length() ) {
43  cerr << "Error: Ambiguous option " << arg << endl;
44  err++;
45  } else {
46  tag = usetag.pre(' ').post('-');
47  if ( usetag.post(' ').pre(' ').length() ) value = arg1;
48  if ( verbose & VERB_DEBUG )
49  cout << "DEBUG Boption: usetag=" << tag << " value=" << value << endl;
50  }
51  }
52  }
53  }
54  if ( tag.length() < 1 ) {
55  cerr << "Error: Option " << arg << " is not defined!" << endl;
56  err++;
57  }
58  }
59 /* int verbosity(char* optarg) {
60  Bstring arg(optarg);
61  return verbosity(arg);
62  }
63  int verbosity(Bstring& optarg) {
64  verbose = optarg.integer();
65  Bstring arg = optarg.lower();
66  if ( arg.contains("non") ) verbose |= VERB_NONE;
67  if ( arg.contains("res") ) verbose |= VERB_RESULT;
68  if ( arg.contains("lab") ) verbose |= VERB_LABEL;
69  if ( arg.contains("pro") ) verbose |= VERB_PROCESS;
70  if ( arg.contains("sta") ) verbose |= VERB_STATS;
71  if ( arg.contains("ful") ) verbose |= VERB_FULL;
72  if ( arg.contains("tim") ) verbose |= VERB_TIME;
73  if ( arg.contains("mem") ) verbose |= VERB_MEMORY;
74  if ( arg.contains("deb") ) verbose |= VERB_DEBUG;
75  if ( arg.contains("star") ) verbose |= VERB_DEBUG_STAR;
76  if ( arg.contains("dm") ) verbose |= VERB_DEBUG_DM;
77  return verbose;
78  }*/
79  int errors() {
80  int n(0);
81  Boption* opt;
82  for ( opt = this; opt; opt = opt->next ) n += opt->err;
83  return n;
84  }
86  if ( value.length() < 1 ) {
87  cerr << "-" << tag << ": A valid file name must be specified!" << endl;
88  err++;
89  }
90  if ( value.contains(",") ) return value.pre(',');
91  else return value;
92  }
94  if ( value.length() < 1 ) {
95  cerr << "-datatype: A data type must be specified!" << endl;
96  err++;
97  }
98  return getdatatype(value[0]);
99  }
100  long integer() {
101  if ( value.length() < 1 ) {
102  cerr << "-" << tag << ": An integer must be specified!" << endl;
103  err++;
104  }
105  return value.integer();
106  }
107  double real() {
108  if ( value.length() < 1 ) {
109  cerr << "-" << tag << ": A real value must be specified!" << endl;
110  err++;
111  }
112  return value.real();
113  }
114  template <typename T1, typename T2>
115  long values(T1& v1, T2& v2) {
116  vector<double> d = value.split_into_doubles(",");
117  if ( d.size() > 0 ) v1 = (T1)d[0];
118  if ( d.size() > 1 ) v2 = (T2)d[1];
119  return d.size();
120  }
121  template <typename T1, typename T2, typename T3>
122  long values(T1& v1, T2& v2, T3& v3) {
123  vector<double> d = value.split_into_doubles(",");
124  if ( d.size() > 0 ) v1 = (T1)d[0];
125  if ( d.size() > 1 ) v2 = (T2)d[1];
126  if ( d.size() > 2 ) v3 = (T3)d[2];
127  return d.size();
128  }
129  template <typename T1, typename T2, typename T3, typename T4>
130  long values(T1& v1, T2& v2, T3& v3, T4& v4) {
131  vector<double> d = value.split_into_doubles(",");
132  if ( d.size() > 0 ) v1 = (T1)d[0];
133  if ( d.size() > 1 ) v2 = (T2)d[1];
134  if ( d.size() > 2 ) v3 = (T3)d[2];
135  if ( d.size() > 3 ) v4 = (T4)d[3];
136  return d.size();
137  }
138  double fill(int& fill_type) {
139  double f = value.real();
140  fill_type = FILL_USER;
141  Bstring arg = value.lower();
142  if ( arg[0] == 'a' ) fill_type = FILL_AVERAGE;
143  if ( arg[0] == 'b' ) fill_type = FILL_BACKGROUND;
144  if ( arg.contains("mi") ) fill_type = FILL_MIN;
145  if ( arg.contains("ma") ) fill_type = FILL_MAX;
146  return f;
147  }
149  Vector3<double> vec;
150  vector<double> d = value.split_into_doubles(",");
151  if ( d.size() < 2 ) {
152  cerr << "-" << tag << ": At least 2 values for the vector must be specified!" << endl;
153  err++;
154  }
155  for ( unsigned long i=0; i<d.size(); i++ ) vec[i] = d[i];
156  return vec;
157  }
159  Vector3<double> vec = vector3();
160  vec.normalize();
161  return vec;
162  }
164  Vector3<long> v(1,1,1);
165  vector<double> d = value.split_into_doubles(",");
166  if ( d.size() < 1 ) {
167  cerr << "-" << tag << ": At least one size value must be specified!" << endl;
168  err++;
169  }
170  for ( unsigned long i=0; i<d.size(); i++ ) v[i] = (long) (d[i] + 0.5);
171  if ( d.size() < 2 ) v[2] = v[1] = v[0];
172  else if ( d.size() < 3 ) v[2] = 1;
173  return v;
174  }
176  Vector3<long> v = size();
177  v[2] = 1;
178  return v;
179  }
181  Vector3<double> ori;
182  vector<double> d = value.split_into_doubles(",");
183  if ( d.size() < 1 ) {
184  cerr << "-" << tag << ": At least one origin value must be specified!" << endl;
185  err++;
186  }
187  for ( unsigned long i=0; i<d.size(); i++ ) ori[i] = d[i];
188  return ori;
189  }
191  Vector3<double> sc(1,1,1);
192  vector<double> d = value.split_into_doubles(",");
193  if ( d.size() < 1 ) {
194  cerr << "-" << tag << ": At least one scale value must be specified!" << endl;
195  err++;
196  }
197  for ( unsigned long i=0; i<d.size() && i<3; i++ ) sc[i] = d[i];
198  if ( d.size() < 2 ) sc[2] = sc[1] = sc[0];
199  return sc;
200  }
201  template <typename T1, typename T2>
202  long box(Vector3<T1>& st, Vector3<T2>& sz) {
203  long i, n(0);
204  vector<double> d = value.split_into_doubles(",");
205  n = d.size();
206  if ( n < 4 ) {
207  cerr << "-" << tag << ": At least one size value must be specified!" << endl;
208  err++;
209  }
210  for ( i=0; i<n && i<3; i++ ) st[i] = (T1) d[i];
211  for ( i=3; i<n && i<6; i++ ) sz[i-3] = (T2) d[i];
212  if ( n < 4 ) sz[2] = sz[1] = sz[0];
213  else if ( n < 5 ) sz[2] = 1;
214  if ( sz.volume() < 1 ) sz = sz.max(1);
215  return n;
216  }
217  template <typename T1, typename T2>
218  long box(vector<T1>& st, vector<T2>& sz) {
219  long i, n(0);
220  vector<double> d = value.split_into_doubles(",");
221  n = d.size();
222  if ( n < 4 ) {
223  cerr << "-" << tag << ": At least one size value must be specified!" << endl;
224  err++;
225  }
226  for ( i=0; i<n && i<3; i++ ) st[i] = (T1) d[i];
227  for ( i=3; i<n && i<6; i++ ) sz[i-3] = (T2) d[i];
228  if ( n < 4 ) sz[2] = sz[1] = sz[0];
229  else if ( n < 5 ) sz[2] = 1;
230  if ( volume(sz) < 1 ) sz = vecmax(sz, 1);
231  return n;
232  }
234  if ( value.length() < 1 ) {
235  cerr << "-symmetry: A symmetry must be specified!" << endl;
236  err++;
237  }
239  }
241  if ( value.length() < 1 ) {
242  cerr << "-symmetry: A symmetry must be specified!" << endl;
243  err++;
244  }
246  }
247  View view() {
248  View v;
249  vector<double> d = value.split_into_doubles(",");
250  if ( d.size() < 3 ) {
251  cerr << "-View: At least 3 values for the vector must be specified!" << endl;
252  err++;
253  }
254  for ( unsigned long i=0; i<d.size(); i++ ) v[i] = d[i];
255  v[3] *= M_PI/180;
256  v.normalize();
257  return v;
258  }
260  Euler e;
261  vector<double> d = value.split_into_doubles(",");
262  if ( d.size() < 3 ) {
263  cerr << "-Euler: All three angles must be specified!" << endl;
264  err++;
265  }
266  for ( long i=0; i<3; i++ ) e[i] = d[i]*M_PI/180.0;
267  return e;
268  }
269  double mass() {
270  double m = value.real();
271  if ( value.contains("k") || value.contains("K") )
272  m *= 1e3;
273  else if ( value.contains("m") || value.contains("M") )
274  m *= 1e6;
275  else if ( value.contains("g") || value.contains("G") )
276  m *= 1e9;
277  return m;
278  }
280  UnitCell uc(0,0,0,M_PI_2,M_PI_2,M_PI_2);
281  vector<double> d = value.split_into_doubles(",");
282  if ( d.size() < 6 ) {
283  cerr << "-unitcell: All 6 values for the unit cell must be specified!" << endl;
284  err++;
285  }
286  for ( unsigned long i=0; i<d.size(); i++ ) uc[i] = d[i];
287  uc.degrees_to_radians();
288  uc.set_angle_range();
289  return uc;
290  }
291  int ctf_action() {
292  int action(0);
293  if ( value.length() < 1 )
294  cerr << "-action: An action must be specified!" << endl;
295  else {
296  action = value.integer();
297  value = value.lower();
298  if ( value.contains("fli") ) action = 1;
299  if ( value.contains("app") ) action = 2;
300  if ( value.contains("cor") ) action = 3;
301  if ( value.contains("wie") ) action = 4;
302  if ( value.contains("bas") ) action = 5;
303  if ( value.contains("baseline2") ) action = 6;
304  if ( value.contains("basef") ) action = 7;
305  if ( value.contains("basec") ) action = 8;
306  if ( value.contains("env") ) action = 9;
307  if ( value.contains("app") &&
308  value.contains("env") ) action = 10;
309  if ( value.contains("prep") ) action = 11;
310  if ( value.contains("fit") ) action = 12;
311  if ( value.contains("prep") &&
312  value.contains("fit") ) action = 13;
313  }
314  return action;
315  }
316 } ;
317 
318 // Function prototypes
319 Boption* get_option_list(const char* use[], int argc, char* argv[], int& optind);
320 int option_kill(Boption* option);
321 int get_option_verbose(char* optarg);
322 int get_option_verbose(Bstring& optarg);
323 double get_option_mass(Bstring& optarg);
324 
Boption
A class for a command line input option.
Definition: options.h:26
get_option_mass
double get_option_mass(Bstring &optarg)
Returns the adjusted mass based on the added character.
Definition: options.cpp:177
Boption::unit_cell
UnitCell unit_cell()
Definition: options.h:279
Boption::scale
Vector3< double > scale()
Definition: options.h:190
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstring
Definition: Bstring.h:29
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Boption::datatype
DataType datatype()
Definition: options.h:93
symmetry.h
Header file for general symmetry functions.
vecmax
vector< T1 > vecmax(vector< T1 > v1, vector< T2 > v2)
Definition: Vector3.h:591
Boption::next
Boption * next
Definition: options.h:28
Vector3< double >
use
const char * use[]
Definition: b3v.cpp:19
Boption::size2
Vector3< long > size2()
Definition: options.h:175
getdatatype
DataType getdatatype(char letter)
Get the data type indicated by a single letter code.
Definition: utilities.cpp:295
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
get_option_verbose
int get_option_verbose(char *optarg)
Sets the verbose option.
Definition: options.cpp:145
FILL_USER
#define FILL_USER
Definition: utilities.h:176
UnitCell.h
Header file for unit cell functions.
Bstring::real
double real()
Definition: Bstring.cpp:553
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:177
Boption::values
long values(T1 &v1, T2 &v2, T3 &v3)
Definition: options.h:122
View
Definition: View.h:26
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
Boption::origin
Vector3< double > origin()
Definition: options.h:180
Boption::view
View view()
Definition: options.h:247
Bsymmetry
Definition: symmetry.h:38
M_PI_2
#define M_PI_2
Definition: utilities.h:66
verbose
int verbose
Boption::symmetry
Bsymmetry * symmetry()
Definition: options.h:240
Boption::errors
int errors()
Definition: options.h:79
Boption::tag
Bstring tag
Definition: options.h:29
Boption::values
long values(T1 &v1, T2 &v2)
Definition: options.h:115
Vector3::normalize
double normalize()
Definition: Vector3.h:238
Boption::real
double real()
Definition: options.h:107
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
Bstring::find
long find(const Bstring &s)
Definition: Bstring.h:104
Boption::ctf_action
int ctf_action()
Definition: options.h:291
Boption::value
Bstring value
Definition: options.h:30
Boption::box
long box(Vector3< T1 > &st, Vector3< T2 > &sz)
Definition: options.h:202
volume
T volume(vector< T > v)
Definition: Vector3.h:547
Vector3.h
Class for 3-value vectors.
Bstring::split_into_doubles
vector< double > split_into_doubles(const Bstring &delim)
Definition: Bstring.cpp:673
View::normalize
double normalize()
Definition: View.h:109
Boption::symmetry_string
Bstring symmetry_string()
Definition: options.h:233
Bstring.h
Header file for the string class.
Boption::mass
double mass()
Definition: options.h:269
M_PI
#define M_PI
Definition: utilities.h:63
Boption::size
Vector3< long > size()
Definition: options.h:163
Boption::vector3
Vector3< double > vector3()
Definition: options.h:148
Boption::fill
double fill(int &fill_type)
Definition: options.h:138
Boption::normalized_vector3
Vector3< double > normalized_vector3()
Definition: options.h:158
Bstring::length
long length() const
Definition: Bstring.cpp:263
init_point_group_symmetry
Bsymmetry * init_point_group_symmetry(Bstring symmetry_string)
Sets up a symmetry structure from a string identifier.
Definition: symmetry.cpp:39
Vector3::volume
double volume()
Definition: Vector3.h:237
View.h
View object.
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Boption::values
long values(T1 &v1, T2 &v2, T3 &v3, T4 &v4)
Definition: options.h:130
Boption::box
long box(vector< T1 > &st, vector< T2 > &sz)
Definition: options.h:218
Boption::Boption
Boption(char *arg, char *arg1, const char *use[])
Definition: options.h:34
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:178
UnitCell
Definition: UnitCell.h:22
Boption::euler
Euler euler()
Definition: options.h:259
FILL_MIN
#define FILL_MIN
Definition: utilities.h:179
DataType
DataType
Base data type specifier.
Definition: utilities.h:150
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
UnitCell::set_angle_range
void set_angle_range()
Definition: UnitCell.h:48
Euler.h
Euler object.
symmetry_clean_pointgroup
Bstring symmetry_clean_pointgroup(Bstring &symmetry_string)
Corrects an existing point group string.
Definition: symmetry.cpp:226
Euler
Definition: Euler.h:28
Bstring::lower
Bstring lower()
Definition: Bstring.cpp:316
UnitCell::degrees_to_radians
void degrees_to_radians()
Definition: UnitCell.h:54
FILL_MAX
#define FILL_MAX
Definition: utilities.h:180
Boption::integer
long integer()
Definition: options.h:100
Vector3::max
Type max()
Definition: Vector3.h:175