source: tags/casa3.2.0asap/src/STSelector.cpp@ 2747

Last change on this file since 2747 was 1930, checked in by Takeshi Nakazato, 14 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

  1. Added selector.get_row().
  2. Constructor allows to set rows and types.
  3. Selection summary string contains SRCTYPE and ROW selection.


File size: 8.6 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::setTypes( const std::vector< int >& types )
90{
91 setint("SRCTYPE", types);
92}
93
94void STSelector::setint(const std::string& key, const std::vector< int >& val)
95{
96 if ( val.size() > 0 ) {
97 intselections_[key] = val;
98 }
99}
100
101void STSelector::setstring( const std::string& key,
102 const std::vector<std::string>& val )
103{
104 if ( val.size() > 0 ) {
105 stringselections_[key] = val;
106 }
107}
108
109void STSelector::setTaQL( const std::string& taql )
110{
111 taql_ = taql;
112}
113
114
115void asap::STSelector::setSortOrder( const std::vector< std::string > & order )
116{
117 order_.resize(order.size(), True);
118 for (unsigned int i=0;i<order.size();++i) {
119 order_[i] = order[i];
120 }
121}
122
123void STSelector::setRows( const std::vector< int >& rows )
124{
125 rowselection_ = rows;
126}
127
128// Table STSelector::apply( const Table& tab )
129// {
130// if ( empty() ) {
131// return sort(tab);
132// }
133// TableExprNode query;
134// intidmap::const_iterator it;
135// for (it = intselections_.begin(); it != intselections_.end(); ++it) {
136// TableExprNode theset(Vector<Int>( (*it).second ));
137// if ( query.isNull() ) {
138// query = tab.col((*it).first).in(theset);
139// } else {
140// query = tab.col((*it).first).in(theset) && query;
141// }
142// }
143// stringidmap::const_iterator it1;
144// for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
145// TableExprNode theset(mathutil::toVectorString( (*it1).second ));
146// if ( query.isNull() ) {
147// query = tab.col((*it1).first).in(theset);
148// } else {
149// query = tab.col((*it1).first).in(theset) && query;
150// }
151// }
152// // add taql query
153// if ( taql_.size() > 0 ) {
154// Table tmpt = tab;
155// std::string pytaql = "USING STYLE PYTHON " + taql_;
156
157// if ( !query.isNull() ) { // taql and selection
158// tmpt = tableCommand(pytaql, tab(query));
159// } else { // taql only
160// tmpt = tableCommand(pytaql, tab);
161// }
162// return sort(tmpt);
163// } else {
164// if ( query.isNull() ) {
165// return sort(tab);
166// } else {
167// return sort(tab(query));
168// }
169// }
170// }
171Table STSelector::apply( const Table& tab )
172{
173 if ( empty() ) {
174 return sort(tab);
175 }
176 Table basetab = tab;
177 // Important!! Be sure to apply row selection first.
178 if (rowselection_.size() > 0){
179 //Vector<Int> intrownrs(rowselection_);
180 Vector<uInt> rownrs( rowselection_.size() );
181 convertArray(rownrs, Vector<Int> ( rowselection_ ));
182 basetab = tab( rownrs );
183 ///TableExprNode theset(Vector<Int>( rowselection_ ));
184 ///query = tab.nodeRownr().in(theset);
185 }
186 TableExprNode query;
187 intidmap::const_iterator it;
188 for (it = intselections_.begin(); it != intselections_.end(); ++it) {
189 TableExprNode theset(Vector<Int>( (*it).second ));
190 if ( query.isNull() ) {
191 //query = tab.col((*it).first).in(theset);
192 query = basetab.col((*it).first).in(theset);
193 } else {
194 //query = tab.col((*it).first).in(theset) && query;
195 query = basetab.col((*it).first).in(theset) && query;
196 }
197 }
198 stringidmap::const_iterator it1;
199 for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
200 TableExprNode theset(mathutil::toVectorString( (*it1).second ));
201 if ( query.isNull() ) {
202 //query = tab.col((*it1).first).in(theset);
203 query = basetab.col((*it1).first).in(theset);
204 } else {
205 //query = tab.col((*it1).first).in(theset) && query;
206 query = basetab.col((*it1).first).in(theset) && query;
207 }
208 }
209 // add taql query
210 if ( taql_.size() > 0 ) {
211 //Table tmpt = tab;
212 Table tmpt = basetab;
213 std::string pytaql = "USING STYLE PYTHON " + taql_;
214
215 if ( !query.isNull() ) { // taql and selection
216 //tmpt = tableCommand(pytaql, tab(query));
217 tmpt = tableCommand(pytaql, basetab(query));
218 } else { // taql only
219 //tmpt = tableCommand(pytaql, tab);
220 tmpt = tableCommand(pytaql, basetab);
221 }
222 return sort(tmpt);
223 } else {
224 if ( query.isNull() ) {
225 //return sort(tab);
226 return sort(basetab);
227 } else {
228 //return sort(tab(query));
229 return sort(basetab(query));
230 }
231 }
232}
233
234std::vector< int > STSelector::getint( const std::string& key ) const
235{
236 if (intselections_.count(key) > 0) {
237 return intselections_[key];
238 }
239 return std::vector<int>();
240}
241
242std::vector< int > STSelector::getScans( ) const
243{
244 return getint("SCANNO");
245}
246
247std::vector< int > STSelector::getBeams( ) const
248{
249 return getint("BEAMNO");
250}
251
252std::vector< int > STSelector::getIFs( ) const
253{
254 return getint("IFNO");
255}
256
257std::vector< int > STSelector::getPols( ) const
258{
259 return getint("POLNO");
260}
261
262std::vector< int > asap::STSelector::getCycles( ) const
263{
264 return getint("CYCLENO");
265}
266
267std::vector< int > asap::STSelector::getTypes( ) const
268{
269 return getint("SRCTYPE") ;
270}
271
272std::vector< int > asap::STSelector::getRows( ) const
273{
274 return rowselection_ ;
275}
276
277std::string asap::STSelector::print( )
278{
279 ostringstream oss;
280 oss.flags(std::ios_base::left);
281 oss << setw(15) << "Selection:";
282 if ( empty() ) {
283 oss << "none";
284 return String(oss);
285 }
286
287 intidmap::const_iterator it = intselections_.begin();
288 while (it != intselections_.end()) {
289 if ( it != intselections_.begin() )
290 oss << setw(15) << " ";
291 oss << it->first << ": " << Vector<Int>(it->second);
292 ++it;
293 if ( it != intselections_.end() ) oss << endl;
294 }
295 stringidmap::const_iterator it1 = stringselections_.begin();
296 while (it1 != stringselections_.end()) {
297 if ( it1 != stringselections_.begin() )
298 oss << setw(15) << " ";
299 oss << it1->first << ": " << mathutil::toVectorString(it1->second);
300 ++it1;
301 if ( it1 != stringselections_.end() ) oss << endl;
302 }
303 if ( taql_.size() > 0 ) {
304 oss << endl << setw(15) << "" << taql_;
305 }
306 return String(oss);
307}
308
309bool asap::STSelector::empty( ) const
310{
311 //return (intselections_.empty() && taql_.size() == 0 );
312 return (intselections_.empty() && taql_.size() == 0 && rowselection_.size() == 0);
313}
314
315casa::Table asap::STSelector::sort( const casa::Table & tab )
316{
317 if (order_.nelements() > 0) {
318 Table t = tab.sort(order_);
319 return t;
320 } else {
321 return tab;
322 }
323}
324
325
326void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols )
327{
328 poltypes_.clear();
329 std::vector<int> polints;
330 std::vector<std::string>::const_iterator strit;
331 for (strit = pols.begin(); strit != pols.end(); ++strit) {
332 std::pair<int, std::string> val;
333 try {
334 val = STPol::polFromString(*strit);
335 } catch (AipsError& e) {
336 poltypes_.clear();
337 throw(e);
338 }
339 polints.push_back(val.first);
340 poltypes_.push_back(val.second);
341 }
342 setint("POLNO", polints);
343}
344
345std::vector< std::string > asap::STSelector::getPolTypes( ) const
346{
347 return poltypes_;
348}
349
350std::vector<std::string> asap::STSelector::getSortOrder() const
351{
352 std::vector<std::string> out;
353 for (uInt i=0;i<order_.nelements(); ++i)
354 out.push_back(order_[i]);
355 return out;
356}
Note: See TracBrowser for help on using the repository browser.