Jpp  17.0.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  * -D "<tripod identifier>"
225  * -K "<tripod identifier>"
226  * </pre>
227  * can be used to add, delete 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 file_name;
239  vector<JModifier> mod;
240  vector<JTripod> add;
241  set<int> del;
242  set<int> keep;
243  bool rm;
244  int debug;
245 
246  try {
247 
248  JParser<> zap("Auxiliary program to modify tripod configuration.");
249 
250  zap['f'] = make_field(file_name, "tripod file");
251  zap['T'] = make_field(mod, "tripod modifier") = JPARSER::initialised();
252  zap['A'] = make_field(add, "add tripod") = JPARSER::initialised();
253  zap['D'] = make_field(del, "delete tripod") = JPARSER::initialised();
254  zap['K'] = make_field(keep, "keep tripod") = JPARSER::initialised();
255  zap['r'] = make_field(rm, "remove previous comments");
256  zap['d'] = make_field(debug) = 1;
257 
258  zap(argc, argv);
259  }
260  catch(const exception &error) {
261  FATAL(error.what() << endl);
262  }
263 
264  gRandom->SetSeed(0);
265 
266 
267  if (!del.empty() && !keep.empty()) {
268  FATAL("Use either option -K or -D." << endl);
269  }
270 
271  container_type tripods;
272 
273  try {
274  tripods.load(file_name.c_str());
275  }
276  catch(const exception&) {}
277 
278  if (rm) {
279  tripods.comment.clear();
280  }
281 
282  tripods.comment.add(JMeta(argc, argv));
283 
284  for (vector<JTripod>::const_iterator i = add.begin(); i != add.end(); ++i) {
285  tripods.push_back(*i);
286  }
287 
288  for (vector<JModifier>::const_iterator i = mod.begin(); i != mod.end(); ++i) {
289 
290  for (container_type::iterator target = tripods.begin(); target != tripods.end(); ++target) {
291 
292  if (target->getID() == i->id || i->id == WILDCARD) {
293 
294  DEBUG("Modifier" << ' '
295  << "(" << FILL(2,'0') << target->getID() << FILL() << ")" << ' '
296  << i->action << ' ' << JEEPZ() << i->data << endl);
297 
298  if (!i->apply(*target)) {
299  ERROR("No valid action: " << *i << endl);
300  }
301  }
302  }
303  }
304 
305  if (!del.empty()) {
306  for (container_type::iterator target = tripods.begin(); target != tripods.end(); ) {
307  if (del.count(target->getID()) != 0)
308  target = tripods.erase(target);
309  else
310  ++target;
311  }
312  }
313 
314  if (!keep.empty()) {
315  for (container_type::iterator target = tripods.begin(); target != tripods.end(); ) {
316  if (keep.count(target->getID()) == 0)
317  target = tripods.erase(target);
318  else
319  ++target;
320  }
321  }
322 
323  sort(tripods.begin(), tripods.end(), make_comparator(&JTripod::getID));
324 
325  tripods.store(file_name.c_str());
326 }
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
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
Definition: JComparator.hh:185
JUTMPosition & sub(const JUTMPosition &pos)
Subtract UTM position.
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:66
void setUTMPosition(const JUTMPosition &position)
Set UTM position.
Definition: JUTMPosition.hh:95
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:1961
bool is_valid(const json &js)
Check validity of JSon data.
ROOT I/O of application specific meta data.
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:66
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:1716
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.
then usage $script< archive >< detectorfile >< run > nGet tripod
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62