source: trunk/src/STSelector.cpp @ 869

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

return empty std::vector<int> in ::get

File size: 4.5 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
19#include "MathUtils.h"
20#include "STSelector.h"
21
22using namespace asap;
23using namespace casa;
24
25STSelector::STSelector() :
26  taql_("")
27{
28}
29
30STSelector::STSelector( const STSelector&  other ) :
31  intselections_(other.intselections_),
32  stringselections_(other.stringselections_),
33  taql_(other.taql_) {
34}
35
36STSelector& STSelector::operator=( const STSelector& other )
37{
38  if (&other != this) {
39    this->intselections_ = other.intselections_;
40    this->stringselections_ = other.stringselections_;
41    this->taql_ = other.taql_;
42  }
43  return *this;
44}
45
46STSelector::~STSelector()
47{
48}
49
50void STSelector::setScans( const std::vector< int >& scans )
51{
52  setint("SCANNO", scans);
53}
54
55void STSelector::setBeams( const std::vector< int >& beams )
56{
57  setint("BEAMNO", beams);
58}
59
60void STSelector::setIFs( const std::vector< int >& ifs )
61{
62  setint("IFNO", ifs);
63}
64
65void STSelector::setPolarizations( const std::vector< int >& pols )
66{
67  setint("POLNO", pols);
68}
69
70void asap::STSelector::setCycles( const std::vector< int >& cycs )
71{
72  setint("CYCLENO", cycs);
73}
74
75void asap::STSelector::setName( const std::string& sname )
76{
77  std::string sql = "SELECT from $1 WHERE SRCNAME == pattern('"+sname+"')";
78  setTaQL(sql);
79}
80
81void STSelector::setint(const std::string& key, const std::vector< int >& val)
82{
83  if ( val.size() > 0 ) {
84    intselections_[key] = val;
85  }
86}
87
88void STSelector::setstring( const std::string& key,
89                            const std::vector<std::string>& val )
90{
91  if ( val.size() > 0 ) {
92    stringselections_[key] = val;
93  }
94}
95
96void STSelector::setTaQL( const std::string& taql )
97{
98  taql_ = taql;
99}
100
101Table STSelector::apply( const Table& tab )
102{
103  if ( empty() ) {
104    return tab;
105  }
106  TableExprNode query;
107  intidmap::const_iterator it = intselections_.begin();
108  for (it; it != intselections_.end(); ++it) {
109    TableExprNode theset(Vector<Int>( (*it).second ));
110    if ( query.isNull() ) {
111      query = tab.col((*it).first).in(theset);
112    } else {
113      query = tab.col((*it).first).in(theset) && query;
114    }
115  }
116  stringidmap::const_iterator it1 = stringselections_.begin();
117  for (it1; it1 != stringselections_.end(); ++it1) {
118    TableExprNode theset(mathutil::toVectorString( (*it1).second ));
119    if ( query.isNull() ) {
120      query = tab.col((*it1).first).in(theset);
121    } else {
122      query = tab.col((*it1).first).in(theset) && query;
123    }
124  }
125  // add taql query
126  if ( taql_.size() > 0 ) {
127    Table tmpt = tab;
128
129    if ( !query.isNull() ) { // taql and selection
130      tmpt = tableCommand(taql_, tab(query));
131    } else { // taql only
132      tmpt = tableCommand(taql_, tab);
133    }
134    return tmpt;
135  } else {
136    return tab(query);
137  }
138}
139
140std::vector< int > STSelector::getint( const std::string& key )
141{
142  if (intselections_.count(key) > 0) {
143    return  intselections_[key];
144  }
145  return  std::vector<int>();
146}
147
148std::vector< int > STSelector::getScans( )
149{
150  return getint("SCANNO");
151}
152
153std::vector< int > STSelector::getBeams( )
154{
155  return getint("BEAMNO");
156}
157
158std::vector< int > STSelector::getIFs( )
159{
160  return getint("IFNO");
161}
162
163std::vector< int > STSelector::getPols( )
164{
165  return getint("POLNO");
166}
167
168std::vector< int > asap::STSelector::getCycles( )
169{
170  return getint("CYCLENO");
171}
172
173std::string asap::STSelector::print( )
174{
175  ostringstream oss;
176  oss.flags(std::ios_base::left);
177  oss << setw(15) << "Selection:";
178  if ( empty() ) {
179    oss << "none";
180    return String(oss);
181  }
182
183  intidmap::const_iterator it = intselections_.begin();
184  while (it != intselections_.end()) {
185    if ( it != intselections_.begin() )
186      oss << setw(15) << " ";
187    oss << it->first << ": " << Vector<Int>(it->second);
188    ++it;
189    if ( it != intselections_.end() ) oss << endl;
190  }
191  stringidmap::const_iterator it1 = stringselections_.begin();
192  while (it1 != stringselections_.end()) {
193    if ( it1 != stringselections_.begin() )
194      oss << setw(15) << " ";
195    oss << it1->first << ": " << mathutil::toVectorString(it1->second);
196    ++it1;
197    if ( it1 != stringselections_.end() ) oss << endl;
198  }
199  if ( taql_.size() > 0 ) {
200    oss << endl << setw(15) << "" << taql_;
201  }
202  return String(oss);
203}
204
205bool asap::STSelector::empty( ) const
206{
207  return (intselections_.empty() && taql_.size() == 0 );
208}
Note: See TracBrowser for help on using the repository browser.