CR2RE Pipeline Reference Manual 1.6.10
hdrl_cat_overlp-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_apio.h"
23#include "../hdrl_cat_areals.h"
24#include "../hdrl_cat_conf.h"
25#include "../hdrl_cat_moments.h"
26#include "../hdrl_cat_overlp.h"
27
28
29#define NTEST 10
30#define NT 117
31#define SIZE_IMAGE 512
32#define CENTER_IMAGE 256
33
34
35void standard_test(void)
36{
37 /* Intitialize */
38 double xpos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
39 double ypos[] = { 100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.};
40 double norm[] = {1000., 100., 200., 500., 550., 600., 650., 700., 750., 800.};
41
42 /* Create input data */
43 cpl_image *im = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
44 cpl_image *bkg = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
45 cpl_image *cnf = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
46
47 cpl_image_fill_noise_uniform(bkg, -10., 10. );
48 cpl_image_fill_noise_uniform(cnf, 99.9, 100.1);
49
50 hdrl_casu_fits *infErr = hdrl_casu_fits_wrap( im);
51 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap(cnf);
52 hdrl_casu_fits *inf = hdrl_casu_fits_wrap(bkg);
53
54 double sigma = 2.;
55 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
56
57 double sky = 500.;
58 cpl_image_add_scalar(bkg, sky);
59
60 for (cpl_size i = 0; i < NTEST; i++) {
61 cpl_image_fill_gaussian(im, xpos[i], ypos[i], norm[i] * norm2, sigma, sigma);
62 cpl_image_add(bkg, im);
63 }
64
65 /*** Tests ***/
66 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
67 cpl_size ipix;
68 cpl_size icrowd;
69
70 /* Test 1: Error input null */
71 ipix = 5;
72 icrowd = 0;
73 cpl_test_eq(hdrl_catalogue_conf(infErr, NULL, ipix, 2.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
74 cpl_test_nonnull( res->catalogue);
75 hdrl_casu_tfits_delete(res->catalogue);
76 cpl_image_delete( res->segmentation_map);
77 cpl_image_delete( res->background);
78
79 /* Test 2 */
80 ipix = 5;
81 icrowd = 0;
82 cpl_test_eq(hdrl_catalogue_conf(infErr, inconf, ipix, 2.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
83 cpl_test_nonnull( res->catalogue);
84 hdrl_casu_tfits_delete(res->catalogue);
85 cpl_image_delete( res->segmentation_map);
86 cpl_image_delete( res->background);
87
88 /* Test 3 */
89 ipix = 10;
90 icrowd = 10;
91 cpl_test_eq(hdrl_catalogue_conf(infErr, NULL, ipix, 2.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
92 cpl_test_nonnull( res->catalogue);
93 hdrl_casu_tfits_delete(res->catalogue);
94 cpl_image_delete( res->segmentation_map);
95 cpl_image_delete( res->background);
96
97 /* Test 4 */
98 ipix = 10;
99 icrowd = 10;
100 cpl_test_eq(hdrl_catalogue_conf(infErr, inconf, ipix, 2.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
101 cpl_test_nonnull( res->catalogue);
102 hdrl_casu_tfits_delete(res->catalogue);
103 cpl_image_delete( res->segmentation_map);
104 cpl_image_delete( res->background);
105
106 /* Test 5 */
107 ipix = 0;
108 icrowd = 5;
109 cpl_test_eq(hdrl_catalogue_conf(inf, NULL, ipix, 1.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
110 cpl_test_nonnull( res->catalogue);
111 hdrl_casu_tfits_delete(res->catalogue);
112 cpl_image_delete( res->segmentation_map);
113 cpl_image_delete( res->background);
114
115 /* Test 6 */
116 ipix = 0;
117 icrowd = 5;
118 cpl_test_eq(hdrl_catalogue_conf(inf, inconf, ipix, 1.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
119 cpl_test_nonnull( res->catalogue);
120 hdrl_casu_tfits_delete(res->catalogue);
121 cpl_image_delete( res->segmentation_map);
122 cpl_image_delete( res->background);
123
124 /* Test 7 */
125 ipix = 10;
126 icrowd = 10;
127 cpl_test_eq(hdrl_catalogue_conf(inf, NULL, ipix, 1.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
128 cpl_test_nonnull( res->catalogue);
129 hdrl_casu_tfits_delete(res->catalogue);
130 cpl_image_delete( res->segmentation_map);
131 cpl_image_delete( res->background);
132
133 /* Test 8 */
134 ipix = 10;
135 icrowd = 10;
136 cpl_test_eq(hdrl_catalogue_conf(inf, inconf, ipix, 1.5, icrowd, 5., 1, 64, 6, 3., 1., INFINITY, res), CPL_ERROR_NONE);
137 cpl_test_nonnull( res->catalogue);
138 hdrl_casu_tfits_delete(res->catalogue);
139 cpl_image_delete( res->segmentation_map);
140 cpl_image_delete( res->background);
141
142 /* Clean up */
143 hdrl_casu_fits_delete(infErr);
144 hdrl_casu_fits_delete(inconf);
146 cpl_free(res);
147}
148
149void advanced_test(void)
150{
151 cpl_size x[] = {398,399,400,397,398,399,400,401,402,403,396,397,398,399,400,
152 401,402,403,404,395,396,397,398,399,400,401,402,403,404,405,
153 395,396,397,398,399,400,401,402,403,404,405,395,396,397,398,
154 399,400,401,402,403,404,405,395,396,397,398,399,400,401,402,
155 403,404,405,406,395,396,397,398,399,400,401,402,403,404,405,
156 395,396,397,398,399,400,401,402,403,404,405,395,396,397,398,
157 399,400,401,402,403,404,405,396,397,398,399,400,401,402,403,
158 404,397,398,399,400,401,402,403,398,399,400,401};
159
160 cpl_size y[] = {394,394,394,395,395,395,395,395,395,395,396,396,396,396,396,
161 396,396,396,396,397,397,397,397,397,397,397,397,397,397,397,
162 398,398,398,398,398,398,398,398,398,398,398,399,399,399,399,
163 399,399,399,399,399,399,399,400,400,400,400,400,400,400,400,
164 400,400,400,400,401,401,401,401,401,401,401,401,401,401,401,
165 402,402,402,402,402,402,402,402,402,402,402,403,403,403,403,
166 403,403,403,403,403,403,403,404,404,404,404,404,404,404,404,
167 404,405,405,405,405,405,405,405,406,406,406,406};
168
169 double z[] = {8.87152,12.515,7.69699,10.8527,22.2509,21.7368,13.0388,
170 12.1853,17.1976,7.43948,15.2245,29.1964,37.9117,57.9371,
171 71.5542,57.1288,34.7726,15.5934,11.5374,15.995,21.3606,
172 60.4006,103.46,147.55,168.274,147.476,98.9157,51.7186,20.188,
173 3.04248,5.77832,49.3103,98.2057,187.557,268.353,310.638,
174 274.295,183.969,94.6933,47.9889,20.245,26.3758,59.1781,
175 152.389,275.916,395.107,450.251,397.53,272.322,147.053,54.767,
176 11.8971,13.3888,73.3689,165.899,298.455,449.707,493.25,441.585,
177 299.31,157.474,70.1224,15.5313,8.76074,20.7188,54.5798,141.249,
178 264.87,382.736,435.452,393.871,268.175,138.485,65.9307,28.7812,
179 19.379,36.6449,93.5458,186.823,270.95,305.093,260.879,183.683,
180 100.676,32.6281,16.6497,5.94965,17.8105,57.256,106.32,145.264,
181 164.271,137.093,88.9384,60.7841,31.8582,10.0435,4.69162,
182 15.2187,32.5385,61.0381,74.5399,67.3727,43.3964,25.0956,
183 16.7595,-0.37323,21.3832,19.2497,18.5883,9.37448,19.6048,
184 11.5006,13.0159,14.5852,13.66,-1.04889};
185
186
187 /* Set up apm structure */
188 ap_t ap;
189 ap.lsiz = SIZE_IMAGE;
190 ap.csiz = SIZE_IMAGE;
191 ap.thresh = 11.0936;
192 ap.inframe = cpl_image_new(SIZE_IMAGE, SIZE_IMAGE, CPL_TYPE_DOUBLE);
193 ap.conframe = cpl_image_new(SIZE_IMAGE, SIZE_IMAGE, CPL_TYPE_DOUBLE);
194
195
196 /* Initialize */
197 hdrl_apinit(&ap);
198
199 ap.npl_pix = NT;
200 ap.plarray = cpl_realloc(ap.plarray, NT * sizeof(*ap.plarray));
201 for (cpl_size i = 0; i < NT; i++) {
202 ap.plarray[i].x = x[i];
203 ap.plarray[i].y = y[i];
204 ap.plarray[i].z = z[i];
205 ap.plarray[i].zsm = z[i];
206 }
207
208 ap.xintmin = 0.;
209 ap.areal_offset = 3.47165;
210 ap.thresh = 11.0936;
211 ap.fconst = 1.4427;
212
213 /* Run the test */
214 cpl_size iareal[NAREAL];
215 hdrl_areals(&ap, iareal);
216
217 ap.indata = cpl_image_get_data_double(ap.inframe);
218 ap.confdata = cpl_image_get_data_double(ap.conframe);
219 ap.mflag = cpl_calloc(2048 * 2048, sizeof(*ap.mflag));
220
221 /* Create a source */
222 double tmax = 1000.;
223 cpl_image_fill_gaussian(ap.inframe, CENTER_IMAGE, CENTER_IMAGE, tmax, 10., 10.);
224 cpl_image_fill_gaussian(ap.conframe, CENTER_IMAGE, CENTER_IMAGE, tmax, 100., 100.);
225
226 /* Do a basic moments analysis and work out the areal profiles*/
227 double momresults[8];
228 hdrl_moments(&ap, momresults);
229
230 double parmall[IMNUM][NPAR];
231 for (cpl_size i = 0; i < IMNUM; i++) {
232 for (cpl_size j = 0; j < NPAR; j++) {
233 parmall[i][j] = 0.;
234 }
235 }
236
237
238 /*** TESTS ***/
239 cpl_size nbit;
240 cpl_error_code e;
241
242 /* NOTE: Only if (iareal[0] >= ap->mulpix && ap->icrowd) */
243
244 /* Test 1 */
245 nbit = 1;
246 ap.ipnop = 1;
247 e = hdrl_overlp( &ap, parmall, &nbit,
248 momresults[1], momresults[2], momresults[3],
249 iareal[0], momresults[7]);
250 cpl_test_eq(e, CPL_ERROR_NONE);
251
252
253 /* Test 2 */
254 nbit = 1;
255 ap.ipnop = 1024;
256 e = hdrl_overlp( &ap, parmall, &nbit,
257 CENTER_IMAGE, CENTER_IMAGE, momresults[3],
258 SIZE_IMAGE * SIZE_IMAGE, tmax);
259 cpl_test_eq(e, CPL_ERROR_NONE);
260
261
262 nbit = 2;
263 ap.ipnop = 2;
264 ap.areal_offset = 1.5;
265 ap.thresh = 15.;
266 ap.fconst = 0.5;
267 hdrl_areals(&ap, iareal);
268 for (cpl_size i = 0; i < IMNUM; i++) {
269 for (cpl_size j = 0; j < NPAR; j++) {
270 parmall[i][j] = tmax;
271 }
272 }
273
274 /* Test 3 */
275 e = hdrl_overlp( &ap, parmall, &nbit,
276 CENTER_IMAGE, CENTER_IMAGE, momresults[3],
277 SIZE_IMAGE * SIZE_IMAGE, tmax);
278 cpl_test_eq(e, CPL_ERROR_NONE);
279
280 /* Test 4 */
281 e = hdrl_overlp_2orMore( &ap, parmall, &nbit,
282 CENTER_IMAGE, CENTER_IMAGE, momresults[3],
283 SIZE_IMAGE * SIZE_IMAGE, 1, 1, 1);
284 cpl_test_eq(e, CPL_ERROR_NONE);
285
286
287 /* Clean up */
288 hdrl_apclose(&ap);
289 cpl_free(ap.mflag);
290 cpl_image_delete(ap.inframe);
291 cpl_image_delete(ap.conframe);
292}
293
294
295int main(void)
296{
297 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
298
299 standard_test();
300 advanced_test();
301
302 return cpl_test_end(0);
303}
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.
void hdrl_apinit(ap_t *ap)
Initialize the ap structure.
Definition: hdrl_cat_apio.c:54
void hdrl_apclose(ap_t *ap)
Close ap structure.
void hdrl_areals(ap_t *ap, cpl_size iareal[NAREAL])
Work out the areal profiles for an object.
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.
void hdrl_moments(ap_t *ap, double results[])
Do moments analysis on an object in a Plessey array.
cpl_error_code hdrl_overlp(ap_t *ap, double parm[IMNUM][NPAR], cpl_size *nbit, double xbar, double ybar, double total, cpl_size npix, double tmax)
Deblend overlapping images.
cpl_error_code hdrl_overlp_2orMore(ap_t *ap, double parm[IMNUM][NPAR], cpl_size *nbit, double xbar, double ybar, double total, cpl_size npix, double curthr_prev, double nexthr_prev, double lasthr_prev)
Deblend overlapping images 2 or more. If you know that exist more than 1 (come to the previous versio...