ERIS Pipeline Reference Manual 1.9.2
eris_ifu_utils.c
1/* $Id: eris_ifu_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
36#include <string.h>
37
38#include "eris_ifu_utils.h"
39#include "eris_ifu_error.h"
40#include "eris_ifu_functions.h"
41#include "eris_utils.h"
42#include "eris_pfits.h"
43/*----------------------------------------------------------------------------*/
112/*----------------------------------------------------------------------------*/
113
142cpl_vector* eris_ifu_idl_where(const cpl_vector *data, double val, int op)
143{
144 cpl_vector *ret_vec = NULL;
145 double *pret_vec = NULL;
146 const double *pdata = NULL;
147 int j = 0,
148 size = 0;
149
150 cpl_ensure(data, CPL_ERROR_NULL_INPUT, NULL);
151
152 TRY
153 {
154 size = cpl_vector_get_size(data);
155
156 // create vector of same size as input data and set default values
157 // to -1.0 (will be shortened at the end)
158 ret_vec = cpl_vector_new(size);
159 pret_vec = cpl_vector_get_data(ret_vec);
160
161 cpl_vector_fill(ret_vec, -1.0);
162
163 // identify valid values in data and copy inidices to temp vector
164 pdata = cpl_vector_get_data_const(data);
165
166 j = 0;
167 for (int i = 0; i < size; i++) {
168 switch (op) {
169 case eq:
170 if (pdata[i] == val)
171 pret_vec[j++] = i;
172 break;
173 case ne:
174 if (fabs(pdata[i]-val) > 0.0001)
175 pret_vec[j++] = i;
176 break;
177 case ge:
178 if (pdata[i] >= val)
179 pret_vec[j++] = i;
180 break;
181 case gt:
182 if (pdata[i] > val)
183 pret_vec[j++] = i;
184 break;
185 case le:
186 if (pdata[i] <= val)
187 pret_vec[j++] = i;
188 break;
189 case lt:
190 if (pdata[i] < val)
191 pret_vec[j++] = i;
192 break;
193 default:
194 SET_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
195 "illegal operator");
196 break;
197 }
198 }
199
200 //cut trailing -1
201 eris_ifu_cut_endings(&ret_vec, NULL, NULL, TRUE);
203 }
204 CATCH
205 {
206 CATCH_MSGS();
207 eris_ifu_free_vector(&ret_vec);
208 }
209 eris_check_error_code("eris_ifu_idl_where");
210 return ret_vec;
211}
212
232cpl_vector* eris_ifu_idl_values_at_indices(const cpl_vector *data,
233 const cpl_vector *indices)
234{
235 cpl_vector* ret_vec = NULL;
236 double *pret_vec = NULL;
237 const double *pdata = NULL,
238 *pindices = NULL;
239 int size = 0;
240
241 cpl_ensure(data, CPL_ERROR_NULL_INPUT, NULL);
242 cpl_ensure(indices, CPL_ERROR_NULL_INPUT, NULL);
243
244 TRY
245 {
246 pdata = cpl_vector_get_data_const(data);
247 pindices = cpl_vector_get_data_const(indices);
248
249 size = cpl_vector_get_size(indices);
250
251 ret_vec = cpl_vector_new(size);
252 pret_vec = cpl_vector_get_data(ret_vec);
253
254 for (int i = 0; i < size; i++) {
255 if ((int)pindices[i] < 0) {
256 SET_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
257 "One of the indices is < 0!");
258 } else {
259 pret_vec[i] = pdata[(int)pindices[i]];
260 }
261 }
262 }
263 CATCH
264 {
265 CATCH_MSGS();
266 eris_ifu_free_vector(&ret_vec);
267 }
268 eris_check_error_code("eris_ifu_idl_values_at_indices");
269 return ret_vec;
270}
271
297cpl_error_code eris_ifu_cut_endings(cpl_vector** vec,
298 int *begin, int *end, int cut)
299{
300 cpl_error_code err = CPL_ERROR_NONE;
301 int min = 0,
302 max = 0;
303 double *pvec = NULL;
304 cpl_vector *tmp_vec = NULL;
305
306 cpl_ensure_code(vec, CPL_ERROR_NULL_INPUT);
307 cpl_ensure_code(*vec, CPL_ERROR_NULL_INPUT);
308
309 TRY
310 {
311 pvec = cpl_vector_get_data(*vec);
312 min = 0; // min_pos
313 max = cpl_vector_get_size(*vec)-1; // max_pos
314
315 // find beginning
316 for (int i = 0; i < cpl_vector_get_size(*vec); i++) {
317 if (pvec[i] == -1) {
318 min = i+1;
319 } else {
320 break;
321 }
322 }
323
324 if (min != cpl_vector_get_size(*vec)) {
325 // find ending
326 for (int i = cpl_vector_get_size(*vec)-1; i >= 0; i--) {
327 if (pvec[i] == -1) {
328 max = i-1;
329 } else {
330 break;
331 }
332 }
333
334 if (cut == TRUE) {
335 // extract appropriate part of vector
336 tmp_vec = cpl_vector_extract(*vec, min, max, 1);
337
338 cpl_vector_delete(*vec); *vec= NULL;
339
340 *vec = tmp_vec;
341 }
342 } else {
343 if (cut == TRUE) {
344 // all values are -1 return NULL
345 cpl_vector_delete(*vec); *vec = NULL;
346 }
347
348 min = 0;
349 max = 0;
350 }
351
352 // set return values if wanted
353 if (begin != NULL) {
354 *begin = min;
355 }
356
357 if (end != NULL) {
358 *end = max;
359 }
360 }
361 CATCH
362 {
363
364 err = cpl_error_get_code();
365// eris_print_rec_status(9);
366// cpl_msg_info(cpl_func,"ok9");
367 if (begin != NULL) {
368 *begin = 0;
369 }
370
371 if (end != NULL) {
372 *end = 0;
373 }
374
376 }
377 eris_check_error_code("eris_ifu_cut_endings");
378 return err;
379}
380
394hdrl_imagelist * eris_ifu_get_hdrlimagelist_by_tag(cpl_frameset *frameset,
395 const char *tag,
396 int exposureCorrectionMode)
397{
398 hdrl_imagelist *hdrl_list = NULL;
399 cpl_frameset *frames = NULL;
400 hdrl_image *tmp_img = NULL;
401 const cpl_frame *fr = NULL;
402 const char *fn = NULL;
403
404 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
405 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
406
407 TRY
408 {
409 frames = eris_ifu_get_frameset_by_tag(frameset, tag);
410 hdrl_list = hdrl_imagelist_new();
411
412 /*
413 * Loop through all FLAT_LAMP frames and save on- and off-frames
414 * accordingly in their cpl_imagelists
415 */
416 fr = cpl_frameset_find_const(frames, tag);
417
418 while (fr != NULL) {
419 fn = cpl_frame_get_filename(fr);
421
422 /* If the frame has a tag we process it. Else it is an object and
423 * is ignored */
424 if (cpl_frame_get_tag(fr) != NULL) {
425 tmp_img = eris_ifu_load_exposure_file(fn,
426 exposureCorrectionMode, NULL);
427
428 hdrl_imagelist_set(hdrl_list, tmp_img,
429 hdrl_imagelist_get_size(hdrl_list));
430 } else {
431 /* if (tag == NULL) do nothing */
432 }
433
434 /* next frame */
435 fr = cpl_frameset_find_const(frames, NULL);
436 }
437 }
438 CATCH
439 {
440 CATCH_MSGS();
442 }
443
444 eris_ifu_free_frameset(&frames);
445 eris_check_error_code("eris_ifu_get_hdrlimagelist_by_tag");
446 return hdrl_list;
447}
448
456 const cpl_frameset *frameset,
457 const char *tag)
458{
459 cpl_frameset *frames = NULL;
460 const cpl_frame *curFrame ;
461 cpl_frame *locFrame ;
462 cpl_size frameCnt;
463 int i;
464
465 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
466 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
467
468 TRY
469 {
470 /* Check whether there are frames with the requested tag */
471 frameCnt = cpl_frameset_count_tags(frameset, tag);
473 if (frameCnt == 0)
474 {
475 BRK_WITH_ERROR_MSG(CPL_ERROR_DATA_NOT_FOUND,
476 "frameset contains no frames with tag %s", tag);
477 }
478
479 /* Create the output frameset */
480 frames = cpl_frameset_new() ;
481
482 /* Extract frames with proper tag into the output frameset */
483 frameCnt = cpl_frameset_get_size(frameset) ;
484 for (i=0 ; i<frameCnt ; i++) {
485 curFrame = cpl_frameset_get_position_const(frameset, i) ;
486 if (!strcmp(cpl_frame_get_tag(curFrame), tag)) {
487 locFrame = cpl_frame_duplicate(curFrame) ;
488 cpl_frameset_insert(frames, locFrame) ;
489 }
490 }
491 }
492 CATCH
493 {
494 frames = NULL;
495 }
496 eris_check_error_code("eris_ifu_get_frameset_by_tag");
497 return frames;
498}
499
509 cpl_parameterlist *p1,
510 const cpl_parameterlist *p2)
511{
512 cpl_error_code err = CPL_ERROR_NONE;
513 const cpl_parameter *p = NULL;
514
515 cpl_ensure_code(p1, CPL_ERROR_NULL_INPUT);
516 cpl_ensure_code(p2, CPL_ERROR_NULL_INPUT);
517
518 TRY
519 {
520 p = cpl_parameterlist_get_first_const(p2);
521 cpl_parameterlist_append(p1, cpl_parameter_duplicate(p));
522 while ((p = cpl_parameterlist_get_next_const(p2)) != NULL) {
523 cpl_parameterlist_append(p1, cpl_parameter_duplicate(p));
524 }
526 }
527 CATCH
528 {
529 err = cpl_error_get_code();
530 }
531 eris_check_error_code("eris_ifu_parameterlist_append_list");
532 return err;
533}
534
542void eris_ifu_free_hdrl_image(hdrl_image **item) {
543 if ((item != NULL) && (*item != NULL)) {
544 hdrl_image_delete(*item);
545 *item = NULL;
546 }
547}
548
556void eris_ifu_free_hdrl_imagelist(hdrl_imagelist **item) {
557 if ((item != NULL) && (*item != NULL)) {
559 *item = NULL;
560 }
561}
562
570void eris_ifu_free_image(cpl_image **item) {
571 if ((item != NULL) && (*item != NULL)) {
572 cpl_image_delete(*item);
573 *item = NULL;
574 }
575}
576
584void eris_ifu_free_imagelist(cpl_imagelist **item) {
585 if ((item != NULL) && (*item != NULL)) {
586 cpl_imagelist_delete(*item);
587 *item = NULL;
588 }
589}
590
598void eris_ifu_free_mask(cpl_mask **item) {
599 if ((item != NULL) && (*item != NULL)) {
600 cpl_mask_delete(*item);
601 *item = NULL;
602 }
603}
604
612void eris_ifu_free_matrix(cpl_matrix **item) {
613 if ((item != NULL) && (*item != NULL)) {
614 cpl_matrix_delete(*item);
615 *item = NULL;
616 }
617}
618
626void eris_ifu_free_propertylist(cpl_propertylist **item) {
627 if ((item != NULL) && (*item != NULL)) {
628 cpl_propertylist_delete(*item);
629 *item = NULL;
630 }
631}
632
640void eris_ifu_free_vector(cpl_vector **item) {
641 if ((item != NULL) && (*item != NULL)) {
642 cpl_vector_delete(*item);
643 *item = NULL;
644 }
645}
646
654void eris_ifu_free_ifu_vector(eris_ifu_vector **item) {
655 if ((item != NULL) && (*item != NULL)) {
657 *item = NULL;
658 }
659}
660
669void eris_ifu_free_string(char **item) {
670 if ((item != NULL) && (*item != NULL)) {
671 cpl_free(*item);
672 *item = NULL;
673 }
674}
675
683void eris_ifu_free_frameset(cpl_frameset **item) {
684 if ((item != NULL) && (*item != NULL)) {
685 cpl_frameset_delete(*item);
686 *item = NULL;
687 }
688}
689
697void eris_ifu_free_frame(cpl_frame **item) {
698 if ((item != NULL) && (*item != NULL)) {
699 cpl_frame_delete(*item);
700 *item = NULL;
701 }
702}
703
711void eris_ifu_free_hdrl_parameter(hdrl_parameter **item) {
712 if ((item != NULL) && (*item != NULL)) {
714 *item = NULL;
715 }
716}
717
725void eris_ifu_free_parameter(cpl_parameter **item) {
726 if ((item != NULL) && (*item != NULL)) {
727 cpl_parameter_delete(*item);
728 *item = NULL;
729 }
730}
731
739void eris_ifu_free_parameterlist(cpl_parameterlist **item) {
740 if ((item != NULL) && (*item != NULL)) {
741 cpl_parameterlist_delete(*item);
742 *item = NULL;
743 }
744}
745
753void eris_ifu_free_table(cpl_table **item) {
754 if ((item != NULL) && (*item != NULL)) {
755 cpl_table_delete(*item);
756 *item = NULL;
757 }
758}
759
768void eris_ifu_free_double_array(double **item) {
769 if ((item != NULL) && (*item != NULL)) {
770 cpl_free(*item);
771 *item = NULL;
772 }
773}
774
783void eris_ifu_free_float_array(float **item) {
784 if ((item != NULL) && (*item != NULL)) {
785 cpl_free(*item);
786 *item = NULL;
787 }
788}
789
798void eris_ifu_free_int_array(int**item) {
799 if ((item != NULL) && (*item != NULL)) {
800 cpl_free(*item);
801 *item = NULL;
802 }
803}
804
812void eris_ifu_free_polynomial(cpl_polynomial **item) {
813 if ((item != NULL) && (*item != NULL)) {
814 cpl_polynomial_delete(*item);
815 *item = NULL;
816 }
817}
818
826void eris_ifu_free_bivector(cpl_bivector **item) {
827 if ((item != NULL) && (*item != NULL)) {
828 cpl_bivector_delete(*item);
829 *item = NULL;
830 }
831}
832
840void eris_ifu_free_apertures(cpl_apertures **item) {
841 if ((item != NULL) && (*item != NULL)) {
842 cpl_apertures_delete(*item);
843 *item = NULL;
844 }
845}
846
865cpl_error_code eris_ifu_save_image(cpl_frameset *fs,
866 const cpl_propertylist *plist,
867 const cpl_parameterlist *parlist,
868 const char *recipe,
869 const char *procatg,
870 const char *filename,
871 cpl_type type,
872 const cpl_image *image)
873{
874 cpl_error_code err = CPL_ERROR_NONE;
875 char *pckgName = NULL;
876 cpl_propertylist *plistdup = NULL;
877
878 cpl_ensure_code(fs, CPL_ERROR_NULL_INPUT);
879 cpl_ensure_code(plist, CPL_ERROR_NULL_INPUT);
880 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
881 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
882 cpl_ensure_code(procatg, CPL_ERROR_NULL_INPUT);
883 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
884 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
885
886 TRY
887 {
888 plistdup = cpl_propertylist_duplicate(plist);
889
890 pckgName = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
891
892 cpl_propertylist_update_string(plistdup, CPL_DFS_PRO_CATG, procatg);
893
894 cpl_dfs_save_image(fs, NULL, parlist, fs, NULL, image, type,
895 recipe, plistdup, "RADECSYS", pckgName, filename);
896
897 cpl_propertylist_delete(plistdup);
898 }
899 CATCH
900 {
901 CATCH_MSGS();
902 err = cpl_error_get_code();
903 }
904
905 eris_ifu_free_string(&pckgName);
906 eris_check_error_code("eris_ifu_save_image");
907 return err;
908}
909
930cpl_error_code eris_ifu_save_imagelist(cpl_frameset *fs,
931 cpl_frame *inherit,
932 const cpl_propertylist *plist,
933 const cpl_parameterlist *parlist,
934 const char *recipe,
935 const char *procatg,
936 const char *filename,
937 cpl_type type,
938 const cpl_imagelist *imglist)
939{
940 cpl_error_code err = CPL_ERROR_NONE;
941 char *pckgName = NULL;
942 cpl_propertylist *plistdup = NULL;
943
944 cpl_ensure_code(fs, CPL_ERROR_NULL_INPUT);
945 cpl_ensure_code(plist, CPL_ERROR_NULL_INPUT);
946 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
947 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
948 cpl_ensure_code(procatg, CPL_ERROR_NULL_INPUT);
949 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
950 cpl_ensure_code(imglist, CPL_ERROR_NULL_INPUT);
951
952 TRY
953 {
954 plistdup = cpl_propertylist_duplicate(plist);
955 pckgName = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
956 cpl_propertylist_update_string(plistdup, CPL_DFS_PRO_CATG, procatg);
957 // TODO: why adding CPL_DFS_PRO_TYPE, CPL_DFS_PRO_TECH ???
958 cpl_propertylist_update_string(plistdup, CPL_DFS_PRO_TYPE, "CUBE");
959 cpl_propertylist_update_string(plistdup, CPL_DFS_PRO_TECH, "CUBE");
960 //cpl_propertylist_append_string(plist, "PRODCATG", "SCIENCE.CUBE.IFS");
961 cpl_propertylist_erase(plistdup, "RADECSYS");
962 //eris_ifu_plist_erase_wcs(plistdup);
964
965 cpl_dfs_save_imagelist(fs, NULL, parlist, fs, inherit, imglist, type,
966 recipe, plistdup, "RADECSYS", pckgName, filename);
967
968 }
969 CATCH
970 {
971 CATCH_MSGS();
972 err = cpl_error_get_code();
973 }
974
975 eris_ifu_free_string(&pckgName);
976 cpl_propertylist_delete(plistdup);
977 eris_check_error_code("eris_ifu_save_imagelist");
978 return err;
979}
980
999cpl_error_code eris_ifu_hdrl_image_reject_mask(hdrl_image *img,
1000 const cpl_mask *mask)
1001{
1002 cpl_error_code err = CPL_ERROR_NONE;
1003 const cpl_binary *pmask = NULL;
1004 cpl_mask *img_mask = NULL;
1005 cpl_binary *pimg_mask = NULL;
1006 cpl_size nRows = 0;
1007 cpl_size nCols = 0;
1008
1009 cpl_ensure_code(img, CPL_ERROR_NULL_INPUT);
1010 cpl_ensure_code(mask, CPL_ERROR_NULL_INPUT);
1011 cpl_ensure_code(hdrl_image_get_size_x(img)==cpl_mask_get_size_x(mask),
1012 CPL_ERROR_ILLEGAL_INPUT);
1013 cpl_ensure_code(hdrl_image_get_size_y(img)==cpl_mask_get_size_y(mask),
1014 CPL_ERROR_ILLEGAL_INPUT);
1015
1016 TRY
1017 {
1018 // reject from image
1019 img_mask = hdrl_image_get_mask(img);
1020 pimg_mask = cpl_mask_get_data(img_mask);
1021 pmask = cpl_mask_get_data_const(mask);
1022
1023 nCols = hdrl_image_get_size_x(img);
1024 nRows = hdrl_image_get_size_y(img);
1025 for (cpl_size y = 0; y < nRows; y++) {
1026 for (cpl_size x = 0; x < nCols; x++) {
1027 if ((pmask[x+y*nCols] == BAD_PIX) &&
1028 (pimg_mask[x+y*nCols] == GOOD_PIX))
1029 {
1030 pimg_mask[x+y*nCols] = BAD_PIX;
1031 }
1032 }
1033 }
1034 }
1035 CATCH
1036 {
1037 CATCH_MSGS();
1038 err = cpl_error_get_code();
1039 }
1040 eris_check_error_code("eris_ifu_hdrl_image_reject_mask");
1041 return err;
1042}
1043
1057cpl_error_code eris_ifu_save_vector_dbg(const cpl_vector *vec,
1058 const char* filename, int create,
1059 const cpl_propertylist *pl)
1060{
1061 cpl_ensure_code(vec,CPL_ERROR_NULL_INPUT);
1062
1063 return cpl_vector_save(vec, filename, CPL_TYPE_DOUBLE, pl, create);
1064}
1065
1078cpl_error_code eris_ifu_save_ifu_vector_dbg(const eris_ifu_vector *vec,
1079 const char* filename, int create)
1080{
1081 return eris_ifu_vector_save(vec, filename, CPL_TYPE_DOUBLE, NULL, create, -10.0);
1082}
1083
1101cpl_error_code eris_ifu_save_bivector_dbg(const cpl_bivector *bivec,
1102 const char* filename, int col, int create)
1103{
1104// int size = cpl_bivector_get_size(bivec);
1105// const double *px = cpl_bivector_get_x_data_const(bivec),
1106// *py = cpl_bivector_get_y_data_const(bivec);
1107 const cpl_vector *x = NULL,
1108 *y = NULL;
1109
1110 if (col == 1) {
1111 x = cpl_bivector_get_x_const(bivec);
1112 return eris_ifu_save_vector_dbg(x, filename, create, NULL);
1113 } else if (col == 2) {
1114 y = cpl_bivector_get_x_const(bivec);
1115 return eris_ifu_save_vector_dbg(y, filename, create, NULL);
1116 } else if (col == 0) {
1117 x = cpl_bivector_get_x_const(bivec);
1118 y = cpl_bivector_get_y_const(bivec);
1119 eris_ifu_save_vector_dbg(x, filename, create, NULL);
1120 return eris_ifu_save_vector_dbg(y, filename, CPL_IO_EXTEND, NULL);
1121 } else {
1122 return CPL_ERROR_ILLEGAL_INPUT;
1123 }
1124}
1125
1139cpl_error_code eris_ifu_save_image_dbg(const cpl_image *img,
1140 const char* filename, int create,
1141 const cpl_propertylist *pl)
1142{
1143 cpl_ensure_code(img,CPL_ERROR_NULL_INPUT);
1144
1145 if (img == NULL) {
1146 return cpl_image_save(img, filename,
1147 CPL_TYPE_INT, pl, create);
1148 } else if (cpl_image_get_type(img) == CPL_TYPE_FLOAT) {
1149 return cpl_image_save(img, filename,
1150 CPL_TYPE_FLOAT, pl, create);
1151 } else if (cpl_image_get_type(img) == CPL_TYPE_DOUBLE) {
1152 return cpl_image_save(img, filename,
1153 CPL_TYPE_DOUBLE, pl, create);
1154 } else if (cpl_image_get_type(img) == CPL_TYPE_INT) {
1155 return cpl_image_save(img, filename,
1156 CPL_TYPE_INT, pl, create);
1157 } else {
1158 return CPL_ERROR_ILLEGAL_INPUT;
1159 }
1160}
1161
1173cpl_error_code eris_ifu_save_table_dbg(const cpl_table *tbl,
1174 const char* filename, int create)
1175{
1176 return cpl_table_save(tbl, NULL, NULL, filename, create);
1177}
1178
1191cpl_error_code eris_ifu_save_mask_dbg(const cpl_mask *mask,
1192 const char* filename, int create,
1193 const cpl_propertylist *pl)
1194{
1195 return cpl_mask_save(mask, filename, pl, create);
1196}
1197
1210cpl_error_code eris_ifu_save_imagelist_dbg(const cpl_imagelist *imglist,
1211 const char* filename, int create)
1212{
1213 cpl_ensure_code(imglist, CPL_ERROR_NULL_INPUT);
1214 cpl_ensure_code(cpl_imagelist_get_size(imglist) >= 0,
1215 CPL_ERROR_ILLEGAL_INPUT);
1216
1217 const cpl_image *img = cpl_imagelist_get_const(imglist, 0);
1218 eris_check_error_code("eris_ifu_save_imagelist_dbg");
1219 if (cpl_image_get_type(img) == CPL_TYPE_FLOAT) {
1220 return cpl_imagelist_save(imglist, filename,
1221 CPL_TYPE_FLOAT, NULL, create);
1222 } else if (cpl_image_get_type(img) == CPL_TYPE_DOUBLE) {
1223 return cpl_imagelist_save(imglist, filename,
1224 CPL_TYPE_DOUBLE, NULL, create);
1225 } else {
1226 return CPL_ERROR_ILLEGAL_INPUT;
1227 }
1228}
1229
1244cpl_error_code eris_ifu_save_cpl_image_dbg(const cpl_image *img,
1245 const char* name, int singlefile,
1246 const cpl_propertylist *pl)
1247{
1248 char *fn = NULL;
1249 const cpl_mask *mask = NULL;
1250 cpl_error_code err = CPL_ERROR_NONE;
1251 int create = CPL_IO_CREATE;
1252
1253 TRY
1254 {
1255 if (singlefile) {
1256 fn = cpl_sprintf("%s.fits", name);
1257 } else {
1258 fn = cpl_sprintf("%s_data.fits", name);
1259 }
1260 eris_ifu_save_image_dbg(img, fn, create, pl);
1261 cpl_free(fn); fn = NULL;
1262
1263 if (singlefile) {
1264 fn = cpl_sprintf("%s.fits", name);
1265 create = CPL_IO_EXTEND;
1266 } else {
1267 fn = cpl_sprintf("%s_mask.fits", name);
1268 create = CPL_IO_CREATE;
1269 }
1270 mask = cpl_image_get_bpm_const(img);
1271 eris_ifu_save_mask_dbg(mask, fn, create, pl);
1272 cpl_free(fn); fn = NULL;
1273 }
1274 CATCH
1275 {
1276 CATCH_MSGS();
1277 err = cpl_error_get_code();
1278 }
1279 cpl_free(fn); fn = NULL;
1280 eris_check_error_code("eris_ifu_save_cpl_image_dbg");
1281 return err;
1282}
1283
1295cpl_error_code eris_ifu_save_cpl_imagelist_dbg(const cpl_imagelist *imglist,
1296 const char* name)
1297{
1298 char *fn = NULL;
1299 cpl_error_code err = CPL_ERROR_NONE;
1300
1301 TRY
1302 {
1303 fn = cpl_sprintf("%s.fits", name);
1304 eris_ifu_save_imagelist_dbg(imglist, fn, CPL_IO_CREATE);
1306 }
1307 CATCH
1308 {
1309 CATCH_MSGS();
1310 err = cpl_error_get_code();
1311 }
1312 eris_check_error_code("eris_ifu_save_cpl_imagelist_dbg");
1313 return err;
1314}
1315
1333cpl_error_code eris_ifu_save_hdrl_image_dbg(const hdrl_image *hdrl_img,
1334 const char* filename, int singlefile,
1335 const cpl_propertylist *pl)
1336{
1337 char *fn = NULL;
1338 const cpl_image *img = NULL;
1339 const cpl_mask *mask = NULL;
1340 cpl_error_code err = CPL_ERROR_NONE;
1341 int create = CPL_IO_CREATE;
1342
1343 TRY
1344 {
1345 if (singlefile) {
1346 fn = cpl_sprintf("%s.fits", filename);
1347 } else {
1348 fn = cpl_sprintf("%s_data.fits", filename);
1349 }
1350 img = hdrl_image_get_image_const(hdrl_img);
1351 eris_ifu_save_image_dbg(img, fn, create, pl);
1352 cpl_free(fn); fn = NULL;
1353
1354 if (singlefile) {
1355 fn = cpl_sprintf("%s.fits", filename);
1356 create = CPL_IO_EXTEND;
1357 } else {
1358 fn = cpl_sprintf("%s_err.fits", filename);
1359 create = CPL_IO_CREATE;
1360 }
1361 img = hdrl_image_get_error_const(hdrl_img);
1362 eris_ifu_save_image_dbg(img, fn, create, pl);
1363 cpl_free(fn); fn = NULL;
1364
1365 if (singlefile) {
1366 fn = cpl_sprintf("%s.fits", filename);
1367 create = CPL_IO_EXTEND;
1368 } else {
1369 fn = cpl_sprintf("%s_mask.fits", filename);
1370 create = CPL_IO_CREATE;
1371 }
1372 mask = hdrl_image_get_mask_const(hdrl_img);
1373 if(mask != NULL) {
1374 eris_ifu_save_mask_dbg(mask, fn, create, pl);
1375 }
1376 cpl_free(fn); fn = NULL;
1377 }
1378 CATCH
1379 {
1380 CATCH_MSGS();
1381 err = cpl_error_get_code();
1382 }
1383 eris_check_error_code("eris_ifu_save_hdrl_image_dbg");
1384 return err;
1385}
1386
1404 const hdrl_imagelist *hdrl_img_list,
1405 const char* filename, int singlefile)
1406{
1407 char *fn = NULL;
1408 cpl_imagelist *img_list = NULL;
1409 const hdrl_image *tmp_hdrl_img = NULL;
1410 const cpl_mask *mask = NULL;
1411 const cpl_binary *pmask = NULL;
1412 cpl_image *img = NULL;
1413 cpl_error_code err = CPL_ERROR_NONE;
1414 float *pimg = NULL;
1415 int nx = 0,
1416 ny = 0,
1417 create = CPL_IO_CREATE;
1418
1419 TRY
1420 {
1421 /* save data */
1422 if (singlefile) {
1423 fn = cpl_sprintf("%s.fits", filename);
1424 } else {
1425 fn = cpl_sprintf("%s_data.fits", filename);
1426 }
1427 img_list = eris_ifu_hdrl_get_imagelist(hdrl_img_list,
1428 eris_hdrl_data);
1429 eris_ifu_save_imagelist_dbg(img_list, fn, create);
1430 eris_ifu_free_imagelist(&img_list);
1431 cpl_free(fn); fn = NULL;
1432
1433 /* save error */
1434 if (singlefile) {
1435 fn = cpl_sprintf("%s.fits", filename);
1436 create = CPL_IO_EXTEND;
1437 } else {
1438 fn = cpl_sprintf("%s_err.fits", filename);
1439 create = CPL_IO_CREATE;
1440 }
1441 img_list = eris_ifu_hdrl_get_imagelist(hdrl_img_list,
1442 eris_hdrl_error);
1443 eris_ifu_save_imagelist_dbg(img_list, fn, create);
1444 eris_ifu_free_imagelist(&img_list);
1445 cpl_free(fn); fn = NULL;
1446
1447 /* save bp-mask */
1448 if (singlefile) {
1449 fn = cpl_sprintf("%s.fits", filename);
1450 create = CPL_IO_EXTEND;
1451 } else {
1452 fn = cpl_sprintf("%s_mask.fits", filename);
1453 create = CPL_IO_CREATE;
1454 }
1455 img_list = cpl_imagelist_new();
1456
1457 for (int i = 0; i < hdrl_imagelist_get_size(hdrl_img_list); i++) {
1458 tmp_hdrl_img = hdrl_imagelist_get_const(hdrl_img_list, i);
1459 mask = hdrl_image_get_mask_const(tmp_hdrl_img);
1460
1461 nx = cpl_mask_get_size_x(mask);
1462 ny = cpl_mask_get_size_y(mask);
1463
1464 img = cpl_image_new(nx, ny, CPL_TYPE_FLOAT);
1465 pimg = cpl_image_get_data_float(img);
1466 pmask = cpl_mask_get_data_const(mask);
1467
1468 for (int j = 0; j < nx*ny; j++) {
1469 if (pmask[j] == BAD_PIX) {
1470 pimg[j] = BAD_PIX;
1471 } else {
1472 pimg[j] = GOOD_PIX;
1473 }
1474 }
1475
1476 cpl_imagelist_set(img_list, img, i);
1477 }
1478 eris_ifu_save_imagelist_dbg(img_list, fn, create);
1479 eris_ifu_free_imagelist(&img_list);
1480 cpl_free(fn); fn = NULL;
1481 }
1482 CATCH
1483 {
1484 CATCH_MSGS();
1485 err = cpl_error_get_code();
1486 }
1487 eris_check_error_code("eris_ifu_save_hdrl_imagelist_dbg");
1488 return err;
1489}
1490
1492// @brief Loading image data of a given category.
1493// @param frameset The input set-of-frames.
1494// @param category The category of the image to load. If NULL, the
1495// next frame with same keyword as accessed right before will
1496// be returned
1497// @param ext The number of the extension. 0 for primary HDU
1498// @return The loaded image.
1499// Data type of CPL_TYPE_FLOAT is assumed.
1500//*/
1501//hdrl_image* eris_ifu_load_image_ctg(
1502// cpl_frameset *frameset,
1503// const char *category,
1504// int ext)
1505//{
1506// cpl_frame *frame = NULL; /* must not be deleted at the end */
1507// hdrl_image *imgHdrl = NULL;
1508//
1509// cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
1510// cpl_ensure(category, CPL_ERROR_NULL_INPUT, NULL);
1511//
1512// TRY
1513// {
1514// ASSURE(ext >= 0,
1515// CPL_ERROR_ILLEGAL_INPUT,
1516// "ext must be 0 or larger!");
1517//
1518// frame = cpl_frameset_find(frameset, category);
1519// CHECK_ERROR_STATE();
1520//
1521// if (frame != NULL) {
1522// imgHdrl = eris_ifu_load_image(cpl_frame_get_filename(frame),
1523// CPL_TYPE_FLOAT, 0, ext);
1524// }
1525// }
1526// CATCH
1527// {
1528// CATCH_MSG();
1529// eris_ifu_free_hdrl_image(&imgHdrl);
1530// }
1531// return imgHdrl;
1532//}
1533
1548 const cpl_frameset *frameset,
1549 const char *category,
1550 int ext)
1551{
1552 const cpl_frame *frame = NULL; /* must not be deleted at the end */
1553 cpl_mask *bp_mask = NULL;
1554
1555 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
1556 cpl_ensure(category, CPL_ERROR_NULL_INPUT, NULL);
1557
1558 TRY
1559 {
1560 ASSURE(ext >= 0,
1561 CPL_ERROR_ILLEGAL_INPUT,
1562 "ext must be 0 or larger!");
1563
1564 frame = cpl_frameset_find_const(frameset, category);
1566
1567 if (frame != NULL) {
1568 bp_mask = cpl_mask_load(cpl_frame_get_filename(frame), 0, ext);
1569 }
1570 }
1571 CATCH
1572 {
1573 CATCH_MSG();
1574 eris_ifu_free_mask(&bp_mask);
1575 }
1576 eris_check_error_code("eris_ifu_load_badpixel_mask");
1577 return bp_mask;
1578}
1579
1581// @brief
1582// Override for cpl_image_load().
1583
1584// @param filename Name of the file to load from.
1585// @param im_type Type of the created image
1586// @param pnum Plane number in the Data Unit (0 for first)
1587// @param xtnum Extension number in the file (0 for primary HDU)
1588
1589// @return The function returns the newly created image or NULL if an
1590// error occurred.
1591
1592// This is an override for cpl_image_load(), just giving a proper error
1593// message if the input file isn't found and rejecting all infinite values
1594// (NaN, +/-Inf). The errors returned are the same as with @c cpl_image_load
1595//*/
1596//hdrl_image* eris_ifu_load_image(const char *filename,
1597// cpl_type im_type,
1598// int pnum,
1599// int xtnum)
1600//{
1601// hdrl_image *imgHdrl = NULL;
1602// cpl_image *img = NULL;
1603// float *pimg = NULL;
1604// cpl_size nx = 0,
1605// ny = 0;
1606// int ix = 0,
1607// iy = 0;
1608
1609// cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
1610
1611// TRY
1612// {
1613// /* for the health of any developer:
1614// check if any pre-existing error is set */
1615// if (cpl_error_get_code() != CPL_ERROR_NONE) {
1616// cpl_msg_error("","An already existing error has been detected. "
1617// "Aborting now.");
1618// CHECK_ERROR_STATE();
1619// }
1620
1621// /* load image */
1622// img = cpl_image_load(filename, im_type, pnum, xtnum);
1623// if (cpl_error_get_code() != CPL_ERROR_NONE) {
1624// if (cpl_error_get_code() == CPL_ERROR_FILE_IO) {
1625// cpl_msg_error("", "File not found: %s", filename);
1626// } else {
1627// cpl_msg_debug("", "Problem loading file '%s' (%s --> Code %d)",
1628// filename, cpl_error_get_message(),
1629// cpl_error_get_code());
1630// }
1631// }
1632// CHECK_ERROR_STATE();
1633
1634// /* reject invalid values for internal bad pixel mask */
1635// pimg = cpl_image_get_data(img);
1636// nx = cpl_image_get_size_x(img);
1637// ny = cpl_image_get_size_y(img);
1638// for (iy = 0; iy < ny; iy++) {
1639// for (ix = 0; ix < nx; ix++) {
1640// if (!isfinite(pimg[ix+iy*nx])) {
1641// cpl_image_reject(img, ix+1, iy+1);
1642// }
1643// }
1644// }
1645
1646// /* create noise map*/
1647// imgHdrl = hdrl_image_create(img, NULL);
1648// }
1649// CATCH
1650// {
1651// eris_ifu_free_hdrl_image(&imgHdrl);
1652// }
1653
1654// eris_ifu_free_image(&img);
1655
1656// return imgHdrl;
1657//}
1658
1668cpl_error_code eris_ifu_file_exists(const char *filename)
1669{
1670 cpl_error_code err = CPL_ERROR_NONE;
1671 FILE *fh = NULL;
1672
1673 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
1674
1675 TRY
1676 {
1677 fh = fopen(filename, "r");
1678 }
1679 CATCH
1680 {
1681 err = cpl_error_get_code();
1682 }
1683
1684 if(fh != NULL) {
1685 fclose(fh);
1686 }
1687 eris_check_error_code("eris_ifu_file_exists");
1688 return err;
1689}
1690
1703cpl_imagelist* eris_ifu_hdrl_get_imagelist(const hdrl_imagelist *hdrl_imglist,
1704 enum hdrl_t type)
1705{
1706 cpl_imagelist *data = NULL;
1707 const hdrl_image *tmp_hdrl_img = NULL;
1708 cpl_image *tmp_img = NULL;
1709
1710 cpl_ensure(hdrl_imglist, CPL_ERROR_NULL_INPUT, NULL);
1711
1712 TRY
1713 {
1714 data = cpl_imagelist_new();
1715
1716 for (int i = 0; i < hdrl_imagelist_get_size(hdrl_imglist); i++) {
1717 tmp_hdrl_img = hdrl_imagelist_get(hdrl_imglist, i);
1718
1719 if (type == eris_hdrl_data) {
1720 /* type == data */
1721 tmp_img = cpl_image_duplicate(
1722 hdrl_image_get_image_const(tmp_hdrl_img));
1723 } else if (type == eris_hdrl_error) {
1724 /* type == error */
1725 tmp_img = cpl_image_duplicate(
1726 hdrl_image_get_error_const(tmp_hdrl_img));
1727 } else {
1728 /* type == badpix */
1729
1730 /* doesn't make sense yet */
1731 // tmp_img = cpl_image_duplicate(
1732 // hdrl_image_get_mask(tmp_hdrl_img));
1733 }
1734
1735 cpl_imagelist_set(data, tmp_img, i);
1736 }
1737 }
1738 CATCH
1739 {
1740 CATCH_MSGS();
1742 eris_ifu_free_image(&tmp_img);
1743 }
1744 eris_check_error_code("eris_ifu_hdrl_get_imagelist");
1745 return data;
1746}
1747
1762char* eris_ifu_get_lampString(int lampStatus)
1763{
1764 char *lampString;
1765 const char *arLampString = "";
1766 const char *krLampString = "";
1767 const char *neLampString = "";
1768 const char *xeLampString = "";
1769
1770 if ((lampStatus & AR_LAMP) == AR_LAMP) {arLampString = "Ar";}
1771 if ((lampStatus & KR_LAMP) == KR_LAMP) {krLampString = "Kr";}
1772 if ((lampStatus & NE_LAMP) == NE_LAMP) {neLampString = "Ne";}
1773 if ((lampStatus & XE_LAMP) == XE_LAMP) {xeLampString = "Xe";}
1774 lampString = cpl_sprintf("%s%s%s%s",
1775 arLampString, krLampString, neLampString, xeLampString);
1776 eris_check_error_code("eris_ifu_get_lampString");
1777 return lampString;
1778}
1779
1790const char* eris_ifu_get_bandString(ifsBand band)
1791{
1792 const char *bandString;
1793 switch (band) {
1794 case UNDEFINED_BAND:
1795 bandString = "";
1796 break;
1797 case J_LOW:
1798 bandString = "J_LOW";
1799 break;
1800 case H_LOW:
1801 bandString = "H_LOW";
1802 break;
1803 case K_LOW:
1804 bandString = "K_LOW";
1805 break;
1806 case J_SHORT:
1807 bandString = "J_SHORT";
1808 break;
1809 case J_MIDDLE:
1810 bandString = "J_MIDDLE";
1811 break;
1812 case J_LONG:
1813 bandString = "J_LONG";
1814 break;
1815 case H_SHORT:
1816 bandString = "H_SHORT";
1817 break;
1818 case H_MIDDLE:
1819 bandString = "H_MIDDLE";
1820 break;
1821 case H_LONG:
1822 bandString = "H_LONG";
1823 break;
1824 case K_SHORT:
1825 bandString = "K_SHORT";
1826 break;
1827 case K_MIDDLE:
1828 bandString = "K_MIDDLE";
1829 break;
1830 case K_LONG:
1831 bandString = "K_LONG";
1832 break;
1833 case J_SPIFFI:
1834 bandString = "J_SPIFFI";
1835 break;
1836 case H_SPIFFI:
1837 bandString = "H_SPIFFI";
1838 break;
1839 case K_SPIFFI:
1840 bandString = "K_SPIFFI";
1841 break;
1842 case HK_SPIFFI:
1843 bandString = "HK_SPIFFI";
1844 break;
1845 default:
1846 bandString = "Unknown";
1847 break;
1848 }
1849 eris_check_error_code("eris_ifu_get_bandString");
1850 return bandString;
1851}
1852
1868{
1869 double resolution;
1870 switch (band) {
1871 case UNDEFINED_BAND: resolution = 0; break;
1872 case J_LOW: resolution = 5000; break;
1873 case H_LOW: resolution = 5200; break;
1874 case K_LOW: resolution = 5600; break;
1875 case J_SHORT: resolution = 10000; break;
1876 case J_MIDDLE: resolution = 10000; break;
1877 case J_LONG: resolution = 10000; break;
1878 case H_SHORT: resolution = 10400; break;
1879 case H_MIDDLE: resolution = 10400; break;
1880 case H_LONG: resolution = 10400; break;
1881 case K_SHORT: resolution = 11200; break;
1882 case K_MIDDLE: resolution = 11200; break;
1883 case K_LONG: resolution = 11200; break;
1884 case J_SPIFFI: resolution = 5000; break;
1885 case H_SPIFFI: resolution = 5000; break;
1886 case K_SPIFFI: resolution = 5000; break;
1887 case HK_SPIFFI: resolution = 3000; break;
1888 default: resolution = 0; break;
1889 }
1890 eris_check_error_code("eris_ifu_get_band_resolution");
1891 return resolution;
1892}
1893
1894
1908cpl_error_code
1909eris_ifu_get_plane_cut_min_max(ifsBand band, cpl_size* zmin, cpl_size* zmax)
1910{
1911
1912 cpl_ensure_code(zmin >= 0, CPL_ERROR_ILLEGAL_INPUT);
1913
1914 switch (band) {
1915 case UNDEFINED_BAND: break;
1916 case J_LOW: *zmin = 46; *zmax = 2015; break;
1917 case H_LOW: *zmin = 48; *zmax = 2023; break;
1918 case K_LOW: *zmin = 62; *zmax = 2022; break;
1919 case J_SHORT: *zmin = 50; *zmax = 2034; break;
1920 case J_MIDDLE: *zmin = 57; *zmax = 2040; break;
1921 case J_LONG: *zmin = 48; *zmax = 2033; break;
1922 case H_SHORT: *zmin = 52; *zmax = 2034; break;
1923 case H_MIDDLE: *zmin = 47; *zmax = 2034; break;
1924 case H_LONG: *zmin = 56; *zmax = 2042; break;
1925 case K_SHORT: *zmin = 54; *zmax = 2036; break;
1926 case K_MIDDLE: *zmin = 49; *zmax = 2029; break;
1927 case K_LONG: *zmin = 50; *zmax = 2034; break;
1928 case J_SPIFFI: *zmin = 32; *zmax = 2058; break;
1929 case H_SPIFFI: *zmin = 32; *zmax = 2058; break;
1930 case K_SPIFFI: *zmin = 32; *zmax = 2058; break;
1931 case HK_SPIFFI: *zmin = 32; *zmax = 2058; break;
1932 default: break;
1933 }
1934 eris_check_error_code("eris_ifu_get_plane_cut_min_max");
1935 return cpl_error_get_code();
1936}
1937
1938
1939
1951//TODO: Why hvalue.data = k * 10.; ?
1952cpl_error_code
1953eris_ifu_cube_set_values(hdrl_imagelist** iml/*, cpl_imagelist** bpm*/) {
1954
1955
1956 cpl_size size = hdrl_imagelist_get_size(*iml);
1957 cpl_msg_info(cpl_func,"org size: %lld", size);
1958 hdrl_image* hima = NULL;
1959// cpl_image* ima = NULL;
1960 hdrl_value hvalue = {0, 0};
1961 for(cpl_size k = 0; k < size; k++) {
1962 hima = hdrl_imagelist_get(*iml, k);
1963 for(cpl_size j = 10; j < 20; j++) {
1964 for(cpl_size i = 10; i < 20; i++) {
1965 hvalue.data = k * 10.; //TODO: why this?
1966 hdrl_image_set_pixel(hima, i, j, hvalue);
1967 }
1968 }
1969 //hdrl_imagelist_unset(*iml, k);
1970 //hdrl_imagelist_set(*iml, k, hima);
1971 //cpl_imagelist_unset(*bpm, k);
1972 }
1973
1974 eris_check_error_code("eris_ifu_cube_set_values");
1975
1976 return cpl_error_get_code();
1977}
1978
2000cpl_error_code
2001eris_ifu_cube_trim_nans(const cpl_size zmin, const cpl_size zmax,
2002 hdrl_imagelist** iml, cpl_imagelist** bpm, cpl_propertylist* header) {
2003
2004 cpl_ensure_code(iml,CPL_ERROR_NULL_INPUT);
2005 cpl_ensure_code(bpm,CPL_ERROR_NULL_INPUT);
2006 cpl_ensure_code(header,CPL_ERROR_NULL_INPUT);
2007 cpl_ensure_code(zmin <= zmax,CPL_ERROR_ILLEGAL_INPUT);
2008 cpl_size size = hdrl_imagelist_get_size(*iml);
2009 cpl_msg_debug(cpl_func,"org size: %lld", size);
2010 /* we peel-off the cube starting from the end thus the index we touch
2011 * is always decreasing (maximum)
2012 */
2013 for(cpl_size i = size-1; i > zmax; i--) {
2014 hdrl_imagelist_unset(*iml, i);
2015 cpl_imagelist_unset(*bpm, i);
2016 }
2017 cpl_msg_debug(cpl_func,"size1: %lld", hdrl_imagelist_get_size(*iml));
2018 /* we peel-off the cube starting from the bottom thus the index we touch
2019 * is always 0 (minimum)
2020 */
2021 for(cpl_size i = 0; i < zmin; i++) {
2022 hdrl_imagelist_unset(*iml, 0);
2023 cpl_imagelist_unset(*bpm, 0);
2024 }
2025 cpl_msg_debug(cpl_func,"size2: %lld", hdrl_imagelist_get_size(*iml));
2026 double crval3 = cpl_propertylist_get_double(header,"CRVAL3");
2027 const double cd3_3 = cpl_propertylist_get_double(header,"CD3_3");
2028 int naxis3 = cpl_propertylist_get_int(header,"NAXIS3");
2029
2030 crval3 += cd3_3 * zmin;
2031 naxis3 = zmax - zmin +1;
2032 cpl_propertylist_set_double(header, "CRVAL3", crval3);
2033 cpl_propertylist_set_int(header, "NAXIS3", naxis3);
2034
2035 eris_check_error_code("eris_ifu_cube_trim_nans");
2036 return cpl_error_get_code();
2037}
2038
2039
2048const char* eris_ifu_get_instrumentString(ifsInstrument instrument)
2049{
2050 const char *instrumentString;
2051 switch (instrument) {
2052 case OTHER_INSTRUMENT: instrumentString = "unknown instrument"; break;
2053 case SPIFFI: instrumentString = "SPIFFI"; break;
2054 case SPIFFIER: instrumentString = "SPIFFIER"; break;
2055 case NIX: instrumentString = "NIX"; break;
2056 case UNSET_INSTRUMENT: instrumentString = "unset instrument"; break;
2057 default: instrumentString = "unset instrument"; break;
2058 }
2059 eris_check_error_code("eris_ifu_get_instrumentString");
2060 return instrumentString;
2061}
2062
2073const char* eris_ifu_get_preopticsScaleString(ifsPreopticsScale scale)
2074{
2075 const char *scaleString;
2076 switch (scale) {
2077 case UNDEFINED_SCALE:
2078 scaleString = "";
2079 break;
2080 case S250MAS:
2081 scaleString = "250mas";
2082 break;
2083 case S100MAS:
2084 scaleString = "100mas";
2085 break;
2086 case S25MAS:
2087 scaleString = "25mas";
2088 break;
2089 case PUPIL:
2090 scaleString = "PUPIL";
2091 break;
2092 default:
2093 scaleString = "Unknown";
2094 break;
2095 }
2096 eris_check_error_code("eris_ifu_get_preopticsScaleString");
2097 return scaleString;
2098}
2099
2111cpl_image* eris_ifu_image_create_window(const cpl_image* img)
2112{
2113 cpl_size nx = 0,
2114 ny = 0;
2115 double *pimg_window = NULL;
2116 const double *pimg = NULL;
2117 cpl_image *img_window = NULL;
2118 const cpl_mask *mask = NULL;
2119 cpl_mask *mask_window = NULL;
2120 const cpl_binary *pmask = NULL;
2121 cpl_binary *pmask_window = NULL;
2122
2123 cpl_ensure(img, CPL_ERROR_NULL_INPUT, NULL);
2124
2125 TRY
2126 {
2127 nx = cpl_image_get_size_x(img);
2128 ny = cpl_image_get_size_y(img);
2130 img_window = cpl_image_new(nx-2*ERIS_IFU_DETECTOR_BP_BORDER,
2131 ny-2*ERIS_IFU_DETECTOR_BP_BORDER,
2132 cpl_image_get_type(img));
2133
2134 pimg = cpl_image_get_data_const(img);
2135 mask = cpl_image_get_bpm_const(img);
2136 pmask = cpl_mask_get_data_const(mask);
2137 pimg_window = cpl_image_get_data(img_window);
2138 mask_window = cpl_image_get_bpm(img_window);
2139 pmask_window = cpl_mask_get_data(mask_window);
2140
2141 for (int x = 0; x < nx-2*ERIS_IFU_DETECTOR_BP_BORDER; x++) {
2142 for (int y = 0; y < ny-2*ERIS_IFU_DETECTOR_BP_BORDER; y++) {
2143 cpl_size nx_new = nx-2*ERIS_IFU_DETECTOR_BP_BORDER,
2144 x_old = x+ERIS_IFU_DETECTOR_BP_BORDER,
2145 y_old = y+ERIS_IFU_DETECTOR_BP_BORDER;
2146 pimg_window[x+y*nx_new] = pimg[x_old+y_old*nx];
2147 pmask_window[x+y*nx_new] = pmask[x_old+y_old*nx];
2148 }
2149 }
2150 }
2151 CATCH
2152 {
2153 CATCH_MSGS();
2154 eris_ifu_free_image(&img_window);
2155 }
2156 eris_check_error_code("eris_ifu_image_create_window");
2157 return img_window;
2158}
2159
2171cpl_mask* eris_ifu_mask_create_border(const cpl_mask* mask)
2172{
2173 cpl_size nx = 0,
2174 ny = 0;
2175 cpl_binary *pmask_border = NULL;
2176 const cpl_binary *pmask = NULL;
2177 cpl_mask *mask_border = NULL;
2178
2179 cpl_ensure(mask, CPL_ERROR_NULL_INPUT, NULL);
2180
2181 TRY
2182 {
2183 nx = cpl_mask_get_size_x(mask);
2184 ny = cpl_mask_get_size_y(mask);
2186
2187 mask_border = cpl_mask_new(nx+2*ERIS_IFU_DETECTOR_BP_BORDER,
2188 ny+2*ERIS_IFU_DETECTOR_BP_BORDER);
2189
2190 pmask = cpl_mask_get_data_const(mask);
2191 pmask_border = cpl_mask_get_data(mask_border);
2192
2193 for (int x = 0; x < nx; x++) {
2194 for (int y = 0; y < ny; y++) {
2195 cpl_size x_new = x+ERIS_IFU_DETECTOR_BP_BORDER,
2196 y_new = y+ERIS_IFU_DETECTOR_BP_BORDER,
2197 nx_new = nx+2*ERIS_IFU_DETECTOR_BP_BORDER;
2198 pmask_border[x_new+y_new*nx_new] = pmask[x+y*nx];
2199 }
2200 }
2201 }
2202 CATCH
2203 {
2204 CATCH_MSGS();
2205 eris_ifu_free_mask(&mask_border);
2206 }
2207 eris_check_error_code("eris_ifu_mask_create_border");
2208 return mask_border;
2209}
2210
2211/*----------------------------------------------------------------------------*/
2223/*----------------------------------------------------------------------------*/
2224
2225cpl_error_code
2227
2228 double* pdata = NULL;
2229 double* perrs = NULL;
2230 cpl_binary* pbpm = NULL;
2231 cpl_size sx = 0;
2232 cpl_size sy = 0;
2233 cpl_size npoints = 0;
2234 cpl_ensure(hima != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2235
2236
2237 sx = hdrl_image_get_size_x( *hima );
2238 sy = hdrl_image_get_size_y( *hima );
2239 npoints = sx * sy;
2240
2241 pdata = cpl_image_get_data(hdrl_image_get_image(*hima));
2242 perrs = cpl_image_get_data(hdrl_image_get_error(*hima));
2243 pbpm = cpl_mask_get_data(hdrl_image_get_mask(*hima));
2244
2245 cpl_size nan_pix = 0;
2246 for(cpl_size i = 0; i < npoints; i++) {
2247 //cpl_msg_error(cpl_func,"pdata[%lld]:%g perrs[%lld]:%g",i,pdata[i],i,perrs[i]);
2248 //if(pdata[i] == CPL_VALUE_NOTFINITE || perrs[i] == CPL_VALUE_NOTFINITE ){
2249 if(isnan(pdata[i]) || isnan(perrs[i]) ){
2250 //cpl_msg_info(cpl_func,"found NAN at %lld",i);
2251 pbpm[i] = BAD_PIX;
2252 nan_pix++;
2253 }
2254 }
2255
2256 eris_check_error_code("eris_ifu_mask_nans_in_hdrlimage");
2257 return cpl_error_get_code();
2258}
2259
2260/*----------------------------------------------------------------------------*/
2273/*----------------------------------------------------------------------------*/
2274cpl_error_code eris_ifu_mask_nans_in_cube(cpl_imagelist *cube)
2275{
2276 cpl_error_code retVal = CPL_ERROR_NONE;
2277 cpl_size nx = 0,
2278 ny = 0,
2279 nz = 0,
2280 idx = 0;
2281 cpl_image *tmpImg = NULL;
2282 cpl_mask *mask = NULL;
2283 cpl_binary *maskData = NULL;
2284 const double *imgDataD = NULL;
2285 const float *imgDataF = NULL;
2286 cpl_type imgType = CPL_TYPE_INVALID;
2287
2288 cpl_ensure(cube, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2289
2290 TRY
2291 {
2292 nz = cpl_imagelist_get_size(cube);
2293 if (nz > 0) {
2294 tmpImg = cpl_imagelist_get(cube,0);
2295 imgType = cpl_image_get_type(tmpImg);
2296 if (! (imgType == CPL_TYPE_FLOAT || imgType == CPL_TYPE_DOUBLE)) {
2297 BRK_WITH_ERROR_MSG(CPL_ERROR_INVALID_TYPE,
2298 "unsupported image type within cube");
2299 }
2300 nx = cpl_image_get_size_x(tmpImg);
2301 ny = cpl_image_get_size_y(tmpImg);
2302 }
2304 for (cpl_size z=0; z < nz; z++) {
2305 tmpImg = cpl_imagelist_get(cube,z);
2306 switch (imgType) {
2307 case CPL_TYPE_FLOAT:
2308 imgDataF = cpl_image_get_data_float_const(tmpImg);
2309 break;
2310 case CPL_TYPE_DOUBLE:
2311 imgDataD = cpl_image_get_data_double_const(tmpImg);
2312 break;
2313 default: break;
2314 }
2315 mask = cpl_image_get_bpm(tmpImg);
2316 maskData = cpl_mask_get_data(mask);
2317 for (cpl_size y=0; y < ny; y++) {
2318 for (cpl_size x=0; x < nx; x++) {
2319 idx = y * nx + x;
2320 switch (imgType) {
2321 case CPL_TYPE_FLOAT:
2322 if (!isfinite(imgDataF[idx])) {
2323 maskData[idx] = BAD_PIX;
2324 }
2325 break;
2326 case CPL_TYPE_DOUBLE:
2327 if (!isfinite(imgDataD[idx])) {
2328 maskData[idx] = BAD_PIX;
2329 }
2330 break;
2331 default: break;
2332 }
2333 }
2334 }
2336
2337 }
2338 }
2339 CATCH {
2340 retVal = cpl_error_get_code();
2341 }
2342 eris_check_error_code("eris_ifu_mask_nans_in_cube");
2343 return retVal;
2344}
2345
2346// /**
2347// * @brief eris_ifu_mask_reset
2348// * @param mask
2349// * @return
2350// */
2351//cpl_error_code eris_ifu_mask_reset(cpl_mask* mask)
2352//{
2353// cpl_error_code err = CPL_ERROR_NONE;
2354// cpl_size nx = 0,
2355// ny = 0;
2356//
2357// cpl_ensure_code(mask, CPL_ERROR_NULL_INPUT);
2358//
2359// TRY
2360// {
2361// nx = cpl_mask_get_size_x(mask);
2362// ny = cpl_mask_get_size_y(mask);
2363// CHECK_ERROR_STATE();
2364//
2365// for (int x = 1; x <= nx; x++) {
2366// for (int y = 1; y <= ny; y++) {
2367// cpl_mask_set(mask, x, y, GOOD_PIX);
2368// }
2369// }
2370// }
2371// CATCH
2372// {
2373// CATCH_MSGS();
2374// err = cpl_error_get_code();
2375// }
2376// return err;
2377//}
2378/*----------------------------------------------------------------------------*/
2379/* TODO: document bit code. Why bit code is double and not integer? */
2387/*----------------------------------------------------------------------------*/
2388cpl_mask* eris_ifu_quality2bp_mask(const cpl_image *qualityMask,
2389 deqQualityType qualityType)
2390{
2391 cpl_mask *bpmMask = NULL;
2392 TRY
2393 {
2394 ASSURE((qualityMask != NULL), CPL_ERROR_NULL_INPUT, NULL);
2395 bpmMask = cpl_mask_threshold_image_create(
2396 qualityMask, .5, 4294967296.5 );
2397 switch(qualityType) {
2398 case maskzero:
2399 cpl_mask_not(bpmMask);
2400 break;
2401 case maskone:
2402 break;
2403 case flag32bit:
2404 break;
2405 case flag16bit:
2406 break;
2407 case unspecified:
2408 cpl_mask_delete(bpmMask);
2409 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
2410 "Unspecified DEQ quality type");
2411
2412 break;
2413 default:
2414 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
2415 "Unknown DEQ quality type");
2416 }
2417 }
2418 CATCH {
2419 bpmMask = NULL;
2420 }
2421 eris_check_error_code("eris_ifu_quality2bp_mask");
2422 return bpmMask;
2423
2424}
2425/*----------------------------------------------------------------------------*/
2433/*----------------------------------------------------------------------------*/
2434cpl_imagelist* eris_ifu_interpolatedMask_to_maskZero(cpl_imagelist *bpmCube,
2435 double threshold)
2436{
2437 cpl_imagelist *maskOneCube = NULL;
2438 cpl_image *img = NULL;
2439 cpl_image *maskOneImg = NULL;
2440 int *maskOneImgPtr = NULL;
2441 float *floatPtr = NULL;
2442 double *doublePtr = NULL;
2443 float floatThreshold;
2444 cpl_size imgPixelCnt = 0,
2445 nx = 0,
2446 ny = 0;
2447 cpl_type bpmType;
2448
2449 TRY
2450 {
2451 ASSURE(bpmCube != NULL, CPL_ERROR_NULL_INPUT, "bpmCube must not be NULL");
2452
2453 maskOneCube = cpl_imagelist_new();
2454
2455 img = cpl_imagelist_get(bpmCube, 0);
2456 bpmType = cpl_image_get_type(img);
2457 ASSURE(bpmType == CPL_TYPE_FLOAT || bpmType == CPL_TYPE_DOUBLE,
2458 CPL_ERROR_ILLEGAL_INPUT, "bpmCube must be of type float or double");
2459
2460 nx = cpl_image_get_size_x(img);
2461 ny = cpl_image_get_size_y(img);
2462 imgPixelCnt = nx * ny;
2463
2464 floatThreshold = (float) threshold;
2465
2466 for (cpl_size cx=0; cx < cpl_imagelist_get_size(bpmCube); cx++) {
2467 img = cpl_imagelist_get(bpmCube, cx);
2468
2469 maskOneImg = cpl_image_new(nx, ny, CPL_TYPE_INT);
2470 maskOneImgPtr = cpl_image_get_data_int(maskOneImg);
2471
2472 if (bpmType == CPL_TYPE_FLOAT) {
2473 floatPtr = cpl_image_get_data_float(img);
2474 for (cpl_size px=0; px < imgPixelCnt; px++) {
2475 if (floatPtr[px] > floatThreshold) {
2476 maskOneImgPtr[px] = 1;
2477 } else {
2478 maskOneImgPtr[px] = 0;
2479 }
2480 }
2481 } else if (bpmType == CPL_TYPE_DOUBLE) {
2482 doublePtr = cpl_image_get_data_double(img);
2483 for (cpl_size px=0; px < imgPixelCnt; px++) {
2484 if (doublePtr[px] > threshold) {
2485 maskOneImgPtr[px] = 1;
2486 } else {
2487 maskOneImgPtr[px] = 0;
2488 }
2489 }
2490
2491 }
2492
2493 cpl_imagelist_set(maskOneCube, maskOneImg, cx);
2494 }
2495 }
2496 CATCH {
2497 maskOneCube = NULL;
2498 }
2499 eris_check_error_code("eris_ifu_interpolatedMask_to_maskZero");
2500 return maskOneCube;
2501
2502}
2503/*----------------------------------------------------------------------------*/
2513/*----------------------------------------------------------------------------*/
2514cpl_error_code eris_ifu_split_hdrl_imagelist(hdrl_imagelist *cube,
2515 cpl_imagelist *dataCube,
2516 cpl_imagelist *errorCube)
2517{
2518 cpl_error_code retVal = CPL_ERROR_NONE;
2519 hdrl_image *image = NULL;
2520 TRY
2521 {
2522
2523 cpl_ensure(cube != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2524 cpl_ensure(dataCube != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2525 cpl_ensure(errorCube != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2526 for (cpl_size ix=0; ix < hdrl_imagelist_get_size(cube); ix++) {
2527 image = hdrl_imagelist_get(cube, ix);
2528 cpl_imagelist_set(dataCube, cpl_image_duplicate(hdrl_image_get_image(image)), ix);
2529 cpl_imagelist_set(errorCube, cpl_image_duplicate(hdrl_image_get_error(image)), ix);
2530
2531 }
2532 }
2533 CATCH {
2534 }
2535 eris_check_error_code("eris_ifu_split_hdrl_imagelist");
2536 return retVal;
2537
2538}
2539
2540/*----------------------------------------------------------------------------*/
2551/*----------------------------------------------------------------------------*/
2552cpl_error_code eris_ifu_split3_hdrl_imagelist(hdrl_imagelist *cube,
2553 cpl_imagelist *dataCube,
2554 cpl_imagelist *errorCube,
2555 cpl_imagelist *qualCube)
2556{
2557 cpl_error_code retVal = CPL_ERROR_NONE;
2558 hdrl_image *image = NULL;
2559 TRY
2560 {
2561 cpl_ensure(cube != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2562 cpl_ensure(dataCube != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2563 cpl_ensure(errorCube != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2564 cpl_ensure(qualCube != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
2565
2566 for (cpl_size ix=0; ix < hdrl_imagelist_get_size(cube); ix++) {
2567 image = hdrl_imagelist_get(cube, ix);
2568 cpl_imagelist_set(dataCube,
2569 hdrl_image_get_image(image), ix);
2570 cpl_imagelist_set(errorCube,
2571 hdrl_image_get_error(image), ix);
2572 cpl_imagelist_set(qualCube,
2573 cpl_image_new_from_mask(hdrl_image_get_mask(image)), ix);
2574
2575 }
2576 }
2577 CATCH {
2578 }
2579 eris_check_error_code("eris_ifu_split3_hdrl_imagelist");
2580 return retVal;
2581
2582}
2583/*----------------------------------------------------------------------------*/
2590/*----------------------------------------------------------------------------*/
2591cpl_table *
2593{
2594
2595 cpl_table *table;
2596
2597 table = cpl_table_new(0);
2598 cpl_table_new_column(table,"key_name", CPL_TYPE_STRING);
2599 cpl_table_new_column(table,"key_type", CPL_TYPE_STRING);
2600 cpl_table_new_column(table,"key_value", CPL_TYPE_STRING);
2601 cpl_table_new_column(table,"key_help", CPL_TYPE_STRING);
2602 eris_check_error_code("eris_qclog_init");
2603 return table;
2604}
2605/*----------------------------------------------------------------------------*/
2616/*----------------------------------------------------------------------------*/
2617cpl_error_code
2618eris_qclog_add_int(cpl_table* table,
2619 const char* key_name,
2620 const int value,
2621 const char* key_help)
2622{
2623
2624 cpl_ensure_code(table != NULL, CPL_ERROR_NULL_INPUT);
2625 cpl_ensure_code(key_name != NULL, CPL_ERROR_NULL_INPUT);
2626 cpl_ensure_code(key_help != NULL, CPL_ERROR_NULL_INPUT);
2627
2628 int sz = cpl_table_get_nrow(table);
2629 int raw = sz;
2630 char* key_value;
2631 char* key_type;
2632
2633 key_value = cpl_sprintf("%d",value);
2634 key_type = cpl_sprintf("%s","CPL_TYPE_INT");
2635
2636 cpl_table_set_size(table,sz+1);
2637
2638 cpl_table_set_string(table,"key_name" ,raw,key_name);
2639 cpl_table_set_string(table,"key_type" ,raw,key_type);
2640 cpl_table_set_string(table,"key_value",raw,key_value);
2641 cpl_table_set_string(table,"key_help" ,raw,key_help);
2642
2643 cpl_free(key_value);
2644 cpl_free(key_type);
2645 eris_check_error_code("eris_qclog_add_int");
2646 return cpl_error_get_code();
2647
2648}
2649
2650/*----------------------------------------------------------------------------*/
2660/*----------------------------------------------------------------------------*/
2661
2662cpl_error_code
2663eris_qclog_add_bool(cpl_table* table,
2664 const char* key_name,
2665 const char* key_help)
2666{
2667 cpl_ensure_code(table != NULL, CPL_ERROR_NULL_INPUT);
2668 cpl_ensure_code(key_name != NULL, CPL_ERROR_NULL_INPUT);
2669 cpl_ensure_code(key_help != NULL, CPL_ERROR_NULL_INPUT);
2670
2671 int sz = cpl_table_get_nrow(table);
2672 int raw = sz;
2673 char* key_value;
2674 char* key_type;
2675
2676 key_value = cpl_sprintf("%s",key_name);
2677 key_type = cpl_sprintf("%s","CPL_TYPE_BOOL");
2678
2679 cpl_table_set_size(table,sz+1);
2680
2681 cpl_table_set_string(table,"key_name" ,raw,key_name);
2682 cpl_table_set_string(table,"key_type" ,raw,key_type);
2683 cpl_table_set_string(table,"key_value",raw,key_value);
2684 cpl_table_set_string(table,"key_help" ,raw,key_help);
2685 cpl_free(key_value);
2686 cpl_free(key_type);
2687 eris_check_error_code("eris_qclog_add_bool");
2688 return cpl_error_get_code();
2689
2690}
2691
2692/*----------------------------------------------------------------------------*/
2703/*----------------------------------------------------------------------------*/
2704cpl_error_code
2705eris_qclog_add_double(cpl_table* table,
2706 const char* key_name,
2707 const double value,
2708 const char* key_help)
2709{
2710 cpl_ensure_code(table != NULL, CPL_ERROR_NULL_INPUT);
2711 cpl_ensure_code(key_name != NULL, CPL_ERROR_NULL_INPUT);
2712 cpl_ensure_code(key_help != NULL, CPL_ERROR_NULL_INPUT);
2713
2714 int sz = cpl_table_get_nrow(table);
2715
2716 int raw = sz;
2717 char* key_value;
2718 char* key_type;
2719
2720 key_value = cpl_sprintf("%g",value);
2721
2722 key_type = cpl_sprintf("%s","CPL_TYPE_DOUBLE");
2723
2724 cpl_table_set_size(table,sz+1);
2725
2726 cpl_table_set_string(table,"key_name" ,raw,key_name);
2727 cpl_table_set_string(table,"key_type" ,raw,key_type);
2728 cpl_table_set_string(table,"key_value",raw,key_value);
2729 cpl_table_set_string(table,"key_help" ,raw,key_help);
2730
2731 cpl_free(key_value);
2732 cpl_free(key_type);
2733 eris_check_error_code("eris_qclog_add_double");
2734 return cpl_error_get_code();
2735
2736}
2737/*----------------------------------------------------------------------------*/
2748/*----------------------------------------------------------------------------*/
2749cpl_error_code
2751 const char* key_name,
2752 const double value,
2753 const char* key_help)
2754{
2755
2756 cpl_ensure_code(table != NULL, CPL_ERROR_NULL_INPUT);
2757 cpl_ensure_code(key_name != NULL, CPL_ERROR_NULL_INPUT);
2758 cpl_ensure_code(key_help != NULL, CPL_ERROR_NULL_INPUT);
2759
2760 int sz = cpl_table_get_nrow(table);
2761 int raw = sz;
2762 char* key_value;
2763 char* key_type;
2764
2765 key_value = cpl_sprintf("%f",value);
2766 key_type = cpl_sprintf("%s","CPL_TYPE_DOUBLE");
2767
2768 cpl_table_set_size(table,sz+1);
2769
2770 cpl_table_set_string(table,"key_name" ,raw,key_name);
2771 cpl_table_set_string(table,"key_type" ,raw,key_type);
2772 cpl_table_set_string(table,"key_value",raw,key_value);
2773 cpl_table_set_string(table,"key_help" ,raw,key_help);
2774 cpl_free(key_value);
2775 cpl_free(key_type);
2776 eris_check_error_code("eris_qclog_add_double_f");
2777 return cpl_error_get_code();
2778
2779}
2780/*----------------------------------------------------------------------------*/
2791/*----------------------------------------------------------------------------*/
2792cpl_error_code
2794 const char* key_name,
2795 const double value,
2796 const char* key_help)
2797{
2798
2799 cpl_ensure_code(table != NULL, CPL_ERROR_NULL_INPUT);
2800 cpl_ensure_code(key_name != NULL, CPL_ERROR_NULL_INPUT);
2801 cpl_ensure_code(key_help != NULL, CPL_ERROR_NULL_INPUT);
2802
2803 int sz = cpl_table_get_nrow(table);
2804 int raw = sz;
2805 char* key_value;
2806 char* key_type;
2807
2808 key_value = cpl_sprintf("%13.6f",value);
2809 key_type = cpl_sprintf("%s","CPL_TYPE_DOUBLE");
2810
2811 cpl_table_set_size(table,sz+1);
2812
2813 cpl_table_set_string(table,"key_name" ,raw,key_name);
2814 cpl_table_set_string(table,"key_type" ,raw,key_type);
2815 cpl_table_set_string(table,"key_value",raw,key_value);
2816 cpl_table_set_string(table,"key_help" ,raw,key_help);
2817 cpl_free(key_value);
2818 cpl_free(key_type);
2819 eris_check_error_code("eris_qclog_add_double_format");
2820 return cpl_error_get_code();
2821
2822}
2823/*----------------------------------------------------------------------------*/
2834/*----------------------------------------------------------------------------*/
2835cpl_error_code
2836eris_qclog_add_string(cpl_table* table,
2837 const char* key_name,
2838 const char* value,
2839 const char* key_help)
2840{
2841
2842 cpl_ensure_code(table != NULL, CPL_ERROR_NULL_INPUT);
2843 cpl_ensure_code(key_name != NULL, CPL_ERROR_NULL_INPUT);
2844 cpl_ensure_code(key_help != NULL, CPL_ERROR_NULL_INPUT);
2845
2846 int sz = cpl_table_get_nrow(table);
2847 int raw = sz;
2848 char* key_value;
2849 char* key_type;
2850
2851 key_value = cpl_sprintf("%s",value);
2852 key_type = cpl_sprintf("%s","CPL_TYPE_STRING");
2853
2854 cpl_table_set_size(table,sz+1);
2855
2856 cpl_table_set_string(table,"key_name" ,raw,key_name);
2857 cpl_table_set_string(table,"key_type" ,raw,key_type);
2858 cpl_table_set_string(table,"key_value",raw,key_value);
2859 cpl_table_set_string(table,"key_help" ,raw,key_help);
2860 cpl_free(key_value);
2861 cpl_free(key_type);
2862 eris_check_error_code("eris_qclog_add_string");
2863 return cpl_error_get_code();
2864
2865}
2866
2867/*----------------------------------------------------------------------------*/
2876/*----------------------------------------------------------------------------*/
2877cpl_error_code
2879 cpl_propertylist * plist,
2880 cpl_table * qclog)
2881{
2882
2883
2884 cpl_ensure_code(plist != NULL, CPL_ERROR_NULL_INPUT);
2885 cpl_ensure_code(qclog != NULL, CPL_ERROR_NULL_INPUT);
2886 char* key_name = NULL;
2887 char* key_value = NULL;
2888 char* key_type = NULL;
2889 char* key_help = NULL;
2890 /*
2891 char key_name[FILE_NAME_SZ];
2892 char key_value[FILE_NAME_SZ];
2893 char key_type[FILE_NAME_SZ];
2894 char key_help[FILE_NAME_SZ] ;
2895 */
2896
2897 int i = 0;
2898 int n = 0;
2899 /* Test entries */
2900 if (plist == NULL) {
2901 cpl_msg_error(cpl_func,"plist=NULL, something strange");
2902 return CPL_ERROR_NULL_INPUT ;
2903 }
2904 /* Parameter Name: PIPEFILE */
2905
2906 //cpl_table_save(qclog,NULL,NULL,"qclog.fits",CPL_IO_CREATE);
2907 n = cpl_table_get_nrow(qclog);
2908 for(i = 0; i < n; i++) {
2909
2910 key_name = cpl_sprintf("ESO %s",cpl_table_get_string(qclog, "key_name", i));
2911 //strcat(key_name, cpl_table_get_string(qclog, "key_name", i));
2912 key_type = cpl_sprintf("%s", cpl_table_get_string(qclog, "key_type", i));
2913 //strcpy(key_type, cpl_table_get_string(qclog, "key_type", i));
2914 key_value = cpl_sprintf("%s", cpl_table_get_string(qclog, "key_value", i));
2915 //strcpy(key_value, cpl_table_get_string(qclog, "key_value", i));
2916 key_help = cpl_sprintf("%s", cpl_table_get_string(qclog, "key_help", i));
2917 //strcpy(key_help, cpl_table_get_string(qclog, "key_help", i));
2918 /*
2919 cpl_msg_info(cpl_func,"raw %i, name=%s type=%s value=%s\n",
2920 i,key_name,key_type,key_value);
2921 */
2922 if( !cpl_propertylist_has(plist, key_name) ) {
2923 if(strcmp(key_type, "CPL_TYPE_STRING") == 0) {
2924 cpl_propertylist_append_string(plist, key_name, key_value) ;
2925 cpl_propertylist_set_comment(plist, key_name, key_help) ;
2926 } else if(strcmp(key_type, "CPL_TYPE_BOOL") == 0) {
2927 cpl_propertylist_append_bool(plist, key_name, atoi(key_value)) ;
2928 cpl_propertylist_set_comment(plist, key_name, key_help) ;
2929 } else if(strcmp(key_type, "CPL_TYPE_INT") == 0) {
2930 cpl_propertylist_append_int(plist, key_name, atoi(key_value)) ;
2931 cpl_propertylist_set_comment(plist, key_name, key_help) ;
2932 } else if(strcmp(key_type, "CPL_TYPE_FLOAT") == 0) {
2933 cpl_propertylist_append_float(plist, key_name, (float) atof(key_value)) ;
2934 cpl_propertylist_set_comment(plist, key_name, key_help) ;
2935 } else if(strcmp(key_type, "CPL_TYPE_DOUBLE") == 0) {
2936 cpl_propertylist_append_double(plist, key_name, atof(key_value)) ;
2937 cpl_propertylist_set_comment(plist, key_name, key_help) ;
2938 }
2939 }
2940 cpl_free(key_name);
2941 cpl_free(key_type);
2942 cpl_free(key_value);
2943 cpl_free(key_help);
2944
2945 }
2946 eris_check_error_code("eris_pfits_put_qc");
2947 return cpl_error_get_code();
2948}
2949/*----------------------------------------------------------------------------*/
2959/*----------------------------------------------------------------------------*/
2960cpl_error_code
2961eris_ifu_get_badpix_qc_from_ima(const cpl_image* image,
2962 cpl_propertylist* qc_list,
2963 const char* prefix)
2964{
2965
2966 cpl_ensure_code(image != NULL, CPL_ERROR_NULL_INPUT);
2967 cpl_ensure_code(qc_list != NULL, CPL_ERROR_NULL_INPUT);
2968 cpl_ensure_code(prefix != NULL, CPL_ERROR_NULL_INPUT);
2969 cpl_size sx = cpl_image_get_size_x(image);
2970 cpl_size sy = cpl_image_get_size_y(image);
2971 cpl_image* ima = cpl_image_duplicate(image);
2972 cpl_image_threshold(ima, 0, 0, 0, 1);
2973 double flux = cpl_image_get_flux(ima);
2974 cpl_image_delete(ima);
2975 cpl_size nBadPix = (cpl_size) flux;
2976
2977 cpl_size npix = sx * sy;
2978 double fracBadPix = flux / npix;
2979 char* kname;
2980 cpl_msg_warning(cpl_func,"nBadPix: %lld",nBadPix);
2981 kname = cpl_sprintf("%s NBADPIX",prefix);
2982 cpl_msg_info(cpl_func,"kname: %s prefix: %s",kname, prefix);
2983 eris_ifu_append_qc_int(qc_list, kname, nBadPix, "Number of bad pixels");
2984 cpl_free(kname);
2985
2986 kname = cpl_sprintf("%s BPIXFRAC",prefix);
2987 eris_ifu_append_qc_double(qc_list, kname, fracBadPix,
2988 "Fraction of bad pixels to total");
2989 cpl_free(kname);
2990 eris_check_error_code("eris_ifu_get_badpix_qc_from_ima");
2991 return cpl_error_get_code();
2992}
2993
2994/*----------------------------------------------------------------------------*/
3004/*----------------------------------------------------------------------------*/
3005cpl_error_code
3006eris_ifu_get_badpix_qc_from_mask(const cpl_mask* bp_mask,
3007 cpl_propertylist* qc_list,
3008 const char* prefix)
3009{
3010 cpl_ensure_code(bp_mask != NULL, CPL_ERROR_NULL_INPUT);
3011 cpl_ensure_code(qc_list != NULL, CPL_ERROR_NULL_INPUT);
3012 cpl_ensure_code(prefix != NULL, CPL_ERROR_NULL_INPUT);
3013 cpl_size sx = cpl_mask_get_size_x(bp_mask);
3014 cpl_size sy = cpl_mask_get_size_y(bp_mask);
3015 cpl_size nBadPix = cpl_mask_count(bp_mask);
3016
3017 cpl_size npix = sx * sy;
3018 double fracBadPix = (double) nBadPix / npix;
3019 char* kname;
3020
3021 kname = cpl_sprintf("%s NBADPIX",prefix);
3022 cpl_msg_info(cpl_func,"kname: %s prefix: %s",kname, prefix);
3023 eris_ifu_append_qc_int(qc_list, kname, nBadPix, "Number of bad pixels");
3024 cpl_free(kname);
3025
3026 kname = cpl_sprintf("%s BPIXFRAC",prefix);
3027 eris_ifu_append_qc_double(qc_list, kname, fracBadPix,
3028 "Fraction of bad pixels to total");
3029 cpl_free(kname);
3030 eris_check_error_code("eris_ifu_get_badpix_qc_from_mask");
3031 return cpl_error_get_code();
3032}
3033
3034/*----------------------------------------------------------------------------*/
3054/*----------------------------------------------------------------------------*/
3055static cpl_error_code eris_apertures_find_max_flux(const cpl_apertures * self,
3056 int * ind, int nfind)
3057{
3058
3059
3060
3061 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
3062
3063 const int nsize = cpl_apertures_get_size(self);
3064 int ifind;
3065
3066
3067 cpl_ensure_code(nsize > 0, CPL_ERROR_ILLEGAL_INPUT);
3068 cpl_ensure_code(ind, CPL_ERROR_NULL_INPUT);
3069 cpl_ensure_code(nfind > 0, CPL_ERROR_ILLEGAL_INPUT);
3070 cpl_ensure_code(nfind <= nsize, CPL_ERROR_ILLEGAL_INPUT);
3071
3072 for (ifind=0; ifind < nfind; ifind++) {
3073 double maxflux = -1;
3074 int maxind = -1;
3075 int i;
3076 for (i=1; i <= nsize; i++) {
3077 int k;
3078
3079 /* The flux has to be the highest among those not already found */
3080 for (k=0; k < ifind; k++) if (ind[k] == i) break;
3081
3082 if (k == ifind) {
3083 /* i has not been inserted into ind */
3084 const double flux = cpl_apertures_get_flux(self, i);
3085
3086 if (maxind < 0 || flux > maxflux) {
3087 maxind = i;
3088 maxflux = flux;
3089 }
3090 }
3091 }
3092 ind[ifind] = maxind;
3093 }
3094 eris_check_error_code("eris_apertures_find_max_flux");
3095 return CPL_ERROR_NONE;
3096
3097}
3098
3099/*----------------------------------------------------------------------------*/
3119/*----------------------------------------------------------------------------*/
3120cpl_error_code
3121eris_gaussian_maxpos(const cpl_image * self,
3122 double sigma,
3123 double * pxpos,
3124 double * pypos,
3125 double * ppeak,
3126 double * sigma_x,
3127 double * sigma_y)
3128{
3129 /* copied from irplib_strehl.c r163170 */
3130
3131
3132 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
3133 cpl_ensure(sigma > 0, CPL_ERROR_ILLEGAL_INPUT, CPL_ERROR_ILLEGAL_INPUT);
3134
3135
3136
3137 const cpl_size nx = cpl_image_get_size_x(self);
3138 const cpl_size ny = cpl_image_get_size_y(self);
3139 int iretry = 3; /* Number retries with decreasing sigma */
3140 int ifluxapert = 0;
3141 double med_dist;
3142 const double median = cpl_image_get_median_dev(self, &med_dist);
3143 cpl_mask * selection = NULL;
3144 cpl_size nlabels = 0;
3145 cpl_image * labels = NULL;
3146 cpl_apertures * aperts;
3147 cpl_size npixobj;
3148 double objradius;
3149 cpl_size winsize;
3150 cpl_size xposmax, yposmax;
3151 double xposcen, yposcen;
3152 double valmax, valfit = -1.0;
3153 cpl_array * gauss_parameters = NULL;
3154 cpl_errorstate prestate = cpl_errorstate_get();
3155 cpl_error_code code = CPL_ERROR_NONE;
3156
3157
3158 cpl_ensure_code( sigma > 0.0, CPL_ERROR_ILLEGAL_INPUT);
3159
3160 selection = cpl_mask_new(nx, ny);
3161
3162 /* find aperture with signal larger than sigma * median deviation */
3163 for (; iretry > 0 && nlabels == 0; iretry--, sigma *= 0.5) {
3164
3165 /* Compute the threshold */
3166 const double threshold = median + sigma * med_dist;
3167
3168 /* Select the pixel above the threshold */
3169 code = cpl_mask_threshold_image(selection, self, threshold, DBL_MAX,
3170 BAD_PIX);
3171
3172 if (code) break;
3173
3174 /* Labelise the thresholded selection */
3175 cpl_image_delete(labels);
3176 labels = cpl_image_labelise_mask_create(selection, &nlabels);
3177 }
3178 sigma *= 2.0; /* reverse last iteration that found no labels */
3179
3180 cpl_mask_delete(selection);
3181
3182 if (code) {
3183 cpl_image_delete(labels);
3184 return cpl_error_set_where(cpl_func);
3185 } else if (nlabels == 0) {
3186 cpl_image_delete(labels);
3187 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
3188 }
3189
3190 aperts = cpl_apertures_new_from_image(self, labels);
3191
3192 /* Find the aperture with the greatest flux */
3193 code = eris_apertures_find_max_flux(aperts, &ifluxapert, 1);
3194
3195 if (code) {
3196 cpl_apertures_delete(aperts);
3197 cpl_image_delete(labels);
3198 return cpl_error_set(cpl_func, CPL_ERROR_DATA_NOT_FOUND);
3199 }
3200
3201 npixobj = cpl_apertures_get_npix(aperts, ifluxapert);
3202 objradius = sqrt((double)npixobj * CPL_MATH_1_PI);
3203 winsize = CX_MIN(CX_MIN(nx, ny), (3.0 * objradius));
3204
3205 xposmax = cpl_apertures_get_maxpos_x(aperts, ifluxapert);
3206 yposmax = cpl_apertures_get_maxpos_y(aperts, ifluxapert);
3207 xposcen = cpl_apertures_get_centroid_x(aperts, ifluxapert);
3208 yposcen = cpl_apertures_get_centroid_y(aperts, ifluxapert);
3209 valmax = cpl_apertures_get_max(aperts, ifluxapert);
3210
3211 cpl_apertures_delete(aperts);
3212 cpl_image_delete(labels);
3213
3214 cpl_msg_debug(cpl_func, "Object radius at S/R=%g: %g (window-size=%u)",
3215 sigma, objradius, (unsigned)winsize);
3216 cpl_msg_debug(cpl_func, "Object-peak @ (%d, %d) = %g", (int)xposmax,
3217 (int)yposmax, valmax);
3218
3219 /* fit gaussian to get subpixel peak position */
3220
3221 gauss_parameters = cpl_array_new(7, CPL_TYPE_DOUBLE);
3222 cpl_array_set_double(gauss_parameters, 0, median);
3223
3224 code = cpl_fit_image_gaussian(self, NULL, xposmax, yposmax,
3225 winsize, winsize, gauss_parameters,
3226 NULL, NULL, NULL,
3227 NULL, NULL, NULL,
3228 NULL, NULL, NULL);
3229 if (!code) {
3230 const double M_x = cpl_array_get_double(gauss_parameters, 3, NULL);
3231 const double M_y = cpl_array_get_double(gauss_parameters, 4, NULL);
3232 const double SIGMA_x = cpl_array_get_double(gauss_parameters, 5, NULL);
3233 const double SIGMA_y = cpl_array_get_double(gauss_parameters, 6, NULL);
3234 valfit = cpl_gaussian_eval_2d(gauss_parameters, M_x, M_y);
3235 if (!cpl_errorstate_is_equal(prestate)) {
3236 code = cpl_error_get_code();
3237 } else {
3238 *pxpos = M_x;
3239 *pypos = M_y;
3240 *ppeak = valfit;
3241 *sigma_x = SIGMA_x;
3242 *sigma_y = SIGMA_y;
3243
3244 cpl_msg_debug(cpl_func, "Gauss-fit @ (%g, %g) = %g, SIGMA: (%g, %g)",
3245 M_x, M_y, valfit,
3246 SIGMA_x,
3247 SIGMA_y);
3248
3249 }
3250 }
3251 cpl_array_delete(gauss_parameters);
3252
3253 if (code || valfit < valmax) {
3254 cpl_errorstate_set(prestate);
3255 *pxpos = xposcen;
3256 *pypos = yposcen;
3257 *ppeak = valmax;
3258 }
3259 eris_check_error_code("eris_gaussian_maxpos");
3260 return code ? cpl_error_set_where(cpl_func) : CPL_ERROR_NONE;
3261}
3262
3263/*----------------------------------------------------------------------------*/
3271/*----------------------------------------------------------------------------*/
3272
3273cpl_propertylist*
3274eris_compute_psf_qc(hdrl_image* hima, const cpl_parameterlist* parlist,
3275 const char* context){
3276
3277 cpl_ensure(hima != NULL, CPL_ERROR_NULL_INPUT, NULL);
3278 cpl_ensure(parlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
3279 cpl_ensure(context != NULL, CPL_ERROR_NULL_INPUT, NULL);
3280
3281 //cpl_errorstate clean_state = cpl_errorstate_get();
3282 cpl_size ima_szx = hdrl_image_get_size_x(hima);
3283 cpl_size ima_szy = hdrl_image_get_size_y(hima);
3284
3285 double peak = 0;
3286 double max_x = 0;
3287 double max_y = 0;
3288 double sigma_x = 0;
3289 double sigma_y = 0;
3290 double FWHM_x = 0;
3291 double FWHM_y = 0;
3292 cpl_image* image = hdrl_image_get_image(hima);
3293 cpl_image* error = hdrl_image_get_image(hima);
3294
3295 cpl_propertylist* qchead = cpl_propertylist_new();
3296 eris_gaussian_maxpos(image, 5, &max_x, &max_y, &peak, &sigma_x, &sigma_y);
3297 FWHM_x = sigma_x * CPL_MATH_FWHM_SIG;
3298 FWHM_y = sigma_y * CPL_MATH_FWHM_SIG;
3299 cpl_msg_info(cpl_func, "max_x: %g max_y: %g FWHM_x:%g FWHM_y: %g",
3300 max_x, max_y, FWHM_x, FWHM_y);
3301
3302 if(cpl_error_get_code() != CPL_ERROR_NONE) {
3303 cpl_msg_error(cpl_func,"Error fitting 2D Gaussian to object for QC. Exit");
3304 cpl_error_reset();
3305 return qchead;
3306 }
3307
3308 int max_ima_x = (int) (max_x + 0.5);
3309 int max_ima_y = (int) (max_y + 0.5);
3310
3311 int psf_sz = 40;
3312 int wllx = ((max_ima_x - psf_sz)>0) ? (max_ima_x - psf_sz) : 1;
3313 int wlly = ((max_ima_y - psf_sz)>0) ? (max_ima_y - psf_sz) : 1;
3314 int wurx = ((max_ima_x + psf_sz)<ima_szx) ? (max_ima_x + psf_sz) : ima_szx ;
3315 int wury = ((max_ima_y + psf_sz)<ima_szy) ? (max_ima_y + psf_sz) : ima_szy ;
3316
3317 cpl_table* qclog_tbl = eris_qclog_init();
3318
3319 /* not used code:
3320 double max_ima_cx = 0;
3321 max_ima_cx = cpl_image_get_centroid_x_window(image, wllx, wlly, wurx, wury);
3322 double max_ima_cy = 0;
3323 max_ima_cy = cpl_image_get_centroid_y_window(image, wllx, wlly, wurx, wury);
3324 */
3325
3326 int llx = 8;
3327 int lly = 8;
3328 int halfbox_x = 16;
3329 int halfbox_y = 16;
3330 eris_qclog_add_int(qclog_tbl,"QC FWHM LLX", llx, "[pix] STD star FWHM LLX");
3331 eris_qclog_add_int(qclog_tbl,"QC FWHM LLY", lly, "[pix] STD star FWHM LLY");
3332 eris_qclog_add_int(qclog_tbl,"QC FWHM HBX", halfbox_x, "[pix] STD star FWHM HBX");
3333 eris_qclog_add_int(qclog_tbl,"QC FWHM HBX", halfbox_y, "[pix] STD star FWHM HBY");
3334
3335
3336 int check1 = 0;
3337 double* pdata = cpl_image_get_data_double(image);
3338 cpl_size npix = cpl_image_get_size_x(image) * cpl_image_get_size_y(image);
3339 cpl_boolean has_nan = CPL_FALSE;
3340 for(cpl_size i =0; i < npix; i++ ) {
3341 if(eris_ifu_is_nan_or_inf(pdata[i])) {
3342 has_nan = CPL_TRUE;
3343 break;
3344 }
3345 }
3346
3347 cpl_bivector * biv = NULL;
3348 if(has_nan == CPL_FALSE) {
3349 cpl_image* fimage = cpl_image_cast(image, CPL_TYPE_FLOAT);
3350 //cpl_image_reject_from_mask(image, mask);
3351 biv = cpl_image_iqe(fimage, wllx, wlly, wurx, wury);
3352 cpl_image_delete(fimage);
3353 }
3354
3355 if( (cpl_error_get_code() == CPL_ERROR_NONE) && (has_nan == CPL_FALSE)) {
3356 const double * x = cpl_bivector_get_x_data_const(biv);
3357 eris_qclog_add_double(qclog_tbl, "QC CENTERX", x[0],
3358 "[pix] STD star X centroid position");
3359 eris_qclog_add_double(qclog_tbl, "QC CENTERY", x[1],
3360 "[pix] STD star Y centroid position");
3361 eris_qclog_add_double(qclog_tbl, "QC FWHM MAJ", x[2],
3362 "[pix] STD star FWHM on major axis");
3363 eris_qclog_add_double(qclog_tbl, "QC FWHM MIN", x[3],
3364 "[pix] STD star FWHM on minor axis");
3365 eris_qclog_add_double(qclog_tbl, "QC THETA", x[4],
3366 "[deg] STD star ellipsis angle theta");
3367 cpl_bivector_delete(biv);
3368 } else {
3369 eris_qclog_add_double(qclog_tbl, "QC CENTERX", max_x,
3370 "[pix] STD star X centroid position");
3371 eris_qclog_add_double(qclog_tbl, "QC CENTERY", max_y,
3372 "[pix] STD star Y centroid position");
3373 eris_qclog_add_double(qclog_tbl, "QC FWHM MAJ", FWHM_x,
3374 "[pix] STD star FWHM on major axis");
3375 eris_qclog_add_double(qclog_tbl, "QC FWHM MIN", FWHM_y,
3376 "[pix] STD star FWHM on minor axis");
3377
3378 cpl_error_reset();
3379 }
3380
3381 double xcen = max_x;
3382 double ycen = max_y;
3383 if (has_nan == CPL_FALSE) {
3384// double norm = 0;
3385
3386// double sig_x = 0;
3387// double sig_y = 0;
3388 double fwhm_x = 0;
3389 double fwhm_y = 0;
3390 //double disp = 0;
3391
3392 double fwhm_factor = 5.0;
3393
3394 char* pname = cpl_sprintf("%s.fwhm-factor",context);
3395 eris_parameters_get_double(parlist, pname, &fwhm_factor);
3396 cpl_free(pname);
3397 double rms = 0;
3398 double red_chisq = 0;
3399 cpl_matrix* covariance = NULL;
3400 double major = 0;
3401 double minor = 0;
3402 double angle = 0;
3403 cpl_array* fit_params = NULL;
3404 cpl_array* gauss2d_fit_params = cpl_array_new(7, CPL_TYPE_DOUBLE);
3405 cpl_array* gauss2d_err_params = cpl_array_new(7, CPL_TYPE_DOUBLE);
3406 cpl_matrix* phys_cov = NULL;
3407 int status = 0;
3408
3409 if(CPL_ERROR_NONE == cpl_fit_image_gaussian(image, error,
3410 max_ima_x, max_ima_y, psf_sz, psf_sz, gauss2d_fit_params,
3411 gauss2d_err_params, fit_params, &rms, &red_chisq, &covariance,
3412 &major, &minor, &angle, &phys_cov) ) {
3413 sigma_x = cpl_array_get(gauss2d_fit_params, 5, &status);
3414 sigma_y = cpl_array_get(gauss2d_fit_params, 6, &status);
3415
3416 fwhm_x = sigma_x * CPL_MATH_FWHM_SIG;
3417 fwhm_y = sigma_y * CPL_MATH_FWHM_SIG;
3418 eris_qclog_add_double_f(qclog_tbl, "QC FWHMX", fwhm_x,
3419 "[pix] STD star FWHM on X");
3420 eris_qclog_add_double_f(qclog_tbl, "QC FWHMY", fwhm_y,
3421 "[pix] STD star FWHM on Y");
3422
3423 halfbox_x = (floor)(0.5 * (fwhm_x + fwhm_y) * fwhm_factor + 0.5);
3424
3425 halfbox_y = (floor)(0.5 * (fwhm_x + fwhm_y) * fwhm_factor + 0.5);
3426
3427 } else {
3428 //TODO remove irplib
3429 //irplib_error_recover(clean_state, "Problem fitting Gaussian");
3430 cpl_array_delete(gauss2d_fit_params);
3431 cpl_array_delete(gauss2d_err_params);
3432 cpl_error_reset();
3433
3434 }
3435 } else {
3436
3437 eris_qclog_add_double_f(qclog_tbl, "QC FWHMX", FWHM_x,
3438 "[pix] STD star FWHM on X");
3439 eris_qclog_add_double_f(qclog_tbl, "QC FWHMY", FWHM_y,
3440 "[pix] STD star FWHM on Y");
3441 }
3442
3443 /* we use a large value of kappa to be sure to flag well the object */
3444 //cpl_mask* obj_mask=NULL;
3445 //int pfx = 3;
3446 //int pfy = 3;
3447 //const char* pfm;
3448 //TODO: maybe use the mask defined elsewhere for extraction
3449 //if(strcmp(extract_method, "optimal") != 0) {
3450 // double kappa = 5.;
3451 // obj_mask = eris_object_mask(image, pfm, pfx, pfy, kappa);
3452 //}
3453
3454
3455 llx = (int)(xcen - halfbox_x);
3456 llx = (llx > 0 ) ? llx : 1;
3457
3458 if((llx + 2 * halfbox_x) >= ima_szx) {
3459 halfbox_x = (int) ((ima_szx - llx - 1) / 2);
3460 check1++;
3461 }
3462
3463 lly = (int)(ycen - halfbox_y);
3464 lly = (lly > 0 ) ? lly : 1;
3465 if((lly + 2 * halfbox_y) >= ima_szy) {
3466 halfbox_y = (int) ((ima_szy - lly - 1) / 2);
3467 check1++;
3468 }
3469 eris_qclog_add_int(qclog_tbl,"QC CHECK1", check1,
3470 "Check on evaluation box");
3471 //cpl_table_save(qclog_tbl,NULL,NULL,"qclog_tbl.fits",CPL_IO_DEFAULT);
3472 eris_pfits_put_qc(qchead, qclog_tbl);
3473 cpl_table_delete(qclog_tbl);
3474 eris_check_error_code("eris_compute_psf_qc");
3475 return qchead;
3476
3477}
3478
3479static cpl_error_code
3480eris_imagelist_reject_value(hdrl_imagelist* iml/*, cpl_value value*/)
3481{
3482
3483 cpl_ensure(iml != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
3484
3485 cpl_size sz = hdrl_imagelist_get_size(iml);
3486
3487 for(cpl_size i = 0; i < sz; i++) {
3488 hdrl_image* hima = hdrl_imagelist_get(iml, i);
3489 hdrl_image_reject_value(hima, CPL_VALUE_NAN);
3490 }
3491 return cpl_error_get_code();
3492}
3493/*----------------------------------------------------------------------------*/
3501/*----------------------------------------------------------------------------*/
3502cpl_mask*
3503eris_ifu_hima_get_obj_mask_percent(hdrl_image* hima, const double percent)
3504{
3505 cpl_mask* mask_obj = NULL;
3506
3507 cpl_size sx = hdrl_image_get_size_x(hima);
3508 cpl_size sy = hdrl_image_get_size_y(hima);
3509
3510 cpl_image* image = hdrl_image_get_image(hima);
3511 cpl_image* ima_tmp = cpl_image_duplicate(image);
3512
3513
3514 cpl_ensure(hima != NULL, CPL_ERROR_NULL_INPUT, NULL);
3515 cpl_ensure(0.25 <= percent && percent <= 0.75, CPL_ERROR_ILLEGAL_INPUT, NULL);
3516 double* data = cpl_image_get_data(ima_tmp);
3517
3518 cpl_size npoints = sx * sy;
3519 cpl_ensure(npoints > 0, CPL_ERROR_ILLEGAL_INPUT, NULL);
3520 cpl_vector* v = cpl_vector_new(npoints);
3521 v = cpl_vector_wrap(npoints, data);
3522
3523 cpl_vector_sort(v, CPL_SORT_ASCENDING);
3524
3525 cpl_size thresh_pos = percent * npoints;
3526 double threshold_max = cpl_vector_get(v, thresh_pos);
3527 //cpl_msg_info(cpl_func,"threshold_max: %g",threshold_max);
3528 cpl_vector_unwrap(v);
3529 cpl_image_delete(ima_tmp);
3530 mask_obj = cpl_mask_threshold_image_create(image, DBL_MIN, threshold_max);
3531 cpl_mask_not(mask_obj);
3532 if (v != NULL) {
3533 cpl_vector_delete(v);
3534 }
3535 return mask_obj;
3536}
3537/*----------------------------------------------------------------------------*/
3546/*----------------------------------------------------------------------------*/
3547cpl_mask*
3548eris_ifu_hima_get_obj_mask(hdrl_image* hima, const cpl_size niter,
3549 const double kappa)
3550{
3551
3552 cpl_mask* mask_tmp = NULL;
3553 cpl_mask* mask_obj = NULL;
3554 hdrl_value median = {0, 0};
3555 double max = 0;
3556 double stdev = 0;
3557
3558 cpl_ensure(hima != NULL, CPL_ERROR_NULL_INPUT, NULL);
3559 cpl_ensure(1 <= niter && niter <= 100, CPL_ERROR_ILLEGAL_INPUT, NULL);
3560 cpl_ensure(kappa > 0., CPL_ERROR_ILLEGAL_INPUT, NULL);
3561
3562 max = cpl_image_get_max(hdrl_image_get_image(hima));
3563
3564 for (cpl_size i = 0; i < niter; i++) {
3565 median = hdrl_image_get_median(hima);
3566 if (!isnan(median.data)) {
3567 stdev = hdrl_image_get_stdev(hima);
3568 double thresh_max = median.data + kappa * stdev;
3569 cpl_msg_info(cpl_func, "median: %g stdev: %g thresh: %g max: %g",
3570 median.data, stdev, thresh_max, max);
3571 thresh_max = (thresh_max < max) ? thresh_max : 0.99 * max;
3572 cpl_msg_info(cpl_func, "median: %g stdev: %g thresh: %g max: %g",
3573 median.data, stdev, thresh_max, max);
3574 if (stdev > 0) {
3575
3576 cpl_image* ima_tmp = hdrl_image_get_image(hima);
3577
3578 mask_tmp = cpl_mask_threshold_image_create(ima_tmp, DBL_MIN, thresh_max);
3579 cpl_mask_not(mask_tmp);
3580
3581 hdrl_image_reject_from_mask(hima, mask_tmp);
3582
3583 if (i == (niter -1)) {
3584 mask_obj = cpl_mask_duplicate(mask_tmp);
3585 }
3586 cpl_mask_delete(mask_tmp);
3587 }
3588 }
3589 }
3590
3591 eris_check_error_code("eris_ifu_hima_get_obj_mask");
3592 return mask_obj;
3593}
3594
3602cpl_error_code
3603eris_get_pupil_shift(hdrl_imagelist* iml, const int n, cpl_table** qclog_tbl)
3604{
3605
3606
3607 cpl_ensure(iml != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
3608 cpl_ensure(qclog_tbl != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
3609 cpl_ensure(*qclog_tbl != NULL, CPL_ERROR_NULL_INPUT, CPL_ERROR_NULL_INPUT);
3610 cpl_ensure(n >= 0, CPL_ERROR_ILLEGAL_INPUT, CPL_ERROR_ILLEGAL_INPUT);
3611
3612 cpl_size max_ima_x = 0;
3613 cpl_size max_ima_y = 0;
3614 int nx = 0;
3615 int ny = 0;
3616
3617 double xshift = 0;
3618 double yshift = 0;
3619
3620 double max_ima_cx = 0;
3621 double max_ima_cy = 0;
3622
3623 hdrl_image* himg = NULL;
3624 cpl_image* img_dup = NULL;
3625 cpl_image* contrib_map = NULL;
3626 char* key_name;
3627
3628 eris_imagelist_reject_value(iml/*, CPL_VALUE_NAN*/);
3629
3630 hdrl_imagelist_collapse_median(iml, &himg, &contrib_map);
3631 nx = hdrl_image_get_size_x(himg);
3632 ny = hdrl_image_get_size_y(himg);
3633
3634 img_dup = hdrl_image_get_image(himg);
3635
3636 cpl_image_get_maxpos(img_dup, &max_ima_x, &max_ima_y);
3637 max_ima_cx = cpl_image_get_centroid_x_window(img_dup, 1, 1, nx, ny);
3638 max_ima_cy = cpl_image_get_centroid_y_window(img_dup, 1, 1, nx, ny);
3639 //cpl_image_delete(img_dup);
3640
3641
3642 xshift = max_ima_cx - (double)nx/2;
3643 yshift = max_ima_cy - (double)ny/2;
3644
3645 key_name = cpl_sprintf("QC PUPIL%d SHIFTX", n);
3646
3647 eris_qclog_add_double_f(*qclog_tbl, key_name, xshift,
3648 "X shift centroid - center image");
3649 cpl_free(key_name);
3650 key_name = cpl_sprintf("QC PUPIL%d SHIFTY", n);
3651 eris_qclog_add_double_f(*qclog_tbl, key_name, yshift,
3652 "Y shift centroid - center image");
3653 cpl_msg_info(cpl_func,"xshift: %g yshift: %g",xshift, yshift);
3654 cpl_free(key_name);
3655 hdrl_image_delete(himg);
3656 cpl_image_delete(contrib_map);
3657 eris_check_error_code("eris_get_pupil_shift");
3658 return cpl_error_get_code();
3659}
3660
3670cpl_mask* eris_ifu_mask_from_image(const cpl_image* img_mask)
3671{
3672 cpl_size nx = 0,
3673 ny = 0;
3674 cpl_mask *mask = NULL;
3675 const double *pimg_mask = NULL;
3676 cpl_binary *pmask = NULL;
3677
3678 cpl_ensure(img_mask != NULL, CPL_ERROR_NULL_INPUT, NULL);
3679
3680 TRY {
3681 nx = cpl_image_get_size_x(img_mask);
3682 ny = cpl_image_get_size_y(img_mask);
3683
3685 mask = cpl_mask_new(nx,ny));
3687 pimg_mask = cpl_image_get_data_double_const(img_mask));
3689 pmask = cpl_mask_get_data(mask));
3690
3691 for (cpl_size x = 0; x < nx; x++) {
3692 for (cpl_size y = 0; y < ny; y++) {
3693 if (pimg_mask[x+nx*y] > 0.5) {
3694 // pimg_mask = 1 -> CPL_BINARY_0
3695 pmask[x+nx*y] = GOOD_PIX;
3696 } else {
3697 // pimg_mask = 0 -> CPL_BINARY_1
3698 pmask[x+nx*y] = BAD_PIX;
3699 }
3700 }
3701 }
3702
3703
3704 } CATCH {
3705 CATCH_MSG();
3706 eris_ifu_free_mask(&mask);
3707 }
3708
3709 return mask;
3710}
3711
cpl_error_code eris_ifu_append_qc_double(cpl_propertylist *pl, const char *name, double val, const char *comment)
Append a QC parameter of type DOUBLE to a property list.
cpl_error_code eris_ifu_append_qc_int(cpl_propertylist *pl, const char *name, int val, const char *comment)
Append a QC parameter of type INT to a property list.
#define ASSURE(condition, error,...)
error handling macro (from fors-pipeline)
#define SET_ERROR_MSG(code, msg)
Set a new error code together with a custom error message.
#define CHECK_ERROR_STATE(void)
Check the CPL error state, and exit the try-block if not CPL_ERROR_NONE.
#define BRK_WITH_ERROR_MSG(code,...)
Set a new CPL error, and exit the try-block.
#define CATCH_MSG()
Displays an error message.
#define TRY
Beginning of a TRY-block.
#define CATCH
End of a TRY-block, beginning of a CATCH-block.
#define BRK_IF_NULL(function)
If function is or returns a NULL pointer, then the try-block is exited.
#define CATCH_MSGS()
Displays an error message stack.
hdrl_image * eris_ifu_load_exposure_file(const char *filename, int exposureCorrectionMode, cpl_image *dqi)
Load a raw detector exposure from file with corrections and noise.
void eris_ifu_free_frameset(cpl_frameset **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_save_table_dbg(const cpl_table *tbl, const char *filename, int create)
Save table for debugging (quick, no DFS overhead)
cpl_error_code eris_ifu_save_cpl_image_dbg(const cpl_image *img, const char *name, int singlefile, const cpl_propertylist *pl)
Save CPL image with mask for debugging.
cpl_error_code eris_ifu_save_imagelist(cpl_frameset *fs, cpl_frame *inherit, const cpl_propertylist *plist, const cpl_parameterlist *parlist, const char *recipe, const char *procatg, const char *filename, cpl_type type, const cpl_imagelist *imglist)
Save imagelist with DFS compliance.
cpl_error_code eris_ifu_save_imagelist_dbg(const cpl_imagelist *imglist, const char *filename, int create)
Save CPL imagelist for debugging (quick, no DFS overhead)
void eris_ifu_free_propertylist(cpl_propertylist **item)
Free memory and set pointer to null.
cpl_vector * eris_ifu_idl_values_at_indices(const cpl_vector *data, const cpl_vector *indices)
Returns a vector of given indices.
void eris_ifu_free_apertures(cpl_apertures **item)
Free memory and set pointer to null.
void eris_ifu_free_string(char **item)
Free memory and set pointer to null.
char * eris_ifu_get_lampString(int lampStatus)
Convert lamp status bitmask to string.
cpl_error_code eris_ifu_parameterlist_append_list(cpl_parameterlist *p1, const cpl_parameterlist *p2)
Append a parameterlist to another one.
void eris_ifu_free_double_array(double **item)
Free memory and set pointer to null.
cpl_table * eris_qclog_init(void)
Initialize QC table.
void eris_ifu_free_vector(cpl_vector **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_save_vector_dbg(const cpl_vector *vec, const char *filename, int create, const cpl_propertylist *pl)
Save vector for debugging (quick, no DFS overhead)
cpl_error_code eris_ifu_save_image_dbg(const cpl_image *img, const char *filename, int create, const cpl_propertylist *pl)
Save image for debugging (quick, no DFS overhead)
void eris_ifu_free_table(cpl_table **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_save_ifu_vector_dbg(const eris_ifu_vector *vec, const char *filename, int create)
Save eris_ifu_vector for debugging (quick, no DFS overhead)
cpl_error_code eris_ifu_cut_endings(cpl_vector **vec, int *begin, int *end, int cut)
Cut leading and trailing -1 of a vector.
cpl_error_code eris_ifu_mask_nans_in_hdrlimage(hdrl_image **hima)
Flag NaNs in HDRL image.
const char * eris_ifu_get_bandString(ifsBand band)
Convert band enum to string.
void eris_ifu_free_ifu_vector(eris_ifu_vector **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_split_hdrl_imagelist(hdrl_imagelist *cube, cpl_imagelist *dataCube, cpl_imagelist *errorCube)
extract from hdrl_imagelist the data and error information
cpl_error_code eris_get_pupil_shift(hdrl_imagelist *iml, const int n, cpl_table **qclog_tbl)
Get what object shift.
cpl_mask * eris_ifu_load_badpixel_mask(const cpl_frameset *frameset, const char *category, int ext)
‍**
cpl_error_code eris_ifu_hdrl_image_reject_mask(hdrl_image *img, const cpl_mask *mask)
Add mask to HDRL image (reject masked pixels)
cpl_error_code eris_ifu_file_exists(const char *filename)
‍**
cpl_error_code eris_ifu_get_plane_cut_min_max(ifsBand band, cpl_size *zmin, cpl_size *zmax)
Get band-specific plane indices for NaN trimming.
const char * eris_ifu_get_instrumentString(ifsInstrument instrument)
Convert instrument enum to string.
cpl_error_code eris_pfits_put_qc(cpl_propertylist *plist, cpl_table *qclog)
convert table with QC parameter information to a propertylist
cpl_error_code eris_ifu_get_badpix_qc_from_ima(const cpl_image *image, cpl_propertylist *qc_list, const char *prefix)
compute QC keyword with number of bad pixels and fraction to total
void eris_ifu_free_polynomial(cpl_polynomial **item)
Free memory and set pointer to null.
hdrl_imagelist * eris_ifu_get_hdrlimagelist_by_tag(cpl_frameset *frameset, const char *tag, int exposureCorrectionMode)
Return HDRL imagelist by searching tagged frames in frameset.
void eris_ifu_free_parameter(cpl_parameter **item)
Free memory and set pointer to null.
cpl_propertylist * eris_compute_psf_qc(hdrl_image *hima, const cpl_parameterlist *parlist, const char *context)
compute QC parameters on a PSF STD image
cpl_error_code eris_ifu_save_bivector_dbg(const cpl_bivector *bivec, const char *filename, int col, int create)
Save bivector for debugging (as vector or table)
cpl_imagelist * eris_ifu_hdrl_get_imagelist(const hdrl_imagelist *hdrl_imglist, enum hdrl_t type)
Extract from an HDRL imagelist a specific CPL imagelist.
cpl_mask * eris_ifu_hima_get_obj_mask_percent(hdrl_image *hima, const double percent)
find mask flagging pixels above percent(%) pixels with lower intensity
cpl_mask * eris_ifu_mask_create_border(const cpl_mask *mask)
Add bad pixel border around mask.
cpl_image * eris_ifu_image_create_window(const cpl_image *img)
Create sub-image by removing bad pixel border.
cpl_error_code eris_qclog_add_double_f(cpl_table *table, const char *key_name, const double value, const char *key_help)
add QC float info to table
void eris_ifu_free_int_array(int **item)
Free memory and set pointer to null.
cpl_error_code eris_qclog_add_string(cpl_table *table, const char *key_name, const char *value, const char *key_help)
add QC string info to table
cpl_error_code eris_qclog_add_double(cpl_table *table, const char *key_name, const double value, const char *key_help)
add QC double info to table
cpl_error_code eris_ifu_save_mask_dbg(const cpl_mask *mask, const char *filename, int create, const cpl_propertylist *pl)
Save mask for debugging (quick, no DFS overhead)
void eris_ifu_free_imagelist(cpl_imagelist **item)
Free memory and set pointer to null.
cpl_vector * eris_ifu_idl_where(const cpl_vector *data, double val, int op)
Implements the where-function known from IDL.
cpl_mask * eris_ifu_quality2bp_mask(const cpl_image *qualityMask, deqQualityType qualityType)
transform input image (quality mask) to output mask with given bit code
cpl_error_code eris_ifu_cube_set_values(hdrl_imagelist **iml)
Set specific pixel values in HDRL imagelist (debugging utility)
void eris_ifu_free_frame(cpl_frame **item)
Free memory and set pointer to null.
cpl_error_code eris_qclog_add_bool(cpl_table *table, const char *key_name, const char *key_help)
add QC boolean info to table
cpl_error_code eris_ifu_mask_nans_in_cube(cpl_imagelist *cube)
Mask NaNs and Infs in CPL imagelist (cube)
void eris_ifu_free_hdrl_imagelist(hdrl_imagelist **item)
Free memory and set pointer to null.
cpl_error_code eris_qclog_add_int(cpl_table *table, const char *key_name, const int value, const char *key_help)
add QC int info to table
cpl_error_code eris_ifu_save_hdrl_imagelist_dbg(const hdrl_imagelist *hdrl_img_list, const char *filename, int singlefile)
Save HDRL imagelist for debugging (data + error + mask planes)
cpl_error_code eris_ifu_save_cpl_imagelist_dbg(const cpl_imagelist *imglist, const char *name)
Save CPL imagelist for debugging.
cpl_error_code eris_ifu_save_hdrl_image_dbg(const hdrl_image *hdrl_img, const char *filename, int singlefile, const cpl_propertylist *pl)
Save HDRL image for debugging (data + error + mask)
cpl_mask * eris_ifu_mask_from_image(const cpl_image *img_mask)
eris_ifu_mask_from_image
void eris_ifu_free_float_array(float **item)
Free memory and set pointer to null.
void eris_ifu_free_hdrl_image(hdrl_image **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_cube_trim_nans(const cpl_size zmin, const cpl_size zmax, hdrl_imagelist **iml, cpl_imagelist **bpm, cpl_propertylist *header)
Trim NaN-dominated planes from cube edges.
cpl_imagelist * eris_ifu_interpolatedMask_to_maskZero(cpl_imagelist *bpmCube, double threshold)
return cube flagging (as 1) pixels above a given threshold
cpl_error_code eris_ifu_get_badpix_qc_from_mask(const cpl_mask *bp_mask, cpl_propertylist *qc_list, const char *prefix)
compute QC keyword with number of bad pixels and fraction to total
void eris_ifu_free_image(cpl_image **item)
Free memory and set pointer to null.
cpl_error_code eris_qclog_add_double_format(cpl_table *table, const char *key_name, const double value, const char *key_help)
add QC double (with format) info to table
void eris_ifu_free_matrix(cpl_matrix **item)
Free memory and set pointer to null.
void eris_ifu_free_mask(cpl_mask **item)
Free memory and set pointer to null.
cpl_frameset * eris_ifu_get_frameset_by_tag(const cpl_frameset *frameset, const char *tag)
Get frames with given tag from frameset.
const char * eris_ifu_get_preopticsScaleString(ifsPreopticsScale scale)
Convert pre-optics scale enum to string.
void eris_ifu_free_parameterlist(cpl_parameterlist **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_split3_hdrl_imagelist(hdrl_imagelist *cube, cpl_imagelist *dataCube, cpl_imagelist *errorCube, cpl_imagelist *qualCube)
extract from hdrl_imagelist the data and error information
cpl_error_code eris_ifu_save_image(cpl_frameset *fs, const cpl_propertylist *plist, const cpl_parameterlist *parlist, const char *recipe, const char *procatg, const char *filename, cpl_type type, const cpl_image *image)
Save image with DFS compliance.
double eris_ifu_get_band_resolution(ifsBand band)
Get nominal spectral resolution for a given band.
void eris_ifu_free_bivector(cpl_bivector **item)
Free memory and set pointer to null.
void eris_ifu_free_hdrl_parameter(hdrl_parameter **item)
Free memory and set pointer to null.
cpl_mask * eris_ifu_hima_get_obj_mask(hdrl_image *hima, const cpl_size niter, const double kappa)
find mask flagging pixels above a threshold set by ks-clip algorithm
cpl_error_code eris_ifu_vector_save(const eris_ifu_vector *ev, const char *filename, cpl_type_bpp bpp, const cpl_propertylist *pl, unsigned mode, double rej_val)
Override for cpl_vector_save().
int eris_ifu_is_nan_or_inf(double A)
Checks if a value is nan, inf or -inf.
void eris_ifu_vector_delete(eris_ifu_vector *ev)
Delete a eris_ifu_vector.
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_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
cpl_error_code hdrl_image_reject_value(hdrl_image *self, cpl_value mode)
Reject pixels with the specified special value(s)
Definition: hdrl_image.c:477
cpl_error_code hdrl_image_reject_from_mask(hdrl_image *self, const cpl_mask *map)
set bpm of hdrl_image
Definition: hdrl_image.c:407
hdrl_value hdrl_image_get_median(const hdrl_image *self)
computes the median and associated error of an image.
double hdrl_image_get_stdev(const hdrl_image *self)
computes the standard deviation of the data of an image
cpl_mask * hdrl_image_get_mask(hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:157
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:131
cpl_size hdrl_image_get_size_y(const hdrl_image *self)
return size of Y dimension of image
Definition: hdrl_image.c:540
const cpl_mask * hdrl_image_get_mask_const(const hdrl_image *himg)
get cpl bad pixel mask from image
Definition: hdrl_image.c:175
cpl_size hdrl_image_get_size_x(const hdrl_image *self)
return size of X dimension of image
Definition: hdrl_image.c:525
const cpl_image * hdrl_image_get_error_const(const hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:144
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:118
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.
const hdrl_image * hdrl_imagelist_get_const(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
hdrl_imagelist * hdrl_imagelist_new(void)
Create an empty imagelist.
cpl_error_code hdrl_imagelist_collapse_median(const hdrl_imagelist *himlist, hdrl_image **out, cpl_image **contrib)
Median collapsing of image list.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.
void hdrl_parameter_delete(hdrl_parameter *obj)
shallow delete of a parameter