source: tags/release-1.1.4/src/param.cc @ 1441

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