source: trunk/src/Cubes/momentMap.cc @ 668

Last change on this file since 668 was 668, checked in by MatthewWhiting, 14 years ago

Moving the moment map calculation to a separate function, so that it can be called from multiple places.

File size: 5.1 KB
Line 
1// -----------------------------------------------------------------------
2// momentMap.cc : functions to calculate the momment-0 map of the cube
3// -----------------------------------------------------------------------
4// Copyright (C) 2006, Matthew Whiting, ATNF
5//
6// This program is free software; you can redistribute it and/or modify it
7// under the terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2 of the License, or (at your
9// option) any later version.
10//
11// Duchamp is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14// for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Duchamp; if not, write to the Free Software Foundation,
18// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
19//
20// Correspondence concerning Duchamp may be directed to:
21//    Internet email: Matthew.Whiting [at] atnf.csiro.au
22//    Postal address: Dr. Matthew Whiting
23//                    Australia Telescope National Facility, CSIRO
24//                    PO Box 76
25//                    Epping NSW 1710
26//                    AUSTRALIA
27// -----------------------------------------------------------------------
28#include <iostream>
29#include <iomanip>
30#include <sstream>
31#include <math.h>
32#include <string.h>
33#include <cpgplot.h>
34#include <wcslib/cpgsbox.h>
35#include <wcslib/pgwcsl.h>
36#include <wcslib/wcs.h>
37#include <duchamp/duchamp.hh>
38#include <duchamp/param.hh>
39#include <duchamp/fitsHeader.hh>
40#include <duchamp/Detection/detection.hh>
41#include <duchamp/PixelMap/Object3D.hh>
42#include <duchamp/Cubes/cubes.hh>
43#include <duchamp/Utils/utils.hh>
44
45using namespace PixelInfo;
46
47namespace duchamp
48{
49
50  void Cube::getMomentMapForPlot(float *momentMap, float &z1, float &z2)
51  {
52
53    long xdim=this->axisDim[0];
54    long ydim=this->axisDim[1];
55
56    std::vector<bool> detectedPixels;
57    this->getMomentMap(momentMap, detectedPixels);
58    std::cerr << momentMap << "\n";
59
60    int count=0;
61    for(int i=0;i<xdim*ydim;i++) {
62      if(momentMap[i]>0.){
63        float logmm = log10(momentMap[i]);
64        bool addPixel = detectedPixels[i];
65        if(addPixel){
66          if(count==0) z1 = z2 = logmm;
67          else{
68            if(logmm < z1) z1 = logmm;
69            if(logmm > z2) z2 = logmm;
70          }
71          count++;
72        }
73      }
74    }
75   
76    for(int i=0;i<xdim*ydim;i++) {
77      bool addPixel = detectedPixels[i];
78      addPixel = addPixel && (momentMap[i]>0.);
79      if(!addPixel) momentMap[i] = z1-1.;
80      else momentMap[i] = log10(momentMap[i]);
81    }
82
83  }
84
85  void Cube::getMomentMap(float *momentMap, std::vector<bool> &detectedPixels)
86  {
87
88    long xdim=this->axisDim[0];
89    long ydim=this->axisDim[1];
90    long zdim=this->axisDim[2];
91
92    // get the list of objects that should be plotted.
93    std::vector<bool> objectChoice = this->par.getObjectChoices(this->objectList->size());
94
95    bool *isObj = new bool[xdim*ydim*zdim];
96    for(int i=0;i<xdim*ydim*zdim;i++) isObj[i] = false;
97    for(size_t i=0;i<this->objectList->size();i++){
98      if(objectChoice[i]){
99        std::vector<Voxel> voxlist = this->objectList->at(i).getPixelSet();
100        std::vector<Voxel>::iterator vox;
101        for(vox=voxlist.begin();vox<voxlist.end();vox++){
102          int pixelpos = vox->getX() + xdim*vox->getY() + xdim*ydim*vox->getZ();
103          isObj[pixelpos] = true;
104        }
105      }
106    }
107
108    // Array containing the moment map, initialised to zero
109    for(int i=0;i<xdim*ydim;i++) momentMap[i] = 0.;
110
111    // Bool vector containing yes/no on whether a given spatial pixel has a detected object.
112    // Initialise this to false everywhere.
113    detectedPixels = std::vector<bool>(xdim*ydim,false);
114
115    // if we are looking for negative features, we need to invert the
116    //  detected pixels for the moment map
117    float sign = 1.;
118    if(this->pars().getFlagNegative()) sign = -1.;
119
120    float deltaVel;
121    double x,y;
122
123    double *zArray  = new double[zdim];
124    for(int z=0; z<zdim; z++) zArray[z] = double(z);
125   
126    for(int pix=0; pix<xdim*ydim; pix++){
127
128      x = double(pix%xdim);
129      y = double(pix/xdim);
130
131      if(!this->isBlank(pix) &&   // only do this for non-blank pixels. Judge this by the first pixel of the channel.
132         this->detectMap[pix] > 0 ){  // and only where something was detected in the spectrum.
133
134        double * world = this->head.pixToVel(x,y,zArray,zdim);
135     
136        for(int z=0; z<zdim; z++){     
137          int pos =  z*xdim*ydim + pix;  // the voxel in the cube
138          if(isObj[pos]){ // if it's an object pixel...
139            // delta-vel is half the distance between adjacent channels.
140            // if at end, then just use 0-1 or (zdim-1)-(zdim-2) distance
141            if(z==0){
142              if(zdim==1) deltaVel=1.; // pathological case -- if 2D image.
143              else deltaVel = world[z+1] - world[z];
144            }
145            else if(z==(zdim-1)) deltaVel = world[z-1] - world[z];
146            else deltaVel = (world[z+1] - world[z-1]) / 2.;
147
148            momentMap[pix] += sign * this->array[pos] * fabs(deltaVel);
149            detectedPixels[pix] = true;
150
151          }
152        }
153        delete [] world;
154      }
155
156    }
157   
158    delete [] zArray;
159    delete [] isObj;
160
161  }
162
163
164}
Note: See TracBrowser for help on using the repository browser.