//#--------------------------------------------------------------------------- //# SDMath.cc: A collection of single dish mathematical operations //#--------------------------------------------------------------------------- //# Copyright (C) 2004 //# Malte Marquarding, ATNF //# //# This program is free software; you can redistribute it and/or modify it //# under the terms of the GNU General Public License as published by the Free //# Software Foundation; either version 2 of the License, or (at your option) //# any later version. //# //# This program is distributed in the hope that it will be useful, but //# WITHOUT ANY WARRANTY; without even the implied warranty of //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General //# Public License for more details. //# //# You should have received a copy of the GNU General Public License along //# with this program; if not, write to the Free Software Foundation, Inc., //# 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning this software should be addressed as follows: //# Internet email: Malte.Marquarding@csiro.au //# Postal address: Malte Marquarding, //# Australia Telescope National Facility, //# P.O. Box 76, //# Epping, NSW, 2121, //# AUSTRALIA //# //# $Id: //#--------------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "MathUtils.h" #include "SDContainer.h" #include "SDMemTable.h" #include "SDMath.h" using namespace atnf_sd; static CountedPtr SDMath::average(const CountedPtr& in) { Table t = in->table(); ROArrayColumn tsys(t, "TSYS"); ROScalarColumn mjd(t, "TIME"); ROScalarColumn srcn(t, "SRCNAME"); ROScalarColumn integr(t, "INTERVAL"); ROArrayColumn freqidc(t, "FREQID"); IPosition ip = in->rowAsMaskedArray(0).shape(); Array outarr(ip); outarr =0.0; Array narr(ip);narr = 0.0; Array narrinc(ip);narrinc = 1.0; Array tsarr(tsys.shape(0)); Array outtsarr(tsys.shape(0)); Double tme = 0.0; Double inttime = 0.0; for (uInt i=0; i < t.nrow(); i++) { // data stuff MaskedArray marr(in->rowAsMaskedArray(i)); outarr += marr; MaskedArray n(narrinc,marr.getMask()); narr += n; // get tsys.get(i, tsarr);// this is probably unneccessary as tsys should outtsarr += tsarr; // be constant Double tmp; mjd.get(i,tmp); tme += tmp;// average time integr.get(i,tmp); inttime += tmp; } // averaging using mask MaskedArray nma(narr,(narr > Float(0))); outarr /= nma; Array outflagsb = !(nma.getMask()); Array outflags(outflagsb.shape()); convertArray(outflags,outflagsb); SDContainer sc(ip(0),ip(1),ip(2),ip(3)); Int n = t.nrow(); outtsarr /= Float(n/2); sc.timestamp = tme/Double(n/2); sc.interval =inttime; String tstr; srcn.getScalar(0,tstr);// get sourcename of "mid" point sc.sourcename = tstr; Vector tvec; freqidc.get(0,tvec); sc.putFreqMap(tvec); sc.scanid = 0; sc.putSpectrum(outarr); sc.putFlags(outflags); SDMemTable* sdmt = new SDMemTable(*in,True); sdmt->putSDContainer(sc); return CountedPtr(sdmt); } static CountedPtr SDMath::quotient(const CountedPtr& on, const CountedPtr& off) { Table ton = on->table(); Table toff = off->table(); ROArrayColumn tsys(toff, "TSYS"); ROScalarColumn mjd(ton, "TIME"); ROScalarColumn integr(ton, "INTERVAL"); ROScalarColumn srcn(ton, "SRCNAME"); ROArrayColumn freqidc(ton, "FREQID"); MaskedArray mon(on->rowAsMaskedArray(0)); MaskedArray moff(off->rowAsMaskedArray(0)); IPosition ipon = mon.shape(); IPosition ipoff = moff.shape(); Array tsarr(tsys.shape(0)); if (ipon != ipoff && ipon != tsarr.shape()) cerr << "on/off not conformant" << endl; //IPosition test = mon.shape()/2; MaskedArray tmp = (mon-moff); Array out(tmp.getArray()); out /= moff; out *= tsarr; Array outflagsb = !(mon.getMask() && moff.getMask()); Array outflags(outflagsb.shape()); convertArray(outflags,outflagsb); SDContainer sc(ipon(0),ipon(1),ipon(2),ipon(3)); String tstr; srcn.getScalar(0,tstr);// get sourcename of "on" scan sc.sourcename = tstr; Double tme; mjd.getScalar(0,tme);// get time of "on" scan sc.timestamp = tme; integr.getScalar(0,tme); sc.interval = tme; Vector tvec; freqidc.get(0,tvec); sc.putFreqMap(tvec); sc.scanid = 0; sc.putSpectrum(out); sc.putFlags(outflags); SDMemTable* sdmt = new SDMemTable(*on, True); sdmt->putSDContainer(sc); return CountedPtr(sdmt); } static CountedPtr SDMath::multiply(const CountedPtr& in, Float factor) { SDMemTable* sdmt = new SDMemTable(*in); Table t = sdmt->table(); ArrayColumn spec(t,"SPECTRA"); for (uInt i=0; i < t.nrow(); i++) { // data stuff MaskedArray marr(sdmt->rowAsMaskedArray(i)); marr *= factor; spec.put(i, marr.getArray()); } return CountedPtr(sdmt); } static std::vector SDMath::baseline(const CountedPtr& in, const std::string& fitexpr) { cout << "Fitting: " << fitexpr << endl; Table t = in->table(); LinearFit fitter; Vector y; in->getSpectrum(y, 0); Vector m; in->getMask(m, 0); Vector x(y.nelements()); indgen(x); CompiledFunction > fn; fn.setFunction(String(fitexpr)); fitter.setFunction(fn); Vector out,out1; out = fitter.fit(x,y,&m); out1 = y; fitter.residual(out1,x); cout << "solution =" << out << endl; std::vector fitted; out1.tovector(fitted); return fitted; } static CountedPtr SDMath::hanning(const CountedPtr& in) { IPosition ip = in->rowAsMaskedArray(0).shape(); MaskedArray marr(in->rowAsMaskedArray(0)); Array arr = marr.getArray(); Array barr = marr.getMask(); for (uInt i=0; inBeam();++i) { for (uInt j=0; jnIF();++j) { for (uInt k=0; knPol();++k) { IPosition start(4,i,j,k,0); IPosition end(4,i,j,k,in->nChan()-1); Array subArr(arr(start,end)); Array subMask(barr(start,end)); Vector outv; Vector outm; Vector v(subArr.nonDegenerate()); Vector m(subMask.nonDegenerate()); ::hanning(outv,outm,v,m); ArrayAccessor > aa0(outv); ArrayAccessor > ba0(outm); ArrayAccessor > ba(subMask); for (ArrayAccessor > aa(subArr); aa != aa.end();++aa) { (*aa) = (*aa0); (*ba) = (*ba0); aa0++; ba0++; ba++; } } } } Array outflags(barr.shape()); convertArray(outflags,!barr); SDContainer sc = in->getSDContainer(); sc.putSpectrum(arr); sc.putFlags(outflags); SDMemTable* sdmt = new SDMemTable(*in,True); sdmt->putSDContainer(sc); return CountedPtr(sdmt); } /* static Float SDMath::rms(const SDMemTable& in, uInt whichRow) { Table t = in.table(); MaskedArray marr(in.rowAsMaskedArray(whichRow,True)); } */