CR2RE Pipeline Reference Manual 1.6.10
hdrl_cat_sim_montecarlo-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#include "hdrl_random.h"
25
26
27#define COMP_TOL_REL 1. / 3.
28#define COMP_TOL_ABS 1e-2
29#define IMG_XSIZE 120
30#define IMG_YSIZE 180
31
32
33void test_basic(void)
34{
35 /* Generate a field with some stars and a confidence map */
36 cpl_image *bkg = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
37 cpl_image *im = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
38 cpl_image *cnf = cpl_image_new(IMG_XSIZE, IMG_YSIZE, CPL_TYPE_DOUBLE);
39
40 /* Generate confidence uniform confidence map */
41 cpl_image_add_scalar(cnf, 100.);
42
43 /* Generate sky and a gaussian source without errors */
44 double sigma = 2.;
45 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
46
47 double sky = 500.;
48 cpl_image_add_scalar(bkg, sky);
49
50 double xpos = 80.;
51 double ypos = 100.;
52 double norm = 3000.;
53 cpl_image_fill_gaussian(im, xpos, ypos, norm * norm2, sigma, sigma);
54 cpl_image_add(bkg, im);
55
56 hdrl_casu_fits *inf = hdrl_casu_fits_wrap( im);
57 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap(cnf);
58
59 /* Give it a WCS */
60 cpl_propertylist *pl = hdrl_casu_fits_get_ehu(inf);
61 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN" );
62 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN" );
63 cpl_propertylist_update_double(pl, "CRVAL1", 30. );
64 cpl_propertylist_update_double(pl, "CRVAL2", 12. );
65 cpl_propertylist_update_double(pl, "CRPIX1", 512. );
66 cpl_propertylist_update_double(pl, "CRPIX2", 512. );
67 cpl_propertylist_update_double(pl, "CD1_1", -1. / 3600.);
68 cpl_propertylist_update_double(pl, "CD1_2", 0. );
69 cpl_propertylist_update_double(pl, "CD2_1", 0. );
70 cpl_propertylist_update_double(pl, "CD2_2", 1. / 3600.);
71 cpl_propertylist_update_int( pl, "NAXIS1", IMG_XSIZE );
72 cpl_propertylist_update_int( pl, "NAXIS2", IMG_YSIZE );
73 cpl_wcs *wcs = cpl_wcs_new_from_propertylist(pl);
74
75 /* Perform the montecarlo simulation */
76 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
77 cpl_table *tabfinal = NULL;
78 for (cpl_size iterate = 0; iterate < 100; iterate++) {
79
80 /* Add realistic poisonnian noise */
81 hdrl_random_state * rng = hdrl_random_state_new(1, NULL);
82 cpl_size size = cpl_image_get_size_x(bkg) * cpl_image_get_size_y(bkg);
83 double *pim = cpl_image_get_data_double(im);
84 double *pbkg= cpl_image_get_data_double(bkg);
85
86 for (cpl_size i = 0; i < size; i++) {
87 pim[i] = hdrl_random_poisson(rng, pbkg[i]);
88 }
89 hdrl_random_state_delete(rng);
90
91 /* Run casu catalogue */
92 hdrl_casu_catalogue(inf, inconf, wcs, 5, 2.5, 0, 3., 1, 32,
93 HDRL_CATALOGUE_ALL, 3.0, 1.0, HDRL_SATURATION_INIT, res);
94
95 cpl_table *tab = hdrl_casu_tfits_get_table(res->catalogue);
96
97 if (iterate == 0) {
98 tabfinal = cpl_table_duplicate(tab);
99 } else {
100 cpl_table_insert(tabfinal, tab, iterate);
101 }
102
103 cpl_image_delete( res->segmentation_map);
104 cpl_image_delete( res->background);
105 hdrl_casu_tfits_delete(res->catalogue);
106 }
107
108 /* Do the checks */
109 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "X_coordinate_err"),
110 cpl_table_get_column_stdev(tabfinal, "X_coordinate" ), COMP_TOL_REL);
111 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Y_coordinate_err"),
112 cpl_table_get_column_stdev(tabfinal, "Y_coordinate" ), COMP_TOL_REL);
113 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Peak_height_err" ),
114 cpl_table_get_column_stdev(tabfinal, "Peak_height" ), COMP_TOL_REL);
115 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_1_err" ),
116 cpl_table_get_column_stdev(tabfinal, "Aper_flux_1" ), COMP_TOL_REL);
117 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_2_err" ),
118 cpl_table_get_column_stdev(tabfinal, "Aper_flux_2" ), COMP_TOL_REL);
119 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_3_err" ),
120 cpl_table_get_column_stdev(tabfinal, "Aper_flux_3" ), COMP_TOL_REL);
121 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_4_err" ),
122 cpl_table_get_column_stdev(tabfinal, "Aper_flux_4" ), COMP_TOL_REL);
123 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_5_err" ),
124 cpl_table_get_column_stdev(tabfinal, "Aper_flux_5" ), COMP_TOL_REL);
125 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_6_err" ),
126 cpl_table_get_column_stdev(tabfinal, "Aper_flux_6" ), COMP_TOL_REL);
127 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_7_err" ),
128 cpl_table_get_column_stdev(tabfinal, "Aper_flux_7" ), COMP_TOL_REL);
129 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_8_err" ),
130 cpl_table_get_column_stdev(tabfinal, "Aper_flux_8" ), COMP_TOL_REL);
131 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_9_err" ),
132 cpl_table_get_column_stdev(tabfinal, "Aper_flux_9" ), COMP_TOL_REL);
133 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_10_err"),
134 cpl_table_get_column_stdev(tabfinal, "Aper_flux_10" ), COMP_TOL_REL);
135 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_11_err"),
136 cpl_table_get_column_stdev(tabfinal, "Aper_flux_11" ), COMP_TOL_REL);
137 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_12_err"),
138 cpl_table_get_column_stdev(tabfinal, "Aper_flux_12" ), COMP_TOL_REL);
139 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Aper_flux_13_err"),
140 cpl_table_get_column_stdev(tabfinal, "Aper_flux_13" ), COMP_TOL_REL);
141 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Petr_flux_err" ),
142 cpl_table_get_column_stdev(tabfinal, "Petr_flux" ), COMP_TOL_REL);
143 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Kron_flux_err" ),
144 cpl_table_get_column_stdev(tabfinal, "Kron_flux" ), COMP_TOL_REL);
145 cpl_test_rel(cpl_table_get_column_mean( tabfinal, "Half_flux_err" ),
146 cpl_table_get_column_stdev(tabfinal, "Half_flux" ), COMP_TOL_REL);
147
148 /* Clean up */
150 hdrl_casu_fits_delete(inconf);
151 cpl_wcs_delete(wcs);
152 cpl_table_delete(tabfinal);
153 cpl_image_delete(bkg);
154 cpl_free(res);
155}
156
157/*----------------------------------------------------------------------------*/
161/*----------------------------------------------------------------------------*/
162int main(void)
163{
164 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
165
166 test_basic();
167
168 return cpl_test_end(0);
169}
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.