Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JEditHydrophone.cc
Go to the documentation of this file.
1#include <iostream>
2#include <iomanip>
3#include <vector>
4#include <set>
5
7
8#include "JSupport/JMeta.hh"
9
10#include "Jeep/JContainer.hh"
11#include "Jeep/JPrint.hh"
12#include "Jeep/JParser.hh"
13#include "Jeep/JMessage.hh"
14
15
16namespace {
17
18 using namespace JPP;
19
20 static const std::string set_t = "set"; //!< Set position
21 static const std::string add_t = "add"; //!< Add position
22 static const std::string sub_t = "sub"; //!< Subtract position
23 static const std::string rot_t = "rot"; //!< Rotate around z-axis by given value [rad]
24
25 /**
26 * Auxiliary class to apply hydrophone modifications.
27 */
28 struct JModifier {
29 /**
30 * Apply modification to given hydrophone.
31 *
32 * \param hydrophone hydrophone
33 * \return true if valid action; else false
34 */
35 bool apply(JHydrophone& hydrophone) const
36 {
37 switch (data.size()) {
38
39 case 1:
40 return apply(hydrophone, action, data[0]); // orientation calibration
41
42 case 3:
43 return apply(hydrophone, action, JVector3D(data[0], data[1], data[2])); // 3D position calibration
44
45 default:
46 return false;
47 }
48 }
49
50
51 /**
52 * Read modifier from input.
53 *
54 * \param in input stream
55 * \param modifier modifier
56 * \return input stream
57 */
58 friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
59 {
60 using namespace std;
61
62 if (in >> modifier.id >> modifier.action) {
63
64 modifier.data.clear();
65
66 for (double x; in >> x; ) {
67 modifier.data.push_back(x);
68 }
69
70 in.clear(ios_base::eofbit);
71 }
72
73 return in;
74 }
75
76
77 /**
78 * Write modifier to output.
79 *
80 * \param out output stream
81 * \param modifier modifier
82 * \return output stream
83 */
84 friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
85 {
86 out << modifier.id;
87 out << ' ';
88 out << modifier.action;
89
90 for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
91 out << ' ' << *i;
92 }
93
94 return out;
95 }
96
97
98 int id;
99 std::string action;
101
102
103 private:
104 /**
105 * Apply orientation calibration to given hydrophone.
106 *
107 * \param hydrophone hydrophone
108 * \param action action
109 * \param value value
110 * \return true if valid action; else false
111 */
112 static bool apply(JHydrophone& hydrophone, const std::string& action, const double value)
113 {
114 if (action == rot_t)
115 hydrophone.rotate(JRotation3Z(value));
116 else
117 return false;
118
119 return true;
120 }
121
122
123 /**
124 * Apply position calibration to given hydrophone.
125 *
126 * \param hydrophone hydrophone
127 * \param action action
128 * \param pos pos
129 * \return true if valid action; else false
130 */
131 static bool apply(JHydrophone& hydrophone, const std::string& action, const JVector3D& pos)
132 {
133 if (action == set_t)
134 hydrophone.setPosition(pos);
135 else if (action == add_t)
136 hydrophone.add(pos);
137 else if (action == sub_t)
138 hydrophone.sub(pos);
139 else
140 return false;
141
142 return true;
143 }
144 };
145}
146
147/**
148 * \file
149 *
150 * Auxiliary program to modify hydrophone configuration.
151 *
152 * Syntax:
153 * <pre>
154 * -S "<string number> (set|add|sub) x y z"
155 * -S "<string number> (rot) phi"
156 * </pre>
157 *
158 * For option <tt>rot</tt>,
159 * the angle <tt>phi</tt> refers to an anti-clockwise rotation around the z-axis.\n
160 * The rotation angle is defined in radians.
161 *
162 * The options
163 * <pre>
164 * -A "<string number> <floor> x y z"
165 * -r "<string number>"
166 * </pre>
167 * can be used to add and remove a hydrophone, respectively.
168 *
169 * \author mdejong
170 */
171int main(int argc, char **argv)
172{
173 using namespace std;
174 using namespace JPP;
175
176 typedef JContainer< vector<JHydrophone> > container_type;
177
178 string inputFile;
179 string outputFile;
182 set<int> keep;
183 set<int> rm;
184 bool squash;
185 int debug;
186
187 try {
188
189 JParser<> zap("Auxiliary program to modify hydrophone configuration.");
190
191 zap['f'] = make_field(inputFile, "hydrophone input file") = "";
192 zap['o'] = make_field(outputFile, "hydrophone output file");
193 zap['S'] = make_field(mod, "hydrophone modifier") = JPARSER::initialised();
194 zap['A'] = make_field(add, "add hydrophone") = JPARSER::initialised();
195 zap['k'] = make_field(keep, "keep hydrophone[s]") = JPARSER::initialised();
196 zap['r'] = make_field(rm, "remove hydrophone[s]") = JPARSER::initialised();
197 zap['q'] = make_field(squash, "squash meta data");
198 zap['d'] = make_field(debug, "debug level") = 2;
199
200 zap(argc, argv);
201 }
202 catch(const exception &error) {
203 FATAL(error.what() << endl);
204 }
205
206 const int ns = ((keep.empty() ? 0 : 1) +
207 (rm .empty() ? 0 : 1));
208
209 if (ns > 1) {
210 FATAL("Use either option -k or -r." << endl);
211 }
212
213 container_type data;
214
215 if (inputFile != "") {
216 try {
217 data.load(inputFile.c_str());
218 }
219 catch(const exception& error) {
220 //FATAL(error.what() << endl);
221 }
222 }
223
224 if (squash) {
225 data.comment.clear();
226 }
227
228 data.comment.add(JMeta(argc, argv));
229
230 for (vector<JHydrophone>::const_iterator i = add.begin(); i != add.end(); ++i) {
231 data.push_back(*i);
232 }
233
234 for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
235
236 for (container_type::iterator target = data.begin(); target != data.end(); ++target) {
237
238 if (target->getString() == i->id) {
239
240 DEBUG("Modifier" << ' '
241 << "(" << FILL(4,'0') << target->getString() << "," << FILL(2,'0') << target->getFloor() << FILL() << ")" << ' '
242 << "action" << ' ' << i->action << JEEPZ() << i->data << endl);
243
244 if (!i->apply(*target)) {
245 ERROR("No valid action: " << *i << endl);
246 }
247 }
248 }
249 }
250
251 for (container_type::iterator i = data.begin(); i != data.end(); ) {
252
253 if ((keep.empty() || keep.count(i->getString()) != 0) &&
254 (rm .empty() || rm .count(i->getString()) == 0))
255 ++i;
256 else
257 i = data.erase(i);
258 }
259
260 data.store(outputFile.c_str());
261}
Container I/O.
string outputFile
int main(int argc, char **argv)
Data structure for hydrophone.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
ROOT I/O of application specific meta data.
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
I/O formatting auxiliaries.
void setPosition(const JVector3D &pos)
Set position.
JPosition3D & rotate(const JRotation3D &R)
Rotate.
Rotation around Z-axis.
Data structure for vector in three dimensions.
Definition JVector3D.hh:36
JVector3D & add(const JVector3D &vector)
Add vector.
Definition JVector3D.hh:142
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition JVector3D.hh:158
Utility class to parse command line options.
Definition JParser.hh:1698
std::istream & operator>>(std::istream &in, JAHRSCalibration &calibration)
Read AHRS calibration from input stream.
std::ostream & operator<<(std::ostream &out, const JAHRSCalibration &calibration)
Write AHRS calibration to output stream.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for sequence of same character.
Definition JManip.hh:330
Target.
Definition JHead.hh:300
Type definition of hydrophone.
Auxiliary wrapper for I/O of container with optional comment (see JComment).
Definition JContainer.hh:42
Auxiliary data structure for streaming of STL containers.
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition JParser.hh:68
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72