CR2RE Pipeline Reference Manual 1.6.2
cr2res_calib-test.c
1/*
2 * This file is part of the CR2RES Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18 */
19
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
25/*-----------------------------------------------------------------------------
26 Includes
27 -----------------------------------------------------------------------------*/
28
29#include <stdlib.h>
30#include <string.h>
31#include <math.h>
32#include <cpl.h>
33#include <hdrl.h>
34#include <cr2res_dfs.h>
35#include <cr2res_calib.h>
36#include <cr2res_io.h>
37#include "cr2res_pfits.h"
38#include "cr2res_utils.h"
39
40#define MODE_FLAT 0
41#define MODE_DARK 1
42#define MODE_BPM 2
43#define MODE_DETLIN 3
44
45#define pow2(x) (x) * (x)
46#define detlin(x, a, b, c) (x) * ((a) + (x) * ((b) + (x) * (c)))
47#define deterr(x, a, b, c, sx, sa, sb, sc) sqrt(detlin(pow2(x), pow2(sa), pow2(sb), pow2(sc)) + pow2(sx) * pow2((a) + 2 * (b) * (x) + 3 * (c) * pow2(x)))
48
49#define localdir "." //getenv("srcdir")
50
51static hdrl_image * cr2res_create_hdrl(int nx, int ny, double value, double error);
52
53static void test_cr2res_calib_image(void);
54static void test_cr2res_calib_cosmic(void);
55static void test_cr2res_calib_flat(void);
56static void test_cr2res_calib_dark(void);
57static void test_cr2res_calib_bpm(void);
58static void test_cr2res_calib_detlin(void);
59
60
61static void create_empty_fits()
62{
63 // Many tests require a empty fits file, since crires code checks if a file exists
64 // when adding a filename to a frame
65 char *my_path = cpl_sprintf("%s/TEST_empty.fits", localdir);
66 cpl_propertylist * list = cpl_propertylist_new();
67 cpl_propertylist_append_string(list, CR2RES_HEADER_DRS_TYPE, "DEBUG");
68 cpl_propertylist_save(list, my_path, CPL_IO_CREATE);
69 cpl_propertylist_delete(list);
70 cpl_free(my_path);
71}
72
73/*----------------------------------------------------------------------------*/
77/*----------------------------------------------------------------------------*/
78static void save_hdrl(char * filename, hdrl_image * hdrl, int mode, double dit)
79{
80 // Empty structures needed for master flat frame, but not for the test
81 // Need an empty fits file with header for the framesets
82 cpl_frame * empty = cpl_frame_new();
83
84 char *my_path = cpl_sprintf("%s/TEST_empty.fits", localdir);
85 cpl_frame_set_filename(empty, my_path);
86 cpl_frame_set_tag(empty, "DEBUG");
87 cpl_frame_set_group(empty, CPL_FRAME_GROUP_CALIB);
88
89 cpl_parameterlist * parlist = cpl_parameterlist_new();
90 cpl_propertylist * ext1 = cpl_propertylist_new();
91 cpl_propertylist_append_double(ext1, CR2RES_HEADER_DIT, dit);
92 cpl_propertylist * ext[] = {ext1, NULL, NULL};
93
94
95 cpl_frameset * all = cpl_frameset_new();
96 cpl_frameset * in = cpl_frameset_new();
97 cpl_frameset_insert(all, empty);
98 empty = cpl_frame_duplicate(empty);
99 cpl_frameset_insert(in, empty);
100
101 hdrl_image * list[] = {hdrl, NULL, NULL};
102
103 if (mode == MODE_FLAT)
104 cr2res_io_save_MASTER_FLAT(filename, all, in, parlist, list, ext1, ext, CR2RES_CAL_FLAT_MASTER_PROCATG, "debug");
105 if (mode == MODE_DARK)
106 cr2res_io_save_MASTER_DARK(filename, all, in, parlist, list, ext1, ext, CR2RES_CAL_DARK_MASTER_PROCATG, "debug");
107 if (mode == MODE_BPM)
108 {
109 cpl_image * list2[] = {hdrl_image_get_image(hdrl), NULL, NULL};
110 cr2res_io_save_BPM(filename, all, in, parlist, list2, ext1, ext, CR2RES_CAL_FLAT_BPM_PROCATG, "debug");
111 }
112 if (mode == MODE_DETLIN){
113 hdrl_imagelist * list3 = hdrl_imagelist_new();
114 hdrl_imagelist_set(list3, hdrl, 0);
115 hdrl_imagelist_set(list3, hdrl, 1);
116 hdrl_imagelist_set(list3, hdrl, 2);
117
118 hdrl_imagelist * list4[] = {list3, NULL, NULL};
119
120 cr2res_io_save_DETLIN_COEFFS(filename, all, in, parlist, list4, ext1, ext, CR2RES_CAL_DETLIN_COEFFS_PROCATG, "debug");
121
122 // hdrl_imagelist_unset(list3, 0);
123 // hdrl_imagelist_unset(list3, 1);
124 // hdrl_imagelist_unset(list3, 2);
126 }
127 cpl_frameset_delete(all);
128 cpl_frameset_delete(in);
129 cpl_free(my_path) ;
130 cpl_parameterlist_delete(parlist);
131 cpl_propertylist_delete(ext1);
132}
133
134/*----------------------------------------------------------------------------*/
138/*----------------------------------------------------------------------------*/
139static cpl_frame * create_master_flat(char * filename, int nx, int ny, double value, double error, cpl_mask ** bpm)
140{
141 cpl_frame * out = cpl_frame_new();
142 cpl_frame_set_filename(out, filename);
143 cpl_frame_set_tag(out, "FLAT");
144 cpl_frame_set_group(out, CPL_FRAME_GROUP_CALIB);
145
146 hdrl_image * hdrl = cr2res_create_hdrl(nx, ny, value, error);
147
148 if (bpm != NULL){
149 hdrl_image_reject(hdrl, 1, 1);
150 *bpm = cpl_mask_duplicate(hdrl_image_get_mask(hdrl));
151 }
152
153 save_hdrl(filename, hdrl, MODE_FLAT, 0);
154 hdrl_image_delete(hdrl);
155
156 return out;
157}
158
159/*----------------------------------------------------------------------------*/
163/*----------------------------------------------------------------------------*/
164static cpl_frame * create_master_dark(char * filename, int nx, int ny, double value, double error, double dit, cpl_mask ** bpm)
165{
166
167 cpl_frame * out = cpl_frame_new();
168 cpl_frame_set_filename(out, filename);
169 cpl_frame_set_tag(out, "DARK");
170 cpl_frame_set_group(out, CPL_FRAME_GROUP_CALIB);
171
172 hdrl_image * hdrl = cr2res_create_hdrl(nx, ny, value, error);
173
174 if (bpm != NULL){
175 hdrl_image_reject(hdrl, 1, 1);
176 *bpm = cpl_mask_duplicate(hdrl_image_get_mask(hdrl));
177 }
178
179 save_hdrl(filename, hdrl, MODE_DARK, dit);
180 hdrl_image_delete(hdrl);
181
182 return out;
183}
184
185/*----------------------------------------------------------------------------*/
189/*----------------------------------------------------------------------------*/
190static cpl_frame * create_bpm(char * filename, int nx, int ny, double value)
191{
192 cpl_frame * out = cpl_frame_new();
193 cpl_frame_set_filename(out, filename);
194 cpl_frame_set_tag(out, "BPM");
195 cpl_frame_set_group(out, CPL_FRAME_GROUP_CALIB);
196
197 hdrl_image * hdrl = cr2res_create_hdrl(nx, ny, value, 0);
198 save_hdrl(filename, hdrl, MODE_BPM, 0);
199 hdrl_image_delete(hdrl);
200
201 return out;
202}
203
204/*----------------------------------------------------------------------------*/
208/*----------------------------------------------------------------------------*/
209static cpl_frame * create_detlin(char * filename, hdrl_image * a, hdrl_image * b, hdrl_image * c)
210{
211 char *my_path = cpl_sprintf("%s/TEST_empty.fits", localdir);
212
213 // Empty structures needed for master flat frame, but not for the test
214 // Need an empty fits file with header for the framesets
215 cpl_frame * empty = cpl_frame_new();
216 cpl_frame_set_filename(empty, my_path);
217 cpl_frame_set_tag(empty, "DEBUG");
218 cpl_frame_set_group(empty, CPL_FRAME_GROUP_CALIB);
219
220 cpl_parameterlist * parlist = cpl_parameterlist_new();
221 cpl_propertylist * ext1 = cpl_propertylist_new();
222 cpl_propertylist * ext[] = {ext1, NULL, NULL};
223
224 cpl_frameset * all = cpl_frameset_new();
225 cpl_frameset * in = cpl_frameset_new();
226 cpl_frameset_insert(all, empty);
227 empty = cpl_frame_duplicate(empty);
228 cpl_frameset_insert(in, empty);
229
230 hdrl_imagelist * list3 = hdrl_imagelist_new();
231 hdrl_imagelist_set(list3, a, 0);
232 hdrl_imagelist_set(list3, b, 1);
233 hdrl_imagelist_set(list3, c, 2);
234
235 hdrl_imagelist * list4[] = {list3, NULL, NULL};
236
237 cr2res_io_save_DETLIN_COEFFS(filename, all, in, parlist, list4, ext1, ext, CR2RES_CAL_DETLIN_COEFFS_PROCATG, "debug");
238
239 hdrl_imagelist_unset(list3, 2);
240 hdrl_imagelist_unset(list3, 1);
241 hdrl_imagelist_unset(list3, 0);
242
244
245 cpl_frameset_delete(all);
246 cpl_frameset_delete(in);
247 cpl_parameterlist_delete(parlist);
248 cpl_propertylist_delete(ext1);
249
250 cpl_frame * out = cpl_frame_new();
251 cpl_frame_set_filename(out, filename);
252 cpl_frame_set_tag(out, "DETLIN");
253 cpl_frame_set_group(out, CPL_FRAME_GROUP_CALIB);
254
255 cpl_free(my_path);
256
257 return out;
258}
259
260/*----------------------------------------------------------------------------*/
264/*----------------------------------------------------------------------------*/
265static hdrl_image * cr2res_create_hdrl(int nx, int ny, double value, double error)
266{
267 hdrl_image * out = hdrl_image_new(nx, ny);
268 hdrl_value hv;
269 hv.data = value;
270 hv.error = error;
271 hdrl_image_add_scalar(out, hv);
272 return out;
273}
274
275static void test_cr2res_calib_image()
276{
277 int nx = 5;
278 int ny = 5;
279 //int badpix;
280
281 double img_value = 100;
282 double img_error = 1;
283
284 hdrl_image * in = cr2res_create_hdrl(nx, ny, img_value, img_error);
285 int chip = 1;
286
287 char *my_path1 = cpl_sprintf("%s/TEST_master_flat.fits", localdir);
288 char *my_path2 = cpl_sprintf("%s/TEST_master_dark.fits", localdir);
289 char *my_path3 = cpl_sprintf("%s/TEST_bpm.fits", localdir);
290 cpl_frame * flat = create_master_flat(my_path1, nx, ny, 1, 0, NULL);
291 cpl_frame * dark = create_master_dark(my_path2, nx, ny, 10, 1, 10, NULL);
292 cpl_frame * bpm = create_bpm(my_path3, nx, ny, 0);
293 cpl_frame * detlin = NULL;
294 double dit = 10;
295
296 hdrl_image * out;
297 //hdrl_image * cmp;
298
299 // NULL input / output
300 out = cr2res_calib_image(NULL, chip, 0, 0, 1, 0, NULL, NULL, NULL, NULL,dit,1);
301 cpl_test_null(out);
302
303 out = cr2res_calib_image(in, 0, 0, 0, 1, 0, NULL, NULL, NULL, NULL, dit, 1);
304 cpl_test_null(out);
305
306 out = cr2res_calib_image(in, CR2RES_NB_DETECTORS + 1, 0, 0, 1, 0,
307 NULL, NULL, NULL, NULL, dit, 1);
308 cpl_test_null(out);
309
310 // No correction
311 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, NULL, NULL, NULL, NULL, dit, 1);
312 cpl_test_nonnull(out);
313 cpl_test_image_abs(hdrl_image_get_image(in), hdrl_image_get_image(out), DBL_EPSILON);
314
315 // add the shot noise error
316 cpl_image_add_scalar(hdrl_image_get_error(in), sqrt(img_value)/sqrt(CR2RES_GAIN_CHIP1));
317 cpl_test_image_abs(hdrl_image_get_error(in), hdrl_image_get_error(out), DBL_EPSILON);
319
320
322 cpl_frame_delete(flat);
323 cpl_frame_delete(dark);
324 cpl_frame_delete(bpm);
325 cpl_frame_delete(detlin);
326 cpl_free(my_path1) ;
327 cpl_free(my_path2) ;
328 cpl_free(my_path3) ;
329}
330
331/*----------------------------------------------------------------------------*/
335/*----------------------------------------------------------------------------*/
336static void test_cr2res_calib_cosmic()
337{
338 int nx = 5;
339 int ny = 5;
340 //int badpix;
341
342 double img_value = 100;
343 double img_error = 1;
344 //double out_value, out_error;
345 //double flat_value, flat_error;
346
347 hdrl_image * in, * out;// * cmp;
348 //cpl_frame * flat;
349 int chip = 1;
350 //int cosmics_corr;
351 double dit = 10;
352
353 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
354
355 // Case 1: No Cosmic Correction, i.e. nothing happens
356 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, NULL, NULL, NULL, NULL, dit, 1);
357 cpl_test_image_abs(hdrl_image_get_image(in), hdrl_image_get_image(out), DBL_EPSILON);
358 // add the shot noise
359 cpl_image_add_scalar(hdrl_image_get_error(in), sqrt(img_value)/sqrt(CR2RES_GAIN_CHIP1));
360 cpl_test_image_abs(hdrl_image_get_error(in), hdrl_image_get_error(out), DBL_EPSILON);
361
362 // Case 2: Cosmic Correction
363 // TODO when cosmic correction is implemented
364
367
368}
369
370
371/*----------------------------------------------------------------------------*/
375/*----------------------------------------------------------------------------*/
376static void test_cr2res_calib_flat()
377{
378 int nx = 5;
379 int ny = 5;
380 //int badpix;
381
382 double img_value = 100;
383 double img_error = 1;
384 double tmp_error = 0;
385 double out_value, out_error;
386 double flat_value, flat_error;
387
388 hdrl_image * in, * out, * cmp;
389 //cpl_mask * bpm;
390 cpl_frame * flat;
391 int chip = 1;
392 double dit = 10;
393 //cpl_error_code error;
394
395 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
396
397 // Case 1: Flat is just 1 and no error, i.e. no change
398 flat_value = 1;
399 flat_error = 0;
400 char *my_path1 = cpl_sprintf("%s/TEST_master_flat.fits", localdir);
401 flat = create_master_flat(my_path1, nx, ny, flat_value, flat_error, NULL);
402 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
403
404 out_value = img_value / flat_value;
405 out_error = sqrt( pow(img_error / flat_value, 2) + pow(img_value * flat_error/ (flat_value * flat_value), 2));
406 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
407 // add the shot noise
408 cpl_image_add_scalar(hdrl_image_get_error(cmp), sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
409
410 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
411 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
412
415 cpl_frame_delete(flat);
416
417 // Case 2: Flat is constant == 2, error 1
418 flat_value = 2;
419 flat_error = 1;
420 flat = create_master_flat(my_path1, nx, ny, flat_value, flat_error, NULL);
421 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
422
423 out_value = img_value / flat_value;
424 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
425 out_error = sqrt( pow(tmp_error / flat_value, 2) + pow(img_value * flat_error/ (flat_value * flat_value), 2));
426 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
427
428 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
429 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
430
433 cpl_frame_delete(flat);
434
435 // Case 3: Flat is 0, i.e. all pixels become bad
436 flat_value = 0;
437 flat_error = 1;
438 flat = create_master_flat(my_path1, nx, ny, flat_value, flat_error, NULL);
439 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
440
441 cpl_test_eq(nx * ny, hdrl_image_count_rejected(out));
442
444 cpl_frame_delete(flat);
445
446 // Case 4: Flat file does not exist
447 flat = cpl_frame_new();
448 cpl_frame_set_filename(flat, "TEST_tobeornottobe.fits");
449 cpl_frame_set_tag(flat, "FLAT");
450 cpl_frame_set_group(flat, CPL_FRAME_GROUP_CALIB);
451
452 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
453 cpl_test_error(CPL_ERROR_FILE_IO);
455 cpl_frame_delete(flat);
456
457 // Case 5: image is in a wrong group
458 flat = create_master_dark(my_path1, nx, ny, 1, 0, 10, NULL);
459 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
460 cpl_test_null(out);
461 cpl_frame_delete(flat);
462
463 // Case 6: No Filename set
464 flat = cpl_frame_new();
465 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
466 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
467 cpl_test_null(out);
468 cpl_frame_delete(flat);
469
470 // Case 7: DataFile is empty, i.e. only header
471 flat = cpl_frame_new();
472 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
473 cpl_frame_set_filename(flat, my_path2);
474 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
475 cpl_test_null(out);
476 cpl_frame_delete(flat);
477
478 // // Case 8: BPM is set
479 // flat_value = 1;
480 // flat_error = 1;
481 // flat = create_master_flat("TEST_master_flat.fits", nx, ny, flat_value, flat_error, &bpm);
482 // out = cr2res_calib_image(in, chip, 0, 0, 0, flat, NULL, NULL, NULL, dit, 1);
483
484 // cpl_test_eq_mask(bpm, hdrl_image_get_mask(out));
485
486 // hdrl_image_delete(out);
487 // cpl_frame_delete(flat);
488 // cpl_mask_delete(bpm);
489
490
491 cpl_free(my_path1) ;
492 cpl_free(my_path2) ;
494}
495
496static void test_cr2res_calib_dark()
497{
498 int nx = 5;
499 int ny = 5;
500 //int badpix;
501
502 double img_value = 100;
503 double img_error = 1;
504 double tmp_error = 0;
505 double out_value, out_error;
506 double dark_value, dark_error, dark_dit;
507
508 hdrl_image * in, * out, * cmp;
509 //cpl_mask *bpm;
510 cpl_frame * dark;
511 int chip = 1;
512 double dit = 10;
513
514 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
515 char *my_path = cpl_sprintf("%s/TEST_master_dark.fits", localdir);
516 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
517
518
519 // Case 1: Dark is 0, dit is the same as the image, i.e. no correction
520 dark_value = 0;
521 dark_error = 0;
522 dark_dit = dit;
523 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
524 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
525
526 out_value = img_value - dit/dark_dit * dark_value;
527 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
528 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
529 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
530
531 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
532 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
533
536 cpl_frame_delete(dark);
537
538 // Case 2: Dark is 0, dit is different, i.e. still nothing
539 dark_value = 0;
540 dark_error = 0;
541 dark_dit = dit * 2.3;
542 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
543 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
544
545 out_value = img_value - dit/dark_dit * dark_value;
546 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
547 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
548 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
549
550 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
551 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
552
555 cpl_frame_delete(dark);
556
557 // Case 3: Dark is 0, dit is same, but dark has error now
558 dark_value = 0;
559 dark_error = 2;
560 dark_dit = dit;
561 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
562 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
563
564 out_value = img_value - dit/dark_dit * dark_value;
565 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
566 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
567 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
568
569 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
570 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
571
574 cpl_frame_delete(dark);
575
576 // Case 4: Dark is constant 10, dit is same, and has error
577 dark_value = 10;
578 dark_error = 2;
579 dark_dit = dit;
580 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
581 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
582
583 out_value = img_value - dit/dark_dit * dark_value;
584 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
585 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
586 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
587
588 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
589 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
590
593 cpl_frame_delete(dark);
594
595 // Case 5: Dark is constant 10, dit is different, and has error
596 dark_value = 10;
597 dark_error = 2;
598 dark_dit = dit * 2.3;
599 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
600 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
601
602 out_value = img_value - dit/dark_dit * dark_value;
603 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
604 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
605 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
606
607 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
608 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
609
612 cpl_frame_delete(dark);
613
614 // Case 6: Dark is negative, dit positive
615 dark_value = -10;
616 dark_error = 2;
617 dark_dit = dit * 2.3;
618 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
619 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
620
621 out_value = img_value - dit/dark_dit * dark_value;
622 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
623 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
624 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
625
626 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
627 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
628
631 cpl_frame_delete(dark);
632
633 // Case 7: Dark is positive, dit negative
634 dark_value = 10;
635 dark_error = 2;
636 dark_dit = -dit * 2.3;
637 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
638 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
639
640 out_value = img_value - dit/dark_dit * dark_value;
641 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
642 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
643 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
644
645 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
646 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
647
650 cpl_frame_delete(dark);
651
652 // Case 8: Dark and dit negative
653 dark_value = -10;
654 dark_error = 2;
655 dark_dit = -dit * 2.3;
656 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
657 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
658
659 out_value = img_value - dit/dark_dit * dark_value;
660 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
661 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
662 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
663
664 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
665 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
666
669 cpl_frame_delete(dark);
670
671 // Case 9: Dark file does not exist
672 dark = cpl_frame_new();
673 cpl_frame_set_filename(dark, "TEST_tobeornottobe.fits");
674 cpl_frame_set_tag(dark, "DARK");
675 cpl_frame_set_group(dark, CPL_FRAME_GROUP_CALIB);
676
677 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
678 cpl_test_error(CPL_ERROR_FILE_IO);
679 cpl_test_null(out);
680 cpl_frame_delete(dark);
681
682 // Case 10: image is in a wrong group
683 dark = create_master_flat(my_path, nx, ny, 1, 0, NULL);
684 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
685 cpl_test_null(out);
686 cpl_frame_delete(dark);
687
688 // Case 11: No Filename set
689 dark = cpl_frame_new();
690 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
691 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
692 cpl_test_null(out);
693 cpl_frame_delete(dark);
694
695
696
697 // Case 12: DataFile is empty, i.e. only header
698 dark = cpl_frame_new();
699 cpl_frame_set_filename(dark, my_path2);
700 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
701 cpl_test_null(out);
702 cpl_frame_delete(dark);
703
704 // // Case 13: Check that bpm of dark is passed on to out
705 // dark_value = 10;
706 // dark_error = 0;
707 // dark_dit = 10;
708 // dark = create_master_dark("TEST_master_dark.fits", nx, ny, dark_value, dark_error, dark_dit, &bpm);
709 // out = cr2res_calib_image(in, chip, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
710
711 // cpl_test_eq_mask(bpm, hdrl_image_get_mask(out));
712
713 // cpl_mask_delete(bpm);
714 // hdrl_image_delete(out);
715 // cpl_frame_delete(dark);
716
717 cpl_free(my_path);
718 cpl_free(my_path2);
719
721}
722
723static void test_cr2res_calib_bpm()
724{
725 int nx = 5;
726 int ny = 5;
727 int badpix;
728
729 double img_value = 100;
730 double img_error = 1;
731 //double out_value, out_error;
732 //double bpm_value;
733 hdrl_image * hdrl;
734 hdrl_value value;
735 cpl_image * tmp;
736
737 hdrl_image * in, * out, * cmp;
738 cpl_frame * bpm;
739 int chip = 1;
740 double dit = 10;
741
742 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
743 value.data = 10; value.error = img_error;
744 hdrl_image_set_pixel(in, 2, 2, value);
745
746 char *my_path = cpl_sprintf("%s/TEST_master_bpm.fits", localdir);
747 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
748
749
750 // Case 1: BPM is all 0, i.e. all good pixels
751 bpm = create_bpm(my_path, nx, ny, 0);
752 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
753 cmp = hdrl_image_duplicate(in);
754 tmp = cpl_image_power_create(hdrl_image_get_image(cmp), 0.5);
755 cpl_image_divide_scalar(tmp, sqrt(CR2RES_GAIN_CHIP1));
756 cpl_image_add(hdrl_image_get_error(cmp), tmp);
757 cpl_image_delete(tmp);
758
759 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
760 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
761 cpl_frame_delete(bpm);
764
765 // Case 2: BPM is all 1, i.e all pixels are bad
766 bpm = create_bpm(my_path, nx, ny, 1);
767 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
768 cpl_test_nonnull(out);
769 cpl_test_eq(hdrl_image_count_rejected(out), nx * ny);
770 cpl_frame_delete(bpm);
772
773 // Case 3: One Bad Pixel
774 bpm = cpl_frame_new();
775 cpl_frame_set_filename(bpm, my_path);
776 cpl_frame_set_tag(bpm, "BPM");
777 cpl_frame_set_group(bpm, CPL_FRAME_GROUP_CALIB);
778
779 hdrl = cr2res_create_hdrl(nx, ny, 0, 0);
780 value.data = 1;
781 hdrl_image_set_pixel(hdrl, 2, 2, value);
782 save_hdrl(my_path, hdrl, MODE_BPM, 0);
783 hdrl_image_delete(hdrl);
784
785 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
786 cmp = hdrl_image_duplicate(in);
787 tmp = cpl_image_power_create(hdrl_image_get_image(cmp), 0.5);
788 cpl_image_divide_scalar(tmp, sqrt(CR2RES_GAIN_CHIP1));
789 cpl_image_add(hdrl_image_get_error(cmp), tmp);
790 cpl_image_delete(tmp);
791
792 cpl_test_eq(hdrl_image_count_rejected(out), 1);
793 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
794 // cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
795
796 for (size_t i = 1; i <= (size_t) nx; i++)
797 {
798 for (size_t j = 1; j <= (size_t) ny; j++)
799 {
800 if (!hdrl_image_is_rejected(out, i, j)){
801 cpl_test_abs(cpl_image_get(hdrl_image_get_error(out), i, j, &badpix),
802 cpl_image_get(hdrl_image_get_error(cmp), i, j, &badpix),
803 DBL_EPSILON);
804 }
805 }
806
807 }
808
809 cpl_frame_delete(bpm);
812
813 // Case 4: Only one good pixel
814 bpm = cpl_frame_new();
815 cpl_frame_set_filename(bpm, my_path);
816 cpl_frame_set_tag(bpm, "BPM");
817 cpl_frame_set_group(bpm, CPL_FRAME_GROUP_CALIB);
818
819 hdrl = cr2res_create_hdrl(nx, ny, 1, 0);
820 value.data = 0;
821 hdrl_image_set_pixel(hdrl, 2, 2, value);
822 save_hdrl(my_path, hdrl, MODE_BPM, 0);
823 hdrl_image_delete(hdrl);
824
825 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
826 cmp = hdrl_image_duplicate(in);
827 tmp = cpl_image_power_create(hdrl_image_get_image(cmp), 0.5);
828 cpl_image_divide_scalar(tmp, sqrt(CR2RES_GAIN_CHIP1));
829 cpl_image_add(hdrl_image_get_error(cmp), tmp);
830 cpl_image_delete(tmp);
831
832 cpl_test_eq(hdrl_image_count_rejected(out), nx*ny - 1);
833 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
834 // cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
835
836 for (size_t i = 1; i <= (size_t) nx; i++)
837 {
838 for (size_t j = 1; j <= (size_t) ny; j++)
839 {
840 if (!hdrl_image_is_rejected(out, i, j)){
841 cpl_test_abs(cpl_image_get(hdrl_image_get_error(out), i, j, &badpix),
842 cpl_image_get(hdrl_image_get_error(cmp), i, j, &badpix),
843 DBL_EPSILON);
844 }
845 }
846
847 }
848
849
850 cpl_frame_delete(bpm);
853
854 // Case 5: BPM file does not exist
855 bpm = cpl_frame_new();
856 cpl_frame_set_filename(bpm, "TEST_tobeornottobe.fits");
857 cpl_frame_set_tag(bpm, "BPM");
858 cpl_frame_set_group(bpm, CPL_FRAME_GROUP_CALIB);
859
860 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
861 cpl_test_error(CPL_ERROR_FILE_IO);
862 cpl_test_null(out);
863 cpl_frame_delete(bpm);
864
865 // Case 6: image is in a wrong group
866 bpm = create_master_flat(my_path, nx, ny, 1, 0, NULL);
867 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
868 cpl_test_null(out);
869 cpl_frame_delete(bpm);
870
871 // Case 7: No Filename set
872 bpm = cpl_frame_new();
873 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
874 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
875 cpl_test_null(out);
876 cpl_frame_delete(bpm);
877
878
879 // Case 8: DataFile is empty, i.e. only header
880 bpm = cpl_frame_new();
881 cpl_frame_set_filename(bpm, my_path2);
882 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
883 cpl_test_null(out);
884 cpl_frame_delete(bpm);
885
886 cpl_free(my_path);
887 cpl_free(my_path2);
888
890}
891
892static void test_cr2res_calib_detlin()
893{
894 int nx = 5;
895 int ny = 5;
896 //int badpix;
897
898 double img_value = 20000;
899 double img_error = 200;
900 double out_value, out_error;
901 //double bpm_value;
902 //hdrl_image * hdrl;
903 //hdrl_value value;
904
905 hdrl_image * in, * out, * cmp;
906 hdrl_image * ima, * imb, * imc;
907 double a, b, c, sa, sb, sc;
908 double i, si;
909 cpl_frame * detlin;
910 int chip = 1;
911 double dit = 10;
912
913 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
914 i = img_value; si = img_error;
915
916 char *my_path = cpl_sprintf("%s/TEST_master_detlin.fits", localdir);
917 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
918
919 // DetLin images aren't actually images, but polynomial coefficients for each pixel
920 // The new image is the old multiplied by the
921 // evaluation of that polynomial
922 // new image = old_image * detlin_poly(old_image)
923
924 // Case 1: DetLin b = c = 0, a = 1, doesn't change anything
925 a = 1; c = 0; b = 0;
926 sa = sb = sc = 0;
927 ima = cr2res_create_hdrl(nx, ny, a, sa);
928 imb = cr2res_create_hdrl(nx, ny, b, sb);
929 imc = cr2res_create_hdrl(nx, ny, c, sc);
930
931 detlin = create_detlin(my_path, ima, imb, imc);
932 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
933 cmp = hdrl_image_duplicate(in);
934 cpl_image_add_scalar(hdrl_image_get_error(cmp),
935 sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1));
936
937 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
938 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
940 cpl_frame_delete(detlin);
945
946 // Case 2: DetLin a = 1, c = 0, b = 1, x + x**2
947 a = b = 1; c = 0;
948 sa = sb = sc = 0;
949 ima = cr2res_create_hdrl(nx, ny, a, sa);
950 imb = cr2res_create_hdrl(nx, ny, b, sb);
951 imc = cr2res_create_hdrl(nx, ny, c, sc);
952 detlin = create_detlin(my_path, ima, imb, imc);
953 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
954 out_value = detlin(img_value, a, b, c);
955 out_error = deterr(img_value, a, b, c, img_error, sa, sb, sc);
956 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
957 cpl_image_add_scalar(hdrl_image_get_error(cmp),
958 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
959
960 cpl_test_image_abs(hdrl_image_get_image(out),
961 hdrl_image_get_image(cmp), DBL_EPSILON);
962 cpl_test_image_abs(hdrl_image_get_error(out),
963 hdrl_image_get_error(cmp), DBL_EPSILON);
966 cpl_frame_delete(detlin);
970
971
972 // Case 3: DetLin a = b = c = 0, everything is 0
973 a = c = b = 0;
974 sa = sb = sc = 0;
975 ima = cr2res_create_hdrl(nx, ny, a, sa);
976 imb = cr2res_create_hdrl(nx, ny, b, sb);
977 imc = cr2res_create_hdrl(nx, ny, c, sc);
978 detlin = create_detlin(my_path, ima, imb, imc);
979 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
980 cmp = cr2res_create_hdrl(nx, ny, 0, 0);
981 // Shot noise is also 0
982 cpl_test_image_abs(hdrl_image_get_image(out),
983 hdrl_image_get_image(cmp), DBL_EPSILON);
984 cpl_test_image_abs(hdrl_image_get_error(out),
985 hdrl_image_get_error(cmp), DBL_EPSILON);
988 cpl_frame_delete(detlin);
992
993 // Case 4: DetLin a = 0.5, c = b = 0, divide all values by 2, including the error
994 a = 0.5; b = c = 0;
995 sa = sb = sc = 0;
996 ima = cr2res_create_hdrl(nx, ny, a, sa);
997 imb = cr2res_create_hdrl(nx, ny, b, sb);
998 imc = cr2res_create_hdrl(nx, ny, c, sc);
999 detlin = create_detlin(my_path, ima, imb, imc);
1000 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1001 out_value = img_value * a;
1002 out_error = img_error * a;
1003 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1004 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1005 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1006
1007 cpl_test_image_abs(hdrl_image_get_image(out),
1008 hdrl_image_get_image(cmp), DBL_EPSILON);
1009 cpl_test_image_abs(hdrl_image_get_error(out),
1010 hdrl_image_get_error(cmp), DBL_EPSILON);
1011 hdrl_image_delete(out);
1012 hdrl_image_delete(cmp);
1013 cpl_frame_delete(detlin);
1014 hdrl_image_delete(ima);
1015 hdrl_image_delete(imb);
1016 hdrl_image_delete(imc);
1017
1018 // Case 5: DetLin a = 1, c = 0, b = 2
1019 // x = a + y * b
1020 a = 2; b = 0; c = 0;
1021 sa = sb = sc = 0;
1022 ima = cr2res_create_hdrl(nx, ny, a, sa);
1023 imb = cr2res_create_hdrl(nx, ny, b, sb);
1024 imc = cr2res_create_hdrl(nx, ny, c, sc);
1025 detlin = create_detlin(my_path, ima, imb, imc);
1026 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1027 out_value = detlin(i, a, b, c);
1028 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1029 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1030 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1031 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1032
1033 cpl_test_image_abs(hdrl_image_get_image(out),
1034 hdrl_image_get_image(cmp), DBL_EPSILON);
1035 cpl_test_image_abs(hdrl_image_get_error(out),
1036 hdrl_image_get_error(cmp), DBL_EPSILON);
1037 hdrl_image_delete(out);
1038 hdrl_image_delete(cmp);
1039 cpl_frame_delete(detlin);
1040 hdrl_image_delete(ima);
1041 hdrl_image_delete(imb);
1042 hdrl_image_delete(imc);
1043
1044 // Case 6: DetLin a = 1, c = 1, b = 2
1045 // x = a + y * b + y**2 * c
1046 a = c = 1; b = 2;
1047 sa = sb = sc = 0;
1048 ima = cr2res_create_hdrl(nx, ny, a, sa);
1049 imb = cr2res_create_hdrl(nx, ny, b, sb);
1050 imc = cr2res_create_hdrl(nx, ny, c, sc);
1051 detlin = create_detlin(my_path, ima, imb, imc);
1052 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1053 out_value = detlin(i, a, b, c);
1054 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1055 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1056 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1057 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1058
1059 cpl_test_image_abs(hdrl_image_get_image(out),
1060 hdrl_image_get_image(cmp), DBL_EPSILON);
1061 cpl_test_image_abs(hdrl_image_get_error(out),
1062 hdrl_image_get_error(cmp), DBL_EPSILON);
1063 hdrl_image_delete(out);
1064 hdrl_image_delete(cmp);
1065 cpl_frame_delete(detlin);
1066 hdrl_image_delete(ima);
1067 hdrl_image_delete(imb);
1068 hdrl_image_delete(imc);
1069
1070 // Case 7: DetLin a = 0, c = 0, b = 1, but now with error
1071 // x = b * y
1072 a = c = 0; b = 1;
1073 sa = sc = 0; sb = 1;
1074 ima = cr2res_create_hdrl(nx, ny, a, sa);
1075 imb = cr2res_create_hdrl(nx, ny, b, sb);
1076 imc = cr2res_create_hdrl(nx, ny, c, sc);
1077 detlin = create_detlin(my_path, ima, imb, imc);
1078 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1079 out_value = pow2(img_value);
1080 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1081 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1082 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1083 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1084
1085 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
1086 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
1087 hdrl_image_delete(out);
1088 hdrl_image_delete(cmp);
1089 cpl_frame_delete(detlin);
1090 hdrl_image_delete(ima);
1091 hdrl_image_delete(imb);
1092 hdrl_image_delete(imc);
1093
1094 // Case 8: DetLin a = 1, c = 0, b = 1, but now with error
1095 a = 1; c = 0; b = 1;
1096 sa = 1 ; sc = 0; sb = 1;
1097 ima = cr2res_create_hdrl(nx, ny, a, sa);
1098 imb = cr2res_create_hdrl(nx, ny, b, sb);
1099 imc = cr2res_create_hdrl(nx, ny, c, sc);
1100 detlin = create_detlin(my_path, ima, imb, imc);
1101 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1102 out_value = detlin(i, a, b, c);
1103 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1104 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1105 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1106 sqrt(out_value / CR2RES_GAIN_CHIP1));
1107
1108 cpl_test_image_abs(hdrl_image_get_image(out),
1109 hdrl_image_get_image(cmp), DBL_EPSILON);
1110 cpl_test_image_abs(hdrl_image_get_error(out),
1111 hdrl_image_get_error(cmp), DBL_EPSILON);
1112 hdrl_image_delete(out);
1113 hdrl_image_delete(cmp);
1114 cpl_frame_delete(detlin);
1115 hdrl_image_delete(ima);
1116 hdrl_image_delete(imb);
1117 hdrl_image_delete(imc);
1118
1119 // Case 9: DetLin a = 1, c = 1, b = 0, but now with error
1120 // x = a + c * y**2
1121 a = 1; c = 1; b = 0;
1122 sa = 1 ; sc = 1; sb = 0;
1123 i = img_value; si = img_error;
1124 ima = cr2res_create_hdrl(nx, ny, a, sa);
1125 imb = cr2res_create_hdrl(nx, ny, b, sb);
1126 imc = cr2res_create_hdrl(nx, ny, c, sc);
1127 detlin = create_detlin(my_path, ima, imb, imc);
1128 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1129 out_value = detlin(i, a, b, c);
1130 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1131 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1132 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1133 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1134
1135 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
1136 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
1137 hdrl_image_delete(out);
1138 hdrl_image_delete(cmp);
1139 cpl_frame_delete(detlin);
1140 hdrl_image_delete(ima);
1141 hdrl_image_delete(imb);
1142 hdrl_image_delete(imc);
1143
1144 // Case 10: DetLin a = 1, c = 1, b = 1, but now with error
1145 // x = a + b * y + c * y**2
1146 a = 1; c = 1; b = 1;
1147 sa = 1 ; sc = 1; sb = 1;
1148 i = img_value; si = img_error;
1149 ima = cr2res_create_hdrl(nx, ny, a, sa);
1150 imb = cr2res_create_hdrl(nx, ny, b, sb);
1151 imc = cr2res_create_hdrl(nx, ny, c, sc);
1152 detlin = create_detlin(my_path, ima, imb, imc);
1153 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1154 out_value = detlin(i, a, b, c);
1155 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1156 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1157 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1158 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1159
1160 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
1161 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), 1e-6);
1162 hdrl_image_delete(out);
1163 hdrl_image_delete(cmp);
1164 cpl_frame_delete(detlin);
1165 hdrl_image_delete(ima);
1166 hdrl_image_delete(imb);
1167 hdrl_image_delete(imc);
1168
1169 // Case 11: No Filename set
1170 detlin = cpl_frame_new();
1171 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1172 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
1173 cpl_test_null(out);
1174 cpl_frame_delete(detlin);
1175
1176
1177 // Case 12: DataFile is empty, i.e. only header
1178 detlin = cpl_frame_new();
1179 cpl_frame_set_filename(detlin, my_path2);
1180 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1181 cpl_test_null(out);
1182 cpl_frame_delete(detlin);
1183
1184 cpl_free(my_path);
1185 cpl_free(my_path2);
1186
1188
1189}
1190
1191/*----------------------------------------------------------------------------*/
1195/*----------------------------------------------------------------------------*/
1196int main(void)
1197{
1198 cpl_error_reset();
1199 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_DEBUG);
1200
1201 create_empty_fits();
1202
1203 test_cr2res_calib_image();
1204 test_cr2res_calib_cosmic();
1205 test_cr2res_calib_flat();
1206 test_cr2res_calib_dark();
1207 test_cr2res_calib_bpm();
1208 test_cr2res_calib_detlin();
1209
1210 return cpl_test_end(0);
1211}
hdrl_image * cr2res_calib_image(const hdrl_image *in, int chip, int clean_bad, int subtract_nolight_rows, int subtract_interorder_column, int cosmics_corr, const cpl_frame *flat, const cpl_frame *dark, const cpl_frame *bpm, const cpl_frame *detlin, double dit, int ndit)
The images calibration routine for a given chip.
Definition: cr2res_calib.c:147
int cr2res_io_save_MASTER_FLAT(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **master_flats, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a MASTER_FLAT.
Definition: cr2res_io.c:1639
int cr2res_io_save_MASTER_DARK(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **master_darks, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a MASTER_DARK.
Definition: cr2res_io.c:1491
int cr2res_io_save_DETLIN_COEFFS(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_imagelist **coeffs, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a DETLIN COEFFS.
Definition: cr2res_io.c:1523
int cr2res_io_save_BPM(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_image **bpms, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a BPM.
Definition: cr2res_io.c:1555
int hdrl_image_is_rejected(hdrl_image *self, cpl_size xpos, cpl_size ypos)
return if pixel is marked bad
Definition: hdrl_image.c:445
cpl_error_code hdrl_image_set_pixel(hdrl_image *self, cpl_size xpos, cpl_size ypos, hdrl_value value)
set pixel values of hdrl_image
Definition: hdrl_image.c:594
hdrl_image * hdrl_image_duplicate(const hdrl_image *himg)
copy hdrl_image
Definition: hdrl_image.c:391
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:157
cpl_error_code hdrl_image_add_scalar(hdrl_image *self, hdrl_value value)
Elementwise addition of a scalar to an image.
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:131
cpl_size hdrl_image_count_rejected(const hdrl_image *self)
return number of rejected pixels
Definition: hdrl_image.c:461
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
cpl_error_code hdrl_image_reject(hdrl_image *self, cpl_size xpos, cpl_size ypos)
mark pixel as bad
Definition: hdrl_image.c:427
hdrl_image * hdrl_image_new(cpl_size nx, cpl_size ny)
create new zero filled hdrl image
Definition: hdrl_image.c:311
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379
hdrl_image * hdrl_imagelist_unset(hdrl_imagelist *himlist, cpl_size pos)
Remove an image from an imagelist.
cpl_error_code hdrl_imagelist_set(hdrl_imagelist *himlist, hdrl_image *himg, cpl_size pos)
Insert an image into an imagelist.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.