CR2RE Pipeline Reference Manual 1.6.7
hdrl_cat_conf-test.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 <cpl_test.h>
21
22#include "../hdrl_cat_conf.h"
23#include "../hdrl_cat_table.h"
24#include "hdrl_types.h"
25
26#define NTEST 10
27
28
29/*----------------------------------------------------------------------------*/
33/*----------------------------------------------------------------------------*/
34void fill_plist(cpl_propertylist *pl)
35{
36 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN" );
37 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN" );
38 cpl_propertylist_update_double(pl, "CRVAL1", 30. );
39 cpl_propertylist_update_double(pl, "CRVAL2", 12. );
40 cpl_propertylist_update_double(pl, "CRPIX1", 512. );
41 cpl_propertylist_update_double(pl, "CRPIX2", 512. );
42 cpl_propertylist_update_double(pl, "CD1_1", -1. / 3600.);
43 cpl_propertylist_update_double(pl, "CD1_2", 0. );
44 cpl_propertylist_update_double(pl, "CD2_1", 0. );
45 cpl_propertylist_update_double(pl, "CD2_2", 1. / 3600.);
46}
47
48/*----------------------------------------------------------------------------*/
54/*----------------------------------------------------------------------------*/
55cpl_image * create_gauss(double dx, double dy, double fwhm)
56{
57 /* sample gaussian on large grid and sum it down to requested fwhm */
58 cpl_size factor = 16;
59 cpl_size nx = fwhm * 20;
60 cpl_size ny = fwhm * 20;
61 cpl_size nnx = nx * factor;
62 cpl_size nny = ny * factor;
63 double sigma = fwhm * factor / (2 * sqrt(2 * log(2)));
64 cpl_image *g = cpl_image_new(nnx, nny, CPL_TYPE_DOUBLE);
65
66 dx *= factor;
67 dy *= factor;
68
69 cpl_image_fill_gaussian( g,
70 nnx / 2 + dx,
71 nny / 2 + dx,
72 2. * CPL_MATH_PI * sigma * sigma,
73 sigma,
74 sigma);
75
76 cpl_image *r = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
77 double *dg = cpl_image_get_data_double(g);
78 double *dr = cpl_image_get_data_double(r);
79
80 for (cpl_size y = 0; y < ny; y++) {
81 for (cpl_size x = 0; x < nx; x++) {
82 for (cpl_size i = y * factor; i < y * factor + factor; i++) {
83 for (cpl_size j = x * factor; j < x * factor + factor; j++) {
84 dr[y * nx + x] += dg[i * nnx + j];
85 }
86 }
87 }
88 }
89
90 for (cpl_size y = 0; y < nny; y++) {
91 for (cpl_size x = 0; x < nnx; x++) {
92 dr[y / factor * nx + x / factor] += dg[y * nx + x];
93 }
94 }
95
96 cpl_image_divide_scalar(r, factor * factor);
97
98 /* avoiding a background rms of exactly zero */
99 cpl_image * noise_bkg = cpl_image_duplicate(r);
100 cpl_image_fill_noise_uniform(noise_bkg, -HDRL_EPS_DATA, +HDRL_EPS_DATA);
101 cpl_image_add(r, noise_bkg);
102 cpl_image_delete(noise_bkg);
103 /* Close up */
104 cpl_image_delete(g);
105
106 return r;
107}
108
109/*----------------------------------------------------------------------------*/
115/*----------------------------------------------------------------------------*/
116cpl_error_code hdrl_catalogue_hdrl_catalogue_conf_basic(void)
117{
118 /* Initialize */
119 double xpos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
120 double ypos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
121 double norm[] = {1000., 100., 200., 500., 550., 600., 650., 700., 750., 800.};
122
123 /* Generate a field with some stars and a confidence map */
124 cpl_image *bkg = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
125 cpl_image *im = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
126 cpl_image *cnf = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
127
128 cpl_image_fill_noise_uniform(bkg, -10., 10. );
129 cpl_image_fill_noise_uniform(cnf, 99.9, 100.1);
130
131 double sigma = 2.;
132 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
133
134 double sky = 500.;
135 cpl_image_add_scalar(bkg, sky);
136
137 double tot[NTEST];
138 for (cpl_size i = 0; i < NTEST; i++) {
139 cpl_image_fill_gaussian(im, xpos[i], ypos[i], norm[i] * norm2, sigma, sigma);
140 tot[i] = cpl_image_get_flux(im);
141 cpl_image_add(bkg, im);
142 }
143 cpl_image_delete(im);
144
145 hdrl_casu_fits *inf = hdrl_casu_fits_wrap(bkg);
146 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap(cnf);
147
148 /* Give it a WCS */
149 fill_plist(hdrl_casu_fits_get_ehu(inf));
150
151 /* Run casu catalogue. TODO: Check results */
152 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
153 cpl_test_eq(hdrl_catalogue_conf(inf, inconf, 5, 1.5, 0, 5., 1, 64, 6, 3., 1.,
154 HDRL_SATURATION_INIT,res), CPL_ERROR_NONE);
155 cpl_test_nonnull(res->catalogue);
156 cpl_image_delete(res->segmentation_map);
157 cpl_image_delete(res->background);
158
159 /* Check the results. Start by checking the number of rows and columns. Sort the table by X */
160 cpl_table *tab = hdrl_casu_tfits_get_table(res->catalogue);
161 cpl_test_nonnull(tab);
162 cpl_test_eq(cpl_table_get_ncol(tab), NCOLS);
163 cpl_test_eq(cpl_table_get_nrow(tab), NTEST);
164
165 cpl_propertylist *pl = cpl_propertylist_new();
166 cpl_propertylist_append_bool(pl, "X_coordinate", 0);
167 cpl_table_sort(tab, pl);
168 cpl_propertylist_delete(pl);
169
170 /* Test the column content of the table */
171 int nl;
172 for (cpl_size i = 0; i < NTEST; i++) {
173
174 cpl_test_abs(xpos[i], cpl_table_get_double(tab, "X_coordinate", i, &nl), 0.2);
175 cpl_test_abs(ypos[i], cpl_table_get_double(tab, "Y_coordinate", i, &nl), 0.2);
176
177 double diff = fabs(cpl_table_get_double(tab, "Aper_flux_5", i, &nl) - tot[i])
178 / cpl_table_get_double(tab, "Aper_flux_5_err", i, &nl);
179 cpl_test_lt(diff, 1.6);
180 }
181
182 /* Clean up */
184 hdrl_casu_fits_delete(inconf);
185 hdrl_casu_tfits_delete(res->catalogue);
186 cpl_free(res);
187
188
189 return cpl_error_get_code();
190}
191
192/*----------------------------------------------------------------------------*/
198/*----------------------------------------------------------------------------*/
199cpl_error_code hdrl_catalogue_hdrl_catalogue_conf_backsub(void)
200{
201 cpl_image *cnf = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
202 cpl_image_add_scalar(cnf, 100.);
203
204 cpl_image *im = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
205 cpl_image_fill_noise_uniform(im, -10., 10. );
206
207 /*Set a region arround the object to zero*/
208 for (cpl_size x = 400; x < 600; x++) {
209 for (cpl_size y = 400; y < 600; y++) {
210 cpl_image_set(im, x, y, 0.);
211 }
212 }
213
214 /* Insert an tophat object */
215 for (cpl_size x = 500; x < 505; x++) {
216 for (cpl_size y = 500; y < 505; y++) {
217 cpl_image_set(im, x, y, 4990.);
218 }
219 }
220
221 cpl_image_add_scalar(im, 10.);
222 hdrl_casu_fits *inf = hdrl_casu_fits_wrap( im);
223 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap(cnf);
224
225 /* Run casu catalogue */
226 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
227 cpl_test_eq(hdrl_catalogue_conf(inf, inconf, 5, 3., 0, 1., 0, 16, 7, 3., 1.,
228 HDRL_SATURATION_INIT,res), CPL_ERROR_NONE);
229 cpl_table *tab = hdrl_casu_tfits_get_table(res->catalogue);
230 cpl_test_nonnull(tab);
231
232 /* Check the results. */
233 cpl_test_eq(cpl_table_get_nrow(tab), 1);
234
235 /* Test the column content of the table */
236 int nl;
237 cpl_test_abs(5000., cpl_table_get_double(tab, "Aper_flux_1", 0, &nl), 0.2);
238
239 /* Clean up */
241 hdrl_casu_fits_delete(inconf);
242 hdrl_casu_tfits_delete(res->catalogue);
243 cpl_image_delete( res->segmentation_map);
244 cpl_image_delete( res->background);
245 cpl_free(res);
246
247
248 return cpl_error_get_code();
249}
250
251cpl_error_code test_gaussians(double dx, double dy, double fwhm)
252{
253 /* Generate a field with some stars and a confidence map */
254 cpl_image *im = create_gauss(dx, dy, fwhm);
255 hdrl_casu_fits *inf = hdrl_casu_fits_wrap(im);
256
257 /* Give it a WCS */
258 fill_plist(hdrl_casu_fits_get_ehu(inf));
259
260 /* Run casu catalogue. TODO: Check results */
261 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
262 cpl_test_eq(hdrl_catalogue_conf(inf, NULL, 5, 2.5, 0, fwhm, 1, fwhm * 3, 6, 3., 1.,
263 HDRL_SATURATION_INIT, res), CPL_ERROR_NONE);
264 cpl_test_nonnull(res->catalogue);
265 cpl_image_delete(res->segmentation_map);
266 cpl_image_delete(res->background);
267
268 /* Check the results. Start by checking the number of rows and columns. Sort the table by X */
269 cpl_table *tab = hdrl_casu_tfits_get_table(res->catalogue);
270 cpl_test_nonnull(tab);
271 cpl_test_eq(cpl_table_get_ncol(tab), NCOLS);
272 cpl_test_eq(cpl_table_get_nrow(tab), 1 );
273
274 /* Test the column content of the table */
275 int nl;
276 cpl_test_abs(fwhm, cpl_table_get_double(tab, "FWHM", 0, &nl), 0.006);
277
278 /* Clean up */
280 hdrl_casu_tfits_delete(res->catalogue);
281 cpl_free(res);
282
283
284 return cpl_error_get_code();
285}
286
287/*----------------------------------------------------------------------------*/
291/*----------------------------------------------------------------------------*/
292int main(void)
293{
294 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
295
296 hdrl_catalogue_hdrl_catalogue_conf_basic();
297 hdrl_catalogue_hdrl_catalogue_conf_backsub();
298
299 test_gaussians(.5, .5, 3.);
300 test_gaussians(.0, .5, 4.);
301 test_gaussians(.5, .5, 5.);
302 test_gaussians(.8, .5, 6.);
303 test_gaussians(.1, .2, 7.);
304
305 cpl_test_error(CPL_ERROR_NONE);
306
307 return cpl_test_end(0);
308}
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.
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.
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.