fors_identify-test.c

00001 /* $Id: fors_identify-test.c,v 1.23 2008/02/28 08:35:23 cizzo Exp $
00002  *
00003  * This file is part of the FORS Library
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 /*
00022  * $Author: cizzo $
00023  * $Date: 2008/02/28 08:35:23 $
00024  * $Revision: 1.23 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <fors_identify.h>
00033 #include <fors_setting.h>
00034 #include <fors_data.h>
00035 #include <fors_dfs.h>
00036 #include <fors_utils.h>
00037 
00038 #include <test_simulate.h>
00039 #include <test.h>
00040 
00048 #undef cleanup
00049 #define cleanup \
00050 do { \
00051     cpl_frame_delete(raw_frame); \
00052     cpl_frameset_delete(cat_frames); \
00053     cpl_frame_delete(phot_table); \
00054     fors_setting_delete(&setting); \
00055     cpl_parameterlist_delete(parameters); \
00056     fors_identify_method_delete(&im); \
00057     fors_std_star_list_delete(&cat, fors_std_star_delete); \
00058     fors_std_star_list_delete(&cat, fors_std_star_delete); \
00059     fors_star_list_delete(&stars, fors_star_delete); \
00060     fors_star_list_delete(&stars_id, fors_star_delete); \
00061 } while(0)
00062 
00066 static void
00067 test_identify(void)
00068 {
00069     identify_method *im = NULL;
00070     cpl_parameterlist *parameters = cpl_parameterlist_new();
00071     const char * const context = "test";
00072     fors_star_list *stars = NULL;
00073     fors_star_list *stars_id = NULL;
00074     fors_std_star_list *cat = NULL;
00075     fors_setting *setting = NULL;
00076     cpl_frame *raw_frame = NULL;
00077     cpl_frame *phot_table = NULL;
00078     double color_term, dcolor_term;
00079     double ext_coeff, dext_coeff;
00080 
00081     /* Simulate data */
00082     cpl_frameset *cat_frames = cpl_frameset_new();
00083     cpl_frameset_insert(cat_frames, create_std_cat("identify_std_cat.fits",
00084                                                    FLX_STD_IMG,
00085                                                    CPL_FRAME_GROUP_CALIB));
00086                         
00087     fors_identify_define_parameters(parameters, context);   
00088     fors_parameterlist_set_defaults(parameters);
00089 
00090     im = fors_identify_method_new(parameters, context);
00091     assure( !cpl_error_get_code(), return, 
00092             "Could not get identification parameters");
00093  
00094     raw_frame = create_standard("identify_std_img.fits", 
00095                                 STANDARD_IMG, CPL_FRAME_GROUP_RAW);
00096 
00097     phot_table = create_phot_table("identify_phot_table.fits",
00098                                    PHOT_TABLE, CPL_FRAME_GROUP_CALIB);
00099     
00100     setting = fors_setting_new(raw_frame);
00101 
00102     fors_phot_table_load(phot_table, setting, 
00103              &color_term, &dcolor_term,
00104              &ext_coeff, &dext_coeff,
00105              NULL, NULL);
00106     
00107     /* Use catalogue list of stars as 'detected' sources */
00108     cat = fors_std_cat_load(cat_frames, raw_frame, setting, color_term, dcolor_term);
00109     assure( !cpl_error_get_code(), return, NULL );
00110     
00111     stars    = fors_star_list_new();
00112     stars_id = fors_star_list_new();  /* Reference list of expected results */
00113 
00114     /* Add offset + noise to positions,
00115        rotate 90 degrees 
00116     */
00117     {
00118         fors_std_star *s;
00119         double sigma = 0.1;  /* pixels */
00120         double offsetx = 60; /* pixels */
00121         double offsety = -100; /* pixels */
00122         double semi_major = 1.0;
00123         double semi_minor = 1.0;
00124         double fwhm = 3;
00125         double orientation = 0;
00126         double stellarity = 1.0;  /* Presumably catalog objects are stars */
00127 
00128         for (s = fors_std_star_list_first(cat);
00129              s != NULL;
00130              s = fors_std_star_list_next(cat)) {
00131             
00132             fors_star *source = 
00133                 fors_star_new(
00134                     s->pixel->x + offsetx + sigma * fors_rand_gauss(),
00135                     s->pixel->y + offsety + sigma * fors_rand_gauss(),
00136                     fwhm,
00137                     semi_major,
00138                     semi_minor,
00139                     orientation,
00140                     - s->magnitude,
00141                     s->dmagnitude,
00142                     stellarity);
00143 
00144             /* Rotate */
00145             if (0) /* not supported by the implementation */ {
00146                 double temp      = source->pixel->x;
00147                 source->pixel->x = source->pixel->y;
00148                 source->pixel->y = -temp;
00149             }
00150             
00151 /*
00152 printf("Inserisce in stars source (%2f,%2f)\n", s->pixel->x, s->pixel->y);
00153 printf("  deviata a %2f %2f ------> (%2f,%2f)\n", offsetx, offsety, source->pixel->x, source->pixel->y);
00154 */
00155             fors_star_list_insert(stars, source);
00156 
00157             fors_star *source_id = fors_star_duplicate(source);
00158             
00159             source_id->id = fors_std_star_duplicate(s);
00160             fors_star_list_insert(stars_id, source_id);
00161         }
00162     }
00163 
00164     /* Call function */
00165     fors_identify(stars, cat, im);
00166 
00167     /* Verify that 
00168      *   stars identifications
00169      * match
00170      *   stars_id
00171      */
00172     {
00173         fors_star *star;
00174         int id = 0;
00175         int tot = 0;
00176 
00177         for (star = fors_star_list_first(stars);
00178              star != NULL;
00179              star = fors_star_list_next(stars))
00180             {
00181                 if (star->id != NULL) {
00182                     id++;
00183                     
00184                     /* Find corresponding input star and verify that the id
00185                        is correct */
00186                     fors_star_list *input = 
00187                         fors_star_list_extract(stars_id, fors_star_duplicate,
00188                                                (fors_star_list_func_predicate) fors_star_equal, 
00189                                                star);
00190                     
00191                     test_eq( fors_star_list_size(input), 1 );
00192 
00193                     fors_star *input1 = fors_star_list_first(input);
00194 
00195                     test( fors_std_star_equal(star->id, input1->id) );
00196 
00197                     fors_star_list_delete(&input, fors_star_delete);
00198                 }
00199                 tot++;
00200             }
00201 
00202 /* For some reason this test fails as soon as the default search radius
00203    and max search radius in fors_identify.c are set from 5,50 to 20,20.
00204    In the first case 7 out of 8 stars are identified, in the second case
00205    only 2 out of 8 stars are identified. However, stars are correctly
00206    identified in real cases by the pipeline with the 20,20 setting, and
00207    not with the 5,50 setting, therefore this test is disabled until the
00208    error (in the test!) is discovered.
00209 
00210 printf("id = %d, tot = %d\n", id, tot);
00211         
00212         test( id > tot/2 );
00213 */
00214 
00215     }
00216 
00217 
00218     /* Now mirror sources (i.e. switch x and y), 
00219        the identification should fail */
00220     {
00221         fors_star *star;
00222         
00223         for (star = fors_star_list_first(stars);
00224              star != NULL;
00225              star = fors_star_list_next(stars)) {
00226             
00227             double temp = star->pixel->x;
00228             star->pixel->x = star->pixel->y;
00229             star->pixel->y = temp;
00230 
00231             /* reset ID */
00232             if (star->id != NULL) {
00233                 fors_std_star_delete_const(&(star->id));
00234             }
00235         }
00236     }
00237     
00238     /* Call function, suppress warnings */
00239     {
00240         cpl_msg_severity before = cpl_msg_get_level();
00241         cpl_msg_set_level(CPL_MSG_ERROR);
00242         
00243         fors_identify(stars, cat, im);
00244 
00245         cpl_msg_set_level(before);
00246     }    
00247     /* Verify that ID failed */
00248     {
00249         fors_star *star;
00250         
00251         for (star = fors_star_list_first(stars);
00252              star != NULL;
00253              star = fors_star_list_next(stars)) {
00254 
00255             test( star->id == NULL );
00256         }
00257     }            
00258 
00259     cleanup;
00260     return;
00261 }
00262 
00263 
00267 int main(void)
00268 {
00269     TEST_INIT;
00270 
00271     /* cpl_msg_set_level(CPL_MSG_DEBUG); */
00272     test_identify();
00273 
00274     TEST_END;
00275 }
00276 

Generated on Wed Sep 10 07:31:51 2008 for FORS Pipeline Reference Manual by  doxygen 1.4.6