source: trunk/src/STSelector.cpp@ 1572

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

applied python taql style at the wrong level

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