| [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 |  | 
|---|
|  | 89 | void STSelector::setint(const std::string& key, const std::vector< int >& val) | 
|---|
|  | 90 | { | 
|---|
| [812] | 91 | if ( val.size() > 0 ) { | 
|---|
| [850] | 92 | intselections_[key] = val; | 
|---|
| [812] | 93 | } | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
| [850] | 96 | void STSelector::setstring( const std::string& key, | 
|---|
|  | 97 | const std::vector<std::string>& val ) | 
|---|
|  | 98 | { | 
|---|
|  | 99 | if ( val.size() > 0 ) { | 
|---|
|  | 100 | stringselections_[key] = val; | 
|---|
|  | 101 | } | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
| [812] | 104 | void STSelector::setTaQL( const std::string& taql ) | 
|---|
|  | 105 | { | 
|---|
| [1428] | 106 | taql_ = "USING STYLE PYTHON " + taql; | 
|---|
| [812] | 107 | } | 
|---|
|  | 108 |  | 
|---|
| [902] | 109 |  | 
|---|
|  | 110 | void asap::STSelector::setSortOrder( const std::vector< std::string > & order ) | 
|---|
|  | 111 | { | 
|---|
|  | 112 | order_.resize(order.size(), True); | 
|---|
| [995] | 113 | for (unsigned int i=0;i<order.size();++i) { | 
|---|
| [902] | 114 | order_[i] = order[i]; | 
|---|
|  | 115 | } | 
|---|
|  | 116 | } | 
|---|
|  | 117 |  | 
|---|
| [812] | 118 | Table STSelector::apply( const Table& tab ) | 
|---|
|  | 119 | { | 
|---|
|  | 120 | if ( empty() ) { | 
|---|
| [902] | 121 | return sort(tab); | 
|---|
| [812] | 122 | } | 
|---|
|  | 123 | TableExprNode query; | 
|---|
| [995] | 124 | intidmap::const_iterator it; | 
|---|
| [1000] | 125 | for (it = intselections_.begin(); it != intselections_.end(); ++it) { | 
|---|
| [812] | 126 | TableExprNode theset(Vector<Int>( (*it).second )); | 
|---|
|  | 127 | if ( query.isNull() ) { | 
|---|
|  | 128 | query = tab.col((*it).first).in(theset); | 
|---|
|  | 129 | } else { | 
|---|
|  | 130 | query = tab.col((*it).first).in(theset) && query; | 
|---|
|  | 131 | } | 
|---|
|  | 132 | } | 
|---|
| [995] | 133 | stringidmap::const_iterator it1; | 
|---|
|  | 134 | for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) { | 
|---|
| [850] | 135 | TableExprNode theset(mathutil::toVectorString( (*it1).second )); | 
|---|
|  | 136 | if ( query.isNull() ) { | 
|---|
|  | 137 | query = tab.col((*it1).first).in(theset); | 
|---|
|  | 138 | } else { | 
|---|
|  | 139 | query = tab.col((*it1).first).in(theset) && query; | 
|---|
|  | 140 | } | 
|---|
|  | 141 | } | 
|---|
| [812] | 142 | // add taql query | 
|---|
|  | 143 | if ( taql_.size() > 0 ) { | 
|---|
|  | 144 | Table tmpt = tab; | 
|---|
|  | 145 |  | 
|---|
|  | 146 | if ( !query.isNull() ) { // taql and selection | 
|---|
|  | 147 | tmpt = tableCommand(taql_, tab(query)); | 
|---|
|  | 148 | } else { // taql only | 
|---|
|  | 149 | tmpt = tableCommand(taql_, tab); | 
|---|
|  | 150 | } | 
|---|
| [902] | 151 | return sort(tmpt); | 
|---|
| [812] | 152 | } else { | 
|---|
| [902] | 153 | if ( query.isNull() ) { | 
|---|
|  | 154 | return sort(tab); | 
|---|
|  | 155 | } else { | 
|---|
|  | 156 | return sort(tab(query)); | 
|---|
|  | 157 | } | 
|---|
| [812] | 158 | } | 
|---|
|  | 159 | } | 
|---|
|  | 160 |  | 
|---|
| [939] | 161 | std::vector< int > STSelector::getint( const std::string& key ) const | 
|---|
| [812] | 162 | { | 
|---|
| [850] | 163 | if (intselections_.count(key) > 0) { | 
|---|
| [869] | 164 | return  intselections_[key]; | 
|---|
| [812] | 165 | } | 
|---|
| [869] | 166 | return  std::vector<int>(); | 
|---|
| [812] | 167 | } | 
|---|
|  | 168 |  | 
|---|
| [939] | 169 | std::vector< int > STSelector::getScans( ) const | 
|---|
| [812] | 170 | { | 
|---|
| [850] | 171 | return getint("SCANNO"); | 
|---|
| [812] | 172 | } | 
|---|
|  | 173 |  | 
|---|
| [939] | 174 | std::vector< int > STSelector::getBeams( ) const | 
|---|
| [812] | 175 | { | 
|---|
| [850] | 176 | return getint("BEAMNO"); | 
|---|
| [812] | 177 | } | 
|---|
|  | 178 |  | 
|---|
| [939] | 179 | std::vector< int > STSelector::getIFs( ) const | 
|---|
| [812] | 180 | { | 
|---|
| [850] | 181 | return getint("IFNO"); | 
|---|
| [812] | 182 | } | 
|---|
|  | 183 |  | 
|---|
| [939] | 184 | std::vector< int > STSelector::getPols( ) const | 
|---|
| [812] | 185 | { | 
|---|
| [850] | 186 | return getint("POLNO"); | 
|---|
| [812] | 187 | } | 
|---|
|  | 188 |  | 
|---|
| [939] | 189 | std::vector< int > asap::STSelector::getCycles( ) const | 
|---|
| [842] | 190 | { | 
|---|
| [850] | 191 | return getint("CYCLENO"); | 
|---|
| [842] | 192 | } | 
|---|
|  | 193 |  | 
|---|
| [812] | 194 | std::string asap::STSelector::print( ) | 
|---|
|  | 195 | { | 
|---|
|  | 196 | ostringstream oss; | 
|---|
|  | 197 | oss.flags(std::ios_base::left); | 
|---|
|  | 198 | oss << setw(15) << "Selection:"; | 
|---|
|  | 199 | if ( empty() ) { | 
|---|
|  | 200 | oss << "none"; | 
|---|
|  | 201 | return String(oss); | 
|---|
|  | 202 | } | 
|---|
|  | 203 |  | 
|---|
| [850] | 204 | intidmap::const_iterator it = intselections_.begin(); | 
|---|
|  | 205 | while (it != intselections_.end()) { | 
|---|
|  | 206 | if ( it != intselections_.begin() ) | 
|---|
| [812] | 207 | oss << setw(15) << " "; | 
|---|
|  | 208 | oss << it->first << ": " << Vector<Int>(it->second); | 
|---|
|  | 209 | ++it; | 
|---|
| [850] | 210 | if ( it != intselections_.end() ) oss << endl; | 
|---|
| [812] | 211 | } | 
|---|
| [850] | 212 | stringidmap::const_iterator it1 = stringselections_.begin(); | 
|---|
|  | 213 | while (it1 != stringselections_.end()) { | 
|---|
|  | 214 | if ( it1 != stringselections_.begin() ) | 
|---|
|  | 215 | oss << setw(15) << " "; | 
|---|
|  | 216 | oss << it1->first << ": " << mathutil::toVectorString(it1->second); | 
|---|
|  | 217 | ++it1; | 
|---|
|  | 218 | if ( it1 != stringselections_.end() ) oss << endl; | 
|---|
|  | 219 | } | 
|---|
| [812] | 220 | if ( taql_.size() > 0 ) { | 
|---|
|  | 221 | oss << endl << setw(15) << "" << taql_; | 
|---|
|  | 222 | } | 
|---|
|  | 223 | return String(oss); | 
|---|
|  | 224 | } | 
|---|
|  | 225 |  | 
|---|
|  | 226 | bool asap::STSelector::empty( ) const | 
|---|
|  | 227 | { | 
|---|
| [850] | 228 | return (intselections_.empty() && taql_.size() == 0 ); | 
|---|
| [812] | 229 | } | 
|---|
| [902] | 230 |  | 
|---|
|  | 231 | casa::Table asap::STSelector::sort( const casa::Table & tab ) | 
|---|
|  | 232 | { | 
|---|
|  | 233 | if (order_.nelements() > 0) { | 
|---|
|  | 234 | Table t = tab.sort(order_); | 
|---|
|  | 235 | return t; | 
|---|
|  | 236 | } else { | 
|---|
|  | 237 | return tab; | 
|---|
|  | 238 | } | 
|---|
|  | 239 | } | 
|---|
|  | 240 |  | 
|---|
|  | 241 |  | 
|---|
|  | 242 | void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols ) | 
|---|
|  | 243 | { | 
|---|
|  | 244 | poltypes_.clear(); | 
|---|
|  | 245 | std::vector<int> polints; | 
|---|
| [995] | 246 | std::vector<std::string>::const_iterator strit; | 
|---|
|  | 247 | for (strit = pols.begin(); strit != pols.end(); ++strit) { | 
|---|
| [902] | 248 | std::pair<int, std::string> val; | 
|---|
|  | 249 | try { | 
|---|
|  | 250 | val = STPol::polFromString(*strit); | 
|---|
|  | 251 | } catch (AipsError& e) { | 
|---|
|  | 252 | poltypes_.clear(); | 
|---|
|  | 253 | throw(e); | 
|---|
|  | 254 | } | 
|---|
|  | 255 | polints.push_back(val.first); | 
|---|
|  | 256 | poltypes_.push_back(val.second); | 
|---|
|  | 257 | } | 
|---|
|  | 258 | setint("POLNO", polints); | 
|---|
|  | 259 | } | 
|---|
|  | 260 |  | 
|---|
| [939] | 261 | std::vector< std::string > asap::STSelector::getPolTypes( ) const | 
|---|
| [902] | 262 | { | 
|---|
|  | 263 | return poltypes_; | 
|---|
|  | 264 | } | 
|---|
| [939] | 265 |  | 
|---|
|  | 266 | std::vector<std::string> asap::STSelector::getSortOrder() const | 
|---|
|  | 267 | { | 
|---|
|  | 268 | std::vector<std::string> out; | 
|---|
|  | 269 | for (uInt i=0;i<order_.nelements(); ++i) | 
|---|
|  | 270 | out.push_back(order_[i]); | 
|---|
|  | 271 | return out; | 
|---|
|  | 272 | } | 
|---|