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
00028
00029
00030 #include <math.h>
00031 #include <string.h>
00032 #include <xsh_error.h>
00033 #include <xsh_utils_ifu.h>
00034 #include <xsh_utils_image.h>
00035 #include <xsh_utils_wrappers.h>
00036 #include <xsh_utils_table.h>
00037 #include <xsh_pfits.h>
00038 #include <xsh_model_io.h>
00039 #include <xsh_data_rec.h>
00040 #include <xsh_data_spectrum.h>
00041 #include <xsh_dfs.h>
00042 #include <xsh_drl.h>
00043
00044 #define PI_NUMB (3.1415926535897932384626433832795)
00045
00046 static double
00047 xsh_interpol(double x,double x1,double x2,double y1,double y2)
00048 {
00049 return y1+(y2-y1)/(x2-x1)*(x-x1);
00050 }
00051
00052
00053 static void
00054 xsh_plist_set_coord1(cpl_propertylist** plist,
00055 const double crpix1,
00056 const double crval1,
00057 const double cdelt1);
00058 static void
00059 xsh_plist_set_coord2(cpl_propertylist** plist,
00060 const double crpix2,
00061 const double crval2,
00062 const double cdelt2);
00063 static void
00064 xsh_plist_set_coord3(cpl_propertylist** plist,
00065 const int crpix3,
00066 const double crval3,
00067 const double cdelt3);
00068
00069
00070 static void
00071 xsh_plist_set_cd_matrix2(cpl_propertylist** plist,
00072 const double cd1_1,
00073 const double cd1_2,
00074 const double cd2_1,
00075 const double cd2_2);
00076
00077
00078 static void
00079 xsh_plist_set_cd_matrix3(cpl_propertylist** plist,
00080 const double cd1_3,
00081 const double cd2_3,
00082 const double cd3_1,
00083 const double cd3_2,
00084 const double cd3_3);
00085
00086
00087
00096 static void
00097 xsh_plist_set_coord1(cpl_propertylist** plist,
00098 const double crpix1,
00099 const double crval1,
00100 const double cdelt1)
00101 {
00102 cpl_propertylist_erase_regexp(*plist, "^CTYPE1",0);
00103 cpl_propertylist_insert_after_string(*plist,"EXPTIME","CTYPE1","RA---TAN");
00104 cpl_propertylist_set_comment(*plist, "CTYPE1", "Projected Rectascension");
00105 cpl_propertylist_erase_regexp(*plist, "^CRPIX1",0);
00106 cpl_propertylist_insert_after_double(*plist,"CTYPE1","CRPIX1", crpix1) ;
00107 cpl_propertylist_set_comment(*plist, "CRPIX1","Reference pixel in RA" ) ;
00108
00109 cpl_propertylist_erase_regexp(*plist, "^CRVAL1",0);
00110 cpl_propertylist_insert_after_double(*plist, "CRPIX1", "CRVAL1", crval1 ) ;
00111 cpl_propertylist_set_comment(*plist, "CRVAL1","Reference RA" ) ;
00112
00113 cpl_propertylist_erase_regexp(*plist, "^CDELT1",0);
00114 cpl_propertylist_insert_after_double(*plist,"CRVAL1","CDELT1",cdelt1 ) ;
00115 cpl_propertylist_set_comment(*plist, "CDELT1","pixel scale" ) ;
00116
00117 cpl_propertylist_erase_regexp(*plist, "^CUNIT1",0);
00118 cpl_propertylist_insert_after_string(*plist, "CDELT1", "CUNIT1", "deg" ) ;
00119 cpl_propertylist_set_comment(*plist, "CUNIT1","RA-UNIT" ) ;
00120
00121 return;
00122 }
00123
00124
00133 static void
00134 xsh_plist_set_coord2(cpl_propertylist** plist,
00135 const double crpix2,
00136 const double crval2,
00137 const double cdelt2)
00138 {
00139 cpl_propertylist_erase_regexp(*plist, "^CTYPE2",0);
00140 cpl_propertylist_insert_after_string(*plist,"CUNIT1","CTYPE2","DEC--TAN");
00141 cpl_propertylist_set_comment(*plist, "CTYPE2", "Projected Declination") ;
00142
00143 cpl_propertylist_erase_regexp(*plist, "^CRPIX2",0);
00144 cpl_propertylist_insert_after_double(*plist,"CTYPE2","CRPIX2",crpix2 ) ;
00145 cpl_propertylist_set_comment(*plist, "CRPIX2", "Reference pixel in DEC") ;
00146
00147 cpl_propertylist_erase_regexp(*plist,"^CRVAL2",0);
00148 cpl_propertylist_insert_after_double(*plist,"CRPIX2","CRVAL2",crval2) ;
00149 cpl_propertylist_set_comment(*plist,"CRVAL2","Reference DEC") ;
00150
00151 cpl_propertylist_erase_regexp(*plist,"^CDELT2",0);
00152 cpl_propertylist_insert_after_double(*plist,"CRVAL2","CDELT2",cdelt2 ) ;
00153 cpl_propertylist_set_comment(*plist,"CDELT2","pixel scale") ;
00154
00155 cpl_propertylist_erase_regexp(*plist,"^CUNIT2",0);
00156 cpl_propertylist_insert_after_string(*plist,"CDELT2","CUNIT2", "deg" ) ;
00157 cpl_propertylist_set_comment(*plist,"CUNIT2","DEC-UNIT") ;
00158
00159
00160 }
00161
00162
00163
00172 static void
00173 xsh_plist_set_coord3(cpl_propertylist** plist,
00174 const int crpix3,
00175 const double crval3,
00176 const double cdelt3)
00177 {
00178 cpl_propertylist_erase_regexp(*plist, "^CTYPE3",0);
00179 cpl_propertylist_insert_after_string(*plist,"EXPTIME", "CTYPE3", "WAVE" ) ;
00180 cpl_propertylist_set_comment(*plist,"CTYPE3","wavelength axis in nm") ;
00181
00182
00183 cpl_propertylist_erase_regexp(*plist, "^CRPIX3",0);
00184 cpl_propertylist_insert_after_double(*plist, "CTYPE3", "CRPIX3", (double)crpix3 ) ;
00185 cpl_propertylist_set_comment(*plist, "CRPIX3", "Reference pixel in z") ;
00186
00187 cpl_propertylist_erase_regexp(*plist, "^CRVAL3",0);
00188 cpl_propertylist_insert_after_double(*plist,"CRPIX3", "CRVAL3", crval3) ;
00189 cpl_propertylist_set_comment(*plist, "CRVAL3", "central wavelength") ;
00190
00191 cpl_propertylist_erase_regexp(*plist, "^CDELT3",0);
00192
00193 cpl_propertylist_insert_after_double(*plist,"CRVAL3","CDELT3",cdelt3) ;
00194 cpl_propertylist_set_comment(*plist, "CDELT3", "nm per pixel") ;
00195
00196 cpl_propertylist_erase_regexp(*plist, "^CUNIT3",0);
00197 cpl_propertylist_insert_after_string(*plist,"CDELT3", "CUNIT3", "nm" ) ;
00198 cpl_propertylist_set_comment(*plist, "CUNIT3", "spectral unit" ) ;
00199
00200 cpl_propertylist_erase_regexp(*plist, "^SPECSYS",0);
00201 cpl_propertylist_insert_after_string(*plist,"CUNIT3", "SPECSYS", "TOPOCENT" ) ;
00202 cpl_propertylist_set_comment(*plist, "SPECSYS", "Coordinate reference frame" ) ;
00203
00204 }
00205
00206
00217 static void
00218 xsh_plist_set_cd_matrix2(cpl_propertylist** plist,
00219 const double cd1_1,
00220 const double cd1_2,
00221 const double cd2_1,
00222 const double cd2_2)
00223 {
00224
00225 check(cpl_propertylist_erase_regexp(*plist, "^CD1_1",0));
00226 check(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
00227 "CD1_1", cd1_1 )) ;
00228 check(cpl_propertylist_set_comment(*plist, "CD1_1",
00229 "CD rotation matrix" )) ;
00230
00231 check(cpl_propertylist_erase_regexp(*plist, "^CD1_2",0));
00232 check(cpl_propertylist_insert_after_double(*plist, "CD1_1",
00233 "CD1_2", cd1_2 )) ;
00234 check(cpl_propertylist_set_comment(*plist, "CD1_2",
00235 "CD rotation matrix" )) ;
00236
00237 check(cpl_propertylist_erase_regexp(*plist, "^CD2_1",0));
00238 check(cpl_propertylist_insert_after_double(*plist, "CD1_2",
00239 "CD2_1", cd2_1 )) ;
00240 check(cpl_propertylist_set_comment(*plist, "CD2_1",
00241 "CD rotation matrix" )) ;
00242
00243 check(cpl_propertylist_erase_regexp(*plist, "^CD2_2",0));
00244 check(cpl_propertylist_insert_after_double(*plist, "CD2_1",
00245 "CD2_2", cd2_2 )) ;
00246 check(cpl_propertylist_set_comment(*plist, "CD2_2",
00247 "CD rotation matrix" )) ;
00248
00249 cleanup:
00250 return;
00251
00252
00253 }
00254
00255
00268 static void
00269 xsh_plist_set_cd_matrix3(cpl_propertylist** plist,
00270 const double cd1_3,
00271 const double cd2_3,
00272 const double cd3_1,
00273 const double cd3_2,
00274 const double cd3_3)
00275 {
00276
00277
00278 check(cpl_propertylist_erase_regexp(*plist, "^CD1_3",0));
00279 check(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
00280 "CD1_3", cd1_3 )) ;
00281 check(cpl_propertylist_set_comment(*plist, "CD1_3",
00282 "CD rotation matrix" )) ;
00283
00284
00285 check(cpl_propertylist_erase_regexp(*plist, "^CD2_3",0));
00286 check(cpl_propertylist_insert_after_double(*plist,"CD1_3",
00287 "CD2_3", cd2_3 )) ;
00288 check(cpl_propertylist_set_comment(*plist, "CD2_3",
00289 "CD rotation matrix" )) ;
00290
00291
00292
00293 check(cpl_propertylist_erase_regexp(*plist, "^CD3_1",0));
00294 check(cpl_propertylist_insert_after_double(*plist,"CD2_3",
00295 "CD3_1", cd3_1 )) ;
00296 check(cpl_propertylist_set_comment(*plist, "CD3_1",
00297 "CD rotation matrix" )) ;
00298
00299 check(cpl_propertylist_erase_regexp(*plist, "^CD3_2",0));
00300 check(cpl_propertylist_insert_after_double(*plist, "CD3_1",
00301 "CD3_2", cd3_2 )) ;
00302 check(cpl_propertylist_set_comment(*plist, "CD3_2",
00303 "CD rotation matrix" )) ;
00304
00305 check(cpl_propertylist_erase_regexp(*plist, "^CD3_3",0));
00306 check(cpl_propertylist_insert_after_double(*plist, "CD3_2",
00307 "CD3_3", cd3_3 )) ;
00308 check(cpl_propertylist_set_comment(*plist, "CD3_3",
00309 "CD rotation matrix" )) ;
00310
00311 cleanup:
00312 return;
00313
00314
00315 }
00316
00328 cpl_error_code
00329 xsh_cube_set_wcs(cpl_propertylist * plist,
00330 float cenLambda,
00331 float disp_x,
00332 float disp_y,
00333 float disp_z,
00334 float center_x,
00335 float center_y,
00336 int center_z)
00337 {
00338
00339
00340 double ra ;
00341 double dec ;
00342 double angle ;
00343 float radangle ;
00344 double cd1_1, cd1_2, cd2_1, cd2_2 ;
00345 int sign_swap = -1;
00346
00347
00348 double cdelt1=disp_x;
00349 double cdelt2=disp_y;
00350 double cdelt3=disp_z;
00351
00352 double crpix1=center_x;
00353 double crpix2=center_y;
00354 int crpix3=center_z;
00355
00356 double crval1=0;
00357 double crval2=0;
00358 double crval3=cenLambda;
00359
00360 ra = xsh_pfits_get_ra(plist) ;
00361 dec = xsh_pfits_get_dec(plist) ;
00362
00363
00364 ra=xsh_pfits_get_tel_targ_alpha(plist);
00365 dec=xsh_pfits_get_tel_targ_delta(plist);
00366
00367
00368
00369 ra=xsh_hms2deg(ra);
00370 dec=xsh_sess2deg(dec);
00371
00372
00373
00374 crval1=ra;
00375 crval2=dec;
00376
00377 angle = xsh_pfits_get_posangle(plist) ;
00378
00379 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00380 cpl_error_reset();
00381 }
00382 cdelt1=sign_swap*disp_x / 3600.;
00383 cdelt2= +disp_y / 3600.;
00384
00385
00386 radangle = angle * PI_NUMB / 180. ;
00387 cd1_1 = +cdelt1*cos(radangle);
00388 cd1_2 = -cdelt2*sin(radangle);
00389 cd2_1 = +cdelt1*sin(radangle);
00390 cd2_2 = +cdelt2*cos(radangle);
00391
00392
00393 xsh_plist_set_coord1(&plist,crpix1,crval1,cdelt1);
00394 xsh_plist_set_coord2(&plist,crpix2,crval2,cdelt2);
00395
00396 xsh_plist_set_coord3(&plist,crpix3,crval3,cdelt3);
00397 xsh_plist_set_cd_matrix2(&plist,cd1_1,cd1_2,cd2_1,cd2_2);
00398 xsh_plist_set_cd_matrix3(&plist,0,0,0,0,disp_z);
00399
00400 return cpl_error_get_code();
00401 }
00402
00403
00404
00405
00406
00407
00408
00411
00422
00423
00424
00425 void
00426 xsh_edge_check(const int px,const int nx,const int rad_x,
00427 int* llx,int* urx)
00428 {
00429
00430
00431 *llx=px-rad_x;
00432 *urx=px+rad_x;
00433 *llx=(*llx>1) ? *llx : 1;
00434 *urx=(*urx<nx) ? *urx : nx;
00435 return;
00436
00437 }
00438
00439
00440
00455
00456
00457 void
00458 xsh_convert_xy_to_ws(double x_centroid,
00459 double* p_obj_cen,
00460 double* p_slit,
00461 double* p_wave,
00462 const int lly,
00463 const int nx,
00464 const int row,
00465 double* p_obj_cen_s,
00466 double* p_obj_cen_w)
00467 {
00468 int x_ceil=0;
00469 int x_floor=0;
00470 double s_ceil=0;
00471 double s_floor=0;
00472 double w_ceil=0;
00473 double w_floor=0;
00474
00475 p_obj_cen[row]=x_centroid;
00476 x_ceil=ceil(x_centroid);
00477 x_floor=floor(x_centroid);
00478
00479 s_ceil =p_slit[lly*nx+x_ceil];
00480 s_floor=p_slit[lly*nx+x_floor];
00481
00482 w_ceil =p_wave[lly*nx+x_ceil];
00483 w_floor=p_wave[lly*nx+x_floor];
00484
00485 p_obj_cen_s[row]=xsh_interpol(x_centroid,x_floor,x_ceil,s_floor,s_ceil);
00486 p_obj_cen_w[row]=xsh_interpol(x_centroid,x_floor,x_ceil,w_floor,w_ceil);
00487
00488 return;
00489 }
00490
00491
00492
00499
00500
00501 cpl_error_code
00502 xsh_table_edges_swap_low_upp(cpl_table** tab)
00503 {
00504
00505 cpl_table_duplicate_column(*tab,"OBJ_LOW_S_TMP",*tab,"OBJ_LOW_S");
00506 cpl_table_duplicate_column(*tab,"OBJ_UPP_S_TMP",*tab,"OBJ_UPP_S");
00507
00508 cpl_table_erase_column(*tab,"OBJ_LOW_S");
00509 cpl_table_erase_column(*tab,"OBJ_UPP_S");
00510
00511 cpl_table_duplicate_column(*tab,"OBJ_UPP_S",*tab,"OBJ_LOW_S_TMP");
00512 cpl_table_duplicate_column(*tab,"OBJ_LOW_S",*tab,"OBJ_UPP_S_TMP");
00513
00514 cpl_table_erase_column(*tab,"OBJ_LOW_S_TMP");
00515 cpl_table_erase_column(*tab,"OBJ_UPP_S_TMP");
00516
00517 cpl_table_duplicate_column(*tab,"OBJ_LOW_W_TMP",*tab,"OBJ_LOW_W");
00518 cpl_table_duplicate_column(*tab,"OBJ_UPP_W_TMP",*tab,"OBJ_UPP_W");
00519
00520 cpl_table_erase_column(*tab,"OBJ_LOW_W");
00521 cpl_table_erase_column(*tab,"OBJ_UPP_W");
00522
00523 cpl_table_duplicate_column(*tab,"OBJ_UPP_W",*tab,"OBJ_LOW_W_TMP");
00524 cpl_table_duplicate_column(*tab,"OBJ_LOW_W",*tab,"OBJ_UPP_W_TMP");
00525
00526 cpl_table_erase_column(*tab,"OBJ_LOW_W_TMP");
00527 cpl_table_erase_column(*tab,"OBJ_UPP_W_TMP");
00528
00529
00530 return cpl_error_get_code();
00531 }
00532
00533
00534
00535
00542
00543
00544 cpl_table*
00545 xsh_table_edge_prepare(const char* name)
00546 {
00547 cpl_table* tab=NULL;
00548 int nrow=0;
00549
00550 check(tab=cpl_table_load(name,2,0));
00551
00552 nrow=cpl_table_get_nrow(tab);
00553
00554 cpl_table_new_column(tab,"OBJ_LOW_X",CPL_TYPE_DOUBLE);
00555 cpl_table_new_column(tab,"OBJ_CEN_X",CPL_TYPE_DOUBLE);
00556 cpl_table_new_column(tab,"OBJ_UPP_X",CPL_TYPE_DOUBLE);
00557
00558 cpl_table_fill_column_window(tab,"OBJ_LOW_X",0,nrow,-1);
00559 cpl_table_fill_column_window(tab,"OBJ_CEN_X",0,nrow,-1);
00560 cpl_table_fill_column_window(tab,"OBJ_UPP_X",0,nrow,-1);
00561
00562 cpl_table_new_column(tab,"OBJ_LOW_S",CPL_TYPE_DOUBLE);
00563 cpl_table_new_column(tab,"OBJ_LOW_W",CPL_TYPE_DOUBLE);
00564 cpl_table_new_column(tab,"OBJ_CEN_S",CPL_TYPE_DOUBLE);
00565 cpl_table_new_column(tab,"OBJ_CEN_W",CPL_TYPE_DOUBLE);
00566 cpl_table_new_column(tab,"OBJ_UPP_S",CPL_TYPE_DOUBLE);
00567 cpl_table_new_column(tab,"OBJ_UPP_W",CPL_TYPE_DOUBLE);
00568
00569 cpl_table_fill_column_window(tab,"OBJ_LOW_S",0,nrow,-1);
00570 cpl_table_fill_column_window(tab,"OBJ_LOW_W",0,nrow,-1);
00571 cpl_table_fill_column_window(tab,"OBJ_CEN_S",0,nrow,-1);
00572 cpl_table_fill_column_window(tab,"OBJ_CEN_W",0,nrow,-1);
00573 cpl_table_fill_column_window(tab,"OBJ_UPP_S",0,nrow,-1);
00574 cpl_table_fill_column_window(tab,"OBJ_UPP_W",0,nrow,-1);
00575
00576 cleanup:
00577 return tab;
00578
00579 }
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00696
00697
00698 cpl_error_code
00699 xsh_ifu_trace_object_calibrate(const char* ifu_object_ff_name,
00700 const char* order_tab_edges_ifu_name,
00701 const char* slit_map_name,
00702 const char* wave_map_name)
00703 {
00704 cpl_image* ifu_object_ff_ima=NULL;
00705 cpl_image* wave_map_ima=NULL;
00706 cpl_image* slit_map_ima=NULL;
00707 cpl_image* ratio_ima=NULL;
00708 cpl_table* tab=NULL;
00709
00710
00711
00712
00713 int nrow=0;
00714
00715 double* p_edge_lo_x=NULL;
00716 double* p_edge_up_x=NULL;
00717
00718 double* p_slice_lo_x=NULL;
00719 double* p_slice_up_x=NULL;
00720
00721 double* p_center_y=NULL;
00722
00723 double* p_obj_low=NULL;
00724 double* p_obj_cen=NULL;
00725 double* p_obj_upp=NULL;
00726
00727 double* p_obj_low_s=NULL;
00728 double* p_obj_low_w=NULL;
00729 double* p_obj_cen_s=NULL;
00730 double* p_obj_cen_w=NULL;
00731 double* p_obj_upp_s=NULL;
00732 double* p_obj_upp_w=NULL;
00733
00734 double* p_slit=NULL;
00735 double* p_wave=NULL;
00736
00737 double x_centroid=0;
00738
00739 int llx=0;
00740 int lly=0;
00741 int urx=0;
00742 int ury=0;
00743
00744 int row=0;
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 int nx=0;
00755 int ny=0;
00756 cpl_propertylist* plist=NULL;
00757 XSH_ARM arm=XSH_ARM_UNDEFINED;
00758 const char* pcatg=NULL;
00759 char tag[25];
00760 char name[256];
00761 cpl_size px=0;
00762 cpl_size py=0;
00763
00764 int rad_x=10;
00765
00766
00767
00768
00769 int biny=1;
00770
00771 typedef enum {centroid, gaussian} xsh_fit_method;
00772 xsh_fit_method fit_method = centroid;
00773
00774
00775 check(ifu_object_ff_ima=cpl_image_load(ifu_object_ff_name,CPL_TYPE_DOUBLE,0,0));
00776 plist=cpl_propertylist_load(ifu_object_ff_name,0);
00777 pcatg=xsh_pfits_get_pcatg(plist);
00778
00779 xsh_msg("pcatg=%s",pcatg);
00780
00781 if(strstr(pcatg,"UVB") != NULL) {
00782 arm=XSH_ARM_UVB;
00783
00784
00785
00786
00787 biny=xsh_pfits_get_biny(plist);
00788
00789
00790 } else if(strstr(pcatg,"VIS") != NULL) {
00791 arm=XSH_ARM_VIS;
00792
00793
00794
00795
00796
00797 biny=xsh_pfits_get_biny(plist);
00798
00799 } else if(strstr(pcatg,"NIR") != NULL) {
00800 arm=XSH_ARM_NIR;
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810 }
00811
00812 check(slit_map_ima=cpl_image_load(slit_map_name,CPL_TYPE_DOUBLE,0,0));
00813 check(wave_map_ima=cpl_image_load(wave_map_name,CPL_TYPE_DOUBLE,0,0));
00814
00815
00816
00817 check(tab=xsh_table_edge_prepare(order_tab_edges_ifu_name));
00818 nrow=cpl_table_get_nrow(tab);
00819
00820 p_edge_lo_x=cpl_table_get_data_double(tab,"EDG_LO_X");
00821 p_slice_lo_x=cpl_table_get_data_double(tab,"SLIC_LO_X");
00822 p_obj_low=cpl_table_get_data_double(tab,"OBJ_LOW_X");
00823 p_obj_low_s=cpl_table_get_data_double(tab,"OBJ_LOW_S");
00824 p_obj_low_w=cpl_table_get_data_double(tab,"OBJ_LOW_W");
00825
00826 p_edge_up_x=cpl_table_get_data_double(tab,"EDG_UP_X");
00827 p_slice_up_x=cpl_table_get_data_double(tab,"SLIC_UP_X");
00828 p_obj_upp=cpl_table_get_data_double(tab,"OBJ_UPP_X");
00829 p_obj_upp_s=cpl_table_get_data_double(tab,"OBJ_UPP_S");
00830 p_obj_upp_w=cpl_table_get_data_double(tab,"OBJ_UPP_W");
00831
00832 p_center_y=cpl_table_get_data_double(tab,"CENTER_Y");
00833 p_obj_cen=cpl_table_get_data_double(tab,"OBJ_CEN_X");
00834 p_obj_cen_s=cpl_table_get_data_double(tab,"OBJ_CEN_S");
00835 p_obj_cen_w=cpl_table_get_data_double(tab,"OBJ_CEN_W");
00836
00837 p_slit=cpl_image_get_data_double(slit_map_ima);
00838 p_wave=cpl_image_get_data_double(wave_map_ima);
00839 nx=cpl_image_get_size_x(wave_map_ima);
00840 ny=cpl_image_get_size_y(wave_map_ima);
00841
00842 for(row=0;row<nrow;row++) {
00843
00844 lly=(int)p_center_y[row]/biny;
00845 ury=(int)p_center_y[row]/biny;
00846
00847
00848 if((llx<nx) && (ury<ny) ) {
00849
00850
00851 llx=floor(p_slice_lo_x[row]);
00852 urx=ceil(p_slice_up_x[row]);
00853
00854 check(cpl_image_get_maxpos_window(ifu_object_ff_ima,
00855 llx,lly,urx,ury,&px,&py));
00856
00857 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00858 if(fit_method==centroid) {
00859 x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00860 llx,lly,urx,ury);
00861
00862 } else {
00863 check(x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00864 llx,urx,lly));
00865 }
00866
00867
00868 p_obj_cen[row]=x_centroid;
00869
00870 if(urx<nx) {
00871 check(xsh_convert_xy_to_ws(x_centroid,p_obj_cen,p_slit,p_wave,
00872 lly,nx,row,p_obj_cen_s,p_obj_cen_w));
00873 }
00874
00875
00876 llx=floor(p_edge_lo_x[row]);
00877 urx=ceil(p_slice_lo_x[row]);
00878 cpl_image_get_maxpos_window(ifu_object_ff_ima,
00879 llx,lly,urx,ury,&px,&py);
00880
00881 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00882 if(fit_method==centroid) {
00883 check(x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00884 llx,lly,urx,ury));
00885 } else {
00886 x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00887 llx,urx,lly);
00888 }
00889
00890
00891
00892 if(urx<nx) {
00893 check(xsh_convert_xy_to_ws(x_centroid,p_obj_low,p_slit,p_wave,
00894 lly,nx,row,p_obj_low_s,p_obj_low_w));
00895 }
00896
00897
00898 llx=floor(p_slice_up_x[row]);
00899 urx=ceil(p_edge_up_x[row]);
00900 cpl_image_get_maxpos_window(ifu_object_ff_ima,
00901 llx,lly,urx,ury,&px,&py);
00902
00903 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00904 if(fit_method==centroid) {
00905 x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00906 llx,lly,urx,ury);
00907 } else {
00908 x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00909 llx,urx,lly);
00910 }
00911
00912 p_obj_upp[row]=x_centroid;
00913
00914 if(urx<nx) {
00915 check(xsh_convert_xy_to_ws(x_centroid,p_obj_upp,p_slit,p_wave,
00916 lly,nx,row,p_obj_upp_s,p_obj_upp_w));
00917 }
00918 }
00919 }
00920
00921 if(arm==XSH_ARM_VIS) {
00922 xsh_table_edges_swap_low_upp(&tab);
00923 }
00924 cpl_table_duplicate_column(tab,"OBJ_CEN_PLUS_UPP_S",tab,"OBJ_CEN_S");
00925
00926 cpl_table_duplicate_column(tab,"OBJ_CEN_PLUS_LOW_S",tab,"OBJ_CEN_S");
00927
00928 cpl_table_add_columns(tab,"OBJ_CEN_PLUS_UPP_S","OBJ_UPP_S");
00929 cpl_table_add_columns(tab,"OBJ_CEN_PLUS_LOW_S","OBJ_LOW_S");
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953 sprintf(tag,"TRACE_OBJ_%s",xsh_arm_tostring(arm));
00954 sprintf(name,"%s.fits",tag);
00955 xsh_pfits_set_pcatg(plist,tag);
00956 check(cpl_table_save(tab,plist,NULL,name,CPL_IO_DEFAULT));
00957
00958
00959
00960 cleanup:
00961 xsh_free_image(&ifu_object_ff_ima);
00962 xsh_free_image(&slit_map_ima);
00963 xsh_free_image(&wave_map_ima);
00964 xsh_free_image(&ratio_ima);
00965 xsh_free_propertylist(&plist);
00966 return cpl_error_get_code();
00967
00968 }
00969
00970
00971
00979
00980
00981 cpl_error_code
00982 xsh_frame_check_model_cfg_is_proper_for_sci(cpl_frame* model_config_frame,
00983 cpl_frame* sci_frame,
00984 xsh_instrument* instrument)
00985 {
00986
00987 cpl_propertylist* afc_plist=NULL;
00988 cpl_propertylist* sci_plist=NULL;
00989 const char* name=NULL;
00990 const char* sci_obs_targ_name=NULL;
00991 const char* afc_obs_targ_name=NULL;
00992 const char* afc_slit_value=NULL;
00993
00994 int sci_obs_id=0;
00995 int afc_obs_id=0;
00996
00997 check(name=cpl_frame_get_filename(model_config_frame));
00998 check(afc_plist=cpl_propertylist_load(name,0));
00999
01000 check(name=cpl_frame_get_filename(sci_frame));
01001 check(sci_plist=cpl_propertylist_load(name,0));
01002
01003 check(afc_slit_value=xsh_pfits_get_slit_value(afc_plist,instrument ));
01004
01005
01006
01007 if(strstr(afc_slit_value,"Pin_0.5") == NULL) {
01008 xsh_msg_error("You have used uncorrect AFC corrected model cfg frame");
01009 xsh_msg_error("IFU AFC corrected model CFG must have INS.OPTIi.NAME='Pin_0.5'");
01010 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01011 }
01012
01013
01014 check(sci_obs_targ_name=xsh_pfits_get_obs_targ_name(sci_plist));
01015 check(afc_obs_targ_name=xsh_pfits_get_obs_targ_name(afc_plist));
01016 if(strcmp(sci_obs_targ_name,afc_obs_targ_name) != 0) {
01017 xsh_msg_error("Improper AFC corrected model cfg frame to reduce sci frame");
01018 xsh_msg_error("Their OBS.TARG.NAME values must match");
01019 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01020
01021 }
01022
01023 check(sci_obs_id=xsh_pfits_get_obs_id(sci_plist));
01024 check(afc_obs_id=xsh_pfits_get_obs_id(afc_plist));
01025
01026 if(sci_obs_id != afc_obs_id) {
01027 xsh_msg_error("Improper AFC corrected model cfg frame to reduce sci frame");
01028 xsh_msg_error("Their OBS.ID values must match");
01029 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01030 }
01031
01032 cleanup:
01033
01034 xsh_free_propertylist(&sci_plist);
01035 xsh_free_propertylist(&afc_plist);
01036
01037 return cpl_error_get_code();
01038 }
01039
01040
01041
01048
01049
01050 cpl_error_code
01051 xsh_frame_check_model_cfg_is_afc_corrected(cpl_frame* model_config_frame){
01052
01053 cpl_propertylist* plist=NULL;
01054 const char* name=NULL;
01055 const char* raw1_catg=NULL;
01056
01057 check(name=cpl_frame_get_filename(model_config_frame));
01058 check(plist=cpl_propertylist_load(name,0));
01059 check(raw1_catg=xsh_pfits_get_raw1catg(plist));
01060 if(strstr(raw1_catg,"AFC_ATT") == NULL) {
01061 xsh_msg_error("model cfg frame seems not to be AFC corrected");
01062 xsh_msg_error("Their PRO.REC1.RAW1..NAME values must contain AFC_ATT");
01063 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01064 }
01065
01066 cleanup:
01067
01068 xsh_free_propertylist(&plist);
01069
01070 return cpl_error_get_code();
01071
01072 }
01073
01074 static cpl_error_code
01075 xsh_add_correct_coeff(cpl_table** table, cpl_propertylist* plist,
01076 const char* prefix, const int row,
01077 const char* col12, const char* col32)
01078 {
01079 double coeff_t1=0;
01080 double coeff_t2=0;
01081 double coeff_t3=0;
01082 double diff_c12=0;
01083 double diff_c32=0;
01084 char key_name[40];
01085
01086
01087 sprintf(key_name,"%s_%s",prefix,"T1");
01088 check(coeff_t1=cpl_propertylist_get_double(plist,key_name));
01089
01090 sprintf(key_name,"%s_%s",prefix,"T2");
01091 coeff_t2=cpl_propertylist_get_double(plist,key_name);
01092
01093 sprintf(key_name,"%s_%s",prefix,"T3");
01094 coeff_t3=cpl_propertylist_get_double(plist,key_name);
01095 diff_c12=coeff_t1-coeff_t2;
01096 diff_c32=coeff_t3-coeff_t2;
01097
01098 cpl_table_set_double(*table,col12 ,row, diff_c12);
01099 cpl_table_set_double(*table,col32 ,row, diff_c32);
01100
01101 cleanup:
01102
01103 return cpl_error_get_code();
01104
01105 }
01106
01107 static cpl_frame*
01108 xsh_crea_correct_coeff(cpl_frame* qc_trace_merged_frame,xsh_instrument* inst)
01109 {
01110
01111 cpl_propertylist* plist=NULL;
01112 const char* fname=NULL;
01113 cpl_table* table=NULL;
01114 cpl_frame* result=NULL;
01115 char pname[256];
01116 char ptag[40];
01117
01118 fname=cpl_frame_get_filename(qc_trace_merged_frame);
01119 plist=cpl_propertylist_load(fname,0);
01120
01121 table=cpl_table_new(3);
01122 check(cpl_table_new_column(table,"DIFF_T12", CPL_TYPE_DOUBLE));
01123 check(cpl_table_new_column(table,"DIFF_T32", CPL_TYPE_DOUBLE));
01124
01125
01126 check(xsh_add_correct_coeff(&table,plist,XSH_QC_TRACE_FIT_C0,0,
01127 "DIFF_T12","DIFF_T32"));
01128
01129 check(xsh_add_correct_coeff(&table,plist,XSH_QC_TRACE_FIT_C1,1,
01130 "DIFF_T12","DIFF_T32"));
01131
01132 check(xsh_add_correct_coeff(&table,plist,XSH_QC_TRACE_FIT_C2,2,
01133 "DIFF_T12","DIFF_T32"));
01134
01135 sprintf(ptag,"IFU_CFG_COR_%s",xsh_instrument_arm_tostring(inst));
01136 sprintf(pname,"%s.fits",ptag);
01137 xsh_msg("tag=%s name=%s",ptag,pname);
01138
01139 check(cpl_table_save(table,plist,NULL,pname,CPL_IO_DEFAULT));
01140 result=xsh_frame_product(pname,ptag,CPL_FRAME_TYPE_TABLE,
01141 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01142
01143
01144 cleanup:
01145
01146 xsh_free_propertylist(&plist);
01147 xsh_free_table(&table);
01148
01149 return result;
01150 }
01151
01152 static cpl_frame*
01153 xsh_frame_build_sky_area(cpl_frame* slitmap_frame, const char* tag) {
01154
01155 char name_o[256];
01156 cpl_frame* result = NULL;
01157
01158 cpl_image* ima_slit = NULL;
01159 cpl_image* ima_sky = NULL;
01160 float* pslit = NULL;
01161 float* psky = NULL;
01162
01163 const char* name = NULL;
01164 cpl_propertylist* plist = NULL;
01165 int sx = 0;
01166 int sy = 0;
01167 int i = 0;
01168 int j = 0;
01169
01170 check(name = cpl_frame_get_filename(slitmap_frame));
01171 cpl_frame_dump(slitmap_frame, stdout);
01172 check(ima_slit = cpl_image_load(name, CPL_TYPE_FLOAT, 0, 0));
01173 check(plist = cpl_propertylist_load(name, 0));
01174 pslit = cpl_image_get_data_float(ima_slit);
01175
01176 sx = cpl_image_get_size_x(ima_slit);
01177 sy = cpl_image_get_size_y(ima_slit);
01178 ima_sky = cpl_image_new(sx, sy, CPL_TYPE_FLOAT);
01179 psky = cpl_image_get_data_float(ima_sky);
01180 for (j = 1; j < sy - 1; j++) {
01181 for (i = 1; i < sx - 1; i++) {
01182
01183 psky[j * sx + i] = 0.25 * (pslit[j * sx + i + 1] - pslit[j * sx + i - 1])
01184 * (pslit[(j + 1) * sx + i] - pslit[(j - 1) * sx + i]);
01185
01186 }
01187 }
01188
01189 sprintf(name_o, "%s.fits", tag);
01190 check(cpl_image_save(ima_sky, name_o, XSH_PRE_DATA_BPP, plist, CPL_IO_DEFAULT));
01191
01192 result = cpl_frame_duplicate(slitmap_frame);
01193 cpl_frame_set_filename(result, name_o);
01194 cpl_frame_set_tag(result, tag);
01195
01196 cleanup: xsh_free_propertylist(&plist);
01197 xsh_free_image(&ima_slit);
01198 xsh_free_image(&ima_sky);
01199 return result;
01200
01201 }
01202
01203 static cpl_frame*
01204 xsh_frame_build_sky_map(cpl_frame* slitmap_frame,const double value,const char* tag)
01205 {
01206
01207 char name_o[256];
01208 cpl_frame* result=NULL;
01209
01210 cpl_image* ima = NULL;
01211 const char* name = NULL;
01212 cpl_propertylist* plist = NULL;
01213
01214 check(name = cpl_frame_get_filename(slitmap_frame));
01215 check(ima = cpl_image_load(name, XSH_PRE_DATA_TYPE, 0, 0));
01216 check(plist = cpl_propertylist_load(name, 0));
01217
01218 check(cpl_image_add_scalar(ima,value));
01219 sprintf(name_o,"%s.fits",tag);
01220 check(cpl_image_save(ima, name_o, XSH_PRE_DATA_BPP, plist, CPL_IO_DEFAULT));
01221
01222 result = cpl_frame_duplicate(slitmap_frame);
01223 cpl_frame_set_filename(result, name_o);
01224 cpl_frame_set_tag(result, tag);
01225 xsh_add_temporary_file(name_o);
01226
01227 cleanup:
01228 xsh_free_propertylist(&plist);
01229 xsh_free_image(&ima);
01230
01231 return result;
01232
01233 }
01234
01244
01245 cpl_frame*
01246 xsh_build_ifu_map(cpl_frame* div_frame,
01247 cpl_frame* wavemap_frame,
01248 cpl_frame* slitmap_frame,
01249 cpl_frame* model_config_frame,
01250 xsh_instrument* instrument)
01251 {
01252
01253 cpl_frame* map = NULL;
01254 cpl_frame* ra_map = NULL;
01255 cpl_frame* dec_map = NULL;
01256 cpl_frame* sky_area = NULL;
01257
01258 char name_o[256];
01259 char tag_o[256];
01260
01261 cpl_image* ima = NULL;
01262 const char* name = NULL;
01263 cpl_propertylist* plist = NULL;
01264 double RA=0;
01265 double DEC=0;
01266
01267 name = cpl_frame_get_filename(div_frame);
01268 plist = cpl_propertylist_load(name, 0);
01269
01270 sprintf(tag_o,"%s_%s",XSH_IFU_MAP_SKY,xsh_instrument_arm_tostring(instrument));
01271 sprintf(name_o,"%s.fits",tag_o);
01272
01273
01274
01275
01276
01277 xsh_frame_image_save2ext(div_frame,name_o,0,0);
01278 xsh_frame_image_save2ext(wavemap_frame,name_o,0,1);
01279 xsh_frame_image_save2ext(slitmap_frame,name_o,0,2);
01280 xsh_frame_image_save2ext(slitmap_frame,name_o,1,3);
01281
01282
01283
01284
01285
01286
01287 RA=xsh_pfits_get_tel_targ_alpha(plist);
01288 DEC=xsh_pfits_get_tel_targ_delta(plist);
01289
01290
01291 RA=xsh_hms2deg(RA);
01292
01293 DEC=xsh_sess2deg(DEC);
01294
01295
01296 RA*=3600;
01297 DEC*=3600;
01298
01299
01300 sprintf(tag_o,"%s_%s","IFU_MAP_SKY_RA",xsh_instrument_arm_tostring(instrument));
01301 check(ra_map=xsh_frame_build_sky_map(slitmap_frame,RA,tag_o));
01302 check(xsh_frame_image_save2ext(ra_map,name_o,0,4));
01303
01304
01305 sprintf(tag_o,"%s_%s","IFU_MAP_SKY_DEC",xsh_instrument_arm_tostring(instrument));
01306 check(dec_map=xsh_frame_build_sky_map(slitmap_frame,DEC,tag_o));
01307 check(xsh_frame_image_save2ext(dec_map,name_o,0,5));
01308
01309
01310 sprintf(tag_o,"%s_%s","IFU_MAP_SKY_AREA",xsh_instrument_arm_tostring(instrument));
01311 check(sky_area=xsh_frame_build_sky_area(slitmap_frame,tag_o));
01312 check(xsh_frame_image_save2ext(sky_area,name_o,0,6));
01313 xsh_add_temporary_file(name_o);
01314
01315
01316
01317
01318
01319 map = xsh_frame_product(name_o, tag_o, CPL_FRAME_TYPE_IMAGE,
01320 CPL_FRAME_GROUP_PRODUCT, CPL_FRAME_LEVEL_FINAL);
01321
01322 cleanup:
01323 xsh_free_propertylist(&plist);
01324 xsh_free_image(&ima);
01325
01326 return map;
01327
01328 }
01329
01330
01331
01332
01333
01351
01352 cpl_error_code
01353 xsh_build_ifu_cube(cpl_frame* div_frame,
01354 cpl_frame* ifu_cfg_tab_frame,
01355 cpl_frame* ifu_cfg_cor_frame,
01356 cpl_frame* spectral_format_frame,
01357 cpl_frame* model_config_frame,
01358 cpl_frame* wavesol_frame,
01359 xsh_instrument* instrument,
01360 cpl_frameset* frameset,
01361 cpl_parameterlist* parameters,
01362 xsh_rectify_param * rectify_par,
01363 const char* recipe_id,
01364 const char* rec_prefix,
01365 const int frame_is_object)
01366 {
01367
01368
01369
01370
01371 cpl_image* data_img=NULL;
01372 cpl_image* errs_img=NULL;
01373 cpl_image* qual_img=NULL;
01374
01375 cpl_image* data_tmp=NULL;
01376 cpl_image* errs_tmp=NULL;
01377 cpl_image* qual_tmp=NULL;
01378 cpl_image* mask_tmp=NULL;
01379 cpl_frame* qc_trace_frame=NULL;
01380 cpl_frame* qc_trace_merged_frame=NULL;
01381 cpl_frame* frame_data_cube=NULL;
01382 cpl_frame* frame_errs_cube=NULL;
01383 cpl_frame* frame_qual_cube=NULL;
01384 cpl_frame* frame_merged_cube=NULL;
01385
01386
01387
01388
01389
01390 XSH_ARM arm;
01391 xsh_xs_3 model_config ;
01392 xsh_rec_list * rec_list = NULL ;
01393
01394 cpl_table* ifu_cfg_tab=NULL;
01395 cpl_table* sp_fmt_tab=NULL;
01396 cpl_vector* profile=NULL;
01397 cpl_imagelist* data_cube=NULL;
01398 cpl_imagelist* errs_cube=NULL;
01399 cpl_imagelist* qual_cube=NULL;
01400
01401 cpl_imagelist* data_cube_merge=NULL;
01402 cpl_imagelist* errs_cube_merge=NULL;
01403 cpl_imagelist* qual_cube_merge=NULL;
01404 cpl_imagelist* mask_data_merge=NULL;
01405 cpl_imagelist* mask_errs_merge=NULL;
01406 cpl_imagelist* mask_qual_merge=NULL;
01407
01408
01409 float* pima=NULL;
01410 float* perr=NULL;
01411 int* pqua=NULL;
01412 int* pmsk=NULL;
01413 int nord=0;
01414 int save_size_uvb=4;
01415 int save_size_vis=2;
01416 int save_size_nir=2;
01417 int save_size=0;
01418 const int peack_search_hsize=5;
01419 int method=0;
01420
01421
01422
01423 cpl_propertylist* data_plist=NULL;
01424 cpl_propertylist* errs_plist=NULL;
01425 cpl_propertylist* qual_plist=NULL;
01426
01427
01428 const char* sci_name=NULL;
01429 const char* sp_fmt_name=NULL;
01430 const char* ifu_cfg_name=NULL;
01431
01432 char name[256];
01433 char data_extid[40];
01434 char errs_extid[40];
01435 char qual_extid[40];
01436
01437 char qualifier[10];
01438 int binx=1;
01439 int biny=1;
01440
01441 double flux_upp=0;
01442 double flux_cen=0;
01443 double flux_low=0;
01444
01445
01446 double errs_upp=0;
01447 double errs_cen=0;
01448 double errs_low=0;
01449 double cube_wave_min=0;
01450 double cube_wave_max=0;
01451
01452
01453 int qual_upp=0;
01454 int qual_cen=0;
01455 int qual_low=0;
01456
01457
01458
01459 double cube_wstep=0;
01460 double cube_sstep=0;
01461
01462 int is=0;
01463
01464 int ord=0;
01465 int ord_min=0;
01466 int ord_max=0;
01467
01468 double wave=0;
01469 double wave_min=0;
01470 double wave_max=0;
01471 double wave_step=0;
01472
01473 double wave_min_old=0;
01474
01475
01476 int nstep_off=0;
01477
01478 double s=0;
01479 double s_min=-2;
01480 double s_max=2;
01481 double s_step=0;
01482
01483 double x=0;
01484 double y=0;
01485 double s_upp=0;
01486 double s_low=0;
01487
01488
01489 int radius=rectify_par->rectif_radius;
01490
01491 double confidence=0;
01492 int naxis1=0;
01493 int naxis2=0;
01494 int naxis3=0;
01495 int ik=0;
01496
01497
01498
01499
01500
01501
01502
01503 double w_low_coeff1=0;
01504 double w_low_coeff2=0;
01505
01506 double w_upp_coeff1=0;
01507 double w_upp_coeff2=0;
01508
01509 double w_low_coeff1_uvb=-0.002972;
01510 double w_low_coeff2_uvb=2.26497e-6+0.2e-6;
01511 double w_upp_coeff1_uvb=8.3355331e-5;
01512 double w_upp_coeff2_uvb=-1.0682e-6;
01513
01514
01515
01516 double w_low_coeff1_vis=-0.0016549569;
01517 double w_low_coeff2_vis=1.183805e-6+0.04e-6;
01518 double w_upp_coeff1_vis=-0.0016610719;
01519 double w_upp_coeff2_vis=1.0823013e-6+0.02e-6;
01520
01521 double w_low_coeff1_nir=-0.00010;
01522 double w_low_coeff2_nir=-5.0e-9;
01523
01524 double w_upp_coeff1_nir=0;
01525 double w_upp_coeff2_nir=0;
01526
01527
01528 double s_upp_off_uvb=4.0514429+0.16;
01529 double s_upp_off_vis=5.2504895+0.16-0.703;
01530 double s_upp_off_nir=3.5452+0.31;
01531 double s_upp_off=0;
01532
01533 double s_low_off_uvb=-3.4636662+0.16;
01534
01535 double s_low_off_vis=-2.9428071+0.16-0.645;
01536 double s_low_off_nir=-4.451682+0.21+0.31;
01537
01538 double s_low_off=0;
01539
01540 int w_step_fct=10;
01541
01542 int status=0;
01543 int mk=0;
01544 char tag[256];
01545 xsh_wavesol *wavesol=NULL;
01546 cpl_frame* trace_corr_tab=NULL;
01547 double s_upp_d0=0;
01548 double s_low_d0=0;
01549 double s_upp_d1=0;
01550 double s_low_d1=0;
01551 double s_upp_d2=0;
01552 double s_low_d2=0;
01553
01554 if(frame_is_object) {
01555 sprintf(qualifier,"OBJ");
01556 } else {
01557 sprintf(qualifier,"SKY");
01558 }
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585 check(sci_name=cpl_frame_get_filename(div_frame));
01586 check(data_plist=cpl_propertylist_load(sci_name,0));
01587 errs_plist=cpl_propertylist_duplicate(data_plist);
01588 qual_plist=cpl_propertylist_duplicate(data_plist);
01589
01590 arm=instrument->arm;
01591
01592
01593 cube_wstep=rectify_par->rectif_bin_lambda;
01594 cube_sstep=rectify_par->rectif_bin_space;
01595
01596 if(arm == XSH_ARM_UVB) {
01597
01598
01599
01600 binx=xsh_pfits_get_binx(data_plist);
01601 biny=xsh_pfits_get_biny(data_plist);
01602
01603 s_low_off=s_low_off_uvb;
01604 s_upp_off=s_upp_off_uvb;
01605 w_low_coeff1=w_low_coeff1_uvb;
01606 w_low_coeff2=w_low_coeff2_uvb;
01607
01608 w_upp_coeff1=w_upp_coeff1_uvb;
01609 w_upp_coeff2=w_upp_coeff2_uvb;
01610 save_size=save_size_uvb;
01611
01612
01613
01614 } else if(arm == XSH_ARM_VIS) {
01615
01616
01617
01618
01619 binx=xsh_pfits_get_binx(data_plist);
01620 biny=xsh_pfits_get_biny(data_plist);
01621
01622 s_low_off=s_low_off_vis;
01623 s_upp_off=s_upp_off_vis;
01624
01625 w_low_coeff1=w_low_coeff1_vis;
01626 w_low_coeff2=w_low_coeff2_vis;
01627
01628 w_upp_coeff1=w_upp_coeff1_vis;
01629 w_upp_coeff2=w_upp_coeff2_vis;
01630 save_size=save_size_vis;
01631
01632
01633 } else if(arm == XSH_ARM_NIR) {
01634
01635
01636
01637
01638
01639 s_low_off=s_low_off_nir;
01640 s_upp_off=s_upp_off_nir;
01641
01642
01643 w_low_coeff1=w_low_coeff1_nir;
01644 w_low_coeff2=w_low_coeff2_nir;
01645
01646 w_upp_coeff1=w_upp_coeff1_nir;
01647 w_upp_coeff2=w_upp_coeff2_nir;
01648 save_size=save_size_nir;
01649
01650 }
01651
01652
01653
01654 if(ifu_cfg_tab_frame) {
01655
01656 ifu_cfg_name=cpl_frame_get_filename(ifu_cfg_tab_frame);
01657 ifu_cfg_tab=cpl_table_load(ifu_cfg_name,1,0);
01658 s_upp_off=cpl_table_get_double(ifu_cfg_tab,"S_UPP_OFF",arm,&status);
01659 s_low_off=cpl_table_get_double(ifu_cfg_tab,"S_LOW_OFF",arm,&status);
01660
01661 w_upp_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF1",arm,&status);
01662 w_low_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF1",arm,&status);
01663 w_upp_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF2",arm,&status);
01664 w_low_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF2",arm,&status);
01665
01666 w_step_fct=cpl_table_get_int(ifu_cfg_tab,"W_STEP_FCT",arm,&status);
01667 xsh_msg("s_upp_off=%10.8g",s_upp_off);
01668 xsh_msg("s_low_off=%10.8g",s_low_off);
01669
01670
01671 xsh_msg("w_upp_coeff1=%10.8g",w_upp_coeff1);
01672 xsh_msg("w_upp_coeff2=%10.8g",w_upp_coeff2);
01673 xsh_msg("w_low_coeff1=%10.8g",w_low_coeff1);
01674 xsh_msg("w_low_coeff2=%10.8g",w_low_coeff2);
01675
01676 xsh_msg("w_step_fct=%d",w_step_fct);
01677
01678 }
01679
01680 if(ifu_cfg_cor_frame) {
01681 ifu_cfg_name=cpl_frame_get_filename(ifu_cfg_cor_frame);
01682 ifu_cfg_tab=cpl_table_load(ifu_cfg_name,1,0);
01683 s_upp_d0=cpl_table_get_double(ifu_cfg_tab,"DIFF_T12",0,&status);
01684 s_low_d0=cpl_table_get_double(ifu_cfg_tab,"DIFF_T32",0,&status);
01685 s_upp_d1=cpl_table_get_double(ifu_cfg_tab,"DIFF_T12",1,&status);
01686 s_low_d1=cpl_table_get_double(ifu_cfg_tab,"DIFF_T32",1,&status);
01687 s_upp_d2=cpl_table_get_double(ifu_cfg_tab,"DIFF_T12",2,&status);
01688 s_low_d2=cpl_table_get_double(ifu_cfg_tab,"DIFF_T32",2,&status);
01689 xsh_msg("upp_d0=%g upp_d1=%g upp_d2=%g low_d0=%g low_d1=%g low_d2=%g",
01690 s_upp_d0,s_upp_d1,s_upp_d2,s_low_d0,s_low_d1,s_low_d2);
01691
01692 }
01693 cube_wstep*=w_step_fct;
01694 if(model_config_frame!=NULL) {
01695 check( xsh_model_config_load_best( model_config_frame, &model_config));
01696 xsh_model_binxy(&model_config,binx,biny);
01697 } else {
01698 check( wavesol = xsh_wavesol_load( wavesol_frame, instrument));
01699 }
01700 check( rec_list = xsh_rec_list_create( instrument));
01701 check( profile = cpl_vector_new( CPL_KERNEL_DEF_SAMPLES));
01702 check(cpl_vector_fill_kernel_profile( profile,rectify_par->kernel_type,
01703 rectify_par->rectif_radius));
01704
01705 check(sci_name=cpl_frame_get_filename(div_frame));
01706 check(data_img=cpl_image_load(sci_name,XSH_PRE_DATA_TYPE,0,0));
01707 check(errs_img=cpl_image_load(sci_name,XSH_PRE_ERRS_TYPE,0,1));
01708 check(qual_img=cpl_image_load(sci_name,XSH_PRE_QUAL_TYPE,0,2));
01709
01710 check(sp_fmt_name=cpl_frame_get_filename(spectral_format_frame));
01711 check(sp_fmt_tab=cpl_table_load(sp_fmt_name,1,0));
01712 check(ord_min=cpl_table_get_column_min(sp_fmt_tab,"ORDER"));
01713 check(ord_max=cpl_table_get_column_max(sp_fmt_tab,"ORDER"));
01714 nord=ord_max-ord_min+1;
01715
01716 check(cube_wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",nord-1,&status));
01717 check(cube_wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",0,&status));
01718
01719 xsh_msg_debug("cube_wave_min=%10.8g cube_wave_max=%10.8g",cube_wave_min,cube_wave_max);
01720
01721
01722 naxis1=3;
01723 xsh_msg_debug("ord_min=%d ord_max=%d",ord_min,ord_max);
01724 wave_step= cube_wstep;
01725 s_step= cube_sstep;
01726 naxis2=(int)((s_max-s_min)/s_step+0.5)+1;
01727
01728
01729 xsh_free_imagelist(&data_cube_merge);
01730 xsh_free_imagelist(&errs_cube_merge);
01731 xsh_free_imagelist(&qual_cube_merge);
01732 xsh_free_imagelist(&mask_data_merge);
01733 xsh_free_imagelist(&mask_errs_merge);
01734 xsh_free_imagelist(&mask_qual_merge);
01735
01736 data_cube_merge=cpl_imagelist_new();
01737 errs_cube_merge=cpl_imagelist_new();
01738 qual_cube_merge=cpl_imagelist_new();
01739 mask_data_merge=cpl_imagelist_new();
01740 mask_errs_merge=cpl_imagelist_new();
01741 mask_qual_merge=cpl_imagelist_new();
01742
01743
01744 xsh_free_image(&data_tmp);
01745 xsh_free_image(&errs_tmp);
01746 xsh_free_image(&qual_tmp);
01747 xsh_free_image(&mask_tmp);
01748
01749 mk=0;
01750
01751 for( ord = ord_max; ord >= ord_min; ord-- ) {
01752
01753 xsh_free_imagelist(&data_cube);
01754 xsh_free_imagelist(&errs_cube);
01755 xsh_free_imagelist(&qual_cube);
01756
01757 data_cube=cpl_imagelist_new();
01758 errs_cube=cpl_imagelist_new();
01759 qual_cube=cpl_imagelist_new();
01760
01761
01762 data_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_DATA_TYPE);
01763 errs_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_ERRS_TYPE);
01764 qual_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
01765 mask_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
01766
01767 check(pima=cpl_image_get_data_float(data_tmp));
01768 check(perr=cpl_image_get_data_float(errs_tmp));
01769 check(pqua=cpl_image_get_data_int(qual_tmp));
01770 check(pmsk=cpl_image_get_data_int(mask_tmp));
01771
01772
01773
01774 check(wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",ord-ord_min,&status));
01775 check(wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",ord-ord_min,&status));
01776
01777 naxis3=(wave_max-wave_min)/wave_step+1;
01778
01779 xsh_msg_debug("order=%d naxis1=%d,naxis2=%d naxis3=%d, wave_min=%g wave_max=%g",
01780 ord,naxis1,naxis2,naxis3,wave_min,wave_max);
01781
01782
01783 if(ord<ord_max) {
01784 nstep_off=(int)((wave_min-wave_min_old)/wave_step+0.5);
01785 wave_min=wave_min_old+nstep_off*wave_step;
01786 }
01787
01788 ik=0;
01789 for( wave = wave_min; wave <= wave_max; wave+=wave_step) {
01790 is=0;
01791 for( s = s_min; s <= s_max; s+=s_step ) {
01792
01793
01794 s_upp=-s+s_upp_off+w_upp_coeff1*wave+w_upp_coeff2*wave*wave;
01795 s_upp-=(s_upp_d0+s_upp_d1*wave+s_upp_d2*wave*wave)*s_step;
01796 if(wavesol!=NULL) {
01797 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s_upp));
01798 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s_upp));
01799 } else {
01800
01801 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_upp,
01802 &x,&y));
01803 }
01804
01805
01806
01807 check(flux_upp=cpl_image_get_interpolated(data_img,x,y,
01808 profile,radius,
01809 profile,radius,
01810 &confidence));
01811 check(errs_upp=cpl_image_get_interpolated(errs_img,x,y,
01812 profile,radius,
01813 profile,radius,
01814 &confidence));
01815 check(qual_upp=cpl_image_get_interpolated(qual_img,x,y,
01816 profile,radius,
01817 profile,radius,
01818 &confidence));
01819
01820 pima[is*naxis1+0]=flux_upp;
01821 perr[is*naxis1+0]=errs_upp;
01822 pqua[is*naxis1+0]=qual_upp;
01823 pmsk[is*naxis1+0]=1;
01824
01825
01826 if(wavesol!=NULL) {
01827 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s));
01828 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s));
01829 } else {
01830 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s,
01831 &x,&y));
01832 }
01833 check(flux_cen=cpl_image_get_interpolated(data_img,x,y,
01834 profile,radius,
01835 profile,radius,
01836 &confidence));
01837
01838 check(errs_cen=cpl_image_get_interpolated(errs_img,x,y,
01839 profile,radius,
01840 profile,radius,
01841 &confidence));
01842
01843 check(qual_cen=cpl_image_get_interpolated(qual_img,x,y,
01844 profile,radius,
01845 profile,radius,
01846 &confidence));
01847
01848 pima[is*naxis1+1]=flux_cen;
01849 perr[is*naxis1+1]=errs_cen;
01850 pqua[is*naxis1+1]=qual_cen;
01851 pmsk[is*naxis1+1]=1;
01852
01853
01854 s_low=-s+s_low_off+w_low_coeff1*wave+w_low_coeff2*wave*wave;
01855 s_low-=(s_low_d0+s_low_d1*wave+s_low_d2*wave*wave)*s_step;
01856 if(wavesol!=NULL) {
01857 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s_low));
01858 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s_low));
01859 } else {
01860
01861 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_low,
01862 &x,&y));
01863 }
01864
01865 check(flux_low=cpl_image_get_interpolated(data_img,x,y,
01866 profile,radius,
01867 profile,radius,
01868 &confidence));
01869 check(errs_low=cpl_image_get_interpolated(errs_img,x,y,
01870 profile,radius,
01871 profile,radius,
01872 &confidence));
01873 check(qual_low=cpl_image_get_interpolated(qual_img,x,y,
01874 profile,radius,
01875 profile,radius,
01876 &confidence));
01877
01878 pima[is*naxis1+2]=flux_low;
01879 perr[is*naxis1+2]=errs_low;
01880 pqua[is*naxis1+2]=qual_low;
01881 pmsk[is*naxis1+2]=1;
01882
01883
01884 is++;
01885 }
01886
01887 wave_min_old=wave_min;
01888
01889 check(cpl_imagelist_set(data_cube,cpl_image_duplicate(data_tmp),ik));
01890 check(cpl_imagelist_set(errs_cube,cpl_image_duplicate(errs_tmp),ik));
01891 check(cpl_imagelist_set(qual_cube,cpl_image_duplicate(qual_tmp),ik));
01892
01893 check(xsh_iml_merge_avg(&data_cube_merge,&mask_data_merge,
01894 data_tmp,mask_tmp,mk));
01895 check(xsh_iml_merge_avg(&errs_cube_merge,&mask_errs_merge,
01896 errs_tmp,mask_tmp,mk));
01897 check(xsh_iml_merge_avg(&qual_cube_merge,&mask_qual_merge,
01898 qual_tmp,mask_tmp,mk));
01899
01900
01901 ik++;
01902 if(ord==ord_max) {
01903 mk++;
01904 } else {
01905 mk=(int)((wave-cube_wave_min)/wave_step+0.5);
01906 }
01907 }
01908
01909 sprintf(tag,"%s_ORDER3D_DATA_%s_%s",rec_prefix,qualifier,
01910 xsh_arm_tostring(arm));
01911 sprintf(name,"%s.fits",tag);
01912 if(frame_is_object==0) {
01913 xsh_add_temporary_file(name);
01914 }
01915 xsh_cube_set_wcs(data_plist,wave_min,
01916 2.,s_step,wave_step,
01917 1,naxis2/2.,0);
01918
01919 xsh_cube_set_wcs(errs_plist,wave_min,
01920 2.,s_step,wave_step,
01921 1,naxis2/2.,0);
01922
01923 xsh_cube_set_wcs(qual_plist,wave_min,
01924 2.,s_step,wave_step,
01925 1,naxis2/2.,0);
01926
01927 sprintf(data_extid,"ORD%2.2d_FLUX",ord);
01928 xsh_pfits_set_extname (data_plist, data_extid);
01929 sprintf(errs_extid,"ORD%2.2d_ERRS",ord);
01930 xsh_pfits_set_extname (errs_plist, errs_extid);
01931 sprintf(qual_extid,"ORD%2.2d_QUAL",ord);
01932 xsh_pfits_set_extname (qual_plist, qual_extid);
01933
01934 xsh_plist_set_extra_keys(data_plist,"IMAGE","DATA","RMSE",
01935 data_extid,errs_extid,qual_extid,0);
01936
01937 xsh_plist_set_extra_keys(errs_plist,"IMAGE","ERROR","RMSE",
01938 data_extid,errs_extid,qual_extid,1);
01939
01940 xsh_plist_set_extra_keys(qual_plist,"IMAGE","QUALITY","FLAG32BIT",
01941 data_extid,errs_extid,qual_extid,2);
01942
01943
01944
01945 if(ord==ord_max) {
01946 xsh_pfits_set_pcatg(data_plist,tag);
01947 frame_data_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01948 cpl_imagelist_save(data_cube,name,XSH_SPECTRUM_DATA_BPP,data_plist,CPL_IO_DEFAULT);
01949 cpl_imagelist_save(errs_cube,name,XSH_SPECTRUM_ERRS_BPP,errs_plist,CPL_IO_EXTEND);
01950 cpl_imagelist_save(qual_cube,name,XSH_SPECTRUM_QUAL_BPP,qual_plist,CPL_IO_EXTEND);
01951 } else {
01952 cpl_imagelist_save(data_cube,name,XSH_SPECTRUM_DATA_BPP,data_plist,CPL_IO_EXTEND);
01953 cpl_imagelist_save(errs_cube,name,XSH_SPECTRUM_ERRS_BPP,errs_plist,CPL_IO_EXTEND);
01954 cpl_imagelist_save(qual_cube,name,XSH_SPECTRUM_QUAL_BPP,qual_plist,CPL_IO_EXTEND);
01955 }
01956
01957
01958 sprintf(name,"QC%s_%2.2d.fits",tag,ord);
01959
01960 xsh_free_image(&data_tmp);
01961 xsh_free_image(&errs_tmp);
01962 xsh_free_image(&qual_tmp);
01963 xsh_free_image(&mask_tmp);
01964
01965 }
01966
01967 sprintf(tag,"%s_MERGE3D_DATA_%s_%s",rec_prefix,qualifier,
01968 xsh_arm_tostring(arm));
01969 sprintf(name,"%s.fits",tag);
01970 xsh_cube_set_wcs(data_plist,cube_wave_min,
01971 2.,s_step,wave_step,
01972 1,naxis2/2.,0);
01973
01974 xsh_cube_set_wcs(errs_plist,cube_wave_min,
01975 2.,s_step,wave_step,
01976 1,naxis2/2.,0);
01977
01978 xsh_cube_set_wcs(qual_plist,cube_wave_min,
01979 2.,s_step,wave_step,
01980 1,naxis2/2.,0);
01981
01982 sprintf(data_extid,"FLUX");
01983 xsh_pfits_set_extname (data_plist, data_extid);
01984 sprintf(errs_extid,"ERRS");
01985 xsh_pfits_set_extname (errs_plist, errs_extid);
01986 sprintf(qual_extid,"QUAL");
01987 xsh_pfits_set_extname (qual_plist, qual_extid);
01988
01989 xsh_plist_set_extra_keys(data_plist,"IMAGE","DATA","RMSE",
01990 data_extid,errs_extid,qual_extid,0);
01991 xsh_plist_set_extra_keys(errs_plist,"IMAGE","ERROR","RMSE",
01992 data_extid,errs_extid,qual_extid,1);
01993 xsh_plist_set_extra_keys(qual_plist,"IMAGE","QUALITY","FLAG32BIT",
01994 data_extid,errs_extid,qual_extid,2);
01995 xsh_pfits_set_pcatg(data_plist,tag);
01996
01997
01998
01999 cpl_imagelist_save(data_cube_merge,name,XSH_SPECTRUM_DATA_BPP,data_plist,CPL_IO_DEFAULT);
02000
02001 cpl_imagelist_save(errs_cube_merge,name,XSH_SPECTRUM_ERRS_BPP,errs_plist,CPL_IO_EXTEND);
02002
02003 cpl_imagelist_save(qual_cube_merge,name,XSH_SPECTRUM_QUAL_BPP,qual_plist,CPL_IO_EXTEND);
02004
02005 xsh_msg_debug("merge cube size=%" CPL_SIZE_FORMAT "",cpl_imagelist_get_size(data_cube_merge));
02006
02007 frame_merged_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
02008 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
02009
02010
02011 check(xsh_add_product_imagelist(frame_merged_cube,frameset,parameters,
02012 recipe_id,instrument,NULL));
02013
02014
02015 if(frame_is_object) {
02016
02017 sprintf(tag,"%s_MERGE3D_%s",rec_prefix,qualifier);
02018
02019 check(qc_trace_merged_frame=xsh_cube_qc_trace_window(frame_merged_cube,
02020 instrument,
02021 "MERGE3D",rec_prefix,
02022 save_size+1,
02023 naxis2-save_size,
02024 peack_search_hsize,
02025 method,1));
02026
02027 trace_corr_tab=xsh_crea_correct_coeff(qc_trace_merged_frame,instrument);
02028
02029 check( xsh_add_product_table( qc_trace_merged_frame, frameset,
02030 parameters, recipe_id, instrument,
02031 NULL));
02032
02033 sprintf(tag,"%s_ORDER3D_DATA_%s",rec_prefix,qualifier);
02034
02035 check(xsh_add_product_imagelist(frame_data_cube,frameset,parameters,
02036 recipe_id,instrument,tag));
02037
02038
02039
02040
02041
02042
02043
02044 }
02045
02046 if(trace_corr_tab) {
02047 xsh_add_product_table( trace_corr_tab, frameset,
02048 parameters, recipe_id, instrument,NULL);
02049 }
02050
02051 cleanup:
02052
02053 xsh_free_propertylist(&data_plist);
02054 xsh_free_propertylist(&errs_plist);
02055 xsh_free_propertylist(&qual_plist);
02056 xsh_free_vector(&profile);
02057
02058 xsh_free_image(&data_tmp);
02059 xsh_free_image(&errs_tmp);
02060 xsh_free_image(&qual_tmp);
02061 xsh_free_image(&mask_tmp);
02062
02063 xsh_free_image(&data_img);
02064 xsh_free_image(&errs_img);
02065 xsh_free_image(&qual_img);
02066
02067
02068 xsh_free_table(&sp_fmt_tab);
02069
02070 xsh_free_imagelist(&data_cube);
02071 xsh_free_imagelist(&errs_cube);
02072 xsh_free_imagelist(&qual_cube);
02073
02074
02075 xsh_free_imagelist(&data_cube_merge);
02076 xsh_free_imagelist(&errs_cube_merge);
02077 xsh_free_imagelist(&qual_cube_merge);
02078 xsh_free_imagelist(&mask_data_merge);
02079 xsh_free_imagelist(&mask_errs_merge);
02080 xsh_free_imagelist(&mask_qual_merge);
02081
02082 xsh_rec_list_free(&rec_list);
02083 xsh_free_frame(&qc_trace_frame);
02084 xsh_free_frame(&qc_trace_merged_frame);
02085 xsh_free_frame(&trace_corr_tab);
02086 xsh_free_frame(&frame_data_cube);
02087 xsh_free_frame(&frame_errs_cube);
02088 xsh_free_frame(&frame_qual_cube);
02089 xsh_free_frame(&frame_merged_cube);
02090
02091
02092
02093
02094
02095 return cpl_error_get_code();
02096
02097 }
02098
02099
02100
02107 cpl_matrix * xsh_atrous( cpl_vector *spec, int nscales){
02108
02109 cpl_matrix *decomp = NULL;
02110 cpl_vector *filter = NULL;
02111 double orig_filter[3] = {3./8., 1./4., 1./16.};
02112 double *new_filter = NULL;
02113 int filter_size = 3;
02114
02115 int spec_size;
02116 cpl_vector *sp = NULL;
02117 cpl_vector *smooth = NULL;
02118 int i,k;
02119 cpl_vector *tmp_filter = NULL;
02120
02121
02122 XSH_ASSURE_NOT_NULL( spec);
02123 XSH_ASSURE_NOT_ILLEGAL( nscales >= 1);
02124
02125
02126 check( spec_size = cpl_vector_get_size( spec));
02127 check( decomp = cpl_matrix_new( nscales+1, spec_size));
02128 sp = cpl_vector_duplicate( spec);
02129 smooth = cpl_vector_duplicate( spec);
02130 check( filter = cpl_vector_wrap( filter_size, orig_filter));
02131
02132
02133 for(k=0; k< nscales; k++){
02134 xsh_msg_dbg_medium("scale %d use filter with size %d", k, filter_size);
02135
02136 check( cpl_wlcalib_xc_convolve( smooth, filter));
02137
02138
02139 for(i=0; i<spec_size; i++){
02140 double sp_val, smooth_val;
02141
02142 check( sp_val = cpl_vector_get( sp, i));
02143 check( smooth_val = cpl_vector_get( smooth, i));
02144 check( cpl_matrix_set( decomp, nscales-k, i, sp_val-smooth_val));
02145 }
02146
02147
02148 xsh_free_vector( &sp);
02149 check( sp = cpl_vector_duplicate(smooth));
02150
02151
02152 check( tmp_filter = cpl_vector_duplicate( filter));
02153 XSH_FREE( new_filter);
02154 filter_size = cpl_vector_get_size( tmp_filter);
02155 XSH_CALLOC( new_filter, double , filter_size*2-1);
02156
02157 for(i=0; i<filter_size; i++){
02158 new_filter[2*i] = cpl_vector_get( tmp_filter, i);
02159 }
02160 xsh_free_vector( &tmp_filter);
02161 xsh_unwrap_vector( &filter);
02162 check( filter = cpl_vector_wrap( filter_size*2-1, new_filter));
02163 filter_size = filter_size*2-1;
02164 }
02165
02166
02167
02168 for(i=0; i<spec_size; i++){
02169 double val;
02170
02171 check( val = cpl_vector_get( sp, i));
02172 check( cpl_matrix_set( decomp, 0, i, val));
02173 }
02174
02175 cleanup:
02176 if ( cpl_error_get_code() != CPL_ERROR_NONE){
02177 xsh_free_matrix( &decomp);
02178 }
02179 xsh_unwrap_vector( &filter);
02180 xsh_free_vector( &sp);
02181 xsh_free_vector( &smooth);
02182 XSH_FREE( new_filter);
02183 return decomp;
02184 }
02185
02186
02187
02188
02195 cpl_frameset * xsh_shift_offsettab( cpl_frameset *shiftifu_frameset,
02196 double offset_low, double offset_up)
02197 {
02198 cpl_frameset *result = NULL;
02199 cpl_frame *lo_frame = NULL;
02200 const char *lo_name = NULL;
02201 cpl_table *lo_table = NULL;
02202 double *lo_data = NULL;
02203 cpl_frame *up_frame = NULL;
02204 const char *up_name = NULL;
02205 cpl_table *up_table = NULL;
02206 double *up_data = NULL;
02207 int i, size;
02208 cpl_propertylist *lo_header = NULL;
02209 cpl_propertylist *up_header = NULL;
02210 cpl_frame *prod_frame = NULL;
02211
02212 lo_frame = cpl_frameset_get_frame(shiftifu_frameset,0);
02213 lo_name = cpl_frame_get_filename(lo_frame);
02214 xsh_msg("Name %s", lo_name);
02215 XSH_TABLE_LOAD( lo_table, lo_name);
02216 check( lo_data = cpl_table_get_data_double( lo_table,
02217 XSH_SHIFTIFU_COLNAME_SHIFTSLIT));
02218
02219 up_frame = cpl_frameset_get_frame(shiftifu_frameset,2);
02220 up_name = cpl_frame_get_filename(up_frame);
02221 xsh_msg("Name %s", up_name);
02222 XSH_TABLE_LOAD( up_table, up_name);
02223 check( up_data = cpl_table_get_data_double( up_table,
02224 XSH_SHIFTIFU_COLNAME_SHIFTSLIT));
02225
02226 size = cpl_table_get_nrow( lo_table);
02227
02228 for( i=0; i<size; i++){
02229 lo_data[i] += offset_low;
02230 up_data[i] += offset_up;
02231 }
02232
02233 lo_header = cpl_propertylist_load( lo_name,0);
02234 check( cpl_table_save( lo_table, lo_header, NULL, "tmp_OFFSET_TAB_LOW.fits", CPL_IO_DEFAULT));
02235 up_header = cpl_propertylist_load( up_name,0);
02236 check( cpl_table_save( up_table, up_header, NULL, "tmp_OFFSET_TAB_UP.fits", CPL_IO_DEFAULT));
02237
02238 result = cpl_frameset_new();
02239 check(prod_frame = xsh_frame_product( "tmp_OFFSET_TAB_LOW.fits",
02240 "OFFSET_TAB",
02241 CPL_FRAME_TYPE_TABLE,
02242 CPL_FRAME_GROUP_PRODUCT,
02243 CPL_FRAME_LEVEL_TEMPORARY));
02244 cpl_frameset_insert( result, prod_frame);
02245
02246 check(prod_frame = cpl_frame_duplicate( cpl_frameset_get_frame(shiftifu_frameset,1)));
02247 cpl_frameset_insert( result, prod_frame);
02248
02249 check(prod_frame = xsh_frame_product( "tmp_OFFSET_TAB_UP.fits",
02250 "OFFSET_TAB",
02251 CPL_FRAME_TYPE_TABLE,
02252 CPL_FRAME_GROUP_PRODUCT,
02253 CPL_FRAME_LEVEL_TEMPORARY));
02254 cpl_frameset_insert( result, prod_frame);
02255
02256 cleanup:
02257 if ( cpl_error_get_code() != CPL_ERROR_NONE){
02258 xsh_free_frameset( &result);
02259 }
02260 XSH_TABLE_FREE( lo_table);
02261 XSH_TABLE_FREE( up_table);
02262 xsh_free_propertylist( &lo_header);
02263 xsh_free_propertylist( &up_header);
02264 return result;
02265 }
02266