CR2RE Pipeline Reference Manual 1.7.0
hdrl_cat_casu_addmul-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_casu.h"
27
28
29#define COMP_TOL_REL 1e-4
30#define COMP_TOL_ABS 1e-2
31#define IMG_XSIZE 110 * 2
32#define IMG_YSIZE 150 * 2
33#define CELL_SIZE 32
34#define APER_FLUX_NUM "Aper_flux_3"
35#define NUM_FACTORS 5
36#define NUM_BIASES 5
37#define BIASES {2., 100., 5000., 5.e4, 5.e5}
38#define FACTORS {2., 10., 100., 1.e4, 1.e6}
39
40
41/*----------------------------------------------------------------------------*/
52/*----------------------------------------------------------------------------*/
53cpl_error_code hdrl_casumul_compute(double factor)
54{
55 /* Check inherited status */
56 hdrl_casu_result *res1 = cpl_calloc(1, sizeof(hdrl_casu_result));
57 hdrl_casu_result *res_p2 = cpl_calloc(1, sizeof(hdrl_casu_result));
58 cpl_test_eq(hdrl_casu_catalogue(NULL, NULL, NULL, 5, 1.5, 1, 3.5, 1,
59 CELL_SIZE, 6, 1.0, 2.0, HDRL_SATURATION_INIT, res1), CPL_ERROR_NULL_INPUT);
60 cpl_test_error(CPL_ERROR_NULL_INPUT);
61 cpl_test_null( res1->catalogue);
62 hdrl_casu_tfits_delete(res1->catalogue);
63 cpl_image_delete( res1->segmentation_map);
64 cpl_image_delete( res1->background);
65 cpl_free(res1);
66
67 /* Generate a field with some stars and a confidence map */
68 cpl_image *bkg = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
69 cpl_image *im = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
70 cpl_image *cnf = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
71
72 double sigma = 2.;
73 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
74 cpl_image_fill_noise_uniform(bkg, -10., 10.);
75
76 double sky = 500.;
77 cpl_image_add_scalar(bkg, sky);
78 cpl_image_fill_noise_uniform(cnf, 99.9, 100.1);
79
80 double xpos = 100.;
81 double ypos = 100.;
82 double norm = 5000.;
83 cpl_image_fill_gaussian(im, xpos, ypos, norm * norm2, sigma, sigma);
84 cpl_image_add(bkg, im);
85 cpl_image_delete(im);
86
87 /* Mult */
88 cpl_image *bkg2 = cpl_image_duplicate(bkg);
89 cpl_image_multiply_scalar(bkg2, factor);
90
91 hdrl_casu_fits *inf = hdrl_casu_fits_wrap( bkg);
92 hdrl_casu_fits *inf2 = hdrl_casu_fits_wrap(bkg2);
93 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap( cnf);
94
95 /* Give it a WCS */
96 cpl_propertylist *pl = hdrl_casu_fits_get_ehu(inf);
97 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN" );
98 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN" );
99 cpl_propertylist_update_double(pl, "CRVAL1", 30. );
100 cpl_propertylist_update_double(pl, "CRVAL2", 12. );
101 cpl_propertylist_update_double(pl, "CRPIX1", 512. );
102 cpl_propertylist_update_double(pl, "CRPIX2", 512. );
103 cpl_propertylist_update_double(pl, "CD1_1", -1. / 3600.);
104 cpl_propertylist_update_double(pl, "CD1_2", 0. );
105 cpl_propertylist_update_double(pl, "CD2_1", 0. );
106 cpl_propertylist_update_double(pl, "CD2_2", 1. / 3600.);
107 cpl_propertylist_update_int( pl, "NAXIS1", IMG_XSIZE );
108 cpl_propertylist_update_int( pl, "NAXIS2", IMG_YSIZE );
109 cpl_wcs *wcs = cpl_wcs_new_from_propertylist(pl);
110
111 /* Run casu catalogue */
112 hdrl_casu_result * res2 = cpl_calloc(1, sizeof(hdrl_casu_result));
113 cpl_test_eq(hdrl_casu_catalogue(inf, inconf, wcs, 5, 1.5, 0, 5.0, 1,
114 CELL_SIZE, HDRL_CATALOGUE_ALL, 3., 1., HDRL_SATURATION_INIT, res2), CPL_ERROR_NONE);
115 cpl_test_nonnull(res2->catalogue);
117
118 /* Run casu catalogue for Mul*/
119 cpl_test_eq(hdrl_casu_catalogue(inf2, inconf, wcs, 5, 1.5, 0, 5., 1,
120 CELL_SIZE, HDRL_CATALOGUE_ALL, 3., 1., HDRL_SATURATION_INIT, res_p2), CPL_ERROR_NONE);
121 cpl_test_nonnull(res_p2->catalogue);
123 hdrl_casu_fits_delete(inconf);
124 cpl_wcs_delete(wcs);
125
126 /*** Tests ***/
127 int nl;
128
129 cpl_test_image_rel(res2->segmentation_map, res_p2->segmentation_map, COMP_TOL_REL);
130
131 cpl_table *tab = hdrl_casu_tfits_get_table(res2->catalogue);
132 cpl_test_nonnull(tab);
133
134 cpl_table *tab_p2 = hdrl_casu_tfits_get_table(res_p2->catalogue);
135 cpl_test_nonnull(tab_p2);
136
137
138 cpl_test_rel( cpl_table_get_double(tab, APER_FLUX_NUM, 0, &nl),
139 cpl_table_get_double(tab_p2, APER_FLUX_NUM, 0, &nl) / factor, COMP_TOL_REL );
140 cpl_test_rel( cpl_table_get_double(tab, "Isophotal_flux", 0, &nl),
141 cpl_table_get_double(tab_p2, "Isophotal_flux", 0, &nl) / factor, COMP_TOL_REL );
142 cpl_test_rel( cpl_table_get_double(tab, "FWHM", 0, &nl),
143 cpl_table_get_double(tab_p2, "FWHM", 0, &nl), COMP_TOL_REL );
144 cpl_test_rel( cpl_table_get_double(tab, "Kron_radius", 0, &nl),
145 cpl_table_get_double(tab_p2, "Kron_radius", 0, &nl), COMP_TOL_REL );
146 cpl_test_eq ( cpl_table_get_double(tab, "Classification", 0, &nl),
147 cpl_table_get_double(tab_p2, "Classification", 0, &nl) );
148 cpl_test_rel( cpl_table_get_double(tab, "X_coordinate", 0, &nl),
149 cpl_table_get_double(tab_p2, "X_coordinate", 0, &nl), COMP_TOL_REL );
150 cpl_test_abs( cpl_table_get_double(tab, "X_coordinate_err", 0, &nl),
151 cpl_table_get_double(tab_p2, "X_coordinate_err", 0, &nl), COMP_TOL_ABS );
152 cpl_test_rel( cpl_table_get_double(tab, "Y_coordinate", 0, &nl),
153 cpl_table_get_double(tab_p2, "Y_coordinate", 0, &nl), COMP_TOL_REL );
154 cpl_test_abs( cpl_table_get_double(tab, "Y_coordinate_err", 0, &nl),
155 cpl_table_get_double(tab_p2, "Y_coordinate_err", 0, &nl), COMP_TOL_ABS );
156
157 /* Clean up */
158 cpl_image_delete( res2->segmentation_map);
159 cpl_image_delete( res2->background);
160 hdrl_casu_tfits_delete(res2->catalogue);
161 cpl_free(res2);
162
163 cpl_image_delete( res_p2->segmentation_map);
164 cpl_image_delete( res_p2->background);
165 hdrl_casu_tfits_delete(res_p2->catalogue);
166 cpl_free(res_p2);
167
168
169 return cpl_error_get_code();
170}
171
172/*----------------------------------------------------------------------------*/
180/*----------------------------------------------------------------------------*/
181cpl_error_code hdrl_casuadd_compute(double bias)
182{
183 /* Check inherited status */
184 hdrl_casu_result *res1 = cpl_calloc(1, sizeof(hdrl_casu_result));
185 hdrl_casu_result *res_p2 = cpl_calloc(1, sizeof(hdrl_casu_result));
186 cpl_test_eq(hdrl_casu_catalogue(NULL, NULL, NULL, 5, 1.5, 1, 3.5, 1,
187 CELL_SIZE, 6, 1., 2., HDRL_SATURATION_INIT, res1), CPL_ERROR_NULL_INPUT);
188 cpl_test_error(CPL_ERROR_NULL_INPUT);
189 cpl_test_null(res1->catalogue);
190 hdrl_casu_tfits_delete(res1->catalogue);
191 cpl_image_delete( res1->segmentation_map);
192 cpl_image_delete( res1->background);
193 cpl_free(res1);
194
195 /* Generate a field with some stars and a confidence map */
196 cpl_image *bkg = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
197 cpl_image *im = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
198 cpl_image *cnf = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
199
200 double sigma = 2.;
201 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
202 cpl_image_fill_noise_uniform(bkg, -10., 10.);
203
204 double sky = 500.;
205 cpl_image_add_scalar(bkg, sky);
206
207 cpl_image_fill_noise_uniform(cnf, 99.9, 100.1);
208
209 double xpos = 100.;
210 double ypos = 100.;
211 double norm = 1000.;
212 cpl_image_fill_gaussian(im, xpos, ypos, norm * norm2, sigma, sigma);
213 cpl_image_add(bkg, im);
214 cpl_image_delete(im);
215
216 /* Add */
217 cpl_image *bkg2 = cpl_image_duplicate(bkg);
218 cpl_image_add_scalar(bkg2, bias);
219
220 hdrl_casu_fits *inf = hdrl_casu_fits_wrap( bkg);
221 hdrl_casu_fits *inf2 = hdrl_casu_fits_wrap(bkg2);
222 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap( cnf);
223
224 /* Give it a WCS */
225 cpl_propertylist *pl = hdrl_casu_fits_get_ehu(inf);
226 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN" );
227 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN" );
228 cpl_propertylist_update_double(pl, "CRVAL1", 30. );
229 cpl_propertylist_update_double(pl, "CRVAL2", 12. );
230 cpl_propertylist_update_double(pl, "CRPIX1", 512. );
231 cpl_propertylist_update_double(pl, "CRPIX2", 512. );
232 cpl_propertylist_update_double(pl, "CD1_1", -1. / 3600.);
233 cpl_propertylist_update_double(pl, "CD1_2", 0. );
234 cpl_propertylist_update_double(pl, "CD2_1", 0. );
235 cpl_propertylist_update_double(pl, "CD2_2", 1. / 3600.);
236 cpl_propertylist_update_int( pl, "NAXIS1", IMG_XSIZE );
237 cpl_propertylist_update_int( pl, "NAXIS2", IMG_YSIZE );
238 cpl_wcs *wcs = cpl_wcs_new_from_propertylist(pl);
239
240 /* Run casu catalogue */
241 hdrl_casu_result *res2 = cpl_calloc(1, sizeof(hdrl_casu_result));
242 cpl_test_eq(hdrl_casu_catalogue(inf, inconf, wcs, 5, 1.5, 0, 5., 1,
243 CELL_SIZE, HDRL_CATALOGUE_ALL, 3., 1., HDRL_SATURATION_INIT, res2), CPL_ERROR_NONE);
244 cpl_test_nonnull(res2->catalogue);
246
247 /* Run casu catalogue for ADD*/
248 cpl_test_eq(hdrl_casu_catalogue(inf2, inconf, wcs, 5, 1.5, 0, 5., 1,
249 CELL_SIZE, HDRL_CATALOGUE_ALL, 3., 1., HDRL_SATURATION_INIT, res_p2), CPL_ERROR_NONE);
250 cpl_test_nonnull(res_p2->catalogue);
252 hdrl_casu_fits_delete(inconf);
253 cpl_wcs_delete(wcs);
254
255 /*** Tests ***/
256 int nl;
257
258 cpl_test_image_rel(res2->segmentation_map, res_p2->segmentation_map, COMP_TOL_REL);
259
260 cpl_table *tab = hdrl_casu_tfits_get_table(res2->catalogue);
261 cpl_test_nonnull(tab);
262
263 cpl_table *tab_p2 = hdrl_casu_tfits_get_table(res_p2->catalogue);
264 cpl_test_nonnull(tab_p2);
265
266 cpl_test_rel( cpl_table_get_double(tab, APER_FLUX_NUM, 0, &nl),
267 cpl_table_get_double(tab_p2, APER_FLUX_NUM, 0, &nl), COMP_TOL_REL );
268 cpl_test_rel( cpl_table_get_double(tab, "Isophotal_flux", 0, &nl),
269 cpl_table_get_double(tab_p2, "Isophotal_flux", 0, &nl), COMP_TOL_REL );
270 cpl_test_rel( cpl_table_get_double(tab, "FWHM", 0, &nl),
271 cpl_table_get_double(tab_p2, "FWHM", 0, &nl), COMP_TOL_REL );
272 cpl_test_rel( cpl_table_get_double(tab, "Kron_radius", 0, &nl),
273 cpl_table_get_double(tab_p2, "Kron_radius", 0, &nl), COMP_TOL_REL );
274 cpl_test_eq ( cpl_table_get_double(tab, "Classification", 0, &nl),
275 cpl_table_get_double(tab_p2, "Classification", 0, &nl) );
276 cpl_test_rel( cpl_table_get_double(tab, "X_coordinate", 0, &nl),
277 cpl_table_get_double(tab_p2, "X_coordinate", 0, &nl), COMP_TOL_REL );
278 cpl_test_abs( cpl_table_get_double(tab, "X_coordinate_err", 0, &nl),
279 cpl_table_get_double(tab_p2, "X_coordinate_err", 0, &nl), COMP_TOL_ABS );
280 cpl_test_rel( cpl_table_get_double(tab, "Y_coordinate", 0, &nl),
281 cpl_table_get_double(tab_p2, "Y_coordinate", 0, &nl), COMP_TOL_REL );
282 cpl_test_abs( cpl_table_get_double(tab, "Y_coordinate_err", 0, &nl),
283 cpl_table_get_double(tab_p2, "Y_coordinate_err", 0, &nl), COMP_TOL_ABS );
284
285 /* Clean up */
286 cpl_image_delete( res2->segmentation_map);
287 cpl_image_delete( res2->background);
288 hdrl_casu_tfits_delete(res2->catalogue);
289 cpl_free(res2);
290
291 cpl_image_delete( res_p2->segmentation_map);
292 cpl_image_delete( res_p2->background);
293 hdrl_casu_tfits_delete(res_p2->catalogue);
294 cpl_free(res_p2);
295
296
297 return cpl_error_get_code();
298}
299
300
301/*----------------------------------------------------------------------------*/
305/*----------------------------------------------------------------------------*/
306int main(void)
307{
308 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
309
310 double bias[] = BIASES;
311 for (cpl_size i = 0; i < NUM_BIASES; i++) {
312 hdrl_casuadd_compute(bias[i]);
313 }
314
315 double factor[] = FACTORS;
316 for (cpl_size i = 0; i < NUM_FACTORS; i++) {
317 hdrl_casumul_compute(factor[i]);
318 }
319
320 cpl_test_error(CPL_ERROR_NONE);
321
322 return cpl_test_end(0);
323}
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.
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.