Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditHydrophone.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 
6 
7 #include "JSupport/JMeta.hh"
8 
9 #include "Jeep/JContainer.hh"
10 #include "Jeep/JPrint.hh"
11 #include "Jeep/JParser.hh"
12 #include "Jeep/JMessage.hh"
13 
14 
15 namespace {
16 
17  using namespace JPP;
18 
19  static const std::string set_t = "set"; //!< Set position
20  static const std::string add_t = "add"; //!< Add position
21  static const std::string sub_t = "sub"; //!< Subtract position
22  static const std::string rot_t = "rot"; //!< Rotate around z-axis by given value [rad]
23 
24  /**
25  * Auxiliary class to apply hydrophone modifications.
26  *
27  * Note that the internal identifier may apply to a module as well as a string.
28  */
29  class JModifier {
30  public:
31  /**
32  * Default constructor.
33  */
34  JModifier()
35  {}
36 
37 
38  /**
39  * Apply modification to given hydrophone.
40  *
41  * \param hydrophone hydrophone
42  * \return true if valid action; else false
43  */
44  bool apply(JHydrophone& hydrophone) const
45  {
46  switch (data.size()) {
47 
48  case 1:
49  return apply(hydrophone, action, data[0]); // orientation calibration
50 
51  case 3:
52  return apply(hydrophone, action, JVector3D(data[0], data[1], data[2])); // 3D position calibration
53 
54  default:
55  return false;
56  }
57  }
58 
59 
60  /**
61  * Read modifier from input.
62  *
63  * \param in input stream
64  * \param modifier modifier
65  * \return input stream
66  */
67  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
68  {
69  using namespace std;
70 
71  if (in >> modifier.id >> modifier.action) {
72 
73  modifier.data.clear();
74 
75  for (double x; in >> x; ) {
76  modifier.data.push_back(x);
77  }
78 
79  in.clear(ios_base::eofbit);
80  }
81 
82  return in;
83  }
84 
85 
86  /**
87  * Write modifier to output.
88  *
89  * \param out output stream
90  * \param modifier modifier
91  * \return output stream
92  */
93  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
94  {
95  out << modifier.id;
96  out << ' ';
97  out << modifier.action;
98 
99  for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
100  out << ' ' << *i;
101  }
102 
103  return out;
104  }
105 
106 
107  int id;
108  std::string action;
109  std::vector<double> data;
110 
111 
112  private:
113  /**
114  * Apply orientation calibration to given hydrophone.
115  *
116  * \param hydrophone hydrophone
117  * \param action action
118  * \param value value
119  * \return true if valid action; else false
120  */
121  static bool apply(JHydrophone& hydrophone, const std::string& action, const double value)
122  {
123  if (action == rot_t)
124  hydrophone.rotate(JRotation3Z(value));
125  else
126  return false;
127 
128  return true;
129  }
130 
131 
132  /**
133  * Apply position calibration to given hydrophone.
134  *
135  * \param hydrophone hydrophone
136  * \param action action
137  * \param pos pos
138  * \return true if valid action; else false
139  */
140  static bool apply(JHydrophone& hydrophone, const std::string& action, const JVector3D& pos)
141  {
142  if (action == set_t)
143  hydrophone.setPosition(pos);
144  else if (action == add_t)
145  hydrophone.add(pos);
146  else if (action == sub_t)
147  hydrophone.sub(pos);
148  else
149  return false;
150 
151  return true;
152  }
153  };
154 }
155 
156 /**
157  * \file
158  *
159  * Auxiliary program to modify hydrophone configuration.
160  *
161  * Syntax:
162  * <pre>
163  * -S "<string number> (set|add|sub) x y z"
164  * -S "<string number> (rot) phi"
165  * </pre>
166  *
167  * For option <tt>rot</tt>,
168  * the angle <tt>phi</tt> refers to an anti-clockwise rotation around the z-axis.\n
169  * The rotation angle is defined in radians.
170  *
171  * \author mdejong
172  */
173 int main(int argc, char **argv)
174 {
175  using namespace std;
176  using namespace JPP;
177 
178  typedef JContainer< vector<JHydrophone> > hydrophones_container;
179 
180  string hydrophoneFile;
181  vector<JModifier> mod;
182  int debug;
183 
184  try {
185 
186  JParser<> zap("Auxiliary program to modify hydrophone configuration.");
187 
188  zap['f'] = make_field(hydrophoneFile, "hydrophone file");
189  zap['S'] = make_field(mod, "hydrophone modifier") = JPARSER::initialised();
190  zap['d'] = make_field(debug) = 1;
191 
192  zap(argc, argv);
193  }
194  catch(const exception &error) {
195  FATAL(error.what() << endl);
196  }
197 
198  hydrophones_container hydrophones;
199 
200  hydrophones.load(hydrophoneFile.c_str());
201 
202  hydrophones.comment.add(JMeta(argc, argv));
203 
204  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
205 
206  for (hydrophones_container::iterator hydrophone = hydrophones.begin(); hydrophone != hydrophones.end(); ++hydrophone) {
207 
208  if (hydrophone->getString() == i->id) {
209 
210  DEBUG("Modifier" << ' '
211  << "(" << FILL(4,'0') << hydrophone->getString() << "," << FILL(2,'0') << hydrophone->getFloor() << FILL() << ")" << ' '
212  << "action" << ' ' << i->action << JEEPZ() << i->data << endl);
213 
214  if (!i->apply(*hydrophone)) {
215  ERROR("No valid action: " << *i << endl);
216  }
217  }
218  }
219  }
220 
221  hydrophones.store(hydrophoneFile.c_str());
222 }
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Utility class to parse command line options.
Definition: JParser.hh:1500
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:66
Type definition of hydrophone.
Definition: JHydrophone.hh:30
Data structure for hydrophone.
Rotation around Z-axis.
Definition: JRotation3D.hh:85
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition: JVector3D.hh:158
I/O formatting auxiliaries.
Data structure for vector in three dimensions.
Definition: JVector3D.hh:34
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition: JContainer.hh:39
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:63
Auxiliary data structure for streaming of STL containers.
Definition: JPrint.hh:65
General purpose messaging.
Auxiliary data structure for sequence of same character.
Definition: JManip.hh:327
#define FATAL(A)
Definition: JMessage.hh:67
int getString() const
Get string number.
Definition: JLocation.hh:134
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1549
Utility class to parse command line options.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Definition: JPosition3D.hh:186
JVector3D & add(const JVector3D &vector)
Add vector.
Definition: JVector3D.hh:142
Container I/O.
void setPosition(const JVector3D &pos)
Set position.
Definition: JPosition3D.hh:152
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
int main(int argc, char *argv[])
Definition: Main.cpp:15