VIRCAM Pipeline  2.3.12
create_table_1.c
1 /* $Id: create_table_1.c,v 1.4 2015/09/15 07:38:12 jim Exp $
2  *
3  * This file is part of the CASU Pipeline utilities
4  * Copyright (C) 2015 European Southern Observatory
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: 2015/09/15 07:38:12 $
24  * $Revision: 1.4 $
25  * $Name: $
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 imcore_tabinit_1(int *imcore_xcol, int *imcore_ycol,
137  cpl_table **tab) {
138 
139  /* Call the generic routine to open a new output table */
140 
141  imcore_tabinit_gen(NCOLS,ttype,tunit,tform,tab);
142 
143  /* Define RA and Dec columns */
144 
145  *imcore_xcol = COL_X;
146  *imcore_ycol = COL_Y;
147 }
148 
149 /*---------------------------------------------------------------------------*/
174 /*---------------------------------------------------------------------------*/
175 
176 extern int imcore_do_seeing_1(ap_t *ap, int nobjects, cpl_table *tab) {
177  int retval,i;
178  char *areal_colnames[NAREAL];
179 
180  /* Sort out the areal profile column names */
181 
182  for (i = 0; i < NAREAL; i++)
183  areal_colnames[i] = (char *)ttype[areal_cols[i]-1];
184 
185  /* Just call the generic seeing routine */
186 
187  retval = imcore_do_seeing_gen(ap,ttype[COL_ELLIPT-1],ttype[COL_PEAKHEIGHT-1],
188  areal_colnames,nobjects,tab);
189 
190  /* Get out of here */
191 
192  return(retval);
193 }
194 
195 /*---------------------------------------------------------------------------*/
222 /*---------------------------------------------------------------------------*/
223 
224 extern int imcore_process_results_1(ap_t *ap, int *nobjects, cpl_table *tab) {
225  float momresults[8],ttotal,parmall[IMNUM][NPAR],ratio,cflux[NRADS*IMNUM];
226  float sxx,syy,srr,sxy,ecc,temp,xx,theta,radeg,ell,iso_flux,total_flux;
227  float apflux1,apflux2,apflux3,apflux4,apflux5,yy,sigma,peak,areal1,apflux6;
228  float areal2,areal3,areal4,areal5,areal6,areal7,areal8;
229  float skylev,skyrms,badpix[IMNUM],avconf[IMNUM];
230  int iareal[NAREAL],nbit,i,k,nr,mbit,j;
231  long nrows;
232 
233  /* Do a basic moments analysis and work out the areal profiles*/
234 
235  imcore_moments(ap,momresults);
236  if (momresults[0] < 0)
237  return(CASU_FATAL);
238  imcore_areals(ap,iareal);
239 
240  /* See if this object makes the cut in terms of its size. If not, then
241  just return with good status */
242 
243  if (iareal[0] < ap->ipnop || momresults[3] < ap->xintmin)
244  return(CASU_OK);
245 
246  /* Work out the total flux */
247 
248  imcore_extend(ap,momresults[3],momresults[1],momresults[2],
249  momresults[4],momresults[5],momresults[6],
250  (float)iareal[0],momresults[7],&ttotal);
251  ratio = MAX(ttotal,momresults[3])/momresults[3];
252 
253  /* Try and deblend the images if it is requested and justified */
254 
255  if (iareal[0] >= ap->mulpix && ap->icrowd)
256  imcore_overlp(ap,parmall,&nbit,momresults[1],momresults[2],
257  momresults[3],iareal[0],momresults[7]);
258  else
259  nbit = 1;
260  if (nbit == 1) {
261  parmall[0][0] = momresults[3];
262  parmall[0][1] = momresults[1];
263  parmall[0][2] = momresults[2];
264  parmall[0][3] = ap->thresh;
265  for (i = 4; i < 8; i++)
266  parmall[0][i] = momresults[i];
267  for (i = 0; i < NAREAL; i++)
268  parmall[0][i+8] = (float)iareal[i];
269  } else {
270  mbit = 0;
271  for (i = 0; i < nbit; i++) {
272  if (parmall[i][1] > 1.0 && parmall[i][1] < ap->lsiz &&
273  parmall[i][2] > 1.0 && parmall[i][2] < ap->csiz) {
274  for (j = 0; j < NPAR; j++)
275  parmall[mbit][j] = parmall[i][j];
276  mbit++;
277  }
278  }
279  nbit = mbit;
280  if (nbit == 0)
281  return(CASU_OK);
282  }
283 
284  /* Create a list of apertures */
285 
286  for (i = 0; i < NRADS; i++)
287  apertures[i] = rmults[i]*(ap->rcore);
288 
289  /* Initialise badpix accumulator */
290 
291  for (i = 0; i < nbit; i++) {
292  badpix[i] = 0.0;
293  avconf[i] = 0.0;
294  }
295 
296  /* Get the core fluxes in all apertures */
297 
298  imcore_phopt(ap,parmall,nbit,NRADS,apertures,cflux,badpix,nrcore,avconf);
299 
300  /* Massage the results and write them to the fits table */
301 
302  radeg = 180.0/CPL_MATH_PI;
303  for (k = 0; k < nbit; k++) {
304  sxx = parmall[k][4];
305  sxy = parmall[k][5];
306  syy = parmall[k][6];
307  if(sxy > 0.0)
308  sxy = MAX(1.0e-4,MIN(sxy,sqrtf(sxx*syy)));
309  else
310  sxy = MIN(-1.0e-4,MAX(sxy,-sqrtf(sxx*syy)));
311 
312  srr = MAX(0.5,sxx+syy);
313  ecc = sqrtf((syy-sxx)*(syy-sxx)+4.0*sxy*sxy)/srr;
314  temp = MAX((1.0-ecc)/(1.0+ecc),0.0);
315  ell = 1.0 - sqrtf(temp);
316  ell = MIN(0.99,MAX(0.0,ell));
317  xx = 0.5*(1.0+ecc)*srr-sxx;
318  if(xx == 0.0)
319  theta = 0.0;
320  else
321  theta = 90.0-radeg*atanf(sxy/xx);
322 
323  /* Create a list of values */
324 
325  nrows = cpl_table_get_nrow(tab);
326  (*nobjects)++;
327  if (*nobjects > nrows)
328  (void)cpl_table_set_size(tab,nrows+INITROWS);
329  nr = *nobjects - 1;
330  iso_flux = parmall[k][0];
331  total_flux = ratio*parmall[k][0];
332  apflux1 = cflux[k*NRADS + 0];
333  apflux2 = cflux[k*NRADS + 1];
334  apflux3 = cflux[k*NRADS + 2];
335  apflux4 = cflux[k*NRADS + 3];
336  apflux5 = cflux[k*NRADS + 4];
337  apflux6 = cflux[k*NRADS + 5];
338  xx = parmall[k][1];
339  yy = parmall[k][2];
340  sigma = sqrt(srr);
341  peak = parmall[k][7];
342  areal1 = parmall[k][8];
343  areal2 = parmall[k][9];
344  areal3 = parmall[k][10];
345  areal4 = parmall[k][11];
346  areal5 = parmall[k][12];
347  areal6 = parmall[k][13];
348  areal7 = parmall[k][14];
349  if (nbit > 1 && k == 0)
350  areal8 = 0.0;
351  else
352  areal8 = parmall[k][15];
353  imcore_backest(ap,xx,yy,&skylev,&skyrms);
354 
355  /* Store away the results for this object */
356 
357  cpl_table_set_int(tab,ttype[COL_NUMBER-1],nr,*nobjects);
358  cpl_table_set_float(tab,ttype[COL_FLUXISO-1],nr,iso_flux);
359  cpl_table_set_float(tab,ttype[COL_FLUXTOTAL-1],nr,total_flux);
360  cpl_table_set_float(tab,ttype[COL_APFLUX1-1],nr,apflux2);
361  cpl_table_set_float(tab,ttype[COL_X-1],nr,xx);
362  cpl_table_set_float(tab,ttype[COL_Y-1],nr,yy);
363  cpl_table_set_float(tab,ttype[COL_SIGMA-1],nr,sigma);
364  cpl_table_set_float(tab,ttype[COL_ELLIPT-1],nr,ell);
365  cpl_table_set_float(tab,ttype[COL_PA-1],nr,theta);
366  cpl_table_set_float(tab,ttype[COL_PEAKHEIGHT-1],nr,peak);
367  cpl_table_set_float(tab,ttype[COL_AREAL1-1],nr,areal1);
368  cpl_table_set_float(tab,ttype[COL_AREAL2-1],nr,areal2);
369  cpl_table_set_float(tab,ttype[COL_AREAL3-1],nr,areal3);
370  cpl_table_set_float(tab,ttype[COL_AREAL4-1],nr,areal4);
371  cpl_table_set_float(tab,ttype[COL_AREAL5-1],nr,areal5);
372  cpl_table_set_float(tab,ttype[COL_AREAL6-1],nr,areal6);
373  cpl_table_set_float(tab,ttype[COL_AREAL7-1],nr,areal7);
374  cpl_table_set_float(tab,ttype[COL_AREAL8-1],nr,areal8);
375  cpl_table_set_float(tab,ttype[COL_APFLUX2-1],nr,apflux1);
376  cpl_table_set_float(tab,ttype[COL_APFLUX3-1],nr,apflux3);
377  cpl_table_set_float(tab,ttype[COL_APFLUX4-1],nr,apflux4);
378  cpl_table_set_float(tab,ttype[COL_APFLUX5-1],nr,apflux5);
379  cpl_table_set_float(tab,ttype[COL_APFLUX6-1],nr,apflux6);
380  cpl_table_set_float(tab,ttype[COL_SKYLEV-1],nr,skylev);
381  cpl_table_set_float(tab,ttype[COL_SKYRMS-1],nr,skyrms);
382  }
383 
384  /* Get outta here */
385 
386  return(CASU_OK);
387 }
388 
391 /*
392 
393 $Log: create_table_1.c,v $
394 Revision 1.4 2015/09/15 07:38:12 jim
395 Trap for when nbit = 0 in phopt
396 
397 Revision 1.3 2015/08/12 11:16:55 jim
398 Modified procedure names to protect namespace
399 
400 Revision 1.2 2015/08/07 13:06:54 jim
401 Fixed copyright to ESO
402 
403 Revision 1.1.1.1 2015/06/12 10:44:32 jim
404 Initial import
405 
406 Revision 1.3 2015/01/09 11:42:36 jim
407 Fixed routines to remove globals
408 
409 Revision 1.2 2014/04/09 09:09:51 jim
410 Detabbed
411 
412 Revision 1.1.1.1 2013/08/27 12:07:48 jim
413 Imported
414 
415 
416 */
void imcore_areals(ap_t *ap, int iareal[NAREAL])
Work out the areal profiles for an object.
Definition: areals.c:67
int imcore_do_seeing_1(ap_t *ap, int nobjects, cpl_table *tab)
Do seeing estimate for type 1 catalogue.
void imcore_backest(ap_t *ap, float x, float y, float *skylev, float *skyrms)
Work out estimated sky for a pixel position.
int imcore_process_results_1(ap_t *ap, int *nobjects, cpl_table *tab)
Process results for type 1 catalogue.
void imcore_moments(ap_t *ap, float results[])
Do moments analysis on an object.
Definition: moments.c:65
int imcore_do_seeing_gen(ap_t *ap, const char *col_ellipt, const char *col_pkht, char *col_areals[NAREAL], int nobjects, cpl_table *tab)
Do seeing estimate (generic)
Definition: create_table.c:335
void imcore_tabinit_1(int *imcore_xcol, int *imcore_ycol, cpl_table **tab)
Initialise type 1 catalogue.
void imcore_tabinit_gen(int ncols, const char *ttype[], const char *tunit[], cpl_type tform[], cpl_table **tab)
Initialise tables (generic)
Definition: create_table.c:281
void imcore_phopt(ap_t *ap, float parm[IMNUM][NPAR], int nbit, int naper, float apertures[], float cflux[], float badpix[], int nrcore, float avconf[])
Do multiple profile fitting.
Definition: imcore_phopt.c:91
void imcore_overlp(ap_t *ap, float parm[IMNUM][NPAR], int *nbit, float xbar, float ybar, float total, int npix, float tmax)
Deblend overlapping images.
Definition: imcore_overlp.c:99
int imcore_extend(ap_t *ap, float xniso, float xbar, float ybar, float sxx, float sxy, float syy, float areal0, float tmax, float *ttotal)
Do aperture integration.
Definition: imcore_extend.c:89