source: trunk/src/param.cc @ 434

Last change on this file since 434 was 434, checked in by MatthewWhiting, 16 years ago

Fixed ticket #35, so that the user can now enter a new flux unit string and the array will be converted to those units.

File size: 41.4 KB
Line 
1// -----------------------------------------------------------------------
2// param.cc: Dealing with the set of parameters for Duchamp.
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <iomanip>
30#include <fstream>
31#include <sstream>
32#include <string>
33#include <stdlib.h>
34#include <ctype.h>
35#include <math.h>
36#include <unistd.h>
37#include <duchamp/param.hh>
38#include <duchamp/fitsHeader.hh>
39#include <duchamp/duchamp.hh>
40#include <duchamp/pgheader.hh>
41#include <duchamp/ATrous/filter.hh>
42#include <duchamp/Utils/utils.hh>
43#include <duchamp/Utils/Section.hh>
44
45namespace duchamp
46{
47
48  /****************************************************************/
49  ///////////////////////////////////////////////////
50  //// Accessor Functions for Parameter class:
51  ///////////////////////////////////////////////////
52  Param::~Param()
53  {
54    /**
55     * Deletes the offsets array if the sizeOffsets parameter is
56     * positive.
57     */
58    if(this->sizeOffsets>0) delete [] this->offsets;
59  }
60
61  Param::Param()
62  {
63    this->defaultValues();
64  }
65
66  void Param::defaultValues()
67  {
68    /**
69     * Provides default intial values for the parameters. Note that
70     * imageFile has no default value!
71     */
72    std::string baseSection = "[*,*,*]";
73    // Input files
74    this->imageFile         = "";
75    this->flagSubsection    = false;
76    this->pixelSec.setSection(baseSection);
77    this->flagReconExists   = false;
78    this->reconFile         = "";
79    this->flagSmoothExists  = false;
80    this->smoothFile        = "";
81    // Output files
82    this->flagLog           = false;
83    this->logFile           = "duchamp-Logfile.txt";
84    this->outFile           = "duchamp-Results.txt";
85    this->flagSeparateHeader= false;
86    this->headerFile        = "duchamp-Results.hdr";
87    this->spectraFile       = "duchamp-Spectra.ps";
88    this->flagTextSpectra   = false;
89    this->spectraTextFile   = "duchamp-Spectra.txt";
90    this->flagOutputMask    = false;
91    this->flagOutputSmooth  = false;
92    this->flagOutputRecon   = false;
93    this->flagOutputResid   = false;
94    this->flagVOT           = false;
95    this->votFile           = "duchamp-Results.xml";
96    this->flagKarma         = false;
97    this->karmaFile         = "duchamp-Results.ann";
98    this->flagMaps          = true;
99    this->detectionMap      = "duchamp-DetectionMap.ps";
100    this->momentMap         = "duchamp-MomentMap.ps";
101    this->flagXOutput       = true;
102    // Cube related parameters
103    this->flagBlankPix      = true;
104    this->blankPixValue     = -8.00061;
105    this->blankKeyword      = 1;
106    this->bscaleKeyword     = -8.00061;
107    this->bzeroKeyword      = 0.;
108    this->newFluxUnits      = "";
109    // Milky-Way parameters
110    this->flagMW            = false;
111    this->maxMW             = 112;
112    this->minMW             = 75;
113    this->numPixBeam        = 10.;
114    this->flagUsingBeam     = false;
115    // Trim-related         
116    this->flagTrim          = false;
117    this->hasBeenTrimmed    = false;
118    this->borderLeft        = 0;
119    this->borderRight       = 0;
120    this->borderBottom      = 0;
121    this->borderTop         = 0;
122    // Subsection offsets
123    this->sizeOffsets       = 0;
124    this->xSubOffset        = 0;
125    this->ySubOffset        = 0;
126    this->zSubOffset        = 0;
127    // Baseline related
128    this->flagBaseline      = false;
129    // Detection-related   
130    this->flagNegative      = false;
131    // Object growth       
132    this->flagGrowth        = false;
133    this->growthCut         = 2.;
134    // FDR analysis         
135    this->flagFDR           = false;
136    this->alphaFDR          = 0.01;
137    // Other detection     
138    this->flagStatSec       = false;
139    this->statSec.setSection(baseSection);
140    this->flagRobustStats   = true;
141    this->snrCut            = 3.;
142    this->threshold         = 0.;
143    this->flagUserThreshold = false;
144    // Smoothing
145    this->flagSmooth        = false;
146    this->smoothType        = "spectral";
147    this->hanningWidth      = 5;
148    this->kernMaj           = 3.;
149    this->kernMin           = -1.;
150    this->kernPA            = 0.;
151    // A trous reconstruction parameters
152    this->flagATrous        = false;
153    this->reconDim          = 1;
154    this->scaleMin          = 1;
155    this->scaleMax          = 0;
156    this->snrRecon          = 4.;
157    this->filterCode        = 1;
158    this->reconFilter.define(this->filterCode);
159    // Volume-merging parameters
160    this->flagAdjacent      = true;
161    this->threshSpatial     = 3.;
162    this->threshVelocity    = 7.;
163    this->minChannels       = 3;
164    this->minPix            = 2;
165    // Input-Output related
166    this->spectralMethod    = "peak";
167    this->spectralUnits     = "km/s";
168    this->pixelCentre       = "centroid";
169    this->borders           = true;
170    this->blankEdge         = true;
171    this->verbose           = true;
172  }
173
174  Param::Param (const Param& p)
175  {
176    operator=(p);
177  }
178
179  Param& Param::operator= (const Param& p)
180  {
181    if(this == &p) return *this;
182    this->imageFile         = p.imageFile;
183    this->flagSubsection    = p.flagSubsection;
184    this->pixelSec          = p.pixelSec;
185    this->flagReconExists   = p.flagReconExists;
186    this->reconFile         = p.reconFile;     
187    this->flagSmoothExists  = p.flagSmoothExists;
188    this->smoothFile        = p.smoothFile;     
189    this->flagLog           = p.flagLog;       
190    this->logFile           = p.logFile;       
191    this->outFile           = p.outFile;       
192    this->flagSeparateHeader= p.flagSeparateHeader;
193    this->headerFile        = p.headerFile;
194    this->spectraFile       = p.spectraFile;   
195    this->flagTextSpectra   = p.flagTextSpectra;   
196    this->spectraTextFile   = p.spectraTextFile;   
197    this->flagOutputMask    = p.flagOutputMask;
198    this->flagOutputSmooth  = p.flagOutputSmooth;
199    this->flagOutputRecon   = p.flagOutputRecon;
200    this->flagOutputResid   = p.flagOutputResid;
201    this->flagVOT           = p.flagVOT;         
202    this->votFile           = p.votFile;       
203    this->flagKarma         = p.flagKarma;     
204    this->karmaFile         = p.karmaFile;     
205    this->flagMaps          = p.flagMaps;       
206    this->detectionMap      = p.detectionMap;   
207    this->momentMap         = p.momentMap;     
208    this->flagXOutput       = p.flagXOutput;       
209    this->flagBlankPix      = p.flagBlankPix;   
210    this->blankPixValue     = p.blankPixValue; 
211    this->blankKeyword      = p.blankKeyword;   
212    this->bscaleKeyword     = p.bscaleKeyword; 
213    this->bzeroKeyword      = p.bzeroKeyword;   
214    this->newFluxUnits      = p.newFluxUnits;
215    this->flagMW            = p.flagMW;         
216    this->maxMW             = p.maxMW;         
217    this->minMW             = p.minMW;         
218    this->numPixBeam        = p.numPixBeam;     
219    this->flagTrim          = p.flagTrim;   
220    this->hasBeenTrimmed    = p.hasBeenTrimmed;   
221    this->borderLeft        = p.borderLeft;     
222    this->borderRight       = p.borderRight;   
223    this->borderBottom      = p.borderBottom;   
224    this->borderTop         = p.borderTop;     
225    if(this->sizeOffsets>0) delete [] this->offsets;
226    this->sizeOffsets       = p.sizeOffsets;
227    if(this->sizeOffsets>0){
228      this->offsets           = new long[this->sizeOffsets];
229      for(int i=0;i<this->sizeOffsets;i++) this->offsets[i] = p.offsets[i];
230    }
231    this->xSubOffset        = p.xSubOffset;     
232    this->ySubOffset        = p.ySubOffset;     
233    this->zSubOffset        = p.zSubOffset;
234    this->flagBaseline      = p.flagBaseline;
235    this->flagNegative      = p.flagNegative;
236    this->flagGrowth        = p.flagGrowth;
237    this->growthCut         = p.growthCut;
238    this->flagFDR           = p.flagFDR;
239    this->alphaFDR          = p.alphaFDR;
240    this->flagStatSec       = p.flagStatSec;
241    this->statSec           = p.statSec;
242    this->flagRobustStats   = p.flagRobustStats;
243    this->snrCut            = p.snrCut;
244    this->threshold         = p.threshold;
245    this->flagUserThreshold = p.flagUserThreshold;
246    this->flagSmooth        = p.flagSmooth;
247    this->smoothType        = p.smoothType;
248    this->hanningWidth      = p.hanningWidth;
249    this->kernMaj           = p.kernMaj;
250    this->kernMin           = p.kernMin;
251    this->kernPA            = p.kernPA;
252    this->flagATrous        = p.flagATrous;
253    this->reconDim          = p.reconDim;
254    this->scaleMin          = p.scaleMin;
255    this->scaleMax          = p.scaleMax;
256    this->snrRecon          = p.snrRecon;
257    this->filterCode        = p.filterCode;
258    this->reconFilter       = p.reconFilter;
259    this->flagAdjacent      = p.flagAdjacent;
260    this->threshSpatial     = p.threshSpatial;
261    this->threshVelocity    = p.threshVelocity;
262    this->minChannels       = p.minChannels;
263    this->minPix            = p.minPix;
264    this->spectralMethod    = p.spectralMethod;
265    this->spectralUnits     = p.spectralUnits;
266    this->pixelCentre       = p.pixelCentre;
267    this->borders           = p.borders;
268    this->verbose           = p.verbose;
269    return *this;
270  }
271  //--------------------------------------------------------------------
272
273  int Param::getopts(int argc, char ** argv)
274  {
275    /** 
276     *   A function that reads in the command-line options, in a manner
277     *    tailored for use with the main Duchamp program.
278     *
279     *   \param argc The number of command line arguments.
280     *   \param argv The array of command line arguments.
281     */
282
283    int returnValue = FAILURE;
284    if(argc==1){
285      std::cout << ERR_USAGE_MSG;
286      returnValue = FAILURE;
287    }
288    else {
289      std::string file;
290      bool changeX = false;
291      this->defaultValues();
292      char c;
293      while( ( c = getopt(argc,argv,"p:f:hvx") )!=-1){
294        switch(c) {
295        case 'p':
296          file = optarg;
297          if(this->readParams(file)==FAILURE){
298            std::stringstream errmsg;
299            errmsg << "Could not open parameter file " << file << ".\n";
300            duchampError("Duchamp",errmsg.str());
301          }
302          else returnValue = SUCCESS;
303          break;
304        case 'f':
305          file = optarg;
306          this->imageFile = file;
307          returnValue = SUCCESS;
308          break;
309        case 'v':
310          std::cout << PROGNAME << " version " << VERSION << std::endl;
311          break;
312        case 'x':
313          changeX = true;
314          break;
315        case 'h':
316        default :
317          std::cout << ERR_USAGE_MSG;
318          break;
319        }
320      }
321      if(changeX){
322        if(returnValue == SUCCESS) this->setFlagXOutput(false);
323        else {
324          duchampError("Duchamp",
325                       "You need to specify either a parameter file or FITS image.\n");
326          std::cout << "\n" << ERR_USAGE_MSG;
327        }
328      }
329    }
330    return returnValue;
331  }
332  //--------------------------------------------------------------------
333
334  bool Param::isBlank(float &value)
335  {
336    /**
337     *  Tests whether the value passed as the argument is BLANK or not.
338     *  \param value Pixel value to be tested.
339     *  \return False if flagBlankPix is false. Else, compare to the
340     *  relevant FITS keywords, using integer comparison.
341     */
342    return this->flagBlankPix &&
343      (this->blankKeyword == int((value-this->bzeroKeyword)/this->bscaleKeyword));
344  }
345
346  bool *Param::makeBlankMask(float *array, int size)
347  {
348    /**
349     *  This returns an array of bools, saying whether each pixel in the
350     *  given array is BLANK or not. If the pixel is BLANK, set mask to
351     *  false, else set to true. The array is allocated by the function.
352     */
353    bool *mask = new bool[size];
354    for(int i=0;i<size;i++) mask[i] = !this->isBlank(array[i]);
355    return mask;
356  }
357
358
359  bool Param::isInMW(int z)
360  {
361    /**
362     *  Tests whether we are flagging Milky Way channels, and if so
363     * whether the given channel number is in the Milky Way range. The
364     * channels are assumed to start at number 0. 
365     * \param z The channel number
366     * \return True if we are flagging Milky Way channels and z is in
367     *  the range.
368     */
369    return ( flagMW && (z>=minMW) && (z<=maxMW) );
370  }
371
372  bool Param::isStatOK(int x, int y, int z)
373  {
374    /**
375     * Test whether a given pixel position lies within the subsection
376     * given by the statSec parameter. Only tested if the flagSubsection
377     * parameter is true -- if it isn't, we just return true since all
378     * pixels are therefore available for statstical calculations.
379     * \param x X-value of pixel being tested.
380     * \param y Y-value of pixel being tested.
381     * \param z Z-value of pixel being tested.
382     * \return True if pixel is able to be used for statistical
383     * calculations. False otherwise.
384     */
385    int xval=x,yval=y,zval=z;
386    if(flagSubsection){
387      xval += pixelSec.getStart(0);
388      yval += pixelSec.getStart(1);
389      zval += pixelSec.getStart(2);
390    }
391    return !flagStatSec || statSec.isInside(xval,yval,zval);
392  }
393
394  /****************************************************************/
395  ///////////////////////////////////////////////////
396  //// Other Functions using the  Parameter class:
397  ///////////////////////////////////////////////////
398
399  std::string makelower( std::string s )
400  {
401    // "borrowed" from Matt Howlett's 'fred'
402    std::string out = "";
403    for( int i=0; i<s.size(); ++i ) {
404      out += tolower(s[i]);
405    }
406    return out;
407  }
408
409  inline std::string stringize(bool b)
410  {
411    /**
412     * Convert a bool variable to the textual equivalent.
413     * \return A std::string with the english equivalent of the bool.
414     */
415    std::string output;
416    if(b) output="true";
417    else output="false";
418    return output;
419  }
420
421  inline bool boolify( std::string s )
422  {
423    /**
424     *  Convert a std::string to a bool variable:
425     *  "1" and "true" get converted to true;
426     *  "0" and "false" (and anything else) get converted to false.
427     * \return The bool equivalent of the string.
428     */
429    if((s=="1") || (makelower(s)=="true")) return true;
430    else if((s=="0") || (makelower(s)=="false")) return false;
431    else return false;
432  }
433
434  inline std::string readSval(std::stringstream& ss)
435  {
436    std::string val; ss >> val; return val;
437  }
438
439  inline bool readFlag(std::stringstream& ss)
440  {
441    std::string val; ss >> val; return boolify(val);
442  }
443
444  inline float readFval(std::stringstream& ss)
445  {
446    float val; ss >> val; return val;
447  }
448
449  inline int readIval(std::stringstream& ss)
450  {
451    int val; ss >> val; return val;
452  }
453
454  int Param::readParams(std::string paramfile)
455  {
456    /**
457     * The parameters are read in from a disk file, on the assumption that each
458     *  line of the file has the format "parameter value" (eg. alphafdr 0.1)
459     *
460     * The case of the parameter name does not matter, nor does the
461     * formatting of the spaces (it can be any amount of whitespace or
462     * tabs).
463     *
464     * \param paramfile A std::string containing the parameter filename.
465     *
466     * \return FAILURE if the parameter file does not exist. SUCCESS if
467     * it is able to read it.
468     */
469    std::ifstream fin(paramfile.c_str());
470    if(!fin.is_open()) return FAILURE;
471    std::string line;
472    while( !std::getline(fin,line,'\n').eof()){
473
474      if(line[0]!='#'){
475        std::stringstream ss;
476        ss.str(line);
477        std::string arg;
478        ss >> arg;
479        arg = makelower(arg);
480        if(arg=="imagefile")       this->imageFile = readSval(ss);
481        if(arg=="flagsubsection")  this->flagSubsection = readFlag(ss);
482        if(arg=="subsection")      this->pixelSec.setSection(readSval(ss));
483        if(arg=="flagreconexists") this->flagReconExists = readFlag(ss);
484        if(arg=="reconfile")       this->reconFile = readSval(ss);
485        if(arg=="flagsmoothexists")this->flagSmoothExists = readFlag(ss);
486        if(arg=="smoothfile")      this->smoothFile = readSval(ss);
487
488        if(arg=="flaglog")         this->flagLog = readFlag(ss);
489        if(arg=="logfile")         this->logFile = readSval(ss);
490        if(arg=="outfile")         this->outFile = readSval(ss);
491        if(arg=="flagseparateheader") this->flagSeparateHeader = readFlag(ss);
492        if(arg=="headerfile")      this->headerFile = readSval(ss);
493        if(arg=="spectrafile")     this->spectraFile = readSval(ss);
494        if(arg=="flagtextspectra") this->flagTextSpectra = readFlag(ss);
495        if(arg=="spectraTextfile") this->spectraTextFile = readSval(ss);
496        if(arg=="flagoutputmask")  this->flagOutputMask = readFlag(ss);
497        if(arg=="flagoutputsmooth")this->flagOutputSmooth = readFlag(ss);
498        if(arg=="flagoutputrecon") this->flagOutputRecon = readFlag(ss);
499        if(arg=="flagoutputresid") this->flagOutputResid = readFlag(ss);
500        if(arg=="flagvot")         this->flagVOT = readFlag(ss);
501        if(arg=="votfile")         this->votFile = readSval(ss);
502        if(arg=="flagkarma")       this->flagKarma = readFlag(ss);
503        if(arg=="karmafile")       this->karmaFile = readSval(ss);
504        if(arg=="flagmaps")        this->flagMaps = readFlag(ss);
505        if(arg=="detectionmap")    this->detectionMap = readSval(ss);
506        if(arg=="momentmap")       this->momentMap = readSval(ss);
507        if(arg=="flagxoutput")     this->flagXOutput = readFlag(ss);
508        if(arg=="newfluxunits")    this->newFluxUnits = readSval(ss);
509
510        if(arg=="flagnegative")    this->flagNegative = readFlag(ss);
511        if(arg=="flagtrim")        this->flagTrim = readFlag(ss);
512        if(arg=="flagmw")          this->flagMW = readFlag(ss);
513        if(arg=="maxmw")           this->maxMW = readIval(ss);
514        if(arg=="minmw")           this->minMW = readIval(ss);
515        if(arg=="beamsize")        this->numPixBeam = readFval(ss);
516
517        if(arg=="flagbaseline")    this->flagBaseline = readFlag(ss);
518        if(arg=="minpix")          this->minPix = readIval(ss);
519        if(arg=="flaggrowth")      this->flagGrowth = readFlag(ss);
520        if(arg=="growthcut")       this->growthCut = readFval(ss);
521
522        if(arg=="flagfdr")         this->flagFDR = readFlag(ss);
523        if(arg=="alphafdr")        this->alphaFDR = readFval(ss);
524
525        if(arg=="flagstatsec")     this->flagStatSec = readFlag(ss);
526        if(arg=="statsec")         this->statSec.setSection(readSval(ss));
527        if(arg=="flagrobuststats") this->flagRobustStats = readFlag(ss);
528        if(arg=="snrcut")          this->snrCut = readFval(ss);
529        if(arg=="threshold"){
530          this->threshold = readFval(ss);
531          this->flagUserThreshold = true;
532        }
533     
534        if(arg=="flagsmooth")      this->flagSmooth = readFlag(ss);
535        if(arg=="smoothtype")      this->smoothType = readSval(ss);
536        if(arg=="hanningwidth")    this->hanningWidth = readIval(ss);
537        if(arg=="kernmaj")         this->kernMaj = readFval(ss);
538        if(arg=="kernmin")         this->kernMin = readFval(ss);
539        if(arg=="kernpa")          this->kernPA = readFval(ss);
540
541        if(arg=="flagatrous")      this->flagATrous = readFlag(ss);
542        if(arg=="recondim")        this->reconDim = readIval(ss);
543        if(arg=="scalemin")        this->scaleMin = readIval(ss);
544        if(arg=="scalemax")        this->scaleMax = readIval(ss);
545        if(arg=="snrrecon")        this->snrRecon = readFval(ss);
546        if(arg=="filtercode"){
547          this->filterCode = readIval(ss);
548          this->reconFilter.define(this->filterCode);
549        }
550
551        if(arg=="flagadjacent")    this->flagAdjacent = readFlag(ss);
552        if(arg=="threshspatial")   this->threshSpatial = readFval(ss);
553        if(arg=="threshvelocity")  this->threshVelocity = readFval(ss);
554        if(arg=="minchannels")     this->minChannels = readIval(ss);
555
556        if(arg=="spectralmethod")  this->spectralMethod=makelower(readSval(ss));
557        if(arg=="spectralunits")   this->spectralUnits = makelower(readSval(ss));
558        if(arg=="pixelcentre")     this->pixelCentre = makelower(readSval(ss));
559        if(arg=="drawborders")     this->borders = readFlag(ss);
560        if(arg=="drawblankedges")  this->blankEdge = readFlag(ss);
561        if(arg=="verbose")         this->verbose = readFlag(ss);
562
563        // Dealing with deprecated parameters.
564        if(arg=="flagblankpix"){
565          this->flagTrim = readFlag(ss);
566          std::stringstream errmsg;
567          errmsg <<"The parameter flagBlankPix is deprecated. "
568                 <<"Please use the flagTrim parameter in future.\n"
569                 <<"Setting flagTrim = " << stringize(this->flagTrim) << ".\n";
570          duchampWarning("Reading parameters",errmsg.str());
571        }
572        if(arg=="blankpixvalue"){
573          std::stringstream errmsg;
574          errmsg <<"The parameter blankPixValue is deprecated.\n"
575                 <<"This value is only taken from the FITS header.\n";
576          duchampWarning("Reading parameters",errmsg.str());
577        }
578
579      }
580    }
581
582    // If pgplot was not included in the compilation, need to set flagXOutput to false
583    if(!USE_PGPLOT) this->flagXOutput = false;
584
585    // The wavelet reconstruction takes precendence over the smoothing.
586    if(this->flagATrous) this->flagSmooth = false;
587
588    // Make sure smoothType is an acceptable type -- default is "spectral"
589    if((this->smoothType!="spectral")&&
590       (this->smoothType!="spatial")){
591      std::stringstream errmsg;
592      errmsg << "The requested value of the parameter smoothType, \""
593             << this->smoothType << "\" is invalid.\n"
594             << "Changing to \"spectral\".\n";
595      duchampWarning("Reading parameters",errmsg.str());
596      this->smoothType = "spectral";
597    }
598    // If kernMin has not been given, or is negative, make it equal to kernMaj
599    if(this->kernMin < 0) this->kernMin = this->kernMaj;
600
601    // Make sure spectralMethod is an acceptable type -- default is "peak"
602    if((this->spectralMethod!="peak")&&
603       (this->spectralMethod!="sum")){
604      std::stringstream errmsg;
605      errmsg << "The requested value of the parameter spectralMethod, \""
606             << this->spectralMethod << "\" is invalid.\n"
607             << "Changing to \"peak\".\n";
608      duchampWarning("Reading parameters",errmsg.str());
609      this->spectralMethod = "peak";
610    }
611
612    // Make sure pixelCentre is an acceptable type -- default is "peak"
613    if((this->pixelCentre!="centroid")&&
614       (this->pixelCentre!="average") &&
615       (this->pixelCentre!="peak")       ){
616      std::stringstream errmsg;
617      errmsg << "The requested value of the parameter pixelCentre, \""
618             << this->pixelCentre << "\" is invalid.\n"
619             << "Changing to \"centroid\".\n";
620      duchampWarning("Reading parameters",errmsg.str());
621      this->pixelCentre = "centroid";
622    }
623
624    return SUCCESS;
625  }
626
627
628  std::ostream& operator<< ( std::ostream& theStream, Param& par)
629  {
630    /**
631     * Print out the parameter set in a formatted, easy to read style.
632     * Lists the parameters, a description of them, and their value.
633     */
634
635    // Only show the [beamSize] bit if we are using the parameter
636    // otherwise we have read it from the FITS header.
637    std::string beamParam = "";
638    if(par.getFlagUsingBeam()) beamParam = "[beamSize]";
639
640    // BUG -- can get error: `boolalpha' is not a member of type `ios' -- old compilers: gcc 2.95.3?
641    //   theStream.setf(std::ios::boolalpha);
642    theStream.setf(std::ios::left);
643    theStream  <<"\n---- Parameters ----"<<std::endl;
644    theStream  << std::setfill('.');
645    const int widthText = 38;
646    const int widthPar  = 18;
647    if(par.getFlagSubsection())
648      theStream<<std::setw(widthText)<<"Image to be analysed"                 
649               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[imageFile]"
650               <<"  =  " <<resetiosflags(std::ios::right)
651               <<(par.getImageFile()+par.getSubsection()) <<std::endl;
652    else
653      theStream<<std::setw(widthText)<<"Image to be analysed"                 
654               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[imageFile]"
655               <<"  =  " <<resetiosflags(std::ios::right)
656               <<par.getImageFile()      <<std::endl;
657    if(par.getFlagReconExists() && par.getFlagATrous()){
658      theStream<<std::setw(widthText)<<"Reconstructed array exists?"         
659               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[reconExists]"
660               <<"  =  " <<resetiosflags(std::ios::right)
661               <<stringize(par.getFlagReconExists())<<std::endl;
662      theStream<<std::setw(widthText)<<"FITS file containing reconstruction" 
663               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[reconFile]"
664               <<"  =  " <<resetiosflags(std::ios::right)
665               <<par.getReconFile()      <<std::endl;
666    }
667    if(par.getFlagSmoothExists() && par.getFlagSmooth()){
668      theStream<<std::setw(widthText)<<"Smoothed array exists?"         
669               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[smoothExists]"
670               <<"  =  " <<resetiosflags(std::ios::right)
671               <<stringize(par.getFlagSmoothExists())<<std::endl;
672      theStream<<std::setw(widthText)<<"FITS file containing smoothed array" 
673               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[smoothFile]"
674               <<"  =  " <<resetiosflags(std::ios::right)
675               <<par.getSmoothFile()      <<std::endl;
676    }
677    theStream  <<std::setw(widthText)<<"Intermediate Logfile"
678               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[logFile]"
679               <<"  =  " <<resetiosflags(std::ios::right)
680               <<par.getLogFile()        <<std::endl;
681    theStream  <<std::setw(widthText)<<"Final Results file"                   
682               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[outFile]"
683               <<"  =  " <<resetiosflags(std::ios::right)
684               <<par.getOutFile()        <<std::endl;
685    if(par.getFlagSeparateHeader())
686      theStream <<std::setw(widthText)<<"Header for results file"
687                <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[headerFile]"
688                <<"  =  " <<resetiosflags(std::ios::right)
689                <<par.getHeaderFile()        <<std::endl;
690    theStream  <<std::setw(widthText)<<"Spectrum file"                       
691               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[spectraFile]"
692               <<"  =  " <<resetiosflags(std::ios::right)
693               <<par.getSpectraFile()    <<std::endl;
694    if(par.getFlagTextSpectra()){
695      theStream<<std::setw(widthText)<<"Text file with ascii spectral data"                         
696               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[spectraTextFile]"
697               <<"  =  " <<resetiosflags(std::ios::right)
698               <<par.getSpectraTextFile()<<std::endl;
699    }
700    if(par.getFlagVOT()){
701      theStream<<std::setw(widthText)<<"VOTable file"                         
702               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[votFile]"
703               <<"  =  " <<resetiosflags(std::ios::right)
704               <<par.getVOTFile()        <<std::endl;
705    }
706    if(par.getFlagKarma()){
707      theStream<<std::setw(widthText)<<"Karma annotation file"               
708               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[karmaFile]"
709               <<"  =  " <<resetiosflags(std::ios::right)
710             
711               <<par.getKarmaFile()      <<std::endl;
712    }
713    if(par.getFlagMaps()){
714      theStream<<std::setw(widthText)<<"0th Moment Map"                       
715               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[momentMap]"
716               <<"  =  " <<resetiosflags(std::ios::right)
717               <<par.getMomentMap()      <<std::endl;
718      theStream<<std::setw(widthText)<<"Detection Map"                       
719               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[detectionMap]"
720               <<"  =  " <<resetiosflags(std::ios::right)
721               <<par.getDetectionMap()   <<std::endl;
722    }
723    theStream  <<std::setw(widthText)<<"Display a map in a pgplot xwindow?"
724               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagXOutput]"
725               <<"  =  " <<resetiosflags(std::ios::right)
726               <<stringize(par.getFlagXOutput())     <<std::endl;
727    if(par.getFlagATrous()){                           
728      theStream<<std::setw(widthText)<<"Saving reconstructed cube?"           
729               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagOutputRecon]"
730               <<"  =  " <<resetiosflags(std::ios::right)
731               <<stringize(par.getFlagOutputRecon())<<std::endl;
732      theStream<<std::setw(widthText)<<"Saving residuals from reconstruction?"
733               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagOutputResid]"
734               <<"  =  " <<resetiosflags(std::ios::right)
735               <<stringize(par.getFlagOutputResid())<<std::endl;
736    }                                                 
737    if(par.getFlagSmooth()){                           
738      theStream<<std::setw(widthText)<<"Saving smoothed cube?"           
739               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagOutputSmooth]"
740               <<"  =  " <<resetiosflags(std::ios::right)
741               <<stringize(par.getFlagOutputSmooth())<<std::endl;
742    }                                                 
743    theStream<<std::setw(widthText)<<"Saving mask cube?"           
744             <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagOutputMask]"
745             <<"  =  " <<resetiosflags(std::ios::right)
746             <<stringize(par.getFlagOutputMask())<<std::endl;
747    theStream  <<"------"<<std::endl;
748    if(par.getFlagBlankPix()){
749      theStream<<std::setw(widthText)<<"Blank Pixel Value"                   
750               <<std::setw(widthPar)<<setiosflags(std::ios::right)<< ""
751               <<"  =  " <<resetiosflags(std::ios::right)
752               <<par.getBlankPixVal()    <<std::endl;
753    }
754    theStream  <<std::setw(widthText)<<"Trimming Blank Pixels?"                 
755               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagTrim]"
756               <<"  =  " <<resetiosflags(std::ios::right)
757               <<stringize(par.getFlagTrim())   <<std::endl;
758    theStream  <<std::setw(widthText)<<"Searching for Negative features?"     
759               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagNegative]"
760               <<"  =  " <<resetiosflags(std::ios::right)
761               <<stringize(par.getFlagNegative())   <<std::endl;
762    theStream  <<std::setw(widthText)<<"Removing Milky Way channels?"         
763               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagMW]"
764               <<"  =  " <<resetiosflags(std::ios::right)
765               <<stringize(par.getFlagMW())         <<std::endl;
766    if(par.getFlagMW()){
767      theStream<<std::setw(widthText)<<"Milky Way Channels"                   
768               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[minMW - maxMW]"
769               <<"  =  " <<resetiosflags(std::ios::right)
770               <<par.getMinMW()
771               <<"-" <<par.getMaxMW()          <<std::endl;
772    }
773    theStream  <<std::setw(widthText)<<"Beam Size (pixels)"                   
774               <<std::setw(widthPar)<<setiosflags(std::ios::right)<< beamParam
775               <<"  =  " <<resetiosflags(std::ios::right)
776               <<par.getBeamSize()       <<std::endl;
777    theStream  <<std::setw(widthText)<<"Removing baselines before search?"   
778               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagBaseline]"
779               <<"  =  " <<resetiosflags(std::ios::right)
780               <<stringize(par.getFlagBaseline())   <<std::endl;
781    theStream  <<std::setw(widthText)<<"Smoothing each spectrum first?"       
782               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagSmooth]"
783               <<"  =  " <<resetiosflags(std::ios::right)
784               <<stringize(par.getFlagSmooth())     <<std::endl;
785    if(par.getFlagSmooth()){           
786      theStream<<std::setw(widthText)<<"Type of smoothing"     
787               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[smoothType]"
788               <<"  =  " <<resetiosflags(std::ios::right)
789               <<par.getSmoothType()       <<std::endl;
790      if(par.getSmoothType()=="spectral")
791        theStream<<std::setw(widthText)<<"Width of hanning filter"     
792                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[hanningWidth]"
793                 <<"  =  " <<resetiosflags(std::ios::right)
794                 <<par.getHanningWidth()       <<std::endl;
795      else{
796        theStream<<std::setw(widthText)<<"Gaussian kernel semi-major axis [pix]"
797                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[kernMaj]"
798                 <<"  =  " <<resetiosflags(std::ios::right)
799                 <<par.getKernMaj() << std::endl;
800        theStream<<std::setw(widthText)<<"Gaussian kernel semi-minor axis [pix]"
801                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[kernMin]"
802                 <<"  =  " <<resetiosflags(std::ios::right)
803                 <<par.getKernMin() << std::endl;
804        theStream<<std::setw(widthText)<<"Gaussian kernel position angle [deg]"
805                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[kernPA]"
806                 <<"  =  " <<resetiosflags(std::ios::right)
807                 <<par.getKernPA() << std::endl;
808      }
809    }
810    theStream  <<std::setw(widthText)<<"Using A Trous reconstruction?"       
811               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagATrous]"
812               <<"  =  " <<resetiosflags(std::ios::right)
813               <<stringize(par.getFlagATrous())     <<std::endl;
814    if(par.getFlagATrous()){                           
815      theStream<<std::setw(widthText)<<"Number of dimensions in reconstruction"     
816               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[reconDim]"
817               <<"  =  " <<resetiosflags(std::ios::right)
818               <<par.getReconDim()       <<std::endl;
819      if(par.getMaxScale()>0){
820        theStream<<std::setw(widthText-1)<<"Scales used in reconstruction"     
821                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[scaleMin-scaleMax]"
822                 <<"  =  " <<resetiosflags(std::ios::right)
823                 <<par.getMinScale() << "-" << par.getMaxScale()   <<std::endl;
824      }
825      else{
826        theStream<<std::setw(widthText)<<"Minimum scale in reconstruction"     
827                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[scaleMin]"
828                 <<"  =  " <<resetiosflags(std::ios::right)
829                 <<par.getMinScale()      <<std::endl;
830      }
831      theStream<<std::setw(widthText)<<"SNR Threshold within reconstruction" 
832               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[snrRecon]"
833               <<"  =  " <<resetiosflags(std::ios::right)
834               <<par.getAtrousCut()      <<std::endl;
835      theStream<<std::setw(widthText)<<"Filter being used for reconstruction"
836               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[filterCode]"
837               <<"  =  " <<resetiosflags(std::ios::right)
838               <<par.getFilterCode()
839               << " (" << par.getFilterName()  << ")" <<std::endl;
840    }                                                 
841    theStream  <<std::setw(widthText)<<"Using Robust statistics?"                 
842               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagRobustStats]"
843               <<"  =  " <<resetiosflags(std::ios::right)
844               <<stringize(par.getFlagRobustStats()) <<std::endl;
845    if(par.getFlagStatSec()){
846      theStream<<std::setw(widthText)<<"Section used by statistics calculation"
847               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[statSec]"
848               <<"  =  " <<resetiosflags(std::ios::right)
849               <<par.statSec.getSection()          <<std::endl;
850    }
851    theStream  <<std::setw(widthText)<<"Using FDR analysis?"                 
852               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagFDR]"
853               <<"  =  " <<resetiosflags(std::ios::right)
854               <<stringize(par.getFlagFDR())        <<std::endl;
855    if(par.getFlagFDR()){                                     
856      theStream<<std::setw(widthText)<<"Alpha value for FDR analysis"         
857               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[alphaFDR]"
858               <<"  =  " <<resetiosflags(std::ios::right)
859               <<par.getAlpha()          <<std::endl;
860    }                                                 
861    else {
862      if(par.getFlagUserThreshold()){
863        theStream<<std::setw(widthText)<<"Detection Threshold"                       
864                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[threshold]"
865                 <<"  =  " <<resetiosflags(std::ios::right)
866                 <<par.getThreshold()            <<std::endl;
867      }
868      else{
869        theStream<<std::setw(widthText)<<"SNR Threshold (in sigma)"
870                 <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[snrCut]"
871                 <<"  =  " <<resetiosflags(std::ios::right)
872                 <<par.getCut()            <<std::endl;
873      }
874    }
875    theStream  <<std::setw(widthText)<<"Minimum # Pixels in a detection"     
876               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[minPix]"
877               <<"  =  " <<resetiosflags(std::ios::right)
878               <<par.getMinPix()         <<std::endl;
879    theStream  <<std::setw(widthText)<<"Minimum # Channels in a detection"   
880               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[minChannels]"
881               <<"  =  " <<resetiosflags(std::ios::right)
882               <<par.getMinChannels()    <<std::endl;
883    theStream  <<std::setw(widthText)<<"Growing objects after detection?"     
884               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagGrowth]"
885               <<"  =  " <<resetiosflags(std::ios::right)
886               <<stringize(par.getFlagGrowth())     <<std::endl;
887    if(par.getFlagGrowth()) {                         
888      theStream<<std::setw(widthText)<<"SNR Threshold for growth"             
889               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[growthCut]"
890               <<"  =  " <<resetiosflags(std::ios::right)
891               <<par.getGrowthCut()      <<std::endl;
892    }
893    theStream  <<std::setw(widthText)<<"Using Adjacent-pixel criterion?"     
894               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[flagAdjacent]"
895               <<"  =  " <<resetiosflags(std::ios::right)
896               <<stringize(par.getFlagAdjacent())   <<std::endl;
897    if(!par.getFlagAdjacent()){
898      theStream<<std::setw(widthText)<<"Max. spatial separation for merging" 
899               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[threshSpatial]"
900               <<"  =  " <<resetiosflags(std::ios::right)
901               <<par.getThreshS()        <<std::endl;
902    }
903    theStream  <<std::setw(widthText)<<"Max. velocity separation for merging"
904               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[threshVelocity]"
905               <<"  =  " <<resetiosflags(std::ios::right)
906               <<par.getThreshV()        <<std::endl;
907    theStream  <<std::setw(widthText)<<"Method of spectral plotting"         
908               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[spectralMethod]"
909               <<"  =  " <<resetiosflags(std::ios::right)
910               <<par.getSpectralMethod() <<std::endl;
911    theStream  <<std::setw(widthText)<<"Type of object centre used in results"
912               <<std::setw(widthPar)<<setiosflags(std::ios::right)<<"[pixelCentre]"
913               <<"  =  " <<resetiosflags(std::ios::right)
914               <<par.getPixelCentre() <<std::endl;
915    theStream  <<"--------------------\n\n";
916    theStream  << std::setfill(' ');
917    theStream.unsetf(std::ios::left);
918    //  theStream.unsetf(std::ios::boolalpha);
919    return theStream;
920  }
921
922
923  void Param::copyHeaderInfo(FitsHeader &head)
924  {
925    /**
926     * A function to copy across relevant header keywords from the
927     *  FitsHeader class to the Param class, as they are needed by
928     *  functions in the Param class.
929     * The parameters are the keywords BLANK, BSCALE, BZERO, and the beam size.
930     */
931
932    this->blankKeyword  = head.getBlankKeyword();
933    this->bscaleKeyword = head.getBscaleKeyword();
934    this->bzeroKeyword  = head.getBzeroKeyword();
935    this->blankPixValue = this->blankKeyword * this->bscaleKeyword +
936      this->bzeroKeyword;
937
938    this->numPixBeam    = head.getBeamSize();
939  }
940
941  std::string Param::outputMaskFile()
942  {
943    /**
944     *  This function produces the required filename in which to save
945     *  the mask image, indicating which pixels have been detected as
946     *  part of an object. If the input image is image.fits, then the
947     *  output will be image.MASK.fits.
948     */
949    std::string inputName = this->imageFile;
950    std::stringstream ss;
951    ss << inputName.substr(0,inputName.size()-5); 
952    // remove the ".fits" on the end.
953    ss << ".MASK.fits";
954    return ss.str();
955  }
956
957  std::string Param::outputSmoothFile()
958  {
959    /**
960     * This function produces the required filename in which to save
961     *  the smoothed array. If the input image is image.fits, then
962     *  the output will be:
963     *   <ul><li> Spectral smoothing: image.SMOOTH-1D-3.fits, where the
964     *            width of the Hanning filter was 3 pixels.
965     *       <li> Spatial smoothing : image.SMOOTH-2D-3-2-20.fits, where
966     *            kernMaj=3, kernMin=2 and kernPA=20 degrees.
967     *   </ul>
968     */
969    std::string inputName = this->imageFile;
970    std::stringstream ss;
971    ss << inputName.substr(0,inputName.size()-5); 
972    // remove the ".fits" on the end.
973    if(this->flagSubsection) ss<<".sub";
974    if(this->smoothType=="spectral")
975      ss << ".SMOOTH-1D-" << this->hanningWidth << ".fits";
976    else if(this->smoothType=="spatial")
977      ss << ".SMOOTH-2D-"
978         << this->kernMaj << "-"
979         << this->kernMin << "-"
980         << this->kernPA  << ".fits";
981    return ss.str();
982  }
983
984  std::string Param::outputReconFile()
985  {
986    /**
987     * This function produces the required filename in which to save
988     *  the reconstructed array. If the input image is image.fits, then
989     *  the output will be eg. image.RECON-3-2-4-1.fits, where the numbers are
990     *  3=reconDim, 2=filterCode, 4=snrRecon, 1=minScale
991     */
992    std::string inputName = this->imageFile;
993    std::stringstream ss;
994    // First we remove the ".fits" from the end of the filename.
995    ss << inputName.substr(0,inputName.size()-5); 
996    if(this->flagSubsection) ss<<".sub";
997    ss << ".RECON-" << this->reconDim
998       << "-"       << this->filterCode
999       << "-"       << this->snrRecon
1000       << "-"       << this->scaleMin
1001       << ".fits";
1002    return ss.str();
1003  }
1004
1005  std::string Param::outputResidFile()
1006  {
1007    /**
1008     * This function produces the required filename in which to save
1009     *  the reconstructed array. If the input image is image.fits, then
1010     *  the output will be eg. image.RESID-3-2-4-1.fits, where the numbers are
1011     *  3=reconDim, 2=filterCode, 4=snrRecon, 1=scaleMin
1012     */
1013    std::string inputName = this->imageFile;
1014    std::stringstream ss;
1015    // First we remove the ".fits" from the end of the filename.
1016    ss << inputName.substr(0,inputName.size()-5);
1017    if(this->flagSubsection) ss<<".sub";
1018    ss << ".RESID-" << this->reconDim
1019       << "-"       << this->filterCode
1020       << "-"       << this->snrRecon
1021       << "-"       << this->scaleMin
1022       << ".fits";
1023    return ss.str();
1024  }
1025
1026}
Note: See TracBrowser for help on using the repository browser.