ERIS Pipeline Reference Manual 1.8.15
eris_utils.c
1/* $Id: eris_utils.c,v 1.12 2013-03-25 11:46:49 cgarcia Exp $
2 *
3 * This file is part of the ERIS Pipeline
4 * Copyright (C) 2002,2003 European Southern Observatory
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * $Author: cgarcia $
23 * $Date: 2013-03-25 11:46:49 $
24 * $Revision: 1.12 $
25 * $Name: not supported by cvs2svn $
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32/*-----------------------------------------------------------------------------
33 Includes
34 -----------------------------------------------------------------------------*/
35#include <string.h>
36#include "eris_utils.h"
37#include <math.h>
38/*----------------------------------------------------------------------------*/
42/*----------------------------------------------------------------------------*/
43
55cpl_error_code
56eris_check_error_code(const char* func_id) {
57
58 if(cpl_error_get_code() != CPL_ERROR_NONE) {
59 cpl_msg_info(cpl_func,"Function %s has an error",func_id);
60 cpl_msg_info(cpl_func, "%s",(const char*) cpl_error_get_message());
61 cpl_msg_info(cpl_func, "%s",(const char*) cpl_error_get_where());
62 //exit(0);
63 }
64 return cpl_error_get_code();
65}
66/*---------------------------------------------------------------------------*/
67/* AMO added (useful to debug)
68 @brief Print recipe status
69 @param val reference point
70 @return status
71 */
72/*---------------------------------------------------------------------------*/
73int eris_print_rec_status(const int val) {
74 if(cpl_error_get_code() != CPL_ERROR_NONE) {
75 cpl_msg_error(cpl_func, "Recipe status at %d",val);
76 cpl_msg_error(cpl_func, "%s",(const char*) cpl_error_get_message());
77 cpl_msg_error(cpl_func, "%s",(const char*) cpl_error_get_where());
78 //exit(0);
79 return -1;
80 }
81 return 0;
82}
83
84
85/*---------------------------------------------------------------------------*/
86/* AMO added
87 * TODO: plist not used. Why?
88 @brief function to create proper FITS header
89 @param filename of product to be saved
90 @param pro_catg of product to be saved
91 @param frame_type frame type
92 @param frameset input frameset
93 @param parlist input parameterlist
94 @param plist
95 @return status
96 */
97/*---------------------------------------------------------------------------*/
98cpl_error_code eris_setup_product_header(const char* filename,
99 const char* pro_catg,
100 cpl_frame_type frame_type,
101 const char* recipe_name,
102 cpl_frameset* frameset,
103 const cpl_parameterlist* parlist,
104 cpl_propertylist *plist)
105{
106
107 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
108 cpl_ensure_code(pro_catg, CPL_ERROR_NULL_INPUT);
109 cpl_ensure_code(recipe_name, CPL_ERROR_NULL_INPUT);
110 cpl_ensure_code(frameset, CPL_ERROR_NULL_INPUT);
111 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
112 cpl_ensure_code(plist, CPL_ERROR_NULL_INPUT);
113
114 cpl_frame* frame = cpl_frame_new();
115 cpl_frame_set_filename(frame, filename);
116 cpl_frame_set_type(frame, frame_type);
117 cpl_frame_set_tag(frame, pro_catg);
118 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT);
119 cpl_frame_set_level(frame, CPL_FRAME_LEVEL_FINAL);
120 cpl_frameset_insert(frameset, frame);
121 cpl_dfs_setup_product_header(plist, frame, frameset, parlist,
122 recipe_name, PACKAGE "/" PACKAGE_VERSION,
123 CPL_DFS_PRO_DID, NULL);
124
125 eris_check_error_code("eris_setup_product_header");
126 return cpl_error_get_code();
127}
128
129/*----------------------------------------------------------------------------*/
137/*----------------------------------------------------------------------------*/
138const char * eris_get_license(void)
139{
140 const char * eris_license = cpl_get_license("ERIS", "2017");
141
142 return eris_license;
143}
144long
145eris_image_get_threshpix(cpl_image* img, double threshold,
146 cpl_boolean thresh_is_min)
147{
148 double* data = cpl_image_get_data_double(img);
149 cpl_size sx = cpl_image_get_size_x(img);
150 cpl_size sy = cpl_image_get_size_y(img);
151 long npix = sx * sy;
152 long nthreshold = 0;
153 //cpl_msg_info(cpl_func,"sx: %lld sy: %lld npix: %ld", sx, sy, npix);
154
155 if(thresh_is_min) {
156 /* count pixels above saturation */
157 for(long i = 0; i < npix; i++) {
158 if(data[i] > threshold) {
159 nthreshold++;
160 }
161 }
162 //cpl_msg_info(cpl_func,"count pixels above saturation: %ld",nthreshold);
163 } else {
164 //cpl_msg_info(cpl_func,"negative threshold: %g",threshold);
165 /* count pixels below saturation */
166 //cpl_msg_info(cpl_func,"count pixels below saturation: %ld",nthreshold);
167 for(long i = 0; i < npix; i++) {
168 if(data[i] <= threshold) {
169 nthreshold++;
170 }
171 }
172 //cpl_msg_info(cpl_func,"count pixels below saturation: %ld",nthreshold);
173 }
174
175 return nthreshold;
176}
177
178cpl_error_code
179eris_image_flag_threshpix(cpl_image** img, double threshold,
180 cpl_boolean thresh_is_min)
181{
182
183 cpl_ensure_code(img != NULL, CPL_ERROR_NULL_INPUT);
184
185 double* data = cpl_image_get_data_double(*img);
186 cpl_size sx = cpl_image_get_size_x(*img);
187 cpl_size sy = cpl_image_get_size_y(*img);
188 cpl_mask* mask = cpl_image_get_bpm(*img);
189 cpl_binary* pbpm = cpl_mask_get_data(mask);
190 long npix = sx * sy;
191
192 //cpl_msg_info(cpl_func,"sx: %lld sy: %lld npix: %ld", sx, sy, npix);
193
194 if(thresh_is_min) {
195 /* count pixels above saturation */
196 for(long i = 0; i < npix; i++) {
197 if(data[i] > threshold) {
198 pbpm[i] = CPL_BINARY_1;
199 }
200 }
201
202 } else {
203 //cpl_msg_info(cpl_func,"negative threshold: %g",threshold);
204 /* count pixels below saturation */
205
206 for(long i = 0; i < npix; i++) {
207 if(data[i] <= threshold) {
208 pbpm[i] = CPL_BINARY_1;
209
210 }
211 }
212
213 }
214
215 return cpl_error_get_code();
216}
217/*---------------------------------------------------------------------------*/
225/*---------------------------------------------------------------------------*/
226cpl_frameset*
227eris_dfs_extract_frames_with_tag (cpl_frameset * input, const char* rtag)
228{
229
230 cpl_ensure(input != NULL, CPL_ERROR_NULL_INPUT, NULL);
231
232 cpl_frame *cur_frame = NULL;
233
234 cpl_size nfrm = 0;
235 const char* tag = NULL;
236 cpl_frameset* out = cpl_frameset_new();
237 /* Loop on frames */
238 nfrm = cpl_frameset_get_size(input);
239 for (cpl_size i = 0; i < nfrm; i++) {
240 cur_frame=cpl_frameset_get_position(input,i);
241 tag = cpl_frame_get_tag(cur_frame);
242 /* RAW frames */
243 if (strcmp(tag, rtag) == 0) {
244 cpl_frameset_insert (out, cpl_frame_duplicate(cur_frame));
245 }
246 }
247
248 return out;
249
250}
251/*---------------------------------------------------------------------------*/
258/*---------------------------------------------------------------------------*/
259cpl_error_code
260eris_dfs_extract_raw_frames (cpl_frameset * input, cpl_frameset * raws)
261{
262 cpl_frame *cur_frame = NULL;
263
264 cpl_size nfrm = 0;
265 /* Loop on frames */
266 nfrm = cpl_frameset_get_size(input);
267 for (cpl_size i = 0; i < nfrm; i++) {
268 cur_frame=cpl_frameset_get_position(input,i);
269 /* RAW frames */
270 if (cpl_frame_get_group (cur_frame) == CPL_FRAME_GROUP_RAW) {
271 cpl_frameset_insert (raws, cpl_frame_duplicate(cur_frame));
272 }
273 }
274
275 return cpl_error_get_code();
276
277}
278/*---------------------------------------------------------------------------*/
285/*---------------------------------------------------------------------------*/
286cpl_error_code
287eris_dfs_extract_cal_frames (cpl_frameset * input, cpl_frameset * calibs)
288{
289 cpl_frame *cur_frame = NULL;
290
291 cpl_size nfrm = 0;
292 /* Loop on frames */
293 nfrm = cpl_frameset_get_size(input);
294 for (cpl_size i = 0; i < nfrm; i++) {
295 cur_frame=cpl_frameset_get_position(input,i);
296 /* RAW frames */
297 if (cpl_frame_get_group (cur_frame) == CPL_FRAME_GROUP_CALIB) {
298 cpl_frameset_insert (calibs, cpl_frame_duplicate(cur_frame));
299 }
300 }
301
302 return cpl_error_get_code();
303
304}
305
306cpl_error_code
307eris_dfs_check_input_tags(cpl_frameset * input, const char** tags, const int n,
308 const int mode)
309{
310 cpl_ensure_code(input, CPL_ERROR_NULL_INPUT);
311 cpl_ensure_code(tags, CPL_ERROR_NULL_INPUT);
312 int found = 0;
313 for(cpl_size i = 0; i < n; i++) {
314 found = cpl_frameset_count_tags(input, tags[i]);
315 if(found == 0) {
316 if(mode == 1) {
317 cpl_msg_error(cpl_func,"Missing required input FITS file with tag: %s", tags[i]);
318 return CPL_ERROR_ILLEGAL_INPUT;
319 } else if (mode == 0) {
320 cpl_msg_info(cpl_func,"Missing optional input FITS file with tag: %s", tags[i]);
321 return CPL_ERROR_NONE;
322 }
323
324 }
325 }
326
327 return cpl_error_get_code();
328}
329/*
330 * @brief count number of pixels that are outside threshold
331 * @param img the input image
332 * @param threshold for QC parameters computation
333 * @param thresh_is_min check if the threshold is min or max
334 * @return cpl error code
335 */
336
337static long
338eris_persistence_get_threshpix(cpl_image* img, double threshold,
339 cpl_boolean thresh_is_min)
340{
341 double* data = cpl_image_get_data_double(img);
342 cpl_size sx = cpl_image_get_size_x(img);
343 cpl_size sy = cpl_image_get_size_y(img);
344 long npix = sx * sy;
345 long nthreshold = 0;
346 //cpl_msg_info(cpl_func,"sx: %lld sy: %lld npix: %ld", sx, sy, npix);
347
348 if(thresh_is_min) {
349 /* count pixels above saturation */
350 for(long i = 0; i < npix; i++) {
351 if(data[i] > threshold) {
352 nthreshold++;
353 }
354 }
355 } else {
356 //cpl_msg_info(cpl_func,"negative threshold: %g",threshold);
357 /* count pixels above saturation */
358 for(long i = 0; i < npix; i++) {
359 if(data[i] <= threshold) {
360 nthreshold++;
361 }
362 }
363 }
364
365 return nthreshold;
366}
367/*
368 * @brief count number of pixels that are outside threshold
369 * @param frame_name the input frame filename
370 * @param frm_mdark the input dark frame
371 * @param threshold for QC parameters computation
372 * @param i extension id
373 * @param propertylist where QC params are stored
374 * @note image is expected in extention
375 * @return cpl error code
376 */
377cpl_error_code
378eris_get_thresh_pix_qc_for_image(const char* frame_name, cpl_frame* frm_mdark,
379 const double threshold, const long long i, cpl_propertylist* applist)
380{
381
382 cpl_ensure_code(frame_name, CPL_ERROR_NULL_INPUT);
383 cpl_ensure_code(frm_mdark, CPL_ERROR_NULL_INPUT);
384 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
385
386 cpl_msg_info(cpl_func,"Image case");
387
388 long int nthreshold = 0;
389 int plane_nthresh_max = 0;
390
391 const char* fname = cpl_frame_get_filename(frm_mdark);
392
393 cpl_image* mdark = cpl_image_load(fname, CPL_TYPE_DOUBLE, 0, 1);
394 cpl_size sx = cpl_image_get_size_x(mdark);
395 cpl_size sy = cpl_image_get_size_y(mdark);
396 cpl_size npix = sx * sy;
397
398 cpl_image* img = cpl_image_load(frame_name, CPL_TYPE_DOUBLE, 0, 0 );
399
400 cpl_image_subtract(img, mdark);
401
402 nthreshold = eris_persistence_get_threshpix(img, threshold, CPL_TRUE);
403
404 cpl_msg_info(cpl_func,"Frame %lld Threshold pixels: %ld", i, nthreshold);
405
406 cpl_image_delete(img);
407 cpl_image_delete(mdark);
408
409 /* Add a QC parameter */
410 char* keyname;
411 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH",i);
412 cpl_propertylist_append_int(applist, keyname, nthreshold);
413 cpl_free(keyname);
414
415 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH MAX",i);
416 cpl_propertylist_append_int(applist, keyname, nthreshold);
417 cpl_free(keyname);
418
419 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH TOT",i);
420 cpl_propertylist_append_int(applist, keyname, nthreshold);
421 cpl_free(keyname);
422
423 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 BPTHRESH FRAC",i);
424 cpl_propertylist_append_double(applist, keyname, (double)nthreshold / npix);
425 cpl_free(keyname);
426
427 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH AVG",i);
428 cpl_propertylist_append_int(applist, keyname, nthreshold);
429 cpl_free(keyname);
430
431 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 SLICE NTHRESH MAX",i);
432 cpl_propertylist_append_int(applist, keyname, plane_nthresh_max);
433 cpl_free(keyname);
434
435
436 return cpl_error_get_code();
437}
438
439/*
440 * @brief count number of pixels that are outside threshold
441 * @param frame_name the input frame filename
442 * @param frm_mdark the input dark frame
443 * @param saturation for QC parameters computation
444 * @param saturation_negative for QC parameters computation
445 * @param i extension id
446 * @param propertylist where QC params are stored
447 * @note image is expected in extention
448 * @return cpl error code
449 */
450
451cpl_error_code
452eris_get_sat_pix_qc_for_image(const char* frame_name, cpl_frame* frm_mdark,
453 const double saturation, const double saturation_negative,
454 const long long i, cpl_propertylist* applist)
455{
456
457 cpl_ensure_code(frame_name, CPL_ERROR_NULL_INPUT);
458 cpl_ensure_code(frm_mdark, CPL_ERROR_NULL_INPUT);
459 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
460
461 cpl_msg_info(cpl_func,"Image case");
462 int nsat = 0;
463 long int nsat_neg = 0;
464 int plane_nsat_max = 0;
465
466 const char* fname = cpl_frame_get_filename(frm_mdark);
467
468 cpl_image* mdark = cpl_image_load(fname, CPL_TYPE_DOUBLE, 0, 1);
469 cpl_size sx = cpl_image_get_size_x(mdark);
470 cpl_size sy = cpl_image_get_size_y(mdark);
471 cpl_size npix = sx * sy;
472
473 cpl_image* img = cpl_image_load(frame_name, CPL_TYPE_DOUBLE, 0, 0 );
474 cpl_image_subtract(img, mdark);
475
476 nsat = eris_persistence_get_threshpix(img, saturation,
477 CPL_TRUE);
478 cpl_msg_info(cpl_func,"Frame %lld saturation pixels above positive threshold: %ld", i, nsat_neg);
479
480 nsat_neg = eris_persistence_get_threshpix(img, saturation_negative, CPL_FALSE);
481 cpl_msg_info(cpl_func,"Frame %lld Saturation pixels below negative threshold: %ld", i, nsat_neg);
482
483 nsat += nsat_neg; // combine two sources of saturation pixels.
484 cpl_image_delete(img);
485 cpl_image_delete(mdark);
486
487 /* Add a QC parameter */
488 char* keyname;
489 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT",i);
490 cpl_propertylist_append_int(applist, keyname, nsat);
491 cpl_free(keyname);
492
493 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT MAX",i);
494 cpl_propertylist_append_int(applist, keyname, nsat);
495 cpl_free(keyname);
496
497 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT TOT",i);
498 cpl_propertylist_append_int(applist, keyname, nsat);
499 cpl_free(keyname);
500
501 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 BPSAT FRAC",i);
502 cpl_propertylist_append_double(applist, keyname, (double)nsat / npix);
503 cpl_free(keyname);
504
505 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT AVG",i);
506 cpl_propertylist_append_int(applist, keyname, nsat);
507 cpl_free(keyname);
508
509 keyname = cpl_sprintf("ESO QC FRM%lld SLICE NSAT MAX",i);
510 cpl_propertylist_append_int(applist, keyname, plane_nsat_max);
511 cpl_free(keyname);
512
513 return cpl_error_get_code();
514}
515
516
517
518cpl_error_code
519eris_get_sat_qc_for_image(const char* frame_name, cpl_frame* frm_mdark,
520 const double saturation, const double saturation_negative,
521 const double threshold, const long long i, cpl_propertylist* applist)
522{
523
524 cpl_ensure_code(frame_name, CPL_ERROR_NULL_INPUT);
525 cpl_ensure_code(frm_mdark, CPL_ERROR_NULL_INPUT);
526 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
527
528 cpl_msg_info(cpl_func,"Image case");
529 int nsat = 0;
530 long int nsat_neg = 0;
531 long int nthreshold = 0;
532 int plane_nsat_max = 0;
533 int plane_nthresh_max = 0;
534
535 const char* fname = cpl_frame_get_filename(frm_mdark);
536
537 cpl_image* mdark = cpl_image_load(fname, CPL_TYPE_DOUBLE, 0, 1);
538 cpl_size sx = cpl_image_get_size_x(mdark);
539 cpl_size sy = cpl_image_get_size_y(mdark);
540 cpl_size npix = sx * sy;
541
542
543
544 cpl_image* img_persistence = cpl_image_load(frame_name, CPL_TYPE_DOUBLE, 0, 0 );
545
546 cpl_image_subtract(img_persistence, mdark);
547 cpl_image_save(img_persistence,"image_persistence.fits",CPL_TYPE_DOUBLE,NULL,CPL_IO_DEFAULT);
548
549 nsat = eris_persistence_get_threshpix(img_persistence, saturation,
550 CPL_TRUE);
551 cpl_msg_info(cpl_func,"Frame %lld saturation pixels above positive threshold: %ld", i, nsat_neg);
552
553 nthreshold = eris_persistence_get_threshpix(img_persistence, threshold,
554 CPL_TRUE);
555
556 cpl_msg_info(cpl_func,"Frame %lld Threshold pixels: %ld", i, nthreshold);
557 nsat_neg = eris_persistence_get_threshpix(img_persistence,
558 saturation_negative, CPL_FALSE);
559 cpl_msg_info(cpl_func,"Frame %lld Saturation pixels below negative threshold: %ld", i, nsat_neg);
560
561 nsat += nsat_neg; // combine two sources of saturation pixels.
562 cpl_image_delete(img_persistence);
563 cpl_image_delete(mdark);
564
565 /* Add a QC parameter */
566 char* keyname;
567 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT",i);
568 cpl_propertylist_append_int(applist, keyname, nsat);
569 cpl_free(keyname);
570
571 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT MAX",i);
572 cpl_propertylist_append_int(applist, keyname, nsat);
573 cpl_free(keyname);
574
575 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT TOT",i);
576 cpl_propertylist_append_int(applist, keyname, nsat);
577 cpl_free(keyname);
578
579 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 BPSAT FRAC",i);
580 cpl_propertylist_append_double(applist, keyname, (double)nsat / npix);
581 cpl_free(keyname);
582
583 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NSAT AVG",i);
584 cpl_propertylist_append_int(applist, keyname, nsat);
585 cpl_free(keyname);
586
587 keyname = cpl_sprintf("ESO QC FRM%lld SLICE NSAT MAX",i);
588 cpl_propertylist_append_int(applist, keyname, plane_nsat_max);
589 cpl_free(keyname);
590
591
592
593 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH",i);
594 cpl_propertylist_append_int(applist, keyname, nthreshold);
595 cpl_free(keyname);
596
597 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH MAX",i);
598 cpl_propertylist_append_int(applist, keyname, nthreshold);
599 cpl_free(keyname);
600
601 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH TOT",i);
602 cpl_propertylist_append_int(applist, keyname, nthreshold);
603 cpl_free(keyname);
604
605 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 BPTHRESH FRAC",i);
606 cpl_propertylist_append_double(applist, keyname, (double)nthreshold / npix);
607 cpl_free(keyname);
608
609 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 NTHRESH AVG",i);
610 cpl_propertylist_append_int(applist, keyname, nthreshold);
611 cpl_free(keyname);
612
613 keyname = cpl_sprintf("ESO QC FRM%lld SLIC0 SLICE NTHRESH MAX",i);
614 cpl_propertylist_append_int(applist, keyname, plane_nthresh_max);
615 cpl_free(keyname);
616
617
618 return cpl_error_get_code();
619}
620
621cpl_error_code
622eris_get_sat_qc_for_cube(const char* frame_name, cpl_frame* frm_mdark,
623 const double saturation, const double saturation_negative,
624 const double threshold, const long long i, cpl_propertylist* applist)
625{
626
627 cpl_ensure_code(frame_name, CPL_ERROR_NULL_INPUT);
628 cpl_ensure_code(frm_mdark, CPL_ERROR_NULL_INPUT);
629 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
630
631
632 cpl_msg_info(cpl_func,"Cube case");
633 const char* fname = cpl_frame_get_filename(frm_mdark);
634
635 cpl_image* mdark = cpl_image_load(fname, CPL_TYPE_DOUBLE, 0, 1);
636 cpl_size sx = cpl_image_get_size_x(mdark);
637 cpl_size sy = cpl_image_get_size_y(mdark);
638 cpl_size npix = sx * sy;
639
640
641 cpl_imagelist* iml_persistence = cpl_imagelist_load(frame_name, CPL_TYPE_DOUBLE, 0 );
642 cpl_size sz = cpl_imagelist_get_size(iml_persistence);
643 char* keyname;
644 int nsat = 0;
645 int nsat_neg = 0;
646 long nsat_max = 0;
647 long nthreshold_max = 0;
648 int nsat_tot = 0;
649 int nthreshold_tot = 0;
650 double nsat_avg = 0;
651 double nthreshold_avg = 0;
652 int nthreshold = 0;
653 int plane_nsat_max = 0;
654 int plane_nthresh_max = 0;
655 double bpsat_frac_tot = 0;
656 double bpthresh_frac_tot = 0;
657 cpl_image* img;
658 for(cpl_size k = 0; k < sz; k++) {
659 cpl_imagelist_subtract_image(iml_persistence, mdark);
660 img = cpl_imagelist_get(iml_persistence, k);
661 nsat = eris_persistence_get_threshpix(img, saturation, CPL_TRUE);
662 nthreshold = eris_persistence_get_threshpix(img, threshold, CPL_TRUE);
663 nsat_neg = eris_persistence_get_threshpix(img,
664 saturation_negative, CPL_FALSE);
665 nsat += nsat_neg; // combine both sources of saturation pixels
666 /* Add a QC parameter */
667
668 keyname = cpl_sprintf("ESO QC FRM%lld SLIC%lld NSAT", i, k);
669 cpl_propertylist_append_int(applist, keyname, nsat);
670 cpl_free(keyname);
671
672 keyname = cpl_sprintf("ESO QC FRM%lld SLIC%lld BPSAT FRAC",i, k);
673 cpl_propertylist_append_double(applist, keyname, (double)nsat / npix);
674 cpl_free(keyname);
675
676 keyname = cpl_sprintf("ESO QC FRM%lld SLIC%lld NTHRESH",i, k);
677 cpl_propertylist_append_int(applist, keyname, nthreshold);
678 cpl_free(keyname);
679
680 keyname = cpl_sprintf("ESO QC FRM%lld SLIC%lld BPTHRESH FRAC", i, k);
681 cpl_propertylist_append_double(applist, keyname, (double)nthreshold / npix);
682 cpl_free(keyname);
683
684 if(nsat > nsat_max) {
685 nsat_max = nsat;
686 plane_nsat_max = k;
687 }
688
689 if(nthreshold > nthreshold_max) {
690 nthreshold_max = nthreshold;
691 plane_nthresh_max = k;
692 }
693 nsat_tot += nsat;
694 bpsat_frac_tot += (double)nsat / npix;
695
696 nthreshold_tot += nthreshold;
697 bpthresh_frac_tot += (double)nthreshold / npix;
698
699
700 }
701 cpl_imagelist_delete(iml_persistence);
702 cpl_image_delete(mdark);
703
704 nsat_avg = (double) nsat_tot / sz;
705
706 nthreshold_avg = (double) nthreshold_tot / sz;
707
708 keyname = cpl_sprintf("ESO QC FRM%lld NSAT MAX", i);
709 cpl_propertylist_append_int(applist, keyname, nsat_max);
710 cpl_free(keyname);
711
712 keyname = cpl_sprintf("ESO QC FRM%lld NSAT TOT", i);
713 cpl_propertylist_append_int(applist, keyname, nsat_tot);
714 cpl_free(keyname);
715
716 keyname = cpl_sprintf("ESO QC FRM%lld BPSAT FRAC", i);
717 cpl_propertylist_append_double(applist, keyname, bpsat_frac_tot / sz);
718 cpl_free(keyname);
719
720 keyname = cpl_sprintf("ESO QC FRM%lld NSAT AVG", i);
721 cpl_propertylist_append_double(applist, keyname, nsat_avg);
722 cpl_free(keyname);
723
724 keyname = cpl_sprintf("ESO QC FRM%lld SLICE NSAT MAX", i);
725 cpl_propertylist_append_int(applist, keyname, plane_nsat_max);
726 cpl_free(keyname);
727
728 keyname = cpl_sprintf("ESO QC FRM%lld NTHRESH MAX", i);
729 cpl_propertylist_append_int(applist, keyname, nthreshold_max);
730 cpl_free(keyname);
731
732 keyname = cpl_sprintf("ESO QC FRM%lld NTHRESH TOT", i);
733 cpl_propertylist_append_int(applist, keyname, nthreshold_tot);
734 cpl_free(keyname);
735
736 keyname = cpl_sprintf("ESO QC FRM%lld BPTHRESH FRAC", i);
737 cpl_propertylist_append_double(applist, keyname, bpthresh_frac_tot / sz);
738 cpl_free(keyname);
739
740 keyname = cpl_sprintf("ESO QC FRM%lld NTHRESH AVG", i);
741 cpl_propertylist_append_double(applist, keyname, nthreshold_avg);
742 cpl_free(keyname);
743
744 keyname = cpl_sprintf("ESO QC FRM%lld SLICE NTHRESH MAX", i);
745 cpl_propertylist_append_int(applist, keyname, plane_nthresh_max);
746 cpl_free(keyname);
747
748 return cpl_error_get_code();
749}
750
751
752
753/*----------------------------------------------------------------------------*/
761/*----------------------------------------------------------------------------*/
762
763/* set propertylist (double) value */
764cpl_error_code
765eris_parameters_get_double(const cpl_parameterlist* parlist,
766 const char* pname, double *pvalue)
767{
768 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
769 const cpl_parameter* p = cpl_parameterlist_find_const(parlist, pname);
770 cpl_boolean p_has_changed = eris_param_has_changed(p);
771
772 if ( cpl_parameter_get_default_flag(p) && p_has_changed != CPL_FALSE) {
773 *pvalue = cpl_parameter_get_double(p);
774 } else {
775 *pvalue = cpl_parameter_get_default_double(p);
776 }
777
778 return cpl_error_get_code();
779}
780
781/* set propertylist (double) value */
782cpl_error_code
783eris_parameters_get_int(const cpl_parameterlist* parlist,
784 const char* pname, int *pvalue)
785{
786 cpl_ensure_code(parlist != NULL, CPL_ERROR_NULL_INPUT);
787 const cpl_parameter* p = cpl_parameterlist_find_const(parlist, pname);
788 cpl_boolean p_has_changed = eris_param_has_changed(p);
789
790 if ( cpl_parameter_get_default_flag(p) && p_has_changed != CPL_FALSE) {
791 *pvalue = cpl_parameter_get_int(p);
792 } else {
793 *pvalue = cpl_parameter_get_default_int(p);
794 }
795
796 return cpl_error_get_code();
797}
798
799/*----------------------------------------------------------------------------*/
806/*----------------------------------------------------------------------------*/
807
808cpl_boolean
809eris_param_has_changed(const cpl_parameter* p)
810{
811 cpl_ensure_code(p != NULL,CPL_FALSE);
812 cpl_type type = cpl_parameter_get_type(p);
813 cpl_boolean has_changed = CPL_FALSE;
814 /* handle different data types */
815 switch (type) {
816
817 case CPL_TYPE_INT: {
818 int val = cpl_parameter_get_int(p);
819 int def = cpl_parameter_get_default_int(p);
820 has_changed = (val != def) ? CPL_TRUE : CPL_FALSE;
821 }
822 break;
823
824 case CPL_TYPE_FLOAT: {
825 float val = cpl_parameter_get_double(p);
826 float def = cpl_parameter_get_default_double(p);
827 has_changed = (val != def) ? CPL_TRUE : CPL_FALSE;
828 }
829 break;
830
831 case CPL_TYPE_DOUBLE: {
832 double val = cpl_parameter_get_double(p);
833 double def = cpl_parameter_get_default_double(p);
834 has_changed = (val != def) ? CPL_TRUE : CPL_FALSE;
835 }
836 break;
837
838 case CPL_TYPE_STRING:{
839 const char* val = cpl_parameter_get_string(p);
840 const char* def = cpl_parameter_get_default_string(p);
841 if ( strcmp(val,def) != 0 ) {
842 has_changed = CPL_TRUE;
843 }
844 }
845 break;
846
847 default:
848 cpl_msg_error(cpl_func,"case not found! %d string type: %d",type,
849 CPL_TYPE_STRING);
850 break;
851
852 }
853 return has_changed;
854}
855/*----------------------------------------------------------------------------*/
865/*----------------------------------------------------------------------------*/
866cpl_error_code
867eris_files_dont_exist(cpl_frameset *frameset)
868{
869 const char *func = "dfs_files_dont_exist";
870 cpl_ensure_code(frameset != NULL, CPL_ERROR_NULL_INPUT);
871
872 if (frameset == NULL ) {
873 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
874 return CPL_ERROR_NULL_INPUT;
875 }
876
877 if (cpl_frameset_is_empty(frameset)) {
878 cpl_error_set(func, CPL_ERROR_FILE_NOT_FOUND);
879 return CPL_ERROR_FILE_NOT_FOUND;
880 }
881
882 cpl_frameset_iterator* it = cpl_frameset_iterator_new(frameset);
883 cpl_frame *frame = cpl_frameset_iterator_get(it);
884 const char* fname = NULL;
885 cpl_propertylist* plist = NULL;
886 while (frame) {
887 fname = cpl_frame_get_filename(frame);
888 cpl_ensure_code(strcmp(fname, "/dev/null"), CPL_ERROR_NULL_INPUT);
889
890 /* test if file exist */
891 if (access(cpl_frame_get_filename(frame), F_OK)) {
892 cpl_msg_error(func, "File %s (%s) was not found",
893 cpl_frame_get_filename(frame),
894 cpl_frame_get_tag(frame));
895 cpl_error_set(func, CPL_ERROR_FILE_NOT_FOUND);
896 cpl_frameset_iterator_delete(it);
897 cpl_ensure_code(CPL_FALSE, CPL_ERROR_FILE_NOT_FOUND);
898 }
899 /* if file exist test that at least INSTRUME is defined */
900 plist = cpl_propertylist_load(fname, 0);
901 if(!cpl_propertylist_has(plist, "INSTRUME")) {
902 cpl_propertylist_delete(plist);
903 cpl_frameset_iterator_delete(it);
904 cpl_ensure_code(CPL_FALSE, CPL_ERROR_DATA_NOT_FOUND);
905 }
906 cpl_propertylist_delete(plist);
907
908 cpl_frameset_iterator_advance(it, 1);
909 frame = cpl_frameset_iterator_get(it);
910
911 }
912
913 cpl_frameset_iterator_delete(it);
914
915 if (cpl_error_get_code())
916 return cpl_error_get_code();
917
918 return CPL_ERROR_NONE;
919}
920
921cpl_error_code
922eris_image_flag_nan(cpl_image** ima)
923{
924 cpl_ensure_code(ima != NULL, CPL_ERROR_NULL_INPUT);
925
926 cpl_mask* bpm = cpl_image_get_bpm(*ima);
927 cpl_binary* pbpm = cpl_mask_get_data(bpm);
928 cpl_size sx = cpl_image_get_size_x(*ima);
929 cpl_size sy = cpl_image_get_size_y(*ima);
930 double* pdata = cpl_image_get_data(*ima);
931 for(cpl_size j = 0; j < sy; j++ ) {
932 for(cpl_size i = 0; i < sx; i++ ) {
933 if(isnan(pdata[i+j*sx])){
934
935 pbpm[i+j*sx] = CPL_BINARY_1;
936
937 }
938 }
939 }
940
941
942 return cpl_error_get_code();
943}
944
cpl_error_code eris_files_dont_exist(cpl_frameset *frameset)
Check if all SOF files exist.
Definition: eris_utils.c:867
cpl_error_code eris_parameters_get_double(const cpl_parameterlist *parlist, const char *pname, double *pvalue)
get double parameter value if changed by the user
Definition: eris_utils.c:765
cpl_error_code eris_check_error_code(const char *func_id)
handle CPL errors
Definition: eris_utils.c:56
cpl_frameset * eris_dfs_extract_frames_with_tag(cpl_frameset *input, const char *rtag)
Extract frames of user given tag.
Definition: eris_utils.c:227
cpl_error_code eris_dfs_extract_raw_frames(cpl_frameset *input, cpl_frameset *raws)
split input sof in groups: raw and calib
Definition: eris_utils.c:260
const char * eris_get_license(void)
Get the pipeline copyright and license.
Definition: eris_utils.c:138
cpl_boolean eris_param_has_changed(const cpl_parameter *p)
verify if a parameter value has been changed (from command line or or rc file by a user)
Definition: eris_utils.c:809
cpl_error_code eris_dfs_extract_cal_frames(cpl_frameset *input, cpl_frameset *calibs)
split input sof in groups: raw and calib
Definition: eris_utils.c:287