source: trunk/src/STSelector.cpp@ 962

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

Fixed Ticket #6 (selector.set_polarisations error)

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