VIRCAM Pipeline  1.3.3
create_table_1.c
1 /* $Id: create_table_1.c,v 1.15 2010-09-09 12:09:57 jim Exp $
2  *
3  * This file is part of the VIRCAM Pipeline
4  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2010-09-09 12:09:57 $
24  * $Revision: 1.15 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #include <stdio.h>
29 #include <math.h>
30 #include "imcore.h"
31 #include "util.h"
32 #include "floatmath.h"
33 
34 /* Column numbers for each item to be stored */
35 
36 #define COL_NUMBER 1
37 #define COL_FLUXISO 2
38 #define COL_FLUXTOTAL 3
39 #define COL_APFLUX1 4
40 #define COL_X 5
41 #define COL_Y 6
42 #define COL_SIGMA 7
43 #define COL_ELLIPT 8
44 #define COL_PA 9
45 #define COL_PEAKHEIGHT 10
46 #define COL_AREAL1 11
47 #define COL_AREAL2 12
48 #define COL_AREAL3 13
49 #define COL_AREAL4 14
50 #define COL_AREAL5 15
51 #define COL_AREAL6 16
52 #define COL_AREAL7 17
53 #define COL_AREAL8 18
54 #define COL_APFLUX2 19
55 #define COL_APFLUX3 20
56 #define COL_APFLUX4 21
57 #define COL_APFLUX5 22
58 #define COL_RA 23
59 #define COL_DEC 24
60 #define COL_CLASS 25
61 #define COL_STAT 26
62 #define COL_APFLUX6 27
63 #define COL_SKYLEV 28
64 #define COL_SKYRMS 29
65 
66 /* Number of columns in the table */
67 
68 #define NCOLS 32
69 
70 /* Core radius extras */
71 
72 #define NRADS 6
73 static float rmults[] = {0.5,1.0,CPL_MATH_SQRT2,2.0,2.0*CPL_MATH_SQRT2,4.0};
74 static int nrcore = 1;
75 static float apertures[NRADS];
76 
77 /* Column definitions */
78 
79 static const char *ttype[NCOLS]={"No.","Isophotal_flux","Total_flux","Core_flux",
80  "X_coordinate","Y_coordinate","Gaussian_sigma",
81  "Ellipticity","Position_angle","Peak_height",
82  "Areal_1_profile","Areal_2_profile","Areal_3_profile",
83  "Areal_4_profile","Areal_5_profile","Areal_6_profile",
84  "Areal_7_profile","Areal_8_profile","Core1_flux",
85  "Core2_flux","Core3_flux","Core4_flux",
86  "RA","DEC","Classification","Statistic",
87  "Core5_flux","Skylev",
88  "Skyrms","Blank_30","Blank_31","Blank_32"};
89 static const char *tunit[NCOLS]={" ","Counts","Counts","Counts","Pixels","Pixels",
90  "Pixels"," ","Degrees","Counts","Pixels","Pixels",
91  "Pixels","Pixels","Pixels","Pixels","Pixels","Pixels",
92  "Counts","Counts","Counts","Counts",
93  "Degrees","Degrees","Flag","N-sigma",
94  "Counts","Counts","Counts","Blank_30",
95  "Blank_31","Blank_32"};
96 static cpl_type tform[NCOLS]={CPL_TYPE_INT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
97  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
98  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
99  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
100  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
101  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
102  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
103  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
104  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
105  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
106  CPL_TYPE_FLOAT,CPL_TYPE_FLOAT};
107 
108 static int areal_cols[NAREAL] = {COL_AREAL1,COL_AREAL2,COL_AREAL3,COL_AREAL4,
109  COL_AREAL5,COL_AREAL6,COL_AREAL7,COL_AREAL8};
112 /*---------------------------------------------------------------------------*/
134 /*---------------------------------------------------------------------------*/
135 
136 extern void tabinit_1(void) {
137 
138  /* Call the generic routine to open a new output table */
139 
140  tabinit_gen(NCOLS,ttype,tunit,tform);
141 
142  /* Define RA and Dec columns */
143 
144  imcore_xcol = COL_X;
145  imcore_ycol = COL_Y;
146 }
147 
148 /*---------------------------------------------------------------------------*/
173 /*---------------------------------------------------------------------------*/
174 
175 extern int do_seeing_1(ap_t *ap) {
176  int retval,i;
177  char *areal_colnames[NAREAL];
178 
179  /* Sort out the areal profile column names */
180 
181  for (i = 0; i < NAREAL; i++)
182  areal_colnames[i] = (char *)ttype[areal_cols[i]-1];
183 
184  /* Just call the generic seeing routine */
185 
186  retval = do_seeing_gen(ap,ttype[COL_ELLIPT-1],ttype[COL_PEAKHEIGHT-1],
187  areal_colnames);
188 
189  /* Get out of here */
190 
191  return(retval);
192 }
193 
194 /*---------------------------------------------------------------------------*/
221 /*---------------------------------------------------------------------------*/
222 
223 extern int process_results_1(ap_t *ap) {
224  float momresults[8],ttotal,parmall[IMNUM][NPAR],ratio,cflux[NRADS*IMNUM];
225  float sxx,syy,srr,sxy,ecc,temp,xx,theta,radeg,ell,iso_flux,total_flux;
226  float apflux1,apflux2,apflux3,apflux4,apflux5,yy,sigma,peak,areal1,apflux6;
227  float areal2,areal3,areal4,areal5,areal6,areal7,areal8;
228  float skylev,skyrms,badpix[IMNUM];
229  int iareal[NAREAL],nbit,i,k,nr,mbit,j;
230  long nrows;
231 
232  /* Do a basic moments analysis and work out the areal profiles*/
233 
234  moments(ap,momresults);
235  if (momresults[0] < 0)
236  return(VIR_FATAL);
237  areals(ap,iareal);
238 
239  /* See if this object makes the cut in terms of its size. If not, then
240  just return with good status */
241 
242  if (iareal[0] < ap->ipnop || momresults[3] < ap->xintmin)
243  return(VIR_OK);
244 
245  /* Work out the total flux */
246 
247  extend(ap,momresults[3],momresults[1],momresults[2],
248  momresults[4],momresults[5],momresults[6],
249  (float)iareal[0],momresults[7],&ttotal);
250  ratio = MAX(ttotal,momresults[3])/momresults[3];
251 
252  /* Try and deblend the images if it is requested and justified */
253 
254  if (iareal[0] >= ap->mulpix && ap->icrowd)
255  overlp(ap,parmall,&nbit,momresults[1],momresults[2],
256  momresults[3],iareal[0],momresults[7]);
257  else
258  nbit = 1;
259  if (nbit == 1) {
260  parmall[0][0] = momresults[3];
261  parmall[0][1] = momresults[1];
262  parmall[0][2] = momresults[2];
263  parmall[0][3] = ap->thresh;
264  for (i = 4; i < 8; i++)
265  parmall[0][i] = momresults[i];
266  for (i = 0; i < NAREAL; i++)
267  parmall[0][i+8] = (float)iareal[i];
268  } else {
269  mbit = 0;
270  for (i = 0; i < nbit; i++) {
271  if (parmall[i][1] > 1.0 && parmall[i][1] < ap->lsiz &&
272  parmall[i][2] > 1.0 && parmall[i][2] < ap->csiz) {
273  for (j = 0; j < NPAR; j++)
274  parmall[mbit][j] = parmall[i][j];
275  mbit++;
276  }
277  }
278  nbit = mbit;
279  }
280 
281  /* Create a list of apertures */
282 
283  for (i = 0; i < NRADS; i++)
284  apertures[i] = rmults[i]*(ap->rcore);
285 
286  /* Initialise badpix accumulator */
287 
288  for (i = 0; i < nbit; i++)
289  badpix[i] = 0.0;
290 
291  /* Get the core fluxes in all apertures */
292 
293  phopt(ap,parmall,nbit,NRADS,apertures,cflux,badpix,nrcore);
294 
295  /* Massage the results and write them to the fits table */
296 
297  radeg = 180.0/CPL_MATH_PI;
298  for (k = 0; k < nbit; k++) {
299  sxx = parmall[k][4];
300  sxy = parmall[k][5];
301  syy = parmall[k][6];
302  if(sxy > 0.0)
303  sxy = MAX(1.0e-4,MIN(sxy,sqrtf(sxx*syy)));
304  else
305  sxy = MIN(-1.0e-4,MAX(sxy,-sqrtf(sxx*syy)));
306 
307  srr = MAX(0.5,sxx+syy);
308  ecc = sqrtf((syy-sxx)*(syy-sxx)+4.0*sxy*sxy)/srr;
309  temp = MAX((1.0-ecc)/(1.0+ecc),0.0);
310  ell = 1.0 - sqrtf(temp);
311  ell = MIN(0.99,MAX(0.0,ell));
312  xx = 0.5*(1.0+ecc)*srr-sxx;
313  if(xx == 0.0)
314  theta = 0.0;
315  else
316  theta = 90.0-radeg*atanf(sxy/xx);
317 
318  /* Create a list of values */
319 
320  nrows = cpl_table_get_nrow(tab);
321  nobjects++;
322  if (nobjects > nrows)
323  (void)cpl_table_set_size(tab,nrows+INITROWS);
324  nr = nobjects - 1;
325  iso_flux = parmall[k][0];
326  total_flux = ratio*parmall[k][0];
327  apflux1 = cflux[k*NRADS + 0];
328  apflux2 = cflux[k*NRADS + 1];
329  apflux3 = cflux[k*NRADS + 2];
330  apflux4 = cflux[k*NRADS + 3];
331  apflux5 = cflux[k*NRADS + 4];
332  apflux6 = cflux[k*NRADS + 5];
333  xx = parmall[k][1];
334  yy = parmall[k][2];
335  sigma = sqrt(srr);
336  peak = parmall[k][7];
337  areal1 = parmall[k][8];
338  areal2 = parmall[k][9];
339  areal3 = parmall[k][10];
340  areal4 = parmall[k][11];
341  areal5 = parmall[k][12];
342  areal6 = parmall[k][13];
343  areal7 = parmall[k][14];
344  if (nbit > 1 && k == 0)
345  areal8 = 0.0;
346  else
347  areal8 = parmall[k][15];
348  imcore_backest(ap,xx,yy,&skylev,&skyrms);
349 
350  /* Store away the results for this object */
351 
352  cpl_table_set_int(tab,ttype[COL_NUMBER-1],nr,nobjects);
353  cpl_table_set_float(tab,ttype[COL_FLUXISO-1],nr,iso_flux);
354  cpl_table_set_float(tab,ttype[COL_FLUXTOTAL-1],nr,total_flux);
355  cpl_table_set_float(tab,ttype[COL_APFLUX1-1],nr,apflux2);
356  cpl_table_set_float(tab,ttype[COL_X-1],nr,xx);
357  cpl_table_set_float(tab,ttype[COL_Y-1],nr,yy);
358  cpl_table_set_float(tab,ttype[COL_SIGMA-1],nr,sigma);
359  cpl_table_set_float(tab,ttype[COL_ELLIPT-1],nr,ell);
360  cpl_table_set_float(tab,ttype[COL_PA-1],nr,theta);
361  cpl_table_set_float(tab,ttype[COL_PEAKHEIGHT-1],nr,peak);
362  cpl_table_set_float(tab,ttype[COL_AREAL1-1],nr,areal1);
363  cpl_table_set_float(tab,ttype[COL_AREAL2-1],nr,areal2);
364  cpl_table_set_float(tab,ttype[COL_AREAL3-1],nr,areal3);
365  cpl_table_set_float(tab,ttype[COL_AREAL4-1],nr,areal4);
366  cpl_table_set_float(tab,ttype[COL_AREAL5-1],nr,areal5);
367  cpl_table_set_float(tab,ttype[COL_AREAL6-1],nr,areal6);
368  cpl_table_set_float(tab,ttype[COL_AREAL7-1],nr,areal7);
369  cpl_table_set_float(tab,ttype[COL_AREAL8-1],nr,areal8);
370  cpl_table_set_float(tab,ttype[COL_APFLUX2-1],nr,apflux1);
371  cpl_table_set_float(tab,ttype[COL_APFLUX3-1],nr,apflux3);
372  cpl_table_set_float(tab,ttype[COL_APFLUX4-1],nr,apflux4);
373  cpl_table_set_float(tab,ttype[COL_APFLUX5-1],nr,apflux5);
374  cpl_table_set_float(tab,ttype[COL_APFLUX6-1],nr,apflux6);
375  cpl_table_set_float(tab,ttype[COL_SKYLEV-1],nr,skylev);
376  cpl_table_set_float(tab,ttype[COL_SKYRMS-1],nr,skyrms);
377  }
378 
379  /* Get outta here */
380 
381  return(VIR_OK);
382 }
383 
386 /*
387 
388 $Log: not supported by cvs2svn $
389 Revision 1.14 2009/09/09 09:40:12 jim
390 Using CPL macros for various constants
391 
392 Revision 1.13 2009/03/19 10:05:46 jim
393 Fixed RA,Dec column units
394 
395 Revision 1.12 2009/02/20 10:49:58 jim
396 Removed superfluous declarations
397 
398 Revision 1.11 2008/10/13 08:09:31 jim
399 Removed redundant code and fixed pixel flagging scheme
400 
401 Revision 1.10 2007/05/03 11:15:34 jim
402 Fixed little problem with table wcs
403 
404 Revision 1.9 2007/05/02 09:11:35 jim
405 Modified to allow for inclusion of table WCS keywords into FITS header
406 
407 Revision 1.8 2007/03/01 12:38:26 jim
408 Small modifications after a bit of code checking
409 
410 Revision 1.7 2006/11/27 11:58:09 jim
411 Modified so that deblended images that fall off the edge of the image are
412 rejected
413 
414 Revision 1.6 2006/08/11 13:00:53 jim
415 Modified to have 8th areal profile set to 0 at the start of a blend
416 
417 Revision 1.5 2006/05/30 12:14:08 jim
418 Fixed indexing bug in table that was causing a memory overwrite
419 
420 Revision 1.4 2006/05/26 15:00:56 jim
421 Fixed blank columns so they have unique names
422 
423 Revision 1.3 2005/09/22 08:40:41 jim
424 Fixed some bugs in imcore and added classification Removed some unnecessary
425 files
426 
427 Revision 1.2 2005/09/20 15:07:46 jim
428 Fixed a few bugs and added a few things
429 
430 Revision 1.1 2005/09/13 13:25:27 jim
431 Initial entry after modifications to make cpl compliant
432 
433 
434 */