create_table_1.c

00001 /* $Id: create_table_1.c,v 1.15 2010/09/09 12:09:57 jim Exp $
00002  *
00003  * This file is part of the VIRCAM Pipeline
00004  * Copyright (C) 2005 Cambridge Astronomy Survey Unit
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jim $
00023  * $Date: 2010/09/09 12:09:57 $
00024  * $Revision: 1.15 $
00025  * $Name: vcam-1_3_2 $
00026  */
00027 
00028 #include <stdio.h>
00029 #include <math.h>
00030 #include "imcore.h"
00031 #include "util.h"
00032 #include "floatmath.h"
00033 
00034 /* Column numbers for each item to be stored */
00035 
00036 #define COL_NUMBER      1
00037 #define COL_FLUXISO     2
00038 #define COL_FLUXTOTAL   3
00039 #define COL_APFLUX1     4
00040 #define COL_X           5
00041 #define COL_Y           6
00042 #define COL_SIGMA       7
00043 #define COL_ELLIPT      8
00044 #define COL_PA          9
00045 #define COL_PEAKHEIGHT 10
00046 #define COL_AREAL1     11
00047 #define COL_AREAL2     12
00048 #define COL_AREAL3     13
00049 #define COL_AREAL4     14
00050 #define COL_AREAL5     15
00051 #define COL_AREAL6     16
00052 #define COL_AREAL7     17
00053 #define COL_AREAL8     18
00054 #define COL_APFLUX2    19
00055 #define COL_APFLUX3    20
00056 #define COL_APFLUX4    21
00057 #define COL_APFLUX5    22
00058 #define COL_RA         23
00059 #define COL_DEC        24
00060 #define COL_CLASS      25
00061 #define COL_STAT       26
00062 #define COL_APFLUX6    27
00063 #define COL_SKYLEV     28
00064 #define COL_SKYRMS     29
00065 
00066 /* Number of columns in the table */
00067 
00068 #define NCOLS 32
00069 
00070 /* Core radius extras */
00071 
00072 #define NRADS 6
00073 static float rmults[] = {0.5,1.0,CPL_MATH_SQRT2,2.0,2.0*CPL_MATH_SQRT2,4.0};
00074 static int nrcore = 1;
00075 static float apertures[NRADS];
00076 
00077 /* Column definitions */
00078 
00079 static const char *ttype[NCOLS]={"No.","Isophotal_flux","Total_flux","Core_flux",
00080                                  "X_coordinate","Y_coordinate","Gaussian_sigma",
00081                                  "Ellipticity","Position_angle","Peak_height",
00082                                  "Areal_1_profile","Areal_2_profile","Areal_3_profile",
00083                                  "Areal_4_profile","Areal_5_profile","Areal_6_profile",
00084                                  "Areal_7_profile","Areal_8_profile","Core1_flux",
00085                                  "Core2_flux","Core3_flux","Core4_flux",
00086                                  "RA","DEC","Classification","Statistic",
00087                                  "Core5_flux","Skylev",
00088                                  "Skyrms","Blank_30","Blank_31","Blank_32"};
00089 static const char *tunit[NCOLS]={" ","Counts","Counts","Counts","Pixels","Pixels",
00090                                  "Pixels"," ","Degrees","Counts","Pixels","Pixels",
00091                                  "Pixels","Pixels","Pixels","Pixels","Pixels","Pixels",
00092                                  "Counts","Counts","Counts","Counts",
00093                                  "Degrees","Degrees","Flag","N-sigma",
00094                                  "Counts","Counts","Counts","Blank_30",
00095                                  "Blank_31","Blank_32"};
00096 static cpl_type tform[NCOLS]={CPL_TYPE_INT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00097                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00098                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00099                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00100                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00101                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00102                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00103                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00104                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00105                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,CPL_TYPE_FLOAT,
00106                               CPL_TYPE_FLOAT,CPL_TYPE_FLOAT};
00107 
00108 static int areal_cols[NAREAL] = {COL_AREAL1,COL_AREAL2,COL_AREAL3,COL_AREAL4,
00109                                  COL_AREAL5,COL_AREAL6,COL_AREAL7,COL_AREAL8};
00112 /*---------------------------------------------------------------------------*/
00134 /*---------------------------------------------------------------------------*/
00135 
00136 extern void tabinit_1(void) {
00137 
00138     /* Call the generic routine to open a new output table */
00139 
00140     tabinit_gen(NCOLS,ttype,tunit,tform);
00141 
00142     /* Define RA and Dec columns */
00143 
00144     imcore_xcol = COL_X;
00145     imcore_ycol = COL_Y;
00146 }
00147 
00148 /*---------------------------------------------------------------------------*/
00173 /*---------------------------------------------------------------------------*/
00174 
00175 extern int do_seeing_1(ap_t *ap) {
00176     int retval,i;
00177     char *areal_colnames[NAREAL];
00178 
00179     /* Sort out the areal profile column names */
00180 
00181     for (i = 0; i < NAREAL; i++) 
00182         areal_colnames[i] = (char *)ttype[areal_cols[i]-1];
00183  
00184     /* Just call the generic seeing routine */
00185  
00186     retval = do_seeing_gen(ap,ttype[COL_ELLIPT-1],ttype[COL_PEAKHEIGHT-1],
00187                            areal_colnames);
00188 
00189     /* Get out of here */
00190  
00191     return(retval);
00192 }
00193         
00194 /*---------------------------------------------------------------------------*/
00221 /*---------------------------------------------------------------------------*/
00222 
00223 extern int process_results_1(ap_t *ap) {
00224     float momresults[8],ttotal,parmall[IMNUM][NPAR],ratio,cflux[NRADS*IMNUM];
00225     float sxx,syy,srr,sxy,ecc,temp,xx,theta,radeg,ell,iso_flux,total_flux;
00226     float apflux1,apflux2,apflux3,apflux4,apflux5,yy,sigma,peak,areal1,apflux6;
00227     float areal2,areal3,areal4,areal5,areal6,areal7,areal8;
00228     float skylev,skyrms,badpix[IMNUM];
00229     int iareal[NAREAL],nbit,i,k,nr,mbit,j;
00230     long nrows;
00231 
00232     /* Do a basic moments analysis and work out the areal profiles*/
00233 
00234     moments(ap,momresults);
00235     if (momresults[0] < 0)
00236         return(VIR_FATAL);
00237     areals(ap,iareal);
00238 
00239     /* See if this object makes the cut in terms of its size.  If not, then
00240        just return with good status */
00241 
00242     if (iareal[0] < ap->ipnop || momresults[3] < ap->xintmin)
00243         return(VIR_OK);
00244 
00245     /* Work out the total flux */
00246 
00247     extend(ap,momresults[3],momresults[1],momresults[2],
00248            momresults[4],momresults[5],momresults[6],
00249            (float)iareal[0],momresults[7],&ttotal);
00250     ratio = MAX(ttotal,momresults[3])/momresults[3];
00251 
00252     /* Try and deblend the images if it is requested and justified */
00253 
00254     if (iareal[0] >= ap->mulpix && ap->icrowd)
00255         overlp(ap,parmall,&nbit,momresults[1],momresults[2],
00256                momresults[3],iareal[0],momresults[7]);
00257     else
00258         nbit = 1;
00259     if (nbit == 1) {
00260         parmall[0][0] = momresults[3];
00261         parmall[0][1] = momresults[1];
00262         parmall[0][2] = momresults[2];
00263         parmall[0][3] = ap->thresh;
00264         for (i = 4; i < 8; i++) 
00265             parmall[0][i] = momresults[i];
00266         for (i = 0; i < NAREAL; i++)
00267             parmall[0][i+8] = (float)iareal[i];
00268     } else {
00269         mbit = 0;
00270         for (i = 0; i < nbit; i++) {
00271             if (parmall[i][1] > 1.0 && parmall[i][1] < ap->lsiz &&
00272                 parmall[i][2] > 1.0 && parmall[i][2] < ap->csiz) {
00273                 for (j = 0; j < NPAR; j++) 
00274                     parmall[mbit][j] = parmall[i][j];
00275                 mbit++;
00276             }
00277         }
00278         nbit = mbit;
00279     } 
00280 
00281     /* Create a list of apertures */
00282 
00283     for (i = 0; i < NRADS; i++) 
00284         apertures[i] = rmults[i]*(ap->rcore);
00285 
00286     /* Initialise badpix accumulator */
00287 
00288     for (i = 0; i < nbit; i++)
00289         badpix[i] = 0.0;
00290 
00291     /* Get the core fluxes in all apertures */
00292 
00293     phopt(ap,parmall,nbit,NRADS,apertures,cflux,badpix,nrcore);
00294 
00295     /* Massage the results and write them to the fits table */
00296 
00297     radeg = 180.0/CPL_MATH_PI;
00298     for (k = 0; k < nbit; k++) {
00299         sxx = parmall[k][4];
00300         sxy = parmall[k][5];
00301         syy = parmall[k][6];    
00302         if(sxy > 0.0)
00303           sxy = MAX(1.0e-4,MIN(sxy,sqrtf(sxx*syy)));
00304         else
00305           sxy = MIN(-1.0e-4,MAX(sxy,-sqrtf(sxx*syy)));
00306 
00307         srr = MAX(0.5,sxx+syy);
00308         ecc = sqrtf((syy-sxx)*(syy-sxx)+4.0*sxy*sxy)/srr;
00309         temp = MAX((1.0-ecc)/(1.0+ecc),0.0);
00310         ell = 1.0 - sqrtf(temp);
00311         ell = MIN(0.99,MAX(0.0,ell));
00312         xx = 0.5*(1.0+ecc)*srr-sxx;
00313         if(xx == 0.0)
00314             theta = 0.0;
00315         else
00316             theta = 90.0-radeg*atanf(sxy/xx);
00317 
00318         /* Create a list of values */
00319 
00320         nrows = cpl_table_get_nrow(tab);
00321         nobjects++;
00322         if (nobjects > nrows) 
00323             (void)cpl_table_set_size(tab,nrows+INITROWS);
00324         nr = nobjects - 1;
00325         iso_flux = parmall[k][0];
00326         total_flux = ratio*parmall[k][0];
00327         apflux1 = cflux[k*NRADS + 0];
00328         apflux2 = cflux[k*NRADS + 1];
00329         apflux3 = cflux[k*NRADS + 2];
00330         apflux4 = cflux[k*NRADS + 3];
00331         apflux5 = cflux[k*NRADS + 4];
00332         apflux6 = cflux[k*NRADS + 5];
00333         xx = parmall[k][1];
00334         yy = parmall[k][2];
00335         sigma = sqrt(srr);
00336         peak = parmall[k][7];
00337         areal1 = parmall[k][8];
00338         areal2 = parmall[k][9];
00339         areal3 = parmall[k][10];
00340         areal4 = parmall[k][11];
00341         areal5 = parmall[k][12];
00342         areal6 = parmall[k][13];
00343         areal7 = parmall[k][14];
00344         if (nbit > 1 && k == 0)
00345             areal8 = 0.0;
00346         else
00347             areal8 = parmall[k][15];
00348         imcore_backest(ap,xx,yy,&skylev,&skyrms);
00349 
00350         /* Store away the results for this object */
00351     
00352         cpl_table_set_int(tab,ttype[COL_NUMBER-1],nr,nobjects);
00353         cpl_table_set_float(tab,ttype[COL_FLUXISO-1],nr,iso_flux);
00354         cpl_table_set_float(tab,ttype[COL_FLUXTOTAL-1],nr,total_flux);
00355         cpl_table_set_float(tab,ttype[COL_APFLUX1-1],nr,apflux2);
00356         cpl_table_set_float(tab,ttype[COL_X-1],nr,xx);
00357         cpl_table_set_float(tab,ttype[COL_Y-1],nr,yy);
00358         cpl_table_set_float(tab,ttype[COL_SIGMA-1],nr,sigma);
00359         cpl_table_set_float(tab,ttype[COL_ELLIPT-1],nr,ell);
00360         cpl_table_set_float(tab,ttype[COL_PA-1],nr,theta);
00361         cpl_table_set_float(tab,ttype[COL_PEAKHEIGHT-1],nr,peak);
00362         cpl_table_set_float(tab,ttype[COL_AREAL1-1],nr,areal1);
00363         cpl_table_set_float(tab,ttype[COL_AREAL2-1],nr,areal2);
00364         cpl_table_set_float(tab,ttype[COL_AREAL3-1],nr,areal3);
00365         cpl_table_set_float(tab,ttype[COL_AREAL4-1],nr,areal4);
00366         cpl_table_set_float(tab,ttype[COL_AREAL5-1],nr,areal5);
00367         cpl_table_set_float(tab,ttype[COL_AREAL6-1],nr,areal6);
00368         cpl_table_set_float(tab,ttype[COL_AREAL7-1],nr,areal7);
00369         cpl_table_set_float(tab,ttype[COL_AREAL8-1],nr,areal8);
00370         cpl_table_set_float(tab,ttype[COL_APFLUX2-1],nr,apflux1);
00371         cpl_table_set_float(tab,ttype[COL_APFLUX3-1],nr,apflux3);
00372         cpl_table_set_float(tab,ttype[COL_APFLUX4-1],nr,apflux4);
00373         cpl_table_set_float(tab,ttype[COL_APFLUX5-1],nr,apflux5);
00374         cpl_table_set_float(tab,ttype[COL_APFLUX6-1],nr,apflux6);
00375         cpl_table_set_float(tab,ttype[COL_SKYLEV-1],nr,skylev);
00376         cpl_table_set_float(tab,ttype[COL_SKYRMS-1],nr,skyrms);
00377     }
00378 
00379     /* Get outta here */
00380 
00381     return(VIR_OK);
00382 }
00383 
00386 /*
00387 
00388 $Log: create_table_1.c,v $
00389 Revision 1.15  2010/09/09 12:09:57  jim
00390 Added docs
00391 
00392 Revision 1.14  2009/09/09 09:40:12  jim
00393 Using CPL macros for various constants
00394 
00395 Revision 1.13  2009/03/19 10:05:46  jim
00396 Fixed RA,Dec column units
00397 
00398 Revision 1.12  2009/02/20 10:49:58  jim
00399 Removed superfluous declarations
00400 
00401 Revision 1.11  2008/10/13 08:09:31  jim
00402 Removed redundant code and fixed pixel flagging scheme
00403 
00404 Revision 1.10  2007/05/03 11:15:34  jim
00405 Fixed little problem with table wcs
00406 
00407 Revision 1.9  2007/05/02 09:11:35  jim
00408 Modified to allow for inclusion of table WCS keywords into FITS header
00409 
00410 Revision 1.8  2007/03/01 12:38:26  jim
00411 Small modifications after a bit of code checking
00412 
00413 Revision 1.7  2006/11/27 11:58:09  jim
00414 Modified so that deblended images that fall off the edge of the image are
00415 rejected
00416 
00417 Revision 1.6  2006/08/11 13:00:53  jim
00418 Modified to have 8th areal profile set to 0 at the start of a blend
00419 
00420 Revision 1.5  2006/05/30 12:14:08  jim
00421 Fixed indexing bug in table that was causing a memory overwrite
00422 
00423 Revision 1.4  2006/05/26 15:00:56  jim
00424 Fixed blank columns so they have unique names
00425 
00426 Revision 1.3  2005/09/22 08:40:41  jim
00427 Fixed some bugs in imcore and added classification Removed some unnecessary
00428 files
00429 
00430 Revision 1.2  2005/09/20 15:07:46  jim
00431 Fixed a few bugs and added a few things
00432 
00433 Revision 1.1  2005/09/13 13:25:27  jim
00434 Initial entry after modifications to make cpl compliant
00435 
00436 
00437 */

Generated on 5 Mar 2013 for VIRCAM Pipeline by  doxygen 1.6.1