source: branches/alma/src/STSelector.cpp @ 1639

Last change on this file since 1639 was 1639, checked in by Kana Sugimoto, 15 years ago

New Development: Yes

JIRA Issue: Yes (CAS-1429)

Ready to Release: Yes

Interface Changes: Yes

What Interface Changed:
A new python method, selector.set_rows(rownrs=[]),
and a cpp function,
STSelector::setRows( const std::vector< int >& rows ),
are added.
The cpp function can be called with selector._setrows()
from python scripts.

Test Programs:

# @casa
s=sd.scantable('ORIGINAL_TABLE_NAME', False)
sel=sd.selector()
rownrs=[1,3,5,7]
sel.set_rows(rownrs)
s.set_selection(sel)
s2=s.copy()
s2.save(name='NEW_TABLE_NAME',format='ASAP',overwrite=True)

Put in Release Notes: Yes

Module(s):

Description:

Data selection by a list of row numbers can be done by
the new method selector.set_rows.

File size: 8.3 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  poltypes_(other.poltypes_),
[995]36  order_(other.order_),
37  taql_(other.taql_)
38{
[812]39}
40
41STSelector& 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
53STSelector::~STSelector()
54{
55}
56
57void STSelector::setScans( const std::vector< int >& scans )
58{
[850]59  setint("SCANNO", scans);
[812]60}
61
62void STSelector::setBeams( const std::vector< int >& beams )
63{
[850]64  setint("BEAMNO", beams);
[812]65}
66
67void STSelector::setIFs( const std::vector< int >& ifs )
68{
[850]69  setint("IFNO", ifs);
[812]70}
71
72void STSelector::setPolarizations( const std::vector< int >& pols )
73{
[850]74  setint("POLNO", pols);
[954]75  poltypes_ = std::vector<std::string>();
[812]76}
77
[842]78void asap::STSelector::setCycles( const std::vector< int >& cycs )
79{
[850]80  setint("CYCLENO", cycs);
[842]81}
82
[850]83void asap::STSelector::setName( const std::string& sname )
[812]84{
[1387]85  std::string sql = "SELECT FROM $1 WHERE SRCNAME == pattern('"+sname+"')";
[850]86  setTaQL(sql);
87}
88
89void 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]96void 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]104void STSelector::setTaQL( const std::string& taql )
105{
106  taql_ = taql;
107}
108
[902]109
110void 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
[1639]118void STSelector::setRows( const std::vector< int >& rows )
119{
120  rowselection_ = rows;
121}
122
123// Table STSelector::apply( const Table& tab )
124// {
125//   if ( empty() ) {
126//     return sort(tab);
127//   }
128//   TableExprNode query;
129//   intidmap::const_iterator it;
130//   for (it = intselections_.begin(); it != intselections_.end(); ++it) {
131//     TableExprNode theset(Vector<Int>( (*it).second ));
132//     if ( query.isNull() ) {
133//       query = tab.col((*it).first).in(theset);
134//     } else {
135//       query = tab.col((*it).first).in(theset) && query;
136//     }
137//   }
138//   stringidmap::const_iterator it1;
139//   for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
140//     TableExprNode theset(mathutil::toVectorString( (*it1).second ));
141//     if ( query.isNull() ) {
142//       query = tab.col((*it1).first).in(theset);
143//     } else {
144//       query = tab.col((*it1).first).in(theset) && query;
145//     }
146//   }
147//   // add taql query
148//   if ( taql_.size() > 0 ) {
149//     Table tmpt = tab;
150//     std::string pytaql = "USING STYLE PYTHON " + taql_;
151
152//     if ( !query.isNull() ) { // taql and selection
153//       tmpt = tableCommand(pytaql, tab(query));
154//     } else { // taql only
155//       tmpt = tableCommand(pytaql, tab);
156//     }
157//     return sort(tmpt);
158//   } else {
159//     if ( query.isNull() ) {
160//       return sort(tab);
161//     } else {
162//       return sort(tab(query));
163//     }
164//   }
165// }
[812]166Table STSelector::apply( const Table& tab )
167{
168  if ( empty() ) {
[902]169    return sort(tab);
[812]170  }
[1639]171  Table basetab = tab;
172  // Important!! Be sure to apply row selection first.
173  if (rowselection_.size() > 0){
174    //Vector<Int> intrownrs(rowselection_);
175    Vector<uInt> rownrs( rowselection_.size() );
176    convertArray(rownrs, Vector<Int> ( rowselection_ ));
177    basetab = tab( rownrs );
178    ///TableExprNode theset(Vector<Int>( rowselection_ ));
179    ///query = tab.nodeRownr().in(theset);
180  }
[812]181  TableExprNode query;
[995]182  intidmap::const_iterator it;
[1000]183  for (it = intselections_.begin(); it != intselections_.end(); ++it) {
[812]184    TableExprNode theset(Vector<Int>( (*it).second ));
185    if ( query.isNull() ) {
[1639]186      //query = tab.col((*it).first).in(theset);
187      query = basetab.col((*it).first).in(theset);
[812]188    } else {
[1639]189      //query = tab.col((*it).first).in(theset) && query;
190      query = basetab.col((*it).first).in(theset) && query;
[812]191    }
192  }
[995]193  stringidmap::const_iterator it1;
194  for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
[850]195    TableExprNode theset(mathutil::toVectorString( (*it1).second ));
196    if ( query.isNull() ) {
[1639]197      //query = tab.col((*it1).first).in(theset);
198      query = basetab.col((*it1).first).in(theset);
[850]199    } else {
[1639]200      //query = tab.col((*it1).first).in(theset) && query;
201      query = basetab.col((*it1).first).in(theset) && query;
[850]202    }
203  }
[812]204  // add taql query
205  if ( taql_.size() > 0 ) {
[1639]206    //Table tmpt = tab;
207    Table tmpt = basetab;
[1603]208    std::string pytaql = "USING STYLE PYTHON " + taql_;
[812]209
210    if ( !query.isNull() ) { // taql and selection
[1639]211      //tmpt = tableCommand(pytaql, tab(query));
212      tmpt = tableCommand(pytaql, basetab(query));
[812]213    } else { // taql only
[1639]214      //tmpt = tableCommand(pytaql, tab);
215      tmpt = tableCommand(pytaql, basetab);
[812]216    }
[902]217    return sort(tmpt);
[812]218  } else {
[902]219    if ( query.isNull() ) {
[1639]220      //return sort(tab);
221      return sort(basetab);
[902]222    } else {
[1639]223      //return sort(tab(query));
224      return sort(basetab(query));
[902]225    }
[812]226  }
227}
228
[939]229std::vector< int > STSelector::getint( const std::string& key ) const
[812]230{
[850]231  if (intselections_.count(key) > 0) {
[869]232    return  intselections_[key];
[812]233  }
[869]234  return  std::vector<int>();
[812]235}
236
[939]237std::vector< int > STSelector::getScans( ) const
[812]238{
[850]239  return getint("SCANNO");
[812]240}
241
[939]242std::vector< int > STSelector::getBeams( ) const
[812]243{
[850]244  return getint("BEAMNO");
[812]245}
246
[939]247std::vector< int > STSelector::getIFs( ) const
[812]248{
[850]249  return getint("IFNO");
[812]250}
251
[939]252std::vector< int > STSelector::getPols( ) const
[812]253{
[850]254  return getint("POLNO");
[812]255}
256
[939]257std::vector< int > asap::STSelector::getCycles( ) const
[842]258{
[850]259  return getint("CYCLENO");
[842]260}
261
[812]262std::string asap::STSelector::print( )
263{
264  ostringstream oss;
265  oss.flags(std::ios_base::left);
266  oss << setw(15) << "Selection:";
267  if ( empty() ) {
268    oss << "none";
269    return String(oss);
270  }
271
[850]272  intidmap::const_iterator it = intselections_.begin();
273  while (it != intselections_.end()) {
274    if ( it != intselections_.begin() )
[812]275      oss << setw(15) << " ";
276    oss << it->first << ": " << Vector<Int>(it->second);
277    ++it;
[850]278    if ( it != intselections_.end() ) oss << endl;
[812]279  }
[850]280  stringidmap::const_iterator it1 = stringselections_.begin();
281  while (it1 != stringselections_.end()) {
282    if ( it1 != stringselections_.begin() )
283      oss << setw(15) << " ";
284    oss << it1->first << ": " << mathutil::toVectorString(it1->second);
285    ++it1;
286    if ( it1 != stringselections_.end() ) oss << endl;
287  }
[812]288  if ( taql_.size() > 0 ) {
289    oss << endl << setw(15) << "" << taql_;
290  }
291  return String(oss);
292}
293
294bool asap::STSelector::empty( ) const
295{
[1639]296  //return (intselections_.empty() && taql_.size() == 0 );
297  return (intselections_.empty() && taql_.size() == 0 && rowselection_.size() == 0);
[812]298}
[902]299
300casa::Table asap::STSelector::sort( const casa::Table & tab )
301{
302  if (order_.nelements() > 0) {
303    Table t = tab.sort(order_);
304    return t;
305  } else {
306    return tab;
307  }
308}
309
310
311void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols )
312{
313  poltypes_.clear();
314  std::vector<int> polints;
[995]315  std::vector<std::string>::const_iterator strit;
316  for (strit = pols.begin(); strit != pols.end(); ++strit) {
[902]317    std::pair<int, std::string> val;
318    try {
319       val = STPol::polFromString(*strit);
320    } catch (AipsError& e) {
321      poltypes_.clear();
322      throw(e);
323    }
324    polints.push_back(val.first);
325    poltypes_.push_back(val.second);
326  }
327  setint("POLNO", polints);
328}
329
[939]330std::vector< std::string > asap::STSelector::getPolTypes( ) const
[902]331{
332  return poltypes_;
333}
[939]334
335std::vector<std::string> asap::STSelector::getSortOrder() const
336{
337  std::vector<std::string> out;
338  for (uInt i=0;i<order_.nelements(); ++i)
339    out.push_back(order_[i]);
340  return out;
341}
Note: See TracBrowser for help on using the repository browser.