35 #include "irplib_strehl.h"
54 #define PIXSCALE 12.25e-3
60 static void irplib_strehl_test_one(
void);
61 static void irplib_psf_test_one(
int);
62 static void irplib_psf_test(
void);
63 static cpl_image * irplib_strehl_create(cpl_size, cpl_size, cpl_type,
64 double,
double,
double);
65 static void irplib_strehl_test(
const cpl_image *,
double,
double,
66 double,
double,
double);
68 static void irplib_strehl_test_fits(
const char *,
double,
double,
double);
74 int main (
int argc,
char * argv[])
79 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
81 irplib_strehl_test_one();
83 for (iarg = 1; iarg < argc; iarg++) {
84 irplib_strehl_test_fits(argv[iarg], PIXSCALE, CWLEN, DWLEN);
89 return cpl_test_end(0);
104 static void irplib_strehl_test_fits(
const char * file,
double pixscale,
105 double lam,
double dlam)
108 cpl_type type = CPL_TYPE_DOUBLE;
111 for (i = 0; i < 2; i++, type = CPL_TYPE_FLOAT) {
113 cpl_image * img = cpl_image_load(file, type, 0, 0);
115 cpl_test_error(CPL_ERROR_NONE);
116 cpl_test_nonnull(img);
118 cpl_msg_info(cpl_func,
"Testing %s with pixel scale=%g, lam=%g, dlam=%g",
119 file, pixscale, lam, dlam);
121 irplib_strehl_test(img, 0.0, 10.0, pixscale, lam, dlam);
123 cpl_image_delete(img);
135 static void irplib_psf_test(
void)
138 const int is_bench = cpl_msg_get_level() <= CPL_MSG_INFO
139 ? CPL_TRUE : CPL_FALSE;
142 const double tstart = cpl_test_get_cputime();
149 for (irep = 0; irep < (is_bench ? 3 : 1); irep++) {
151 for (isz = szstart; isz <= szstop; isz *= 2) {
152 irplib_psf_test_one(isz);
156 tstop = cpl_test_get_cputime() - tstart;
158 cpl_msg_info(cpl_func,
"Time to generate %d set(s) of PSFs up to size "
159 "%d X %d [s]: %g", irep, szstop, szstop, tstop);
169 static void irplib_psf_test_one(
int size)
173 CWLEN, DWLEN, PIXSCALE,
175 cpl_test_error(CPL_ERROR_NONE);
176 cpl_test_nonnull(imgpsf);
177 cpl_test_eq(cpl_image_get_size_x(imgpsf),
178 cpl_image_get_size_y(imgpsf));
180 cpl_test_eq(cpl_image_get_size_x(imgpsf), size);
182 cpl_image_delete(imgpsf);
192 static void irplib_strehl_test_one(
void)
194 cpl_type type = CPL_TYPE_DOUBLE;
197 for (i = 0; i < 2; i++, type = CPL_TYPE_FLOAT) {
199 cpl_image * img = irplib_strehl_create(IMAGESZ, IMAGESZ, type,
202 irplib_strehl_test(img, 1000.0, 1.0, 0.03,
205 cpl_image_delete(img);
223 static cpl_image * irplib_strehl_create(cpl_size nx, cpl_size ny,
230 const double noise = FLT_EPSILON;
232 cpl_image * im1 = cpl_image_new(nx, ny, type);
235 code = cpl_image_fill_gaussian(im1, nx/2, ny/2, norm, sig_x, sig_y);
236 cpl_test_eq_error(code, CPL_ERROR_NONE);
239 cpl_image * im0 = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
240 code = cpl_image_fill_noise_uniform(im0, -noise, noise);
241 cpl_test_eq_error(code, CPL_ERROR_NONE);
242 code = cpl_image_subtract(im1, im0);
243 cpl_test_eq_error(code, CPL_ERROR_NONE);
244 cpl_image_delete(im0);
247 code = cpl_image_get_maxpos(im1, &mx, &my);
248 cpl_test_eq_error(code, CPL_ERROR_NONE);
249 cpl_test_eq(mx, nx/2);
250 cpl_test_eq(my, ny/2);
268 static void irplib_strehl_test(
const cpl_image * im1,
double norm,
270 double pixscale,
double lam,
double dlam)
273 const cpl_size nx = cpl_image_get_size_x(im1);
274 const cpl_size ny = cpl_image_get_size_y(im1);
278 const double psigmas[] = {sigma, sigma/2.0, sigma/4.0};
279 const size_t nsigmas =
sizeof(psigmas)/
sizeof(*psigmas);
281 cpl_vector * sigmas = cpl_vector_wrap(nsigmas, (
double*)psigmas);
282 cpl_apertures * apert = NULL;
283 double fwhm_x, fwhm_y;
287 double strehl = 0.0, strehl_err = 0.0;
288 double star_bg = 0.0, star_peak = 0.0, star_flux = 0.0;
289 double psf_peak = 0.0, psf_flux = 0.0, bg_noise = 0.0;
295 code = cpl_image_get_maxpos(im1, &mx, &my);
296 cpl_test_eq_error(code, CPL_ERROR_NONE);
297 cpl_test_leq(mx - nx/4, mx);
298 cpl_test_leq(my - ny/4, my);
299 cpl_test_leq(mx, mx + nx/4);
300 cpl_test_leq(my, my + ny/4);
302 apert = cpl_apertures_extract_window(im1, sigmas,
303 mx - nx/4, my - ny/4,
304 mx + nx/4, my + ny/4,
306 cpl_test_error(CPL_ERROR_NONE);
307 cpl_test_nonnull(apert);
308 cpl_test_zero(isigma);
310 cpl_apertures_delete(apert);
311 cpl_test_eq_ptr(cpl_vector_unwrap(sigmas), psigmas);
313 cpl_test_lt(0.0, pixscale);
315 code = cpl_image_get_fwhm(im1, mx, my, &fwhm_x, &fwhm_y);
316 cpl_test_eq_error(code, CPL_ERROR_NONE);
318 cpl_msg_info(cpl_func,
"Expected star-radius vs. actual FWHM [pixel]: "
319 "%g <=> (%g, %g)", star_radius / pixscale,
322 cpl_msg_debug(cpl_func,
"Inner and outer radius of ring-zone for noise "
323 "estimate [pixel]: %g < %g", background_1 / pixscale,
324 background_2 / pixscale);
330 cpl_test_error(CPL_ERROR_NONE);
331 cpl_test_nonnull(imgpsf);
332 cpl_test_eq(cpl_image_get_size_x(imgpsf),
333 cpl_image_get_size_y(imgpsf));
335 cpl_test_eq(cpl_image_get_size_x(imgpsf),
338 cpl_image_delete(imgpsf);
356 cpl_test_eq_error(code, CPL_ERROR_NONE);
357 cpl_test_leq(0.0, strehl);
358 cpl_test_leq(strehl, 1.0);
359 cpl_test_leq(0.0, strehl_err);
361 cpl_test_rel(norm, star_flux, 0.01);
364 cpl_msg_info(cpl_func,
"Strehl: ratio=%g, error=%g; Background: flux=%g, "
365 "noise=%g; Star: peak=%g, flux=%g; PSF: peak=%g, flux=%g",
366 strehl, strehl_err, star_bg, bg_noise, star_peak, star_flux,
388 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
408 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
428 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
447 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
466 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
485 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
504 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
523 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
542 cpl_test_eq_error(code, CPL_ERROR_ILLEGAL_INPUT);
561 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
580 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
599 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
618 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
637 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
656 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
676 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);