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
Line 
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
24using namespace asap;
25using namespace casa;
26
27STSelector::STSelector() :
28  taql_("")
29{
30}
31
32STSelector::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
41STSelector& 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
53STSelector::~STSelector()
54{
55}
56
57void STSelector::setScans( const std::vector< int >& scans )
58{
59  setint("SCANNO", scans);
60}
61
62void STSelector::setBeams( const std::vector< int >& beams )
63{
64  setint("BEAMNO", beams);
65}
66
67void STSelector::setIFs( const std::vector< int >& ifs )
68{
69  setint("IFNO", ifs);
70}
71
72void STSelector::setPolarizations( const std::vector< int >& pols )
73{
74  setint("POLNO", pols);
75  poltypes_ = std::vector<std::string>();
76}
77
78void asap::STSelector::setCycles( const std::vector< int >& cycs )
79{
80  setint("CYCLENO", cycs);
81}
82
83void asap::STSelector::setName( const std::string& sname )
84{
85  std::string sql = "SELECT FROM $1 WHERE SRCNAME == pattern('"+sname+"')";
86  setTaQL(sql);
87}
88
89void STSelector::setint(const std::string& key, const std::vector< int >& val)
90{
91  if ( val.size() > 0 ) {
92    intselections_[key] = val;
93  }
94}
95
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
104void STSelector::setTaQL( const std::string& taql )
105{
106  taql_ = taql;
107}
108
109
110void 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
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// }
166Table STSelector::apply( const Table& tab )
167{
168  if ( empty() ) {
169    return sort(tab);
170  }
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  }
181  TableExprNode query;
182  intidmap::const_iterator it;
183  for (it = intselections_.begin(); it != intselections_.end(); ++it) {
184    TableExprNode theset(Vector<Int>( (*it).second ));
185    if ( query.isNull() ) {
186      //query = tab.col((*it).first).in(theset);
187      query = basetab.col((*it).first).in(theset);
188    } else {
189      //query = tab.col((*it).first).in(theset) && query;
190      query = basetab.col((*it).first).in(theset) && query;
191    }
192  }
193  stringidmap::const_iterator it1;
194  for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
195    TableExprNode theset(mathutil::toVectorString( (*it1).second ));
196    if ( query.isNull() ) {
197      //query = tab.col((*it1).first).in(theset);
198      query = basetab.col((*it1).first).in(theset);
199    } else {
200      //query = tab.col((*it1).first).in(theset) && query;
201      query = basetab.col((*it1).first).in(theset) && query;
202    }
203  }
204  // add taql query
205  if ( taql_.size() > 0 ) {
206    //Table tmpt = tab;
207    Table tmpt = basetab;
208    std::string pytaql = "USING STYLE PYTHON " + taql_;
209
210    if ( !query.isNull() ) { // taql and selection
211      //tmpt = tableCommand(pytaql, tab(query));
212      tmpt = tableCommand(pytaql, basetab(query));
213    } else { // taql only
214      //tmpt = tableCommand(pytaql, tab);
215      tmpt = tableCommand(pytaql, basetab);
216    }
217    return sort(tmpt);
218  } else {
219    if ( query.isNull() ) {
220      //return sort(tab);
221      return sort(basetab);
222    } else {
223      //return sort(tab(query));
224      return sort(basetab(query));
225    }
226  }
227}
228
229std::vector< int > STSelector::getint( const std::string& key ) const
230{
231  if (intselections_.count(key) > 0) {
232    return  intselections_[key];
233  }
234  return  std::vector<int>();
235}
236
237std::vector< int > STSelector::getScans( ) const
238{
239  return getint("SCANNO");
240}
241
242std::vector< int > STSelector::getBeams( ) const
243{
244  return getint("BEAMNO");
245}
246
247std::vector< int > STSelector::getIFs( ) const
248{
249  return getint("IFNO");
250}
251
252std::vector< int > STSelector::getPols( ) const
253{
254  return getint("POLNO");
255}
256
257std::vector< int > asap::STSelector::getCycles( ) const
258{
259  return getint("CYCLENO");
260}
261
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
272  intidmap::const_iterator it = intselections_.begin();
273  while (it != intselections_.end()) {
274    if ( it != intselections_.begin() )
275      oss << setw(15) << " ";
276    oss << it->first << ": " << Vector<Int>(it->second);
277    ++it;
278    if ( it != intselections_.end() ) oss << endl;
279  }
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  }
288  if ( taql_.size() > 0 ) {
289    oss << endl << setw(15) << "" << taql_;
290  }
291  return String(oss);
292}
293
294bool asap::STSelector::empty( ) const
295{
296  //return (intselections_.empty() && taql_.size() == 0 );
297  return (intselections_.empty() && taql_.size() == 0 && rowselection_.size() == 0);
298}
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;
315  std::vector<std::string>::const_iterator strit;
316  for (strit = pols.begin(); strit != pols.end(); ++strit) {
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
330std::vector< std::string > asap::STSelector::getPolTypes( ) const
331{
332  return poltypes_;
333}
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.