source: trunk/src/Detection/columns.cc @ 221

Last change on this file since 221 was 221, checked in by Matthew Whiting, 17 years ago

More documentation being added to source code, with a new file (src/Utils/Hanning.cc) added as well.

File size: 12.5 KB
Line 
1#include <iostream>
2#include <sstream>
3
4#include <math.h>
5#include <duchamp.hh>
6#include <param.hh>
7#include <vector>
8#include <string>
9#include <Detection/detection.hh>
10#include <Detection/columns.hh>
11
12using std::string;
13using std::vector;
14using namespace Column;
15
16Col::Col(){
17  width=1;
18  precision=0;
19  name=" ";
20  units=" ";
21};
22
23Col::~Col(){}
24
25Col::Col(int num)
26{
27  /**
28   * A specialised constructor that defines one of the default
29   *  columns, as defined in the Column namespace
30   * \param num The number of the column to be constructed.
31   *            Corresponds to the order of the columns in the const
32   *            arrays in the Column namespace.
33   */
34  if((num>=0)&&(num<numColumns)){
35    this->width =     defaultWidth[num];
36    this->precision = defaultPrec[num];
37    this->name =      defaultName[num];
38    this->units =     defaultUnits[num];
39  }
40  else{
41    std::stringstream errmsg;
42    errmsg << "Incorrect value for Col(num) --> num="<<num
43           << ", should be between 0 and " << numColumns-1 << ".\n";
44    duchampError("Col constructor", errmsg.str());
45    this->width = 1;
46    this->precision = 0;
47    this->name = " ";
48    this->units = " ";
49  }
50}
51
52template <class T> void Col::printEntry(std::ostream &stream, T value)
53{
54  stream << std::setprecision(this->precision)
55         << std::setw(this->width)
56         << std::setfill(' ')
57         << value;
58}
59template void Col::printEntry<int>(std::ostream &stream, int value);
60template void Col::printEntry<long>(std::ostream &stream, long value);
61template void Col::printEntry<unsigned>(std::ostream &stream, unsigned value);
62template void Col::printEntry<float>(std::ostream &stream, float value);
63template void Col::printEntry<double>(std::ostream &stream, double value);
64template void Col::printEntry<string>(std::ostream &stream, string value);
65
66
67vector<Col> getFullColSet(vector<Detection> &objectList, FitsHeader &head)
68{
69  /**
70   *  Returns a vector of Col for results file output.
71   *
72   *  A function that returns a vector of Col objects containing
73   *    information on the columns necessary for output to the results file:
74   *    Obj#,NAME,X,Y,Z,RA,DEC,VEL,w_RA,w_DEC,w_VEL,F_tot,F_int,F_peak,
75   *                X1,X2,Y1,Y2,Z1,Z2,Npix,Flag
76   *
77   *   Each object in the provided objectList is checked to see if
78   *    it requires any column to be widened, or for that column to
79   *    have its precision increased.
80   *
81   *   Both Ftot and Fint are provided -- it is up to the calling function to
82   *    determine which to use.
83   */
84
85  vector<Col> newset;
86
87  // set up the default columns
88
89  newset.push_back( Col(NUM) );
90  newset.push_back( Col(NAME) );
91  newset.push_back( Col(X) );
92  newset.push_back( Col(Y) );
93  newset.push_back( Col(Z) );
94  newset.push_back( Col(RA) );
95  newset.push_back( Col(DEC) );
96  newset.push_back( Col(VEL) );
97  newset.push_back( Col(WRA) );
98  newset.push_back( Col(WDEC) );
99  newset.push_back( Col(WVEL) );
100  newset.push_back( Col(FINT) );
101  newset.push_back( Col(FTOT) );
102  newset.push_back( Col(FPEAK) );
103  newset.push_back( Col(SNRPEAK) );
104  newset.push_back( Col(X1) );
105  newset.push_back( Col(X2) );
106  newset.push_back( Col(Y1) );
107  newset.push_back( Col(Y2) );
108  newset.push_back( Col(Z1) );
109  newset.push_back( Col(Z2) );
110  newset.push_back( Col(NPIX) );
111  newset.push_back( Col(FLAG) );
112
113   // Now test each object against each new column
114  for( int obj = 0; obj < objectList.size(); obj++){
115    string tempstr;
116    int tempwidth;
117    float val,minval;
118
119    // Obj#
120    tempwidth = int( log10(objectList[obj].getID()) + 1) +
121      newset[NUM].getPrecision() + 1;
122    for(int i=newset[NUM].getWidth();i<tempwidth;i++) newset[NUM].widen();
123
124    // Name
125    tempwidth = objectList[obj].getName().size() + 1;
126    for(int i=newset[NAME].getWidth();i<tempwidth;i++) newset[NAME].widen();
127
128    // X position
129    val = objectList[obj].getXcentre();
130    tempwidth = int( log10(val) + 1) + newset[X].getPrecision() + 2;
131    for(int i=newset[X].getWidth();i<tempwidth;i++) newset[X].widen();
132    if((val<1.)&&(val>0.)){
133      minval = pow(10, -1. * newset[X].getPrecision()+1);
134      if(val < minval) newset[X].upPrec();
135    }
136    // Y position
137    val = objectList[obj].getYcentre();
138    tempwidth = int( log10(val) + 1) + newset[Y].getPrecision() + 2;
139    for(int i=newset[Y].getWidth();i<tempwidth;i++) newset[Y].widen();
140    if((val<1.)&&(val>0.)){
141      minval = pow(10, -1. * newset[Y].getPrecision()+1);
142      if(val < minval) newset[Y].upPrec();
143    }
144    // Z position
145    val = objectList[obj].getZcentre();
146    tempwidth = int( log10(val) + 1) + newset[Z].getPrecision() + 2;
147    for(int i=newset[Z].getWidth();i<tempwidth;i++) newset[Z].widen();
148    if((val<1.)&&(val>0.)){
149      minval = pow(10, -1. * newset[Z].getPrecision()+1);
150      if((val>0.)&&(val < minval)) newset[Z].upPrec();
151    }
152
153    if(head.isWCS()){ 
154      // RA -- assign correct title. Check width but should be ok by definition
155      tempstr = head.getWCS()->lngtyp;
156      newset[RA].setName(tempstr);
157      tempwidth = objectList[obj].getRAs().size() + 1;
158      for(int i=newset[RA].getWidth();i<tempwidth;i++) newset[RA].widen();
159     
160      // Dec -- assign correct title. Check width, should be ok by definition
161      tempstr = head.getWCS()->lattyp;
162      newset[DEC].setName(tempstr);
163      tempwidth = objectList[obj].getDecs().size() + 1;
164      for(int i=newset[DEC].getWidth();i<tempwidth;i++) newset[DEC].widen();
165
166      // Vel -- check width, title and units.
167      if(head.getWCS()->restfrq == 0) // using frequency, not velocity
168        newset[VEL].setName("FREQ");
169      newset[VEL].setUnits("[" + head.getSpectralUnits() + "]");
170      tempwidth = newset[VEL].getUnits().size() + 1;
171      for(int i=newset[VEL].getWidth();i<tempwidth;i++) newset[VEL].widen();
172
173      val = objectList[obj].getVel();
174      tempwidth = int( log10(fabs(val)) + 1) + newset[VEL].getPrecision() + 2;
175      if(val<0) tempwidth++;
176      for(int i=newset[VEL].getWidth();i<tempwidth;i++) newset[VEL].widen();
177      if((fabs(val) < 1.)&&(val>0.)){
178        minval = pow(10, -1. * newset[VEL].getPrecision()+1);
179        if(val < minval) newset[VEL].upPrec();
180      }
181
182      // w_RA -- check width & title. leave units for the moment.
183      tempwidth = newset[RA].getUnits().size() + 1;
184      for(int i=newset[RA].getWidth();i<tempwidth;i++) newset[RA].widen();
185      val = objectList[obj].getRAWidth();
186      tempwidth = int( log10(fabs(val)) + 1) + newset[WRA].getPrecision() + 2;
187      if(val<0) tempwidth++;
188      for(int i=newset[WRA].getWidth();i<tempwidth;i++) newset[WRA].widen();
189      if((fabs(val) < 1.)&&(val>0.)){
190        minval = pow(10, -1. * newset[WRA].getPrecision()+1);
191        if(val < minval) newset[WRA].upPrec();
192      }
193
194      // w_DEC -- check width & title. leave units for the moment.
195      tempwidth = newset[DEC].getUnits().size() + 1;
196      for(int i=newset[DEC].getWidth();i<tempwidth;i++) newset[DEC].widen();
197      val = objectList[obj].getDecWidth();
198      tempwidth = int( log10(fabs(val)) + 1) + newset[WDEC].getPrecision() + 2;
199      if(val<0) tempwidth++;
200      for(int i=newset[WDEC].getWidth();i<tempwidth;i++) newset[WDEC].widen();
201      if((fabs(val) < 1.)&&(val>0.)){
202        minval = pow(10, -1. * newset[WDEC].getPrecision()+1);
203        if(val < minval) newset[WDEC].upPrec();
204      }
205
206      // w_Vel -- check width, title and units.
207      if(head.getWCS()->restfrq == 0) // using frequency, not velocity
208        newset[WVEL].setName("w_FREQ");
209      newset[WVEL].setUnits("[" + head.getSpectralUnits() + "]");
210      tempwidth = newset[WVEL].getUnits().size() + 1;
211      for(int i=newset[WVEL].getWidth();i<tempwidth;i++) newset[WVEL].widen();
212      val = objectList[obj].getVel();
213      tempwidth = int( log10(fabs(val)) + 1) + newset[WVEL].getPrecision() + 2;
214      if(val<0) tempwidth++;
215      for(int i=newset[WVEL].getWidth();i<tempwidth;i++) newset[WVEL].widen();
216      if((fabs(val) < 1.)&&(val>0.)){
217        minval = pow(10, -1. * newset[WVEL].getPrecision()+1);
218        if(val < minval) newset[WVEL].upPrec();
219      }
220
221      // F_int -- check width & units
222      newset[FINT].setUnits("[" + head.getIntFluxUnits() + "]");
223      tempwidth = newset[FINT].getUnits().size() + 1;
224      for(int i=newset[FINT].getWidth();i<tempwidth;i++) newset[FINT].widen();
225      val = objectList[obj].getIntegFlux();
226      tempwidth = int( log10(fabs(val)) + 1) + newset[FINT].getPrecision() + 2;
227      if(val<0) tempwidth++;
228      for(int i=newset[FINT].getWidth();i<tempwidth;i++) newset[FINT].widen();
229      if((fabs(val) < 1.)&&(val>0.)){
230        minval = pow(10, -1. * newset[FINT].getPrecision()+1);
231        if(val < minval) newset[FINT].upPrec();
232      }
233    }
234
235    // F_tot
236    newset[FTOT].setUnits("[" + head.getFluxUnits() + "]");
237    tempwidth = newset[FTOT].getUnits().size() + 1;
238    for(int i=newset[FTOT].getWidth();i<tempwidth;i++) newset[FTOT].widen();
239    val = objectList[obj].getTotalFlux();
240    tempwidth = int( log10(fabs(val)) + 1) + newset[FTOT].getPrecision() + 2;
241    if(val<0) tempwidth++;
242    for(int i=newset[FTOT].getWidth();i<tempwidth;i++) newset[FTOT].widen();
243    if((fabs(val) < 1.)&&(val>0.)){
244      minval = pow(10, -1. * newset[FTOT].getPrecision()+1);
245      if(val < minval) newset[FTOT].upPrec();
246    }
247
248    // F_peak
249    newset[FPEAK].setUnits("[" + head.getFluxUnits() + "]");
250    tempwidth = newset[FPEAK].getUnits().size() + 1;
251    for(int i=newset[FPEAK].getWidth();i<tempwidth;i++) newset[FPEAK].widen();
252    val = objectList[obj].getPeakFlux();
253    tempwidth = int( log10(fabs(val)) + 1) + newset[FPEAK].getPrecision() + 2;
254    if(val<0) tempwidth++;
255    for(int i=newset[FPEAK].getWidth();i<tempwidth;i++) newset[FPEAK].widen();
256    if((fabs(val) < 1.)&&(val>0.)){
257      minval = pow(10, -1. * newset[FPEAK].getPrecision()+1);
258      if(val < minval) newset[FPEAK].upPrec();
259    }
260
261    // S/N_peak
262    val = objectList[obj].getPeakSNR();
263    tempwidth = int( log10(fabs(val)) + 1) + newset[SNRPEAK].getPrecision() +2;
264    if(val<0) tempwidth++;
265    for(int i=newset[SNRPEAK].getWidth();i<tempwidth;i++)
266      newset[SNRPEAK].widen();
267    if((fabs(val) < 1.)&&(val>0.)){
268      minval = pow(10, -1. * newset[SNRPEAK].getPrecision()+1);
269      if(val < minval) newset[SNRPEAK].upPrec();
270    }
271
272    // X1 position
273    tempwidth = int( log10(objectList[obj].getXmin()) + 1) +
274      newset[X1].getPrecision() + 1;
275    for(int i=newset[X1].getWidth();i<tempwidth;i++) newset[X1].widen();
276    // X2 position
277    tempwidth = int( log10(objectList[obj].getXmax()) + 1) +
278      newset[X2].getPrecision() + 1;
279    for(int i=newset[X2].getWidth();i<tempwidth;i++) newset[X2].widen();
280    // Y1 position
281    tempwidth = int( log10(objectList[obj].getYmin()) + 1) +
282      newset[Y1].getPrecision() + 1;
283    for(int i=newset[Y1].getWidth();i<tempwidth;i++) newset[Y1].widen();
284    // Y2 position
285    tempwidth = int( log10(objectList[obj].getYmax()) + 1) +
286      newset[Y2].getPrecision() + 1;
287    for(int i=newset[Y2].getWidth();i<tempwidth;i++) newset[Y2].widen();
288    // Z1 position
289    tempwidth = int( log10(objectList[obj].getZmin()) + 1) +
290      newset[Z1].getPrecision() + 1;
291    for(int i=newset[Z1].getWidth();i<tempwidth;i++) newset[Z1].widen();
292    // Z2 position
293    tempwidth = int( log10(objectList[obj].getZmax()) + 1) +
294      newset[Z2].getPrecision() + 1;
295    for(int i=newset[Z2].getWidth();i<tempwidth;i++) newset[Z2].widen();
296
297    // Npix
298    tempwidth = int( log10(objectList[obj].getSize()) + 1) +
299      newset[NPIX].getPrecision() + 1;
300    for(int i=newset[NPIX].getWidth();i<tempwidth;i++) newset[NPIX].widen();
301   
302  }
303
304  return newset;
305
306}
307
308vector<Col> getLogColSet(vector<Detection> &objectList, FitsHeader &head)
309{
310  /**
311   *  Returns a vector of Col for logfile output.
312   *   
313   *  A function that returns a vector of Col objects containing
314   *    information on the columns necessary for logfile output:
315   *    Obj#,X,Y,Z,F_tot,F_peak,X1,X2,Y1,Y2,Z1,Z2,Npix
316   *
317   *   Each object in the provided objectList is checked to see if
318   *    it requires any column to be widened, or for that column to
319   *    have its precision increased.
320   */
321
322  vector<Col> newset,tempset;
323 
324  // set up the default columns:
325  //  get from FullColSet, and select only the ones we want.
326  tempset = getFullColSet(objectList,head);
327
328  newset.push_back( tempset[NUM] );
329  newset.push_back( tempset[X] );
330  newset.push_back( tempset[Y] );
331  newset.push_back( tempset[Z] );
332  newset.push_back( tempset[FTOT] );
333  newset.push_back( tempset[FPEAK] );
334  newset.push_back( tempset[SNRPEAK] );
335  newset.push_back( tempset[X1] );
336  newset.push_back( tempset[X2] );
337  newset.push_back( tempset[Y1] );
338  newset.push_back( tempset[Y2] );
339  newset.push_back( tempset[Z1] );
340  newset.push_back( tempset[Z2] );
341  newset.push_back( tempset[NPIX] );
342
343  return newset;
344
345}
Note: See TracBrowser for help on using the repository browser.