ERIS Pipeline Reference Manual 1.8.15
eris_ifu_dfs.c
1/* $Id: eris_dfs.c,v 1.9 2013-03-26 17:00:44 jtaylor 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: jtaylor $
23 * $Date: 2013-03-26 17:00:44 $
24 * $Revision: 1.9 $
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 "eris_ifu_dfs.h"
37#include "eris_ifu_utils.h"
38#include "eris_ifu_functions.h"
39#include "eris_ifu_error.h"
40#include "eris_ifu_distortion_static.h"
41#include "eris_ifu_debug.h"
42#include "eris_utils.h"
43#include <eris_pfits.h>
44#include <string.h>
45
46/*----------------------------------------------------------------------------*/
69/*----------------------------------------------------------------------------*/
70
73/*----------------------------------------------------------------------------*/
88/*----------------------------------------------------------------------------*/
89cpl_error_code eris_ifu_dfs_set_groups(cpl_frameset * self)
90{
91 cpl_error_code retVal = CPL_ERROR_NONE;
92 const cpl_size n = cpl_frameset_get_size(self);
93 ifsInstrument instrument = UNSET_INSTRUMENT,
94 currInstrument = UNSET_INSTRUMENT;
95 cpl_frame *frame = NULL; /* Not allocated, just modified */
96 const char *tag = NULL;
97 char *stag = NULL;
98
99 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
100
101 TRY
102 {
103 /* Loop on frames */
104 for (cpl_size i = 0; i < n; i++) {
106 frame = cpl_frameset_get_position(self, i));
107
108 tag = cpl_frame_get_tag(frame);
109 if (tag == NULL) {
110 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
111 "Frame %d of %d has no tag",
112 1 + (int)i, (int)n);
113 }
114 cpl_free(stag);
116 stag = cpl_sprintf(",%s,", tag));
117
118 if (strstr(ERIS_IFU_RAW_TAGS, stag) != NULL) {
119 /* SPIFFIER RAW frames */
121 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW));
122
123 // check if all frames are from the same instrument
124
125 if (instrument == UNSET_INSTRUMENT) {
126 instrument = eris_ifu_get_instrument_frame(frame);
127 } else {
128 currInstrument = eris_ifu_get_instrument_frame(frame);
129 if (currInstrument != instrument) {
131 CPL_ERROR_BAD_FILE_FORMAT,
132 "SOF input files are from "
133 "different instruments");
134 }
135 }
136
137 } else if (strstr(ERIS_IFU_REF_CALIB_TAGS, stag) != NULL) {
138 /* SPIFFIER reference CALIB frames */
140 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB));
141 } else if (strstr(ERIS_IFU_REF_UTIL_TAGS, stag) != NULL) {
142 /* SPIFFIER utility frames */
144 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB));
145 } else if (strstr(ERIS_IFU_PRO_CALIB_TAGS, stag) != NULL) {
146 /* SPIFFIER product CALIB frames */
148 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB));
149 } else if (strstr(ERIS_IFU_PRO_JITTER_TAGS, stag) != NULL) {
150 /* SPIFFIER product Jitter recipe frames */
152 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT));
153
154 // check if all frames are from the same instrument
155 if (instrument == UNSET_INSTRUMENT) {
156 instrument = eris_ifu_get_instrument_frame(frame);
157 } else {
158 currInstrument = eris_ifu_get_instrument_frame(frame);
159 if (currInstrument != instrument) {
161 CPL_ERROR_BAD_FILE_FORMAT,
162 "SOF input files are from "
163 "different instruments");
164 }
165 }
166
167 } else {
168 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
169 "Unkown tag \"%s\" for frame %d of %d",
170 cpl_frame_get_tag(frame), 1 + (int)i, (int)n);
171 }
172
173 }
174 }
175 CATCH
176 {
177 retVal = cpl_error_get_code();
178 }
179
180 cpl_free(stag);
181 eris_check_error_code("eris_ifu_dfs_set_groups");
182 return retVal;
183}
184
185
186/*----------------------------------------------------------------------------*/
199/*----------------------------------------------------------------------------*/
200cpl_frameset *
202 const cpl_frameset * in,
203 const char * tag)
204{
205 cpl_frameset * out;
206 cpl_frame * loc_frame;
207 int nbframes;
208 int i ;
209
210 /* Test entries */
211 if (in == NULL) return NULL ;
212 if (tag == NULL) return NULL ;
213
214 /* Initialise */
215 nbframes = cpl_frameset_get_size(in) ;
216
217 /* Count the frames with the tag */
218 if (cpl_frameset_count_tags(in, tag) == 0) return NULL ;
219
220 /* Create the output frameset */
221 out = cpl_frameset_new() ;
222
223 /* Loop on the requested frames and store them in out */
224 for (i=0 ; i<nbframes ; i++) {
225 const cpl_frame *cur_frame = cpl_frameset_get_position_const(in, i);
226 if (!strcmp(cpl_frame_get_tag(cur_frame), tag)) {
227 loc_frame = cpl_frame_duplicate(cur_frame) ;
228 cpl_frameset_insert(out, loc_frame) ;
229 }
230 }
231 eris_check_error_code("eris_ifu_extract_frameset");
232 return out ;
233}
234
235/*----------------------------------------------------------------------------*/
247/*----------------------------------------------------------------------------*/
248cpl_error_code eris_ifu_heades_add_hduclass_common(cpl_propertylist* plist,
249 const char* deq_hduclas)
250{
251 cpl_propertylist_append_string(plist, FHDR_HDUCLASS, DEQ_HDUCLASS);
252 cpl_propertylist_append_string(plist, FHDR_HDUDOC, DEQ_HDUDOC);
253 cpl_propertylist_append_string(plist, FHDR_HDUVERS, DEQ_HDUVERS);
254 cpl_propertylist_append_string(plist, FHDR_HDUCLAS1, deq_hduclas);
255 eris_check_error_code("eris_ifu_heades_add_hduclass_common");
256 return cpl_error_get_code();
257}
258
259
260
261/*----------------------------------------------------------------------------*/
272/*----------------------------------------------------------------------------*/
273cpl_error_code eris_ifu_heades_add_hduclass_data(cpl_propertylist* plist)
274{
275 cpl_propertylist_append_string(plist, FHDR_HDUCLAS2, DEQ_HDUCLAS2_DATA);
276 cpl_propertylist_update_string(plist, FHDR_EXTNAME, DEQ_SCIDATA);
277 cpl_propertylist_append_string(plist, FHDR_ERRDATA, DEQ_ERRDATA);
278 cpl_propertylist_append_string(plist, FHDR_QUALDATA, DEQ_QUALDATA);
279 eris_check_error_code("eris_ifu_heades_add_hduclass_data");
280 return cpl_error_get_code();
281}
282
283
284/*----------------------------------------------------------------------------*/
296/*----------------------------------------------------------------------------*/
297cpl_error_code eris_ifu_heades_add_hduclass_errs(cpl_propertylist* plist,
298 deqErrorType errorType)
299{
300 cpl_propertylist_update_string(plist, FHDR_HDUCLAS2, DEQ_HDUCLAS2_ERROR);
301 cpl_propertylist_update_string(plist, FHDR_HDUCLAS3,
302 deqErrorTypeString[errorType]);
303 cpl_propertylist_update_string(plist, FHDR_EXTNAME, DEQ_ERRDATA);
304 cpl_propertylist_update_string(plist, FHDR_SCIDATA, DEQ_SCIDATA);
305 cpl_propertylist_update_string(plist, FHDR_QUALDATA, DEQ_QUALDATA);
306 eris_check_error_code("eris_ifu_heades_add_hduclass_data");
307 return cpl_error_get_code();
308}
309
310/*----------------------------------------------------------------------------*/
323/*----------------------------------------------------------------------------*/
324cpl_error_code eris_ifu_heades_add_hduclass_qual(cpl_propertylist* plist,
325 deqQualityType qualityType){
326 cpl_propertylist_update_string(plist, FHDR_HDUCLAS2, DEQ_HDUCLAS2_QUAL);
327 cpl_propertylist_update_string(plist, FHDR_HDUCLAS3,
328 deqQualityTypeString[qualityType]);
329 cpl_propertylist_update_string(plist, FHDR_EXTNAME, DEQ_QUALDATA);
330 cpl_propertylist_update_string(plist, FHDR_SCIDATA, DEQ_SCIDATA);
331 cpl_propertylist_update_string(plist, FHDR_ERRDATA, DEQ_ERRDATA);
332 eris_check_error_code("eris_ifu_heades_add_hduclass_qual");
333 return cpl_error_get_code();
334}
335
336
337/*----------------------------------------------------------------------------*/
366/*----------------------------------------------------------------------------*/
368 cpl_frameset *allframes,
369 cpl_propertylist * header,
370 const cpl_parameterlist *parlist,
371 const cpl_frameset *usedframes,
372 const cpl_frame *inherit,
373 const char *recipe,
374 const cpl_propertylist *applist,
375 const char *remregexp,
376 const char *filename,
377 const cpl_image *image,
378 const cpl_image *error,
379 deqErrorType errorType,
380 const cpl_image *dataQualityMask,
381 deqQualityType dataQualityType,
382 const char* unit)
383{
384 cpl_error_code retVal = CPL_ERROR_NONE;
385 cpl_propertylist *dataHeader = NULL,
386 *errsHeader = NULL,
387 *qualHeader = NULL;
388 char *pipe_id = NULL;
389
390 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
391 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
392 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
393 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
394 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
395 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
396 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
397
398
399 //cpl_ensure_code(error, CPL_ERROR_NULL_INPUT);
400 //cpl_ensure_code(dataQualityMask, CPL_ERROR_NULL_INPUT);
401 cpl_propertylist* head_wcs_2d = NULL;
402 TRY
403 {
404
405 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
406 dataHeader = cpl_propertylist_new();
407 eris_ifu_heades_add_hduclass_common(dataHeader, DEQ_HDUCLAS1);
409 head_wcs_2d = eris_ifu_plist_extract_wcs2D(applist);
410 /* remove WCS from primary header as data go into extensions */
411 eris_ifu_plist_erase_wcs( (cpl_propertylist *) applist);
412 if(cpl_propertylist_has( (cpl_propertylist *) applist,"BUNIT")) {
413 cpl_propertylist_erase((cpl_propertylist *) applist, "BUNIT");
414 }
415 if(cpl_propertylist_has( (cpl_propertylist *) applist,"EXTNAME")) {
416 cpl_propertylist_erase( (cpl_propertylist *)applist,"EXTNAME");
417 }
418 if(cpl_propertylist_has( (cpl_propertylist *) applist,"HDUCLAS2")) {
419 cpl_propertylist_erase( (cpl_propertylist *)applist,"HDUCLAS2");
420 }
421 if(cpl_propertylist_has( (cpl_propertylist *) applist,"COMMENT")) {
422 cpl_propertylist_erase( (cpl_propertylist *)applist,"COMMENT");
423 }
424 /* Create an empty primary HDU with only the header information*/
425 cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
426 inherit, recipe, applist, remregexp, pipe_id, filename);
427 cpl_propertylist_append(dataHeader, head_wcs_2d);
428
429 /* Write the extension units */
430 if(cpl_propertylist_has(dataHeader,"BUNIT")) {
431 cpl_propertylist_set_string(dataHeader,"BUNIT", unit);
432 } else {
433 cpl_propertylist_append_string(dataHeader,"BUNIT", unit);
434 }
435 eris_pfits_clean_header_ra_dec_mjd_obs(dataHeader);
436 cpl_image_save(image, filename, cpl_image_get_type(image), dataHeader,
437 CPL_IO_EXTEND);
438
439 if(error) {
440 errsHeader = cpl_propertylist_new();
441 eris_ifu_heades_add_hduclass_common(errsHeader, DEQ_HDUCLAS1);
442 eris_ifu_heades_add_hduclass_errs(errsHeader, errorType);
443
444 if(cpl_propertylist_has(errsHeader,"BUNIT")) {
445 cpl_propertylist_set_string(errsHeader,"BUNIT", unit);
446 } else {
447 cpl_propertylist_append_string(errsHeader,"BUNIT", unit);
448 }
449 cpl_propertylist_append(errsHeader, head_wcs_2d);
450 eris_pfits_clean_header_ra_dec_mjd_obs(errsHeader);
451 cpl_image_save(error, filename, cpl_image_get_type(error), errsHeader,
452 CPL_IO_EXTEND);
453 }
454
455 if(dataQualityMask) {
456 qualHeader = cpl_propertylist_new();
457 eris_ifu_heades_add_hduclass_common(qualHeader, DEQ_HDUCLAS1);
458 eris_ifu_heades_add_hduclass_qual(qualHeader, dataQualityType);
459
460 if(cpl_propertylist_has(qualHeader,"BUNIT")) {
461 cpl_propertylist_set_string(qualHeader,"BUNIT", "");
462 } else {
463 cpl_propertylist_append_string(qualHeader,"BUNIT", "");
464 }
465 cpl_propertylist_append(qualHeader, head_wcs_2d);
466 eris_pfits_clean_header_ra_dec_mjd_obs(qualHeader);
467 cpl_image_save(dataQualityMask, filename,
468 cpl_image_get_type(dataQualityMask), qualHeader, CPL_IO_EXTEND);
469 }
470
471 }
472 CATCH
473 {
474 retVal = cpl_error_get_code();
475 }
476 eris_ifu_free_propertylist(&dataHeader);
477 eris_ifu_free_propertylist(&errsHeader);
478 eris_ifu_free_propertylist(&qualHeader);
479 eris_ifu_free_propertylist(&head_wcs_2d);
480 eris_ifu_free_string(&pipe_id);
481 eris_check_error_code("eris_ifu_save_deq_image");
482 return retVal;
483}
484
485
486
487/*----------------------------------------------------------------------------*/
515/*----------------------------------------------------------------------------*/
517 cpl_frameset *allframes,
518 cpl_propertylist * header,
519 const cpl_parameterlist *parlist,
520 const cpl_frameset *usedframes,
521 const cpl_frame *inherit,
522 const char *recipe,
523 cpl_propertylist *applist,
524 const char *remregexp,
525 const char *filename,
526 cpl_imagelist *data,
527 cpl_imagelist *error,
528 deqErrorType errorType,
529 const cpl_imagelist *dataQualityMask,
530 deqQualityType qualityType/*,
531 cpl_type imageType*/)
532{
533 cpl_error_code retVal = CPL_ERROR_NONE;
534 cpl_propertylist *dataHeader = NULL,
535 *errsHeader = NULL,
536 *qualHeader = NULL;
537 char *pipe_id = NULL;
538
539 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
540 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
541 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
542 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
543 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
544 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
545 cpl_ensure_code(data, CPL_ERROR_NULL_INPUT);
546 cpl_ensure_code(error, CPL_ERROR_NULL_INPUT);
547 cpl_ensure_code(dataQualityMask, CPL_ERROR_NULL_INPUT);
548
549 TRY
550 {
551
552 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
553
554 /* Create FITS headers for the extensions */
555 if(cpl_propertylist_has(applist,"CDELT3")) {
556 cpl_propertylist_erase(applist, "CDELT3");
557 }
558 dataHeader = cpl_propertylist_duplicate(applist);
559 errsHeader = cpl_propertylist_duplicate(applist);
560 qualHeader = cpl_propertylist_duplicate(applist);
561
562 eris_ifu_heades_add_hduclass_common(dataHeader, "IMAGE");
564 eris_ifu_heades_add_hduclass_common(errsHeader, "IMAGE");
565 eris_ifu_heades_add_hduclass_errs(errsHeader, errorType);
566 eris_ifu_heades_add_hduclass_common(qualHeader, "IMAGE");
567 eris_ifu_heades_add_hduclass_qual(qualHeader, qualityType);
568
569 if(cpl_propertylist_has(dataHeader,"HDRVER")) {
570 cpl_propertylist_erase(dataHeader,"HDRVER");
571 }
572 if(cpl_propertylist_has(errsHeader,"HDRVER")) {
573 cpl_propertylist_erase(errsHeader,"HDRVER");
574 }
575 if(cpl_propertylist_has(qualHeader,"HDRVER")) {
576 cpl_propertylist_erase(qualHeader,"HDRVER");
577 }
578 if(cpl_propertylist_has(dataHeader,"CTYPE1")) {
579 const char* ctype1 = cpl_propertylist_get_string(dataHeader,"CTYPE1");
580 if (!cpl_propertylist_has(errsHeader,"CTYPE1"))
581 cpl_propertylist_append_string(errsHeader, "CTYPE1", ctype1);
582 if (!cpl_propertylist_has(qualHeader,"CTYPE1"))
583 cpl_propertylist_append_string(qualHeader, "CTYPE1", ctype1);
584 }
585 if(cpl_propertylist_has(dataHeader,"CTYPE2")) {
586 const char* ctype2 = cpl_propertylist_get_string(dataHeader,"CTYPE2");
587 if (!cpl_propertylist_has(errsHeader,"CTYPE2"))
588 cpl_propertylist_append_string(errsHeader, "CTYPE2",ctype2);
589 if (!cpl_propertylist_has(errsHeader,"CTYPE2"))
590 cpl_propertylist_append_string(qualHeader, "CTYPE2", ctype2);
591 }
592
593 /* Create an empty primary HDU with only the header information*/
594 eris_ifu_plist_erase_wcs( (cpl_propertylist *)applist);
595 eris_pfits_clean_header(dataHeader, CPL_TRUE);
596 eris_pfits_clean_header(errsHeader, CPL_TRUE);
597 eris_pfits_clean_header(qualHeader, CPL_TRUE);
598 if(strstr(filename, "sky") != NULL) {
599 cpl_propertylist_append_string(applist, "PRODCATG", PRODCATG_CUBE_SKY);
600 } else {
601 cpl_propertylist_append_string(applist, "PRODCATG", PRODCATG_CUBE_OBJ);
602 }
603 if(cpl_propertylist_has(applist,"BUNIT")) {
604 cpl_propertylist_erase(applist,"BUNIT");
605 }
606 if(cpl_propertylist_has(applist,"COMMENT")) {
607 cpl_propertylist_erase(applist,"COMMENT");
608 }
609
610
611 cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
612 inherit, recipe, applist, remregexp,
613 pipe_id, filename);
614
615 /* Write the extension units */
616 cpl_imagelist_save(data, filename,
617 cpl_image_get_type(cpl_imagelist_get(data,0)),
618 dataHeader, CPL_IO_EXTEND);
619
620 cpl_imagelist_save(error, filename,
621 cpl_image_get_type(cpl_imagelist_get(error,0)),
622 errsHeader, CPL_IO_EXTEND);
623 if(cpl_propertylist_has(qualHeader,"BUNIT")) {
624 cpl_propertylist_set_string(qualHeader,"BUNIT","");
625 } else {
626 cpl_propertylist_append_string(qualHeader,"BUNIT","");
627 }
628 cpl_imagelist_save(dataQualityMask, filename,
629 CPL_TYPE_INT,
630 qualHeader, CPL_IO_EXTEND);
631 }
632 CATCH
633 {
634 retVal = cpl_error_get_code();
635 }
636 cpl_propertylist_delete(dataHeader);
637 cpl_propertylist_delete(errsHeader);
638 cpl_propertylist_delete(qualHeader);
639 cpl_free(pipe_id);
640 eris_check_error_code("eris_ifu_save_deq_cube");
641 return retVal;
642}
643
644/*----------------------------------------------------------------------------*/
665/*----------------------------------------------------------------------------*/
667 cpl_frameset *allframes,
668 const cpl_parameterlist *parlist,
669 const cpl_frameset *usedframes,
670 const char *recipe,
671 const cpl_propertylist *applist,
672 const char *remregexp,
673 const char *filename,
674 const cpl_image *image,
675 cpl_type type,
676 const char* unit)
677{
678 cpl_error_code retVal = CPL_ERROR_NONE;
679 cpl_propertylist *dataHeader = NULL;
680 char *pipe_id = NULL;
681
682 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
683 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
684 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
685 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
686 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
687 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
688 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
689
690 cpl_propertylist* head_wcs_2d = NULL;
691 TRY
692 {
693
694 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
695 dataHeader = cpl_propertylist_new();
696
697 if(cpl_propertylist_has( (cpl_propertylist *) applist,"EXTNAME")) {
698 cpl_propertylist_erase( (cpl_propertylist *)applist,"EXTNAME");
699 }
700
701
702 if(cpl_propertylist_has(applist,"BUNIT")) {
703 cpl_propertylist_set_string((cpl_propertylist *) applist,"BUNIT", unit);
704 } else {
705 cpl_propertylist_append_string((cpl_propertylist *) applist,"BUNIT", unit);
706 }
707 cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, image, type,
708 recipe, applist, remregexp, pipe_id, filename);
709 }
710 CATCH
711 {
712 retVal = cpl_error_get_code();
713 }
714 eris_ifu_free_propertylist(&dataHeader);
715 eris_ifu_free_propertylist(&head_wcs_2d);
716 eris_ifu_free_string(&pipe_id);
717 eris_check_error_code("eris_ifu_save_image");
718 return retVal;
719}
720
721/*----------------------------------------------------------------------------*/
744/*----------------------------------------------------------------------------*/
745cpl_error_code eris_ifu_save_table(
746 cpl_frameset *allframes,
747 cpl_propertylist *header,
748 const cpl_parameterlist *parlist,
749 const cpl_frameset *usedframes,
750 const cpl_frame *inherit,
751 const char *recipe,
752 const char *procatg,
753 cpl_propertylist *applist,
754 const char *remregexp,
755 const char *filename,
756 const cpl_table *table)
757{
758 cpl_error_code retVal = CPL_ERROR_NONE;
759 cpl_propertylist *dataHeader = NULL;
760 char *pipe_id = NULL;
761
762 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
763 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
764 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
765 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
766 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
767 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
768 cpl_ensure_code(table, CPL_ERROR_NULL_INPUT);
769 cpl_ensure_code(procatg, CPL_ERROR_NULL_INPUT);
770
771 TRY
772 {
773
774 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
775 /* Create FITS headers for the extensions */
776 dataHeader = cpl_propertylist_new();
777 eris_ifu_heades_add_hduclass_common(dataHeader, "TABLE");
778
779 cpl_propertylist_append_string(dataHeader, FHDR_HDUCLAS2, DEQ_HDUCLAS2_DATA);
780 cpl_propertylist_append_string(dataHeader, FHDR_EXTNAME, DEQ_SCIDATA);
781 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG, procatg);
782
783 /* Create an empty primary HDU with only the header information*/
784 cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
785 inherit, recipe, applist, remregexp, pipe_id, filename);
786
787 /* Write the extension unit */
788 cpl_table_save(table, dataHeader, applist, filename, CPL_IO_EXTEND);
789 }
790 CATCH
791 {
792 CATCH_MSGS();
793 retVal = cpl_error_get_code();
794 }
795
796 eris_ifu_free_propertylist(&dataHeader);
797 eris_ifu_free_string(&pipe_id);
798 eris_check_error_code("eris_ifu_save_table");
799 return retVal;
800}
801
803// * according to VLT-SPE-ESO-19500-5667
804// @brief Write DFS pipeline product with table.
805// @param allframes The list of input frames for the recipe
806// @param header NULL or filled with properties written to product header
807// @param parlist The list of input parameters
808// @param usedframes The list of raw/calibration frames used for this product
809// @param inherit NULL or product frames inherit their header from this frame
810// @param recipe The recipe name
811// @param applist Propertylist to append to primary header, w. PRO.CATG
812// @param remregexp NULL or regexp of properties not to put in main header
813// @param filename Filename of created product
814// @param bpm The badpixel map to be saved
815// @return CPL_ERROR_NONE if OK
816//*/
817//cpl_error_code eris_ifu_save_bpm(
818// cpl_frameset *allframes,
819// cpl_propertylist *header,
820// const cpl_parameterlist *parlist,
821// const cpl_frameset *usedframes,
822// const cpl_frame *inherit,
823// const char *recipe,
824// const char *procatg,
825// cpl_propertylist *applist,
826// const char *remregexp,
827// const char *filename,
828// const cpl_image *bpm)
829//{
830// cpl_error_code retVal = CPL_ERROR_NONE;
831// cpl_propertylist *dataHeader = NULL;
832// char *pipe_id = NULL;
833// cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
834// cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
835// cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
836// cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
837// cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
838// cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
839// cpl_ensure_code(bpm, CPL_ERROR_NULL_INPUT);
840// cpl_ensure_code(procatg, CPL_ERROR_NULL_INPUT);
841// TRY
842// {
843// pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
844// /* Create FITS headers for the extensions */
845//
846// dataHeader = cpl_propertylist_new();
847// eris_ifu_heades_add_hduclass_common(dataHeader, "TABLE");
848// cpl_propertylist_append_string(dataHeader, FHDR_HDUCLAS2, DEQ_HDUCLAS2_DATA);
849// cpl_propertylist_append_string(dataHeader, FHDR_EXTNAME, DEQ_SCIDATA);
850// cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG, procatg);
851// /* Create an empty primary HDU with only the header information*/
852// cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
853// inherit, recipe, applist, remregexp,
854// pipe_id, filename));
855// /* Write the extension unit */
856// cpl_image_save(bpm, filename, cpl_image_get_type(bpm), dataHeader,
857// CPL_IO_EXTEND);
858// }
859// CATCH
860// {
861// CATCH_MSGS();
862// retVal = cpl_error_get_code();
863// }
864// eris_ifu_free_propertylist(&dataHeader);
865// eris_ifu_free_string(&pipe_id);
866// return retVal;
867//}
868
888/*----------------------------------------------------------------------------*/
890 const char *filename,
891 cpl_propertylist **primaryHeader,
892 cpl_image **dataImage,
893 cpl_image **errImage,
894 cpl_image **qualImage,
895 deqErrorType *errorType,
896 deqQualityType *qualityType)
897{
898 cpl_error_code retVal = CPL_ERROR_NONE;
899 cpl_size nExt;
900 int extension;
901 int match;
902 cpl_propertylist *pl = NULL;
903 const char *property;
904
905 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
906 cpl_ensure_code(dataImage, CPL_ERROR_NULL_INPUT);
907
908 TRY
909 {
910 nExt = cpl_fits_count_extensions(filename);
912 if (nExt != 3) {
913 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
914 "For a DEQ FITS file 3 extensions are expected, but %d are found",
915 (int) nExt);
916 }
917 *dataImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED, 0, 1);
918
919 if (primaryHeader != NULL) {
920 *primaryHeader = cpl_propertylist_load(filename, 0);
921 }
922
923 if (errImage != NULL) {
924 extension = 2;
925 *errImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED,
926 0, extension);
927 pl = cpl_propertylist_load(filename, extension);
928 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
929 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
930 } else {
931 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
932 "keyword %s does not exist",
933 FHDR_HDUCLAS3);
934 }
935 for (match=0; match<4; match++) {
936 if (strcmp(property, deqErrorTypeString[match]) == 0) {
937 *errorType = match;
938 break;
939 }
940 }
941 if (match > 3) {
942 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
943 "The HDUCLAS3 keyword for the error extension (%d) has an"
944 " unknown value of %s", extension, property);
945 }
947 }
948
949 if (qualImage != NULL) {
950 extension = 3;
951 *qualImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED,
952 0, 3);
953 pl = cpl_propertylist_load(filename, extension);
954 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
955 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
956 } else {
957 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
958 "keyword %s does not exist",
959 FHDR_HDUCLAS3);
960 }
961 for (match=0; match<4; match++) {
962 if (strcmp(property, deqQualityTypeString[match]) == 0) {
963 *qualityType = match;
964 break;
965 }
966 }
967 if (match > 3) {
968 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
969 "The HDUCLAS3 keyword for the error extension (%d) has an"
970 " unknown value of %s", (int) extension, property);
971 }
973 }
974 }
975 CATCH
976 {
977 retVal = cpl_error_get_code();
978 }
979 eris_check_error_code("eris_ifu_load_deq_image");
980 return retVal;
981}
982
983/*----------------------------------------------------------------------------*/
1001/*----------------------------------------------------------------------------*/
1003 const char *filename,
1004 cpl_propertylist **primaryHeader,
1005 cpl_imagelist **dataImagelist,
1006 cpl_imagelist **errImagelist,
1007 cpl_imagelist **qualImagelist,
1008 deqErrorType *errorType,
1009 deqQualityType *qualityType)
1010{
1011 cpl_error_code retVal = CPL_ERROR_NONE;
1012 cpl_size nExt;
1013 int extension;
1014 int match;
1015 cpl_propertylist *pl = NULL;
1016 const char *property;
1017
1018 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
1019 cpl_ensure_code(dataImagelist, CPL_ERROR_NULL_INPUT);
1020
1021 TRY
1022 {
1023 nExt = cpl_fits_count_extensions(filename);
1025 if (nExt != 3) {
1026 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1027 "For a DEQ FITS file 3 extensions are expected, but %d are found",
1028 (int) nExt);
1029 }
1030 *dataImagelist = cpl_imagelist_load(filename, CPL_TYPE_UNSPECIFIED, 1);
1031
1032 if (primaryHeader != NULL) {
1033 *primaryHeader = cpl_propertylist_load(filename, 0);
1034 }
1035
1036 if (errImagelist != NULL) {
1037 extension = 2;
1038 *errImagelist = cpl_imagelist_load(filename, CPL_TYPE_UNSPECIFIED,
1039 extension);
1040 pl = cpl_propertylist_load(filename, extension);
1041 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
1042 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
1043 } else {
1044 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1045 "keyword %s does not exist",
1046 FHDR_HDUCLAS3);
1047 }
1048 for (match=0; match<4; match++) {
1049 if (strcmp(property, deqErrorTypeString[match]) == 0) {
1050 *errorType = match;
1051 break;
1052 }
1053 }
1054 if (match > 3) {
1055 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1056 "The HDUCLAS3 keyword for the error extension (%d) has an"
1057 " unknown value of %s", extension, property);
1058 }
1060 }
1061
1062 if (qualImagelist != NULL) {
1063 extension = 3;
1064 *qualImagelist = cpl_imagelist_load(filename, CPL_TYPE_UNSPECIFIED,
1065 extension);
1066 pl = cpl_propertylist_load(filename, extension);
1067 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
1068 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
1069 } else {
1070 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1071 "keyword %s does not exist",
1072 FHDR_HDUCLAS3);
1073 }
1074 for (match=0; match<4; match++) {
1075 if (strcmp(property, deqQualityTypeString[match]) == 0) {
1076 *qualityType = match;
1077 break;
1078 }
1079 }
1080 if (match > 3) {
1081 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1082 "The HDUCLAS3 keyword for the error extension (%d) has an"
1083 " unknown value of %s", (int) extension, property);
1084 }
1086 }
1087 }
1088 CATCH
1089 {
1090 retVal = cpl_error_get_code();
1091 }
1092 eris_check_error_code("eris_ifu_load_deq_imagelist");
1093 return retVal;
1094}
1095
1096/*----------------------------------------------------------------------------*/
1115/*----------------------------------------------------------------------------*/
1117 const char *filename,
1118 cpl_propertylist **primaryHeader,
1119 cpl_image **qualImage,
1120 deqQualityType *qualityType)
1121{
1122 hdrl_image *image = NULL;
1123 cpl_image *dataImage = NULL;
1124 cpl_image *errImage = NULL;
1125 cpl_mask *mask = NULL;
1126
1127 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
1128
1129 TRY
1130 {
1131 deqErrorType errorType;
1132 eris_ifu_load_deq_image(filename, primaryHeader,
1133 &dataImage, &errImage, qualImage, &errorType, qualityType);
1134
1135 switch(errorType) {
1136 case mse:
1137 cpl_image_power(errImage, 0.5);
1138 break;
1139 case rmse:
1140 // nothing to do, RMSE is Ok
1141 break;
1142 case invmse:
1143 CPL_ATTR_FALLTRHU; //intended fall through !
1144 case invrmse:
1145 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1146 "Cannot transfer error extension type %s to a HDLR error"
1147 " image", deqErrorTypeString[errorType]);
1148 break;
1149 default:
1150 BRK_WITH_ERROR_MSG(CPL_ERROR_ACCESS_OUT_OF_RANGE,
1151 "Internal error: unknown errorType enumeration value: %d",
1152 errorType);
1153 break;
1154 }
1155
1156 if (qualImage != NULL) {
1157 switch (*qualityType) {
1158 case maskzero:
1159 //intended fall through !
1160 CPL_ATTR_FALLTRHU;
1161 case flag16bit:
1162 //intended fall through !
1163 CPL_ATTR_FALLTRHU;
1164 case flag32bit: // The value "0" indicates a "good" pixel
1165 mask = cpl_mask_threshold_image_create(*qualImage,
1166 -1.0, 1.0);
1167 break;
1168 case maskone: // The value "1" indicates a "good" pixel
1169 mask = cpl_mask_threshold_image_create(*qualImage,
1170 0.0, 2.0);
1171 break;
1172 default:
1174 CPL_ERROR_ACCESS_OUT_OF_RANGE,
1175 "Internal error: unknown qualityType enumeration"
1176 " value: %d",
1177 *qualityType);
1178 break;
1179 }
1180 }
1181
1182 cpl_mask_not(mask); //hey it is a BAD pixel mask!
1183 cpl_image_set_bpm(dataImage, mask);
1184 mask = NULL;
1185 image = hdrl_image_create(dataImage, errImage);
1187 }
1188 CATCH
1189 {
1190 cpl_mask_delete(mask);
1191 hdrl_image_delete(image);
1192 image = NULL;
1193 }
1194 cpl_image_delete(dataImage);
1195 cpl_image_delete(errImage);
1196 eris_check_error_code("eris_ifu_load_deq_hdrl_image");
1197 return image;
1198}
1199
1200/*----------------------------------------------------------------------------*/
1217/*----------------------------------------------------------------------------*/
1219 const char *filename,
1220 cpl_propertylist **primaryHeader,
1221 cpl_imagelist **qualImagelist,
1222 deqQualityType *qualityType)
1223{
1224 hdrl_imagelist *imagelist = NULL;
1225 cpl_imagelist *dataImagelist = NULL;
1226 cpl_imagelist *errImagelist = NULL;
1227 cpl_image *dataImage = NULL;
1228 cpl_image *errImage = NULL;
1229 cpl_image *qualImage = NULL;
1230 cpl_mask *mask = NULL;
1231 deqErrorType errorType;
1232
1233 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
1234
1235 TRY
1236 {
1237 eris_ifu_load_deq_imagelist(filename, primaryHeader,
1238 &dataImagelist, &errImagelist, qualImagelist, &errorType, qualityType);
1239
1240 if ((cpl_imagelist_get_size(dataImagelist) != cpl_imagelist_get_size(errImagelist))
1241 ||
1242 (cpl_imagelist_get_size(dataImagelist) != cpl_imagelist_get_size(*qualImagelist)))
1243 {
1244 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
1245 "All imagelists of an DEQ imagelist file must have the same size");
1246 }
1247
1248 for (cpl_size ix=0; ix < cpl_imagelist_get_size(dataImagelist); ix++) {
1249 dataImage = cpl_imagelist_get(dataImagelist, ix);
1250 errImage = cpl_imagelist_get(errImagelist, ix);
1251 qualImage = cpl_imagelist_get(*qualImagelist, ix);
1252
1253 switch(errorType) {
1254 case mse:
1256 cpl_image_power(errImage, 0.5));
1257 break;
1258 case rmse:
1259 // nothing to do, RMSE is Ok
1260 break;
1261 case invmse:
1262 //intended fall through !
1263 CPL_ATTR_FALLTRHU;
1264 case invrmse:
1265 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1266 "Cannot transfer error extension type %s to a HDLR error"
1267 " image", deqErrorTypeString[errorType]);
1268 break;
1269 default:
1270 BRK_WITH_ERROR_MSG(CPL_ERROR_ACCESS_OUT_OF_RANGE,
1271 "Internal error: unknown errorType enumeration value: %d",
1272 errorType);
1273 break;
1274 }
1275
1276 if (qualImage != NULL) {
1277 switch (*qualityType) {
1278 case maskzero:
1279 //intended fall through !
1280 CPL_ATTR_FALLTRHU;
1281 case flag16bit:
1282 //intended fall through !
1283 CPL_ATTR_FALLTRHU;
1284 case flag32bit: // The value "0" indicates a "good" pixel
1285 mask = cpl_mask_threshold_image_create(qualImage,
1286 -1.0, 1.0);
1287 break;
1288 case maskone: // The value "1" indicates a "good" pixel
1289 mask = cpl_mask_threshold_image_create(qualImage,
1290 0.5, 2.0);
1291 break;
1292 default:
1294 CPL_ERROR_ACCESS_OUT_OF_RANGE,
1295 "Internal error: unknown qualityType enumeration"
1296 " value: %d",
1297 *qualityType);
1298 break;
1299 }
1300 }
1301 cpl_mask_not(mask); //hey it is a BAD pixel mask!
1302
1303 int pis_rejected;
1304 for (cpl_size ny = 1; ny <= cpl_image_get_size_y(dataImage); ny++) {
1305 for (cpl_size nx = 1; nx <= cpl_image_get_size_x(dataImage); nx++) {
1306 if (eris_ifu_is_nan_or_inf(cpl_image_get(dataImage, nx, ny, &pis_rejected)) ||
1307 eris_ifu_is_nan_or_inf(cpl_image_get(errImage, nx, ny, &pis_rejected))
1308 ) {
1309 cpl_mask_set(mask, nx, ny, BAD_PIX);
1310 }
1311 }
1312 }
1313
1314 cpl_image_set_bpm(dataImage, mask);
1315 cpl_image_set_bpm(errImage, cpl_mask_duplicate(mask));
1317 cpl_imagelist_set(dataImagelist, dataImage, ix);
1318 cpl_imagelist_set(errImagelist, errImage, ix);
1319
1320 }
1321 imagelist = hdrl_imagelist_create(
1322 dataImagelist, errImagelist);
1323 }
1324 CATCH
1325 {
1326 hdrl_imagelist_delete(imagelist);
1327 imagelist = NULL;
1328 }
1329 cpl_imagelist_delete(dataImagelist);
1330 cpl_imagelist_delete(errImagelist);
1331 eris_check_error_code("eris_ifu_load_deq_hdrl_imagelist");
1332 return imagelist;
1333}
1334
1335/*----------------------------------------------------------------------------*/
1352/*----------------------------------------------------------------------------*/
1354 const cpl_frame *frame,
1355 ifsBand band,
1356 ifsPreopticsScale scale,
1357 cpl_image **qualImage,
1358 deqQualityType *qualType)
1359{
1360 hdrl_image *image = NULL;
1361 const char *filename;
1362
1363 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, NULL);
1364
1365 TRY
1366 {
1367 filename = cpl_frame_get_filename(frame);
1369 filename, band, scale, qualImage, qualType);
1370 }
1371 CATCH
1372 {
1373 hdrl_image_delete(image);
1374 image = NULL;
1375 }
1376 eris_check_error_code("eris_ifu_load_cal_image_frame");
1377 return image;
1378}
1379
1380/*----------------------------------------------------------------------------*/
1398/*----------------------------------------------------------------------------*/
1400 const char *filename,
1401 ifsBand band,
1402 ifsPreopticsScale scale,
1403 cpl_image **qualImage,
1404 deqQualityType *qualType)
1405{
1406 hdrl_image *image = NULL;
1407 cpl_propertylist *header = NULL;
1408 cpl_image *dataImage = NULL;
1409 cpl_image *errorImage = NULL;
1410 cpl_size nExt;
1411 // ifsBand thisBand;
1412 // ifsPreopticsScale thisScale;
1413
1414 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
1415
1416 TRY
1417 {
1418 nExt = cpl_fits_count_extensions(filename);
1419 if (nExt == 3) { // new format data/error/quality DEQ
1420 image = eris_ifu_load_deq_hdrl_image(filename, &header,
1421 qualImage, qualType);
1422 } else { // old format, data only, error will be simulated
1423 header = cpl_propertylist_load(filename, 0);
1424 dataImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED,
1425 0 , 0);
1426 errorImage = cpl_image_abs_create(dataImage);
1427 cpl_image_multiply_scalar(errorImage, 0.1);
1428 image = hdrl_image_create(dataImage, errorImage);
1429 *qualImage = NULL;
1430 if (qualType != NULL) {
1431 *qualType = unspecified;
1432 }
1433 }
1434 if ((band != UNDEFINED_BAND) &&
1435 (band != eris_ifu_get_band(header))) {
1436 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1437 "different instrument band setting"
1438 " in calibration frame %s", filename);
1439 }
1440 if ((scale != UNDEFINED_SCALE) &&
1441 (scale != PUPIL) &&
1442 (scale != eris_ifu_get_preopticsScale(header))) {
1443 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1444 "different instrument pre-optics setting"
1445 " in calibration frame %s", filename);
1446 }
1447
1448 }
1449 CATCH
1450 {
1451 hdrl_image_delete(image);
1452 image = NULL;
1453 }
1455 eris_ifu_free_image(&dataImage);
1456 eris_ifu_free_image(&errorImage);
1457 eris_check_error_code("eris_ifu_load_cal_image_file");
1458 return image;
1459}
1460
1461/*----------------------------------------------------------------------------*/
1476/*----------------------------------------------------------------------------*/
1478 const char* filename,
1479 cpl_polynomial ** poly_u,
1480 cpl_polynomial ** poly_v)
1481{
1482 cpl_error_code retVal = CPL_ERROR_NONE;
1483 cpl_table *distortionTable = NULL;
1484 cpl_size nTableRows;
1485 cpl_size pows[2];
1486 double coeff;
1487
1488 TRY
1489 {
1490 *poly_u = cpl_polynomial_new(2);
1491 *poly_v = cpl_polynomial_new(2);
1492
1493 distortionTable = cpl_table_load(filename,1,0);
1494 nTableRows = cpl_table_get_nrow(distortionTable);
1495
1496 for (cpl_size i=0; i<nTableRows; i++) {
1497 pows[0] = cpl_table_get_int(distortionTable, "degx", i, NULL);
1498 pows[1] = cpl_table_get_int(distortionTable, "degy", i, NULL);
1499 coeff = cpl_table_get_double(distortionTable, "coeff", i, NULL);
1500 cpl_polynomial_set_coeff(*poly_u, pows, coeff);
1501 }
1503
1504 pows[0] = 0;
1505 pows[1] = 1;
1506 cpl_polynomial_set_coeff(*poly_v, pows, 1.0);
1508 }
1509 CATCH
1510 {
1511 retVal = cpl_error_get_code();
1512 }
1513
1514 eris_ifu_free_table(&distortionTable);
1515 eris_check_error_code("eris_ifu_load_cal_image_file");
1516 return retVal;
1517}
1518
1519/*----------------------------------------------------------------------------*/
1535/*----------------------------------------------------------------------------*/
1537 const char* filename,
1538 cpl_polynomial ***polynomials,
1539 cpl_table **borders)
1540{
1541 cpl_error_code retVal = CPL_ERROR_NONE;
1542 cpl_table *distortionTable = NULL;
1543
1544 TRY
1545 {
1546 *borders = NULL;
1547
1548 *polynomials =
1549 cpl_calloc(SLITLET_CNT, sizeof(cpl_polynomial*));
1550
1551 for (int sx = 0; sx < SLITLET_CNT; sx++) {
1552 cpl_size nTableRows;
1553
1554 eris_ifu_free_table(&distortionTable);
1555 distortionTable = cpl_table_load(filename, sx+1, 0);
1556 nTableRows = cpl_table_get_nrow(distortionTable);
1557
1558 (*polynomials)[sx] = cpl_polynomial_new(2);
1559
1560 for (cpl_size i=0; i<nTableRows; i++) {
1561 cpl_size pows[2];
1562 double coeff;
1563
1564 pows[0] = cpl_table_get_int(distortionTable, "degx", i, NULL);
1565 pows[1] = cpl_table_get_int(distortionTable, "degy", i, NULL);
1566 coeff = cpl_table_get_double(distortionTable, "coeff", i, NULL);
1567 cpl_polynomial_set_coeff((*polynomials)[sx], pows, coeff);
1568 }
1570
1571 }
1572
1573 *borders = cpl_table_load(filename, SLITLET_CNT+1, 0);
1574 }
1575 CATCH
1576 {
1577 if (*polynomials != NULL) {
1578 for (int sx = 0; sx < SLITLET_CNT; sx++) {
1579 cpl_polynomial_delete((*polynomials)[sx]);
1580 }
1581 }
1582 eris_ifu_free_table(borders);
1583 retVal = cpl_error_set_where(cpl_func);
1584 }
1585
1586 eris_ifu_free_table(&distortionTable);
1587 eris_check_error_code("eris_ifu_load_distortion_polynomials");
1588 return retVal;
1589
1590}
1591
1592#define ERIS_IFU_DIST_SLIT_DISTANCE "slitlet_distance"
1593
1594/*----------------------------------------------------------------------------*/
1605/*----------------------------------------------------------------------------*/
1607 const char* filename)
1608{
1609 cpl_vector *distances = NULL;
1610 cpl_table *table = NULL;
1611 cpl_size nTableRows = 0;
1612 double val = 0.;
1613
1614 TRY
1615 {
1616 table = cpl_table_load(filename,1,0);
1617 nTableRows = cpl_table_get_nrow(table);
1618
1619 distances = cpl_vector_new(nTableRows);
1620 cpl_type type = cpl_table_get_column_type(table, ERIS_IFU_DIST_SLIT_DISTANCE);
1621 for (cpl_size i=0; i<nTableRows; i++) {
1622 if (type == CPL_TYPE_DOUBLE) {
1623 val = cpl_table_get_double(table, ERIS_IFU_DIST_SLIT_DISTANCE, i, NULL);
1624 } else if (type == CPL_TYPE_FLOAT) {
1625 val = cpl_table_get_float(table, ERIS_IFU_DIST_SLIT_DISTANCE, i, NULL);
1626 } else {
1627 SET_ERROR(CPL_ERROR_INVALID_TYPE);
1628 }
1630 cpl_vector_set(distances, i, val));
1631 }
1632 }
1633 CATCH
1634 {
1635 cpl_vector_delete(distances);
1636 distances = NULL;
1637 }
1638
1639 eris_ifu_free_table(&table);
1640 eris_check_error_code("eris_ifu_load_distances");
1641 return distances;
1642}
1643
1644#define ERIS_IFU_DIST_EDGE_L_old "pos1"
1645#define ERIS_IFU_DIST_EDGE_R_old "pos2"
1646
1647/*----------------------------------------------------------------------------*/
1658/*----------------------------------------------------------------------------*/
1660 const char* filename)
1661{
1662 cpl_bivector *slitPos = NULL;
1663 cpl_vector *pos1 = NULL;
1664 cpl_vector *pos2 = NULL;
1665 cpl_table *slitPosTable = NULL;
1666 cpl_size nTableRows;
1667
1668 TRY
1669 {
1670 slitPosTable = cpl_table_load(filename,1,0);
1671 nTableRows = cpl_table_get_nrow(slitPosTable);
1672
1673 slitPos = cpl_bivector_new(nTableRows);
1674 pos1 = cpl_bivector_get_x(slitPos);
1675 pos2 = cpl_bivector_get_y(slitPos);
1677
1678 for (cpl_size i=0; i<nTableRows; i++) {
1679 double val = 0.;
1680 // check if name of table column is "edge_left"
1681 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_L, i, NULL);
1682 if (cpl_error_get_code()== CPL_ERROR_DATA_NOT_FOUND) {
1683 // ok, it seems to be old fashioned sinfoni "pos1"
1684 RECOVER();
1685 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_L_old, i, NULL);
1686 }
1687 cpl_vector_set(pos1, i, val);
1688
1689 // check if name of table column is "edge_right"
1690 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_R, i, NULL);
1691 if (cpl_error_get_code()== CPL_ERROR_DATA_NOT_FOUND) {
1692 // ok, it seems to be old fashioned sinfoni "pos2"
1693 RECOVER();
1694 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_R_old, i, NULL);
1695 }
1696 cpl_vector_set(pos2, i, val);
1697 }
1698 }
1699 CATCH
1700 {
1701 cpl_bivector_delete(slitPos);
1702 slitPos = NULL;
1703 }
1704
1705 eris_ifu_free_table(&slitPosTable);
1706 eris_check_error_code("eris_ifu_load_slit_positions");
1707 return slitPos;
1708}
1709
1710/*----------------------------------------------------------------------------*/
1724/*----------------------------------------------------------------------------*/
1726 cpl_propertylist *pl,
1727 const char *name,
1728 int val,
1729 const char* comment)
1730{
1731 cpl_error_code retVal = CPL_ERROR_NONE;
1732 char *qcName = NULL;
1733
1734 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1735 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1736 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1737
1738 TRY
1739 {
1740 qcName = cpl_sprintf("ESO QC %s", name);
1741 cpl_propertylist_append_int(pl, qcName, val);
1742 cpl_propertylist_set_comment(pl , qcName, comment);
1743 }
1744 CATCH
1745 {
1746 retVal = cpl_error_get_code();
1747 }
1748
1749 eris_ifu_free_string(&qcName);
1750 eris_check_error_code("eris_ifu_append_qc_int");
1751 return retVal;
1752}
1753
1754/*----------------------------------------------------------------------------*/
1768/*----------------------------------------------------------------------------*/
1770 cpl_propertylist *pl,
1771 const char *name,
1772 double val,
1773 const char* comment)
1774{
1775 cpl_error_code retVal = CPL_ERROR_NONE;
1776 char *qcName = NULL;
1777
1778 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1779 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1780 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1781
1782 TRY
1783 {
1784 qcName = cpl_sprintf("ESO QC %s", name);
1785 cpl_propertylist_append_double(pl, qcName, val);
1786 cpl_propertylist_set_comment(pl , qcName, comment);
1787 }
1788 CATCH
1789 {
1790 retVal = cpl_error_get_code();
1791 }
1792
1793 eris_ifu_free_string(&qcName);
1794 eris_check_error_code("eris_ifu_append_qc_double");
1795 return retVal;
1796}
1797
1798/*----------------------------------------------------------------------------*/
1812/*----------------------------------------------------------------------------*/
1814 cpl_propertylist *pl,
1815 const char *name,
1816 float val,
1817 const char* comment)
1818{
1819 cpl_error_code retVal = CPL_ERROR_NONE;
1820 char *qcName = NULL;
1821
1822 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1823 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1824 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1825
1826 TRY
1827 {
1828 qcName = cpl_sprintf("ESO QC %s", name);
1829 cpl_propertylist_append_float(pl, qcName, val);
1830 cpl_propertylist_set_comment(pl , qcName, comment);
1831 }
1832 CATCH
1833 {
1834 retVal = cpl_error_get_code();
1835 }
1836
1837 eris_ifu_free_string(&qcName);
1838 eris_check_error_code("eris_ifu_append_qc_float");
1839 return retVal;
1840}
1841
1842/*----------------------------------------------------------------------------*/
1856/*----------------------------------------------------------------------------*/
1857cpl_error_code eris_ifu_set_qc_int(cpl_propertylist *pl,
1858 const char *name,
1859 int val,
1860 const char *comment)
1861{
1862 cpl_error_code retVal = CPL_ERROR_NONE;
1863 char *qcName = NULL;
1864
1865 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1866 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1867 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1868
1869 TRY
1870 {
1871 qcName = cpl_sprintf("ESO QC %s", name);
1872 cpl_propertylist_set_int(pl, qcName, val);
1873 cpl_propertylist_set_comment(pl, qcName, comment);
1874 }
1875 CATCH
1876 {
1877 retVal = cpl_error_get_code();
1878 }
1879
1880 eris_ifu_free_string(&qcName);
1881 eris_check_error_code("eris_ifu_append_qc_int");
1882 return retVal;
1883}
1884
1885/*----------------------------------------------------------------------------*/
1898/*----------------------------------------------------------------------------*/
1899bool eris_ifu_frame_is_on(const cpl_frame *fr)
1900{
1901 bool result = false;
1902 cpl_propertylist *pl = NULL;
1903 const char *fn = NULL;
1904 char *dpr_type = NULL;
1905
1906 cpl_ensure(fr != NULL, CPL_ERROR_NULL_INPUT, false);
1907
1908 TRY
1909 {
1910 fn = cpl_frame_get_filename(fr);
1912
1913 pl = cpl_propertylist_load(fn, 0);
1914
1915 if (cpl_propertylist_has(pl, FHDR_DPR_TYPE)) {
1916 dpr_type = cpl_strdup(
1917 cpl_propertylist_get_string(pl, FHDR_DPR_TYPE));
1918 } else {
1919 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
1920 "keyword %s does not exist",
1921 FHDR_DPR_TYPE);
1922 }
1923
1924 if ((strstr(dpr_type, "STD") != NULL) ||
1925 (strstr(dpr_type, "PSF") != NULL) ||
1926 (strstr(dpr_type, "SKY") != NULL) ||
1927 (strstr(dpr_type, "OBJECT") != NULL))
1928 {
1929 result = true;
1930 } else {
1931 if (eris_ifu_get_callamp_status(pl) != 0)
1932 {
1933 result = true;
1934 }
1935 }
1936 }
1937 CATCH
1938 {
1939 result = false;
1940 }
1941
1943 eris_ifu_free_string(&dpr_type);
1944 eris_check_error_code("eris_ifu_frame_is_on");
1945 return result;
1946}
1947
1948/*----------------------------------------------------------------------------*/
1959/*----------------------------------------------------------------------------*/
1960bool eris_ifu_frame_is_sky(const cpl_frame *fr)
1961{
1962 bool result = false;
1963 cpl_propertylist *pl = NULL;
1964 const char *fn = NULL;
1965 char *dpr_type = NULL;
1966
1967 cpl_ensure(fr, CPL_ERROR_NULL_INPUT, false);
1968
1969 TRY
1970 {
1971 fn = cpl_frame_get_filename(fr);
1973 pl = cpl_propertylist_load(fn, 0);
1974
1975
1976 if (cpl_propertylist_has(pl, FHDR_DPR_TYPE)) {
1977 dpr_type = cpl_strdup(
1978 cpl_propertylist_get_string(pl, FHDR_DPR_TYPE));
1979 } else {
1980 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
1981 "keyword %s does not exist",
1982 FHDR_DPR_TYPE);
1983 // go to catch clause
1984 }
1985
1986 if (strstr(dpr_type, ERIS_IFU_RAW_SKY) != NULL) {
1987 result = true;
1988 }
1989 }
1990 CATCH
1991 {
1992 result = false;
1993 }
1994
1996 eris_ifu_free_string(&dpr_type);
1997 eris_check_error_code("eris_ifu_frame_is_sky");
1998 return result;
1999}
2000
2001/*----------------------------------------------------------------------------*/
2012/*----------------------------------------------------------------------------*/
2013float eris_ifu_get_dit(cpl_propertylist *header) {
2014 float dit = 0;
2015 ifsInstrument instrument = UNSET_INSTRUMENT;
2016
2017 cpl_ensure(header, CPL_ERROR_NULL_INPUT, 0);
2018
2019 TRY
2020 {
2021 instrument = eris_ifu_get_instrument(header);
2023 if (instrument == SPIFFIER) {
2024 dit = cpl_propertylist_get_float(header, FHDR_E_DIT);
2025
2026 } else if (instrument == SPIFFI) {
2027 dit = cpl_propertylist_get_float(header, FHDR_S_DIT);
2028 }
2030 }
2031 CATCH
2032 {
2033 dit = 0;
2034 }
2035 eris_check_error_code("eris_ifu_get_dit");
2036 return dit;
2037}
2038
2053int eris_ifu_get_callamp_status(cpl_propertylist *header)
2054{
2055 ifsInstrument instrument = UNSET_INSTRUMENT;
2056 int lampMask = 0;
2057 int lampAr = 0;
2058 int lampKr = 0;
2059 int lampNe = 0;
2060 int lampXe = 0;
2061 int lampQth = 0;
2062
2063 cpl_ensure_code(header, CPL_ERROR_NULL_INPUT);
2064
2065 TRY
2066 {
2067 instrument = eris_ifu_get_instrument(header);
2069 if (instrument == SPIFFIER) {
2070 cpl_msg_debug("eris_ifu_get_callamp_status", "ERIS/SPIFFIER found");
2071 if (cpl_propertylist_has(header, FHDR_E_AR_LAMP_ST)) {
2072 if (cpl_propertylist_get_bool(header, FHDR_E_AR_LAMP_ST)) {
2073 lampMask += AR_LAMP;
2074 lampAr = 1;
2075 }
2076 }
2077 if (cpl_propertylist_has(header, FHDR_E_KR_LAMP_ST)) {
2078 if (cpl_propertylist_get_bool(header, FHDR_E_KR_LAMP_ST)) {
2079 lampMask += KR_LAMP;
2080 lampKr = 1;
2081 }
2082 }
2083 if (cpl_propertylist_has(header, FHDR_E_NE_LAMP_ST)) {
2084 if (cpl_propertylist_get_bool(header, FHDR_E_NE_LAMP_ST)) {
2085 lampMask += NE_LAMP;
2086 lampNe = 1;
2087 }
2088 }
2089 if (cpl_propertylist_has(header, FHDR_E_XE_LAMP_ST)) {
2090 if (cpl_propertylist_get_bool(header, FHDR_E_XE_LAMP_ST)) {
2091 lampMask += XE_LAMP;
2092 lampXe = 1;
2093 }
2094 }
2095 if (cpl_propertylist_has(header, FHDR_E_QTH_LAMP_ST)) {
2096 if (cpl_propertylist_get_bool(header,FHDR_E_QTH_LAMP_ST)) {
2097 lampMask += QTH_LAMP;
2098 lampQth = 1;
2099 }
2100 }
2101 } else if (instrument == SPIFFI) {
2102 cpl_msg_debug("eris_ifu_get_callamp_status", "SINFONI/SPIFFI found");
2103 if (cpl_propertylist_get_bool(header,FHDR_S_AR_LAMP_ST)) {
2104 lampMask += AR_LAMP;
2105 lampAr = 1;
2106 }
2107 if (cpl_propertylist_get_bool(header,FHDR_S_KR_LAMP_ST)) {
2108 lampMask += KR_LAMP;
2109 lampKr = 1;
2110 }
2111 if (cpl_propertylist_get_bool(header,FHDR_S_NE_LAMP_ST)) {
2112 lampMask += NE_LAMP;
2113 lampNe = 1;
2114 }
2115 if (cpl_propertylist_get_bool(header,FHDR_S_XE_LAMP_ST)) {
2116 lampMask += XE_LAMP;
2117 lampXe = 1;
2118 }
2119 if (cpl_propertylist_get_bool(header,FHDR_S_QTH_LAMP_ST)) {
2120 lampMask += QTH_LAMP;
2121 lampQth = 1;
2122 }
2123 }
2124 }
2125 CATCH
2126 {
2127 lampMask = 0;
2128 }
2129 cpl_msg_debug(cpl_func,
2130 "Lamps: Ar: %d, Kr: %d, Ne: %d, Xe: %d, Qth: %d, mask: %d",
2131 lampAr, lampKr, lampNe, lampXe, lampQth, lampMask);
2132 eris_check_error_code("eris_ifu_get_callamp_status");
2133 return lampMask;
2134}
2135/*----------------------------------------------------------------------------*/
2141ifsBand eris_ifu_get_band(const cpl_propertylist *header)
2142{
2143 ifsBand band = UNDEFINED_BAND;
2144 ifsInstrument instrument = UNSET_INSTRUMENT;
2145 const char *bandId = NULL;
2146
2147 TRY
2148 {
2149 if (header == NULL) {
2150 BRK_WITH_ERROR(CPL_ERROR_NULL_INPUT);
2151 }
2152
2153 instrument = eris_ifu_get_instrument(header);
2155 if (instrument == SPIFFIER) {
2156 bandId = cpl_propertylist_get_string(header, FHDR_E_GRATING_ID);
2158 if (strcmp(bandId, E_GRATING_J_LOW) == 0) {
2159 band = J_LOW;
2160 } else if (strcmp(bandId, E_GRATING_J_SHORT) == 0) {
2161 band = J_SHORT;
2162 } else if (strcmp(bandId, E_GRATING_J_MIDDLE) == 0) {
2163 band = J_MIDDLE;
2164 } else if (strcmp(bandId, E_GRATING_J_LONG) == 0) {
2165 band = J_LONG;
2166 } else if (strcmp(bandId, E_GRATING_H_LOW) == 0) {
2167 band = H_LOW;
2168 } else if (strcmp(bandId, E_GRATING_H_SHORT) == 0) {
2169 band = H_SHORT;
2170 } else if (strcmp(bandId, E_GRATING_H_MIDDLE) == 0) {
2171 band = H_MIDDLE;
2172 } else if (strcmp(bandId, E_GRATING_H_LONG) == 0) {
2173 band = H_LONG;
2174 } else if (strcmp(bandId, E_GRATING_K_LOW) == 0) {
2175 band = K_LOW;
2176 } else if (strcmp(bandId, E_GRATING_K_SHORT) == 0) {
2177 band = K_SHORT;
2178 } else if (strcmp(bandId, E_GRATING_K_MIDDLE) == 0) {
2179 band = K_MIDDLE;
2180 } else if (strcmp(bandId, E_GRATING_K_LONG) == 0) {
2181 band = K_LONG;
2182 } else if (strcmp(bandId, E_PREOPTICS_PUPIL) == 0) {
2183 cpl_msg_warning(cpl_func,"found pupil data");
2184 band = B_PUPIL;
2185 }
2186 } else if (instrument == SPIFFI) {
2187 bandId = cpl_propertylist_get_string(header, FHDR_S_GRATING_ID);
2189 if (strcmp(bandId, S_GRATING_J_BAND) == 0) {
2190 band = J_SPIFFI;
2191 } else if (strcmp(bandId, S_GRATING_H_BAND) == 0) {
2192 band = H_SPIFFI;
2193 } else if (strcmp(bandId, S_GRATING_K_BAND) == 0) {
2194 band = K_SPIFFI;
2195 } else if (strcmp(bandId, S_GRATING_HK_BAND) == 0) {
2196 band = HK_SPIFFI;
2197 } else {
2198 band = UNDEFINED_BAND;
2199 }
2200 }
2201
2202 }CATCH
2203 {
2204 band = UNDEFINED_BAND;
2205 }
2206 eris_check_error_code("eris_ifu_get_band");
2207 return band;
2208}
2209
2220ifsInstrument eris_ifu_get_instrument(const cpl_propertylist *header)
2221{
2222 ifsInstrument instrument = UNSET_INSTRUMENT;
2223 const char *instrumentStr = NULL;
2224 const char *armStr = NULL;
2225
2226 TRY
2227 {
2228 if (header == NULL) {
2229 BRK_WITH_ERROR(CPL_ERROR_NULL_INPUT);
2230 }
2231
2232 instrumentStr = cpl_propertylist_get_string(header, FHDR_INSTRUMENT);
2234 "Cannot read "FHDR_INSTRUMENT" FITS keyword");
2235 if (strcmp(instrumentStr, INSTRUMENT_SINFONI) == 0) {
2236 instrument = SPIFFI;
2237 } else if (strcmp(instrumentStr, INSTRUMENT_ERIS) == 0) {
2238 armStr = cpl_propertylist_get_string(header, FHDR_E_ARM);
2240 "Cannot read "FHDR_E_ARM" FITS keyword");
2241 if (strcmp(armStr, ARM_SPIFFIER) == 0) {
2242 instrument = SPIFFIER;
2243 } else if (strcmp(armStr, ARM_NIX) == 0) {
2244 cpl_msg_error(cpl_func, "ERIS/NIX frame detected");
2245 SET_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
2246 "ERIS/NIX frame detected");
2247 instrument = NIX;
2248 } else {
2249 cpl_msg_error("",
2250 "Unkown instrument arm %s, neither SPIFFIER nor NIX",
2251 armStr);
2252 SET_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
2253 "Unkown instrument arm, neither SPIFFIER nor NIX");
2254 instrument = OTHER_INSTRUMENT;
2255 }
2256 } else {
2257 cpl_msg_error("",
2258 "Unkown instrument %s, neither ERIS/SPIFFIER nor SINFONI",
2259 instrumentStr);
2260 SET_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
2261 "Unkown instrument, neither ERIS/SPIFFIER nor SINFONI");
2262 instrument = OTHER_INSTRUMENT;
2263 }
2264 }
2265 CATCH
2266 {
2267 instrument = UNSET_INSTRUMENT;
2268 }
2269 eris_check_error_code("eris_ifu_get_instrument");
2270 return instrument;
2271}
2272/*----------------------------------------------------------------------------*/
2278ifsPreopticsScale
2280{
2281
2282 ifsPreopticsScale scale = UNDEFINED_SCALE;
2283 if (strcmp(scaleId, E_PREOPTICS_250MAS_SCALE) == 0) {
2284 scale = S250MAS;
2285 } else if (strcmp(scaleId, E_PREOPTICS_100MAS_SCALE) == 0) {
2286 scale = S100MAS;
2287 } else if (strcmp(scaleId, E_PREOPTICS_25MAS_SCALE) == 0) {
2288 scale = S25MAS;
2289 } else if (strcmp(scaleId, E_PREOPTICS_PUPIL) == 0) {
2290 scale = PUPIL;
2291 } else {
2292 scale = UNDEFINED_SCALE;
2293 }
2294 eris_check_error_code("eris_ifu_get_spiffier_preoptics_scale");
2295 return scale;
2296}
2297/*----------------------------------------------------------------------------*/
2303ifsPreopticsScale
2305{
2306 ifsPreopticsScale scale = UNDEFINED_SCALE;
2307 if (strcmp(scaleId, S_PREOPTICS_250MAS_SCALE) == 0) {
2308 scale = S250MAS;
2309 } else if (strcmp(scaleId, S_PREOPTICS_100MAS_SCALE) == 0) {
2310 scale = S100MAS;
2311 } else if (strcmp(scaleId, S_PREOPTICS_25MAS_SCALE) == 0) {
2312 scale = S25MAS;
2313 } else if (strcmp(scaleId, S_PREOPTICS_PUPIL) == 0) {
2314 scale = PUPIL;
2315 } else {
2316 scale = UNDEFINED_SCALE;
2317 }
2318 eris_check_error_code("eris_ifu_get_spiffi_preoptics_scale");
2319 return scale;
2320}
2321
2322/*----------------------------------------------------------------------------*/
2335/*----------------------------------------------------------------------------*/
2337 cpl_propertylist *header)
2338{
2339 ifsPreopticsScale scale = UNDEFINED_SCALE;
2340 ifsInstrument instrument = UNSET_INSTRUMENT;
2341 const char *scaleId;
2342
2343 TRY
2344 {
2345 instrument = eris_ifu_get_instrument(header);
2347 if (instrument == SPIFFIER) {
2348 scaleId = cpl_propertylist_get_string(header, FHDR_E_PREOPTICS_ID);
2351 } else if (instrument == SPIFFI) {
2352 scaleId = cpl_propertylist_get_string(header, FHDR_S_PREOPTICS_ID);
2354 scale = eris_ifu_get_spiffi_preoptics_scale(scaleId);
2355 }
2356
2357 }
2358 CATCH
2359 {
2360 scale = UNDEFINED_SCALE;
2361 }
2362 eris_check_error_code("eris_ifu_get_preopticsScale");
2363 return scale;
2364
2365}
2366
2367#define KEY_NAME_COMOFFS_RA "ESO OCS CUMOFFS RA"
2368#define KEY_NAME_COMOFFS_DEC "ESO OCS CUMOFFS DEC"
2369
2370
2371/*----------------------------------------------------------------------------*/
2379/*----------------------------------------------------------------------------*/
2380
2381double eris_get_cumoffs_ra(cpl_frame * frame)
2382{
2383 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, -1.0);
2384 cpl_propertylist* plist=NULL;
2385 char* file=NULL;
2386
2387 double result=0.;
2388 file = cpl_strdup( cpl_frame_get_filename(frame)) ;
2389
2390 if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2391 cpl_msg_error(cpl_func, "getting header from reference frame %s",file);
2392 cpl_propertylist_delete(plist) ;
2393 cpl_free(file);
2394 return -1 ;
2395 }
2396
2397 if (cpl_propertylist_has(plist, KEY_NAME_COMOFFS_RA)) {
2398 result=cpl_propertylist_get_double(plist, KEY_NAME_COMOFFS_RA);
2399 } else {
2400 cpl_msg_error(cpl_func, "keyword %s does not exist",KEY_NAME_COMOFFS_RA);
2401 cpl_propertylist_delete(plist) ;
2402 cpl_free(file);
2403 return -1;
2404 }
2405 cpl_propertylist_delete(plist) ;
2406 cpl_free(file);
2407 eris_check_error_code("eris_get_cumoffs_ra");
2408 return result;
2409
2410}
2411
2412
2413/*----------------------------------------------------------------------------*/
2421/*----------------------------------------------------------------------------*/
2422
2423double eris_get_cumoffs_dec(cpl_frame * frame)
2424{
2425 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, -999.0);
2426 cpl_propertylist* plist=NULL;
2427 char* file=NULL;
2428
2429 double result=0.;
2430 file = cpl_strdup( cpl_frame_get_filename(frame)) ;
2431
2432 if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2433 cpl_msg_error(cpl_func, "getting header from reference frame %s",file);
2434 cpl_propertylist_delete(plist) ;
2435 cpl_free(file);
2436 return -1 ;
2437 }
2438
2439 if (cpl_propertylist_has(plist, KEY_NAME_COMOFFS_DEC)) {
2440 result=cpl_propertylist_get_double(plist, KEY_NAME_COMOFFS_DEC);
2441 } else {
2442 cpl_msg_error(cpl_func, "keyword %s does not exist",KEY_NAME_COMOFFS_DEC);
2443 cpl_propertylist_delete(plist) ;
2444 cpl_free(file);
2445 return -1;
2446 }
2447 cpl_propertylist_delete(plist) ;
2448 cpl_free(file);
2449 eris_check_error_code("eris_get_cumoffs_dec");
2450 return result;
2451
2452}
2453
2454/*----------------------------------------------------------------------------*/
2462/*----------------------------------------------------------------------------*/
2463double eris_get_mjd_obs(cpl_frame * frame)
2464{
2465 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, -1.0);
2466
2467 cpl_propertylist* plist=NULL;
2468 const char* file=NULL;
2469
2470 double mjd_obs=0.;
2471 file = cpl_frame_get_filename(frame) ;
2472
2473 if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2474 cpl_msg_error(cpl_func, "getting header from reference frame %s",file);
2475 cpl_propertylist_delete(plist) ;
2476 return -1.0;
2477 }
2478 if(cpl_propertylist_has(plist,"MJD-OBS")){
2479 mjd_obs = eris_pfits_get_mjdobs(plist);
2480 } else {
2481 cpl_propertylist_delete(plist) ;
2482 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
2483 return -1.0;
2484 }
2485
2486
2487 cpl_propertylist_delete(plist) ;
2488 eris_check_error_code("eris_get_mjd_obs");
2489 return mjd_obs;
2490
2491}
2492
2493/*---------------------------------------------------------------------------*/
2499/*---------------------------------------------------------------------------*/
2500
2501cpl_boolean eris_ifu_tag_is_cdb(char *tag)
2502{
2503 cpl_boolean result = CPL_FALSE;
2504
2505 if (tag == NULL) {
2506 return result;
2507 }
2508
2509 if ((strstr(ERIS_IFU_PRO_CALIB_TAGS, tag) != NULL) ||
2510 (strstr(ERIS_IFU_REF_CALIB_TAGS, tag) != NULL))
2511 {
2512 result = CPL_TRUE;
2513 }
2514
2515 return result;
2516}
2517
2518/*---------------------------------------------------------------------------*/
2524/*---------------------------------------------------------------------------*/
2525
2526cpl_boolean eris_ifu_tag_is_sky(const char *tag)
2527{
2528 cpl_boolean result = CPL_FALSE;
2529
2530 if (tag == NULL) {
2531 return result;
2532 }
2533
2534 char* tag_loc = (char*) tag;
2535 if ((strcmp(tag_loc, ERIS_IFU_RAW_SKY) == 0) ||
2536 (strcmp(tag_loc, ERIS_IFU_RAW_OBJ_SKY) == 0) ||
2537 (strcmp(tag_loc, ERIS_IFU_RAW_STD_FLUX_SKY) == 0) ||
2538 (strcmp(tag_loc, ERIS_IFU_RAW_STD_SKY) == 0) ||
2539 (strcmp(tag_loc, ERIS_IFU_RAW_PSF_SKY) == 0) ||
2540 (strcmp(tag_loc, ERIS_IFU_RAW_PUPIL_SKY) == 0))
2541 {
2542 result = CPL_TRUE;
2543 }
2544
2545 return result;
2546}
2547
2553cpl_boolean eris_ifu_tag_is_obj(const char *tag)
2554{
2555 cpl_boolean result = CPL_FALSE;
2556
2557 if (tag == NULL) {
2558 return result;
2559 }
2560
2561 char* tag_loc = (char*) tag;
2562 if ((strcmp(tag_loc, ERIS_IFU_RAW_OBJ) == 0) ||
2563 (strcmp(tag_loc, ERIS_IFU_RAW_STD) == 0) ||
2564 (strcmp(tag_loc, ERIS_IFU_RAW_STD_FLUX) == 0) ||
2565 (strcmp(tag_loc, ERIS_IFU_RAW_PSF) == 0) ||
2566 (strcmp(tag_loc, ERIS_IFU_RAW_PUPIL_LAMP) == 0))
2567 {
2568 result = CPL_TRUE;
2569 }
2570
2571 return result;
2572}
2573
2580cpl_error_code
2581eris_ifu_extract_obj_frames(const cpl_frameset * sof, cpl_frameset* obj)
2582{
2583 cpl_ensure_code(sof, CPL_ERROR_NULL_INPUT);
2584 cpl_ensure_code(obj, CPL_ERROR_NULL_INPUT);
2585
2586 cpl_size nsof = cpl_frameset_get_size(sof);
2587
2588 for (cpl_size i = 0 ; i < nsof ; i++) {
2589 const cpl_frame* frame = cpl_frameset_get_position_const(sof, i);
2590 cpl_frame_group group = cpl_frame_get_group(frame);
2591
2592 if(group == CPL_FRAME_GROUP_RAW) {
2593
2594 //cpl_msg_info(cpl_func,"fname: %s", cpl_frame_get_filename(frame));
2595 const char* tag = cpl_frame_get_tag(frame);
2596
2597 if(eris_ifu_tag_is_obj(tag)) {
2598
2599 cpl_frame* frame_dup = cpl_frame_duplicate(frame);
2600 cpl_frameset_insert(obj, frame_dup);
2601
2602 }
2603 }
2604
2605 }
2606
2607 eris_check_error_code("eris_ifu_extract_obj_frames");
2608 return cpl_error_get_code();
2609}
2610
2611/*---------------------------------------------------------------------------*/
2618/*---------------------------------------------------------------------------*/
2619cpl_error_code
2620eris_ifu_extract_sky_frames(const cpl_frameset * sof, cpl_frameset* sky)
2621{
2622 cpl_ensure_code(sof, CPL_ERROR_NULL_INPUT);
2623 cpl_ensure_code(sky, CPL_ERROR_NULL_INPUT);
2624
2625 cpl_size nsof = cpl_frameset_get_size(sof);
2626 for (cpl_size i = 0 ; i < nsof ; i++) {
2627 const cpl_frame* frame = cpl_frameset_get_position_const(sof, i);
2628 cpl_frame_group group = cpl_frame_get_group(frame);
2629
2630 if(group == CPL_FRAME_GROUP_RAW) {
2631 //cpl_msg_info(cpl_func,"fname: %s", cpl_frame_get_filename(frame));
2632 const char* tag = cpl_frame_get_tag(frame);
2633 if(eris_ifu_tag_is_sky(tag)) {
2634 cpl_frame* frame_dup = cpl_frame_duplicate(frame);
2635 cpl_frameset_insert(sky, frame_dup);
2636 }
2637
2638 }
2639 }
2640
2641 eris_check_error_code("eris_ifu_extract_sky_frames");
2642 return cpl_error_get_code();
2643}
2644
2645/*---------------------------------------------------------------------------*/
2652/*---------------------------------------------------------------------------*/
2653cpl_error_code
2654eris_ifu_extract_mst_frames(const cpl_frameset * sof, cpl_frameset* cdb)
2655{
2656 cpl_ensure_code(sof, CPL_ERROR_NULL_INPUT);
2657 cpl_ensure_code(cdb, CPL_ERROR_NULL_INPUT);
2658
2659 cpl_size nsof = cpl_frameset_get_size(sof);
2660 for (cpl_size i = 0 ; i < nsof ; i++) {
2661 const cpl_frame* frame = cpl_frameset_get_position_const(sof,i);
2662 cpl_frame_group group = cpl_frame_get_group(frame);
2663
2664 if(group == CPL_FRAME_GROUP_CALIB) {
2665 /* to go on the file must exist */
2666 if(cpl_frame_get_tag(frame) != NULL) {
2667 /* If the frame has a tag we process it. Else it is an object */
2668 char* tag= (char*) cpl_frame_get_tag(frame);
2669 if(eris_ifu_tag_is_cdb(tag) == 1) {
2670 cpl_frame* frame_dup = cpl_frame_duplicate(frame);
2671 cpl_frameset_insert(cdb, frame_dup);
2672 }
2673 }
2674 }
2675 }
2676
2677 eris_check_error_code("eris_ifu_extract_mst_frames");
2678 return cpl_error_get_code();
2679}
2680
2681
cpl_error_code eris_ifu_load_deq_image(const char *filename, cpl_propertylist **primaryHeader, cpl_image **dataImage, cpl_image **errImage, cpl_image **qualImage, deqErrorType *errorType, deqQualityType *qualityType)
‍** Function to write a DFS compliant pipeline product with a badpixel map
Definition: eris_ifu_dfs.c:889
cpl_error_code eris_ifu_append_qc_float(cpl_propertylist *pl, const char *name, float val, const char *comment)
Append a QC parameter of type FLOAT to a property list.
ifsPreopticsScale eris_ifu_get_preopticsScale(cpl_propertylist *header)
Return the the pre-optics scaling.
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_bivector * eris_ifu_load_slit_positions(const char *filename)
Load slitlet position bivector from a table.
float eris_ifu_get_dit(cpl_propertylist *header)
Get the detector integration time (DIT) from FITS header.
cpl_error_code eris_ifu_heades_add_hduclass_qual(cpl_propertylist *plist, deqQualityType qualityType)
Add quality extension classification properties to FITS header.
Definition: eris_ifu_dfs.c:324
bool eris_ifu_frame_is_sky(const cpl_frame *fr)
Determine if a frame is a sky frame.
cpl_error_code eris_ifu_load_deq_imagelist(const char *filename, cpl_propertylist **primaryHeader, cpl_imagelist **dataImagelist, cpl_imagelist **errImagelist, cpl_imagelist **qualImagelist, deqErrorType *errorType, deqQualityType *qualityType)
Load a DEQ-format imagelist (cube) with data, error, and quality extensions.
ifsPreopticsScale eris_ifu_get_spiffier_preoptics_scale(const char *scaleId)
Determine observing scale on Sky.
cpl_error_code eris_ifu_extract_obj_frames(const cpl_frameset *sof, cpl_frameset *obj)
extract object raw frames from frame set and put them in a new one
cpl_error_code eris_ifu_load_distortion_polynomials(const char *filename, cpl_polynomial ***polynomials, cpl_table **borders)
Load distortion polynomials and slitlet borders from a table.
cpl_frameset * eris_ifu_extract_frameset(const cpl_frameset *in, const char *tag)
Extract frames with a specific tag from a frameset.
Definition: eris_ifu_dfs.c:201
cpl_error_code eris_ifu_save_image_phase3(cpl_frameset *allframes, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const char *recipe, const cpl_propertylist *applist, const char *remregexp, const char *filename, const cpl_image *image, cpl_type type, const char *unit)
Save a DFS-compliant Phase 3 image product.
Definition: eris_ifu_dfs.c:666
hdrl_image * eris_ifu_load_cal_image_frame(const cpl_frame *frame, ifsBand band, ifsPreopticsScale scale, cpl_image **qualImage, deqQualityType *qualType)
Load a calibration image from a frame.
double eris_get_mjd_obs(cpl_frame *frame)
find out the Julian Date of the observation
hdrl_image * eris_ifu_load_cal_image_file(const char *filename, ifsBand band, ifsPreopticsScale scale, cpl_image **qualImage, deqQualityType *qualType)
Load a calibration image from a filename.
ifsPreopticsScale eris_ifu_get_spiffi_preoptics_scale(const char *scaleId)
Determine observing scale on Sky.
ifsInstrument eris_ifu_get_instrument(const cpl_propertylist *header)
Return the used instrument of the FITS file header.
hdrl_imagelist * eris_ifu_load_deq_hdrl_imagelist(const char *filename, cpl_propertylist **primaryHeader, cpl_imagelist **qualImagelist, deqQualityType *qualityType)
Load a DEQ-format cube into an HDRL imagelist structure.
cpl_error_code eris_ifu_heades_add_hduclass_common(cpl_propertylist *plist, const char *deq_hduclas)
Add common HDU classification properties to FITS header.
Definition: eris_ifu_dfs.c:248
double eris_get_cumoffs_ra(cpl_frame *frame)
find out the ESO OCS CUMOFFS RA of the observation
cpl_boolean eris_ifu_tag_is_obj(const char *tag)
Add PRO* keywords to a SINFONI FITS header.
hdrl_image * eris_ifu_load_deq_hdrl_image(const char *filename, cpl_propertylist **primaryHeader, cpl_image **qualImage, deqQualityType *qualityType)
Load a DEQ-format file into an HDRL image structure.
cpl_error_code eris_ifu_dfs_set_groups(cpl_frameset *self)
Set the frame group (RAW, CALIB, or PRODUCT) for all frames in a frameset.
Definition: eris_ifu_dfs.c:89
double eris_get_cumoffs_dec(cpl_frame *frame)
find out the ESO OCS CUMOFFS DEC of the observation
cpl_boolean eris_ifu_tag_is_sky(const char *tag)
check if input frame tag is a static of master calibration
cpl_error_code eris_ifu_set_qc_int(cpl_propertylist *pl, const char *name, int val, const char *comment)
Set (or update) a QC parameter of type INT in a property list.
cpl_boolean eris_ifu_tag_is_cdb(char *tag)
check if input frame tag is a static of master calibration
cpl_error_code eris_ifu_save_deq_image(cpl_frameset *allframes, cpl_propertylist *header, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_frame *inherit, const char *recipe, const cpl_propertylist *applist, const char *remregexp, const char *filename, const cpl_image *image, const cpl_image *error, deqErrorType errorType, const cpl_image *dataQualityMask, deqQualityType dataQualityType, const char *unit)
Save a DFS-compliant image product with data, error, and quality extensions.
Definition: eris_ifu_dfs.c:367
int eris_ifu_get_callamp_status(cpl_propertylist *header)
Return a bit mask indicating which calibration lamps are turned on.
cpl_error_code eris_ifu_save_deq_cube(cpl_frameset *allframes, cpl_propertylist *header, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_frame *inherit, const char *recipe, cpl_propertylist *applist, const char *remregexp, const char *filename, cpl_imagelist *data, cpl_imagelist *error, deqErrorType errorType, const cpl_imagelist *dataQualityMask, deqQualityType qualityType)
Save a DFS-compliant cube product with data, error, and quality extensions.
Definition: eris_ifu_dfs.c:516
cpl_error_code eris_ifu_save_table(cpl_frameset *allframes, cpl_propertylist *header, const cpl_parameterlist *parlist, const cpl_frameset *usedframes, const cpl_frame *inherit, const char *recipe, const char *procatg, cpl_propertylist *applist, const char *remregexp, const char *filename, const cpl_table *table)
Save a DFS-compliant table product.
Definition: eris_ifu_dfs.c:745
cpl_error_code eris_ifu_heades_add_hduclass_data(cpl_propertylist *plist)
Add data extension classification properties to FITS header.
Definition: eris_ifu_dfs.c:273
cpl_vector * eris_ifu_load_distances(const char *filename)
Load slitlet distance vector from a table.
ifsBand eris_ifu_get_band(const cpl_propertylist *header)
Determine preoptic band.
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.
cpl_error_code eris_ifu_extract_sky_frames(const cpl_frameset *sof, cpl_frameset *sky)
extract sky raw frames from frame set and put them in a new one
cpl_error_code eris_ifu_heades_add_hduclass_errs(cpl_propertylist *plist, deqErrorType errorType)
Add error extension classification properties to FITS header.
Definition: eris_ifu_dfs.c:297
cpl_error_code eris_ifu_load_distortion_polynomials_old(const char *filename, cpl_polynomial **poly_u, cpl_polynomial **poly_v)
Load old-format distortion polynomials from a table.
cpl_error_code eris_ifu_extract_mst_frames(const cpl_frameset *sof, cpl_frameset *cdb)
extract master and ref frames from input set and put them in a new one
bool eris_ifu_frame_is_on(const cpl_frame *fr)
Determine if a frame has calibration lamps ON or OFF.
#define BRK_WITH_ERROR(code)
Set a new CPL error, and exit the try-block.
#define SET_ERROR_MSG(code, msg)
Set a new error code together with a custom error message.
#define BRK_IF_ERROR(function)
If function is or returns an error != CPL_ERROR_NONE, then the try-block is exited.
#define RECOVER(void)
Recover the error state which was present during TRY (at the beginning of the try-block).
#define CHECK_ERROR_STATE(void)
Check the CPL error state, and exit the try-block if not CPL_ERROR_NONE.
#define CHECK_ERROR_STATE_MSG(msg)
Check the CPL error state, and exit the try-block if not CPL_ERROR_NONE. An additional error message ...
#define BRK_WITH_ERROR_MSG(code,...)
Set a new CPL error, and exit the try-block.
#define SET_ERROR(code)
Set a new error code.
#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.
ifsInstrument eris_ifu_get_instrument_frame(cpl_frame *frame)
Get instrument identifier from a CPL frame.
void eris_ifu_free_propertylist(cpl_propertylist **item)
Free memory and set pointer to null.
void eris_ifu_free_string(char **item)
Free memory and set pointer to null.
void eris_ifu_free_table(cpl_table **item)
Free memory and set pointer to null.
cpl_error_code eris_ifu_file_exists(const char *filename)
‍**
void eris_ifu_free_image(cpl_image **item)
Free memory and set pointer to null.
int eris_ifu_is_nan_or_inf(double A)
Checks if a value is nan, inf or -inf.
double eris_pfits_get_mjdobs(const cpl_propertylist *aHeaders)
find out the Julian Date of the observation
Definition: eris_pfits.c:811
cpl_error_code eris_check_error_code(const char *func_id)
handle CPL errors
Definition: eris_utils.c:56
hdrl_image * hdrl_image_create(const cpl_image *image, const cpl_image *error)
create a new hdrl_image from to existing images by copying them
Definition: hdrl_image.c:295
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379
hdrl_imagelist * hdrl_imagelist_create(cpl_imagelist *imlist, cpl_imagelist *errlist)
Create an hdrl_imagelist out of 2 cpl_imagelist.
void hdrl_imagelist_delete(hdrl_imagelist *himlist)
Free all memory used by a hdrl_imagelist object including the images.