source: trunk/src/param.cc @ 1242

Last change on this file since 1242 was 1242, checked in by MatthewWhiting, 11 years ago

Tickets #193 & #195 - Implementing channel flagging. Still a lot of commented-out code, plus the MW code is still present (although not used anywhere). Also making use of the new plotting classes.

File size: 70.9 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 <algorithm>
34#include <stdlib.h>
35#include <ctype.h>
36#include <math.h>
37#include <unistd.h>
38#include <duchamp/param.hh>
39#include <duchamp/fitsHeader.hh>
40#include <duchamp/duchamp.hh>
41#include <duchamp/pgheader.hh>
42#include <duchamp/ATrous/filter.hh>
43#include <duchamp/Utils/utils.hh>
44#include <duchamp/Utils/Section.hh>
45#include <duchamp/Utils/VOParam.hh>
46#include <duchamp/Outputs/columns.hh>
47
48namespace duchamp
49{
50  const std::string defaultSection = "[*,*,*]";
51
52  /****************************************************************/
53  ///////////////////////////////////////////////////
54  //// Accessor Functions for Parameter class:
55  ///////////////////////////////////////////////////
56  Param::~Param()
57  {
58    /// Deletes the offsets array if the sizeOffsets parameter is
59    /// positive.
60    if(this->sizeOffsets>0) delete [] this->offsets;
61  }
62
63  Param::Param()
64  {
65    this->defaultValues();
66  }
67
68  void Param::defaultValues()
69  {
70    /// Provides default intial values for the parameters. Note that
71    /// imageFile has no default value!
72
73    // Input files
74    this->imageFile         = "";
75    this->flagSubsection    = false;
76    this->pixelSec          = Section();
77    this->flagReconExists   = false;
78    this->reconFile         = "";
79    this->flagSmoothExists  = false;
80    this->smoothFile        = "";
81    this->usePrevious       = false;
82    this->objectList        = "";
83    // Output files
84    this->flagLog           = false;
85    this->logFile           = "duchamp-Logfile.txt";
86    this->outFile           = "duchamp-Results.txt";
87    this->flagSeparateHeader= false;
88    this->headerFile        = "duchamp-Results.hdr";
89    this->flagWriteBinaryCatalogue = true;
90    this->binaryCatalogue   = "duchamp-Catalogue.dpc";
91    this->flagPlotSpectra   = true;
92    this->spectraFile       = "duchamp-Spectra.ps";
93    this->flagPlotIndividualSpectra = false;
94    this->flagTextSpectra   = false;
95    this->spectraTextFile   = "duchamp-Spectra.txt";
96    this->flagOutputBaseline    = false;
97    this->fileOutputBaseline    = "";
98    this->flagOutputMomentMap    = false;
99    this->fileOutputMomentMap    = "";
100    this->flagOutputMomentMask    = false;
101    this->fileOutputMomentMask    = "";
102    this->flagOutputMask    = false;
103    this->fileOutputMask    = "";
104    this->flagMaskWithObjectNum = false;
105    this->flagOutputSmooth  = false;
106    this->fileOutputSmooth  = "";
107    this->flagOutputRecon   = false;
108    this->fileOutputRecon   = "";
109    this->flagOutputResid   = false;
110    this->fileOutputResid   = "";
111    this->flagVOT           = false;
112    this->votFile           = "duchamp-Results.xml";
113    this->flagKarma         = false;
114    this->karmaFile         = "duchamp-Results.ann";
115    this->flagDS9           = false;
116    this->ds9File           = "duchamp-Results.reg";
117    this->flagCasa          = false;
118    this->casaFile          = "duchamp-Results.crf";
119    this->annotationType    = "borders";
120    this->flagMaps          = true;
121    this->detectionMap      = "duchamp-DetectionMap.ps";
122    this->momentMap         = "duchamp-MomentMap.ps";
123    this->flagXOutput       = true;
124    this->precFlux          = Catalogues::prFLUX;
125    this->precVel           = Catalogues::prVEL;
126    this->precSNR           = Catalogues::prSNR;
127    // Cube related parameters
128    this->flagBlankPix      = false;
129    this->blankPixValue     = -8.00061;
130    this->blankKeyword      = 1;
131    this->bscaleKeyword     = -8.00061;
132    this->bzeroKeyword      = 0.;
133    this->newFluxUnits      = "";
134    // Milky-Way parameters
135    this->flaggedChannelList = "";
136    this->flaggedChannels   = std::vector<int>(0);
137    this->flaggedChannelMask= std::vector<bool>(0);
138    this->flagMW            = false;
139    this->maxMW             = 112;
140    this->minMW             = 75;
141    this->areaBeam          = 0.;
142    this->fwhmBeam          = 0.;
143    this->beamAsUsed.empty();
144    this->searchType        = "spatial";
145    // Trim-related         
146    this->flagTrim          = false;
147    this->hasBeenTrimmed    = false;
148    this->borderLeft        = 0;
149    this->borderRight       = 0;
150    this->borderBottom      = 0;
151    this->borderTop         = 0;
152    // Subsection offsets
153    this->sizeOffsets       = 0;
154    this->xSubOffset        = 0;
155    this->ySubOffset        = 0;
156    this->zSubOffset        = 0;
157    // Baseline related
158    this->flagBaseline      = false;
159    // Detection-related   
160    this->flagNegative      = false;
161    // Object growth       
162    this->flagGrowth        = false;
163    this->growthCut         = 3.;
164    this->flagUserGrowthThreshold = false;
165    this->growthThreshold   = 0.;
166    // FDR analysis         
167    this->flagFDR           = false;
168    this->alphaFDR          = 0.01;
169    this->FDRnumCorChan     = 2;
170    // Other detection     
171    this->flagStatSec       = false;
172    this->statSec           = Section();
173    this->flagRobustStats   = true;
174    this->snrCut            = 5.;
175    this->threshold         = 0.;
176    this->flagUserThreshold = false;
177    // Smoothing
178    this->flagSmooth        = false;
179    this->smoothType        = "spectral";
180    this->hanningWidth      = 5;
181    this->kernMaj           = 3.;
182    this->kernMin           = -1.;
183    this->kernPA            = 0.;
184    // A trous reconstruction parameters
185    this->flagATrous        = false;
186    this->reconDim          = 1;
187    this->scaleMin          = 1;
188    this->scaleMax          = 0;
189    this->snrRecon          = 4.;
190    this->reconConvergence  = 0.005;
191    this->filterCode        = 1;
192    this->reconFilter.define(this->filterCode);
193    // Volume-merging parameters
194    this->flagAdjacent      = true;
195    this->threshSpatial     = 3.;
196    this->threshVelocity    = 7.;
197    this->minChannels       = 3;
198    this->minPix            = 2;
199    this->minVoxels         = 4;
200    this->flagRejectBeforeMerge = false;
201    this->flagTwoStageMerging = true;
202    // Input-Output related
203    this->spectralMethod    = "peak";
204    this->spectralType      = "";
205    this->restFrequency     = -1.;
206    this->restFrequencyUsed = false;
207    this->spectralUnits     = "";
208    this->pixelCentre       = "centroid";
209    this->sortingParam      = "vel";
210    this->borders           = true;
211    this->blankEdge         = true;
212    this->verbose           = true;
213    this->commentStr        = "";
214  }
215
216  Param::Param (const Param& p)
217  {
218    operator=(p);
219  }
220
221  Param& Param::operator= (const Param& p)
222  {
223    if(this == &p) return *this;
224    this->imageFile         = p.imageFile;
225    this->flagSubsection    = p.flagSubsection;
226    this->pixelSec          = p.pixelSec;
227    this->flagReconExists   = p.flagReconExists;
228    this->reconFile         = p.reconFile;     
229    this->flagSmoothExists  = p.flagSmoothExists;
230    this->smoothFile        = p.smoothFile;     
231    this->usePrevious       = p.usePrevious;
232    this->objectList        = p.objectList;
233    this->flagLog           = p.flagLog;       
234    this->logFile           = p.logFile;       
235    this->outFile           = p.outFile;       
236    this->flagSeparateHeader= p.flagSeparateHeader;
237    this->headerFile        = p.headerFile;
238    this->flagWriteBinaryCatalogue = p.flagWriteBinaryCatalogue;
239    this->binaryCatalogue   = p.binaryCatalogue;
240    this->flagPlotSpectra   = p.flagPlotSpectra;
241    this->spectraFile       = p.spectraFile;   
242    this->flagPlotIndividualSpectra = p.flagPlotIndividualSpectra;
243    this->flagTextSpectra   = p.flagTextSpectra;   
244    this->spectraTextFile   = p.spectraTextFile;   
245    this->flagOutputBaseline    = p.flagOutputBaseline;
246    this->fileOutputBaseline    = p.fileOutputBaseline;
247    this->flagOutputMomentMap    = p.flagOutputMomentMap;
248    this->fileOutputMomentMap    = p.fileOutputMomentMap;
249    this->flagOutputMomentMask   = p.flagOutputMomentMask;
250    this->fileOutputMomentMask   = p.fileOutputMomentMask;
251    this->flagOutputMask    = p.flagOutputMask;
252    this->fileOutputMask    = p.fileOutputMask;
253    this->flagMaskWithObjectNum = p.flagMaskWithObjectNum;
254    this->flagOutputSmooth  = p.flagOutputSmooth;
255    this->fileOutputSmooth  = p.fileOutputSmooth;
256    this->flagOutputRecon   = p.flagOutputRecon;
257    this->fileOutputRecon   = p.fileOutputRecon;
258    this->flagOutputResid   = p.flagOutputResid;
259    this->fileOutputResid   = p.fileOutputResid;
260    this->flagVOT           = p.flagVOT;         
261    this->votFile           = p.votFile;       
262    this->flagKarma         = p.flagKarma;     
263    this->karmaFile         = p.karmaFile;     
264    this->flagDS9           = p.flagDS9;     
265    this->ds9File           = p.ds9File;     
266    this->flagCasa          = p.flagCasa;     
267    this->casaFile          = p.casaFile;     
268    this->annotationType    = p.annotationType;
269    this->flagMaps          = p.flagMaps;       
270    this->detectionMap      = p.detectionMap;   
271    this->momentMap         = p.momentMap;     
272    this->flagXOutput       = p.flagXOutput;       
273    this->precFlux          = p.precFlux;
274    this->precVel           = p.precVel;
275    this->precSNR           = p.precSNR;
276    this->flagNegative      = p.flagNegative;
277    this->flagBlankPix      = p.flagBlankPix;   
278    this->blankPixValue     = p.blankPixValue; 
279    this->blankKeyword      = p.blankKeyword;   
280    this->bscaleKeyword     = p.bscaleKeyword; 
281    this->bzeroKeyword      = p.bzeroKeyword;   
282    this->newFluxUnits      = p.newFluxUnits;
283    this->flaggedChannelList = p.flaggedChannelList;
284    this->flaggedChannels   = p.flaggedChannels;
285    this->flaggedChannelMask= p.flaggedChannelMask;
286    this->flagMW            = p.flagMW;         
287    this->maxMW             = p.maxMW;         
288    this->minMW             = p.minMW;         
289    this->areaBeam          = p.areaBeam;     
290    this->fwhmBeam          = p.fwhmBeam;     
291    this->beamAsUsed        = p.beamAsUsed;
292    this->searchType        = p.searchType;
293    this->flagTrim          = p.flagTrim;   
294    this->hasBeenTrimmed    = p.hasBeenTrimmed;   
295    this->borderLeft        = p.borderLeft;     
296    this->borderRight       = p.borderRight;   
297    this->borderBottom      = p.borderBottom;   
298    this->borderTop         = p.borderTop;     
299    if(this->sizeOffsets>0) delete [] this->offsets;
300    this->sizeOffsets       = p.sizeOffsets;
301    if(this->sizeOffsets>0){
302      this->offsets           = new long[this->sizeOffsets];
303      for(int i=0;i<this->sizeOffsets;i++) this->offsets[i] = p.offsets[i];
304    }
305    this->xSubOffset        = p.xSubOffset;     
306    this->ySubOffset        = p.ySubOffset;     
307    this->zSubOffset        = p.zSubOffset;
308    this->flagBaseline      = p.flagBaseline;
309    this->flagGrowth        = p.flagGrowth;
310    this->growthCut         = p.growthCut;
311    this->growthThreshold   = p.growthThreshold;
312    this->flagUserGrowthThreshold = p.flagUserGrowthThreshold;
313    this->flagFDR           = p.flagFDR;
314    this->alphaFDR          = p.alphaFDR;
315    this->FDRnumCorChan     = p.FDRnumCorChan;
316    this->flagStatSec       = p.flagStatSec;
317    this->statSec           = p.statSec;
318    this->flagRobustStats   = p.flagRobustStats;
319    this->snrCut            = p.snrCut;
320    this->threshold         = p.threshold;
321    this->flagUserThreshold = p.flagUserThreshold;
322    this->flagSmooth        = p.flagSmooth;
323    this->smoothType        = p.smoothType;
324    this->hanningWidth      = p.hanningWidth;
325    this->kernMaj           = p.kernMaj;
326    this->kernMin           = p.kernMin;
327    this->kernPA            = p.kernPA;
328    this->flagATrous        = p.flagATrous;
329    this->reconDim          = p.reconDim;
330    this->scaleMin          = p.scaleMin;
331    this->scaleMax          = p.scaleMax;
332    this->snrRecon          = p.snrRecon;
333    this->reconConvergence  = p.reconConvergence;
334    this->filterCode        = p.filterCode;
335    this->reconFilter       = p.reconFilter;
336    this->flagAdjacent      = p.flagAdjacent;
337    this->threshSpatial     = p.threshSpatial;
338    this->threshVelocity    = p.threshVelocity;
339    this->minChannels       = p.minChannels;
340    this->minPix            = p.minPix;
341    this->minVoxels         = p.minVoxels;
342    this->flagRejectBeforeMerge = p.flagRejectBeforeMerge;
343    this->flagTwoStageMerging = p.flagTwoStageMerging;
344    this->spectralMethod    = p.spectralMethod;
345    this->spectralType      = p.spectralType;
346    this->restFrequency     = p.restFrequency;
347    this->restFrequencyUsed = p.restFrequencyUsed;
348    this->spectralUnits     = p.spectralUnits;
349    this->pixelCentre       = p.pixelCentre;
350    this->sortingParam      = p.sortingParam;
351    this->borders           = p.borders;
352    this->blankEdge         = p.blankEdge;
353    this->verbose           = p.verbose;
354    this->commentStr        = p.commentStr;
355    return *this;
356  }
357  //--------------------------------------------------------------------
358
359  OUTCOME Param::getopts(int argc, char ** argv, std::string progname)
360  {
361    ///   A function that reads in the command-line options, in a manner
362    ///    tailored for use with the main Duchamp program.
363    ///
364    ///   \param argc The number of command line arguments.
365    ///   \param argv The array of command line arguments.
366
367    OUTCOME returnValue = FAILURE;
368    if(argc==1){
369      std::cout << ERR_USAGE_MSG;
370      returnValue = FAILURE;
371    }
372    else {
373      std::string file;
374      float thresh;
375      bool changeX = false, changeThresh=false;
376      this->defaultValues();
377      char c;
378      while( ( c = getopt(argc,argv,"p:f:t:hvx") )!=-1){
379        switch(c) {
380        case 'p':
381          file = optarg;
382          if(this->readParams(file)==FAILURE){
383            DUCHAMPERROR(progname,"Could not open parameter file " << file);
384          }
385          else returnValue = SUCCESS;
386          break;
387        case 'f':
388          file = optarg;
389          this->imageFile = file;
390          returnValue = SUCCESS;
391          break;
392        case 'v':
393          std::cout << PROGNAME << " version " << VERSION << std::endl;
394          break;
395        case 'x':
396          changeX = true;
397          break;
398        case 't':
399          thresh = atof(optarg);
400          changeThresh = true;
401          break;
402        case 'h':
403        default :
404          std::cout << ERR_USAGE_MSG;
405          break;
406        }
407      }
408      if(changeX){
409        if(returnValue == SUCCESS) this->setFlagXOutput(false);
410        else {
411          DUCHAMPERROR(progname, "You need to specify either a parameter file or FITS image.\n");
412          std::cout << "\n" << ERR_USAGE_MSG;
413        }
414      }
415      if(changeThresh){
416        this->threshold = thresh;
417        this->flagUserThreshold = true;
418        this->checkPars();
419      }
420    }
421    return returnValue;
422  }
423  //--------------------------------------------------------------------
424
425  bool Param::isBlank(float &value)
426  {
427    ///  Tests whether the value passed as the argument is BLANK or not.
428    ///  \param value Pixel value to be tested.
429    ///  \return False if flagBlankPix is false. Else, compare to the
430    ///  relevant FITS keywords, using integer comparison.
431
432    return this->flagBlankPix &&
433      (this->blankKeyword == int((value-this->bzeroKeyword)/this->bscaleKeyword));
434  }
435
436  bool *Param::makeBlankMask(float *array, size_t size)
437  {
438    ///  This returns an array of bools, saying whether each pixel in the
439    ///  given array is BLANK or not. If the pixel is BLANK, set mask to
440    ///  false, else set to true. The array is allocated by the function.
441
442    bool *mask = new bool[size];
443    for(size_t i=0;i<size;i++) mask[i] = !this->isBlank(array[i]);
444    return mask;
445  }
446
447
448  bool *Param::makeStatMask(float *array, size_t *dim)
449  {
450    ///  This returns an array of bools, saying whether each pixel in
451    ///  the given array is suitable for a stats calculation. It needs
452    ///  to be in the StatSec (if defined), not blank and not a MW
453    ///  channel. The array is allocated by the function with a 'new' call.
454
455    bool *mask = new bool[dim[0]*dim[1]*dim[2]];
456    std::vector<bool> flaggedChans = this->getChannelFlags(dim[2]);
457    for(size_t x=0;x<dim[0];x++) {
458      for(size_t y=0;y<dim[1];y++) {
459        for(size_t z=0;z<dim[2];z++) {
460          size_t i = x+y*dim[0]+z*dim[0]*dim[1];
461          // mask[i] = !this->isBlank(array[i]) && !this->isInMW(z) && this->isStatOK(x,y,z);
462          mask[i] = !this->isBlank(array[i]) && !flaggedChans[z] && this->isStatOK(x,y,z);
463        }
464      }
465    }
466    return mask;
467  }
468
469
470  bool Param::isInMW(int z)
471  {
472    ///  Tests whether we are flagging Milky Way channels, and if so
473    /// whether the given channel number is in the Milky Way range. The
474    /// channels are assumed to start at number 0. 
475    /// \param z The channel number
476    /// \return True if we are flagging Milky Way channels and z is in
477    ///  the range.
478
479    return ( this->flagMW && (z>=this->getMinMW()) && (z<=this->getMaxMW()) );
480  }
481
482    bool Param::isFlaggedChannel(int z)
483    {
484        /// Tests whether a given channel has been flagged by the user
485
486        // bool isFlagged=false;
487        // for(size_t i=0;i<this->flaggedChannels.size()&&!isFlagged;i++)
488        //     isFlagged=(z==this->flaggedChannels[i]);
489        // return isFlagged;
490
491        if(z>this->flaggedChannelMask.size() || z<0) return false;
492        else return this->flaggedChannelMask[z];
493
494    }
495
496    std::vector<bool> Param::getChannelFlags(int numChannels)
497    {
498
499        std::vector<bool> flags(numChannels,false);
500        for(std::vector<int>::iterator chan = this->flaggedChannels.begin(); chan!=this->flaggedChannels.end(); chan++){
501            // channels are zero-based, but channel-specification is 1-based
502            if(*chan<=numChannels && *chan>-1) flags[*chan-1] = true;
503        }
504        return flags;
505    }
506
507
508  bool Param::isStatOK(int x, int y, int z)
509  {
510    /// Test whether a given pixel position lies within the subsection
511    /// given by the statSec parameter. Only tested if the flagSubsection
512    /// parameter is true -- if it isn't, we just return true since all
513    /// pixels are therefore available for statstical calculations.
514    /// \param x X-value of pixel being tested.
515    /// \param y Y-value of pixel being tested.
516    /// \param z Z-value of pixel being tested.
517    /// \return True if pixel is able to be used for statistical
518    /// calculations. False otherwise.
519
520    int xval=x,yval=y,zval=z;
521    if(flagSubsection){
522      xval += pixelSec.getStart(0);
523      yval += pixelSec.getStart(1);
524      zval += pixelSec.getStart(2);
525    }
526    return !flagStatSec || statSec.isInside(xval,yval,zval);
527  }
528
529  std::vector<bool> Param::getObjectChoices()
530  {
531    ///  Returns a list of bool values, indicating whether a given
532    ///  object was requested or not. The size of the vector is
533    ///  determined by the maximum value in objectList. For instance,
534    ///  if objectList="2,3,5-8", then the returned vector will be
535    ///  [0,1,1,0,1,1,1,1].
536    ///  \return Vector of bool values.
537
538      if(this->objectListVector.size()==0) return std::vector<bool>(0);
539      else {
540          int maxNum = *std::max_element(this->objectListVector.begin(), this->objectListVector.end());
541          std::vector<bool> choices(maxNum,false);
542          for(std::vector<int>::iterator obj = objectListVector.begin();obj!=objectListVector.end();obj++)
543              choices[*obj-1] = true;
544          return choices;
545      }
546  }
547
548  std::vector<bool> Param::getObjectChoices(int numObjects)
549  {
550    ///  Returns a list of bool values, indicating whether a given
551    ///  object was requested or not. The size of the vector is given
552    ///  by the numObjects parameter. So, if objectList="2,3,5-8", then
553    ///  the returned vector from a getObjectChoices(10) call will be
554    ///  [0,1,1,0,1,1,1,1,0,0].
555    ///  \param numObjects How many objects there are in total.
556    ///  \return Vector of bool values.
557
558    if(this->objectList==""){
559      std::vector<bool> choices(numObjects,true);
560      return choices;
561    }
562    else{
563      std::vector<bool> choices(numObjects,false);
564      std::vector<int> duds,goodlist;
565      for(std::vector<int>::iterator obj = this->objectListVector.begin();obj!=this->objectListVector.end();obj++){
566          if(*obj<=numObjects) {
567              choices[*obj-1] = true;
568              goodlist.push_back(*obj);
569          }
570          else duds.push_back(*obj);
571      }
572      if(duds.size()>0){
573          this->objectListVector = goodlist;
574          std::stringstream dudlist;
575          dudlist << duds[0];
576          for(size_t i=1;i<duds.size();i++) dudlist << ","<<duds[i];
577          std::string obj="object";
578          if(duds.size()>1) obj+= "s";
579          DUCHAMPWARN("Object Selection", "You have only detected " << numObjects << " objects, so the requested "
580                      << obj << " '" << dudlist.str() << "' cannot be returned. Please check your request, as it doesn't match the results.");
581      }
582      return choices;
583    }
584
585  }
586
587  /****************************************************************/
588  /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///
589  /// / Other Functions using the  Parameter class:
590  /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///
591
592  OUTCOME Param::readParams(std::string paramfile)
593  {
594    /// The parameters are read in from a disk file, on the assumption that each
595    ///  line of the file has the format "parameter value" (eg. alphafdr 0.1)
596    ///
597    /// The case of the parameter name does not matter, nor does the
598    /// formatting of the spaces (it can be any amount of whitespace or
599    /// tabs).
600    ///
601    /// \param paramfile A std::string containing the parameter filename.
602    ///
603    /// \return FAILURE if the parameter file does not exist. SUCCESS if
604    /// it is able to read it.
605
606    if(!USE_PGPLOT){
607      // Change default values for these parameters when we don't use PGPlot
608      this->flagXOutput = false;
609      this->flagMaps = false;
610      this->flagPlotSpectra = false;
611    }
612
613    std::ifstream fin(paramfile.c_str());
614    if(!fin.is_open()) return FAILURE;
615    std::string line;
616    while( !std::getline(fin,line,'\n').eof()){
617
618      if(line[0]!='#'){
619        std::stringstream ss;
620        ss.str(line);
621        std::string arg;
622        ss >> arg;
623        arg = makelower(arg);
624        if(arg=="imagefile")       this->imageFile = readFilename(ss);
625        if(arg=="flagsubsection")  this->flagSubsection = readFlag(ss);
626        if(arg=="subsection")      this->pixelSec.setSection(readSval(ss));
627        if(arg=="flagreconexists") this->flagReconExists = readFlag(ss);
628        if(arg=="reconfile")       this->reconFile = readSval(ss);
629        if(arg=="flagsmoothexists")this->flagSmoothExists = readFlag(ss);
630        if(arg=="smoothfile")      this->smoothFile = readSval(ss);
631        if(arg=="beamarea")        this->areaBeam = readFval(ss);
632        if(arg=="beamfwhm")        this->fwhmBeam = readFval(ss);
633        if(arg=="useprevious")     this->usePrevious = readFlag(ss);
634        if(arg=="objectlist")      this->objectList = readSval(ss);
635
636        if(arg=="flaglog")         this->flagLog = readFlag(ss);
637        if(arg=="logfile")         this->logFile = readSval(ss);
638        if(arg=="outfile")         this->outFile = readSval(ss);
639        if(arg=="flagseparateheader") this->flagSeparateHeader = readFlag(ss);
640        if(arg=="headerfile")      this->headerFile = readFilename(ss);
641        if(arg=="flagwritebinarycatalogue") this->flagWriteBinaryCatalogue = readFlag(ss);
642        if(arg=="binarycatalogue") this->binaryCatalogue = readFilename(ss);
643        if(arg=="flagplotspectra") this->flagPlotSpectra = readFlag(ss);
644        if(arg=="spectrafile")     this->spectraFile = readFilename(ss);
645        if(arg=="flagplotindividualspectra") this->flagPlotIndividualSpectra = readFlag(ss);
646        if(arg=="flagtextspectra") this->flagTextSpectra = readFlag(ss);
647        if(arg=="spectratextfile") this->spectraTextFile = readFilename(ss);
648        if(arg=="flagoutputbaseline")  this->flagOutputBaseline = readFlag(ss);
649        if(arg=="fileoutputbaseline")  this->fileOutputBaseline = readFilename(ss);
650        if(arg=="flagoutputmomentmap")  this->flagOutputMomentMap = readFlag(ss);
651        if(arg=="fileoutputmomentmap")  this->fileOutputMomentMap = readFilename(ss);
652        if(arg=="flagoutputmomentmask")  this->flagOutputMomentMask = readFlag(ss);
653        if(arg=="fileoutputmomentmask")  this->fileOutputMomentMask = readFilename(ss);
654        if(arg=="flagoutputmask")  this->flagOutputMask = readFlag(ss);
655        if(arg=="fileoutputmask")  this->fileOutputMask = readFilename(ss);
656        if(arg=="flagmaskwithobjectnum") this->flagMaskWithObjectNum = readFlag(ss);
657        if(arg=="flagoutputsmooth")this->flagOutputSmooth = readFlag(ss);
658        if(arg=="fileoutputsmooth")this->fileOutputSmooth = readFilename(ss);
659        if(arg=="flagoutputrecon") this->flagOutputRecon = readFlag(ss);
660        if(arg=="fileoutputrecon") this->fileOutputRecon = readFilename(ss);
661        if(arg=="flagoutputresid") this->flagOutputResid = readFlag(ss);
662        if(arg=="fileoutputresid") this->fileOutputResid = readFilename(ss);
663        if(arg=="flagvot")         this->flagVOT = readFlag(ss);
664        if(arg=="votfile")         this->votFile = readFilename(ss);
665        if(arg=="flagkarma")       this->flagKarma = readFlag(ss);
666        if(arg=="karmafile")       this->karmaFile = readFilename(ss);
667        if(arg=="flagds9")         this->flagDS9 = readFlag(ss);
668        if(arg=="ds9file")         this->ds9File = readFilename(ss);
669        if(arg=="flagcasa")        this->flagCasa = readFlag(ss);
670        if(arg=="casafile")        this->casaFile = readFilename(ss);
671        if(arg=="annotationtype")  this->annotationType = readSval(ss);
672        if(arg=="flagmaps")        this->flagMaps = readFlag(ss);
673        if(arg=="detectionmap")    this->detectionMap = readFilename(ss);
674        if(arg=="momentmap")       this->momentMap = readFilename(ss);
675        if(arg=="flagxoutput")     this->flagXOutput = readFlag(ss);
676        if(arg=="newfluxunits")    this->newFluxUnits = readSval(ss);
677        if(arg=="precflux")        this->precFlux = readIval(ss);
678        if(arg=="precvel")         this->precVel = readIval(ss);
679        if(arg=="precsnr")         this->precSNR = readIval(ss);
680
681        if(arg=="flagtrim")        this->flagTrim = readFlag(ss);
682        if(arg=="flaggedchannels") this->flaggedChannelList = readSval(ss);
683        if(arg=="flagmw")          this->flagMW = readFlag(ss);
684        if(arg=="maxmw")           this->maxMW = readIval(ss);
685        if(arg=="minmw")           this->minMW = readIval(ss);
686        if(arg=="flagbaseline")    this->flagBaseline = readFlag(ss);
687        if(arg=="searchtype")      this->searchType = readSval(ss);
688
689        if(arg=="flagnegative")    this->flagNegative = readFlag(ss);
690        if(arg=="minpix")          this->minPix = readIval(ss);
691        if(arg=="flaggrowth")      this->flagGrowth = readFlag(ss);
692        if(arg=="growthcut")       this->growthCut = readFval(ss);
693        if(arg=="growththreshold"){
694          this->growthThreshold = readFval(ss);
695          this->flagUserGrowthThreshold = true;
696        }
697
698        if(arg=="flagfdr")         this->flagFDR = readFlag(ss);
699        if(arg=="alphafdr")        this->alphaFDR = readFval(ss);
700        if(arg=="fdrnumcorchan")   this->FDRnumCorChan = readIval(ss);
701        if(arg=="flagstatsec")     this->flagStatSec = readFlag(ss);
702        if(arg=="statsec")         this->statSec.setSection(readSval(ss));
703        if(arg=="flagrobuststats") this->flagRobustStats = readFlag(ss);
704        if(arg=="snrcut")          this->snrCut = readFval(ss);
705        if(arg=="threshold"){
706          this->threshold = readFval(ss);
707          this->flagUserThreshold = true;
708        }
709     
710        if(arg=="flagsmooth")      this->flagSmooth = readFlag(ss);
711        if(arg=="smoothtype")      this->smoothType = readSval(ss);
712        if(arg=="hanningwidth")    this->hanningWidth = readIval(ss);
713        if(arg=="kernmaj")         this->kernMaj = readFval(ss);
714        if(arg=="kernmin")         this->kernMin = readFval(ss);
715        if(arg=="kernpa")          this->kernPA = readFval(ss);
716
717        if(arg=="flagatrous")      this->flagATrous = readFlag(ss);
718        if(arg=="recondim")        this->reconDim = readIval(ss);
719        if(arg=="scalemin")        this->scaleMin = readIval(ss);
720        if(arg=="scalemax")        this->scaleMax = readIval(ss);
721        if(arg=="snrrecon")        this->snrRecon = readFval(ss);
722        if(arg=="reconconvergence") this->reconConvergence = readFval(ss);
723        if(arg=="filtercode")      this->filterCode = readIval(ss);
724
725        if(arg=="flagadjacent")    this->flagAdjacent = readFlag(ss);
726        if(arg=="threshspatial")   this->threshSpatial = readFval(ss);
727        if(arg=="threshvelocity")  this->threshVelocity = readFval(ss);
728        if(arg=="minchannels")     this->minChannels = readIval(ss);
729        if(arg=="minvoxels")       this->minVoxels = readIval(ss);
730        if(arg=="flagrejectbeforemerge") this->flagRejectBeforeMerge = readFlag(ss);
731        if(arg=="flagtwostagemerging") this->flagTwoStageMerging = readFlag(ss);
732
733        if(arg=="spectralmethod")  this->spectralMethod=makelower(readSval(ss));
734        if(arg=="spectraltype")    this->spectralType = readSval(ss);
735        if(arg=="restfrequency")   this->restFrequency = readFval(ss);
736        if(arg=="spectralunits")   this->spectralUnits = readSval(ss);
737        if(arg=="pixelcentre")     this->pixelCentre = makelower(readSval(ss));
738        if(arg=="sortingparam")    this->sortingParam = makelower(readSval(ss));
739        if(arg=="drawborders")     this->borders = readFlag(ss);
740        if(arg=="drawblankedges")  this->blankEdge = readFlag(ss);
741        if(arg=="verbose")         this->verbose = readFlag(ss);
742
743        // Dealing with deprecated parameters.
744        if(arg=="flagblankpix"){
745          this->flagTrim = readFlag(ss);
746          DUCHAMPWARN("Reading parameters","The parameter flagBlankPix is deprecated. Please use the flagTrim parameter in future.");
747          DUCHAMPWARN("Reading parameters","Setting flagTrim = " << stringize(this->flagTrim));
748        }
749        if(arg=="blankpixvalue"){
750          DUCHAMPWARN("Reading parameters","The parameter blankPixValue is deprecated. This value is only taken from the FITS header.");
751        }
752        if(arg=="beamsize"){
753          this->areaBeam = readFval(ss);
754          DUCHAMPWARN("Reading parameters","The parameter beamSize is deprecated. You can specify the beam size by beamArea or beamFWHM.");
755          DUCHAMPWARN("Reading parameters","Setting beamArea = " << this->areaBeam);
756        }
757
758      }
759    }
760
761    this->checkPars();
762
763    return SUCCESS;
764
765  }
766 
767  void Param::checkPars()
768  {
769
770    // If flagSubsection is false, but the parset had a subsection string in it, we want to set this back to the default.
771    if(!this->flagSubsection){
772      this->pixelSec.setSection(defaultSection);
773    }
774    if(!this->flagStatSec){
775      this->statSec.setSection(defaultSection);
776    }
777
778    // If we have usePrevious=false, set the objectlist to blank so that we use all of them. Otherwise, define the vector list of choices.
779    if(!this->usePrevious) this->objectList = "";
780    else this->objectListVector = selectionToIntVec(this->objectList);
781
782    // Defining the vector list of flagged channels
783    this->flaggedChannels = selectionToIntVec(this->flaggedChannelList);
784    this->flaggedChannelMask = std::vector<bool>(*std::max_element(this->flaggedChannels.begin(),this->flaggedChannels.end()),false);
785    for(size_t i=0;i<this->flaggedChannels.size();i++) this->flaggedChannelMask[ this->flaggedChannels[i] ] = true;
786
787    std::cerr << flaggedChannels[0];
788    for(size_t i=1;i<flaggedChannels.size();i++) std::cerr << " " << flaggedChannels[i];
789    std::cerr << "\n";
790
791    // If pgplot was not included in the compilation, need to set flagXOutput to false
792    if(!USE_PGPLOT){
793      if(this->flagXOutput || this->flagMaps || this->flagPlotSpectra || this->flagPlotIndividualSpectra)
794        DUCHAMPWARN("Reading parameters","PGPlot has not been enabled, so setting flagXOutput, flagMaps, flagPlotSpectra and flagPlotIndividualSpectra to false.");
795      this->flagXOutput = false;
796      this->flagMaps = false;
797      this->flagPlotSpectra = false;
798      this->flagPlotIndividualSpectra = false;
799    }
800
801    if(!this->flagPlotSpectra && this->flagPlotIndividualSpectra){
802      DUCHAMPWARN("Reading parameters","flagPlotSpectra is false, so setting flagPlotIndividualSpectra to false as well.");
803      this->flagPlotIndividualSpectra = false;
804    }
805
806    // Correcting bad precision values -- if negative, set to 0
807    if(this->precFlux<0) this->precFlux = 0;
808    if(this->precVel<0)  this->precVel = 0;
809    if(this->precSNR<0)  this->precSNR = 0;
810
811    // Can only have "spatial" or "spectral" as search types
812    if(this->searchType != "spatial" && this->searchType != "spectral"){
813      DUCHAMPWARN("Reading parameters","You have requested a search type of \""<<this->searchType<<"\" -- Only \"spectral\" and \"spatial\" are accepted, so setting to \"spatial\".");
814      this->searchType = "spatial";
815    }
816
817    // The wavelet reconstruction takes precendence over the smoothing.
818    if(this->flagATrous) this->flagSmooth = false;
819
820    // Check validity of recon parameters
821    if(this->flagATrous){
822      if(this->reconConvergence < 0.){
823        DUCHAMPWARN("Reading Parameters","Your reconConvergence value is negative ("<<this->reconConvergence<<") - setting to " << -this->reconConvergence <<".");
824        this->reconConvergence *= -1.;
825      }
826
827      this->reconFilter.define(this->filterCode);
828
829      if((this->scaleMax) > 0 && (this->scaleMax < this->scaleMin)){
830        DUCHAMPWARN("Reading Parameters","Reconstruction scaleMax ("<<this->scaleMax<<") is less than scaleMin ("<<this->scaleMin<<"): setting both to "<<this->scaleMin);
831        this->scaleMax = this->scaleMin;
832      }
833
834      if( (this->reconDim < 1) || (this->reconDim > 3) ){
835        DUCHAMPWARN("Reading Parameters", "You requested a " << this->reconDim << " dimensional reconstruction. Setting reconDim to 1");
836        this->reconDim = 1;
837      }
838
839      if( this->snrRecon < 0.){
840        DUCHAMPWARN("Reading Parameters", "Your snrRecon value is negative (" << this->snrRecon<<"). Turning reconstruction off -- fix your parameter file!");
841        this->flagATrous = false;
842      }
843
844    }
845
846    if(this->flagUserThreshold){
847
848      // If we specify a manual threshold, need to also specify a manual growth threshold
849      // If we haven't done so, turn growing off
850      if(this->flagGrowth && !this->flagUserGrowthThreshold){
851        DUCHAMPWARN("Reading parameters","You have specified a manual search threshold, but not a manual growth threshold. You need to do so using the \"growthThreshold\" parameter.");
852        DUCHAMPWARN("Reading parameters","The growth function is being turned off.");
853        this->flagGrowth = false;
854      }
855
856      // If we specify a manual threshold, we don't need the FDR method, so turn it off if requested.
857      if(this->flagFDR){
858        DUCHAMPWARN("Reading parameters","You have specified a manual search threshold, so we don't need to use the FDR method. Setting \"flagFDR=false\".");
859        this->flagFDR = false;
860      }
861
862    }   
863
864    // Make sure the growth level is less than the detection level. Else turn off growing.
865    if(this->flagGrowth){
866      std::stringstream errmsg;
867      bool doWarn = false;
868      if(this->flagUserThreshold &&
869         ( (this->threshold < this->growthThreshold)
870           || (this->snrCut < this->growthCut) ) ){
871        errmsg << "Your \"growthThreshold\" parameter" << this->growthThreshold <<" is larger than your \"threshold\"" << this->threshold;
872        doWarn = true;
873      }
874     
875      if(!this->flagUserThreshold &&
876         (this->snrCut < this->growthCut)) {
877        errmsg << "Your \"growthCut\" parameter " << this->growthCut << " is larger than your \"snrCut\"" << this->snrCut;
878        doWarn = true;
879      }
880
881      if(doWarn){
882        DUCHAMPWARN("Reading parameters",errmsg);
883        DUCHAMPWARN("Reading parameters","The growth function is being turned off.");
884
885      }
886    }
887
888    // Make sure the annnotationType is an acceptable option -- default is "borders"
889    if((this->annotationType != "borders") && (this->annotationType!="circles") && (this->annotationType!="ellipses")){
890      DUCHAMPWARN("Reading parameters","The requested value of the parameter annotationType, \"" << this->annotationType << "\", is invalid -- changing to \"borders\".");
891      this->annotationType = "borders";
892    }
893     
894    // Make sure smoothType is an acceptable type -- default is "spectral"
895    if((this->smoothType!="spectral")&&
896       (this->smoothType!="spatial")){
897      DUCHAMPWARN("Reading parameters","The requested value of the parameter smoothType, \"" << this->smoothType << "\", is invalid -- changing to \"spectral\".");
898      this->smoothType = "spectral";
899    }
900    // If kernMin has not been given, or is negative, make it equal to kernMaj
901    if(this->kernMin < 0) this->kernMin = this->kernMaj;
902
903    // Make sure spectralMethod is an acceptable type -- default is "peak"
904    if((this->spectralMethod!="peak")&&
905       (this->spectralMethod!="sum")){
906      DUCHAMPWARN("Reading parameters","The requested value of the parameter spectralMethod, \"" << this->spectralMethod << "\", is invalid -- changing to \"peak\".");
907      this->spectralMethod = "peak";
908    }
909
910    // make sure pixelCentre is an acceptable type -- default is "peak"
911    if((this->pixelCentre!="centroid")&&
912       (this->pixelCentre!="average") &&
913       (this->pixelCentre!="peak")       ){
914      DUCHAMPWARN("Reading parameters","The requested value of the parameter pixelCentre, \"" << this->pixelCentre << "\", is invalid -- changing to \"centroid\".");
915      this->pixelCentre = "centroid";
916    }
917
918    // Make sure sortingParam is an acceptable type -- default is "vel"
919    bool OK = false;
920    int loc=(this->sortingParam[0]=='-') ? 1 : 0;
921    for(int i=0;i<numSortingParamOptions;i++)
922      OK = OK || this->sortingParam.substr(loc)==sortingParamOptions[i];
923    if(!OK){
924      DUCHAMPWARN("Reading parameters","The requested value of the parameter sortingParam, \"" << this->sortingParam << "\", is invalid. -- changing to \"vel\".");
925      this->sortingParam = "vel";
926    }
927
928    // Make sure minVoxels is appropriate given minChannels & minPixels
929    if(this->minVoxels < (this->minPix + this->minChannels - 1) ){
930      DUCHAMPWARN("Reading parameters","Changing minVoxels to " << this->minPix + this->minChannels - 1 << " given minPix="<<this->minPix << " and minChannels="<<this->minChannels);
931      this->minVoxels = this->minPix + this->minChannels - 1;
932    }
933
934    // check that baselines are being calculated if we want to save them to a FITS file
935    if(this->flagOutputBaseline && !this->flagBaseline){
936      DUCHAMPWARN("Reading parameters","Saving of baseline values to a FITS file has been requested, but baselines are not being calculated. Turning off saving of baseline values.");
937      this->flagOutputBaseline = false;
938    }
939     
940  }
941
942  OUTCOME Param::checkImageExists()
943  {
944    /// A simple check to see whether the image actually exists or not, using the cfitsio interface.
945    /// If it does, we return SUCCESS, otherwise we throw an exception.
946
947    int exists,status = 0;  /* MUST initialize status */
948    fits_file_exists(this->imageFile.c_str(),&exists,&status);
949    if(exists<=0){
950      fits_report_error(stderr, status);
951      DUCHAMPTHROW("Cube Reader","Requested image " << this->imageFile << " does not exist!");
952      return FAILURE;
953    }
954    return SUCCESS;
955  }
956
957
958  void recordParameters(std::ostream& theStream, Param &par, std::string paramName, std::string paramDesc, std::string paramValue)
959  {
960   
961    const int width = 60;
962    int widthText = width - paramName.size();
963
964    theStream << par.commentString()
965              << std::setw(widthText) << paramDesc
966              << setiosflags(std::ios::right) << paramName
967              << "  =  " << resetiosflags(std::ios::right) << paramValue
968              <<std::endl;
969  }
970
971  std::string fileOption(bool flag, std::string file)
972  {
973    std::ostringstream ss;
974    ss << stringize(flag);
975    if(flag) ss << " --> " << file;
976    return ss.str();
977   
978  }
979
980  std::ostream& operator<< ( std::ostream& theStream, Param& par)
981  {
982    /// Print out the parameter set in a formatted, easy to read style.
983    /// Lists the parameters, a description of them, and their value.
984
985    // BUG -- can get error: `boolalpha' is not a member of type `ios' -- old compilers: gcc 2.95.3?
986    //   theStream.setf(std::ios::boolalpha);
987    theStream.setf(std::ios::left);
988    theStream  <<par.commentString()<<"\n"<<par.commentString()<<"---- Parameters ----"<<std::endl;
989    theStream  << std::setfill('.');
990    if(par.getFlagSubsection())
991      recordParam(theStream, par, "[imageFile]", "Image to be analysed", par.getImageFile()<<par.getSubsection());
992    else
993      recordParam(theStream, par, "[imageFile]", "Image to be analysed", par.getImageFile());
994    if(par.getFlagRestFrequencyUsed()){
995      recordParam(theStream, par, "[restFrequency]","Rest frequency as used", par.getRestFrequency());
996    }
997    if(par.getFlagReconExists() && par.getFlagATrous()){
998      recordParam(theStream, par, "[reconExists]", "Reconstructed array exists?", stringize(par.getFlagReconExists()));
999      recordParam(theStream, par, "[reconFile]", "FITS file containing reconstruction", par.getReconFile());
1000    }
1001    if(par.getFlagSmoothExists() && par.getFlagSmooth()){
1002      recordParam(theStream, par, "[smoothExists]", "Smoothed array exists?", stringize(par.getFlagSmoothExists()));
1003      recordParam(theStream, par, "[smoothFile]", "FITS file containing smoothed array", par.getSmoothFile());
1004    }
1005    recordParam(theStream, par, "[logFile]", "Intermediate Logfile", par.logFile);
1006    recordParam(theStream, par, "[outFile]", "Final Results file", par.getOutFile());
1007    if(par.getFlagSeparateHeader()){
1008      recordParam(theStream, par, "[headerFile]", "Header for results file", par.getHeaderFile());
1009    }
1010    if(USE_PGPLOT && par.getFlagPlotSpectra()){
1011      std::string label=par.getSpectraFile();
1012      if(par.getFlagPlotIndividualSpectra()) label += ", with individual spectra too";
1013      recordParam(theStream, par, "[spectraFile]", "Spectrum file", label);
1014    }
1015    if(par.getFlagTextSpectra()){
1016      recordParam(theStream, par, "[spectraTextFile]", "Text file with ascii spectral data", par.getSpectraTextFile());
1017    }
1018    if(par.getFlagVOT()){
1019      recordParam(theStream, par, "[votFile]", "VOTable file", par.getVOTFile());
1020    }
1021    if(par.getFlagKarma()){
1022      recordParam(theStream, par, "[karmaFile]", "Karma annotation file" , par.getKarmaFile());
1023    }
1024    if(par.getFlagDS9()){
1025      recordParam(theStream, par, "[ds9File]", "DS9 annotation file" , par.getDS9File());
1026    }
1027    if(par.getFlagCasa()){
1028      recordParam(theStream, par, "[casaFile]", "CASA annotation file" , par.getCasaFile());
1029    }
1030    if(USE_PGPLOT && par.getFlagMaps()){
1031      recordParam(theStream, par, "[momentMap]", "0th Moment Map", par.getMomentMap());
1032      recordParam(theStream, par, "[detectionMap]", "Detection Map", par.getDetectionMap());
1033    }
1034    if(USE_PGPLOT){
1035      recordParam(theStream, par, "[flagXOutput]", "Display a map in a pgplot xwindow?", stringize(par.getFlagXOutput()));
1036    }
1037    if(par.getFlagATrous()){
1038      recordParam(theStream, par, "[flagOutputRecon]", "Saving reconstructed cube?", fileOption(par.getFlagOutputRecon(),par.outputReconFile()));
1039      recordParam(theStream, par, "[flagOutputResid]", "Saving residuals from reconstruction?", fileOption(par.getFlagOutputResid(),par.outputResidFile()));
1040    }                                                 
1041    if(par.getFlagSmooth()){   
1042      recordParam(theStream, par, "[flagOutputSmooth]", "Saving smoothed cube?", fileOption(par.getFlagOutputSmooth(),par.outputSmoothFile()));
1043    }                                                 
1044    recordParam(theStream, par, "[flagOutputMask]", "Saving mask cube?", fileOption(par.getFlagOutputMask(),par.outputMaskFile()));
1045    recordParam(theStream, par, "[flagOutputMomentMap]", "Saving 0th moment to FITS file?", fileOption(par.getFlagOutputMomentMap(),par.outputMomentMapFile()));
1046    recordParam(theStream, par, "[flagOutputMomentMask]", "Saving 0th moment mask to FITS file?", fileOption(par.getFlagOutputMomentMask(),par.outputMomentMaskFile()));
1047    recordParam(theStream, par, "[flagOutputBaseline]", "Saving baseline values to FITS file?", fileOption(par.getFlagOutputBaseline(),par.outputBaselineFile()));
1048
1049    theStream  << par.commentString() <<"------"<<std::endl;
1050
1051    recordParam(theStream, par, "[searchType]", "Type of searching performed", par.getSearchType());
1052    if(par.getFlagBlankPix()){
1053      recordParam(theStream, par, "", "Blank Pixel Value", par.getBlankPixVal());
1054    }
1055    recordParam(theStream, par, "[flagTrim]", "Trimming Blank Pixels?", stringize(par.getFlagTrim()));
1056    recordParam(theStream, par, "[flagNegative]", "Searching for Negative features?", stringize(par.getFlagNegative()));
1057    recordParam(theStream, par, "[flagMW]", "Removing Milky Way channels?", stringize(par.getFlagMW()));
1058    if(par.getFlagMW()){
1059      // need to remove the offset correction, as we want to report the parameters actually entered
1060      recordParam(theStream, par, "[minMW - maxMW]", "Milky Way Channels", par.getMinMW()+par.getZOffset()<<"-"<<par.getMaxMW()+par.getZOffset());
1061    }
1062    if(par.getFlaggedChannelList().size()>0){
1063        recordParam(theStream, par, "[flaggedChannels]", "Channels flagged by user", par.getFlaggedChannelList());
1064    }
1065    if(par.beamAsUsed.origin()==EMPTY){  // No beam in FITS file and no information provided
1066      recordParam(theStream, par, "", "Area of Beam", "No beam");
1067    }
1068    else if(par.beamAsUsed.origin()==HEADER){ // Report beam size from FITS file
1069      recordParam(theStream, par, "", "Area of Beam (pixels)", par.beamAsUsed.area() << "   (beam: " << par.beamAsUsed.maj() << " x " << par.beamAsUsed.min() <<" pixels)");
1070    }
1071    else if(par.beamAsUsed.origin()==PARAM){ // Report beam size requested in parameter set input
1072      if(par.fwhmBeam>0.) recordParam(theStream, par, "[beamFWHM]", "FWHM of Beam (pixels)", par.beamAsUsed.maj() << "   (beam area = " << par.beamAsUsed.area() <<" pixels)");
1073      else  recordParam(theStream, par, "[beamArea]", "Area of Beam (pixels)", par.beamAsUsed.area());
1074    }
1075    else{
1076      recordParam(theStream, par, "[beam info]", "Size & shape of beam", "No information available!");
1077    }
1078    recordParam(theStream, par, "[flagBaseline]", "Removing baselines before search?", stringize(par.getFlagBaseline()));
1079    recordParam(theStream, par, "[flagSmooth]", "Smoothing data prior to searching?", stringize(par.getFlagSmooth()));
1080    if(par.getFlagSmooth()){           
1081      recordParam(theStream, par, "[smoothType]", "Type of smoothing", par.getSmoothType());
1082      if(par.getSmoothType()=="spectral")
1083        recordParam(theStream, par, "[hanningWidth]", "Width of hanning filter", par.getHanningWidth());
1084      else{
1085        recordParam(theStream, par, "[kernMaj]", "Gaussian kernel semi-major axis [pix]", par.getKernMaj());
1086        recordParam(theStream, par, "[kernMin]", "Gaussian kernel semi-minor axis [pix]", par.getKernMin());
1087        recordParam(theStream, par, "[kernPA]",  "Gaussian kernel position angle [deg]",  par.getKernPA());
1088      }
1089    }
1090    recordParam(theStream, par, "[flagATrous]", "Using A Trous reconstruction?", stringize(par.getFlagATrous()));
1091    if(par.getFlagATrous()){                           
1092      recordParam(theStream, par, "[reconDim]", "Number of dimensions in reconstruction", par.getReconDim());
1093      if(par.getMaxScale()>0){
1094        recordParam(theStream, par, "[scaleMin-scaleMax]", "Scales used in reconstruction", par.getMinScale()<<"-"<<par.getMaxScale());
1095      }
1096      else{
1097        recordParam(theStream, par, "[scaleMin]", "Minimum scale in reconstruction", par.getMinScale());
1098      }
1099      recordParam(theStream, par, "[snrRecon]", "SNR Threshold within reconstruction", par.getAtrousCut());
1100      recordParam(theStream, par, "[reconConvergence]", "Residual convergence criterion", par.getReconConvergence());
1101      recordParam(theStream, par, "[filterCode]", "Filter being used for reconstruction", par.getFilterCode()<<" ("<<par.getFilterName()<<")");
1102    }                                                 
1103    recordParam(theStream, par, "[flagRobustStats]", "Using Robust statistics?", stringize(par.getFlagRobustStats()));
1104    if(par.getFlagStatSec()){
1105      recordParam(theStream, par, "[statSec]", "Section used by statistics calculation", par.statSec.getSection());
1106    }
1107    recordParam(theStream, par, "[flagFDR]", "Using FDR analysis?", stringize(par.getFlagFDR()));
1108    if(par.getFlagFDR()){                                     
1109      recordParam(theStream, par, "[alphaFDR]", "Alpha value for FDR analysis", par.getAlpha());
1110      recordParam(theStream, par, "[FDRnumCorChan]", "Number of correlated channels for FDR", par.getFDRnumCorChan());
1111    }                                                 
1112    else {
1113      if(par.getFlagUserThreshold()){
1114        recordParam(theStream, par, "[threshold]", "Detection Threshold", par.getThreshold());
1115      }
1116      else{
1117        recordParam(theStream, par, "[snrCut]", "SNR Threshold (in sigma)", par.getCut());
1118      }
1119    }
1120    recordParam(theStream, par, "[minPix]", "Minimum # Pixels in a detection", par.getMinPix());
1121    recordParam(theStream, par, "[minChannels]", "Minimum # Channels in a detection", par.getMinChannels());
1122    recordParam(theStream, par, "[minVoxels]", "Minimum # Voxels in a detection", par.getMinVoxels());
1123    recordParam(theStream, par, "[flagGrowth]", "Growing objects after detection?", stringize(par.getFlagGrowth()));
1124    if(par.getFlagGrowth()) {                         
1125      if(par.getFlagUserGrowthThreshold()){
1126        recordParam(theStream, par, "[growthThreshold]", "Threshold for growth", par.getGrowthThreshold());
1127      }
1128      else{
1129        recordParam(theStream, par, "[growthCut]", "SNR Threshold for growth", par.getGrowthCut());
1130      }
1131    }
1132    recordParam(theStream, par, "[flagAdjacent]", "Using Adjacent-pixel criterion?", stringize(par.getFlagAdjacent()));
1133    if(!par.getFlagAdjacent()){
1134      recordParam(theStream, par, "[threshSpatial]", "Max. spatial separation for merging", par.getThreshS());
1135    }
1136    recordParam(theStream, par, "[threshVelocity]", "Max. velocity separation for merging", par.getThreshV());
1137    recordParam(theStream, par, "[flagRejectBeforeMerge]", "Reject objects before merging?", stringize(par.getFlagRejectBeforeMerge()));
1138    recordParam(theStream, par, "[flagTwoStageMerging]", "Merge objects in two stages?", stringize(par.getFlagTwoStageMerging()));
1139    recordParam(theStream, par, "[spectralMethod]", "Method of spectral plotting", par.getSpectralMethod());
1140    recordParam(theStream, par, "[pixelCentre]", "Type of object centre used in results", par.getPixelCentre());
1141
1142    theStream  << par.commentString() <<"--------------------\n";
1143    theStream  << std::setfill(' ');
1144    theStream.unsetf(std::ios::left);
1145    //  theStream.unsetf(std::ios::boolalpha);
1146    return theStream;
1147  }
1148
1149  std::vector<VOParam> Param::getVOParams()
1150  {
1151    std::vector<VOParam> vopars;
1152    vopars.push_back(VOParam("imageFile","meta.file;meta.fits","char",this->imageFile,this->imageFile.size(),""));
1153    vopars.push_back(VOParam("flagSubsection","meta.code","boolean",this->flagSubsection,0,""));
1154    if(this->flagSubsection)
1155      vopars.push_back(VOParam("subsection","","char",this->pixelSec.getSection(),this->pixelSec.getSection().size(),""));
1156    vopars.push_back(VOParam("flagStatSec","meta.code","boolean",this->flagStatSec,0,""));
1157    if(this->flagSubsection)
1158      vopars.push_back(VOParam("StatSec","","char",this->statSec.getSection(),this->statSec.getSection().size(),""));
1159    if(this->flagReconExists)
1160      vopars.push_back(VOParam("reconfile","meta.file;meta.fits","char",this->reconFile, this->reconFile.size(),""));
1161    if(this->flagSmoothExists)
1162      vopars.push_back(VOParam("smoothfile","meta.file;meta.fits","char",this->smoothFile, this->smoothFile.size(),""));
1163    if(this->usePrevious)
1164      vopars.push_back(VOParam("objectlist","","char",this->objectList,this->objectList.size(),""));
1165
1166    vopars.push_back(VOParam("searchType","meta.note","char",this->searchType,this->searchType.size(),""));
1167    vopars.push_back(VOParam("flagNegative","meta.code","boolean",this->flagNegative,0,""));
1168    vopars.push_back(VOParam("flagBaseline","meta.code","boolean",this->flagBaseline,0,""));
1169    vopars.push_back(VOParam("flagRobustStats","meta.code","boolean",this->flagRobustStats,0,""));
1170    vopars.push_back(VOParam("flagFDR","meta.code","boolean",this->flagFDR,0,""));
1171    if(this->flagFDR){
1172      vopars.push_back(VOParam("alphaFDR","stat.param","float",this->alphaFDR,0,""));
1173      vopars.push_back(VOParam("FDRnumCorChan","stat.param","int",this->FDRnumCorChan,0,""));
1174    }
1175    else{
1176      if(this->flagUserThreshold)
1177            vopars.push_back(VOParam("threshold","phot.flux;stat.min","float",this->threshold,0,""));
1178      else
1179        vopars.push_back(VOParam("snrCut","stat.snr;phot;stat.min","float",this->snrCut,0,""));
1180    }
1181    vopars.push_back(VOParam("flagGrowth","meta.code","boolean",this->flagGrowth,0,""));
1182    if(this->flagGrowth){
1183      if(this->flagUserGrowthThreshold)
1184        vopars.push_back(VOParam("growthThreshold","phot.flux;stat.min","float",this->growthThreshold,0,""));
1185      else
1186        vopars.push_back(VOParam("growthCut","stat.snr;phot;stat.min","float",this->growthCut,0,""));
1187    }
1188    vopars.push_back(VOParam("minVoxels","","int",minVoxels,0,""));
1189    vopars.push_back(VOParam("minPix","","int",minPix,0,""));
1190    vopars.push_back(VOParam("minChannels","","int",minChannels,0,""));
1191    vopars.push_back(VOParam("flagAdjacent","meta.code","boolean",this->flagAdjacent,0,""));
1192    if(!this->flagAdjacent)
1193      vopars.push_back(VOParam("threshSpatial","","float",this->threshSpatial,0,""));
1194    vopars.push_back(VOParam("threshVelocity","","float",this->threshVelocity,0,""));
1195    vopars.push_back(VOParam("flagRejectBeforeMerge","","boolean",this->flagRejectBeforeMerge,0,""));
1196    vopars.push_back(VOParam("flagTwoStageMerging","","boolean",this->flagTwoStageMerging,0,""));
1197    vopars.push_back(VOParam("pixelCentre","","char",this->pixelCentre,this->pixelCentre.size(),""));
1198    vopars.push_back(VOParam("flagSmooth","meta.code","boolean",this->flagSmooth,0,""));
1199    if(this->flagSmooth){
1200      vopars.push_back(VOParam("smoothType","","char",this->smoothType,this->smoothType.size(),""));
1201      if(this->smoothType=="spectral")
1202        vopars.push_back(VOParam("hanningWidth","","int",this->hanningWidth,0,""));
1203      else{
1204        vopars.push_back(VOParam("kernMaj","","float",this->kernMaj,0,""));
1205        vopars.push_back(VOParam("kernMin","","float",this->kernMin,0,""));
1206        vopars.push_back(VOParam("kernPA","","float",this->kernPA,0,""));
1207      }
1208    }
1209    vopars.push_back(VOParam("flagATrous","meta.code","boolean",this->flagATrous,0,""));
1210    if(this->flagATrous){
1211      vopars.push_back(VOParam("reconDim","","int",this->reconDim,0,""));
1212      vopars.push_back(VOParam("scaleMin","","int",this->scaleMin,0,""));
1213      if(this->scaleMax>0)
1214        vopars.push_back(VOParam("scaleMax","","int",this->scaleMax,0,""));
1215      vopars.push_back(VOParam("snrRecon","","float",this->snrRecon,0,""));
1216      vopars.push_back(VOParam("reconConvergence","","float",this->reconConvergence,0,""));
1217      vopars.push_back(VOParam("filterCode","","int",this->filterCode,0,""));
1218    }
1219    if(this->beamAsUsed.origin()==PARAM){
1220      if(this->fwhmBeam>0)
1221        vopars.push_back(VOParam("beamFWHM","","float",this->fwhmBeam,0,""));
1222      else
1223        vopars.push_back(VOParam("beamArea","","float",this->areaBeam,0,""));
1224    }
1225    if(this->restFrequencyUsed){
1226      vopars.push_back(VOParam("restFrequency","em.freq","float",this->restFrequency,0,"Hz"));
1227    }
1228
1229    return vopars;
1230
1231  }
1232
1233  void Param::writeToBinaryFile(std::string &filename)
1234  {
1235    std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary | std::ios::app);
1236    writeStringToBinaryFile(outfile,this->imageFile);
1237    outfile.write(reinterpret_cast<const char*>(&this->flagSubsection), sizeof this->flagSubsection);
1238    if(this->flagSubsection) writeStringToBinaryFile(outfile,this->pixelSec.getSection());
1239    outfile.write(reinterpret_cast<const char*>(&this->flagStatSec), sizeof this->flagStatSec);
1240    if(this->flagStatSec) writeStringToBinaryFile(outfile,this->statSec.getSection());
1241    outfile.write(reinterpret_cast<const char*>(&this->flagReconExists), sizeof this->flagReconExists);
1242    if(this->flagReconExists) writeStringToBinaryFile(outfile,this->reconFile);
1243    outfile.write(reinterpret_cast<const char*>(&this->flagSmoothExists), sizeof this->flagSmoothExists);
1244    if(this->flagSmoothExists) writeStringToBinaryFile(outfile,this->smoothFile);
1245    //
1246    writeStringToBinaryFile(outfile,this->searchType);
1247    outfile.write(reinterpret_cast<const char*>(&this->flagNegative), sizeof this->flagNegative);
1248    outfile.write(reinterpret_cast<const char*>(&this->flagBaseline), sizeof this->flagBaseline);
1249    outfile.write(reinterpret_cast<const char*>(&this->flagRobustStats), sizeof this->flagRobustStats);
1250    outfile.write(reinterpret_cast<const char*>(&this->flagFDR), sizeof this->flagFDR);
1251    if(this->flagFDR){
1252      outfile.write(reinterpret_cast<const char*>(&this->alphaFDR), sizeof this->alphaFDR);
1253      outfile.write(reinterpret_cast<const char*>(&this->FDRnumCorChan), sizeof this->FDRnumCorChan);
1254      if(this->beamAsUsed.origin()==PARAM){
1255        outfile.write(reinterpret_cast<const char*>(&this->fwhmBeam), sizeof this->fwhmBeam);
1256        outfile.write(reinterpret_cast<const char*>(&this->areaBeam), sizeof this->areaBeam);
1257      }
1258    }
1259    else{
1260      outfile.write(reinterpret_cast<const char*>(&this->flagUserThreshold), sizeof this->flagUserThreshold);
1261      if(this->flagUserThreshold)
1262        outfile.write(reinterpret_cast<const char*>(&this->threshold), sizeof this->threshold);
1263      else
1264        outfile.write(reinterpret_cast<const char*>(&this->snrCut), sizeof this->snrCut);
1265    }
1266    outfile.write(reinterpret_cast<const char*>(&this->flagGrowth), sizeof this->flagGrowth);
1267    if(this->flagGrowth){
1268      if(this->flagUserGrowthThreshold)
1269        outfile.write(reinterpret_cast<const char*>(&this->growthThreshold), sizeof this->growthThreshold);
1270      else
1271        outfile.write(reinterpret_cast<const char*>(&this->growthCut), sizeof this->growthCut);
1272    }
1273    outfile.write(reinterpret_cast<const char*>(&this->minVoxels), sizeof this->minVoxels);
1274    outfile.write(reinterpret_cast<const char*>(&this->minPix), sizeof this->minPix);
1275    outfile.write(reinterpret_cast<const char*>(&this->minChannels), sizeof this->minChannels);
1276    outfile.write(reinterpret_cast<const char*>(&this->flagAdjacent), sizeof this->flagAdjacent);
1277    if(!this->flagAdjacent)
1278      outfile.write(reinterpret_cast<const char*>(&this->threshSpatial), sizeof this->threshSpatial);
1279    outfile.write(reinterpret_cast<const char*>(&this->threshVelocity), sizeof this->threshVelocity);
1280    outfile.write(reinterpret_cast<const char*>(&this->flagRejectBeforeMerge), sizeof this->flagRejectBeforeMerge);
1281    outfile.write(reinterpret_cast<const char*>(&this->flagTwoStageMerging), sizeof this->flagTwoStageMerging);
1282    outfile.write(reinterpret_cast<const char*>(&this->flagSmooth), sizeof this->flagSmooth);
1283    if(this->flagSmooth){
1284      bool type=(this->smoothType=="spectral");
1285      outfile.write(reinterpret_cast<const char*>(&type), sizeof type);
1286      if(type)
1287        outfile.write(reinterpret_cast<const char*>(&this->hanningWidth), sizeof this->hanningWidth);
1288      else{
1289        outfile.write(reinterpret_cast<const char*>(&this->kernMaj), sizeof this->kernMaj);
1290        outfile.write(reinterpret_cast<const char*>(&this->kernMin), sizeof this->kernMin);
1291        outfile.write(reinterpret_cast<const char*>(&this->kernPA), sizeof this->kernPA);
1292      }
1293    }
1294    outfile.write(reinterpret_cast<const char*>(&this->flagATrous), sizeof this->flagATrous);
1295    if(this->flagATrous){
1296      outfile.write(reinterpret_cast<const char*>(&this->reconDim), sizeof this->reconDim);
1297      outfile.write(reinterpret_cast<const char*>(&this->scaleMin), sizeof this->scaleMin);
1298      outfile.write(reinterpret_cast<const char*>(&this->scaleMax), sizeof this->scaleMax);
1299      outfile.write(reinterpret_cast<const char*>(&this->snrRecon), sizeof this->snrRecon);
1300      outfile.write(reinterpret_cast<const char*>(&this->reconConvergence), sizeof this->reconConvergence);
1301      outfile.write(reinterpret_cast<const char*>(&this->filterCode), sizeof this->filterCode);
1302    }
1303    outfile.write(reinterpret_cast<const char*>(&this->flagMW), sizeof this->flagMW);
1304    if(this->flagMW){
1305      outfile.write(reinterpret_cast<const char*>(&this->minMW), sizeof this->minMW);
1306      outfile.write(reinterpret_cast<const char*>(&this->maxMW), sizeof this->maxMW);
1307    }
1308    writeStringToBinaryFile(outfile,this->flaggedChannelList);
1309
1310    outfile.close();
1311  }
1312
1313  std::streampos Param::readFromBinaryFile(std::string &filename, std::streampos loc)
1314  {
1315    std::ifstream infile(filename.c_str(), std::ios::in | std::ios::binary);
1316    if(!infile.is_open()){
1317      DUCHAMPERROR("read binary parameters","Could not open binary catalogue \""<<filename <<"\"");
1318      return -1;
1319    }
1320    infile.seekg(loc);
1321    this->imageFile = readStringFromBinaryFile(infile);
1322    infile.read(reinterpret_cast<char*>(&this->flagSubsection), sizeof this->flagSubsection);
1323    if(this->flagSubsection){
1324      std::string sec=readStringFromBinaryFile(infile);
1325      this->pixelSec.setSection(sec);
1326    }
1327    infile.read(reinterpret_cast<char*>(&this->flagStatSec), sizeof this->flagStatSec);
1328    if(this->flagStatSec){
1329      std::string sec=readStringFromBinaryFile(infile);
1330      this->statSec.setSection(sec);
1331    }
1332    infile.read(reinterpret_cast<char*>(&this->flagReconExists), sizeof this->flagReconExists);
1333    if(this->flagReconExists) this->reconFile=readStringFromBinaryFile(infile);
1334    infile.read(reinterpret_cast<char*>(&this->flagSmoothExists), sizeof this->flagSmoothExists);
1335    if(this->flagSmoothExists) this->smoothFile=readStringFromBinaryFile(infile);
1336    //
1337    this->searchType=readStringFromBinaryFile(infile);
1338    infile.read(reinterpret_cast<char*>(&this->flagNegative), sizeof this->flagNegative);
1339    infile.read(reinterpret_cast<char*>(&this->flagBaseline), sizeof this->flagBaseline);
1340    infile.read(reinterpret_cast<char*>(&this->flagRobustStats), sizeof this->flagRobustStats);
1341    infile.read(reinterpret_cast<char*>(&this->flagFDR), sizeof this->flagFDR);
1342    if(this->flagFDR){
1343      infile.read(reinterpret_cast<char*>(&this->alphaFDR), sizeof this->alphaFDR);
1344      infile.read(reinterpret_cast<char*>(&this->FDRnumCorChan), sizeof this->FDRnumCorChan);
1345      if(this->beamAsUsed.origin()==PARAM){
1346        infile.read(reinterpret_cast<char*>(&this->fwhmBeam), sizeof this->fwhmBeam);
1347        infile.read(reinterpret_cast<char*>(&this->areaBeam), sizeof this->areaBeam);
1348      }
1349    }
1350    else{
1351      infile.read(reinterpret_cast<char*>(&this->flagUserThreshold), sizeof this->flagUserThreshold);
1352      if(this->flagUserThreshold)
1353        infile.read(reinterpret_cast<char*>(&this->threshold), sizeof this->threshold);
1354      else
1355        infile.read(reinterpret_cast<char*>(&this->snrCut), sizeof this->snrCut);
1356    }
1357    infile.read(reinterpret_cast<char*>(&this->flagGrowth), sizeof this->flagGrowth);
1358    if(this->flagGrowth){
1359      if(this->flagUserGrowthThreshold)
1360        infile.read(reinterpret_cast<char*>(&this->growthThreshold), sizeof this->growthThreshold);
1361      else
1362        infile.read(reinterpret_cast<char*>(&this->growthCut), sizeof this->growthCut);
1363    }
1364    infile.read(reinterpret_cast<char*>(&this->minVoxels), sizeof this->minVoxels);
1365    infile.read(reinterpret_cast<char*>(&this->minPix), sizeof this->minPix);
1366    infile.read(reinterpret_cast<char*>(&this->minChannels), sizeof this->minChannels);
1367    infile.read(reinterpret_cast<char*>(&this->flagAdjacent), sizeof this->flagAdjacent);
1368    if(!this->flagAdjacent)
1369      infile.read(reinterpret_cast<char*>(&this->threshSpatial), sizeof this->threshSpatial);
1370    infile.read(reinterpret_cast<char*>(&this->threshVelocity), sizeof this->threshVelocity);
1371    infile.read(reinterpret_cast<char*>(&this->flagRejectBeforeMerge), sizeof this->flagRejectBeforeMerge);
1372    infile.read(reinterpret_cast<char*>(&this->flagTwoStageMerging), sizeof this->flagTwoStageMerging);
1373    infile.read(reinterpret_cast<char*>(&this->flagSmooth), sizeof this->flagSmooth);
1374    if(this->flagSmooth){
1375      bool type;
1376      infile.read(reinterpret_cast<char*>(&type), sizeof type);
1377      this->smoothType = type ? "spectral" : "spatial";
1378      if(type)
1379        infile.read(reinterpret_cast<char*>(&this->hanningWidth), sizeof this->hanningWidth);
1380      else{
1381        infile.read(reinterpret_cast<char*>(&this->kernMaj), sizeof this->kernMaj);
1382        infile.read(reinterpret_cast<char*>(&this->kernMin), sizeof this->kernMin);
1383        infile.read(reinterpret_cast<char*>(&this->kernPA), sizeof this->kernPA);
1384      }
1385    }
1386    infile.read(reinterpret_cast<char*>(&this->flagATrous), sizeof this->flagATrous);
1387    if(this->flagATrous){
1388      infile.read(reinterpret_cast<char*>(&this->reconDim), sizeof this->reconDim);
1389      infile.read(reinterpret_cast<char*>(&this->scaleMin), sizeof this->scaleMin);
1390      infile.read(reinterpret_cast<char*>(&this->scaleMax), sizeof this->scaleMax);
1391      infile.read(reinterpret_cast<char*>(&this->snrRecon), sizeof this->snrRecon);
1392      infile.read(reinterpret_cast<char*>(&this->reconConvergence), sizeof this->reconConvergence);
1393      infile.read(reinterpret_cast<char*>(&this->filterCode), sizeof this->filterCode);
1394    }
1395    infile.read(reinterpret_cast<char*>(&this->flagMW), sizeof this->flagMW);
1396    if(this->flagMW){
1397      infile.read(reinterpret_cast<char*>(&this->minMW), sizeof this->minMW);
1398      infile.read(reinterpret_cast<char*>(&this->maxMW), sizeof this->maxMW);
1399    }
1400    this->flaggedChannelList=readStringFromBinaryFile(infile);
1401
1402    std::streampos newloc = infile.tellg();
1403    infile.close();
1404    return newloc;
1405
1406  }
1407
1408
1409
1410
1411  void Param::copyHeaderInfo(FitsHeader &head)
1412  {
1413    ///  A function to copy across relevant header keywords from the
1414    ///  FitsHeader class to the Param class, as they are needed by
1415    ///  functions in the Param class.
1416    ///  The parameters are the keywords BLANK, BSCALE, BZERO, and the beam size.
1417
1418    this->blankKeyword  = head.getBlankKeyword();
1419    this->bscaleKeyword = head.getBscaleKeyword();
1420    this->bzeroKeyword  = head.getBzeroKeyword();
1421    this->blankPixValue = this->blankKeyword * this->bscaleKeyword +
1422      this->bzeroKeyword;
1423  }
1424
1425  std::string Param::outputMaskFile()
1426  {
1427    ///  This function produces the required filename in which to save
1428    ///  the mask image, indicating which pixels have been detected as
1429    ///  part of an object. If the input image is image.fits, then the
1430    ///  output will be image.MASK.fits.
1431
1432    if(this->fileOutputMask==""){
1433      std::string inputName = this->imageFile;
1434      std::string outputName = inputName;
1435      if(this->imageFile.size()>5 && inputName.substr(inputName.size()-5,5)==".fits")
1436        outputName = inputName.substr(0,inputName.size()-5); 
1437      // remove the ".fits" on the end.
1438      outputName += ".MASK.fits";
1439      return outputName;
1440    }
1441    else return this->fileOutputMask;
1442  }
1443
1444  std::string Param::outputMomentMapFile()
1445  {
1446    ///  This function produces the required filename in which to save
1447    ///  the moment-0 FITS image. If the input image is image.fits, then the
1448    ///  output will be image.MOM0.fits.
1449
1450    if(this->fileOutputMomentMap==""){
1451      std::string inputName = this->imageFile;
1452      std::string outputName = inputName;
1453      if(this->imageFile.size()>5 && inputName.substr(inputName.size()-5,5)==".fits")
1454        outputName = inputName.substr(0,inputName.size()-5); 
1455      // remove the ".fits" on the end.
1456      outputName += ".MOM0.fits";
1457      return outputName;
1458    }
1459    else return this->fileOutputMomentMap;
1460  }
1461
1462  std::string Param::outputMomentMaskFile()
1463  {
1464    ///  This function produces the required filename in which to save
1465    ///  the moment-0 FITS image. If the input image is image.fits, then the
1466    ///  output will be image.MOM0.fits.
1467
1468    if(this->fileOutputMomentMask==""){
1469      std::string inputName = this->imageFile;
1470      std::string outputName = inputName;
1471      if(this->imageFile.size()>5 && inputName.substr(inputName.size()-5,5)==".fits")
1472        outputName = inputName.substr(0,inputName.size()-5); 
1473      // remove the ".fits" on the end.
1474      outputName += ".MOM0MASK.fits";
1475      return outputName;
1476    }
1477    else return this->fileOutputMomentMask;
1478  }
1479
1480  std::string Param::outputBaselineFile()
1481  {
1482    ///  This function produces the required filename in which to save
1483    ///  the baseline FITS image. If the input image is image.fits, then the
1484    ///  output will be image.BASE.fits.
1485
1486    if(this->fileOutputBaseline==""){
1487      std::string inputName = this->imageFile;
1488      std::string outputName = inputName;
1489      if(this->imageFile.size()>5 && inputName.substr(inputName.size()-5,5)==".fits")
1490        outputName = inputName.substr(0,inputName.size()-5); 
1491      // remove the ".fits" on the end.
1492      outputName += ".BASE.fits";
1493      return outputName;
1494    }
1495    else return this->fileOutputBaseline;
1496  }
1497
1498  std::string Param::outputSmoothFile()
1499  {
1500    ///  This function produces the required filename in which to save
1501    ///   the smoothed array. If the input image is image.fits, then
1502    ///   the output will be:
1503    ///    <ul><li> Spectral smoothing: image.SMOOTH-1D-3.fits, where the
1504    ///             width of the Hanning filter was 3 pixels.
1505    ///        <li> Spatial smoothing : image.SMOOTH-2D-3-2-20.fits, where
1506    ///             kernMaj=3, kernMin=2 and kernPA=20 degrees.
1507    ///    </ul>
1508
1509    if(this->fileOutputSmooth==""){
1510      std::string inputName = this->imageFile;
1511      std::stringstream ss;
1512      if(this->imageFile.size()>5 && inputName.substr(inputName.size()-5,5)==".fits")
1513        ss << inputName.substr(0,inputName.size()-5); 
1514      else
1515        ss << inputName;
1516      // remove the ".fits" on the end if necessary.
1517      if(this->flagSubsection) ss<<".sub";
1518      if(this->smoothType=="spectral")
1519        ss << ".SMOOTH-1D-" << this->hanningWidth << ".fits";
1520      else if(this->smoothType=="spatial")
1521        ss << ".SMOOTH-2D-"
1522           << this->kernMaj << "-"
1523           << this->kernMin << "-"
1524           << this->kernPA  << ".fits";
1525      return ss.str();
1526    }
1527    else return this->fileOutputSmooth;
1528  }
1529
1530  std::string Param::outputReconFile()
1531  {
1532    /// This function produces the required filename in which to save
1533    ///  the reconstructed array. If the input image is image.fits, then
1534    ///  the output will be eg. image.RECON-3-2-4-1.fits, where the numbers are
1535    ///  3=reconDim, 2=filterCode, 4=snrRecon, 1=minScale
1536
1537    if(this->fileOutputRecon==""){
1538      std::string inputName = this->imageFile;
1539      std::stringstream ss;
1540      // First we remove the ".fits" from the end of the filename.
1541      if(this->imageFile.size()>5 && inputName.substr(inputName.size()-5,5)==".fits")
1542          ss << inputName.substr(0,inputName.size()-5); 
1543      else
1544          ss << inputName;
1545      if(this->flagSubsection) ss<<".sub";
1546      ss << ".RECON-" << this->reconDim
1547         << "-"       << this->filterCode
1548         << "-"       << this->snrRecon
1549         << "-"       << this->scaleMin
1550         << "-"       << this->scaleMax
1551         << "-"       << this->reconConvergence
1552         << ".fits";
1553      return ss.str();
1554    }
1555    else return this->fileOutputRecon;
1556  }
1557
1558  std::string Param::outputResidFile()
1559  {
1560    /// This function produces the required filename in which to save
1561    ///  the reconstructed array. If the input image is image.fits, then
1562    ///  the output will be eg. image.RESID-3-2-4-1.fits, where the numbers are
1563    ///  3=reconDim, 2=filterCode, 4=snrRecon, 1=scaleMin
1564
1565    if(this->fileOutputResid==""){
1566      std::string inputName = this->imageFile;
1567      std::stringstream ss;
1568      // First we remove the ".fits" from the end of the filename.
1569      if(this->imageFile.size()>5 && inputName.substr(inputName.size()-5,5)==".fits")
1570          ss << inputName.substr(0,inputName.size()-5);
1571      else
1572          ss << inputName;
1573      if(this->flagSubsection) ss<<".sub";
1574      ss << ".RESID-" << this->reconDim
1575         << "-"       << this->filterCode
1576         << "-"       << this->snrRecon
1577         << "-"       << this->scaleMin
1578         << "-"       << this->scaleMax
1579         << "-"       << this->reconConvergence
1580         << ".fits";
1581      return ss.str();
1582    }
1583    else return this->fileOutputResid;
1584  }
1585
1586}
Note: See TracBrowser for help on using the repository browser.