CR2RE Pipeline Reference Manual 1.6.7
cr2res_io.c
1/*
2 * This file is part of the CR2RES Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include <string.h>
29#include <math.h>
30
31#include <cpl.h>
32
33#include "irplib_wlxcorr.h"
34
35#include "cr2res_io.h"
36#include "cr2res_io_eop_table.h"
37#include "cr2res_idp.h"
38#include "cr2res_utils.h"
39#include "cr2res_dfs.h"
40#include "cr2res_pfits.h"
41#include "cr2res_bpm.h"
42#include "cr2res_qc.h"
43
44/*-----------------------------------------------------------------------------
45 Functions prototypes
46 -----------------------------------------------------------------------------*/
47
48static int cr2res_io_set_bpm_as_NaNs(
49 cpl_image * in) ;
50static int cr2res_io_set_NaNs_as_bpm(
51 cpl_image * in) ;
52static int cr2res_io_check_drs_type(
53 const char * filename,
54 const char * expected_drstype) ;
55static cpl_imagelist * cr2res_hdrl_to_cpl_list(
56 const hdrl_imagelist * in,
57 int data) ;
58static int cr2res_io_save_imagelist(
59 const char * filename,
60 cpl_frameset * allframes,
61 cpl_frameset * inframes,
62 const cpl_parameterlist * parlist,
63 hdrl_imagelist ** data,
64 const cpl_propertylist * qc_list,
65 cpl_propertylist ** ext_plist,
66 cpl_type type,
67 const char * recipe,
68 const char * procatg,
69 const char * drstype) ;
70
71static int cr2res_io_save_image(
72 const char * filename,
73 cpl_frameset * allframes,
74 cpl_frameset * inframes,
75 const cpl_parameterlist * parlist,
76 hdrl_image ** data,
77 const cpl_propertylist * qc_list,
78 cpl_propertylist ** ext_plist,
79 cpl_type type,
80 const char * recipe,
81 const char * procatg,
82 const char * drstype) ;
83static int cr2res_io_save_table(
84 const char * filename,
85 cpl_frameset * allframes,
86 cpl_frameset * inframes,
87 const cpl_parameterlist * parlist,
88 cpl_table ** slit_func,
89 const cpl_propertylist * qc_list,
90 cpl_propertylist ** ext_plist,
91 const char * recipe,
92 const char * procatg,
93 const char * drstype) ;
94static int cr2res_io_save_one_table(
95 const char * filename,
96 cpl_frameset * allframes,
97 cpl_frameset * inframes,
98 const cpl_parameterlist * parlist,
99 cpl_table * tab,
100 const cpl_propertylist * qc_list,
101 cpl_propertylist * ext_plist,
102 const char * extname,
103 const char * recipe,
104 const char * procatg,
105 const char * drstype) ;
106
107/*----------------------------------------------------------------------------*/
111/*----------------------------------------------------------------------------*/
112
114/*----------------------------------------------------------------------------*/
119/*----------------------------------------------------------------------------*/
121{
122 cpl_table * eop_table;
123 cpl_size i ;
124
125 eop_table = cpl_table_new(EOP_TABLE_SIZE);
126 cpl_table_new_column(eop_table, "MJD", CPL_TYPE_DOUBLE) ;
127 cpl_table_new_column(eop_table, "PMX", CPL_TYPE_DOUBLE) ;
128 cpl_table_new_column(eop_table, "PMY", CPL_TYPE_DOUBLE) ;
129 cpl_table_new_column(eop_table, "DUT", CPL_TYPE_DOUBLE) ;
130 cpl_table_new_column(eop_table, "FLAG", CPL_TYPE_STRING) ;
131 for (i=0; i<EOP_TABLE_SIZE; i++) {
132 cpl_table_set_double(eop_table, "MJD", i,
133 eop_table_double_entries[i][0]) ;
134 cpl_table_set_double(eop_table, "PMX", i,
135 eop_table_double_entries[i][1]) ;
136 cpl_table_set_double(eop_table, "PMY", i,
137 eop_table_double_entries[i][2]) ;
138 cpl_table_set_double(eop_table, "DUT", i,
139 eop_table_double_entries[i][3]) ;
140 cpl_table_set_string(eop_table, "FLAG", i,
141 eop_table_string_entries[i]) ;
142 }
143
144 /*
145 cpl_table_save(eop_table, NULL, NULL, "test.fits", CPL_IO_CREATE);
146
147 In order to by-pass the usage of cr2res_io_eop_table.h, load
148 directly the FITS cr2rep/catalogs/esotk_eop_param.fits file.
149 -> Uncomment the 2 following lines
150
151 cpl_table_delete(eop_table);
152 eop_table = cpl_table_load("/home/yjung/P_cr2res/cr2re/cr2rep/catalogs/esotk_eop_param.fits", 1,0);
153 */
154
155 return eop_table ;
156}
157
158/*----------------------------------------------------------------------------*/
166/*----------------------------------------------------------------------------*/
168 const cpl_frameset * in,
169 const char * setting_id,
170 cr2res_decker decker)
171{
172 cpl_frameset * fset ;
173 char ** tags ;
174 int ntags ;
175 cpl_frame * out ;
176 const cpl_frame * cur_frame ;
177 const char * cur_fname ;
178 cpl_propertylist * plist ;
179 cr2res_decker cur_decker ;
180 char * cur_setting ;
181 int i ;
182
183 /* Check entries */
184 if (in == NULL) return NULL ;
185
186 /* Initialise */
187 out = NULL ;
188
189 /* Create the tags list */
190 ntags = 4 ;
191 tags = cpl_malloc(ntags * sizeof(char *)) ;
192 tags[0] = cpl_sprintf(CR2RES_CAL_FLAT_SLIT_MODEL_PROCATG) ;
193 tags[1] = cpl_sprintf(CR2RES_UTIL_SLIT_MODEL_PROCATG) ;
194 tags[2] = cpl_sprintf( CR2RES_OBS_NODDING_SLITMODELA_PROCATG) ;
195 tags[3] = cpl_sprintf(CR2RES_OBS_NODDING_SLITMODELB_PROCATG) ;
196
197 /* Get the frameset */
198 fset = cr2res_extract_frameset_several_tags(in, (const char**)tags, ntags) ;
199
200 for (i=0 ; i<ntags ; i++) cpl_free(tags[i]) ;
201 cpl_free(tags) ;
202 if (fset == NULL) return NULL ;
203
204 /* Find out if there is a matching one */
205 for (i=0 ; i<cpl_frameset_get_size(fset) ; i++) {
206 if (out == NULL) {
207 /* Get the Current Frame */
208 cur_frame = cpl_frameset_get_position(fset, i) ;
209 cur_fname = cpl_frame_get_filename(cur_frame) ;
210 plist = cpl_propertylist_load(cur_fname, 0);
211 cur_setting = cpl_strdup(cr2res_pfits_get_wlen_id(plist)) ;
212 cr2res_format_setting(cur_setting) ;
213 cur_decker = cr2res_pfits_get_decker_position(plist) ;
214 cpl_propertylist_delete(plist) ;
215 if (!strcmp(cur_setting, setting_id) && cur_decker == decker)
216 out = cpl_frame_duplicate(cur_frame) ;
217 cpl_free(cur_setting) ;
218 }
219 }
220 cpl_frameset_delete(fset) ;
221 return out ;
222}
223
224/*----------------------------------------------------------------------------*/
230/*----------------------------------------------------------------------------*/
231const cpl_frame * cr2res_io_find_TRACE_WAVE(const cpl_frameset * in)
232{
233 const cpl_frame * out ;
234
235 /* Check entries */
236 if (in == NULL) return NULL ;
237
238 out=cpl_frameset_find_const(in, CR2RES_CAL_FLAT_TW_PROCATG) ;
239 if (out == NULL)
240 out=cpl_frameset_find_const(in, CR2RES_CAL_FLAT_TW_MERGED_PROCATG) ;
241 if (out == NULL)
242 out=cpl_frameset_find_const(in, CR2RES_UTIL_TRACE_TW_PROCATG) ;
243 if (out == NULL)
244 out=cpl_frameset_find_const(in, CR2RES_UTIL_WAVE_TW_PROCATG) ;
245 if (out == NULL)
246 out=cpl_frameset_find_const(in, CR2RES_CAL_WAVE_TW_PROCATG) ;
247 if (out == NULL)
248 out=cpl_frameset_find_const(in, CR2RES_UTIL_SLIT_CURV_TW_PROCATG);
249 if (out == NULL)
250 out=cpl_frameset_find_const(in, CR2RES_OBS_NODDING_TWA_PROCATG);
251 if (out == NULL)
252 out=cpl_frameset_find_const(in, CR2RES_OBS_NODDING_TWB_PROCATG);
253 if (out == NULL)
254 out=cpl_frameset_find_const(in, CR2RES_OBS_POL_TWA_PROCATG);
255 if (out == NULL)
256 out=cpl_frameset_find_const(in, CR2RES_OBS_POL_TWB_PROCATG);
257 return out ;
258}
259
260/*----------------------------------------------------------------------------*/
266/*----------------------------------------------------------------------------*/
267const cpl_frame * cr2res_io_find_SLIT_FUNC(const cpl_frameset * in)
268{
269 const cpl_frame * out ;
270
271 /* Check entries */
272 if (in == NULL) return NULL ;
273
274 out=cpl_frameset_find_const(in, CR2RES_CAL_FLAT_SLIT_FUNC_PROCATG) ;
275 if (out == NULL)
276 out=cpl_frameset_find_const(in, CR2RES_UTIL_SLIT_FUNC_PROCATG) ;
277 if (out == NULL)
278 out=cpl_frameset_find_const(in, CR2RES_OBS_NODDING_SLITFUNCA_PROCATG) ;
279 if (out == NULL)
280 out=cpl_frameset_find_const(in, CR2RES_OBS_NODDING_SLITFUNCB_PROCATG) ;
281 if (out == NULL)
282 out=cpl_frameset_find_const(in, CR2RES_OBS_STARING_SLITFUNC_PROCATG) ;
283 return out ;
284}
285
286/*----------------------------------------------------------------------------*/
292/*----------------------------------------------------------------------------*/
293cpl_frameset * cr2res_io_find_TRACE_WAVE_all(const cpl_frameset * in)
294{
295 cpl_frameset * out ;
296 char ** tags ;
297 int i, ntags ;
298
299 /* Check entries */
300 if (in == NULL) return NULL ;
301
302 /* Create the tags list */
303 ntags = 10 ;
304 tags = cpl_malloc(ntags * sizeof(char *)) ;
305 tags[0] = cpl_sprintf(CR2RES_CAL_FLAT_TW_PROCATG) ;
306 tags[1] = cpl_sprintf(CR2RES_CAL_FLAT_TW_MERGED_PROCATG) ;
307 tags[2] = cpl_sprintf(CR2RES_UTIL_TRACE_TW_PROCATG) ;
308 tags[3] = cpl_sprintf(CR2RES_UTIL_WAVE_TW_PROCATG) ;
309 tags[4] = cpl_sprintf(CR2RES_CAL_WAVE_TW_PROCATG) ;
310 tags[5] = cpl_sprintf(CR2RES_UTIL_SLIT_CURV_TW_PROCATG) ;
311 tags[6] = cpl_sprintf(CR2RES_OBS_NODDING_TWA_PROCATG) ;
312 tags[7] = cpl_sprintf(CR2RES_OBS_NODDING_TWB_PROCATG) ;
313 tags[8] = cpl_sprintf(CR2RES_OBS_POL_TWA_PROCATG) ;
314 tags[9] = cpl_sprintf(CR2RES_OBS_POL_TWB_PROCATG) ;
315
316 /* Get the frameset */
317 out = cr2res_extract_frameset_several_tags(in, (const char**)tags, ntags) ;
318
319 for (i=0 ; i<ntags ; i++) cpl_free(tags[i]) ;
320 cpl_free(tags) ;
321 return out ;
322}
323
324/*----------------------------------------------------------------------------*/
330/*----------------------------------------------------------------------------*/
331cpl_frameset * cr2res_io_find_EXTRACT_1D_all(const cpl_frameset * in)
332{
333 cpl_frameset * out ;
334 char ** tags ;
335 int i, ntags ;
336
337 /* Check entries */
338 if (in == NULL) return NULL ;
339
340 /* Create the tags list */
341 ntags = 10 ;
342 tags = cpl_malloc(ntags * sizeof(char *)) ;
343 tags[0] = cpl_sprintf(CR2RES_CAL_FLAT_EXTRACT_1D_PROCATG) ;
344 tags[1] = cpl_sprintf(CR2RES_UTIL_EXTRACT_1D_PROCATG) ;
345 tags[2] = cpl_sprintf(CR2RES_UTIL_WAVE_EXTRACT_1D_PROCATG) ;
346 tags[3] = cpl_sprintf(CR2RES_CAL_WAVE_EXTRACT_1D_PROCATG) ;
347 tags[4] = cpl_sprintf(CR2RES_OBS_NODDING_EXTRACTA_PROCATG) ;
348 tags[5] = cpl_sprintf(CR2RES_OBS_NODDING_EXTRACTB_PROCATG) ;
349 tags[6] = cpl_sprintf(CR2RES_OBS_NODDING_EXTRACTC_PROCATG) ;
350 tags[7] = cpl_sprintf(CR2RES_OBS_STARING_EXTRACT_PROCATG) ;
351 tags[8] = cpl_sprintf(CR2RES_OBS_POL_EXTRACTA_PROCATG) ;
352 tags[9] = cpl_sprintf(CR2RES_OBS_POL_EXTRACTB_PROCATG) ;
353
354 /* Get the frameset */
355 out = cr2res_extract_frameset_several_tags(in, (const char**)tags, ntags) ;
356
357 for (i=0 ; i<ntags ; i++) cpl_free(tags[i]) ;
358 cpl_free(tags) ;
359 return out ;
360}
361
362/*----------------------------------------------------------------------------*/
368/*----------------------------------------------------------------------------*/
369const cpl_frame * cr2res_io_find_BPM(const cpl_frameset * in)
370{
371 const cpl_frame * out ;
372
373 /* Check entries */
374 if (in == NULL) return NULL ;
375
376 out=cpl_frameset_find_const(in, CR2RES_CAL_DARK_BPM_PROCATG) ;
377 if (out == NULL)
378 out = cpl_frameset_find_const(in, CR2RES_CAL_FLAT_BPM_PROCATG) ;
379 if (out == NULL)
380 out = cpl_frameset_find_const(in, CR2RES_CAL_DETLIN_BPM_PROCATG) ;
381 if (out == NULL)
382 out = cpl_frameset_find_const(in, CR2RES_UTIL_BPM_MERGE_PROCATG) ;
383 if (out == NULL)
384 out = cpl_frameset_find_const(in, CR2RES_UTIL_BPM_SPLIT_PROCATG) ;
385 if (out == NULL)
386 out = cpl_frameset_find_const(in, CR2RES_UTIL_NORM_BPM_PROCATG) ;
387 return out ;
388}
389
390/*----------------------------------------------------------------------------*/
396/*----------------------------------------------------------------------------*/
397cpl_frameset * cr2res_io_find_BPM_all(const cpl_frameset * in)
398{
399 cpl_frameset * out ;
400 char ** tags ;
401 int i, ntags ;
402
403 /* Check entries */
404 if (in == NULL) return NULL ;
405
406 /* Create the tags list */
407 ntags = 6 ;
408 tags = cpl_malloc(ntags * sizeof(char *)) ;
409 tags[0] = cpl_sprintf(CR2RES_CAL_DARK_BPM_PROCATG) ;
410 tags[1] = cpl_sprintf(CR2RES_CAL_FLAT_BPM_PROCATG) ;
411 tags[2] = cpl_sprintf(CR2RES_CAL_DETLIN_BPM_PROCATG) ;
412 tags[3] = cpl_sprintf(CR2RES_UTIL_BPM_MERGE_PROCATG) ;
413 tags[4] = cpl_sprintf(CR2RES_UTIL_BPM_SPLIT_PROCATG) ;
414 tags[5] = cpl_sprintf(CR2RES_UTIL_NORM_BPM_PROCATG) ;
415
416 /* Get the frameset */
417 out = cr2res_extract_frameset_several_tags(in, (const char**)tags, ntags) ;
418
419 for (i=0 ; i<ntags ; i++) cpl_free(tags[i]) ;
420 cpl_free(tags) ;
421
422 return out ;
423}
424
425/*----------------------------------------------------------------------------*/
431/*----------------------------------------------------------------------------*/
432cpl_vector * cr2res_io_read_dits(const cpl_frameset * in)
433{
434 cpl_vector * dits ;
435 cpl_size i ;
436
437 /* Check entries */
438 if (in == NULL) return NULL ;
439
440 /* Allocate the vector */
441 dits = cpl_vector_new(cpl_frameset_get_size(in)) ;
442
443 /* Loop on the frames */
444 for (i=0 ; i< cpl_vector_get_size(dits) ; i++) {
445 cpl_propertylist * plist ;
446 plist = cpl_propertylist_load(cpl_frame_get_filename(
447 cpl_frameset_get_position_const(in, i)), 0) ;
448 cpl_vector_set(dits, i, cr2res_pfits_get_dit(plist)) ;
449 cpl_propertylist_delete(plist) ;
450 }
451
452 return dits ;
453}
454
455/*----------------------------------------------------------------------------*/
461/*----------------------------------------------------------------------------*/
462cpl_vector * cr2res_io_read_ndits(const cpl_frameset * in)
463{
464 cpl_vector * ndits ;
465 cpl_size i ;
466
467 /* Check entries */
468 if (in == NULL) return NULL ;
469
470 /* Allocate the vector */
471 ndits = cpl_vector_new(cpl_frameset_get_size(in)) ;
472
473 /* Loop on the frames */
474 for (i=0 ; i< cpl_vector_get_size(ndits) ; i++) {
475 cpl_propertylist * plist ;
476 plist = cpl_propertylist_load(cpl_frame_get_filename(
477 cpl_frameset_get_position_const(in, i)), 0) ;
478 cpl_vector_set(ndits, i, cr2res_pfits_get_ndit(plist)) ;
479 cpl_propertylist_delete(plist) ;
480 }
481
482 return ndits ;
483}
484/*----------------------------------------------------------------------------*/
490/*----------------------------------------------------------------------------*/
491cr2res_decker * cr2res_io_read_decker_positions(const cpl_frameset * in)
492{
493 cr2res_decker * out ;
494 cpl_size nframes, i ;
495
496 /* Check entries */
497 if (in == NULL) return NULL ;
498
499 /* Initialise */
500 nframes = cpl_frameset_get_size(in) ;
501
502 /* Allocate the vector */
503 out = cpl_malloc(nframes * sizeof(cr2res_decker)) ;
504
505 /* Loop on the frames */
506 for (i = 0; i < nframes; i++) {
507 cpl_propertylist *plist;
508 plist = cpl_propertylist_load(cpl_frame_get_filename(
509 cpl_frameset_get_position_const(in, i)), 0) ;
510 out[i] = cr2res_pfits_get_decker_position(plist) ;
511 cpl_propertylist_delete(plist) ;
512 }
513 return out ;
514}
515
516/*----------------------------------------------------------------------------*/
526/*----------------------------------------------------------------------------*/
528 const cpl_frameset * in,
529 const char * tag,
530 cr2res_decker decker)
531{
532 cpl_frameset * out ;
533 cpl_frame * loc_frame ;
534 int nbframes;
535 cpl_propertylist * plist ;
536 int i ;
537
538 /* Test entries */
539 if (in == NULL) return NULL ;
540 if (tag == NULL) return NULL ;
541 if (decker != CR2RES_DECKER_NONE && decker != CR2RES_DECKER_1_3 &&
542 decker != CR2RES_DECKER_2_4)
543 return NULL ;
544
545 /* Initialise */
546 nbframes = cpl_frameset_get_size(in) ;
547
548 /* Count the frames with the tag */
549 if ((cpl_frameset_count_tags(in, tag)) == 0) return NULL ;
550
551 /* Create the output frameset */
552 out = cpl_frameset_new() ;
553
554 /* Loop on the requested frames and store them in out */
555 for (i=0 ; i<nbframes ; i++) {
556 const cpl_frame * cur_frame ;
557 cur_frame = cpl_frameset_get_position_const(in, i) ;
558 if (!strcmp(cpl_frame_get_tag(cur_frame), tag)) {
559 /* Get the propertylist */
560 plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
561 if (cr2res_pfits_get_decker_position(plist) == decker) {
562 loc_frame = cpl_frame_duplicate(cur_frame) ;
563 cpl_frameset_insert(out, loc_frame) ;
564 }
565 cpl_propertylist_delete(plist) ;
566 }
567 }
568 /* No matching frame found */
569 if (cpl_frameset_get_size(out) == 0){
570 cpl_frameset_delete(out) ;
571 return NULL ;
572 }
573 return out ;
574}
575/*----------------------------------------------------------------------------*/
582/*----------------------------------------------------------------------------*/
584{
585 /* Check entries */
586 if (order_idx < -49 || order_idx > 50) return -1 ;
587
588 /* Conversion order_idx -> order_idxp */
589 if (order_idx < 0) return order_idx + 100 ;
590 else return order_idx ;
591}
592#ifdef CR2RES_UNUSED
593/*----------------------------------------------------------------------------*/
599/*----------------------------------------------------------------------------*/
600int cr2res_io_convert_order_idxp_to_idx(int order_idxp)
601{
602 /* Check entries */
603 if (order_idxp < 0 || order_idxp > 99) return -1 ;
604
605 /* Conversion order <-> keyword Index */
606 if (order_idxp > 50) return order_idxp - 100 ;
607 else return order_idxp ;
608}
609#endif
610/*----------------------------------------------------------------------------*/
617/*----------------------------------------------------------------------------*/
619 int detector,
620 int data)
621{
622 char * wished_extname ;
623
624 /* Check entries */
625 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
626
627 /* Create wished EXTNAME */
628 /* If this changes, update warning message in cr2res_io_get_ext_idx() */
629 if (data) wished_extname = cpl_sprintf("CHIP%d.INT1", detector) ;
630 else wished_extname = cpl_sprintf("CHIP%dERR.INT1", detector) ;
631
632 return wished_extname ;
633}
634
635/*----------------------------------------------------------------------------*/
643/*----------------------------------------------------------------------------*/
645 const char * filename,
646 int detector,
647 int data)
648{
649 char * wished_extname ;
650 int wished_ext_nb = -1 ;
651 int nb_ext, i ;
652
653 /* Check entries */
654 if (filename == NULL) return -1 ;
655 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return -1 ;
656
657 /* Create wished EXTNAME */
658 wished_extname = cr2res_io_create_extname(detector, data) ;
659
660 /* Get the number of extensions */
661 nb_ext = cpl_fits_count_extensions(filename) ;
662
663 /* Loop on the extensions */
664 for (i=1 ; i<=nb_ext ; i++) {
665 const char * extname ;
666 cpl_propertylist * pl ;
667 /* Get the header */
668 pl = cpl_propertylist_load(filename, i) ;
669 /* Read the EXTNAME */
670 extname = cpl_propertylist_get_string(pl, "EXTNAME");
671
672 /* Compare to the wished one */
673 if (strcmp(extname, wished_extname)==0) wished_ext_nb = i ;
674 cpl_propertylist_delete(pl) ;
675 }
676 cpl_free(wished_extname) ;
677
678 /* EXTNAME expectation */
679 if (wished_ext_nb < 0) {
680 /* Warning only for data - error are sometimes optional */
681 if (data == 1)
682 cpl_msg_warning(__func__,
683 "EXTNAME is supposed to match CHIPn.INT1 or CHIPnERR.INT1 (n=1/2/3)") ;
684 }
685
686 return wished_ext_nb ;
687}
688
689/*----------------------------------------------------------------------------*/
690/*-------------------- LOADING FUNCTIONS -------------------------*/
691/*----------------------------------------------------------------------------*/
692
693/*----------------------------------------------------------------------------*/
703/*----------------------------------------------------------------------------*/
705 const char * in,
706 int detector)
707{
708 hdrl_image * out ;
709 cpl_image * data ;
710 cpl_image * err ;
711 int ext_nr_data, ext_nr_err ;
712
713 /* Check entries */
714 if (in == NULL) return NULL ;
715 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
716
717 /* Get the extension numbers for this detector */
718 ext_nr_data = cr2res_io_get_ext_idx(in, detector, 1) ;
719 ext_nr_err = cr2res_io_get_ext_idx(in, detector, 0) ;
720
721 /* The wished extension was not found */
722 if (ext_nr_data < 0) return NULL ;
723
724 /* Load the image */
725 data = cpl_image_load(in, CPL_TYPE_DOUBLE, 0, ext_nr_data) ;
726 if (ext_nr_err >= 0)
727 err = cpl_image_load(in, CPL_TYPE_DOUBLE, 0, ext_nr_err) ;
728 else
729 err = NULL ;
730
731 /* Set the NaN pixels as bad */
732 cr2res_io_set_NaNs_as_bpm(data) ;
733
734 /* Create output hdrl image */
735 out = hdrl_image_create(data, err) ;
736
737 /* Return */
738 if (data != NULL) cpl_image_delete(data) ;
739 if (err != NULL) cpl_image_delete(err) ;
740 return out ;
741}
742
743/*----------------------------------------------------------------------------*/
753/*----------------------------------------------------------------------------*/
755 const char * in,
756 int detector)
757{
758 hdrl_imagelist * out ;
759 cpl_imagelist * data ;
760 cpl_imagelist * error ;
761 cpl_size i ;
762 int wished_ext_nb_data, wished_ext_nb_error ;
763
764 /* Check entries */
765 if (in == NULL) return NULL ;
766 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
767
768 /* Get the extension number for this detector */
769 wished_ext_nb_data = cr2res_io_get_ext_idx(in, detector, 1) ;
770 wished_ext_nb_error = cr2res_io_get_ext_idx(in, detector, 0) ;
771
772 /* The wished extension was not found */
773 if (wished_ext_nb_data < 0) return NULL ;
774
775 /* Load the image list */
776 data = cpl_imagelist_load(in, CPL_TYPE_DOUBLE, wished_ext_nb_data);
777 if (wished_ext_nb_error >= 0)
778 error = cpl_imagelist_load(in, CPL_TYPE_DOUBLE, wished_ext_nb_error);
779 else
780 error = NULL ;
781
782 /* Set the NaN pixels as bad */
783 for (i=0 ; i<cpl_imagelist_get_size(data) ; i++)
784 cr2res_io_set_NaNs_as_bpm(cpl_imagelist_get(data, i)) ;
785
786 /* Create output hdrl image */
787 out = hdrl_imagelist_create(data, error) ;
788
789 /* Return */
790 if (data != NULL) cpl_imagelist_delete(data) ;
791 if (error != NULL) cpl_imagelist_delete(error) ;
792 return out ;
793}
794
795/*----------------------------------------------------------------------------*/
807/*----------------------------------------------------------------------------*/
809 const cpl_frameset * in,
810 int detector)
811{
812 const char * first_file ;
813 hdrl_imagelist * out ;
814 cpl_imagelist * data ;
815 cpl_imagelist * err ;
816 cpl_size i ;
817 int ext_nr_data, ext_nr_err ;
818
819 /* Check entries */
820 if (in == NULL) return NULL ;
821 if (cpl_frameset_get_size(in) < 1) return NULL ;
822 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
823
824 /* Get the extension number for this detector */
825 first_file = cpl_frame_get_filename(cpl_frameset_get_position_const(in,0)) ;
826 ext_nr_data = cr2res_io_get_ext_idx(first_file, detector, 1) ;
827 ext_nr_err = cr2res_io_get_ext_idx(first_file, detector, 0) ;
828
829 /* The wished extension was not found */
830 if (ext_nr_data < 0) return NULL ;
831
832 /* Load the image list */
833 data = cpl_imagelist_load_frameset(in, CPL_TYPE_DOUBLE, 1, ext_nr_data) ;
834 if (ext_nr_err >= 0)
835 err = cpl_imagelist_load_frameset(in, CPL_TYPE_DOUBLE, 1, ext_nr_err) ;
836 else
837 err = NULL ;
838
839 /* Set the NaN pixels as bad */
840 for (i=0 ; i<cpl_imagelist_get_size(data) ; i++)
841 cr2res_io_set_NaNs_as_bpm(cpl_imagelist_get(data, i)) ;
842
843 /* Create output hdrl image */
844 out = hdrl_imagelist_create(data, err) ;
845
846 /* Return */
847 if (data != NULL) cpl_imagelist_delete(data) ;
848 if (err != NULL) cpl_imagelist_delete(err) ;
849 return out ;
850}
851
852/*----------------------------------------------------------------------------*/
861/*----------------------------------------------------------------------------*/
863 const char * in,
864 int det_nr,
865 int pmin,
866 int pmax)
867{
868 cpl_table * out ;
869 cpl_table * out_tmp ;
870 //int ext_nr ;
871
872 /* Check entries */
873 if (in == NULL) return NULL ;
874 if (det_nr < 1 || det_nr > CR2RES_NB_DETECTORS) return NULL ;
875
876 /* Get the extension number for this detector */
877 //ext_nr = cr2res_io_get_ext_idx(in, det_nr, 1) ;
878
879 /* Load the table */
880 if ((out = cpl_table_load(in, det_nr, 0)) == NULL) {
881 cpl_msg_error(__func__, "Cannot load %s as a table", in) ;
882 return NULL ;
883 }
884
885 /* Select only between pmin and pmax */
886 if (pmin>0 && pmax>0 && pmax >= pmin && pmax <= cpl_table_get_nrow(out)) {
887 out_tmp = cpl_table_extract(out, pmin, pmax-pmin+1) ;
888 if (out_tmp != NULL) {
889 cpl_table_delete(out) ;
890 out = out_tmp ;
891 }
892 }
893 return out ;
894}
895
896/*----------------------------------------------------------------------------*/
902/*----------------------------------------------------------------------------*/
904 const char * filename)
905{
906 cpl_bivector * lines ;
907 double * lines_x ;
908 double * lines_y ;
909 cpl_table * lines_tab ;
910 int i, tab_size;// log_flag ;
911
912 /* Check Entries */
913 if (filename == NULL) return NULL ;
914
915 /* Check PRO.TYPE */
916 if (cr2res_io_check_drs_type(filename, CR2RES_DRSTYPE_CATALOG) != 1) {
917 cpl_msg_info(__func__, "File check failed for %s", filename);
918 return NULL ;
919 }
920
921 /* Initialise */
922 //log_flag = 0 ;
923
924 /* Load the file in a table */
925 lines_tab = cpl_table_load(filename, 1, 1) ;
926 tab_size = cpl_table_get_nrow(lines_tab) ;
927
928 /* Create the bivector */
929 lines = cpl_bivector_new(tab_size) ;
930 lines_x = cpl_bivector_get_x_data(lines) ;
931 lines_y = cpl_bivector_get_y_data(lines) ;
932 for (i=0 ; i<tab_size ; i++) {
933
934 double val ;
935 lines_x[i] = cpl_table_get(lines_tab, CR2RES_COL_WAVELENGTH, i, NULL) ;
936 val = cpl_table_get(lines_tab, CR2RES_COL_EMISSION, i, NULL) ;
937 //if (log_flag && val > 0) lines_y[i] = log10(val) ;
938 //else lines_y[i] = val ;
939 lines_y[i] = val ;
940 }
941
942 /* Free and return */
943 cpl_table_delete(lines_tab) ;
944 return lines ;
945}
946
947/*----------------------------------------------------------------------------*/
956/*----------------------------------------------------------------------------*/
958 const char * filename,
959 int detector,
960 int data)
961{
962 int wished_ext_nb ;
963
964 /* Check entries */
965 if (filename == NULL) return NULL ;
966 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
967
968 /* Check PRO.TYPE */
969 if (cr2res_io_check_drs_type(filename, CR2RES_BPM_DRSTYPE) != 1)
970 return NULL ;
971
972 /* Get the extension number for this detector */
973 wished_ext_nb = cr2res_io_get_ext_idx(filename, detector, data) ;
974
975 /* The wished extension was not found */
976 if (wished_ext_nb < 0) return NULL ;
977
978 return cpl_image_load(filename, CPL_TYPE_INT, 0, wished_ext_nb);
979}
980
981/*----------------------------------------------------------------------------*/
989/*----------------------------------------------------------------------------*/
991 const char * filename,
992 int detector)
993{
994 hdrl_image * master_dark ;
995
996 /* Check entries */
997 if (filename == NULL) return NULL ;
998 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
999
1000 /* Check PRO.TYPE */
1001 if (cr2res_io_check_drs_type(filename, CR2RES_MASTER_DARK_DRSTYPE) != 1)
1002 return NULL ;
1003
1004 /* Load */
1005 master_dark = cr2res_io_load_image(filename, detector) ;
1006
1007 /* In order to not destroy values in data, the NaNs in the */
1008 /* loaded mastercal are replaced by 0.0, and masked, so that they */
1009 /* can be unmasked later. */
1010 hdrl_image_reject_value(master_dark, CPL_VALUE_NAN);
1011 cpl_image_fill_rejected(hdrl_image_get_image(master_dark), 0.0);
1012
1013 /* Error must exist */
1014 if (hdrl_image_get_error(master_dark) == NULL) {
1015 cpl_msg_error(__func__, "The error is missing") ;
1016 hdrl_image_delete(master_dark) ;
1017 return NULL ;
1018 }
1019
1020 /* Return */
1021 return master_dark ;
1022}
1023
1024/*----------------------------------------------------------------------------*/
1033/*----------------------------------------------------------------------------*/
1035 const char * filename,
1036 int detector)
1037{
1038 hdrl_imagelist * detlin_coeffs ;
1039
1040 /* Check entries */
1041 if (filename == NULL) return NULL ;
1042 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1043
1044 /* Check PRO.TYPE */
1045 if (cr2res_io_check_drs_type(filename, CR2RES_DETLIN_COEFFS_DRSTYPE) != 1)
1046 return NULL ;
1047
1048 /* Load */
1049 detlin_coeffs = cr2res_io_load_image_list(filename, detector) ;
1050
1051 /* TODO - Error must exist */
1052
1053 /* Return */
1054 return detlin_coeffs ;
1055}
1056
1057/*----------------------------------------------------------------------------*/
1065/*----------------------------------------------------------------------------*/
1067 const char * filename,
1068 int detector)
1069{
1070 hdrl_image * master_flat ;
1071
1072 /* Check entries */
1073 if (filename == NULL) return NULL ;
1074 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1075
1076 /* Check PRO.TYPE */
1077 if (cr2res_io_check_drs_type(filename, CR2RES_MASTER_FLAT_DRSTYPE) != 1)
1078 return NULL ;
1079
1080 /* Load */
1081 master_flat = cr2res_io_load_image(filename, detector) ;
1082
1083 /* In order to not destroy values in data, the NaNs in the */
1084 /* loaded mastercal are replaced by 1.0, and masked, so that they */
1085 /* can be unmasked later. */
1086 hdrl_image_reject_value(master_flat, CPL_VALUE_NAN);
1087 cpl_image_fill_rejected(hdrl_image_get_image(master_flat),1.0);
1088
1089 /* Error must exist */
1090 if (hdrl_image_get_error(master_flat) == NULL) {
1091 cpl_msg_error(__func__, "The error is missing") ;
1092 hdrl_image_delete(master_flat) ;
1093 return NULL ;
1094 }
1095
1096 /* Return */
1097 return master_flat ;
1098}
1099
1100/*----------------------------------------------------------------------------*/
1108/*----------------------------------------------------------------------------*/
1110 const char * filename,
1111 int detector)
1112{
1113 cpl_table * trace_wave_tab ;
1114
1115 /* Check entries */
1116 if (filename == NULL) return NULL ;
1117 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1118
1119 /* Check PRO.TYPE */
1120 if (cr2res_io_check_drs_type(filename, CR2RES_TW_DRSTYPE) != 1)
1121 return NULL ;
1122
1123 /* Load the table */
1124 trace_wave_tab = cr2res_load_table(filename, detector, -1, -1) ;
1125
1126 /* Return */
1127 return trace_wave_tab ;
1128}
1129
1130/*----------------------------------------------------------------------------*/
1138/*----------------------------------------------------------------------------*/
1140 const char * filename,
1141 int detector)
1142{
1143 cpl_table * slit_func_tab ;
1144
1145 /* Check entries */
1146 if (filename == NULL) return NULL ;
1147 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1148
1149 /* Check PRO.TYPE */
1150 if (cr2res_io_check_drs_type(filename, CR2RES_SLIT_FUNC_DRSTYPE) != 1)
1151 return NULL ;
1152
1153 /* Load the table */
1154 slit_func_tab = cr2res_load_table(filename, detector, -1, -1) ;
1155
1156 /* Return */
1157 return slit_func_tab ;
1158}
1159
1160/*----------------------------------------------------------------------------*/
1168/*----------------------------------------------------------------------------*/
1170 const char * filename,
1171 int detector)
1172{
1173 hdrl_image * slit_model ;
1174
1175 /* Check entries */
1176 if (filename == NULL) return NULL ;
1177 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1178
1179 /* Check PRO.TYPE */
1180 if (cr2res_io_check_drs_type(filename, CR2RES_SLIT_MODEL_DRSTYPE) != 1)
1181 return NULL ;
1182
1183 /* Load */
1184 slit_model = cr2res_io_load_image(filename, detector) ;
1185
1186 /* Error must exist */
1187 if (hdrl_image_get_error(slit_model) == NULL) {
1188 cpl_msg_error(__func__, "The error is missing") ;
1189 hdrl_image_delete(slit_model) ;
1190 return NULL ;
1191 }
1192
1193 /* Return */
1194 return slit_model ;
1195}
1196#ifdef CR2RES_UNUSED
1197/*----------------------------------------------------------------------------*/
1205/*----------------------------------------------------------------------------*/
1206hdrl_image * cr2res_io_load_TRACE_MAP(
1207 const char * filename,
1208 int detector)
1209{
1210 hdrl_image * trace_map ;
1211
1212 /* Check entries */
1213 if (filename == NULL) return NULL ;
1214 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1215
1216 /* Check PRO.TYPE */
1217 if (cr2res_io_check_drs_type(filename, CR2RES_TRACE_MAP_DRSTYPE) != 1)
1218 return NULL ;
1219
1220 /* Load */
1221 trace_map = cr2res_io_load_image(filename, detector) ;
1222
1223 /* Return */
1224 return trace_map ;
1225}
1226
1227/*----------------------------------------------------------------------------*/
1235/*----------------------------------------------------------------------------*/
1236hdrl_image * cr2res_io_load_WAVE_MAP(
1237 const char * filename,
1238 int detector)
1239{
1240 hdrl_image * wave_map ;
1241
1242 /* Check entries */
1243 if (filename == NULL) return NULL ;
1244 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1245
1246 /* Check PRO.TYPE */
1247 if (cr2res_io_check_drs_type(filename, CR2RES_WAVE_MAP_DRSTYPE) != 1)
1248 return NULL ;
1249
1250 /* Load */
1251 wave_map = cr2res_io_load_image(filename, detector) ;
1252
1253 /* Return */
1254 return wave_map ;
1255}
1256
1257/*----------------------------------------------------------------------------*/
1265/*----------------------------------------------------------------------------*/
1266hdrl_image * cr2res_io_load_SLIT_CURV_MAP(
1267 const char * filename,
1268 int detector)
1269{
1270 hdrl_image * slit_curv_map ;
1271
1272 /* Check entries */
1273 if (filename == NULL) return NULL ;
1274 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1275
1276 /* Check PRO.TYPE */
1277 if (cr2res_io_check_drs_type(filename, CR2RES_SLIT_CURV_MAP_DRSTYPE) != 1)
1278 return NULL ;
1279
1280 /* Load */
1281 slit_curv_map = cr2res_io_load_image(filename, detector) ;
1282
1283 /* Return */
1284 return slit_curv_map ;
1285}
1286#endif
1287/*----------------------------------------------------------------------------*/
1295/*----------------------------------------------------------------------------*/
1297 const char * filename,
1298 int detector)
1299{
1300 cpl_table * extract_1D_tab ;
1301
1302 /* Check entries */
1303 if (filename == NULL) return NULL ;
1304 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1305
1306 /* Check PRO.TYPE */
1307 if (cr2res_io_check_drs_type(filename, CR2RES_EXTRACT_1D_DRSTYPE) != 1)
1308 return NULL ;
1309
1310 /* Load the table */
1311 extract_1D_tab = cr2res_load_table(filename, detector, -1, -1) ;
1312
1313 /* Return */
1314 return extract_1D_tab ;
1315}
1316#ifdef CR2RES_UNUSED
1317/*----------------------------------------------------------------------------*/
1325/*----------------------------------------------------------------------------*/
1326cpl_table * cr2res_io_load_SPLICED_1D(
1327 const char * filename,
1328 int detector)
1329{
1330 cpl_table * spliced_1D_tab ;
1331
1332 /* Check entries */
1333 if (filename == NULL) return NULL ;
1334 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1335
1336 /* Check PRO.TYPE */
1337 if (cr2res_io_check_drs_type(filename, CR2RES_SPLICED_1D_DRSTYPE) != 1)
1338 return NULL ;
1339
1340 /* Load the table */
1341 spliced_1D_tab = cr2res_load_table(filename, detector, -1, -1) ;
1342
1343 /* Return */
1344 return spliced_1D_tab ;
1345}
1346
1347/*----------------------------------------------------------------------------*/
1355/*----------------------------------------------------------------------------*/
1356cpl_table * cr2res_io_load_EXTRACT_2D(
1357 const char * filename,
1358 int detector)
1359{
1360 cpl_table * extract_2D_tab ;
1361
1362 /* Check PRO.TYPE */
1363 if (cr2res_io_check_drs_type(filename, CR2RES_EXTRACT_2D_DRSTYPE) != 1)
1364 return NULL ;
1365
1366 /* Load the table */
1367 extract_2D_tab = cr2res_load_table(filename, detector, -1, -1) ;
1368
1369 /* Return */
1370 return extract_2D_tab ;
1371}
1372#endif
1373/*----------------------------------------------------------------------------*/
1374/*--------------------- SAVING FUNCTIONS -------------------------*/
1375/*----------------------------------------------------------------------------*/
1376/* TODO ? set frame levels to mark temp, intermediate and final frames? */
1377
1378/*----------------------------------------------------------------------------*/
1388/*----------------------------------------------------------------------------*/
1390 const char * filename,
1391 cpl_table * out_table,
1392 const cpl_parameterlist * parlist,
1393 cpl_frameset * set,
1394 const char * recipe)
1395{
1396 cpl_propertylist * plist ;
1397 double mjd_obs ;
1398
1399 plist = cpl_propertylist_new();
1400 cpl_propertylist_append_string(plist, CR2RES_HEADER_INSTRUMENT, "CRIRES") ;
1401 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
1402 CR2RES_PHOTO_FLUX_PROCATG) ;
1403 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, "STATIC") ;
1404 cpl_propertylist_append_string(plist, CR2RES_HEADER_DRS_TYPE,
1405 CR2RES_PHOTO_FLUX_DRSTYPE) ;
1406 mjd_obs = cr2res_mjd_obs_now() ;
1407 cpl_propertylist_append_double(plist, CR2RES_HEADER_MJD_OBS, mjd_obs) ;
1408
1409
1410 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
1411 NULL, recipe, plist, NULL,
1412 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
1413 cpl_msg_error(__func__, "Cannot save the table") ;
1414 cpl_propertylist_delete(plist) ;
1415 return -1 ;
1416 }
1417 cpl_propertylist_delete(plist) ;
1418
1419 /* Return */
1420 return 0 ;
1421}
1422
1423/*----------------------------------------------------------------------------*/
1434/*----------------------------------------------------------------------------*/
1436 const char * filename,
1437 cpl_table * out_table,
1438 const cpl_parameterlist * parlist,
1439 cpl_frameset * set,
1440 const char * recipe,
1441 const char * setting_string)
1442{
1443 cpl_propertylist * plist ;
1444 double mjd_obs ;
1445
1446 plist = cpl_propertylist_new();
1447 cpl_propertylist_append_string(plist, CR2RES_HEADER_INSTRUMENT, "CRIRES") ;
1448 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
1449 CR2RES_EMISSION_LINES_PROCATG) ;
1450 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, "STATIC") ;
1451 cpl_propertylist_append_string(plist, CR2RES_HEADER_DRS_TYPE,
1452 CR2RES_DRSTYPE_CATALOG) ;
1453
1454 mjd_obs = cr2res_mjd_obs_now() ;
1455 cpl_propertylist_append_double(plist, CR2RES_HEADER_MJD_OBS, mjd_obs) ;
1456
1457 if (setting_string != NULL) {
1458 cpl_propertylist_append_string(plist, CR2RES_HEADER_WLEN_ID,
1459 setting_string) ;
1460 }
1461
1462 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
1463 NULL, recipe, plist, NULL,
1464 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
1465 cpl_msg_error(__func__, "Cannot save the table") ;
1466 cpl_propertylist_delete(plist) ;
1467 return -1 ;
1468 }
1469 cpl_propertylist_delete(plist) ;
1470
1471 /* Return */
1472 return 0 ;
1473}
1474
1475/*----------------------------------------------------------------------------*/
1490/*----------------------------------------------------------------------------*/
1492 const char * filename,
1493 cpl_frameset * allframes,
1494 cpl_frameset * inframes,
1495 const cpl_parameterlist * parlist,
1496 hdrl_image ** master_darks,
1497 const cpl_propertylist * qc_list,
1498 cpl_propertylist ** ext_plist,
1499 const char * procatg,
1500 const char * recipe)
1501{
1502 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1503 master_darks, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1504 procatg, CR2RES_MASTER_DARK_DRSTYPE) ;
1505}
1506
1507/*----------------------------------------------------------------------------*/
1522/*----------------------------------------------------------------------------*/
1524 const char * filename,
1525 cpl_frameset * allframes,
1526 cpl_frameset * inframes,
1527 const cpl_parameterlist * parlist,
1528 hdrl_imagelist ** coeffs,
1529 const cpl_propertylist * qc_list,
1530 cpl_propertylist ** ext_plist,
1531 const char * procatg,
1532 const char * recipe)
1533{
1534 return cr2res_io_save_imagelist(filename, allframes, inframes, parlist,
1535 coeffs, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1536 procatg, CR2RES_DETLIN_COEFFS_DRSTYPE) ;
1537}
1538
1539/*----------------------------------------------------------------------------*/
1554/*----------------------------------------------------------------------------*/
1556 const char * filename,
1557 cpl_frameset * allframes,
1558 cpl_frameset * inframes,
1559 const cpl_parameterlist * parlist,
1560 cpl_image ** bpms,
1561 const cpl_propertylist * qc_list,
1562 cpl_propertylist ** ext_plist,
1563 const char * procatg,
1564 const char * recipe)
1565{
1566 hdrl_image * hdrl_bpms[CR2RES_NB_DETECTORS] ;
1567 int det_nr, ret ;
1568 cpl_image * bpm_tmp;
1569
1570 /* Convert to HDRL images */
1571 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
1572 if (bpms[det_nr-1] == NULL){
1573 hdrl_bpms[det_nr-1] = NULL ;
1574 } else{
1575 bpm_tmp = cr2res_bpm_mask_edgepix(bpms[det_nr-1]);
1576 hdrl_bpms[det_nr-1] = hdrl_image_create(bpm_tmp, NULL) ;
1577 cpl_image_delete(bpm_tmp);
1578 }
1579 }
1580
1581 /* Save */
1582 ret = cr2res_io_save_image(filename, allframes, inframes, parlist,
1583 hdrl_bpms, qc_list, ext_plist, CPL_TYPE_INT, recipe,
1584 procatg, CR2RES_BPM_DRSTYPE) ;
1585
1586 /* Free and return */
1587 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
1588 hdrl_image_delete(hdrl_bpms[det_nr-1]) ;
1589 }
1590 return ret ;
1591}
1592
1593/*----------------------------------------------------------------------------*/
1607/*----------------------------------------------------------------------------*/
1609 const char * filename,
1610 cpl_frameset * allframes,
1611 cpl_frameset * inframes,
1612 const cpl_parameterlist * parlist,
1613 hdrl_image ** calib_collapsed,
1614 const cpl_propertylist * qc_list,
1615 cpl_propertylist ** ext_plist,
1616 const char * procatg,
1617 const char * recipe)
1618{
1619 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1620 calib_collapsed, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1621 procatg, CR2RES_CALIBRATED_DRSTYPE) ;
1622}
1623
1624/*----------------------------------------------------------------------------*/
1638/*----------------------------------------------------------------------------*/
1640 const char * filename,
1641 cpl_frameset * allframes,
1642 cpl_frameset * inframes,
1643 const cpl_parameterlist * parlist,
1644 hdrl_image ** master_flats,
1645 const cpl_propertylist * qc_list,
1646 cpl_propertylist ** ext_plist,
1647 const char * procatg,
1648 const char * recipe)
1649{
1650 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1651 master_flats, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1652 procatg, CR2RES_MASTER_FLAT_DRSTYPE) ;
1653}
1654
1655/*----------------------------------------------------------------------------*/
1669/*----------------------------------------------------------------------------*/
1671 const char * filename,
1672 cpl_frameset * allframes,
1673 cpl_frameset * inframes,
1674 const cpl_parameterlist * parlist,
1675 cpl_table ** tables,
1676 const cpl_propertylist * qc_list,
1677 cpl_propertylist ** ext_plist,
1678 const char * procatg,
1679 const char * recipe)
1680{
1681
1682 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
1683 qc_list, ext_plist, recipe, procatg, CR2RES_TW_DRSTYPE) ;
1684}
1685
1686/*----------------------------------------------------------------------------*/
1700/*----------------------------------------------------------------------------*/
1702 const char * filename,
1703 cpl_frameset * allframes,
1704 cpl_frameset * inframes,
1705 const cpl_parameterlist * parlist,
1706 cpl_table ** tables,
1707 const cpl_propertylist * qc_list,
1708 cpl_propertylist ** ext_plist,
1709 const char * procatg,
1710 const char * recipe)
1711{
1712 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
1713 qc_list, ext_plist, recipe, procatg,
1714 CR2RES_LINES_DIAGNOSTICS_DRSTYPE) ;
1715}
1716
1717/*----------------------------------------------------------------------------*/
1731/*----------------------------------------------------------------------------*/
1733 const char * filename,
1734 cpl_frameset * allframes,
1735 cpl_frameset * inframes,
1736 const cpl_parameterlist * parlist,
1737 cpl_table ** tables,
1738 const cpl_propertylist * qc_list,
1739 cpl_propertylist ** ext_plist,
1740 const char * procatg,
1741 const char * recipe)
1742{
1743 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
1744 qc_list, ext_plist, recipe, procatg, CR2RES_EXTRACT_1D_DRSTYPE) ;
1745}
1746
1747/*----------------------------------------------------------------------------*/
1761/*----------------------------------------------------------------------------*/
1763 const char * filename,
1764 cpl_frameset * allframes,
1765 cpl_frameset * inframes,
1766 const cpl_parameterlist * parlist,
1767 cpl_table ** tables,
1768 const cpl_propertylist * qc_list,
1769 cpl_propertylist ** ext_plist,
1770 const char * procatg,
1771 const char * recipe)
1772{
1773 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
1774 qc_list, ext_plist, recipe, procatg, CR2RES_THROUGHPUT_DRSTYPE) ;
1775}
1776
1777/*----------------------------------------------------------------------------*/
1791/*----------------------------------------------------------------------------*/
1793 const char * filename,
1794 cpl_frameset * allframes,
1795 cpl_frameset * inframes,
1796 const cpl_parameterlist * parlist,
1797 cpl_table ** slit_func,
1798 const cpl_propertylist * qc_list,
1799 cpl_propertylist ** ext_plist,
1800 const char * procatg,
1801 const char * recipe)
1802{
1803 return cr2res_io_save_table(filename, allframes, inframes, parlist,
1804 slit_func, qc_list, ext_plist, recipe, procatg,
1805 CR2RES_SLIT_FUNC_DRSTYPE) ;
1806}
1807
1808/*----------------------------------------------------------------------------*/
1822/*----------------------------------------------------------------------------*/
1824 const char * filename,
1825 cpl_frameset * allframes,
1826 cpl_frameset * inframes,
1827 const cpl_parameterlist * parlist,
1828 hdrl_image ** data,
1829 const cpl_propertylist * qc_list,
1830 cpl_propertylist ** ext_plist,
1831 const char * procatg,
1832 const char * recipe)
1833{
1834 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1835 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe, procatg,
1836 CR2RES_SLIT_MODEL_DRSTYPE) ;
1837}
1838
1839/*----------------------------------------------------------------------------*/
1853/*----------------------------------------------------------------------------*/
1855 const char * filename,
1856 cpl_frameset * allframes,
1857 cpl_frameset * inframes,
1858 const cpl_parameterlist * parlist,
1859 hdrl_image ** data,
1860 const cpl_propertylist * qc_list,
1861 cpl_propertylist ** ext_plist,
1862 const char * procatg,
1863 const char * recipe)
1864{
1865 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1866 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe, procatg,
1867 CR2RES_COMBINED_DRSTYPE) ;
1868}
1869
1870/*----------------------------------------------------------------------------*/
1884/*----------------------------------------------------------------------------*/
1886 const char * filename,
1887 cpl_frameset * allframes,
1888 cpl_frameset * inframes,
1889 const cpl_parameterlist * parlist,
1890 hdrl_image ** data,
1891 const cpl_propertylist * qc_list,
1892 cpl_propertylist ** ext_plist,
1893 const char * procatg,
1894 const char * recipe)
1895{
1896 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1897 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1898 procatg, CR2RES_TRACE_MAP_DRSTYPE) ;
1899}
1900
1901/*----------------------------------------------------------------------------*/
1915/*----------------------------------------------------------------------------*/
1917 const char * filename,
1918 cpl_frameset * allframes,
1919 cpl_frameset * inframes,
1920 const cpl_parameterlist * parlist,
1921 hdrl_image ** data,
1922 const cpl_propertylist * qc_list,
1923 cpl_propertylist ** ext_plist,
1924 const char * procatg,
1925 const char * recipe)
1926{
1927 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1928 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1929 procatg, CR2RES_WAVE_MAP_DRSTYPE) ;
1930}
1931
1932/*----------------------------------------------------------------------------*/
1946/*----------------------------------------------------------------------------*/
1948 const char * filename,
1949 cpl_frameset * allframes,
1950 cpl_frameset * inframes,
1951 const cpl_parameterlist * parlist,
1952 hdrl_image ** data,
1953 const cpl_propertylist * qc_list,
1954 cpl_propertylist ** ext_plist,
1955 const char * procatg,
1956 const char * recipe)
1957{
1958 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1959 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1960 procatg, CR2RES_SLIT_CURV_MAP_DRSTYPE) ;
1961}
1962
1963/*----------------------------------------------------------------------------*/
1977/*----------------------------------------------------------------------------*/
1979 const char * filename,
1980 cpl_frameset * allframes,
1981 cpl_frameset * inframes,
1982 const cpl_parameterlist * parlist,
1983 cpl_table * spliced_1d,
1984 const cpl_propertylist * qc_list,
1985 cpl_propertylist * ext_plist,
1986 const char * procatg,
1987 const char * recipe)
1988{
1989 return cr2res_io_save_one_table(filename, allframes, inframes, parlist,
1990 spliced_1d, qc_list, ext_plist, "SPLICED", recipe, procatg,
1991 CR2RES_SPLICED_1D_DRSTYPE) ;
1992}
1993
1994/*----------------------------------------------------------------------------*/
2008/*----------------------------------------------------------------------------*/
2010 const char * filename,
2011 cpl_frameset * allframes,
2012 cpl_frameset * inframes,
2013 const cpl_parameterlist * parlist,
2014 cpl_table ** tables,
2015 const cpl_propertylist * qc_list,
2016 cpl_propertylist ** ext_plist,
2017 const char * procatg,
2018 const char * recipe)
2019{
2020 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
2021 qc_list, ext_plist, recipe, procatg, CR2RES_EXTRACT_2D_DRSTYPE) ;
2022}
2023
2024/*----------------------------------------------------------------------------*/
2038/*----------------------------------------------------------------------------*/
2040 const char * filename,
2041 cpl_frameset * allframes,
2042 cpl_frameset * inframes,
2043 const cpl_parameterlist * parlist,
2044 cpl_table ** tables,
2045 const cpl_propertylist * qc_list,
2046 cpl_propertylist ** ext_plist,
2047 const char * procatg,
2048 const char * recipe)
2049{
2050 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
2051 qc_list, ext_plist, recipe, procatg, CR2RES_POL_SPEC_DRSTYPE) ;
2052}
2053
2056/*----------------------------------------------------------------------------*/
2063/*----------------------------------------------------------------------------*/
2064static int cr2res_io_set_bpm_as_NaNs(
2065 cpl_image * in)
2066{
2067 cpl_size nx, ny, i, j ;
2068 double * pdata ;
2069
2070 /* Check Entries */
2071 if (in == NULL) return -1 ;
2072 if (cpl_image_get_type(in) != CPL_TYPE_DOUBLE) return 0 ;
2073
2074 /* Initialize */
2075 nx = cpl_image_get_size_x(in) ;
2076 ny = cpl_image_get_size_y(in) ;
2077 pdata = cpl_image_get_data_double(in) ;
2078
2079 /* Loop on the pixels */
2080 for (j=0 ; j<ny ; j++) {
2081 for (i=0 ; i<nx ; i++) {
2082 if (cpl_image_is_rejected(in, i+1, j+1)) pdata[i+j*nx] = NAN ;
2083 }
2084 }
2085 return 0 ;
2086}
2087/*----------------------------------------------------------------------------*/
2094/*----------------------------------------------------------------------------*/
2095static int cr2res_io_set_NaNs_as_bpm(
2096 cpl_image * in)
2097{
2098 cpl_size nx, ny, i, j ;
2099 double * pdata ;
2100
2101 /* Check Entries */
2102 if (in == NULL) return -1 ;
2103 if (cpl_image_get_type(in) != CPL_TYPE_DOUBLE) return 0 ;
2104
2105 /* Initialize */
2106 nx = cpl_image_get_size_x(in) ;
2107 ny = cpl_image_get_size_y(in) ;
2108 pdata = cpl_image_get_data_double(in) ;
2109
2110 /* Loop on the pixels */
2111 for (j=0 ; j<ny ; j++) {
2112 for (i=0 ; i<nx ; i++) {
2113 if (isnan(pdata[i+j*nx])) cpl_image_reject(in, i+1, j+1) ;
2114 }
2115 }
2116 return 0 ;
2117}
2118
2119/*----------------------------------------------------------------------------*/
2134/*----------------------------------------------------------------------------*/
2135static int cr2res_io_save_table(
2136 const char * filename,
2137 cpl_frameset * allframes,
2138 cpl_frameset * inframes,
2139 const cpl_parameterlist * parlist,
2140 cpl_table ** tab,
2141 const cpl_propertylist * qc_list,
2142 cpl_propertylist ** ext_plist,
2143 const char * recipe,
2144 const char * procatg,
2145 const char * drstype)
2146{
2147 cpl_propertylist * pro_list ;
2148 cpl_propertylist * ext_head ;
2149 char * wished_extname ;
2150 char * to_remove ;
2151 int i ;
2152
2153 /* Test entries */
2154 if (allframes == NULL || filename == NULL || ext_plist == NULL) return -1 ;
2155
2156 /* Add the PRO keys */
2157 if (qc_list != NULL) pro_list = cpl_propertylist_duplicate(qc_list) ;
2158 else pro_list = cpl_propertylist_new() ;
2159
2160 /* Add PRO Keys */
2161 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procatg) ;
2162 cpl_propertylist_append_string(pro_list, CR2RES_HEADER_DRS_TYPE, drstype) ;
2163
2164 cr2res_qc_dup_mtrlgy_key(inframes, pro_list);
2165
2166 /* Create the first extension header */
2167 if (ext_plist[0]==NULL) {
2168 ext_head = cpl_propertylist_new() ;
2169 } else {
2170 ext_head = cpl_propertylist_duplicate(ext_plist[0]);
2171 cpl_propertylist_erase(ext_head, "EXTNAME");
2172 if (cpl_propertylist_get_property(ext_head, "BUNIT") != NULL)
2173 cpl_propertylist_erase(ext_head, "BUNIT");
2174 }
2175 wished_extname = cr2res_io_create_extname(1, 1) ;
2176 cpl_propertylist_update_string(ext_head, "EXTNAME", wished_extname) ;
2177 cpl_free(wished_extname) ;
2178
2179 cr2res_qc_dup_chip_idx(ext_head);
2180
2181 /* Remove keywords in the primary header matching NODPOS (PIPE-9952) */
2182 to_remove = cpl_sprintf("NODPOS") ;
2183
2184 /* Save the first extension */
2185 if (tab[0] != NULL) {
2186 if (cpl_dfs_save_table(allframes, NULL, parlist, inframes, NULL,
2187 tab[0], ext_head, recipe, pro_list, to_remove,
2188 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2189 cpl_msg_error(__func__, "Cannot save the first extension table") ;
2190 cpl_propertylist_delete(ext_head) ;
2191 cpl_propertylist_delete(pro_list) ;
2192 cpl_free(to_remove) ;
2193 return -1 ;
2194 }
2195 } else {
2196 if (cpl_dfs_save_propertylist(allframes, NULL, parlist,
2197 inframes, NULL, recipe, pro_list, to_remove, PACKAGE "/"
2198 PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2199 cpl_msg_error(__func__, "Cannot save the empty HDU") ;
2200 cpl_propertylist_delete(ext_head) ;
2201 cpl_propertylist_delete(pro_list) ;
2202 cpl_free(to_remove) ;
2203 return -1 ;
2204 }
2205 cpl_propertylist_save(ext_head, filename, CPL_IO_EXTEND) ;
2206 }
2207 cpl_free(to_remove) ;
2208 cpl_propertylist_delete(ext_head) ;
2209 cpl_propertylist_delete(pro_list) ;
2210
2211 /* Save the extensions */
2212 for (i=1 ; i<CR2RES_NB_DETECTORS ; i++) {
2213 /* Create the first extension header */
2214 if (ext_plist[i] == NULL) {
2215 ext_head = cpl_propertylist_new() ;
2216 } else {
2217 ext_head = cpl_propertylist_duplicate(ext_plist[i]);
2218 cpl_propertylist_erase(ext_head, "EXTNAME");
2219 cpl_propertylist_erase(ext_head, "BUNIT");
2220 }
2221 wished_extname = cr2res_io_create_extname(i+1, 1) ;
2222 cpl_propertylist_update_string(ext_head, "EXTNAME", wished_extname) ;
2223 cpl_free(wished_extname) ;
2224 cr2res_qc_dup_chip_idx(ext_head);
2225
2226 if (tab[i] != NULL) {
2227 cpl_table_save(tab[i], NULL, ext_head, filename, CPL_IO_EXTEND) ;
2228 } else {
2229 cpl_propertylist_save(ext_head, filename, CPL_IO_EXTEND) ;
2230 }
2231 cpl_propertylist_delete(ext_head) ;
2232 }
2233 return 0 ;
2234}
2235
2236/*----------------------------------------------------------------------------*/
2252/*----------------------------------------------------------------------------*/
2253static int cr2res_io_save_one_table(
2254 const char * filename,
2255 cpl_frameset * allframes,
2256 cpl_frameset * inframes,
2257 const cpl_parameterlist * parlist,
2258 cpl_table * tab,
2259 const cpl_propertylist * qc_list,
2260 cpl_propertylist * ext_plist,
2261 const char * extname,
2262 const char * recipe,
2263 const char * procatg,
2264 const char * drstype)
2265{
2266 cpl_propertylist * pro_list ;
2267 cpl_propertylist * ext_head ;
2268
2269 /* Test entries */
2270 if (allframes == NULL || filename == NULL || ext_plist == NULL ||
2271 extname == NULL) return -1 ;
2272
2273 /* Add the PRO keys */
2274 if (qc_list != NULL) pro_list = cpl_propertylist_duplicate(qc_list) ;
2275 else pro_list = cpl_propertylist_new() ;
2276
2277 /* Add PRO Keys */
2278 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procatg) ;
2279 cpl_propertylist_append_string(pro_list, CR2RES_HEADER_DRS_TYPE, drstype) ;
2280
2281 /* Create the first extension header */
2282 ext_head = cpl_propertylist_duplicate(ext_plist);
2283 cpl_propertylist_erase(ext_head, "EXTNAME");
2284 cpl_propertylist_update_string(ext_head, "EXTNAME", extname) ;
2285
2286 /* Save the first extension */
2287 if (cpl_dfs_save_table(allframes, NULL, parlist, inframes, NULL,
2288 tab, ext_head, recipe, pro_list, NULL,
2289 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2290 cpl_msg_error(__func__, "Cannot save the first extension table") ;
2291 cpl_propertylist_delete(ext_head) ;
2292 cpl_propertylist_delete(pro_list) ;
2293 return -1 ;
2294 }
2295 cpl_propertylist_delete(ext_head) ;
2296 cpl_propertylist_delete(pro_list) ;
2297
2298 return 0 ;
2299}
2300
2301/*----------------------------------------------------------------------------*/
2317/*----------------------------------------------------------------------------*/
2318static int cr2res_io_save_image(
2319 const char * filename,
2320 cpl_frameset * allframes,
2321 cpl_frameset * inframes,
2322 const cpl_parameterlist * parlist,
2323 hdrl_image ** data,
2324 const cpl_propertylist * qc_list,
2325 cpl_propertylist ** ext_plist,
2326 cpl_type type,
2327 const char * recipe,
2328 const char * procatg,
2329 const char * drstype)
2330{
2331 cpl_propertylist * qclist_loc ;
2332 cpl_image * to_save ;
2333 char * to_remove ;
2334 int det_nr ;
2335
2336 /* Create a local QC list and add the PRO.CATG */
2337 if (qc_list == NULL) {
2338 qclist_loc = cpl_propertylist_new();
2339 } else {
2340 qclist_loc = cpl_propertylist_duplicate(qc_list) ;
2341 }
2342 cpl_propertylist_update_string(qclist_loc, CPL_DFS_PRO_CATG, procatg);
2343 cpl_propertylist_update_string(qclist_loc, CR2RES_HEADER_DRS_TYPE, drstype);
2344
2345 cr2res_qc_dup_mtrlgy_key(inframes, qclist_loc);
2346
2347 /* Remove keywords in the primary header matching NODPOS (PIPE-9952) */
2348 to_remove = cpl_sprintf("NODPOS") ;
2349
2350 /* Create the Primary Data Unit without data */
2351 if (cpl_dfs_save_propertylist(allframes, NULL, parlist, inframes, NULL,
2352 recipe, qclist_loc, to_remove,
2353 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2354 cpl_msg_error(__func__, "Cannot save the empty primary HDU") ;
2355 cpl_propertylist_delete(qclist_loc) ;
2356 cpl_free(to_remove) ;
2357 return -1 ;
2358 }
2359 cpl_free(to_remove) ;
2360
2361 /* Delete PRO LIST */
2362 cpl_propertylist_delete(qclist_loc) ;
2363
2364 /* Save the extensions */
2365 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
2366 char * wished_extname ;
2367 if (ext_plist[det_nr-1] == NULL) {
2368 qclist_loc = cpl_propertylist_new();
2369 } else {
2370 qclist_loc = cpl_propertylist_duplicate(ext_plist[det_nr-1]) ;
2371 cpl_propertylist_erase(qclist_loc, "EXTNAME");
2372 }
2373
2374 /* Save the DATA */
2375 wished_extname = cr2res_io_create_extname(det_nr, 1) ;
2376 cpl_propertylist_prepend_string(qclist_loc, "EXTNAME", wished_extname) ;
2377
2378 cr2res_qc_dup_chip_idx(qclist_loc);
2379
2380 if (data[det_nr-1] == NULL)
2381 to_save = NULL ;
2382 else
2383 to_save = hdrl_image_get_image(data[det_nr-1]);
2384
2385 /* Set BPMs as NaNs */
2386 if (to_save != NULL) cr2res_io_set_bpm_as_NaNs(to_save) ;
2387
2388 /* Save */
2389 cpl_image_save(to_save, filename, type, qclist_loc, CPL_IO_EXTEND) ;
2390 cpl_free(wished_extname) ;
2391
2392 /* Save the NOISE - always but for the BPM */
2393 if (strcmp(drstype, CR2RES_BPM_DRSTYPE)) {
2394 wished_extname = cr2res_io_create_extname(det_nr, 0) ;
2395 cpl_propertylist_update_string(qclist_loc, "EXTNAME",
2396 wished_extname) ;
2397 if (data[det_nr-1] == NULL)
2398 to_save = NULL ;
2399 else
2400 to_save = hdrl_image_get_error(data[det_nr-1]);
2401 cpl_image_save(to_save, filename, type, qclist_loc, CPL_IO_EXTEND) ;
2402 cpl_free(wished_extname) ;
2403 }
2404 cpl_propertylist_delete(qclist_loc) ;
2405 }
2406
2407 return 0 ;
2408}
2409
2410/*----------------------------------------------------------------------------*/
2426/*----------------------------------------------------------------------------*/
2427static int cr2res_io_save_imagelist(
2428 const char * filename,
2429 cpl_frameset * allframes,
2430 cpl_frameset * inframes,
2431 const cpl_parameterlist * parlist,
2432 hdrl_imagelist ** data,
2433 const cpl_propertylist * qc_list,
2434 cpl_propertylist ** ext_plist,
2435 cpl_type type,
2436 const char * recipe,
2437 const char * procatg,
2438 const char * drstype)
2439{
2440 cpl_propertylist * qclist_loc ;
2441 cpl_imagelist * list_data ;
2442 cpl_imagelist * list_noise ;
2443 int det_nr ;
2444 cpl_size i ;
2445
2446 /* Create a local QC list and add the PRO.CATG */
2447 if (qc_list == NULL) {
2448 qclist_loc = cpl_propertylist_new();
2449 } else {
2450 qclist_loc = cpl_propertylist_duplicate(qc_list) ;
2451 }
2452 cpl_propertylist_update_string(qclist_loc, CPL_DFS_PRO_CATG, procatg);
2453 cpl_propertylist_update_string(qclist_loc, CR2RES_HEADER_DRS_TYPE, drstype);
2454
2455 cr2res_qc_dup_mtrlgy_key(inframes, qclist_loc);
2456
2457 /* Create the Primary Data Unit without data */
2458 if (cpl_dfs_save_propertylist(allframes, NULL, parlist, inframes, NULL,
2459 recipe, qclist_loc, NULL,
2460 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2461 cpl_msg_error(__func__, "Cannot save the empty primary HDU") ;
2462 cpl_propertylist_delete(qclist_loc) ;
2463 return -1 ;
2464 }
2465 /* Delete PRO LIST */
2466 cpl_propertylist_delete(qclist_loc) ;
2467
2468 /* Save the extensions */
2469 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
2470 char * wished_extname ;
2471
2472 /* Extract separately data / noise */
2473 if (data[det_nr-1] == NULL) {
2474 list_data = list_noise = NULL ;
2475 } else {
2476 list_data = cr2res_hdrl_to_cpl_list(data[det_nr-1], 1) ;
2477 list_noise = cr2res_hdrl_to_cpl_list(data[det_nr-1], 0) ;
2478 }
2479
2480 /* Set BPMs as NaNs */
2481 if (list_data != NULL)
2482 for (i=0 ; i<cpl_imagelist_get_size(list_data) ; i++)
2483 cr2res_io_set_bpm_as_NaNs(cpl_imagelist_get(list_data, i)) ;
2484
2485 /* Prepare header */
2486 if (ext_plist[det_nr-1] == NULL) {
2487 qclist_loc = cpl_propertylist_new();
2488 } else {
2489 qclist_loc = cpl_propertylist_duplicate(ext_plist[det_nr-1]) ;
2490 cpl_propertylist_erase(qclist_loc, "EXTNAME");
2491 }
2492
2493 /* Save the DATA */
2494 wished_extname = cr2res_io_create_extname(det_nr, 1) ;
2495 cpl_propertylist_prepend_string(qclist_loc, "EXTNAME", wished_extname) ;
2496 cr2res_qc_dup_chip_idx(qclist_loc);
2497 if (list_data == NULL) {
2498 cpl_propertylist_save(qclist_loc, filename, CPL_IO_EXTEND) ;
2499 } else {
2500 cpl_imagelist_save(list_data, filename, type, qclist_loc,
2501 CPL_IO_EXTEND) ;
2502 cpl_imagelist_delete(list_data) ;
2503 }
2504 cpl_free(wished_extname) ;
2505
2506 /* Save the NOISE */
2507 wished_extname = cr2res_io_create_extname(det_nr, 0) ;
2508 cpl_propertylist_update_string(qclist_loc, "EXTNAME", wished_extname) ;
2509 if (list_noise == NULL) {
2510 cpl_propertylist_save(qclist_loc, filename, CPL_IO_EXTEND) ;
2511 } else {
2512 cpl_imagelist_save(list_noise, filename, type, qclist_loc,
2513 CPL_IO_EXTEND) ;
2514 cpl_imagelist_delete(list_noise) ;
2515 }
2516 cpl_propertylist_delete(qclist_loc) ;
2517 cpl_free(wished_extname) ;
2518 }
2519
2520 return 0 ;
2521}
2522
2523/*----------------------------------------------------------------------------*/
2530/*----------------------------------------------------------------------------*/
2531static cpl_imagelist * cr2res_hdrl_to_cpl_list(
2532 const hdrl_imagelist * in,
2533 int data)
2534{
2535 cpl_imagelist * out ;
2536 const cpl_image * cur_cpl_im ;
2537 cpl_size i ;
2538
2539 /* Check Entries */
2540 if (in == NULL) return NULL ;
2541 if (data != 0 && data != 1) return NULL ;
2542
2543 /* Create output list */
2544 out = cpl_imagelist_new() ;
2545 for (i=0 ; i< hdrl_imagelist_get_size(in) ; i++) {
2546 const hdrl_image * cur_im ;
2547 cur_im = hdrl_imagelist_get(in, i) ;
2548 if (data == 1) cur_cpl_im = hdrl_image_get_image_const(cur_im) ;
2549 if (data == 0) cur_cpl_im = hdrl_image_get_error_const(cur_im) ;
2550
2551 cpl_imagelist_set(out, cpl_image_duplicate(cur_cpl_im),
2552 cpl_imagelist_get_size(out)) ;
2553 }
2554
2555 /* Check output */
2556 if (hdrl_imagelist_get_size(in) != cpl_imagelist_get_size(out)) {
2557 cpl_imagelist_delete(out) ;
2558 return NULL ;
2559 }
2560 /* Return */
2561 return out ;
2562}
2563
2564static int cr2res_io_check_drs_type(
2565 const char * filename,
2566 const char * expected_drstype)
2567{
2568 cpl_propertylist * plist ;
2569 const char * drstype ;
2570
2571 /* Check entries */
2572 if (filename == NULL) return -1 ;
2573
2574 /* Check the DRS.TYPE */
2575 plist = cpl_propertylist_load(filename, 0) ;
2576 if (plist == NULL) return -1;
2577 drstype = cr2res_pfits_get_drstype(plist) ;
2578 if (cpl_error_get_code()) {
2579
2580 const char * protype ;
2581 cpl_error_reset() ;
2582
2583 /* TRANSITION PHASE starting 09.02.2023 : See ticket PIPE-10551 */
2584 /*
2585 DRS.TYPE was in PRO.TYPE so far.
2586 Adding here check of PRO.TYPE to support old data
2587 Looking in PRO.TYPE id DRS.TYPE is missing
2588 */
2589 protype = cr2res_pfits_get_protype(plist) ;
2590 if (cpl_error_get_code()) {
2591 cpl_error_reset() ;
2592 cpl_propertylist_delete(plist) ;
2593 return -1 ;
2594 }
2595 if (strcmp(protype, expected_drstype)) {
2596 cpl_msg_error(__func__, "Unexpected PRO.TYPE (old data): %s != %s",
2597 protype, expected_drstype) ;
2598 cpl_propertylist_delete(plist) ;
2599 return 0 ;
2600 }
2601 } else {
2602 if (strcmp(drstype, expected_drstype)) {
2603 cpl_msg_error(__func__, "Unexpected DRS.TYPE: %s != %s",
2604 drstype, expected_drstype) ;
2605 cpl_propertylist_delete(plist) ;
2606 return 0 ;
2607 }
2608 }
2609 cpl_propertylist_delete(plist) ;
2610 return 1 ;
2611}
2612
int cr2res_io_save_EMISSION_LINES(const char *filename, cpl_table *out_table, const cpl_parameterlist *parlist, cpl_frameset *set, const char *recipe, const char *setting_string)
Save EMISSION_LINES file.
Definition: cr2res_io.c:1435
cpl_table * cr2res_io_load_EXTRACT_1D(const char *filename, int detector)
Load a table from a EXTRACT_1D.
Definition: cr2res_io.c:1296
cpl_frame * cr2res_io_find_SLIT_MODEL(const cpl_frameset *in, const char *setting_id, cr2res_decker decker)
Get the first CR2RES_SLIT_MODEL_DRSTYPE frame from a frameset.
Definition: cr2res_io.c:167
const cpl_frame * cr2res_io_find_BPM(const cpl_frameset *in)
Get the first CR2RES_BPM_DRSTYPE frame from a frameset.
Definition: cr2res_io.c:369
int cr2res_io_save_SLIT_MODEL(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a SLIT_MODEL.
Definition: cr2res_io.c:1823
const cpl_frame * cr2res_io_find_SLIT_FUNC(const cpl_frameset *in)
Get the first CR2RES_SLIT_FUNC_DRSTYPE frame from a frameset.
Definition: cr2res_io.c:267
const cpl_frame * cr2res_io_find_TRACE_WAVE(const cpl_frameset *in)
Get the first CR2RES_TW_DRSTYPE frame from a frameset.
Definition: cr2res_io.c:231
int cr2res_io_save_PHOTO_FLUX(const char *filename, cpl_table *out_table, const cpl_parameterlist *parlist, cpl_frameset *set, const char *recipe)
Save PHOTO_FLUX file.
Definition: cr2res_io.c:1389
char * cr2res_io_create_extname(int detector, int data)
Create Extname.
Definition: cr2res_io.c:618
int cr2res_io_save_WAVE_MAP(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a WAVE_MAP.
Definition: cr2res_io.c:1916
int cr2res_io_convert_order_idx_to_idxp(int order_idx)
Convert the order_idx to the order_idxp.
Definition: cr2res_io.c:583
int cr2res_io_save_TRACE_WAVE(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **tables, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a TRACE_WAVE.
Definition: cr2res_io.c:1670
int cr2res_io_save_SPLICED_1D(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table *spliced_1d, const cpl_propertylist *qc_list, cpl_propertylist *ext_plist, const char *procatg, const char *recipe)
Save a SPLICED_1D.
Definition: cr2res_io.c:1978
cpl_frameset * cr2res_io_extract_decker_frameset(const cpl_frameset *in, const char *tag, cr2res_decker decker)
Extract the frames with the given tag and Decker position.
Definition: cr2res_io.c:527
int cr2res_io_save_EXTRACT_2D(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **tables, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a 2D extracted spectrum.
Definition: cr2res_io.c:2009
cpl_vector * cr2res_io_read_dits(const cpl_frameset *in)
Get the DITS from a frame set.
Definition: cr2res_io.c:432
int cr2res_io_save_EXTRACT_1D(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **tables, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a 1D extracted spectrum.
Definition: cr2res_io.c:1732
cpl_table * cr2res_load_table(const char *in, int det_nr, int pmin, int pmax)
Load the table accordingly.
Definition: cr2res_io.c:862
hdrl_image * cr2res_io_load_SLIT_MODEL(const char *filename, int detector)
Load an hdrl image from a SLIT MODEL.
Definition: cr2res_io.c:1169
cpl_frameset * cr2res_io_find_TRACE_WAVE_all(const cpl_frameset *in)
Get the CR2RES_TW_DRSTYPE frames from a frameset.
Definition: cr2res_io.c:293
hdrl_imagelist * cr2res_io_load_image_list_from_set(const cpl_frameset *in, int detector)
Load an hdrl image list from an images frameset.
Definition: cr2res_io.c:808
hdrl_imagelist * cr2res_io_load_DETLIN_COEFFS(const char *filename, int detector)
Load the detlin coefficients.
Definition: cr2res_io.c:1034
hdrl_image * cr2res_io_load_MASTER_DARK(const char *filename, int detector)
Load an image from a MASTER_DARK.
Definition: cr2res_io.c:990
cpl_table * cr2res_io_get_eop_table()
Get the eop_table.
Definition: cr2res_io.c:120
int cr2res_io_save_MASTER_FLAT(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **master_flats, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a MASTER_FLAT.
Definition: cr2res_io.c:1639
int cr2res_io_save_MASTER_DARK(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **master_darks, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a MASTER_DARK.
Definition: cr2res_io.c:1491
cpl_frameset * cr2res_io_find_BPM_all(const cpl_frameset *in)
Get the CR2RES_BPM_DRSTYPE frames from a frameset.
Definition: cr2res_io.c:397
int cr2res_io_save_DETLIN_COEFFS(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_imagelist **coeffs, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a DETLIN COEFFS.
Definition: cr2res_io.c:1523
int cr2res_io_save_THROUGHPUT(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **tables, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a THROUGHPUT table.
Definition: cr2res_io.c:1762
int cr2res_io_get_ext_idx(const char *filename, int detector, int data)
Get the wished extension number for a detector.
Definition: cr2res_io.c:644
int cr2res_io_save_TRACE_MAP(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a TRACE_MAP.
Definition: cr2res_io.c:1885
cpl_table * cr2res_io_load_SLIT_FUNC(const char *filename, int detector)
Load a table from a SLIT_FUNC.
Definition: cr2res_io.c:1139
int cr2res_io_save_SLIT_CURV_MAP(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a SLIT_CURV_MAP.
Definition: cr2res_io.c:1947
hdrl_image * cr2res_io_load_image(const char *in, int detector)
Load an hdrl image from a image file.
Definition: cr2res_io.c:704
int cr2res_io_save_CALIBRATED(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **calib_collapsed, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a CALIBRATED frame.
Definition: cr2res_io.c:1608
int cr2res_io_save_SLIT_FUNC(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **slit_func, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a SLIT_FUNC.
Definition: cr2res_io.c:1792
int cr2res_io_save_BPM(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_image **bpms, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a BPM.
Definition: cr2res_io.c:1555
cpl_frameset * cr2res_io_find_EXTRACT_1D_all(const cpl_frameset *in)
Get the CR2RES_EXTRACT_1D_DRSTYPE frames from a frameset.
Definition: cr2res_io.c:331
int cr2res_io_save_COMBINED(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, hdrl_image **data, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a COMBINED.
Definition: cr2res_io.c:1854
cr2res_decker * cr2res_io_read_decker_positions(const cpl_frameset *in)
Get the decker positions from a frame set.
Definition: cr2res_io.c:491
cpl_bivector * cr2res_io_load_EMISSION_LINES(const char *filename)
Load an EMISSION_LINES bivector.
Definition: cr2res_io.c:903
cpl_table * cr2res_io_load_TRACE_WAVE(const char *filename, int detector)
Load a table from a TRACE_WAVE.
Definition: cr2res_io.c:1109
int cr2res_io_save_LINES_DIAGNOSTICS(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **tables, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a LINES_DIAGNOSTICS.
Definition: cr2res_io.c:1701
cpl_image * cr2res_io_load_BPM(const char *filename, int detector, int data)
Load an image from a BPM.
Definition: cr2res_io.c:957
cpl_vector * cr2res_io_read_ndits(const cpl_frameset *in)
Get the NDITs from a frame set.
Definition: cr2res_io.c:462
hdrl_imagelist * cr2res_io_load_image_list(const char *in, int detector)
Load an hdrl image list from a cube file.
Definition: cr2res_io.c:754
int cr2res_io_save_POL_SPEC(const char *filename, cpl_frameset *allframes, cpl_frameset *inframes, const cpl_parameterlist *parlist, cpl_table **tables, const cpl_propertylist *qc_list, cpl_propertylist **ext_plist, const char *procatg, const char *recipe)
Save a Polarimetry spectrum.
Definition: cr2res_io.c:2039
hdrl_image * cr2res_io_load_MASTER_FLAT(const char *filename, int detector)
Load an hdrl image from a MASTER_FLAT.
Definition: cr2res_io.c:1066
cr2res_decker cr2res_pfits_get_decker_position(const cpl_propertylist *plist)
find out the decker position
Definition: cr2res_pfits.c:467
const char * cr2res_pfits_get_wlen_id(const cpl_propertylist *plist)
find out the Setting
Definition: cr2res_pfits.c:137
const char * cr2res_pfits_get_protype(const cpl_propertylist *plist)
find out the PRO.TYPE
Definition: cr2res_pfits.c:98
double cr2res_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
Definition: cr2res_pfits.c:199
int cr2res_pfits_get_ndit(const cpl_propertylist *plist)
find out the NDIT value
Definition: cr2res_pfits.c:363
const char * cr2res_pfits_get_drstype(const cpl_propertylist *plist)
find out the DRS.TYPE
Definition: cr2res_pfits.c:86
double cr2res_mjd_obs_now(void)
Get the current MJD-OBS.
Definition: cr2res_utils.c:58
int cr2res_format_setting(char *setting_id)
Format the setting.
Definition: cr2res_utils.c:152
cpl_frameset * cr2res_extract_frameset_several_tags(const cpl_frameset *in, const char **tags, int ntags)
Extract the frames whose tag is within a list from a frameset.
cpl_error_code hdrl_image_reject_value(hdrl_image *self, cpl_value mode)
Reject pixels with the specified special value(s)
Definition: hdrl_image.c:477
cpl_image * hdrl_image_get_error(hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:131
const cpl_image * hdrl_image_get_error_const(const hdrl_image *himg)
get error as cpl image
Definition: hdrl_image.c:144
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
cpl_image * hdrl_image_get_image(hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:105
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:118
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379
hdrl_imagelist * hdrl_imagelist_create(cpl_imagelist *imlist, cpl_imagelist *errlist)
Create an hdrl_imagelist out of 2 cpl_imagelist.
cpl_size hdrl_imagelist_get_size(const hdrl_imagelist *himlist)
Get the number of images in the imagelist.
hdrl_image * hdrl_imagelist_get(const hdrl_imagelist *himlist, cpl_size inum)
Get an image from a list of images.