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