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