X-shooter Pipeline Reference Manual 3.8.15
xsh_dfs.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library 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, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2013-04-17 09:07:36 $
23 * $Revision: 1.224 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39
40/*----------------------------------------------------------------------------
41 Includes
42 ----------------------------------------------------------------------------*/
43
44#include <xsh_baryvel.h>
45#include <xsh_dfs.h>
46#include <xsh_pfits.h>
47#include <xsh_pfits_qc.h>
48#include <xsh_data_pre.h>
49#include <xsh_data_pre_3d.h>
50#include <xsh_error.h>
51#include <xsh_msg.h>
52#include <cpl.h>
53#include <stdio.h>
54#include <string.h>
55#include <strings.h>
56#include <time.h>
57#include <math.h>
58#include <assert.h>
59#include <ctype.h>
60#include <errno.h>
61#include <xsh_paf_save.h>
62#include <xsh_utils_table.h>
63#include <xsh_utils_image.h>
64#include <xsh_utils.h>
65#include <xsh_data_spectrum.h>
66
67#include <irplib_sdp_spectrum.h>
68
69
70/*-----------------------------------------------------------------------------
71 Utility functions
72 -----------------------------------------------------------------------------*/
73
74 static void replace_spaces_with_underscores(char *str)
75 {
76
77 for (int i = 0; str[i] != '\0'; i++) {
78 if (str[i] == ' ') {
79 str[i] = '_'; // Replace space with an underscore
80 }
81 }
82 }
83
84/*----------------------------------------------------------------------------
85 Function prototypes
86 ----------------------------------------------------------------------------*/
87
88static cpl_frame *xsh_find_frame (cpl_frameset * frames,
89 const char *tags[]);
90
91
94cpl_error_code
95xsh_dfs_fix_key_start_end(cpl_frameset* set, cpl_propertylist* head)
96{
97
98 cpl_frameset* raws=NULL;
99 raws=cpl_frameset_new();
101 check( xsh_pfits_combine_headers(head,raws));
102
103 xsh_free_frameset(&raws);
104 cleanup:
105 return cpl_error_get_code();
106
107}
108
114int
115xsh_parameter_get_default_flag(const cpl_parameter* p) {
116 int flag_gasgano=0;
117 int flag_norm=0;
118 int flag=0;
119 cpl_type type =0;
120
121 flag_norm = (cpl_parameter_get_default_flag(p) == 0) ? 1 : 0;
122 type=cpl_parameter_get_type(p);
123
124 switch(type) {
125 case CPL_TYPE_BOOL:
126 flag_gasgano = (cpl_parameter_get_default_bool(p) ==
127 cpl_parameter_get_bool(p)) ? 1:0;
128 break;
129 case CPL_TYPE_INT:
130 flag_gasgano = (cpl_parameter_get_default_int(p) ==
131 cpl_parameter_get_int(p)) ? 1:0;
132 break;
133 case CPL_TYPE_DOUBLE:
134 flag_gasgano = (cpl_parameter_get_default_double(p) ==
135 cpl_parameter_get_double(p)) ? 1:0;
136 break;
137 case CPL_TYPE_STRING:
138 flag_gasgano = (cpl_parameter_get_default_string(p) ==
139 cpl_parameter_get_string(p)) ? 1:0;
140 break;
141
142 default:
143 xsh_msg_error("type not supported");
144 }
145
146 flag = (flag_gasgano && flag_norm) ? 0 : 1;
147
148 return flag;
149}
150
151static cpl_error_code
152xsh_clean_header(cpl_propertylist* header)
153{
154 // PIPE-9899 - force any present RADECSYS to go away
155 cpl_propertylist_erase_regexp(header,
156 "^[:blank:]*(COMMENT|CHECKSUM|DATASUM|RADECSYS)[:blank:]*$",
157 0);
158
159 return cpl_error_get_code();
160}
161
168cpl_error_code
169xsh_validate_model_cfg(cpl_frame* mod, cpl_frameset* set) {
170
171 const char* mod_name=NULL;
172 const char* raw_name=NULL;
173 cpl_frame* raw_frame=0;
174 cpl_propertylist* mod_header=NULL;
175 //cpl_propertylist* raw_header=NULL;
176 double mod_mjd=0;
177 double raw_mjd=0;
178
179 raw_frame=cpl_frameset_get_frame(set,0);
180
181 mod_name=cpl_frame_get_filename(mod);
182 mod_header=cpl_propertylist_load(mod_name,0);
183 mod_mjd=xsh_pfits_get_mjdobs(mod_header);
184
185 raw_name=cpl_frame_get_filename(raw_frame);
186 //raw_header=cpl_propertylist_load(mod_name,0);
187 check(raw_mjd=xsh_pfits_get_mjdobs(mod_header));
188
189 if(raw_mjd < mod_mjd) {
190
191 xsh_msg_warning("Raw frame %s has MJD-OBS prior than model cfg frame %s",
192 raw_name,mod_name);
193 xsh_msg_warning("The user should use a model cfg frame corresponding to a more recent period");
194 }
195
196 cleanup:
197 return cpl_error_get_code();
198
199 }
200
206 cpl_frameset*
207 xsh_frameset_ext_table_frames(cpl_frameset* set) {
208 cpl_frameset* result=NULL;
209 int nset=0;
210 int i=0;
211 cpl_frame* frm=NULL;
212 cpl_propertylist* plist=NULL;
213 const char* name=NULL;
214 int naxis=0;
215
216 check(nset=cpl_frameset_get_size(set));
217 result=cpl_frameset_new();
218 for(i=0;i<nset;i++) {
219 check(frm=cpl_frameset_get_frame(set,i));
220 check(name=cpl_frame_get_filename(frm));
221 check(plist=cpl_propertylist_load(name,0));
222 check(naxis=xsh_pfits_get_naxis(plist));
223 if(naxis==0) {
224 check(cpl_frameset_insert(result,cpl_frame_duplicate(frm)));
225 }
226 xsh_free_propertylist(&plist);
227 }
228
229 cleanup:
230
231 return result;
232 }
233
234/*----------------------------------------------------------------------------*/
244/*----------------------------------------------------------------------------*/
245int xsh_dfs_files_dont_exist(cpl_frameset *frameset)
246{
247 const char *func = "dfs_files_dont_exist";
248 cpl_frame *frame;
249
250
251 if (frameset == NULL) {
252 cpl_error_set(func, CPL_ERROR_NULL_INPUT);
253 return 1;
254 }
255
256 if (cpl_frameset_is_empty(frameset)) {
257 return 0;
258 }
259
260 cpl_frameset_iterator* it = cpl_frameset_iterator_new(frameset);
261 frame = cpl_frameset_iterator_get(it);
262 while (frame) {
263 if (access(cpl_frame_get_filename(frame), F_OK)) {
264 cpl_msg_error(func, "File %s (%s) was not found",
265 cpl_frame_get_filename(frame),
266 cpl_frame_get_tag(frame));
267 cpl_error_set(func, CPL_ERROR_FILE_NOT_FOUND);
268 }
269 cpl_frameset_iterator_advance(it, 1);
270 frame = cpl_frameset_iterator_get(it);
271 }
272 cpl_frameset_iterator_delete(it);
273
274 if (cpl_error_get_code())
275 return 1;
276
277 return 0;
278}
279
285cpl_frameset*
287 cpl_frameset* result=NULL;
288 int nset=0;
289 int i=0;
290 cpl_frame* frm=NULL;
291 cpl_propertylist* plist=NULL;
292 const char* name=NULL;
293 int naxis=0;
294
295 check(nset=cpl_frameset_get_size(set));
296 result=cpl_frameset_new();
297 for(i=0;i<nset;i++) {
298 check(frm=cpl_frameset_get_frame(set,i));
299 check(name=cpl_frame_get_filename(frm));
300 check(plist=cpl_propertylist_load(name,0));
301 check(naxis=xsh_pfits_get_naxis(plist));
302 if(naxis==2) {
303 check(cpl_frameset_insert(result,cpl_frame_duplicate(frm)));
304 }
305 xsh_free_propertylist(&plist);
306 }
307
308 cleanup:
309
310 return result;
311}
312
318cpl_frameset*
319xsh_frameset_drl_frames(cpl_frameset* set) {
320 cpl_frameset* result=NULL;
321 int nset=0;
322 int i=0;
323 cpl_frame* frm=NULL;
324 cpl_propertylist* plist=NULL;
325 const char* name=NULL;
326 int naxis=0;
327
328 check(nset=cpl_frameset_get_size(set));
329 result=cpl_frameset_new();
330 for(i=0;i<nset;i++) {
331 check(frm=cpl_frameset_get_frame(set,i));
332 check(name=cpl_frame_get_filename(frm));
333 check(plist=cpl_propertylist_load(name,0));
334 check(naxis=xsh_pfits_get_naxis(plist));
335 if(naxis==0) {
336 check(cpl_frameset_insert(result,cpl_frame_duplicate(frm)));
337 }
338 xsh_free_propertylist(&plist);
339 }
340
341 cleanup:
342
343 return result;
344}
351cpl_error_code
354{
355 int nraws=0;
356 cpl_frameset* obj=NULL;
357 cpl_frameset* sky=NULL;
358 const char * obj_tag=NULL;
359 const char * sky_tag=NULL;
360 int nobj=0;
361 int nsky=0;
362 int i=0;
363 int ndif=0;
364 cpl_frame* frm=NULL;
365
366 check(nraws=cpl_frameset_get_size(*raws));
367
368 assure(nraws>=2, CPL_ERROR_ILLEGAL_INPUT, "Too few input frames. At least one OBJ and a SKY frame are required" );
369
372 } else {
374 }
375
376 check(obj=xsh_frameset_extract(*raws,obj_tag));
377 nobj=cpl_frameset_get_size(obj);
378 if(nobj==0) {
381 } else {
383 }
384 xsh_free_frameset(&obj);
385 check(obj=xsh_frameset_extract(*raws,obj_tag));
386 nobj=cpl_frameset_get_size(obj);
387 }
388
391 } else {
393 }
394 xsh_free_frameset(&sky);
395 check(sky=xsh_frameset_extract(*raws,sky_tag));
396
397
398
399 nsky=cpl_frameset_get_size(sky);
400
401 assure(nobj>0, CPL_ERROR_ILLEGAL_INPUT,
402 "Too few input obj frames. At least a OBJ frame is required" );
403
404 assure(nsky>0, CPL_ERROR_ILLEGAL_INPUT,
405 "Too few input sky frames. At least a SKY frame is required" );
406
407 if(nobj>nsky) {
408 xsh_msg("case nobj> nsky");
409 ndif=nobj-nsky;
410 for(i=0;i<ndif;i++) {
411 check(frm=cpl_frameset_get_frame(obj,nobj-i-1));
412 check(cpl_frameset_erase_frame(obj,frm));
413 }
414 } else if(nsky>nobj) {
415 xsh_msg("case nsky> nobj");
416 ndif=nsky-nobj;
417 for(i=0;i<ndif;i++) {
418 check(frm=cpl_frameset_get_frame(sky,nsky-i-1));
419 check(cpl_frameset_erase_frame(sky,frm));
420 }
421 } else {
422 ndif=nsky-nobj;
423 xsh_free_frameset(&obj);
424 xsh_free_frameset(&sky);
425 return CPL_ERROR_NONE;
426
427 }
428 xsh_free_frameset(raws);
429 check(*raws=cpl_frameset_duplicate(obj));
430 check(xsh_frameset_merge(*raws,sky));
431
432 cleanup:
433 xsh_free_frameset(&obj);
434 xsh_free_frameset(&sky);
435
436 return cpl_error_get_code();
437
438}
439
440
447static double
448xsh_get_offsety(cpl_propertylist* plist)
449{
450
451 double offsety=0;
452 double ra_off=0;
453 double dec_off=0;
454 double posang=0;
455
456 if(cpl_propertylist_has(plist,XSH_NOD_CUMULATIVE_OFFSETY)) {
457 offsety=xsh_pfits_get_cumoffsety(plist);
458 } else {
459 ra_off=xsh_pfits_get_ra_cumoffset(plist);
460 dec_off=xsh_pfits_get_dec_cumoffset(plist);
461 posang = xsh_pfits_get_posang(plist);
462 posang = posang/180.0* M_PI;
463/*
464 shift_pix = xsh_round_double((cos(-posang)*dec_off+
465 sin(-posang)*ra_off)/bin_space);
466*/
467 offsety=(cos(-posang)*dec_off+
468 sin(-posang)*ra_off);
469 xsh_msg("ra_off=%f,dec_off=%f,offsety=%f,posang=%f",
470 ra_off,dec_off,offsety,posang);
471 }
472
473 return offsety;
474
475}
476
484cpl_frameset*
485xsh_frameset_extract_offsety_matches(cpl_frameset* raws,const double offset)
486{
487 cpl_frame* frm=NULL;
488 cpl_frameset* result=NULL;
489 cpl_propertylist* plist=NULL;
490 double offsety=0;
491 const char* name=NULL;
492 int nraw=0;
493 int i=0;
494
495 XSH_ASSURE_NOT_NULL_MSG(raws,"null input frameset");
496 nraw=cpl_frameset_get_size(raws);
497 result=cpl_frameset_new();
498 for(i=0;i<nraw;i++) {
499 frm=cpl_frameset_get_frame(raws,i);
500 name=cpl_frame_get_filename(frm);
501 plist=cpl_propertylist_load(name,0);
502 offsety=xsh_get_offsety(plist);
503
504 if(fabs(offsety-offset)<1.E-8) {
505 cpl_frameset_insert(result,cpl_frame_duplicate(frm));
506 }
507 xsh_free_propertylist(&plist);
508 }
509
510 cleanup:
511 xsh_free_propertylist(&plist);
512
513 return result;
514
515}
516
517
518
526cpl_frameset*
527xsh_frameset_extract_offsety_mismatches(cpl_frameset* raws,const double offset)
528{
529 cpl_frame* frm=NULL;
530 cpl_frameset* result=NULL;
531 cpl_propertylist* plist=NULL;
532 double offsety=0;
533 const char* name=NULL;
534 int nraw=0;
535 int i=0;
536
537 double ra_off=0;
538 double dec_off=0;
539 double posang=0;
540
541 XSH_ASSURE_NOT_NULL_MSG(raws,"null input frameset");
542 nraw=cpl_frameset_get_size(raws);
543 result=cpl_frameset_new();
544 for(i=0;i<nraw;i++) {
545 frm=cpl_frameset_get_frame(raws,i);
546 name=cpl_frame_get_filename(frm);
547 plist=cpl_propertylist_load(name,0);
548 if(cpl_propertylist_has(plist,XSH_NOD_CUMULATIVE_OFFSETY)) {
549 offsety=xsh_pfits_get_cumoffsety(plist);
550 } else {
551 ra_off=xsh_pfits_get_ra_cumoffset(plist);
552 dec_off=xsh_pfits_get_dec_cumoffset(plist);
553 posang = xsh_pfits_get_posang(plist);
554 posang = posang/180.0* M_PI;
555/*
556 shift_pix = xsh_round_double((cos(-posang)*dec_off+
557 sin(-posang)*ra_off)/bin_space);
558*/
559 offsety=(cos(-posang)*dec_off+
560 sin(-posang)*ra_off);
561 xsh_msg("ra_off=%f,dec_off=%f,offsety=%f,posang=%f",
562 ra_off,dec_off,offsety,posang);
563 }
564 if(offsety!=offset) {
565 cpl_frameset_insert(result,cpl_frame_duplicate(frm));
566 }
567 xsh_free_propertylist(&plist);
568 }
569
570 cleanup:
571 xsh_free_propertylist(&plist);
572
573 return result;
574
575}
576
583cpl_frameset*
584xsh_extract_nod_pairs(cpl_frameset* raws,
585 cpl_vector* offset)
586{
587 int nraws=0;
588 cpl_frameset* set_a=NULL;
589 cpl_frameset* set_b=NULL;
590 cpl_frameset* result=NULL;
591 cpl_propertylist* plist=NULL;
592 double offsety_a=0;
593
594
595 int na=0;
596 int nb=0;
597 int i=0;
598 int j=0;
599 int k=0;
600 cpl_frame* frm_a=NULL;
601 cpl_frame* frm_b=NULL;
602 cpl_frame* frm_tmp=NULL;
603 double mjd_obs_a=0;
604 double mjd_obs_b=0;
605 double mjd_obs_tmp=0;
606 const char* name=NULL;
607 int noff=0;
608 double mjd_obs_ref=-999999.;
609 XSH_ASSURE_NOT_NULL_MSG(raws,"null input frameset");
610
611 check(nraws=cpl_frameset_get_size(raws));
612 xsh_msg("nraws=%d",nraws);
613 assure(nraws>=2, CPL_ERROR_ILLEGAL_INPUT,
614 "Too few input frames. At least two NOD positions are required" );
615
616 noff=cpl_vector_get_size(offset);
617 result=cpl_frameset_new();
618 for(i=0;i<noff;i++) {
619 offsety_a=cpl_vector_get(offset,i);
620 xsh_msg("offsety_a=%f",offsety_a);
621 xsh_free_frameset(&set_a);
622 xsh_free_frameset(&set_b);
623 check(set_a=xsh_frameset_extract_offsety_matches(raws,offsety_a));
624 check(set_b=xsh_frameset_extract_offsety_mismatches(raws,offsety_a));
625
626 na=cpl_frameset_get_size(set_a);
627 nb=cpl_frameset_get_size(set_b);
628 xsh_msg("na=%d nb=%d",na,nb);
629 for(k=0;k<na;k++) {
630
631 check(frm_a=cpl_frameset_get_frame(set_a,k));
632 check(name=cpl_frame_get_filename(frm_a));
633 check(plist=cpl_propertylist_load(name,0));
634 check(mjd_obs_a=xsh_pfits_get_mjdobs(plist));
635 xsh_free_propertylist(&plist);
636
637
638 /* get the closest in mjd-obs */
639 mjd_obs_b=mjd_obs_ref;
640 for(j=0;j<nb;j++) {
641
642 check(frm_tmp=cpl_frameset_get_frame(set_b,j));
643 check(name=cpl_frame_get_filename(frm_tmp));
644 check(plist=cpl_propertylist_load(name,0));
645 check(mjd_obs_tmp=xsh_pfits_get_mjdobs(plist));
646
647 if(fabs(mjd_obs_tmp-mjd_obs_a) < fabs(mjd_obs_b-mjd_obs_a)) {
648 mjd_obs_b=mjd_obs_tmp;
649 frm_b=frm_tmp;
650 }
651 }
652
653
654 cpl_frameset_erase_frame(raws,frm_a);
655
656/*
657 xsh_msg("before");
658 xsh_frameset_dump(raws);
659 xsh_msg("after");
660 xsh_frameset_dump(raws);
661 */
662 cpl_frameset_erase_frame(raws,frm_b);
663 cpl_frameset_insert(result,cpl_frame_duplicate(frm_a));
664 cpl_frameset_insert(result,cpl_frame_duplicate(frm_b));
665 xsh_free_frameset(&set_a);
666 xsh_free_frameset(&set_b);
667 check(set_a=xsh_frameset_extract_offsety_matches(raws,offsety_a));
668 check(set_b=xsh_frameset_extract_offsety_mismatches(raws,offsety_a));
669 na=cpl_frameset_get_size(set_a);
670 nb=cpl_frameset_get_size(set_b);
671
672 xsh_msg("check again na=%d nb=%d",na,nb);
673
674 }
675 xsh_msg("ok0 i=%d",i);
676
677 }
678
679 cleanup:
680 xsh_free_frameset(&set_a);
681 xsh_free_frameset(&set_b);
682 xsh_free_propertylist(&plist);
683
684
685 return result;
686
687}
688
689
690
691
698cpl_frameset*
700{
701 int nraws=0;
702 cpl_frameset* obj=NULL;
703 cpl_frameset* sky=NULL;
704 cpl_frameset* result=NULL;
705 cpl_propertylist* plist=NULL;
706
707 const char * obj_tag=NULL;
708 const char * sky_tag=NULL;
709 int nobj=0;
710 int nsky=0;
711 int i=0;
712 int j=0;
713 cpl_frame* frm_obj=NULL;
714 cpl_frame* frm_sky=NULL;
715 cpl_frame* frm_tmp=NULL;
716 double mjdobs_obj=0;
717 double mjdobs_sky=0;
718 double mjdobs_tmp=0;
719 const char* name=NULL;
720
721 check(nraws=cpl_frameset_get_size(raws));
722 xsh_msg("nraws=%d",nraws);
723 assure(nraws>=2, CPL_ERROR_ILLEGAL_INPUT,
724 "Too few input frames. At least one OBJ and a SKY frame are required");
728 } else {
731 }
732
733 check(obj=xsh_frameset_extract(raws,obj_tag));
734 check(sky=xsh_frameset_extract(raws,sky_tag));
735
736 nobj=cpl_frameset_get_size(obj);
737 nsky=cpl_frameset_get_size(sky);
738
739 assure(nobj>0, CPL_ERROR_ILLEGAL_INPUT,
740 "Too few input obj frames. At least a OBJ frame is required" );
741
742 assure(nsky>0, CPL_ERROR_ILLEGAL_INPUT,
743 "Too few input sky frames. At least a SKY frame is required" );
744
745 result=cpl_frameset_new();
746 for(i=0;i<nobj;i++) {
747 check(frm_obj=cpl_frameset_get_frame(obj,i));
748 check(name=cpl_frame_get_filename(frm_obj));
749 check(plist=cpl_propertylist_load(name,0));
750 check(mjdobs_obj=xsh_pfits_get_mjdobs(plist));
751 xsh_free_propertylist(&plist);
752
753
754 check(frm_sky=cpl_frameset_get_frame(sky,0));
755 check(name=cpl_frame_get_filename(frm_sky));
756 check(plist=cpl_propertylist_load(name,0));
757 check(mjdobs_sky=xsh_pfits_get_mjdobs(plist));
758 xsh_free_propertylist(&plist);
759 cpl_frameset_insert(result,cpl_frame_duplicate(frm_obj));
760
761 for(j=1;j<nsky;j++) {
762 check(frm_tmp=cpl_frameset_get_frame(sky,j));
763 check(name=cpl_frame_get_filename(frm_tmp));
764 check(plist=cpl_propertylist_load(name,0));
765 check(mjdobs_tmp=xsh_pfits_get_mjdobs(plist));
766
767 if(fabs(mjdobs_tmp-mjdobs_obj) < fabs(mjdobs_sky-mjdobs_obj)) {
768 mjdobs_sky=mjdobs_tmp;
769 frm_sky=frm_tmp;
770 }
771 }
772 cpl_frameset_insert(result,cpl_frame_duplicate(frm_sky));
773 }
774
775
776 cleanup:
777 xsh_free_frameset(&obj);
778 xsh_free_frameset(&sky);
779 xsh_free_propertylist(&plist);
780
781 return result;
782
783}
784
791cpl_frame*
793{
794 int nraws=0;
795 cpl_frame* rejected=NULL;
796 cpl_frame* frame1=NULL;
797 cpl_frame* frame2=NULL;
798 cpl_frame* frameN=NULL;
799 cpl_propertylist* plist1=NULL;
800 cpl_propertylist* plist2=NULL;
801 cpl_propertylist* plistN=NULL;
802 const char* name1=NULL;
803 const char* name2=NULL;
804 const char* nameN=NULL;
805 double yshift1=0;
806 double yshift2=0;
807 double yshiftN=0;
808
809 //rejected=cpl_frameset_new();
810 check(nraws=cpl_frameset_get_size(raws));
811 if ( (nraws % 2) != 0 ) {
812 xsh_msg_warning("odd number of frames: nraws=%d",nraws);
813 check(frame1=cpl_frameset_get_frame(raws,0));
814 check(frame2=cpl_frameset_get_frame(raws,1));
815 check(frameN=cpl_frameset_get_frame(raws,nraws-1));
816 check(name1=cpl_frame_get_filename(frame1));
817 check(name2=cpl_frame_get_filename(frame2));
818 check(nameN=cpl_frame_get_filename(frameN));
819 check(plist1=cpl_propertylist_load(name1,0));
820 check(plist2=cpl_propertylist_load(name2,0));
821 check(plistN=cpl_propertylist_load(nameN,0));
822 check(yshift1=xsh_get_offsety(plist1));
823 check(yshift2=xsh_get_offsety(plist2));
824 check(yshiftN=xsh_get_offsety(plistN));
825 /*
826 xsh_msg("yshift1=%g",yshift1);
827 xsh_msg("yshift2=%g",yshift2);
828 xsh_msg("yshiftN=%g",yshiftN);
829 */
830 if(yshift1 == yshiftN) {
831 xsh_msg_warning("yshift(Frame1)==yshift(FrameN)");
832 if ( fabs(yshift1-yshift2) <= DBL_MIN ) {
833 xsh_msg_warning("yshift(Frame1)==yshift(Frame2)");
834 xsh_msg_warning("Remove last from list. Frame %s",
835 cpl_frame_get_filename(frameN));
836 cpl_frameset_erase_frame(raws,frameN);
837 rejected=frameN;
838 } else {
839 xsh_msg_warning("yshift(Frame1)!=yshift(Frame2)");
840 xsh_msg_warning("Remove first from list. Frame %s",
841 cpl_frame_get_filename(frame1));
842 cpl_frameset_erase_frame(raws,frame1);
843 rejected=frame1;
844 }
845 } else {
846 xsh_msg_warning("yshift(Frame1)!=yshift(FrameN)");
847 if (fabs(yshift1 - yshift2) <= DBL_MIN) {
848 xsh_msg_warning("yshift(Frame1)==yshift(Frame2)");
849 xsh_msg_warning("Remove first from list. Frame %s",
850 cpl_frame_get_filename(frame1));
851 cpl_frameset_erase_frame(raws,frame1);
852 rejected=frame1;
853 } else {
854 xsh_msg_warning("yshift(Frame1)!=yshift(Frame2)");
855 xsh_msg_warning("Remove last from list. Frame %s",
856 cpl_frame_get_filename(frameN));
857 cpl_frameset_erase_frame(raws,frameN);
858 rejected=frameN;
859 }
860 }
861 }
862
863 cleanup:
864 xsh_free_propertylist(&plist1);
865 xsh_free_propertylist(&plist2);
866 xsh_free_propertylist(&plistN);
867
868 return rejected;
869
870}
876int
877xsh_print_rec_status(const int val) {
878 if(cpl_error_get_code() != CPL_ERROR_NONE) {
879 xsh_msg_error("Recipe status at %d",val);
880 xsh_msg_error("%s",(const char*) cpl_error_get_message());
881 xsh_msg_error("%s",(const char*) cpl_error_get_where());
882 return -1;
883 }
884 return 0;
885}
886
887
899void
900xsh_frame_config(const char* fname,
901 const char* tag,
902 cpl_frame_type type,
903 cpl_frame_group group,
904 cpl_frame_level level,
905 cpl_frame** frame)
906{
907
908 check(cpl_frame_set_filename(*frame,fname));
909 check(cpl_frame_set_tag(*frame,tag));
910 check(cpl_frame_set_type(*frame,type));
911 check(cpl_frame_set_group(*frame,group));
912 check(cpl_frame_set_level(*frame,level));
913
914 cleanup:
915
916 return;
917
918}
929cpl_frame*
930xsh_frame_product(const char* fname, const char* tag, cpl_frame_type type,
931 cpl_frame_group group,cpl_frame_level level)
932{
933 cpl_frame* frame=NULL;
934 check(frame=cpl_frame_new());
935
936 check(xsh_frame_config(fname,tag,type,group,level,&frame));
937
938 return frame;
939
940 cleanup:
941 xsh_free_frame(&frame);
942
943 return NULL;
944}
945/*-------------------------------------------------------------------------*/
956/*--------------------------------------------------------------------------*/
957
958
959int xsh_file_exists(const char * filename)
960{
961 int exists=0;
962 FILE* fo=NULL;
963 if ((fo=fopen(filename,"r"))==NULL) {
964 exists=0;
965 } else {
966 exists=1;
967 }
968 if(fo != NULL ) {
969 fclose(fo);
970 }
971 return exists;
972}
973
974/*---------------------------------------------------------------------------*/
981/*---------------------------------------------------------------------------*/
982cpl_frameset *
983xsh_frameset_extract(const cpl_frameset *frames,
984 const char *tag)
985{
986 cpl_frameset *subset = NULL;
987 const cpl_frame *f;
988
989
990
991 assure( frames != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null frameset" );
992 assure( tag != NULL, CPL_ERROR_ILLEGAL_INPUT, "Null tag" );
993
994 subset = cpl_frameset_new();
995
996 for (f = cpl_frameset_find_const(frames, tag);
997 f != NULL;
998 f = cpl_frameset_find_const(frames, NULL)) {
999
1000 cpl_frameset_insert(subset, cpl_frame_duplicate(f));
1001 }
1002 cleanup:
1003 return subset;
1004}
1005
1012/*----------------------------------------------------------------------------*/
1013cpl_error_code
1014xsh_frameset_merge(cpl_frameset * set1, cpl_frameset* set2)
1015{
1016
1017 cpl_frame* frm_tmp=NULL;
1018 cpl_frame* frm_dup=NULL;
1019
1020 passure(set1 != NULL, "Wrong input set");
1021
1022 cpl_frameset_iterator* it = cpl_frameset_iterator_new(set2);
1023 frm_tmp = cpl_frameset_iterator_get(it);
1024
1025 while (frm_tmp != NULL)
1026 {
1027 frm_dup=cpl_frame_duplicate(frm_tmp);
1028 cpl_frameset_insert(set1,frm_dup);
1029 cpl_frameset_iterator_advance(it, 1);
1030 frm_tmp = cpl_frameset_iterator_get(it);
1031
1032 }
1033 cpl_frameset_iterator_delete(it);
1034 cleanup:
1035 return cpl_error_get_code();
1036}
1037
1038/*---------------------------------------------------------------------------*/
1045/*---------------------------------------------------------------------------*/
1047{
1048 cpl_frame * cur_frame = NULL;
1049 xsh_instrument* instrument = NULL;
1050 int i;
1051 int nfrm;
1052 /* Check entries */
1053 assure (set != NULL, CPL_ERROR_NULL_INPUT, "Null frame set");
1054 assure (!cpl_frameset_is_empty(set),CPL_ERROR_ILLEGAL_INPUT,
1055 "Empty frame set");
1056
1057 /* allocate instrument structure */
1059 nfrm=cpl_frameset_get_size(set);
1060 /* Loop on frames */
1061 for (i = 0; i<nfrm; i++) {
1062 const char *tag = NULL;
1063 cur_frame=cpl_frameset_get_frame(set,i);
1064 check(tag = cpl_frame_get_tag (cur_frame));
1065
1066 /* SET GROUP */
1067 /* Initialize group to none */
1068 check(cpl_frame_set_group (cur_frame, CPL_FRAME_GROUP_NONE));
1069
1070 /* RAW frames */
1071 if (XSH_IS_RAW (tag)) {
1072 check(cpl_frame_set_group (cur_frame, CPL_FRAME_GROUP_RAW));
1073 }
1074 /* CALIB frames */
1075 else if (XSH_IS_CALIB (tag)) {
1076 check(cpl_frame_set_group (cur_frame, CPL_FRAME_GROUP_CALIB));
1077 }
1078 /* UNRECOGNIZED frames */
1079 else {
1080 xsh_msg_error("Unrecognized frame tag '%s'",tag);
1081 check(cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT ));
1082 //assure(0,CPL_ERROR_ILLEGAL_INPUT,
1083 // "Unrecognized frame tag: %s", tag);
1084 }
1085 /* we have validate the tag frame like an XSH tag */
1086
1087 /* CHECK INSTRUMENT */
1089 "invalid tag %s in file %s",
1090 tag, cpl_frame_get_filename (cur_frame));
1091 }
1092 /* validate arm */
1094 CPL_ERROR_TYPE_MISMATCH,"Arm is undefined");
1096 xsh_msg( "Arm %s, Mode %s Nb Orders %d, min: %d, max: %d",
1101 cleanup:
1102 if (cpl_error_get_code () != CPL_ERROR_NONE){
1104 instrument = NULL;
1105 }
1106 return instrument;
1107}
1108/*---------------------------------------------------------------------------*/
1115/*---------------------------------------------------------------------------*/
1116cpl_error_code
1117xsh_dfs_extract_raw_frames (cpl_frameset * input, cpl_frameset * raws)
1118{
1119 cpl_frame *cur_frame = NULL;
1120 int i=0;
1121 int nfrm=0;
1122 /* Loop on frames */
1123 nfrm=cpl_frameset_get_size(input);
1124 for (i=0; i < nfrm; i++) {
1125 cur_frame=cpl_frameset_get_frame(input,i);
1126 /* RAW frames */
1127 if (cpl_frame_get_group (cur_frame) == CPL_FRAME_GROUP_RAW) {
1128 cpl_frameset_insert (raws, cpl_frame_duplicate(cur_frame));
1129 }
1130 }
1131
1132return cpl_error_get_code();
1133
1134}
1135/*---------------------------------------------------------------------------*/
1142/*---------------------------------------------------------------------------*/
1143cpl_error_code
1144xsh_dfs_extract_calib_frames (cpl_frameset * input, cpl_frameset * calib)
1145{
1146 cpl_frame *cur_frame = NULL;
1147 int i;
1148 int nfrm;
1149 /* Loop on frames */
1150 nfrm=cpl_frameset_get_size(input);
1151
1152 for (i = 0; i< nfrm; i++) {
1153 cur_frame=cpl_frameset_get_frame(input,i);
1154 /* RAW frames */
1155 if (cpl_frame_get_group (cur_frame) == CPL_FRAME_GROUP_CALIB) {
1156 cpl_frameset_insert (calib, cpl_frame_duplicate(cur_frame));
1157 }
1158 }
1159
1160return cpl_error_get_code();
1161
1162}
1163
1164/*---------------------------------------------------------------------------*/
1171/*---------------------------------------------------------------------------*/
1172cpl_error_code
1173xsh_dfs_extract_pro_frames (cpl_frameset * input, cpl_frameset * pros)
1174{
1175 cpl_frame *cur_frame = NULL;
1176 int i=0;
1177 int nfrm=0;
1178 XSH_ASSURE_NOT_NULL_MSG(pros,"Null pros frameset. Alllocated it outside!");
1179 /* Loop on frames */
1180 nfrm=cpl_frameset_get_size(input);
1181 for (i=0;i<nfrm; i++) {
1182 cur_frame=cpl_frameset_get_frame(input,i);
1183 /* PRO frames */
1184 if (cpl_frame_get_group (cur_frame) == CPL_FRAME_GROUP_PRODUCT) {
1185 cpl_frameset_insert (pros, cpl_frame_duplicate(cur_frame));
1186 }
1187 }
1188 cleanup:
1189 return cpl_error_get_code();
1190
1191}
1192
1193/*---------------------------------------------------------------------------*/
1200/*---------------------------------------------------------------------------*/
1201void
1202xsh_dfs_split_in_group (cpl_frameset * input, cpl_frameset * raws,
1203 cpl_frameset * calib)
1204{
1205 cpl_frame *cur_frame = NULL;
1206 int i=0;
1207 int nfrm=0;
1208 /* Loop on frames */
1209 nfrm=cpl_frameset_get_size(input);
1210
1211 for (i = 0; i < nfrm; i++) {
1212 cur_frame=cpl_frameset_get_frame(input,i);
1213 /* RAW frames */
1214 if (cpl_frame_get_group (cur_frame) == CPL_FRAME_GROUP_RAW) {
1215 //xsh_dfs_check_nan(cur_frame);
1216 cpl_frameset_insert (raws, cpl_frame_duplicate(cur_frame));
1217 }
1218 /* CALIB frames */
1219 else if (cpl_frame_get_group (cur_frame) == CPL_FRAME_GROUP_CALIB) {
1220 cpl_frameset_insert (calib, cpl_frame_duplicate(cur_frame));
1221 }
1222 /* UNRECOGNIZED frames */
1223 else {
1224 xsh_msg_error ("Unrecognized group for frame %s",
1225 cpl_frame_get_filename (cur_frame));
1226 }
1227 }
1228}
1229
1230
1231/*---------------------------------------------------------------------------*/
1239/*---------------------------------------------------------------------------*/
1240void
1241xsh_dfs_split_qth_d2(cpl_frameset * input,
1242 cpl_frameset** qth,
1243 cpl_frameset** d2)
1244{
1245 cpl_frame* cur_frame = NULL;
1246 cpl_frame* temp =NULL;
1247 int i=0;
1248 int nfrm=0;
1249 /* check parameters input */
1250 XSH_ASSURE_NOT_NULL( input);
1251 XSH_ASSURE_NOT_NULL( qth);
1253
1254 /* create frameset */
1255 XSH_NEW_FRAMESET( *qth);
1256 XSH_NEW_FRAMESET( *d2);
1257
1258 nfrm=cpl_frameset_get_size(input);
1259 /* Loop on frames */
1260 for (i = 0; i < nfrm; i++) {
1261 const char* tag = NULL;
1262 cur_frame=cpl_frameset_get_frame(input,i);
1263 check(tag = cpl_frame_get_tag(cur_frame));
1264 /* ON frames */
1265 if ( strstr(tag,"D2") != NULL) {
1266 check(temp = cpl_frame_duplicate(cur_frame));
1267 check(cpl_frameset_insert(*d2, temp));
1268 }
1269 /* OFF frames */
1270 else if ( strstr(tag, "QTH") != NULL) {
1271 check(temp = cpl_frame_duplicate(cur_frame));
1272 check(cpl_frameset_insert(*qth, temp));
1273 }
1274 /* UNRECOGNIZED frames */
1275 else {
1276 xsh_msg_error ("Invalid tag %s for frame %s",tag,
1277 cpl_frame_get_filename (cur_frame));
1278 }
1279 }
1280
1281 cleanup:
1282 if (cpl_error_get_code() != CPL_ERROR_NONE){
1283 xsh_free_frame(&temp);
1285 xsh_free_frameset(qth);
1286 }
1287 return;
1288}
1289/*---------------------------------------------------------------------------*/
1296/*---------------------------------------------------------------------------*/
1297void xsh_dfs_split_nir(cpl_frameset * input, cpl_frameset** on,
1298 cpl_frameset** off)
1299{
1300 cpl_frame* cur_frame = NULL;
1301 cpl_frame* temp =NULL;
1302 int i=0;
1303 int nfrm=0;
1304 /* check parameters input */
1305 XSH_ASSURE_NOT_NULL(input);
1308
1309 /* create frameset */
1310 XSH_NEW_FRAMESET(*on);
1311 XSH_NEW_FRAMESET(*off);
1312 nfrm=cpl_frameset_get_size(input);
1313 /* Loop on frames */
1314 for (i = 0; i< nfrm ; i++) {
1315 const char* tag = NULL;
1316 cur_frame = cpl_frameset_get_frame(input,i);
1317 check(tag = cpl_frame_get_tag(cur_frame));
1318 /* ON frames */
1319 if ( strstr(tag,"ON") != NULL) {
1320 check(temp = cpl_frame_duplicate(cur_frame));
1321 check(cpl_frameset_insert(*on, temp));
1322 }
1323 /* OFF frames */
1324 else if ( strstr(tag, "OFF") != NULL) {
1325 check(temp = cpl_frame_duplicate(cur_frame));
1326 check(cpl_frameset_insert(*off, temp));
1327 }
1328 /* UNRECOGNIZED frames */
1329 else {
1330 xsh_msg_error ("Invalid tag %s for frame %s",tag,
1331 cpl_frame_get_filename (cur_frame));
1332 }
1333 }
1334
1335 cleanup:
1336 if (cpl_error_get_code() != CPL_ERROR_NONE){
1337 xsh_free_frame(&temp);
1339 xsh_free_frameset(off);
1340 }
1341 return;
1342}
1343
1344/*---------------------------------------------------------------------------*/
1355/*---------------------------------------------------------------------------*/
1356int
1357xsh_dfs_split_offset(cpl_frameset * input,
1358 const char * object_tag,
1359 const char * sky_tag,
1360 cpl_frameset ** object,
1361 cpl_frameset ** sky)
1362{
1363 cpl_frame* cur_frame = NULL;
1364 cpl_frame* temp =NULL;
1365 int nobj = 0, nsky = 0 ;
1366 int i=0;
1367 int nfrm=0;
1368 /* check parameters input */
1369 XSH_ASSURE_NOT_NULL(input);
1370 XSH_ASSURE_NOT_NULL(object);
1372
1373 /* create frameset */
1374 *object=cpl_frameset_new();
1375 *sky=cpl_frameset_new();
1376 nfrm=cpl_frameset_get_size(input);
1377 /* Loop on frames */
1378 for (i = 0; i < nfrm; i++ ) {
1379 const char* tag = NULL;
1380 cur_frame=cpl_frameset_get_frame(input,i);
1381
1382 check(tag = cpl_frame_get_tag(cur_frame));
1383 /* OBJECT frames */
1384 xsh_msg_dbg_medium( "Tag: %s", tag ) ;
1385
1386 if ( strstr(tag, object_tag) != NULL) {
1387 check(temp = cpl_frame_duplicate(cur_frame));
1388 check(cpl_frameset_insert(*object, temp));
1389 nobj++ ;
1390 }
1391 /* OFF frames */
1392 else if ( strstr(tag, sky_tag) != NULL) {
1393 check(temp = cpl_frame_duplicate(cur_frame));
1394 check(cpl_frameset_insert(*sky, temp));
1395 nsky++ ;
1396 }
1397 /* UNRECOGNIZED frames */
1398 else {
1399 xsh_msg_error ("Invalid tag %s for frame %s",tag,
1400 cpl_frame_get_filename (cur_frame));
1401 }
1402
1403 }
1404
1405 xsh_msg_dbg_medium( "Nobj: %d, Nsky: %d", nobj, nsky ) ;
1406 XSH_ASSURE_NOT_ILLEGAL( nobj == nsky ) ;
1407
1408 cleanup:
1409
1410 if (cpl_error_get_code() != CPL_ERROR_NONE){
1412 xsh_free_frame(&temp);
1413 return 0 ;
1414 }
1415 return nobj ;
1416}
1417
1418
1419
1420/*---------------------------------------------------------------------------*/
1427static const char*
1428xsh_create_final_tag(const char* pcatg_prefix, xsh_instrument* inst,
1429 const cpl_frame* frame)
1430{
1431
1432
1433 const char* pro_catg=NULL;
1434 /* set the procatg */
1435 if(pcatg_prefix==NULL) {
1436 pro_catg = cpl_frame_get_tag (frame);
1437 } else {
1438 pro_catg=xsh_stringcat_any(pcatg_prefix,"_",
1439 xsh_instrument_arm_tostring(inst),"" ) ;
1440 }
1441
1442 return pro_catg;
1443}
1444/*---------------------------------------------------------------------------*/
1450static char*
1451xsh_create_final_name(const char* final_prefix)
1452{
1453 char* final_name=NULL;
1454 time_t now ;
1455 char* date = NULL ;
1456 if ( xsh_time_stamp_get() ) {
1457 time( &now ) ;
1458 date = xsh_sdate_utc( &now ) ;
1460 final_prefix, "_", date, ".fits", "" ) ;
1461 } else if ( strstr( final_prefix, ".fits" ) == NULL ) {
1462 final_name=xsh_stringcat_any(XSH_PRODUCT_PREFIX,final_prefix,".fits","");
1463 } else {
1464 final_name=xsh_stringcat_any(XSH_PRODUCT_PREFIX,final_prefix, "" );
1465 }
1466
1467 XSH_FREE(date);
1468
1469 return final_name;
1470}
1471
1472/*---------------------------------------------------------------------------*/
1484/*---------------------------------------------------------------------------*/
1485static bool
1486xsh_dfs_tag_check( const char* tag, const char* tag_list[], int size)
1487{
1488 int i;
1489
1490 for( i=0; i< size; i++){
1491 if ( strstr( tag, tag_list[i])!=NULL){
1492 return true;
1493 }
1494 }
1495 return false;
1496}
1497
1498/*---------------------------------------------------------------------------*/
1509/*---------------------------------------------------------------------------*/
1510void
1511xsh_dfs_filter( cpl_frameset * set, const char* tags[], int size){
1512 cpl_frame *cur_frame = NULL;
1513
1514 XSH_ASSURE_NOT_NULL( set);
1515 XSH_ASSURE_NOT_NULL( tags);
1517
1518 /* Loop on frames */
1519 cpl_frameset_iterator* it = cpl_frameset_iterator_new(set);
1520 cur_frame = cpl_frameset_iterator_get(it);
1521
1522 while(cur_frame != NULL){
1523 const char* ftag = NULL;
1524
1525 check( ftag = cpl_frame_get_tag( cur_frame));
1526 if (!xsh_dfs_tag_check( ftag, tags, size)){
1527 cpl_frame *del_frame = NULL;
1528
1529 del_frame = cur_frame;
1530 cpl_frameset_iterator_advance(it, 1);
1531 cur_frame = cpl_frameset_iterator_get(it);
1532
1533 check( cpl_frameset_erase_frame( set, del_frame));
1534 }
1535 else{
1536 cpl_frameset_iterator_advance(it, 1);
1537 cur_frame = cpl_frameset_iterator_get(it);
1538 }
1539 }
1540 cpl_frameset_iterator_delete(it);
1541
1542 cleanup:
1543 return;
1544}
1545
1546/*---------------------------------------------------------------------------*/
1554/*---------------------------------------------------------------------------*/
1555cpl_frameset *
1556xsh_frameset_extract_pre(cpl_frameset * frameset, const char* prefix)
1557{
1558 cpl_frameset* set=NULL;
1559 cpl_frame* current=NULL;
1560 cpl_frame* found=NULL;
1561 int sz=0;
1562 int i=0;
1563 char tag[256];
1564 char name[256];
1565
1566 check(sz=cpl_frameset_get_size(frameset));
1567
1568 check(set=cpl_frameset_new());
1569 for(i=0;i<sz;i++) {
1570 check( current = cpl_frameset_get_frame(frameset, i));
1571 sprintf(name, "%s_PRE_%d.fits", prefix, i);
1572 sprintf(tag, "%s_PRE_%d", prefix, i);
1573
1574 if(xsh_file_exists(name) == 1) {
1575 check(found=cpl_frame_duplicate(current));
1576 check( cpl_frame_set_filename(found, name) );
1577 cpl_frameset_insert(set,found);
1578 }
1579
1580 }
1581 cleanup:
1582
1583 return set;
1584
1585}
1586
1587/*---------------------------------------------------------------------------*/
1593/*---------------------------------------------------------------------------*/
1594cpl_frame *
1595xsh_find_bpmap (cpl_frameset * set)
1596{
1597 cpl_frame *cur_frame = NULL;
1598 int i=0;
1599 int nfrm=0;
1600
1601 nfrm=cpl_frameset_get_size(set);
1602 /* Loop on frames */
1603 for (i = 0; i < nfrm; i++) {
1604 cur_frame = cpl_frameset_get_frame(set,i);
1605 if (strstr (cpl_frame_get_tag (cur_frame), "BADPIXEL_MAP")) {
1606 return cur_frame;
1607 }
1608 }
1609 return NULL;
1610}
1611
1612
1613/*---------------------------------------------------------------------------*/
1619/*---------------------------------------------------------------------------*/
1620cpl_frame *
1621xsh_find_master_bpmap (cpl_frameset * set)
1622{
1623 cpl_frame *cur_frame = NULL;
1624 int i=0;
1625 int nfrm=0;
1626
1627 nfrm=cpl_frameset_get_size(set);
1628 /* Loop on frames */
1629 for (i = 0; i < nfrm; i++) {
1630 cur_frame=cpl_frameset_get_frame(set,i);
1631 if (strstr (cpl_frame_get_tag (cur_frame), XSH_MASTER_BP_MAP)) {
1632 return cur_frame;
1633 }
1634 }
1635 return NULL;
1636}
1637
1638/*---------------------------------------------------------------------------*/
1644/*---------------------------------------------------------------------------*/
1645cpl_frame * xsh_find_off(cpl_frameset* set)
1646{
1647 cpl_frame *cur_frame = NULL;
1648 int i=0;
1649 int nfrm=0;
1650
1651 nfrm=cpl_frameset_get_size(set);
1652 /* Loop on frames */
1653 for (i=0;i<nfrm; i++) {
1654 cur_frame=cpl_frameset_get_frame(set,i);
1655 if (strstr (cpl_frame_get_tag (cur_frame), "OFF")) {
1656 return cur_frame;
1657 }
1658 }
1659 return NULL;
1660}
1661
1662/*---------------------------------------------------------------------------*/
1668/*---------------------------------------------------------------------------*/
1669cpl_frame * xsh_find_on(cpl_frameset* set)
1670{
1671 cpl_frame *cur_frame = NULL;
1672 int i=0;
1673 int nfrm=0;
1674 nfrm=cpl_frameset_get_size(set);
1675 /* Loop on frames */
1676 for (i = 0; i< nfrm; i++) {
1677 cur_frame=cpl_frameset_get_frame(set,i);
1678 if (strstr (cpl_frame_get_tag (cur_frame), "ON")) {
1679 return cur_frame;
1680 }
1681 }
1682 return NULL;
1683}
1684/*---------------------------------------------------------------------------*/
1691/*---------------------------------------------------------------------------*/
1692void
1693xsh_dfs_find_flat(cpl_frameset* raws,cpl_frameset* flat)
1694{
1695 cpl_frame *cur_frame = NULL;
1696 const char* tag = NULL;
1697 int i=0;
1698 int nfrm=0;
1699
1700 /* check input */
1701 assure (raws != NULL, CPL_ERROR_NULL_INPUT, "Null frameset");
1702 assure (flat != NULL, CPL_ERROR_NULL_INPUT, "Null frameset");
1703
1704 /* init */
1705 nfrm=cpl_frameset_get_size(raws);
1706 /* Loop on frames */
1707 for (i = 0; i < nfrm; i++)
1708 {
1709 cur_frame=cpl_frameset_get_frame(raws,i);
1710 tag = cpl_frame_get_tag (cur_frame);
1711 if (strstr(tag,"FLAT")!=NULL)
1712 cpl_frameset_insert (flat, cur_frame);
1713 }
1714
1715 cleanup:
1716 return;
1717
1718}
1719
1720/*---------------------------------------------------------------------------*/
1735/*---------------------------------------------------------------------------*/
1736char *
1737xsh_unique_filename (const char *context, const char *caller_id,
1738 const char *pro_catg)
1739{
1740 if (context != NULL) {
1741 //fixme: if context contains '.' replace it with '-'
1742 return xsh_stringcat_6 (context, "-", caller_id, "-", pro_catg, ".fits");
1743 }
1744 else {
1745 return xsh_stringcat_4 (caller_id, "-", pro_catg, ".fits");
1746 }
1747}
1748
1749
1761void xsh_add_product(cpl_frame *frame,
1762 cpl_frameset * frameset,
1763 const cpl_parameterlist * parameters,
1764 const char *recipe_id,
1766 const char *final_prefix ,
1767 const char* type)
1768{
1769 cpl_propertylist *primary_header = NULL;
1770 cpl_propertylist *tbl_header = NULL;
1771 int extension=0;
1772 const char *pro_catg = NULL;
1773 cpl_frame* product_frame = NULL;
1774
1775 cpl_table *table = NULL ;
1776 cpl_image *image = NULL ;
1777 cpl_imagelist *imagelist = NULL ;
1778
1779
1780 const char *fname = NULL;
1781 char *final_name = NULL ;
1782 char* date = NULL ;
1783 /* char *paf_name = NULL ; */
1784
1785 /* Check input parameters */
1786 XSH_ASSURE_NOT_NULL(frame);
1787 XSH_ASSURE_NOT_NULL(frameset);
1788 XSH_ASSURE_NOT_NULL(parameters);
1789 XSH_ASSURE_NOT_NULL(recipe_id);
1791 XSH_ASSURE_NOT_NULL(final_prefix);
1792
1793 check(pro_catg = cpl_frame_get_tag (frame));
1794 XSH_ASSURE_NOT_NULL(pro_catg);
1795
1796 /* Load header and imagelist of bpmap frame */
1797 check(fname = cpl_frame_get_filename( frame ));
1798 check(primary_header = cpl_propertylist_load( fname, 0 ));
1799
1800 if(strcmp(type,"imagelist") == 0) {
1801 check(imagelist = cpl_imagelist_load( fname, CPL_TYPE_FLOAT, 0 ));
1802 } else if(strcmp(type,"image") == 0) {
1803 check(image = cpl_image_load( fname, CPL_TYPE_FLOAT, 0,0 ));
1804 } else if(strcmp(type,"table") == 0) {
1805 check(table = cpl_table_load( fname, 1,0 ));
1806 } else {
1807 xsh_msg_error("Frame type %s not supported",type);
1808 }
1809 xsh_clean_header(primary_header);
1810 /* update header */
1811 check( cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT));
1812 check_msg (cpl_dfs_setup_product_header ( primary_header,
1813 frame,frameset,parameters,
1814 recipe_id,
1816 instrument->dictionary,NULL),
1817 "Problem in the product DFS-compliance");
1818
1819 /* Create final name */
1820 final_name=xsh_create_final_name(final_prefix);
1821
1822 xsh_msg_dbg_low( "Final product name: %s", final_name ) ;
1823
1824
1825
1826 /* Now save file with updated header */
1827 if(strcmp(type,"imagelist") == 0) {
1828 cpl_imagelist_save(imagelist, final_name, CPL_BPP_IEEE_FLOAT,
1829 primary_header,CPL_IO_DEFAULT ) ;
1830 } else if(strcmp(type,"image") == 0) {
1831 cpl_image_save(image, final_name, CPL_BPP_IEEE_FLOAT,
1832 primary_header,CPL_IO_DEFAULT ) ;
1833 } else if(strcmp(type,"table") == 0) {
1834 check(cpl_table_save( table, primary_header, tbl_header,
1835 final_name, extension ));
1836 }
1837
1838
1839
1840
1841 /*
1842 Save PAF
1843
1844 paf_name = xsh_stringcat_any( final_prefix, ".paf", (void*)NULL ) ;
1845 XSH_ASSURE_NOT_NULL( paf_name ) ;
1846 check_msg( xsh_paf_save( instrument, recipe_id,
1847 primary_header, paf_name, pro_catg ),
1848 "Cant save PAF file" ) ;
1849
1850 */
1851
1852
1853 /* Create product frame */
1854 if(strcmp(type,"imagelist") == 0) {
1855 check(product_frame=xsh_frame_product(final_name,pro_catg,
1856 CPL_FRAME_TYPE_IMAGE,
1857 CPL_FRAME_GROUP_PRODUCT,
1858 CPL_FRAME_LEVEL_FINAL));
1859 } else if(strcmp(type,"image") == 0) {
1860 check(product_frame=xsh_frame_product(final_name,pro_catg,
1861 CPL_FRAME_TYPE_IMAGE,
1862 CPL_FRAME_GROUP_PRODUCT,
1863 CPL_FRAME_LEVEL_FINAL));
1864 } else if(strcmp(type,"table") == 0) {
1865 check(product_frame=xsh_frame_product(final_name,pro_catg,
1866 CPL_FRAME_TYPE_TABLE,
1867 CPL_FRAME_GROUP_PRODUCT,
1868 CPL_FRAME_LEVEL_FINAL));
1869
1870 }
1871
1872 check(cpl_frameset_insert(frameset, product_frame));
1873 xsh_add_product_file(final_name);
1874 cleanup:
1875 XSH_FREE(date);
1876 xsh_free_propertylist(&primary_header);
1877 xsh_free_image( &image ) ;
1878 xsh_free_imagelist( &imagelist ) ;
1879 xsh_free_table( &table ) ;
1880 XSH_FREE( final_name ) ;
1881 /* XSH_FREE( paf_name ) ; */
1882 return;
1883}
1884
1894void
1895xsh_add_product_vector( cpl_frame *frame,
1896 cpl_frameset *frameset,
1897 const cpl_parameterlist *parameters,
1898 const char *recipe_id,
1900 const char * final_prefix)
1901{
1902
1903 cpl_vector *vect = NULL;
1904 cpl_propertylist *primary_header = NULL;
1905 const char *pro_catg = "";
1906 cpl_frame* product_frame = NULL;
1907 const char *fname = NULL;
1908 char *final_name = NULL ;
1909 /* time_t now ; */
1910 char* date = NULL ;
1911 /* char *paf_name = NULL ; */
1912
1913 /* Check input parameters */
1914 XSH_ASSURE_NOT_NULL(frame);
1915 XSH_ASSURE_NOT_NULL(frameset);
1916 XSH_ASSURE_NOT_NULL(parameters);
1917 XSH_ASSURE_NOT_NULL(recipe_id);
1919 XSH_ASSURE_NOT_NULL(final_prefix);
1920
1921 check(pro_catg = cpl_frame_get_tag (frame));
1922 XSH_ASSURE_NOT_NULL(pro_catg);
1923
1924 /* Load header and image of bpmap frame */
1925 check( fname = cpl_frame_get_filename( frame));
1926 check( primary_header = cpl_propertylist_load( fname, 0));
1927 check( vect = cpl_vector_load( fname, 0));
1928 xsh_clean_header(primary_header);
1929 /* update header */
1930 check( cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT));
1931 check_msg (cpl_dfs_setup_product_header ( primary_header,
1932 frame,frameset,parameters,
1933 recipe_id,
1935 instrument->dictionary,NULL),
1936 "Problem in the product DFS-compliance");
1937
1938 /* Create final name */
1939 final_name=xsh_create_final_name(final_prefix);
1940 xsh_msg_dbg_low( "Final product name: %s", final_name ) ;
1941
1942 /* Now save file with updated header */
1943 check( cpl_vector_save( vect, final_name, XSH_PRE_QUAL_BPP,
1944 primary_header, CPL_IO_CREATE));
1945
1946 /* Create product frame */
1947 check(product_frame=xsh_frame_product(final_name,pro_catg,
1948 CPL_FRAME_TYPE_IMAGE,
1949 CPL_FRAME_GROUP_PRODUCT,
1950 CPL_FRAME_LEVEL_FINAL));
1951
1952 check(cpl_frameset_insert(frameset, product_frame));
1953 xsh_add_product_file(final_name);
1954
1955 cleanup:
1956 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1957 xsh_free_frame( &product_frame);
1958 product_frame = NULL;
1959 }
1960 XSH_FREE(date);
1961 xsh_free_propertylist(&primary_header);
1962 xsh_free_vector( &vect) ;
1963 XSH_FREE( final_name);
1964 return;
1965}
1966
1979static double xsh_calculate_exptime(const cpl_frameset *usedframes,
1980 XSH_ARM arm)
1981{
1982 cpl_propertylist* keywords = NULL;
1983 cpl_frameset_iterator* iter = NULL;
1984 double total = 0;
1985
1986 iter = cpl_frameset_iterator_new(usedframes);
1987 const cpl_frame* frame = cpl_frameset_iterator_get_const(iter);
1988 while (frame != NULL) {
1989 if (cpl_frame_get_group(frame) == CPL_FRAME_GROUP_RAW) {
1990 /* Load the primary header keywords from the raw frame. */
1991 const char* filename;
1992 check(filename = cpl_frame_get_filename(frame));
1993 check(keywords = cpl_propertylist_load(filename, 0));
1994 /* Extract the EXPTIME keyword and add it to the sum. */
1995 double exptime, dit;
1996 int ndit, nditskip;
1997 switch (arm) {
1998 case XSH_ARM_NIR:
1999 check(dit = cpl_propertylist_get_double(keywords, XSH_DET_DIT));
2000 check(ndit = cpl_propertylist_get_int(keywords, XSH_DET_NDIT));
2001 check(nditskip = cpl_propertylist_get_int(keywords,
2003 if (nditskip == 0) {
2004 exptime = dit * ndit;
2005 } else {
2006 xsh_msg_warning("Found '%s' with a non zero value. Do not know how"
2007 " to handle this case so will not set '%s'. This keyword must"
2008 " be set manually.", XSH_DET_NDITSKIP, XSH_EXPTIME);
2009 goto cleanup;
2010 }
2011 break;
2012 default:
2013 check(exptime = cpl_propertylist_get_double(keywords, XSH_EXPTIME));
2014 break;
2015 }
2016 /* PIPE-8857: Make sure that we are only counting DPR.TYPE=OBJECT frames */
2017 const char* dpr_type = cpl_propertylist_get_string(keywords, XSH_DPR_TYPE) ;
2018// xsh_msg_warning("I'm looking at the EXPTIME from type '%s'", dpr_type) ;
2019 int dpr_is_not_obj = strcmp(dpr_type, "OBJECT") ;
2020 int dpr_is_not_stdtell = strcmp(dpr_type, "STD,TELLURIC") ;
2021 //xsh_msg_warning("dpr_is_not_obj is %d", dpr_is_not_obj) ;
2022 //xsh_msg_warning("dpr_is_not_stdtell is %d", dpr_is_not_stdtell) ;
2023 if (!dpr_is_not_obj || !dpr_is_not_stdtell) {
2024// xsh_msg_warning("I'm skipping a file for EXPTIME because it's not OBJECT (%s)", dpr_type);
2025// } else {
2026// xsh_msg_warning("Allowing file for EXPTIME calculation");
2027 total += exptime;
2028 xsh_msg_warning("Added %f to (T)EXPTIME", exptime) ;
2029 }
2030 cpl_propertylist_delete(keywords);
2031 keywords = NULL;
2032 }
2033 /* Increment the iterator to the next frame. */
2034 cpl_errorstate status = cpl_errorstate_get();
2035 cpl_frameset_iterator_advance(iter, 1);
2036 if (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
2037 cpl_errorstate_set(status);
2038 }
2039 frame = cpl_frameset_iterator_get_const(iter);
2040 }
2041
2042 cpl_frameset_iterator_delete(iter);
2043 cpl_propertylist_delete(keywords);
2044// xsh_msg_warning("EXPTIME/TEXPTIME total: %f", total);
2045 return total;
2046
2047 cleanup:
2048 /* Enter this cleanup section if an error occurs. */
2049 cpl_frameset_iterator_delete(iter);
2050 cpl_propertylist_delete(keywords);
2051 return NAN;
2052}
2053
2066void
2068 cpl_frameset *frameset,
2069 const cpl_parameterlist *parameters,
2070 const char *recipe_id,
2071 xsh_instrument* instr,
2072 cpl_frame ** result_frame,
2073 cpl_propertylist* qclist)
2074{
2075
2076 const char *pro_catg = "";
2077 xsh_spectrum *spectrum = NULL;
2078 cpl_frame *product_frame = NULL;
2079 char product_id[256];
2080 char* product_name = NULL;
2081 /* char* paf_name = NULL; */
2082 time_t now ;
2083 char* date = NULL;
2084 xsh_bary_corr_param* bary_param ;
2085
2086 /* Check entries */
2087 XSH_ASSURE_NOT_NULL( frame);
2088 XSH_ASSURE_NOT_NULL( frameset);
2089 XSH_ASSURE_NOT_NULL( parameters);
2090 XSH_ASSURE_NOT_NULL( recipe_id);
2091 XSH_ASSURE_NOT_NULL( instr);
2092
2093 check( pro_catg = cpl_frame_get_tag (frame));
2094
2095 assure( pro_catg != NULL, CPL_ERROR_NULL_INPUT,
2096 "Frame tag has not been set");
2097
2098 /* Load PRE */
2099 check( spectrum = xsh_spectrum_load( frame));
2100 xsh_clean_header(spectrum->flux_header);
2101
2102 /* update header */
2103 cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT);
2104 check( cpl_dfs_setup_product_header( spectrum->flux_header,
2105 frame, frameset, parameters, recipe_id, instr->pipeline_id,
2106 instr->dictionary, NULL));
2107 //if(pro_catg contains "MERGE2D" and recipe ==...)
2108 //This is now done only on the non-flux cal frame
2109 if(qclist != NULL && (strstr(pro_catg,XSH_MERGE2D) != NULL || strstr(pro_catg,XSH_MERGE1D) != NULL) && spectrum->flux != NULL && spectrum->flux_header != NULL){
2110 //calc_curve_qc(spectrum->flux,instr,spectrum->flux_header);
2111 if(cpl_propertylist_has(qclist,"ESO QC CURVE 0") &&
2112 cpl_propertylist_has(qclist,"ESO QC CURVE 1") &&
2113 cpl_propertylist_has(qclist,"ESO QC CURVE X")){
2114 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC CURVE 0",cpl_propertylist_get_double(qclist,"ESO QC CURVE 0"));
2115 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC CURVE 1",cpl_propertylist_get_double(qclist,"ESO QC CURVE 1"));
2116 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC CURVE X",cpl_propertylist_get_double(qclist,"ESO QC CURVE X"));
2117 }
2118 if(cpl_propertylist_has(qclist,"ESO QC RESP SPEC A") &&
2119 cpl_propertylist_has(qclist,"ESO QC RESP SPEC B") &&
2120 cpl_propertylist_has(qclist,"ESO QC RESP SPEC DA") &&
2121 cpl_propertylist_has(qclist,"ESO QC RESP SPEC DB")){
2122 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC A",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC A"));
2123 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC B",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC B"));
2124 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC DA",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC DA"));
2125 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC DB",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC DB"));
2126 }
2127 if(cpl_propertylist_has(qclist,"ESO QC RESP SPEC A QTH") &&
2128 cpl_propertylist_has(qclist,"ESO QC RESP SPEC B QTH") &&
2129 cpl_propertylist_has(qclist,"ESO QC RESP SPEC DA QTH") &&
2130 cpl_propertylist_has(qclist,"ESO QC RESP SPEC DB QTH")){
2131 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC A QTH",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC A QTH"));
2132 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC B QTH",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC B QTH"));
2133 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC DA QTH",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC DA QTH"));
2134 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC DB QTH",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC DB QTH"));
2135 }
2136 if(cpl_propertylist_has(qclist,"ESO QC RESP SPEC A D2") &&
2137 cpl_propertylist_has(qclist,"ESO QC RESP SPEC B D2") &&
2138 cpl_propertylist_has(qclist,"ESO QC RESP SPEC DA D2") &&
2139 cpl_propertylist_has(qclist,"ESO QC RESP SPEC DB D2")){
2140 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC A D2",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC A D2"));
2141 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC B D2",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC B D2"));
2142 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC DA D2",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC DA D2"));
2143 cpl_propertylist_update_double(spectrum->flux_header,"ESO QC RESP SPEC DB D2",cpl_propertylist_get_double(qclist,"ESO QC RESP SPEC DB D2"));
2144 }
2145 }
2146 //could also check if SDP is generated...
2147 if(strstr(pro_catg,XSH_MERGE1D) != NULL && strstr(pro_catg,"FLUX") == NULL && qclist != NULL){
2148 cpl_image* flux = cpl_image_duplicate(spectrum->flux);
2149 cpl_image* errs = cpl_image_duplicate(spectrum->errs);
2150 cpl_propertylist_append_double(qclist,"ESO QC MEAN RED",cpl_image_get_mean(flux));
2151 cpl_propertylist_append_double(qclist,"ESO QC RMS RED",cpl_image_get_stdev(flux));
2152 cpl_image* div = cpl_image_duplicate(flux);
2153 //div has now been divided by errs (in place)
2154 cpl_image_divide(div,errs);
2155 cpl_propertylist_append_double(qclist,"ESO QC MEAN S2N",cpl_image_get_mean(div));
2156 cpl_image_delete(flux);
2157 cpl_image_delete(errs);
2158 cpl_image_delete(div);
2159 }
2160
2161 // Only needed for IDP products, but as there's no other IDP specific save routines
2162 // for thses files we add it to all output.
2163 if(strstr(pro_catg,"SCI_SLIT_MERGE2D") || strstr(pro_catg,"TELL_SLIT_MERGE2D"))
2164 {
2165 cpl_propertylist_append_string(spectrum->flux_header,XSH_SDP_KEYWORD_PRODCATG,XSH_ANC_SPECTRUM);
2166 }
2167
2168
2169 if(
2170 strstr(recipe_id,"xsh_respon") != NULL ||
2171 strstr(recipe_id,"xsh_scired") != NULL ||
2172 strstr(recipe_id,"xsh_util_ifu") != NULL ) {
2173 xsh_dfs_fix_key_start_end(frameset,spectrum->flux_header);
2174 }
2175
2176 /* PIPE-8857 - compute a TEXPTIME for MERGE1D and MERGE2D products */
2177// cpl_error_code error = CPL_ERROR_NONE;
2178// cpl_errorstate prestate;
2179// prestate = cpl_errorstate_get();
2180 double exptime = xsh_calculate_exptime(frameset,
2181 xsh_instrument_get_arm(instr));
2182 if (! isnan(exptime) && exptime > 0.) {
2183// xsh_msg_warning("I am attempting a TEXPTIME of %f", exptime) ;
2184 cpl_propertylist_append_double(spectrum->flux_header, "TEXPTIME", exptime) ;
2185// } else if (! cpl_errorstate_is_equal(prestate)) {
2186// error = cpl_error_get_code();
2187 }
2188
2189 // PIPE-10061 - barycentric correct the header, if requested
2190 bary_param = xsh_parameters_bary_corr_get(recipe_id, parameters);
2192 bary_param);
2194 bary_param);
2196 bary_param);
2197
2198
2199 /* Save product */
2200 if (xsh_time_stamp_get() ){
2201 time( &now );
2202 date = xsh_sdate_utc(&now);
2203 sprintf( product_id, "%s%s_%s", XSH_PRODUCT_PREFIX,
2204 pro_catg, date);
2205 }
2206 else {
2207 sprintf( product_id, "%s%s", XSH_PRODUCT_PREFIX,
2208 pro_catg);
2209 }
2210 product_name = xsh_stringcat_any( product_id, ".fits", (void*)NULL);
2211 xsh_msg_warning("For product %s", product_name) ;
2212 check( product_frame = xsh_spectrum_save(spectrum, product_name,pro_catg));
2213
2214 /* Save PAF
2215 paf_name = xsh_stringcat_any( product_id, ".paf", (void*)NULL);
2216 XSH_ASSURE_NOT_NULL( paf_name);
2217 check_msg( xsh_paf_save( instr, recipe_id,
2218 spectrum->flux_header, paf_name,
2219 pro_catg ),
2220 "Cant save PAF file" ) ;
2221 */
2222
2223 /* Add the new frame into the frameset */
2224 cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE);
2225 cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT);
2226 cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL);
2227 cpl_frame_set_tag( product_frame, pro_catg);
2228 cpl_frameset_insert( frameset, product_frame);
2229 xsh_add_product_file(product_name);
2230
2231 if (result_frame != NULL) {
2232 *result_frame = cpl_frame_duplicate(product_frame);
2233 }
2234
2235 cleanup:
2236 if (cpl_error_get_code () != CPL_ERROR_NONE) {
2237 xsh_free_frame( &product_frame);
2238 product_frame = NULL;
2239 }
2240 XSH_FREE(bary_param);
2241 XSH_FREE( date);
2242 xsh_spectrum_free( &spectrum);
2243 XSH_FREE( product_name);
2244 /* XSH_FREE( paf_name); */
2245 return;
2246}
2259void
2261 cpl_frameset *frameset,
2262 const cpl_parameterlist *parameters,
2263 const char *recipe_id,
2264 xsh_instrument* instr,
2265 cpl_frame ** result_frame)
2266{
2267
2268 const char *pro_catg = "";
2269 //xsh_spectrum *spectrum = NULL;
2270 cpl_frame *product_frame = NULL;
2271 char product_id[256];
2272 char* product_name = NULL;
2273 /* char* paf_name = NULL; */
2274 time_t now ;
2275 char cmd[256];
2276 char* date = NULL;
2277 cpl_vector* vec_data=NULL;
2278 cpl_vector* vec_errs=NULL;
2279 cpl_vector* vec_qual=NULL;
2280 const char *name_s = NULL;
2281 cpl_propertylist* h_data=NULL;
2282 cpl_propertylist* h_errs=NULL;
2283 cpl_propertylist* h_qual=NULL;
2284 char *final_name = NULL ;
2285 xsh_bary_corr_param* bary_param ;
2286
2287 /* Check entries */
2288 XSH_ASSURE_NOT_NULL( frame);
2289 XSH_ASSURE_NOT_NULL( frameset);
2290 XSH_ASSURE_NOT_NULL( parameters);
2291 XSH_ASSURE_NOT_NULL( recipe_id);
2292 XSH_ASSURE_NOT_NULL( instr);
2293
2294 check( pro_catg = cpl_frame_get_tag (frame));
2295
2296 assure( pro_catg != NULL, CPL_ERROR_NULL_INPUT,
2297 "Frame tag has not been set");
2298
2299 /* set the procatg */
2300 pro_catg=xsh_create_final_tag(NULL,instr,frame);
2301
2302 /* Define product name */
2303 if (xsh_time_stamp_get() ){
2304 time( &now );
2305 date = xsh_sdate_utc(&now);
2306 sprintf( product_id, "%s%s_%s", XSH_PRODUCT_PREFIX,
2307 pro_catg, date);
2308 }
2309 else {
2310 sprintf( product_id, "%s%s", XSH_PRODUCT_PREFIX,
2311 pro_catg);
2312 }
2313
2314 product_name = xsh_stringcat_any( product_id, ".fits", (void*)NULL);
2315 final_name=xsh_create_final_name(pro_catg);
2316 xsh_msg( "Final product name: %s", final_name ) ;
2317 name_s=cpl_frame_get_filename(frame);
2318 int next=cpl_frame_get_nextensions(frame);
2319
2320 if(strcmp(name_s,final_name) == 0) {
2321 sprintf(cmd,"mv %s tmp_spc.fits",name_s);
2322 system(cmd);
2323 name_s="tmp_spc.fits";
2324 xsh_add_temporary_file("tmp_spc.fits");
2325 }
2326
2327 cpl_propertylist* primary_header;
2328 check( primary_header = cpl_propertylist_load( name_s, 0));
2329 if(
2330 strstr(recipe_id,"xsh_respon") != NULL ||
2331 strstr(recipe_id,"xsh_scired") != NULL ||
2332 strstr(recipe_id,"xsh_util_ifu") != NULL ) {
2333 xsh_msg("Fix key");
2334 //exit(0);
2335 xsh_dfs_fix_key_start_end(frameset,primary_header);
2336 }
2337
2338 /* PIPE-8857 - compute a TEXPTIME for MERGE1D and MERGE2D products */
2339// cpl_error_code error = CPL_ERROR_NONE;
2340// cpl_errorstate prestate;
2341// prestate = cpl_errorstate_get();
2342 double exptime = xsh_calculate_exptime(frameset,
2343 xsh_instrument_get_arm(instr));
2344 if (! isnan(exptime) && exptime > 0.) {
2345// xsh_msg_warning("I am attempting a TEXPTIME of %f", exptime) ;
2346 cpl_propertylist_append_double(primary_header, "TEXPTIME", exptime) ;
2347// } else if (! cpl_errorstate_is_equal(prestate)) {
2348// error = cpl_error_get_code();
2349 }
2350
2351 // PIPE-10061 - barycentric correct the header, if requested
2352 bary_param = xsh_parameters_bary_corr_get(recipe_id, parameters);
2353 xsh_baryvel_correct_header(primary_header, primary_header, bary_param);
2354
2355 /* Now save file with updated header */
2356 xsh_pfits_set_pcatg(primary_header,pro_catg);
2357 xsh_plist_set_extra_keys(primary_header,"IMAGE","DATA","RMSE",
2358 "FLUX","ERRS","QUAL",0);
2359 xsh_clean_header(primary_header);
2360
2361 //int norders = (next+1)/3;
2362
2363 for(int i=0;i<next;i+=3) {
2364 vec_data=cpl_vector_load(name_s,i);
2365 vec_errs=cpl_vector_load(name_s,i+1);
2366 vec_qual=cpl_vector_load(name_s,i+2);
2367
2368 check(h_data=cpl_propertylist_load(name_s,i));
2369 check(h_errs=cpl_propertylist_load(name_s,i+1));
2370 check(h_qual=cpl_propertylist_load(name_s,i+2));
2371
2372 // PIPE-10061 - Correct these headers
2373 xsh_baryvel_correct_header(h_data, primary_header, bary_param);
2374 xsh_baryvel_correct_header(h_errs, primary_header, bary_param);
2375 xsh_baryvel_correct_header(h_qual, primary_header, bary_param);
2376
2377 if(i==0) {
2378 /*
2379 //xsh_clean_header(h_data);
2380 cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT);
2381 cpl_dfs_setup_product_header( h_data,frame,frameset,parameters,
2382 recipe_id,instr->pipeline_id,
2383 instr->dictionary,NULL);
2384 */
2385 cpl_dfs_setup_product_header(primary_header,frame, frameset, parameters,
2386 recipe_id, instr->pipeline_id,
2387 instr->dictionary, NULL);
2388
2389 cpl_vector_save(vec_data,product_name,XSH_SPECTRUM_DATA_BPP,primary_header,CPL_IO_DEFAULT);
2390 cpl_vector_save(vec_errs,product_name,XSH_SPECTRUM_ERRS_BPP,h_errs,CPL_IO_EXTEND);
2391 cpl_vector_save(vec_qual,product_name,XSH_SPECTRUM_QUAL_BPP,h_qual,CPL_IO_EXTEND);
2392
2393 } else{
2394
2395 cpl_vector_save(vec_data,product_name,XSH_SPECTRUM_DATA_BPP,h_data,CPL_IO_EXTEND);
2396 cpl_vector_save(vec_errs,product_name,XSH_SPECTRUM_ERRS_BPP,h_errs,CPL_IO_EXTEND);
2397 cpl_vector_save(vec_qual,product_name,XSH_SPECTRUM_QUAL_BPP,h_qual,CPL_IO_EXTEND);
2398
2399 }
2400 XSH_FREE(final_name);
2401 xsh_free_vector(&vec_data);
2402 xsh_free_vector(&vec_errs);
2403 xsh_free_vector(&vec_qual);
2404 xsh_free_propertylist(&h_data);
2405 xsh_free_propertylist(&h_errs);
2406 xsh_free_propertylist(&h_qual);
2407
2408 }
2409
2410 /* Add the new frame into the frameset */
2411 product_frame=cpl_frame_new();
2412
2413 cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE);
2414 cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT);
2415 cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL);
2416 cpl_frame_set_tag( product_frame, pro_catg);
2417 cpl_frame_set_filename( product_frame, product_name);
2418
2419 cpl_frameset_insert( frameset, product_frame);
2420 xsh_add_product_file(product_name);
2421
2422 if (result_frame != NULL) {
2423 *result_frame = cpl_frame_duplicate(product_frame);
2424 }
2425
2426 cleanup:
2427 if (cpl_error_get_code () != CPL_ERROR_NONE) {
2428 xsh_free_frame( &product_frame);
2429 product_frame = NULL;
2430 }
2431 xsh_free_propertylist(&primary_header);
2432 XSH_FREE(bary_param);
2433 XSH_FREE( date);
2434 XSH_FREE( product_name);
2435
2436 return;
2437}
2438/*---------------------------------------------------------------------------*/
2463/*---------------------------------------------------------------------------*/
2464void
2465xsh_add_product_pre(cpl_frame * frame,
2466 cpl_frameset * frameset,
2467 const cpl_parameterlist *parameters,
2468 const char *recipe_id,
2469 xsh_instrument* instr,
2470 cpl_propertylist* qclist)
2471{
2472 const char *pro_catg = "";
2473 xsh_pre* product_pre = NULL;
2474 cpl_frame* product_frame = NULL;
2475 char product_id[256];
2476 char* product_name = NULL;
2477 /* char* paf_name = NULL; */
2478 time_t now ;
2479 char* date = NULL;
2480 cpl_propertylist* plist=NULL;
2481 //cpl_propertylist* qclist=cpl_propertylist_new();
2482 //char arcfile[256];
2483 //bool arcfile_found=false;
2484 xsh_bary_corr_param* bary_param ;
2485
2486 /* Check entries */
2487 XSH_ASSURE_NOT_NULL( frame);
2488 XSH_ASSURE_NOT_NULL( frameset);
2489 XSH_ASSURE_NOT_NULL( parameters);
2490 XSH_ASSURE_NOT_NULL( recipe_id);
2491 XSH_ASSURE_NOT_NULL( instr);
2492
2493 check( pro_catg = cpl_frame_get_tag (frame));
2494
2495 assure( pro_catg != NULL, CPL_ERROR_NULL_INPUT,
2496 "Frame tag has not been set");
2497
2498 /* Load PRE */
2499 check( product_pre = xsh_pre_load( frame,instr));
2500 /*
2501 if(cpl_propertylist_has(product_pre->data_header,"ARCFILE")) {
2502 sprintf(arcfile,"%s",xsh_pfits_get_arcfile(product_pre->data_header));
2503 arcfile_found=true;
2504 }
2505 */
2506 xsh_clean_header(product_pre->data_header);
2507
2508 if(qclist != NULL && cpl_propertylist_get_size(qclist) > 0){
2509 cpl_propertylist_append(product_pre->data_header,qclist);
2510 }
2511 /* update header */
2512 cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT);
2513 check( cpl_dfs_setup_product_header( product_pre->data_header,
2514 frame, frameset, parameters, recipe_id, instr->pipeline_id,
2515 instr->dictionary,NULL));
2516
2517
2518 if(
2519 strstr(recipe_id,"xsh_respon") != NULL ||
2520 strstr(recipe_id,"xsh_scired") != NULL ||
2521 strstr(recipe_id,"xsh_util_ifu") != NULL ) {
2522 xsh_dfs_fix_key_start_end(frameset,product_pre->data_header);
2523 }
2524
2525 /* PIPE-8857 - compute a TEXPTIME for MERGE1D and MERGE2D products */
2526// cpl_error_code error = CPL_ERROR_NONE;
2527// cpl_errorstate prestate;
2528// prestate = cpl_errorstate_get();
2529 double exptime = xsh_calculate_exptime(frameset,
2530 xsh_instrument_get_arm(instr));
2531 if (! isnan(exptime) && exptime > 0.) {
2532// xsh_msg_warning("I am attempting a TEXPTIME of %f", exptime) ;
2533 cpl_propertylist_append_double(product_pre->data_header, "TEXPTIME", exptime) ;
2534// } else if (! cpl_errorstate_is_equal(prestate)) {
2535// error = cpl_error_get_code();
2536 }
2537
2538 // PIPE-10061 - barycentric correct the header, if requested
2539 bary_param = xsh_parameters_bary_corr_get(recipe_id, parameters);
2541 product_pre->data_header, bary_param);
2542
2543 // Only needed for IDP products, but as there's no other IDP specific requirements
2544 // for thses files we add it to all output.
2545 if(strstr(pro_catg,"SCI_SLIT_MERGE2D") || strstr(pro_catg,"TELL_SLIT_MERGE2D"))
2546 {
2547 cpl_propertylist_append_string(product_pre->data_header,XSH_SDP_KEYWORD_PRODCATG,XSH_ANC_SPECTRUM);
2548 }
2549
2550 /* Save product */
2551 if (xsh_time_stamp_get() ){
2552 time( &now );
2553 date = xsh_sdate_utc(&now);
2554 sprintf( product_id, "%s%s_%s", XSH_PRODUCT_PREFIX, pro_catg, date);
2555 }
2556 else {
2557 sprintf( product_id, "%s%s", XSH_PRODUCT_PREFIX, pro_catg);
2558 }
2559 product_name = xsh_stringcat_any( product_id, ".fits", (void*)NULL);
2560 xsh_plist_set_extra_keys(product_pre->data_header,"IMAGE","DATA","RMSE",
2561 "FLUX","ERRS","QUAL",0);
2562 xsh_pfits_set_extname( product_pre->errs_header, "ERRS");
2563 xsh_plist_set_extra_keys(product_pre->errs_header,"IMAGE","DATA","RMSE",
2564 "FLUX","ERRS","QUAL",1);
2565 xsh_pfits_set_extname( product_pre->qual_header, "QUAL");
2566 xsh_plist_set_extra_keys(product_pre->qual_header,"IMAGE","DATA","RMSE",
2567 "FLUX","ERRS","QUAL",2);
2568
2569 // PIPE-10061 - barycentric correct the additional headers
2571 product_pre->data_header,
2572 bary_param);
2574 product_pre->data_header,
2575 bary_param);
2576
2577 check( product_frame = xsh_pre_save( product_pre, product_name, pro_catg,0));
2578
2579
2580
2581 check(plist=cpl_propertylist_duplicate(product_pre->data_header));
2582
2583/*
2584 if(arcfile_found) {
2585
2586 check(cpl_propertylist_append_string(plist,"ARCFILE",arcfile));
2587 check(cpl_propertylist_set_comment(plist,"ARCFILE","Archive File Name"));
2588
2589 }
2590*/
2591 /* Add the new frame into the frameset */
2592 cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE);
2593 cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT);
2594 cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL);
2595 cpl_frame_set_tag( product_frame, pro_catg);
2596 cpl_frameset_insert( frameset, product_frame);
2597 xsh_add_product_file(product_name);
2598
2599 cleanup:
2600 if (cpl_error_get_code () != CPL_ERROR_NONE) {
2601 xsh_free_frame( &product_frame);
2602 product_frame = NULL;
2603 }
2604 XSH_FREE( date);
2605 XSH_FREE(bary_param);
2606 xsh_pre_free( &product_pre);
2607 xsh_free_propertylist(&plist);
2608 XSH_FREE( product_name);
2609 /* XSH_FREE( paf_name); */
2610 return;
2611}
2612
2613/*---------------------------------------------------------------------------*/
2625/*---------------------------------------------------------------------------*/
2626void
2627xsh_add_product_pre_3d( cpl_frame * frame,
2628 cpl_frameset * frameset,
2629 const cpl_parameterlist *parameters,
2630 const char *recipe_id,
2631 xsh_instrument* instr)
2632{
2633 const char * pro_catg = "";
2634 xsh_pre_3d * product_pre = NULL;
2635 cpl_frame * product_frame = NULL;
2636 char product_id[256];
2637 char * product_name = NULL;
2638 /* char * paf_name = NULL; */
2639 time_t now ;
2640 char * date = NULL;
2641
2642 /* Check entries */
2643 XSH_ASSURE_NOT_NULL( frame);
2644 XSH_ASSURE_NOT_NULL( frameset);
2645 XSH_ASSURE_NOT_NULL( parameters);
2646 XSH_ASSURE_NOT_NULL( recipe_id);
2647 XSH_ASSURE_NOT_NULL( instr);
2648
2649 check( pro_catg = cpl_frame_get_tag (frame));
2650
2651 assure( pro_catg != NULL, CPL_ERROR_NULL_INPUT,
2652 "Frame tag has not been set");
2653
2654 /* Load PRE */
2655 check( product_pre = xsh_pre_3d_load( frame));
2656
2657 /* update header */
2658 xsh_clean_header(product_pre->data_header);
2659 cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT);
2660 check( cpl_dfs_setup_product_header( product_pre->data_header,
2661 frame, frameset, parameters,
2662 recipe_id, instr->pipeline_id,
2663 instr->dictionary,NULL));
2664
2665 /* Save product */
2666 if (xsh_time_stamp_get() ){
2667 time( &now );
2668 date = xsh_sdate_utc(&now);
2669 sprintf( product_id, "%s%s_%s", XSH_PRODUCT_PREFIX, pro_catg, date);
2670 }
2671 else {
2672 sprintf( product_id, "%s%s", XSH_PRODUCT_PREFIX, pro_catg );
2673 }
2674 product_name = xsh_stringcat_any( product_id, ".fits", (void*)NULL);
2675 check( product_frame = xsh_pre_3d_save( product_pre, product_name, 0));
2676
2677 /* Save PAF
2678 paf_name = xsh_stringcat_any( product_id, ".paf", (void*)NULL);
2679 XSH_ASSURE_NOT_NULL( paf_name);
2680 check_msg( xsh_paf_save( instr, recipe_id,
2681 product_pre->data_header, paf_name,
2682 pro_catg ),
2683 "Cant save PAF file" ) ;
2684 */
2685 /* Add the new frame into the frameset */
2686 cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE);
2687 cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT);
2688 cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL);
2689 cpl_frame_set_tag( product_frame, pro_catg);
2690 cpl_frameset_insert( frameset, product_frame);
2691 xsh_add_product_file(product_name);
2692
2693 cleanup:
2694 if (cpl_error_get_code () != CPL_ERROR_NONE) {
2695 xsh_free_frame( &product_frame);
2696 product_frame = NULL;
2697 }
2698 XSH_FREE( date);
2699 xsh_pre_3d_free( &product_pre);
2700 XSH_FREE( product_name);
2701 /* XSH_FREE( paf_name); */
2702 return;
2703}
2704
2715void
2716xsh_add_product_bpmap( cpl_frame *frame,
2717 cpl_frameset * frameset,
2718 const cpl_parameterlist * parameters,
2719 const char *recipe_id,
2721 const char *final_prefix )
2722{
2723 cpl_propertylist *primary_header = NULL;
2724 const char* pro_catg=NULL;
2725 cpl_frame* product_frame = NULL;
2726 cpl_image *image = NULL ;
2727 const char *fname = NULL;
2728 char *final_name = NULL ;
2729 char* date = NULL ;
2730 /* char *paf_name = NULL ; */
2731
2732
2733 /* Check input parameters */
2734 XSH_ASSURE_NOT_NULL(frame);
2735 XSH_ASSURE_NOT_NULL(frameset);
2736 XSH_ASSURE_NOT_NULL(parameters);
2737 XSH_ASSURE_NOT_NULL(recipe_id);
2739 //XSH_ASSURE_NOT_NULL(final_prefix);
2740
2741 /* set the procatg */
2742 pro_catg=xsh_create_final_tag(final_prefix,instrument,frame);
2743
2744 //check(pro_catg = cpl_frame_get_tag (frame));
2745 XSH_ASSURE_NOT_NULL(pro_catg);
2746
2747 /* Load header and image of bpmap frame */
2748 check(fname = cpl_frame_get_filename( frame ));
2749 check(primary_header = cpl_propertylist_load( fname, 0 ));
2750 check(image = cpl_image_load( fname, CPL_TYPE_INT, 0, 0 ));
2751 xsh_clean_header(primary_header);
2752 /* update header */
2753 check( cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT));
2754 check_msg (cpl_dfs_setup_product_header ( primary_header,
2755 frame,frameset,parameters,
2756 recipe_id,
2758 instrument->dictionary,NULL),
2759 "Problem in the product DFS-compliance");
2760
2761
2762 /* Create final name */
2763 final_name=xsh_create_final_name(pro_catg);
2764 xsh_msg_dbg_low( "Final product name: %s", final_name ) ;
2765
2766 /* Now save file with updated header */
2767 cpl_image_save( image, final_name, CPL_BPP_32_SIGNED, primary_header,
2768 CPL_IO_DEFAULT ) ;
2769
2770 /*
2771 Save PAF
2772
2773 paf_name = xsh_stringcat_any( final_prefix, ".paf", (void*)NULL ) ;
2774 XSH_ASSURE_NOT_NULL( paf_name ) ;
2775 check_msg( xsh_paf_save( instrument, recipe_id,
2776 primary_header, paf_name, pro_catg ),
2777 "Cant save PAF file" ) ;
2778 */
2779 /* Create product frame */
2780 check(product_frame=xsh_frame_product(final_name,pro_catg,
2781 CPL_FRAME_TYPE_IMAGE,
2782 CPL_FRAME_GROUP_PRODUCT,
2783 CPL_FRAME_LEVEL_FINAL));
2784
2785 /*
2786 XSH_NEW_FRAME(product_frame);
2787
2788 check(cpl_frame_set_filename (product_frame, final_name));
2789 check(cpl_frame_set_type (product_frame, CPL_FRAME_TYPE_IMAGE));
2790
2791 // Add the new frame into the frameset
2792 check(cpl_frame_set_group (product_frame, CPL_FRAME_GROUP_PRODUCT));
2793 check(cpl_frame_set_level (product_frame, CPL_FRAME_LEVEL_FINAL));
2794 check(cpl_frame_set_tag (product_frame, pro_catg));
2795 */
2796
2797 check(cpl_frameset_insert(frameset, product_frame));
2798 xsh_add_product_file(final_name);
2799
2800 cleanup:
2801 XSH_FREE(date);
2802 xsh_free_propertylist(&primary_header);
2803 xsh_free_image( &image ) ;
2804 XSH_FREE( final_name ) ;
2805 if(final_prefix!=NULL) {
2806 xsh_free(pro_catg);
2807 }
2808 /* XSH_FREE( paf_name ) ; */
2809 return;
2810}
2811
2812/*****************************************************************************/
2823/*****************************************************************************/
2824void
2825xsh_add_product_imagelist( cpl_frame *frame,
2826 cpl_frameset *frameset,
2827 const cpl_parameterlist *parameters,
2828 const char *recipe_id,
2830 const char *final_prefix)
2831{
2832 cpl_propertylist *primary_header = NULL;
2833 const char *pro_catg = NULL;
2834 cpl_frame* product_frame = NULL;
2835 const char *fname = NULL;
2836 char *final_name = NULL ;
2837 char* date = NULL ;
2838 int nbext=0;
2839 int i=0;
2840 char cmd[256];
2841 //char arcfile[256];
2842
2843 //bool arcfile_found=false;
2844 int extension =0;
2845 cpl_imagelist* ext_iml=NULL;
2846 cpl_propertylist* ext_header=NULL;
2847
2848 /* char *paf_name = NULL ; */
2849
2850 /* Check input parameters */
2851 XSH_ASSURE_NOT_NULL(frame);
2852 XSH_ASSURE_NOT_NULL(frameset);
2853 XSH_ASSURE_NOT_NULL(parameters);
2854 XSH_ASSURE_NOT_NULL(recipe_id);
2856
2857 /* set the procatg */
2858 pro_catg=xsh_create_final_tag(final_prefix,instrument,frame);
2859
2860 /* Load header and imagelist of bpmap frame */
2861 check(fname = cpl_frame_get_filename( frame ));
2862 nbext = cpl_frame_get_nextensions( frame);
2863 check(primary_header = cpl_propertylist_load( fname, 0 ));
2864 check(ext_iml = cpl_imagelist_load( fname, CPL_TYPE_FLOAT, 0 ));
2865 /*
2866 if(cpl_propertylist_has(primary_header,"ARCFILE")) {
2867 sprintf(arcfile,"%s",xsh_pfits_get_arcfile(primary_header));
2868 arcfile_found=true;
2869 }
2870 */
2871 xsh_clean_header(primary_header);
2872 /* update header */
2873 check( cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT));
2874 check_msg ( cpl_dfs_setup_product_header ( primary_header,
2875 frame,frameset,parameters,
2876 recipe_id,
2878 instrument->dictionary,NULL),
2879 "Problem in the product DFS-compliance");
2880
2881 /* Create final name */
2882 final_name=xsh_create_final_name(pro_catg);
2883 xsh_msg( "Final product name: %s", final_name ) ;
2884
2885 if(strcmp(fname,final_name) == 0) {
2886 sprintf(cmd,"mv %s tmp_ima.fits",fname);
2887 system(cmd);
2888 fname="tmp_ima.fits";
2889 xsh_add_temporary_file("tmp_ima.fits");
2890 }
2891
2892 /* Now save file with updated header */
2893 xsh_pfits_set_pcatg(primary_header,pro_catg);
2894 check(cpl_imagelist_save(ext_iml, final_name, CPL_BPP_IEEE_FLOAT,
2895 primary_header,CPL_IO_DEFAULT));
2896
2897 for( i = 0 ; i<=nbext ; i++ ) {
2898 xsh_free_imagelist( &ext_iml) ;
2899 xsh_free_propertylist( &ext_header) ;
2900 check(ext_iml = cpl_imagelist_load( fname, CPL_TYPE_FLOAT, i ));
2901 check(ext_header = cpl_propertylist_load( fname ,i));
2902
2903 if ( i == 0 ) {
2904 extension = CPL_IO_DEFAULT ;
2905 check(cpl_imagelist_save(ext_iml, final_name, CPL_BPP_IEEE_FLOAT,
2906 primary_header,extension));
2907 }
2908 else {
2909 extension = CPL_IO_EXTEND ;
2910 check(cpl_imagelist_save(ext_iml, final_name, CPL_BPP_IEEE_FLOAT,
2911 ext_header,extension));
2912 }
2913 }
2914 /*
2915 if(arcfile_found) {
2916 check(cpl_propertylist_append_string(primary_header,"ARCFILE",arcfile));
2917 check(cpl_propertylist_set_comment(primary_header,"ARCFILE",
2918 "Archive File Name"));
2919 }
2920 */
2921 /* Create product frame */
2922 check(product_frame=xsh_frame_product(final_name,pro_catg,
2923 CPL_FRAME_TYPE_IMAGE,
2924 CPL_FRAME_GROUP_PRODUCT,
2925 CPL_FRAME_LEVEL_FINAL));
2926
2927 if(
2928 strstr(recipe_id,"xsh_respon") != NULL ||
2929 strstr(recipe_id,"xsh_scired") != NULL ||
2930 strstr(recipe_id,"xsh_util_ifu") != NULL ) {
2931 xsh_dfs_fix_key_start_end(frameset,primary_header);
2932 }
2933
2934 check(cpl_frameset_insert(frameset, product_frame));
2935 xsh_add_product_file(final_name);
2936
2937 cleanup:
2938 xsh_free_imagelist( &ext_iml) ;
2939 xsh_free_propertylist( &ext_header) ;
2940
2941 if(final_prefix!=NULL) {
2942 xsh_free(pro_catg);
2943 }
2944 XSH_FREE(date);
2945 xsh_free_propertylist(&primary_header);
2946 xsh_free_imagelist( &ext_iml ) ;
2947 XSH_FREE( final_name ) ;
2948
2949 return;
2950}
2951/*****************************************************************************/
2952/*****************************************************************************/
2963/*****************************************************************************/
2964void
2965xsh_add_product_image( cpl_frame *frame,
2966 cpl_frameset *frameset,
2967 const cpl_parameterlist *parameters,
2968 const char *recipe_id,
2970 const char *final_prefix)
2971{
2972 cpl_propertylist *primary_header = NULL;
2973 const char *pro_catg = NULL;
2974 cpl_frame* product_frame = NULL;
2975 cpl_image *image = NULL ;
2976 const char *fname = NULL;
2977 char *final_name = NULL ;
2978 /* char *paf_name = NULL ; */
2979 int i=0;
2980 int nbext=0;
2981 //char arcfile[256];
2982 char cmd[256];
2983 //bool arcfile_found=false;
2984 xsh_bary_corr_param* bary_param ;
2985
2986 /* Check input parameters */
2987 XSH_ASSURE_NOT_NULL(frame);
2988 XSH_ASSURE_NOT_NULL(frameset);
2989 XSH_ASSURE_NOT_NULL(parameters);
2990 XSH_ASSURE_NOT_NULL(recipe_id);
2992
2993 /* set the procatg */
2994 pro_catg=xsh_create_final_tag(final_prefix,instrument,frame);
2995
2996 /* Load header and image of bpmap frame */
2997 check( fname = cpl_frame_get_filename( frame));
2998 nbext = cpl_frame_get_nextensions( frame);
2999 //xsh_msg("fname=%s",fname);
3000 check( primary_header = cpl_propertylist_load( fname, 0));
3001 check( image = cpl_image_load( fname, CPL_TYPE_FLOAT, 0, 0));
3002 /*
3003 if(cpl_propertylist_has(primary_header,"ARCFILE")) {
3004 sprintf(arcfile,"%s",xsh_pfits_get_arcfile(primary_header));
3005 arcfile_found=true;
3006 }
3007 */
3008
3009 /* update header */
3010 check( cpl_frame_set_group( frame, CPL_FRAME_GROUP_PRODUCT));
3011 check_msg (cpl_dfs_setup_product_header ( primary_header,
3012 frame,frameset,parameters,
3013 recipe_id,
3015 instrument->dictionary,NULL),
3016 "Problem in the product DFS-compliance");
3017
3018 xsh_msg("recipe_id=%s",recipe_id);
3019 if(
3020 strstr(recipe_id,"xsh_respon") != NULL ||
3021 strstr(recipe_id,"xsh_scired") != NULL ||
3022 strstr(recipe_id,"xsh_util_ifu") != NULL ) {
3023 xsh_dfs_fix_key_start_end(frameset,primary_header);
3024 }
3025
3026 /* PIPE-8857 - compute a TEXPTIME for MERGE1D and MERGE2D products */
3027// cpl_error_code error = CPL_ERROR_NONE;
3028// cpl_errorstate prestate;
3029// prestate = cpl_errorstate_get();
3030 double exptime = xsh_calculate_exptime(frameset,
3032 if (! isnan(exptime) && exptime > 0.) {
3033 xsh_msg_warning("I am attempting a TEXPTIME of %f", exptime) ;
3034 cpl_propertylist_append_double(primary_header, "TEXPTIME", exptime) ;
3035// } else if (! cpl_errorstate_is_equal(prestate)) {
3036// error = cpl_error_get_code();
3037 }
3038
3039 // PIPE-10061 - barycentric correct the header, if requested
3040 bary_param = xsh_parameters_bary_corr_get(recipe_id, parameters);
3041 xsh_baryvel_correct_header(primary_header,
3042 primary_header,
3043 bary_param);
3044
3045 /* Create final name */
3046 final_name=xsh_create_final_name(pro_catg);
3047 xsh_msg( "Final product name: %s", final_name ) ;
3048
3049 if(strcmp(fname,final_name) == 0) {
3050 sprintf(cmd,"mv %s tmp_ima.fits",fname);
3051 system(cmd);
3052 fname="tmp_ima.fits";
3053 xsh_add_temporary_file("tmp_ima.fits");
3054 }
3055 /* Now save file with updated header */
3056 xsh_pfits_set_pcatg(primary_header,pro_catg);
3057 xsh_plist_set_extra_keys(primary_header,"IMAGE","DATA","RMSE",
3058 "FLUX","ERRS","QUAL",0);
3059 xsh_clean_header(primary_header);
3060 check(cpl_image_save( image, final_name, CPL_BPP_IEEE_FLOAT, primary_header,
3061 CPL_IO_DEFAULT )) ;
3062
3063
3064
3065 for( i = 0 ; i<=nbext ; i++ ) {
3066 int extension ;
3067 cpl_image* ext_img=NULL;
3068 cpl_propertylist* ext_header=NULL;
3069
3070 check( ext_img = cpl_image_load( fname, CPL_TYPE_FLOAT,0, i));
3071 check( ext_header = cpl_propertylist_load( fname ,i));
3072
3073 // PIPE-10061 - Barycentric correct the remaining headers of this file
3074 xsh_baryvel_correct_header(ext_header, primary_header, bary_param);
3075
3076
3077 if ( i == 0 ) {
3078 extension = CPL_IO_DEFAULT ;
3079 xsh_pfits_set_pcatg(ext_header,pro_catg);
3080 }
3081 else {
3082 extension = CPL_IO_EXTEND ;
3083 check(xsh_plist_set_extra_keys(ext_header,"IMAGE","DATA","RMSE",
3084 "FLUX","ERRS","QUAL",i));
3085 check(cpl_propertylist_erase_regexp(ext_header,
3086 "^(ARCFILE|ORIGFILE|CHECKSUM|DATASUM|COMMENT)$", CPL_FALSE));
3087 check(cpl_image_save( ext_img, final_name, CPL_BPP_IEEE_FLOAT,ext_header,
3088 extension));
3089 }
3090 xsh_free_image( &ext_img) ;
3091 xsh_free_propertylist( &ext_header) ;
3092 }
3093 /* Save PAF
3094 paf_name = xsh_stringcat_any( final_prefix, ".paf", (void*)NULL);
3095 XSH_ASSURE_NOT_NULL( paf_name);
3096 */
3097 /*
3098 if(arcfile_found) {
3099 check(cpl_propertylist_append_string(primary_header,"ARCFILE",arcfile));
3100 check(cpl_propertylist_set_comment(primary_header,"ARCFILE",
3101 "Archive File Name"));
3102 }
3103 */
3104 /*
3105 check_msg( xsh_paf_save( instrument, recipe_id,
3106 primary_header, paf_name, pro_catg ),
3107 "Cant save PAF file" ) ;
3108 */
3109 /* Create product frame */
3110 check( product_frame = xsh_frame_product( final_name,pro_catg,
3111 CPL_FRAME_TYPE_IMAGE,
3112 CPL_FRAME_GROUP_PRODUCT,
3113 CPL_FRAME_LEVEL_FINAL));
3114
3115 check(cpl_frameset_insert(frameset, product_frame));
3116 cpl_msg_info(__func__, "Adding XSH product %s",
3117 final_name);
3118// getchar();
3119 xsh_add_product_file(final_name);
3120
3121 cleanup:
3122 if(final_prefix!=NULL) {
3123 xsh_free(pro_catg);
3124 }
3125 XSH_FREE(bary_param);
3126 xsh_free_propertylist( &primary_header);
3127 xsh_free_image( &image);
3128 XSH_FREE( final_name);
3129 /* XSH_FREE( paf_name); */
3130 return;
3131}
3132/*****************************************************************************/
3133
3134/*****************************************************************************/
3144/*****************************************************************************/
3145void
3146xsh_add_product_table( cpl_frame *frame,
3147 cpl_frameset *frameset,
3148 const cpl_parameterlist * parameters,
3149 const char *recipe_id,
3151 const char *final_prefix)
3152{
3153 cpl_propertylist *primary_header = NULL;
3154 cpl_propertylist *tbl_header = NULL;
3155 const char *pro_catg =NULL;
3156 cpl_frame* product_frame = NULL;
3157 cpl_table * table = NULL ;
3158 const char *fname = NULL;
3159 char *product_id = NULL;
3160 char *product_name = NULL ;
3161 char *tmp_name = NULL ;
3162 char* date = NULL ;
3163 /* char *paf_name = NULL ; */
3164 int nbext, i ;
3165
3166 /* Check input parameters */
3167 XSH_ASSURE_NOT_NULL(frame);
3168 XSH_ASSURE_NOT_NULL(frameset);
3169 XSH_ASSURE_NOT_NULL(parameters);
3170 XSH_ASSURE_NOT_NULL(recipe_id);
3172
3173 /* set the procatg */
3174 pro_catg=xsh_create_final_tag(final_prefix,instrument,frame);
3175
3176
3177 /* Load header and image of table frame */
3178 check( fname = cpl_frame_get_filename( frame));
3179 check( primary_header = cpl_propertylist_load( fname, 0));
3180 XSH_ASSURE_NOT_NULL( primary_header);
3181 check( tbl_header = cpl_propertylist_load( fname, 1));
3182 XSH_ASSURE_NOT_NULL( tbl_header);
3183 nbext = cpl_frame_get_nextensions( frame);
3184 xsh_clean_header(primary_header);
3185 /* update header */
3186 check( cpl_frame_set_group (frame, CPL_FRAME_GROUP_PRODUCT));
3187 check( cpl_dfs_setup_product_header ( primary_header,
3188 frame, frameset, parameters, recipe_id, instrument->pipeline_id,
3189 instrument->dictionary,NULL));
3190
3191
3192 if(
3193 strstr(recipe_id,"xsh_respon") != NULL ||
3194 strstr(recipe_id,"xsh_scired") != NULL ||
3195 strstr(recipe_id,"xsh_util_ifu") != NULL ) {
3196 xsh_dfs_fix_key_start_end(frameset,primary_header);
3197 }
3198
3199 /* Create final name */
3200 product_name=xsh_create_final_name(pro_catg);
3201
3202 /* Now save file with updated header */
3203 tmp_name=xsh_stringcat_any( "tmp_",product_name, (void*)NULL );
3204 for( i = 0 ; i<nbext ; i++ ) {
3205 int extension ;
3206 cpl_table *tbl_ext = NULL;
3207 cpl_propertylist *tbl_ext_header = NULL;
3208 /* Load table
3209 save table
3210 */
3211 check( tbl_ext = cpl_table_load( fname, i+1, 0));
3212 check( tbl_ext_header = cpl_propertylist_load( fname, i+1));
3213
3214 if ( i == 0 ) extension = CPL_IO_DEFAULT ;
3215 else extension = CPL_IO_EXTEND ;
3216 check(cpl_table_save( tbl_ext, primary_header, tbl_ext_header,
3217 tmp_name, extension));
3218 xsh_free_table( &tbl_ext);
3219 xsh_free_propertylist( &tbl_ext_header);
3220 }
3221
3222 xsh_fileutils_move( tmp_name,product_name );
3223
3224
3225 /* Create product frame */
3226 check(product_frame=xsh_frame_product(product_name,pro_catg,
3227 CPL_FRAME_TYPE_TABLE,
3228 CPL_FRAME_GROUP_PRODUCT,
3229 CPL_FRAME_LEVEL_FINAL));
3230
3231 cpl_frameset_insert(frameset, product_frame);
3232
3233
3234 cleanup:
3235 if(final_prefix!=NULL) {
3236 xsh_free(pro_catg);
3237 }
3238 xsh_free_propertylist(&primary_header);
3239 xsh_free_propertylist(&tbl_header);
3240 XSH_TABLE_FREE( table) ;
3241 XSH_FREE(product_id);
3242 XSH_FREE(tmp_name);
3243 XSH_FREE(product_name);
3244 XSH_FREE(date);
3245
3246 return;
3247}
3248/*****************************************************************************/
3249
3250/*---------------------------------------------------------------------------*/
3259/*---------------------------------------------------------------------------*/
3260
3261cpl_frame *
3262xsh_find_std_flux (cpl_frameset * frames)
3263{
3264 const char *tags[] = {
3277 NULL
3278 };
3279
3280 return xsh_find_frame (frames, tags);
3281}
3282/*---------------------------------------------------------------------------*/
3291/*---------------------------------------------------------------------------*/
3292
3293cpl_frame *
3294xsh_find_std_star_flux (cpl_frameset * frames)
3295{
3296 const char *tags[] = { XSH_STD_STAR_FLUX (XSH_ARM_UVB),
3299 NULL
3300 };
3301
3302 return xsh_find_frame (frames, tags);
3303}
3304
3305/*---------------------------------------------------------------------------*/
3316/*---------------------------------------------------------------------------*/
3317
3318cpl_frame*
3319xsh_find_master_bias (cpl_frameset * frames, xsh_instrument* instr)
3320{
3321 const char *tags[2] ={NULL,NULL};
3322 cpl_frame* result = NULL;
3323
3324 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_MASTER_BIAS, instr);
3325
3326 check(result = xsh_find_frame (frames,(const char**) tags));
3327
3328 cleanup:
3329 return result;
3330}
3331
3332/*---------------------------------------------------------------------------*/
3344/*---------------------------------------------------------------------------*/
3345
3346cpl_frame*
3347xsh_find_frame_with_tag (cpl_frameset * frames,
3348 const char* tag,
3349 xsh_instrument* instr)
3350{
3351 char *tags[2];
3352 cpl_frame* result = NULL;
3353
3354 tags[0] = xsh_get_tag_from_arm( tag, instr);
3355 tags[1] = NULL;
3356 xsh_msg_dbg_high("search for tag %s",tags[0]);
3357 result = xsh_find_frame (frames,(const char**) tags);
3358 cpl_free( tags[0]);
3359 return result;
3360}
3361
3362
3363/*---------------------------------------------------------------------------*/
3372/*---------------------------------------------------------------------------*/
3373
3374char* xsh_get_tag_from_arm(const char* tag, xsh_instrument* instr){
3375 const char* arm=xsh_instrument_arm_tostring(instr);
3376 char* composed_tag=NULL;
3377
3378 int len = strlen(tag);
3379
3380 if(tag[len-1]=='_') {
3381 composed_tag=cpl_sprintf("%s%s",tag,arm);
3382 } else {
3383 composed_tag=cpl_sprintf("%s%s%s",tag,"_",arm);
3384 }
3385 xsh_msg_dbg_high("composed tag='%s'",composed_tag);
3386
3387 return composed_tag;
3388}
3389
3390
3391/*---------------------------------------------------------------------------*/
3402/*---------------------------------------------------------------------------*/
3403
3404cpl_frame* xsh_find_master_dark (cpl_frameset * frames, xsh_instrument* instr)
3405{
3406 const char *tags[2];
3407
3408 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_MASTER_DARK, instr);
3409 tags[1] = NULL;
3410
3411 return xsh_find_frame (frames, tags);
3412}
3413
3414/*---------------------------------------------------------------------------*/
3425/*---------------------------------------------------------------------------*/
3426cpl_frame* xsh_find_master_flat (cpl_frameset * frames, xsh_instrument* instr)
3427{
3428 const char *tags[2];
3429
3430 tags[0] = XSH_GET_TAG_FROM_MODE ( XSH_MASTER_FLAT, instr);
3431 tags[1] = NULL;
3432
3433 return xsh_find_frame (frames, tags);
3434}
3435
3436/*---------------------------------------------------------------------------*/
3444/*---------------------------------------------------------------------------*/
3445cpl_frame *xsh_find_theo_tab_sing( cpl_frameset *frames, xsh_instrument* instr)
3446{
3447 const char* tags[2] = {NULL, NULL};
3448 cpl_frame* result = NULL;
3449
3450 XSH_ASSURE_NOT_NULL( frames);
3451 XSH_ASSURE_NOT_NULL( instr);
3452 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_THEO_TAB_SING, instr);
3453 check(result = xsh_find_frame (frames, tags));
3454
3455 cleanup:
3456 return result;
3457}
3458
3459/*---------------------------------------------------------------------------*/
3467/*---------------------------------------------------------------------------*/
3468cpl_frame *xsh_find_theo_tab_mult( cpl_frameset *frames, xsh_instrument* instr)
3469{
3470 const char *tags[2] = {NULL, NULL};
3471 cpl_frame* result = NULL;
3472
3473 XSH_ASSURE_NOT_NULL( frames);
3474 XSH_ASSURE_NOT_NULL( instr);
3475 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_THEO_TAB_MULT, instr);
3476 check(result = xsh_find_frame (frames, tags));
3477
3478 cleanup:
3479 return result;
3480}
3481/*---------------------------------------------------------------------------*/
3489/*---------------------------------------------------------------------------*/
3490cpl_frame *xsh_find_theo_tab_mode( cpl_frameset *frames, xsh_instrument* instr)
3491{
3492 const char *tags[2] = {NULL, NULL};
3493 cpl_frame* result = NULL;
3494
3495 XSH_ASSURE_NOT_NULL( frames);
3496 XSH_ASSURE_NOT_NULL( instr);
3497
3498 if ( instr->mode == XSH_MODE_IFU){
3499 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_THEO_TAB_IFU, instr);
3500 }
3501 else{
3502 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_THEO_TAB_SING, instr);
3503 }
3504 check(result = xsh_find_frame (frames, tags));
3505
3506 cleanup:
3507 return result;
3508}
3509
3510/*---------------------------------------------------------------------------*/
3518/*---------------------------------------------------------------------------*/
3519cpl_frame *
3520xsh_find_order_tab_recov( cpl_frameset *frames, xsh_instrument* instr)
3521{
3522 const char *tags[2] = {NULL, NULL};
3523 cpl_frame* result = NULL;
3524
3525 XSH_ASSURE_NOT_NULL( frames);
3526 XSH_ASSURE_NOT_NULL( instr);
3527
3528 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_ORDER_TAB_RECOV, instr);
3529 check( result = xsh_find_frame (frames, tags));
3530
3531 cleanup:
3532 return result;
3533}
3534
3535/*---------------------------------------------------------------------------*/
3543/*---------------------------------------------------------------------------*/
3544cpl_frame *
3545xsh_find_order_tab_guess( cpl_frameset *frames, xsh_instrument* instr)
3546{
3547 const char *tags[2] = {NULL, NULL};
3548 cpl_frame* result = NULL;
3549
3550 XSH_ASSURE_NOT_NULL( frames);
3551 XSH_ASSURE_NOT_NULL( instr);
3552
3553 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_ORDER_TAB_GUESS, instr);
3554 check(result = xsh_find_frame (frames, tags));
3555
3556 cleanup:
3557 return result;
3558}
3559
3560/*---------------------------------------------------------------------------*/
3568/*---------------------------------------------------------------------------*/
3569cpl_frame *
3570xsh_find_order_tab_centr( cpl_frameset *frames, xsh_instrument* instr)
3571{
3572 const char *tags[2] = {NULL, NULL};
3573 cpl_frame* result = NULL;
3574
3575 XSH_ASSURE_NOT_NULL( frames);
3576 XSH_ASSURE_NOT_NULL( instr);
3577
3578 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_ORDER_TAB_CENTR, instr);
3579 check(result = xsh_find_frame (frames, tags));
3580
3581 cleanup:
3582 return result;
3583}
3584
3585/*---------------------------------------------------------------------------*/
3593/*---------------------------------------------------------------------------*/
3594cpl_frame *
3595xsh_find_order_tab_edges( cpl_frameset *frames, xsh_instrument* instr)
3596{
3597 const char *tags[3] = {NULL, NULL};
3598 cpl_frame* result = NULL;
3599
3600 XSH_ASSURE_NOT_NULL( frames);
3601 XSH_ASSURE_NOT_NULL( instr);
3602
3603 tags[0] = XSH_GET_TAG_FROM_MODE( XSH_ORDER_TAB_AFC, instr);
3604 tags[1] = XSH_GET_TAG_FROM_LAMP( XSH_ORDER_TAB_EDGES, instr);
3605
3606 check(result = xsh_find_frame (frames, tags));
3607
3608 xsh_msg_dbg_medium( "ORDER_TAB => %s", cpl_frame_get_filename( result));
3609 xsh_msg( "Use order tab: %s", cpl_frame_get_tag( result));
3610
3611 cleanup:
3612 return result;
3613}
3614
3615/*---------------------------------------------------------------------------*/
3623/*---------------------------------------------------------------------------*/
3624cpl_frame *
3625xsh_find_wave_tab_guess( cpl_frameset *frames, xsh_instrument* instr)
3626{
3627 const char *tags[2] = {NULL, NULL};
3628 cpl_frame* result = NULL;
3629
3630 XSH_ASSURE_NOT_NULL( frames);
3631 XSH_ASSURE_NOT_NULL( instr);
3632
3633 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_GUESS, instr);
3634 check(result = xsh_find_frame (frames, tags));
3635
3636 cleanup:
3637 return result;
3638}
3639
3640/*---------------------------------------------------------------------------*/
3648/*---------------------------------------------------------------------------*/
3649cpl_frame *
3650xsh_find_wave_tab_2d( cpl_frameset *frames, xsh_instrument* instr)
3651{
3652 const char *tags[2] = {NULL, NULL};
3653 cpl_frame* result = NULL;
3654
3655 XSH_ASSURE_NOT_NULL( frames);
3656 XSH_ASSURE_NOT_NULL( instr);
3657
3658 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_2D, instr);
3659 check(result = xsh_find_frame (frames, tags));
3660
3661 cleanup:
3662 return result;
3663}
3664
3665/*---------------------------------------------------------------------------*/
3673cpl_frame *xsh_find_slitmap( cpl_frameset *frames,
3674 xsh_instrument* instr)
3675{
3676 const char *tags[3] = {NULL, NULL, NULL};
3677 cpl_frame* result = NULL;
3678
3679 XSH_ASSURE_NOT_NULL( frames);
3680 XSH_ASSURE_NOT_NULL( instr);
3681
3682 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_SLIT_MAP, instr);
3683 tags[1] = XSH_GET_TAG_FROM_ARM( XSH_IFU_MAP, instr);
3684 check(result = xsh_find_frame (frames, tags));
3685
3686 cleanup:
3687 return result;
3688}
3689
3690/*---------------------------------------------------------------------------*/
3698/*---------------------------------------------------------------------------*/
3699cpl_frame *xsh_find_wave_tab( cpl_frameset *frames,
3700 xsh_instrument* instr)
3701{
3702 const char *tags[4] = {NULL, NULL};
3703 cpl_frame* result = NULL;
3704
3705 XSH_ASSURE_NOT_NULL( frames);
3706 XSH_ASSURE_NOT_NULL( instr);
3707
3708 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_AFC, instr);
3709 tags[1] = XSH_GET_TAG_FROM_MODE( XSH_WAVE_TAB_ARC, instr);
3710 tags[2] = XSH_GET_TAG_FROM_ARM( XSH_WAVE_TAB_2D, instr);
3711 check(result = xsh_find_frame (frames, tags));
3712
3713 cleanup:
3714 return result;
3715}
3716
3717
3718/*---------------------------------------------------------------------------*/
3726/*---------------------------------------------------------------------------*/
3727cpl_frame *
3728xsh_find_model_config( cpl_frameset *frames, xsh_instrument* instr)
3729{
3730 const char *tags[4] = {NULL, NULL};
3731 cpl_frame* result = NULL;
3732
3733 XSH_ASSURE_NOT_NULL( frames);
3734 XSH_ASSURE_NOT_NULL( instr);
3735
3736 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_MOD_CFG_OPT_AFC, instr);
3737 tags[1] = XSH_GET_TAG_FROM_ARM( XSH_MOD_CFG_OPT_2D, instr);
3738 tags[2] = XSH_GET_TAG_FROM_ARM( XSH_MOD_CFG_TAB, instr);
3739 check(result = xsh_find_frame (frames, tags));
3740
3741 cleanup:
3742 return result;
3743}
3744
3745/*---------------------------------------------------------------------------*/
3754/*---------------------------------------------------------------------------*/
3755cpl_frameset *
3757{
3758 const char * tags[3] = { NULL, NULL, NULL} ;
3759 cpl_frameset * result = NULL;
3760 cpl_frame * resframe = NULL ;
3761
3762 XSH_ASSURE_NOT_NULL( frames);
3764
3765 /* Create frameset */
3766 check( result = cpl_frameset_new() ) ;
3767
3768 /*
3769 If only one wave solution "XSH_WAVE_TAB_ARC", duplicat the frame 3
3770 times and set the correct tags for each of them
3771 else fail if the 3 wave solutions are not avaiable
3772 */
3775 resframe = xsh_find_frame (frames, tags) ;
3776 if (resframe != NULL ) {
3777 cpl_frame * down = NULL, * cen = NULL , * up = NULL ;
3778
3779 xsh_msg_warning( "Only one Wave Solution Frame %s, use it for the 3 slitlets",
3780 cpl_frame_get_filename( resframe)) ;
3781 check( down = cpl_frame_duplicate( resframe ) ) ;
3783 check( cpl_frame_set_tag( down, tags[0] ) ) ;
3784 check( cpl_frameset_insert( result, down ) ) ;
3785
3786 check( cen = cpl_frame_duplicate( resframe ) ) ;
3788 check( cpl_frame_set_tag( cen, tags[0] ) ) ;
3789 check( cpl_frameset_insert( result, cen ) ) ;
3790
3791 check( up = cpl_frame_duplicate( resframe ) ) ;
3793 check( cpl_frame_set_tag( up, tags[0] ) ) ;
3794 check( cpl_frameset_insert( result, up ) ) ;
3795 }
3796 else {
3797 xsh_msg( "Three wave solution found" ) ;
3798
3800 check(resframe = xsh_find_frame (frames, tags));
3801 check( cpl_frameset_insert( result, resframe ) ) ;
3802
3804 check(resframe = xsh_find_frame (frames, tags));
3805 check( cpl_frameset_insert( result, resframe ) ) ;
3806
3808 check(resframe = xsh_find_frame (frames, tags));
3809 check( cpl_frameset_insert( result, resframe ) ) ;
3810 }
3811
3812 cleanup:
3813 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
3814 cpl_frameset_delete( result ) ;
3815 return NULL ;
3816 }
3817 else return result;
3818}
3819/*---------------------------------------------------------------------------*/
3820
3821/*---------------------------------------------------------------------------*/
3830/*---------------------------------------------------------------------------*/
3831cpl_frameset *
3832xsh_find_offset_tab_ifu( cpl_frameset *frames, xsh_instrument* instr)
3833{
3834 cpl_frameset *result = NULL;
3835 const char *tags[2] = {NULL, NULL};
3836 cpl_frame *down = NULL;
3837 cpl_frame *cen = NULL;
3838 cpl_frame *up = NULL;
3839
3840 XSH_ASSURE_NOT_NULL( frames);
3841 XSH_ASSURE_NOT_NULL( instr);
3842
3843 /* Create frameset */
3844 check( result = cpl_frameset_new());
3845
3846 /* search down */
3847 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_OFFSET_DOWN_IFU, instr);
3848 check( down = xsh_find_frame (frames, tags));
3849 check( cpl_frameset_insert( result, cpl_frame_duplicate(down)));
3850
3851 /* search cen */
3852 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_OFFSET_CEN_IFU, instr);
3853 check( cen = xsh_find_frame (frames, tags));
3854 check( cpl_frameset_insert( result, cpl_frame_duplicate(cen)));
3855
3856 /* search up */
3857 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_OFFSET_UP_IFU, instr);
3858 check( up = xsh_find_frame (frames, tags));
3859 check( cpl_frameset_insert( result, cpl_frame_duplicate(up)));
3860
3861 cleanup:
3862 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
3863 xsh_msg_warning( "No complete OFFSET_TAB dataset is found." \
3864 " The rectified spectra will not be straightened and the resulting" \
3865 " datacube may not be aligned.");
3866 xsh_free_frameset( &result);
3867 }
3868 return result;
3869}
3870/*---------------------------------------------------------------------------*/
3871
3872/*---------------------------------------------------------------------------*/
3880/*---------------------------------------------------------------------------*/
3881cpl_frame *
3882xsh_find_arc_line_list_clean( cpl_frameset *frames, xsh_instrument* instr)
3883{
3884 const char *tags[2] = {NULL, NULL};
3885 cpl_frame* result = NULL;
3886
3887 XSH_ASSURE_NOT_NULL( frames);
3888 XSH_ASSURE_NOT_NULL( instr);
3889
3890 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_ARC_LINE_LIST, instr);
3891 check(result = xsh_find_frame (frames, tags));
3892
3893 cleanup:
3894 return result;
3895}
3896
3897
3898/*---------------------------------------------------------------------------*/
3906/*---------------------------------------------------------------------------*/
3907cpl_frame *
3908xsh_find_arc_line_list( cpl_frameset *frames, xsh_instrument* instr)
3909{
3910 const char *tags[2] = {NULL, NULL};
3911 cpl_frame* result = NULL;
3912
3913 XSH_ASSURE_NOT_NULL( frames);
3914 XSH_ASSURE_NOT_NULL( instr);
3915
3916 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_ARC_LINE_LIST, instr);
3917 check(result = xsh_find_frame (frames, tags));
3918
3919 cleanup:
3920 return result;
3921}
3922/*---------------------------------------------------------------------------*/
3930/*---------------------------------------------------------------------------*/
3931cpl_frame *
3932xsh_find_usr_lines_guess_tab( cpl_frameset *frames, xsh_instrument* instr)
3933{
3934 const char *tags[2] = {NULL, NULL};
3935 cpl_frame* result = NULL;
3936
3937 XSH_ASSURE_NOT_NULL( frames);
3938 XSH_ASSURE_NOT_NULL( instr);
3939
3940 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_GUESS_LINES_POS, instr);
3941 check(result = xsh_find_frame (frames, tags));
3942
3943 cleanup:
3944 return result;
3945}
3946
3947/*---------------------------------------------------------------------------*/
3955/*---------------------------------------------------------------------------*/
3956cpl_frame *
3957xsh_find_model_config_tab( cpl_frameset *frames, xsh_instrument* instr)
3958{
3959 const char *tags[2] = {NULL, NULL};
3960 cpl_frame* result = NULL;
3961
3962 XSH_ASSURE_NOT_NULL( frames);
3963 XSH_ASSURE_NOT_NULL( instr);
3964
3965 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_MOD_CFG_TAB, instr);
3966 check(result = xsh_find_frame (frames, tags));
3967
3968 cleanup:
3969 return result;
3970}
3971
3982/*---------------------------------------------------------------------------*/
3983cpl_frame * xsh_find_wavemap(cpl_frameset *frames,
3984 xsh_instrument* instr)
3985{
3986 const char *tags[2] ={NULL,NULL};
3987 cpl_frame* result = NULL;
3988
3989 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_WAVE_MAP, instr);
3990 check(result = xsh_find_frame (frames,(const char**) tags));
3991
3992 cleanup:
3993 return result;
3994}
3995
4003/*---------------------------------------------------------------------------*/
4004
4005cpl_frame * xsh_find_raw_orderdef_vis_uvb ( cpl_frameset * frames )
4006{
4007 char *tags[4] ={NULL,NULL, NULL,NULL};
4008 cpl_frame* result = NULL;
4009 const char* tag=NULL;
4010 int nraw=0;
4011 check(tags[0] = xsh_stringcat_any( XSH_ORDERDEF_D2_UVB, (void*)NULL )) ;
4012 check(tags[1] = xsh_stringcat_any( XSH_ORDERDEF_QTH_UVB, (void*)NULL )) ;
4013 check(tags[2] = xsh_stringcat_any( XSH_ORDERDEF_VIS, (void*)NULL )) ;
4014
4015 check(result = xsh_find_frame (frames,(const char**) tags));
4016 tag=cpl_frame_get_tag(result);
4017 nraw=cpl_frameset_get_size(frames);
4018 if(nraw>1){
4019 /* this can happen only in UVB */
4020 if(strcmp(tag,XSH_ORDERDEF_D2_UVB)==0) {
4021 cpl_frameset_erase(frames,XSH_ORDERDEF_QTH_UVB );
4022 } else if(strcmp(tag,XSH_ORDERDEF_QTH_UVB)==0){
4023 cpl_frameset_erase(frames,XSH_ORDERDEF_D2_UVB );
4024 }
4025 }
4026 xsh_msg("Use orderdef frame %s",tag);
4027 cleanup:
4028 cpl_free( tags[0]);
4029 cpl_free( tags[1]);
4030 cpl_free( tags[2]);
4031 return result;
4032}
4033
4041/*---------------------------------------------------------------------------*/
4042
4043cpl_frame * xsh_find_raw_orderdef_nir( cpl_frameset * frames )
4044{
4045 char *tags[] ={NULL,NULL};
4046 cpl_frame* result = NULL;
4047
4048 check(tags[0] = xsh_stringcat_any( XSH_ORDERDEF_NIR, (void*)NULL ) );
4049 check(result = xsh_find_frame (frames,(const char**) tags));
4050
4051 cleanup:
4052 cpl_free(tags[0]);
4053 return result;
4054}
4055
4056/*---------------------------------------------------------------------------*/
4065/*---------------------------------------------------------------------------*/
4066cpl_frame* xsh_find_disp_tab( cpl_frameset *frames,
4067 xsh_instrument* instr)
4068{
4069 const char *tags[3] ={NULL,NULL};
4070 cpl_frame* result = NULL;
4071
4072 tags[0] = XSH_GET_TAG_FROM_ARM( XSH_DISP_TAB_AFC, instr);
4073 tags[1] = XSH_GET_TAG_FROM_ARM( XSH_DISP_TAB, instr);
4074
4075 check( result = xsh_find_frame( frames,(const char**) tags));
4076
4077 if (result == NULL){
4078 xsh_msg( "No DISP TAB frame found !!");
4079 } else {
4080 xsh_msg( "Use DISP TAB %s",cpl_frame_get_tag(result));
4081 }
4082
4083 cleanup:
4084 return result;
4085}
4086/*---------------------------------------------------------------------------*/
4087
4088
4096/*---------------------------------------------------------------------------*/
4097
4098cpl_frame * xsh_find_raw_orderdef_nir_off( cpl_frameset * frames )
4099{
4100 char *tags[] ={NULL,NULL};
4101 cpl_frame* result = NULL;
4102
4103 check(tags[0] = xsh_stringcat_any( XSH_ORDERDEF_OFF, (void*)NULL ) ) ;
4104 check(result = xsh_find_frame (frames,(const char**) tags));
4105
4106 cleanup:
4107 cpl_free(tags[0]);
4108 return result;
4109}
4110
4118cpl_frame * xsh_find_raw_arc_slit_uvb_vis( cpl_frameset * frames,
4119 XSH_ARM arm )
4120{
4121 char *tags[] ={NULL,NULL};
4122 cpl_frame* result = NULL;
4123
4124 if ( arm == XSH_ARM_UVB )
4125 check( tags[0] = xsh_stringcat_any( XSH_ARC_SLIT_UVB, (void*)NULL ) ) ;
4126 else if ( arm == XSH_ARM_VIS )
4127 check( tags[0] = xsh_stringcat_any( XSH_ARC_SLIT_VIS, (void*)NULL ) ) ;
4128 else goto cleanup ;
4129
4130 check(result = xsh_find_frame (frames,(const char**) tags));
4131
4132 cleanup:
4133 cpl_free(tags[0]);
4134 return result;
4135}
4136
4144cpl_frame * xsh_find_raw_arc_slit_nir_on( cpl_frameset * frames )
4145{
4146 char *tags[] ={NULL,NULL};
4147 cpl_frame* result = NULL;
4148
4149 check(tags[0] = xsh_stringcat_any( XSH_ARC_SLIT_NIR, (void*)NULL ) ) ;
4150 check(result = xsh_find_frame (frames,(const char**) tags));
4151
4152 cleanup:
4153 cpl_free(tags[0]);
4154 return result;
4155}
4156
4164cpl_frame * xsh_find_raw_arc_slit_nir_off( cpl_frameset * frames )
4165{
4166 char *tags[] ={NULL,NULL};
4167 cpl_frame* result = NULL;
4168
4169 check(tags[0] = xsh_stringcat_any( XSH_ARC_SLIT_OFF, (void*)NULL ) ) ;
4170 check(result = xsh_find_frame (frames,(const char**) tags));
4171
4172 cleanup:
4173 cpl_free(tags[0]);
4174 return result;
4175}
4176
4177
4186cpl_frame * xsh_find_calpro_model_config( cpl_frameset * frames,
4187 xsh_instrument * instr )
4188{
4189 char *tags[] ={NULL,NULL};
4190 cpl_frame* result = NULL;
4191
4194 (void*)NULL ) ) ;
4195 xsh_msg_debug("tag=%s",tags[0]);
4196 check(result = xsh_find_frame (frames,(const char**) tags));
4197
4198 cleanup:
4199 cpl_free(tags[0]);
4200 return result;
4201}
4202
4203
4213cpl_frame * xsh_find_model_config_open( cpl_frameset * frames,
4214 xsh_instrument * instr )
4215{
4216 char *tags[] ={NULL,NULL};
4217 cpl_frame* result = NULL;
4218
4221 (void*)NULL ) ) ;
4222 xsh_msg_debug("tag=%s",tags[0]);
4223 check(result = xsh_find_frame (frames,(const char**) tags));
4224
4225 cleanup:
4226 cpl_free(tags[0]);
4227 return result;
4228}
4229
4230
4240cpl_frame * xsh_find_calpro_model_meas_coord( cpl_frameset * frames,
4241 xsh_instrument * instr )
4242{
4243 char *tags[] ={NULL,NULL};
4244 cpl_frame* result = NULL;
4245
4248 (void*)NULL ) ) ;
4249 check(result = xsh_find_frame (frames,(const char**) tags));
4250
4251 cleanup:
4252 cpl_free(tags[0]);
4253 return result;
4254}
4255
4265cpl_frame * xsh_find_model_wavelist( cpl_frameset * frames,
4266 xsh_instrument * instr )
4267{
4268 char *tags[] ={NULL,NULL};
4269 cpl_frame* result = NULL;
4270
4273 (void*)NULL ) ) ;
4274 check(result = xsh_find_frame (frames,(const char**) tags));
4275
4276 cleanup:
4277 cpl_free(tags[0]);
4278 return result;
4279}
4280
4281
4282
4292cpl_frame * xsh_find_model_testpar( cpl_frameset * frames,
4293 xsh_instrument * instr )
4294{
4295 char *tags[] ={NULL,NULL};
4296 cpl_frame* result = NULL;
4297
4300 (void*)NULL ) ) ;
4301 check(result = xsh_find_frame (frames,(const char**) tags));
4302
4303 cleanup:
4304 cpl_free(tags[0]);
4305 return result;
4306}
4307
4308
4318cpl_frame * xsh_find_spectral_format( cpl_frameset * frames,
4319 xsh_instrument * instr )
4320{
4321 char *tags[] ={NULL,NULL};
4322 cpl_frame* result = NULL;
4323 if(instr->arm == XSH_ARM_NIR) {
4324 if((result=cpl_frameset_find(frames,XSH_SPECTRAL_FORMAT_JH_NIR)) != NULL){
4325 return result;
4326 }
4327 }
4330 (void*)NULL ) ) ;
4331 check(result = xsh_find_frame (frames,(const char**) tags));
4332
4333 cleanup:
4334 cpl_free(tags[0]);
4335 return result;
4336}
4337
4338/*---------------------------------------------------------------------------*/
4348/*---------------------------------------------------------------------------*/
4349static cpl_frame *
4350xsh_find_frame( cpl_frameset *frames, const char *tags[])
4351{
4352 cpl_frame *frame = NULL;
4353 bool found = false;
4354 char *tags_string = NULL; /* Used for error message, only */
4355 char *temp = NULL;
4356 int i;
4357
4358 assure (frames != NULL, CPL_ERROR_NULL_INPUT, "Null frameset");
4359 assure (tags != NULL, CPL_ERROR_NULL_INPUT, "Null tags");
4360
4361 tags_string = xsh_stringdup (""); /* Message used for empty frame set */
4362
4363 for (i = 0; !found && tags[i] != NULL; i++) {
4364 /* Concatenate name to 'tags_string' */
4365 cpl_free (temp);
4366 temp = NULL;
4367 temp = xsh_stringdup (tags_string);
4368
4369 if (i == 0) {
4370 cpl_free (tags_string);
4371 tags_string = NULL;
4372 check (tags_string = xsh_stringdup (tags[i]));
4373 }
4374 else {
4375 cpl_free (tags_string);
4376 tags_string = NULL;
4377 check (tags_string = xsh_stringcat_3 (temp, ", ", tags[i]));
4378 }
4379
4380 /* Search for frame */
4381 frame = cpl_frameset_find (frames, tags[i]);
4382
4383 if (frame != NULL) {
4384 found = true;
4385 }
4386 }
4387 if(!found) {
4388 /*
4389 xsh_msg_warning("%d, Frameset does not contain any %s frame(s)",
4390 CPL_ERROR_DATA_NOT_FOUND,tags_string);
4391 */
4392 }
4393 cleanup:
4394 cpl_free (tags_string);
4395 cpl_free (temp);
4396
4397 return frame;
4398}
4399
4400cpl_propertylist*
4402
4403 cpl_propertylist* qc = NULL;
4404 const char* name = NULL;
4405 name = cpl_frame_get_filename(frm);
4406 qc = cpl_propertylist_load_regexp(name, 0, "^ESO QC *", 0);
4407 return qc;
4408}
4409
4410cpl_boolean
4412{
4413 cpl_boolean found;
4414 cpl_frame* frm1=0;
4415 cpl_frame* frm2=0;
4416 cpl_frame* frm3=0;
4417 cpl_frame* frm4=0;
4418
4423
4424 if(
4425 (frm1!=NULL) ||
4426 (frm2!=NULL) ||
4427 (frm3!=NULL) ||
4428 (frm4!=NULL)
4429 ) {
4430 found=CPL_TRUE;
4431 } else {
4432 found=CPL_FALSE;
4433 }
4434 return found;
4435}
4436
4437cpl_error_code
4438xsh_dfs_check_binning(cpl_frameset* set,cpl_frameset* calib)
4439{
4440
4441 cpl_frame* ref=NULL;
4442 cpl_frame* frm=NULL;
4443 const char* namer=NULL;
4444 const char* namef=NULL;
4445 const char* tagr=NULL;
4446 const char* tagf=NULL;
4447
4448
4449 cpl_propertylist* href=NULL;
4450 cpl_propertylist* head=NULL;
4451
4452 int ref_binx=0;
4453 int ref_biny=0;
4454 //int ref_naxis=0;
4455
4456 int frm_binx=0;
4457 int frm_biny=0;
4458 int frm_naxis=0;
4459
4460 int i=0;
4461 int ncal=0;
4462
4463 XSH_ASSURE_NOT_NULL_MSG(set,"Null input raw framest");
4464 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input cal framest");
4465
4466 check(ref=cpl_frameset_get_frame(set,0));
4467 namer=cpl_frame_get_filename(ref);
4468 tagr=cpl_frame_get_tag(ref);
4469
4470 href=cpl_propertylist_load(namer,0);
4471
4472 ref_binx=xsh_pfits_get_binx(href);
4473 ref_biny=xsh_pfits_get_biny(href);
4474 //ref_naxis=xsh_pfits_get_naxis(href);
4475
4476 ncal=cpl_frameset_get_size(calib);
4477
4478 for(i=0;i<ncal;i++) {
4479
4480 frm=cpl_frameset_get_frame(calib,i);
4481 namef=cpl_frame_get_filename(frm);
4482 head=cpl_propertylist_load(namef,0);
4483 frm_naxis=xsh_pfits_get_naxis(head);
4484
4485 if(frm_naxis == 2) {
4486 /* if the input frame is not a tab we do the check */
4487 frm_binx=xsh_pfits_get_binx(head);
4488 frm_biny=xsh_pfits_get_biny(head);
4489
4490 if(frm_binx != ref_binx ||
4491 frm_biny != ref_biny )
4492 {
4493 tagf=cpl_frame_get_tag(frm);
4494 xsh_msg_error("Calib frame %s (tag=%s, bin=%d,%d)",
4495 namef,tagf,frm_binx,frm_biny);
4496 xsh_msg_error("mismatch raw frame's bin %s (tag=%s, bin=%d,%d).",
4497 namer,tagr,ref_binx,ref_biny);
4498 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
4499 }
4500 }
4501 xsh_free_propertylist(&head);
4502
4503 }
4504 cleanup:
4505
4506 xsh_free_propertylist(&href);
4507 xsh_free_propertylist(&head);
4508 return cpl_error_get_code();
4509}
4510
4511
4512
4513static cpl_frame*
4514xsh_frame_pre_subsample(cpl_frame* frm_i, const int binx, const int biny,
4515 const int rescale,xsh_instrument* inst)
4516{
4517
4518 const char* name=NULL;
4519 xsh_pre* pre_i=NULL;
4520 xsh_pre* pre_o=NULL;
4521
4522 cpl_frame* frm_o=NULL;
4523 const char* tag=NULL;
4524
4525 const char* basename=NULL;
4526 char new_name[256];
4527 cpl_propertylist* plist=NULL;
4528 check(name=cpl_frame_get_filename(frm_i));
4529 check(tag=cpl_frame_get_tag(frm_i));
4530
4531 check(basename=xsh_get_basename(name));
4532
4533 sprintf(new_name,"fctx%d_fcty%d_%s",binx,biny,basename);
4534 pre_i=xsh_pre_load(frm_i,inst);
4535 xsh_msg("new_name=%s",new_name);
4536 plist=pre_i->data_header;
4538 pre_o=xsh_pre_subsample (pre_i, binx, biny, rescale,inst);
4542 pre_o->data_header=cpl_propertylist_duplicate(pre_i->data_header);
4543 pre_o->errs_header=cpl_propertylist_duplicate(pre_i->errs_header);
4544 pre_o->qual_header=cpl_propertylist_duplicate(pre_i->qual_header);
4545
4546 frm_o=xsh_pre_save(pre_o,new_name,tag,1);
4547 cpl_frame_set_filename(frm_o,new_name);
4548 cleanup:
4549
4550 xsh_pre_free(&pre_o);
4551 xsh_pre_free(&pre_i);
4552
4553 return frm_o;
4554}
4555
4556
4557
4558
4559static cpl_frameset*
4560xsh_correct_frameset_calib_bin(cpl_frameset* input, const int ref_binx,
4561 const int ref_biny,xsh_instrument* inst)
4562{
4563 cpl_frameset* correct=NULL;
4564 cpl_frame* frm=NULL;
4565 cpl_frame* frm_new=NULL;
4566 cpl_propertylist* plist=NULL;
4567
4568 const char* name=NULL;
4569 const char* tag=NULL;
4570 int inp_binx=1;
4571 int inp_biny=1;
4572 int i=0;
4573 int sz=0;
4574 int fctx=0;
4575 int fcty=0;
4576
4577 sz=cpl_frameset_get_size(input);
4578 correct=cpl_frameset_new();
4579 for(i=0;i<sz;i++) {
4580 frm=cpl_frameset_get_frame(input,i);
4581 name=cpl_frame_get_filename(frm);
4582 tag=cpl_frame_get_tag(frm);
4583 if(strstr(tag,"MASTER") != NULL) {
4584 plist=cpl_propertylist_load(name,0);
4585 inp_binx=xsh_pfits_get_binx(plist);
4586 inp_biny=xsh_pfits_get_biny(plist);
4587
4588 if(inp_binx < ref_binx || inp_biny < ref_biny) {
4589 xsh_msg("rescaling calib frame %s",cpl_frame_get_tag(frm));
4590 fctx=ref_binx/inp_binx;
4591 fcty=ref_biny/inp_biny;
4592 frm_new=xsh_frame_pre_subsample(frm,fctx,fcty,1,inst);
4593 check(cpl_frameset_insert(correct,frm_new));
4594 } else {
4595 /* input of proper binning */
4596 check(cpl_frameset_insert(correct,cpl_frame_duplicate(frm)));
4597 }
4598 } else {
4599 check(cpl_frameset_insert(correct,cpl_frame_duplicate(frm)));
4600 }
4601 xsh_free_propertylist(&plist);
4602
4603 }
4604
4605 cleanup:
4606 return correct;
4607}
4608
4609
4610
4611static cpl_frameset*
4612xsh_correct_frameset_raws_bin(cpl_frameset* input, const int ref_binx,
4613 const int ref_biny,xsh_instrument* inst)
4614{
4615 cpl_frameset* correct=NULL;
4616 cpl_frame* frm=NULL;
4617 cpl_frame* frm_new=NULL;
4618 cpl_propertylist* plist=NULL;
4619
4620 const char* name=NULL;
4621 int inp_binx=1;
4622 int inp_biny=1;
4623 int i=0;
4624 int sz=0;
4625 int fctx=0;
4626 int fcty=0;
4627
4628 sz=cpl_frameset_get_size(input);
4629 correct=cpl_frameset_new();
4630 for(i=0;i<sz;i++) {
4631 frm=cpl_frameset_get_frame(input,i);
4632 name=cpl_frame_get_filename(frm);
4633
4634 plist = cpl_propertylist_load(name, 0);
4635 inp_binx = xsh_pfits_get_binx(plist);
4636 inp_biny = xsh_pfits_get_biny(plist);
4637
4638
4639 xsh_msg("rescaling raw frame %s", cpl_frame_get_tag(frm));
4640
4641 fctx = ref_binx / inp_binx;
4642 fcty = ref_biny / inp_biny;
4643
4644 /* rescale flux */
4645 frm_new=xsh_frame_pre_subsample(frm,fctx,fcty,0,inst);
4646 check(cpl_frameset_insert(correct,frm_new));
4647
4648 xsh_free_propertylist(&plist);
4649
4650 }
4651
4652 cleanup:
4653 return correct;
4654}
4655/* in case the input master flat/bias do not match in bin size with the raw data,
4656 * correct the binning so that :
4657 *
4658 * 1x1 std 2x2 mflat,mbias --> correct bin of std
4659 * 2x2 std 1x1 mflat,mbias --> correct bin of mflat,mbias
4660 *
4661 */
4662
4663cpl_error_code
4664xsh_frameset_uniform_bin(cpl_frameset** raws, cpl_frameset** calib,
4666{
4667
4668 if((*raws != NULL) && (*calib != NULL) &&
4670 int raw_binx=0;
4671 int raw_biny=0;
4672 int cal_binx=0;
4673 int cal_biny=0;
4674 const char* name=NULL;
4675 cpl_propertylist* plist=NULL;
4676 //cpl_frameset_dump(*calib,stdout);
4677 //cpl_frameset_dump(*raws,stdout);
4678 //check(*calib=xsh_correct_calib(*raws,*calib));
4679 cpl_frame* raw_frm=NULL;
4680 cpl_frame* mflat=NULL;
4681 raw_frm=cpl_frameset_get_frame(*raws,0);
4682
4683 name=cpl_frame_get_filename(raw_frm);
4684 plist=cpl_propertylist_load(name,0);
4685 raw_binx=xsh_pfits_get_binx(plist);
4686 raw_biny=xsh_pfits_get_biny(plist);
4687 xsh_free_propertylist(&plist);
4688 //cpl_frameset_dump(*calib,stdout);
4689 if( (mflat = xsh_find_frame_with_tag(*calib,XSH_MASTER_FLAT_SLIT,instrument)) == NULL) {
4690 xsh_msg_error("Missing required input %s",XSH_GET_TAG_FROM_MODE ( XSH_MASTER_FLAT, instrument));
4691 cpl_error_set(cpl_func, CPL_ERROR_FILE_NOT_FOUND);
4692 goto cleanup;
4693 }
4694
4695 name=cpl_frame_get_filename(mflat);
4696 plist=cpl_propertylist_load(name,0);
4697 cal_binx=xsh_pfits_get_binx(plist);
4698 cal_biny=xsh_pfits_get_biny(plist);
4699 xsh_free_propertylist(&plist);
4700 /*
4701 xsh_msg("raw frame %s %s",cpl_frame_get_filename(raw_frm),cpl_frame_get_tag(raw_frm));
4702 xsh_msg("cal frame %s %s",cpl_frame_get_filename(cal_frm),cpl_frame_get_tag(cal_frm));
4703 xsh_msg("raw bin: %d %d",raw_binx,raw_biny);
4704 xsh_msg("cal bin: %d %d",cal_binx,cal_biny);
4705 */
4706 if ( raw_binx == cal_binx && raw_biny == cal_biny) {
4707 } else if ( raw_binx > cal_binx || raw_biny > cal_biny) {
4708 check(*calib=xsh_correct_frameset_calib_bin(*calib,raw_binx,raw_biny,instrument));
4709 } else {
4710
4711 //xsh_msg("cal bin=%d %d",cal_binx,cal_biny);
4712 check(*raws=xsh_correct_frameset_raws_bin(*raws,cal_binx,cal_biny,instrument));
4715
4716 }
4717 //check(xsh_dfs_check_binning(*raws,*calib));
4718 }
4719
4720 cleanup:
4721
4722 return cpl_error_get_code();
4723
4724}
4725
4726
4727cpl_frameset*
4728xsh_correct_calib(cpl_frameset* raws, cpl_frameset* calib){
4729 int raw_binx=1;
4730 int raw_biny=1;
4731 int cal_binx=1;
4732 int cal_biny=1;
4733 int fctx=0;
4734 int fcty=0;
4735
4736 cpl_propertylist* plist=NULL;
4737 const char* name=NULL;
4738 cpl_frame* frm=NULL;
4739 cpl_frame* frm_new=NULL;
4740 int i=0;
4741 int sz=0;
4742 const char* tag=NULL;
4743 cpl_frameset* calib_new=NULL;
4744
4745 check(frm=cpl_frameset_get_frame(raws,0));
4746 name=cpl_frame_get_filename(frm);
4747 plist=cpl_propertylist_load(name,0);
4748 raw_binx=xsh_pfits_get_binx(plist);
4749 raw_biny=xsh_pfits_get_biny(plist);
4750 xsh_free_propertylist(&plist);
4751 sz=cpl_frameset_get_size(calib);
4752
4753 calib_new=cpl_frameset_new();
4754 for(i=0;i<sz;i++) {
4755 frm=cpl_frameset_get_frame(calib,i);
4756 name=cpl_frame_get_filename(frm);
4757 tag=cpl_frame_get_tag(frm);
4758 if(strstr(tag,"MASTER") != NULL) {
4759 plist=cpl_propertylist_load(name,0);
4760 cal_binx=xsh_pfits_get_binx(plist);
4761 cal_biny=xsh_pfits_get_biny(plist);
4762
4763 if(cal_binx > raw_binx || cal_biny > raw_biny) {
4764 xsh_msg("rescaling cal frame %s",cpl_frame_get_tag(frm));
4765 fctx=cal_binx/raw_binx;
4766 fcty=cal_biny/raw_biny;
4767 frm_new=xsh_frame_image_mult_by_fct(frm,fctx,fcty);
4768 cpl_frameset_insert(calib_new,frm_new);
4769 } else if(cal_binx < raw_binx || cal_biny < raw_biny) {
4770 xsh_msg("rescaling cal frame %s",cpl_frame_get_tag(frm));
4771 fctx=raw_binx/cal_binx;
4772 fcty=raw_biny/cal_biny;
4773 frm_new=xsh_frame_image_div_by_fct(frm,fctx,fcty);
4774 check(cpl_frameset_insert(calib_new,frm_new));
4775 } else {
4776 /* input of proper binning */
4777 check(cpl_frameset_insert(calib_new,cpl_frame_duplicate(frm)));
4778 }
4779 } else {
4780 check(cpl_frameset_insert(calib_new,cpl_frame_duplicate(frm)));
4781 }
4782 xsh_free_propertylist(&plist);
4783
4784 }
4785
4786 cleanup:
4787 xsh_free_propertylist(&plist);
4788 xsh_free_frameset(&calib);
4789
4790 return calib_new;
4791
4792}
4793
4794static cpl_error_code
4795xsh_frame_nir_tab_chop_Kband(cpl_frame* frame, const int absordmin,
4796 const char* colname)
4797{
4798
4799 cpl_table* tab=NULL;
4800 cpl_table* ext=NULL;
4801 const char* name=NULL;
4802 const char* basename=NULL;
4803 char new_name[256];
4804 cpl_propertylist* head=NULL;
4805 cpl_propertylist* hext=NULL;
4806 int next=0;
4807 int i=0;
4808
4809 name=cpl_frame_get_filename(frame);
4810 basename=xsh_get_basename(name);
4811 sprintf(new_name,"cut_nir_HJ_%s",basename);
4812 next=cpl_frame_get_nextensions(frame);
4813
4814 head=cpl_propertylist_load(name,0);
4815 for(i=1;i<=next;i++) {
4816 tab=cpl_table_load(name,i,0);
4817 hext=cpl_propertylist_load(name,i);
4818 cpl_table_and_selected_int(tab,colname,CPL_GREATER_THAN,absordmin-1);
4819 ext=cpl_table_extract_selected(tab);
4820 if(i==1) {
4821 check(cpl_table_save(ext,head,hext,new_name,CPL_IO_DEFAULT));
4822 } else {
4823 check(cpl_table_save(ext,head,hext,new_name,CPL_IO_EXTEND));
4824 }
4825 xsh_free_propertylist(&hext);
4826 xsh_free_table(&tab);
4827 xsh_free_table(&ext);
4828 }
4829 cpl_frame_set_filename(frame,new_name);
4830 xsh_add_temporary_file(new_name);
4831
4832 cleanup:
4833 xsh_free_propertylist(&head);
4834 xsh_free_propertylist(&hext);
4835 xsh_free_table(&tab);
4836 xsh_free_table(&ext);
4837
4838 return cpl_error_get_code();
4839
4840}
4841
4842
4843
4844static cpl_error_code
4846{
4847 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4848 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4849
4850 cleanup:
4851 return cpl_error_get_code();
4852
4853}
4854
4855static cpl_error_code
4857{
4858 int absordmin=instr->config->order_min;
4859 cpl_frame* order_tab_guess=NULL;
4860 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4861 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4862
4863 if(NULL!= (order_tab_guess = xsh_find_frame_with_tag(calib,
4865 instr)) ) {
4866 xsh_frame_nir_tab_chop_Kband( order_tab_guess,absordmin,"ABSORDER");
4867 }
4868
4869
4870 cleanup:
4871 return cpl_error_get_code();
4872
4873}
4874
4875
4876
4877static cpl_error_code
4879{
4880 cpl_frame* cen_order_tab_frame = NULL;
4881 int absordmin=instr->config->order_min;
4882
4883 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4884 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4885
4886 check( cen_order_tab_frame = xsh_find_order_tab_centr( calib, instr));
4887
4888 xsh_frame_nir_tab_chop_Kband( cen_order_tab_frame,absordmin,"ABSORDER");
4889
4890 cleanup:
4891
4892 return cpl_error_get_code();
4893
4894}
4895
4896
4897static cpl_error_code
4899{
4900 cpl_frame* order_tab_edges = NULL;
4901 cpl_frame* theo_tab_mult = NULL;
4902 int absordmin=instr->config->order_min;
4903
4904 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4905 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4906
4907 check(order_tab_edges = xsh_find_order_tab_edges( calib, instr));
4908 xsh_frame_nir_tab_chop_Kband(order_tab_edges,absordmin,"ABSORDER");
4909 if(NULL!=(theo_tab_mult = xsh_find_frame_with_tag( calib,XSH_THEO_TAB_MULT,
4910 instr))) {
4911 xsh_frame_nir_tab_chop_Kband(theo_tab_mult,absordmin,"Order");
4912
4913 }
4914
4915 cleanup:
4916
4917 return cpl_error_get_code();
4918
4919}
4920
4921
4922
4923static cpl_error_code
4925{
4926 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4927 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4928
4929 cpl_frame* order_tab_edges=NULL;
4930 cpl_frame* disp_tab_frame=NULL;
4931 int absordmin=instr->config->order_min;
4932 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4933 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4934
4935 check(order_tab_edges = xsh_find_order_tab_edges( calib, instr));
4936 xsh_frame_nir_tab_chop_Kband(order_tab_edges,absordmin,"ABSORDER");
4937 if(NULL != (disp_tab_frame = xsh_find_disp_tab( calib, instr))) {
4938 xsh_frame_nir_tab_chop_Kband(disp_tab_frame,absordmin,"ORDER");
4939 }
4940
4941
4942 cleanup:
4943 return cpl_error_get_code();
4944
4945}
4946
4947
4948static cpl_error_code
4950{
4951
4952 cpl_frame* order_tab_edges=NULL;
4953 cpl_frame* disp_tab_frame=NULL;
4954 cpl_frame* wave_tab_frame=NULL;
4955 int absordmin=instr->config->order_min;
4956 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4957 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4958
4959 check(order_tab_edges = xsh_find_order_tab_edges( calib, instr));
4960 check(xsh_frame_nir_tab_chop_Kband(order_tab_edges,absordmin,"ABSORDER"));
4961 if(NULL != (disp_tab_frame = xsh_find_disp_tab( calib, instr))) {
4962 check(xsh_frame_nir_tab_chop_Kband(disp_tab_frame,absordmin,"ORDER"));
4963 }
4964 if(NULL != (wave_tab_frame = xsh_find_wave_tab( calib, instr))) {
4965 //check(xsh_frame_nir_tab_chop_Kband(wave_tab_frame,absordmin,"ORDER"));
4966 }
4967 cleanup:
4968 return cpl_error_get_code();
4969
4970}
4971
4972
4973cpl_error_code
4975{
4976 cpl_frame* edges_order_tab_frame = NULL;
4977 cpl_frame* spectral_format_frame = NULL;
4978 cpl_table* tab_edges=NULL;
4979
4980 const char* fname = NULL;
4981 double min=0;
4982
4983 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
4984 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
4985
4986 check( edges_order_tab_frame = xsh_find_order_tab_edges( calib, instr));
4987 fname=cpl_frame_get_filename(edges_order_tab_frame);
4988 tab_edges=cpl_table_load(fname,1,0);
4989 min=cpl_table_get_column_min(tab_edges,"ABSORDER");
4990
4991 if(min==13) {
4992 xsh_msg("entrato");
4993 instr->config->order_min=13;
4994 instr->config->order_max=26;
4995 instr->config->orders=14;
4996
4997 /* correct spectral format table */
4998 check( spectral_format_frame = xsh_find_spectral_format( calib, instr));
4999 xsh_frame_nir_tab_chop_Kband( spectral_format_frame,13,"ORDER");
5000
5001 /* correct other tables */
5003
5004
5005 }
5006
5007 cleanup:
5008 xsh_free_table(&tab_edges);
5009
5010 return cpl_error_get_code();
5011
5012}
5013cpl_error_code
5014xsh_calib_nir_corr_if_JH(cpl_frameset* calib,xsh_instrument* instr,
5015 const char* recid)
5016{
5017
5018 XSH_ASSURE_NOT_NULL_MSG(calib,"Null input calib par");
5019 XSH_ASSURE_NOT_NULL_MSG(instr,"Null input instr par");
5020 XSH_ASSURE_NOT_NULL_MSG(recid,"Null input recid par");
5021
5022 if( instr->arm == XSH_ARM_NIR &&
5023 instr->config->order_min==13 &&
5024 instr->config->order_max==26 &&
5025 instr->config->orders==14
5026 ) {
5027 /* this can happen only in NIR-JH */
5028
5029 if(strcmp(recid,"xsh_predict") == 0) {
5031 } else if(strcmp(recid,"xsh_orderpos") == 0) {
5033 } else if(strcmp(recid,"xsh_mflat") == 0) {
5035 } else if(strcmp(recid,"xsh_2dmap") == 0) {
5037 } else if(strcmp(recid,"xsh_wavecal") == 0) {
5039 } else if(strcmp(recid,"xsh_flexcomp") == 0) {
5041 } else if(strcmp(recid,"xsh_respon_slit_stare") == 0) {
5043 } else if(strcmp(recid,"xsh_respon_slit_offset") == 0) {
5045 } else if(strcmp(recid,"xsh_respon_slit_nod") == 0) {
5047 } else if(strcmp(recid,"xsh_scired_slit_stare") == 0) {
5049 } else if(strcmp(recid,"xsh_scired_slit_offset") == 0) {
5051 } else if(strcmp(recid,"xsh_scired_slit_nod") == 0) {
5053 }
5054
5055 }
5056 cleanup:
5057 return cpl_error_get_code();
5058
5059}
5060
5061cpl_error_code
5063{
5064
5065 cpl_frame* frm_mflat=NULL;
5066 cpl_frame* frm_mflat_new=NULL;
5067 int ref_binx=0;
5068 int ref_biny=0;
5069 int binx=0;
5070 int biny=0;
5071 int fctx=1;
5072 int fcty=1;
5073 cpl_propertylist* plist=NULL;
5074 const char* name=NULL;
5075 char tag[256];
5076
5077
5078 sprintf(tag,"%s_%s",XSH_MASTER_FLAT_SLIT,xsh_instrument_arm_tostring(inst));
5079 ref_binx=inst->binx;
5080 ref_biny=inst->biny;
5081 xsh_msg("tag=%s",tag);
5082 xsh_msg("binx=%d biny=%d",ref_binx,ref_biny);
5083 check(frm_mflat=cpl_frameset_find(*calib,tag));
5084 check(name=cpl_frame_get_filename(frm_mflat));
5085 plist=cpl_propertylist_load(name,0);
5086 binx=xsh_pfits_get_binx(plist);
5087 biny=xsh_pfits_get_biny(plist);
5088
5089 if(binx > ref_binx ||biny > ref_biny) {
5090 xsh_msg("compute syntetic frame by division");
5091 fctx=binx/ref_binx;
5092 fcty=biny/ref_biny;
5093 frm_mflat_new=xsh_frame_image_div_by_fct(frm_mflat,fctx,fcty);
5094 cpl_frameset_erase_frame(*calib,frm_mflat);
5095 cpl_frameset_insert(*calib,frm_mflat_new);
5096
5097 } else if(binx < ref_binx ||biny < ref_biny) {
5098 xsh_msg("compute syntetic frame by multiplication");
5099 fctx=ref_binx/binx;
5100 fcty=ref_biny/biny;
5101 frm_mflat_new=xsh_frame_image_mult_by_fct(frm_mflat,fctx,fcty);
5102 cpl_frameset_erase_frame(*calib,frm_mflat);
5103 cpl_frameset_insert(*calib,frm_mflat_new);
5104 } else {
5105 xsh_msg("keep same frame binx=%d ref_binx=%d biny=%d ref_biny=%d",
5106 binx,ref_binx,biny,ref_biny);
5107 /* do nothing, frames are proper */
5108 }
5109
5110
5111 cleanup:
5112 xsh_free_propertylist(&plist);
5113 return cpl_error_get_code();
5114
5115}
5116
5117
5118cpl_error_code
5120{
5121 int i=0;
5122 int sz=0;
5123 cpl_frame* frm=NULL;
5124 cpl_propertylist* plist=NULL;
5125 const char* name=NULL;
5126 double ref_exptime=0;
5127 double tmp_exptime=0;
5128 XSH_ARM arm;
5129 arm=xsh_instrument_get_arm(inst);
5130 const char* info=NULL;
5131 if(arm==XSH_ARM_NIR) {
5132 info="DIT";
5133 } else {
5134 info="EXPTIME";
5135 }
5136 sz=cpl_frameset_get_size(raws);
5137 for(i=0;i<sz;i++){
5138 check(frm=cpl_frameset_get_frame(raws,i));
5139 name=cpl_frame_get_filename(frm);
5140 plist=cpl_propertylist_load(name,0);
5141
5142 if(i==0) {
5143 if(arm==XSH_ARM_NIR) {
5144 ref_exptime=xsh_pfits_get_dit(plist);
5145 } else {
5146 ref_exptime=xsh_pfits_get_det_win1_uit1(plist);
5147 }
5148 tmp_exptime=ref_exptime;
5149 } else {
5150 if(arm==XSH_ARM_NIR) {
5151 tmp_exptime=xsh_pfits_get_dit(plist);
5152 } else {
5153 tmp_exptime=xsh_pfits_get_det_win1_uit1(plist);
5154 }
5155 if( fabs(tmp_exptime-ref_exptime) > 0.001) {
5156 xsh_msg_error("Some dark has %s different from others.",info);
5157 xsh_msg("%s(%d)=%g %s(0)=%g",info,i,tmp_exptime,info,ref_exptime);
5158 cpl_error_set(cpl_func, CPL_ERROR_ILLEGAL_INPUT);
5159 }
5160 }
5161 xsh_free_propertylist(&plist);
5162 }
5163
5164 cleanup:
5165 xsh_free_propertylist(&plist);
5166 return cpl_error_get_code();
5167}
5168
5169
5170cpl_error_code
5171xsh_table_save(cpl_table* t, cpl_propertylist* ph, cpl_propertylist* xh,
5172 const char* fname, const int ext) {
5173
5174 if (ext==0) {
5175 cpl_table_save(t,ph,xh,fname,CPL_IO_DEFAULT);
5176 } else {
5177 cpl_table_save(t,ph,xh,fname,CPL_IO_EXTEND);
5178 }
5179
5180 return cpl_error_get_code();
5181
5182}
5183
5184cpl_error_code
5185xsh_vector_save(cpl_vector* v, cpl_propertylist* ph, const char* fname,
5186 const int ext) {
5187
5188 if (ext==0) {
5189 cpl_vector_save(v,fname,CPL_BPP_IEEE_FLOAT,ph,CPL_IO_DEFAULT);
5190 } else {
5191 cpl_vector_save(v,fname,CPL_BPP_IEEE_FLOAT,ph,CPL_IO_EXTEND);
5192 }
5193
5194 return cpl_error_get_code();
5195
5196}
5197
5198cpl_error_code
5199xsh_add_afc_info(cpl_frame* frm_m, cpl_frame* frm_o) {
5200
5201 double yshift=0;
5202 double xshift=0;
5203 cpl_propertylist* head_m=NULL;
5204 cpl_propertylist* head_o=NULL;
5205 const char* name_m=NULL;
5206 const char* name_o=NULL;
5207 cpl_image* ima=NULL;
5208
5209 name_m=cpl_frame_get_filename(frm_m);
5210 name_o=cpl_frame_get_filename(frm_o);
5211 head_m=cpl_propertylist_load(name_m,0);
5212 head_o=cpl_propertylist_load(name_o,0);
5213 ima=cpl_image_load(name_o,XSH_PRE_DATA_TYPE,0,0);
5214
5215 if(cpl_propertylist_has(head_m,XSH_QC_AFC_XSHIFT)) {
5216 xshift=cpl_propertylist_get_double(head_m,XSH_QC_AFC_XSHIFT);
5217 cpl_propertylist_append_double(head_o,XSH_QC_AFC_XSHIFT,xshift);
5218 cpl_propertylist_set_comment(head_o,XSH_QC_AFC_XSHIFT,XSH_QC_AFC_XSHIFT_C);
5219 }
5220
5221 if(cpl_propertylist_has(head_m,XSH_QC_AFC_YSHIFT)) {
5222 yshift=cpl_propertylist_get_double(head_m,XSH_QC_AFC_YSHIFT);
5223 cpl_propertylist_append_double(head_o,XSH_QC_AFC_YSHIFT,yshift);
5224 cpl_propertylist_set_comment(head_o,XSH_QC_AFC_YSHIFT,XSH_QC_AFC_YSHIFT_C);
5225 }
5226
5227
5228 cpl_image_save(ima,name_o,XSH_PRE_DATA_BPP,head_o,CPL_IO_DEFAULT);
5229
5230 xsh_free_image(&ima);
5231 xsh_free_propertylist(&head_m);
5232 xsh_free_propertylist(&head_o);
5233
5234 return cpl_error_get_code();
5235
5236}
5237
5238
5249static cpl_boolean xsh_aperture_string_to_double(const char* string,
5250 double* aperture)
5251{
5252 assert(string != NULL);
5253 assert(aperture != NULL);
5254
5255 /* Find the 'x' character in the string.
5256 * Expected format is "%fx%f <optional>". Where <optional> is any string of
5257 * characters. */
5258 const char* x = strchr(string, 'x');
5259 if (x == NULL) return CPL_FALSE;
5260 int lenx = strlen(x);
5261 /* Here we assume that what we need are just the 1st 3 chars of the string
5262 better would be to use lenx and take all string elements up to lenx-1 */
5263 char value[10];
5264 strncpy(value,string,lenx);
5265 //xsh_msg("value=%s",value);
5266 //++x; /* Skip the character 'x'. */
5267 /* Convert the rest of the string to double. */
5268 char* endptr = NULL;
5269 errno = 0; /* Reset ERANGE. */
5270 *aperture = strtod(value, &endptr);
5271 if (endptr == NULL) return CPL_FALSE;
5272 /* Check that there was a space character after converting the number of the
5273 * end of line was already reached. */
5274 if (*endptr != '\0') {
5275 if (isdigit(*endptr)) return CPL_FALSE;
5276 }
5277 /* Check for overflows. */
5278 if (errno == ERANGE) {
5279 cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_OUTPUT,
5280 "Value overflowed and is out of range.");
5281 return CPL_FALSE;
5282 }
5283 *aperture /= 3600.; /* Convert to degrees. */
5284
5285 return CPL_TRUE;
5286}
5287
5295static const char * xsh_get_column_unit(cpl_propertylist * header,
5296 const char * colname)
5297{
5298 cpl_errorstate prestate = cpl_errorstate_get();
5299 const char* bunit = cpl_propertylist_get_string(header, XSH_BUNIT);
5300 const char* column_unit = NULL;
5301 if (bunit != NULL) {
5302 if (strcasecmp(bunit, "ADU") == 0) {
5303 column_unit = XSH_SDP_ADU_UNIT;
5304 } else if (strcasecmp(bunit, "erg/s/cm2/Angstrom") == 0) {
5305 column_unit = XSH_SDP_FLUX_UNIT;
5306 } else {
5307 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
5308 "Could not identify the units for the %s.", colname);
5309 }
5310 }
5311 if (! cpl_errorstate_is_equal(prestate)) return NULL;
5312 return column_unit;
5313}
5320static int xsh_get_nraws(const cpl_frameset* frames)
5321{
5322 cpl_frameset_iterator* iter = NULL;
5323 int counter=0;
5324 iter = cpl_frameset_iterator_new(frames);
5325 const cpl_frame* frame = cpl_frameset_iterator_get_const(iter);
5326 while (frame != NULL) {
5327 if (cpl_frame_get_group(frame) == CPL_FRAME_GROUP_RAW) {
5328 counter++;
5329 }
5330 cpl_errorstate status = cpl_errorstate_get();
5331 cpl_frameset_iterator_advance(iter, 1);
5332 if (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
5333 cpl_errorstate_set(status);
5334 }
5335 frame = cpl_frameset_iterator_get_const(iter);
5336 }
5337 cpl_frameset_iterator_delete(iter);
5338 return counter;
5339}
5340
5356static double xsh_calculate_mjd_end(const cpl_frame *product_frame,
5357 const cpl_frameset *usedframes,
5358 XSH_ARM arm)
5359{
5360 cpl_propertylist* keywords = NULL;
5361 cpl_frameset_iterator* iter = NULL;
5362 double mjdend = NAN;
5363
5364 /* Calculate MJD-END by finding the largest MJD-OBS value from the raw input
5365 * files. */
5366 const char* filename;
5367 check(filename = cpl_frame_get_filename(product_frame));
5368 check(keywords = cpl_propertylist_load(filename, 0));
5369 check(mjdend = cpl_propertylist_get_double(keywords, XSH_MJDOBS));
5370
5371 iter = cpl_frameset_iterator_new(usedframes);
5372 const cpl_frame* frame = cpl_frameset_iterator_get_const(iter);
5373 while (frame != NULL) {
5374 if (cpl_frame_get_group(frame) == CPL_FRAME_GROUP_RAW) {
5375 /* Load the primary header keywords from the raw frame. */
5376 check(filename = cpl_frame_get_filename(frame));
5377 cpl_propertylist_delete(keywords);
5378 check(keywords = cpl_propertylist_load(filename, 0));
5379 /* Extract the MJD-OBS and EXPTIME keywords and update the mjdend
5380 * candidate. */
5381 double obsval=0, expval=0, total=0, dit=0;
5382 double factor=1./(86400.);
5383 int ndit, nditskip;
5384 check(obsval = cpl_propertylist_get_double(keywords, XSH_MJDOBS));
5385 switch (arm) {
5386 case XSH_ARM_NIR:
5387 check(dit = cpl_propertylist_get_double(keywords, XSH_DET_DIT));
5388 check(ndit = cpl_propertylist_get_int(keywords, XSH_DET_NDIT));
5389 check(nditskip = cpl_propertylist_get_int(keywords,
5391 if (nditskip == 0) {
5392 expval = dit * ndit;
5393 } else {
5394 xsh_msg_warning("Found '%s' with a non zero value. Do not know how"
5395 " to handle this case so will over estimate '%s'.",
5397 }
5398 break;
5399 default:
5400 check(expval = cpl_propertylist_get_double(keywords, XSH_EXPTIME));
5401 break;
5402 }
5403 total = obsval + expval * factor;
5404 if (total > mjdend) mjdend = total;
5405
5406 }
5407 /* Increment the iterator to the next frame. */
5408 cpl_errorstate status = cpl_errorstate_get();
5409 cpl_frameset_iterator_advance(iter, 1);
5410 if (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
5411 cpl_errorstate_set(status);
5412 }
5413 frame = cpl_frameset_iterator_get_const(iter);
5414 }
5415
5416cleanup:
5417
5418 cpl_frameset_iterator_delete(iter);
5419 cpl_propertylist_delete(keywords);
5420 return mjdend;
5421}
5422
5434static double xsh_find_first_mjd_obs(const cpl_frameset *usedframes)
5435{
5436 cpl_propertylist* keywords = NULL;
5437 cpl_frameset_iterator* iter = NULL;
5438 double mjdobs = NAN;
5439
5440 iter = cpl_frameset_iterator_new(usedframes);
5441 const cpl_frame* frame = cpl_frameset_iterator_get_const(iter);
5442 while (frame != NULL) {
5443 if (cpl_frame_get_group(frame) == CPL_FRAME_GROUP_RAW) {
5444 /* Load the primary header keywords from the raw frame. */
5445 const char* filename;
5446 check(filename = cpl_frame_get_filename(frame));
5447 check(keywords = cpl_propertylist_load(filename, 0));
5448 /* Extract the MJD-OBS keyword and check if its the earliest one. */
5449 double val;
5450 check(val = cpl_propertylist_get_double(keywords, XSH_MJDOBS));
5451 cpl_propertylist_delete(keywords);
5452 keywords = NULL;
5453 if (isnan(mjdobs)) {
5454 mjdobs = val;
5455 } else if (val < mjdobs) {
5456 mjdobs = val;
5457 }
5458 }
5459 /* Increment the iterator to the next frame. */
5460 cpl_errorstate status = cpl_errorstate_get();
5461 cpl_frameset_iterator_advance(iter, 1);
5462 if (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
5463 cpl_errorstate_set(status);
5464 }
5465 frame = cpl_frameset_iterator_get_const(iter);
5466 }
5467
5468cleanup:
5469 cpl_frameset_iterator_delete(iter);
5470 cpl_propertylist_delete(keywords);
5471 return mjdobs;
5472}
5473
5490static void xsh_fill_provenance_keywords(irplib_sdp_spectrum* spectrum,
5491 const cpl_frameset* usedframes)
5492{
5493 cpl_propertylist* keywords = NULL;
5494 cpl_frameset_iterator* iter = NULL;
5495 int ncombine = 0;
5496// xsh_msg_warning("Inside xsh_fill_provenance_keywords");
5497
5498 iter = cpl_frameset_iterator_new(usedframes);
5499 const cpl_frame* frame = cpl_frameset_iterator_get_const(iter);
5500 while (frame != NULL) {
5501 if (cpl_frame_get_group(frame) == CPL_FRAME_GROUP_RAW) {
5502 /* Load the keywords from the raw frame. */
5503 const char* filename;
5504 check(filename = cpl_frame_get_filename(frame));
5505 check(keywords = cpl_propertylist_load(filename, 0));
5506
5507 /* Try set the prov_value to ARCFILE or ORIGFILE or just the filename,
5508 * whichever is found first in that order. */
5509 const char* prov_value = NULL;
5510 if (cpl_propertylist_has(keywords, XSH_ARCFILE)) {
5511 check(prov_value = cpl_propertylist_get_string(keywords, XSH_ARCFILE));
5512 } else if (cpl_propertylist_has(keywords, XSH_ORIGFILE)) {
5513 check(prov_value = cpl_propertylist_get_string(keywords, XSH_ORIGFILE));
5514 } else {
5515 prov_value = filename;
5516 }
5517 /* Add the next PROVi keyword. */
5518 /* PIPE-9314: Check the DPR TYPE of the incoming file
5519 * Knock SKY frames out of contention */
5520 const char* dpr_type = cpl_propertylist_get_string(keywords, XSH_DPR_TYPE);
5521 int is_a_sky = strcmp(dpr_type, "SKY");
5522// xsh_msg_warning("xsh_fill_provenance_keywords: is_a_sky = %d, dpr_type = %s", is_a_sky, dpr_type);
5523// int is_a_sky = 0;
5524 if (is_a_sky == 0) {
5525 xsh_msg_warning("xsh_fill_provenance_keywords: Disallowing a SKY input from being tracked as PROV: %s", prov_value);
5526 } else {
5527 ++ncombine;
5528// xsh_msg_warning("xsh_fill_provenance_keywords: Use irplib_sdp_spectrum_set_prov as follows:");
5529// xsh_msg_warning("ncombine = %d, prov_value = %s", ncombine, prov_value);
5530 check(irplib_sdp_spectrum_set_prov(spectrum, ncombine, prov_value));
5531 }
5532 cpl_propertylist_delete(keywords);
5533 keywords = NULL;
5534 }
5535 /* Increment the iterator to the next frame. */
5536 cpl_errorstate status = cpl_errorstate_get();
5537 cpl_frameset_iterator_advance(iter, 1);
5538 if (cpl_error_get_code() == CPL_ERROR_ACCESS_OUT_OF_RANGE) {
5539 cpl_errorstate_set(status);
5540 }
5541 frame = cpl_frameset_iterator_get_const(iter);
5542 }
5543
5544 /* Finally add the NCOMBINE keyword. */
5545 check(irplib_sdp_spectrum_set_ncombine(spectrum, ncombine));
5546
5547cleanup:
5548 cpl_frameset_iterator_delete(iter);
5549 cpl_propertylist_delete(keywords);
5550}
5551
5552
5553/*
5554 * This is a row record structure used in the lookup tables implemented in the
5555 * xsh_sdp_spectrum_create function.
5556 */
5558 const char* name;
5559 cpl_type type;
5560 const char* unit;
5561 const char* format;
5562 const char* tutyp;
5563 const char* tucd;
5564};
5565
5566
5583static cpl_error_code xsh_sdp_spectrum_create(
5584 irplib_sdp_spectrum * sdp_spectrum,
5585 const cpl_frameset * usedframes,
5586 const cpl_propertylist * sourcekeys,
5588 xsh_spectrum * spectrum,
5589 xsh_spectrum * uncal_spectrum)
5590{
5591 cpl_errorstate prestate;
5592 cpl_error_code error = CPL_ERROR_NONE;
5593
5594 cpl_ensure_code(sdp_spectrum != NULL, CPL_ERROR_NULL_INPUT);
5595 cpl_ensure_code(usedframes != NULL, CPL_ERROR_NULL_INPUT);
5596 cpl_ensure_code(sourcekeys != NULL, CPL_ERROR_NULL_INPUT);
5597 cpl_ensure_code(instrument != NULL, CPL_ERROR_NULL_INPUT);
5598
5599 assert( !(spectrum == NULL && uncal_spectrum == NULL) );
5600
5601 /* From here we start filling the spectrum data. If we only have the
5602 * uncalibrated spectrum then pretend that it is the flux calibrated one
5603 * during this data copy. */
5604 if (spectrum == NULL) {
5605 spectrum = uncal_spectrum;
5606 uncal_spectrum = NULL;
5607 }
5608
5609 cpl_size nelem = (cpl_size) xsh_spectrum_get_size(spectrum);
5610 /* The spectra must be the same size if both are provided. */
5611 if (uncal_spectrum != NULL) {
5612 cpl_size uncal_size = (cpl_size) xsh_spectrum_get_size(uncal_spectrum);
5613 if (nelem != uncal_size) {
5614 return cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
5615 "Both input spectra must have the same size.");
5616 }
5617 }
5618
5619 prestate = cpl_errorstate_get();
5620 double wavemin = xsh_spectrum_get_lambda_min(spectrum);
5621 double wavemax = xsh_spectrum_get_lambda_max(spectrum);
5622 double wavestep = xsh_spectrum_get_lambda_step(spectrum);
5623 if (! cpl_errorstate_is_equal(prestate)) {
5624 return cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
5625 "Failed to find required wavelength information.");
5626 }
5627
5628 /* Set default values for various SDP keywords. */
5629 error |= irplib_sdp_spectrum_set_prodcatg(sdp_spectrum, XSH_SCIENCE_SPECTRUM);
5630 error |= irplib_sdp_spectrum_set_mepoch(sdp_spectrum, CPL_FALSE);
5631 error |= irplib_sdp_spectrum_set_contnorm(sdp_spectrum, CPL_FALSE);
5632 error |= irplib_sdp_spectrum_set_fluxerr(sdp_spectrum,
5634 error |= irplib_sdp_spectrum_set_referenc(sdp_spectrum,
5636 error |= irplib_sdp_spectrum_set_voclass(sdp_spectrum,
5638 error |= irplib_sdp_spectrum_set_vopub(sdp_spectrum,
5640
5641 /* Take the PRODLVL, ORIGIN and SPECSYS keywords from the source list or
5642 * hardcode their values. */
5643 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_PRODLVL)) {
5644 error |= irplib_sdp_spectrum_copy_prodlvl(sdp_spectrum, sourcekeys,
5646 } else {
5647 error |= irplib_sdp_spectrum_set_prodlvl(sdp_spectrum,
5649 }
5650 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_ORIGIN)) {
5651 error |= irplib_sdp_spectrum_copy_origin(sdp_spectrum, sourcekeys,
5653 } else {
5654 error |= irplib_sdp_spectrum_set_origin(sdp_spectrum,
5656 }
5657 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_SPECSYS)) {
5658 error |= irplib_sdp_spectrum_copy_specsys(sdp_spectrum, sourcekeys,
5660 } else {
5661 error |= irplib_sdp_spectrum_set_specsys(sdp_spectrum,
5663 }
5664
5665 /* Copy ESO.PRO.REC1.PIPE.ID to PROCSOFT if it is available. */
5666 if (cpl_propertylist_has(sourcekeys, XSH_PRO_REC1_PIPE_ID)) {
5667 error |= irplib_sdp_spectrum_copy_procsoft(sdp_spectrum, sourcekeys,
5669 }
5670
5671 /* Set the DISPELEM based on the X-Shooter arm. */
5672 const char* dispelem;
5674 case XSH_ARM_UVB:
5675 case XSH_ARM_VIS:
5676 case XSH_ARM_NIR:
5678 break;
5679 default:
5680 dispelem = NULL;
5681 break;
5682 }
5683 if (dispelem != NULL) {
5684 error |= irplib_sdp_spectrum_set_dispelem(sdp_spectrum, dispelem);
5685 } else {
5686 error = cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
5687 "Could not identify the correct ARM value to be used"
5688 " for '%s'.", XSH_SDP_KEYWORD_DISPELEM);
5689 }
5690
5691 /* Set PROG_ID from ESO.OBS.PROG.ID */
5692 error |= irplib_sdp_spectrum_copy_progid(sdp_spectrum, sourcekeys,
5694
5695 /* Set OBID1 from ESO.OBS.ID */
5696 error |= irplib_sdp_spectrum_copy_obid(sdp_spectrum, 1,
5697 sourcekeys, XSH_OBS_ID);
5698
5699 /* Set OBSTECH from ESO.PRO.TECH */
5700 error |= irplib_sdp_spectrum_copy_obstech(sdp_spectrum, sourcekeys,
5701 XSH_PRO_TECH);
5702
5703 /* Set the FLUXCAL keyword based on the content of ESO.PRO.CATG. If the result
5704 * is flux calibrated then the ESO.PRO.CATG keyword will contain a name that
5705 * contains the "_FLUX_" substring. */
5706 const char* procatg = cpl_propertylist_get_string(sourcekeys,
5707 CPL_DFS_PRO_CATG);
5708 if (procatg != NULL) {
5709 const char* fluxcal = "UNCALIBRATED";
5710 if (strstr(procatg, "_FLUX_") != NULL) {
5711 fluxcal = "ABSOLUTE";
5712 }
5713 error |= irplib_sdp_spectrum_set_fluxcal(sdp_spectrum, fluxcal);
5714 } else {
5715 error = cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
5716 "Could not set '%s' since the '%s' keyword was not"
5717 " found.", XSH_SDP_KEYWORD_FLUXCAL, CPL_DFS_PRO_CATG);
5718 }
5719
5720 /* Setup the WAVELMIN, WAVELMAX and SPEC_BIN keywords from WCS information. */
5721 prestate = cpl_errorstate_get();
5722 double crpix1 = cpl_propertylist_get_double(sourcekeys, XSH_CRPIX1);
5723 double cdelt1 = cpl_propertylist_get_double(sourcekeys, XSH_CDELT1);
5724 double crval1 = cpl_propertylist_get_double(sourcekeys, XSH_CRVAL1);
5725 long naxis1 = cpl_propertylist_get_long(sourcekeys, XSH_NAXIS1);
5726 if (cpl_errorstate_is_equal(prestate)) {
5727 double wavelmin = (1 - crpix1) * cdelt1 + crval1;
5728 double wavelmax = (naxis1 - crpix1) * cdelt1 + crval1;
5729 double specbin = cdelt1;
5730 cpl_msg_info(__func__, "Setting SDP wavelmin = %3.10f from crval1 = %3.10f", wavelmin, crval1);
5731// getchar();
5732 error |= irplib_sdp_spectrum_set_wavelmin(sdp_spectrum, wavelmin);
5733 error |= irplib_sdp_spectrum_set_wavelmax(sdp_spectrum, wavelmax);
5734 error |= irplib_sdp_spectrum_set_specbin(sdp_spectrum, specbin);
5735 } else {
5736 error = cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
5737 "Could not find WCS information.");
5738 }
5739
5740 /* Reassign the OBJECT keyword to use the ESO.OBS.TARG.NAME value.
5741 * Also get the TITLE from the same OBJECT keyword value. */
5742 const char* object = cpl_propertylist_get_string(sourcekeys,
5744 if (object != NULL) {
5745 error |= irplib_sdp_spectrum_set_object(sdp_spectrum, object);
5746
5747 char* objstr = cpl_strdup(object);
5748 /* Find second occurrence of ',' in objstr and set it to NULL to trim off
5749 * anything after that comma. Then generate the title. */
5750 char* c = strchr(objstr, ',');
5751 if (c != NULL) {
5752 c = strchr(c+1, ',');
5753 if (c != NULL) *c = '\0';
5754 }
5755 int obsid = xsh_pfits_get_obs_id(sourcekeys);
5756 const char* dateobs = xsh_pfits_get_date_obs(sourcekeys);
5757 char* title = cpl_sprintf("%s_%d_%s", objstr,obsid, dateobs);
5759 error |= irplib_sdp_spectrum_set_title(sdp_spectrum, title);
5760 cpl_free(title);
5761 cpl_free(objstr);
5762 } else {
5763 error = cpl_error_set_message(cpl_func, cpl_error_get_code(),
5764 "Could not assign '%s' or '%s' since the '%s' keyword could"
5765 " not be found.", XSH_SDP_KEYWORD_OBJECT,
5767 }
5768
5769 error |= irplib_sdp_spectrum_copy_ra(sdp_spectrum, sourcekeys, XSH_RA);
5770 error |= irplib_sdp_spectrum_copy_dec(sdp_spectrum, sourcekeys, XSH_DEC);
5771
5772 /* Calculate and set the EXPTIME and TEXPTIME (must be identical). */
5773 prestate = cpl_errorstate_get();
5774 double exptime = xsh_calculate_exptime(usedframes,
5776 if (! isnan(exptime)) {
5777 error |= irplib_sdp_spectrum_set_exptime(sdp_spectrum, exptime);
5778 error |= irplib_sdp_spectrum_set_texptime(sdp_spectrum, exptime);
5779 } else if (! cpl_errorstate_is_equal(prestate)) {
5780 error = cpl_error_get_code();
5781 }
5782
5783 /* Copy the MJD-OBS and MJD-END keywords and store their values, which will be
5784 * used later. */
5785 prestate = cpl_errorstate_get();
5786 double mjdobs = cpl_propertylist_get_double(sourcekeys, XSH_MJDOBS);
5787 double mjdend = cpl_propertylist_get_double(sourcekeys, XSH_MJDEND);
5788
5789 if (cpl_errorstate_is_equal(prestate)) {
5790 error |= irplib_sdp_spectrum_set_mjdobs(sdp_spectrum, mjdobs);
5791 error |= irplib_sdp_spectrum_set_mjdend(sdp_spectrum, mjdend);
5792 } else {
5793 mjdobs = mjdend = NAN;
5794 }
5795
5796 /* Copy the SNR keyword from the ESO.QC.FLUX.SN keyword. */
5797 if (cpl_propertylist_has(sourcekeys, XSH_QC_FLUX_SN)) {
5798 error |= irplib_sdp_spectrum_copy_snr(sdp_spectrum, sourcekeys,
5800 }
5801
5802 /* Get nominal values for LAMNLIN, LAMRMS and SPEC_RES from lookup tables */
5803 int lamnlin = -1;
5804 double lamrms = -1;
5805 double specres = -1;
5806 /* UVB, VIS, NIR */
5807 int lamnlin_lut[3] = {280, 310, 140};
5808 double lamrms_lut[3][2] = {
5809 /* Before After
5810 * 1st Jan 2012 1st Jan 2012 ARM */
5811 { 0.013, 0.003}, /* UVB */
5812 { 0.013, 0.002}, /* VIS */
5813 { 0.060, 0.006} /* NIR */
5814 };
5815 double specres_lut[14][3] = {
5816 /* MCW 200831 (PIPE-9214) - values updated */
5817 /* Slit UVB VIS NIR */
5818 /* 0.4x11 */ { -1 , 18340 , 11424 },
5819 /* 0.5x11 */ { 9861 , -1 , -1 },
5820 /* 0.6x11 */ { -1 , -1 , 8031 },
5821 /* 0.7x11 */ { -1 , 11333 , -1 },
5822 /* 0.8x11 */ { 6655 , -1 , -1 },
5823 /* 0.9x11 */ { -1 , 8935 , 5573 },
5824 /* 1.0x11 */ { 5453 , -1 , -1 },
5825 /* 1.2x11 */ { -1 , 6505 , 4287 },
5826 /* 1.3x11 */ { 4112 , -1 , -1 },
5827 /* 1.5x11 */ { -1 , 5040 , 3416 },
5828 /* 1.6x11 */ { 3251 , -1 , -1 },
5829 /* 5.0x11 */ { 1040 , 1512 , 1029 },
5830 /* MCW 200831 (PIPE-9214) - Add K-blocked slits */
5831 /* 0.6JHx11 */ { -1 , -1 , 8014 },
5832 /* 0.9JHx11 */ { -1 , -1 , 5567 }
5833 };
5834
5835 int usedarm;
5836 const char* used_keyword;
5838 case XSH_ARM_UVB: usedarm = 0; used_keyword = XSH_SLIT_UVB; break;
5839 case XSH_ARM_VIS: usedarm = 1; used_keyword = XSH_SLIT_VIS; break;
5840 case XSH_ARM_NIR: usedarm = 2; used_keyword = XSH_SLIT_NIR; break;
5841 default: usedarm = -1; used_keyword = NULL; break;
5842 }
5843
5844 /* Figure out the date epoch of the raw data. We check if the date range
5845 * overlaps the epoch threshold. If the data does then we dont try figure out
5846 * which epoch to choose LAMRMS for. */
5847 double date_threshold = 55927.0; /* Julian Date for 1/1/2012 */
5848 int softepoch = -1; /* epoch of the software used. 0 = before 1/1/2012 and
5849 1 = after 1/1/2012. */
5850 prestate = cpl_errorstate_get();
5851 double first_mjdobs = xsh_find_first_mjd_obs(usedframes);
5852 if (cpl_errorstate_is_equal(prestate)) {
5853 if (! (first_mjdobs < date_threshold && date_threshold < mjdend)) {
5854 if (first_mjdobs < date_threshold) {
5855 softepoch = 0;
5856 } else {
5857 softepoch = 1;
5858 }
5859 }
5860 } else {
5861 error = cpl_error_get_code();
5862 }
5863
5864 /* Identify the lookup entry for the slit that was used. */
5865 int usedslit = -1;
5866 const char* lengthstr = cpl_propertylist_get_string(sourcekeys, used_keyword);
5867 if (lengthstr != NULL) {
5868 if (strstr(lengthstr, "0.4x11") == lengthstr) {
5869 usedslit = 0;
5870 } else if (strstr(lengthstr, "0.5x11") == lengthstr) {
5871 usedslit = 1;
5872 } else if (strstr(lengthstr, "0.6x11") == lengthstr) {
5873 usedslit = 2;
5874 } else if (strstr(lengthstr, "0.7x11") == lengthstr) {
5875 usedslit = 3;
5876 } else if (strstr(lengthstr, "0.8x11") == lengthstr) {
5877 usedslit = 4;
5878 } else if (strstr(lengthstr, "0.9x11") == lengthstr) {
5879 usedslit = 5;
5880 } else if (strstr(lengthstr, "1.0x11") == lengthstr) {
5881 usedslit = 6;
5882 } else if (strstr(lengthstr, "1.2x11") == lengthstr) {
5883 usedslit = 7;
5884 } else if (strstr(lengthstr, "1.3x11") == lengthstr) {
5885 usedslit = 8;
5886 } else if (strstr(lengthstr, "1.5x11") == lengthstr) {
5887 usedslit = 9;
5888 } else if (strstr(lengthstr, "1.6x11") == lengthstr) {
5889 usedslit = 10;
5890 } else if (strstr(lengthstr, "5.0x11") == lengthstr) {
5891 usedslit = 11;
5892 } else if (strstr(lengthstr, "0.6JHx11") == lengthstr) {
5893 usedslit = 12;
5894 } else if (strstr(lengthstr, "0.9JHx11") == lengthstr) {
5895 usedslit = 13;
5896 }
5897 } else {
5898 error = cpl_error_get_code();
5899 }
5900
5901 /* Copy the LAMNLIN, LAMRMS and SPEC_RES keywords from the source keywords
5902 * if they already exist, otherwise set them from the lookup table. */
5903 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_LAMNLIN)) {
5904 prestate = cpl_errorstate_get();
5905 int val = cpl_propertylist_get_int(sourcekeys, XSH_SDP_KEYWORD_LAMNLIN);
5906 if (cpl_errorstate_is_equal(prestate)) {
5907 lamnlin = val;
5908 } else {
5909 error = cpl_error_get_code();
5910 }
5911 } else {
5912 if (usedarm != -1) {
5913 lamnlin = lamnlin_lut[usedarm];
5914 }
5915 }
5916 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_LAMRMS)) {
5917 prestate = cpl_errorstate_get();
5918 double val = cpl_propertylist_get_double(sourcekeys,
5920 if (cpl_errorstate_is_equal(prestate)) {
5921 lamrms = val;
5922 } else {
5923 error = cpl_error_get_code();
5924 }
5925 } else {
5926 if (usedarm != -1 && softepoch != -1) {
5927 lamrms = lamrms_lut[usedarm][softepoch];
5928 }
5929 }
5930 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_SPEC_RES)) {
5931 prestate = cpl_errorstate_get();
5932 double val = cpl_propertylist_get_double(sourcekeys,
5934 if (cpl_errorstate_is_equal(prestate)) {
5935 specres = val;
5936 } else {
5937 error = cpl_error_get_code();
5938 }
5939 } else {
5940 if (usedslit != -1 && usedarm != -1) {
5941 specres = specres_lut[usedslit][usedarm];
5942 }
5943 }
5944
5945 /* Set the LAMNLIN, LAMRMS and SPEC_RES keywords if they were actually
5946 * calculated. */
5947 if (lamnlin != -1) {
5948 error |= irplib_sdp_spectrum_set_lamnlin(sdp_spectrum, lamnlin);
5949 }
5950 if (lamrms != -1) {
5951 error |= irplib_sdp_spectrum_set_lamrms(sdp_spectrum, lamrms);
5952 }
5953 if (specres != -1) {
5954 error |= irplib_sdp_spectrum_set_specres(sdp_spectrum, specres);
5955 }
5956
5957 /* Calculate the SPEC_ERR keyword if it is not already in the source keywords.
5958 * Otherwise we just copy it to the primary header. If we could not calculate
5959 * the keyword then do not set it. */
5960 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_SPEC_ERR)) {
5961 prestate = cpl_errorstate_get();
5962 double val = cpl_propertylist_get_double(sourcekeys,
5964 if (cpl_errorstate_is_equal(prestate)) {
5965 error |= irplib_sdp_spectrum_set_specerr(sdp_spectrum, val);
5966 } else {
5967 error = cpl_error_get_code();
5968 }
5969 } else if (lamnlin > 0) {
5970 double specerr = -1;
5971 if (cpl_propertylist_has(sourcekeys, XSH_CRDER1)) {
5972 prestate = cpl_errorstate_get();
5973 double crder1 = cpl_propertylist_get_double(sourcekeys, XSH_CRDER1);
5974 if (cpl_errorstate_is_equal(prestate) && crder1 != 0) {
5975 specerr = crder1 / sqrt(lamnlin);
5976 } else {
5977 error = cpl_error_get_code();
5978 }
5979 }
5980 if (specerr == -1 && lamrms != -1) {
5981 specerr = lamrms / sqrt(lamnlin);
5982 }
5983 if (specerr != -1) {
5984 error |= irplib_sdp_spectrum_set_specerr(sdp_spectrum, specerr);
5985 }
5986 }
5987
5988 /* Calculate the SPEC_SYE keyword if it is not already in the source keywords.
5989 * Otherwise we just copy it to the primary header. If we could not calculate
5990 * the keyword then do not set it. */
5991 if (cpl_propertylist_has(sourcekeys, XSH_SDP_KEYWORD_SPEC_SYE)) {
5992 error |= irplib_sdp_spectrum_copy_specsye(sdp_spectrum, sourcekeys,
5994 } else if (lamnlin > 0) {
5995 double specsye = NAN;
5996 if (cpl_propertylist_has(sourcekeys, XSH_CSYER1)) {
5997 prestate = cpl_errorstate_get();
5998 double csyer1 = cpl_propertylist_get_double(sourcekeys, XSH_CSYER1);
5999 if (cpl_errorstate_is_equal(prestate)) {
6000 specsye = csyer1 / sqrt(lamnlin);
6001 } else {
6002 error = cpl_error_get_code();
6003 }
6004 }
6005 if (! isnan(specsye)) {
6006 error |= irplib_sdp_spectrum_set_specsye(sdp_spectrum, specsye);
6007 }
6008 }
6009
6010 prestate = cpl_errorstate_get();
6011 double gainvalue = NAN;
6012 double detron = NAN;
6013 double dit = NAN;
6015 case XSH_ARM_NIR:
6016 /* Hardcode the gain for NIR. */
6017 gainvalue = 1./2.12;
6018 /* Hardcode the RON for NIR. */
6019 dit = xsh_pfits_get_dit(sourcekeys);
6020 detron = xsh_compute_ron_nir(dit);
6021 if (! cpl_errorstate_is_equal(prestate)) {
6022 error = cpl_error_get_code();
6023 }
6024 break;
6025 default:
6026 /* Update the GAIN from ESO.DET.OUT1.CONAD */
6027 gainvalue = cpl_propertylist_get_double(sourcekeys, XSH_CONAD);
6028 if (! cpl_errorstate_is_equal(prestate)) {
6029 error = cpl_error_set_message(cpl_func, cpl_error_get_code(),
6030 "Could not assign '%s' since the '%s' keyword was"
6031 " not found.", XSH_SDP_KEYWORD_GAIN, XSH_CONAD);
6032 }
6033 /* Update DETRON and EFFRON from ESO.DET.OUT1.RON */
6034 detron = cpl_propertylist_get_double(sourcekeys, XSH_RON);
6035 if (! cpl_errorstate_is_equal(prestate)) {
6036 error = cpl_error_set_message(cpl_func, cpl_error_get_code(),
6037 "Could not assign '%s' and '%s' since the '%s' keyword"
6038 " was not found.", XSH_SDP_KEYWORD_DETRON,
6040 }
6041 break;
6042 }
6043 /* Removing non-HIERARCH GAIN keyword, see PIPE-10945 */
6044 /*if (! isnan(gainvalue)) {
6045 error |= irplib_sdp_spectrum_set_gain(sdp_spectrum, gainvalue);
6046 }*/
6047 if (! isnan(detron)) {
6048 error |= irplib_sdp_spectrum_set_detron(sdp_spectrum, detron);
6049 error |= irplib_sdp_spectrum_set_effron(sdp_spectrum, detron);
6050 }
6051
6052 /* Set the TOT_FLUX keyword to true if the ESO.INS.OPTIi.NAME keyword
6053 * contains "5.0x<Length>" where <Length> can be any number.
6054 * Also setup the APERTURE keyword from the ESO.INS.OPTIi.NAME keyword. */
6055 if (lengthstr != NULL) {
6056 double aperture;
6057 cpl_boolean totflux = CPL_FALSE;
6058 if (strstr(lengthstr, "5.0x") == lengthstr) totflux = CPL_TRUE;
6059 error |= irplib_sdp_spectrum_set_totflux(sdp_spectrum, totflux);
6060 if (xsh_aperture_string_to_double(lengthstr, &aperture)) {
6061 error |= irplib_sdp_spectrum_set_aperture(sdp_spectrum, aperture);
6062 } else {
6063 error = cpl_error_set_message(cpl_func, CPL_ERROR_ILLEGAL_INPUT,
6064 "The '%s' keyword contains an invalid value '%s'."
6065 " An example of the expected format is '0.9x11'.",
6066 used_keyword, lengthstr);
6067 }
6068 } else {
6069 error = cpl_error_set_message(cpl_func, cpl_error_get_code(),
6070 "Could not assign '%s' or '%s' since one could not read the"
6071 " '%s' keyword.", XSH_SDP_KEYWORD_TOT_FLUX,
6072 XSH_SDP_KEYWORD_APERTURE, used_keyword);
6073 }
6074
6075 /* Calculate the TELAPSE and TMID keywords from MJD-OBS and MJD-END. */
6076 if (! isnan(mjdobs) && ! isnan(mjdend)) {
6077 int nframes=xsh_get_nraws(usedframes);
6078 if(nframes==1) {
6079 /* this is a special case for which to prevent rounding errors due to
6080 * the very small difference MJDEND - MJDSTART, and using the fact that
6081 * MJDEND - MJDSTART= EXPTIME (or DIT*NDIT) we compute TELAPSE as
6082 * TELAPSE=EXPTIME (UVB,VIS) or TELAPSE=DIT*NDIT
6083 */
6084 double telapse=0;
6086 double dit = cpl_propertylist_get_double(sourcekeys, XSH_DET_DIT);
6087 int ndit = cpl_propertylist_get_int(sourcekeys, XSH_DET_NDIT);
6088 int nditskip = cpl_propertylist_get_int(sourcekeys,XSH_DET_NDITSKIP);
6089 if (nditskip == 0) {
6090 telapse = dit * ndit;
6091 } else {
6092 xsh_msg_warning("Found '%s' with a non zero value. Do not know how"
6093 " to handle this case so will over estimate '%s'.",
6095 }
6096 } else {
6097 telapse = cpl_propertylist_get_double(sourcekeys, XSH_EXPTIME);
6098 }
6099 error |= irplib_sdp_spectrum_set_telapse(sdp_spectrum,telapse);
6100 } else {
6101 error |= irplib_sdp_spectrum_set_telapse(sdp_spectrum,
6102 (mjdend - mjdobs) * 86400.);
6103 }
6104 error |= irplib_sdp_spectrum_set_tmid(sdp_spectrum,
6105 (mjdend + mjdobs) * 0.5);
6106 } else {
6107 error = cpl_error_set_message(cpl_func, cpl_error_get_code(),
6108 "Could not assign '%s' nor '%s' since one could not read"
6109 " either the '%s' or '%s' keyword.", XSH_SDP_KEYWORD_TELAPSE,
6111 }
6112
6113 /* Setup the TDMIN and TDMAX keywords from wavelength minimum and maximum. */
6114 error |= irplib_sdp_spectrum_set_tdmin(sdp_spectrum, wavemin);
6115 error |= irplib_sdp_spectrum_set_tdmax(sdp_spectrum, wavemax);
6116
6117 /* Setup the SPEC_VAL and SPEC_BW keywords from the spectrum wavelength
6118 * information. */
6119 error |= irplib_sdp_spectrum_set_specval(sdp_spectrum,
6120 (wavemax + wavemin)*0.5);
6121 error |= irplib_sdp_spectrum_set_specbw(sdp_spectrum, wavemax - wavemin);
6122
6123 error |= irplib_sdp_spectrum_set_nelem(sdp_spectrum, nelem);
6124 error |= irplib_sdp_spectrum_set_extname(sdp_spectrum,
6126 error |= irplib_sdp_spectrum_set_inherit(sdp_spectrum,
6128
6129 /* Check the error state and stop at this point (cleaning up memory) if there
6130 * have been any errors. */
6131 if (error) {
6132 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
6133 "Failed to initialise keyword list for the SDP spectrum.");
6134 }
6135
6136 /* Figure out the units to use for FLUX and ERR table columns. */
6137 const char* column_flux_unit = xsh_get_column_unit(spectrum->flux_header,
6138 "flux");
6139 const char* column_errs_unit = xsh_get_column_unit(spectrum->errs_header,
6140 "flux error");
6141 if (column_flux_unit == NULL || column_errs_unit == NULL) {
6142 return cpl_error_get_code();
6143 }
6144
6145 /* The following constructs a list of columns that we should setup in the
6146 * spectrum table. */
6147 struct _xsh_column_info columns[] = {
6151 {XSH_SDP_COLUMN_FLUX, CPL_TYPE_DOUBLE, column_flux_unit,
6154 {XSH_SDP_COLUMN_ERR, CPL_TYPE_DOUBLE, column_errs_unit,
6163 {NULL, CPL_TYPE_INVALID, NULL, NULL, NULL, NULL}
6164 };
6165 struct _xsh_column_info* column;
6166
6167 /* Add the spectrum columns to the table. */
6168 for (column = columns; column->name != NULL; ++column) {
6169 error = irplib_sdp_spectrum_add_column(sdp_spectrum, column->name,
6170 column->type, column->unit, column->format,
6171 column->tutyp, column->tucd, NULL);
6172 if (error) {
6173 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
6174 "Failed to setup the '%s' column.", column->name);
6175 }
6176 }
6177 error = irplib_sdp_spectrum_replace_column_comment(sdp_spectrum, XSH_SDP_COLUMN_WAVE,
6178 "TUCD", "Air wavelength");
6179
6180 /* Add the columns for the uncalibrated data if we got any. */
6181 if (uncal_spectrum != NULL) {
6182
6183 /* Figure out the units to use for FLUX_REDUCED and ERR_REDUCED columns. */
6184 column_flux_unit = xsh_get_column_unit(uncal_spectrum->flux_header,
6185 "uncalibrated flux");
6186 column_errs_unit = xsh_get_column_unit(uncal_spectrum->errs_header,
6187 "uncalibrated flux error");
6188 if (column_flux_unit == NULL || column_errs_unit == NULL) {
6189 return cpl_error_get_code();
6190 }
6191
6192 /* The following constructs a list of columns for the uncalibrated data that
6193 * we should setup in the spectrum table. */
6194 struct _xsh_column_info uncalib_columns[] = {
6195 {XSH_SDP_COLUMN_FLUX_REDUCED, CPL_TYPE_DOUBLE, column_flux_unit,
6198 {XSH_SDP_COLUMN_ERR_REDUCED, CPL_TYPE_DOUBLE, column_errs_unit,
6201 {NULL, CPL_TYPE_INVALID, NULL, NULL, NULL, NULL}
6202 };
6203
6204 /* Add the columns for the uncalibrated data to the table. */
6205 for (column = uncalib_columns; column->name != NULL; ++column) {
6206 error = irplib_sdp_spectrum_add_column(sdp_spectrum, column->name,
6207 column->type, column->unit, column->format,
6208 column->tutyp, column->tucd, NULL);
6209
6210
6211 if (error) {
6212 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
6213 "Failed to setup the '%s' column.", column->name);
6214 }
6215 }
6216 }
6217
6218 /* Finally copy the table data. */
6219 double* specflux = xsh_spectrum_get_flux(spectrum);
6220 double* specerrs = xsh_spectrum_get_errs(spectrum);
6221 int* specqual = xsh_spectrum_get_qual(spectrum);
6222 if (specflux != NULL && specerrs != NULL && specqual != NULL) {
6223 /* Prepare the wavelength array and set it. */
6224 double* data = cpl_malloc(nelem * sizeof(double));
6225 cpl_array* array = cpl_array_wrap_double(data, nelem);
6226 if (data != NULL) {
6227 cpl_size i;
6228 for (i = 0; i < nelem; ++i) {
6229 data[i] = wavemin + i * wavestep;
6230 }
6231 } else {
6232 error |= cpl_error_get_code();
6233 }
6234 error |= irplib_sdp_spectrum_set_column_data(sdp_spectrum,
6235 XSH_SDP_COLUMN_WAVE, array);
6236 cpl_array_unwrap(array);
6237 cpl_free(data);
6238
6239 /* Prepare the other data arrays and assign them. */
6240 array = cpl_array_wrap_double(specflux, nelem);
6241 error |= irplib_sdp_spectrum_set_column_data(sdp_spectrum,
6242 XSH_SDP_COLUMN_FLUX, array);
6243 cpl_array_unwrap(array);
6244 array = cpl_array_wrap_double(specerrs, nelem);
6245 error |= irplib_sdp_spectrum_set_column_data(sdp_spectrum,
6246 XSH_SDP_COLUMN_ERR, array);
6247 cpl_array_unwrap(array);
6248 array = cpl_array_wrap_int(specqual, nelem);
6249 error |= irplib_sdp_spectrum_set_column_data(sdp_spectrum,
6250 XSH_SDP_COLUMN_QUAL, array);
6251 cpl_array_unwrap(array);
6252
6253 /* Prepare the signal to noise column data. */
6254 data = cpl_malloc(nelem * sizeof(double));
6255 array = cpl_array_wrap_double(data, nelem);
6256 if (data != NULL) {
6257 cpl_size i;
6258 for (i = 0; i < nelem; ++i) {
6259 double denom = fabs(specerrs[i]);
6260 data[i] = denom != 0 ? fabs(specflux[i]) / denom : 0;
6261 }
6262 } else {
6263 error |= cpl_error_get_code();
6264 }
6265 error |= irplib_sdp_spectrum_set_column_data(sdp_spectrum,
6266 XSH_SDP_COLUMN_SNR, array);
6267 cpl_array_unwrap(array);
6268 cpl_free(data);
6269
6270 } else {
6271 error = cpl_error_get_code();
6272 }
6273
6274 /* Copy the uncalibrated data if available. */
6275 if (uncal_spectrum != NULL) {
6276 specflux = xsh_spectrum_get_flux(uncal_spectrum);
6277 specerrs = xsh_spectrum_get_errs(uncal_spectrum);
6278 if (specflux != NULL && specerrs != NULL) {
6279 cpl_array* array = cpl_array_wrap_double(specflux, nelem);
6280 error |= irplib_sdp_spectrum_set_column_data(sdp_spectrum,
6282 cpl_array_unwrap(array);
6283 array = cpl_array_wrap_double(specerrs, nelem);
6284 error |= irplib_sdp_spectrum_set_column_data(sdp_spectrum,
6286 cpl_array_unwrap(array);
6287 } else {
6288 error = cpl_error_get_code();
6289 }
6290 }
6291
6292 if (error) {
6293 return cpl_error_set_message(cpl_func, cpl_error_get_code(),
6294 "Failed to copy spectrum data to table.");
6295 }
6296 return CPL_ERROR_NONE;
6297}
6298
6326void xsh_add_sdp_product_spectrum(const cpl_frame *flux_cal_frame,
6327 const cpl_frame *uncal_frame,
6328 cpl_frameset *frameset,
6329 const cpl_frameset *usedframes,
6330 const cpl_parameterlist *parameters,
6331 const char *recipe_id,
6333 cpl_propertylist* qclist)
6334{
6335 cpl_table* table = cpl_table_new(0);
6336 cpl_propertylist* tablekeys = cpl_propertylist_new();
6337 cpl_propertylist* primarykeys = cpl_propertylist_new();
6338 cpl_propertylist* sourcekeys = NULL;
6339 char* output_procatg = NULL;
6340 char* filename = NULL;
6341 xsh_spectrum* spectrum = NULL;
6342 xsh_spectrum* uncal_spectrum = NULL;
6343 irplib_sdp_spectrum* sdp_spectrum = irplib_sdp_spectrum_new();
6344
6345 XSH_ASSURE_NOT_NULL(frameset);
6346 XSH_ASSURE_NOT_NULL(usedframes);
6347 XSH_ASSURE_NOT_NULL(parameters);
6348 XSH_ASSURE_NOT_NULL(recipe_id);
6350
6351 if (flux_cal_frame == NULL && uncal_frame == NULL) {
6352 cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
6353 "Cannot have both the flux calibrated and"
6354 " uncalibrated frames set to NULL.");
6355 xsh_error_msg("%s", cpl_error_get_message());
6356 goto cleanup;
6357 }
6358
6359 const cpl_frame* product_frame = flux_cal_frame != NULL ? flux_cal_frame
6360 : uncal_frame;
6361 const char* fname = cpl_frame_get_filename(product_frame);
6362 filename = cpl_sprintf("SDP_%s", fname);
6363
6364 check(sourcekeys = cpl_propertylist_load(fname, 0));
6365
6366 /* Copy all existing QC keywords from the product we are converting. */
6367 check(cpl_propertylist_copy_property_regexp(primarykeys, sourcekeys,
6368 "^ESO QC ", CPL_FALSE));
6369
6370 /* Need to copy the ESO.QC.FLUX.SN keyword from uncal_frame. */
6371 if (uncal_frame != NULL) {
6372 if (! cpl_propertylist_has(sourcekeys, XSH_QC_FLUX_SN)) {
6373 cpl_propertylist* tmpkeys;
6374 tmpkeys = cpl_propertylist_load(cpl_frame_get_filename(uncal_frame), 0);
6375 assure(tmpkeys != NULL, cpl_error_get_code(),
6376 "%s", cpl_error_get_message());
6377 if (cpl_propertylist_has(tmpkeys, XSH_QC_FLUX_SN)) {
6378 cpl_propertylist_copy_property(sourcekeys, tmpkeys, XSH_QC_FLUX_SN);
6379 }
6380 /* Again copy all QC keywords from the non-flux calibrated product. */
6381 cpl_propertylist_copy_property_regexp(primarykeys, tmpkeys,
6382 "^ESO QC ", CPL_FALSE);
6383 cpl_propertylist_delete(tmpkeys);
6384 assure(cpl_error_get_code() == CPL_ERROR_NONE, cpl_error_get_code(),
6385 "%s", cpl_error_get_message());
6386 }
6387 }
6388
6389 /* Force the INSTRUME keyword to be "XSHOOTER" since old data can have a
6390 * different value for this keyword. */
6391 check(cpl_propertylist_update_string(primarykeys, XSH_INSTRUME,
6393 const char* inst_comment = cpl_propertylist_get_comment(primarykeys,
6394 XSH_INSTRUME);
6395 if (inst_comment == NULL || strcmp(inst_comment, "") == 0) {
6396 check(cpl_propertylist_set_comment(primarykeys, XSH_INSTRUME,
6397 "Instrument used."));
6398 }
6399
6400 /* Set the ESO.PRO.CATG keyword based on the instrument arm. */
6401 const char* armtag;
6403 case XSH_ARM_UVB:
6404 case XSH_ARM_VIS:
6405 case XSH_ARM_NIR:
6407 break;
6408 default:
6409 armtag = NULL;
6410 }
6411 // PIPE-9157 - search the usedframes for DRP TYPES, and choose one accordingly
6412 const char* producttype = NULL;
6413 const char* dprtype ;
6414 cpl_frameset_iterator* iter_new = cpl_frameset_iterator_new(usedframes);
6415 const cpl_frame* testframe = cpl_frameset_iterator_get_const(iter_new);
6416 cpl_propertylist* proptest;
6417 while (testframe != NULL) {
6418 proptest = cpl_propertylist_load(cpl_frame_get_filename(testframe), 0);
6419 if (cpl_propertylist_has(proptest, XSH_DPR_TYPE)) {
6420 dprtype = cpl_propertylist_get_string(proptest, XSH_DPR_TYPE);
6421 if (strcmp(dprtype, "OBJECT") == 0) {
6422// xsh_msg_warning("Have dprtype %s, setting IDP product to SCI", dprtype);
6423 producttype = "SCI" ;
6424 testframe = NULL ;
6425 break ;
6426 } else if (strcmp(dprtype, "STD,TELLURIC") == 0) {
6427// xsh_msg_warning("Have dprtype %s, setting IDP product to TELL (unless OBJECT found later)", dprtype);
6428 producttype = "TELL" ;
6429 }
6430 }
6431 cpl_frameset_iterator_advance(iter_new, 1);
6432 testframe = cpl_frameset_iterator_get_const(iter_new);
6433 cpl_propertylist_delete(proptest);
6434 }
6435 cpl_frameset_iterator_delete(iter_new);
6436
6437 if (producttype == NULL) {
6438 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
6439 "Could not identify the correct product type value to be used"
6440 " for '%s'.", CPL_DFS_PRO_CATG);
6441 goto cleanup;
6442 }
6443 if (armtag != NULL) {
6444 output_procatg = cpl_sprintf("%s_SLIT_FLUX_IDP_%s", producttype, armtag);
6445 check(cpl_propertylist_update_string(primarykeys, CPL_DFS_PRO_CATG,
6446 output_procatg));
6447 } else {
6448 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
6449 "Could not identify the correct ARM value to be used"
6450 " for '%s'.", CPL_DFS_PRO_CATG);
6451 xsh_error_msg("%s", cpl_error_get_message());
6452 goto cleanup;
6453 }
6454
6455 /* Calculate MJD-END by finding the largest MJD-OBS value from the raw input
6456 * files. */
6457 double mjdend;
6458 check(mjdend = xsh_calculate_mjd_end(product_frame, usedframes,
6460 check(cpl_propertylist_update_double(sourcekeys, XSH_MJDEND, mjdend));
6461
6462 /* Load the raw spectra and fill the SDP spectrum object. */
6463 if (flux_cal_frame != NULL) {
6464 check(spectrum = xsh_spectrum_load((cpl_frame*)flux_cal_frame));
6465 }
6466 if (uncal_frame != NULL) {
6467 check(uncal_spectrum = xsh_spectrum_load((cpl_frame*)uncal_frame));
6468 }
6469 cpl_errorstate prestate = cpl_errorstate_get();
6470 xsh_sdp_spectrum_create(sdp_spectrum, usedframes, sourcekeys, instrument,
6471 spectrum, uncal_spectrum);
6472 if (! cpl_errorstate_is_equal(prestate)) {
6473 xsh_error_msg("%s", cpl_error_get_message());
6474 goto cleanup;
6475 }
6476
6477 /* PIPE-10061 - Do barycentric correction of these data, if requested */
6478 /* Currently, IRPLIB will hardcode the 'not corrected' header kw if
6479 * one isn't specified - the solution seems to be to supply one in the
6480 * header keywords of the sdp_spectrum object (this is the only way
6481 * it gets through the irplib save routines without being overwritten
6482 * by the hardcoded default) */
6483 /* Leaving here for reference, but call not required given correction
6484 * is now done to the underlying non-SDP products. This includes the
6485 * wavelength column of the output table. */
6486// xsh_bary_corr_param* bary_param;
6487// bary_param = xsh_parameters_bary_corr_get(recipe_id, parameters);
6488// if (bary_param != NULL) {
6489// check(xsh_baryvel_correct_irplib_spectrum(
6490// sdp_spectrum, bary_param, primarykeys));
6491// }
6492// XSH_FREE(bary_param);
6493
6494 check(xsh_fill_provenance_keywords(sdp_spectrum, usedframes));
6495
6496 /* Create dummy association keywords. */
6497 int assoc_count;
6498 check(assoc_count = xsh_parameters_get_int(parameters, recipe_id,
6499 "dummy-association-keys"));
6500 int assoi;
6501 for (assoi = 1; assoi <= assoc_count; ++assoi) {
6502 check(irplib_sdp_spectrum_set_asson(sdp_spectrum, assoi, " "));
6503 check(irplib_sdp_spectrum_set_assoc(sdp_spectrum, assoi, " "));
6504 check(irplib_sdp_spectrum_set_assom(sdp_spectrum, assoi, " "));
6505 }
6506
6507 cpl_msg_info(cpl_func, "Writing SDP spectrum as FITS table product(%s): %s",
6508 output_procatg, filename);
6509// printf("Press ENTER to continue\n");
6510// getchar();
6511 if(qclist != NULL && primarykeys != NULL){
6512 if(!cpl_propertylist_has(primarykeys,"ESO QC FLUX SN") &&
6513 cpl_propertylist_has(primarykeys,"ESO QC FLUX1 SN") &&
6514 cpl_propertylist_has(primarykeys,"ESO QC FLUX2 SN")){
6515 double avg = 0.5*(cpl_propertylist_get_double(primarykeys,"ESO QC FLUX1 SN") + cpl_propertylist_get_double(primarykeys,"ESO QC FLUX2 SN"));
6516 cpl_propertylist_append_double(qclist,"ESO QC FLUX SN",avg);
6517 }
6518 }
6519
6520 /* Add any extra QC values to the primary header */
6521 if(qclist != NULL){
6522 if(cpl_propertylist_get_size(qclist) > 0){
6523 cpl_propertylist_append(primarykeys,qclist);
6524 }
6525 }
6526
6527 /* Save the filled SDP spectrum to file, but strip the checksum keywords. */
6528 const char* remregexp = "^(CHECKSUM|DATASUM)$";
6529 check(irplib_dfs_save_spectrum(
6530 frameset, NULL, parameters, usedframes, product_frame,
6531 sdp_spectrum, recipe_id, primarykeys, tablekeys, remregexp,
6533
6534 /* Only update the checksums if the CPL version is new enough so that exorex
6535 * will not break the CHECKSUM of the primary header when it updates the
6536 * DATAMD5 in the cpl_dfs_update_product_header() function call. */
6537 if (cpl_version_get_binary_version() >= CPL_VERSION(6, 4, 2)) {
6538 check(irplib_fits_update_checksums(filename));
6539 }
6540
6541 xsh_add_product_file(filename);
6542
6543cleanup:
6544 irplib_sdp_spectrum_delete(sdp_spectrum);
6545 xsh_spectrum_free(&uncal_spectrum);
6546 xsh_spectrum_free(&spectrum);
6547 cpl_free(filename);
6548 cpl_free(output_procatg);
6549 cpl_propertylist_delete(sourcekeys);
6550 cpl_propertylist_delete(primarykeys);
6551 cpl_propertylist_delete(tablekeys);
6552 cpl_table_delete(table);
6553}
6554
static double exptime
static xsh_instrument * instrument
int binx
int biny
cpl_error_code xsh_baryvel_correct_header(cpl_propertylist *header, cpl_propertylist *bary_header, xsh_bary_corr_param *bary_param)
Definition: xsh_baryvel.c:1310
void xsh_pre_3d_free(xsh_pre_3d **pre_3d)
xsh_pre_3d * xsh_pre_3d_load(cpl_frame *frame)
Load a xsh_pre_3d structure from a frame.
cpl_frame * xsh_pre_3d_save(const xsh_pre_3d *pre, const char *filename, int temp)
Save PRE_3D on disk.
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
Definition: xsh_data_pre.c:849
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
double xsh_compute_ron_nir(const double dit)
Definition: xsh_data_pre.c:247
xsh_pre * xsh_pre_subsample(xsh_pre *self, const int binx, const int biny, const int rescale, xsh_instrument *inst)
Subsample a PRE image of a bin factor binx X biny.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
double xsh_spectrum_get_lambda_min(xsh_spectrum *s)
Get minimum lambda of spectrum.
xsh_spectrum * xsh_spectrum_load(cpl_frame *s1d_frame)
Load a 1D spectrum structure.
double * xsh_spectrum_get_errs(xsh_spectrum *s)
Get errs of spectrum.
double xsh_spectrum_get_lambda_step(xsh_spectrum *s)
Get bin in lambda of spectrum.
int * xsh_spectrum_get_qual(xsh_spectrum *s)
Get qual of spectrum.
int xsh_spectrum_get_size(xsh_spectrum *s)
Get size of spectrum.
cpl_frame * xsh_spectrum_save(xsh_spectrum *s, const char *filename, const char *tag)
save a spectrum
double * xsh_spectrum_get_flux(xsh_spectrum *s)
Get flux of spectrum.
double xsh_spectrum_get_lambda_max(xsh_spectrum *s)
Get maximum lambda of spectrum.
void xsh_spectrum_free(xsh_spectrum **s)
free memory associated to an 1D spectrum
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
Definition: xsh_error.h:103
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define xsh_error_msg(...)
Definition: xsh_error.h:94
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
#define passure(CONDITION,...)
Definition: xsh_error.h:82
const char * xsh_instrument_mode_tostring(xsh_instrument *i)
Get the string associated with a mode.
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
XSH_MODE xsh_instrument_get_mode(xsh_instrument *i)
Get a mode on instrument structure.
void xsh_instrument_set_biny(xsh_instrument *instrument, const int biny)
const char * xsh_arm_tostring(XSH_ARM arm)
Get the string associated with an arm.
void xsh_instrument_set_binx(xsh_instrument *instrument, const int binx)
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
void xsh_instrument_parse_tag(xsh_instrument *inst, const char *tag)
analyse a frame tag to set data in instrument structure
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
xsh_instrument * xsh_instrument_new(void)
create new instrument structure
int size
DOUBLE ** ref
int * x
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
#define xsh_msg_debug(...)
Print a debug message.
Definition: xsh_msg.h:99
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
#define xsh_msg_dbg_high(...)
Definition: xsh_msg.h:40
cpl_error_code xsh_plist_set_extra_keys(cpl_propertylist *plist, const char *hduclas1, const char *hduclas2, const char *hduclas3, const char *scidata, const char *errdata, const char *qualdata, const int type)
set hdu keys
Definition: xsh_pfits.c:4250
double xsh_pfits_get_ra_cumoffset(const cpl_propertylist *plist)
Definition: xsh_pfits.c:3585
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
int xsh_pfits_get_binx(const cpl_propertylist *plist)
find out the BINX value
Definition: xsh_pfits.c:289
double xsh_pfits_get_det_win1_uit1(const cpl_propertylist *plist)
find out the user defined exposure time
Definition: xsh_pfits.c:2274
double xsh_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
Definition: xsh_pfits.c:1405
void xsh_pfits_set_extname(cpl_propertylist *plist, const char *value)
Write the EXTNAME value.
Definition: xsh_pfits.c:979
double xsh_pfits_get_posang(const cpl_propertylist *plist)
Definition: xsh_pfits.c:3513
double xsh_pfits_get_dec_cumoffset(const cpl_propertylist *plist)
Definition: xsh_pfits.c:3600
const char * xsh_pfits_get_date_obs(const cpl_propertylist *plist)
find out the DATE
Definition: xsh_pfits.c:1746
cpl_error_code xsh_plist_div_by_fct(cpl_propertylist **plist, const int fctx, const int fcty)
Definition: xsh_pfits.c:4417
int xsh_pfits_get_biny(const cpl_propertylist *plist)
find out the BINY value
Definition: xsh_pfits.c:306
int xsh_pfits_get_obs_id(cpl_propertylist *plist)
find out the OBS ID
Definition: xsh_pfits.c:3388
double xsh_pfits_get_mjdobs(const cpl_propertylist *plist)
Find out the modified julian observation date.
Definition: xsh_pfits.c:186
cpl_error_code xsh_pfits_combine_headers(cpl_propertylist *header, cpl_frameset *set)
Definition: xsh_pfits.c:4431
int xsh_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
Definition: xsh_pfits.c:209
double xsh_pfits_get_cumoffsety(const cpl_propertylist *plist)
Definition: xsh_pfits.c:3571
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
char * xsh_stringcat_4(const char *s1, const char *s2, const char *s3, const char *s4)
String concatenation.
Definition: xsh_utils.c:1791
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
int xsh_fileutils_move(const char *srcpath, const char *dstpath)
Definition: xsh_utils.c:534
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
Definition: xsh_utils.c:2254
char * xsh_stringcat_6(const char *s1, const char *s2, const char *s3, const char *s4, const char *s5, const char *s6)
String concatenation.
Definition: xsh_utils.c:1875
char * xsh_stringcat_3(const char *s1, const char *s2, const char *s3)
String concatenation.
Definition: xsh_utils.c:1753
char * xsh_get_basename(const char *filename)
Return base filename.
Definition: xsh_utils.c:1175
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
Definition: xsh_utils.c:1925
int xsh_time_stamp_get(void)
get timestamp
Definition: xsh_utils.c:3196
char * xsh_stringdup(const char *s)
String duplication.
Definition: xsh_utils.c:1658
char * xsh_sdate_utc(time_t *now)
Definition: xsh_utils.c:1691
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_add_product_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1472
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
Definition: xsh_utils.c:2164
void xsh_free(const void *mem)
Deallocate memory.
Definition: xsh_utils.c:2102
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
const char * name
Definition: xsh_dfs.c:5558
const char * format
Definition: xsh_dfs.c:5561
const char * tucd
Definition: xsh_dfs.c:5563
const char * unit
Definition: xsh_dfs.c:5560
cpl_type type
Definition: xsh_dfs.c:5559
const char * tutyp
Definition: xsh_dfs.c:5562
const char * pipeline_id
const char * dictionary
XSH_INSTRCONFIG * config
cpl_propertylist * data_header
cpl_propertylist * qual_header
Definition: xsh_data_pre.h:72
cpl_propertylist * errs_header
Definition: xsh_data_pre.h:69
cpl_propertylist * data_header
Definition: xsh_data_pre.h:66
cpl_propertylist * errs_header
cpl_propertylist * flux_header
cpl_image * flux
cpl_propertylist * qual_header
cpl_image * errs
@ XSH_ARM_UNDEFINED
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
@ XSH_MODE_IFU
#define XSH_PRE_DATA_TYPE
Definition: xsh_data_pre.h:42
#define XSH_PRE_QUAL_BPP
Definition: xsh_data_pre.h:47
#define XSH_PRE_DATA_BPP
Definition: xsh_data_pre.h:43
#define XSH_SPECTRUM_DATA_BPP
#define XSH_SPECTRUM_QUAL_BPP
#define XSH_SPECTRUM_ERRS_BPP
cpl_boolean rescale
cpl_frame * xsh_find_wavemap(cpl_frameset *frames, xsh_instrument *instr)
Find Wave Map frame. The frame returned should not be free by the caller.
Definition: xsh_dfs.c:3983
void xsh_dfs_split_qth_d2(cpl_frameset *input, cpl_frameset **qth, cpl_frameset **d2)
Extracts QTH and D2 frames from input frameset.
Definition: xsh_dfs.c:1241
cpl_error_code xsh_dfs_fix_key_start_end(cpl_frameset *set, cpl_propertylist *head)
Definition: xsh_dfs.c:95
cpl_frame * xsh_find_bpmap(cpl_frameset *set)
find the bad pixel map in a set of files
Definition: xsh_dfs.c:1595
cpl_frame * xsh_find_order_tab_centr(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab CENTR.
Definition: xsh_dfs.c:3570
static double xsh_calculate_exptime(const cpl_frameset *usedframes, XSH_ARM arm)
Calculates a value for EXPTIME from the list of used frames.
Definition: xsh_dfs.c:1979
cpl_frame * xsh_find_raw_orderdef_vis_uvb(cpl_frameset *frames)
Find orderdef raw frame for UVB or VIS.
Definition: xsh_dfs.c:4005
cpl_frame * xsh_find_std_star_flux(cpl_frameset *frames)
Find standard standard star flux frame.
Definition: xsh_dfs.c:3294
cpl_error_code xsh_vector_save(cpl_vector *v, cpl_propertylist *ph, const char *fname, const int ext)
Definition: xsh_dfs.c:5185
void xsh_add_product_imagelist(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Definition: xsh_dfs.c:2825
static cpl_error_code xsh_sdp_spectrum_create(irplib_sdp_spectrum *sdp_spectrum, const cpl_frameset *usedframes, const cpl_propertylist *sourcekeys, xsh_instrument *instrument, xsh_spectrum *spectrum, xsh_spectrum *uncal_spectrum)
Converts data into Science Data Product 1D spectrum format.
Definition: xsh_dfs.c:5583
void xsh_add_product_vector(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Definition: xsh_dfs.c:1895
cpl_frame * xsh_find_master_dark(cpl_frameset *frames, xsh_instrument *instr)
Find master dark frame.
Definition: xsh_dfs.c:3404
cpl_frame * xsh_find_spectral_format(cpl_frameset *frames, xsh_instrument *instr)
Find spectral format frame.
Definition: xsh_dfs.c:4318
static cpl_frameset * xsh_correct_frameset_raws_bin(cpl_frameset *input, const int ref_binx, const int ref_biny, xsh_instrument *inst)
Definition: xsh_dfs.c:4612
int xsh_dfs_split_offset(cpl_frameset *input, const char *object_tag, const char *sky_tag, cpl_frameset **object, cpl_frameset **sky)
split input RAW offset sof in OBJECT_... and SKY_... framesets
Definition: xsh_dfs.c:1357
cpl_error_code xsh_calib_nir_respon_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr)
Definition: xsh_dfs.c:4974
cpl_frame * xsh_find_disp_tab(cpl_frameset *frames, xsh_instrument *instr)
Find Dispersol tab frame. The frame returned should not be free by the caller.
Definition: xsh_dfs.c:4066
void xsh_add_product_pre(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instr, cpl_propertylist *qclist)
Add DFS keywords to a product frame.
Definition: xsh_dfs.c:2465
cpl_error_code xsh_dfs_check_binning(cpl_frameset *set, cpl_frameset *calib)
Definition: xsh_dfs.c:4438
cpl_error_code xsh_calib_nir_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr, const char *recid)
Definition: xsh_dfs.c:5014
cpl_frameset * xsh_frameset_drl_frames(cpl_frameset *set)
extract DRL specific frames from frameset
Definition: xsh_dfs.c:319
cpl_frame * xsh_find_wave_tab_guess(cpl_frameset *frames, xsh_instrument *instr)
Find a wave tab guess.
Definition: xsh_dfs.c:3625
cpl_frame * xsh_find_model_wavelist(cpl_frameset *frames, xsh_instrument *instr)
Find model wavelength list frame.
Definition: xsh_dfs.c:4265
cpl_error_code xsh_frameset_merge(cpl_frameset *set1, cpl_frameset *set2)
Extracts raw frames.
Definition: xsh_dfs.c:1014
cpl_frame * xsh_find_usr_lines_guess_tab(cpl_frameset *frames, xsh_instrument *instr)
Find a user guess line positions table frame.
Definition: xsh_dfs.c:3932
cpl_error_code xsh_frameset_uniform_bin(cpl_frameset **raws, cpl_frameset **calib, xsh_instrument *instrument)
Definition: xsh_dfs.c:4664
static void replace_spaces_with_underscores(char *str)
Definition: xsh_dfs.c:74
static char * xsh_create_final_name(const char *final_prefix)
creates file name based on tag prefix
Definition: xsh_dfs.c:1451
cpl_frame * xsh_find_raw_arc_slit_nir_off(cpl_frameset *frames)
Find raw frame for Slit NIR OFF.
Definition: xsh_dfs.c:4164
static double xsh_get_offsety(cpl_propertylist *plist)
Computes offsety.
Definition: xsh_dfs.c:448
void xsh_add_product(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix, const char *type)
Definition: xsh_dfs.c:1761
cpl_frameset * xsh_frameset_extract_offsety_mismatches(cpl_frameset *raws, const double offset)
Extract frames that has different cumoffy than offset.
Definition: xsh_dfs.c:527
cpl_frame * xsh_find_calpro_model_meas_coord(cpl_frameset *frames, xsh_instrument *instr)
Find model config frame.
Definition: xsh_dfs.c:4240
static cpl_error_code xsh_clean_header(cpl_propertylist *header)
Definition: xsh_dfs.c:152
void xsh_add_product_bpmap(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Definition: xsh_dfs.c:2716
cpl_frameset * xsh_frameset_ext_table_frames(cpl_frameset *set)
Extract frameset sub set containing only table frames.
Definition: xsh_dfs.c:207
static void xsh_fill_provenance_keywords(irplib_sdp_spectrum *spectrum, const cpl_frameset *usedframes)
Fills SDP spectrum object with PROVi keywords.
Definition: xsh_dfs.c:5490
cpl_frame * xsh_find_order_tab_edges(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab EDGES.
Definition: xsh_dfs.c:3595
cpl_frame * xsh_find_master_bias(cpl_frameset *frames, xsh_instrument *instr)
Find master bias frame.
Definition: xsh_dfs.c:3319
void xsh_add_product_pre_3d(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instr)
Add 3d product frame.
Definition: xsh_dfs.c:2627
cpl_frame * xsh_find_order_tab_recov(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab RECOV.
Definition: xsh_dfs.c:3520
cpl_frame * xsh_find_raw_orderdef_nir(cpl_frameset *frames)
Find orderdef raw frame for NIR.
Definition: xsh_dfs.c:4043
int xsh_parameter_get_default_flag(const cpl_parameter *p)
Check if an input parameter has been changed by the user.
Definition: xsh_dfs.c:115
static cpl_error_code xsh_calib_nir_mflat_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr)
Definition: xsh_dfs.c:4878
int xsh_print_rec_status(const int val)
Check if an error has happened and returns error kind and location.
Definition: xsh_dfs.c:877
cpl_frame * xsh_find_wave_tab_2d(cpl_frameset *frames, xsh_instrument *instr)
Find a wave tab 2D.
Definition: xsh_dfs.c:3650
cpl_boolean xsh_mode_is_physmod(cpl_frameset *set, xsh_instrument *instrument)
Definition: xsh_dfs.c:4411
cpl_error_code xsh_dfs_extract_calib_frames(cpl_frameset *input, cpl_frameset *calib)
split input sof in groups: raw and calib
Definition: xsh_dfs.c:1144
void xsh_add_product_image(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Definition: xsh_dfs.c:2965
void xsh_dfs_split_in_group(cpl_frameset *input, cpl_frameset *raws, cpl_frameset *calib)
split input sof in groups: raw and calib
Definition: xsh_dfs.c:1202
int xsh_file_exists(const char *filename)
Definition: xsh_dfs.c:959
void xsh_add_product_table(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Save Table product (input frame has several extensions, 1 table per extension)
Definition: xsh_dfs.c:3146
void xsh_dfs_split_nir(cpl_frameset *input, cpl_frameset **on, cpl_frameset **off)
split input RAW NIR sof in ON and OFF
Definition: xsh_dfs.c:1297
static cpl_frame * xsh_frame_pre_subsample(cpl_frame *frm_i, const int binx, const int biny, const int rescale, xsh_instrument *inst)
Definition: xsh_dfs.c:4514
cpl_frame * xsh_find_off(cpl_frameset *set)
find the off frame in set of files
Definition: xsh_dfs.c:1645
static const char * xsh_create_final_tag(const char *pcatg_prefix, xsh_instrument *inst, const cpl_frame *frame)
creates tag value based on tag prefix or uses frame tag
Definition: xsh_dfs.c:1428
cpl_frameset * xsh_find_wave_tab_ifu(cpl_frameset *frames, xsh_instrument *instrument)
Find wave tab ARC (for IFU 3 frames)
Definition: xsh_dfs.c:3756
cpl_frameset * xsh_find_offset_tab_ifu(cpl_frameset *frames, xsh_instrument *instr)
Find offset tab (One for each slitlet)
Definition: xsh_dfs.c:3832
static cpl_error_code xsh_calib_nir_orderpos_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr)
Definition: xsh_dfs.c:4856
static const char * xsh_get_column_unit(cpl_propertylist *header, const char *colname)
Tries to identify the units string to used based in BUNIT.
Definition: xsh_dfs.c:5295
cpl_frameset * xsh_frameset_extract_offsety_matches(cpl_frameset *raws, const double offset)
Extract frames that has same cumoffy as offset.
Definition: xsh_dfs.c:485
cpl_error_code xsh_table_save(cpl_table *t, cpl_propertylist *ph, cpl_propertylist *xh, const char *fname, const int ext)
Definition: xsh_dfs.c:5171
static cpl_error_code xsh_frame_nir_tab_chop_Kband(cpl_frame *frame, const int absordmin, const char *colname)
Definition: xsh_dfs.c:4795
void xsh_add_product_spectrum(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instr, cpl_frame **result_frame, cpl_propertylist *qclist)
Definition: xsh_dfs.c:2067
cpl_frame * xsh_find_slitmap(cpl_frameset *frames, xsh_instrument *instr)
Find a slit map.
Definition: xsh_dfs.c:3673
static cpl_error_code xsh_calib_nir_scired_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr)
Definition: xsh_dfs.c:4949
cpl_frame * xsh_find_raw_arc_slit_uvb_vis(cpl_frameset *frames, XSH_ARM arm)
Find raw frame for Slit UVB/VIS.
Definition: xsh_dfs.c:4118
cpl_frame * xsh_find_order_tab_guess(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab GUESS.
Definition: xsh_dfs.c:3545
static bool xsh_dfs_tag_check(const char *tag, const char *tag_list[], int size)
check if the tag is in the tag list
Definition: xsh_dfs.c:1486
cpl_frameset * xsh_frameset_ext_image_frames(cpl_frameset *set)
Extract frameset sub set containing only table frames.
Definition: xsh_dfs.c:286
int xsh_dfs_files_dont_exist(cpl_frameset *frameset)
Check if all SOF files exist.
Definition: xsh_dfs.c:245
static cpl_error_code xsh_calib_nir_predict_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr)
Definition: xsh_dfs.c:4845
cpl_frame * xsh_find_std_flux(cpl_frameset *frames)
Find standard star frame.
Definition: xsh_dfs.c:3262
void xsh_add_product_orders_spectrum(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instr, cpl_frame **result_frame)
Definition: xsh_dfs.c:2260
cpl_frame * xsh_find_theo_tab_mult(cpl_frameset *frames, xsh_instrument *instr)
Find a theoretical tab multi pinhole in SOF.
Definition: xsh_dfs.c:3468
cpl_error_code xsh_dfs_extract_raw_frames(cpl_frameset *input, cpl_frameset *raws)
split input sof in groups: raw and calib
Definition: xsh_dfs.c:1117
xsh_instrument * xsh_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset and return the instrument detected.
Definition: xsh_dfs.c:1046
cpl_frame * xsh_find_wave_tab(cpl_frameset *frames, xsh_instrument *instr)
Find a wave tab 2D or a wave tab ARC.
Definition: xsh_dfs.c:3699
void xsh_dfs_find_flat(cpl_frameset *raws, cpl_frameset *flat)
find the flat set of files in the input SOF and detect the mode of instrument. This function assure t...
Definition: xsh_dfs.c:1693
cpl_propertylist * xsh_frame_head_extract_qc(cpl_frame *frm)
Definition: xsh_dfs.c:4401
static int xsh_get_nraws(const cpl_frameset *frames)
Calculates number of raw frames.
Definition: xsh_dfs.c:5320
void xsh_dfs_filter(cpl_frameset *set, const char *tags[], int size)
filter the frameset with the given tag list
Definition: xsh_dfs.c:1511
char * xsh_get_tag_from_arm(const char *tag, xsh_instrument *instr)
Find arm specific tag from base and instrument setting.
Definition: xsh_dfs.c:3374
cpl_error_code xsh_validate_model_cfg(cpl_frame *mod, cpl_frameset *set)
Validate model cfg.
Definition: xsh_dfs.c:169
cpl_frame * xsh_find_model_config_tab(cpl_frameset *frames, xsh_instrument *instr)
Find a model configuration table frame.
Definition: xsh_dfs.c:3957
cpl_frame * xsh_find_model_testpar(cpl_frameset *frames, xsh_instrument *instr)
Find model test parameter list.
Definition: xsh_dfs.c:4292
cpl_frameset * xsh_frameset_extract_pre(cpl_frameset *frameset, const char *prefix)
Extracts pre frames from a frameset.
Definition: xsh_dfs.c:1556
static cpl_frameset * xsh_correct_frameset_calib_bin(cpl_frameset *input, const int ref_binx, const int ref_biny, xsh_instrument *inst)
Definition: xsh_dfs.c:4560
cpl_error_code xsh_add_afc_info(cpl_frame *frm_m, cpl_frame *frm_o)
Definition: xsh_dfs.c:5199
static double xsh_calculate_mjd_end(const cpl_frame *product_frame, const cpl_frameset *usedframes, XSH_ARM arm)
Calculates a value for MJD-END from the list of used frames.
Definition: xsh_dfs.c:5356
cpl_frame * xsh_ensure_raws_number_is_even(cpl_frameset *raws)
Make sure input frames is an even number (eventually removes the last from the input list)
Definition: xsh_dfs.c:792
static cpl_frame * xsh_find_frame(cpl_frameset *frames, const char *tags[])
Find a frame.
Definition: xsh_dfs.c:4350
cpl_frame * xsh_find_theo_tab_sing(cpl_frameset *frames, xsh_instrument *instr)
Find a theoretical tab signle pinhole in SOF.
Definition: xsh_dfs.c:3445
cpl_frameset * xsh_extract_obj_and_sky_pairs(cpl_frameset *raws, xsh_instrument *instrument)
Make sure input frames is composed by obj-sky pairs.
Definition: xsh_dfs.c:699
void xsh_add_sdp_product_spectrum(const cpl_frame *flux_cal_frame, const cpl_frame *uncal_frame, cpl_frameset *frameset, const cpl_frameset *usedframes, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, cpl_propertylist *qclist)
Creates a 1D spectrum product in the Science Data Product format.
Definition: xsh_dfs.c:6326
cpl_frame * xsh_find_model_config_open(cpl_frameset *frames, xsh_instrument *instr)
Find model config frame.
Definition: xsh_dfs.c:4213
cpl_error_code xsh_ensure_raws_input_offset_recipe_is_proper(cpl_frameset **raws, xsh_instrument *instrument)
Make sure input frames is an even number (eventually removes the last from the input list)
Definition: xsh_dfs.c:352
cpl_frame * xsh_find_raw_orderdef_nir_off(cpl_frameset *frames)
Find orderdef raw frame for NIR OFF.
Definition: xsh_dfs.c:4098
cpl_frame * xsh_find_arc_line_list_clean(cpl_frameset *frames, xsh_instrument *instr)
Find an arc lines list clean frame.
Definition: xsh_dfs.c:3882
static cpl_boolean xsh_aperture_string_to_double(const char *string, double *aperture)
Extracts the aperture from a slit string.
Definition: xsh_dfs.c:5249
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
cpl_frame * xsh_find_arc_line_list(cpl_frameset *frames, xsh_instrument *instr)
Find an arc line list frame.
Definition: xsh_dfs.c:3908
cpl_frameset * xsh_correct_calib(cpl_frameset *raws, cpl_frameset *calib)
Definition: xsh_dfs.c:4728
cpl_frameset * xsh_extract_nod_pairs(cpl_frameset *raws, cpl_vector *offset)
Make sure input frames is composed by nod a-b sequence.
Definition: xsh_dfs.c:584
cpl_frame * xsh_find_on(cpl_frameset *set)
find the on frame in set of files
Definition: xsh_dfs.c:1669
cpl_frame * xsh_find_theo_tab_mode(cpl_frameset *frames, xsh_instrument *instr)
Find a theoretical tab IFU or SINg depending of MODE in SOF.
Definition: xsh_dfs.c:3490
cpl_error_code xsh_dfs_check_mflat_is_proper(xsh_instrument *inst, cpl_frameset **calib)
Definition: xsh_dfs.c:5062
cpl_frame * xsh_find_raw_arc_slit_nir_on(cpl_frameset *frames)
Find raw frame for Slit NIR ON.
Definition: xsh_dfs.c:4144
cpl_error_code xsh_dfs_extract_pro_frames(cpl_frameset *input, cpl_frameset *pros)
split input sof in groups: raw and calib
Definition: xsh_dfs.c:1173
static cpl_error_code xsh_calib_nir_wavecal_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr)
Definition: xsh_dfs.c:4924
cpl_frame * xsh_find_frame_with_tag(cpl_frameset *frames, const char *tag, xsh_instrument *instr)
Find frame with a given tag.
Definition: xsh_dfs.c:3347
cpl_frame * xsh_find_master_flat(cpl_frameset *frames, xsh_instrument *instr)
Find master flat frame.
Definition: xsh_dfs.c:3426
static cpl_error_code xsh_calib_nir_2dmap_corr_if_JH(cpl_frameset *calib, xsh_instrument *instr)
Definition: xsh_dfs.c:4898
static double xsh_find_first_mjd_obs(const cpl_frameset *usedframes)
Finds the earliest value for MJD_OBS in the set of inputs.
Definition: xsh_dfs.c:5434
char * xsh_unique_filename(const char *context, const char *caller_id, const char *pro_catg)
Get unique product filename.
Definition: xsh_dfs.c:1737
void xsh_frame_config(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level, cpl_frame **frame)
Define a frame characteristics.
Definition: xsh_dfs.c:900
cpl_frame * xsh_find_calpro_model_config(cpl_frameset *frames, xsh_instrument *instr)
Find model config frame.
Definition: xsh_dfs.c:4186
cpl_frameset * xsh_frameset_extract(const cpl_frameset *frames, const char *tag)
Extract frames with given tag from frameset.
Definition: xsh_dfs.c:983
cpl_error_code xsh_frameset_check_uniform_exptime(cpl_frameset *raws, xsh_instrument *inst)
Definition: xsh_dfs.c:5119
cpl_frame * xsh_find_model_config(cpl_frameset *frames, xsh_instrument *instr)
Find a model config.
Definition: xsh_dfs.c:3728
cpl_frame * xsh_find_master_bpmap(cpl_frameset *set)
find the master bad pixel map in a set of files
Definition: xsh_dfs.c:1621
#define XSH_WAVE_TAB_ARC_CEN_IFU
Definition: xsh_dfs.h:560
#define XSH_MOD_CFG_OPT_AFC
Definition: xsh_dfs.h:1256
#define XSH_THEO_TAB_SING
Definition: xsh_dfs.h:1084
#define XSH_MOD_CFG
Definition: xsh_dfs.h:1261
#define XSH_GUESS_LINES_POS
Definition: xsh_dfs.h:378
#define XSH_OFFSET_CEN_IFU
Definition: xsh_dfs.h:1120
#define XSH_WAVE_MAP
Definition: xsh_dfs.h:928
#define XSH_MOD_CFG_OPEN
Definition: xsh_dfs.h:1266
#define XSH_MASTER_FLAT
Definition: xsh_dfs.h:569
#define XSH_MODEL_TEST_PAR
Definition: xsh_dfs.h:1212
#define XSH_WAVE_TAB_ARC_DOWN_IFU
Definition: xsh_dfs.h:559
#define XSH_MASTER_BIAS
Definition: xsh_dfs.h:549
#define XSH_MOD_CFG_OPT_2D
Definition: xsh_dfs.h:1233
#define XSH_ARC_SLIT_VIS
Definition: xsh_dfs.h:431
#define XSH_MASTER_DARK
Definition: xsh_dfs.h:550
#define XSH_SKY_SLIT
Definition: xsh_dfs.h:233
#define XSH_OBJECT_SLIT_OFFSET
Definition: xsh_dfs.h:229
#define XSH_ORDER_TAB_EDGES
Definition: xsh_dfs.h:554
#define XSH_STD_FLUX_IFU_OFFSET
Definition: xsh_dfs.h:499
#define XSH_MOD_CFG_TAB
Definition: xsh_dfs.h:1251
#define XSH_OFFSET_UP_IFU
Definition: xsh_dfs.h:1121
#define XSH_SCIENCE_SPECTRUM
Definition: xsh_dfs.h:1303
#define XSH_DISP_TAB
Definition: xsh_dfs.h:897
#define XSH_STD_TELL_SLIT(arm)
Definition: xsh_dfs.h:1486
#define XSH_WAVE_TAB_AFC
Definition: xsh_dfs.h:557
#define XSH_MODEL_WAVE_LIST
Definition: xsh_dfs.h:1207
#define XSH_ANC_SPECTRUM
Definition: xsh_dfs.h:1304
#define XSH_STD_FLUX_SLIT_OFFSET
Definition: xsh_dfs.h:493
#define XSH_STD_TELL_OFFSET(arm)
Definition: xsh_dfs.h:1497
#define XSH_ARC_SLIT_UVB
Definition: xsh_dfs.h:430
#define XSH_MEASCOORD
Definition: xsh_dfs.h:1271
#define XSH_WAVE_TAB_GUESS
Definition: xsh_dfs.h:555
#define XSH_SKY_IFU
Definition: xsh_dfs.h:234
#define XSH_IFU_MAP
Definition: xsh_dfs.h:912
#define XSH_ORDERDEF_VIS
Definition: xsh_dfs.h:411
#define XSH_THEO_TAB_IFU
Definition: xsh_dfs.h:1094
#define XSH_ORDER_TAB_GUESS
Definition: xsh_dfs.h:552
#define XSH_ORDERDEF_OFF
Definition: xsh_dfs.h:413
#define XSH_IS_CALIB(tag)
Definition: xsh_dfs.h:1380
#define XSH_MASTER_BP_MAP
Definition: xsh_dfs.h:643
#define XSH_STD_FLUX_SLIT(arm)
Definition: xsh_dfs.h:1491
#define XSH_ORDER_TAB_AFC
Definition: xsh_dfs.h:712
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_PRODUCT_PREFIX
Definition: xsh_dfs.h:1301
#define XSH_ARC_SLIT_OFF
Definition: xsh_dfs.h:433
#define XSH_SLIT_MAP
Definition: xsh_dfs.h:907
#define XSH_ORDER_TAB_RECOV
Definition: xsh_dfs.h:551
#define XSH_ORDER_TAB_CENTR
Definition: xsh_dfs.h:553
#define XSH_MERGE2D
Definition: xsh_dfs.h:594
#define XSH_OFFSET_DOWN_IFU
Definition: xsh_dfs.h:1119
#define XSH_WAVE_TAB_ARC
Definition: xsh_dfs.h:558
#define XSH_ORDERDEF_NIR
Definition: xsh_dfs.h:412
#define XSH_IS_RAW(tag)
Definition: xsh_dfs.h:1306
#define XSH_MOD_CFG_OPT_FMT
Definition: xsh_dfs.h:1227
#define XSH_ARC_SLIT_NIR
Definition: xsh_dfs.h:432
#define XSH_MERGE1D
Definition: xsh_dfs.h:592
#define XSH_SPECTRAL_FORMAT
Definition: xsh_dfs.h:1196
#define XSH_WAVE_TAB_2D
Definition: xsh_dfs.h:556
#define XSH_OBJECT_IFU_OFFSET
Definition: xsh_dfs.h:231
#define XSH_ORDERDEF_QTH_UVB
Definition: xsh_dfs.h:409
#define XSH_STD_FLUX_OFFSET(arm)
Definition: xsh_dfs.h:1502
#define XSH_MASTER_FLAT_SLIT
Definition: xsh_dfs.h:869
#define XSH_ARC_LINE_LIST
Definition: xsh_dfs.h:955
#define XSH_THEO_TAB_MULT
Definition: xsh_dfs.h:1089
#define XSH_STD_STAR_FLUX(arm)
Definition: xsh_dfs.h:1513
#define XSH_SPECTRAL_FORMAT_JH_NIR
Definition: xsh_dfs.h:1204
#define XSH_GET_TAG_FROM_MODE(TAG, instr)
Definition: xsh_dfs.h:1594
#define XSH_DISP_TAB_AFC
Definition: xsh_dfs.h:902
#define XSH_ORDERDEF_D2_UVB
Definition: xsh_dfs.h:410
#define XSH_WAVE_TAB_ARC_UP_IFU
Definition: xsh_dfs.h:561
#define XSH_GET_TAG_FROM_LAMP(TAG, instr)
Definition: xsh_dfs.h:1608
xsh_bary_corr_param * xsh_parameters_bary_corr_get(const char *recipe_id, cpl_parameterlist *list)
get the barycentric correction parameters in a parameters list
int xsh_parameters_get_int(const cpl_parameterlist *list, const char *recipe_id, const char *name)
#define XSH_SLIT_NIR
Definition: xsh_pfits.h:138
#define XSH_SDP_KEYWORD_FLUXERR_VALUE
Definition: xsh_pfits.h:322
#define XSH_SDP_COLUMN_ERR_FORMAT
Definition: xsh_pfits.h:351
#define XSH_SDP_COLUMN_ERR_UCD
Definition: xsh_pfits.h:353
#define XSH_SDP_COLUMN_FLUX_REDUCED_TYPE
Definition: xsh_pfits.h:366
#define XSH_SDP_COLUMN_ERR_TYPE
Definition: xsh_pfits.h:352
#define XSH_SDP_COLUMN_FLUX_REDUCED_FORMAT
Definition: xsh_pfits.h:365
#define XSH_SDP_KEYWORD_PRODCATG
Definition: xsh_pfits.h:286
#define XSH_SDP_KEYWORD_GAIN
Definition: xsh_pfits.h:330
#define XSH_SDP_COLUMN_QUAL_UCD
Definition: xsh_pfits.h:358
#define XSH_CRVAL1
Definition: xsh_pfits.h:100
#define XSH_DET_NDITSKIP
Definition: xsh_pfits.h:169
#define XSH_SDP_KEYWORD_TELAPSE
Definition: xsh_pfits.h:298
#define XSH_SDP_KEYWORD_PRODLVL_VALUE
Definition: xsh_pfits.h:284
#define XSH_INSTRUME_VALUE
Definition: xsh_pfits.h:279
#define XSH_SDP_COLUMN_FLUX
Definition: xsh_pfits.h:342
#define XSH_BUNIT
Definition: xsh_pfits.h:113
#define XSH_EXPTIME
Definition: xsh_pfits.h:125
#define XSH_ORIGFILE
Definition: xsh_pfits.h:121
#define XSH_SDP_KEYWORD_LAMNLIN
Definition: xsh_pfits.h:328
#define XSH_MJDEND
Definition: xsh_pfits.h:40
#define XSH_SDP_COLUMN_WAVE
Definition: xsh_pfits.h:337
#define XSH_SDP_COLUMN_FLUX_UCD
Definition: xsh_pfits.h:347
#define XSH_SDP_COLUMN_ERR_REDUCED
Definition: xsh_pfits.h:368
#define XSH_SDP_KEYWORD_LAMRMS
Definition: xsh_pfits.h:329
#define XSH_SLIT_VIS
Definition: xsh_pfits.h:137
#define XSH_SDP_COLUMN_WAVE_UCD
Definition: xsh_pfits.h:341
#define XSH_NAXIS1
Definition: xsh_pfits.h:87
#define XSH_RA
Definition: xsh_pfits.h:44
#define XSH_SLIT_UVB
Definition: xsh_pfits.h:136
#define XSH_SDP_COLUMN_WAVE_UNIT
Definition: xsh_pfits.h:338
#define XSH_SDP_COLUMN_QUAL_FORMAT
Definition: xsh_pfits.h:356
#define XSH_SDP_COLUMN_ERR_REDUCED_UCD
Definition: xsh_pfits.h:371
#define XSH_SDP_FLUX_UNIT
Definition: xsh_pfits.h:336
#define XSH_SDP_KEYWORD_EXTNAME_VALUE
Definition: xsh_pfits.h:306
#define XSH_SDP_KEYWORD_OBJECT
Definition: xsh_pfits.h:285
#define XSH_SDP_KEYWORD_ORIGIN
Definition: xsh_pfits.h:287
#define XSH_SDP_COLUMN_QUAL
Definition: xsh_pfits.h:354
#define XSH_SDP_KEYWORD_INHERIT_VALUE
Definition: xsh_pfits.h:308
#define XSH_SDP_KEYWORD_APERTURE
Definition: xsh_pfits.h:297
#define XSH_SDP_COLUMN_FLUX_REDUCED
Definition: xsh_pfits.h:364
#define XSH_SDP_COLUMN_SNR
Definition: xsh_pfits.h:359
#define XSH_SDP_KEYWORD_SPEC_RES
Definition: xsh_pfits.h:325
#define XSH_SDP_COLUMN_QUAL_UNIT
Definition: xsh_pfits.h:355
#define XSH_SDP_COLUMN_ERR
Definition: xsh_pfits.h:348
#define XSH_NOD_CUMULATIVE_OFFSETY
Definition: xsh_pfits.h:226
#define XSH_SDP_KEYWORD_PRODLVL
Definition: xsh_pfits.h:283
#define XSH_DET_NDIT
Definition: xsh_pfits.h:168
#define XSH_PRO_REC1_PIPE_ID
Definition: xsh_pfits.h:50
#define XSH_SDP_KEYWORD_ORIGIN_VALUE
Definition: xsh_pfits.h:288
#define XSH_SDP_KEYWORD_SPEC_ERR
Definition: xsh_pfits.h:326
#define XSH_ARCFILE
Definition: xsh_pfits.h:120
#define XSH_CDELT1
Definition: xsh_pfits.h:96
#define XSH_SDP_COLUMN_SNR_FORMAT
Definition: xsh_pfits.h:361
#define XSH_SDP_COLUMN_SNR_UNIT
Definition: xsh_pfits.h:360
#define XSH_SDP_COLUMN_WAVE_FORMAT
Definition: xsh_pfits.h:339
#define XSH_SDP_KEYWORD_REFERENC_VALUE
Definition: xsh_pfits.h:324
#define XSH_SDP_COLUMN_FLUX_TYPE
Definition: xsh_pfits.h:346
#define XSH_INSTRUME
Definition: xsh_pfits.h:42
#define XSH_SDP_KEYWORD_VOCLASS_VALUE
Definition: xsh_pfits.h:293
#define XSH_DEC
Definition: xsh_pfits.h:45
#define XSH_SDP_KEYWORD_DISPELEM
Definition: xsh_pfits.h:289
#define XSH_MJDOBS
Definition: xsh_pfits.h:39
#define XSH_SDP_KEYWORD_SPECSYS
Definition: xsh_pfits.h:290
#define XSH_OBS_PROG_ID
Definition: xsh_pfits.h:47
#define XSH_SDP_KEYWORD_DETRON
Definition: xsh_pfits.h:331
#define XSH_SDP_COLUMN_QUAL_TYPE
Definition: xsh_pfits.h:357
#define XSH_SDP_KEYWORD_FLUXCAL
Definition: xsh_pfits.h:315
#define XSH_SDP_COLUMN_FLUX_REDUCED_UCD
Definition: xsh_pfits.h:367
#define XSH_SDP_KEYWORD_SPECSYS_VALUE
Definition: xsh_pfits.h:291
#define XSH_SDP_KEYWORD_TOT_FLUX
Definition: xsh_pfits.h:320
#define XSH_SDP_KEYWORD_TITLE
Definition: xsh_pfits.h:296
#define XSH_RON
Definition: xsh_pfits.h:161
#define XSH_SDP_KEYWORD_VOPUB_VALUE
Definition: xsh_pfits.h:295
#define XSH_OBS_TARG_NAME
Definition: xsh_pfits.h:128
#define XSH_SDP_ADU_UNIT
Definition: xsh_pfits.h:335
#define XSH_DPR_TYPE
Definition: xsh_pfits.h:147
#define XSH_SDP_COLUMN_SNR_UCD
Definition: xsh_pfits.h:363
#define XSH_SDP_COLUMN_FLUX_FORMAT
Definition: xsh_pfits.h:345
#define XSH_SDP_COLUMN_SNR_TYPE
Definition: xsh_pfits.h:362
#define XSH_SDP_KEYWORD_TMID
Definition: xsh_pfits.h:299
#define XSH_PRO_TECH
Definition: xsh_pfits.h:48
#define XSH_SDP_COLUMN_WAVE_TYPE
Definition: xsh_pfits.h:340
#define XSH_CONAD
Definition: xsh_pfits.h:162
#define XSH_SDP_COLUMN_ERR_REDUCED_TYPE
Definition: xsh_pfits.h:370
#define XSH_CRPIX1
Definition: xsh_pfits.h:91
#define XSH_SDP_COLUMN_ERR_REDUCED_FORMAT
Definition: xsh_pfits.h:369
#define XSH_SDP_KEYWORD_SPEC_SYE
Definition: xsh_pfits.h:327
#define XSH_SDP_KEYWORD_EFFRON
Definition: xsh_pfits.h:332
#define XSH_DET_DIT
Definition: xsh_pfits.h:167
#define XSH_OBS_ID
Definition: xsh_pfits.h:127
#define XSH_QC_FLUX_SN
Definition: xsh_pfits_qc.h:117
#define XSH_QC_AFC_XSHIFT_C
Definition: xsh_pfits_qc.h:60
#define XSH_QC_AFC_YSHIFT
Definition: xsh_pfits_qc.h:58
#define XSH_QC_AFC_XSHIFT
Definition: xsh_pfits_qc.h:57
#define XSH_QC_AFC_YSHIFT_C
Definition: xsh_pfits_qc.h:61
#define XSH_CRDER1
#define XSH_CSYER1
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define M_PI
Definition: xsh_utils.h:43
#define XSH_NEW_FRAMESET(POINTER)
Definition: xsh_utils.h:84
cpl_frame * xsh_frame_image_mult_by_fct(cpl_frame *frm, const int fctx, const int fcty)
cpl_frame * xsh_frame_image_div_by_fct(cpl_frame *frm, const int fctx, const int fcty)
#define XSH_TABLE_FREE(TABLE)