source: trunk/src/Cubes/cubes.cc @ 884

Last change on this file since 884 was 884, checked in by MatthewWhiting, 12 years ago

A large set of changes aimed at making the use of indexing variables consistent. We have moved to size_t as much as possible to represent the location in memory. This includes making the dimension array within DataArray? and derived classes an array of size_t variables. Still plenty of compilation warnings (principally comparing signed and unsigned variables) - these will need to be cleaned up.

File size: 65.8 KB
Line 
1// -----------------------------------------------------------------------
2// cubes.cc: Member functions for the DataArray, Cube and Image classes.
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 <unistd.h>
29#include <iostream>
30#include <iomanip>
31#include <vector>
32#include <algorithm>
33#include <string>
34#include <math.h>
35
36#include <wcslib/wcs.h>
37
38#include <duchamp/pgheader.hh>
39
40#include <duchamp/duchamp.hh>
41#include <duchamp/param.hh>
42#include <duchamp/fitsHeader.hh>
43#include <duchamp/Cubes/cubes.hh>
44#include <duchamp/PixelMap/Voxel.hh>
45#include <duchamp/PixelMap/Object3D.hh>
46#include <duchamp/Detection/detection.hh>
47#include <duchamp/Detection/columns.hh>
48#include <duchamp/Detection/finders.hh>
49#include <duchamp/Utils/utils.hh>
50#include <duchamp/Utils/feedback.hh>
51#include <duchamp/Utils/mycpgplot.hh>
52#include <duchamp/Utils/Statistics.hh>
53#include <duchamp/FitsIO/DuchampBeam.hh>
54
55using namespace mycpgplot;
56using namespace Statistics;
57using namespace PixelInfo;
58
59#ifdef TEST_DEBUG
60const bool TESTING=true;
61#else
62const bool TESTING=false;
63#endif
64
65namespace duchamp
66{
67
68  using namespace Column;
69
70  /****************************************************************/
71  ///////////////////////////////////////////////////
72  //// Functions for DataArray class:
73  ///////////////////////////////////////////////////
74
75  DataArray::DataArray(){
76    /// Fundamental constructor for DataArray.
77    /// Number of dimensions and pixels are set to 0. Nothing else allocated.
78
79    this->numDim=0;
80    this->numPixels=0;
81    this->objectList = new std::vector<Detection>;
82    this->axisDimAllocated = false;
83    this->arrayAllocated = false;
84  }
85  //--------------------------------------------------------------------
86
87  DataArray::DataArray(const DataArray &d){
88    operator=(d);
89  }
90
91  DataArray& DataArray::operator=(const DataArray &d){
92    if(this==&d) return *this;
93    this->numDim = d.numDim;
94    if(this->axisDimAllocated) delete [] this->axisDim;
95    this->axisDimAllocated = d.axisDimAllocated;
96    if(this->axisDimAllocated){
97      this->axisDim = new size_t[this->numDim];
98      for(size_t i=0;i<size_t(this->numDim);i++) this->axisDim[i] = d.axisDim[i];
99    }
100    this->numPixels = d.numPixels;
101    if(this->arrayAllocated) delete [] this->array;
102    this->arrayAllocated = d.arrayAllocated;
103    if(this->arrayAllocated) {
104      this->array = new float[this->numPixels];
105      for(size_t i=0;i<size_t(this->numPixels);i++) this->array[i] = d.array[i];
106    }
107    this->objectList = d.objectList;
108    this->par = d.par;
109    this->Stats = d.Stats;
110    return *this;
111  }
112
113
114  DataArray::DataArray(short int nDim){
115    /// @details
116    /// N-dimensional constructor for DataArray.
117    /// Number of dimensions defined, and dimension array allocated.
118    /// Number of pixels are set to 0.
119    /// \param nDim Number of dimensions.
120
121    this->axisDimAllocated = false;
122    this->arrayAllocated = false;
123    if(nDim>0){
124      this->axisDim = new size_t[nDim];
125      this->axisDimAllocated = true;
126    }
127    this->numDim=nDim;
128    this->numPixels=0;
129    this->objectList = new std::vector<Detection>;
130  }
131  //--------------------------------------------------------------------
132
133  DataArray::DataArray(short int nDim, size_t size){
134    /// @details
135    /// N-dimensional constructor for DataArray.
136    /// Number of dimensions and number of pixels defined.
137    /// Arrays allocated based on these values.
138    /// \param nDim Number of dimensions.
139    /// \param size Number of pixels.
140    ///
141    /// Note that we can assign values to the dimension array.
142
143    this->axisDimAllocated = false;
144    this->arrayAllocated = false;
145    if(size<0)
146      duchampError("DataArray(nDim,size)",
147                   "Negative size -- could not define DataArray");
148    else if(nDim<0)
149      duchampError("DataArray(nDim,size)",
150                   "Negative number of dimensions: could not define DataArray");
151    else {
152      if(size>0){
153        this->array = new float[size];
154        this->arrayAllocated = true;
155      }
156      this->numPixels = size;
157      if(nDim>0){
158        this->axisDim = new size_t[nDim];
159        this->axisDimAllocated = true;
160      }
161      this->numDim = nDim;
162    }
163    this->objectList = new std::vector<Detection>;
164  }
165  //--------------------------------------------------------------------
166
167  DataArray::DataArray(short int nDim, size_t *dimensions)
168  {
169    /// @details
170    /// Most robust constructor for DataArray.
171    /// Number and sizes of dimensions are defined, and hence the number of
172    /// pixels. Arrays allocated based on these values.
173    /// \param nDim Number of dimensions.
174    /// \param dimensions Array giving sizes of dimensions.
175
176    this->axisDimAllocated = false;
177    this->arrayAllocated = false;
178    if(nDim<0)
179      duchampError("DataArray(nDim,dimArray)",
180                   "Negative number of dimensions: could not define DataArray");
181    else {
182      int size = dimensions[0];
183      for(int i=1;i<nDim;i++) size *= dimensions[i];
184      if(size<0)
185        duchampError("DataArray(nDim,dimArray)",
186                     "Negative size: could not define DataArray");
187      else{
188        this->numPixels = size;
189        if(size>0){
190          this->array = new float[size];
191          this->arrayAllocated = true;
192        }
193        this->numDim=nDim;
194        if(nDim>0){
195          this->axisDim = new size_t[nDim];
196          this->axisDimAllocated = true;
197        }
198        for(int i=0;i<nDim;i++) this->axisDim[i] = dimensions[i];
199      }
200    }
201  }
202  //--------------------------------------------------------------------
203
204  DataArray::~DataArray()
205  {
206    ///  @details
207    ///  Destructor -- arrays deleted if they have been allocated, and the
208    ///   object list is deleted.
209
210    if(this->numPixels>0 && this->arrayAllocated){
211      delete [] this->array;
212      this->arrayAllocated = false;
213    }
214    if(this->numDim>0 && this->axisDimAllocated){
215      delete [] this->axisDim;
216      this->axisDimAllocated = false;
217    }
218    delete this->objectList;
219  }
220  //--------------------------------------------------------------------
221  //--------------------------------------------------------------------
222
223  void DataArray::getDim(size_t &x, size_t &y, size_t &z)
224  {
225    /// @details
226    /// The sizes of the first three dimensions (if they exist) are returned.
227    /// \param x The first dimension. Defaults to 0 if numDim \f$\le\f$ 0.
228    /// \param y The second dimension. Defaults to 1 if numDim \f$\le\f$ 1.
229    /// \param z The third dimension. Defaults to 1 if numDim \f$\le\f$ 2.
230
231    if(this->numDim>0) x=this->axisDim[0];
232    else x=0;
233    if(this->numDim>1) y=this->axisDim[1];
234    else y=1;
235    if(this->numDim>2) z=this->axisDim[2];
236    else z=1;
237  }
238  //--------------------------------------------------------------------
239
240  void DataArray::getDimArray(size_t *output)
241  {
242    /// @details
243    /// The axisDim array is written to output. This needs to be defined
244    ///  beforehand: no checking is done on the memory.
245    /// \param output The array that is written to.
246
247    for(int i=0;i<this->numDim;i++) output[i] = this->axisDim[i];
248  }
249  //--------------------------------------------------------------------
250
251  void DataArray::getArray(float *output)
252  {
253    /// @details
254    /// The pixel value array is written to output. This needs to be defined
255    ///  beforehand: no checking is done on the memory.
256    /// \param output The array that is written to.
257
258    for(int i=0;i<this->numPixels;i++) output[i] = this->array[i];
259  }
260  //--------------------------------------------------------------------
261
262  void DataArray::saveArray(float *input, size_t size)
263  {
264    /// @details
265    /// Saves the array in input to the pixel array DataArray::array.
266    /// The size of the array given must be the same as the current number of
267    /// pixels, else an error message is returned and nothing is done.
268    /// \param input The array of values to be saved.
269    /// \param size The size of input.
270
271    if(size != this->numPixels)
272      duchampError("DataArray::saveArray",
273                   "Input array different size to existing array. Cannot save.");
274    else {
275      if(this->numPixels>0 && this->arrayAllocated) delete [] this->array;
276      this->numPixels = size;
277      this->array = new float[size];
278      this->arrayAllocated = true;
279      for(int i=0;i<size;i++) this->array[i] = input[i];
280    }
281  }
282  //--------------------------------------------------------------------
283
284  void DataArray::addObject(Detection object)
285  {
286    /// \param object The object to be added to the object list.
287
288    // objectList is a vector, so just use push_back()
289    this->objectList->push_back(object);
290  }
291  //--------------------------------------------------------------------
292
293  void DataArray::addObjectList(std::vector <Detection> newlist)
294  {
295    /// \param newlist The list of objects to be added to the object list.
296
297    std::vector<Detection>::iterator obj;
298    for(obj=newlist.begin();obj<newlist.end();obj++) this->objectList->push_back(*obj);
299  }
300  //--------------------------------------------------------------------
301
302  bool DataArray::isDetection(float value)
303  {
304    ///  @details
305    /// Is a given value a detection, based on the statistics in the
306    /// DataArray's StatsContainer?
307    /// \param value The pixel value to test.
308
309    if(par.isBlank(value)) return false;
310    else return Stats.isDetection(value);
311  }
312  //--------------------------------------------------------------------
313
314  bool DataArray::isDetection(size_t voxel)
315  {
316    ///  @details
317    /// Is a given pixel a detection, based on the statistics in the
318    /// DataArray's StatsContainer?
319    /// If the pixel lies outside the valid range for the data array, return false.
320    /// \param voxel Location of the DataArray's pixel to be tested.
321
322    if((voxel<0)||(voxel>this->numPixels)) return false;
323    else if(par.isBlank(this->array[voxel])) return false;
324    else return Stats.isDetection(this->array[voxel]);
325  } 
326  //--------------------------------------------------------------------
327
328  std::ostream& operator<< ( std::ostream& theStream, DataArray &array)
329  {
330    /// @details
331    /// A way to print out the pixel coordinates & flux values of the
332    /// list of detected objects belonging to the DataArray.
333    /// These are formatted nicely according to the << operator for Detection,
334    ///  with a line indicating the number of detections at the start.
335    /// \param theStream The ostream object to which the output should be sent.
336    /// \param array The DataArray containing the list of objects.
337
338    for(int i=0;i<array.numDim;i++){
339      if(i>0) theStream<<"x";
340      theStream<<array.axisDim[i];
341    }
342    theStream<<std::endl;
343
344    theStream<<"Threshold\tmiddle\tspread\trobust\n" << array.stats().getThreshold() << "\t";
345    if(array.pars().getFlagUserThreshold())
346      theStream << "0.0000\t" << array.stats().getThreshold() << "\t";
347    else
348      theStream << array.stats().getMiddle() << " " << array.stats().getSpread() << "\t";
349    theStream << array.stats().getRobust()<<"\n";
350
351    theStream<<array.objectList->size()<<" detections:\n--------------\n";
352    std::vector<Detection>::iterator obj;
353    for(obj=array.objectList->begin();obj<array.objectList->end();obj++){
354      theStream << "Detection #" << obj->getID()<<std::endl;
355      Detection *newobj = new Detection;
356      *newobj = *obj;
357      newobj->addOffsets();
358      theStream<<*newobj;
359      delete newobj;
360    }
361    theStream<<"--------------\n";
362    return theStream;
363  }
364
365  /****************************************************************/
366  /////////////////////////////////////////////////////////////
367  //// Functions for Cube class
368  /////////////////////////////////////////////////////////////
369
370  Cube::Cube()
371  {
372    /// @details
373    /// Basic Constructor for Cube class.
374    /// numDim set to 3, but numPixels to 0 and all bool flags to false.
375    /// No allocation done.
376
377    numPixels=0; numDim=3;
378    reconExists = false; reconAllocated = false; baselineAllocated = false;
379  }
380  //--------------------------------------------------------------------
381
382  Cube::Cube(size_t size)
383  {
384    /// @details
385    /// Alternative Cube constructor, where size is given but not individual
386    ///  dimensions. Arrays are allocated as appropriate (according to the
387    ///  relevant flags in Param set), but the Cube::axisDim array is not.
388
389    this->reconAllocated = false;
390    this->baselineAllocated = false;
391    this->axisDimAllocated = false;
392    this->arrayAllocated = false;
393    this->numPixels = this->numDim = 0;
394    if(size<0)
395      duchampError("Cube(size)","Negative size -- could not define Cube");
396    else{
397      if(size>0){
398        this->array = new float[size];
399        this->arrayAllocated = true;
400        if(this->par.getFlagATrous()||this->par.getFlagSmooth()){
401          this->recon = new float[size];
402          this->reconAllocated = true;
403        }
404        if(this->par.getFlagBaseline()){
405          this->baseline = new float[size];
406          this->baselineAllocated = true;
407        }
408      }
409      this->numPixels = size;
410      this->axisDim = new size_t[3];
411      this->axisDimAllocated = true;
412      this->numDim = 3;
413      this->reconExists = false;
414    }
415  }
416  //--------------------------------------------------------------------
417
418  Cube::Cube(size_t *dimensions)
419  {
420    /// Alternative Cube constructor, where sizes of dimensions are given.
421    /// Arrays are allocated as appropriate (according to the
422    ///  relevant flags in Param set), as is the Cube::axisDim array.
423
424    int size   = dimensions[0] * dimensions[1] * dimensions[2];
425    int imsize = dimensions[0] * dimensions[1];
426    this->reconAllocated = false;
427    this->baselineAllocated = false;
428    this->axisDimAllocated = false;
429    this->arrayAllocated = false;
430    this->numPixels = this->numDim = 0;
431    if((size<0) || (imsize<0) )
432      duchampError("Cube(dimArray)","Negative size -- could not define Cube");
433    else{
434      this->numPixels = size;
435      if(size>0){
436        this->array      = new float[size];
437        this->arrayAllocated = true;
438        this->detectMap  = new short[imsize];
439        if(this->par.getFlagATrous()||this->par.getFlagSmooth()){
440          this->recon    = new float[size];
441          this->reconAllocated = true;
442        }
443        if(this->par.getFlagBaseline()){
444          this->baseline = new float[size];
445          this->baselineAllocated = true;
446        }
447      }
448      this->numDim  = 3;
449      this->axisDim = new size_t[3];
450      this->axisDimAllocated = true;
451      for(int i=0;i<3     ;i++) this->axisDim[i]   = dimensions[i];
452      for(int i=0;i<imsize;i++) this->detectMap[i] = 0;
453      this->reconExists = false;
454    }
455  }
456  //--------------------------------------------------------------------
457
458  Cube::~Cube()
459  {
460    /// @details
461    ///  The destructor deletes the memory allocated for Cube::detectMap, and,
462    ///  if these have been allocated, Cube::recon and Cube::baseline.
463
464    if(this->numPixels>0 && this->arrayAllocated) delete [] this->detectMap;
465    if(this->reconAllocated)    delete [] this->recon;
466    if(this->baselineAllocated) delete [] this->baseline;
467  }
468  //--------------------------------------------------------------------
469
470  Cube::Cube(const Cube &c):
471    DataArray(c)
472  {
473    this->operator=(c);
474  }
475  //--------------------------------------------------------------------
476
477  Cube& Cube::operator=(const Cube &c)
478  {
479    if(this==&c) return *this;
480    if(this->arrayAllocated) delete [] this->detectMap;
481    ((DataArray &) *this) = c;
482    this->reconExists = c.reconExists;
483    if(this->reconAllocated) delete [] this->recon;
484    this->reconAllocated = c.reconAllocated;
485    if(this->reconAllocated) {
486      this->recon = new float[this->numPixels];
487      for(size_t i=0;i<size_t(this->numPixels);i++) this->recon[i] = c.recon[i];
488    }
489    if(this->arrayAllocated){
490      this->detectMap = new short[this->axisDim[0]*this->axisDim[1]];
491      for(size_t i=0;i<size_t(this->axisDim[0]*this->axisDim[1]);i++) this->detectMap[i] = c.detectMap[i];
492    }
493    if(this->baselineAllocated) delete [] this->baseline;
494    this->baselineAllocated = c.baselineAllocated;
495    if(this->baselineAllocated){
496      this->baseline = new float[this->numPixels];
497      for(size_t i=0;i<size_t(this->numPixels);i++) this->baseline[i] = c.baseline[i];
498    }
499    this->head = c.head;
500    this->fullCols = c.fullCols;
501    this->logCols = c.logCols;
502    return *this;
503  }
504  //--------------------------------------------------------------------
505
506  Cube* Cube::slice(Section subsection)
507  {
508    Cube *output = new Cube;
509    Section thisSection;
510    std::string nullsec=nullSection(this->numDim);
511    if(this->par.section().isParsed()) thisSection=this->par.section();
512    else{
513      thisSection = Section(nullsec);
514      thisSection.parse(this->axisDim, this->numDim);
515    }
516
517    subsection.parse(this->axisDim, this->numDim);
518    if(subsection.isValid()){
519      output->par = this->par;
520      output->par.section() = thisSection * subsection;
521      output->par.setXOffset(output->par.getXOffset()+subsection.getStart(0));
522      output->par.setYOffset(output->par.getYOffset()+subsection.getStart(1));
523      output->par.setZOffset(output->par.getZOffset()+subsection.getStart(2));
524      output->head = this->head;
525      // correct the reference pixel in the WCS
526      output->head.WCS().crpix[output->head.WCS().lng] -= subsection.getStart(output->head.WCS().lng);
527      output->head.WCS().crpix[output->head.WCS().lat] -= subsection.getStart(output->head.WCS().lat);
528      if(output->head.WCS().spec>0)
529        output->head.WCS().crpix[output->head.WCS().spec] -= subsection.getStart(output->head.WCS().spec);
530      output->Stats = this->Stats;
531      output->fullCols = this->fullCols;
532      output->logCols = this->logCols;
533      size_t *dims = new size_t[3];
534      for(size_t i=0;i<3;i++){
535        dims[i] = subsection.getDimList()[i];
536        std::cout << "Dim " << i+1 << " = " << dims[i] << "\n";
537      }
538     
539      output->initialiseCube(dims,true);
540      for(size_t z=0;z<output->axisDim[2];z++){
541        for(size_t y=0;y<output->axisDim[1];y++){
542          for(size_t x=0;x<output->axisDim[0];x++){
543            size_t impos=x+y*output->axisDim[0];
544            size_t pos=impos+z*output->axisDim[0]*output->axisDim[1];
545            if(pos>=output->numPixels) duchampError("cube slicer","Out of bounds in new Cube");
546            size_t imposIn=(x+subsection.getStart(0)) + (y+subsection.getStart(1))*this->axisDim[0];
547            size_t posIn=imposIn + (z+subsection.getStart(2))*this->axisDim[0]*this->axisDim[1];
548            if(posIn>=this->numPixels) duchampError("cube slicer","Out of bounds in new Cube");
549            output->array[pos] = this->array[posIn];
550            output->detectMap[impos] = this->detectMap[imposIn];
551            if(this->reconAllocated) output->recon[pos] = this->recon[posIn];
552            if(this->baselineAllocated) output->baseline[pos] = this->baseline[posIn];
553          }
554        }
555      }
556       std::cout << this->par << "\n"<<output->par <<"\n";
557    }
558    else{
559      duchampError("cube slicer","Subsection does not parse");
560    }
561
562    return output;
563
564  }
565  //--------------------------------------------------------------------
566
567  OUTCOME Cube::initialiseCube(long *dimensions, bool allocateArrays)
568  {
569    int numAxes = this->head.getNumAxes();
570    if(numAxes<=0) numAxes=3;
571    size_t *dim = new size_t[numAxes];
572    for(int i=0;i<numAxes;i++) dim[i]=dimensions[i];
573    this->initialiseCube(dim,allocateArrays);
574    delete dim;
575  }
576
577
578  OUTCOME Cube::initialiseCube(size_t *dimensions, bool allocateArrays)
579  {
580    /// @details
581    ///  This function will set the sizes of all arrays that will be used by Cube.
582    ///  It will also define the values of the axis dimensions: this will be done
583    ///   using the WCS in the FitsHeader class, so the WCS needs to be good and
584    ///   have three axes. If this is not the case, the axes are assumed to be
585    ///   ordered in the sense of lng,lat,spc.
586    ///
587    ///  \param dimensions An array of values giving the dimensions (sizes) for
588    ///  all axes. 
589    ///  \param allocateArrays A flag indicating whether to allocate
590    ///  the memory for the data arrays: the default is true. The
591    ///  dimension arrays will be allocated and filled.
592
593    int lng,lat,spc;
594    size_t size,imsize;
595 
596    int numAxes = this->head.getNumAxes();
597    if(numAxes<=0) numAxes=3;
598
599    if(this->head.isWCS() && (numAxes>=3) && (this->head.WCS().spec>=0)){
600      // if there is a WCS and there is at least 3 axes
601      lng = this->head.WCS().lng;
602      lat = this->head.WCS().lat;
603      spc = this->head.WCS().spec;
604    }
605    else{
606      // just take dimensions[] at face value
607      lng = 0;
608      lat = 1;
609      spc = 2;
610    }
611
612    size   = dimensions[lng];
613    if(numAxes>1) size *= dimensions[lat];
614    if(this->head.canUseThirdAxis() && numAxes>spc) size *= dimensions[spc];
615
616    imsize = dimensions[lng];
617    if(numAxes>1) imsize *= dimensions[lat];
618
619    this->reconAllocated = false;
620    this->baselineAllocated = false;
621
622    if(this->axisDimAllocated){
623      delete [] this->axisDim;
624      this->axisDimAllocated = false;
625    }
626
627    if(this->arrayAllocated){
628      delete [] this->array;
629      delete [] this->detectMap;
630      this->arrayAllocated = false;
631    }
632    if(this->reconAllocated){
633      delete [] this->recon;
634      this->reconAllocated = false;
635    }
636    if(this->baselineAllocated){
637      delete [] this->baseline;
638      this->baselineAllocated = false;
639    }
640
641    if((size<0) || (imsize<0) ) {
642      duchampError("Cube::initialiseCube(dimArray)",
643                   "Negative size -- could not define Cube.\n");
644      return FAILURE;
645    }
646    else{
647      this->numPixels = size;
648      this->numDim  = 3;
649
650      this->axisDim = new size_t[this->numDim];
651      this->axisDimAllocated = true;
652      this->axisDim[0] = dimensions[lng];
653      if(numAxes>1) this->axisDim[1] = dimensions[lat];
654      else this->axisDim[1] = 1;
655      if(this->head.canUseThirdAxis() && numAxes>spc) this->axisDim[2] = dimensions[spc];
656      else this->axisDim[2] = 1;
657
658      int numNondegDim=0;
659      for(int i=0;i<3;i++) if(this->axisDim[i]>1) numNondegDim++;
660
661      if(this->par.getFlagSmooth()){
662        if(this->par.getSmoothType()=="spectral" && numNondegDim==2){
663          duchampWarning("Cube::initialiseCube", "Spectral smooth requested, but have a 2D image. Setting flagSmooth=false");
664          this->par.setFlagSmooth(false);
665        }
666        if(this->par.getSmoothType()=="spatial" && numNondegDim==1){
667          duchampWarning("Cube::initialiseCube", "Spatial smooth requested, but have a 1D image. Setting flagSmooth=false");
668          this->par.setFlagSmooth(false);
669        }
670      }
671      if(this->par.getFlagATrous()){
672        for(int d=3; d>=1; d--){
673          if(this->par.getReconDim()==d && numNondegDim==(d-1)){
674            std::stringstream ss;
675            ss << d << "D reconstruction requested, but image is " << d-1 <<"D. Setting flagAtrous=false";
676            duchampWarning("Cube::initialiseCube", ss.str());
677            this->par.setFlagATrous(false);
678          }
679        }
680      }
681
682      if(allocateArrays && this->par.isVerbose()) this->reportMemorySize(std::cout,allocateArrays);
683
684      this->reconExists = false;
685      if(size>0 && allocateArrays){
686        this->array      = new float[size];
687        this->arrayAllocated = true;
688        this->detectMap  = new short[imsize];
689        for(int i=0;i<imsize;i++) this->detectMap[i] = 0;
690        if(this->par.getFlagATrous() || this->par.getFlagSmooth()){
691          this->recon    = new float[size];
692          this->reconAllocated = true;
693          for(int i=0;i<size;i++) this->recon[i] = 0.;
694        }
695        if(this->par.getFlagBaseline()){
696          this->baseline = new float[size];
697          this->baselineAllocated = true;
698          for(int i=0;i<size;i++) this->baseline[i] = 0.;
699        }
700      }
701    }
702    return SUCCESS;
703  }
704  //--------------------------------------------------------------------
705
706  void Cube::reportMemorySize(std::ostream &theStream, bool allocateArrays)
707  {
708    std::string unitlist[4]={"kB","MB","GB","TB"};
709    size_t size=axisDim[0]*axisDim[1]*axisDim[2];
710    size_t imsize=axisDim[0]*axisDim[1];
711   
712      // Calculate and report the total size of memory to be allocated.
713      float allocSize=3*sizeof(size_t);  // axisDim
714      float arrAllocSize=0.;
715      if(size>0 && allocateArrays){
716        allocSize += size * sizeof(float); // array
717        arrAllocSize = size*sizeof(float);
718        allocSize += imsize * sizeof(short); // detectMap
719        if(this->par.getFlagATrous() || this->par.getFlagSmooth())
720          allocSize += size * sizeof(float); // recon
721        if(this->par.getFlagBaseline())
722          allocSize += size * sizeof(float); // baseline
723      }
724      std::string units="bytes";
725      for(int i=0;i<4 && allocSize>1024.;i++){
726        allocSize/=1024.;
727        arrAllocSize /= 1024.;
728        units=unitlist[i];
729      }
730
731      theStream << "\n About to allocate " << allocSize << units;
732      if(arrAllocSize > 0.) theStream << " of which " << arrAllocSize << units << " is for the image\n";
733      else theStream << "\n";
734  }
735
736
737  bool Cube::is2D()
738  {
739    /// @details
740    ///   Check whether the image is 2-dimensional, by counting
741    ///   the number of dimensions that are greater than 1
742
743    if(this->head.WCS().naxis==2) return true;
744    else{
745      int numDim=0;
746      for(int i=0;i<this->numDim;i++) if(axisDim[i]>1) numDim++;
747      return numDim<=2;
748    }
749
750  }
751  //--------------------------------------------------------------------
752
753  OUTCOME Cube::getCube()
754  { 
755    ///  @details
756    /// A front-end to the Cube::getCube() function, that does
757    ///  subsection checks.
758    /// Assumes the Param is set up properly.
759
760    std::string fname = par.getImageFile();
761    if(par.getFlagSubsection()) fname+=par.getSubsection();
762    return getCube(fname);
763  }
764  //--------------------------------------------------------------------
765
766  void Cube::saveArray(float *input, size_t size)
767  {
768    if(size != this->numPixels){
769      std::stringstream errmsg;
770      errmsg << "Input array different size to existing array ("
771             << size << " cf. " << this->numPixels << "). Cannot save.\n";
772      duchampError("Cube::saveArray",errmsg.str());
773    }
774    else {
775      if(this->numPixels>0 && this->arrayAllocated) delete [] array;
776      this->numPixels = size;
777      this->array = new float[size];
778      this->arrayAllocated = true;
779      for(int i=0;i<size;i++) this->array[i] = input[i];
780    }
781  }
782  //--------------------------------------------------------------------
783
784  void Cube::saveArray(std::vector<float> &input)
785  {
786    /// @details
787    /// Saves the array in input to the pixel array Cube::array.
788    /// The size of the array given must be the same as the current number of
789    /// pixels, else an error message is returned and nothing is done.
790    /// \param input The array of values to be saved.
791
792    if(input.size() != this->numPixels){
793      std::stringstream errmsg;
794      errmsg << "Input array different size to existing array ("
795             << input.size() << " cf. " << this->numPixels << "). Cannot save.\n";
796      duchampError("Cube::saveArray",errmsg.str());
797    }
798    else {
799      if(this->numPixels>0 && this->arrayAllocated) delete [] this->array;
800      this->numPixels = input.size();
801      this->array = new float[input.size()];
802      this->arrayAllocated = true;
803      for(size_t i=0;i<input.size();i++) this->array[i] = input[i];
804    }
805  }
806  //--------------------------------------------------------------------
807
808  void Cube::saveRecon(float *input, size_t size)
809  {
810    /// @details
811    /// Saves the array in input to the reconstructed array Cube::recon
812    /// The size of the array given must be the same as the current number of
813    /// pixels, else an error message is returned and nothing is done.
814    /// If the recon array has already been allocated, it is deleted first, and
815    /// then the space is allocated.
816    /// Afterwards, the appropriate flags are set.
817    /// \param input The array of values to be saved.
818    /// \param size The size of input.
819
820    if(size != this->numPixels){
821      std::stringstream errmsg;
822      errmsg << "Input array different size to existing array ("
823             << size << " cf. " << this->numPixels << "). Cannot save.\n";
824      duchampError("Cube::saveRecon",errmsg.str());
825    }
826    else {
827      if(this->numPixels>0){
828        if(this->reconAllocated) delete [] this->recon;
829        this->numPixels = size;
830        this->recon = new float[size];
831        this->reconAllocated = true;
832        for(int i=0;i<size;i++) this->recon[i] = input[i];
833        this->reconExists = true;
834      }
835    }
836  }
837  //--------------------------------------------------------------------
838
839  void Cube::getRecon(float *output)
840  {
841    /// @details
842    /// The reconstructed array is written to output. The output array needs to
843    ///  be defined beforehand: no checking is done on the memory.
844    /// \param output The array that is written to.
845
846    // Need check for change in number of pixels!
847    for(int i=0;i<this->numPixels;i++){
848      if(this->reconExists) output[i] = this->recon[i];
849      else output[i] = 0.;
850    }
851  }
852  //--------------------------------------------------------------------
853
854  void Cube::removeMW()
855  {
856    /// @details
857    /// The channels corresponding to the Milky Way range (as given by the Param
858    ///  set) are all set to 0 in the pixel array.
859    /// Only done if the appropriate flag is set, and the pixels are not BLANK.
860    /// \deprecated
861
862    if(this->par.getFlagMW()){
863      for(int pix=0;pix<this->axisDim[0]*this->axisDim[1];pix++){
864        for(int z=0;z<this->axisDim[2];z++){
865          int pos = z*this->axisDim[0]*this->axisDim[1] + pix;
866          if(!this->isBlank(pos) && this->par.isInMW(z)) this->array[pos]=0.;
867        }
868      }
869    }
870  }
871  //--------------------------------------------------------------------
872
873  void Cube::setCubeStats()
874  {
875    ///   @details
876    ///   Calculates the full statistics for the cube:
877    ///     mean, rms, median, madfm
878    ///   Only do this if the threshold has not been defined (ie. is still 0.,
879    ///    its default).
880    ///   Also work out the threshold and store it in the par set.
881    ///   
882    ///   Different from Cube::setCubeStatsOld() as it doesn't use the
883    ///    getStats functions but has own versions of them hardcoded to
884    ///    ignore BLANKs and MW channels. This saves on memory usage -- necessary
885    ///    for dealing with very big files.
886    ///
887    ///   Three cases exist:
888    ///  <ul><li>Simple case, with no reconstruction/smoothing: all stats
889    ///          calculated from the original array.
890    ///      <li>Wavelet reconstruction: mean & median calculated from the
891    ///          original array, and stddev & madfm from the residual.
892    ///      <li>Smoothing: all four stats calculated from the recon array
893    ///          (which holds the smoothed data).
894    ///  </ul>
895
896    if(this->par.getFlagUserThreshold() ){
897      // if the user has defined a threshold, set this in the StatsContainer
898      this->Stats.setThreshold( this->par.getThreshold() );
899    }
900    else{
901      // only work out the stats if we need to.
902      // the only reason we don't is if the user has specified a threshold.
903   
904      this->Stats.setRobust(this->par.getFlagRobustStats());
905
906      if(this->par.isVerbose())
907        std::cout << "Calculating the cube statistics... " << std::flush;
908   
909      // size_t xysize = this->axisDim[0]*this->axisDim[1];
910
911      bool *mask = new bool[this->numPixels];
912      int vox=0,goodSize = 0;
913      for(int z=0;z<this->axisDim[2];z++){
914        for(int y=0;y<this->axisDim[1];y++){
915          for(int x=0;x<this->axisDim[0];x++){
916            //      vox = z * xysize + y*this->axisDim[0] + x;
917            bool isBlank=this->isBlank(vox);
918            bool isMW = this->par.isInMW(z);
919            bool statOK = this->par.isStatOK(x,y,z);
920            mask[vox] = (!isBlank && !isMW && statOK );
921            if(mask[vox]) goodSize++;
922            vox++;
923          }
924        }
925      }
926
927      //      float mean,median,stddev,madfm;
928      if( this->par.getFlagATrous() ){
929        // Case #2 -- wavelet reconstruction
930        // just get mean & median from orig array, and rms & madfm from
931        // residual recompute array values to be residuals & then find
932        // stddev & madfm
933        if(!this->reconExists)
934          duchampError("setCubeStats",
935                       "Reconstruction not yet done!\nCannot calculate stats!\n");
936        else{
937          float *tempArray = new float[goodSize];
938
939          goodSize=0;
940          vox=0;
941          for(int z=0;z<this->axisDim[2];z++){
942            for(int y=0;y<this->axisDim[1];y++){
943              for(int x=0;x<this->axisDim[0];x++){
944                //              vox = z * xysize + y*this->axisDim[0] + x;
945                if(mask[vox]) tempArray[goodSize++] = this->array[vox];
946                vox++;
947              }
948            }
949          }
950
951          // First, find the mean of the original array. Store it.
952          this->Stats.setMean( findMean(tempArray, goodSize) );
953       
954          // Now sort it and find the median. Store it.
955          this->Stats.setMedian( findMedian(tempArray, goodSize, true) );
956
957          // Now calculate the residuals and find the mean & median of
958          // them. We don't store these, but they are necessary to find
959          // the sttdev & madfm.
960          goodSize = 0;
961          //      for(int p=0;p<xysize;p++){
962          vox=0;
963          for(int z=0;z<this->axisDim[2];z++){
964            for(int y=0;y<this->axisDim[1];y++){
965              for(int x=0;x<this->axisDim[0];x++){
966                //            vox = z * xysize + p;
967              if(mask[vox])
968                tempArray[goodSize++] = this->array[vox] - this->recon[vox];
969              vox++;
970              }
971            }
972          }
973           
974          this->Stats.setStddev( findStddev(tempArray, goodSize) );
975
976          // Now find the madfm of the residuals. Store it.
977          this->Stats.setMadfm( findMADFM(tempArray, goodSize, true) );
978
979          delete [] tempArray;
980        }
981      }
982      else if(this->par.getFlagSmooth()) {
983        // Case #3 -- smoothing
984        // get all four stats from the recon array, which holds the
985        // smoothed data. This can just be done with the
986        // StatsContainer::calculate function, using the mask generated
987        // earlier.
988        if(!this->reconExists)
989          duchampError("setCubeStats","Smoothing not yet done!\nCannot calculate stats!\n");
990        else this->Stats.calculate(this->recon,this->numPixels,mask);
991      }
992      else{
993        // Case #1 -- default case, with no smoothing or reconstruction.
994        // get all four stats from the original array. This can just be
995        // done with the StatsContainer::calculate function, using the
996        // mask generated earlier.
997        this->Stats.calculate(this->array,this->numPixels,mask);
998      }
999
1000      this->Stats.setUseFDR( this->par.getFlagFDR() );
1001      // If the FDR method has been requested, define the P-value
1002      // threshold
1003      if(this->par.getFlagFDR())  this->setupFDR();
1004      else{
1005        // otherwise, calculate threshold based on the requested SNR cut
1006        // level, and then set the threshold parameter in the Par set.
1007        this->Stats.setThresholdSNR( this->par.getCut() );
1008        this->par.setThreshold( this->Stats.getThreshold() );
1009      }
1010   
1011      delete [] mask;
1012
1013    }
1014
1015    if(this->par.isVerbose()){
1016      std::cout << "Using ";
1017      if(this->par.getFlagFDR()) std::cout << "effective ";
1018      std::cout << "flux threshold of: ";
1019      float thresh = this->Stats.getThreshold();
1020      if(this->par.getFlagNegative()) thresh *= -1.;
1021      std::cout << thresh;
1022      if(this->par.getFlagGrowth()){
1023        std::cout << " and growing to threshold of: ";
1024        if(this->par.getFlagUserGrowthThreshold()) thresh= this->par.getGrowthThreshold();
1025        else thresh= this->Stats.snrToValue(this->par.getGrowthCut());
1026        if(this->par.getFlagNegative()) thresh *= -1.;
1027        std::cout << thresh;
1028      }
1029      std::cout << std::endl;
1030    }
1031
1032  }
1033  //--------------------------------------------------------------------
1034
1035  void Cube::setupFDR()
1036  {
1037    /// @details
1038    ///  Call the setupFDR(float *) function on the pixel array of the
1039    ///  cube. This is the usual way of running it.
1040    ///
1041    ///  However, if we are in smoothing mode, we calculate the FDR
1042    ///  parameters using the recon array, which holds the smoothed
1043    ///  data. Gives an error message if the reconExists flag is not set.
1044
1045    if(this->par.getFlagSmooth())
1046      if(this->reconExists) this->setupFDR(this->recon);
1047      else{
1048        duchampError("setupFDR",
1049                     "Smoothing not done properly! Using original array for defining threshold.\n");
1050        this->setupFDR(this->array);
1051      }
1052    else if( this->par.getFlagATrous() ){
1053      this->setupFDR(this->recon);
1054    }
1055    else{
1056      this->setupFDR(this->array);
1057    }
1058  }
1059  //--------------------------------------------------------------------
1060
1061  void Cube::setupFDR(float *input)
1062  {
1063    ///   @details
1064    ///   Determines the critical Probability value for the False
1065    ///   Discovery Rate detection routine. All pixels in the given arry
1066    ///   with Prob less than this value will be considered detections.
1067    ///
1068    ///   Note that the Stats of the cube need to be calculated first.
1069    ///
1070    ///   The Prob here is the probability, assuming a Normal
1071    ///   distribution, of obtaining a value as high or higher than the
1072    ///   pixel value (ie. only the positive tail of the PDF).
1073    ///
1074    ///   The probabilities are calculated using the
1075    ///   StatsContainer::getPValue(), which calculates the z-statistic,
1076    ///   and then the probability via
1077    ///   \f$0.5\operatorname{erfc}(z/\sqrt{2})\f$ -- giving the positive
1078    ///   tail probability.
1079
1080    // first calculate p-value for each pixel -- assume Gaussian for now.
1081
1082    float *orderedP = new float[this->numPixels];
1083    int count = 0;
1084    for(int x=0;x<this->axisDim[0];x++){
1085      for(int y=0;y<this->axisDim[1];y++){
1086        for(int z=0;z<this->axisDim[2];z++){
1087          int pix = z * this->axisDim[0]*this->axisDim[1] +
1088            y*this->axisDim[0] + x;
1089
1090          if(!(this->par.isBlank(this->array[pix])) && !this->par.isInMW(z)){
1091            // only look at non-blank, valid pixels
1092            //            orderedP[count++] = this->Stats.getPValue(this->array[pix]);
1093            orderedP[count++] = this->Stats.getPValue(input[pix]);
1094          }
1095        }
1096      }
1097    }
1098
1099    // now order them
1100    std::stable_sort(orderedP,orderedP+count);
1101 
1102    // now find the maximum P value.
1103    int max = 0;
1104    double cN = 0.;
1105    // Calculate number of correlated pixels. Assume all spatial
1106    // pixels within the beam are correlated, and multiply this by the
1107    // number of correlated pixels as determined by the beam
1108    int numVox;
1109    if(this->head.beam().isDefined()) numVox = int(ceil(this->head.beam().area()));
1110    else  numVox = 1;
1111    if(this->head.canUseThirdAxis()) numVox *= this->par.getFDRnumCorChan();
1112    for(int psfCtr=1;psfCtr<=numVox;psfCtr++) cN += 1./float(psfCtr);
1113
1114    double slope = this->par.getAlpha()/cN;
1115    for(int loopCtr=0;loopCtr<count;loopCtr++) {
1116      if( orderedP[loopCtr] < (slope * double(loopCtr+1)/ double(count)) ){
1117        max = loopCtr;
1118      }
1119    }
1120
1121    this->Stats.setPThreshold( orderedP[max] );
1122
1123
1124    // Find real value of the P threshold by finding the inverse of the
1125    //  error function -- root finding with brute force technique
1126    //  (relatively slow, but we only do it once).
1127    double zStat     = 0.;
1128    double deltaZ    = 0.1;
1129    double tolerance = 1.e-6;
1130    double initial   = 0.5 * erfc(zStat/M_SQRT2) - this->Stats.getPThreshold();
1131    do{
1132      zStat+=deltaZ;
1133      double current = 0.5 * erfc(zStat/M_SQRT2) - this->Stats.getPThreshold();
1134      if((initial*current)<0.){
1135        zStat-=deltaZ;
1136        deltaZ/=2.;
1137      }
1138    }while(deltaZ>tolerance);
1139    this->Stats.setThreshold( zStat*this->Stats.getSpread() +
1140                              this->Stats.getMiddle() );
1141
1142    ///////////////////////////
1143    //   if(TESTING){
1144    //     std::stringstream ss;
1145    //     float *xplot = new float[2*max];
1146    //     for(int i=0;i<2*max;i++) xplot[i]=float(i)/float(count);
1147    //     cpgopen("latestFDR.ps/vcps");
1148    //     cpgpap(8.,1.);
1149    //     cpgslw(3);
1150    //     cpgenv(0,float(2*max)/float(count),0,orderedP[2*max],0,0);
1151    //     cpglab("i/N (index)", "p-value","");
1152    //     cpgpt(2*max,xplot,orderedP,DOT);
1153
1154    //     ss.str("");
1155    //     ss << "\\gm = " << this->Stats.getMiddle();
1156    //     cpgtext(max/(4.*count),0.9*orderedP[2*max],ss.str().c_str());
1157    //     ss.str("");
1158    //     ss << "\\gs = " << this->Stats.getSpread();
1159    //     cpgtext(max/(4.*count),0.85*orderedP[2*max],ss.str().c_str());
1160    //     ss.str("");
1161    //     ss << "Slope = " << slope;
1162    //     cpgtext(max/(4.*count),0.8*orderedP[2*max],ss.str().c_str());
1163    //     ss.str("");
1164    //     ss << "Alpha = " << this->par.getAlpha();
1165    //     cpgtext(max/(4.*count),0.75*orderedP[2*max],ss.str().c_str());
1166    //     ss.str("");
1167    //     ss << "c\\dN\\u = " << cN;
1168    //     cpgtext(max/(4.*count),0.7*orderedP[2*max],ss.str().c_str());
1169    //     ss.str("");
1170    //     ss << "max = "<<max << " (out of " << count << ")";
1171    //     cpgtext(max/(4.*count),0.65*orderedP[2*max],ss.str().c_str());
1172    //     ss.str("");
1173    //     ss << "Threshold = "<<zStat*this->Stats.getSpread()+this->Stats.getMiddle();
1174    //     cpgtext(max/(4.*count),0.6*orderedP[2*max],ss.str().c_str());
1175 
1176    //     cpgslw(1);
1177    //     cpgsci(RED);
1178    //     cpgmove(0,0);
1179    //     cpgdraw(1,slope);
1180    //     cpgsci(BLUE);
1181    //     cpgsls(DOTTED);
1182    //     cpgmove(0,orderedP[max]);
1183    //     cpgdraw(2*max/float(count),orderedP[max]);
1184    //     cpgmove(max/float(count),0);
1185    //     cpgdraw(max/float(count),orderedP[2*max]);
1186    //     cpgsci(GREEN);
1187    //     cpgsls(SOLID);
1188    //     for(int i=1;i<=10;i++) {
1189    //       ss.str("");
1190    //       ss << float(i)/2. << "\\gs";
1191    //       float prob = 0.5*erfc((float(i)/2.)/M_SQRT2);
1192    //       cpgtick(0, 0, 0, orderedP[2*max],
1193    //        prob/orderedP[2*max],
1194    //        0, 1, 1.5, 90., ss.str().c_str());
1195    //     }
1196    //     cpgend();
1197    //     delete [] xplot;
1198    //   }
1199    delete [] orderedP;
1200
1201  }
1202  //--------------------------------------------------------------------
1203
1204  void Cube::Search()
1205  {
1206    /// @details
1207    /// This acts as a switching function to select the correct searching function based on the user's parameters.
1208    /// @param verboseFlag If true, text is written to stdout describing the search function being used.
1209    if(this->par.getFlagATrous()){
1210      if(this->par.isVerbose()) std::cout<<"Commencing search in reconstructed cube..."<<std::endl;
1211      this->ReconSearch();
1212    } 
1213    else if(this->par.getFlagSmooth()){
1214      if(this->par.isVerbose()) std::cout<<"Commencing search in smoothed cube..."<<std::endl;
1215      this->SmoothSearch();
1216    }
1217    else{
1218      if(this->par.isVerbose()) std::cout<<"Commencing search in cube..."<<std::endl;
1219      this->CubicSearch();
1220    }
1221
1222  }
1223
1224  bool Cube::isDetection(size_t x, size_t y, size_t z)
1225  {
1226    ///  @details
1227    /// Is a given voxel at position (x,y,z) a detection, based on the statistics
1228    ///  in the Cube's StatsContainer?
1229    /// If the pixel lies outside the valid range for the data array,
1230    /// return false.
1231    /// \param x X-value of the Cube's voxel to be tested.
1232    /// \param y Y-value of the Cube's voxel to be tested.
1233    /// \param z Z-value of the Cube's voxel to be tested.
1234
1235    size_t voxel = z*axisDim[0]*axisDim[1] + y*axisDim[0] + x;
1236    return DataArray::isDetection(array[voxel]);
1237  }
1238  //--------------------------------------------------------------------
1239
1240  void Cube::calcObjectFluxes()
1241  {
1242    /// @details
1243    ///  A function to calculate the fluxes and centroids for each
1244    ///  object in the Cube's list of detections. Uses
1245    ///  Detection::calcFluxes() for each object.
1246
1247    std::vector<Detection>::iterator obj;
1248    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1249      obj->calcFluxes(this->array, this->axisDim);
1250      if(this->par.getFlagUserThreshold())
1251        obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() );
1252      else
1253        obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() );
1254    }
1255  }
1256  //--------------------------------------------------------------------
1257
1258  void Cube::calcObjectWCSparams()
1259  {
1260    ///  @details
1261    ///  A function that calculates the WCS parameters for each object in the
1262    ///  Cube's list of detections.
1263    ///  Each object gets an ID number assigned to it (which is simply its order
1264    ///   in the list), and if the WCS is good, the WCS paramters are calculated.
1265
1266    std::vector<Detection>::iterator obj;
1267    int ct=0;
1268    ProgressBar bar;
1269    if(this->par.isVerbose()) bar.init(this->objectList->size());
1270    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1271      //      std::cerr << ct << ' ' << this->array << '\n';
1272      if(this->par.isVerbose()) bar.update(ct);
1273      obj->setID(ct++);
1274      if(!obj->hasParams()){
1275        obj->setCentreType(this->par.getPixelCentre());
1276        obj->calcFluxes(this->array,this->axisDim);
1277        //      obj->calcWCSparams(this->array,this->axisDim,this->head);
1278        obj->calcWCSparams(this->head);
1279        obj->calcIntegFlux(this->array,this->axisDim,this->head);
1280       
1281        if(this->par.getFlagUserThreshold())
1282          obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() );
1283        else
1284          obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() );
1285      }
1286    } 
1287    if(this->par.isVerbose()) bar.remove();
1288
1289    if(!this->head.isWCS()){
1290      // if the WCS is bad, set the object names to Obj01 etc
1291      int numspaces = int(log10(this->objectList->size())) + 1;
1292      std::stringstream ss;
1293      for(size_t i=0;i<this->objectList->size();i++){
1294        ss.str("");
1295        ss << "Obj" << std::setfill('0') << std::setw(numspaces) << i+1;
1296        this->objectList->at(i).setName(ss.str());
1297      }
1298    }
1299 
1300  }
1301  //--------------------------------------------------------------------
1302
1303  void Cube::calcObjectWCSparams(std::vector< std::vector<PixelInfo::Voxel> > bigVoxList)
1304  {
1305    ///  @details
1306    ///  A function that calculates the WCS parameters for each object in the
1307    ///  Cube's list of detections.
1308    ///  Each object gets an ID number assigned to it (which is simply its order
1309    ///   in the list), and if the WCS is good, the WCS paramters are calculated.
1310    ///
1311    ///  This version uses vectors of Voxels to define the fluxes.
1312    ///
1313    /// \param bigVoxList A vector of vectors of Voxels, with the same
1314    /// number of elements as this->objectList, where each element is a
1315    /// vector of Voxels corresponding to the same voxels in each
1316    /// detection and indicating the flux of each voxel.
1317 
1318    std::vector<Detection>::iterator obj;
1319    int ct=0;
1320    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1321      obj->setID(ct+1);
1322      if(!obj->hasParams()){
1323        obj->setCentreType(this->par.getPixelCentre());
1324        obj->calcFluxes(bigVoxList[ct]);
1325        obj->calcWCSparams(this->head);
1326        obj->calcIntegFlux(this->axisDim[2],bigVoxList[ct],this->head);
1327       
1328        if(this->par.getFlagUserThreshold())
1329          obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() );
1330        else
1331          obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() );
1332      }
1333      ct++;
1334    } 
1335
1336    if(!this->head.isWCS()){
1337      // if the WCS is bad, set the object names to Obj01 etc
1338      int numspaces = int(log10(this->objectList->size())) + 1;
1339      std::stringstream ss;
1340      for(size_t i=0;i<this->objectList->size();i++){
1341        ss.str("");
1342        ss << "Obj" << std::setfill('0') << std::setw(numspaces) << i+1;
1343        this->objectList->at(i).setName(ss.str());
1344      }
1345    }
1346 
1347  }
1348  //--------------------------------------------------------------------
1349
1350  void Cube::calcObjectWCSparams(std::map<PixelInfo::Voxel,float> &voxelMap)
1351  {
1352    ///  @details
1353    ///  A function that calculates the WCS parameters for each object in the
1354    ///  Cube's list of detections.
1355    ///  Each object gets an ID number assigned to it (which is simply its order
1356    ///   in the list), and if the WCS is good, the WCS paramters are calculated.
1357    ///
1358    ///  This version uses vectors of Voxels to define the fluxes.
1359    ///
1360    /// \param bigVoxList A vector of vectors of Voxels, with the same
1361    /// number of elements as this->objectList, where each element is a
1362    /// vector of Voxels corresponding to the same voxels in each
1363    /// detection and indicating the flux of each voxel.
1364 
1365    std::vector<Detection>::iterator obj;
1366    int ct=0;
1367    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1368      obj->setID(ct+1);
1369      if(!obj->hasParams()){
1370        obj->setCentreType(this->par.getPixelCentre());
1371        obj->calcFluxes(voxelMap);
1372        obj->calcWCSparams(this->head);
1373        obj->calcIntegFlux(this->axisDim[2],voxelMap,this->head);
1374       
1375        if(this->par.getFlagUserThreshold())
1376          obj->setPeakSNR( obj->getPeakFlux() / this->Stats.getThreshold() );
1377        else
1378          obj->setPeakSNR( (obj->getPeakFlux() - this->Stats.getMiddle()) / this->Stats.getSpread() );
1379      }
1380      ct++;
1381    } 
1382
1383    if(!this->head.isWCS()){
1384      // if the WCS is bad, set the object names to Obj01 etc
1385      int numspaces = int(log10(this->objectList->size())) + 1;
1386      std::stringstream ss;
1387      for(size_t i=0;i<this->objectList->size();i++){
1388        ss.str("");
1389        ss << "Obj" << std::setfill('0') << std::setw(numspaces) << i+1;
1390        this->objectList->at(i).setName(ss.str());
1391      }
1392    }
1393 
1394  }
1395  //--------------------------------------------------------------------
1396
1397  void Cube::updateDetectMap()
1398  {
1399    /// @details A function that, for each detected object in the
1400    ///  cube's list, increments the cube's detection map by the
1401    ///  required amount at each pixel. Uses
1402    ///  updateDetectMap(Detection).
1403
1404    std::vector<Detection>::iterator obj;
1405    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1406      this->updateDetectMap(*obj);
1407    }
1408
1409  }
1410  //--------------------------------------------------------------------
1411
1412  void Cube::updateDetectMap(Detection obj)
1413  {
1414    ///  @details
1415    ///  A function that, for the given object, increments the cube's
1416    ///  detection map by the required amount at each pixel.
1417    ///
1418    ///  \param obj A Detection object that is being incorporated into the map.
1419
1420    std::vector<Voxel> vlist = obj.getPixelSet();
1421    for(std::vector<Voxel>::iterator vox=vlist.begin();vox<vlist.end();vox++)
1422      this->detectMap[vox->getX()+vox->getY()*this->axisDim[0]]++;
1423
1424  }
1425  //--------------------------------------------------------------------
1426
1427  float Cube::enclosedFlux(Detection obj)
1428  {
1429    ///  @details
1430    ///   A function to calculate the flux enclosed by the range
1431    ///    of pixels detected in the object obj (not necessarily all
1432    ///    pixels will have been detected).
1433    ///
1434    ///   \param obj The Detection under consideration.
1435
1436    obj.calcFluxes(this->array, this->axisDim);
1437    int xsize = obj.getXmax()-obj.getXmin()+1;
1438    int ysize = obj.getYmax()-obj.getYmin()+1;
1439    int zsize = obj.getZmax()-obj.getZmin()+1;
1440    std::vector <float> fluxArray(xsize*ysize*zsize,0.);
1441    for(int x=0;x<xsize;x++){
1442      for(int y=0;y<ysize;y++){
1443        for(int z=0;z<zsize;z++){
1444          fluxArray[x+y*xsize+z*ysize*xsize] =
1445            this->getPixValue(x+obj.getXmin(),
1446                              y+obj.getYmin(),
1447                              z+obj.getZmin());
1448          if(this->par.getFlagNegative())
1449            fluxArray[x+y*xsize+z*ysize*xsize] *= -1.;
1450        }
1451      }
1452    }
1453    float sum = 0.;
1454    for(size_t i=0;i<fluxArray.size();i++)
1455      if(!this->par.isBlank(fluxArray[i])) sum+=fluxArray[i];
1456    return sum;
1457  }
1458  //--------------------------------------------------------------------
1459
1460  void Cube::setupColumns()
1461  {
1462    /// @details
1463    ///  A front-end to the two setup routines in columns.cc. 
1464    ///
1465    ///  This first gets the starting precisions, which may be from
1466    ///  the input parameters. It then sets up the columns (calculates
1467    ///  their widths and precisions and so on based on the values
1468    ///  within). The precisions are also stored in each Detection
1469    ///  object.
1470    ///
1471    ///  Need to have called calcObjectWCSparams() somewhere
1472    ///  beforehand.
1473
1474    std::vector<Detection>::iterator obj;
1475    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1476      obj->setVelPrec( this->par.getPrecVel() );
1477      obj->setFpeakPrec( this->par.getPrecFlux() );
1478      obj->setXYZPrec( Column::prXYZ );
1479      obj->setPosPrec( Column::prWPOS );
1480      obj->setFintPrec( this->par.getPrecFlux() );
1481      obj->setSNRPrec( this->par.getPrecSNR() );
1482    }
1483 
1484    this->fullCols.clear();
1485    this->fullCols = getFullColSet(*(this->objectList), this->head);
1486
1487    this->logCols.clear();
1488    this->logCols = getLogColSet(*(this->objectList), this->head);
1489
1490    int vel,fpeak,fint,pos,xyz,snr;
1491    vel = fullCols[VEL].getPrecision();
1492    fpeak = fullCols[FPEAK].getPrecision();
1493    snr = fullCols[SNRPEAK].getPrecision();
1494    xyz = fullCols[X].getPrecision();
1495    xyz = std::max(xyz, fullCols[Y].getPrecision());
1496    xyz = std::max(xyz, fullCols[Z].getPrecision());
1497    if(this->head.isWCS()) fint = fullCols[FINT].getPrecision();
1498    else fint = fullCols[FTOT].getPrecision();
1499    pos = fullCols[WRA].getPrecision();
1500    pos = std::max(pos, fullCols[WDEC].getPrecision());
1501 
1502    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1503      obj->setVelPrec(vel);
1504      obj->setFpeakPrec(fpeak);
1505      obj->setXYZPrec(xyz);
1506      obj->setPosPrec(pos);
1507      obj->setFintPrec(fint);
1508      obj->setSNRPrec(snr);
1509    }
1510
1511  }
1512  //--------------------------------------------------------------------
1513
1514  bool Cube::objAtSpatialEdge(Detection obj)
1515  {
1516    ///  @details
1517    ///   A function to test whether the object obj
1518    ///    lies at the edge of the cube's spatial field --
1519    ///    either at the boundary, or next to BLANKs.
1520    ///
1521    ///   \param obj The Detection under consideration.
1522
1523    bool atEdge = false;
1524
1525    size_t pix = 0;
1526    std::vector<Voxel> voxlist = obj.getPixelSet();
1527    while(!atEdge && pix<voxlist.size()){
1528      // loop over each pixel in the object, until we find an edge pixel.
1529      for(int dx=-1;dx<=1;dx+=2){
1530        if( ((voxlist[pix].getX()+dx)<0) ||
1531            ((voxlist[pix].getX()+dx)>=this->axisDim[0]) )
1532          atEdge = true;
1533        else if(this->isBlank(voxlist[pix].getX()+dx,
1534                              voxlist[pix].getY(),
1535                              voxlist[pix].getZ()))
1536          atEdge = true;
1537      }
1538      for(int dy=-1;dy<=1;dy+=2){
1539        if( ((voxlist[pix].getY()+dy)<0) ||
1540            ((voxlist[pix].getY()+dy)>=this->axisDim[1]) )
1541          atEdge = true;
1542        else if(this->isBlank(voxlist[pix].getX(),
1543                              voxlist[pix].getY()+dy,
1544                              voxlist[pix].getZ()))
1545          atEdge = true;
1546      }
1547      pix++;
1548    }
1549
1550    return atEdge;
1551  }
1552  //--------------------------------------------------------------------
1553
1554  bool Cube::objAtSpectralEdge(Detection obj)
1555  {
1556    ///   @details
1557    ///   A function to test whether the object obj
1558    ///    lies at the edge of the cube's spectral extent --
1559    ///    either at the boundary, or next to BLANKs.
1560    ///
1561    ///   \param obj The Detection under consideration.
1562
1563    bool atEdge = false;
1564
1565    size_t pix = 0;
1566    std::vector<Voxel> voxlist = obj.getPixelSet();
1567    while(!atEdge && pix<voxlist.size()){
1568      // loop over each pixel in the object, until we find an edge pixel.
1569      for(int dz=-1;dz<=1;dz+=2){
1570        if( ((voxlist[pix].getZ()+dz)<0) ||
1571            ((voxlist[pix].getZ()+dz)>=this->axisDim[2]))
1572          atEdge = true;
1573        else if(this->isBlank(voxlist[pix].getX(),
1574                              voxlist[pix].getY(),
1575                              voxlist[pix].getZ()+dz))
1576          atEdge = true;
1577      }
1578      pix++;
1579    }
1580
1581    return atEdge;
1582  }
1583  //--------------------------------------------------------------------
1584
1585  void Cube::setObjectFlags()
1586  {
1587    /// @details
1588    ///   A function to set any warning flags for all the detected objects
1589    ///    associated with the cube.
1590    ///   Flags to be looked for:
1591    ///    <ul><li> Negative enclosed flux (N)
1592    ///        <li> Detection at edge of field (spatially) (E)
1593    ///        <li> Detection at edge of spectral region (S)
1594    ///    </ul>
1595
1596    std::vector<Detection>::iterator obj;
1597    for(obj=this->objectList->begin();obj<this->objectList->end();obj++){
1598
1599      if( this->enclosedFlux(*obj) < 0. ) 
1600        obj->addToFlagText("N");
1601
1602      if( this->objAtSpatialEdge(*obj) )
1603        obj->addToFlagText("E");
1604
1605      if( this->objAtSpectralEdge(*obj) && (this->axisDim[2] > 2))
1606        obj->addToFlagText("S");
1607
1608      if(obj->getFlagText()=="") obj->addToFlagText("-");
1609
1610    }
1611
1612  }
1613  //--------------------------------------------------------------------
1614
1615  /****************************************************************/
1616  /////////////////////////////////////////////////////////////
1617  //// Functions for Image class
1618  /////////////////////////////////////////////////////////////
1619
1620  Image::Image(size_t size)
1621  {
1622    // need error handling in case size<0 !!!
1623    this->numPixels = this->numDim = 0;
1624    this->minSize = 2;
1625    if(size<0)
1626      duchampError("Image(size)","Negative size -- could not define Image");
1627    else{
1628      if(size>0 && !this->arrayAllocated){
1629        this->array = new float[size];
1630        this->arrayAllocated = true;
1631      }
1632      this->numPixels = size;
1633      this->axisDim = new size_t[2];
1634      this->axisDimAllocated = true;
1635      this->numDim = 2;
1636    }
1637  }
1638  //--------------------------------------------------------------------
1639
1640  Image::Image(size_t *dimensions)
1641  {
1642    this->numPixels = this->numDim = 0;
1643    this->minSize = 2;
1644    int size = dimensions[0] * dimensions[1];
1645    if(size<0)
1646      duchampError("Image(dimArray)","Negative size -- could not define Image");
1647    else{
1648      this->numPixels = size;
1649      if(size>0){
1650        this->array = new float[size];
1651        this->arrayAllocated = true;
1652      }
1653      this->numDim=2;
1654      this->axisDim = new size_t[2];
1655      this->axisDimAllocated = true;
1656      for(int i=0;i<2;i++) this->axisDim[i] = dimensions[i];
1657    }
1658  }
1659  //--------------------------------------------------------------------
1660  Image::Image(const Image &i):
1661    DataArray(i)
1662  {
1663    this->operator=(i);
1664  }
1665
1666  Image& Image::operator=(const Image &i)
1667  {
1668    if(this==&i) return *this;
1669    ((DataArray &) *this) = i;
1670    this->minSize = i.minSize;
1671    return *this;
1672  }
1673
1674  //--------------------------------------------------------------------
1675
1676  void Image::saveArray(float *input, size_t size)
1677  {
1678    /// @details
1679    /// Saves the array in input to the pixel array Image::array.
1680    /// The size of the array given must be the same as the current number of
1681    /// pixels, else an error message is returned and nothing is done.
1682    /// \param input The array of values to be saved.
1683    /// \param size The size of input.
1684
1685    if(size != this->numPixels)
1686      duchampError("Image::saveArray",
1687                   "Input array different size to existing array. Cannot save.");
1688    else {
1689      if(this->numPixels>0 && this->arrayAllocated) delete [] array;
1690      this->numPixels = size;
1691      if(this->numPixels>0){
1692        this->array = new float[size];
1693        this->arrayAllocated = true;
1694        for(int i=0;i<size;i++) this->array[i] = input[i];
1695      }
1696    }
1697  }
1698  //--------------------------------------------------------------------
1699
1700  void Image::extractSpectrum(float *Array, size_t *dim, size_t pixel)
1701  {
1702    /// @details
1703    ///  A function to extract a 1-D spectrum from a 3-D array.
1704    ///  The array is assumed to be 3-D with the third dimension the spectral one.
1705    ///  The spectrum extracted is the one lying in the spatial pixel referenced
1706    ///    by the third argument.
1707    ///  The extracted spectrum is stored in the pixel array Image::array.
1708    /// \param Array The array containing the pixel values, from which
1709    ///               the spectrum is extracted.
1710    /// \param dim The array of dimension values.
1711    /// \param pixel The spatial pixel that contains the desired spectrum.
1712
1713    if((pixel<0)||(pixel>=dim[0]*dim[1]))
1714      duchampError("Image::extractSpectrum",
1715                   "Requested spatial pixel outside allowed range. Cannot save.");
1716    else if(dim[2] != this->numPixels)
1717      duchampError("Image::extractSpectrum",
1718                   "Input array different size to existing array. Cannot save.");
1719    else {
1720      if(this->numPixels>0 && this->arrayAllocated) delete [] array;
1721      this->numPixels = dim[2];
1722      if(this->numPixels>0){
1723        this->array = new float[dim[2]];
1724        this->arrayAllocated = true;
1725        for(int z=0;z<dim[2];z++) this->array[z] = Array[z*dim[0]*dim[1] + pixel];
1726      }
1727    }
1728  }
1729  //--------------------------------------------------------------------
1730
1731  void Image::extractSpectrum(Cube &cube, size_t pixel)
1732  {
1733    /// @details
1734    ///  A function to extract a 1-D spectrum from a Cube class
1735    ///  The spectrum extracted is the one lying in the spatial pixel referenced
1736    ///    by the second argument.
1737    ///  The extracted spectrum is stored in the pixel array Image::array.
1738    /// \param cube The Cube containing the pixel values, from which the spectrum is extracted.
1739    /// \param pixel The spatial pixel that contains the desired spectrum.
1740
1741    size_t zdim = cube.getDimZ();
1742    size_t spatSize = cube.getDimX()*cube.getDimY();
1743    if((pixel<0)||(pixel>=spatSize))
1744      duchampError("Image::extractSpectrum",
1745                   "Requested spatial pixel outside allowed range. Cannot save.");
1746    else if(zdim != this->numPixels)
1747      duchampError("Image::extractSpectrum",
1748                   "Input array different size to existing array. Cannot save.");
1749    else {
1750      if(this->numPixels>0 && this->arrayAllocated) delete [] array;
1751      this->numPixels = zdim;
1752      if(this->numPixels>0){
1753        this->array = new float[zdim];
1754        this->arrayAllocated = true;
1755        for(int z=0;z<zdim;z++)
1756          this->array[z] = cube.getPixValue(z*spatSize + pixel);
1757      }
1758    }
1759  }
1760  //--------------------------------------------------------------------
1761
1762  void Image::extractImage(float *Array, size_t *dim, size_t channel)
1763  {
1764    /// @details
1765    ///  A function to extract a 2-D image from a 3-D array.
1766    ///  The array is assumed to be 3-D with the third dimension the spectral one.
1767    ///  The dimensions of the array are in the dim[] array.
1768    ///  The image extracted is the one lying in the channel referenced
1769    ///    by the third argument.
1770    ///  The extracted image is stored in the pixel array Image::array.
1771    /// \param Array The array containing the pixel values, from which the image is extracted.
1772    /// \param dim The array of dimension values.
1773    /// \param channel The spectral channel that contains the desired image.
1774
1775    size_t spatSize = dim[0]*dim[1];
1776    if((channel<0)||(channel>=dim[2]))
1777      duchampError("Image::extractImage",
1778                   "Requested channel outside allowed range. Cannot save.");
1779    else if(spatSize != this->numPixels)
1780      duchampError("Image::extractImage",
1781                   "Input array different size to existing array. Cannot save.");
1782    else {
1783      if(this->numPixels>0 && this->arrayAllocated) delete [] array;
1784      this->numPixels = spatSize;
1785      if(this->numPixels>0){
1786        this->array = new float[spatSize];
1787        this->arrayAllocated = true;
1788        for(int npix=0; npix<spatSize; npix++)
1789          this->array[npix] = Array[channel*spatSize + npix];
1790      }
1791    }
1792  }
1793  //--------------------------------------------------------------------
1794
1795  void Image::extractImage(Cube &cube, size_t channel)
1796  {
1797    /// @details
1798    ///  A function to extract a 2-D image from Cube class.
1799    ///  The image extracted is the one lying in the channel referenced
1800    ///    by the second argument.
1801    ///  The extracted image is stored in the pixel array Image::array.
1802    /// \param cube The Cube containing the pixel values, from which the image is extracted.
1803    /// \param channel The spectral channel that contains the desired image.
1804
1805    size_t spatSize = cube.getDimX()*cube.getDimY();
1806    if((channel<0)||(channel>=cube.getDimZ()))
1807      duchampError("Image::extractImage",
1808                   "Requested channel outside allowed range. Cannot save.");
1809    else if(spatSize != this->numPixels)
1810      duchampError("Image::extractImage",
1811                   "Input array different size to existing array. Cannot save.");
1812    else {
1813      if(this->numPixels>0 && this->arrayAllocated) delete [] array;
1814      this->numPixels = spatSize;
1815      if(this->numPixels>0){
1816        this->array = new float[spatSize];
1817        this->arrayAllocated = true;
1818        for(int npix=0; npix<spatSize; npix++)
1819          this->array[npix] = cube.getPixValue(channel*spatSize + npix);
1820      }
1821    }
1822  }
1823  //--------------------------------------------------------------------
1824
1825  void Image::removeMW()
1826  {
1827    /// @details
1828    ///  A function to remove the Milky Way range of channels from a 1-D spectrum.
1829    ///  The array in this Image is assumed to be 1-D, with only the first axisDim
1830    ///    equal to 1.
1831    ///  The values of the MW channels are set to 0, unless they are BLANK.
1832
1833    if(this->par.getFlagMW() && (this->axisDim[1]==1) ){
1834      for(int z=0;z<this->axisDim[0];z++){
1835        if(!this->isBlank(z) && this->par.isInMW(z)) this->array[z]=0.;
1836      }
1837    }
1838  }
1839  //--------------------------------------------------------------------
1840
1841  std::vector<Object2D> Image::findSources2D()
1842  {
1843    std::vector<bool> thresholdedArray(this->axisDim[0]*this->axisDim[1]);
1844    for(int posY=0;posY<this->axisDim[1];posY++){
1845      for(int posX=0;posX<this->axisDim[0];posX++){
1846        int loc = posX + this->axisDim[0]*posY;
1847        thresholdedArray[loc] = this->isDetection(posX,posY);
1848      }
1849    }
1850    return lutz_detect(thresholdedArray, this->axisDim[0], this->axisDim[1], this->minSize);
1851  }
1852
1853  std::vector<Scan> Image::findSources1D()
1854  {
1855    std::vector<bool> thresholdedArray(this->axisDim[0]);
1856    for(int posX=0;posX<this->axisDim[0];posX++){
1857      thresholdedArray[posX] = this->isDetection(posX,0);
1858    }
1859    return spectrumDetect(thresholdedArray, this->axisDim[0], this->minSize);
1860  }
1861
1862
1863  std::vector< std::vector<PixelInfo::Voxel> > Cube::getObjVoxList()
1864  {
1865   
1866    std::vector< std::vector<PixelInfo::Voxel> > biglist;
1867   
1868    std::vector<Detection>::iterator obj;
1869    for(obj=this->objectList->begin(); obj<this->objectList->end(); obj++) {
1870
1871      Cube *subcube = new Cube;
1872      subcube->pars() = this->par;
1873      subcube->pars().setVerbosity(false);
1874      subcube->pars().setFlagSubsection(true);
1875      duchamp::Section sec = obj->getBoundingSection();
1876      subcube->pars().setSubsection( sec.getSection() );
1877      if(subcube->pars().verifySubsection() == FAILURE)
1878        duchampError("get object voxel list","Unable to verify the subsection - something's wrong!");
1879      if(subcube->getCube() == FAILURE)
1880        duchampError("get object voxel list","Unable to read the FITS file - something's wrong!");
1881      std::vector<PixelInfo::Voxel> voxlist = obj->getPixelSet();
1882      std::vector<PixelInfo::Voxel>::iterator vox;
1883      for(vox=voxlist.begin(); vox<voxlist.end(); vox++){
1884        size_t pix = (vox->getX()-subcube->pars().getXOffset()) +
1885          subcube->getDimX()*(vox->getY()-subcube->pars().getYOffset()) +
1886          subcube->getDimX()*subcube->getDimY()*(vox->getZ()-subcube->pars().getZOffset());
1887        vox->setF( subcube->getPixValue(pix) );
1888      }
1889      biglist.push_back(voxlist);
1890      delete subcube;
1891
1892    }
1893
1894    return biglist;
1895
1896  }
1897
1898}
Note: See TracBrowser for help on using the repository browser.