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/TaQL/ExprNode.h> |
---|
13 | #include <tables/TaQL/TableParse.h> |
---|
14 | #include <casa/BasicSL/String.h> |
---|
15 | #include <casa/iostream.h> |
---|
16 | #include <casa/iomanip.h> |
---|
17 | #include <casa/Exceptions/Error.h> |
---|
18 | |
---|
19 | #include "MathUtils.h" |
---|
20 | #include "STPol.h" |
---|
21 | #include "STSelector.h" |
---|
22 | |
---|
23 | using namespace asap; |
---|
24 | using namespace casacore; |
---|
25 | |
---|
26 | STSelector::STSelector() : |
---|
27 | taql_("") |
---|
28 | { |
---|
29 | } |
---|
30 | |
---|
31 | STSelector::STSelector( const STSelector& other ) : |
---|
32 | intselections_(other.intselections_), |
---|
33 | stringselections_(other.stringselections_), |
---|
34 | poltypes_(other.poltypes_), |
---|
35 | order_(other.order_), |
---|
36 | taql_(other.taql_), |
---|
37 | rowselection_(other.rowselection_) |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | STSelector& 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 | this->rowselection_ = other.rowselection_; |
---|
50 | } |
---|
51 | return *this; |
---|
52 | } |
---|
53 | |
---|
54 | STSelector::~STSelector() |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | void STSelector::setScans( const std::vector< int >& scans ) |
---|
59 | { |
---|
60 | setint("SCANNO", scans); |
---|
61 | } |
---|
62 | |
---|
63 | void STSelector::setBeams( const std::vector< int >& beams ) |
---|
64 | { |
---|
65 | setint("BEAMNO", beams); |
---|
66 | } |
---|
67 | |
---|
68 | void STSelector::setIFs( const std::vector< int >& ifs ) |
---|
69 | { |
---|
70 | setint("IFNO", ifs); |
---|
71 | } |
---|
72 | |
---|
73 | void STSelector::setPolarizations( const std::vector< int >& pols ) |
---|
74 | { |
---|
75 | setint("POLNO", pols); |
---|
76 | poltypes_ = std::vector<std::string>(); |
---|
77 | } |
---|
78 | |
---|
79 | void asap::STSelector::setCycles( const std::vector< int >& cycs ) |
---|
80 | { |
---|
81 | setint("CYCLENO", cycs); |
---|
82 | } |
---|
83 | |
---|
84 | void asap::STSelector::setName( const std::string& sname ) |
---|
85 | { |
---|
86 | std::string sql = "SELECT FROM $1 WHERE SRCNAME == pattern('"+sname+"')"; |
---|
87 | setTaQL(sql); |
---|
88 | } |
---|
89 | |
---|
90 | void STSelector::setTypes( const std::vector< int >& types ) |
---|
91 | { |
---|
92 | setint("SRCTYPE", types); |
---|
93 | } |
---|
94 | |
---|
95 | void STSelector::setint(const std::string& key, const std::vector< int >& val) |
---|
96 | { |
---|
97 | if ( val.size() > 0 ) { |
---|
98 | intselections_[key] = val; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | void STSelector::setstring( const std::string& key, |
---|
103 | const std::vector<std::string>& val ) |
---|
104 | { |
---|
105 | if ( val.size() > 0 ) { |
---|
106 | stringselections_[key] = val; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | void STSelector::setTaQL( const std::string& taql ) |
---|
111 | { |
---|
112 | taql_ = taql; |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | void asap::STSelector::setSortOrder( const std::vector< std::string > & order ) |
---|
117 | { |
---|
118 | order_.resize(order.size(), True); |
---|
119 | for (unsigned int i=0;i<order.size();++i) { |
---|
120 | order_[i] = order[i]; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | void STSelector::setRows( const std::vector< int >& rows ) |
---|
125 | { |
---|
126 | rowselection_ = rows; |
---|
127 | } |
---|
128 | |
---|
129 | // Table STSelector::apply( const Table& tab ) |
---|
130 | // { |
---|
131 | // if ( empty() ) { |
---|
132 | // return sort(tab); |
---|
133 | // } |
---|
134 | // TableExprNode query; |
---|
135 | // intidmap::const_iterator it; |
---|
136 | // for (it = intselections_.begin(); it != intselections_.end(); ++it) { |
---|
137 | // TableExprNode theset(Vector<Int>( (*it).second )); |
---|
138 | // if ( query.isNull() ) { |
---|
139 | // query = tab.col((*it).first).in(theset); |
---|
140 | // } else { |
---|
141 | // query = tab.col((*it).first).in(theset) && query; |
---|
142 | // } |
---|
143 | // } |
---|
144 | // stringidmap::const_iterator it1; |
---|
145 | // for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) { |
---|
146 | // TableExprNode theset(mathutil::toVectorString( (*it1).second )); |
---|
147 | // if ( query.isNull() ) { |
---|
148 | // query = tab.col((*it1).first).in(theset); |
---|
149 | // } else { |
---|
150 | // query = tab.col((*it1).first).in(theset) && query; |
---|
151 | // } |
---|
152 | // } |
---|
153 | // // add taql query |
---|
154 | // if ( taql_.size() > 0 ) { |
---|
155 | // Table tmpt = tab; |
---|
156 | // std::string pytaql = "USING STYLE PYTHON " + taql_; |
---|
157 | |
---|
158 | // if ( !query.isNull() ) { // taql and selection |
---|
159 | // tmpt = tableCommand(pytaql, tab(query)); |
---|
160 | // } else { // taql only |
---|
161 | // tmpt = tableCommand(pytaql, tab); |
---|
162 | // } |
---|
163 | // return sort(tmpt); |
---|
164 | // } else { |
---|
165 | // if ( query.isNull() ) { |
---|
166 | // return sort(tab); |
---|
167 | // } else { |
---|
168 | // return sort(tab(query)); |
---|
169 | // } |
---|
170 | // } |
---|
171 | // } |
---|
172 | Table STSelector::apply( const Table& tab ) |
---|
173 | { |
---|
174 | if ( empty() ) { |
---|
175 | return sort(tab); |
---|
176 | } |
---|
177 | Table basetab = tab; |
---|
178 | // Important!! Be sure to apply row selection first. |
---|
179 | if (rowselection_.size() > 0){ |
---|
180 | //Vector<Int> intrownrs(rowselection_); |
---|
181 | Vector<uInt> rownrs( rowselection_.size() ); |
---|
182 | convertArray(rownrs, Vector<Int> ( rowselection_ )); |
---|
183 | basetab = tab( rownrs ); |
---|
184 | ///TableExprNode theset(Vector<Int>( rowselection_ )); |
---|
185 | ///query = tab.nodeRownr().in(theset); |
---|
186 | } |
---|
187 | TableExprNode query; |
---|
188 | intidmap::const_iterator it; |
---|
189 | for (it = intselections_.begin(); it != intselections_.end(); ++it) { |
---|
190 | TableExprNode theset(Vector<Int>( (*it).second )); |
---|
191 | if ( query.isNull() ) { |
---|
192 | //query = tab.col((*it).first).in(theset); |
---|
193 | query = basetab.col((*it).first).in(theset); |
---|
194 | } else { |
---|
195 | //query = tab.col((*it).first).in(theset) && query; |
---|
196 | query = basetab.col((*it).first).in(theset) && query; |
---|
197 | } |
---|
198 | } |
---|
199 | stringidmap::const_iterator it1; |
---|
200 | for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) { |
---|
201 | TableExprNode theset(mathutil::toVectorString( (*it1).second )); |
---|
202 | if ( query.isNull() ) { |
---|
203 | //query = tab.col((*it1).first).in(theset); |
---|
204 | query = basetab.col((*it1).first).in(theset); |
---|
205 | } else { |
---|
206 | //query = tab.col((*it1).first).in(theset) && query; |
---|
207 | query = basetab.col((*it1).first).in(theset) && query; |
---|
208 | } |
---|
209 | } |
---|
210 | // add taql query |
---|
211 | if ( taql_.size() > 0 ) { |
---|
212 | //Table tmpt = tab; |
---|
213 | Table tmpt = basetab; |
---|
214 | std::string pytaql = "USING STYLE PYTHON " + taql_; |
---|
215 | |
---|
216 | if ( !query.isNull() ) { // taql and selection |
---|
217 | //tmpt = tableCommand(pytaql, tab(query)); |
---|
218 | tmpt = tableCommand(pytaql, basetab(query)); |
---|
219 | } else { // taql only |
---|
220 | //tmpt = tableCommand(pytaql, tab); |
---|
221 | tmpt = tableCommand(pytaql, basetab); |
---|
222 | } |
---|
223 | return sort(tmpt); |
---|
224 | } else { |
---|
225 | if ( query.isNull() ) { |
---|
226 | //return sort(tab); |
---|
227 | return sort(basetab); |
---|
228 | } else { |
---|
229 | //return sort(tab(query)); |
---|
230 | return sort(basetab(query)); |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | std::vector< int > STSelector::getint( const std::string& key ) const |
---|
236 | { |
---|
237 | if (intselections_.count(key) > 0) { |
---|
238 | return intselections_[key]; |
---|
239 | } |
---|
240 | return std::vector<int>(); |
---|
241 | } |
---|
242 | |
---|
243 | std::vector< int > STSelector::getScans( ) const |
---|
244 | { |
---|
245 | return getint("SCANNO"); |
---|
246 | } |
---|
247 | |
---|
248 | std::vector< int > STSelector::getBeams( ) const |
---|
249 | { |
---|
250 | return getint("BEAMNO"); |
---|
251 | } |
---|
252 | |
---|
253 | std::vector< int > STSelector::getIFs( ) const |
---|
254 | { |
---|
255 | return getint("IFNO"); |
---|
256 | } |
---|
257 | |
---|
258 | std::vector< int > STSelector::getPols( ) const |
---|
259 | { |
---|
260 | return getint("POLNO"); |
---|
261 | } |
---|
262 | |
---|
263 | std::vector< int > asap::STSelector::getCycles( ) const |
---|
264 | { |
---|
265 | return getint("CYCLENO"); |
---|
266 | } |
---|
267 | |
---|
268 | std::vector< int > asap::STSelector::getTypes( ) const |
---|
269 | { |
---|
270 | return getint("SRCTYPE") ; |
---|
271 | } |
---|
272 | |
---|
273 | std::vector< int > asap::STSelector::getRows( ) const |
---|
274 | { |
---|
275 | return rowselection_ ; |
---|
276 | } |
---|
277 | |
---|
278 | std::string asap::STSelector::print( ) |
---|
279 | { |
---|
280 | ostringstream oss; |
---|
281 | oss.flags(std::ios_base::left); |
---|
282 | oss << setw(15) << "Selection:"; |
---|
283 | if ( empty() ) { |
---|
284 | oss << "none"; |
---|
285 | return String(oss); |
---|
286 | } |
---|
287 | |
---|
288 | intidmap::const_iterator it = intselections_.begin(); |
---|
289 | while (it != intselections_.end()) { |
---|
290 | if ( it != intselections_.begin() ) |
---|
291 | oss << setw(15) << " "; |
---|
292 | oss << it->first << ": " << Vector<Int>(it->second); |
---|
293 | ++it; |
---|
294 | if ( it != intselections_.end() ) oss << endl; |
---|
295 | } |
---|
296 | stringidmap::const_iterator it1 = stringselections_.begin(); |
---|
297 | while (it1 != stringselections_.end()) { |
---|
298 | if ( it1 != stringselections_.begin() ) |
---|
299 | oss << setw(15) << " "; |
---|
300 | oss << it1->first << ": " << mathutil::toVectorString(it1->second); |
---|
301 | ++it1; |
---|
302 | if ( it1 != stringselections_.end() ) oss << endl; |
---|
303 | } |
---|
304 | if ( taql_.size() > 0 ) { |
---|
305 | oss << endl << setw(15) << "" << taql_; |
---|
306 | } |
---|
307 | return String(oss); |
---|
308 | } |
---|
309 | |
---|
310 | bool asap::STSelector::empty( ) const |
---|
311 | { |
---|
312 | //return (intselections_.empty() && taql_.size() == 0 ); |
---|
313 | return (intselections_.empty() && taql_.size() == 0 && rowselection_.size() == 0); |
---|
314 | } |
---|
315 | |
---|
316 | casacore::Table asap::STSelector::sort( const casacore::Table & tab ) |
---|
317 | { |
---|
318 | if (order_.nelements() > 0) { |
---|
319 | Table t = tab.sort(order_); |
---|
320 | return t; |
---|
321 | } else { |
---|
322 | return tab; |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols ) |
---|
328 | { |
---|
329 | poltypes_.clear(); |
---|
330 | std::vector<int> polints; |
---|
331 | std::vector<std::string>::const_iterator strit; |
---|
332 | for (strit = pols.begin(); strit != pols.end(); ++strit) { |
---|
333 | std::pair<int, std::string> val; |
---|
334 | try { |
---|
335 | val = STPol::polFromString(*strit); |
---|
336 | } catch (AipsError& e) { |
---|
337 | poltypes_.clear(); |
---|
338 | throw(e); |
---|
339 | } |
---|
340 | polints.push_back(val.first); |
---|
341 | poltypes_.push_back(val.second); |
---|
342 | } |
---|
343 | setint("POLNO", polints); |
---|
344 | } |
---|
345 | |
---|
346 | std::vector< std::string > asap::STSelector::getPolTypes( ) const |
---|
347 | { |
---|
348 | return poltypes_; |
---|
349 | } |
---|
350 | |
---|
351 | std::vector<std::string> asap::STSelector::getSortOrder() const |
---|
352 | { |
---|
353 | std::vector<std::string> out; |
---|
354 | for (uInt i=0;i<order_.nelements(); ++i) |
---|
355 | out.push_back(order_[i]); |
---|
356 | return out; |
---|
357 | } |
---|