vircam_imcore.c

00001 /* $Id: vircam_imcore.c,v 1.28 2012/01/15 17:40:09 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: 2012/01/15 17:40:09 $
00024  * $Revision: 1.28 $
00025  * $Name: vcam-1_3_0 $
00026  */
00027 
00028 /* Includes */
00029 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include <math.h>
00035 
00036 #include "vircam_mods.h"
00037 #include "vircam_utils.h"
00038 #include "vircam_wcsutils.h"
00039 #include "vircam_fits.h"
00040 #include "catalogue/imcore.h"
00041 #include "vircam_pfits.h"
00042 
00043 #define NOMPIXSIZE 0.34
00044 
00045 static double pixsize (cpl_propertylist *plist);
00046 
00049 /*---------------------------------------------------------------------------*/
00148 /*---------------------------------------------------------------------------*/
00149 
00150 extern int vircam_imcore(vir_fits *infile, vir_fits *conf, int ipix,
00151                          float threshold, int icrowd, float rcore, int nbsize,
00152                          int cattyp, float filtfwhm, vir_tfits **outtab, 
00153                          int *status) {
00154     int retval;
00155     const char *fctid = "vircam_imcore";
00156     cpl_propertylist *plist,*elist;
00157     vir_fits *in,*c;
00158     double pixarcsec,*cd,theta_east,theta_north,theta_north_2;
00159     float fwhm,fitpa;
00160     cpl_wcs *wcs;
00161 
00162     /* Inherited status */
00163 
00164     *outtab = NULL;
00165     if (*status != VIR_OK)
00166         return(*status);
00167 
00168     /* Copy the input */
00169 
00170     cattype = cattyp;
00171     in = vircam_fits_duplicate(infile);
00172     c = vircam_fits_duplicate(conf);
00173 
00174     /* Call the main processing routine and get the catalogue */
00175 
00176     retval = imcore_conf(in,c,ipix,threshold,icrowd,rcore,nbsize,cattype,
00177                          filtfwhm,outtab);
00178     vircam_fits_delete(in);
00179     vircam_fits_delete(c);
00180     if (retval != VIR_OK)
00181         FATAL_ERROR;
00182     if ((int)cpl_table_get_nrow(vircam_tfits_get_table(*outtab)) == 0) {
00183         cpl_msg_warning(fctid,"No objects found in %s",
00184                         vircam_fits_get_fullname(infile));
00185         freetfits(*outtab);
00186         WARN_RETURN
00187     }
00188 
00189     /* Get the property list from the input frame */
00190 
00191     plist = vircam_fits_get_phu(infile);
00192     if (plist == NULL) {
00193         cpl_msg_error(fctid,"Unable to open propertylist %s",
00194                       vircam_fits_get_filename(infile));
00195         FATAL_ERROR
00196     }
00197 
00198     /* Do the classification */
00199 
00200     retval = classify(*outtab,plist,16.0,cattype);
00201     if (retval != VIR_OK) 
00202         WARN_RETURN
00203 
00204     /* Convert the QC FWHM value to arcsec */
00205  
00206     elist = vircam_fits_get_ehu(infile);
00207     pixarcsec = pixsize(elist);
00208     fwhm = cpl_propertylist_get_float(vircam_tfits_get_ehu(*outtab),
00209                                          "ESO QC IMAGE_SIZE");
00210     fwhm *= pixarcsec;
00211     cpl_propertylist_update_float(vircam_tfits_get_ehu(*outtab),
00212                                   "ESO QC IMAGE_SIZE",fwhm);
00213     cpl_propertylist_set_comment(vircam_tfits_get_ehu(*outtab),
00214                                  "ESO QC IMAGE_SIZE",
00215                                  "[arcsec] Average FWHM of stellar objects");
00216 
00217     /* Now convert the median position angle estimate to degrees from 
00218        North in an Easterly direction...*/
00219 
00220     fitpa = cpl_propertylist_get_float(vircam_tfits_get_ehu(*outtab),
00221                                        "ESO QC POSANG");
00222     if (fitpa != 0.0) {
00223         wcs = cpl_wcs_new_from_propertylist(elist);
00224         cd = cpl_matrix_get_data((cpl_matrix *)cpl_wcs_get_cd(wcs));
00225         theta_east = DEGRAD*atan2(cd[1],cd[0]);
00226         theta_north = DEGRAD*atan2(cd[3],cd[2]);
00227         theta_north_2 = (theta_north < 0.0 ? theta_north + 360.0 : theta_north);
00228         if (abs(theta_north-theta_east-90.0) < 5.0 ||
00229             abs(theta_north-theta_east+270.0) < 5.0) {
00230             fitpa = theta_north_2 - fitpa;
00231         } else {
00232             fitpa = 360.0 - theta_north_2 + fitpa;
00233         }
00234         if (fitpa < 0.0)
00235             fitpa += 360.0;
00236         if (fitpa > 180.0)
00237             fitpa -= 180.0;
00238         cpl_wcs_delete((cpl_wcs *)wcs);
00239         cpl_propertylist_update_float(vircam_tfits_get_ehu(*outtab),
00240                                       "ESO QC POSANG",fitpa);
00241         cpl_propertylist_set_comment(vircam_tfits_get_ehu(*outtab),
00242                                      "ESO QC POSANG",
00243                                      "[degrees] Median position angle (from North)");
00244     }
00245             
00246     GOOD_STATUS
00247 }
00248 
00249 /*---------------------------------------------------------------------------*/
00266 /*---------------------------------------------------------------------------*/
00267 
00268 static double pixsize (cpl_propertylist *plist) {
00269     double cd1_1,cd1_2,pix;
00270 
00271     if (vircam_pfits_get_cd11(plist,&cd1_1) != VIR_OK ||
00272         vircam_pfits_get_cd12(plist,&cd1_2) != VIR_OK) {
00273         pix = NOMPIXSIZE;
00274     } else {
00275         pix = 3600.0*sqrt(cd1_1*cd1_1 + cd1_2*cd1_2);
00276     }
00277     return(pix);
00278 }
00279 
00282 /*
00283 
00284 $Log: vircam_imcore.c,v $
00285 Revision 1.28  2012/01/15 17:40:09  jim
00286 Minor modifications to take into accout the changes in cpl API for v6
00287 
00288 Revision 1.27  2011/01/21 10:57:26  jim
00289 Modified the position angle section to make sure the result is always
00290 in the range [0,180]
00291 
00292 Revision 1.26  2010/12/09 13:23:32  jim
00293 Modified to recalculate the median position angle so that it's in degrees
00294 from North to East
00295 
00296 Revision 1.25  2010/09/09 12:11:09  jim
00297 Fixed problems with docs that make doxygen barf
00298 
00299 Revision 1.24  2010/03/12 10:44:07  lbilbao
00300 Added missing header inclusion.
00301 
00302 Revision 1.23  2010/01/31 19:26:46  jim
00303 Image size is now reported in arcseconds
00304 
00305 Revision 1.22  2009/11/18 21:06:33  jim
00306 Added minpix argument to call to classify
00307 
00308 Revision 1.21  2007/05/02 09:13:57  jim
00309 Changed api so that outtab is now a vir_tfits structure
00310 
00311 Revision 1.20  2007/03/29 12:19:39  jim
00312 Little changes to improve documentation
00313 
00314 Revision 1.19  2007/03/01 12:42:41  jim
00315 Modified slightly after code checking
00316 
00317 Revision 1.18  2006/11/27 12:06:27  jim
00318 Tarted up some docs
00319 
00320 Revision 1.17  2006/08/07 14:20:31  jim
00321 Now duplicates input so that imcore doesn't write over it
00322 
00323 Revision 1.16  2006/08/01 11:27:53  jim
00324 Modifications to imcore background estimation and to add ability to
00325 specify the smoothing kernel width
00326 
00327 Revision 1.15  2006/07/11 14:52:47  jim
00328 Fixed possible memory leak
00329 
00330 Revision 1.14  2006/06/08 14:49:35  jim
00331 If the imcore_conf call generates a table with no rows, then it proceeds
00332 no further
00333 
00334 Revision 1.13  2006/06/08 09:03:49  jim
00335 Initialise outtab and extra to be NULL
00336 
00337 Revision 1.12  2006/03/23 21:18:49  jim
00338 Minor changes mainly to comment headers
00339 
00340 Revision 1.11  2006/03/22 13:58:32  jim
00341 Cosmetic fixes to keep lint happy
00342 
00343 Revision 1.10  2006/03/01 10:31:28  jim
00344 Now uses new vir_fits objects
00345 
00346 Revision 1.9  2006/02/22 10:08:58  jim
00347 Added status variable to call
00348 
00349 Revision 1.8  2006/02/18 11:48:55  jim
00350 *** empty log message ***
00351 
00352 Revision 1.7  2006/01/23 10:30:49  jim
00353 Mainly documentation mods
00354 
00355 Revision 1.6  2005/12/14 22:17:33  jim
00356 Updated docs
00357 
00358 Revision 1.5  2005/11/25 09:56:15  jim
00359 Tidied up some more documentation
00360 
00361 Revision 1.4  2005/11/03 15:16:28  jim
00362 Lots of changes mainly to strengthen error reporting
00363 
00364 Revision 1.3  2005/11/03 13:28:49  jim
00365 All sorts of changes to tighten up error handling
00366 
00367 Revision 1.2  2005/09/22 08:40:41  jim
00368 Fixed some bugs in imcore and added classification Removed some unnecessary
00369 files
00370 
00371 
00372 */

Generated on 15 Mar 2012 for VIRCAM Pipeline by  doxygen 1.6.1