CR2RE Pipeline Reference Manual 1.7.0
hdrl_cat_casu-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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <cpl_test.h>
25
26#include "../hdrl_cat_casu.h"
27#include "../hdrl_cat_table.h"
28
29
30#define NTEST 10
31
32
33int main(void)
34{
35 /* Initialise */
36 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
37
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 /* Check inherited status */
43 hdrl_casu_result *res = cpl_calloc(1, sizeof(hdrl_casu_result));
44 cpl_test_eq(hdrl_casu_catalogue(NULL, NULL, NULL, 5, 1.5, 1, 3.5, 1, 64, 6, 1., 2.,
45 HDRL_SATURATION_INIT, res), CPL_ERROR_NULL_INPUT);
46 cpl_test_error(CPL_ERROR_NULL_INPUT);
47 cpl_test_null( res->catalogue);
48 cpl_image_delete( res->segmentation_map);
49 cpl_image_delete( res->background);
50 hdrl_casu_tfits_delete(res->catalogue);
51 cpl_free(res);
52
53 /* tests tfits */
54 cpl_table *test_table = NULL;
55 cpl_propertylist *test_ehu = NULL;
56 hdrl_casu_tfits *test_tinf = hdrl_casu_tfits_wrap(test_table, test_ehu);
57 cpl_test_null(test_tinf);
58 test_table = cpl_table_new(10);
59 test_tinf = hdrl_casu_tfits_wrap(test_table, test_ehu);
60 cpl_test_nonnull(test_tinf);
61 hdrl_casu_tfits_delete(test_tinf);
62 test_table = cpl_table_new(10);
63 test_ehu = cpl_propertylist_new();
64 test_tinf = hdrl_casu_tfits_wrap(test_table, test_ehu);
65 cpl_test_nonnull(test_tinf);
66 hdrl_casu_tfits_delete(test_tinf);
67
68
69 /* Generate a field with some stars and a confidence map */
70 cpl_image *bkg = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
71 cpl_image *im = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
72 cpl_image *cnf = cpl_image_new(1024, 1024, CPL_TYPE_DOUBLE);
73
74 double sigma = 2.;
75 double norm2 = 2. * CPL_MATH_PI * sigma * sigma;
76 cpl_image_fill_noise_uniform(bkg, -10., 10.);
77
78 double sky = 500.;
79 cpl_image_add_scalar(bkg, sky);
80
81 cpl_image_fill_noise_uniform(cnf, 99.9, 100.1);
82
83 double tot[NTEST];
84 for (cpl_size i = 0; i < NTEST; i++) {
85 cpl_image_fill_gaussian(im, xpos[i], ypos[i], norm[i] * norm2, sigma, sigma);
86 tot[i] = cpl_image_get_flux(im);
87 cpl_image_add(bkg, im);
88 }
89 cpl_image_delete(im);
90
91 hdrl_casu_fits *inf = hdrl_casu_fits_wrap(bkg);
92 hdrl_casu_fits *inconf = hdrl_casu_fits_wrap(cnf);
93
94 /* Give it a WCS */
95 cpl_propertylist *pl = hdrl_casu_fits_get_ehu(inf);
96 cpl_propertylist_update_string(pl, "CTYPE1", "RA---TAN" );
97 cpl_propertylist_update_string(pl, "CTYPE2", "DEC--TAN" );
98 cpl_propertylist_update_double(pl, "CRVAL1", 30. );
99 cpl_propertylist_update_double(pl, "CRVAL2", 12. );
100 cpl_propertylist_update_double(pl, "CRPIX1", 512. );
101 cpl_propertylist_update_double(pl, "CRPIX2", 512. );
102 cpl_propertylist_update_double(pl, "CD1_1", -1. / 3600.);
103 cpl_propertylist_update_double(pl, "CD1_2", 0. );
104 cpl_propertylist_update_double(pl, "CD2_1", 0. );
105 cpl_propertylist_update_double(pl, "CD2_2", 1. / 3600.);
106 cpl_propertylist_update_int( pl, "NAXIS1", 1024 );
107 cpl_propertylist_update_int( pl, "NAXIS2", 1024 );
108 cpl_wcs *wcs = cpl_wcs_new_from_propertylist(pl);
109
110 /* Run casu catalogue */
111 res = cpl_calloc(1, sizeof(*res));
112 cpl_test_eq(hdrl_casu_catalogue(inf, inconf, wcs, 5, 1.5, 0, 5., 1, 64, 6, 3., 1.,
113 HDRL_SATURATION_INIT, res), CPL_ERROR_NONE);
114 cpl_test_nonnull(res->catalogue);
116 hdrl_casu_fits_delete(inconf);
117
118 cpl_wcs_delete(wcs);
119 cpl_image_delete(res->segmentation_map);
120 cpl_image_delete(res->background);
121
122 /* Check the results. Start by checking the number of rows and columns. Sort the table by X */
123 cpl_table *tab = hdrl_casu_tfits_get_table(res->catalogue);
124 cpl_test_nonnull(tab);
125 cpl_test_eq(cpl_table_get_ncol(tab), NCOLS);
126 cpl_test_eq(cpl_table_get_nrow(tab), NTEST);
127
128 pl = cpl_propertylist_new();
129 cpl_propertylist_append_bool(pl, "X_coordinate", 0);
130 cpl_table_sort(tab, pl);
131 cpl_propertylist_delete(pl);
132
133 /* Test the column content of the table */
134 int nl;
135 for (cpl_size i = 0; i < NTEST; i++) {
136
137 cpl_test_abs(xpos[i], cpl_table_get_double(tab, "X_coordinate", i, &nl), 0.2);
138 cpl_test_abs(ypos[i], cpl_table_get_double(tab, "Y_coordinate", i, &nl), 0.2);
139
140 double diff = fabs( cpl_table_get_double(tab, "Aper_flux_5", i, &nl) - tot[i])
141 / cpl_table_get_double(tab, "Aper_flux_5_err", i, &nl);
142 cpl_test_lt(diff, 1.6);
143 cpl_test_eq( cpl_table_get_double(tab, "Classification", i, &nl), -1.);
144 }
145
146 /* Compare some header info */
147 pl = hdrl_casu_tfits_get_ehu(res->catalogue);
148 cpl_test_rel( cpl_propertylist_get_double(pl, "ESO QC IMAGE_SIZE") / 2.355, sigma, 0.1);
149
150 /* Clean up*/
151 hdrl_casu_tfits_delete(res->catalogue);
152 cpl_free(res);
153
154 return cpl_test_end(0);
155}
hdrl_casu_fits * hdrl_casu_fits_wrap(cpl_image *im)
Wrap an image in a hdrl_casu_fits wrapper.
cpl_propertylist * hdrl_casu_tfits_get_ehu(hdrl_casu_tfits *p)
Get the propertylist for the extension header for a given hdrl_casu_tfits image.
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.
hdrl_casu_tfits * hdrl_casu_tfits_wrap(cpl_table *tab, cpl_propertylist *ehu)
Wrap an table in a hdrl_casu_tfits wrapper.
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.