| 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 |     std::string pytaql = "USING STYLE PYTHON " + taql_;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |     if ( !query.isNull() ) { // taql and selection
 | 
|---|
| 148 |       tmpt = tableCommand(pytaql, tab(query));
 | 
|---|
| 149 |     } else { // taql only
 | 
|---|
| 150 |       tmpt = tableCommand(pytaql, tab);
 | 
|---|
| 151 |     }
 | 
|---|
| 152 |     return sort(tmpt);
 | 
|---|
| 153 |   } else {
 | 
|---|
| 154 |     if ( query.isNull() ) {
 | 
|---|
| 155 |       return sort(tab);
 | 
|---|
| 156 |     } else {
 | 
|---|
| 157 |       return sort(tab(query));
 | 
|---|
| 158 |     }
 | 
|---|
| 159 |   }
 | 
|---|
| 160 | }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | std::vector< int > STSelector::getint( const std::string& key ) const
 | 
|---|
| 163 | {
 | 
|---|
| 164 |   if (intselections_.count(key) > 0) {
 | 
|---|
| 165 |     return  intselections_[key];
 | 
|---|
| 166 |   }
 | 
|---|
| 167 |   return  std::vector<int>();
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | std::vector< int > STSelector::getScans( ) const
 | 
|---|
| 171 | {
 | 
|---|
| 172 |   return getint("SCANNO");
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | std::vector< int > STSelector::getBeams( ) const
 | 
|---|
| 176 | {
 | 
|---|
| 177 |   return getint("BEAMNO");
 | 
|---|
| 178 | }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | std::vector< int > STSelector::getIFs( ) const
 | 
|---|
| 181 | {
 | 
|---|
| 182 |   return getint("IFNO");
 | 
|---|
| 183 | }
 | 
|---|
| 184 | 
 | 
|---|
| 185 | std::vector< int > STSelector::getPols( ) const
 | 
|---|
| 186 | {
 | 
|---|
| 187 |   return getint("POLNO");
 | 
|---|
| 188 | }
 | 
|---|
| 189 | 
 | 
|---|
| 190 | std::vector< int > asap::STSelector::getCycles( ) const
 | 
|---|
| 191 | {
 | 
|---|
| 192 |   return getint("CYCLENO");
 | 
|---|
| 193 | }
 | 
|---|
| 194 | 
 | 
|---|
| 195 | std::string asap::STSelector::print( )
 | 
|---|
| 196 | {
 | 
|---|
| 197 |   ostringstream oss;
 | 
|---|
| 198 |   oss.flags(std::ios_base::left);
 | 
|---|
| 199 |   oss << setw(15) << "Selection:";
 | 
|---|
| 200 |   if ( empty() ) {
 | 
|---|
| 201 |     oss << "none";
 | 
|---|
| 202 |     return String(oss);
 | 
|---|
| 203 |   }
 | 
|---|
| 204 | 
 | 
|---|
| 205 |   intidmap::const_iterator it = intselections_.begin();
 | 
|---|
| 206 |   while (it != intselections_.end()) {
 | 
|---|
| 207 |     if ( it != intselections_.begin() )
 | 
|---|
| 208 |       oss << setw(15) << " ";
 | 
|---|
| 209 |     oss << it->first << ": " << Vector<Int>(it->second);
 | 
|---|
| 210 |     ++it;
 | 
|---|
| 211 |     if ( it != intselections_.end() ) oss << endl;
 | 
|---|
| 212 |   }
 | 
|---|
| 213 |   stringidmap::const_iterator it1 = stringselections_.begin();
 | 
|---|
| 214 |   while (it1 != stringselections_.end()) {
 | 
|---|
| 215 |     if ( it1 != stringselections_.begin() )
 | 
|---|
| 216 |       oss << setw(15) << " ";
 | 
|---|
| 217 |     oss << it1->first << ": " << mathutil::toVectorString(it1->second);
 | 
|---|
| 218 |     ++it1;
 | 
|---|
| 219 |     if ( it1 != stringselections_.end() ) oss << endl;
 | 
|---|
| 220 |   }
 | 
|---|
| 221 |   if ( taql_.size() > 0 ) {
 | 
|---|
| 222 |     oss << endl << setw(15) << "" << taql_;
 | 
|---|
| 223 |   }
 | 
|---|
| 224 |   return String(oss);
 | 
|---|
| 225 | }
 | 
|---|
| 226 | 
 | 
|---|
| 227 | bool asap::STSelector::empty( ) const
 | 
|---|
| 228 | {
 | 
|---|
| 229 |   return (intselections_.empty() && taql_.size() == 0 );
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | casa::Table asap::STSelector::sort( const casa::Table & tab )
 | 
|---|
| 233 | {
 | 
|---|
| 234 |   if (order_.nelements() > 0) {
 | 
|---|
| 235 |     Table t = tab.sort(order_);
 | 
|---|
| 236 |     return t;
 | 
|---|
| 237 |   } else {
 | 
|---|
| 238 |     return tab;
 | 
|---|
| 239 |   }
 | 
|---|
| 240 | }
 | 
|---|
| 241 | 
 | 
|---|
| 242 | 
 | 
|---|
| 243 | void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols )
 | 
|---|
| 244 | {
 | 
|---|
| 245 |   poltypes_.clear();
 | 
|---|
| 246 |   std::vector<int> polints;
 | 
|---|
| 247 |   std::vector<std::string>::const_iterator strit;
 | 
|---|
| 248 |   for (strit = pols.begin(); strit != pols.end(); ++strit) {
 | 
|---|
| 249 |     std::pair<int, std::string> val;
 | 
|---|
| 250 |     try {
 | 
|---|
| 251 |        val = STPol::polFromString(*strit);
 | 
|---|
| 252 |     } catch (AipsError& e) {
 | 
|---|
| 253 |       poltypes_.clear();
 | 
|---|
| 254 |       throw(e);
 | 
|---|
| 255 |     }
 | 
|---|
| 256 |     polints.push_back(val.first);
 | 
|---|
| 257 |     poltypes_.push_back(val.second);
 | 
|---|
| 258 |   }
 | 
|---|
| 259 |   setint("POLNO", polints);
 | 
|---|
| 260 | }
 | 
|---|
| 261 | 
 | 
|---|
| 262 | std::vector< std::string > asap::STSelector::getPolTypes( ) const
 | 
|---|
| 263 | {
 | 
|---|
| 264 |   return poltypes_;
 | 
|---|
| 265 | }
 | 
|---|
| 266 | 
 | 
|---|
| 267 | std::vector<std::string> asap::STSelector::getSortOrder() const
 | 
|---|
| 268 | {
 | 
|---|
| 269 |   std::vector<std::string> out;
 | 
|---|
| 270 |   for (uInt i=0;i<order_.nelements(); ++i)
 | 
|---|
| 271 |     out.push_back(order_[i]);
 | 
|---|
| 272 |   return out;
 | 
|---|
| 273 | }
 | 
|---|