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