[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_), |
---|
[812] | 35 | taql_(other.taql_) { |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | STSelector& STSelector::operator=( const STSelector& other ) |
---|
| 39 | { |
---|
| 40 | if (&other != this) { |
---|
[850] | 41 | this->intselections_ = other.intselections_; |
---|
| 42 | this->stringselections_ = other.stringselections_; |
---|
[812] | 43 | this->taql_ = other.taql_; |
---|
| 44 | } |
---|
| 45 | return *this; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | STSelector::~STSelector() |
---|
| 49 | { |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | void STSelector::setScans( const std::vector< int >& scans ) |
---|
| 53 | { |
---|
[850] | 54 | setint("SCANNO", scans); |
---|
[812] | 55 | } |
---|
| 56 | |
---|
| 57 | void STSelector::setBeams( const std::vector< int >& beams ) |
---|
| 58 | { |
---|
[850] | 59 | setint("BEAMNO", beams); |
---|
[812] | 60 | } |
---|
| 61 | |
---|
| 62 | void STSelector::setIFs( const std::vector< int >& ifs ) |
---|
| 63 | { |
---|
[850] | 64 | setint("IFNO", ifs); |
---|
[812] | 65 | } |
---|
| 66 | |
---|
| 67 | void STSelector::setPolarizations( const std::vector< int >& pols ) |
---|
| 68 | { |
---|
[850] | 69 | setint("POLNO", pols); |
---|
[812] | 70 | } |
---|
| 71 | |
---|
[842] | 72 | void asap::STSelector::setCycles( const std::vector< int >& cycs ) |
---|
| 73 | { |
---|
[850] | 74 | setint("CYCLENO", cycs); |
---|
[842] | 75 | } |
---|
| 76 | |
---|
[850] | 77 | void asap::STSelector::setName( const std::string& sname ) |
---|
[812] | 78 | { |
---|
[850] | 79 | std::string sql = "SELECT from $1 WHERE SRCNAME == pattern('"+sname+"')"; |
---|
| 80 | setTaQL(sql); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void STSelector::setint(const std::string& key, const std::vector< int >& val) |
---|
| 84 | { |
---|
[812] | 85 | if ( val.size() > 0 ) { |
---|
[850] | 86 | intselections_[key] = val; |
---|
[812] | 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
[850] | 90 | void 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 | |
---|
[812] | 98 | void STSelector::setTaQL( const std::string& taql ) |
---|
| 99 | { |
---|
| 100 | taql_ = taql; |
---|
| 101 | } |
---|
| 102 | |
---|
[902] | 103 | |
---|
| 104 | void 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 | |
---|
[812] | 112 | Table STSelector::apply( const Table& tab ) |
---|
| 113 | { |
---|
| 114 | if ( empty() ) { |
---|
[902] | 115 | return sort(tab); |
---|
[812] | 116 | } |
---|
| 117 | TableExprNode query; |
---|
[850] | 118 | intidmap::const_iterator it = intselections_.begin(); |
---|
| 119 | for (it; it != intselections_.end(); ++it) { |
---|
[812] | 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 | } |
---|
[850] | 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 | } |
---|
[812] | 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 | } |
---|
[902] | 145 | return sort(tmpt); |
---|
[812] | 146 | } else { |
---|
[902] | 147 | if ( query.isNull() ) { |
---|
| 148 | return sort(tab); |
---|
| 149 | } else { |
---|
| 150 | return sort(tab(query)); |
---|
| 151 | } |
---|
[812] | 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
[850] | 155 | std::vector< int > STSelector::getint( const std::string& key ) |
---|
[812] | 156 | { |
---|
[850] | 157 | if (intselections_.count(key) > 0) { |
---|
[869] | 158 | return intselections_[key]; |
---|
[812] | 159 | } |
---|
[869] | 160 | return std::vector<int>(); |
---|
[812] | 161 | } |
---|
| 162 | |
---|
| 163 | std::vector< int > STSelector::getScans( ) |
---|
| 164 | { |
---|
[850] | 165 | return getint("SCANNO"); |
---|
[812] | 166 | } |
---|
| 167 | |
---|
| 168 | std::vector< int > STSelector::getBeams( ) |
---|
| 169 | { |
---|
[850] | 170 | return getint("BEAMNO"); |
---|
[812] | 171 | } |
---|
| 172 | |
---|
| 173 | std::vector< int > STSelector::getIFs( ) |
---|
| 174 | { |
---|
[850] | 175 | return getint("IFNO"); |
---|
[812] | 176 | } |
---|
| 177 | |
---|
| 178 | std::vector< int > STSelector::getPols( ) |
---|
| 179 | { |
---|
[850] | 180 | return getint("POLNO"); |
---|
[812] | 181 | } |
---|
| 182 | |
---|
[842] | 183 | std::vector< int > asap::STSelector::getCycles( ) |
---|
| 184 | { |
---|
[850] | 185 | return getint("CYCLENO"); |
---|
[842] | 186 | } |
---|
| 187 | |
---|
[812] | 188 | std::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 | |
---|
[850] | 198 | intidmap::const_iterator it = intselections_.begin(); |
---|
| 199 | while (it != intselections_.end()) { |
---|
| 200 | if ( it != intselections_.begin() ) |
---|
[812] | 201 | oss << setw(15) << " "; |
---|
| 202 | oss << it->first << ": " << Vector<Int>(it->second); |
---|
| 203 | ++it; |
---|
[850] | 204 | if ( it != intselections_.end() ) oss << endl; |
---|
[812] | 205 | } |
---|
[850] | 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 | } |
---|
[812] | 214 | if ( taql_.size() > 0 ) { |
---|
| 215 | oss << endl << setw(15) << "" << taql_; |
---|
| 216 | } |
---|
| 217 | return String(oss); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | bool asap::STSelector::empty( ) const |
---|
| 221 | { |
---|
[850] | 222 | return (intselections_.empty() && taql_.size() == 0 ); |
---|
[812] | 223 | } |
---|
[902] | 224 | |
---|
| 225 | casa::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 | |
---|
| 237 | void 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 | |
---|
| 256 | std::vector< std::string > asap::STSelector::getPolTypes( ) |
---|
| 257 | { |
---|
| 258 | return poltypes_; |
---|
| 259 | } |
---|