| 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> | 
|---|
| 14 | #include <tables/Tables/ExprNode.h> | 
|---|
| 15 | #include <casa/BasicSL/String.h> | 
|---|
| 16 | #include <casa/iostream.h> | 
|---|
| 17 | #include <casa/iomanip.h> | 
|---|
| 18 | #include <casa/Exceptions/Error.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include "MathUtils.h" | 
|---|
| 21 | #include "STPol.h" | 
|---|
| 22 | #include "STSelector.h" | 
|---|
| 23 |  | 
|---|
| 24 | using namespace asap; | 
|---|
| 25 | using namespace casa; | 
|---|
| 26 |  | 
|---|
| 27 | STSelector::STSelector() : | 
|---|
| 28 | taql_("") | 
|---|
| 29 | { | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | STSelector::STSelector( const STSelector&  other ) : | 
|---|
| 33 | intselections_(other.intselections_), | 
|---|
| 34 | stringselections_(other.stringselections_), | 
|---|
| 35 | poltypes_(other.poltypes_), | 
|---|
| 36 | order_(other.order_), | 
|---|
| 37 | taql_(other.taql_) | 
|---|
| 38 | { | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | STSelector& STSelector::operator=( const STSelector& other ) | 
|---|
| 42 | { | 
|---|
| 43 | if (&other != this) { | 
|---|
| 44 | this->intselections_ = other.intselections_; | 
|---|
| 45 | this->stringselections_ = other.stringselections_; | 
|---|
| 46 | this->taql_ = other.taql_; | 
|---|
| 47 | this->poltypes_ = other.poltypes_; | 
|---|
| 48 | this->order_ = other.order_; | 
|---|
| 49 | } | 
|---|
| 50 | return *this; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | STSelector::~STSelector() | 
|---|
| 54 | { | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | void STSelector::setScans( const std::vector< int >& scans ) | 
|---|
| 58 | { | 
|---|
| 59 | setint("SCANNO", scans); | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void STSelector::setBeams( const std::vector< int >& beams ) | 
|---|
| 63 | { | 
|---|
| 64 | setint("BEAMNO", beams); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | void STSelector::setIFs( const std::vector< int >& ifs ) | 
|---|
| 68 | { | 
|---|
| 69 | setint("IFNO", ifs); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void STSelector::setPolarizations( const std::vector< int >& pols ) | 
|---|
| 73 | { | 
|---|
| 74 | setint("POLNO", pols); | 
|---|
| 75 | poltypes_ = std::vector<std::string>(); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void asap::STSelector::setCycles( const std::vector< int >& cycs ) | 
|---|
| 79 | { | 
|---|
| 80 | setint("CYCLENO", cycs); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void asap::STSelector::setName( const std::string& sname ) | 
|---|
| 84 | { | 
|---|
| 85 | std::string sql = "SELECT from $1 WHERE SRCNAME == pattern('"+sname+"')"; | 
|---|
| 86 | setTaQL(sql); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | void STSelector::setint(const std::string& key, const std::vector< int >& val) | 
|---|
| 90 | { | 
|---|
| 91 | if ( val.size() > 0 ) { | 
|---|
| 92 | intselections_[key] = val; | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 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 |  | 
|---|
| 104 | void STSelector::setTaQL( const std::string& taql ) | 
|---|
| 105 | { | 
|---|
| 106 | taql_ = taql; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | void asap::STSelector::setSortOrder( const std::vector< std::string > & order ) | 
|---|
| 111 | { | 
|---|
| 112 | order_.resize(order.size(), True); | 
|---|
| 113 | for (unsigned int i=0;i<order.size();++i) { | 
|---|
| 114 | order_[i] = order[i]; | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | Table STSelector::apply( const Table& tab ) | 
|---|
| 119 | { | 
|---|
| 120 | if ( empty() ) { | 
|---|
| 121 | return sort(tab); | 
|---|
| 122 | } | 
|---|
| 123 | TableExprNode query; | 
|---|
| 124 | intidmap::const_iterator it; | 
|---|
| 125 | for (it = intselections_.begin(); it != intselections_.end(); ++it) { | 
|---|
| 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 | } | 
|---|
| 133 | stringidmap::const_iterator it1; | 
|---|
| 134 | for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) { | 
|---|
| 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 | } | 
|---|
| 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 | } | 
|---|
| 151 | return sort(tmpt); | 
|---|
| 152 | } else { | 
|---|
| 153 | if ( query.isNull() ) { | 
|---|
| 154 | return sort(tab); | 
|---|
| 155 | } else { | 
|---|
| 156 | return sort(tab(query)); | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | std::vector< int > STSelector::getint( const std::string& key ) const | 
|---|
| 162 | { | 
|---|
| 163 | if (intselections_.count(key) > 0) { | 
|---|
| 164 | return  intselections_[key]; | 
|---|
| 165 | } | 
|---|
| 166 | return  std::vector<int>(); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | std::vector< int > STSelector::getScans( ) const | 
|---|
| 170 | { | 
|---|
| 171 | return getint("SCANNO"); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | std::vector< int > STSelector::getBeams( ) const | 
|---|
| 175 | { | 
|---|
| 176 | return getint("BEAMNO"); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | std::vector< int > STSelector::getIFs( ) const | 
|---|
| 180 | { | 
|---|
| 181 | return getint("IFNO"); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | std::vector< int > STSelector::getPols( ) const | 
|---|
| 185 | { | 
|---|
| 186 | return getint("POLNO"); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | std::vector< int > asap::STSelector::getCycles( ) const | 
|---|
| 190 | { | 
|---|
| 191 | return getint("CYCLENO"); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 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 |  | 
|---|
| 204 | intidmap::const_iterator it = intselections_.begin(); | 
|---|
| 205 | while (it != intselections_.end()) { | 
|---|
| 206 | if ( it != intselections_.begin() ) | 
|---|
| 207 | oss << setw(15) << " "; | 
|---|
| 208 | oss << it->first << ": " << Vector<Int>(it->second); | 
|---|
| 209 | ++it; | 
|---|
| 210 | if ( it != intselections_.end() ) oss << endl; | 
|---|
| 211 | } | 
|---|
| 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 | } | 
|---|
| 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 | { | 
|---|
| 228 | return (intselections_.empty() && taql_.size() == 0 ); | 
|---|
| 229 | } | 
|---|
| 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; | 
|---|
| 246 | std::vector<std::string>::const_iterator strit; | 
|---|
| 247 | for (strit = pols.begin(); strit != pols.end(); ++strit) { | 
|---|
| 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 |  | 
|---|
| 261 | std::vector< std::string > asap::STSelector::getPolTypes( ) const | 
|---|
| 262 | { | 
|---|
| 263 | return poltypes_; | 
|---|
| 264 | } | 
|---|
| 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 | } | 
|---|