source: trunk/src/param.cc @ 269

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