CR2RE Pipeline Reference Manual 1.6.10
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 double calc_read_noise(double dit, int ndit, int chip)
276{
277 double min_dit = 1.427;
278 double lim_dit = 50.0;
279 double min_rn;
280 if (chip ==1){
281 min_rn = 11.0;
282 } else {
283 min_rn = 12.0;
284 }
285 double lim_rn = 6.0;
286 if(dit <= min_dit){
287 return min_rn/CR2RES_GAIN_CHIP1;
288 } else if(dit >= lim_dit) {
289 return lim_rn/CR2RES_GAIN_CHIP1;
290 } else {
291 double rn = min_rn + (lim_rn-min_rn)*(dit-min_dit)/(lim_dit-min_dit);
292 return rn/CR2RES_GAIN_CHIP1;
293 }
294}
295
296static void add_read_noise(hdrl_image * im, double dit, int chip)
297{
298 double read_noise = calc_read_noise(dit, 1, chip);
299 cpl_image_add_scalar(hdrl_image_get_error(im), read_noise);
300}
301
302static void add_read_noise_cpl(cpl_image * im, double dit, int chip)
303{
304 double read_noise = calc_read_noise(dit, 1, chip);
305 cpl_image_add_scalar(im, read_noise);
306}
307
308static void test_cr2res_calib_image()
309{
310 int nx = 5;
311 int ny = 5;
312 //int badpix;
313
314 double img_value = 100;
315 double img_error = 1;
316
317 hdrl_image * in = cr2res_create_hdrl(nx, ny, img_value, img_error);
318 int chip = 1;
319
320 char *my_path1 = cpl_sprintf("%s/TEST_master_flat.fits", localdir);
321 char *my_path2 = cpl_sprintf("%s/TEST_master_dark.fits", localdir);
322 char *my_path3 = cpl_sprintf("%s/TEST_bpm.fits", localdir);
323 cpl_frame * flat = create_master_flat(my_path1, nx, ny, 1, 0, NULL);
324 cpl_frame * dark = create_master_dark(my_path2, nx, ny, 10, 1, 10, NULL);
325 cpl_frame * bpm = create_bpm(my_path3, nx, ny, 0);
326 cpl_frame * detlin = NULL;
327 double dit = 10;
328
329 hdrl_image * out;
330 //hdrl_image * cmp;
331
332 // NULL input / output
333 out = cr2res_calib_image(NULL, chip, 0, 0, 1, 0, NULL, NULL, NULL, NULL,dit,1);
334 cpl_test_null(out);
335
336 out = cr2res_calib_image(in, 0, 0, 0, 1, 0, NULL, NULL, NULL, NULL, dit, 1);
337 cpl_test_null(out);
338
339 out = cr2res_calib_image(in, CR2RES_NB_DETECTORS + 1, 0, 0, 1, 0,
340 NULL, NULL, NULL, NULL, dit, 1);
341 cpl_test_null(out);
342
343 // No correction
344 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, NULL, NULL, NULL, NULL, dit, 1);
345 cpl_test_nonnull(out);
346 cpl_test_image_abs(hdrl_image_get_image(in), hdrl_image_get_image(out), DBL_EPSILON);
347
348 // add the shot noise error
349 cpl_image_add_scalar(hdrl_image_get_error(in), sqrt(img_value)/sqrt(CR2RES_GAIN_CHIP1));
350
351 add_read_noise(in, dit, chip);
352
353 cpl_test_image_abs(hdrl_image_get_error(in), hdrl_image_get_error(out), DBL_EPSILON);
355
356
358 cpl_frame_delete(flat);
359 cpl_frame_delete(dark);
360 cpl_frame_delete(bpm);
361 cpl_frame_delete(detlin);
362 cpl_free(my_path1) ;
363 cpl_free(my_path2) ;
364 cpl_free(my_path3) ;
365}
366
367/*----------------------------------------------------------------------------*/
371/*----------------------------------------------------------------------------*/
372static void test_cr2res_calib_cosmic()
373{
374 int nx = 5;
375 int ny = 5;
376 //int badpix;
377
378 double img_value = 100;
379 double img_error = 1;
380 //double out_value, out_error;
381 //double flat_value, flat_error;
382
383 hdrl_image * in, * out;// * cmp;
384 //cpl_frame * flat;
385 int chip = 1;
386 //int cosmics_corr;
387 double dit = 10;
388
389 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
390
391 // Case 1: No Cosmic Correction, i.e. nothing happens
392 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, NULL, NULL, NULL, NULL, dit, 1);
393 cpl_test_image_abs(hdrl_image_get_image(in), hdrl_image_get_image(out), DBL_EPSILON);
394 // add the shot noise
395 cpl_image_add_scalar(hdrl_image_get_error(in), sqrt(img_value)/sqrt(CR2RES_GAIN_CHIP1));
396
397 add_read_noise(in, dit, chip);
398
399 cpl_test_image_abs(hdrl_image_get_error(in), hdrl_image_get_error(out), DBL_EPSILON);
400
401 // Case 2: Cosmic Correction
402 // TODO when cosmic correction is implemented
403
406
407}
408
409
410/*----------------------------------------------------------------------------*/
414/*----------------------------------------------------------------------------*/
415static void test_cr2res_calib_flat()
416{
417 int nx = 5;
418 int ny = 5;
419 //int badpix;
420
421 double img_value = 100;
422 double img_error = 1;
423 double tmp_error = 0;
424 double out_value, out_error;
425 double flat_value, flat_error;
426
427 hdrl_image * in, * out, * cmp;
428 //cpl_mask * bpm;
429 cpl_frame * flat;
430 int chip = 1;
431 double dit = 10;
432 //cpl_error_code error;
433
434 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
435
436 // Case 1: Flat is just 1 and no error, i.e. no change
437 flat_value = 1;
438 flat_error = 0;
439 char *my_path1 = cpl_sprintf("%s/TEST_master_flat.fits", localdir);
440 flat = create_master_flat(my_path1, nx, ny, flat_value, flat_error, NULL);
441 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
442
443 out_value = img_value / flat_value;
444 out_error = sqrt( pow(img_error / flat_value, 2) + pow(img_value * flat_error/ (flat_value * flat_value), 2));
445 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
446 // add the shot noise
447 cpl_image_add_scalar(hdrl_image_get_error(cmp), sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
448
449 add_read_noise(cmp, dit, chip);
450
451 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
452 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
453
456 cpl_frame_delete(flat);
457
458 // Case 2: Flat is constant == 2, error 1
459 flat_value = 2;
460 flat_error = 1;
461 flat = create_master_flat(my_path1, nx, ny, flat_value, flat_error, NULL);
462 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
463
464 out_value = img_value / flat_value;
465 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
466
467 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
468
469 out_error = sqrt( pow(tmp_error / flat_value, 2) + pow(img_value * flat_error/ (flat_value * flat_value), 2));
470 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
471
472 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
473 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
474
477 cpl_frame_delete(flat);
478
479 // Case 3: Flat is 0, i.e. all pixels become bad
480 flat_value = 0;
481 flat_error = 1;
482 flat = create_master_flat(my_path1, nx, ny, flat_value, flat_error, NULL);
483 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
484
485 cpl_test_eq(nx * ny, hdrl_image_count_rejected(out));
486
488 cpl_frame_delete(flat);
489
490 // Case 4: Flat file does not exist
491 flat = cpl_frame_new();
492 cpl_frame_set_filename(flat, "TEST_tobeornottobe.fits");
493 cpl_frame_set_tag(flat, "FLAT");
494 cpl_frame_set_group(flat, CPL_FRAME_GROUP_CALIB);
495
496 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
497 cpl_test_error(CPL_ERROR_FILE_IO);
499 cpl_frame_delete(flat);
500
501 // Case 5: image is in a wrong group
502 flat = create_master_dark(my_path1, nx, ny, 1, 0, 10, NULL);
503 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
504 cpl_test_null(out);
505 cpl_frame_delete(flat);
506
507 // Case 6: No Filename set
508 flat = cpl_frame_new();
509 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
510 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
511 cpl_test_null(out);
512 cpl_frame_delete(flat);
513
514 // Case 7: DataFile is empty, i.e. only header
515 flat = cpl_frame_new();
516 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
517 cpl_frame_set_filename(flat, my_path2);
518 out = cr2res_calib_image(in, chip, 0, 0, 1, 0, flat, NULL, NULL, NULL, dit, 1);
519 cpl_test_null(out);
520 cpl_frame_delete(flat);
521
522 // // Case 8: BPM is set
523 // flat_value = 1;
524 // flat_error = 1;
525 // flat = create_master_flat("TEST_master_flat.fits", nx, ny, flat_value, flat_error, &bpm);
526 // out = cr2res_calib_image(in, chip, 0, 0, 0, flat, NULL, NULL, NULL, dit, 1);
527
528 // cpl_test_eq_mask(bpm, hdrl_image_get_mask(out));
529
530 // hdrl_image_delete(out);
531 // cpl_frame_delete(flat);
532 // cpl_mask_delete(bpm);
533
534
535 cpl_free(my_path1) ;
536 cpl_free(my_path2) ;
538}
539
540static void test_cr2res_calib_dark()
541{
542 int nx = 5;
543 int ny = 5;
544 //int badpix;
545
546 double img_value = 100;
547 double img_error = 1;
548 double tmp_error = 0;
549 double out_value, out_error;
550 double dark_value, dark_error, dark_dit;
551
552 hdrl_image * in, * out, * cmp;
553 //cpl_mask *bpm;
554 cpl_frame * dark;
555 int chip = 1;
556 double dit = 10;
557
558 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
559 char *my_path = cpl_sprintf("%s/TEST_master_dark.fits", localdir);
560 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
561
562
563 // Case 1: Dark is 0, dit is the same as the image, i.e. no correction
564 dark_value = 0;
565 dark_error = 0;
566 dark_dit = dit;
567 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
568 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
569
570 out_value = img_value - dit/dark_dit * dark_value;
571 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
572 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
573 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
574 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
575
576 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
577 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
578
581 cpl_frame_delete(dark);
582
583 // Case 2: Dark is 0, dit is different, i.e. still nothing
584 dark_value = 0;
585 dark_error = 0;
586 dark_dit = dit * 2.3;
587 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
588 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
589
590 out_value = img_value - dit/dark_dit * dark_value;
591 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
592 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
593 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
594 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
595
596 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
597 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
598
601 cpl_frame_delete(dark);
602
603 // Case 3: Dark is 0, dit is same, but dark has error now
604 dark_value = 0;
605 dark_error = 2;
606 dark_dit = dit;
607 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
608 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
609
610 out_value = img_value - dit/dark_dit * dark_value;
611 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
612 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
613 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
614 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
615
616 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
617 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
618
621 cpl_frame_delete(dark);
622
623 // Case 4: Dark is constant 10, dit is same, and has error
624 dark_value = 10;
625 dark_error = 2;
626 dark_dit = dit;
627 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
628 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
629
630 out_value = img_value - dit/dark_dit * dark_value;
631 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
632 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
633 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
634 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
635
636 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
637 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
638
641 cpl_frame_delete(dark);
642
643 // Case 5: Dark is constant 10, dit is different, and has error
644 dark_value = 10;
645 dark_error = 2;
646 dark_dit = dit * 2.3;
647 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
648 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
649
650 out_value = img_value - dit/dark_dit * dark_value;
651 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
652 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
653 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
654 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
655
656 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
657 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
658
661 cpl_frame_delete(dark);
662
663 // Case 6: Dark is negative, dit positive
664 dark_value = -10;
665 dark_error = 2;
666 dark_dit = dit * 2.3;
667 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
668 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
669
670 out_value = img_value - dit/dark_dit * dark_value;
671 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
672 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
673 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
674 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
675
676 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
677 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
678
681 cpl_frame_delete(dark);
682
683 // Case 7: Dark is positive, dit negative
684 dark_value = 10;
685 dark_error = 2;
686 dark_dit = -dit * 2.3;
687 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
688 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
689
690 out_value = img_value - dit/dark_dit * dark_value;
691 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
692 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
693 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
694 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
695
696 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
697 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
698
701 cpl_frame_delete(dark);
702
703 // Case 8: Dark and dit negative
704 dark_value = -10;
705 dark_error = 2;
706 dark_dit = -dit * 2.3;
707 dark = create_master_dark(my_path, nx, ny, dark_value, dark_error, dark_dit, NULL);
708 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
709
710 out_value = img_value - dit/dark_dit * dark_value;
711 tmp_error = img_error + sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1);
712 tmp_error = tmp_error + calc_read_noise(dit, 1, chip);
713 out_error = sqrt(pow(tmp_error, 2) + pow(dit/dark_dit * dark_error, 2));
714 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
715
716 cpl_test_image_abs(hdrl_image_get_image(cmp), hdrl_image_get_image(out), DBL_EPSILON);
717 cpl_test_image_abs(hdrl_image_get_error(cmp), hdrl_image_get_error(out), DBL_EPSILON);
718
721 cpl_frame_delete(dark);
722
723 // Case 9: Dark file does not exist
724 dark = cpl_frame_new();
725 cpl_frame_set_filename(dark, "TEST_tobeornottobe.fits");
726 cpl_frame_set_tag(dark, "DARK");
727 cpl_frame_set_group(dark, CPL_FRAME_GROUP_CALIB);
728
729 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
730 cpl_test_error(CPL_ERROR_FILE_IO);
731 cpl_test_null(out);
732 cpl_frame_delete(dark);
733
734 // Case 10: image is in a wrong group
735 dark = create_master_flat(my_path, nx, ny, 1, 0, NULL);
736 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
737 cpl_test_null(out);
738 cpl_frame_delete(dark);
739
740 // Case 11: No Filename set
741 dark = cpl_frame_new();
742 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
743 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
744 cpl_test_null(out);
745 cpl_frame_delete(dark);
746
747
748
749 // Case 12: DataFile is empty, i.e. only header
750 dark = cpl_frame_new();
751 cpl_frame_set_filename(dark, my_path2);
752 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
753 cpl_test_null(out);
754 cpl_frame_delete(dark);
755
756 // // Case 13: Check that bpm of dark is passed on to out
757 // dark_value = 10;
758 // dark_error = 0;
759 // dark_dit = 10;
760 // dark = create_master_dark("TEST_master_dark.fits", nx, ny, dark_value, dark_error, dark_dit, &bpm);
761 // out = cr2res_calib_image(in, chip, 0, 0, 0, NULL, dark, NULL, NULL, dit, 1);
762
763 // cpl_test_eq_mask(bpm, hdrl_image_get_mask(out));
764
765 // cpl_mask_delete(bpm);
766 // hdrl_image_delete(out);
767 // cpl_frame_delete(dark);
768
769 cpl_free(my_path);
770 cpl_free(my_path2);
771
773}
774
775static void test_cr2res_calib_bpm()
776{
777 int nx = 5;
778 int ny = 5;
779 int badpix;
780
781 double img_value = 100;
782 double img_error = 1;
783 //double out_value, out_error;
784 //double bpm_value;
785 hdrl_image * hdrl;
786 hdrl_value value;
787 cpl_image * tmp;
788
789 hdrl_image * in, * out, * cmp;
790 cpl_frame * bpm;
791 int chip = 1;
792 double dit = 10;
793
794 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
795 value.data = 10; value.error = img_error;
796 hdrl_image_set_pixel(in, 2, 2, value);
797
798 char *my_path = cpl_sprintf("%s/TEST_master_bpm.fits", localdir);
799 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
800
801
802 // Case 1: BPM is all 0, i.e. all good pixels
803 bpm = create_bpm(my_path, nx, ny, 0);
804 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
805 cmp = hdrl_image_duplicate(in);
806 tmp = cpl_image_power_create(hdrl_image_get_image(cmp), 0.5);
807 cpl_image_divide_scalar(tmp, sqrt(CR2RES_GAIN_CHIP1));
808 add_read_noise_cpl(tmp, dit, chip);
809 cpl_image_add(hdrl_image_get_error(cmp), tmp);
810 cpl_image_delete(tmp);
811
812 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
813 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
814 cpl_frame_delete(bpm);
817
818 // Case 2: BPM is all 1, i.e all pixels are bad
819 bpm = create_bpm(my_path, nx, ny, 1);
820 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
821 cpl_test_nonnull(out);
822 cpl_test_eq(hdrl_image_count_rejected(out), nx * ny);
823 cpl_frame_delete(bpm);
825
826 // Case 3: One Bad Pixel
827 bpm = cpl_frame_new();
828 cpl_frame_set_filename(bpm, my_path);
829 cpl_frame_set_tag(bpm, "BPM");
830 cpl_frame_set_group(bpm, CPL_FRAME_GROUP_CALIB);
831
832 hdrl = cr2res_create_hdrl(nx, ny, 0, 0);
833 value.data = 1;
834 hdrl_image_set_pixel(hdrl, 2, 2, value);
835 save_hdrl(my_path, hdrl, MODE_BPM, 0);
836 hdrl_image_delete(hdrl);
837
838 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
839 cmp = hdrl_image_duplicate(in);
840 tmp = cpl_image_power_create(hdrl_image_get_image(cmp), 0.5);
841 cpl_image_divide_scalar(tmp, sqrt(CR2RES_GAIN_CHIP1));
842 add_read_noise_cpl(tmp, dit, chip);
843 cpl_image_add(hdrl_image_get_error(cmp), tmp);
844 cpl_image_delete(tmp);
845
846 cpl_test_eq(hdrl_image_count_rejected(out), 1);
847 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
848 // cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
849
850 for (size_t i = 1; i <= (size_t) nx; i++)
851 {
852 for (size_t j = 1; j <= (size_t) ny; j++)
853 {
854 if (!hdrl_image_is_rejected(out, i, j)){
855 cpl_test_abs(cpl_image_get(hdrl_image_get_error(out), i, j, &badpix),
856 cpl_image_get(hdrl_image_get_error(cmp), i, j, &badpix),
857 DBL_EPSILON);
858 }
859 }
860
861 }
862
863 cpl_frame_delete(bpm);
866
867 // Case 4: Only one good pixel
868 bpm = cpl_frame_new();
869 cpl_frame_set_filename(bpm, my_path);
870 cpl_frame_set_tag(bpm, "BPM");
871 cpl_frame_set_group(bpm, CPL_FRAME_GROUP_CALIB);
872
873 hdrl = cr2res_create_hdrl(nx, ny, 1, 0);
874 value.data = 0;
875 hdrl_image_set_pixel(hdrl, 2, 2, value);
876 save_hdrl(my_path, hdrl, MODE_BPM, 0);
877 hdrl_image_delete(hdrl);
878
879 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
880 cmp = hdrl_image_duplicate(in);
881 tmp = cpl_image_power_create(hdrl_image_get_image(cmp), 0.5);
882 cpl_image_divide_scalar(tmp, sqrt(CR2RES_GAIN_CHIP1));
883 add_read_noise_cpl(tmp, dit, chip);
884 cpl_image_add(hdrl_image_get_error(cmp), tmp);
885 cpl_image_delete(tmp);
886
887 cpl_test_eq(hdrl_image_count_rejected(out), nx*ny - 1);
888 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
889 // cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
890
891 for (size_t i = 1; i <= (size_t) nx; i++)
892 {
893 for (size_t j = 1; j <= (size_t) ny; j++)
894 {
895 if (!hdrl_image_is_rejected(out, i, j)){
896 cpl_test_abs(cpl_image_get(hdrl_image_get_error(out), i, j, &badpix),
897 cpl_image_get(hdrl_image_get_error(cmp), i, j, &badpix),
898 DBL_EPSILON);
899 }
900 }
901
902 }
903
904
905 cpl_frame_delete(bpm);
908
909 // Case 5: BPM file does not exist
910 bpm = cpl_frame_new();
911 cpl_frame_set_filename(bpm, "TEST_tobeornottobe.fits");
912 cpl_frame_set_tag(bpm, "BPM");
913 cpl_frame_set_group(bpm, CPL_FRAME_GROUP_CALIB);
914
915 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
916 cpl_test_error(CPL_ERROR_FILE_IO);
917 cpl_test_null(out);
918 cpl_frame_delete(bpm);
919
920 // Case 6: image is in a wrong group
921 bpm = create_master_flat(my_path, nx, ny, 1, 0, NULL);
922 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
923 cpl_test_null(out);
924 cpl_frame_delete(bpm);
925
926 // Case 7: No Filename set
927 bpm = cpl_frame_new();
928 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
929 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
930 cpl_test_null(out);
931 cpl_frame_delete(bpm);
932
933
934 // Case 8: DataFile is empty, i.e. only header
935 bpm = cpl_frame_new();
936 cpl_frame_set_filename(bpm, my_path2);
937 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, bpm, NULL, dit, 1);
938 cpl_test_null(out);
939 cpl_frame_delete(bpm);
940
941 cpl_free(my_path);
942 cpl_free(my_path2);
943
945}
946
947static void test_cr2res_calib_detlin()
948{
949 int nx = 5;
950 int ny = 5;
951 //int badpix;
952
953 double img_value = 20000;
954 double img_error = 200;
955 double out_value, out_error;
956 //double bpm_value;
957 //hdrl_image * hdrl;
958 //hdrl_value value;
959
960 hdrl_image * in, * out, * cmp;
961 hdrl_image * ima, * imb, * imc;
962 double a, b, c, sa, sb, sc;
963 double i, si;
964 cpl_frame * detlin;
965 int chip = 1;
966 double dit = 10;
967
968 in = cr2res_create_hdrl(nx, ny, img_value, img_error);
969 i = img_value; si = img_error;
970
971 char *my_path = cpl_sprintf("%s/TEST_master_detlin.fits", localdir);
972 char *my_path2 = cpl_sprintf("%s/TEST_empty.fits", localdir);
973
974 // DetLin images aren't actually images, but polynomial coefficients for each pixel
975 // The new image is the old multiplied by the
976 // evaluation of that polynomial
977 // new image = old_image * detlin_poly(old_image)
978
979 // Case 1: DetLin b = c = 0, a = 1, doesn't change anything
980 a = 1; c = 0; b = 0;
981 sa = sb = sc = 0;
982 ima = cr2res_create_hdrl(nx, ny, a, sa);
983 imb = cr2res_create_hdrl(nx, ny, b, sb);
984 imc = cr2res_create_hdrl(nx, ny, c, sc);
985
986 detlin = create_detlin(my_path, ima, imb, imc);
987 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
988 cmp = hdrl_image_duplicate(in);
989 cpl_image_add_scalar(hdrl_image_get_error(cmp),
990 sqrt(img_value) / sqrt(CR2RES_GAIN_CHIP1));
991
992 add_read_noise(cmp, dit, chip);
993
994 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
995 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
997 cpl_frame_delete(detlin);
1000 hdrl_image_delete(imc);
1001 hdrl_image_delete(cmp);
1002
1003 // Case 2: DetLin a = 1, c = 0, b = 1, x + x**2
1004 a = b = 1; c = 0;
1005 sa = sb = sc = 0;
1006 ima = cr2res_create_hdrl(nx, ny, a, sa);
1007 imb = cr2res_create_hdrl(nx, ny, b, sb);
1008 imc = cr2res_create_hdrl(nx, ny, c, sc);
1009 detlin = create_detlin(my_path, ima, imb, imc);
1010 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1011 out_value = detlin(img_value, a, b, c);
1012 out_error = deterr(img_value, a, b, c, img_error, sa, sb, sc);
1013 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1014 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1015 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1016
1017 add_read_noise(cmp, dit, chip);
1018
1019 cpl_test_image_abs(hdrl_image_get_image(out),
1020 hdrl_image_get_image(cmp), DBL_EPSILON);
1021 cpl_test_image_abs(hdrl_image_get_error(out),
1022 hdrl_image_get_error(cmp), 1e-9);
1023 hdrl_image_delete(out);
1024 hdrl_image_delete(cmp);
1025 cpl_frame_delete(detlin);
1026 hdrl_image_delete(ima);
1027 hdrl_image_delete(imb);
1028 hdrl_image_delete(imc);
1029
1030
1031 // Case 3: DetLin a = b = c = 0, everything is 0
1032 a = c = b = 0;
1033 sa = sb = sc = 0;
1034 ima = cr2res_create_hdrl(nx, ny, a, sa);
1035 imb = cr2res_create_hdrl(nx, ny, b, sb);
1036 imc = cr2res_create_hdrl(nx, ny, c, sc);
1037 detlin = create_detlin(my_path, ima, imb, imc);
1038 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1039 cmp = cr2res_create_hdrl(nx, ny, 0, 0);
1040 add_read_noise(cmp, dit, chip);
1041 // Shot noise is also 0
1042 cpl_test_image_abs(hdrl_image_get_image(out),
1043 hdrl_image_get_image(cmp), DBL_EPSILON);
1044 cpl_test_image_abs(hdrl_image_get_error(out),
1045 hdrl_image_get_error(cmp), 1e-14);
1046 hdrl_image_delete(out);
1047 hdrl_image_delete(cmp);
1048 cpl_frame_delete(detlin);
1049 hdrl_image_delete(ima);
1050 hdrl_image_delete(imb);
1051 hdrl_image_delete(imc);
1052
1053 // Case 4: DetLin a = 0.5, c = b = 0, divide all values by 2, including the error
1054 a = 0.5; b = c = 0;
1055 sa = sb = sc = 0;
1056 ima = cr2res_create_hdrl(nx, ny, a, sa);
1057 imb = cr2res_create_hdrl(nx, ny, b, sb);
1058 imc = cr2res_create_hdrl(nx, ny, c, sc);
1059 detlin = create_detlin(my_path, ima, imb, imc);
1060 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1061 out_value = img_value * a;
1062 out_error = img_error * a;
1063 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1064 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1065 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1066
1067 add_read_noise(cmp, dit, chip);
1068
1069 cpl_test_image_abs(hdrl_image_get_image(out),
1070 hdrl_image_get_image(cmp), DBL_EPSILON);
1071 cpl_test_image_abs(hdrl_image_get_error(out),
1072 hdrl_image_get_error(cmp), DBL_EPSILON);
1073 hdrl_image_delete(out);
1074 hdrl_image_delete(cmp);
1075 cpl_frame_delete(detlin);
1076 hdrl_image_delete(ima);
1077 hdrl_image_delete(imb);
1078 hdrl_image_delete(imc);
1079
1080 // Case 5: DetLin a = 1, c = 0, b = 2
1081 // x = a + y * b
1082 a = 2; b = 0; c = 0;
1083 sa = sb = sc = 0;
1084 ima = cr2res_create_hdrl(nx, ny, a, sa);
1085 imb = cr2res_create_hdrl(nx, ny, b, sb);
1086 imc = cr2res_create_hdrl(nx, ny, c, sc);
1087 detlin = create_detlin(my_path, ima, imb, imc);
1088 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1089 out_value = detlin(i, a, b, c);
1090 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1091 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1092 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1093 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1094
1095 add_read_noise(cmp, dit, chip);
1096
1097 cpl_test_image_abs(hdrl_image_get_image(out),
1098 hdrl_image_get_image(cmp), DBL_EPSILON);
1099 cpl_test_image_abs(hdrl_image_get_error(out),
1100 hdrl_image_get_error(cmp), DBL_EPSILON);
1101 hdrl_image_delete(out);
1102 hdrl_image_delete(cmp);
1103 cpl_frame_delete(detlin);
1104 hdrl_image_delete(ima);
1105 hdrl_image_delete(imb);
1106 hdrl_image_delete(imc);
1107
1108 // Case 6: DetLin a = 1, c = 1, b = 2
1109 // x = a + y * b + y**2 * c
1110 a = c = 1; b = 2;
1111 sa = sb = sc = 0;
1112 ima = cr2res_create_hdrl(nx, ny, a, sa);
1113 imb = cr2res_create_hdrl(nx, ny, b, sb);
1114 imc = cr2res_create_hdrl(nx, ny, c, sc);
1115 detlin = create_detlin(my_path, ima, imb, imc);
1116 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1117 out_value = detlin(i, a, b, c);
1118 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1119 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1120 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1121 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1122
1123 add_read_noise(cmp, dit, chip);
1124
1125 cpl_test_image_abs(hdrl_image_get_image(out),
1126 hdrl_image_get_image(cmp), DBL_EPSILON);
1127 cpl_test_image_abs(hdrl_image_get_error(out),
1128 hdrl_image_get_error(cmp), DBL_EPSILON);
1129 hdrl_image_delete(out);
1130 hdrl_image_delete(cmp);
1131 cpl_frame_delete(detlin);
1132 hdrl_image_delete(ima);
1133 hdrl_image_delete(imb);
1134 hdrl_image_delete(imc);
1135
1136 // Case 7: DetLin a = 0, c = 0, b = 1, but now with error
1137 // x = b * y
1138 a = c = 0; b = 1;
1139 sa = sc = 0; sb = 1;
1140 ima = cr2res_create_hdrl(nx, ny, a, sa);
1141 imb = cr2res_create_hdrl(nx, ny, b, sb);
1142 imc = cr2res_create_hdrl(nx, ny, c, sc);
1143 detlin = create_detlin(my_path, ima, imb, imc);
1144 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1145 out_value = pow2(img_value);
1146 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1147 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1148 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1149 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1150
1151 add_read_noise(cmp, dit, chip);
1152
1153 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
1154 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), 1e-6);
1155 hdrl_image_delete(out);
1156 hdrl_image_delete(cmp);
1157 cpl_frame_delete(detlin);
1158 hdrl_image_delete(ima);
1159 hdrl_image_delete(imb);
1160 hdrl_image_delete(imc);
1161
1162 // Case 8: DetLin a = 1, c = 0, b = 1, but now with error
1163 a = 1; c = 0; b = 1;
1164 sa = 1 ; sc = 0; sb = 1;
1165 ima = cr2res_create_hdrl(nx, ny, a, sa);
1166 imb = cr2res_create_hdrl(nx, ny, b, sb);
1167 imc = cr2res_create_hdrl(nx, ny, c, sc);
1168 detlin = create_detlin(my_path, ima, imb, imc);
1169 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1170 out_value = detlin(i, a, b, c);
1171 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1172 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1173 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1174 sqrt(out_value / CR2RES_GAIN_CHIP1));
1175
1176 add_read_noise(cmp, dit, chip);
1177
1178 cpl_test_image_abs(hdrl_image_get_image(out),
1179 hdrl_image_get_image(cmp), DBL_EPSILON);
1180 cpl_test_image_abs(hdrl_image_get_error(out),
1181 hdrl_image_get_error(cmp), DBL_EPSILON);
1182 hdrl_image_delete(out);
1183 hdrl_image_delete(cmp);
1184 cpl_frame_delete(detlin);
1185 hdrl_image_delete(ima);
1186 hdrl_image_delete(imb);
1187 hdrl_image_delete(imc);
1188
1189 // Case 9: DetLin a = 1, c = 1, b = 0, but now with error
1190 // x = a + c * y**2
1191 a = 1; c = 1; b = 0;
1192 sa = 1 ; sc = 1; sb = 0;
1193 i = img_value; si = img_error;
1194 ima = cr2res_create_hdrl(nx, ny, a, sa);
1195 imb = cr2res_create_hdrl(nx, ny, b, sb);
1196 imc = cr2res_create_hdrl(nx, ny, c, sc);
1197 detlin = create_detlin(my_path, ima, imb, imc);
1198 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1199 out_value = detlin(i, a, b, c);
1200 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1201 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1202 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1203 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1204
1205 add_read_noise(cmp, dit, chip);
1206
1207 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
1208 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), DBL_EPSILON);
1209 hdrl_image_delete(out);
1210 hdrl_image_delete(cmp);
1211 cpl_frame_delete(detlin);
1212 hdrl_image_delete(ima);
1213 hdrl_image_delete(imb);
1214 hdrl_image_delete(imc);
1215
1216 // Case 10: DetLin a = 1, c = 1, b = 1, but now with error
1217 // x = a + b * y + c * y**2
1218 a = 1; c = 1; b = 1;
1219 sa = 1 ; sc = 1; sb = 1;
1220 i = img_value; si = img_error;
1221 ima = cr2res_create_hdrl(nx, ny, a, sa);
1222 imb = cr2res_create_hdrl(nx, ny, b, sb);
1223 imc = cr2res_create_hdrl(nx, ny, c, sc);
1224 detlin = create_detlin(my_path, ima, imb, imc);
1225 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1226 out_value = detlin(i, a, b, c);
1227 out_error = deterr(i, a, b, c, si, sa, sb, sc);
1228 cmp = cr2res_create_hdrl(nx, ny, out_value, out_error);
1229 cpl_image_add_scalar(hdrl_image_get_error(cmp),
1230 sqrt(out_value)/sqrt(CR2RES_GAIN_CHIP1));
1231
1232 add_read_noise(cmp, dit, chip);
1233
1234 cpl_test_image_abs(hdrl_image_get_image(out), hdrl_image_get_image(cmp), DBL_EPSILON);
1235 cpl_test_image_abs(hdrl_image_get_error(out), hdrl_image_get_error(cmp), 1e-6);
1236 hdrl_image_delete(out);
1237 hdrl_image_delete(cmp);
1238 cpl_frame_delete(detlin);
1239 hdrl_image_delete(ima);
1240 hdrl_image_delete(imb);
1241 hdrl_image_delete(imc);
1242
1243 // Case 11: No Filename set
1244 detlin = cpl_frame_new();
1245 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1246 cpl_test_error(CPL_ERROR_DATA_NOT_FOUND);
1247 cpl_test_null(out);
1248 cpl_frame_delete(detlin);
1249
1250
1251 // Case 12: DataFile is empty, i.e. only header
1252 detlin = cpl_frame_new();
1253 cpl_frame_set_filename(detlin, my_path2);
1254 out = cr2res_calib_image(in, chip, 0, 0, 0, 0, NULL, NULL, NULL, detlin, dit, 1);
1255 cpl_test_null(out);
1256 cpl_frame_delete(detlin);
1257
1258 cpl_free(my_path);
1259 cpl_free(my_path2);
1260
1262
1263}
1264
1265/*----------------------------------------------------------------------------*/
1269/*----------------------------------------------------------------------------*/
1270int main(void)
1271{
1272 cpl_error_reset();
1273 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_DEBUG);
1274
1275 create_empty_fits();
1276
1277 test_cr2res_calib_image();
1278 test_cr2res_calib_cosmic();
1279 test_cr2res_calib_flat();
1280 test_cr2res_calib_dark();
1281 test_cr2res_calib_bpm();
1282 test_cr2res_calib_detlin();
1283
1284 return cpl_test_end(0);
1285}
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:148
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.