source: trunk/src/param.cc @ 420

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