VIRCAM Pipeline  2.3.12
casu_backmap-test.c
1 /* $Id: casu_backmap-test.c,v 1.2 2015/08/07 13:06: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/08/07 13:06:54 $
24  * $Revision: 1.2 $
25  * $Name: $
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include <cpl_init.h>
33 #include <cpl_test.h>
34 #include <casu_utils.h>
35 #include <casu_mods.h>
36 
37 int main(void) {
38  int status,retval,nx,ny,nbsize,npts,j;
39  float *map,*skyout,avback;
40  cpl_binary *bpm;
41  cpl_image *maptest,*test;
42  cpl_mask *bpmtest;
43  double val;
44 
45  /* Initialise */
46 
47  cpl_test_init(PACKAGE_BUGREPORT,CPL_MSG_WARNING);
48 
49  /* Start with input values that should cause problems. Then do
50  the input testing by removing one problem at a time until they
51  are all sensible. Start by checking the inherited status.
52  It should return with the input value and the output sky map
53  should always be NULL in an error condition */
54 
55  status = CASU_FATAL;
56  nx = 1000;
57  ny = 1000;
58  npts = nx*ny;
59  maptest = cpl_image_new((cpl_size)nx,(cpl_size)ny,CPL_TYPE_FLOAT);
60  bpmtest = cpl_image_get_bpm(maptest);
61  map = cpl_image_get_data_float(maptest);
62  bpm = cpl_mask_get_data(bpmtest);
63  nbsize = 100;
64  retval = casu_backmap(map,bpm,nx,ny,nbsize,&avback,&skyout,&status);
65  cpl_test_eq(status,CASU_FATAL);
66  cpl_test_eq(status,retval);
67  cpl_test_null(skyout);
68 
69  /* Reset status now. It should now fail because the input arrays
70  are null */
71 
72  status = CASU_OK;
73  map = NULL;
74  bpm = NULL;
75  retval = casu_backmap(map,bpm,nx,ny,nbsize,&avback,&skyout,&status);
76  cpl_test_eq(status,CASU_FATAL);
77  cpl_test_eq(status,retval);
78  cpl_test_null(skyout);
79 
80  /* Now test image size errors */
81 
82  status = CASU_OK;
83  map = cpl_image_get_data_float(maptest);
84  bpm = cpl_mask_get_data(bpmtest);
85  nx = -1;
86  retval = casu_backmap(map,bpm,nx,ny,nbsize,&avback,&skyout,&status);
87  cpl_test_eq(status,CASU_FATAL);
88  cpl_test_eq(status,retval);
89  cpl_test_null(skyout);
90 
91  /* Now the condition where every pixel is flagged as bad */
92 
93  status = CASU_OK;
94  nx = 1000;
95  for (j = 0; j < npts; j++)
96  bpm[j] = 1;
97  retval = casu_backmap(map,bpm,nx,ny,nbsize,&avback,&skyout,&status);
98  cpl_test_eq(status,CASU_FATAL);
99  cpl_test_eq(status,retval);
100  cpl_test_null(skyout);
101 
102  /* Finally the condition where nbsize is stupid */
103 
104  status = CASU_OK;
105  memset(bpm,0,npts*sizeof(cpl_binary));
106  nbsize = -1;
107  retval = casu_backmap(map,bpm,nx,ny,nbsize,&avback,&skyout,&status);
108  cpl_test_eq(status,CASU_FATAL);
109  cpl_test_eq(status,retval);
110  cpl_test_null(skyout);
111  nbsize = 100;
112 
113  /* Do a background test now. Start by assuming all the pixels are good.
114  Give the map a background level of 2000 with some noise. See if it
115  comes back as 2000... */
116 
117  status = CASU_OK;
118  cpl_image_fill_noise_uniform(maptest,1990.0,2010.0);
119  retval = casu_backmap(map,bpm,nx,ny,nbsize,&avback,&skyout,&status);
120  cpl_test_eq(status,CASU_OK);
121  cpl_test_eq(status,retval);
122  cpl_test_nonnull(skyout);
123  cpl_test_rel(avback,2000.0,0.1);
124  test = cpl_image_wrap_float((cpl_size)nx,(cpl_size)ny,skyout);
125  val = cpl_image_get_mean_window(test,480,520,480,520);
126  cpl_test_rel(val,2000.0,0.1);
127  cpl_image_unwrap(test);
128  cpl_free(skyout);
129 
130  /* Now put some different values into a small area, but mask this
131  area out and see if we still get 2000 */
132 
133  status = CASU_OK;
134  cpl_image_fill_window(maptest,500,500,520,520,3000.0);
135  cpl_mask_threshold_image(bpmtest,(const cpl_image *)maptest,
136  2999.0,3001.0,CPL_BINARY_1);
137  retval = casu_backmap(map,bpm,nx,ny,nbsize,&avback,&skyout,&status);
138  cpl_test_eq(status,CASU_OK);
139  cpl_test_eq(status,retval);
140  cpl_test_nonnull(skyout);
141  cpl_test_rel(avback,2000.0,0.1);
142  test = cpl_image_wrap_float((cpl_size)nx,(cpl_size)ny,skyout);
143  val = cpl_image_get_mean_window(test,480,520,480,520);
144  cpl_image_unwrap(test);
145  cpl_free(skyout);
146 
147  /* Tidy and exit */
148 
149  cpl_image_delete(maptest);
150  return(cpl_test_end(0));
151 }
152 
153 /*
154 
155 $Log: casu_backmap-test.c,v $
156 Revision 1.2 2015/08/07 13:06:54 jim
157 Fixed copyright to ESO
158 
159 Revision 1.1.1.1 2015/06/12 10:44:32 jim
160 Initial import
161 
162 Revision 1.2 2015/03/12 09:16:51 jim
163 Modified to remove some compiler moans
164 
165 Revision 1.1 2015/01/09 11:39:55 jim
166 new entry
167 
168 
169 */
int casu_backmap(float *map, cpl_binary *bpm, int nx, int ny, int nbsize, float *avback, float **skymap, int *status)
Model background of an image.
Definition: casu_backmap.c:108