source: trunk/src/STSelector.cpp @ 939

Last change on this file since 939 was 939, checked in by mar637, 18 years ago

added get functions

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