FORS Pipeline Reference Manual  5.0.9
fors_image-test.c
1 /* $Id: fors_image-test.c,v 1.20 2007-11-23 14:24:24 jmlarsen 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: jmlarsen $
23  * $Date: 2007-11-23 14:24:24 $
24  * $Revision: 1.20 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #include <fors_image.h>
33 #include <fors_pfits.h>
34 #include <fors_saturation.h>
35 #include <test_simulate.h>
36 #include <test.h>
37 
38 #include <math.h>
39 
46 #undef cleanup
47 #define cleanup \
48 do { \
49  fors_setting_delete(&setting); \
50 } while(0)
51 
55 static void
57 {
58  const int nx = 200; /* Duplication here. Must be updated
59  in synchronization with nx and ny in ./test_simulate.c */
60  const int ny = 200;
61  const char *const filename = "fors_image.fits";
62 
63  cpl_frame *frame = cpl_frame_new();
64  cpl_propertylist *header = cpl_propertylist_new();
65  fors_setting *setting = NULL;
66  cpl_image *data = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
67  cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
68  fors_image *image;
69  double saturated;
70 
71  cpl_image_add_scalar(variance, 1.0);
72  cpl_image_set(data, 1, 1, 1); /* One non-saturated pixel */
73  image = fors_image_new(data, variance);
74 
75  /* save, load */
76  {
77  double exptime = 423;
78  create_standard_keys(header, exptime);
79  }
80 
81  fors_image_save(image, header, NULL, filename);
82  fors_image_delete(&image);
83  cpl_frame_set_filename(frame, filename);
84 
85  setting = fors_setting_new(frame);
86 
87  image = fors_image_load(frame);
88 
89  saturated = fors_saturation_img_satper(image);
90 
91 
92  test_rel( saturated, 100 * (1 - 1.0 / (nx*ny)), 0.01 );
93  test_eq( nx, fors_image_get_size_x(image) );
94  test_eq( ny, fors_image_get_size_y(image) );
95 
96  cpl_frame_delete(frame);
97  cpl_propertylist_delete(header);
98  fors_image_delete(&image);
99 
100 
101  /* Test croppping */
102  data = cpl_image_new(3, 2, FORS_IMAGE_TYPE);
103  variance = cpl_image_new(3, 2, FORS_IMAGE_TYPE);
104 
105  {
106  int x, y;
107  for (y = 1; y <= 2; y++) {
108  for (x = 1; x <= 3; x++) {
109  cpl_image_set(data, x, y, x*y);
110  }
111  }
112  /*
113  Input data now:
114  2 4 6
115  1 2 3
116  */
117  }
118  cpl_image_add_scalar(variance, 1.0);
119  image = fors_image_new(data, variance);
120 
121  int xlo = 2;
122  int xhi = 2;
123  int ylo = 1;
124  int yhi = 2;
125  fors_image_crop(image,
126  xlo, ylo, xhi, yhi);
127 
128  /*
129  Should have now:
130  4
131  2
132  */
133  test_eq( fors_image_get_size_x(image), 1 );
134  test_eq( fors_image_get_size_y(image), 2 );
135 
136  test_rel( fors_image_get_min(image), 2, 0.0001 );
137  test_rel( fors_image_get_max(image), 4, 0.0001 );
138  test_rel( fors_image_get_mean(image, NULL), 3, 0.0001 );
139  test_rel( fors_image_get_stdev(image, NULL), sqrt(2), 0.0001 );
140 
141  fors_image_delete(&image);
142 
143  cleanup;
144  return;
145 }
146 
150 static void
152 {
153  //const int nx = 4000;
154  //const int ny = 2000;
155  const int nx = 400;
156  const int ny = 200;
157 
158  const int xradius = 1;
159  const int yradius = 1;
160  const int xstart = 1;
161  const int ystart = 1;
162  const int xend = nx;
163  const int yend = ny;
164  const int xstep = 1;
165  const int ystep = 1;
166 
167  cpl_image *data = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
168  cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
169 
170  fors_image *image;
171  cpl_image *smooth;
172 
173  cpl_image_add_scalar(variance, 1.0);
174 
175  image = fors_image_new(data, variance);
176  bool use_data = true;
177  smooth = fors_image_filter_median_create(image,
178  xradius, yradius,
179  xstart, ystart,
180  xend, yend,
181  xstep, ystep,
182  use_data);
183 
184  cpl_image_delete(smooth);
185  fors_image_delete(&image);
186 
187  return;
188 }
189 
190 #undef cleanup
191 #define cleanup \
192 do { \
193  fors_image_delete(&left); \
194  fors_image_delete(&right); \
195 } while(0)
196 
200 static void
202 {
203  /* Simulate data */
204  const int nx = 20;
205  const int ny = 30;
206  const double values = 17;
207  const double variance = 5;
208  cpl_image *left_data = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
209  cpl_image *left_variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
210  cpl_image *right_data;
211  cpl_image *right_variance;
212  fors_image *left, *right;
213  double error_before, error_after;
214 
215  cpl_image_add_scalar(left_data, values);
216  cpl_image_add_scalar(left_variance, variance);
217 
218  right_data = cpl_image_multiply_scalar_create(left_data, 0.6);
219  right_variance = cpl_image_duplicate(left_variance);
220 
221  left = fors_image_new(left_data , left_variance);
222  right = fors_image_new(right_data, right_variance);
223 
224  error_before = fors_image_get_error_mean(left, NULL);
225 
226  /* Call function */
227  fors_image_subtract(left, right);
228 
229  /* Check results */
230  error_after = fors_image_get_error_mean(left, NULL);
231  test_rel( fors_image_get_mean(left, NULL), values*(1-0.6), 0.001);
232  test_rel( error_after, error_before*sqrt(2.0), 0.001);
233 
234  cleanup;
235  return;
236 }
237 
238 #undef cleanup
239 #define cleanup \
240 do { \
241  fors_image_delete(&image); \
242 } while(0)
243 
247 static void
249 {
250  /* Simulate data */
251  const int nx = 20;
252  const int ny = 30;
253  const double values = 17;
254  const double var_val = 5;
255  cpl_image *data = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
256  cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
257  double base = 10;
258  double dbase = -1;
259  fors_image *image;
260 
261  cpl_image_add_scalar(data, values);
262  cpl_image_add_scalar(variance, var_val);
263 
264  image = fors_image_new(data, variance);
265 
266  /* Call function */
267  fors_image_exponential(image, base, dbase);
268 
269  /* Check results */
270  test_rel( fors_image_get_mean(image, NULL), pow(base, values), 0.001);
271 
272  cleanup;
273  return;
274 }
275 
276 
277 #undef cleanup
278 #define cleanup \
279 do { \
280  fors_image_delete(&image); \
281 } while(0)
282 
285 static void
287 {
288  /* Simulate data */
289  const int nx = 20;
290  const int ny = 30;
291  const double values = 17;
292  const double variance_value = 5;
293  cpl_image *data = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
294  cpl_image *variance = cpl_image_new(nx, ny, FORS_IMAGE_TYPE);
295  fors_image *image = NULL;
296  double error_before, error_after;
297 
298  cpl_image_add_scalar(data , values);
299  cpl_image_add_scalar(variance, variance_value);
300 
301  image = fors_image_new(data, variance);
302 
303  error_before =
304  fors_image_get_error_mean(image, NULL) /
305  fors_image_get_mean (image, NULL);
306 
307  /* Call function */
308  fors_image_divide(image, image);
309 
310  /* Check results,
311  * relative errors add in quadrature
312  */
313  error_after =
314  fors_image_get_error_mean(image, NULL) /
315  fors_image_get_mean (image, NULL);
316  test_rel( fors_image_get_mean(image, NULL), 1.0, 0.001 );
317  test_rel( error_after, error_before*sqrt(2.0), 0.001 );
318 
319  cleanup;
320  return;
321 }
322 
323 
324 
328 int main(void)
329 {
330  TEST_INIT;
331 
332  test_image();
333 
335 
336  test_subtract();
337 
338  test_divide();
339 
341 
342  TEST_END;
343 }
344 
cpl_image * fors_image_filter_median_create(const fors_image *image, int xradius, int yradius, int xstart, int ystart, int xend, int yend, int xstep, int ystep, bool use_data)
Smooth image.
Definition: fors_image.c:1065
static void test_subtract(void)
Test image subtraction.
fors_image * fors_image_new(cpl_image *data, cpl_image *variance)
Create image.
Definition: fors_image.c:102
fors_setting * fors_setting_new(const cpl_frame *raw)
Create setting from FITS header.
Definition: fors_setting.c:64
static void test_exponential(void)
Test image exponentiation.
double fors_image_get_max(const fors_image *image)
Get max data value.
Definition: fors_image.c:950
int main(void)
Test of image module.
void fors_image_subtract(fors_image *left, const fors_image *right)
Subtract images.
Definition: fors_image.c:595
static void test_divide(void)
Test image division.
void fors_image_crop(fors_image *image, int xlo, int ylo, int xhi, int yhi)
Crop image.
Definition: fors_image.c:1008
double fors_image_get_stdev(const fors_image *image, double *dstdev)
Get empirical stdev of data.
Definition: fors_image.c:1373
void fors_image_delete(fors_image **image)
Deallocate image and set pointer to NULL.
Definition: fors_image.c:164
cpl_size fors_image_get_size_y(const fors_image *image)
Get image height.
Definition: fors_image.c:514
void create_standard_keys(cpl_propertylist *header, double exptime)
Write FORS standard keywords to simulated header.
double fors_image_get_min(const fors_image *image)
Get min data value.
Definition: fors_image.c:935
fors_image * fors_image_load(const cpl_frame *frame)
Load image.
Definition: fors_image.c:291
double fors_image_get_mean(const fors_image *image, double *dmean)
Get mean data value.
Definition: fors_image.c:966
static void test_image(void)
Test functions.
static void test_median_filter(void)
Median filtering benchmark.
cpl_size fors_image_get_size_x(const fors_image *image)
Get image width.
Definition: fors_image.c:501
void fors_image_divide(fors_image *left, const fors_image *right)
Divide images.
Definition: fors_image.c:729
void fors_image_exponential(fors_image *image, double b, double db)
Exponential.
Definition: fors_image.c:907
double fors_image_get_error_mean(const fors_image *image, double *dmean)
Get mean of error bars.
Definition: fors_image.c:1437
void fors_image_save(const fors_image *image, const cpl_propertylist *header, const cpl_propertylist *err_header, const char *filename)
Save image.
Definition: fors_image.c:375