Jpp  16.0.1
the software that should make you happy
 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  struct JModifier {
28  /**
29  * Apply modification to given hydrophone.
30  *
31  * \param hydrophone hydrophone
32  * \return true if valid action; else false
33  */
34  bool apply(JHydrophone& hydrophone) const
35  {
36  switch (data.size()) {
37 
38  case 1:
39  return apply(hydrophone, action, data[0]); // orientation calibration
40 
41  case 3:
42  return apply(hydrophone, action, JVector3D(data[0], data[1], data[2])); // 3D position calibration
43 
44  default:
45  return false;
46  }
47  }
48 
49 
50  /**
51  * Read modifier from input.
52  *
53  * \param in input stream
54  * \param modifier modifier
55  * \return input stream
56  */
57  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
58  {
59  using namespace std;
60 
61  if (in >> modifier.id >> modifier.action) {
62 
63  modifier.data.clear();
64 
65  for (double x; in >> x; ) {
66  modifier.data.push_back(x);
67  }
68 
69  in.clear(ios_base::eofbit);
70  }
71 
72  return in;
73  }
74 
75 
76  /**
77  * Write modifier to output.
78  *
79  * \param out output stream
80  * \param modifier modifier
81  * \return output stream
82  */
83  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
84  {
85  out << modifier.id;
86  out << ' ';
87  out << modifier.action;
88 
89  for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
90  out << ' ' << *i;
91  }
92 
93  return out;
94  }
95 
96 
97  int id;
98  std::string action;
100 
101 
102  private:
103  /**
104  * Apply orientation calibration to given hydrophone.
105  *
106  * \param hydrophone hydrophone
107  * \param action action
108  * \param value value
109  * \return true if valid action; else false
110  */
111  static bool apply(JHydrophone& hydrophone, const std::string& action, const double value)
112  {
113  if (action == rot_t)
114  hydrophone.rotate(JRotation3Z(value));
115  else
116  return false;
117 
118  return true;
119  }
120 
121 
122  /**
123  * Apply position calibration to given hydrophone.
124  *
125  * \param hydrophone hydrophone
126  * \param action action
127  * \param pos pos
128  * \return true if valid action; else false
129  */
130  static bool apply(JHydrophone& hydrophone, const std::string& action, const JVector3D& pos)
131  {
132  if (action == set_t)
133  hydrophone.setPosition(pos);
134  else if (action == add_t)
135  hydrophone.add(pos);
136  else if (action == sub_t)
137  hydrophone.sub(pos);
138  else
139  return false;
140 
141  return true;
142  }
143  };
144 }
145 
146 /**
147  * \file
148  *
149  * Auxiliary program to modify hydrophone configuration.
150  *
151  * Syntax:
152  * <pre>
153  * -S "<string number> (set|add|sub) x y z"
154  * -S "<string number> (rot) phi"
155  * </pre>
156  *
157  * For option <tt>rot</tt>,
158  * the angle <tt>phi</tt> refers to an anti-clockwise rotation around the z-axis.\n
159  * The rotation angle is defined in radians.
160  *
161  * The options
162  * <pre>
163  * -A "<string number> <floor> x y z"
164  * -D "<string number>"
165  * </pre>
166  * can be used to add and remove a hydrophone, respectively.
167  *
168  * \author mdejong
169  */
170 int main(int argc, char **argv)
171 {
172  using namespace std;
173  using namespace JPP;
174 
175  typedef JContainer< vector<JHydrophone> > container_type;
176 
177  string file_name;
178  vector<JModifier> mod;
180  vector<int> del;
181  bool rm;
182  int debug;
183 
184  try {
185 
186  JParser<> zap("Auxiliary program to modify hydrophone configuration.");
187 
188  zap['f'] = make_field(file_name, "hydrophone file");
189  zap['S'] = make_field(mod, "hydrophone modifier") = JPARSER::initialised();
190  zap['A'] = make_field(add, "add hydrophone") = JPARSER::initialised();
191  zap['D'] = make_field(del, "delete hydrophone") = JPARSER::initialised();
192  zap['r'] = make_field(rm, "remove previous comments");
193  zap['d'] = make_field(debug) = 1;
194 
195  zap(argc, argv);
196  }
197  catch(const exception &error) {
198  FATAL(error.what() << endl);
199  }
200 
201  container_type data;
202 
203  try {
204  data.load(file_name.c_str());
205  }
206  catch(const exception&) {}
207 
208  if (rm) {
209  data.comment.clear();
210  }
211 
212  data.comment.add(JMeta(argc, argv));
213 
214  for (vector<JHydrophone>::const_iterator i = add.begin(); i != add.end(); ++i) {
215  data.push_back(*i);
216  }
217 
218  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
219 
220  for (container_type::iterator target = data.begin(); target != data.end(); ++target) {
221 
222  if (target->getString() == i->id) {
223 
224  DEBUG("Modifier" << ' '
225  << "(" << FILL(4,'0') << target->getString() << "," << FILL(2,'0') << target->getFloor() << FILL() << ")" << ' '
226  << "action" << ' ' << i->action << JEEPZ() << i->data << endl);
227 
228  if (!i->apply(*target)) {
229  ERROR("No valid action: " << *i << endl);
230  }
231  }
232  }
233  }
234 
235  for (vector<int>::const_iterator i = del.begin(); i != del.end(); ++i) {
236  for (container_type::iterator target = data.begin(); target != data.end(); ) {
237  if (target->getString() == *i)
238  target = data.erase(target);
239  else
240  ++target;
241  }
242  }
243 
244  data.store(file_name.c_str());
245 }
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 main(int argc, char *argv[])
Definition: Main.cc:15
Target.
Definition: JHead.hh:296
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:328
#define FATAL(A)
Definition: JMessage.hh:67
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1693
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
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 CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
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