source: trunk/src/param.cc @ 1302

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

Ticket #77 - Implementing the two new parameters, one to select the edge method, and the other to set the cutoff for determining the kernel size.

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