CR2RE Pipeline Reference Manual 1.6.7
hdrl_cat_casu_bkg_badpatch-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 NTEST 10
26#define COMP_TOLERANCE 1e-2
27#define CORNER_OFFSET 10
28#define CORNER_REL_TOL 1e-2
29#define X_OS_P1 60
30#define X_OS_P2 95
31#define STAR 7
32#define PATCH_SIZE 100 /* even number (patch is a square) */
33#define IMG_XSIZE 1100
34#define IMG_YSIZE 1500
35#define CELL_SIZE 32
36#define MIN_RAMP 10.
37#define MAX_RAMP 100.
38#define APER_FLUX_NUM "Aper_flux_3"
39
40/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
50cpl_error_code hdrl_casubkg_badpatch_compute(void)
51{
52 double xpos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
53 double ypos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
54 double norm[] = {1000., 100., 200., 500., 550., 600., 650., 700., 750., 800.};
55
56 /* Check inherited status */
57 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
58 hdrl_casu_result *res_p1 = cpl_calloc(1, sizeof(hdrl_casu_result));
59 hdrl_casu_result *res_p2 = cpl_calloc(1, sizeof(hdrl_casu_result));
60 cpl_test_eq(hdrl_casu_catalogue(NULL, NULL, NULL, 5, 1.5, 1, 3.5, 1, CELL_SIZE,
61 6, 1.0, 2.0, HDRL_SATURATION_INIT, res), CPL_ERROR_NULL_INPUT);
62 cpl_test_error(CPL_ERROR_NULL_INPUT);
63 cpl_test_null(res->catalogue);
64
65 cpl_image_delete(res->segmentation_map);
66 cpl_image_delete(res->background);
67 hdrl_casu_tfits_delete(res->catalogue);
68 cpl_free(res);
69
70 /* Generate a field with some stars and a confidence map */
71 cpl_image *bkg = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
72 cpl_image *im = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
73 cpl_image *cnf = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
74 cpl_image *aux = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
75
76 double sigma = 2.;
77 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
78 cpl_image_fill_noise_uniform(bkg, -10., 10.);
79
80 double sky = 500.;
81 cpl_image_add_scalar(bkg, sky);
82
83 cpl_image_fill_noise_uniform(cnf, 99.9, 100.1);
84 cpl_image *cnf_p1 = cpl_image_duplicate(cnf);
85 cpl_image *cnf_p2 = cpl_image_duplicate(cnf);
86
87 double tot[NTEST];
88 for (cpl_size i = 0; i < NTEST; i++) {
89 cpl_image_fill_gaussian(im, xpos[i], ypos[i], norm[i] * norm2, sigma, sigma);
90 tot[i] = cpl_image_get_flux(im);
91 cpl_image_add(bkg, im);
92 }
93 cpl_image_delete(im);
94
95 /* Add gradient */
96 double dlt_ramp = MAX_RAMP - MIN_RAMP;
97 for (cpl_size i = 1; i <= IMG_XSIZE; i++) {
98 for (cpl_size j = 1; j <= IMG_YSIZE; j++) {
99 cpl_image_set(aux, i, j, MIN_RAMP + dlt_ramp * (double)i / IMG_XSIZE);
100 }
101 }
102 cpl_image_add(bkg, aux);
103 cpl_image_delete(aux);
104
105 cpl_size y_os = 0;
106
107 /* Generation of confidence map "Patch Close" */
108 cpl_size x_os = X_OS_P1;
109 for (cpl_size i = 0; i <= PATCH_SIZE; i++) {
110 for (cpl_size j = 0; j <= PATCH_SIZE; j++) {
111 cpl_image_set(cnf_p1, xpos[STAR]-(PATCH_SIZE/2)+i +x_os,
112 ypos[STAR]-(PATCH_SIZE/2)+j +y_os, 0);
113 }
114 }
115
116 /* Generation of confidence map "Patch Away" */
117 x_os = X_OS_P2;
118 for (cpl_size i = 0; i <= PATCH_SIZE; i++) {
119 for (cpl_size j = 0; j <= PATCH_SIZE; j++) {
120 cpl_image_set(cnf_p2, xpos[STAR]-(PATCH_SIZE/2)+i +x_os,
121 ypos[STAR]-(PATCH_SIZE/2)+j +y_os, 0);
122 }
123 }
124
125 hdrl_casu_fits *inf = hdrl_casu_fits_wrap( bkg);
126 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap( cnf);
127 hdrl_casu_fits *inconf_p1 = hdrl_casu_fits_wrap(cnf_p1);
128 hdrl_casu_fits *inconf_p2 = hdrl_casu_fits_wrap(cnf_p2);
129
130 /* Give it a WCS */
131 cpl_propertylist *pl = hdrl_casu_fits_get_ehu(inf);
132 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN" );
133 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN" );
134 cpl_propertylist_update_double(pl, "CRVAL1", 30. );
135 cpl_propertylist_update_double(pl, "CRVAL2", 12. );
136 cpl_propertylist_update_double(pl, "CRPIX1", 512. );
137 cpl_propertylist_update_double(pl, "CRPIX2", 512. );
138 cpl_propertylist_update_double(pl, "CD1_1", -1. / 3600.);
139 cpl_propertylist_update_double(pl, "CD1_2", 0. );
140 cpl_propertylist_update_double(pl, "CD2_1", 0. );
141 cpl_propertylist_update_double(pl, "CD2_2", 1. / 3600.);
142 cpl_propertylist_update_int( pl, "NAXIS1", IMG_XSIZE );
143 cpl_propertylist_update_int( pl, "NAXIS2", IMG_YSIZE );
144 cpl_wcs *wcs = cpl_wcs_new_from_propertylist(pl);
145
146 /* Run casu catalogue */
147 res = cpl_calloc(1, sizeof(hdrl_casu_result));
148 cpl_test_eq(hdrl_casu_catalogue(inf, inconf, wcs, 5, 1.5, 0, 5., 1,
149 CELL_SIZE, HDRL_CATALOGUE_ALL, 3., 1., HDRL_SATURATION_INIT, res), CPL_ERROR_NONE);
150 cpl_test_nonnull(res->catalogue);
151 hdrl_casu_fits_delete(inconf);
152
153 /* Run casu catalogue for PC */
154 cpl_test_eq(hdrl_casu_catalogue(inf, inconf_p1, wcs, 5, 1.5, 0, 5., 1,
155 CELL_SIZE, HDRL_CATALOGUE_ALL, 3., 1., HDRL_SATURATION_INIT, res_p1), CPL_ERROR_NONE);
156 cpl_test_nonnull(res_p1->catalogue);
157 hdrl_casu_fits_delete(inconf_p1);
158
159 /* Run casu catalogue for PA*/
160 cpl_test_eq(hdrl_casu_catalogue(inf, inconf_p2, wcs, 5, 1.5, 0, 5., 1,
161 CELL_SIZE, HDRL_CATALOGUE_ALL, 3., 1., HDRL_SATURATION_INIT, res_p2), CPL_ERROR_NONE);
162 cpl_test_nonnull(res_p2->catalogue);
163 hdrl_casu_fits_delete(inconf_p2);
165 cpl_wcs_delete(wcs);
166
167 /*** Tests ***/
168
169 cpl_test_image_rel(res->background, res_p1->background, COMP_TOLERANCE);
170 cpl_test_image_rel(res->background, res_p2->background, COMP_TOLERANCE);
171
172 cpl_size corner_x = cpl_image_get_size_x(res->background) -CORNER_OFFSET;
173 cpl_size corner_y = cpl_image_get_size_y(res->background) -CORNER_OFFSET;
174
175 int rej;
176 cpl_test_abs(cpl_image_get(res ->background, corner_x, corner_y, &rej),
177 cpl_image_get(res_p1->background, corner_x, corner_y, &rej), CORNER_REL_TOL);
178 cpl_test_abs(cpl_image_get(res ->background, corner_x, corner_y, &rej),
179 cpl_image_get(res_p2->background, corner_x, corner_y, &rej), CORNER_REL_TOL);
180
181 cpl_test_image_rel(res->segmentation_map, res_p1->segmentation_map, COMP_TOLERANCE);
182 cpl_test_image_rel(res->segmentation_map, res_p2->segmentation_map, COMP_TOLERANCE);
183
184
185 /* Check the results. Start by checking the number of rows and columns. Sort the table by X */
186 cpl_table *tab = hdrl_casu_tfits_get_table(res ->catalogue);
187 cpl_table *tab_p1 = hdrl_casu_tfits_get_table(res_p1->catalogue);
188 cpl_table *tab_p2 = hdrl_casu_tfits_get_table(res_p2->catalogue);
189 cpl_test_nonnull(tab);
190 cpl_test_nonnull(tab_p1);
191 cpl_test_nonnull(tab_p2);
192
193 int nl;
194 for (cpl_size i = 0; i < NTEST; i++) {
195 cpl_test_rel( cpl_table_get_double(tab, APER_FLUX_NUM, i, &nl),
196 cpl_table_get_double(tab_p1, APER_FLUX_NUM, i, &nl), COMP_TOLERANCE );
197 cpl_test_rel( cpl_table_get_double(tab, APER_FLUX_NUM, i, &nl),
198 cpl_table_get_double(tab_p2, APER_FLUX_NUM, i, &nl), COMP_TOLERANCE );
199 }
200
201 cpl_test_eq(cpl_table_get_ncol(tab), 63);
202 cpl_test_eq(cpl_table_get_nrow(tab), NTEST);
203 pl = cpl_propertylist_new();
204 cpl_propertylist_append_bool(pl, "X_coordinate", 0);
205 cpl_table_sort(tab, pl);
206 cpl_propertylist_delete(pl);
207
208 /* Test the column content of the table */
209 for (cpl_size i = 0; i < NTEST; i++) {
210 cpl_test_abs(xpos[i], cpl_table_get_double(tab, "X_coordinate", i, &nl), 0.2);
211 cpl_test_abs(ypos[i], cpl_table_get_double(tab, "Y_coordinate", i, &nl), 0.2);
212 double diff = fabs( cpl_table_get_double(tab, "Aper_flux_5", i, &nl) -tot[i])
213 / cpl_table_get_double(tab, "Aper_flux_5_err", i, &nl);
214 cpl_test_lt(diff, 1.5);
215 cpl_test_eq( cpl_table_get_double(tab, "Classification", i, &nl), -1.);
216 }
217
218 /* Clean up */
219 cpl_image_delete( res->segmentation_map);
220 cpl_image_delete( res->background);
221 hdrl_casu_tfits_delete(res->catalogue);
222 cpl_free(res);
223
224 cpl_image_delete( res_p1->segmentation_map);
225 cpl_image_delete( res_p1->background);
226 hdrl_casu_tfits_delete(res_p1->catalogue);
227 cpl_free(res_p1);
228
229 cpl_image_delete( res_p2->segmentation_map);
230 cpl_image_delete( res_p2->background);
231 hdrl_casu_tfits_delete(res_p2->catalogue);
232 cpl_free(res_p2);
233
234
235 return cpl_error_get_code();
236}
237
238/*----------------------------------------------------------------------------*/
242/*----------------------------------------------------------------------------*/
243int main(void)
244{
245 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
246
247 hdrl_casubkg_badpatch_compute();
248
249 cpl_test_error(CPL_ERROR_NONE);
250
251 return cpl_test_end(0);
252}
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.