CR2RE Pipeline Reference Manual 1.6.10
hdrl_cat_casu.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2017 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "hdrl_cat_conf.h"
21
22#include "hdrl_cat_classify.h"
23
24
25/*** Prototypes ***/
26
27static void hdrl_casu_xytoradec(const cpl_wcs *wcs, double x, double y, double *ra, double *dec);
28
29
30/*----------------------------------------------------------------------------*/
38/*----------------------------------------------------------------------------*/
39
42/* ---------------------------------------------------------------------------*/
58/* ---------------------------------------------------------------------------*/
59hdrl_casu_tfits * hdrl_casu_tfits_wrap(cpl_table *tab, cpl_propertylist *ehu)
60{
61 /* Check for nonsense input */
62 if (tab == NULL) {
63 return NULL;
64 }
65
66 /* Get the hdrl_casu_tfits structure */
67 hdrl_casu_tfits *p = cpl_malloc(sizeof(hdrl_casu_tfits));
68
69 /* Load stuff in */
70 p->table = tab;
71
72 if (ehu != NULL) {
73 p->ehu = ehu;
74 } else {
75 p->ehu = cpl_propertylist_new();
76 }
77
78 return p;
79}
80
81/* ---------------------------------------------------------------------------*/
97/* ---------------------------------------------------------------------------*/
98cpl_propertylist * hdrl_casu_tfits_get_ehu(hdrl_casu_tfits *p)
99{
100 /* Check for nonsense input */
101 if (p == NULL) {
102 return NULL;
103 }
104
105 return p->ehu;
106}
107
108/* ---------------------------------------------------------------------------*/
120/* ---------------------------------------------------------------------------*/
121cpl_table * hdrl_casu_tfits_get_table(hdrl_casu_tfits *p)
122{
123 /* Check for nonsense input */
124 if (p == NULL) {
125 return NULL;
126 }
127
128 return p->table;
129}
130
131/* ---------------------------------------------------------------------------*/
138/* ---------------------------------------------------------------------------*/
139void hdrl_casu_tfits_delete(hdrl_casu_tfits *p)
140{
141 /* Check for nonsense input */
142 if (p != NULL) {
143
144 /* Free up workspace if it's been used */
145 if (p->table ) cpl_table_delete(p->table);
146 if (p->ehu ) cpl_propertylist_delete(p->ehu);
147
148 cpl_free(p);
149 }
150}
151
152/* ---------------------------------------------------------------------------*/
164/* ---------------------------------------------------------------------------*/
165hdrl_casu_fits * hdrl_casu_fits_wrap(cpl_image *im)
166{
167 /* Check for nonsense input */
168 if (im == NULL) {
169 return NULL;
170 }
171
172 /* Get the hdrl_casu_fits structure */
173 hdrl_casu_fits *p = cpl_malloc(sizeof(hdrl_casu_fits));
174
175 /* Load stuff in */
176 p->image = im;
177 p->ehu = cpl_propertylist_new();
178
179 return p;
180}
181
182/* ---------------------------------------------------------------------------*/
193/* ---------------------------------------------------------------------------*/
194hdrl_casu_fits * hdrl_casu_fits_duplicate(hdrl_casu_fits *in)
195{
196 /* Check for nonsense input */
197 if (in == NULL) {
198 return NULL;
199 }
200
201 /* Copy the hdrl_casu_fits structure */
202 hdrl_casu_fits *p = cpl_malloc(sizeof(hdrl_casu_fits));
203 p->image = cpl_image_duplicate(in->image);
204 p->ehu = cpl_propertylist_duplicate(hdrl_casu_fits_get_ehu(in));
205
206 return p;
207}
208
209/* ---------------------------------------------------------------------------*/
225/* ---------------------------------------------------------------------------*/
226cpl_propertylist * hdrl_casu_fits_get_ehu(hdrl_casu_fits *p)
227{
228 /* Check for nonsense input */
229 if (p == NULL) {
230 return NULL;
231 }
232
233 return p->ehu;
234}
235
236/* ---------------------------------------------------------------------------*/
248/* ---------------------------------------------------------------------------*/
249cpl_image * hdrl_casu_fits_get_image(hdrl_casu_fits *p)
250{
251 /* Check for nonsense input */
252 if (p == NULL) {
253 return NULL;
254 }
255
256 return p->image;
257}
258
259/* ---------------------------------------------------------------------------*/
266/* ---------------------------------------------------------------------------*/
267void hdrl_casu_fits_delete(hdrl_casu_fits *p)
268{
269 /* Check for nonsense input */
270 if (p != NULL) {
271
272 /* Free up workspace if it's been used */
273 if (p->image ) cpl_image_delete(p->image);
274 if (p->ehu ) cpl_propertylist_delete(p->ehu);
275
276 cpl_free(p);
277 }
278}
279
280/* ---------------------------------------------------------------------------*/
320/* ---------------------------------------------------------------------------*/
321cpl_error_code hdrl_casu_catalogue(
322 hdrl_casu_fits *infile, hdrl_casu_fits *conf,
323 const cpl_wcs *wcs, cpl_size ipix,
324 double threshold, cpl_size icrowd, double rcore,
325 cpl_size bkg_subtr, cpl_size nbsize,
326 hdrl_catalogue_options cattype,
327 double filtfwhm, double gainloc, double saturation,
328 hdrl_casu_result *res)
329{
330 /* Inherited status */
331 res->catalogue = NULL;
332
333 /* Copy the input, the background is subtracted in-place */
334 hdrl_casu_fits *in = hdrl_casu_fits_duplicate(infile);
335
336 /* Call the main processing routine and get the catalogue */
337 if(hdrl_catalogue_conf(in, conf, ipix, threshold, icrowd, rcore, bkg_subtr,
338 nbsize, cattype, filtfwhm, gainloc, saturation, res) != CPL_ERROR_NONE) {
340 return cpl_error_get_code();
341 }
342
343 if (cpl_table_get_nrow(hdrl_casu_tfits_get_table(res->catalogue)) == 0) {
345 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND, "hdrl_cat_casu_catalogue - No objects found in image");
346 return CPL_ERROR_DATA_NOT_FOUND;
347 }
348
349 /* Do the classification */
350 if (cattype & HDRL_CATALOGUE_CAT_COMPLETE) {
351
352 if (hdrl_classify(res->catalogue, 16.) != CPL_ERROR_NONE) {
354 return cpl_error_get_code();
355 }
356
357
358 /* Update the RA and DEC of the objects in the object catalogue */
359 if (wcs) {
360
361 /* Update the RA and DEC of the objects in the object catalogue */
362 cpl_table* cat = hdrl_casu_tfits_get_table(res->catalogue);
363
364 double* x = cpl_table_get_data_double(cat, "X_coordinate");
365 double* y = cpl_table_get_data_double(cat, "Y_coordinate");
366 double* ra = cpl_table_get_data_double(cat, "RA");
367 double* dec = cpl_table_get_data_double(cat, "DEC");
368
369 cpl_size n = cpl_table_get_nrow(cat);
370 for (cpl_size i = 0; i < n; i++) {
371 hdrl_casu_xytoradec(wcs, x[i], y[i], &(ra[i]), &(dec[i]));
372 }
373 }
374
375 cpl_propertylist_set_comment(hdrl_casu_tfits_get_ehu(res->catalogue),
376 "ESO QC IMAGE_SIZE", "[pixel] Average FWHM of stellar objects");
377
378 } else {
379
380 cpl_table_select_all( hdrl_casu_tfits_get_table(res->catalogue));
381 cpl_table_erase_selected(hdrl_casu_tfits_get_table(res->catalogue));
382 }
383
385
386 return CPL_ERROR_NONE;
387}
388
391/* ---------------------------------------------------------------------------*/
405/* ---------------------------------------------------------------------------*/
406static void hdrl_casu_xytoradec(const cpl_wcs *wcs, double x, double y, double *ra, double *dec)
407{
408 /* Load up the information */
409 cpl_matrix *from = cpl_matrix_new(1, 2);
410 double *xy = cpl_matrix_get_data(from);
411 xy[0] = x;
412 xy[1] = y;
413
414 /* Call the conversion routine */
415 cpl_matrix *to = NULL;
416 cpl_array *status = NULL;
417 cpl_wcs_convert(wcs, from, &to, &status, CPL_WCS_PHYS2WORLD);
418
419 /* Pass it back now */
420 double *radec = cpl_matrix_get_data(to);
421 *ra = radec[0];
422 *dec = radec[1];
423
424 /* Tidy and exit */
425 cpl_matrix_delete(from);
426 cpl_matrix_delete(to);
427 cpl_array_delete(status);
428}
cpl_error_code hdrl_catalogue_conf(hdrl_casu_fits *infile, hdrl_casu_fits *conf, cpl_size ipix, double threshold, cpl_size icrowd, double rcore, cpl_size bkg_subtr, cpl_size nbsize, cpl_size cattype, double filtfwhm, double gain, double saturation, hdrl_casu_result *res)
Do source extraction.
hdrl_casu_fits * hdrl_casu_fits_wrap(cpl_image *im)
Wrap an image in a hdrl_casu_fits wrapper.
cpl_propertylist * hdrl_casu_tfits_get_ehu(hdrl_casu_tfits *p)
Get the propertylist for the extension header for a given hdrl_casu_tfits image.
Definition: hdrl_cat_casu.c:98
cpl_image * hdrl_casu_fits_get_image(hdrl_casu_fits *p)
Get the CPL image from the hdrl_casu_fits object.
hdrl_casu_fits * hdrl_casu_fits_duplicate(hdrl_casu_fits *in)
Copy a hdrl_casu_fits structure into another one.
void hdrl_casu_tfits_delete(hdrl_casu_tfits *p)
Free all the workspace associated with a hdrl_casu_fits object.
void hdrl_casu_fits_delete(hdrl_casu_fits *p)
Free all the workspace associated with a hdrl_casu_fits object.
cpl_table * hdrl_casu_tfits_get_table(hdrl_casu_tfits *p)
Get the CPL table from the hdrl_casu_tfits object.
hdrl_casu_tfits * hdrl_casu_tfits_wrap(cpl_table *tab, cpl_propertylist *ehu)
Wrap an table in a hdrl_casu_tfits wrapper.
Definition: hdrl_cat_casu.c:59
cpl_propertylist * hdrl_casu_fits_get_ehu(hdrl_casu_fits *p)
Get the propertylist for the extension header for a given hdrl_casu_fits image.
cpl_error_code hdrl_casu_catalogue(hdrl_casu_fits *infile, hdrl_casu_fits *conf, const cpl_wcs *wcs, cpl_size ipix, double threshold, cpl_size icrowd, double rcore, cpl_size bkg_subtr, cpl_size nbsize, hdrl_catalogue_options cattype, double filtfwhm, double gainloc, double saturation, hdrl_casu_result *res)
Generate object catalogues from input images.
cpl_error_code hdrl_classify(hdrl_casu_tfits *catalogue, double minsize)
Do star/galaxy classification.