source: trunk/src/STSelector.cpp @ 975

Last change on this file since 975 was 954, checked in by mar637, 18 years ago

Fixed Ticket #6 (selector.set_polarisations error)

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