X-shooter Pipeline Reference Manual 3.8.15
xsh_data_spectrum.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-03-01 17:09:41 $
23 * $Revision: 1.29 $
24 * $Name: not supported by cvs2svn $
25 */
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
41/*-----------------------------------------------------------------------------
42 Includes
43 ----------------------------------------------------------------------------*/
44
45#include <math.h>
46#include <xsh_data_spectrum.h>
47#include <xsh_utils.h>
48#include <xsh_error.h>
49#include <xsh_msg.h>
50#include <xsh_pfits.h>
51#include <xsh_dfs.h>
52#include <cpl.h>
53
54/*----------------------------------------------------------------------------
55 Function implementation
56 ----------------------------------------------------------------------------*/
57
58/*---------------------------------------------------------------------------*/
68/*---------------------------------------------------------------------------*/
69xsh_spectrum* xsh_spectrum_1D_create( double lambda_min, double lambda_max,
70 double lambda_step)
71{
72 xsh_spectrum* result = NULL;
73
74
75 /* check input parameters */
76 XSH_ASSURE_NOT_ILLEGAL( lambda_min >= 0.0 && lambda_min <= lambda_max);
78
79 XSH_CALLOC(result, xsh_spectrum,1);
80
81 result->lambda_min = lambda_min;
82 result->lambda_max = lambda_max;
83 result->lambda_step = lambda_step;
84
86 check(xsh_pfits_set_wcs1(result->flux_header, 1.0, lambda_min, lambda_step));
87
89 check( xsh_pfits_set_extname ( result->errs_header, "ERRS"));
90 check(xsh_pfits_set_wcs1(result->errs_header, 1.0, lambda_min, lambda_step));
91
93 check( xsh_pfits_set_extname ( result->qual_header, "QUAL"));
94
95
96 result->size_lambda = (int)((lambda_max-lambda_min)/lambda_step+0.5)+1;
97 result->size_slit = 1;
98 result->slit_min = 0;
99 result->slit_max = 0;
100 result->size = result->size_lambda;
101
102 check( result->flux = cpl_image_new( result->size_lambda, 1,
103 CPL_TYPE_DOUBLE));
104 check( result->errs = cpl_image_new( result->size_lambda, 1,
105 CPL_TYPE_DOUBLE));
106 check( result->qual = cpl_image_new( result->size_lambda, 1,
107 CPL_TYPE_INT));
108
109 cleanup:
110 if (cpl_error_get_code() != CPL_ERROR_NONE) {
111 xsh_spectrum_free(&result);
112 }
113 return result;
114}
115
116/*---------------------------------------------------------------------------*/
129/*---------------------------------------------------------------------------*/
130xsh_spectrum* xsh_spectrum_2D_create( double lambda_min, double lambda_max,
131 double lambda_step, double slit_min, double slit_max, double slit_step)
132{
133 xsh_spectrum* result = NULL;
134
135 /* check input parameters */
136 XSH_ASSURE_NOT_ILLEGAL( lambda_min >= 0.0 && lambda_min <= lambda_max);
138 XSH_ASSURE_NOT_ILLEGAL( slit_min <= slit_max);
140
141
142 XSH_CALLOC(result, xsh_spectrum,1);
143
144 result->lambda_min = lambda_min;
145 result->lambda_max = lambda_max;
146 result->lambda_step = lambda_step;
147 result->slit_min = slit_min;
148 result->slit_max = slit_max;
149 result->slit_step = slit_step;
150
152 check(xsh_pfits_set_wcs1(result->flux_header, 1.0, lambda_min, lambda_step));
153 check(xsh_pfits_set_wcs2(result->flux_header, 1.0, slit_min, slit_step));
154
156
158 check( xsh_pfits_set_extname ( result->errs_header, "ERRS"));
160 check( xsh_pfits_set_extname ( result->qual_header, "QUAL"));
161
162
163 result->size_lambda = (int)((lambda_max-lambda_min)/lambda_step+0.5)+1;
164 result->size_slit = (int)((slit_max-slit_min)/slit_step+0.5)+1;
165 result->size = result->size_lambda * result->size_slit;
166 check( result->flux = cpl_image_new( result->size_lambda, result->size_slit,
167 CPL_TYPE_DOUBLE));
168 check( result->errs = cpl_image_new( result->size_lambda, result->size_slit,
169 CPL_TYPE_DOUBLE));
170 check( result->qual = cpl_image_new( result->size_lambda, result->size_slit,
171 CPL_TYPE_INT));
172
173 cleanup:
174 if (cpl_error_get_code() != CPL_ERROR_NONE) {
175 xsh_spectrum_free(&result);
176 }
177 return result;
178}
179
180
181/*---------------------------------------------------------------------------*/
188/*---------------------------------------------------------------------------*/
189xsh_spectrum* xsh_spectrum_load( cpl_frame* s1d_frame)
190{
191 xsh_spectrum* result = NULL;
192 const char *s1dname = NULL;
193 int naxis;
194
195 XSH_ASSURE_NOT_NULL( s1d_frame);
196
197 XSH_ASSURE_NOT_ILLEGAL(cpl_frame_get_nextensions(s1d_frame) == 2);
198
199 check( s1dname = cpl_frame_get_filename( s1d_frame));
200
201 XSH_CALLOC(result, xsh_spectrum,1);
202
203 check( result->flux_header = cpl_propertylist_load( s1dname,0));
204 check( result->errs_header = cpl_propertylist_load( s1dname,1));
205 check( result->qual_header = cpl_propertylist_load( s1dname,2));
206
207 check( result->lambda_min = xsh_pfits_get_crval1( result->flux_header));
209 check( result->size = xsh_pfits_get_naxis1( result->flux_header));
211 result->lambda_max = result->lambda_min+
212 result->lambda_step*(result->size-1);
213
214 check( naxis = xsh_pfits_get_naxis( result->flux_header));
215
216 if (naxis > 1){
217 check( result->slit_min = xsh_pfits_get_crval2( result->flux_header));
218 check( result->slit_step = xsh_pfits_get_cdelt2( result->flux_header));
219 check( result->size_slit = xsh_pfits_get_naxis2( result->flux_header));
220 result->slit_max = result->slit_min+
221 result->slit_step*(result->size_slit-1);
222
223 check( result->flux = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0, 0));
224 check( result->errs = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0, 1));
225 check( result->qual = cpl_image_load( s1dname, CPL_TYPE_INT, 0, 2));
226 }
227 else{
228 double *flux_data = NULL;
229 double *errs_data = NULL;
230 int *qual_data = NULL;
231 int i;
232 cpl_vector *flux = NULL;
233 cpl_vector *errs = NULL;
234 cpl_vector *qual = NULL;
235 int size = 0;
236
237 check( flux = cpl_vector_load( s1dname, 0));
238 check( size = cpl_vector_get_size( flux));
239 check( errs = cpl_vector_load( s1dname, 1));
240 check( qual = cpl_vector_load( s1dname, 2));
241 check( flux_data = cpl_vector_get_data( flux));
242 check( result->flux = cpl_image_wrap_double( size, 1 , flux_data));
243 xsh_unwrap_vector( &flux);
244
245 check( errs_data = cpl_vector_get_data( errs));
246 check( result->errs = cpl_image_wrap_double( size, 1, errs_data));
247
248 check( result->qual = cpl_image_new ( size, 1, CPL_TYPE_INT));
249 check( qual_data = cpl_image_get_data_int( result->qual));
250 for(i=0; i< size; i++){
251 check( qual_data[i] = (int)cpl_vector_get(qual,i));
252 }
253 xsh_unwrap_vector( &errs);
254 xsh_free_vector( &qual);
255 }
256 cleanup:
257 if (cpl_error_get_code() != CPL_ERROR_NONE) {
258 xsh_spectrum_free(&result);
259 }
260 return result;
261}
262
263
264/*---------------------------------------------------------------------------*/
273/*---------------------------------------------------------------------------*/
274xsh_spectrum* xsh_spectrum_load_order( cpl_frame* s1d_frame,
275 xsh_instrument* instr,
276 const int order)
277{
278 xsh_spectrum* result = NULL;
279 const char *s1dname = NULL;
280 int naxis;
281
282 XSH_ASSURE_NOT_NULL( s1d_frame);
283 XSH_ASSURE_NOT_NULL( instr);
284
285 //XSH_ASSURE_NOT_ILLEGAL(cpl_frame_get_nextensions(s1d_frame) == 2);
286
287 check( s1dname = cpl_frame_get_filename( s1d_frame));
288
289 XSH_CALLOC(result, xsh_spectrum,1);
290
291 check( result->flux_header = cpl_propertylist_load( s1dname,order+0));
292 check( result->errs_header = cpl_propertylist_load( s1dname,order+1));
293 check( result->qual_header = cpl_propertylist_load( s1dname,order+2));
294
295 check( result->lambda_min = xsh_pfits_get_crval1( result->flux_header));
297 check( result->size = xsh_pfits_get_naxis1( result->flux_header));
299 result->lambda_max = result->lambda_min+
300 (result->lambda_step*result->size-1);
301
302 check( naxis = xsh_pfits_get_naxis( result->flux_header));
303
304 if (naxis > 1){
305 check( result->slit_min = xsh_pfits_get_crval2( result->flux_header));
306 check( result->slit_step = xsh_pfits_get_cdelt2( result->flux_header));
307 check( result->size_slit = xsh_pfits_get_naxis2( result->flux_header));
308 result->slit_max = result->slit_min+
309 result->slit_step*(result->size_slit-1);
310
311 check( result->flux = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0,order+0));
312 check( result->errs = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0,order+1));
313 check( result->qual = cpl_image_load( s1dname, CPL_TYPE_INT, 0, order+2));
314 }
315 else{
316 double *flux_data = NULL;
317 double *errs_data = NULL;
318 int *qual_data = NULL;
319 int i;
320 cpl_vector *flux = NULL;
321 cpl_vector *errs = NULL;
322 cpl_vector *qual = NULL;
323 int size = 0;
324
325 check( flux = cpl_vector_load( s1dname, order+0));
326 check( size = cpl_vector_get_size( flux));
327 check( errs = cpl_vector_load( s1dname, order+1));
328 check( qual = cpl_vector_load( s1dname, order+2));
329 check( flux_data = cpl_vector_get_data( flux));
330 check( result->flux = cpl_image_wrap_double( size, 1 , flux_data));
331 xsh_unwrap_vector( &flux);
332
333 check( errs_data = cpl_vector_get_data( errs));
334 check( result->errs = cpl_image_wrap_double( size, 1, errs_data));
335
336 check( result->qual = cpl_image_new ( size, 1, CPL_TYPE_INT));
337 check( qual_data = cpl_image_get_data_int( result->qual));
338 for(i=0; i< size; i++){
339 check( qual_data[i] = (int)cpl_vector_get(qual,i));
340 }
341 xsh_unwrap_vector( &errs);
342 xsh_free_vector( &qual);
343 }
344 cleanup:
345 if (cpl_error_get_code() != CPL_ERROR_NONE) {
346 xsh_spectrum_free(&result);
347 }
348 return result;
349}
350
351/*---------------------------------------------------------------------------*/
359/*---------------------------------------------------------------------------*/
361{
362 int res=0;
363
365
366 res = s->size;
367
368 cleanup:
369 return res;
370}
371
372/*---------------------------------------------------------------------------*/
380/*---------------------------------------------------------------------------*/
382{
383 int res=0;
384
386
387 res = s->size_lambda;
388
389 cleanup:
390 return res;
391}
392
393
394/*---------------------------------------------------------------------------*/
402/*---------------------------------------------------------------------------*/
404{
405 int res=0;
406
408
409 res = s->size_slit;
410
411 cleanup:
412 return res;
413}
414
415/*---------------------------------------------------------------------------*/
423/*---------------------------------------------------------------------------*/
425{
426 double res=0.0;
427
429
430 res = s->lambda_min;
431
432 cleanup:
433 return res;
434}
435
436/*---------------------------------------------------------------------------*/
444/*---------------------------------------------------------------------------*/
446{
447 double res=0.0;
448
450
451 res = s->lambda_max;
452
453 cleanup:
454 return res;
455}
456
457/*---------------------------------------------------------------------------*/
465/*---------------------------------------------------------------------------*/
467{
468 double res=0.0;
469
471
472 res = s->lambda_step;
473
474 cleanup:
475 return res;
476}
477
478
479/*---------------------------------------------------------------------------*/
487/*---------------------------------------------------------------------------*/
489{
490 double *res=NULL;
491
493
494 check( res = cpl_image_get_data_double( s->flux));
495
496 cleanup:
497 return res;
498}
499
500
501/*---------------------------------------------------------------------------*/
509/*---------------------------------------------------------------------------*/
511{
512 double *res=NULL;
513
515
516 check( res = cpl_image_get_data_double( s->errs));
517
518 cleanup:
519 return res;
520}
521
522
523/*---------------------------------------------------------------------------*/
531/*---------------------------------------------------------------------------*/
533{
534 int* res = NULL;
535
537
538 check( res = cpl_image_get_data_int( s->qual));
539
540 cleanup:
541 return res;
542}
543
544/*---------------------------------------------------------------------------*/
552/*---------------------------------------------------------------------------*/
554{
555
557
558 cleanup:
559 return s->flux;
560}
561
562/*---------------------------------------------------------------------------*/
570/*---------------------------------------------------------------------------*/
572{
573
575
576 cleanup:
577 return s->errs;
578}
579
580
581/*---------------------------------------------------------------------------*/
589/*---------------------------------------------------------------------------*/
591{
592
594
595 cleanup:
596 return s->qual;
597}
598
599
600/*---------------------------------------------------------------------------*/
606/*---------------------------------------------------------------------------*/
608{
609 if (s && *s){
610 xsh_free_propertylist( &((*s)->flux_header));
611 xsh_free_propertylist( &((*s)->errs_header));
612 xsh_free_propertylist( &((*s)->qual_header));
613 xsh_free_image( &((*s)->flux));
614 xsh_free_image( &((*s)->errs));
615 xsh_free_image( &((*s)->qual));
616 XSH_FREE( (*s));
617 }
618}
619
620/*---------------------------------------------------------------------------*/
630/*---------------------------------------------------------------------------*/
631cpl_frame* xsh_spectrum_save( xsh_spectrum* s, const char* filename,
632 const char* tag)
633{
634 cpl_frame *product_frame = NULL;
635
637 XSH_ASSURE_NOT_NULL(filename);
638
639 check( xsh_pfits_set_extname(s->flux_header , "FLUX"));
640 check(xsh_plist_set_extra_keys(s->flux_header,"IMAGE","DATA","RMSE",
641 "FLUX","ERRS","QUAL",0));
642
643 check( xsh_pfits_set_extname(s->errs_header , "ERRS"));
644 check(xsh_plist_set_extra_keys(s->errs_header,"IMAGE","DATA","RMSE",
645 "FLUX","ERRS","QUAL",1));
646
647 check( xsh_pfits_set_extname(s->qual_header , "QUAL"));
648 check(xsh_plist_set_extra_keys(s->qual_header,"IMAGE","DATA","RMSE",
649 "FLUX","ERRS","QUAL",2));
650
651 /* Save the file */
652 if ( s->size_slit > 1){
653
654 double crval1=0;
655 double crpix1=0;
656 double cdelt1=0;
657
658 double crval2=0;
659 double crpix2=0;
660 double cdelt2=0;
661
662 crval1=xsh_pfits_get_crval1(s->flux_header);
663 crpix1=xsh_pfits_get_crpix1(s->flux_header);
664 cdelt1=xsh_pfits_get_cdelt1(s->flux_header);
665
666 crval2=xsh_pfits_get_crval2(s->flux_header);
667 crpix2=xsh_pfits_get_crpix2(s->flux_header);
668 cdelt2=xsh_pfits_get_cdelt2(s->flux_header);
669
670 xsh_pfits_set_wcs(s->errs_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
671 xsh_pfits_set_wcs(s->qual_header,crpix1,crval1,cdelt1,crpix2,crval2,cdelt2);
672
673 check( xsh_pfits_set_pcatg( s->flux_header, tag));
674 check_msg (cpl_image_save ( s->flux, filename, XSH_SPECTRUM_DATA_BPP,
675 s->flux_header, CPL_IO_DEFAULT),
676 "Could not save data to %s extension 0", filename);
677 check_msg (cpl_image_save ( s->errs, filename, XSH_SPECTRUM_ERRS_BPP,
678 s->errs_header, CPL_IO_EXTEND),
679 "Could not save errs to %s extension 1", filename);
680 check_msg (cpl_image_save ( s->qual, filename, XSH_SPECTRUM_QUAL_BPP,
681 s->qual_header, CPL_IO_EXTEND),
682 "Could not save qual to %s extension 2", filename);
683 }
684 else{
685 cpl_vector *flux1D = NULL;
686 cpl_vector *err1D = NULL;
687 cpl_vector *qual1D = NULL;
688
689 double crval1=0;
690 double crpix1=0;
691 double cdelt1=0;
692
693 crval1=xsh_pfits_get_crval1(s->flux_header);
694 crpix1=xsh_pfits_get_crpix1(s->flux_header);
695 cdelt1=xsh_pfits_get_cdelt1(s->flux_header);
696
697
698
699 xsh_pfits_set_ctype1(s->flux_header,"LINEAR");
700 xsh_pfits_set_cunit1(s->flux_header,"nm");
701 cpl_propertylist_erase_regexp(s->flux_header, "^CTYPE2", 0);
702
703
704
705 check(xsh_pfits_set_wcs1(s->errs_header, crpix1, crval1, cdelt1));
706 xsh_pfits_set_cunit1(s->errs_header,"nm");
707
708 check(xsh_pfits_set_wcs1(s->qual_header, crpix1, crval1, cdelt1));
709 xsh_pfits_set_cunit1(s->qual_header,"nm");
711
712 check( flux1D = cpl_vector_new_from_image_row( s->flux, 1));
713 check( err1D = cpl_vector_new_from_image_row( s->errs, 1));
714 check( qual1D = cpl_vector_new_from_image_row( s->qual, 1));
715 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
716 s->flux_header, CPL_IO_DEFAULT));
717 check( cpl_vector_save( err1D, filename, XSH_SPECTRUM_ERRS_BPP,
718 s->errs_header, CPL_IO_EXTEND));
719 check( cpl_vector_save( qual1D, filename, XSH_SPECTRUM_QUAL_BPP,
720 s->qual_header, CPL_IO_EXTEND));
721 xsh_free_vector( &flux1D);
722 xsh_free_vector( &err1D);
723 xsh_free_vector( &qual1D);
724 }
725
726
727 check( product_frame = cpl_frame_new() ) ;
728 check( cpl_frame_set_filename( product_frame,filename ));
729 check( cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE )) ;
730 check( cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL )) ;
731 check( cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT ));
732
733 cleanup:
734 if (cpl_error_get_code () != CPL_ERROR_NONE) {
735 xsh_free_frame(&product_frame);
736 product_frame = NULL;
737 }
738 return product_frame;
739}
740
741
742
743
744/*---------------------------------------------------------------------------*/
755/*---------------------------------------------------------------------------*/
756cpl_frame* xsh_spectrum_save_order( xsh_spectrum* s, const char* filename,
757 const char* tag,const int order)
758{
759 cpl_frame *product_frame = NULL;
760
762 XSH_ASSURE_NOT_NULL(filename);
763
764
765 /* Save the file */
766 if ( s->size_slit > 1){
767
768 check( xsh_pfits_set_pcatg( s->flux_header, tag));
769 if(order==0) {
770 check_msg (cpl_image_save ( s->flux, filename, XSH_SPECTRUM_DATA_BPP,
771 s->flux_header, CPL_IO_DEFAULT),
772 "Could not save data to %s extension 0", filename);
773 } else {
774 check_msg (cpl_image_save ( s->errs, filename, XSH_SPECTRUM_ERRS_BPP,
775 s->errs_header, CPL_IO_EXTEND),
776 "Could not save errs to %s extension 1", filename);
777 check_msg (cpl_image_save ( s->qual, filename, XSH_SPECTRUM_QUAL_BPP,
778 s->qual_header, CPL_IO_EXTEND),
779 "Could not save qual to %s extension 2", filename);
780 }
781 }
782 else{
783 cpl_vector *flux1D = NULL;
784 cpl_vector *err1D = NULL;
785 cpl_vector *qual1D = NULL;
786
787 check( flux1D = cpl_vector_new_from_image_row( s->flux, 1));
788 check( err1D = cpl_vector_new_from_image_row( s->errs, 1));
789 check( qual1D = cpl_vector_new_from_image_row( s->qual, 1));
790 if(order==0) {
791 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
792 s->flux_header, CPL_IO_DEFAULT));
793 } else {
794 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
795 s->flux_header, CPL_IO_EXTEND));
796 }
797 check( cpl_vector_save( err1D, filename, XSH_SPECTRUM_ERRS_BPP,
798 s->errs_header, CPL_IO_EXTEND));
799 check( cpl_vector_save( qual1D, filename, XSH_SPECTRUM_QUAL_BPP,
800 s->qual_header, CPL_IO_EXTEND));
801 xsh_free_vector( &flux1D);
802 xsh_free_vector( &err1D);
803 xsh_free_vector( &qual1D);
804 }
805
806
807 check( product_frame = cpl_frame_new() ) ;
808 check( cpl_frame_set_filename( product_frame,filename ));
809 check( cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE )) ;
810 check( cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL )) ;
811 check( cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT ));
812
813 cleanup:
814 if (cpl_error_get_code () != CPL_ERROR_NONE) {
815 xsh_free_frame(&product_frame);
816 product_frame = NULL;
817 }
818 return product_frame;
819}
820
821
823{
824 xsh_spectrum * result = NULL ;
825
826 XSH_ASSURE_NOT_NULL( org ) ;
827
828 /* Duplicate the spectrum */
829
830 XSH_CALLOC( result, xsh_spectrum, 1 ) ;
831
832 result->lambda_min = org->lambda_min;
833 result->lambda_max = org->lambda_max;
834 result->lambda_step = org->lambda_step;
835 result->size_lambda = org->size_lambda ;
836 result->slit_min = org->slit_min;
837 result->slit_max = org->slit_max;
838 result->size = org->size;
839 result->size_slit = org->size_slit ;
840 check( result->flux = cpl_image_duplicate( org->flux ) ) ;
841 check( result->flux_header = cpl_propertylist_duplicate( org->flux_header ));
842 check( result->errs = cpl_image_duplicate( org->errs ) ) ;
843 check( result->errs_header = cpl_propertylist_duplicate( org->errs_header ));
844 check( result->qual = cpl_image_duplicate( org->qual ) ) ;
845 check( result->qual_header = cpl_propertylist_duplicate( org->qual_header ));
846
847 cleanup:
848 return result ;
849}
850
851
853xsh_spectrum_extract_range( xsh_spectrum * org, const double wmin,const double wmax )
854{
855 xsh_spectrum * result = NULL ;
856
857 XSH_ASSURE_NOT_NULL( org ) ;
858 int size_x=0;
859 int size_y=0;
860 /* Duplicate the spectrum */
861
862 XSH_CALLOC( result, xsh_spectrum, 1 ) ;
863
864 result->lambda_min = wmin;
865 result->lambda_max = wmax;
866 result->lambda_step = org->lambda_step;
867 size_x=(int)((wmax-wmin)/org->lambda_step+0.5);
868 result->size_lambda = size_x ;
869 result->slit_min = org->slit_min;
870 result->slit_max = org->slit_max;
871 result->size_slit = org->size_slit ;
872 check(result->size = size_x);
873 /*
874 xsh_msg("size_lambda=%d",org->size_lambda);
875 xsh_msg("size_slit=%d",org->size_slit);
876 xsh_msg("slit_min=%g",org->slit_min);
877 xsh_msg("slit_max=%g",org->slit_max);
878 xsh_msg("lambda_min=%g",org->lambda_min);
879 xsh_msg("lambda_max=%g",org->lambda_max);
880 xsh_msg("size=%d",org->size);
881 xsh_msg("out spectrum size=%d",size_x);
882
883 xsh_msg("ima sx=%d",(int)cpl_image_get_size_x(org->flux));
884 xsh_msg("ima sy=%d",(int)cpl_image_get_size_y(org->flux));
885 */
886
887 size_y=(result->size_slit>1) ? result->size_slit: 1;
888 //xsh_msg("out spectrum X size=%d",size_x);
889 //xsh_msg("out spectrum Y size=%d",size_y);
890 check( result->flux = cpl_image_extract( org->flux,1,1,size_x,size_y));
891 check( result->flux_header = cpl_propertylist_duplicate( org->flux_header ));
892
893 check( result->errs = cpl_image_extract( org->errs,1,1,size_x,size_y));
894 check( result->errs_header = cpl_propertylist_duplicate( org->errs_header ));
895
896 check( result->qual = cpl_image_extract( org->qual,1,1,size_x,size_y));
897 check( result->qual_header = cpl_propertylist_duplicate( org->qual_header ));
898
899 cleanup:
900 return result ;
901}
902
903
904cpl_error_code
906{
907 cpl_propertylist* phead=NULL;
908 xsh_spectrum* spectrum_i=NULL;
909 xsh_spectrum* spectrum_o=NULL;
910
911 const char* pcatg=NULL;
912 const char* fname=NULL;
913 char oname[128];
914
915 int naxis1=0;
916 int xcut=0;
917
918 double wave_cut=XSH_UVB_DICHROIC_WAVE_CUT; /* 556 nm dichroic cut */
919 double wave_min=0;
920 double wave_max=0;
921 double wave_del=0;
922 char cmd[256];
923 fname=cpl_frame_get_filename(frame1d);
924
925 phead = cpl_propertylist_load(fname, 0);
926 pcatg=xsh_pfits_get_pcatg(phead) ;
927
928 //xsh_msg("fname=%s",fname);
929 spectrum_i=xsh_spectrum_load( frame1d);
930
931 check(wave_min=spectrum_i->lambda_min);
932 check(wave_max=spectrum_i->lambda_max);
933 check(wave_del=spectrum_i->lambda_step);
934 check(naxis1=spectrum_i->size);
935
936 cpl_ensure_code(wave_max > wave_cut, CPL_ERROR_ILLEGAL_INPUT);
937 xcut = (int) ( (wave_cut-wave_min) / wave_del + 0.5 );
938 cpl_ensure_code(xcut <= naxis1, CPL_ERROR_ILLEGAL_INPUT);
939
940 if (xcut == naxis1) {
941 return CPL_ERROR_NONE;
942 }
943 sprintf(oname,"tmp_%s",fname);
944 /*
945 xsh_msg("wave_min=%g",wave_min);
946 xsh_msg("wave_max=%g",wave_max);
947 xsh_msg("wave_del=%g",wave_del);
948 xsh_msg("wave_cut=%g",wave_cut);
949 xsh_msg("naxis1=%d",naxis1);
950 xsh_msg("xcut=%d",xcut);
951 */
952 check(spectrum_o=xsh_spectrum_extract_range(spectrum_i, wave_min, wave_cut));
953 cpl_frame* frm=xsh_spectrum_save(spectrum_o, oname,pcatg);
954 xsh_free_frame(&frm);
955 sprintf(cmd,"mv %s %s",oname,fname);
956 assure(system(cmd)==0,CPL_ERROR_UNSPECIFIED,"unable to mv file");
957 //cpl_frame_set_filename(frame1d,oname);
958
959 cleanup:
960 xsh_spectrum_free(&spectrum_i);
961 xsh_spectrum_free(&spectrum_o);
962 xsh_free_propertylist(&phead);
963
964 return cpl_error_get_code();
965}
966
967
968cpl_error_code
970{
971 cpl_propertylist* phead=NULL;
972 xsh_spectrum* spectrum_i=NULL;
973 xsh_spectrum* spectrum_o=NULL;
974
975 const char* pcatg=NULL;
976 const char* fname=NULL;
977 char oname[128];
978
979 int naxis1=0;
980 int xcut=0;
981 int next=0;
982
983 double wave_cut=XSH_UVB_DICHROIC_WAVE_CUT; /* 556 nm dichroic cut */
984 double wave_min=0;
985 double wave_max=0;
986 double wave_del=0;
987 char cmd[256];
988 fname=cpl_frame_get_filename(frame1d);
989 next=cpl_frame_get_nextensions(frame1d);
990
991 phead = cpl_propertylist_load(fname, 0);
992 pcatg=xsh_pfits_get_pcatg(phead) ;
993
994 //xsh_msg("fname=%s",fname);
995
996 spectrum_i=xsh_spectrum_load_order( frame1d, instr,0);
997
998 check(wave_min=spectrum_i->lambda_min);
999 check(wave_max=spectrum_i->lambda_max);
1000 check(wave_del=spectrum_i->lambda_step);
1001 check(naxis1=spectrum_i->size);
1002
1003 cpl_ensure_code(wave_max > wave_cut, CPL_ERROR_ILLEGAL_INPUT);
1004 xcut = (int) ( (wave_cut-wave_min) / wave_del + 0.5 );
1005 cpl_ensure_code(xcut <= naxis1, CPL_ERROR_ILLEGAL_INPUT);
1006
1007
1008 if (xcut == naxis1) {
1009 return CPL_ERROR_NONE;
1010 }
1011 sprintf(oname,"tmp_%s",fname);
1012
1013 check(spectrum_o=xsh_spectrum_extract_range(spectrum_i, wave_min, wave_cut));
1014 cpl_frame* frm=xsh_spectrum_save(spectrum_o, oname,pcatg);
1015 xsh_free_frame(&frm);
1016 /* loop over each frame extensions */
1017 cpl_frame* frame;
1018 xsh_spectrum_free(&spectrum_i);
1019 for(int i=3;i<next;i+=3) {
1020
1021 spectrum_i=xsh_spectrum_load_order( frame1d, instr,i);
1022 frame=xsh_spectrum_save_order(spectrum_i, oname, pcatg,i);
1023 xsh_spectrum_free(&spectrum_i);
1024 xsh_free_frame(&frame);
1025
1026 }
1027
1028
1029 sprintf(cmd,"mv %s %s",oname,fname);
1030 assure(system(cmd)==0,CPL_ERROR_UNSPECIFIED,"unable to mv file");
1031
1032 cleanup:
1033
1034 xsh_spectrum_free(&spectrum_i);
1035 xsh_spectrum_free(&spectrum_o);
1036 xsh_free_propertylist(&phead);
1037
1038 return cpl_error_get_code();
1039}
1040
1051/*---------------------------------------------------------------------------*/
1052cpl_frame* xsh_phys_spectrum_save( xsh_spectrum* s, const char* filename,
1053 xsh_instrument* instr)
1054{
1055 cpl_frame *product_frame = NULL;
1056 const char* tag = NULL;
1057
1059 XSH_ASSURE_NOT_NULL(filename);
1060
1061
1062 /* Save the file */
1063 if ( s->size_slit > 1){
1065 check( xsh_pfits_set_pcatg( s->flux_header, tag));
1066 check_msg (cpl_image_save ( s->flux, filename, XSH_SPECTRUM_DATA_BPP,
1067 s->flux_header, CPL_IO_DEFAULT),
1068 "Could not save data to %s extension 0", filename);
1069 check_msg (cpl_image_save ( s->errs, filename, XSH_SPECTRUM_ERRS_BPP,
1070 s->errs_header, CPL_IO_EXTEND),
1071 "Could not save errs to %s extension 1", filename);
1072 check_msg (cpl_image_save ( s->qual, filename, XSH_SPECTRUM_QUAL_BPP,
1073 s->qual_header, CPL_IO_EXTEND),
1074 "Could not save qual to %s extension 2", filename);
1075 }
1076 else{
1077 cpl_vector *flux1D = NULL;
1078 cpl_vector *err1D = NULL;
1079 cpl_vector *qual1D = NULL;
1080
1082
1083 check( xsh_pfits_set_pcatg( s->flux_header, tag));
1084 check( flux1D = cpl_vector_new_from_image_row( s->flux, 1));
1085 check( err1D = cpl_vector_new_from_image_row( s->errs, 1));
1086 check( qual1D = cpl_vector_new_from_image_row( s->qual, 1));
1087 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
1088 s->flux_header, CPL_IO_DEFAULT));
1089 check( cpl_vector_save( err1D, filename, XSH_SPECTRUM_ERRS_BPP,
1090 s->errs_header, CPL_IO_EXTEND));
1091 check( cpl_vector_save( qual1D, filename, XSH_SPECTRUM_QUAL_BPP,
1092 s->qual_header, CPL_IO_EXTEND));
1093 xsh_free_vector( &flux1D);
1094 xsh_free_vector( &err1D);
1095 xsh_free_vector( &qual1D);
1096 }
1097
1098
1099 check( product_frame = cpl_frame_new() ) ;
1100 check( cpl_frame_set_filename( product_frame,filename ));
1101 check( cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE )) ;
1102 check( cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL )) ;
1103 check( cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT ));
1104
1105 cleanup:
1106 if (cpl_error_get_code () != CPL_ERROR_NONE) {
1107 xsh_free_frame(&product_frame);
1108 product_frame = NULL;
1109 }
1110 return product_frame;
1111}
1112
1113
static float slit_step
static float lambda_step
int xsh_spectrum_get_size_lambda(xsh_spectrum *s)
Get lambda axis size of spectrum.
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.
cpl_image * xsh_spectrum_get_errs_ima(xsh_spectrum *s)
Get flux of spectrum as image.
double xsh_spectrum_get_lambda_step(xsh_spectrum *s)
Get bin in lambda of spectrum.
cpl_error_code xsh_spectrum_cut_dichroic_uvb(cpl_frame *frame1d)
cpl_frame * xsh_phys_spectrum_save(xsh_spectrum *s, const char *filename, xsh_instrument *instr)
save a spectrum
xsh_spectrum * xsh_spectrum_load_order(cpl_frame *s1d_frame, xsh_instrument *instr, const int order)
Load a 1D spectrum structure.
int * xsh_spectrum_get_qual(xsh_spectrum *s)
Get qual of spectrum.
int xsh_spectrum_get_size(xsh_spectrum *s)
Get size of spectrum.
xsh_spectrum * xsh_spectrum_duplicate(xsh_spectrum *org)
cpl_frame * xsh_spectrum_save(xsh_spectrum *s, const char *filename, const char *tag)
save a spectrum
cpl_error_code xsh_spectrum_orders_cut_dichroic_uvb(cpl_frame *frame1d, xsh_instrument *instr)
cpl_frame * xsh_spectrum_save_order(xsh_spectrum *s, const char *filename, const char *tag, const int order)
save a spectrum
cpl_image * xsh_spectrum_get_flux_ima(xsh_spectrum *s)
Get flux of spectrum as image.
double * xsh_spectrum_get_flux(xsh_spectrum *s)
Get flux of spectrum.
xsh_spectrum * xsh_spectrum_extract_range(xsh_spectrum *org, const double wmin, const double wmax)
cpl_image * xsh_spectrum_get_qual_ima(xsh_spectrum *s)
Get flux of spectrum as image.
xsh_spectrum * xsh_spectrum_2D_create(double lambda_min, double lambda_max, double lambda_step, double slit_min, double slit_max, double slit_step)
Create a 2D spectrum structure.
xsh_spectrum * xsh_spectrum_1D_create(double lambda_min, double lambda_max, double lambda_step)
Create a 1D spectrum structure.
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
int xsh_spectrum_get_size_slit(xsh_spectrum *s)
Get slit axis ize of spectrum.
#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_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
int size
static SimAnneal s
Definition: xsh_model_sa.c:99
cpl_error_code xsh_pfits_set_wcs2(cpl_propertylist *header, const double crpix2, const double crval2, const double cdelt2)
Definition: xsh_pfits.c:4322
double xsh_pfits_get_crval2(const cpl_propertylist *plist)
find out the crval2
Definition: xsh_pfits.c:1926
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
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
double xsh_pfits_get_cdelt2(const cpl_propertylist *plist)
find out the cdelt2
Definition: xsh_pfits.c:2216
void xsh_pfits_set_ctype1(cpl_propertylist *plist, const char *value)
Write the CTYPE1 value.
Definition: xsh_pfits.c:2667
void xsh_pfits_set_extname(cpl_propertylist *plist, const char *value)
Write the EXTNAME value.
Definition: xsh_pfits.c:979
cpl_error_code xsh_pfits_set_wcs(cpl_propertylist *header, const double crpix1, const double crval1, const double cdelt1, const double crpix2, const double crval2, const double cdelt2)
Definition: xsh_pfits.c:4364
const char * xsh_pfits_get_pcatg(const cpl_propertylist *plist)
find out the pcatg
Definition: xsh_pfits.c:1627
double xsh_pfits_get_crpix1(const cpl_propertylist *plist)
find out the CRPIX1 value
Definition: xsh_pfits.c:1965
cpl_error_code xsh_pfits_set_wcs1(cpl_propertylist *header, const double crpix1, const double crval1, const double cdelt1)
Definition: xsh_pfits.c:4307
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
Definition: xsh_pfits.c:2196
void xsh_pfits_set_cunit1(cpl_propertylist *plist, const char *value)
Write the CUNIT1 value.
Definition: xsh_pfits.c:2622
void xsh_pfits_set_bunit(cpl_propertylist *plist, const char *value)
Write the BUNIT value.
Definition: xsh_pfits.c:2606
int xsh_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
Definition: xsh_pfits.c:227
double xsh_pfits_get_crpix2(const cpl_propertylist *plist)
find out the CRPIX2 value
Definition: xsh_pfits.c:1982
double xsh_pfits_get_crval1(const cpl_propertylist *plist)
find out the crval1
Definition: xsh_pfits.c:1907
int xsh_pfits_get_naxis(const cpl_propertylist *plist)
find out the NAXIS value
Definition: xsh_pfits.c:209
int xsh_pfits_get_naxis2(const cpl_propertylist *plist)
find out the NAXIS2 value
Definition: xsh_pfits.c:244
void xsh_unwrap_vector(cpl_vector **v)
Unwrap a vector and set the pointer to NULL.
Definition: xsh_utils.c:2345
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
cpl_error_code xsh_set_cd_matrix2d(cpl_propertylist *plist)
Set CD matrix.
Definition: xsh_utils.c:718
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_propertylist * errs_header
cpl_propertylist * flux_header
cpl_image * flux
cpl_propertylist * qual_header
cpl_image * qual
cpl_image * errs
#define XSH_SPECTRUM_DATA_BPP
#define XSH_SPECTRUM_QUAL_BPP
#define XSH_SPECTRUM_ERRS_BPP
int order
Definition: xsh_detmon_lg.c:80
#define XSH_PHYS_MERGE2D
Definition: xsh_dfs.h:598
#define XSH_PHYS_MERGE1D
Definition: xsh_dfs.h:597
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_UVB_DICHROIC_WAVE_CUT
Definition: xsh_globals.h:38
#define XSH_BUNIT_NONE_C
Definition: xsh_pfits.h:116
#define XSH_NEW_PROPERTYLIST(POINTER)
Definition: xsh_utils.h:70
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56