CR2RE Pipeline Reference Manual 1.6.7
hdrl_maglim-test.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2016 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
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25/*-----------------------------------------------------------------------------
26 Includes
27 -----------------------------------------------------------------------------*/
28
29#include "hdrl_maglim.h"
30
31#include <cpl.h>
32#include <stdint.h>
33#include <math.h>
34
35
36
37/*----------------------------------------------------------------------------*/
42/*----------------------------------------------------------------------------*/
43
52static cpl_image*
53hdrl_matrix_to_image_create(const cpl_matrix * kernel,
54 const cpl_boolean normalise) {
55
56 cpl_ensure(kernel != NULL, CPL_ERROR_NULL_INPUT, NULL);
57 cpl_size sx = cpl_matrix_get_ncol(kernel);
58 cpl_size sy = cpl_matrix_get_nrow(kernel);
59
60 cpl_image* image = cpl_image_new(sx, sy, CPL_TYPE_DOUBLE);
61 double* pimage = cpl_image_get_data(image);
62 const double* pkernel = cpl_matrix_get_data_const(kernel);
63 cpl_size pix, piy;
64 for(cpl_size j = 0; j < sy; j++) {
65 piy = sx*j;
66 for(cpl_size i = 0; i < sx; i++) {
67 pix = piy + i;
68 pimage[pix] = pkernel[pix];
69 }
70 }
71 if(normalise) {
72 double sum = cpl_matrix_get_mean(kernel);
73 sum *= sx * sy;
74 cpl_image_divide_scalar(image, sum);
75 }
76 return image;
77
78}
79
80
81
82
83cpl_error_code
84test_hdrl_extend_image(void)
85{
86 cpl_image* ima;
87 cpl_image* ima1;
88 double* pinp;
89 const cpl_size border_nx = 3;
90 const cpl_size border_ny = 5;
91 hdrl_image_extend_method extend_method = HDRL_IMAGE_EXTEND_NEAREST;
92
93 cpl_size nx = 100;
94 cpl_size ny = 100;
95 ima = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
96 ima1 = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
97
98 for(cpl_size i = 1; i <= nx; i++) {
99 cpl_image_fill_window(ima1, i, 1, i, ny, i-1);
100 }
101 cpl_image_add(ima,ima1);
102
103 //cpl_image_save(ima, "ima1.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
104 for(cpl_size j = 1; j <= ny; j++) {
105 cpl_image_fill_window(ima1, 1, j, nx, j, (j-1) * 100);
106 }
107 //cpl_image_save(ima1, "ima2.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
108 cpl_image_add(ima,ima1);
109 //cpl_image_save(ima, "ima3.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
110 cpl_image_delete(ima1);
111
112 cpl_image* extended;
113
114 extended = hdrl_extend_image(NULL, border_nx, border_ny, extend_method);
115 cpl_test_error(CPL_ERROR_NULL_INPUT);
116 cpl_test_null(extended);
117
118 extended = hdrl_extend_image(ima, -1, border_ny, extend_method);
119 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
120 cpl_test_null(extended);
121
122 extended = hdrl_extend_image(ima, border_nx, -1, extend_method);
123 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
124 cpl_test_null(extended);
125
126 extended = hdrl_extend_image(ima, border_nx, border_ny, 3);
127 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
128 cpl_test_null(extended);
129
130 double* pext = cpl_image_get_data_double(ima);
131 for(cpl_size j = border_ny; j <= ny-border_ny; j++) {
132 for(cpl_size i = border_nx; i <= nx-border_nx; i++) {
133 cpl_test_abs(pext[j*nx+i], j*nx+i, HDRL_EPS_DATA);
134 }
135 }
136 extended = hdrl_extend_image(ima, border_nx, border_ny, extend_method);
137 cpl_test_error(CPL_ERROR_NONE);
138 cpl_test_nonnull(extended);
139
140 pext = cpl_image_get_data_double(extended);
141 pinp = cpl_image_get_data_double(ima);
142 /* the first (and last) border_nx X pixels and border_ny Y pixels of extended
143 * image have same value as 1st X pixel and Y pixel of input image
144 */
145 cpl_size sx = cpl_image_get_size_x(extended);
146 cpl_size sy = cpl_image_get_size_y(extended);
147 /* check central Y range */
148 for(cpl_size j = border_ny; j < sy-border_ny; j++) {
149 //cpl_msg_info(cpl_func,"j=%lld",j);
150 /* check early X range (i=0)*/
151 for(cpl_size i = 0; i < border_nx; i++) {
152 cpl_test_abs(pext[j*sx+i], pinp[(j-border_ny)*nx], HDRL_EPS_DATA);
153 }
154 // check end X range (i=nx-1)
155 for(cpl_size i = sx-border_nx; i < sx; i++) {
156 //cpl_msg_info(cpl_func,"i=%lld",i);
157 cpl_test_abs(pext[j*sx+i], pinp[(j-border_ny)*nx+(nx-1)], HDRL_EPS_DATA);
158 }
159 }
160
161
162 /* check early Y range */
163 for(cpl_size j = 0; j < border_ny; j++) {
164 // check central X range
165 for(cpl_size i = border_nx; i < sx-border_nx; i++) {
166 cpl_test_abs(pext[j*sx+i], pinp[0*nx+i-border_nx], HDRL_EPS_DATA);
167 } /* 0 is first j index of input image */
168 }
169
170
171 /* check end Y range */
172 for(cpl_size j = sy - border_ny; j < sy; j++) {
173 //cpl_msg_info(cpl_func,"j=%lld",j);
174 // check central X range
175 for(cpl_size i = border_nx; i < sx-border_nx; i++) {
176 //cpl_msg_info(cpl_func,"i=%lld",i);
177 cpl_test_abs(pext[j*sx+i], pinp[(ny-1)*nx+i-border_nx], HDRL_EPS_DATA);
178 } /* ny-1 is last j index of input image */
179 }
180
181 cpl_image_delete(extended);
182 cpl_image_delete(ima);
183 return cpl_error_get_code();
184}
185
186cpl_error_code
187test_hdrl_maglim_kernel_create(void)
188{
189 const cpl_size kernel_sx = 9;
190 const cpl_size kernel_sy = 9;
191 /* To simplify this unit test we choose
192 * fwhm_seeing = sigma_to_fwhm=sqrt(4*log(4))
193 * so that exponential: exp{-[(x^2+y^2)/2.0*(fwhm_seeing/sigma_to_fwhm)^2]}
194 * simplifies to exp[-(x^2+y^2)/2.0)]
195 * sqrt(4*log(4))=2,35482004503
196 */
197 const double fwhm_seeing = sqrt(4*log(4));
198 cpl_matrix* kernel_mat;
199 kernel_mat = hdrl_maglim_kernel_create(-1, kernel_sy, fwhm_seeing);
200 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
201 cpl_test_null(kernel_mat);
202
203 kernel_mat = hdrl_maglim_kernel_create(kernel_sx, -1, fwhm_seeing);
204 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
205 cpl_test_null(kernel_mat);
206
207 kernel_mat = hdrl_maglim_kernel_create(kernel_sx, kernel_sy, -1);
208 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
209 cpl_test_null(kernel_mat);
210
211 kernel_mat = hdrl_maglim_kernel_create(kernel_sx, kernel_sy, fwhm_seeing);
212 cpl_test_error(CPL_ERROR_NONE);
213 cpl_test_nonnull(kernel_mat);
214 double arg;
215 double x, y;
216
217 for(cpl_size j = 0; j < kernel_sy; j++) {
218 y = j - 0.5 * (kernel_sy-1);
219
220 for(cpl_size i = 0; i < kernel_sx; i++) {
221
222 x = i - 0.5 * (kernel_sx-1);
223 arg = (x * x + y * y) / 2;
224 cpl_test_abs(cpl_matrix_get(kernel_mat,j,i), exp(-arg), HDRL_EPS_DATA);
225
226 }
227 }
228
229 cpl_image*
230 kernel_ima = hdrl_matrix_to_image_create(kernel_mat, CPL_TRUE);
231 //cpl_image_save(kernel_ima, "kernel_ima.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
232 cpl_image_delete(kernel_ima);
233
234 cpl_matrix_delete(kernel_mat);
235 return cpl_error_get_code();
236}
237
238static cpl_image*
239test_util_crea_9x9_annular_image(void) {
240 /* We test with an image 9x9 with
241 * center at 100, then
242 * 1st annular at 90,
243 * 2nd annular at 80
244 */
245 cpl_image* image = cpl_image_new(9, 9, CPL_TYPE_DOUBLE);
246 cpl_image_add_scalar(image, 60);
247 cpl_image_fill_window(image, 2, 2, 8, 8, 70);
248 cpl_image_fill_window(image, 3, 3, 7, 7, 80);
249 cpl_image_fill_window(image, 4, 4, 6, 6, 90);
250 cpl_image_fill_window(image, 5, 5, 5, 5, 100);
251
252 return image;
253}
254cpl_error_code
255test_hdrl_image_convolve(void)
256{
257 cpl_image * input_image ;
258 cpl_matrix * kernel;
259 cpl_image * kernel_image;
260 hdrl_image_extend_method method = HDRL_IMAGE_EXTEND_MIRROR;
261 cpl_image* convolved;
262 const cpl_size kernel_sx = 9;
263 const cpl_size kernel_sy = 9;
264 const double fwhm_seeing = sqrt(4*log(4));
265 const int sx = 100;
266 const int sy = 100;
267
268 input_image = cpl_image_new(sx, sy, CPL_TYPE_DOUBLE);
269 cpl_image_add_scalar(input_image,1);
270
271 kernel = hdrl_maglim_kernel_create(kernel_sx, kernel_sy, fwhm_seeing);
272 kernel_image = hdrl_matrix_to_image_create(kernel, CPL_TRUE);
273 //cpl_image_save(kernel_image, "kernel_image.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
274 cpl_image_delete(kernel_image);
275
276 convolved = hdrl_image_convolve(NULL, kernel, method);
277 cpl_test_error(CPL_ERROR_NULL_INPUT);
278 cpl_test_null(convolved);
279
280 convolved = hdrl_image_convolve(input_image, NULL, method);
281 cpl_test_error(CPL_ERROR_NULL_INPUT);
282 cpl_test_null(convolved);
283
284 convolved = hdrl_image_convolve(input_image, kernel, 2);
285 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
286 cpl_test_null(convolved);
287
288 //cpl_image_save(input_image, "input_image.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
289 convolved = hdrl_image_convolve(input_image, kernel, method);
290 //cpl_image_save(convolved, "convolved.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
291 cpl_test_error(CPL_ERROR_NONE);
292 cpl_test_nonnull(convolved);
293 cpl_image_delete(input_image);
294 cpl_matrix_delete(kernel);
295 cpl_image_delete(convolved);
296
297 /*
298 * Kernel 3x3, central pixel 1.
299 */
300
301 input_image = test_util_crea_9x9_annular_image();
302 //cpl_image_save(input_image, "convolved.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
303
304 kernel = cpl_matrix_new(3, 3);
305 cpl_matrix_set(kernel, 1, 1, 1);
306 cpl_matrix_set(kernel, 1, 0, 0.7);
307 cpl_matrix_set(kernel, 0, 1, 0.7);
308 cpl_matrix_set(kernel, 2, 1, 0.7);
309 cpl_matrix_set(kernel, 1, 2, 0.7);
310 kernel_image = hdrl_matrix_to_image_create(kernel, CPL_FALSE);
311 //cpl_image_save(kernel_image, "kernel_image.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
312 cpl_image_delete(kernel_image);
313
314 convolved = hdrl_image_convolve(input_image, kernel, method);
315 //cpl_image_save(convolved, "convolved.fits", CPL_TYPE_DOUBLE, NULL, CPL_IO_DEFAULT);
316
317 int status = 0;
318 //cpl_msg_info(cpl_func,"value: %16.16g",cpl_image_get(convolved, 4, 4, &status));
319 /* test some values */
320 cpl_test_abs(cpl_image_get(convolved, 5, 5, &status),92.63157894736842, HDRL_EPS_DATA);
321 cpl_test_abs(cpl_image_get(convolved, 4, 5, &status),90.0, HDRL_EPS_DATA);
322 cpl_test_abs(cpl_image_get(convolved, 3, 5, &status),80.0, HDRL_EPS_DATA);
323 cpl_test_abs(cpl_image_get(convolved, 2, 5, &status),70.0, HDRL_EPS_DATA);
324 cpl_test_abs(cpl_image_get(convolved, 1, 5, &status),61.8421052631579, HDRL_EPS_DATA);
325 cpl_test_abs(cpl_image_get(convolved, 1, 1, &status),60.0, HDRL_EPS_DATA);
326 cpl_test_abs(cpl_image_get(convolved, 2, 2, &status),66.31578947368422, HDRL_EPS_DATA);
327 cpl_test_abs(cpl_image_get(convolved, 3, 3, &status),76.31578947368422, HDRL_EPS_DATA);
328 cpl_test_abs(cpl_image_get(convolved, 4, 4, &status),86.31578947368422, HDRL_EPS_DATA);
329 cpl_image_delete(convolved);
330 cpl_image_delete(input_image);
331 cpl_matrix_delete(kernel);
332 return cpl_error_get_code();
333}
334
335cpl_error_code
336test_hdrl_matrix_to_image_create(void)
337{
338 cpl_matrix* kernel;
339 cpl_image* image;
340 const cpl_size kernel_sx = 9;
341 const cpl_size kernel_sy = 9;
342 const double fwhm_seeing = sqrt(4*log(4));
343
344
345 kernel = hdrl_maglim_kernel_create(kernel_sx, kernel_sy, fwhm_seeing);
346
347 image = hdrl_matrix_to_image_create(NULL, CPL_TRUE);
348 cpl_test_error(CPL_ERROR_NULL_INPUT);
349 cpl_test_null(image);
350
351 image = hdrl_matrix_to_image_create(kernel, CPL_TRUE);
352 cpl_test_nonnull(image);
353
354 cpl_test_abs(cpl_image_get_size_x(image), kernel_sx, HDRL_EPS_DATA);
355 cpl_test_abs(cpl_image_get_size_y(image), kernel_sy, HDRL_EPS_DATA);
356
357 cpl_image_delete(image);
358 cpl_matrix_delete(kernel);
359 return cpl_error_get_code();
360}
361
362cpl_error_code
363test_hdrl_maglim_compute(void) {
364 cpl_image* image;
365 double zeropoint = 0;
366 double fwhm_seeing = sqrt(4*log(4));;
367 const cpl_size kernel_sx = 9;
368 const cpl_size kernel_sy = 9;
369 double limiting_magnitude = 0;
370
371 hdrl_image_extend_method method = HDRL_IMAGE_EXTEND_MIRROR;
372 double histo_min = 0.;
373 double histo_max = 0.;
374 double bin_size = 0.;
375 cpl_size error_niter = 0;
376 hdrl_mode_type mode_method = HDRL_MODE_MEDIAN;
377 hdrl_parameter * mode_parameter = hdrl_collapse_mode_parameter_create(histo_min,
378 histo_max,
379 bin_size,
380 mode_method,
381 error_niter);
382 /* testinvalid input */
383 hdrl_maglim_compute(NULL, zeropoint, fwhm_seeing, kernel_sx, kernel_sy,
384 method, mode_parameter, &limiting_magnitude);
385 cpl_test_error(CPL_ERROR_NULL_INPUT);
386
387 image = test_util_crea_9x9_annular_image();
388 hdrl_maglim_compute(image, zeropoint, -1, kernel_sx, kernel_sy,
389 method, mode_parameter, &limiting_magnitude);
390 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
391
392
393 hdrl_maglim_compute(image, zeropoint, fwhm_seeing, -1, kernel_sy,
394 method, mode_parameter, &limiting_magnitude);
395 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
396
397 hdrl_maglim_compute(image, zeropoint, fwhm_seeing, kernel_sx, -1,
398 method, mode_parameter, &limiting_magnitude);
399 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
400
401
402 hdrl_maglim_compute(image, zeropoint, fwhm_seeing, kernel_sx, kernel_sy,
403 -1, mode_parameter, &limiting_magnitude);
404 cpl_test_error(CPL_ERROR_ILLEGAL_INPUT);
405
406 hdrl_maglim_compute(image, zeropoint, fwhm_seeing, kernel_sx, kernel_sy,
407 method, NULL, &limiting_magnitude);
408 cpl_test_error(CPL_ERROR_INCOMPATIBLE_INPUT);
409
410
411 hdrl_maglim_compute(image, zeropoint, fwhm_seeing, kernel_sx, kernel_sy,
412 method, mode_parameter, &limiting_magnitude);
413
414
415 cpl_test_abs(limiting_magnitude,-5.591854160255954,HDRL_EPS_DATA);
416
417
418 cpl_image_delete(image);
419 hdrl_parameter_delete(mode_parameter);
420 return cpl_error_get_code();
421}
422
423
424/*----------------------------------------------------------------------------*/
428/*----------------------------------------------------------------------------*/
429int main(void)
430{
431 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_INFO);
432
433 test_hdrl_extend_image();
434 test_hdrl_maglim_kernel_create();
435 test_hdrl_image_convolve();
436 test_hdrl_matrix_to_image_create();
437 test_hdrl_maglim_compute();
438
439 return cpl_test_end(0);
440}
hdrl_parameter * hdrl_collapse_mode_parameter_create(double histo_min, double histo_max, double bin_size, hdrl_mode_type mode_method, cpl_size error_niter)
create a parameter object for the mode
cpl_error_code hdrl_maglim_compute(const cpl_image *image, const double zeropoint, const double fwhm, const cpl_size kernel_size_x, const cpl_size kernel_size_y, const hdrl_image_extend_method image_extend_method, const hdrl_parameter *mode_parameter, double *limiting_magnitude)
Computes the limiting magnitude of an image.
Definition: hdrl_maglim.c:114
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter