source: branches/mergetest/src/STSelector.cpp @ 1779

Last change on this file since 1779 was 1779, checked in by Kana Sugimoto, 14 years ago

New Development: Yes

JIRA Issue: No (test merging alma branch)

Ready for Test: Yes

Interface Changes: Yes

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s):

Description:


File size: 8.5 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::setTypes( const std::vector< int >& types )
90{
91  setint("SRCTYPE", types);
92}
93
94void STSelector::setint(const std::string& key, const std::vector< int >& val)
95{
96  if ( val.size() > 0 ) {
97    intselections_[key] = val;
98  }
99}
100
101void STSelector::setstring( const std::string& key,
102                            const std::vector<std::string>& val )
103{
104  if ( val.size() > 0 ) {
105    stringselections_[key] = val;
106  }
107}
108
109void STSelector::setTaQL( const std::string& taql )
110{
111  taql_ = taql;
112}
113
114
115void asap::STSelector::setSortOrder( const std::vector< std::string > & order )
116{
117  order_.resize(order.size(), True);
118  for (unsigned int i=0;i<order.size();++i) {
119    order_[i] = order[i];
120  }
121}
122
123void STSelector::setRows( const std::vector< int >& rows )
124{
125  rowselection_ = rows;
126}
127
128// Table STSelector::apply( const Table& tab )
129// {
130//   if ( empty() ) {
131//     return sort(tab);
132//   }
133//   TableExprNode query;
134//   intidmap::const_iterator it;
135//   for (it = intselections_.begin(); it != intselections_.end(); ++it) {
136//     TableExprNode theset(Vector<Int>( (*it).second ));
137//     if ( query.isNull() ) {
138//       query = tab.col((*it).first).in(theset);
139//     } else {
140//       query = tab.col((*it).first).in(theset) && query;
141//     }
142//   }
143//   stringidmap::const_iterator it1;
144//   for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
145//     TableExprNode theset(mathutil::toVectorString( (*it1).second ));
146//     if ( query.isNull() ) {
147//       query = tab.col((*it1).first).in(theset);
148//     } else {
149//       query = tab.col((*it1).first).in(theset) && query;
150//     }
151//   }
152//   // add taql query
153//   if ( taql_.size() > 0 ) {
154//     Table tmpt = tab;
155//     std::string pytaql = "USING STYLE PYTHON " + taql_;
156
157//     if ( !query.isNull() ) { // taql and selection
158//       tmpt = tableCommand(pytaql, tab(query));
159//     } else { // taql only
160//       tmpt = tableCommand(pytaql, tab);
161//     }
162//     return sort(tmpt);
163//   } else {
164//     if ( query.isNull() ) {
165//       return sort(tab);
166//     } else {
167//       return sort(tab(query));
168//     }
169//   }
170// }
171Table STSelector::apply( const Table& tab )
172{
173  if ( empty() ) {
174    return sort(tab);
175  }
176  Table basetab = tab;
177  // Important!! Be sure to apply row selection first.
178  if (rowselection_.size() > 0){
179    //Vector<Int> intrownrs(rowselection_);
180    Vector<uInt> rownrs( rowselection_.size() );
181    convertArray(rownrs, Vector<Int> ( rowselection_ ));
182    basetab = tab( rownrs );
183    ///TableExprNode theset(Vector<Int>( rowselection_ ));
184    ///query = tab.nodeRownr().in(theset);
185  }
186  TableExprNode query;
187  intidmap::const_iterator it;
188  for (it = intselections_.begin(); it != intselections_.end(); ++it) {
189    TableExprNode theset(Vector<Int>( (*it).second ));
190    if ( query.isNull() ) {
191      //query = tab.col((*it).first).in(theset);
192      query = basetab.col((*it).first).in(theset);
193    } else {
194      //query = tab.col((*it).first).in(theset) && query;
195      query = basetab.col((*it).first).in(theset) && query;
196    }
197  }
198  stringidmap::const_iterator it1;
199  for (it1 = stringselections_.begin(); it1 != stringselections_.end(); ++it1) {
200    TableExprNode theset(mathutil::toVectorString( (*it1).second ));
201    if ( query.isNull() ) {
202      //query = tab.col((*it1).first).in(theset);
203      query = basetab.col((*it1).first).in(theset);
204    } else {
205      //query = tab.col((*it1).first).in(theset) && query;
206      query = basetab.col((*it1).first).in(theset) && query;
207    }
208  }
209  // add taql query
210  if ( taql_.size() > 0 ) {
211    //Table tmpt = tab;
212    Table tmpt = basetab;
213    std::string pytaql = "USING STYLE PYTHON " + taql_;
214
215    if ( !query.isNull() ) { // taql and selection
216      //tmpt = tableCommand(pytaql, tab(query));
217      tmpt = tableCommand(pytaql, basetab(query));
218    } else { // taql only
219      //tmpt = tableCommand(pytaql, tab);
220      tmpt = tableCommand(pytaql, basetab);
221    }
222    return sort(tmpt);
223  } else {
224    if ( query.isNull() ) {
225      //return sort(tab);
226      return sort(basetab);
227    } else {
228      //return sort(tab(query));
229      return sort(basetab(query));
230    }
231  }
232}
233
234std::vector< int > STSelector::getint( const std::string& key ) const
235{
236  if (intselections_.count(key) > 0) {
237    return  intselections_[key];
238  }
239  return  std::vector<int>();
240}
241
242std::vector< int > STSelector::getScans( ) const
243{
244  return getint("SCANNO");
245}
246
247std::vector< int > STSelector::getBeams( ) const
248{
249  return getint("BEAMNO");
250}
251
252std::vector< int > STSelector::getIFs( ) const
253{
254  return getint("IFNO");
255}
256
257std::vector< int > STSelector::getPols( ) const
258{
259  return getint("POLNO");
260}
261
262std::vector< int > asap::STSelector::getCycles( ) const
263{
264  return getint("CYCLENO");
265}
266
267std::vector< int > asap::STSelector::getTypes( ) const
268{
269  return getint("SRCTYPE") ;
270}
271
272std::string asap::STSelector::print( )
273{
274  ostringstream oss;
275  oss.flags(std::ios_base::left);
276  oss << setw(15) << "Selection:";
277  if ( empty() ) {
278    oss << "none";
279    return String(oss);
280  }
281
282  intidmap::const_iterator it = intselections_.begin();
283  while (it != intselections_.end()) {
284    if ( it != intselections_.begin() )
285      oss << setw(15) << " ";
286    oss << it->first << ": " << Vector<Int>(it->second);
287    ++it;
288    if ( it != intselections_.end() ) oss << endl;
289  }
290  stringidmap::const_iterator it1 = stringselections_.begin();
291  while (it1 != stringselections_.end()) {
292    if ( it1 != stringselections_.begin() )
293      oss << setw(15) << " ";
294    oss << it1->first << ": " << mathutil::toVectorString(it1->second);
295    ++it1;
296    if ( it1 != stringselections_.end() ) oss << endl;
297  }
298  if ( taql_.size() > 0 ) {
299    oss << endl << setw(15) << "" << taql_;
300  }
301  return String(oss);
302}
303
304bool asap::STSelector::empty( ) const
305{
306  //return (intselections_.empty() && taql_.size() == 0 );
307  return (intselections_.empty() && taql_.size() == 0 && rowselection_.size() == 0);
308}
309
310casa::Table asap::STSelector::sort( const casa::Table & tab )
311{
312  if (order_.nelements() > 0) {
313    Table t = tab.sort(order_);
314    return t;
315  } else {
316    return tab;
317  }
318}
319
320
321void asap::STSelector::setPolFromStrings( const std::vector< std::string >& pols )
322{
323  poltypes_.clear();
324  std::vector<int> polints;
325  std::vector<std::string>::const_iterator strit;
326  for (strit = pols.begin(); strit != pols.end(); ++strit) {
327    std::pair<int, std::string> val;
328    try {
329       val = STPol::polFromString(*strit);
330    } catch (AipsError& e) {
331      poltypes_.clear();
332      throw(e);
333    }
334    polints.push_back(val.first);
335    poltypes_.push_back(val.second);
336  }
337  setint("POLNO", polints);
338}
339
340std::vector< std::string > asap::STSelector::getPolTypes( ) const
341{
342  return poltypes_;
343}
344
345std::vector<std::string> asap::STSelector::getSortOrder() const
346{
347  std::vector<std::string> out;
348  for (uInt i=0;i<order_.nelements(); ++i)
349    out.push_back(order_[i]);
350  return out;
351}
Note: See TracBrowser for help on using the repository browser.