source: trunk/src/STSelector.cpp @ 1428

Last change on this file since 1428 was 1428, checked in by Malte Marquarding, 16 years ago

Use TaQL style python in queries

File size: 6.0 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{
39}
40
41STSelector& STSelector::operator=( const STSelector& other )
42{
43  if (&other != this) {
44    this->intselections_ = other.intselections_;
45    this->stringselections_ = other.stringselections_;
46    this->taql_ = other.taql_;
47    this->poltypes_ = other.poltypes_;
48    this->order_ = other.order_;
49  }
50  return *this;
51}
52
53STSelector::~STSelector()
54{
55}
56
57void STSelector::setScans( const std::vector< int >& scans )
58{
59  setint("SCANNO", scans);
60}
61
62void STSelector::setBeams( const std::vector< int >& beams )
63{
64  setint("BEAMNO", beams);
65}
66
67void STSelector::setIFs( const std::vector< int >& ifs )
68{
69  setint("IFNO", ifs);
70}
71
72void STSelector::setPolarizations( const std::vector< int >& pols )
73{
74  setint("POLNO", pols);
75  poltypes_ = std::vector<std::string>();
76}
77
78void asap::STSelector::setCycles( const std::vector< int >& cycs )
79{
80  setint("CYCLENO", cycs);
81}
82
83void asap::STSelector::setName( const std::string& sname )
84{
85  std::string sql = "SELECT FROM $1 WHERE SRCNAME == pattern('"+sname+"')";
86  setTaQL(sql);
87}
88
89void STSelector::setint(const std::string& key, const std::vector< int >& val)
90{
91  if ( val.size() > 0 ) {
92    intselections_[key] = val;
93  }
94}
95
96void STSelector::setstring( const std::string& key,
97                            const std::vector<std::string>& val )
98{
99  if ( val.size() > 0 ) {
100    stringselections_[key] = val;
101  }
102}
103
104void STSelector::setTaQL( const std::string& taql )
105{
106  taql_ = "USING STYLE PYTHON " + taql;
107}
108
109
110void asap::STSelector::setSortOrder( const std::vector< std::string > & order )
111{
112  order_.resize(order.size(), True);
113  for (unsigned int i=0;i<order.size();++i) {
114    order_[i] = order[i];
115  }
116}
117
118Table STSelector::apply( const Table& tab )
119{
120  if ( empty() ) {
121    return sort(tab);
122  }
123  TableExprNode query;
124  intidmap::const_iterator it;
125  for (it = intselections_.begin(); it != intselections_.end(); ++it) {
126    TableExprNode theset(Vector<Int>( (*it).second ));
127    if ( query.isNull() ) {
128      query = tab.col((*it).first).in(theset);
129    } else {
130      query = tab.col((*it).first).in(theset) && query;
131    }
132  }
133  stringidmap::const_iterator it1;
134  for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
135    TableExprNode theset(mathutil::toVectorString( (*it1).second ));
136    if ( query.isNull() ) {
137      query = tab.col((*it1).first).in(theset);
138    } else {
139      query = tab.col((*it1).first).in(theset) && query;
140    }
141  }
142  // add taql query
143  if ( taql_.size() > 0 ) {
144    Table tmpt = tab;
145
146    if ( !query.isNull() ) { // taql and selection
147      tmpt = tableCommand(taql_, tab(query));
148    } else { // taql only
149      tmpt = tableCommand(taql_, tab);
150    }
151    return sort(tmpt);
152  } else {
153    if ( query.isNull() ) {
154      return sort(tab);
155    } else {
156      return sort(tab(query));
157    }
158  }
159}
160
161std::vector< int > STSelector::getint( const std::string& key ) const
162{
163  if (intselections_.count(key) > 0) {
164    return  intselections_[key];
165  }
166  return  std::vector<int>();
167}
168
169std::vector< int > STSelector::getScans( ) const
170{
171  return getint("SCANNO");
172}
173
174std::vector< int > STSelector::getBeams( ) const
175{
176  return getint("BEAMNO");
177}
178
179std::vector< int > STSelector::getIFs( ) const
180{
181  return getint("IFNO");
182}
183
184std::vector< int > STSelector::getPols( ) const
185{
186  return getint("POLNO");
187}
188
189std::vector< int > asap::STSelector::getCycles( ) const
190{
191  return getint("CYCLENO");
192}
193
194std::string asap::STSelector::print( )
195{
196  ostringstream oss;
197  oss.flags(std::ios_base::left);
198  oss << setw(15) << "Selection:";
199  if ( empty() ) {
200    oss << "none";
201    return String(oss);
202  }
203
204  intidmap::const_iterator it = intselections_.begin();
205  while (it != intselections_.end()) {
206    if ( it != intselections_.begin() )
207      oss << setw(15) << " ";
208    oss << it->first << ": " << Vector<Int>(it->second);
209    ++it;
210    if ( it != intselections_.end() ) oss << endl;
211  }
212  stringidmap::const_iterator it1 = stringselections_.begin();
213  while (it1 != stringselections_.end()) {
214    if ( it1 != stringselections_.begin() )
215      oss << setw(15) << " ";
216    oss << it1->first << ": " << mathutil::toVectorString(it1->second);
217    ++it1;
218    if ( it1 != stringselections_.end() ) oss << endl;
219  }
220  if ( taql_.size() > 0 ) {
221    oss << endl << setw(15) << "" << taql_;
222  }
223  return String(oss);
224}
225
226bool asap::STSelector::empty( ) const
227{
228  return (intselections_.empty() && taql_.size() == 0 );
229}
230
231casa::Table asap::STSelector::sort( const casa::Table & tab )
232{
233  if (order_.nelements() > 0) {
234    Table t = tab.sort(order_);
235    return t;
236  } else {
237    return tab;
238  }
239}
240
241
242void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols )
243{
244  poltypes_.clear();
245  std::vector<int> polints;
246  std::vector<std::string>::const_iterator strit;
247  for (strit = pols.begin(); strit != pols.end(); ++strit) {
248    std::pair<int, std::string> val;
249    try {
250       val = STPol::polFromString(*strit);
251    } catch (AipsError& e) {
252      poltypes_.clear();
253      throw(e);
254    }
255    polints.push_back(val.first);
256    poltypes_.push_back(val.second);
257  }
258  setint("POLNO", polints);
259}
260
261std::vector< std::string > asap::STSelector::getPolTypes( ) const
262{
263  return poltypes_;
264}
265
266std::vector<std::string> asap::STSelector::getSortOrder() const
267{
268  std::vector<std::string> out;
269  for (uInt i=0;i<order_.nelements(); ++i)
270    out.push_back(order_[i]);
271  return out;
272}
Note: See TracBrowser for help on using the repository browser.