VIRCAM Pipeline  2.3.10
imcore_classify-test.c
1 /* $Id: imcore_classify-test.c,v 1.3 2015/11/18 20:03:54 jim Exp $
2  *
3  * This file is part of the CASU Pipeline utilities
4  * Copyright (C) 2015 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: jim $
23  * $Date: 2015/11/18 20:03:54 $
24  * $Revision: 1.3 $
25  * $Name: $
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <math.h>
31 
32 #include <cpl_init.h>
33 #include <cpl_test.h>
34 #include <casu_tfits.h>
35 #include <casu_utils.h>
36 #include <casu_mods.h>
37 #include "../imcore.h"
38 
39 #define NTEST 10
40 
41 int main(void) {
42  int retval,i,nrow,ncol,nl;
43  casu_tfits *outtab;
44  cpl_image *im,*bkg,*cnf;
45  cpl_table *tab;
46  double sigma=2.0,norm2,tot[NTEST],sky=500.0,diff,val;
47  double xpos[] = {100.0,200.0,300.0,400.0,500.0,600.0,700.0,800.0,900.0,
48  1000.0};
49  double ypos[] = {100.0,200.0,300.0,400.0,500.0,600.0,700.0,800.0,900.0,
50  1000.0};
51  double norm[] = {1000.0,100.0,200.0,500.0,550.0,600.0,650.0,700.0,
52  750.0,800.0};
53  casu_fits *inf,*inconf;
54  cpl_propertylist *pl;
55 
56  /* Initialise */
57 
58  cpl_test_init(PACKAGE_BUGREPORT,CPL_MSG_WARNING);
59 
60  /* Generate a field with some stars and a confidence map */
61 
62  bkg = cpl_image_new(1024,1024,CPL_TYPE_FLOAT);
63  im = cpl_image_new(1024,1024,CPL_TYPE_FLOAT);
64  cnf = cpl_image_new(1024,1024,CPL_TYPE_INT);
65  norm2 = 2.0*CPL_MATH_PI*sigma*sigma;
66  cpl_image_fill_noise_uniform(bkg,-10.0,10.0);
67  cpl_image_add_scalar(bkg,sky);
68  cpl_image_fill_noise_uniform(cnf,99.9,100.1);
69  for (i = 0; i < NTEST; i++) {
70  cpl_image_fill_gaussian(im,xpos[i],ypos[i],norm[i]*norm2,sigma,sigma);
71  tot[i] = cpl_image_get_flux(im);
72  cpl_image_add(bkg,im);
73  }
74  pl = cpl_propertylist_new();
75  inf = casu_fits_wrap(bkg,NULL,NULL,pl);
76  inconf = casu_fits_wrap(cnf,NULL,NULL,NULL);
77  cpl_image_delete(im);
78  cpl_propertylist_delete(pl);
79 
80  /* Give it a WCS */
81 
82  pl = casu_fits_get_ehu(inf);
83  cpl_propertylist_update_string(pl,"CTYPE1","RA---TAN");
84  cpl_propertylist_update_string(pl,"CTYPE2","DEC--TAN");
85  cpl_propertylist_update_double(pl,"CRVAL1",30.0);
86  cpl_propertylist_update_double(pl,"CRVAL2",12.0);
87  cpl_propertylist_update_double(pl,"CRPIX1",512.0);
88  cpl_propertylist_update_double(pl,"CRPIX2",512.0);
89  cpl_propertylist_update_double(pl,"CD1_1",-1.0/3600);
90  cpl_propertylist_update_double(pl,"CD1_2",0.0);
91  cpl_propertylist_update_double(pl,"CD2_1",0.0);
92  cpl_propertylist_update_double(pl,"CD2_2",1.0/3600);
93 
94  /* Run imcore */
95 
96  retval = imcore_conf(inf,inconf,5,1.5,0,5.0,64,6,3.0,1.0,&outtab);
97  cpl_test_eq(retval,CASU_OK);
98  cpl_test_nonnull(outtab);
99 
100  /* Check the results. Start by checking the number of rows and columns.
101  Sort the table by X */
102 
103  tab = casu_tfits_get_table(outtab);
104  cpl_test_nonnull(tab);
105  ncol = cpl_table_get_ncol(tab);
106  cpl_test_eq(ncol,80);
107  nrow = cpl_table_get_nrow(tab);
108  cpl_test_eq(nrow,NTEST);
109  pl = cpl_propertylist_new();
110  cpl_propertylist_append_bool(pl,"X_coordinate",0);
111  cpl_table_sort(tab,pl);
112  cpl_propertylist_delete(pl);
113 
114  /* Test the column content of the table */
115 
116  for (i = 0; i < NTEST; i++) {
117  cpl_test_abs(xpos[i],cpl_table_get_float(tab,"X_coordinate",(cpl_size)i,
118  &nl),0.2);
119  cpl_test_abs(ypos[i],cpl_table_get_float(tab,"Y_coordinate",(cpl_size)i,
120  &nl),0.2);
121  diff = fabs(cpl_table_get_float(tab,"Aper_flux_5",(cpl_size)i,&nl) -
122  tot[i]);
123  diff /= cpl_table_get_float(tab,"Aper_flux_5_err",(cpl_size)i,&nl);
124  cpl_test_lt(diff,1.5);
125  }
126 
127  /* Run classify and test the values of the classification */
128 
129  retval = imcore_classify(outtab,5,6);
130  cpl_test_eq(retval,CASU_OK);
131  pl = casu_tfits_get_ehu(outtab);
132  cpl_test_rel(cpl_propertylist_get_float(pl,"ESO QC IMAGE_SIZE"),4.57,0.01);
133  cpl_test_eq(cpl_propertylist_get_bool(pl,"ESO DRS CLASSIFD"),1);
134  cpl_test_abs(cpl_propertylist_get_float(pl,"APCOR3"),0.145,0.05);
135  for (i = 0; i < NTEST; i++) {
136  val = cpl_table_get(casu_tfits_get_table(outtab),"Classification",
137  (cpl_size)i,NULL);
138  cpl_test_rel(val,-1.0,0.001);
139  }
140 
141  /* Get out of here */
142 
143  casu_fits_delete(inf);
144  casu_fits_delete(inconf);
145  casu_tfits_delete(outtab);
146  return(cpl_test_end(0));
147 
148 }
149 
150 /*
151 
152 $Log: imcore_classify-test.c,v $
153 Revision 1.3 2015/11/18 20:03:54 jim
154 Fixed a couple of tests
155 
156 Revision 1.2 2015/11/12 18:25:36 jim
157 Fixed missing include
158 
159 Revision 1.1 2015/10/15 11:27:22 jim
160 new
161 
162 
163 */
void casu_fits_delete(casu_fits *p)
Definition: casu_fits.c:364
casu_fits * casu_fits_wrap(cpl_image *im, casu_fits *model, cpl_propertylist *phu, cpl_propertylist *ehu)
Definition: casu_fits.c:883
cpl_propertylist * casu_fits_get_ehu(casu_fits *p)
Definition: casu_fits.c:576
void casu_tfits_delete(casu_tfits *p)
Definition: casu_tfits.c:292
cpl_table * casu_tfits_get_table(casu_tfits *p)
Definition: casu_tfits.c:364
cpl_propertylist * casu_tfits_get_ehu(casu_tfits *p)
Definition: casu_tfits.c:473
int imcore_conf(casu_fits *infile, casu_fits *conf, int ipix, float threshold, int icrowd, float rcore, int nbsize, int cattype, float filtfwhm, float gain, casu_tfits **outcat)
Do source extraction.
Definition: imcore_conf.c:147
int imcore_classify(casu_tfits *catalogue, float minsize, int cattype)
Do star/galaxy classification.
Definition: classify.c:203