[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 | taql_(other.taql_),
|
---|
| 36 | poltypes_(other.poltypes_),
|
---|
| 37 | order_(other.order_) {
|
---|
[812] | 38 | }
|
---|
| 39 |
|
---|
| 40 | STSelector& STSelector::operator=( const STSelector& other )
|
---|
| 41 | {
|
---|
| 42 | if (&other != this) {
|
---|
[850] | 43 | this->intselections_ = other.intselections_;
|
---|
| 44 | this->stringselections_ = other.stringselections_;
|
---|
[812] | 45 | this->taql_ = other.taql_;
|
---|
| 46 | }
|
---|
| 47 | return *this;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | STSelector::~STSelector()
|
---|
| 51 | {
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | void STSelector::setScans( const std::vector< int >& scans )
|
---|
| 55 | {
|
---|
[850] | 56 | setint("SCANNO", scans);
|
---|
[812] | 57 | }
|
---|
| 58 |
|
---|
| 59 | void STSelector::setBeams( const std::vector< int >& beams )
|
---|
| 60 | {
|
---|
[850] | 61 | setint("BEAMNO", beams);
|
---|
[812] | 62 | }
|
---|
| 63 |
|
---|
| 64 | void STSelector::setIFs( const std::vector< int >& ifs )
|
---|
| 65 | {
|
---|
[850] | 66 | setint("IFNO", ifs);
|
---|
[812] | 67 | }
|
---|
| 68 |
|
---|
| 69 | void STSelector::setPolarizations( const std::vector< int >& pols )
|
---|
| 70 | {
|
---|
[850] | 71 | setint("POLNO", pols);
|
---|
[812] | 72 | }
|
---|
| 73 |
|
---|
[842] | 74 | void asap::STSelector::setCycles( const std::vector< int >& cycs )
|
---|
| 75 | {
|
---|
[850] | 76 | setint("CYCLENO", cycs);
|
---|
[842] | 77 | }
|
---|
| 78 |
|
---|
[850] | 79 | void asap::STSelector::setName( const std::string& sname )
|
---|
[812] | 80 | {
|
---|
[850] | 81 | std::string sql = "SELECT from $1 WHERE SRCNAME == pattern('"+sname+"')";
|
---|
| 82 | setTaQL(sql);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void STSelector::setint(const std::string& key, const std::vector< int >& val)
|
---|
| 86 | {
|
---|
[812] | 87 | if ( val.size() > 0 ) {
|
---|
[850] | 88 | intselections_[key] = val;
|
---|
[812] | 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[850] | 92 | void STSelector::setstring( const std::string& key,
|
---|
| 93 | const std::vector<std::string>& val )
|
---|
| 94 | {
|
---|
| 95 | if ( val.size() > 0 ) {
|
---|
| 96 | stringselections_[key] = val;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[812] | 100 | void STSelector::setTaQL( const std::string& taql )
|
---|
| 101 | {
|
---|
| 102 | taql_ = taql;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[902] | 105 |
|
---|
| 106 | void asap::STSelector::setSortOrder( const std::vector< std::string > & order )
|
---|
| 107 | {
|
---|
| 108 | order_.resize(order.size(), True);
|
---|
| 109 | for (int i=0;i<order.size();++i) {
|
---|
| 110 | order_[i] = order[i];
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[812] | 114 | Table STSelector::apply( const Table& tab )
|
---|
| 115 | {
|
---|
| 116 | if ( empty() ) {
|
---|
[902] | 117 | return sort(tab);
|
---|
[812] | 118 | }
|
---|
| 119 | TableExprNode query;
|
---|
[850] | 120 | intidmap::const_iterator it = intselections_.begin();
|
---|
| 121 | for (it; it != intselections_.end(); ++it) {
|
---|
[812] | 122 | TableExprNode theset(Vector<Int>( (*it).second ));
|
---|
| 123 | if ( query.isNull() ) {
|
---|
| 124 | query = tab.col((*it).first).in(theset);
|
---|
| 125 | } else {
|
---|
| 126 | query = tab.col((*it).first).in(theset) && query;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
[850] | 129 | stringidmap::const_iterator it1 = stringselections_.begin();
|
---|
| 130 | for (it1; it1 != stringselections_.end(); ++it1) {
|
---|
| 131 | TableExprNode theset(mathutil::toVectorString( (*it1).second ));
|
---|
| 132 | if ( query.isNull() ) {
|
---|
| 133 | query = tab.col((*it1).first).in(theset);
|
---|
| 134 | } else {
|
---|
| 135 | query = tab.col((*it1).first).in(theset) && query;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
[812] | 138 | // add taql query
|
---|
| 139 | if ( taql_.size() > 0 ) {
|
---|
| 140 | Table tmpt = tab;
|
---|
| 141 |
|
---|
| 142 | if ( !query.isNull() ) { // taql and selection
|
---|
| 143 | tmpt = tableCommand(taql_, tab(query));
|
---|
| 144 | } else { // taql only
|
---|
| 145 | tmpt = tableCommand(taql_, tab);
|
---|
| 146 | }
|
---|
[902] | 147 | return sort(tmpt);
|
---|
[812] | 148 | } else {
|
---|
[902] | 149 | if ( query.isNull() ) {
|
---|
| 150 | return sort(tab);
|
---|
| 151 | } else {
|
---|
| 152 | return sort(tab(query));
|
---|
| 153 | }
|
---|
[812] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[939] | 157 | std::vector< int > STSelector::getint( const std::string& key ) const
|
---|
[812] | 158 | {
|
---|
[850] | 159 | if (intselections_.count(key) > 0) {
|
---|
[869] | 160 | return intselections_[key];
|
---|
[812] | 161 | }
|
---|
[869] | 162 | return std::vector<int>();
|
---|
[812] | 163 | }
|
---|
| 164 |
|
---|
[939] | 165 | std::vector< int > STSelector::getScans( ) const
|
---|
[812] | 166 | {
|
---|
[850] | 167 | return getint("SCANNO");
|
---|
[812] | 168 | }
|
---|
| 169 |
|
---|
[939] | 170 | std::vector< int > STSelector::getBeams( ) const
|
---|
[812] | 171 | {
|
---|
[850] | 172 | return getint("BEAMNO");
|
---|
[812] | 173 | }
|
---|
| 174 |
|
---|
[939] | 175 | std::vector< int > STSelector::getIFs( ) const
|
---|
[812] | 176 | {
|
---|
[850] | 177 | return getint("IFNO");
|
---|
[812] | 178 | }
|
---|
| 179 |
|
---|
[939] | 180 | std::vector< int > STSelector::getPols( ) const
|
---|
[812] | 181 | {
|
---|
[850] | 182 | return getint("POLNO");
|
---|
[812] | 183 | }
|
---|
| 184 |
|
---|
[939] | 185 | std::vector< int > asap::STSelector::getCycles( ) const
|
---|
[842] | 186 | {
|
---|
[850] | 187 | return getint("CYCLENO");
|
---|
[842] | 188 | }
|
---|
| 189 |
|
---|
[812] | 190 | std::string asap::STSelector::print( )
|
---|
| 191 | {
|
---|
| 192 | ostringstream oss;
|
---|
| 193 | oss.flags(std::ios_base::left);
|
---|
| 194 | oss << setw(15) << "Selection:";
|
---|
| 195 | if ( empty() ) {
|
---|
| 196 | oss << "none";
|
---|
| 197 | return String(oss);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[850] | 200 | intidmap::const_iterator it = intselections_.begin();
|
---|
| 201 | while (it != intselections_.end()) {
|
---|
| 202 | if ( it != intselections_.begin() )
|
---|
[812] | 203 | oss << setw(15) << " ";
|
---|
| 204 | oss << it->first << ": " << Vector<Int>(it->second);
|
---|
| 205 | ++it;
|
---|
[850] | 206 | if ( it != intselections_.end() ) oss << endl;
|
---|
[812] | 207 | }
|
---|
[850] | 208 | stringidmap::const_iterator it1 = stringselections_.begin();
|
---|
| 209 | while (it1 != stringselections_.end()) {
|
---|
| 210 | if ( it1 != stringselections_.begin() )
|
---|
| 211 | oss << setw(15) << " ";
|
---|
| 212 | oss << it1->first << ": " << mathutil::toVectorString(it1->second);
|
---|
| 213 | ++it1;
|
---|
| 214 | if ( it1 != stringselections_.end() ) oss << endl;
|
---|
| 215 | }
|
---|
[812] | 216 | if ( taql_.size() > 0 ) {
|
---|
| 217 | oss << endl << setw(15) << "" << taql_;
|
---|
| 218 | }
|
---|
| 219 | return String(oss);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | bool asap::STSelector::empty( ) const
|
---|
| 223 | {
|
---|
[850] | 224 | return (intselections_.empty() && taql_.size() == 0 );
|
---|
[812] | 225 | }
|
---|
[902] | 226 |
|
---|
| 227 | casa::Table asap::STSelector::sort( const casa::Table & tab )
|
---|
| 228 | {
|
---|
| 229 | if (order_.nelements() > 0) {
|
---|
| 230 | Table t = tab.sort(order_);
|
---|
| 231 | return t;
|
---|
| 232 | } else {
|
---|
| 233 | return tab;
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 | void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols )
|
---|
| 239 | {
|
---|
| 240 | poltypes_.clear();
|
---|
| 241 | std::vector<int> polints;
|
---|
| 242 | std::vector<std::string>::const_iterator strit = pols.begin();
|
---|
| 243 | for (strit; strit != pols.end(); ++strit) {
|
---|
| 244 | std::pair<int, std::string> val;
|
---|
| 245 | try {
|
---|
| 246 | val = STPol::polFromString(*strit);
|
---|
| 247 | } catch (AipsError& e) {
|
---|
| 248 | poltypes_.clear();
|
---|
| 249 | throw(e);
|
---|
| 250 | }
|
---|
| 251 | polints.push_back(val.first);
|
---|
| 252 | poltypes_.push_back(val.second);
|
---|
| 253 | }
|
---|
| 254 | setint("POLNO", polints);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[939] | 257 | std::vector< std::string > asap::STSelector::getPolTypes( ) const
|
---|
[902] | 258 | {
|
---|
| 259 | return poltypes_;
|
---|
| 260 | }
|
---|
[939] | 261 |
|
---|
| 262 | std::vector<std::string> asap::STSelector::getSortOrder() const
|
---|
| 263 | {
|
---|
| 264 | std::vector<std::string> out;
|
---|
| 265 | for (uInt i=0;i<order_.nelements(); ++i)
|
---|
| 266 | out.push_back(order_[i]);
|
---|
| 267 | return out;
|
---|
| 268 | }
|
---|