ERIS Pipeline Reference Manual 1.8.14
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/*----------------------------------------------------------------------------*/
52/*----------------------------------------------------------------------------*/
53
56/*----------------------------------------------------------------------------*/
62/*----------------------------------------------------------------------------*/
63cpl_error_code eris_ifu_dfs_set_groups(cpl_frameset * self)
64{
65 cpl_error_code retVal = CPL_ERROR_NONE;
66 const cpl_size n = cpl_frameset_get_size(self);
67 ifsInstrument instrument = UNSET_INSTRUMENT,
68 currInstrument = UNSET_INSTRUMENT;
69 cpl_frame *frame = NULL; /* Not allocated, just modified */
70 const char *tag = NULL;
71 char *stag = NULL;
72
73 cpl_ensure_code(self, CPL_ERROR_NULL_INPUT);
74
75 TRY
76 {
77 /* Loop on frames */
78 for (cpl_size i = 0; i < n; i++) {
80 frame = cpl_frameset_get_position(self, i));
81
82 tag = cpl_frame_get_tag(frame);
83 if (tag == NULL) {
84 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
85 "Frame %d of %d has no tag",
86 1 + (int)i, (int)n);
87 }
88 cpl_free(stag);
90 stag = cpl_sprintf(",%s,", tag));
91
92 if (strstr(ERIS_IFU_RAW_TAGS, stag) != NULL) {
93 /* SPIFFIER RAW frames */
95 cpl_frame_set_group(frame, CPL_FRAME_GROUP_RAW));
96
97 // check if all frames are from the same instrument
98
99 if (instrument == UNSET_INSTRUMENT) {
100 instrument = eris_ifu_get_instrument_frame(frame);
101 } else {
102 currInstrument = eris_ifu_get_instrument_frame(frame);
103 if (currInstrument != instrument) {
105 CPL_ERROR_BAD_FILE_FORMAT,
106 "SOF input files are from "
107 "different instruments");
108 }
109 }
110
111 } else if (strstr(ERIS_IFU_REF_CALIB_TAGS, stag) != NULL) {
112 /* SPIFFIER reference CALIB frames */
114 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB));
115 } else if (strstr(ERIS_IFU_REF_UTIL_TAGS, stag) != NULL) {
116 /* SPIFFIER utility frames */
118 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB));
119 } else if (strstr(ERIS_IFU_PRO_CALIB_TAGS, stag) != NULL) {
120 /* SPIFFIER product CALIB frames */
122 cpl_frame_set_group(frame, CPL_FRAME_GROUP_CALIB));
123 } else if (strstr(ERIS_IFU_PRO_JITTER_TAGS, stag) != NULL) {
124 /* SPIFFIER product Jitter recipe frames */
126 cpl_frame_set_group(frame, CPL_FRAME_GROUP_PRODUCT));
127
128 // check if all frames are from the same instrument
129 if (instrument == UNSET_INSTRUMENT) {
130 instrument = eris_ifu_get_instrument_frame(frame);
131 } else {
132 currInstrument = eris_ifu_get_instrument_frame(frame);
133 if (currInstrument != instrument) {
135 CPL_ERROR_BAD_FILE_FORMAT,
136 "SOF input files are from "
137 "different instruments");
138 }
139 }
140
141 } else {
142 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
143 "Unkown tag \"%s\" for frame %d of %d",
144 cpl_frame_get_tag(frame), 1 + (int)i, (int)n);
145 }
146
147 }
148 }
149 CATCH
150 {
151 retVal = cpl_error_get_code();
152 }
153
154 cpl_free(stag);
155 eris_check_error_code("eris_ifu_dfs_set_groups");
156 return retVal;
157}
158
159
160/*----------------------------------------------------------------------------*/
169/*----------------------------------------------------------------------------*/
170cpl_frameset *
172 const cpl_frameset * in,
173 const char * tag)
174{
175 cpl_frameset * out;
176 cpl_frame * loc_frame;
177 int nbframes;
178 int i ;
179
180 /* Test entries */
181 if (in == NULL) return NULL ;
182 if (tag == NULL) return NULL ;
183
184 /* Initialise */
185 nbframes = cpl_frameset_get_size(in) ;
186
187 /* Count the frames with the tag */
188 if (cpl_frameset_count_tags(in, tag) == 0) return NULL ;
189
190 /* Create the output frameset */
191 out = cpl_frameset_new() ;
192
193 /* Loop on the requested frames and store them in out */
194 for (i=0 ; i<nbframes ; i++) {
195 const cpl_frame *cur_frame = cpl_frameset_get_position_const(in, i);
196 if (!strcmp(cpl_frame_get_tag(cur_frame), tag)) {
197 loc_frame = cpl_frame_duplicate(cur_frame) ;
198 cpl_frameset_insert(out, loc_frame) ;
199 }
200 }
201 eris_check_error_code("eris_ifu_extract_frameset");
202 return out ;
203}
204
211cpl_error_code eris_ifu_heades_add_hduclass_common(cpl_propertylist* plist,
212 const char* deq_hduclas)
213{
214 cpl_propertylist_append_string(plist, FHDR_HDUCLASS, DEQ_HDUCLASS);
215 cpl_propertylist_append_string(plist, FHDR_HDUDOC, DEQ_HDUDOC);
216 cpl_propertylist_append_string(plist, FHDR_HDUVERS, DEQ_HDUVERS);
217 cpl_propertylist_append_string(plist, FHDR_HDUCLAS1, deq_hduclas);
218 eris_check_error_code("eris_ifu_heades_add_hduclass_common");
219 return cpl_error_get_code();
220}
221
222
223
229cpl_error_code eris_ifu_heades_add_hduclass_data(cpl_propertylist* plist)
230{
231 cpl_propertylist_append_string(plist, FHDR_HDUCLAS2, DEQ_HDUCLAS2_DATA);
232 cpl_propertylist_update_string(plist, FHDR_EXTNAME, DEQ_SCIDATA);
233 cpl_propertylist_append_string(plist, FHDR_ERRDATA, DEQ_ERRDATA);
234 cpl_propertylist_append_string(plist, FHDR_QUALDATA, DEQ_QUALDATA);
235 eris_check_error_code("eris_ifu_heades_add_hduclass_data");
236 return cpl_error_get_code();
237}
238
239
246cpl_error_code eris_ifu_heades_add_hduclass_errs(cpl_propertylist* plist,
247 deqErrorType errorType)
248{
249 cpl_propertylist_update_string(plist, FHDR_HDUCLAS2, DEQ_HDUCLAS2_ERROR);
250 cpl_propertylist_update_string(plist, FHDR_HDUCLAS3,
251 deqErrorTypeString[errorType]);
252 cpl_propertylist_update_string(plist, FHDR_EXTNAME, DEQ_ERRDATA);
253 cpl_propertylist_update_string(plist, FHDR_SCIDATA, DEQ_SCIDATA);
254 cpl_propertylist_update_string(plist, FHDR_QUALDATA, DEQ_QUALDATA);
255 eris_check_error_code("eris_ifu_heades_add_hduclass_data");
256 return cpl_error_get_code();
257}
258
265cpl_error_code eris_ifu_heades_add_hduclass_qual(cpl_propertylist* plist,
266 deqQualityType qualityType){
267 cpl_propertylist_update_string(plist, FHDR_HDUCLAS2, DEQ_HDUCLAS2_QUAL);
268 cpl_propertylist_update_string(plist, FHDR_HDUCLAS3,
269 deqQualityTypeString[qualityType]);
270 cpl_propertylist_update_string(plist, FHDR_EXTNAME, DEQ_QUALDATA);
271 cpl_propertylist_update_string(plist, FHDR_SCIDATA, DEQ_SCIDATA);
272 cpl_propertylist_update_string(plist, FHDR_ERRDATA, DEQ_ERRDATA);
273 eris_check_error_code("eris_ifu_heades_add_hduclass_qual");
274 return cpl_error_get_code();
275}
276
277
301 cpl_frameset *allframes,
302 cpl_propertylist * header,
303 const cpl_parameterlist *parlist,
304 const cpl_frameset *usedframes,
305 const cpl_frame *inherit,
306 const char *recipe,
307 const cpl_propertylist *applist,
308 const char *remregexp,
309 const char *filename,
310 const cpl_image *image,
311 const cpl_image *error,
312 deqErrorType errorType,
313 const cpl_image *dataQualityMask,
314 deqQualityType dataQualityType,
315 const char* unit)
316{
317 cpl_error_code retVal = CPL_ERROR_NONE;
318 cpl_propertylist *dataHeader = NULL,
319 *errsHeader = NULL,
320 *qualHeader = NULL;
321 char *pipe_id = NULL;
322
323 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
324 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
325 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
326 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
327 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
328 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
329 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
330
331
332 //cpl_ensure_code(error, CPL_ERROR_NULL_INPUT);
333 //cpl_ensure_code(dataQualityMask, CPL_ERROR_NULL_INPUT);
334 cpl_propertylist* head_wcs_2d = NULL;
335 TRY
336 {
337
338 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
339 dataHeader = cpl_propertylist_new();
340 eris_ifu_heades_add_hduclass_common(dataHeader, DEQ_HDUCLAS1);
342 head_wcs_2d = eris_ifu_plist_extract_wcs2D(applist);
343 /* remove WCS from primary header as data go into extensions */
344 eris_ifu_plist_erase_wcs( (cpl_propertylist *) applist);
345 if(cpl_propertylist_has( (cpl_propertylist *) applist,"BUNIT")) {
346 cpl_propertylist_erase((cpl_propertylist *) applist, "BUNIT");
347 }
348 if(cpl_propertylist_has( (cpl_propertylist *) applist,"EXTNAME")) {
349 cpl_propertylist_erase( (cpl_propertylist *)applist,"EXTNAME");
350 }
351 if(cpl_propertylist_has( (cpl_propertylist *) applist,"HDUCLAS2")) {
352 cpl_propertylist_erase( (cpl_propertylist *)applist,"HDUCLAS2");
353 }
354 if(cpl_propertylist_has( (cpl_propertylist *) applist,"COMMENT")) {
355 cpl_propertylist_erase( (cpl_propertylist *)applist,"COMMENT");
356 }
357 /* Create an empty primary HDU with only the header information*/
358 cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
359 inherit, recipe, applist, remregexp, pipe_id, filename);
360 cpl_propertylist_append(dataHeader, head_wcs_2d);
361
362 /* Write the extension units */
363 if(cpl_propertylist_has(dataHeader,"BUNIT")) {
364 cpl_propertylist_set_string(dataHeader,"BUNIT", unit);
365 } else {
366 cpl_propertylist_append_string(dataHeader,"BUNIT", unit);
367 }
368 eris_pfits_clean_header_ra_dec_mjd_obs(dataHeader);
369 cpl_image_save(image, filename, cpl_image_get_type(image), dataHeader,
370 CPL_IO_EXTEND);
371
372 if(error) {
373 errsHeader = cpl_propertylist_new();
374 eris_ifu_heades_add_hduclass_common(errsHeader, DEQ_HDUCLAS1);
375 eris_ifu_heades_add_hduclass_errs(errsHeader, errorType);
376
377 if(cpl_propertylist_has(errsHeader,"BUNIT")) {
378 cpl_propertylist_set_string(errsHeader,"BUNIT", unit);
379 } else {
380 cpl_propertylist_append_string(errsHeader,"BUNIT", unit);
381 }
382 cpl_propertylist_append(errsHeader, head_wcs_2d);
383 eris_pfits_clean_header_ra_dec_mjd_obs(errsHeader);
384 cpl_image_save(error, filename, cpl_image_get_type(error), errsHeader,
385 CPL_IO_EXTEND);
386 }
387
388 if(dataQualityMask) {
389 qualHeader = cpl_propertylist_new();
390 eris_ifu_heades_add_hduclass_common(qualHeader, DEQ_HDUCLAS1);
391 eris_ifu_heades_add_hduclass_qual(qualHeader, dataQualityType);
392
393 if(cpl_propertylist_has(qualHeader,"BUNIT")) {
394 cpl_propertylist_set_string(qualHeader,"BUNIT", "");
395 } else {
396 cpl_propertylist_append_string(qualHeader,"BUNIT", "");
397 }
398 cpl_propertylist_append(qualHeader, head_wcs_2d);
399 eris_pfits_clean_header_ra_dec_mjd_obs(qualHeader);
400 cpl_image_save(dataQualityMask, filename,
401 cpl_image_get_type(dataQualityMask), qualHeader, CPL_IO_EXTEND);
402 }
403
404 }
405 CATCH
406 {
407 retVal = cpl_error_get_code();
408 }
409 eris_ifu_free_propertylist(&dataHeader);
410 eris_ifu_free_propertylist(&errsHeader);
411 eris_ifu_free_propertylist(&qualHeader);
412 eris_ifu_free_propertylist(&head_wcs_2d);
413 eris_ifu_free_string(&pipe_id);
414 eris_check_error_code("eris_ifu_save_deq_image");
415 return retVal;
416}
417
418
419
442 cpl_frameset *allframes,
443 cpl_propertylist * header,
444 const cpl_parameterlist *parlist,
445 const cpl_frameset *usedframes,
446 const cpl_frame *inherit,
447 const char *recipe,
448 cpl_propertylist *applist,
449 const char *remregexp,
450 const char *filename,
451 cpl_imagelist *data,
452 cpl_imagelist *error,
453 deqErrorType errorType,
454 const cpl_imagelist *dataQualityMask,
455 deqQualityType qualityType/*,
456 cpl_type imageType*/)
457{
458 cpl_error_code retVal = CPL_ERROR_NONE;
459 cpl_propertylist *dataHeader = NULL,
460 *errsHeader = NULL,
461 *qualHeader = NULL;
462 char *pipe_id = NULL;
463
464 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
465 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
466 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
467 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
468 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
469 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
470 cpl_ensure_code(data, CPL_ERROR_NULL_INPUT);
471 cpl_ensure_code(error, CPL_ERROR_NULL_INPUT);
472 cpl_ensure_code(dataQualityMask, CPL_ERROR_NULL_INPUT);
473
474 TRY
475 {
476
477 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
478
479 /* Create FITS headers for the extensions */
480 if(cpl_propertylist_has(applist,"CDELT3")) {
481 cpl_propertylist_erase(applist, "CDELT3");
482 }
483 dataHeader = cpl_propertylist_duplicate(applist);
484 errsHeader = cpl_propertylist_duplicate(applist);
485 qualHeader = cpl_propertylist_duplicate(applist);
486
487 eris_ifu_heades_add_hduclass_common(dataHeader, "IMAGE");
489 eris_ifu_heades_add_hduclass_common(errsHeader, "IMAGE");
490 eris_ifu_heades_add_hduclass_errs(errsHeader, errorType);
491 eris_ifu_heades_add_hduclass_common(qualHeader, "IMAGE");
492 eris_ifu_heades_add_hduclass_qual(qualHeader, qualityType);
493
494 if(cpl_propertylist_has(dataHeader,"HDRVER")) {
495 cpl_propertylist_erase(dataHeader,"HDRVER");
496 }
497 if(cpl_propertylist_has(errsHeader,"HDRVER")) {
498 cpl_propertylist_erase(errsHeader,"HDRVER");
499 }
500 if(cpl_propertylist_has(qualHeader,"HDRVER")) {
501 cpl_propertylist_erase(qualHeader,"HDRVER");
502 }
503 if(cpl_propertylist_has(dataHeader,"CTYPE1")) {
504 const char* ctype1 = cpl_propertylist_get_string(dataHeader,"CTYPE1");
505 if (!cpl_propertylist_has(errsHeader,"CTYPE1"))
506 cpl_propertylist_append_string(errsHeader, "CTYPE1", ctype1);
507 if (!cpl_propertylist_has(qualHeader,"CTYPE1"))
508 cpl_propertylist_append_string(qualHeader, "CTYPE1", ctype1);
509 }
510 if(cpl_propertylist_has(dataHeader,"CTYPE2")) {
511 const char* ctype2 = cpl_propertylist_get_string(dataHeader,"CTYPE2");
512 if (!cpl_propertylist_has(errsHeader,"CTYPE2"))
513 cpl_propertylist_append_string(errsHeader, "CTYPE2",ctype2);
514 if (!cpl_propertylist_has(errsHeader,"CTYPE2"))
515 cpl_propertylist_append_string(qualHeader, "CTYPE2", ctype2);
516 }
517
518 /* Create an empty primary HDU with only the header information*/
519 eris_ifu_plist_erase_wcs( (cpl_propertylist *)applist);
520 eris_pfits_clean_header(dataHeader, CPL_TRUE);
521 eris_pfits_clean_header(errsHeader, CPL_TRUE);
522 eris_pfits_clean_header(qualHeader, CPL_TRUE);
523 if(strstr(filename, "sky") != NULL) {
524 cpl_propertylist_append_string(applist, "PRODCATG", PRODCATG_CUBE_SKY);
525 } else {
526 cpl_propertylist_append_string(applist, "PRODCATG", PRODCATG_CUBE_OBJ);
527 }
528 if(cpl_propertylist_has(applist,"BUNIT")) {
529 cpl_propertylist_erase(applist,"BUNIT");
530 }
531 if(cpl_propertylist_has(applist,"COMMENT")) {
532 cpl_propertylist_erase(applist,"COMMENT");
533 }
534
535
536 cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
537 inherit, recipe, applist, remregexp,
538 pipe_id, filename);
539
540 /* Write the extension units */
541 cpl_imagelist_save(data, filename,
542 cpl_image_get_type(cpl_imagelist_get(data,0)),
543 dataHeader, CPL_IO_EXTEND);
544
545 cpl_imagelist_save(error, filename,
546 cpl_image_get_type(cpl_imagelist_get(error,0)),
547 errsHeader, CPL_IO_EXTEND);
548 if(cpl_propertylist_has(qualHeader,"BUNIT")) {
549 cpl_propertylist_set_string(qualHeader,"BUNIT","");
550 } else {
551 cpl_propertylist_append_string(qualHeader,"BUNIT","");
552 }
553 cpl_imagelist_save(dataQualityMask, filename,
554 CPL_TYPE_INT,
555 qualHeader, CPL_IO_EXTEND);
556 }
557 CATCH
558 {
559 retVal = cpl_error_get_code();
560 }
561 cpl_propertylist_delete(dataHeader);
562 cpl_propertylist_delete(errsHeader);
563 cpl_propertylist_delete(qualHeader);
564 cpl_free(pipe_id);
565 eris_check_error_code("eris_ifu_save_deq_cube");
566 return retVal;
567}
568
587 cpl_frameset *allframes,
588 const cpl_parameterlist *parlist,
589 const cpl_frameset *usedframes,
590 const char *recipe,
591 const cpl_propertylist *applist,
592 const char *remregexp,
593 const char *filename,
594 const cpl_image *image,
595 cpl_type type,
596 const char* unit)
597{
598 cpl_error_code retVal = CPL_ERROR_NONE;
599 cpl_propertylist *dataHeader = NULL;
600 char *pipe_id = NULL;
601
602 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
603 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
604 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
605 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
606 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
607 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
608 cpl_ensure_code(image, CPL_ERROR_NULL_INPUT);
609
610 cpl_propertylist* head_wcs_2d = NULL;
611 TRY
612 {
613
614 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
615 dataHeader = cpl_propertylist_new();
616
617 if(cpl_propertylist_has( (cpl_propertylist *) applist,"EXTNAME")) {
618 cpl_propertylist_erase( (cpl_propertylist *)applist,"EXTNAME");
619 }
620
621
622 if(cpl_propertylist_has(applist,"BUNIT")) {
623 cpl_propertylist_set_string((cpl_propertylist *) applist,"BUNIT", unit);
624 } else {
625 cpl_propertylist_append_string((cpl_propertylist *) applist,"BUNIT", unit);
626 }
627 cpl_dfs_save_image(allframes, NULL, parlist, usedframes, NULL, image, type,
628 recipe, applist, remregexp, pipe_id, filename);
629 }
630 CATCH
631 {
632 retVal = cpl_error_get_code();
633 }
634 eris_ifu_free_propertylist(&dataHeader);
635 eris_ifu_free_propertylist(&head_wcs_2d);
636 eris_ifu_free_string(&pipe_id);
637 eris_check_error_code("eris_ifu_save_image");
638 return retVal;
639}
640
660cpl_error_code eris_ifu_save_table(
661 cpl_frameset *allframes,
662 cpl_propertylist *header,
663 const cpl_parameterlist *parlist,
664 const cpl_frameset *usedframes,
665 const cpl_frame *inherit,
666 const char *recipe,
667 const char *procatg,
668 cpl_propertylist *applist,
669 const char *remregexp,
670 const char *filename,
671 const cpl_table *table)
672{
673 cpl_error_code retVal = CPL_ERROR_NONE;
674 cpl_propertylist *dataHeader = NULL;
675 char *pipe_id = NULL;
676
677 cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
678 cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
679 cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
680 cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
681 cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
682 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
683 cpl_ensure_code(table, CPL_ERROR_NULL_INPUT);
684 cpl_ensure_code(procatg, CPL_ERROR_NULL_INPUT);
685
686 TRY
687 {
688
689 pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
690 /* Create FITS headers for the extensions */
691 dataHeader = cpl_propertylist_new();
692 eris_ifu_heades_add_hduclass_common(dataHeader, "TABLE");
693
694 cpl_propertylist_append_string(dataHeader, FHDR_HDUCLAS2, DEQ_HDUCLAS2_DATA);
695 cpl_propertylist_append_string(dataHeader, FHDR_EXTNAME, DEQ_SCIDATA);
696 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG, procatg);
697
698 /* Create an empty primary HDU with only the header information*/
699 cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
700 inherit, recipe, applist, remregexp, pipe_id, filename);
701
702 /* Write the extension unit */
703 cpl_table_save(table, dataHeader, applist, filename, CPL_IO_EXTEND);
704 }
705 CATCH
706 {
707 CATCH_MSGS();
708 retVal = cpl_error_get_code();
709 }
710
711 eris_ifu_free_propertylist(&dataHeader);
712 eris_ifu_free_string(&pipe_id);
713 eris_check_error_code("eris_ifu_save_table");
714 return retVal;
715}
716
718// * according to VLT-SPE-ESO-19500-5667
719// @brief Write DFS pipeline product with table.
720// @param allframes The list of input frames for the recipe
721// @param header NULL or filled with properties written to product header
722// @param parlist The list of input parameters
723// @param usedframes The list of raw/calibration frames used for this product
724// @param inherit NULL or product frames inherit their header from this frame
725// @param recipe The recipe name
726// @param applist Propertylist to append to primary header, w. PRO.CATG
727// @param remregexp NULL or regexp of properties not to put in main header
728// @param filename Filename of created product
729// @param bpm The badpixel map to be saved
730// @return CPL_ERROR_NONE if OK
731//*/
732//cpl_error_code eris_ifu_save_bpm(
733// cpl_frameset *allframes,
734// cpl_propertylist *header,
735// const cpl_parameterlist *parlist,
736// const cpl_frameset *usedframes,
737// const cpl_frame *inherit,
738// const char *recipe,
739// const char *procatg,
740// cpl_propertylist *applist,
741// const char *remregexp,
742// const char *filename,
743// const cpl_image *bpm)
744//{
745// cpl_error_code retVal = CPL_ERROR_NONE;
746// cpl_propertylist *dataHeader = NULL;
747// char *pipe_id = NULL;
748// cpl_ensure_code(allframes, CPL_ERROR_NULL_INPUT);
749// cpl_ensure_code(parlist, CPL_ERROR_NULL_INPUT);
750// cpl_ensure_code(usedframes, CPL_ERROR_NULL_INPUT);
751// cpl_ensure_code(recipe, CPL_ERROR_NULL_INPUT);
752// cpl_ensure_code(applist, CPL_ERROR_NULL_INPUT);
753// cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
754// cpl_ensure_code(bpm, CPL_ERROR_NULL_INPUT);
755// cpl_ensure_code(procatg, CPL_ERROR_NULL_INPUT);
756// TRY
757// {
758// pipe_id = cpl_sprintf("%s%s%s", PACKAGE, "/", PACKAGE_VERSION);
759// /* Create FITS headers for the extensions */
760//
761// dataHeader = cpl_propertylist_new();
762// eris_ifu_heades_add_hduclass_common(dataHeader, "TABLE");
763// cpl_propertylist_append_string(dataHeader, FHDR_HDUCLAS2, DEQ_HDUCLAS2_DATA);
764// cpl_propertylist_append_string(dataHeader, FHDR_EXTNAME, DEQ_SCIDATA);
765// cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG, procatg);
766// /* Create an empty primary HDU with only the header information*/
767// cpl_dfs_save_propertylist(allframes, header, parlist, usedframes,
768// inherit, recipe, applist, remregexp,
769// pipe_id, filename));
770// /* Write the extension unit */
771// cpl_image_save(bpm, filename, cpl_image_get_type(bpm), dataHeader,
772// CPL_IO_EXTEND);
773// }
774// CATCH
775// {
776// CATCH_MSGS();
777// retVal = cpl_error_get_code();
778// }
779// eris_ifu_free_propertylist(&dataHeader);
780// eris_ifu_free_string(&pipe_id);
781// return retVal;
782//}
783
797 const char *filename,
798 cpl_propertylist **primaryHeader,
799 cpl_image **dataImage,
800 cpl_image **errImage,
801 cpl_image **qualImage,
802 deqErrorType *errorType,
803 deqQualityType *qualityType)
804{
805 cpl_error_code retVal = CPL_ERROR_NONE;
806 cpl_size nExt;
807 int extension;
808 int match;
809 cpl_propertylist *pl = NULL;
810 const char *property;
811
812 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
813 cpl_ensure_code(dataImage, CPL_ERROR_NULL_INPUT);
814
815 TRY
816 {
817 nExt = cpl_fits_count_extensions(filename);
819 if (nExt != 3) {
820 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
821 "For a DEQ FITS file 3 extensions are expected, but %d are found",
822 (int) nExt);
823 }
824 *dataImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED, 0, 1);
825
826 if (primaryHeader != NULL) {
827 *primaryHeader = cpl_propertylist_load(filename, 0);
828 }
829
830 if (errImage != NULL) {
831 extension = 2;
832 *errImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED,
833 0, extension);
834 pl = cpl_propertylist_load(filename, extension);
835 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
836 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
837 } else {
838 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
839 "keyword %s does not exist",
840 FHDR_HDUCLAS3);
841 }
842 for (match=0; match<4; match++) {
843 if (strcmp(property, deqErrorTypeString[match]) == 0) {
844 *errorType = match;
845 break;
846 }
847 }
848 if (match > 3) {
849 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
850 "The HDUCLAS3 keyword for the error extension (%d) has an"
851 " unknown value of %s", extension, property);
852 }
854 }
855
856 if (qualImage != NULL) {
857 extension = 3;
858 *qualImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED,
859 0, 3);
860 pl = cpl_propertylist_load(filename, extension);
861 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
862 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
863 } else {
864 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
865 "keyword %s does not exist",
866 FHDR_HDUCLAS3);
867 }
868 for (match=0; match<4; match++) {
869 if (strcmp(property, deqQualityTypeString[match]) == 0) {
870 *qualityType = match;
871 break;
872 }
873 }
874 if (match > 3) {
875 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
876 "The HDUCLAS3 keyword for the error extension (%d) has an"
877 " unknown value of %s", (int) extension, property);
878 }
880 }
881 }
882 CATCH
883 {
884 retVal = cpl_error_get_code();
885 }
886 eris_check_error_code("eris_ifu_load_deq_image");
887 return retVal;
888}
902 const char *filename,
903 cpl_propertylist **primaryHeader,
904 cpl_imagelist **dataImagelist,
905 cpl_imagelist **errImagelist,
906 cpl_imagelist **qualImagelist,
907 deqErrorType *errorType,
908 deqQualityType *qualityType)
909{
910 cpl_error_code retVal = CPL_ERROR_NONE;
911 cpl_size nExt;
912 int extension;
913 int match;
914 cpl_propertylist *pl = NULL;
915 const char *property;
916
917 cpl_ensure_code(filename, CPL_ERROR_NULL_INPUT);
918 cpl_ensure_code(dataImagelist, CPL_ERROR_NULL_INPUT);
919
920 TRY
921 {
922 nExt = cpl_fits_count_extensions(filename);
924 if (nExt != 3) {
925 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
926 "For a DEQ FITS file 3 extensions are expected, but %d are found",
927 (int) nExt);
928 }
929 *dataImagelist = cpl_imagelist_load(filename, CPL_TYPE_UNSPECIFIED, 1);
930
931 if (primaryHeader != NULL) {
932 *primaryHeader = cpl_propertylist_load(filename, 0);
933 }
934
935 if (errImagelist != NULL) {
936 extension = 2;
937 *errImagelist = cpl_imagelist_load(filename, CPL_TYPE_UNSPECIFIED,
938 extension);
939 pl = cpl_propertylist_load(filename, extension);
940 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
941 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
942 } else {
943 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
944 "keyword %s does not exist",
945 FHDR_HDUCLAS3);
946 }
947 for (match=0; match<4; match++) {
948 if (strcmp(property, deqErrorTypeString[match]) == 0) {
949 *errorType = match;
950 break;
951 }
952 }
953 if (match > 3) {
954 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
955 "The HDUCLAS3 keyword for the error extension (%d) has an"
956 " unknown value of %s", extension, property);
957 }
959 }
960
961 if (qualImagelist != NULL) {
962 extension = 3;
963 *qualImagelist = cpl_imagelist_load(filename, CPL_TYPE_UNSPECIFIED,
964 extension);
965 pl = cpl_propertylist_load(filename, extension);
966 if (cpl_propertylist_has(pl, FHDR_HDUCLAS3)) {
967 property = cpl_propertylist_get_string(pl, FHDR_HDUCLAS3);
968 } else {
969 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
970 "keyword %s does not exist",
971 FHDR_HDUCLAS3);
972 }
973 for (match=0; match<4; match++) {
974 if (strcmp(property, deqQualityTypeString[match]) == 0) {
975 *qualityType = match;
976 break;
977 }
978 }
979 if (match > 3) {
980 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
981 "The HDUCLAS3 keyword for the error extension (%d) has an"
982 " unknown value of %s", (int) extension, property);
983 }
985 }
986 }
987 CATCH
988 {
989 retVal = cpl_error_get_code();
990 }
991 eris_check_error_code("eris_ifu_load_deq_imagelist");
992 return retVal;
993}
1004 const char *filename,
1005 cpl_propertylist **primaryHeader,
1006 cpl_image **qualImage,
1007 deqQualityType *qualityType)
1008{
1009 hdrl_image *image = NULL;
1010 cpl_image *dataImage = NULL;
1011 cpl_image *errImage = NULL;
1012 cpl_mask *mask = NULL;
1013
1014 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
1015
1016 TRY
1017 {
1018 deqErrorType errorType;
1019 eris_ifu_load_deq_image(filename, primaryHeader,
1020 &dataImage, &errImage, qualImage, &errorType, qualityType);
1021
1022 switch(errorType) {
1023 case mse:
1024 cpl_image_power(errImage, 0.5);
1025 break;
1026 case rmse:
1027 // nothing to do, RMSE is Ok
1028 break;
1029 case invmse:
1030 CPL_ATTR_FALLTRHU; //intended fall through !
1031 case invrmse:
1032 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1033 "Cannot transfer error extension type %s to a HDLR error"
1034 " image", deqErrorTypeString[errorType]);
1035 break;
1036 default:
1037 BRK_WITH_ERROR_MSG(CPL_ERROR_ACCESS_OUT_OF_RANGE,
1038 "Internal error: unknown errorType enumeration value: %d",
1039 errorType);
1040 break;
1041 }
1042
1043 if (qualImage != NULL) {
1044 switch (*qualityType) {
1045 case maskzero:
1046 //intended fall through !
1047 CPL_ATTR_FALLTRHU;
1048 case flag16bit:
1049 //intended fall through !
1050 CPL_ATTR_FALLTRHU;
1051 case flag32bit: // The value "0" indicates a "good" pixel
1052 mask = cpl_mask_threshold_image_create(*qualImage,
1053 -1.0, 1.0);
1054 break;
1055 case maskone: // The value "1" indicates a "good" pixel
1056 mask = cpl_mask_threshold_image_create(*qualImage,
1057 0.0, 2.0);
1058 break;
1059 default:
1061 CPL_ERROR_ACCESS_OUT_OF_RANGE,
1062 "Internal error: unknown qualityType enumeration"
1063 " value: %d",
1064 *qualityType);
1065 break;
1066 }
1067 }
1068
1069 cpl_mask_not(mask); //hey it is a BAD pixel mask!
1070 cpl_image_set_bpm(dataImage, mask);
1071 mask = NULL;
1072 image = hdrl_image_create(dataImage, errImage);
1074 }
1075 CATCH
1076 {
1077 cpl_mask_delete(mask);
1078 hdrl_image_delete(image);
1079 image = NULL;
1080 }
1081 cpl_image_delete(dataImage);
1082 cpl_image_delete(errImage);
1083 eris_check_error_code("eris_ifu_load_deq_hdrl_image");
1084 return image;
1085}
1096 const char *filename,
1097 cpl_propertylist **primaryHeader,
1098 cpl_imagelist **qualImagelist,
1099 deqQualityType *qualityType)
1100{
1101 hdrl_imagelist *imagelist = NULL;
1102 cpl_imagelist *dataImagelist = NULL;
1103 cpl_imagelist *errImagelist = NULL;
1104 cpl_image *dataImage = NULL;
1105 cpl_image *errImage = NULL;
1106 cpl_image *qualImage = NULL;
1107 cpl_mask *mask = NULL;
1108 deqErrorType errorType;
1109
1110 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
1111
1112 TRY
1113 {
1114 eris_ifu_load_deq_imagelist(filename, primaryHeader,
1115 &dataImagelist, &errImagelist, qualImagelist, &errorType, qualityType);
1116
1117 if ((cpl_imagelist_get_size(dataImagelist) != cpl_imagelist_get_size(errImagelist))
1118 ||
1119 (cpl_imagelist_get_size(dataImagelist) != cpl_imagelist_get_size(*qualImagelist)))
1120 {
1121 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
1122 "All imagelists of an DEQ imagelist file must have the same size");
1123 }
1124
1125 for (cpl_size ix=0; ix < cpl_imagelist_get_size(dataImagelist); ix++) {
1126 dataImage = cpl_imagelist_get(dataImagelist, ix);
1127 errImage = cpl_imagelist_get(errImagelist, ix);
1128 qualImage = cpl_imagelist_get(*qualImagelist, ix);
1129
1130 switch(errorType) {
1131 case mse:
1133 cpl_image_power(errImage, 0.5));
1134 break;
1135 case rmse:
1136 // nothing to do, RMSE is Ok
1137 break;
1138 case invmse:
1139 //intended fall through !
1140 CPL_ATTR_FALLTRHU;
1141 case invrmse:
1142 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1143 "Cannot transfer error extension type %s to a HDLR error"
1144 " image", deqErrorTypeString[errorType]);
1145 break;
1146 default:
1147 BRK_WITH_ERROR_MSG(CPL_ERROR_ACCESS_OUT_OF_RANGE,
1148 "Internal error: unknown errorType enumeration value: %d",
1149 errorType);
1150 break;
1151 }
1152
1153 if (qualImage != NULL) {
1154 switch (*qualityType) {
1155 case maskzero:
1156 //intended fall through !
1157 CPL_ATTR_FALLTRHU;
1158 case flag16bit:
1159 //intended fall through !
1160 CPL_ATTR_FALLTRHU;
1161 case flag32bit: // The value "0" indicates a "good" pixel
1162 mask = cpl_mask_threshold_image_create(qualImage,
1163 -1.0, 1.0);
1164 break;
1165 case maskone: // The value "1" indicates a "good" pixel
1166 mask = cpl_mask_threshold_image_create(qualImage,
1167 0.5, 2.0);
1168 break;
1169 default:
1171 CPL_ERROR_ACCESS_OUT_OF_RANGE,
1172 "Internal error: unknown qualityType enumeration"
1173 " value: %d",
1174 *qualityType);
1175 break;
1176 }
1177 }
1178 cpl_mask_not(mask); //hey it is a BAD pixel mask!
1179
1180 int pis_rejected;
1181 for (cpl_size ny = 1; ny <= cpl_image_get_size_y(dataImage); ny++) {
1182 for (cpl_size nx = 1; nx <= cpl_image_get_size_x(dataImage); nx++) {
1183 if (eris_ifu_is_nan_or_inf(cpl_image_get(dataImage, nx, ny, &pis_rejected)) ||
1184 eris_ifu_is_nan_or_inf(cpl_image_get(errImage, nx, ny, &pis_rejected))
1185 ) {
1186 cpl_mask_set(mask, nx, ny, BAD_PIX);
1187 }
1188 }
1189 }
1190
1191 cpl_image_set_bpm(dataImage, mask);
1192 cpl_image_set_bpm(errImage, cpl_mask_duplicate(mask));
1194 cpl_imagelist_set(dataImagelist, dataImage, ix);
1195 cpl_imagelist_set(errImagelist, errImage, ix);
1196
1197 }
1198 imagelist = hdrl_imagelist_create(
1199 dataImagelist, errImagelist);
1200 }
1201 CATCH
1202 {
1203 hdrl_imagelist_delete(imagelist);
1204 imagelist = NULL;
1205 }
1206 cpl_imagelist_delete(dataImagelist);
1207 cpl_imagelist_delete(errImagelist);
1208 eris_check_error_code("eris_ifu_load_deq_hdrl_imagelist");
1209 return imagelist;
1210}
1222 const cpl_frame *frame,
1223 ifsBand band,
1224 ifsPreopticsScale scale,
1225 cpl_image **qualImage,
1226 deqQualityType *qualType)
1227{
1228 hdrl_image *image = NULL;
1229 const char *filename;
1230
1231 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, NULL);
1232
1233 TRY
1234 {
1235 filename = cpl_frame_get_filename(frame);
1237 filename, band, scale, qualImage, qualType);
1238 }
1239 CATCH
1240 {
1241 hdrl_image_delete(image);
1242 image = NULL;
1243 }
1244 eris_check_error_code("eris_ifu_load_cal_image_frame");
1245 return image;
1246}
1258 const char *filename,
1259 ifsBand band,
1260 ifsPreopticsScale scale,
1261 cpl_image **qualImage,
1262 deqQualityType *qualType)
1263{
1264 hdrl_image *image = NULL;
1265 cpl_propertylist *header = NULL;
1266 cpl_image *dataImage = NULL;
1267 cpl_image *errorImage = NULL;
1268 cpl_size nExt;
1269 // ifsBand thisBand;
1270 // ifsPreopticsScale thisScale;
1271
1272 cpl_ensure(filename, CPL_ERROR_NULL_INPUT, NULL);
1273
1274 TRY
1275 {
1276 nExt = cpl_fits_count_extensions(filename);
1277 if (nExt == 3) { // new format data/error/quality DEQ
1278 image = eris_ifu_load_deq_hdrl_image(filename, &header,
1279 qualImage, qualType);
1280 } else { // old format, data only, error will be simulated
1281 header = cpl_propertylist_load(filename, 0);
1282 dataImage = cpl_image_load(filename, CPL_TYPE_UNSPECIFIED,
1283 0 , 0);
1284 errorImage = cpl_image_abs_create(dataImage);
1285 cpl_image_multiply_scalar(errorImage, 0.1);
1286 image = hdrl_image_create(dataImage, errorImage);
1287 *qualImage = NULL;
1288 if (qualType != NULL) {
1289 *qualType = unspecified;
1290 }
1291 }
1292 if ((band != UNDEFINED_BAND) &&
1293 (band != eris_ifu_get_band(header))) {
1294 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1295 "different instrument band setting"
1296 " in calibration frame %s", filename);
1297 }
1298 if ((scale != UNDEFINED_SCALE) &&
1299 (scale != PUPIL) &&
1300 (scale != eris_ifu_get_preopticsScale(header))) {
1301 BRK_WITH_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
1302 "different instrument pre-optics setting"
1303 " in calibration frame %s", filename);
1304 }
1305
1306 }
1307 CATCH
1308 {
1309 hdrl_image_delete(image);
1310 image = NULL;
1311 }
1313 eris_ifu_free_image(&dataImage);
1314 eris_ifu_free_image(&errorImage);
1315 eris_check_error_code("eris_ifu_load_cal_image_file");
1316 return image;
1317}
1318
1328 const char* filename,
1329 cpl_polynomial ** poly_u,
1330 cpl_polynomial ** poly_v)
1331{
1332 cpl_error_code retVal = CPL_ERROR_NONE;
1333 cpl_table *distortionTable = NULL;
1334 cpl_size nTableRows;
1335 cpl_size pows[2];
1336 double coeff;
1337
1338 TRY
1339 {
1340 *poly_u = cpl_polynomial_new(2);
1341 *poly_v = cpl_polynomial_new(2);
1342
1343 distortionTable = cpl_table_load(filename,1,0);
1344 nTableRows = cpl_table_get_nrow(distortionTable);
1345
1346 for (cpl_size i=0; i<nTableRows; i++) {
1347 pows[0] = cpl_table_get_int(distortionTable, "degx", i, NULL);
1348 pows[1] = cpl_table_get_int(distortionTable, "degy", i, NULL);
1349 coeff = cpl_table_get_double(distortionTable, "coeff", i, NULL);
1350 cpl_polynomial_set_coeff(*poly_u, pows, coeff);
1351 }
1353
1354 pows[0] = 0;
1355 pows[1] = 1;
1356 cpl_polynomial_set_coeff(*poly_v, pows, 1.0);
1358 }
1359 CATCH
1360 {
1361 retVal = cpl_error_get_code();
1362 }
1363
1364 eris_ifu_free_table(&distortionTable);
1365 eris_check_error_code("eris_ifu_load_cal_image_file");
1366 return retVal;
1367}
1368
1378 const char* filename,
1379 cpl_polynomial ***polynomials,
1380 cpl_table **borders)
1381{
1382 cpl_error_code retVal = CPL_ERROR_NONE;
1383 cpl_table *distortionTable = NULL;
1384
1385 TRY
1386 {
1387 *borders = NULL;
1388
1389 *polynomials =
1390 cpl_calloc(SLITLET_CNT, sizeof(cpl_polynomial*));
1391
1392 for (int sx = 0; sx < SLITLET_CNT; sx++) {
1393 cpl_size nTableRows;
1394
1395 eris_ifu_free_table(&distortionTable);
1396 distortionTable = cpl_table_load(filename, sx+1, 0);
1397 nTableRows = cpl_table_get_nrow(distortionTable);
1398
1399 (*polynomials)[sx] = cpl_polynomial_new(2);
1400
1401 for (cpl_size i=0; i<nTableRows; i++) {
1402 cpl_size pows[2];
1403 double coeff;
1404
1405 pows[0] = cpl_table_get_int(distortionTable, "degx", i, NULL);
1406 pows[1] = cpl_table_get_int(distortionTable, "degy", i, NULL);
1407 coeff = cpl_table_get_double(distortionTable, "coeff", i, NULL);
1408 cpl_polynomial_set_coeff((*polynomials)[sx], pows, coeff);
1409 }
1411
1412 }
1413
1414 *borders = cpl_table_load(filename, SLITLET_CNT+1, 0);
1415 }
1416 CATCH
1417 {
1418 if (*polynomials != NULL) {
1419 for (int sx = 0; sx < SLITLET_CNT; sx++) {
1420 cpl_polynomial_delete((*polynomials)[sx]);
1421 }
1422 }
1423 eris_ifu_free_table(borders);
1424 retVal = cpl_error_set_where(cpl_func);
1425 }
1426
1427 eris_ifu_free_table(&distortionTable);
1428 eris_check_error_code("eris_ifu_load_distortion_polynomials");
1429 return retVal;
1430
1431}
1432#define ERIS_IFU_DIST_SLIT_DISTANCE "slitlet_distance"
1433
1441 const char* filename)
1442{
1443 cpl_vector *distances = NULL;
1444 cpl_table *table = NULL;
1445 cpl_size nTableRows = 0;
1446 double val = 0.;
1447
1448 TRY
1449 {
1450 table = cpl_table_load(filename,1,0);
1451 nTableRows = cpl_table_get_nrow(table);
1452
1453 distances = cpl_vector_new(nTableRows);
1454 cpl_type type = cpl_table_get_column_type(table, ERIS_IFU_DIST_SLIT_DISTANCE);
1455 for (cpl_size i=0; i<nTableRows; i++) {
1456 if (type == CPL_TYPE_DOUBLE) {
1457 val = cpl_table_get_double(table, ERIS_IFU_DIST_SLIT_DISTANCE, i, NULL);
1458 } else if (type == CPL_TYPE_FLOAT) {
1459 val = cpl_table_get_float(table, ERIS_IFU_DIST_SLIT_DISTANCE, i, NULL);
1460 } else {
1461 SET_ERROR(CPL_ERROR_INVALID_TYPE);
1462 }
1464 cpl_vector_set(distances, i, val));
1465 }
1466 }
1467 CATCH
1468 {
1469 cpl_vector_delete(distances);
1470 distances = NULL;
1471 }
1472
1473 eris_ifu_free_table(&table);
1474 eris_check_error_code("eris_ifu_load_distances");
1475 return distances;
1476}
1477
1478#define ERIS_IFU_DIST_EDGE_L_old "pos1"
1479#define ERIS_IFU_DIST_EDGE_R_old "pos2"
1487 const char* filename)
1488{
1489 cpl_bivector *slitPos = NULL;
1490 cpl_vector *pos1 = NULL;
1491 cpl_vector *pos2 = NULL;
1492 cpl_table *slitPosTable = NULL;
1493 cpl_size nTableRows;
1494
1495 TRY
1496 {
1497 slitPosTable = cpl_table_load(filename,1,0);
1498 nTableRows = cpl_table_get_nrow(slitPosTable);
1499
1500 slitPos = cpl_bivector_new(nTableRows);
1501 pos1 = cpl_bivector_get_x(slitPos);
1502 pos2 = cpl_bivector_get_y(slitPos);
1504
1505 for (cpl_size i=0; i<nTableRows; i++) {
1506 double val = 0.;
1507 // check if name of table column is "edge_left"
1508 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_L, i, NULL);
1509 if (cpl_error_get_code()== CPL_ERROR_DATA_NOT_FOUND) {
1510 // ok, it seems to be old fashioned sinfoni "pos1"
1511 RECOVER();
1512 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_L_old, i, NULL);
1513 }
1514 cpl_vector_set(pos1, i, val);
1515
1516 // check if name of table column is "edge_right"
1517 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_R, i, NULL);
1518 if (cpl_error_get_code()== CPL_ERROR_DATA_NOT_FOUND) {
1519 // ok, it seems to be old fashioned sinfoni "pos2"
1520 RECOVER();
1521 val = cpl_table_get_double(slitPosTable, ERIS_IFU_DIST_EDGE_R_old, i, NULL);
1522 }
1523 cpl_vector_set(pos2, i, val);
1524 }
1525 }
1526 CATCH
1527 {
1528 cpl_bivector_delete(slitPos);
1529 slitPos = NULL;
1530 }
1531
1532 eris_ifu_free_table(&slitPosTable);
1533 eris_check_error_code("eris_ifu_load_slit_positions");
1534 return slitPos;
1535}
1536
1537/*----------------------------------------------------------------------------*/
1546/*----------------------------------------------------------------------------*/
1548 cpl_propertylist *pl,
1549 const char *name,
1550 int val,
1551 const char* comment)
1552{
1553 cpl_error_code retVal = CPL_ERROR_NONE;
1554 char *qcName = NULL;
1555
1556 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1557 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1558 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1559
1560 TRY
1561 {
1562 qcName = cpl_sprintf("ESO QC %s", name);
1563 cpl_propertylist_append_int(pl, qcName, val);
1564 cpl_propertylist_set_comment(pl , qcName, comment);
1565 }
1566 CATCH
1567 {
1568 retVal = cpl_error_get_code();
1569 }
1570
1571 eris_ifu_free_string(&qcName);
1572 eris_check_error_code("eris_ifu_append_qc_int");
1573 return retVal;
1574}
1575/*----------------------------------------------------------------------------*/
1584/*----------------------------------------------------------------------------*/
1586 cpl_propertylist *pl,
1587 const char *name,
1588 double val,
1589 const char* comment)
1590{
1591 cpl_error_code retVal = CPL_ERROR_NONE;
1592 char *qcName = NULL;
1593
1594 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1595 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1596 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1597
1598 TRY
1599 {
1600 qcName = cpl_sprintf("ESO QC %s", name);
1601 cpl_propertylist_append_double(pl, qcName, val);
1602 cpl_propertylist_set_comment(pl , qcName, comment);
1603 }
1604 CATCH
1605 {
1606 retVal = cpl_error_get_code();
1607 }
1608
1609 eris_ifu_free_string(&qcName);
1610 eris_check_error_code("eris_ifu_append_qc_double");
1611 return retVal;
1612}
1613
1614/*----------------------------------------------------------------------------*/
1623/*----------------------------------------------------------------------------*/
1625 cpl_propertylist *pl,
1626 const char *name,
1627 float val,
1628 const char* comment)
1629{
1630 cpl_error_code retVal = CPL_ERROR_NONE;
1631 char *qcName = NULL;
1632
1633 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1634 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1635 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1636
1637 TRY
1638 {
1639 qcName = cpl_sprintf("ESO QC %s", name);
1640 cpl_propertylist_append_float(pl, qcName, val);
1641 cpl_propertylist_set_comment(pl , qcName, comment);
1642 }
1643 CATCH
1644 {
1645 retVal = cpl_error_get_code();
1646 }
1647
1648 eris_ifu_free_string(&qcName);
1649 eris_check_error_code("eris_ifu_append_qc_float");
1650 return retVal;
1651}
1652
1653/*----------------------------------------------------------------------------*/
1662/*----------------------------------------------------------------------------*/
1663cpl_error_code eris_ifu_set_qc_int(cpl_propertylist *pl,
1664 const char *name,
1665 int val,
1666 const char *comment)
1667{
1668 cpl_error_code retVal = CPL_ERROR_NONE;
1669 char *qcName = NULL;
1670
1671 cpl_ensure_code(pl, CPL_ERROR_NULL_INPUT);
1672 cpl_ensure_code(name, CPL_ERROR_NULL_INPUT);
1673 cpl_ensure_code(comment, CPL_ERROR_NULL_INPUT);
1674
1675 TRY
1676 {
1677 qcName = cpl_sprintf("ESO QC %s", name);
1678 cpl_propertylist_set_int(pl, qcName, val);
1679 cpl_propertylist_set_comment(pl, qcName, comment);
1680 }
1681 CATCH
1682 {
1683 retVal = cpl_error_get_code();
1684 }
1685
1686 eris_ifu_free_string(&qcName);
1687 eris_check_error_code("eris_ifu_append_qc_int");
1688 return retVal;
1689}
1690/*----------------------------------------------------------------------------*/
1696/*----------------------------------------------------------------------------*/
1697bool eris_ifu_frame_is_on(const cpl_frame *fr)
1698{
1699 bool result = false;
1700 cpl_propertylist *pl = NULL;
1701 const char *fn = NULL;
1702 char *dpr_type = NULL;
1703
1704 cpl_ensure(fr != NULL, CPL_ERROR_NULL_INPUT, false);
1705
1706 TRY
1707 {
1708 fn = cpl_frame_get_filename(fr);
1709 eris_ifu_file_exists(fn);
1710
1711 pl = cpl_propertylist_load(fn, 0);
1712
1713 if (cpl_propertylist_has(pl, FHDR_DPR_TYPE)) {
1714 dpr_type = cpl_strdup(
1715 cpl_propertylist_get_string(pl, FHDR_DPR_TYPE));
1716 } else {
1717 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
1718 "keyword %s does not exist",
1719 FHDR_DPR_TYPE);
1720 }
1721
1722 if ((strstr(dpr_type, "STD") != NULL) ||
1723 (strstr(dpr_type, "PSF") != NULL) ||
1724 (strstr(dpr_type, "SKY") != NULL) ||
1725 (strstr(dpr_type, "OBJECT") != NULL))
1726 {
1727 result = true;
1728 } else {
1729 if (eris_ifu_get_callamp_status(pl) != 0)
1730 {
1731 result = true;
1732 }
1733 }
1734 }
1735 CATCH
1736 {
1737 result = false;
1738 }
1739
1741 eris_ifu_free_string(&dpr_type);
1742 eris_check_error_code("eris_ifu_frame_is_on");
1743 return result;
1744}
1745/*----------------------------------------------------------------------------*/
1751/*----------------------------------------------------------------------------*/
1752bool eris_ifu_frame_is_sky(const cpl_frame *fr)
1753{
1754 bool result = false;
1755 cpl_propertylist *pl = NULL;
1756 const char *fn = NULL;
1757 char *dpr_type = NULL;
1758
1759 cpl_ensure(fr, CPL_ERROR_NULL_INPUT, false);
1760
1761 TRY
1762 {
1763 fn = cpl_frame_get_filename(fr);
1764 eris_ifu_file_exists(fn);
1765 pl = cpl_propertylist_load(fn, 0);
1766
1767
1768 if (cpl_propertylist_has(pl, FHDR_DPR_TYPE)) {
1769 dpr_type = cpl_strdup(
1770 cpl_propertylist_get_string(pl, FHDR_DPR_TYPE));
1771 } else {
1772 BRK_WITH_ERROR_MSG(CPL_ERROR_ILLEGAL_INPUT,
1773 "keyword %s does not exist",
1774 FHDR_DPR_TYPE);
1775 // go to catch clause
1776 }
1777
1778 if (strstr(dpr_type, ERIS_IFU_RAW_SKY) != NULL) {
1779 result = true;
1780 }
1781 }
1782 CATCH
1783 {
1784 result = false;
1785 }
1786
1788 eris_ifu_free_string(&dpr_type);
1789 eris_check_error_code("eris_ifu_frame_is_sky");
1790 return result;
1791}
1792/*----------------------------------------------------------------------------*/
1799float eris_ifu_get_dit(cpl_propertylist *header) {
1800 float dit = 0;
1801 ifsInstrument instrument = UNSET_INSTRUMENT;
1802
1803 cpl_ensure(header, CPL_ERROR_NULL_INPUT, 0);
1804
1805 TRY
1806 {
1807 instrument = eris_ifu_get_instrument(header);
1809 if (instrument == SPIFFIER) {
1810 dit = cpl_propertylist_get_float(header, FHDR_E_DIT);
1811
1812 } else if (instrument == SPIFFI) {
1813 dit = cpl_propertylist_get_float(header, FHDR_S_DIT);
1814 }
1816 }
1817 CATCH
1818 {
1819 dit = 0;
1820 }
1821 eris_check_error_code("eris_ifu_get_dit");
1822 return dit;
1823}
1824
1839int eris_ifu_get_callamp_status(cpl_propertylist *header)
1840{
1841 ifsInstrument instrument = UNSET_INSTRUMENT;
1842 int lampMask = 0;
1843 int lampAr = 0;
1844 int lampKr = 0;
1845 int lampNe = 0;
1846 int lampXe = 0;
1847 int lampQth = 0;
1848
1849 cpl_ensure_code(header, CPL_ERROR_NULL_INPUT);
1850
1851 TRY
1852 {
1853 instrument = eris_ifu_get_instrument(header);
1855 if (instrument == SPIFFIER) {
1856 cpl_msg_debug("eris_ifu_get_callamp_status", "ERIS/SPIFFIER found");
1857 if (cpl_propertylist_has(header, FHDR_E_AR_LAMP_ST)) {
1858 if (cpl_propertylist_get_bool(header, FHDR_E_AR_LAMP_ST)) {
1859 lampMask += AR_LAMP;
1860 lampAr = 1;
1861 }
1862 }
1863 if (cpl_propertylist_has(header, FHDR_E_KR_LAMP_ST)) {
1864 if (cpl_propertylist_get_bool(header, FHDR_E_KR_LAMP_ST)) {
1865 lampMask += KR_LAMP;
1866 lampKr = 1;
1867 }
1868 }
1869 if (cpl_propertylist_has(header, FHDR_E_NE_LAMP_ST)) {
1870 if (cpl_propertylist_get_bool(header, FHDR_E_NE_LAMP_ST)) {
1871 lampMask += NE_LAMP;
1872 lampNe = 1;
1873 }
1874 }
1875 if (cpl_propertylist_has(header, FHDR_E_XE_LAMP_ST)) {
1876 if (cpl_propertylist_get_bool(header, FHDR_E_XE_LAMP_ST)) {
1877 lampMask += XE_LAMP;
1878 lampXe = 1;
1879 }
1880 }
1881 if (cpl_propertylist_has(header, FHDR_E_QTH_LAMP_ST)) {
1882 if (cpl_propertylist_get_bool(header,FHDR_E_QTH_LAMP_ST)) {
1883 lampMask += QTH_LAMP;
1884 lampQth = 1;
1885 }
1886 }
1887 } else if (instrument == SPIFFI) {
1888 cpl_msg_debug("eris_ifu_get_callamp_status", "SINFONI/SPIFFI found");
1889 if (cpl_propertylist_get_bool(header,FHDR_S_AR_LAMP_ST)) {
1890 lampMask += AR_LAMP;
1891 lampAr = 1;
1892 }
1893 if (cpl_propertylist_get_bool(header,FHDR_S_KR_LAMP_ST)) {
1894 lampMask += KR_LAMP;
1895 lampKr = 1;
1896 }
1897 if (cpl_propertylist_get_bool(header,FHDR_S_NE_LAMP_ST)) {
1898 lampMask += NE_LAMP;
1899 lampNe = 1;
1900 }
1901 if (cpl_propertylist_get_bool(header,FHDR_S_XE_LAMP_ST)) {
1902 lampMask += XE_LAMP;
1903 lampXe = 1;
1904 }
1905 if (cpl_propertylist_get_bool(header,FHDR_S_QTH_LAMP_ST)) {
1906 lampMask += QTH_LAMP;
1907 lampQth = 1;
1908 }
1909 }
1910 }
1911 CATCH
1912 {
1913 lampMask = 0;
1914 }
1915 cpl_msg_debug(cpl_func,
1916 "Lamps: Ar: %d, Kr: %d, Ne: %d, Xe: %d, Qth: %d, mask: %d",
1917 lampAr, lampKr, lampNe, lampXe, lampQth, lampMask);
1918 eris_check_error_code("eris_ifu_get_callamp_status");
1919 return lampMask;
1920}
1921/*----------------------------------------------------------------------------*/
1927ifsBand eris_ifu_get_band(const cpl_propertylist *header)
1928{
1929 ifsBand band = UNDEFINED_BAND;
1930 ifsInstrument instrument = UNSET_INSTRUMENT;
1931 const char *bandId = NULL;
1932
1933 TRY
1934 {
1935 if (header == NULL) {
1936 BRK_WITH_ERROR(CPL_ERROR_NULL_INPUT);
1937 }
1938
1939 instrument = eris_ifu_get_instrument(header);
1941 if (instrument == SPIFFIER) {
1942 bandId = cpl_propertylist_get_string(header, FHDR_E_GRATING_ID);
1944 if (strcmp(bandId, E_GRATING_J_LOW) == 0) {
1945 band = J_LOW;
1946 } else if (strcmp(bandId, E_GRATING_J_SHORT) == 0) {
1947 band = J_SHORT;
1948 } else if (strcmp(bandId, E_GRATING_J_MIDDLE) == 0) {
1949 band = J_MIDDLE;
1950 } else if (strcmp(bandId, E_GRATING_J_LONG) == 0) {
1951 band = J_LONG;
1952 } else if (strcmp(bandId, E_GRATING_H_LOW) == 0) {
1953 band = H_LOW;
1954 } else if (strcmp(bandId, E_GRATING_H_SHORT) == 0) {
1955 band = H_SHORT;
1956 } else if (strcmp(bandId, E_GRATING_H_MIDDLE) == 0) {
1957 band = H_MIDDLE;
1958 } else if (strcmp(bandId, E_GRATING_H_LONG) == 0) {
1959 band = H_LONG;
1960 } else if (strcmp(bandId, E_GRATING_K_LOW) == 0) {
1961 band = K_LOW;
1962 } else if (strcmp(bandId, E_GRATING_K_SHORT) == 0) {
1963 band = K_SHORT;
1964 } else if (strcmp(bandId, E_GRATING_K_MIDDLE) == 0) {
1965 band = K_MIDDLE;
1966 } else if (strcmp(bandId, E_GRATING_K_LONG) == 0) {
1967 band = K_LONG;
1968 } else if (strcmp(bandId, E_PREOPTICS_PUPIL) == 0) {
1969 cpl_msg_warning(cpl_func,"found pupil data");
1970 band = B_PUPIL;
1971 }
1972 } else if (instrument == SPIFFI) {
1973 bandId = cpl_propertylist_get_string(header, FHDR_S_GRATING_ID);
1975 if (strcmp(bandId, S_GRATING_J_BAND) == 0) {
1976 band = J_SPIFFI;
1977 } else if (strcmp(bandId, S_GRATING_H_BAND) == 0) {
1978 band = H_SPIFFI;
1979 } else if (strcmp(bandId, S_GRATING_K_BAND) == 0) {
1980 band = K_SPIFFI;
1981 } else if (strcmp(bandId, S_GRATING_HK_BAND) == 0) {
1982 band = HK_SPIFFI;
1983 } else {
1984 band = UNDEFINED_BAND;
1985 }
1986 }
1987
1988 }CATCH
1989 {
1990 band = UNDEFINED_BAND;
1991 }
1992 eris_check_error_code("eris_ifu_get_band");
1993 return band;
1994}
1995
2006ifsInstrument eris_ifu_get_instrument(const cpl_propertylist *header)
2007{
2008 ifsInstrument instrument = UNSET_INSTRUMENT;
2009 const char *instrumentStr = NULL;
2010 const char *armStr = NULL;
2011
2012 TRY
2013 {
2014 if (header == NULL) {
2015 BRK_WITH_ERROR(CPL_ERROR_NULL_INPUT);
2016 }
2017
2018 instrumentStr = cpl_propertylist_get_string(header, FHDR_INSTRUMENT);
2020 "Cannot read "FHDR_INSTRUMENT" FITS keyword");
2021 if (strcmp(instrumentStr, INSTRUMENT_SINFONI) == 0) {
2022 instrument = SPIFFI;
2023 } else if (strcmp(instrumentStr, INSTRUMENT_ERIS) == 0) {
2024 armStr = cpl_propertylist_get_string(header, FHDR_E_ARM);
2026 "Cannot read "FHDR_E_ARM" FITS keyword");
2027 if (strcmp(armStr, ARM_SPIFFIER) == 0) {
2028 instrument = SPIFFIER;
2029 } else if (strcmp(armStr, ARM_NIX) == 0) {
2030 cpl_msg_error(cpl_func, "ERIS/NIX frame detected");
2031 SET_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
2032 "ERIS/NIX frame detected");
2033 instrument = NIX;
2034 } else {
2035 cpl_msg_error("",
2036 "Unkown instrument arm %s, neither SPIFFIER nor NIX",
2037 armStr);
2038 SET_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
2039 "Unkown instrument arm, neither SPIFFIER nor NIX");
2040 instrument = OTHER_INSTRUMENT;
2041 }
2042 } else {
2043 cpl_msg_error("",
2044 "Unkown instrument %s, neither ERIS/SPIFFIER nor SINFONI",
2045 instrumentStr);
2046 SET_ERROR_MSG(CPL_ERROR_BAD_FILE_FORMAT,
2047 "Unkown instrument, neither ERIS/SPIFFIER nor SINFONI");
2048 instrument = OTHER_INSTRUMENT;
2049 }
2050 }
2051 CATCH
2052 {
2053 instrument = UNSET_INSTRUMENT;
2054 }
2055 eris_check_error_code("eris_ifu_get_instrument");
2056 return instrument;
2057}
2058/*----------------------------------------------------------------------------*/
2064ifsPreopticsScale
2066{
2067
2068 ifsPreopticsScale scale = UNDEFINED_SCALE;
2069 if (strcmp(scaleId, E_PREOPTICS_250MAS_SCALE) == 0) {
2070 scale = S250MAS;
2071 } else if (strcmp(scaleId, E_PREOPTICS_100MAS_SCALE) == 0) {
2072 scale = S100MAS;
2073 } else if (strcmp(scaleId, E_PREOPTICS_25MAS_SCALE) == 0) {
2074 scale = S25MAS;
2075 } else if (strcmp(scaleId, E_PREOPTICS_PUPIL) == 0) {
2076 scale = PUPIL;
2077 } else {
2078 scale = UNDEFINED_SCALE;
2079 }
2080 eris_check_error_code("eris_ifu_get_spiffier_preoptics_scale");
2081 return scale;
2082}
2083/*----------------------------------------------------------------------------*/
2089ifsPreopticsScale
2091{
2092 ifsPreopticsScale scale = UNDEFINED_SCALE;
2093 if (strcmp(scaleId, S_PREOPTICS_250MAS_SCALE) == 0) {
2094 scale = S250MAS;
2095 } else if (strcmp(scaleId, S_PREOPTICS_100MAS_SCALE) == 0) {
2096 scale = S100MAS;
2097 } else if (strcmp(scaleId, S_PREOPTICS_25MAS_SCALE) == 0) {
2098 scale = S25MAS;
2099 } else if (strcmp(scaleId, S_PREOPTICS_PUPIL) == 0) {
2100 scale = PUPIL;
2101 } else {
2102 scale = UNDEFINED_SCALE;
2103 }
2104 eris_check_error_code("eris_ifu_get_spiffi_preoptics_scale");
2105 return scale;
2106}
2107
2108/*----------------------------------------------------------------------------*/
2121/*----------------------------------------------------------------------------*/
2123 cpl_propertylist *header)
2124{
2125 ifsPreopticsScale scale = UNDEFINED_SCALE;
2126 ifsInstrument instrument = UNSET_INSTRUMENT;
2127 const char *scaleId;
2128
2129 TRY
2130 {
2131 instrument = eris_ifu_get_instrument(header);
2133 if (instrument == SPIFFIER) {
2134 scaleId = cpl_propertylist_get_string(header, FHDR_E_PREOPTICS_ID);
2137 } else if (instrument == SPIFFI) {
2138 scaleId = cpl_propertylist_get_string(header, FHDR_S_PREOPTICS_ID);
2140 scale = eris_ifu_get_spiffi_preoptics_scale(scaleId);
2141 }
2142
2143 }
2144 CATCH
2145 {
2146 scale = UNDEFINED_SCALE;
2147 }
2148 eris_check_error_code("eris_ifu_get_preopticsScale");
2149 return scale;
2150
2151}
2152
2153#define KEY_NAME_COMOFFS_RA "ESO OCS CUMOFFS RA"
2154#define KEY_NAME_COMOFFS_DEC "ESO OCS CUMOFFS DEC"
2155
2156
2157/*----------------------------------------------------------------------------*/
2165/*----------------------------------------------------------------------------*/
2166
2167double eris_get_cumoffs_ra(cpl_frame * frame)
2168{
2169 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, -1.0);
2170 cpl_propertylist* plist=NULL;
2171 char* file=NULL;
2172
2173 double result=0.;
2174 file = cpl_strdup( cpl_frame_get_filename(frame)) ;
2175
2176 if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2177 cpl_msg_error(cpl_func, "getting header from reference frame %s",file);
2178 cpl_propertylist_delete(plist) ;
2179 cpl_free(file);
2180 return -1 ;
2181 }
2182
2183 if (cpl_propertylist_has(plist, KEY_NAME_COMOFFS_RA)) {
2184 result=cpl_propertylist_get_double(plist, KEY_NAME_COMOFFS_RA);
2185 } else {
2186 cpl_msg_error(cpl_func, "keyword %s does not exist",KEY_NAME_COMOFFS_RA);
2187 cpl_propertylist_delete(plist) ;
2188 cpl_free(file);
2189 return -1;
2190 }
2191 cpl_propertylist_delete(plist) ;
2192 cpl_free(file);
2193 eris_check_error_code("eris_get_cumoffs_ra");
2194 return result;
2195
2196}
2197
2198
2199/*----------------------------------------------------------------------------*/
2207/*----------------------------------------------------------------------------*/
2208
2209double eris_get_cumoffs_dec(cpl_frame * frame)
2210{
2211 cpl_ensure(frame, CPL_ERROR_NULL_INPUT, -999.0);
2212 cpl_propertylist* plist=NULL;
2213 char* file=NULL;
2214
2215 double result=0.;
2216 file = cpl_strdup( cpl_frame_get_filename(frame)) ;
2217
2218 if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2219 cpl_msg_error(cpl_func, "getting header from reference frame %s",file);
2220 cpl_propertylist_delete(plist) ;
2221 cpl_free(file);
2222 return -1 ;
2223 }
2224
2225 if (cpl_propertylist_has(plist, KEY_NAME_COMOFFS_DEC)) {
2226 result=cpl_propertylist_get_double(plist, KEY_NAME_COMOFFS_DEC);
2227 } else {
2228 cpl_msg_error(cpl_func, "keyword %s does not exist",KEY_NAME_COMOFFS_DEC);
2229 cpl_propertylist_delete(plist) ;
2230 cpl_free(file);
2231 return -1;
2232 }
2233 cpl_propertylist_delete(plist) ;
2234 cpl_free(file);
2235 eris_check_error_code("eris_get_cumoffs_dec");
2236 return result;
2237
2238}
2239
2240/*----------------------------------------------------------------------------*/
2248/*----------------------------------------------------------------------------*/
2249double eris_get_mjd_obs(cpl_frame * frame)
2250{
2251 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, -1.0);
2252
2253 cpl_propertylist* plist=NULL;
2254 const char* file=NULL;
2255
2256 double mjd_obs=0.;
2257 file = cpl_frame_get_filename(frame) ;
2258
2259 if ((cpl_error_code)((plist = cpl_propertylist_load(file, 0)) == NULL)) {
2260 cpl_msg_error(cpl_func, "getting header from reference frame %s",file);
2261 cpl_propertylist_delete(plist) ;
2262 return -1.0;
2263 }
2264 if(cpl_propertylist_has(plist,"MJD-OBS")){
2265 mjd_obs = eris_pfits_get_mjdobs(plist);
2266 } else {
2267 cpl_propertylist_delete(plist) ;
2268 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
2269 return -1.0;
2270 }
2271
2272
2273 cpl_propertylist_delete(plist) ;
2274 eris_check_error_code("eris_get_mjd_obs");
2275 return mjd_obs;
2276
2277}
2278
2279/*---------------------------------------------------------------------------*/
2285/*---------------------------------------------------------------------------*/
2286
2287cpl_boolean eris_ifu_tag_is_cdb(char *tag)
2288{
2289 cpl_boolean result = CPL_FALSE;
2290
2291 if (tag == NULL) {
2292 return result;
2293 }
2294
2295 if ((strstr(ERIS_IFU_PRO_CALIB_TAGS, tag) != NULL) ||
2296 (strstr(ERIS_IFU_REF_CALIB_TAGS, tag) != NULL))
2297 {
2298 result = CPL_TRUE;
2299 }
2300
2301 return result;
2302}
2303
2304/*---------------------------------------------------------------------------*/
2310/*---------------------------------------------------------------------------*/
2311
2312cpl_boolean eris_ifu_tag_is_sky(const char *tag)
2313{
2314 cpl_boolean result = CPL_FALSE;
2315
2316 if (tag == NULL) {
2317 return result;
2318 }
2319
2320 char* tag_loc = (char*) tag;
2321 if ((strcmp(tag_loc, ERIS_IFU_RAW_SKY) == 0) ||
2322 (strcmp(tag_loc, ERIS_IFU_RAW_OBJ_SKY) == 0) ||
2323 (strcmp(tag_loc, ERIS_IFU_RAW_STD_FLUX_SKY) == 0) ||
2324 (strcmp(tag_loc, ERIS_IFU_RAW_STD_SKY) == 0) ||
2325 (strcmp(tag_loc, ERIS_IFU_RAW_PSF_SKY) == 0) ||
2326 (strcmp(tag_loc, ERIS_IFU_RAW_PUPIL_SKY) == 0))
2327 {
2328 result = CPL_TRUE;
2329 }
2330
2331 return result;
2332}
2333
2339cpl_boolean eris_ifu_tag_is_obj(const char *tag)
2340{
2341 cpl_boolean result = CPL_FALSE;
2342
2343 if (tag == NULL) {
2344 return result;
2345 }
2346
2347 char* tag_loc = (char*) tag;
2348 if ((strcmp(tag_loc, ERIS_IFU_RAW_OBJ) == 0) ||
2349 (strcmp(tag_loc, ERIS_IFU_RAW_STD) == 0) ||
2350 (strcmp(tag_loc, ERIS_IFU_RAW_STD_FLUX) == 0) ||
2351 (strcmp(tag_loc, ERIS_IFU_RAW_PSF) == 0) ||
2352 (strcmp(tag_loc, ERIS_IFU_RAW_PUPIL_LAMP) == 0))
2353 {
2354 result = CPL_TRUE;
2355 }
2356
2357 return result;
2358}
2359
2366cpl_error_code
2367eris_ifu_extract_obj_frames(const cpl_frameset * sof, cpl_frameset* obj)
2368{
2369 cpl_ensure_code(sof, CPL_ERROR_NULL_INPUT);
2370 cpl_ensure_code(obj, CPL_ERROR_NULL_INPUT);
2371
2372 cpl_size nsof = cpl_frameset_get_size(sof);
2373
2374 for (cpl_size i = 0 ; i < nsof ; i++) {
2375 const cpl_frame* frame = cpl_frameset_get_position_const(sof, i);
2376 cpl_frame_group group = cpl_frame_get_group(frame);
2377
2378 if(group == CPL_FRAME_GROUP_RAW) {
2379
2380 //cpl_msg_info(cpl_func,"fname: %s", cpl_frame_get_filename(frame));
2381 const char* tag = cpl_frame_get_tag(frame);
2382
2383 if(eris_ifu_tag_is_obj(tag)) {
2384
2385 cpl_frame* frame_dup = cpl_frame_duplicate(frame);
2386 cpl_frameset_insert(obj, frame_dup);
2387
2388 }
2389 }
2390
2391 }
2392
2393 eris_check_error_code("eris_ifu_extract_obj_frames");
2394 return cpl_error_get_code();
2395}
2396
2397/*---------------------------------------------------------------------------*/
2404/*---------------------------------------------------------------------------*/
2405cpl_error_code
2406eris_ifu_extract_sky_frames(const cpl_frameset * sof, cpl_frameset* sky)
2407{
2408 cpl_ensure_code(sof, CPL_ERROR_NULL_INPUT);
2409 cpl_ensure_code(sky, CPL_ERROR_NULL_INPUT);
2410
2411 cpl_size nsof = cpl_frameset_get_size(sof);
2412 for (cpl_size i = 0 ; i < nsof ; i++) {
2413 const cpl_frame* frame = cpl_frameset_get_position_const(sof, i);
2414 cpl_frame_group group = cpl_frame_get_group(frame);
2415
2416 if(group == CPL_FRAME_GROUP_RAW) {
2417 //cpl_msg_info(cpl_func,"fname: %s", cpl_frame_get_filename(frame));
2418 const char* tag = cpl_frame_get_tag(frame);
2419 if(eris_ifu_tag_is_sky(tag)) {
2420 cpl_frame* frame_dup = cpl_frame_duplicate(frame);
2421 cpl_frameset_insert(sky, frame_dup);
2422 }
2423
2424 }
2425 }
2426
2427 eris_check_error_code("eris_ifu_extract_sky_frames");
2428 return cpl_error_get_code();
2429}
2430
2431/*---------------------------------------------------------------------------*/
2438/*---------------------------------------------------------------------------*/
2439cpl_error_code
2440eris_ifu_extract_mst_frames(const cpl_frameset * sof, cpl_frameset* cdb)
2441{
2442 cpl_ensure_code(sof, CPL_ERROR_NULL_INPUT);
2443 cpl_ensure_code(cdb, CPL_ERROR_NULL_INPUT);
2444
2445 cpl_size nsof = cpl_frameset_get_size(sof);
2446 for (cpl_size i = 0 ; i < nsof ; i++) {
2447 const cpl_frame* frame = cpl_frameset_get_position_const(sof,i);
2448 cpl_frame_group group = cpl_frame_get_group(frame);
2449
2450 if(group == CPL_FRAME_GROUP_CALIB) {
2451 /* to go on the file must exist */
2452 if(cpl_frame_get_tag(frame) != NULL) {
2453 /* If the frame has a tag we process it. Else it is an object */
2454 char* tag= (char*) cpl_frame_get_tag(frame);
2455 if(eris_ifu_tag_is_cdb(tag) == 1) {
2456 cpl_frame* frame_dup = cpl_frame_duplicate(frame);
2457 cpl_frameset_insert(cdb, frame_dup);
2458 }
2459 }
2460 }
2461 }
2462
2463 eris_check_error_code("eris_ifu_extract_mst_frames");
2464 return cpl_error_get_code();
2465}
2466
2467
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:796
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)
Function to load slitlet positions.
float eris_ifu_get_dit(cpl_propertylist *header)
Determine if a frame is a Sky frame or not.
cpl_error_code eris_ifu_heades_add_hduclass_qual(cpl_propertylist *plist, deqQualityType qualityType)
add common quality info properties on errors
Definition: eris_ifu_dfs.c:265
bool eris_ifu_frame_is_sky(const cpl_frame *fr)
Determine if a frame is a Sky frame or not.
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)
Function to load an imagelist with error an quality extensions.
Definition: eris_ifu_dfs.c:901
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)
Function to load distortion polynomials.
cpl_frameset * eris_ifu_extract_frameset(const cpl_frameset *in, const char *tag)
Extract the frames with the given tag from a frameset.
Definition: eris_ifu_dfs.c:171
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)
Write DFS pipeline product with image, error and data qual.
Definition: eris_ifu_dfs.c:586
hdrl_image * eris_ifu_load_cal_image_frame(const cpl_frame *frame, ifsBand band, ifsPreopticsScale scale, cpl_image **qualImage, deqQualityType *qualType)
Function to load a calibration image 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)
Function to load a calibration image 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)
Function to load an HDRL imagelist with quality extensions.
cpl_error_code eris_ifu_heades_add_hduclass_common(cpl_propertylist *plist, const char *deq_hduclas)
add common properties
Definition: eris_ifu_dfs.c:211
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)
Function to load an HDRL image with quality extensions.
cpl_error_code eris_ifu_dfs_set_groups(cpl_frameset *self)
Set the group as RAW or CALIB in a frameset.
Definition: eris_ifu_dfs.c:63
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 a QC parameter of type INT to 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)
Write DFS pipeline product with image, error and data qual.
Definition: eris_ifu_dfs.c:300
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)
Write DFS pipeline product with image, error and data qual.
Definition: eris_ifu_dfs.c:441
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)
Write DFS pipeline product with table.
Definition: eris_ifu_dfs.c:660
cpl_error_code eris_ifu_heades_add_hduclass_data(cpl_propertylist *plist)
add common data info properties
Definition: eris_ifu_dfs.c:229
cpl_vector * eris_ifu_load_distances(const char *filename)
Function to load slitlet distances.
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 common errors info properties
Definition: eris_ifu_dfs.c:246
cpl_error_code eris_ifu_load_distortion_polynomials_old(const char *filename, cpl_polynomial **poly_u, cpl_polynomial **poly_v)
Function to load distortion polynomials.
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 is obtained with 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)
Return value of INSTRUME from a given input 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
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.