source: trunk/src/STSelector.cpp @ 2339

Last change on this file since 2339 was 2339, checked in by Malte Marquarding, 13 years ago

Ticket #256: somebody forget to copy rowselection in copy contructor/assignment operator

File size: 8.7 KB
Line 
1//
2// C++ Implementation: STSelector
3//
4// Description:
5//
6//
7// Author: Malte Marquarding <asap@atnf.csiro.au>, (C) 2006
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
12#include <tables/Tables/ExprNode.h>
13#include <tables/Tables/TableParse.h>
14#include <tables/Tables/ExprNode.h>
15#include <casa/BasicSL/String.h>
16#include <casa/iostream.h>
17#include <casa/iomanip.h>
18#include <casa/Exceptions/Error.h>
19
20#include "MathUtils.h"
21#include "STPol.h"
22#include "STSelector.h"
23
24using namespace asap;
25using namespace casa;
26
27STSelector::STSelector() :
28  taql_("")
29{
30}
31
32STSelector::STSelector( const STSelector&  other ) :
33  intselections_(other.intselections_),
34  stringselections_(other.stringselections_),
35  poltypes_(other.poltypes_),
36  order_(other.order_),
37  taql_(other.taql_),
38  rowselection_(other.rowselection_)
39{
40}
41
42STSelector& STSelector::operator=( const STSelector& other )
43{
44  if (&other != this) {
45    this->intselections_ = other.intselections_;
46    this->stringselections_ = other.stringselections_;
47    this->taql_ = other.taql_;
48    this->poltypes_ = other.poltypes_;
49    this->order_ = other.order_;
50    this->rowselection_ = other.rowselection_;
51  }
52  return *this;
53}
54
55STSelector::~STSelector()
56{
57}
58
59void STSelector::setScans( const std::vector< int >& scans )
60{
61  setint("SCANNO", scans);
62}
63
64void STSelector::setBeams( const std::vector< int >& beams )
65{
66  setint("BEAMNO", beams);
67}
68
69void STSelector::setIFs( const std::vector< int >& ifs )
70{
71  setint("IFNO", ifs);
72}
73
74void STSelector::setPolarizations( const std::vector< int >& pols )
75{
76  setint("POLNO", pols);
77  poltypes_ = std::vector<std::string>();
78}
79
80void asap::STSelector::setCycles( const std::vector< int >& cycs )
81{
82  setint("CYCLENO", cycs);
83}
84
85void asap::STSelector::setName( const std::string& sname )
86{
87  std::string sql = "SELECT FROM $1 WHERE SRCNAME == pattern('"+sname+"')";
88  setTaQL(sql);
89}
90
91void STSelector::setTypes( const std::vector< int >& types )
92{
93  setint("SRCTYPE", types);
94}
95
96void STSelector::setint(const std::string& key, const std::vector< int >& val)
97{
98  if ( val.size() > 0 ) {
99    intselections_[key] = val;
100  }
101}
102
103void STSelector::setstring( const std::string& key,
104                            const std::vector<std::string>& val )
105{
106  if ( val.size() > 0 ) {
107    stringselections_[key] = val;
108  }
109}
110
111void STSelector::setTaQL( const std::string& taql )
112{
113  taql_ = taql;
114}
115
116
117void asap::STSelector::setSortOrder( const std::vector< std::string > & order )
118{
119  order_.resize(order.size(), True);
120  for (unsigned int i=0;i<order.size();++i) {
121    order_[i] = order[i];
122  }
123}
124
125void STSelector::setRows( const std::vector< int >& rows )
126{
127  rowselection_ = rows;
128}
129
130// Table STSelector::apply( const Table& tab )
131// {
132//   if ( empty() ) {
133//     return sort(tab);
134//   }
135//   TableExprNode query;
136//   intidmap::const_iterator it;
137//   for (it = intselections_.begin(); it != intselections_.end(); ++it) {
138//     TableExprNode theset(Vector<Int>( (*it).second ));
139//     if ( query.isNull() ) {
140//       query = tab.col((*it).first).in(theset);
141//     } else {
142//       query = tab.col((*it).first).in(theset) && query;
143//     }
144//   }
145//   stringidmap::const_iterator it1;
146//   for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
147//     TableExprNode theset(mathutil::toVectorString( (*it1).second ));
148//     if ( query.isNull() ) {
149//       query = tab.col((*it1).first).in(theset);
150//     } else {
151//       query = tab.col((*it1).first).in(theset) && query;
152//     }
153//   }
154//   // add taql query
155//   if ( taql_.size() > 0 ) {
156//     Table tmpt = tab;
157//     std::string pytaql = "USING STYLE PYTHON " + taql_;
158
159//     if ( !query.isNull() ) { // taql and selection
160//       tmpt = tableCommand(pytaql, tab(query));
161//     } else { // taql only
162//       tmpt = tableCommand(pytaql, tab);
163//     }
164//     return sort(tmpt);
165//   } else {
166//     if ( query.isNull() ) {
167//       return sort(tab);
168//     } else {
169//       return sort(tab(query));
170//     }
171//   }
172// }
173Table STSelector::apply( const Table& tab )
174{
175  if ( empty() ) {
176    return sort(tab);
177  }
178  Table basetab = tab;
179  // Important!! Be sure to apply row selection first.
180  if (rowselection_.size() > 0){
181    //Vector<Int> intrownrs(rowselection_);
182    Vector<uInt> rownrs( rowselection_.size() );
183    convertArray(rownrs, Vector<Int> ( rowselection_ ));
184    basetab = tab( rownrs );
185    ///TableExprNode theset(Vector<Int>( rowselection_ ));
186    ///query = tab.nodeRownr().in(theset);
187  }
188  TableExprNode query;
189  intidmap::const_iterator it;
190  for (it = intselections_.begin(); it != intselections_.end(); ++it) {
191    TableExprNode theset(Vector<Int>( (*it).second ));
192    if ( query.isNull() ) {
193      //query = tab.col((*it).first).in(theset);
194      query = basetab.col((*it).first).in(theset);
195    } else {
196      //query = tab.col((*it).first).in(theset) && query;
197      query = basetab.col((*it).first).in(theset) && query;
198    }
199  }
200  stringidmap::const_iterator it1;
201  for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
202    TableExprNode theset(mathutil::toVectorString( (*it1).second ));
203    if ( query.isNull() ) {
204      //query = tab.col((*it1).first).in(theset);
205      query = basetab.col((*it1).first).in(theset);
206    } else {
207      //query = tab.col((*it1).first).in(theset) && query;
208      query = basetab.col((*it1).first).in(theset) && query;
209    }
210  }
211  // add taql query
212  if ( taql_.size() > 0 ) {
213    //Table tmpt = tab;
214    Table tmpt = basetab;
215    std::string pytaql = "USING STYLE PYTHON " + taql_;
216
217    if ( !query.isNull() ) { // taql and selection
218      //tmpt = tableCommand(pytaql, tab(query));
219      tmpt = tableCommand(pytaql, basetab(query));
220    } else { // taql only
221      //tmpt = tableCommand(pytaql, tab);
222      tmpt = tableCommand(pytaql, basetab);
223    }
224    return sort(tmpt);
225  } else {
226    if ( query.isNull() ) {
227      //return sort(tab);
228      return sort(basetab);
229    } else {
230      //return sort(tab(query));
231      return sort(basetab(query));
232    }
233  }
234}
235
236std::vector< int > STSelector::getint( const std::string& key ) const
237{
238  if (intselections_.count(key) > 0) {
239    return  intselections_[key];
240  }
241  return  std::vector<int>();
242}
243
244std::vector< int > STSelector::getScans( ) const
245{
246  return getint("SCANNO");
247}
248
249std::vector< int > STSelector::getBeams( ) const
250{
251  return getint("BEAMNO");
252}
253
254std::vector< int > STSelector::getIFs( ) const
255{
256  return getint("IFNO");
257}
258
259std::vector< int > STSelector::getPols( ) const
260{
261  return getint("POLNO");
262}
263
264std::vector< int > asap::STSelector::getCycles( ) const
265{
266  return getint("CYCLENO");
267}
268
269std::vector< int > asap::STSelector::getTypes( ) const
270{
271  return getint("SRCTYPE") ;
272}
273
274std::vector< int > asap::STSelector::getRows( ) const
275{
276  return rowselection_ ;
277}
278
279std::string asap::STSelector::print( )
280{
281  ostringstream oss;
282  oss.flags(std::ios_base::left);
283  oss << setw(15) << "Selection:";
284  if ( empty() ) {
285    oss << "none";
286    return String(oss);
287  }
288
289  intidmap::const_iterator it = intselections_.begin();
290  while (it != intselections_.end()) {
291    if ( it != intselections_.begin() )
292      oss << setw(15) << " ";
293    oss << it->first << ": " << Vector<Int>(it->second);
294    ++it;
295    if ( it != intselections_.end() ) oss << endl;
296  }
297  stringidmap::const_iterator it1 = stringselections_.begin();
298  while (it1 != stringselections_.end()) {
299    if ( it1 != stringselections_.begin() )
300      oss << setw(15) << " ";
301    oss << it1->first << ": " << mathutil::toVectorString(it1->second);
302    ++it1;
303    if ( it1 != stringselections_.end() ) oss << endl;
304  }
305  if ( taql_.size() > 0 ) {
306    oss << endl << setw(15) << "" << taql_;
307  }
308  return String(oss);
309}
310
311bool asap::STSelector::empty( ) const
312{
313  //return (intselections_.empty() && taql_.size() == 0 );
314  return (intselections_.empty() && taql_.size() == 0 && rowselection_.size() == 0);
315}
316
317casa::Table asap::STSelector::sort( const casa::Table & tab )
318{
319  if (order_.nelements() > 0) {
320    Table t = tab.sort(order_);
321    return t;
322  } else {
323    return tab;
324  }
325}
326
327
328void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols )
329{
330  poltypes_.clear();
331  std::vector<int> polints;
332  std::vector<std::string>::const_iterator strit;
333  for (strit = pols.begin(); strit != pols.end(); ++strit) {
334    std::pair<int, std::string> val;
335    try {
336       val = STPol::polFromString(*strit);
337    } catch (AipsError& e) {
338      poltypes_.clear();
339      throw(e);
340    }
341    polints.push_back(val.first);
342    poltypes_.push_back(val.second);
343  }
344  setint("POLNO", polints);
345}
346
347std::vector< std::string > asap::STSelector::getPolTypes( ) const
348{
349  return poltypes_;
350}
351
352std::vector<std::string> asap::STSelector::getSortOrder() const
353{
354  std::vector<std::string> out;
355  for (uInt i=0;i<order_.nelements(); ++i)
356    out.push_back(order_[i]);
357  return out;
358}
Note: See TracBrowser for help on using the repository browser.