source: branches/alma/src/STMath.h @ 1677

Last change on this file since 1677 was 1677, checked in by Takeshi Nakazato, 14 years ago

New Development: No

JIRA Issue: Yes CAS-1823

Ready to Release: Yes

Interface Changes: Yes

What Interface Changed: The mode parameter is added to scantable.scale() method.

Test Programs: s = sd.scantable('yourfile',False)

factor = []
for i in range(s.nrow()):

factor.append(i)

s2 = s + factor

Put in Release Notes: Yes

Module(s): -

Description: Describe your changes here...

Basic operations (addition, subtraction, multiplication, division)
of scantable with one dimensional list are implemented.
Size of list operand should be equal to either number of spectral channel
or number of row. In the former case, the list is operated as
channel-by-channel manner, while it is operated as row-by-row manner
in the latter case.
If number of spectral channel is equal to number of row, row-by-row
operation will be done.

The user is able to select operation mode (channel-by-channel or row-by-row)
manually by using lower level function, stmath.arrayop().

The scantable.scale() method is updated to allow list scaling factor.
Scaling is done in channel-by-channel manner if mode is set to 'channel',
while in row-by-row manner if mode is set to 'row'.


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1//
2// C++ Interface: STMath
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#ifndef ASAPSTMATH_H
13#define ASAPSTMATH_H
14
15#include <string>
16#include <map>
17
18#include <casa/aips.h>
19#include <casa/Utilities/CountedPtr.h>
20#include <casa/BasicSL/String.h>
21#include <casa/Arrays/Vector.h>
22#include <scimath/Mathematics/InterpolateArray1D.h>
23
24#include "Scantable.h"
25#include "STDefs.h"
26#include "STPol.h"
27#include "Logger.h"
28
29namespace asap {
30
31/**
32        * Mathmatical operations on Scantable objects
33        * @author Malte Marquarding
34*/
35class STMath : private Logger {
36public:
37        // typedef for long method name
38  typedef casa::InterpolateArray1D<casa::Double,
39                                   casa::Float>::InterpolationMethod imethod;
40
41  // typedef for std::map
42  typedef std::map<std::string, imethod> imap;
43
44/**
45  * whether to operate on the given Scantable or return a new one
46  * @param insitu the toggle for this behaviour
47  */
48  explicit STMath(bool insitu=true);
49
50  virtual ~STMath();
51
52  /**
53   * get the currnt @attr inistu_ state
54   */
55  bool insitu() const { return insitu_;};
56
57  /**
58   * set the currnt @attr inistu state
59   * @param b the new state
60   */
61  void setInsitu(bool b) { insitu_ = b; };
62
63
64  /**
65    * average a vector of Scantables
66    * @param in the vector of Scantables to average
67    * @param mask an optional mask to apply on specific weights
68    * @param weight weighting scheme
69    * @param avmode the mode ov averaging. Per "SCAN" or "ALL".
70    * @return a casa::CountedPtr<Scantable> which either holds a new Scantable
71    * or returns the imput pointer.
72    */
73  casa::CountedPtr<Scantable>
74    average( const std::vector<casa::CountedPtr<Scantable> >& in,
75             const std::vector<bool>& mask = std::vector<bool>(),
76             const std::string& weight = "NONE",
77             const std::string& avmode = "SCAN");
78
79  /**
80    * median average a vector of Scantables. See also STMath::average
81    * @param in the Scantable to average
82    * @param mode the averaging mode. Currently only "MEDIAN"
83    * @param avmode the mode ov averaging. Per "SCAN" or "ALL".
84    * @return a casa::CountedPtr<Scantable> which either holds a new Scantable
85    * or returns the imput pointer.
86    */
87  casa::CountedPtr<Scantable>
88    averageChannel( const casa::CountedPtr<Scantable> & in,
89                    const std::string& mode = "MEDIAN",
90                    const std::string& avmode = "SCAN");
91
92  /**
93    * Average polarisations together. really only useful if only linears are
94    * available.
95    * @param in the input Scantable
96    * @param mask an optional mask if weight allows one
97    * @param weight weighting scheme
98    * @return
99    */
100  casa::CountedPtr< Scantable >
101    averagePolarisations( const casa::CountedPtr< Scantable > & in,
102                          const std::vector<bool>& mask,
103                          const std::string& weight );
104
105  /**
106    * Average beams together.
107    * @param in the input Scantable
108    * @param mask an optional mask if weight allows one
109    * @param weight weighting scheme
110    * @return
111    */
112  casa::CountedPtr< Scantable >
113    averageBeams( const casa::CountedPtr< Scantable > & in,
114                   const std::vector<bool>& mask,
115                   const std::string& weight );
116
117  casa::CountedPtr<Scantable>
118    unaryOperate( const casa::CountedPtr<Scantable>& in, float val,
119                  const std::string& mode, bool tsys=false );
120
121  // array operation
122  casa::CountedPtr<Scantable>
123    arrayOperate( const casa::CountedPtr<Scantable>& in,
124                  const std::vector<float> val,
125                  const std::string& mode, bool tsys=false,
126                  const std::string& op="channel" );
127
128  // channel operation
129  casa::CountedPtr<Scantable>
130    arrayOperateChannel( const casa::CountedPtr<Scantable>& in,
131                         const std::vector<float> val,
132                         const std::string& mode, bool tsys=false );
133
134  // row operation
135  casa::CountedPtr<Scantable>
136    arrayOperateRow( const casa::CountedPtr<Scantable>& in,
137                     const std::vector<float> val,
138                     const std::string& mode, bool tsys=false );
139
140  // 2d array operation
141  casa::CountedPtr<Scantable>
142    array2dOperate( const casa::CountedPtr<Scantable>& in,
143                  const std::vector< std::vector<float> > val,
144                  const std::string& mode, bool tsys=false );
145
146  casa::CountedPtr<Scantable>
147    binaryOperate( const casa::CountedPtr<Scantable>& left,
148                   const casa::CountedPtr<Scantable>& right,
149                   const std::string& mode);
150
151  casa::CountedPtr<Scantable> autoQuotient(const casa::CountedPtr<Scantable>& in,
152                                           const std::string& mode = "NEAREST",
153                                           bool preserve = true);
154
155  casa::CountedPtr<Scantable> quotient( const casa::CountedPtr<Scantable>& on,
156                                        const casa::CountedPtr<Scantable>& off,
157                                        bool preserve = true );
158
159  /**
160    * Calibrate total power scans (translated from GBTIDL)
161    * @param calon uncalibrated Scantable with CAL noise signal
162    * @param caloff uncalibrated Scantable with no CAL signal
163    * @param tcal optional scalar Tcal, CAL temperature (K)
164    * @return casa::CountedPtr<Scantable> which holds a calibrated Scantable
165    * (spectrum - average of the two CAL on and off spectra;
166    * tsys - mean Tsys = <caloff>*Tcal/<calon-caloff> + Tcal/2)
167    */             
168  casa::CountedPtr<Scantable> dototalpower( const casa::CountedPtr<Scantable>& calon,
169                                            const casa::CountedPtr<Scantable>& caloff,
170                                            casa::Float tcal=1.0 );
171
172  /**
173    * Combine signal and reference scans (translated from GBTIDL)
174    * @param sig Scantable which contains signal scans
175    * @param ref Scantable which contains reference scans
176    * @param smoothref optional Boxcar smooth width of the reference scans
177    * default: no smoothing (=1)
178    * @param tsysv optional scalar Tsys value at the zenith, required to
179    * set tau, as well
180    * @param tau optional scalar Tau value
181    * @return casa::CountedPtr<Scantable> which holds combined scans
182    * (spectrum = (sig-ref)/ref * Tsys )
183    */
184  casa::CountedPtr<Scantable> dosigref( const casa::CountedPtr<Scantable>& sig,
185                                        const casa::CountedPtr<Scantable>& ref,
186                                        int smoothref=1,
187                                        casa::Float tsysv=0.0,
188                                        casa::Float tau=0.0 );
189
190  /**
191    * Calibrate GBT Nod scan pairs (translated from GBTIDL)
192    * @param s Scantable which contains Nod scans
193    * @param scans Vector of scan numbers
194    * @param smoothref optional Boxcar smooth width of the reference scans
195    * @param tsysv optional scalar Tsys value at the zenith, required to
196    * set tau, as well
197    * @param tau optional scalar Tau value
198    * @param tcal optional scalar Tcal, CAL temperature (K)
199    * @return casa::CountedPtr<Scantable> which holds calibrated scans
200    */
201  casa::CountedPtr<Scantable> donod( const casa::CountedPtr<Scantable>& s,
202                                     const std::vector<int>& scans,
203                                     int smoothref=1,
204                                     casa::Float tsysv=0.0,
205                                     casa::Float tau=0.0,
206                                     casa::Float tcal=0.0 );
207
208  /**
209    * Calibrate frequency switched scans (translated from GBTIDL)
210    * @param s Scantable which contains frequency switched  scans
211    * @param scans Vector of scan numbers
212    * @param smoothref optional Boxcar smooth width of the reference scans
213    * @param tsysv optional scalar Tsys value at the zenith, required to
214    * set tau, as well
215    * @param tau optional scalar Tau value
216    * @param tcal optional scalar Tcal, CAL temperature (K)
217    * @return casa::CountedPtr<Scantable> which holds calibrated scans
218    */
219  casa::CountedPtr<Scantable> dofs( const casa::CountedPtr<Scantable>& s,
220                                    const std::vector<int>& scans,
221                                    int smoothref=1,
222                                    casa::Float tsysv=0.0,
223                                    casa::Float tau=0.0,
224                                    casa::Float tcal=0.0 );
225
226  /**
227   * Calibrate data with Chopper-Wheel like calibration method
228   * which adopts position switching by antenna motion,
229   * wobbler (nutator) switching and On-The-Fly observation.
230   *
231   * The method is applicable to APEX, and other telescopes other than GBT.
232   *
233   * @param a Scantable which contains ON and OFF scans
234   * @param a string that indicates calibration mode
235   * @param a string that indicates antenna name
236   **/
237  casa::CountedPtr<Scantable> cwcal( const casa::CountedPtr<Scantable>& s,
238                                       const casa::String calmode,
239                                       const casa::String antname );
240
241  /**
242   * Calibrate frequency switched scans with Chopper-Wheel like
243   * calibration method.
244   *
245   * The method is applicable to APEX, and other telescopes other than GBT.
246   *
247   * @param a Scantable which contains ON and OFF scans
248   * @param a string that indicates antenna name
249   **/
250  casa::CountedPtr<Scantable> cwcalfs( const casa::CountedPtr<Scantable>& s,
251                                       const casa::String antname );
252
253
254  /**
255   * Folding frequency-switch data
256   * @param sig
257   * @param ref
258   * @param choffset
259   **/
260  casa::CountedPtr<Scantable> dofold( const casa::CountedPtr<Scantable> &sig,
261                                      const casa::CountedPtr<Scantable> &ref,
262                                      casa::Double choffset,
263                                      casa::Double choffset = 0.0 );
264
265  /**
266   * ALMA calibration
267   **/
268  casa::CountedPtr<Scantable> almacal( const casa::CountedPtr<Scantable>& s,
269                                       const casa::String calmode ) ;
270  casa::CountedPtr<Scantable> almacalfs( const casa::CountedPtr<Scantable>& s ) ;
271
272  casa::CountedPtr<Scantable>
273    freqSwitch( const casa::CountedPtr<Scantable>& in );
274
275  std::vector<float> statistic(const casa::CountedPtr<Scantable>& in,
276                               const std::vector<bool>& mask,
277                               const std::string& which);
278
279  std::vector< int > minMaxChan(const casa::CountedPtr<Scantable>& in,
280                                const std::vector<bool>& mask,
281                                const std::string& which);
282
283  casa::CountedPtr<Scantable> bin( const casa::CountedPtr<Scantable>& in,
284                                   int width=5);
285  casa::CountedPtr<Scantable>
286    resample(const casa::CountedPtr<Scantable>& in,
287             const std::string& method, float width);
288
289  casa::CountedPtr<Scantable>
290    smooth(const casa::CountedPtr<Scantable>& in, const std::string& kernel,
291                      float width);
292
293  casa::CountedPtr<Scantable>
294    gainElevation(const casa::CountedPtr<Scantable>& in,
295                  const std::vector<float>& coeff,
296                  const std::string& fileName,
297                  const std::string& method);
298  casa::CountedPtr<Scantable>
299    convertFlux(const casa::CountedPtr<Scantable>& in, float d,
300                float etaap, float jyperk);
301
302  casa::CountedPtr<Scantable> opacity(const casa::CountedPtr<Scantable>& in,
303                                      float tau);
304
305  casa::CountedPtr<Scantable>
306    merge(const std::vector<casa::CountedPtr<Scantable> >& in);
307
308  casa::CountedPtr<Scantable>
309    invertPhase( const casa::CountedPtr<Scantable>& in);
310
311  casa::CountedPtr<Scantable>
312    rotateXYPhase( const casa::CountedPtr<Scantable>& in, float phase);
313
314  casa::CountedPtr<Scantable>
315    rotateLinPolPhase( const casa::CountedPtr<Scantable>& in, float phase);
316
317  casa::CountedPtr<Scantable>
318    swapPolarisations(const casa::CountedPtr<Scantable>& in);
319
320  casa::CountedPtr<Scantable>
321    frequencyAlign( const casa::CountedPtr<Scantable>& in,
322                    const std::string& refTime = "",
323                    const std::string& method = "cubic" );
324
325  casa::CountedPtr<Scantable>
326    convertPolarisation( const casa::CountedPtr<Scantable>& in,
327                         const std::string& newtype);
328
329  casa::CountedPtr<Scantable>
330    mxExtract( const casa::CountedPtr<Scantable>& in,
331               const std::string& srctype = "on");
332
333  /**
334   * "hard" flag the data, this flags everything selected in setSelection()
335   * @param frequency the frequency to remove
336   * @param width the number of lags to flag left to the side of the frequency
337   */
338  casa::CountedPtr<Scantable>
339    lagFlag( const casa::CountedPtr<Scantable>& in, double frequency,
340              double width);
341
342  // test for average spectra with different channel/resolution
343  casa::CountedPtr<Scantable>
344    new_average( const std::vector<casa::CountedPtr<Scantable> >& in,
345                 const bool& compel,
346                 const std::vector<bool>& mask = std::vector<bool>(),
347                 const std::string& weight = "NONE",
348                 const std::string& avmode = "SCAN" )
349    throw (casa::AipsError) ;
350
351private:
352  casa::CountedPtr<Scantable>  applyToPol( const casa::CountedPtr<Scantable>& in,
353                                           STPol::polOperation fptr,
354                                           casa::Float phase);
355
356  static imethod stringToIMethod(const std::string& in);
357  static WeightType stringToWeight(const std::string& in);
358
359  void scaleByVector(casa::Table& in,
360                     const casa::Vector<casa::Float>& factor,
361                     bool dotsys);
362
363  void scaleFromAsciiTable(casa::Table& in, const std::string& filename,
364                           const std::string& method,
365                           const casa::Vector<casa::Float>& xout,
366                           bool dotsys);
367
368  void scaleFromTable(casa::Table& in, const casa::Table& table,
369                      const std::string& method,
370                      const casa::Vector<casa::Float>& xout, bool dotsys);
371
372  void convertBrightnessUnits(casa::CountedPtr<Scantable>& in,
373                              bool tokelvin, float cfac);
374
375  casa::CountedPtr< Scantable >
376    smoothOther( const casa::CountedPtr< Scantable >& in,
377                 const std::string& kernel,
378                 float width );
379
380  casa::CountedPtr< Scantable >
381    getScantable(const casa::CountedPtr< Scantable >& in, bool droprows);
382
383  casa::MaskedArray<casa::Float>
384    maskedArray( const casa::Vector<casa::Float>& s,
385                 const casa::Vector<casa::uChar>& f );
386  casa::MaskedArray<casa::Double>
387    maskedArray( const casa::Vector<casa::Double>& s,
388                 const casa::Vector<casa::uChar>& f );
389  casa::Vector<casa::uChar>
390    flagsFromMA(const casa::MaskedArray<casa::Float>& ma);
391
392  vector<float> getSpectrumFromTime( string reftime, casa::CountedPtr<Scantable>& s, string mode = "before" ) ;
393  vector<float> getTcalFromTime( string reftime, casa::CountedPtr<Scantable>& s, string mode="before" ) ;
394  vector<float> getTsysFromTime( string reftime, casa::CountedPtr<Scantable>& s, string mode="before" ) ;
395  vector<int> getRowIdFromTime( string reftime, casa::CountedPtr<Scantable>& s ) ;
396
397  // Chopper-Wheel type calibration
398  vector<float> getCalibratedSpectra( casa::CountedPtr<Scantable>& on,
399                                      casa::CountedPtr<Scantable>& off,
400                                      casa::CountedPtr<Scantable>& sky,
401                                      casa::CountedPtr<Scantable>& hot,
402                                      casa::CountedPtr<Scantable>& cold,
403                                      int index,
404                                      string antname ) ;
405  // Tsys * (ON-OFF)/OFF
406  vector<float> getCalibratedSpectra( casa::CountedPtr<Scantable>& on,
407                                      casa::CountedPtr<Scantable>& off,
408                                      int index ) ;
409  vector<float> getFSCalibratedSpectra( casa::CountedPtr<Scantable>& sig,
410                                        casa::CountedPtr<Scantable>& ref,
411                                        casa::CountedPtr<Scantable>& sky,
412                                        casa::CountedPtr<Scantable>& hot,
413                                        casa::CountedPtr<Scantable>& cold,
414                                        int index ) ;
415  vector<float> getFSCalibratedSpectra( casa::CountedPtr<Scantable>& sig,
416                                        casa::CountedPtr<Scantable>& ref,
417                                        vector< casa::CountedPtr<Scantable> >& sky,
418                                        vector< casa::CountedPtr<Scantable> >& hot,
419                                        vector< casa::CountedPtr<Scantable> >& cold,
420                                        int index ) ;
421  double getMJD( string strtime ) ;
422
423  bool insitu_;
424};
425
426}
427#endif
Note: See TracBrowser for help on using the repository browser.