Jpp  17.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JEditTripod.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <set>
5 #include <algorithm>
6 
7 #include "TRandom3.h"
8 
9 #include "JDetector/JTripod.hh"
10 #include "JSupport/JMeta.hh"
11 #include "JLang/JComparator.hh"
12 #include "JLang/JComparison.hh"
13 #include "Jeep/JContainer.hh"
14 #include "Jeep/JPrint.hh"
15 #include "Jeep/JParser.hh"
16 #include "Jeep/JMessage.hh"
17 
18 
19 namespace {
20 
21  using namespace JPP;
22 
23  /**
24  * Wild card for tripod identifier.
25  */
26  static const int WILDCARD = -1;
27 
28  static const std::string set_t = "set"; //!< Set UTM position
29  static const std::string add_t = "add"; //!< Add UTM position
30  static const std::string sub_t = "sub"; //!< Subtract UTM position
31  static const std::string setx_t = "setx"; //!< Set East"
32  static const std::string sety_t = "sety"; //!< Set North"
33  static const std::string setz_t = "setz"; //!< Set depth
34  static const std::string addx_t = "addx"; //!< Add East"
35  static const std::string addy_t = "addy"; //!< Add North"
36  static const std::string addz_t = "addz"; //!< Add depth
37  static const std::string subx_t = "subx"; //!< Subtract East"
38  static const std::string suby_t = "suby"; //!< Subtract North"
39  static const std::string subz_t = "subz"; //!< Subtract depth
40 
41  static const std::string rand_t = "rand"; //!< Random value(s)
42  static const std::string randset_t = rand_t + set_t; //!< Set UTM position
43  static const std::string randadd_t = rand_t + add_t; //!< Add UTM position
44  static const std::string randsub_t = rand_t + sub_t; //!< Subtract UTM position
45 
46  /**
47  * Auxiliary class to apply tripod modifications.
48  */
49  struct JModifier {
50  /**
51  * Default constructor.
52  */
53  JModifier()
54  {}
55 
56 
57  /**
58  * Check validity.
59  *
60  * \return true if valid modifier; else false
61  */
62  bool is_valid() const
63  {
64  return (action != "" && !data.empty());
65  }
66 
67  /**
68  * Apply modification to given tripod.
69  *
70  * \param tripod tripod
71  * \return true if valid action; else false
72  */
73  bool apply(JTripod& tripod) const
74  {
75  switch (data.size()) {
76 
77  case 1:
78  return apply(tripod, action, data[0]);
79 
80  case 3:
81  return apply(tripod, action, JUTMPosition(data[0], data[1], data[2]));
82 
83  default:
84  return false;
85  }
86  }
87 
88 
89  /**
90  * Apply modification to given tripod.
91  *
92  * \param tripod tripod
93  * \param action action
94  * \param value value
95  * \return true if valid action; else false
96  */
97  static bool apply(JTripod& tripod, const std::string& action, const double value)
98  {
99  if (action == setx_t)
100  tripod.setUTMPosition(JUTMPosition(value, tripod.getUTMNorth(), tripod.getUTMZ()));
101  else if (action == addx_t)
102  tripod.add(JUTMPosition(value, 0.0, 0.0));
103  else if (action == subx_t)
104  tripod.sub(JUTMPosition(value, 0.0, 0.0));
105  else if (action == sety_t)
106  tripod.setUTMPosition(JUTMPosition(tripod.getUTMEast(), value, tripod.getUTMZ()));
107  else if (action == addy_t)
108  tripod.add(JUTMPosition(0.0, value, 0.0));
109  else if (action == suby_t)
110  tripod.sub(JUTMPosition(0.0, value, 0.0));
111  else if (action == setz_t)
112  tripod.setUTMPosition(JUTMPosition(tripod.getUTMEast(), tripod.getUTMNorth(), value));
113  else if (action == addz_t)
114  tripod.add(JUTMPosition(0.0, 0.0, value));
115  else if (action == subz_t)
116  tripod.sub(JUTMPosition(0.0, 0.0, value));
117  else
118  return false;
119 
120  return true;
121  }
122 
123 
124  /**
125  * Apply modification to given tripod.
126  *
127  * \param tripod tripod
128  * \param action action
129  * \param pos position
130  * \return true if valid action; else false
131  */
132  static bool apply(JTripod& tripod, const std::string& action, const JUTMPosition& pos)
133  {
134  const JUTMPosition randpos(gRandom->Gaus(0.0, pos.getUTMEast()),
135  gRandom->Gaus(0.0, pos.getUTMNorth()),
136  gRandom->Gaus(0.0, pos.getUTMZ()));
137 
138  if (action == set_t) // actions with fixed values
139  tripod.setUTMPosition(pos);
140  else if (action == add_t)
141  tripod.add(pos);
142  else if (action == sub_t)
143  tripod.sub(pos);
144  else if (action == randset_t) // actions with random values
145  tripod.setUTMPosition(randpos);
146  else if (action == randadd_t)
147  tripod.add(randpos);
148  else if (action == randsub_t)
149  tripod.sub(randpos);
150  else
151  return false;
152 
153  return true;
154  }
155 
156 
157  /**
158  * Read modifier from input.
159  *
160  * \param in input stream
161  * \param modifier modifier
162  * \return input stream
163  */
164  friend inline std::istream& operator>>(std::istream& in, JModifier& modifier)
165  {
166  if (in >> modifier.id >> modifier.action) {
167 
168  modifier.data.clear();
169 
170  for (double x; in >> x; ) {
171  modifier.data.push_back(x);
172  }
173 
174  in.clear(std::ios_base::eofbit);
175  }
176 
177  return in;
178  }
179 
180 
181  /**
182  * Write modifier to output.
183  *
184  * \param out output stream
185  * \param modifier modifier
186  * \return output stream
187  */
188  friend inline std::ostream& operator<<(std::ostream& out, const JModifier& modifier)
189  {
190  out << modifier.id;
191  out << ' ';
192  out << modifier.action;
193 
194  for (std::vector<double>::const_iterator i = modifier.data.begin(); i != modifier.data.end(); ++i) {
195  out << ' ' << *i;
196  }
197 
198  return out;
199  }
200 
201 
202  int id;
203  std::string action;
204  std::vector<double> data;
205  };
206 }
207 
208 /**
209  * \file
210  *
211  * Auxiliary program to modify tripod configuration.
212  *
213  * Syntax:
214  * <pre>
215  * -T "<tripod identifier> (set|add|sub) East North depth"
216  * -T "<tripod identifier> (setx|addx|subx) East"
217  * -T "<tripod identifier> (sety|addy|suby) North"
218  * -T "<tripod identifier> (setz|addz|subz) depth"
219  * </pre>
220  *
221  * The options
222  * <pre>
223  * -A "<tripod identifier> East North depth"
224  * -r "<tripod identifier>"
225  * -k "<tripod identifier>"
226  * </pre>
227  * can be used to add, remove and keep a specific tripod, respectively.
228  *
229  * \author mdejong
230  */
231 int main(int argc, char **argv)
232 {
233  using namespace std;
234  using namespace JPP;
235 
236  typedef JContainer< vector<JTripod> > container_type;
237 
238  string inputFile;
239  string outputFile;
240  vector<JModifier> mod;
241  vector<JTripod> add;
242  set<int> rm;
243  set<int> keep;
244  bool squash;
245  int debug;
246 
247  try {
248 
249  JParser<> zap("Auxiliary program to modify tripod configuration.");
250 
251  zap['f'] = make_field(inputFile, "tripod input file");
252  zap['o'] = make_field(outputFile, "tripod output file");
253  zap['T'] = make_field(mod, "tripod modifier") = JPARSER::initialised();
254  zap['A'] = make_field(add, "add tripod") = JPARSER::initialised();
255  zap['r'] = make_field(rm, "remove tripod[s]") = JPARSER::initialised();
256  zap['k'] = make_field(keep, "keep tripod[s]") = JPARSER::initialised();
257  zap['q'] = make_field(squash, "squash meta data");
258  zap['d'] = make_field(debug, "debug level") = 2;
259 
260  zap(argc, argv);
261  }
262  catch(const exception &error) {
263  FATAL(error.what() << endl);
264  }
265 
266  gRandom->SetSeed(0);
267 
268 
269  if (!rm.empty() && !keep.empty()) {
270  FATAL("Use either option -K or -D." << endl);
271  }
272 
273  container_type tripods;
274 
275  try {
276  tripods.load(inputFile.c_str());
277  }
278  catch(const exception&) {}
279 
280  if (squash) {
281  tripods.comment.clear();
282  }
283 
284  tripods.comment.add(JMeta(argc, argv));
285 
286  for (vector<JTripod>::const_iterator i = add.begin(); i != add.end(); ++i) {
287  tripods.push_back(*i);
288  }
289 
290  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
291 
292  for (container_type::iterator target = tripods.begin(); target != tripods.end(); ++target) {
293 
294  if (target->getID() == i->id || i->id == WILDCARD) {
295 
296  DEBUG("Modifier" << ' '
297  << "(" << FILL(2,'0') << target->getID() << FILL() << ")" << ' '
298  << i->action << ' ' << JEEPZ() << i->data << endl);
299 
300  if (!i->apply(*target)) {
301  ERROR("No valid action: " << *i << endl);
302  }
303  }
304  }
305  }
306 
307  if (!rm.empty()) {
308  for (container_type::iterator target = tripods.begin(); target != tripods.end(); ) {
309  if (rm.count(target->getID()) != 0)
310  target = tripods.erase(target);
311  else
312  ++target;
313  }
314  }
315 
316  if (!keep.empty()) {
317  for (container_type::iterator target = tripods.begin(); target != tripods.end(); ) {
318  if (keep.count(target->getID()) == 0)
319  target = tripods.erase(target);
320  else
321  ++target;
322  }
323  }
324 
325  sort(tripods.begin(), tripods.end(), make_comparator(&JTripod::getID));
326 
327  tripods.store(outputFile.c_str());
328 }
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:1517
int main(int argc, char *argv[])
Definition: Main.cc:15
Target.
Definition: JHead.hh:298
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
JUTMPosition & sub(const JUTMPosition &pos)
Subtract UTM position.
bool is_valid(const json &js)
Check validity of JSon data.
double getUTMEast() const
Get UTM east.
then fatal Number of tripods
Definition: JFootprint.sh:45
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
void setUTMPosition(const JUTMPosition &position)
Set UTM position.
Definition: JUTMPosition.hh:95
string outputFile
Data structure for UTM position.
Definition: JUTMPosition.hh:36
double getUTMZ() const
Get UTM Z.
I/O formatting auxiliaries.
double getUTMNorth() const
Get UTM north.
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:1993
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
then awk string
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:1756
Utility class to parse command line options.
Data structure for tripod.
Definition: JTripod.hh:32
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
JUTMPosition & add(const JUTMPosition &pos)
Add UTM position.
Data structure for tripod.
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
Container I/O.
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62