source: trunk/src/STSelector.cpp@ 928

Last change on this file since 928 was 902, checked in by mar637, 19 years ago

More work on polarisation. STPol and labelling

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