00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00036
00037
00041
00042
00043
00044
00045 #include <math.h>
00046 #include <xsh_data_spectrum.h>
00047 #include <xsh_utils.h>
00048 #include <xsh_error.h>
00049 #include <xsh_msg.h>
00050 #include <xsh_pfits.h>
00051 #include <xsh_dfs.h>
00052 #include <cpl.h>
00053
00054
00055
00056
00057
00058
00068
00069 xsh_spectrum* xsh_spectrum_1D_create( double lambda_min, double lambda_max,
00070 double lambda_step)
00071 {
00072 xsh_spectrum* result = NULL;
00073
00074
00075
00076 XSH_ASSURE_NOT_ILLEGAL( lambda_min >= 0.0 && lambda_min <= lambda_max);
00077 XSH_ASSURE_NOT_ILLEGAL( lambda_step >=0);
00078
00079 XSH_CALLOC(result, xsh_spectrum,1);
00080
00081 result->lambda_min = lambda_min;
00082 result->lambda_max = lambda_max;
00083 result->lambda_step = lambda_step;
00084
00085 XSH_NEW_PROPERTYLIST( result->flux_header);
00086 check( xsh_pfits_set_crpix1( result->flux_header, 1.0));
00087 check( xsh_pfits_set_crval1( result->flux_header, lambda_min));
00088 check( xsh_pfits_set_cdelt1( result->flux_header, lambda_step));
00089 check( xsh_pfits_set_ctype1( result->flux_header, "LINEAR"));
00090
00091 XSH_NEW_PROPERTYLIST( result->errs_header);
00092 check( xsh_pfits_set_extname ( result->errs_header, "ERRS"));
00093 check( xsh_pfits_set_crpix1( result->errs_header, 1.0));
00094 check( xsh_pfits_set_crval1( result->errs_header, lambda_min));
00095 check( xsh_pfits_set_cdelt1( result->errs_header, lambda_step));
00096 check( xsh_pfits_set_ctype1( result->errs_header, "LINEAR"));
00097
00098 XSH_NEW_PROPERTYLIST( result->qual_header);
00099 check( xsh_pfits_set_extname ( result->qual_header, "QUAL"));
00100
00101
00102 result->size_lambda = (int)((lambda_max-lambda_min)/lambda_step+0.5)+1;
00103 result->size_slit = 1;
00104 result->slit_min = 0;
00105 result->slit_max = 0;
00106 result->size = result->size_lambda;
00107
00108 check( result->flux = cpl_image_new( result->size_lambda, 1,
00109 CPL_TYPE_DOUBLE));
00110 check( result->errs = cpl_image_new( result->size_lambda, 1,
00111 CPL_TYPE_DOUBLE));
00112 check( result->qual = cpl_image_new( result->size_lambda, 1,
00113 CPL_TYPE_INT));
00114
00115 cleanup:
00116 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00117 xsh_spectrum_free(&result);
00118 }
00119 return result;
00120 }
00121
00122
00135
00136 xsh_spectrum* xsh_spectrum_2D_create( double lambda_min, double lambda_max,
00137 double lambda_step, double slit_min, double slit_max, double slit_step)
00138 {
00139 xsh_spectrum* result = NULL;
00140
00141
00142 XSH_ASSURE_NOT_ILLEGAL( lambda_min >= 0.0 && lambda_min <= lambda_max);
00143 XSH_ASSURE_NOT_ILLEGAL( lambda_step >=0);
00144 XSH_ASSURE_NOT_ILLEGAL( slit_min <= slit_max);
00145 XSH_ASSURE_NOT_ILLEGAL( slit_step >=0);
00146
00147
00148 XSH_CALLOC(result, xsh_spectrum,1);
00149
00150 result->lambda_min = lambda_min;
00151 result->lambda_max = lambda_max;
00152 result->lambda_step = lambda_step;
00153 result->slit_min = slit_min;
00154 result->slit_max = slit_max;
00155 result->slit_step = slit_step;
00156
00157 XSH_NEW_PROPERTYLIST( result->flux_header);
00158 check( xsh_pfits_set_crpix1( result->flux_header, 1.0));
00159 check( xsh_pfits_set_crval1( result->flux_header, lambda_min));
00160 check( xsh_pfits_set_cdelt1( result->flux_header, lambda_step));
00161 check( xsh_pfits_set_crpix2( result->flux_header, 1.0));
00162 check( xsh_pfits_set_crval2( result->flux_header, slit_min));
00163 check( xsh_pfits_set_cdelt2( result->flux_header, slit_step));
00164 check( xsh_pfits_set_ctype1( result->flux_header, "LINEAR"));
00165 check( xsh_pfits_set_ctype2( result->flux_header, "LINEAR"));
00166 check(xsh_set_cd_matrix2d(result->flux_header));
00167
00168 XSH_NEW_PROPERTYLIST( result->errs_header);
00169 check( xsh_pfits_set_extname ( result->errs_header, "ERRS"));
00170 XSH_NEW_PROPERTYLIST( result->qual_header);
00171 check( xsh_pfits_set_extname ( result->qual_header, "QUAL"));
00172
00173
00174 result->size_lambda = (int)((lambda_max-lambda_min)/lambda_step+0.5)+1;
00175 result->size_slit = (int)((slit_max-slit_min)/slit_step+0.5)+1;
00176 result->size = result->size_lambda * result->size_slit;
00177 check( result->flux = cpl_image_new( result->size_lambda, result->size_slit,
00178 CPL_TYPE_DOUBLE));
00179 check( result->errs = cpl_image_new( result->size_lambda, result->size_slit,
00180 CPL_TYPE_DOUBLE));
00181 check( result->qual = cpl_image_new( result->size_lambda, result->size_slit,
00182 CPL_TYPE_INT));
00183
00184 cleanup:
00185 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00186 xsh_spectrum_free(&result);
00187 }
00188 return result;
00189 }
00190
00191
00192
00199
00200 xsh_spectrum* xsh_spectrum_load( cpl_frame* s1d_frame)
00201 {
00202 xsh_spectrum* result = NULL;
00203 const char *s1dname = NULL;
00204 int naxis;
00205
00206 XSH_ASSURE_NOT_NULL( s1d_frame);
00207
00208 XSH_ASSURE_NOT_ILLEGAL(cpl_frame_get_nextensions(s1d_frame) == 2);
00209
00210 check( s1dname = cpl_frame_get_filename( s1d_frame));
00211
00212 XSH_CALLOC(result, xsh_spectrum,1);
00213
00214 check( result->flux_header = cpl_propertylist_load( s1dname,0));
00215 check( result->errs_header = cpl_propertylist_load( s1dname,1));
00216 check( result->qual_header = cpl_propertylist_load( s1dname,2));
00217
00218 check( result->lambda_min = xsh_pfits_get_crval1( result->flux_header));
00219 check( result->lambda_step = xsh_pfits_get_cdelt1( result->flux_header));
00220 check( result->size = xsh_pfits_get_naxis1( result->flux_header));
00221 check( result->size_lambda = xsh_pfits_get_naxis1( result->flux_header));
00222 result->lambda_max = result->lambda_min+
00223 result->lambda_step*(result->size-1);
00224
00225 check( naxis = xsh_pfits_get_naxis( result->flux_header));
00226
00227 if (naxis > 1){
00228 check( result->slit_min = xsh_pfits_get_crval2( result->flux_header));
00229 check( result->slit_step = xsh_pfits_get_cdelt2( result->flux_header));
00230 check( result->size_slit = xsh_pfits_get_naxis2( result->flux_header));
00231 result->slit_max = result->slit_min+
00232 result->slit_step*(result->size_slit-1);
00233
00234 check( result->flux = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0, 0));
00235 check( result->errs = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0, 1));
00236 check( result->qual = cpl_image_load( s1dname, CPL_TYPE_INT, 0, 2));
00237 }
00238 else{
00239 double *flux_data = NULL;
00240 double *errs_data = NULL;
00241 int *qual_data = NULL;
00242 int i;
00243 cpl_vector *flux = NULL;
00244 cpl_vector *errs = NULL;
00245 cpl_vector *qual = NULL;
00246 int size = 0;
00247
00248 check( flux = cpl_vector_load( s1dname, 0));
00249 check( size = cpl_vector_get_size( flux));
00250 check( errs = cpl_vector_load( s1dname, 1));
00251 check( qual = cpl_vector_load( s1dname, 2));
00252 check( flux_data = cpl_vector_get_data( flux));
00253 check( result->flux = cpl_image_wrap_double( size, 1 , flux_data));
00254 xsh_unwrap_vector( &flux);
00255
00256 check( errs_data = cpl_vector_get_data( errs));
00257 check( result->errs = cpl_image_wrap_double( size, 1, errs_data));
00258
00259 check( result->qual = cpl_image_new ( size, 1, CPL_TYPE_INT));
00260 check( qual_data = cpl_image_get_data_int( result->qual));
00261 for(i=0; i< size; i++){
00262 check( qual_data[i] = (int)cpl_vector_get(qual,i));
00263 }
00264 xsh_unwrap_vector( &errs);
00265 xsh_free_vector( &qual);
00266 }
00267 cleanup:
00268 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00269 xsh_spectrum_free(&result);
00270 }
00271 return result;
00272 }
00273
00274
00275
00284
00285 xsh_spectrum* xsh_spectrum_load_order( cpl_frame* s1d_frame,
00286 xsh_instrument* instr,
00287 const int order)
00288 {
00289 xsh_spectrum* result = NULL;
00290 const char *s1dname = NULL;
00291 int naxis;
00292
00293 XSH_ASSURE_NOT_NULL( s1d_frame);
00294 XSH_ASSURE_NOT_NULL( instr);
00295
00296
00297
00298 check( s1dname = cpl_frame_get_filename( s1d_frame));
00299
00300 XSH_CALLOC(result, xsh_spectrum,1);
00301
00302 check( result->flux_header = cpl_propertylist_load( s1dname,order+0));
00303 check( result->errs_header = cpl_propertylist_load( s1dname,order+1));
00304 check( result->qual_header = cpl_propertylist_load( s1dname,order+2));
00305
00306 check( result->lambda_min = xsh_pfits_get_crval1( result->flux_header));
00307 check( result->lambda_step = xsh_pfits_get_cdelt1( result->flux_header));
00308 check( result->size = xsh_pfits_get_naxis1( result->flux_header));
00309 check( result->size_lambda = xsh_pfits_get_naxis1( result->flux_header));
00310 result->lambda_max = result->lambda_min+
00311 (result->lambda_step*result->size-1);
00312
00313 check( naxis = xsh_pfits_get_naxis( result->flux_header));
00314
00315 if (naxis > 1){
00316 check( result->slit_min = xsh_pfits_get_crval2( result->flux_header));
00317 check( result->slit_step = xsh_pfits_get_cdelt2( result->flux_header));
00318 check( result->size_slit = xsh_pfits_get_naxis2( result->flux_header));
00319 result->slit_max = result->slit_min+
00320 result->slit_step*(result->size_slit-1);
00321
00322 check( result->flux = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0,order+0));
00323 check( result->errs = cpl_image_load( s1dname, CPL_TYPE_DOUBLE, 0,order+1));
00324 check( result->qual = cpl_image_load( s1dname, CPL_TYPE_INT, 0, order+2));
00325 }
00326 else{
00327 double *flux_data = NULL;
00328 double *errs_data = NULL;
00329 int *qual_data = NULL;
00330 int i;
00331 cpl_vector *flux = NULL;
00332 cpl_vector *errs = NULL;
00333 cpl_vector *qual = NULL;
00334 int size = 0;
00335
00336 check( flux = cpl_vector_load( s1dname, order+0));
00337 check( size = cpl_vector_get_size( flux));
00338 check( errs = cpl_vector_load( s1dname, order+1));
00339 check( qual = cpl_vector_load( s1dname, order+2));
00340 check( flux_data = cpl_vector_get_data( flux));
00341 check( result->flux = cpl_image_wrap_double( size, 1 , flux_data));
00342 xsh_unwrap_vector( &flux);
00343
00344 check( errs_data = cpl_vector_get_data( errs));
00345 check( result->errs = cpl_image_wrap_double( size, 1, errs_data));
00346
00347 check( result->qual = cpl_image_new ( size, 1, CPL_TYPE_INT));
00348 check( qual_data = cpl_image_get_data_int( result->qual));
00349 for(i=0; i< size; i++){
00350 check( qual_data[i] = (int)cpl_vector_get(qual,i));
00351 }
00352 xsh_unwrap_vector( &errs);
00353 xsh_free_vector( &qual);
00354 }
00355 cleanup:
00356 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00357 xsh_spectrum_free(&result);
00358 }
00359 return result;
00360 }
00361
00362
00370
00371 int xsh_spectrum_get_size( xsh_spectrum* s)
00372 {
00373 int res=0;
00374
00375 XSH_ASSURE_NOT_NULL( s);
00376
00377 res = s->size;
00378
00379 cleanup:
00380 return res;
00381 }
00382
00383
00391
00392 int xsh_spectrum_get_size_lambda( xsh_spectrum* s)
00393 {
00394 int res=0;
00395
00396 XSH_ASSURE_NOT_NULL( s);
00397
00398 res = s->size_lambda;
00399
00400 cleanup:
00401 return res;
00402 }
00403
00404
00405
00413
00414 int xsh_spectrum_get_size_slit( xsh_spectrum* s)
00415 {
00416 int res=0;
00417
00418 XSH_ASSURE_NOT_NULL( s);
00419
00420 res = s->size_slit;
00421
00422 cleanup:
00423 return res;
00424 }
00425
00426
00434
00435 double xsh_spectrum_get_lambda_min( xsh_spectrum* s)
00436 {
00437 double res=0.0;
00438
00439 XSH_ASSURE_NOT_NULL( s);
00440
00441 res = s->lambda_min;
00442
00443 cleanup:
00444 return res;
00445 }
00446
00447
00455
00456 double xsh_spectrum_get_lambda_max( xsh_spectrum* s)
00457 {
00458 double res=0.0;
00459
00460 XSH_ASSURE_NOT_NULL( s);
00461
00462 res = s->lambda_max;
00463
00464 cleanup:
00465 return res;
00466 }
00467
00468
00476
00477 double xsh_spectrum_get_lambda_step( xsh_spectrum* s)
00478 {
00479 double res=0.0;
00480
00481 XSH_ASSURE_NOT_NULL( s);
00482
00483 res = s->lambda_step;
00484
00485 cleanup:
00486 return res;
00487 }
00488
00489
00490
00498
00499 double* xsh_spectrum_get_flux( xsh_spectrum* s)
00500 {
00501 double *res=NULL;
00502
00503 XSH_ASSURE_NOT_NULL( s);
00504
00505 check( res = cpl_image_get_data_double( s->flux));
00506
00507 cleanup:
00508 return res;
00509 }
00510
00511
00512
00520
00521 double* xsh_spectrum_get_errs( xsh_spectrum* s)
00522 {
00523 double *res=NULL;
00524
00525 XSH_ASSURE_NOT_NULL( s);
00526
00527 check( res = cpl_image_get_data_double( s->errs));
00528
00529 cleanup:
00530 return res;
00531 }
00532
00533
00534
00542
00543 int* xsh_spectrum_get_qual( xsh_spectrum* s)
00544 {
00545 int* res = NULL;
00546
00547 XSH_ASSURE_NOT_NULL( s);
00548
00549 check( res = cpl_image_get_data_int( s->qual));
00550
00551 cleanup:
00552 return res;
00553 }
00554
00555
00561
00562 void xsh_spectrum_free( xsh_spectrum** s)
00563 {
00564 if (s && *s){
00565 xsh_free_propertylist( &((*s)->flux_header));
00566 xsh_free_propertylist( &((*s)->errs_header));
00567 xsh_free_propertylist( &((*s)->qual_header));
00568 xsh_free_image( &((*s)->flux));
00569 xsh_free_image( &((*s)->errs));
00570 xsh_free_image( &((*s)->qual));
00571 XSH_FREE( (*s));
00572 }
00573 }
00574
00575
00585
00586 cpl_frame* xsh_spectrum_save( xsh_spectrum* s, const char* filename,
00587 const char* tag)
00588 {
00589 cpl_frame *product_frame = NULL;
00590
00591 XSH_ASSURE_NOT_NULL(s);
00592 XSH_ASSURE_NOT_NULL(filename);
00593
00594 check( xsh_pfits_set_extname(s->flux_header , "FLUX"));
00595 check(xsh_plist_set_extra_keys(s->flux_header,"IMAGE","DATA","RMSE",
00596 "FLUX","ERRS","QUAL",0));
00597
00598 check( xsh_pfits_set_extname(s->errs_header , "ERRS"));
00599 check(xsh_plist_set_extra_keys(s->errs_header,"IMAGE","DATA","RMSE",
00600 "FLUX","ERRS","QUAL",1));
00601
00602 check( xsh_pfits_set_extname(s->qual_header , "QUAL"));
00603 check(xsh_plist_set_extra_keys(s->qual_header,"IMAGE","DATA","RMSE",
00604 "FLUX","ERRS","QUAL",2));
00605
00606
00607 if ( s->size_slit > 1){
00608
00609 check( xsh_pfits_set_pcatg( s->flux_header, tag));
00610 check_msg (cpl_image_save ( s->flux, filename, XSH_SPECTRUM_DATA_BPP,
00611 s->flux_header, CPL_IO_DEFAULT),
00612 "Could not save data to %s extension 0", filename);
00613 check_msg (cpl_image_save ( s->errs, filename, XSH_SPECTRUM_ERRS_BPP,
00614 s->errs_header, CPL_IO_EXTEND),
00615 "Could not save errs to %s extension 1", filename);
00616 check_msg (cpl_image_save ( s->qual, filename, XSH_SPECTRUM_QUAL_BPP,
00617 s->qual_header, CPL_IO_EXTEND),
00618 "Could not save qual to %s extension 2", filename);
00619 }
00620 else{
00621 cpl_vector *flux1D = NULL;
00622 cpl_vector *err1D = NULL;
00623 cpl_vector *qual1D = NULL;
00624
00625 check( flux1D = cpl_vector_new_from_image_row( s->flux, 1));
00626 check( err1D = cpl_vector_new_from_image_row( s->errs, 1));
00627 check( qual1D = cpl_vector_new_from_image_row( s->qual, 1));
00628 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
00629 s->flux_header, CPL_IO_DEFAULT));
00630 check( cpl_vector_save( err1D, filename, XSH_SPECTRUM_ERRS_BPP,
00631 s->errs_header, CPL_IO_EXTEND));
00632 check( cpl_vector_save( qual1D, filename, XSH_SPECTRUM_QUAL_BPP,
00633 s->qual_header, CPL_IO_EXTEND));
00634 xsh_free_vector( &flux1D);
00635 xsh_free_vector( &err1D);
00636 xsh_free_vector( &qual1D);
00637 }
00638
00639
00640 check( product_frame = cpl_frame_new() ) ;
00641 check( cpl_frame_set_filename( product_frame,filename ));
00642 check( cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE )) ;
00643 check( cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL )) ;
00644 check( cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT ));
00645
00646 cleanup:
00647 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00648 xsh_free_frame(&product_frame);
00649 product_frame = NULL;
00650 }
00651 return product_frame;
00652 }
00653
00654
00655
00656
00657
00668
00669 cpl_frame* xsh_spectrum_save_order( xsh_spectrum* s, const char* filename,
00670 const char* tag,const int order)
00671 {
00672 cpl_frame *product_frame = NULL;
00673
00674 XSH_ASSURE_NOT_NULL(s);
00675 XSH_ASSURE_NOT_NULL(filename);
00676
00677
00678
00679 if ( s->size_slit > 1){
00680
00681 check( xsh_pfits_set_pcatg( s->flux_header, tag));
00682 if(order==0) {
00683 check_msg (cpl_image_save ( s->flux, filename, XSH_SPECTRUM_DATA_BPP,
00684 s->flux_header, CPL_IO_DEFAULT),
00685 "Could not save data to %s extension 0", filename);
00686 } else {
00687 check_msg (cpl_image_save ( s->errs, filename, XSH_SPECTRUM_ERRS_BPP,
00688 s->errs_header, CPL_IO_EXTEND),
00689 "Could not save errs to %s extension 1", filename);
00690 check_msg (cpl_image_save ( s->qual, filename, XSH_SPECTRUM_QUAL_BPP,
00691 s->qual_header, CPL_IO_EXTEND),
00692 "Could not save qual to %s extension 2", filename);
00693 }
00694 }
00695 else{
00696 cpl_vector *flux1D = NULL;
00697 cpl_vector *err1D = NULL;
00698 cpl_vector *qual1D = NULL;
00699
00700 check( flux1D = cpl_vector_new_from_image_row( s->flux, 1));
00701 check( err1D = cpl_vector_new_from_image_row( s->errs, 1));
00702 check( qual1D = cpl_vector_new_from_image_row( s->qual, 1));
00703 if(order==0) {
00704 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
00705 s->flux_header, CPL_IO_DEFAULT));
00706 } else {
00707 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
00708 s->flux_header, CPL_IO_DEFAULT));
00709 }
00710 check( cpl_vector_save( err1D, filename, XSH_SPECTRUM_ERRS_BPP,
00711 s->errs_header, CPL_IO_EXTEND));
00712 check( cpl_vector_save( qual1D, filename, XSH_SPECTRUM_QUAL_BPP,
00713 s->qual_header, CPL_IO_EXTEND));
00714 xsh_free_vector( &flux1D);
00715 xsh_free_vector( &err1D);
00716 xsh_free_vector( &qual1D);
00717 }
00718
00719
00720 check( product_frame = cpl_frame_new() ) ;
00721 check( cpl_frame_set_filename( product_frame,filename ));
00722 check( cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE )) ;
00723 check( cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL )) ;
00724 check( cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT ));
00725
00726 cleanup:
00727 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00728 xsh_free_frame(&product_frame);
00729 product_frame = NULL;
00730 }
00731 return product_frame;
00732 }
00733
00734
00735 xsh_spectrum * xsh_spectrum_duplicate( xsh_spectrum * org )
00736 {
00737 xsh_spectrum * result = NULL ;
00738
00739 XSH_ASSURE_NOT_NULL( org ) ;
00740
00741
00742
00743 XSH_CALLOC( result, xsh_spectrum, 1 ) ;
00744
00745 result->lambda_min = org->lambda_min;
00746 result->lambda_max = org->lambda_max;
00747 result->lambda_step = org->lambda_step;
00748 result->size_lambda = org->size_lambda ;
00749 result->slit_min = org->slit_min;
00750 result->slit_max = org->slit_max;
00751 result->size = org->size;
00752 result->size_slit = org->size_slit ;
00753 check( result->flux = cpl_image_duplicate( org->flux ) ) ;
00754 check( result->flux_header = cpl_propertylist_duplicate( org->flux_header ));
00755 check( result->errs = cpl_image_duplicate( org->errs ) ) ;
00756 check( result->errs_header = cpl_propertylist_duplicate( org->errs_header ));
00757 check( result->qual = cpl_image_duplicate( org->qual ) ) ;
00758 check( result->qual_header = cpl_propertylist_duplicate( org->qual_header ));
00759
00760 cleanup:
00761 return result ;
00762 }
00773
00774 cpl_frame* xsh_phys_spectrum_save( xsh_spectrum* s, const char* filename,
00775 xsh_instrument* instr)
00776 {
00777 cpl_frame *product_frame = NULL;
00778 const char* tag = NULL;
00779
00780 XSH_ASSURE_NOT_NULL(s);
00781 XSH_ASSURE_NOT_NULL(filename);
00782
00783
00784
00785 if ( s->size_slit > 1){
00786 tag = XSH_GET_TAG_FROM_ARM( XSH_PHYS_MERGE2D, instr);
00787 check( xsh_pfits_set_pcatg( s->flux_header, tag));
00788 check_msg (cpl_image_save ( s->flux, filename, XSH_SPECTRUM_DATA_BPP,
00789 s->flux_header, CPL_IO_DEFAULT),
00790 "Could not save data to %s extension 0", filename);
00791 check_msg (cpl_image_save ( s->errs, filename, XSH_SPECTRUM_ERRS_BPP,
00792 s->errs_header, CPL_IO_EXTEND),
00793 "Could not save errs to %s extension 1", filename);
00794 check_msg (cpl_image_save ( s->qual, filename, XSH_SPECTRUM_QUAL_BPP,
00795 s->qual_header, CPL_IO_EXTEND),
00796 "Could not save qual to %s extension 2", filename);
00797 }
00798 else{
00799 cpl_vector *flux1D = NULL;
00800 cpl_vector *err1D = NULL;
00801 cpl_vector *qual1D = NULL;
00802
00803 tag = XSH_GET_TAG_FROM_ARM( XSH_PHYS_MERGE1D, instr);
00804
00805 check( xsh_pfits_set_pcatg( s->flux_header, tag));
00806 check( flux1D = cpl_vector_new_from_image_row( s->flux, 1));
00807 check( err1D = cpl_vector_new_from_image_row( s->errs, 1));
00808 check( qual1D = cpl_vector_new_from_image_row( s->qual, 1));
00809 check( cpl_vector_save( flux1D, filename, XSH_SPECTRUM_DATA_BPP,
00810 s->flux_header, CPL_IO_DEFAULT));
00811 check( cpl_vector_save( err1D, filename, XSH_SPECTRUM_ERRS_BPP,
00812 s->errs_header, CPL_IO_EXTEND));
00813 check( cpl_vector_save( qual1D, filename, XSH_SPECTRUM_QUAL_BPP,
00814 s->qual_header, CPL_IO_EXTEND));
00815 xsh_free_vector( &flux1D);
00816 xsh_free_vector( &err1D);
00817 xsh_free_vector( &qual1D);
00818 }
00819
00820
00821 check( product_frame = cpl_frame_new() ) ;
00822 check( cpl_frame_set_filename( product_frame,filename ));
00823 check( cpl_frame_set_type( product_frame, CPL_FRAME_TYPE_IMAGE )) ;
00824 check( cpl_frame_set_level( product_frame, CPL_FRAME_LEVEL_FINAL )) ;
00825 check( cpl_frame_set_group( product_frame, CPL_FRAME_GROUP_PRODUCT ));
00826
00827 cleanup:
00828 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00829 xsh_free_frame(&product_frame);
00830 product_frame = NULL;
00831 }
00832 return product_frame;
00833 }
00834
00835