source: trunk/src/SDMath.cc @ 43

Last change on this file since 43 was 38, checked in by mmarquar, 20 years ago

Added baseline and hanning functions. Also added copying of scanid and freqid.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1//#---------------------------------------------------------------------------
2//# SDMath.cc: A collection of single dish mathematical operations
3//#---------------------------------------------------------------------------
4//# Copyright (C) 2004
5//# Malte Marquarding, ATNF
6//#
7//# This program is free software; you can redistribute it and/or modify it
8//# under the terms of the GNU General Public License as published by the Free
9//# Software Foundation; either version 2 of the License, or (at your option)
10//# any later version.
11//#
12//# This program is distributed in the hope that it will be useful, but
13//# WITHOUT ANY WARRANTY; without even the implied warranty of
14//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15//# Public License for more details.
16//#
17//# You should have received a copy of the GNU General Public License along
18//# with this program; if not, write to the Free Software Foundation, Inc.,
19//# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
20//#
21//# Correspondence concerning this software should be addressed as follows:
22//#        Internet email: Malte.Marquarding@csiro.au
23//#        Postal address: Malte Marquarding,
24//#                        Australia Telescope National Facility,
25//#                        P.O. Box 76,
26//#                        Epping, NSW, 2121,
27//#                        AUSTRALIA
28//#
29//# $Id:
30//#---------------------------------------------------------------------------
31#include <vector>
32
33#include <aips/aips.h>
34#include <aips/Utilities/String.h>
35#include <aips/Arrays/IPosition.h>
36#include <aips/Arrays/Array.h>
37#include <aips/Arrays/ArrayAccessor.h>
38#include <aips/Arrays/Slice.h>
39#include <aips/Arrays/ArrayMath.h>
40#include <aips/Arrays/ArrayLogical.h>
41#include <aips/Arrays/MaskedArray.h>
42#include <aips/Arrays/MaskArrMath.h>
43#include <aips/Arrays/MaskArrLogi.h>
44
45#include <aips/Tables/Table.h>
46#include <aips/Tables/ScalarColumn.h>
47#include <aips/Tables/ArrayColumn.h>
48
49#include <aips/Fitting.h>
50#include <trial/Fitting/LinearFit.h>
51#include <trial/Functionals/CompiledFunction.h>
52#include <aips/Mathematics/AutoDiff.h>
53#include <aips/Mathematics/AutoDiffMath.h>
54
55#include "MathUtils.h"
56#include "SDContainer.h"
57#include "SDMemTable.h"
58
59#include "SDMath.h"
60
61using namespace atnf_sd;
62
63static CountedPtr<SDMemTable> SDMath::average(const CountedPtr<SDMemTable>& in) {
64  Table t = in->table();
65  ROArrayColumn<Float> tsys(t, "TSYS"); 
66  ROScalarColumn<Double> mjd(t, "TIME");
67  ROScalarColumn<String> srcn(t, "SRCNAME");
68  ROScalarColumn<Double> integr(t, "INTERVAL");
69  ROArrayColumn<uInt> freqidc(t, "FREQID");
70  IPosition ip = in->rowAsMaskedArray(0).shape();
71  Array<Float> outarr(ip); outarr =0.0;
72  Array<Float> narr(ip);narr = 0.0;
73  Array<Float> narrinc(ip);narrinc = 1.0;
74
75  Array<Float> tsarr(tsys.shape(0));
76  Array<Float> outtsarr(tsys.shape(0));
77  Double tme = 0.0;
78  Double inttime = 0.0;
79
80  for (uInt i=0; i < t.nrow(); i++) {
81    // data stuff
82    MaskedArray<Float> marr(in->rowAsMaskedArray(i));
83    outarr += marr;
84    MaskedArray<Float> n(narrinc,marr.getMask());
85    narr += n;
86    // get
87    tsys.get(i, tsarr);// this is probably unneccessary as tsys should
88    outtsarr += tsarr; // be constant
89    Double tmp;
90    mjd.get(i,tmp);
91    tme += tmp;// average time
92    integr.get(i,tmp);
93    inttime += tmp;
94  }
95  // averaging using mask
96  MaskedArray<Float> nma(narr,(narr > Float(0)));
97  outarr /= nma;
98  Array<Bool> outflagsb = !(nma.getMask());
99  Array<uChar> outflags(outflagsb.shape());
100  convertArray(outflags,outflagsb);
101  SDContainer sc(ip(0),ip(1),ip(2),ip(3));
102
103  Int n = t.nrow();
104  outtsarr /= Float(n/2);
105  sc.timestamp = tme/Double(n/2);
106  sc.interval =inttime;
107  String tstr; srcn.getScalar(0,tstr);// get sourcename of "mid" point
108  sc.sourcename = tstr;
109  Vector<uInt> tvec;
110  freqidc.get(0,tvec);
111  sc.putFreqMap(tvec);
112  sc.scanid = 0;
113  sc.putSpectrum(outarr);
114  sc.putFlags(outflags); 
115  SDMemTable* sdmt = new SDMemTable(*in,True);
116  sdmt->putSDContainer(sc);
117  return CountedPtr<SDMemTable>(sdmt);
118}
119
120static CountedPtr<SDMemTable>
121SDMath::quotient(const CountedPtr<SDMemTable>& on,
122                 const CountedPtr<SDMemTable>& off) {
123 
124  Table ton = on->table();
125  Table toff = off->table();
126  ROArrayColumn<Float> tsys(toff, "TSYS"); 
127  ROScalarColumn<Double> mjd(ton, "TIME");
128  ROScalarColumn<Double> integr(ton, "INTERVAL");
129  ROScalarColumn<String> srcn(ton, "SRCNAME");
130  ROArrayColumn<uInt> freqidc(ton, "FREQID");
131
132  MaskedArray<Float> mon(on->rowAsMaskedArray(0));
133  MaskedArray<Float> moff(off->rowAsMaskedArray(0));
134  IPosition ipon = mon.shape();
135  IPosition ipoff = moff.shape();
136  Array<Float> tsarr(tsys.shape(0));
137
138  if (ipon != ipoff && ipon != tsarr.shape())
139    cerr << "on/off not conformant" << endl;
140 
141  //IPosition test = mon.shape()/2;
142  MaskedArray<Float> tmp = (mon-moff);
143  Array<Float> out(tmp.getArray());
144  out /= moff;
145  out *= tsarr;
146  Array<Bool> outflagsb = !(mon.getMask() && moff.getMask());
147  Array<uChar> outflags(outflagsb.shape());
148  convertArray(outflags,outflagsb);
149
150  SDContainer sc(ipon(0),ipon(1),ipon(2),ipon(3));
151  String tstr; srcn.getScalar(0,tstr);// get sourcename of "on" scan
152  sc.sourcename = tstr;
153  Double tme; mjd.getScalar(0,tme);// get time of "on" scan
154  sc.timestamp = tme;
155  integr.getScalar(0,tme);
156  sc.interval = tme;
157  Vector<uInt> tvec;
158  freqidc.get(0,tvec);
159  sc.putFreqMap(tvec);
160  sc.scanid = 0;
161  sc.putSpectrum(out);
162  sc.putFlags(outflags); 
163  SDMemTable* sdmt = new SDMemTable(*on, True);
164  sdmt->putSDContainer(sc);
165  return CountedPtr<SDMemTable>(sdmt);
166 
167}
168static CountedPtr<SDMemTable>
169SDMath::multiply(const CountedPtr<SDMemTable>& in, Float factor) {
170  SDMemTable* sdmt = new SDMemTable(*in);
171  Table t = sdmt->table();
172  ArrayColumn<Float> spec(t,"SPECTRA");
173
174  for (uInt i=0; i < t.nrow(); i++) {
175    // data stuff
176    MaskedArray<Float> marr(sdmt->rowAsMaskedArray(i));
177    marr *= factor;
178    spec.put(i, marr.getArray());
179  }
180  return CountedPtr<SDMemTable>(sdmt);
181}
182static std::vector<float> SDMath::baseline(const CountedPtr<SDMemTable>& in,
183                                           const std::string& fitexpr) {
184  cout << "Fitting: " << fitexpr << endl;
185  Table t = in->table();
186  LinearFit<Float> fitter;
187  Vector<Float> y;
188  in->getSpectrum(y, 0);
189  Vector<Bool> m;
190  in->getMask(m, 0);
191  Vector<Float> x(y.nelements());
192  indgen(x);
193  CompiledFunction<AutoDiff<Float> > fn;
194  fn.setFunction(String(fitexpr));
195  fitter.setFunction(fn);
196  Vector<Float> out,out1;
197  out = fitter.fit(x,y,&m);
198  out1 = y;
199  fitter.residual(out1,x);
200  cout << "solution =" << out << endl;
201  std::vector<float> fitted;
202  out1.tovector(fitted);
203  return fitted;
204}
205
206
207static CountedPtr<SDMemTable>
208SDMath::hanning(const CountedPtr<SDMemTable>& in) {
209  IPosition ip = in->rowAsMaskedArray(0).shape();
210  MaskedArray<Float> marr(in->rowAsMaskedArray(0));
211
212  Array<Float> arr = marr.getArray();
213  Array<Bool> barr = marr.getMask();
214  for (uInt i=0; i<in->nBeam();++i) {
215    for (uInt j=0; j<in->nIF();++j) {
216      for (uInt k=0; k<in->nPol();++k) {
217        IPosition start(4,i,j,k,0);
218        IPosition end(4,i,j,k,in->nChan()-1);
219        Array<Float> subArr(arr(start,end));
220        Array<Bool> subMask(barr(start,end));
221        Vector<Float> outv;
222        Vector<Bool> outm;
223        Vector<Float> v(subArr.nonDegenerate());
224        Vector<Bool> m(subMask.nonDegenerate());
225        ::hanning(outv,outm,v,m);
226        ArrayAccessor<Float, Axis<0> > aa0(outv);       
227        ArrayAccessor<Bool, Axis<0> > ba0(outm);       
228        ArrayAccessor<Bool, Axis<3> > ba(subMask);     
229        for (ArrayAccessor<Float, Axis<3> > aa(subArr); aa != aa.end();++aa) {
230          (*aa) = (*aa0);
231          (*ba) = (*ba0);
232          aa0++;
233          ba0++;
234          ba++;
235        }
236      }
237    }
238  }
239  Array<uChar> outflags(barr.shape());
240  convertArray(outflags,!barr);
241  SDContainer sc = in->getSDContainer();
242  sc.putSpectrum(arr);
243  sc.putFlags(outflags);
244  SDMemTable* sdmt = new SDMemTable(*in,True);
245  sdmt->putSDContainer(sc);
246  return CountedPtr<SDMemTable>(sdmt);
247}
248
249/*
250static Float SDMath::rms(const SDMemTable& in, uInt whichRow) {
251  Table t = in.table(); 
252  MaskedArray<Float> marr(in.rowAsMaskedArray(whichRow,True));
253 
254}
255*/
Note: See TracBrowser for help on using the repository browser.