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