VIRCAM Pipeline  1.3.4
vircam/vircam_imcore.c
1 /* $Id: vircam_imcore.c,v 1.29 2013-10-15 16:44:42 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: 2013-10-15 16:44:42 $
24  * $Revision: 1.29 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 /* Includes */
29 
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #include <math.h>
35 
36 #include "vircam_mods.h"
37 #include "vircam_utils.h"
38 #include "vircam_wcsutils.h"
39 #include "vircam_fits.h"
40 #include "catalogue/imcore.h"
41 #include "vircam_pfits.h"
42 
43 #define NOMPIXSIZE 0.34
44 
45 static double pixsize (cpl_propertylist *plist);
46 
49 /*---------------------------------------------------------------------------*/
148 /*---------------------------------------------------------------------------*/
149 
150 extern int vircam_imcore(vir_fits *infile, vir_fits *conf, int ipix,
151  float threshold, int icrowd, float rcore, int nbsize,
152  int cattyp, float filtfwhm, vir_tfits **outtab,
153  int *status) {
154  int retval;
155  const char *fctid = "vircam_imcore";
156  cpl_propertylist *plist,*elist;
157  vir_fits *in,*c;
158  double pixarcsec,*cd,theta_east,theta_north,theta_north_2;
159  float fwhm,fitpa;
160  cpl_wcs *wcs;
161 
162  /* Inherited status */
163 
164  *outtab = NULL;
165  if (*status != VIR_OK)
166  return(*status);
167 
168  /* Copy the input */
169 
170  cattype = cattyp;
171  in = vircam_fits_duplicate(infile);
172  c = vircam_fits_duplicate(conf);
173 
174  /* Call the main processing routine and get the catalogue */
175 
176  retval = imcore_conf(in,c,ipix,threshold,icrowd,rcore,nbsize,cattype,
177  filtfwhm,outtab);
178  vircam_fits_delete(in);
180  if (retval != VIR_OK)
181  FATAL_ERROR;
182  if ((int)cpl_table_get_nrow(vircam_tfits_get_table(*outtab)) == 0) {
183  cpl_msg_warning(fctid,"No objects found in %s",
184  vircam_fits_get_fullname(infile));
185  freetfits(*outtab);
186  WARN_RETURN
187  }
188 
189  /* Get the property list from the input frame */
190 
191  plist = vircam_fits_get_phu(infile);
192  if (plist == NULL) {
193  cpl_msg_error(fctid,"Unable to open propertylist %s",
194  vircam_fits_get_filename(infile));
195  FATAL_ERROR
196  }
197 
198  /* Do the classification */
199 
200  if (cattype != 3) {
201  retval = classify(*outtab,plist,16.0,cattype);
202  if (retval != VIR_OK)
203  WARN_RETURN
204 
205  /* Convert the QC FWHM value to arcsec */
206 
207  elist = vircam_fits_get_ehu(infile);
208  pixarcsec = pixsize(elist);
209  fwhm = cpl_propertylist_get_float(vircam_tfits_get_ehu(*outtab),
210  "ESO QC IMAGE_SIZE");
211  fwhm *= pixarcsec;
212  cpl_propertylist_update_float(vircam_tfits_get_ehu(*outtab),
213  "ESO QC IMAGE_SIZE",fwhm);
214  cpl_propertylist_set_comment(vircam_tfits_get_ehu(*outtab),
215  "ESO QC IMAGE_SIZE",
216  "[arcsec] Average FWHM of stellar objects");
217 
218  /* Now convert the median position angle estimate to degrees from
219  North in an Easterly direction...*/
220 
221  fitpa = cpl_propertylist_get_float(vircam_tfits_get_ehu(*outtab),
222  "ESO QC POSANG");
223  if (fitpa != 0.0) {
224  wcs = cpl_wcs_new_from_propertylist(elist);
225  cd = cpl_matrix_get_data((cpl_matrix *)cpl_wcs_get_cd(wcs));
226  theta_east = DEGRAD*atan2(cd[1],cd[0]);
227  theta_north = DEGRAD*atan2(cd[3],cd[2]);
228  theta_north_2 = (theta_north < 0.0 ? theta_north + 360.0 : theta_north);
229  if (abs(theta_north-theta_east-90.0) < 5.0 ||
230  abs(theta_north-theta_east+270.0) < 5.0) {
231  fitpa = theta_north_2 - fitpa;
232  } else {
233  fitpa = 360.0 - theta_north_2 + fitpa;
234  }
235  if (fitpa < 0.0)
236  fitpa += 360.0;
237  if (fitpa > 180.0)
238  fitpa -= 180.0;
239  cpl_wcs_delete((cpl_wcs *)wcs);
240  cpl_propertylist_update_float(vircam_tfits_get_ehu(*outtab),
241  "ESO QC POSANG",fitpa);
242  cpl_propertylist_set_comment(vircam_tfits_get_ehu(*outtab),
243  "ESO QC POSANG",
244  "[degrees] Median position angle (from North)");
245  }
246  }
247  GOOD_STATUS
248 }
249 
250 /*---------------------------------------------------------------------------*/
267 /*---------------------------------------------------------------------------*/
268 
269 static double pixsize (cpl_propertylist *plist) {
270  double cd1_1,cd1_2,pix;
271 
272  if (vircam_pfits_get_cd11(plist,&cd1_1) != VIR_OK ||
273  vircam_pfits_get_cd12(plist,&cd1_2) != VIR_OK) {
274  pix = NOMPIXSIZE;
275  } else {
276  pix = 3600.0*sqrt(cd1_1*cd1_1 + cd1_2*cd1_2);
277  }
278  return(pix);
279 }
280 
283 /*
284 
285 $Log: not supported by cvs2svn $
286 Revision 1.28 2012/01/15 17:40:09 jim
287 Minor modifications to take into accout the changes in cpl API for v6
288 
289 Revision 1.27 2011/01/21 10:57:26 jim
290 Modified the position angle section to make sure the result is always
291 in the range [0,180]
292 
293 Revision 1.26 2010/12/09 13:23:32 jim
294 Modified to recalculate the median position angle so that it's in degrees
295 from North to East
296 
297 Revision 1.25 2010/09/09 12:11:09 jim
298 Fixed problems with docs that make doxygen barf
299 
300 Revision 1.24 2010/03/12 10:44:07 lbilbao
301 Added missing header inclusion.
302 
303 Revision 1.23 2010/01/31 19:26:46 jim
304 Image size is now reported in arcseconds
305 
306 Revision 1.22 2009/11/18 21:06:33 jim
307 Added minpix argument to call to classify
308 
309 Revision 1.21 2007/05/02 09:13:57 jim
310 Changed api so that outtab is now a vir_tfits structure
311 
312 Revision 1.20 2007/03/29 12:19:39 jim
313 Little changes to improve documentation
314 
315 Revision 1.19 2007/03/01 12:42:41 jim
316 Modified slightly after code checking
317 
318 Revision 1.18 2006/11/27 12:06:27 jim
319 Tarted up some docs
320 
321 Revision 1.17 2006/08/07 14:20:31 jim
322 Now duplicates input so that imcore doesn't write over it
323 
324 Revision 1.16 2006/08/01 11:27:53 jim
325 Modifications to imcore background estimation and to add ability to
326 specify the smoothing kernel width
327 
328 Revision 1.15 2006/07/11 14:52:47 jim
329 Fixed possible memory leak
330 
331 Revision 1.14 2006/06/08 14:49:35 jim
332 If the imcore_conf call generates a table with no rows, then it proceeds
333 no further
334 
335 Revision 1.13 2006/06/08 09:03:49 jim
336 Initialise outtab and extra to be NULL
337 
338 Revision 1.12 2006/03/23 21:18:49 jim
339 Minor changes mainly to comment headers
340 
341 Revision 1.11 2006/03/22 13:58:32 jim
342 Cosmetic fixes to keep lint happy
343 
344 Revision 1.10 2006/03/01 10:31:28 jim
345 Now uses new vir_fits objects
346 
347 Revision 1.9 2006/02/22 10:08:58 jim
348 Added status variable to call
349 
350 Revision 1.8 2006/02/18 11:48:55 jim
351 *** empty log message ***
352 
353 Revision 1.7 2006/01/23 10:30:49 jim
354 Mainly documentation mods
355 
356 Revision 1.6 2005/12/14 22:17:33 jim
357 Updated docs
358 
359 Revision 1.5 2005/11/25 09:56:15 jim
360 Tidied up some more documentation
361 
362 Revision 1.4 2005/11/03 15:16:28 jim
363 Lots of changes mainly to strengthen error reporting
364 
365 Revision 1.3 2005/11/03 13:28:49 jim
366 All sorts of changes to tighten up error handling
367 
368 Revision 1.2 2005/09/22 08:40:41 jim
369 Fixed some bugs in imcore and added classification Removed some unnecessary
370 files
371 
372 
373 */
char * vircam_fits_get_filename(vir_fits *p)
Definition: vircam_fits.c:526
cpl_table * vircam_tfits_get_table(vir_tfits *p)
Definition: vircam_tfits.c:364
cpl_propertylist * vircam_tfits_get_ehu(vir_tfits *p)
Definition: vircam_tfits.c:473
void vircam_fits_delete(vir_fits *p)
Definition: vircam_fits.c:277
char * vircam_fits_get_fullname(vir_fits *p)
Definition: vircam_fits.c:560
int imcore_conf(vir_fits *infile, vir_fits *conf, int ipix, float threshold, int icrowd, float rcore, int nbsize, int cattyp, float filtfwhm, vir_tfits **outcat)
Do source extraction.
Definition: imcore_conf.c:146
int vircam_pfits_get_cd12(const cpl_propertylist *plist, double *cd12)
Get the value of cd1_2.
Definition: vircam_pfits.c:150
vir_fits * vircam_fits_duplicate(vir_fits *in)
Definition: vircam_fits.c:159
cpl_propertylist * vircam_fits_get_phu(vir_fits *p)
Definition: vircam_fits.c:416
cpl_propertylist * vircam_fits_get_ehu(vir_fits *p)
Definition: vircam_fits.c:457
int classify(vir_tfits *catalogue, cpl_propertylist *plist, float minsize, int cattype)
Do star/galaxy classification.
Definition: classify.c:204
int vircam_pfits_get_cd11(const cpl_propertylist *plist, double *cd11)
Get the value of cd1_1.
Definition: vircam_pfits.c:135
int vircam_imcore(vir_fits *infile, vir_fits *conf, int ipix, float threshold, int icrowd, float rcore, int nbsize, int cattyp, float filtfwhm, vir_tfits **outtab, int *status)
Generate object catalogues from input images.