//#--------------------------------------------------------------------------- //# SDLineFinder.cc: A class for automated spectral line search //#-------------------------------------------------------------------------- //# Copyright (C) 2004 //# 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: //#--------------------------------------------------------------------------- // ASAP #include "SDLineFinder.h" // STL #include using namespace asap; using namespace casa; using namespace std; using namespace boost::python; // SDLineFinder - a class for automated spectral line search SDLineFinder::SDLineFinder() throw() : edge(0,0) { // detection threshold - the minimal signal to noise ratio threshold=3.; // 3 sigma is a default box_size=1./16.; // default box size for running mean calculations is // 1/16 of the whole spectrum // A minimum number of consequtive channels, which should satisfy // the detection criterion, to be a detection min_nchan=3; // default is 3 channels } SDLineFinder::~SDLineFinder() throw(AipsError) {} // set scan to work with (in_scan parameter), associated mask (in_mask // parameter) and the edge channel rejection (in_edge parameter) // if in_edge has zero length, all channels chosen by mask will be used // if in_edge has one element only, it represents the number of // channels to drop from both sides of the spectrum // in_edge is introduced for convinience, although all functionality // can be achieved using a spectrum mask only void SDLineFinder::setScan(const SDMemTableWrapper &in_scan, const std::vector &in_mask, const boost::python::tuple &in_edge) throw(AipsError) { try { scan=in_scan.getCP(); AlwaysAssert(!scan.null(),AipsError); if (scan->nRow()!=1) throw AipsError("SDLineFinder::setScan - in_scan contains more than 1 row." "Choose one first."); mask=in_mask; if (mask.nelements()!=scan->nChan()) throw AipsError("SDLineFinder::setScan - in_scan and in_mask have different" "number of spectral channels."); // number of elements in the in_edge tuple int n=extract(in_edge.attr("__len__")()); if (n>2 || n<0) throw AipsError("SDLineFinder::setScan - the length of the in_edge parameter" "should not exceed 2"); if (!n) { // all spectrum, no rejection edge.first=0; edge.second=scan->nChan(); } else { edge.first=extract(in_edge.attr("__getitem__")(0)); if (edge.first<0) throw AipsError("SDLineFinder::setScan - the in_edge parameter has a negative" "number of channels to drop"); if (edge.first>=scan->nChan()) throw AipsError("SDLineFinder::setScan - all channels are rejected by the in_edge parameter"); if (n==2) { edge.second=extract(in_edge.attr("__getitem__")(1)); if (edge.second<0) throw AipsError("SDLineFinder::setScan - the in_edge parameter has a negative" "number of channels to drop"); edge.second=scan->nChan()-edge.second; } else edge.second=scan->nChan()-edge.first; if (edge.second<0 || (edge.second+edge.first)>scan->nChan()) throw AipsError("SDLineFinder::setScan - all channels are rejected by the in_edge parameter"); } } catch(const AipsError &ae) { // setScan is unsuccessfull, reset scan/mask/edge scan=CountedConstPtr(); // null pointer mask.resize(0); edge=pair(0,0); throw; } } // search for spectral lines. Number of lines found is returned int SDLineFinder::findLines() throw(casa::AipsError) { const int minboxnchan=4; if (scan.null()) throw AipsError("SDLineFinder::findLines - a scan should be set first," " use set_scan"); DebugAssert(mask.nelements()==scan->nChan(), AipsError); lines.resize(0); // search from the scratch max_box_nchan=int(scan->nChan()*box_size); // number of channels in running // box if (max_box_nchan<2) throw AipsError("SDLineFinder::findLines - box_size is too small"); scan->getSpectrum(spectrum); // fill statistics for initial box box_chan_cntr=0; // no channels are currently in the box sum=0; // initialize statistics sumsq=0; int initial_box_ch=edge.first; for (;initial_box_ch=minboxnchan) // there is a minimum amount of data. We can search in the // half of the initial box for (int n=edge.first;n=minboxnchan) // have enough data to process processChannel(n); else processCurLine(); // just finish what was accumulated before } return int(lines.size()); } // supplementary function to control running mean calculations. // It adds a specified channel to the running mean box and // removes (ch-max_box_nchan+1)'th channel from there // Channels, for which the mask is false or index is beyond the // allowed range, are ignored void SDLineFinder::advanceRunningBox(int ch) throw(AipsError) { if (ch>=edge.first && ch=edge.first && ch2remove=edge.second) return False; if (!mask[ch]) return False; DebugAssert(box_chan_cntr, AipsError); Float mean=sum/Float(box_chan_cntr); Float variance=sqrt(sumsq/Float(box_chan_cntr)-square(mean)); /* if (ch>3900 && ch<4100) cout<<"Tested "<min_nchan) { // it was a detection. We need to change the list Bool add_new_line=False; if (lines.size()) { for (int i=lines.back().second;i SDLineFinder::getMask(bool invert) const throw(casa::AipsError) { try { if (scan.null()) throw AipsError("SDLineFinder::getMask - a scan should be set first," " use set_scan followed by find_lines"); DebugAssert(mask.nelements()==scan->nChan(), AipsError); /* if (!lines.size()) throw AipsError("SDLineFinder::getMask - one have to search for " "lines first, use find_lines"); */ std::vector res_mask(mask.nelements()); // iterator through lines std::list >::const_iterator cli=lines.begin(); for (int ch=0;ch=edge.second) res_mask[ch]=false; else if (!mask[ch]) res_mask[ch]=false; else { res_mask[ch]=!invert; // no line by default if (cli==lines.end()) continue; if (ch>=cli->first && chsecond) res_mask[ch]=invert; // this is a line if (ch>=cli->second) ++cli; // next line in the list } return res_mask; } catch (const AipsError &ae) { throw; } catch (const exception &ex) { throw AipsError(String("SDLineFinder::getMask - STL error: ")+ex.what()); } } // get range for all lines found. If defunits is true (default), the // same units as used in the scan will be returned (e.g. velocity // instead of channels). If defunits is false, channels will be returned std::vector SDLineFinder::getLineRanges(bool defunits) const throw(casa::AipsError) { try { if (scan.null()) throw AipsError("SDLineFinder::getLineRanges - a scan should be set first," " use set_scan followed by find_lines"); DebugAssert(mask.nelements()==scan->nChan(), AipsError); if (!lines.size()) throw AipsError("SDLineFinder::getLineRanges - one have to search for " "lines first, use find_lines"); // temporary if (defunits) throw AipsError("SDLineFinder::getLineRanges - sorry, defunits=true have not " "yet been implemented"); // std::vector res(2*lines.size()); // iterator through lines & result std::list >::const_iterator cli=lines.begin(); std::vector::iterator ri=res.begin(); for (;cli!=lines.end() && ri!=res.end();++cli,++ri) { *ri=cli->first; if (++ri!=res.end()) *ri=cli->second-1; } return res; } catch (const AipsError &ae) { throw; } catch (const exception &ex) { throw AipsError(String("SDLineFinder::getLineRanges - STL error: ")+ex.what()); } }