[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 | // |
---|
[3084] | 12 | #include <tables/TaQL/ExprNode.h> |
---|
| 13 | #include <tables/TaQL/TableParse.h> |
---|
[812] | 14 | #include <casa/BasicSL/String.h> |
---|
| 15 | #include <casa/iostream.h> |
---|
| 16 | #include <casa/iomanip.h> |
---|
[902] | 17 | #include <casa/Exceptions/Error.h> |
---|
[812] | 18 | |
---|
[850] | 19 | #include "MathUtils.h" |
---|
[902] | 20 | #include "STPol.h" |
---|
[812] | 21 | #include "STSelector.h" |
---|
| 22 | |
---|
| 23 | using namespace asap; |
---|
[3106] | 24 | using namespace casacore; |
---|
[812] | 25 | |
---|
| 26 | STSelector::STSelector() : |
---|
| 27 | taql_("") |
---|
| 28 | { |
---|
| 29 | } |
---|
| 30 | |
---|
[850] | 31 | STSelector::STSelector( const STSelector& other ) : |
---|
| 32 | intselections_(other.intselections_), |
---|
| 33 | stringselections_(other.stringselections_), |
---|
[939] | 34 | poltypes_(other.poltypes_), |
---|
[995] | 35 | order_(other.order_), |
---|
[2339] | 36 | taql_(other.taql_), |
---|
| 37 | rowselection_(other.rowselection_) |
---|
[995] | 38 | { |
---|
[812] | 39 | } |
---|
| 40 | |
---|
| 41 | STSelector& 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_; |
---|
[2339] | 49 | this->rowselection_ = other.rowselection_; |
---|
[812] | 50 | } |
---|
| 51 | return *this; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | STSelector::~STSelector() |
---|
| 55 | { |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void STSelector::setScans( const std::vector< int >& scans ) |
---|
| 59 | { |
---|
[850] | 60 | setint("SCANNO", scans); |
---|
[812] | 61 | } |
---|
| 62 | |
---|
| 63 | void STSelector::setBeams( const std::vector< int >& beams ) |
---|
| 64 | { |
---|
[850] | 65 | setint("BEAMNO", beams); |
---|
[812] | 66 | } |
---|
| 67 | |
---|
| 68 | void STSelector::setIFs( const std::vector< int >& ifs ) |
---|
| 69 | { |
---|
[850] | 70 | setint("IFNO", ifs); |
---|
[812] | 71 | } |
---|
| 72 | |
---|
| 73 | void STSelector::setPolarizations( const std::vector< int >& pols ) |
---|
| 74 | { |
---|
[850] | 75 | setint("POLNO", pols); |
---|
[954] | 76 | poltypes_ = std::vector<std::string>(); |
---|
[812] | 77 | } |
---|
| 78 | |
---|
[842] | 79 | void asap::STSelector::setCycles( const std::vector< int >& cycs ) |
---|
| 80 | { |
---|
[850] | 81 | setint("CYCLENO", cycs); |
---|
[842] | 82 | } |
---|
| 83 | |
---|
[850] | 84 | void asap::STSelector::setName( const std::string& sname ) |
---|
[812] | 85 | { |
---|
[1391] | 86 | std::string sql = "SELECT FROM $1 WHERE SRCNAME == pattern('"+sname+"')"; |
---|
[850] | 87 | setTaQL(sql); |
---|
| 88 | } |
---|
| 89 | |
---|
[1819] | 90 | void STSelector::setTypes( const std::vector< int >& types ) |
---|
| 91 | { |
---|
| 92 | setint("SRCTYPE", types); |
---|
| 93 | } |
---|
| 94 | |
---|
[850] | 95 | void STSelector::setint(const std::string& key, const std::vector< int >& val) |
---|
| 96 | { |
---|
[812] | 97 | if ( val.size() > 0 ) { |
---|
[850] | 98 | intselections_[key] = val; |
---|
[812] | 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
[850] | 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 | |
---|
[812] | 110 | void STSelector::setTaQL( const std::string& taql ) |
---|
| 111 | { |
---|
[1542] | 112 | taql_ = taql; |
---|
[812] | 113 | } |
---|
| 114 | |
---|
[902] | 115 | |
---|
| 116 | void asap::STSelector::setSortOrder( const std::vector< std::string > & order ) |
---|
| 117 | { |
---|
| 118 | order_.resize(order.size(), True); |
---|
[995] | 119 | for (unsigned int i=0;i<order.size();++i) { |
---|
[902] | 120 | order_[i] = order[i]; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
[1819] | 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 | // } |
---|
[812] | 172 | Table STSelector::apply( const Table& tab ) |
---|
| 173 | { |
---|
| 174 | if ( empty() ) { |
---|
[902] | 175 | return sort(tab); |
---|
[812] | 176 | } |
---|
[1819] | 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 | } |
---|
[812] | 187 | TableExprNode query; |
---|
[995] | 188 | intidmap::const_iterator it; |
---|
[1000] | 189 | for (it = intselections_.begin(); it != intselections_.end(); ++it) { |
---|
[812] | 190 | TableExprNode theset(Vector<Int>( (*it).second )); |
---|
| 191 | if ( query.isNull() ) { |
---|
[1819] | 192 | //query = tab.col((*it).first).in(theset); |
---|
| 193 | query = basetab.col((*it).first).in(theset); |
---|
[812] | 194 | } else { |
---|
[1819] | 195 | //query = tab.col((*it).first).in(theset) && query; |
---|
| 196 | query = basetab.col((*it).first).in(theset) && query; |
---|
[812] | 197 | } |
---|
| 198 | } |
---|
[995] | 199 | stringidmap::const_iterator it1; |
---|
| 200 | for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) { |
---|
[850] | 201 | TableExprNode theset(mathutil::toVectorString( (*it1).second )); |
---|
| 202 | if ( query.isNull() ) { |
---|
[1819] | 203 | //query = tab.col((*it1).first).in(theset); |
---|
| 204 | query = basetab.col((*it1).first).in(theset); |
---|
[850] | 205 | } else { |
---|
[1819] | 206 | //query = tab.col((*it1).first).in(theset) && query; |
---|
| 207 | query = basetab.col((*it1).first).in(theset) && query; |
---|
[850] | 208 | } |
---|
| 209 | } |
---|
[812] | 210 | // add taql query |
---|
| 211 | if ( taql_.size() > 0 ) { |
---|
[1819] | 212 | //Table tmpt = tab; |
---|
| 213 | Table tmpt = basetab; |
---|
[1542] | 214 | std::string pytaql = "USING STYLE PYTHON " + taql_; |
---|
[812] | 215 | |
---|
| 216 | if ( !query.isNull() ) { // taql and selection |
---|
[1819] | 217 | //tmpt = tableCommand(pytaql, tab(query)); |
---|
| 218 | tmpt = tableCommand(pytaql, basetab(query)); |
---|
[812] | 219 | } else { // taql only |
---|
[1819] | 220 | //tmpt = tableCommand(pytaql, tab); |
---|
| 221 | tmpt = tableCommand(pytaql, basetab); |
---|
[812] | 222 | } |
---|
[902] | 223 | return sort(tmpt); |
---|
[812] | 224 | } else { |
---|
[902] | 225 | if ( query.isNull() ) { |
---|
[1819] | 226 | //return sort(tab); |
---|
| 227 | return sort(basetab); |
---|
[902] | 228 | } else { |
---|
[1819] | 229 | //return sort(tab(query)); |
---|
| 230 | return sort(basetab(query)); |
---|
[902] | 231 | } |
---|
[812] | 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
[939] | 235 | std::vector< int > STSelector::getint( const std::string& key ) const |
---|
[812] | 236 | { |
---|
[850] | 237 | if (intselections_.count(key) > 0) { |
---|
[869] | 238 | return intselections_[key]; |
---|
[812] | 239 | } |
---|
[869] | 240 | return std::vector<int>(); |
---|
[812] | 241 | } |
---|
| 242 | |
---|
[939] | 243 | std::vector< int > STSelector::getScans( ) const |
---|
[812] | 244 | { |
---|
[850] | 245 | return getint("SCANNO"); |
---|
[812] | 246 | } |
---|
| 247 | |
---|
[939] | 248 | std::vector< int > STSelector::getBeams( ) const |
---|
[812] | 249 | { |
---|
[850] | 250 | return getint("BEAMNO"); |
---|
[812] | 251 | } |
---|
| 252 | |
---|
[939] | 253 | std::vector< int > STSelector::getIFs( ) const |
---|
[812] | 254 | { |
---|
[850] | 255 | return getint("IFNO"); |
---|
[812] | 256 | } |
---|
| 257 | |
---|
[939] | 258 | std::vector< int > STSelector::getPols( ) const |
---|
[812] | 259 | { |
---|
[850] | 260 | return getint("POLNO"); |
---|
[812] | 261 | } |
---|
| 262 | |
---|
[939] | 263 | std::vector< int > asap::STSelector::getCycles( ) const |
---|
[842] | 264 | { |
---|
[850] | 265 | return getint("CYCLENO"); |
---|
[842] | 266 | } |
---|
| 267 | |
---|
[1819] | 268 | std::vector< int > asap::STSelector::getTypes( ) const |
---|
| 269 | { |
---|
| 270 | return getint("SRCTYPE") ; |
---|
| 271 | } |
---|
| 272 | |
---|
[1930] | 273 | std::vector< int > asap::STSelector::getRows( ) const |
---|
| 274 | { |
---|
| 275 | return rowselection_ ; |
---|
| 276 | } |
---|
| 277 | |
---|
[812] | 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 | |
---|
[850] | 288 | intidmap::const_iterator it = intselections_.begin(); |
---|
| 289 | while (it != intselections_.end()) { |
---|
| 290 | if ( it != intselections_.begin() ) |
---|
[812] | 291 | oss << setw(15) << " "; |
---|
| 292 | oss << it->first << ": " << Vector<Int>(it->second); |
---|
| 293 | ++it; |
---|
[850] | 294 | if ( it != intselections_.end() ) oss << endl; |
---|
[812] | 295 | } |
---|
[850] | 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 | } |
---|
[812] | 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 | { |
---|
[1819] | 312 | //return (intselections_.empty() && taql_.size() == 0 ); |
---|
| 313 | return (intselections_.empty() && taql_.size() == 0 && rowselection_.size() == 0); |
---|
[812] | 314 | } |
---|
[902] | 315 | |
---|
[3106] | 316 | casacore::Table asap::STSelector::sort( const casacore::Table & tab ) |
---|
[902] | 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; |
---|
[995] | 331 | std::vector<std::string>::const_iterator strit; |
---|
| 332 | for (strit = pols.begin(); strit != pols.end(); ++strit) { |
---|
[902] | 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 | |
---|
[939] | 346 | std::vector< std::string > asap::STSelector::getPolTypes( ) const |
---|
[902] | 347 | { |
---|
| 348 | return poltypes_; |
---|
| 349 | } |
---|
[939] | 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 | } |
---|