CR2RE Pipeline Reference Manual 1.7.0
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
485/*----------------------------------------------------------------------------*/
491/*----------------------------------------------------------------------------*/
492cpl_vector * cr2res_io_read_ndsamples(const cpl_frameset * in)
493{
494 cpl_vector * ndsamples ;
495 cpl_size i ;
496
497 /* Check entries */
498 if (in == NULL) return NULL ;
499
500 /* Allocate the vector */
501 ndsamples = cpl_vector_new(cpl_frameset_get_size(in)) ;
502
503 /* Loop on the frames */
504 for (i=0 ; i< cpl_vector_get_size(ndsamples) ; i++) {
505 cpl_propertylist * plist ;
506 plist = cpl_propertylist_load(cpl_frame_get_filename(
507 cpl_frameset_get_position_const(in, i)), 0) ;
508 cpl_vector_set(ndsamples, i, cr2res_pfits_get_ndsamples(plist)) ;
509 cpl_propertylist_delete(plist) ;
510 }
511
512 return ndsamples ;
513}
514/*----------------------------------------------------------------------------*/
520/*----------------------------------------------------------------------------*/
521cr2res_decker * cr2res_io_read_decker_positions(const cpl_frameset * in)
522{
523 cr2res_decker * out ;
524 cpl_size nframes, i ;
525
526 /* Check entries */
527 if (in == NULL) return NULL ;
528
529 /* Initialise */
530 nframes = cpl_frameset_get_size(in) ;
531
532 /* Allocate the vector */
533 out = cpl_malloc(nframes * sizeof(cr2res_decker)) ;
534
535 /* Loop on the frames */
536 for (i = 0; i < nframes; i++) {
537 cpl_propertylist *plist;
538 plist = cpl_propertylist_load(cpl_frame_get_filename(
539 cpl_frameset_get_position_const(in, i)), 0) ;
540 out[i] = cr2res_pfits_get_decker_position(plist) ;
541 cpl_propertylist_delete(plist) ;
542 }
543 return out ;
544}
545
546/*----------------------------------------------------------------------------*/
556/*----------------------------------------------------------------------------*/
558 const cpl_frameset * in,
559 const char * tag,
560 cr2res_decker decker)
561{
562 cpl_frameset * out ;
563 cpl_frame * loc_frame ;
564 int nbframes;
565 cpl_propertylist * plist ;
566 int i ;
567
568 /* Test entries */
569 if (in == NULL) return NULL ;
570 if (tag == NULL) return NULL ;
571 if (decker != CR2RES_DECKER_NONE && decker != CR2RES_DECKER_1_3 &&
572 decker != CR2RES_DECKER_2_4)
573 return NULL ;
574
575 /* Initialise */
576 nbframes = cpl_frameset_get_size(in) ;
577
578 /* Count the frames with the tag */
579 if ((cpl_frameset_count_tags(in, tag)) == 0) return NULL ;
580
581 /* Create the output frameset */
582 out = cpl_frameset_new() ;
583
584 /* Loop on the requested frames and store them in out */
585 for (i=0 ; i<nbframes ; i++) {
586 const cpl_frame * cur_frame ;
587 cur_frame = cpl_frameset_get_position_const(in, i) ;
588 if (!strcmp(cpl_frame_get_tag(cur_frame), tag)) {
589 /* Get the propertylist */
590 plist = cpl_propertylist_load(cpl_frame_get_filename(cur_frame), 0);
591 if (cr2res_pfits_get_decker_position(plist) == decker) {
592 loc_frame = cpl_frame_duplicate(cur_frame) ;
593 cpl_frameset_insert(out, loc_frame) ;
594 }
595 cpl_propertylist_delete(plist) ;
596 }
597 }
598 /* No matching frame found */
599 if (cpl_frameset_get_size(out) == 0){
600 cpl_frameset_delete(out) ;
601 return NULL ;
602 }
603 return out ;
604}
605/*----------------------------------------------------------------------------*/
612/*----------------------------------------------------------------------------*/
614{
615 /* Check entries */
616 if (order_idx < -49 || order_idx > 50) return -1 ;
617
618 /* Conversion order_idx -> order_idxp */
619 if (order_idx < 0) return order_idx + 100 ;
620 else return order_idx ;
621}
622#ifdef CR2RES_UNUSED
623/*----------------------------------------------------------------------------*/
629/*----------------------------------------------------------------------------*/
630int cr2res_io_convert_order_idxp_to_idx(int order_idxp)
631{
632 /* Check entries */
633 if (order_idxp < 0 || order_idxp > 99) return -1 ;
634
635 /* Conversion order <-> keyword Index */
636 if (order_idxp > 50) return order_idxp - 100 ;
637 else return order_idxp ;
638}
639#endif
640/*----------------------------------------------------------------------------*/
647/*----------------------------------------------------------------------------*/
649 int detector,
650 int data)
651{
652 char * wished_extname ;
653
654 /* Check entries */
655 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
656
657 /* Create wished EXTNAME */
658 /* If this changes, update warning message in cr2res_io_get_ext_idx() */
659 if (data) wished_extname = cpl_sprintf("CHIP%d.INT1", detector) ;
660 else wished_extname = cpl_sprintf("CHIP%dERR.INT1", detector) ;
661
662 return wished_extname ;
663}
664
665/*----------------------------------------------------------------------------*/
673/*----------------------------------------------------------------------------*/
675 const char * filename,
676 int detector,
677 int data)
678{
679 char * wished_extname ;
680 int wished_ext_nb = -1 ;
681 int nb_ext, i ;
682
683 /* Check entries */
684 if (filename == NULL) return -1 ;
685 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return -1 ;
686
687 /* Create wished EXTNAME */
688 wished_extname = cr2res_io_create_extname(detector, data) ;
689
690 /* Get the number of extensions */
691 nb_ext = cpl_fits_count_extensions(filename) ;
692
693 /* Loop on the extensions */
694 for (i=1 ; i<=nb_ext ; i++) {
695 const char * extname ;
696 cpl_propertylist * pl ;
697 /* Get the header */
698 pl = cpl_propertylist_load(filename, i) ;
699 /* Read the EXTNAME */
700 extname = cpl_propertylist_get_string(pl, "EXTNAME");
701
702 /* Compare to the wished one */
703 if (strcmp(extname, wished_extname)==0) wished_ext_nb = i ;
704 cpl_propertylist_delete(pl) ;
705 }
706 cpl_free(wished_extname) ;
707
708 /* EXTNAME expectation */
709 if (wished_ext_nb < 0) {
710 /* Warning only for data - error are sometimes optional */
711 if (data == 1)
712 cpl_msg_warning(__func__,
713 "EXTNAME is supposed to match CHIPn.INT1 or CHIPnERR.INT1 (n=1/2/3)") ;
714 }
715
716 return wished_ext_nb ;
717}
718
719/*----------------------------------------------------------------------------*/
720/*-------------------- LOADING FUNCTIONS -------------------------*/
721/*----------------------------------------------------------------------------*/
722
723/*----------------------------------------------------------------------------*/
733/*----------------------------------------------------------------------------*/
735 const char * in,
736 int detector)
737{
738 hdrl_image * out ;
739 cpl_image * data ;
740 cpl_image * err ;
741 int ext_nr_data, ext_nr_err ;
742
743 /* Check entries */
744 if (in == NULL) return NULL ;
745 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
746
747 /* Get the extension numbers for this detector */
748 ext_nr_data = cr2res_io_get_ext_idx(in, detector, 1) ;
749 ext_nr_err = cr2res_io_get_ext_idx(in, detector, 0) ;
750
751 /* The wished extension was not found */
752 if (ext_nr_data < 0) return NULL ;
753
754 /* Load the image */
755 data = cpl_image_load(in, CPL_TYPE_DOUBLE, 0, ext_nr_data) ;
756 if (ext_nr_err >= 0)
757 err = cpl_image_load(in, CPL_TYPE_DOUBLE, 0, ext_nr_err) ;
758 else
759 err = NULL ;
760
761 /* Set the NaN pixels as bad */
762 cr2res_io_set_NaNs_as_bpm(data) ;
763
764 /* Create output hdrl image */
765 out = hdrl_image_create(data, err) ;
766
767 /* Return */
768 if (data != NULL) cpl_image_delete(data) ;
769 if (err != NULL) cpl_image_delete(err) ;
770 return out ;
771}
772
773/*----------------------------------------------------------------------------*/
783/*----------------------------------------------------------------------------*/
785 const char * in,
786 int detector)
787{
788 hdrl_imagelist * out ;
789 cpl_imagelist * data ;
790 cpl_imagelist * error ;
791 cpl_size i ;
792 int wished_ext_nb_data, wished_ext_nb_error ;
793
794 /* Check entries */
795 if (in == NULL) return NULL ;
796 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
797
798 /* Get the extension number for this detector */
799 wished_ext_nb_data = cr2res_io_get_ext_idx(in, detector, 1) ;
800 wished_ext_nb_error = cr2res_io_get_ext_idx(in, detector, 0) ;
801
802 /* The wished extension was not found */
803 if (wished_ext_nb_data < 0) return NULL ;
804
805 /* Load the image list */
806 data = cpl_imagelist_load(in, CPL_TYPE_DOUBLE, wished_ext_nb_data);
807 if (wished_ext_nb_error >= 0)
808 error = cpl_imagelist_load(in, CPL_TYPE_DOUBLE, wished_ext_nb_error);
809 else
810 error = NULL ;
811
812 /* Set the NaN pixels as bad */
813 for (i=0 ; i<cpl_imagelist_get_size(data) ; i++)
814 cr2res_io_set_NaNs_as_bpm(cpl_imagelist_get(data, i)) ;
815
816 /* Create output hdrl image */
817 out = hdrl_imagelist_create(data, error) ;
818
819 /* Return */
820 if (data != NULL) cpl_imagelist_delete(data) ;
821 if (error != NULL) cpl_imagelist_delete(error) ;
822 return out ;
823}
824
825/*----------------------------------------------------------------------------*/
837/*----------------------------------------------------------------------------*/
839 const cpl_frameset * in,
840 int detector)
841{
842 const char * first_file ;
843 hdrl_imagelist * out ;
844 cpl_imagelist * data ;
845 cpl_imagelist * err ;
846 cpl_size i ;
847 int ext_nr_data, ext_nr_err ;
848
849 /* Check entries */
850 if (in == NULL) return NULL ;
851 if (cpl_frameset_get_size(in) < 1) return NULL ;
852 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
853
854 /* Get the extension number for this detector */
855 first_file = cpl_frame_get_filename(cpl_frameset_get_position_const(in,0)) ;
856 ext_nr_data = cr2res_io_get_ext_idx(first_file, detector, 1) ;
857 ext_nr_err = cr2res_io_get_ext_idx(first_file, detector, 0) ;
858
859 /* The wished extension was not found */
860 if (ext_nr_data < 0) return NULL ;
861
862 /* Load the image list */
863 data = cpl_imagelist_load_frameset(in, CPL_TYPE_DOUBLE, 1, ext_nr_data) ;
864 if (ext_nr_err >= 0)
865 err = cpl_imagelist_load_frameset(in, CPL_TYPE_DOUBLE, 1, ext_nr_err) ;
866 else
867 err = NULL ;
868
869 /* Set the NaN pixels as bad */
870 for (i=0 ; i<cpl_imagelist_get_size(data) ; i++)
871 cr2res_io_set_NaNs_as_bpm(cpl_imagelist_get(data, i)) ;
872
873 /* Create output hdrl image */
874 out = hdrl_imagelist_create(data, err) ;
875
876 /* Return */
877 if (data != NULL) cpl_imagelist_delete(data) ;
878 if (err != NULL) cpl_imagelist_delete(err) ;
879 return out ;
880}
881
882/*----------------------------------------------------------------------------*/
891/*----------------------------------------------------------------------------*/
893 const char * in,
894 int det_nr,
895 int pmin,
896 int pmax)
897{
898 cpl_table * out ;
899 cpl_table * out_tmp ;
900 //int ext_nr ;
901
902 /* Check entries */
903 if (in == NULL) return NULL ;
904 if (det_nr < 1 || det_nr > CR2RES_NB_DETECTORS) return NULL ;
905
906 /* Get the extension number for this detector */
907 //ext_nr = cr2res_io_get_ext_idx(in, det_nr, 1) ;
908
909 /* Load the table */
910 if ((out = cpl_table_load(in, det_nr, 0)) == NULL) {
911 cpl_msg_error(__func__, "Cannot load %s as a table", in) ;
912 return NULL ;
913 }
914
915 /* Select only between pmin and pmax */
916 if (pmin>0 && pmax>0 && pmax >= pmin && pmax <= cpl_table_get_nrow(out)) {
917 out_tmp = cpl_table_extract(out, pmin, pmax-pmin+1) ;
918 if (out_tmp != NULL) {
919 cpl_table_delete(out) ;
920 out = out_tmp ;
921 }
922 }
923 return out ;
924}
925
926/*----------------------------------------------------------------------------*/
932/*----------------------------------------------------------------------------*/
934 const char * filename)
935{
936 cpl_bivector * lines ;
937 double * lines_x ;
938 double * lines_y ;
939 cpl_table * lines_tab ;
940 int i, tab_size;// log_flag ;
941
942 /* Check Entries */
943 if (filename == NULL) return NULL ;
944
945 /* Check PRO.TYPE */
946 if (cr2res_io_check_drs_type(filename, CR2RES_DRSTYPE_CATALOG) != 1) {
947 cpl_msg_info(__func__, "File check failed for %s", filename);
948 return NULL ;
949 }
950
951 /* Initialise */
952 //log_flag = 0 ;
953
954 /* Load the file in a table */
955 lines_tab = cpl_table_load(filename, 1, 1) ;
956 tab_size = cpl_table_get_nrow(lines_tab) ;
957
958 /* Create the bivector */
959 lines = cpl_bivector_new(tab_size) ;
960 lines_x = cpl_bivector_get_x_data(lines) ;
961 lines_y = cpl_bivector_get_y_data(lines) ;
962 for (i=0 ; i<tab_size ; i++) {
963
964 double val ;
965 lines_x[i] = cpl_table_get(lines_tab, CR2RES_COL_WAVELENGTH, i, NULL) ;
966 val = cpl_table_get(lines_tab, CR2RES_COL_EMISSION, i, NULL) ;
967 //if (log_flag && val > 0) lines_y[i] = log10(val) ;
968 //else lines_y[i] = val ;
969 lines_y[i] = val ;
970 }
971
972 /* Free and return */
973 cpl_table_delete(lines_tab) ;
974 return lines ;
975}
976
977/*----------------------------------------------------------------------------*/
986/*----------------------------------------------------------------------------*/
988 const char * filename,
989 int detector,
990 int data)
991{
992 int wished_ext_nb ;
993
994 /* Check entries */
995 if (filename == NULL) return NULL ;
996 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
997
998 /* Check PRO.TYPE */
999 if (cr2res_io_check_drs_type(filename, CR2RES_BPM_DRSTYPE) != 1)
1000 return NULL ;
1001
1002 /* Get the extension number for this detector */
1003 wished_ext_nb = cr2res_io_get_ext_idx(filename, detector, data) ;
1004
1005 /* The wished extension was not found */
1006 if (wished_ext_nb < 0) return NULL ;
1007
1008 return cpl_image_load(filename, CPL_TYPE_INT, 0, wished_ext_nb);
1009}
1010
1011/*----------------------------------------------------------------------------*/
1019/*----------------------------------------------------------------------------*/
1021 const char * filename,
1022 int detector)
1023{
1024 hdrl_image * master_dark ;
1025
1026 /* Check entries */
1027 if (filename == NULL) return NULL ;
1028 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1029
1030 /* Check PRO.TYPE */
1031 if (cr2res_io_check_drs_type(filename, CR2RES_MASTER_DARK_DRSTYPE) != 1)
1032 return NULL ;
1033
1034 /* Load */
1035 master_dark = cr2res_io_load_image(filename, detector) ;
1036
1037 /* In order to not destroy values in data, the NaNs in the */
1038 /* loaded mastercal are replaced by 0.0, and masked, so that they */
1039 /* can be unmasked later. */
1040 hdrl_image_reject_value(master_dark, CPL_VALUE_NAN);
1041 cpl_image_fill_rejected(hdrl_image_get_image(master_dark), 0.0);
1042
1043 /* Error must exist */
1044 if (hdrl_image_get_error(master_dark) == NULL) {
1045 cpl_msg_error(__func__, "The error is missing") ;
1046 hdrl_image_delete(master_dark) ;
1047 return NULL ;
1048 }
1049
1050 /* Return */
1051 return master_dark ;
1052}
1053
1054/*----------------------------------------------------------------------------*/
1063/*----------------------------------------------------------------------------*/
1065 const char * filename,
1066 int detector)
1067{
1068 hdrl_imagelist * detlin_coeffs ;
1069
1070 /* Check entries */
1071 if (filename == NULL) return NULL ;
1072 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1073
1074 /* Check PRO.TYPE */
1075 if (cr2res_io_check_drs_type(filename, CR2RES_DETLIN_COEFFS_DRSTYPE) != 1)
1076 return NULL ;
1077
1078 /* Load */
1079 detlin_coeffs = cr2res_io_load_image_list(filename, detector) ;
1080
1081 /* TODO - Error must exist */
1082
1083 /* Return */
1084 return detlin_coeffs ;
1085}
1086
1087/*----------------------------------------------------------------------------*/
1095/*----------------------------------------------------------------------------*/
1097 const char * filename,
1098 int detector)
1099{
1100 hdrl_image * master_flat ;
1101
1102 /* Check entries */
1103 if (filename == NULL) return NULL ;
1104 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1105
1106 /* Check PRO.TYPE */
1107 if (cr2res_io_check_drs_type(filename, CR2RES_MASTER_FLAT_DRSTYPE) != 1)
1108 return NULL ;
1109
1110 /* Load */
1111 master_flat = cr2res_io_load_image(filename, detector) ;
1112
1113 /* In order to not destroy values in data, the NaNs in the */
1114 /* loaded mastercal are replaced by 1.0, and masked, so that they */
1115 /* can be unmasked later. */
1116 hdrl_image_reject_value(master_flat, CPL_VALUE_NAN);
1117 cpl_image_fill_rejected(hdrl_image_get_image(master_flat),1.0);
1118
1119 /* Error must exist */
1120 if (hdrl_image_get_error(master_flat) == NULL) {
1121 cpl_msg_error(__func__, "The error is missing") ;
1122 hdrl_image_delete(master_flat) ;
1123 return NULL ;
1124 }
1125
1126 /* Return */
1127 return master_flat ;
1128}
1129
1130/*----------------------------------------------------------------------------*/
1138/*----------------------------------------------------------------------------*/
1140 const char * filename,
1141 int detector)
1142{
1143 cpl_table * trace_wave_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_TW_DRSTYPE) != 1)
1151 return NULL ;
1152
1153 /* Load the table */
1154 trace_wave_tab = cr2res_load_table(filename, detector, -1, -1) ;
1155
1156 /* Return */
1157 return trace_wave_tab ;
1158}
1159
1160/*----------------------------------------------------------------------------*/
1168/*----------------------------------------------------------------------------*/
1170 const char * filename,
1171 int detector)
1172{
1173 cpl_table * slit_func_tab ;
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_FUNC_DRSTYPE) != 1)
1181 return NULL ;
1182
1183 /* Load the table */
1184 slit_func_tab = cr2res_load_table(filename, detector, -1, -1) ;
1185
1186 /* Return */
1187 return slit_func_tab ;
1188}
1189
1190/*----------------------------------------------------------------------------*/
1198/*----------------------------------------------------------------------------*/
1200 const char * filename,
1201 int detector)
1202{
1203 hdrl_image * slit_model ;
1204
1205 /* Check entries */
1206 if (filename == NULL) return NULL ;
1207 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1208
1209 /* Check PRO.TYPE */
1210 if (cr2res_io_check_drs_type(filename, CR2RES_SLIT_MODEL_DRSTYPE) != 1)
1211 return NULL ;
1212
1213 /* Load */
1214 slit_model = cr2res_io_load_image(filename, detector) ;
1215
1216 /* Error must exist */
1217 if (hdrl_image_get_error(slit_model) == NULL) {
1218 cpl_msg_error(__func__, "The error is missing") ;
1219 hdrl_image_delete(slit_model) ;
1220 return NULL ;
1221 }
1222
1223 /* Return */
1224 return slit_model ;
1225}
1226#ifdef CR2RES_UNUSED
1227/*----------------------------------------------------------------------------*/
1235/*----------------------------------------------------------------------------*/
1236hdrl_image * cr2res_io_load_TRACE_MAP(
1237 const char * filename,
1238 int detector)
1239{
1240 hdrl_image * trace_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_TRACE_MAP_DRSTYPE) != 1)
1248 return NULL ;
1249
1250 /* Load */
1251 trace_map = cr2res_io_load_image(filename, detector) ;
1252
1253 /* Return */
1254 return trace_map ;
1255}
1256
1257/*----------------------------------------------------------------------------*/
1265/*----------------------------------------------------------------------------*/
1266hdrl_image * cr2res_io_load_WAVE_MAP(
1267 const char * filename,
1268 int detector)
1269{
1270 hdrl_image * wave_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_WAVE_MAP_DRSTYPE) != 1)
1278 return NULL ;
1279
1280 /* Load */
1281 wave_map = cr2res_io_load_image(filename, detector) ;
1282
1283 /* Return */
1284 return wave_map ;
1285}
1286
1287/*----------------------------------------------------------------------------*/
1295/*----------------------------------------------------------------------------*/
1296hdrl_image * cr2res_io_load_SLIT_CURV_MAP(
1297 const char * filename,
1298 int detector)
1299{
1300 hdrl_image * slit_curv_map ;
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_SLIT_CURV_MAP_DRSTYPE) != 1)
1308 return NULL ;
1309
1310 /* Load */
1311 slit_curv_map = cr2res_io_load_image(filename, detector) ;
1312
1313 /* Return */
1314 return slit_curv_map ;
1315}
1316#endif
1317/*----------------------------------------------------------------------------*/
1325/*----------------------------------------------------------------------------*/
1327 const char * filename,
1328 int detector)
1329{
1330 cpl_table * extract_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_EXTRACT_1D_DRSTYPE) != 1)
1338 return NULL ;
1339
1340 /* Load the table */
1341 extract_1D_tab = cr2res_load_table(filename, detector, -1, -1) ;
1342
1343 /* Return */
1344 return extract_1D_tab ;
1345}
1346#ifdef CR2RES_UNUSED
1347/*----------------------------------------------------------------------------*/
1355/*----------------------------------------------------------------------------*/
1356cpl_table * cr2res_io_load_SPLICED_1D(
1357 const char * filename,
1358 int detector)
1359{
1360 cpl_table * spliced_1D_tab ;
1361
1362 /* Check entries */
1363 if (filename == NULL) return NULL ;
1364 if (detector < 1 || detector > CR2RES_NB_DETECTORS) return NULL ;
1365
1366 /* Check PRO.TYPE */
1367 if (cr2res_io_check_drs_type(filename, CR2RES_SPLICED_1D_DRSTYPE) != 1)
1368 return NULL ;
1369
1370 /* Load the table */
1371 spliced_1D_tab = cr2res_load_table(filename, detector, -1, -1) ;
1372
1373 /* Return */
1374 return spliced_1D_tab ;
1375}
1376
1377/*----------------------------------------------------------------------------*/
1385/*----------------------------------------------------------------------------*/
1386cpl_table * cr2res_io_load_EXTRACT_2D(
1387 const char * filename,
1388 int detector)
1389{
1390 cpl_table * extract_2D_tab ;
1391
1392 /* Check PRO.TYPE */
1393 if (cr2res_io_check_drs_type(filename, CR2RES_EXTRACT_2D_DRSTYPE) != 1)
1394 return NULL ;
1395
1396 /* Load the table */
1397 extract_2D_tab = cr2res_load_table(filename, detector, -1, -1) ;
1398
1399 /* Return */
1400 return extract_2D_tab ;
1401}
1402#endif
1403/*----------------------------------------------------------------------------*/
1404/*--------------------- SAVING FUNCTIONS -------------------------*/
1405/*----------------------------------------------------------------------------*/
1406/* TODO ? set frame levels to mark temp, intermediate and final frames? */
1407
1408/*----------------------------------------------------------------------------*/
1418/*----------------------------------------------------------------------------*/
1420 const char * filename,
1421 cpl_table * out_table,
1422 const cpl_parameterlist * parlist,
1423 cpl_frameset * set,
1424 const char * recipe)
1425{
1426 cpl_propertylist * plist ;
1427 double mjd_obs ;
1428
1429 plist = cpl_propertylist_new();
1430 cpl_propertylist_append_string(plist, CR2RES_HEADER_INSTRUMENT, "CRIRES") ;
1431 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
1432 CR2RES_PHOTO_FLUX_PROCATG) ;
1433 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, "STATIC") ;
1434 cpl_propertylist_append_string(plist, CR2RES_HEADER_DRS_TYPE,
1435 CR2RES_PHOTO_FLUX_DRSTYPE) ;
1436 mjd_obs = cr2res_mjd_obs_now() ;
1437 cpl_propertylist_append_double(plist, CR2RES_HEADER_MJD_OBS, mjd_obs) ;
1438
1439
1440 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
1441 NULL, recipe, plist, NULL,
1442 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
1443 cpl_msg_error(__func__, "Cannot save the table") ;
1444 cpl_propertylist_delete(plist) ;
1445 return -1 ;
1446 }
1447 cpl_propertylist_delete(plist) ;
1448
1449 /* Return */
1450 return 0 ;
1451}
1452
1453/*----------------------------------------------------------------------------*/
1464/*----------------------------------------------------------------------------*/
1466 const char * filename,
1467 cpl_table * out_table,
1468 const cpl_parameterlist * parlist,
1469 cpl_frameset * set,
1470 const char * recipe,
1471 const char * setting_string)
1472{
1473 cpl_propertylist * plist ;
1474 double mjd_obs ;
1475
1476 plist = cpl_propertylist_new();
1477 cpl_propertylist_append_string(plist, CR2RES_HEADER_INSTRUMENT, "CRIRES") ;
1478 cpl_propertylist_append_string(plist, CPL_DFS_PRO_CATG,
1479 CR2RES_EMISSION_LINES_PROCATG) ;
1480 cpl_propertylist_append_string(plist, CPL_DFS_PRO_TYPE, "STATIC") ;
1481 cpl_propertylist_append_string(plist, CR2RES_HEADER_DRS_TYPE,
1482 CR2RES_DRSTYPE_CATALOG) ;
1483
1484 mjd_obs = cr2res_mjd_obs_now() ;
1485 cpl_propertylist_append_double(plist, CR2RES_HEADER_MJD_OBS, mjd_obs) ;
1486
1487 if (setting_string != NULL) {
1488 cpl_propertylist_append_string(plist, CR2RES_HEADER_WLEN_ID,
1489 setting_string) ;
1490 }
1491
1492 if (cpl_dfs_save_table(set, NULL, parlist, set, NULL, out_table,
1493 NULL, recipe, plist, NULL,
1494 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
1495 cpl_msg_error(__func__, "Cannot save the table") ;
1496 cpl_propertylist_delete(plist) ;
1497 return -1 ;
1498 }
1499 cpl_propertylist_delete(plist) ;
1500
1501 /* Return */
1502 return 0 ;
1503}
1504
1505/*----------------------------------------------------------------------------*/
1520/*----------------------------------------------------------------------------*/
1522 const char * filename,
1523 cpl_frameset * allframes,
1524 cpl_frameset * inframes,
1525 const cpl_parameterlist * parlist,
1526 hdrl_image ** master_darks,
1527 const cpl_propertylist * qc_list,
1528 cpl_propertylist ** ext_plist,
1529 const char * procatg,
1530 const char * recipe)
1531{
1532 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1533 master_darks, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1534 procatg, CR2RES_MASTER_DARK_DRSTYPE) ;
1535}
1536
1537/*----------------------------------------------------------------------------*/
1552/*----------------------------------------------------------------------------*/
1554 const char * filename,
1555 cpl_frameset * allframes,
1556 cpl_frameset * inframes,
1557 const cpl_parameterlist * parlist,
1558 hdrl_imagelist ** coeffs,
1559 const cpl_propertylist * qc_list,
1560 cpl_propertylist ** ext_plist,
1561 const char * procatg,
1562 const char * recipe)
1563{
1564 return cr2res_io_save_imagelist(filename, allframes, inframes, parlist,
1565 coeffs, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1566 procatg, CR2RES_DETLIN_COEFFS_DRSTYPE) ;
1567}
1568
1569/*----------------------------------------------------------------------------*/
1584/*----------------------------------------------------------------------------*/
1586 const char * filename,
1587 cpl_frameset * allframes,
1588 cpl_frameset * inframes,
1589 const cpl_parameterlist * parlist,
1590 cpl_image ** bpms,
1591 const cpl_propertylist * qc_list,
1592 cpl_propertylist ** ext_plist,
1593 const char * procatg,
1594 const char * recipe)
1595{
1596 hdrl_image * hdrl_bpms[CR2RES_NB_DETECTORS] ;
1597 int det_nr, ret ;
1598 cpl_image * bpm_tmp;
1599
1600 /* Convert to HDRL images */
1601 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
1602 if (bpms[det_nr-1] == NULL){
1603 hdrl_bpms[det_nr-1] = NULL ;
1604 } else{
1605 bpm_tmp = cr2res_bpm_mask_edgepix(bpms[det_nr-1]);
1606 hdrl_bpms[det_nr-1] = hdrl_image_create(bpm_tmp, NULL) ;
1607 cpl_image_delete(bpm_tmp);
1608 }
1609 }
1610
1611 /* Save */
1612 ret = cr2res_io_save_image(filename, allframes, inframes, parlist,
1613 hdrl_bpms, qc_list, ext_plist, CPL_TYPE_INT, recipe,
1614 procatg, CR2RES_BPM_DRSTYPE) ;
1615
1616 /* Free and return */
1617 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
1618 hdrl_image_delete(hdrl_bpms[det_nr-1]) ;
1619 }
1620 return ret ;
1621}
1622
1623/*----------------------------------------------------------------------------*/
1637/*----------------------------------------------------------------------------*/
1639 const char * filename,
1640 cpl_frameset * allframes,
1641 cpl_frameset * inframes,
1642 const cpl_parameterlist * parlist,
1643 hdrl_image ** calib_collapsed,
1644 const cpl_propertylist * qc_list,
1645 cpl_propertylist ** ext_plist,
1646 const char * procatg,
1647 const char * recipe)
1648{
1649 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1650 calib_collapsed, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1651 procatg, CR2RES_CALIBRATED_DRSTYPE) ;
1652}
1653
1654/*----------------------------------------------------------------------------*/
1668/*----------------------------------------------------------------------------*/
1670 const char * filename,
1671 cpl_frameset * allframes,
1672 cpl_frameset * inframes,
1673 const cpl_parameterlist * parlist,
1674 hdrl_image ** master_flats,
1675 const cpl_propertylist * qc_list,
1676 cpl_propertylist ** ext_plist,
1677 const char * procatg,
1678 const char * recipe)
1679{
1680 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1681 master_flats, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1682 procatg, CR2RES_MASTER_FLAT_DRSTYPE) ;
1683}
1684
1685/*----------------------------------------------------------------------------*/
1699/*----------------------------------------------------------------------------*/
1701 const char * filename,
1702 cpl_frameset * allframes,
1703 cpl_frameset * inframes,
1704 const cpl_parameterlist * parlist,
1705 cpl_table ** tables,
1706 const cpl_propertylist * qc_list,
1707 cpl_propertylist ** ext_plist,
1708 const char * procatg,
1709 const char * recipe)
1710{
1711
1712 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
1713 qc_list, ext_plist, recipe, procatg, CR2RES_TW_DRSTYPE) ;
1714}
1715
1716/*----------------------------------------------------------------------------*/
1730/*----------------------------------------------------------------------------*/
1732 const char * filename,
1733 cpl_frameset * allframes,
1734 cpl_frameset * inframes,
1735 const cpl_parameterlist * parlist,
1736 cpl_table ** tables,
1737 const cpl_propertylist * qc_list,
1738 cpl_propertylist ** ext_plist,
1739 const char * procatg,
1740 const char * recipe)
1741{
1742 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
1743 qc_list, ext_plist, recipe, procatg,
1744 CR2RES_LINES_DIAGNOSTICS_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_EXTRACT_1D_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 ** tables,
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, tables,
1804 qc_list, ext_plist, recipe, procatg, CR2RES_THROUGHPUT_DRSTYPE) ;
1805}
1806
1807/*----------------------------------------------------------------------------*/
1821/*----------------------------------------------------------------------------*/
1823 const char * filename,
1824 cpl_frameset * allframes,
1825 cpl_frameset * inframes,
1826 const cpl_parameterlist * parlist,
1827 cpl_table ** slit_func,
1828 const cpl_propertylist * qc_list,
1829 cpl_propertylist ** ext_plist,
1830 const char * procatg,
1831 const char * recipe)
1832{
1833 return cr2res_io_save_table(filename, allframes, inframes, parlist,
1834 slit_func, qc_list, ext_plist, recipe, procatg,
1835 CR2RES_SLIT_FUNC_DRSTYPE) ;
1836}
1837
1838/*----------------------------------------------------------------------------*/
1852/*----------------------------------------------------------------------------*/
1854 const char * filename,
1855 cpl_frameset * allframes,
1856 cpl_frameset * inframes,
1857 const cpl_parameterlist * parlist,
1858 hdrl_image ** data,
1859 const cpl_propertylist * qc_list,
1860 cpl_propertylist ** ext_plist,
1861 const char * procatg,
1862 const char * recipe)
1863{
1864 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1865 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe, procatg,
1866 CR2RES_SLIT_MODEL_DRSTYPE) ;
1867}
1868
1869/*----------------------------------------------------------------------------*/
1883/*----------------------------------------------------------------------------*/
1885 const char * filename,
1886 cpl_frameset * allframes,
1887 cpl_frameset * inframes,
1888 const cpl_parameterlist * parlist,
1889 hdrl_image ** data,
1890 const cpl_propertylist * qc_list,
1891 cpl_propertylist ** ext_plist,
1892 const char * procatg,
1893 const char * recipe)
1894{
1895 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1896 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe, procatg,
1897 CR2RES_COMBINED_DRSTYPE) ;
1898}
1899
1900/*----------------------------------------------------------------------------*/
1914/*----------------------------------------------------------------------------*/
1916 const char * filename,
1917 cpl_frameset * allframes,
1918 cpl_frameset * inframes,
1919 const cpl_parameterlist * parlist,
1920 hdrl_image ** data,
1921 const cpl_propertylist * qc_list,
1922 cpl_propertylist ** ext_plist,
1923 const char * procatg,
1924 const char * recipe)
1925{
1926 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1927 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1928 procatg, CR2RES_TRACE_MAP_DRSTYPE) ;
1929}
1930
1931/*----------------------------------------------------------------------------*/
1945/*----------------------------------------------------------------------------*/
1947 const char * filename,
1948 cpl_frameset * allframes,
1949 cpl_frameset * inframes,
1950 const cpl_parameterlist * parlist,
1951 hdrl_image ** data,
1952 const cpl_propertylist * qc_list,
1953 cpl_propertylist ** ext_plist,
1954 const char * procatg,
1955 const char * recipe)
1956{
1957 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1958 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1959 procatg, CR2RES_WAVE_MAP_DRSTYPE) ;
1960}
1961
1962/*----------------------------------------------------------------------------*/
1976/*----------------------------------------------------------------------------*/
1978 const char * filename,
1979 cpl_frameset * allframes,
1980 cpl_frameset * inframes,
1981 const cpl_parameterlist * parlist,
1982 hdrl_image ** data,
1983 const cpl_propertylist * qc_list,
1984 cpl_propertylist ** ext_plist,
1985 const char * procatg,
1986 const char * recipe)
1987{
1988 return cr2res_io_save_image(filename, allframes, inframes, parlist,
1989 data, qc_list, ext_plist, CPL_TYPE_DOUBLE, recipe,
1990 procatg, CR2RES_SLIT_CURV_MAP_DRSTYPE) ;
1991}
1992
1993/*----------------------------------------------------------------------------*/
2007/*----------------------------------------------------------------------------*/
2009 const char * filename,
2010 cpl_frameset * allframes,
2011 cpl_frameset * inframes,
2012 const cpl_parameterlist * parlist,
2013 cpl_table * spliced_1d,
2014 const cpl_propertylist * qc_list,
2015 cpl_propertylist * ext_plist,
2016 const char * procatg,
2017 const char * recipe)
2018{
2019 return cr2res_io_save_one_table(filename, allframes, inframes, parlist,
2020 spliced_1d, qc_list, ext_plist, "SPLICED", recipe, procatg,
2021 CR2RES_SPLICED_1D_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_EXTRACT_2D_DRSTYPE) ;
2052}
2053
2054/*----------------------------------------------------------------------------*/
2068/*----------------------------------------------------------------------------*/
2070 const char * filename,
2071 cpl_frameset * allframes,
2072 cpl_frameset * inframes,
2073 const cpl_parameterlist * parlist,
2074 cpl_table ** tables,
2075 const cpl_propertylist * qc_list,
2076 cpl_propertylist ** ext_plist,
2077 const char * procatg,
2078 const char * recipe)
2079{
2080 return cr2res_io_save_table(filename, allframes, inframes, parlist, tables,
2081 qc_list, ext_plist, recipe, procatg, CR2RES_POL_SPEC_DRSTYPE) ;
2082}
2083
2085
2086/*----------------------------------------------------------------------------*/
2093/*----------------------------------------------------------------------------*/
2094static int cr2res_io_set_bpm_as_NaNs(
2095 cpl_image * in)
2096{
2097 cpl_size nx, ny, i, j ;
2098 double * pdata ;
2099
2100 /* Check Entries */
2101 if (in == NULL) return -1 ;
2102 if (cpl_image_get_type(in) != CPL_TYPE_DOUBLE) return 0 ;
2103
2104 /* Initialize */
2105 nx = cpl_image_get_size_x(in) ;
2106 ny = cpl_image_get_size_y(in) ;
2107 pdata = cpl_image_get_data_double(in) ;
2108
2109 /* Loop on the pixels */
2110 for (j=0 ; j<ny ; j++) {
2111 for (i=0 ; i<nx ; i++) {
2112 if (cpl_image_is_rejected(in, i+1, j+1)) pdata[i+j*nx] = NAN ;
2113 }
2114 }
2115 return 0 ;
2116}
2117/*----------------------------------------------------------------------------*/
2124/*----------------------------------------------------------------------------*/
2125static int cr2res_io_set_NaNs_as_bpm(
2126 cpl_image * in)
2127{
2128 cpl_size nx, ny, i, j ;
2129 double * pdata ;
2130
2131 /* Check Entries */
2132 if (in == NULL) return -1 ;
2133 if (cpl_image_get_type(in) != CPL_TYPE_DOUBLE) return 0 ;
2134
2135 /* Initialize */
2136 nx = cpl_image_get_size_x(in) ;
2137 ny = cpl_image_get_size_y(in) ;
2138 pdata = cpl_image_get_data_double(in) ;
2139
2140 /* Loop on the pixels */
2141 for (j=0 ; j<ny ; j++) {
2142 for (i=0 ; i<nx ; i++) {
2143 if (isnan(pdata[i+j*nx])) cpl_image_reject(in, i+1, j+1) ;
2144 }
2145 }
2146 return 0 ;
2147}
2148
2149/*----------------------------------------------------------------------------*/
2164/*----------------------------------------------------------------------------*/
2165static int cr2res_io_save_table(
2166 const char * filename,
2167 cpl_frameset * allframes,
2168 cpl_frameset * inframes,
2169 const cpl_parameterlist * parlist,
2170 cpl_table ** tab,
2171 const cpl_propertylist * qc_list,
2172 cpl_propertylist ** ext_plist,
2173 const char * recipe,
2174 const char * procatg,
2175 const char * drstype)
2176{
2177 cpl_propertylist * pro_list ;
2178 cpl_propertylist * ext_head ;
2179 char * wished_extname ;
2180 char * to_remove ;
2181 int i ;
2182
2183 /* Test entries */
2184 if (allframes == NULL || filename == NULL || ext_plist == NULL) return -1 ;
2185
2186 /* Add the PRO keys */
2187 if (qc_list != NULL) pro_list = cpl_propertylist_duplicate(qc_list) ;
2188 else pro_list = cpl_propertylist_new() ;
2189
2190 /* Add PRO Keys */
2191 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procatg) ;
2192 cpl_propertylist_append_string(pro_list, CR2RES_HEADER_DRS_TYPE, drstype) ;
2193
2194 cr2res_qc_dup_mtrlgy_key(inframes, pro_list);
2195
2196 /* Create the first extension header */
2197 if (ext_plist[0]==NULL) {
2198 ext_head = cpl_propertylist_new() ;
2199 } else {
2200 ext_head = cpl_propertylist_duplicate(ext_plist[0]);
2201 cpl_propertylist_erase(ext_head, "EXTNAME");
2202 if (cpl_propertylist_get_property(ext_head, "BUNIT") != NULL)
2203 cpl_propertylist_erase(ext_head, "BUNIT");
2204 }
2205 wished_extname = cr2res_io_create_extname(1, 1) ;
2206 cpl_propertylist_update_string(ext_head, "EXTNAME", wished_extname) ;
2207 cpl_free(wished_extname) ;
2208
2209 cr2res_qc_dup_chip_idx(ext_head);
2210
2211 /* Remove keywords in the primary header matching NODPOS (PIPE-9952) */
2212 to_remove = cpl_sprintf("NODPOS") ;
2213
2214 /* Save the first extension */
2215 if (tab[0] != NULL) {
2216 if (cpl_dfs_save_table(allframes, NULL, parlist, inframes, NULL,
2217 tab[0], ext_head, recipe, pro_list, to_remove,
2218 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2219 cpl_msg_error(__func__, "Cannot save the first extension table") ;
2220 cpl_propertylist_delete(ext_head) ;
2221 cpl_propertylist_delete(pro_list) ;
2222 cpl_free(to_remove) ;
2223 return -1 ;
2224 }
2225 } else {
2226 if (cpl_dfs_save_propertylist(allframes, NULL, parlist,
2227 inframes, NULL, recipe, pro_list, to_remove, PACKAGE "/"
2228 PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2229 cpl_msg_error(__func__, "Cannot save the empty HDU") ;
2230 cpl_propertylist_delete(ext_head) ;
2231 cpl_propertylist_delete(pro_list) ;
2232 cpl_free(to_remove) ;
2233 return -1 ;
2234 }
2235 cpl_propertylist_save(ext_head, filename, CPL_IO_EXTEND) ;
2236 }
2237 cpl_free(to_remove) ;
2238 cpl_propertylist_delete(ext_head) ;
2239 cpl_propertylist_delete(pro_list) ;
2240
2241 /* Save the extensions */
2242 for (i=1 ; i<CR2RES_NB_DETECTORS ; i++) {
2243 /* Create the first extension header */
2244 if (ext_plist[i] == NULL) {
2245 ext_head = cpl_propertylist_new() ;
2246 } else {
2247 ext_head = cpl_propertylist_duplicate(ext_plist[i]);
2248 cpl_propertylist_erase(ext_head, "EXTNAME");
2249 cpl_propertylist_erase(ext_head, "BUNIT");
2250 }
2251 wished_extname = cr2res_io_create_extname(i+1, 1) ;
2252 cpl_propertylist_update_string(ext_head, "EXTNAME", wished_extname) ;
2253 cpl_free(wished_extname) ;
2254 cr2res_qc_dup_chip_idx(ext_head);
2255
2256 if (tab[i] != NULL) {
2257 cpl_table_save(tab[i], NULL, ext_head, filename, CPL_IO_EXTEND) ;
2258 } else {
2259 cpl_propertylist_save(ext_head, filename, CPL_IO_EXTEND) ;
2260 }
2261 cpl_propertylist_delete(ext_head) ;
2262 }
2263 return 0 ;
2264}
2265
2266/*----------------------------------------------------------------------------*/
2282/*----------------------------------------------------------------------------*/
2283static int cr2res_io_save_one_table(
2284 const char * filename,
2285 cpl_frameset * allframes,
2286 cpl_frameset * inframes,
2287 const cpl_parameterlist * parlist,
2288 cpl_table * tab,
2289 const cpl_propertylist * qc_list,
2290 cpl_propertylist * ext_plist,
2291 const char * extname,
2292 const char * recipe,
2293 const char * procatg,
2294 const char * drstype)
2295{
2296 cpl_propertylist * pro_list ;
2297 cpl_propertylist * ext_head ;
2298
2299 /* Test entries */
2300 if (allframes == NULL || filename == NULL || ext_plist == NULL ||
2301 extname == NULL) return -1 ;
2302
2303 /* Add the PRO keys */
2304 if (qc_list != NULL) pro_list = cpl_propertylist_duplicate(qc_list) ;
2305 else pro_list = cpl_propertylist_new() ;
2306
2307 /* Add PRO Keys */
2308 cpl_propertylist_append_string(pro_list, CPL_DFS_PRO_CATG, procatg) ;
2309 cpl_propertylist_append_string(pro_list, CR2RES_HEADER_DRS_TYPE, drstype) ;
2310
2311 /* Create the first extension header */
2312 ext_head = cpl_propertylist_duplicate(ext_plist);
2313 cpl_propertylist_erase(ext_head, "EXTNAME");
2314 cpl_propertylist_update_string(ext_head, "EXTNAME", extname) ;
2315
2316 /* Save the first extension */
2317 if (cpl_dfs_save_table(allframes, NULL, parlist, inframes, NULL,
2318 tab, ext_head, recipe, pro_list, NULL,
2319 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2320 cpl_msg_error(__func__, "Cannot save the first extension table") ;
2321 cpl_propertylist_delete(ext_head) ;
2322 cpl_propertylist_delete(pro_list) ;
2323 return -1 ;
2324 }
2325 cpl_propertylist_delete(ext_head) ;
2326 cpl_propertylist_delete(pro_list) ;
2327
2328 return 0 ;
2329}
2330
2331/*----------------------------------------------------------------------------*/
2347/*----------------------------------------------------------------------------*/
2348static int cr2res_io_save_image(
2349 const char * filename,
2350 cpl_frameset * allframes,
2351 cpl_frameset * inframes,
2352 const cpl_parameterlist * parlist,
2353 hdrl_image ** data,
2354 const cpl_propertylist * qc_list,
2355 cpl_propertylist ** ext_plist,
2356 cpl_type type,
2357 const char * recipe,
2358 const char * procatg,
2359 const char * drstype)
2360{
2361 cpl_propertylist * qclist_loc ;
2362 cpl_image * to_save ;
2363 char * to_remove ;
2364 int det_nr ;
2365
2366 /* Create a local QC list and add the PRO.CATG */
2367 if (qc_list == NULL) {
2368 qclist_loc = cpl_propertylist_new();
2369 } else {
2370 qclist_loc = cpl_propertylist_duplicate(qc_list) ;
2371 }
2372 cpl_propertylist_update_string(qclist_loc, CPL_DFS_PRO_CATG, procatg);
2373 cpl_propertylist_update_string(qclist_loc, CR2RES_HEADER_DRS_TYPE, drstype);
2374
2375 cr2res_qc_dup_mtrlgy_key(inframes, qclist_loc);
2376
2377 /* Remove keywords in the primary header matching NODPOS (PIPE-9952) */
2378 to_remove = cpl_sprintf("NODPOS") ;
2379
2380 /* Create the Primary Data Unit without data */
2381 if (cpl_dfs_save_propertylist(allframes, NULL, parlist, inframes, NULL,
2382 recipe, qclist_loc, to_remove,
2383 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2384 cpl_msg_error(__func__, "Cannot save the empty primary HDU") ;
2385 cpl_propertylist_delete(qclist_loc) ;
2386 cpl_free(to_remove) ;
2387 return -1 ;
2388 }
2389 cpl_free(to_remove) ;
2390
2391 /* Delete PRO LIST */
2392 cpl_propertylist_delete(qclist_loc) ;
2393
2394 /* Save the extensions */
2395 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
2396 char * wished_extname ;
2397 if (ext_plist[det_nr-1] == NULL) {
2398 qclist_loc = cpl_propertylist_new();
2399 } else {
2400 qclist_loc = cpl_propertylist_duplicate(ext_plist[det_nr-1]) ;
2401 cpl_propertylist_erase(qclist_loc, "EXTNAME");
2402 }
2403
2404 /* Save the DATA */
2405 wished_extname = cr2res_io_create_extname(det_nr, 1) ;
2406 cpl_propertylist_prepend_string(qclist_loc, "EXTNAME", wished_extname) ;
2407
2408 cr2res_qc_dup_chip_idx(qclist_loc);
2409
2410 if (data[det_nr-1] == NULL)
2411 to_save = NULL ;
2412 else
2413 to_save = hdrl_image_get_image(data[det_nr-1]);
2414
2415 /* Set BPMs as NaNs */
2416 if (to_save != NULL) cr2res_io_set_bpm_as_NaNs(to_save) ;
2417
2418 /* Save */
2419 cpl_image_save(to_save, filename, type, qclist_loc, CPL_IO_EXTEND) ;
2420 cpl_free(wished_extname) ;
2421
2422 /* Save the NOISE - always but for the BPM */
2423 if (strcmp(drstype, CR2RES_BPM_DRSTYPE)) {
2424 wished_extname = cr2res_io_create_extname(det_nr, 0) ;
2425 cpl_propertylist_update_string(qclist_loc, "EXTNAME",
2426 wished_extname) ;
2427 if (data[det_nr-1] == NULL)
2428 to_save = NULL ;
2429 else
2430 to_save = hdrl_image_get_error(data[det_nr-1]);
2431 cpl_image_save(to_save, filename, type, qclist_loc, CPL_IO_EXTEND) ;
2432 cpl_free(wished_extname) ;
2433 }
2434 cpl_propertylist_delete(qclist_loc) ;
2435 }
2436
2437 return 0 ;
2438}
2439
2440/*----------------------------------------------------------------------------*/
2456/*----------------------------------------------------------------------------*/
2457static int cr2res_io_save_imagelist(
2458 const char * filename,
2459 cpl_frameset * allframes,
2460 cpl_frameset * inframes,
2461 const cpl_parameterlist * parlist,
2462 hdrl_imagelist ** data,
2463 const cpl_propertylist * qc_list,
2464 cpl_propertylist ** ext_plist,
2465 cpl_type type,
2466 const char * recipe,
2467 const char * procatg,
2468 const char * drstype)
2469{
2470 cpl_propertylist * qclist_loc ;
2471 cpl_imagelist * list_data ;
2472 cpl_imagelist * list_noise ;
2473 int det_nr ;
2474 cpl_size i ;
2475
2476 /* Create a local QC list and add the PRO.CATG */
2477 if (qc_list == NULL) {
2478 qclist_loc = cpl_propertylist_new();
2479 } else {
2480 qclist_loc = cpl_propertylist_duplicate(qc_list) ;
2481 }
2482 cpl_propertylist_update_string(qclist_loc, CPL_DFS_PRO_CATG, procatg);
2483 cpl_propertylist_update_string(qclist_loc, CR2RES_HEADER_DRS_TYPE, drstype);
2484
2485 cr2res_qc_dup_mtrlgy_key(inframes, qclist_loc);
2486
2487 /* Create the Primary Data Unit without data */
2488 if (cpl_dfs_save_propertylist(allframes, NULL, parlist, inframes, NULL,
2489 recipe, qclist_loc, NULL,
2490 PACKAGE "/" PACKAGE_VERSION, filename) != CPL_ERROR_NONE) {
2491 cpl_msg_error(__func__, "Cannot save the empty primary HDU") ;
2492 cpl_propertylist_delete(qclist_loc) ;
2493 return -1 ;
2494 }
2495 /* Delete PRO LIST */
2496 cpl_propertylist_delete(qclist_loc) ;
2497
2498 /* Save the extensions */
2499 for (det_nr=1 ; det_nr<=CR2RES_NB_DETECTORS ; det_nr++) {
2500 char * wished_extname ;
2501
2502 /* Extract separately data / noise */
2503 if (data[det_nr-1] == NULL) {
2504 list_data = list_noise = NULL ;
2505 } else {
2506 list_data = cr2res_hdrl_to_cpl_list(data[det_nr-1], 1) ;
2507 list_noise = cr2res_hdrl_to_cpl_list(data[det_nr-1], 0) ;
2508 }
2509
2510 /* Set BPMs as NaNs */
2511 if (list_data != NULL)
2512 for (i=0 ; i<cpl_imagelist_get_size(list_data) ; i++)
2513 cr2res_io_set_bpm_as_NaNs(cpl_imagelist_get(list_data, i)) ;
2514
2515 /* Prepare header */
2516 if (ext_plist[det_nr-1] == NULL) {
2517 qclist_loc = cpl_propertylist_new();
2518 } else {
2519 qclist_loc = cpl_propertylist_duplicate(ext_plist[det_nr-1]) ;
2520 cpl_propertylist_erase(qclist_loc, "EXTNAME");
2521 }
2522
2523 /* Save the DATA */
2524 wished_extname = cr2res_io_create_extname(det_nr, 1) ;
2525 cpl_propertylist_prepend_string(qclist_loc, "EXTNAME", wished_extname) ;
2526 cr2res_qc_dup_chip_idx(qclist_loc);
2527 if (list_data == NULL) {
2528 cpl_propertylist_save(qclist_loc, filename, CPL_IO_EXTEND) ;
2529 } else {
2530 cpl_imagelist_save(list_data, filename, type, qclist_loc,
2531 CPL_IO_EXTEND) ;
2532 cpl_imagelist_delete(list_data) ;
2533 }
2534 cpl_free(wished_extname) ;
2535
2536 /* Save the NOISE */
2537 wished_extname = cr2res_io_create_extname(det_nr, 0) ;
2538 cpl_propertylist_update_string(qclist_loc, "EXTNAME", wished_extname) ;
2539 if (list_noise == NULL) {
2540 cpl_propertylist_save(qclist_loc, filename, CPL_IO_EXTEND) ;
2541 } else {
2542 cpl_imagelist_save(list_noise, filename, type, qclist_loc,
2543 CPL_IO_EXTEND) ;
2544 cpl_imagelist_delete(list_noise) ;
2545 }
2546 cpl_propertylist_delete(qclist_loc) ;
2547 cpl_free(wished_extname) ;
2548 }
2549
2550 return 0 ;
2551}
2552
2553/*----------------------------------------------------------------------------*/
2560/*----------------------------------------------------------------------------*/
2561static cpl_imagelist * cr2res_hdrl_to_cpl_list(
2562 const hdrl_imagelist * in,
2563 int data)
2564{
2565 cpl_imagelist * out ;
2566 const cpl_image * cur_cpl_im ;
2567 cpl_size i ;
2568
2569 /* Check Entries */
2570 if (in == NULL) return NULL ;
2571 if (data != 0 && data != 1) return NULL ;
2572
2573 /* Create output list */
2574 out = cpl_imagelist_new() ;
2575 for (i=0 ; i< hdrl_imagelist_get_size(in) ; i++) {
2576 const hdrl_image * cur_im ;
2577 cur_im = hdrl_imagelist_get(in, i) ;
2578 if (data == 1) cur_cpl_im = hdrl_image_get_image_const(cur_im) ;
2579 if (data == 0) cur_cpl_im = hdrl_image_get_error_const(cur_im) ;
2580
2581 cpl_imagelist_set(out, cpl_image_duplicate(cur_cpl_im),
2582 cpl_imagelist_get_size(out)) ;
2583 }
2584
2585 /* Check output */
2586 if (hdrl_imagelist_get_size(in) != cpl_imagelist_get_size(out)) {
2587 cpl_imagelist_delete(out) ;
2588 return NULL ;
2589 }
2590 /* Return */
2591 return out ;
2592}
2593
2594static int cr2res_io_check_drs_type(
2595 const char * filename,
2596 const char * expected_drstype)
2597{
2598 cpl_propertylist * plist ;
2599 const char * drstype ;
2600
2601 /* Check entries */
2602 if (filename == NULL) return -1 ;
2603
2604 /* Check the DRS.TYPE */
2605 plist = cpl_propertylist_load(filename, 0) ;
2606 if (plist == NULL) return -1;
2607 drstype = cr2res_pfits_get_drstype(plist) ;
2608 if (cpl_error_get_code()) {
2609
2610 const char * protype ;
2611 cpl_error_reset() ;
2612
2613 /* TRANSITION PHASE starting 09.02.2023 : See ticket PIPE-10551 */
2614 /*
2615 DRS.TYPE was in PRO.TYPE so far.
2616 Adding here check of PRO.TYPE to support old data
2617 Looking in PRO.TYPE id DRS.TYPE is missing
2618 */
2619 protype = cr2res_pfits_get_protype(plist) ;
2620 if (cpl_error_get_code()) {
2621 cpl_error_reset() ;
2622 cpl_propertylist_delete(plist) ;
2623 return -1 ;
2624 }
2625 if (strcmp(protype, expected_drstype)) {
2626 cpl_msg_error(__func__, "Unexpected PRO.TYPE (old data): %s != %s",
2627 protype, expected_drstype) ;
2628 cpl_propertylist_delete(plist) ;
2629 return 0 ;
2630 }
2631 } else {
2632 if (strcmp(drstype, expected_drstype)) {
2633 cpl_msg_error(__func__, "Unexpected DRS.TYPE: %s != %s",
2634 drstype, expected_drstype) ;
2635 cpl_propertylist_delete(plist) ;
2636 return 0 ;
2637 }
2638 }
2639 cpl_propertylist_delete(plist) ;
2640 return 1 ;
2641}
2642
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:1465
cpl_table * cr2res_io_load_EXTRACT_1D(const char *filename, int detector)
Load a table from a EXTRACT_1D.
Definition cr2res_io.c:1326
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:1853
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
cpl_vector * cr2res_io_read_ndsamples(const cpl_frameset *in)
Get the NDSAMPLES from a frame set.
Definition cr2res_io.c:492
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:1419
char * cr2res_io_create_extname(int detector, int data)
Create Extname.
Definition cr2res_io.c:648
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:1946
int cr2res_io_convert_order_idx_to_idxp(int order_idx)
Convert the order_idx to the order_idxp.
Definition cr2res_io.c:613
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:1700
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:2008
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:557
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:2039
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:1762
cpl_table * cr2res_load_table(const char *in, int det_nr, int pmin, int pmax)
Load the table accordingly.
Definition cr2res_io.c:892
hdrl_image * cr2res_io_load_SLIT_MODEL(const char *filename, int detector)
Load an hdrl image from a SLIT MODEL.
Definition cr2res_io.c:1199
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:838
hdrl_imagelist * cr2res_io_load_DETLIN_COEFFS(const char *filename, int detector)
Load the detlin coefficients.
Definition cr2res_io.c:1064
hdrl_image * cr2res_io_load_MASTER_DARK(const char *filename, int detector)
Load an image from a MASTER_DARK.
Definition cr2res_io.c:1020
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:1669
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:1521
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:1553
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:1792
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:674
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:1915
cpl_table * cr2res_io_load_SLIT_FUNC(const char *filename, int detector)
Load a table from a SLIT_FUNC.
Definition cr2res_io.c:1169
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:1977
hdrl_image * cr2res_io_load_image(const char *in, int detector)
Load an hdrl image from a image file.
Definition cr2res_io.c:734
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:1638
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:1822
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:1585
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:1884
cr2res_decker * cr2res_io_read_decker_positions(const cpl_frameset *in)
Get the decker positions from a frame set.
Definition cr2res_io.c:521
cpl_bivector * cr2res_io_load_EMISSION_LINES(const char *filename)
Load an EMISSION_LINES bivector.
Definition cr2res_io.c:933
cpl_table * cr2res_io_load_TRACE_WAVE(const char *filename, int detector)
Load a table from a TRACE_WAVE.
Definition cr2res_io.c:1139
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:1731
cpl_image * cr2res_io_load_BPM(const char *filename, int detector, int data)
Load an image from a BPM.
Definition cr2res_io.c:987
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:784
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:2069
hdrl_image * cr2res_io_load_MASTER_FLAT(const char *filename, int detector)
Load an hdrl image from a MASTER_FLAT.
Definition cr2res_io.c:1096
cr2res_decker cr2res_pfits_get_decker_position(const cpl_propertylist *plist)
find out the decker position
const char * cr2res_pfits_get_wlen_id(const cpl_propertylist *plist)
find out the Setting
const char * cr2res_pfits_get_protype(const cpl_propertylist *plist)
find out the PRO.TYPE
double cr2res_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
int cr2res_pfits_get_ndsamples(const cpl_propertylist *plist)
find out the NDSAMPLES value (up-the-ramp reads per DIT)
int cr2res_pfits_get_ndit(const cpl_propertylist *plist)
find out the NDIT value
const char * cr2res_pfits_get_drstype(const cpl_propertylist *plist)
find out the DRS.TYPE
double cr2res_mjd_obs_now(void)
Get the current MJD-OBS.
int cr2res_format_setting(char *setting_id)
Format the setting.
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.