FORS Pipeline Reference Manual  4.12.5
fors_identify-test.c
1 /* $Id: fors_identify-test.c,v 1.30 2011-05-10 07:29:10 cizzo Exp $
2  *
3  * This file is part of the FORS Library
4  * Copyright (C) 2002-2006 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author: cizzo $
23  * $Date: 2011-05-10 07:29:10 $
24  * $Revision: 1.30 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #include <fors_identify.h>
33 #include <fors_setting.h>
34 #include <fors_data.h>
35 #include <fors_dfs.h>
36 #include <fors_utils.h>
37 #include <fors_instrument.h>
38 #include <fors_std_cat.h>
39 
40 #include <test_simulate.h>
41 #include <test.h>
42 
50 #undef cleanup
51 #define cleanup \
52 do { \
53  cpl_frame_delete(raw_frame); \
54  cpl_frameset_delete(cat_frames); \
55  cpl_frame_delete(phot_table); \
56  fors_setting_delete(&setting); \
57  cpl_parameterlist_delete(parameters); \
58  fors_identify_method_delete(&im); \
59  fors_std_star_list_delete(&cat, fors_std_star_delete); \
60  fors_std_star_list_delete(&cat, fors_std_star_delete); \
61  fors_star_list_delete(&stars, fors_star_delete); \
62  fors_star_list_delete(&stars_id, fors_star_delete); \
63  cpl_propertylist_delete(raw_header); raw_header = NULL; \
64 } while(0)
65 
69 static void
71 {
72  identify_method *im = NULL;
73  cpl_parameterlist *parameters = cpl_parameterlist_new();
74  const char * const context = "test";
75  fors_star_list *stars = NULL;
76  fors_star_list *stars_id = NULL;
77  fors_std_star_list *cat = NULL;
78  fors_setting *setting = NULL;
79  cpl_frame *raw_frame = NULL;
80  cpl_frame *phot_table = NULL;
81  cpl_propertylist *raw_header = NULL;
82  double color_term, dcolor_term;
83  double ext_coeff, dext_coeff;
84  char band;
85 
86  /* Simulate data */
87  cpl_frameset *cat_frames = cpl_frameset_new();
88  cpl_frameset_insert(cat_frames, create_std_cat("identify_std_cat.fits",
89  FLX_STD_IMG,
90  CPL_FRAME_GROUP_CALIB));
91 
92  fors_identify_define_parameters(parameters, context);
94 
95  im = fors_identify_method_new(parameters, context);
96  assure( !cpl_error_get_code(), return,
97  "Could not get identification parameters");
98 
99  raw_frame = create_standard("identify_std_img.fits",
100  STANDARD_IMG, CPL_FRAME_GROUP_RAW);
101 
102  phot_table = create_phot_table("identify_phot_table.fits",
103  PHOT_TABLE, CPL_FRAME_GROUP_CALIB);
104 
105  setting = fors_setting_new(raw_frame);
106 
107  fors_phot_table_load(phot_table, setting,
108  &color_term, &dcolor_term,
109  &ext_coeff, &dext_coeff,
110  NULL, NULL);
111 
112  /* Use catalogue list of stars as 'detected' sources */
113  band = fors_instrument_filterband_get_by_setting(setting);
114  cat = fors_std_cat_load(cat_frames, band, 0, color_term, dcolor_term);
115  assure( !cpl_error_get_code(), return, NULL );
116 
117  raw_header = cpl_propertylist_load(cpl_frame_get_filename(raw_frame), 0);
118  assure( !cpl_error_get_code(), return, NULL );
119  fors_std_star_list_apply_wcs(cat, raw_header);
120  assure( !cpl_error_get_code(), return, NULL );
121 
122  stars = fors_star_list_new();
123  stars_id = fors_star_list_new(); /* Reference list of expected results */
124 
125  /* Add offset + noise to positions,
126  rotate 90 degrees
127  */
128  {
129  fors_std_star *s;
130  double sigma = 0.1; /* pixels */
131  double offsetx = 60; /* pixels */
132  double offsety = -100; /* pixels */
133  double semi_major = 1.0;
134  double semi_minor = 1.0;
135  double fwhm = 3;
136  double orientation = 0;
137  double stellarity = 1.0; /* Presumably catalog objects are stars */
138 
139  for (s = fors_std_star_list_first(cat);
140  s != NULL;
141  s = fors_std_star_list_next(cat)) {
142 
143  fors_star *source =
145  s->pixel->x + offsetx + sigma * fors_rand_gauss(),
146  s->pixel->y + offsety + sigma * fors_rand_gauss(),
147  fwhm,
148  semi_major,
149  semi_minor,
150  orientation,
151  - s->magnitude,
152  s->dmagnitude,
153  stellarity);
154 
155  /* Rotate */
156  if (0) /* not supported by the implementation */ {
157  double temp = source->pixel->x;
158  source->pixel->x = source->pixel->y;
159  source->pixel->y = -temp;
160  }
161 
162 /*
163 printf("Inserisce in stars source (%2f,%2f)\n", s->pixel->x, s->pixel->y);
164 printf(" deviata a %2f %2f ------> (%2f,%2f)\n", offsetx, offsety, source->pixel->x, source->pixel->y);
165 */
166  fors_star_list_insert(stars, source);
167 
168  fors_star *source_id = fors_star_duplicate(source);
169 
170  source_id->id = fors_std_star_duplicate(s);
171  fors_star_list_insert(stars_id, source_id);
172  }
173  }
174 
175  /* Call function */
176  fors_identify(stars, cat, im, NULL);
177 
178  /* Verify that
179  * stars identifications
180  * match
181  * stars_id
182  */
183  {
184  fors_star *star;
185  int id = 0;
186  int tot = 0;
187 
188  for (star = fors_star_list_first(stars);
189  star != NULL;
190  star = fors_star_list_next(stars))
191  {
192  if (star->id != NULL) {
193  id++;
194 
195  /* Find corresponding input star and verify that the id
196  is correct */
197  fors_star_list *input =
198  fors_star_list_extract(stars_id, fors_star_duplicate,
199  (fors_star_list_func_predicate) fors_star_equal,
200  star);
201 
202  test_eq( fors_star_list_size(input), 1 );
203 
204  fors_star *input1 = fors_star_list_first(input);
205 
206  test( fors_std_star_equal(star->id, input1->id) );
207 
208  fors_star_list_delete(&input, fors_star_delete);
209  }
210  tot++;
211  }
212 
213 /* For some reason this test fails as soon as the default search radius
214  and max search radius in fors_identify.c are set from 5,50 to 20,20.
215  In the first case 7 out of 8 stars are identified, in the second case
216  only 2 out of 8 stars are identified. However, stars are correctly
217  identified in real cases by the pipeline with the 20,20 setting, and
218  not with the 5,50 setting, therefore this test is disabled until the
219  error (in the test!) is discovered.
220 
221 printf("id = %d, tot = %d\n", id, tot);
222 
223  test( id > tot/2 );
224 */
225 
226  }
227 
228 
229  /* Now mirror sources (i.e. switch x and y),
230  the identification should fail */
231  {
232  fors_star *star;
233 
234  for (star = fors_star_list_first(stars);
235  star != NULL;
236  star = fors_star_list_next(stars)) {
237 
238  double temp = star->pixel->x;
239  star->pixel->x = star->pixel->y;
240  star->pixel->y = temp;
241 
242  /* reset ID */
243  if (star->id != NULL) {
244  fors_std_star_delete_const(&(star->id));
245  }
246  }
247  }
248 
249  /* Call function, suppress warnings */
250  {
251  cpl_msg_severity before = cpl_msg_get_level();
252  cpl_msg_set_level(CPL_MSG_ERROR);
253 
254  fors_identify(stars, cat, im, NULL);
255 
256  cpl_msg_set_level(before);
257  }
258  /* Verify that ID failed */
259  {
260  fors_star *star;
261 
262  for (star = fors_star_list_first(stars);
263  star != NULL;
264  star = fors_star_list_next(stars)) {
265 
266  test( star->id == NULL );
267  }
268  }
269 
270  cleanup;
271  return;
272 }
273 
274 
278 int main(void)
279 {
280  TEST_INIT;
281 
282  /* cpl_msg_set_level(CPL_MSG_DEBUG); */
283  test_identify();
284 
285  TEST_END;
286 }
287 
double fors_rand_gauss(void)
Pseudo-random gaussian distributed number.
Definition: fors_utils.c:181
fors_setting * fors_setting_new(const cpl_frame *raw)
Create setting from FITS header.
Definition: fors_setting.c:64
static void test_identify(void)
Test identification.
identify_method * fors_identify_method_new(const cpl_parameterlist *parameters, const char *context)
Get id method from parameter list.
bool fors_star_equal(const fors_star *s, const fors_star *t)
Test for equality.
Definition: fors_star.c:309
void fors_star_delete(fors_star **star)
Delete object and set pointer to NULL.
Definition: fors_star.c:273
#define assure(EXPR)
Definition: list.c:101
cpl_frame * create_std_cat(const char *filename, const char *tag, cpl_frame_group group)
Create standard star catalogue.
fors_star * fors_star_new(double x, double y, double fwhm, double smajor, double sminor, double orientation, double m, double dm, double si)
Constructor.
Definition: fors_star.c:114
int main(void)
Test of identification module.
fors_star * fors_star_duplicate(const fors_star *star)
Copy constructor.
Definition: fors_star.c:248
cpl_frame * create_phot_table(const char *filename, const char *tag, cpl_frame_group group)
Create photometry table.
void fors_parameterlist_set_defaults(cpl_parameterlist *parlist)
Set unset parameters to default value.
Definition: fors_utils.c:545
void fors_identify(fors_star_list *stars, fors_std_star_list *cat, const identify_method *im, cpl_image **histogram)
Identify sources.
cpl_frame * create_standard(const char *filename, const char *tag, cpl_frame_group group)
Create standard star image.
void fors_identify_define_parameters(cpl_parameterlist *parameters, const char *context)
Define recipe parameters.
Definition: fors_identify.c:82