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