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 int sx=0;
00711 int sy=0;
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 double range1_w_min=0;
00747 double range1_w_max=0;
00748 double range2_w_min=0;
00749 double range2_w_max=0;
00750
00751 double range_ks_w_min=0;
00752 double range_ks_w_max=0;
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[80];
00761 cpl_size px=0;
00762 cpl_size py=0;
00763
00764 int rad_x=10;
00765
00766
00767
00768
00769 int binx=1;
00770 int biny=1;
00771
00772 typedef enum {centroid, gaussian} xsh_fit_method;
00773 xsh_fit_method fit_method = centroid;
00774
00775
00776 check(ifu_object_ff_ima=cpl_image_load(ifu_object_ff_name,CPL_TYPE_DOUBLE,0,0));
00777 plist=cpl_propertylist_load(ifu_object_ff_name,0);
00778 pcatg=xsh_pfits_get_pcatg(plist);
00779
00780 xsh_msg("pcatg=%s",pcatg);
00781
00782 if(strstr(pcatg,"UVB") != NULL) {
00783 arm=XSH_ARM_UVB;
00784 range1_w_min=300;
00785 range1_w_max=500;
00786 range_ks_w_min=380;
00787 range_ks_w_max=550;
00788 binx=xsh_pfits_get_binx(plist);
00789 biny=xsh_pfits_get_biny(plist);
00790
00791
00792 } else if(strstr(pcatg,"VIS") != NULL) {
00793 arm=XSH_ARM_VIS;
00794 range1_w_min=600;
00795 range1_w_max=950;
00796
00797 range_ks_w_min=600;
00798 range_ks_w_max=900;
00799 binx=xsh_pfits_get_binx(plist);
00800 biny=xsh_pfits_get_biny(plist);
00801
00802 } else if(strstr(pcatg,"NIR") != NULL) {
00803 arm=XSH_ARM_NIR;
00804 range1_w_min=1050;
00805 range1_w_max=1250;
00806 range2_w_min=2000;
00807 range2_w_max=2200;
00808
00809 range_ks_w_min=1100;
00810 range_ks_w_max=2200;
00811
00812
00813 }
00814
00815 check(slit_map_ima=cpl_image_load(slit_map_name,CPL_TYPE_DOUBLE,0,0));
00816 check(wave_map_ima=cpl_image_load(wave_map_name,CPL_TYPE_DOUBLE,0,0));
00817 sx=cpl_image_get_size_x(ifu_object_ff_ima);
00818 sy=cpl_image_get_size_y(ifu_object_ff_ima);
00819
00820 check(tab=xsh_table_edge_prepare(order_tab_edges_ifu_name));
00821 nrow=cpl_table_get_nrow(tab);
00822
00823 p_edge_lo_x=cpl_table_get_data_double(tab,"EDG_LO_X");
00824 p_slice_lo_x=cpl_table_get_data_double(tab,"SLIC_LO_X");
00825 p_obj_low=cpl_table_get_data_double(tab,"OBJ_LOW_X");
00826 p_obj_low_s=cpl_table_get_data_double(tab,"OBJ_LOW_S");
00827 p_obj_low_w=cpl_table_get_data_double(tab,"OBJ_LOW_W");
00828
00829 p_edge_up_x=cpl_table_get_data_double(tab,"EDG_UP_X");
00830 p_slice_up_x=cpl_table_get_data_double(tab,"SLIC_UP_X");
00831 p_obj_upp=cpl_table_get_data_double(tab,"OBJ_UPP_X");
00832 p_obj_upp_s=cpl_table_get_data_double(tab,"OBJ_UPP_S");
00833 p_obj_upp_w=cpl_table_get_data_double(tab,"OBJ_UPP_W");
00834
00835 p_center_y=cpl_table_get_data_double(tab,"CENTER_Y");
00836 p_obj_cen=cpl_table_get_data_double(tab,"OBJ_CEN_X");
00837 p_obj_cen_s=cpl_table_get_data_double(tab,"OBJ_CEN_S");
00838 p_obj_cen_w=cpl_table_get_data_double(tab,"OBJ_CEN_W");
00839
00840 p_slit=cpl_image_get_data_double(slit_map_ima);
00841 p_wave=cpl_image_get_data_double(wave_map_ima);
00842 nx=cpl_image_get_size_x(wave_map_ima);
00843 ny=cpl_image_get_size_y(wave_map_ima);
00844
00845
00846 for(row=0;row<nrow;row++) {
00847
00848 lly=(int)p_center_y[row]/biny;
00849 ury=(int)p_center_y[row]/biny;
00850
00851
00852 if((llx<nx) && (ury<ny) ) {
00853
00854
00855 llx=floor(p_slice_lo_x[row]);
00856 urx=ceil(p_slice_up_x[row]);
00857
00858 check(cpl_image_get_maxpos_window(ifu_object_ff_ima,
00859 llx,lly,urx,ury,&px,&py));
00860
00861 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00862 if(fit_method==centroid) {
00863 x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00864 llx,lly,urx,ury);
00865
00866 } else {
00867 check(x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00868 llx,urx,lly));
00869 }
00870
00871
00872 p_obj_cen[row]=x_centroid;
00873
00874 if(urx<nx) {
00875 check(xsh_convert_xy_to_ws(x_centroid,p_obj_cen,p_slit,p_wave,
00876 lly,nx,row,p_obj_cen_s,p_obj_cen_w));
00877 }
00878
00879
00880 llx=floor(p_edge_lo_x[row]);
00881 urx=ceil(p_slice_lo_x[row]);
00882 cpl_image_get_maxpos_window(ifu_object_ff_ima,
00883 llx,lly,urx,ury,&px,&py);
00884
00885 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00886 if(fit_method==centroid) {
00887 check(x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00888 llx,lly,urx,ury));
00889 } else {
00890 x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00891 llx,urx,lly);
00892 }
00893
00894
00895
00896 if(urx<nx) {
00897 check(xsh_convert_xy_to_ws(x_centroid,p_obj_low,p_slit,p_wave,
00898 lly,nx,row,p_obj_low_s,p_obj_low_w));
00899 }
00900
00901
00902 llx=floor(p_slice_up_x[row]);
00903 urx=ceil(p_edge_up_x[row]);
00904 cpl_image_get_maxpos_window(ifu_object_ff_ima,
00905 llx,lly,urx,ury,&px,&py);
00906
00907 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00908 if(fit_method==centroid) {
00909 x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00910 llx,lly,urx,ury);
00911 } else {
00912 x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00913 llx,urx,lly);
00914 }
00915
00916 p_obj_upp[row]=x_centroid;
00917
00918 if(urx<nx) {
00919 check(xsh_convert_xy_to_ws(x_centroid,p_obj_upp,p_slit,p_wave,
00920 lly,nx,row,p_obj_upp_s,p_obj_upp_w));
00921 }
00922 }
00923 }
00924
00925 if(arm==XSH_ARM_VIS) {
00926 xsh_table_edges_swap_low_upp(&tab);
00927 }
00928 cpl_table_duplicate_column(tab,"OBJ_CEN_PLUS_UPP_S",tab,"OBJ_CEN_S");
00929
00930 cpl_table_duplicate_column(tab,"OBJ_CEN_PLUS_LOW_S",tab,"OBJ_CEN_S");
00931
00932 cpl_table_add_columns(tab,"OBJ_CEN_PLUS_UPP_S","OBJ_UPP_S");
00933 cpl_table_add_columns(tab,"OBJ_CEN_PLUS_LOW_S","OBJ_LOW_S");
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957 sprintf(tag,"TRACE_OBJ_%s",xsh_arm_tostring(arm));
00958 sprintf(name,"%s.fits",tag);
00959 xsh_pfits_set_pcatg(plist,tag);
00960 check(cpl_table_save(tab,plist,NULL,name,CPL_IO_DEFAULT));
00961
00962
00963
00964 cleanup:
00965 xsh_free_image(&ifu_object_ff_ima);
00966 xsh_free_image(&slit_map_ima);
00967 xsh_free_image(&wave_map_ima);
00968 xsh_free_image(&ratio_ima);
00969 xsh_free_propertylist(&plist);
00970 return cpl_error_get_code();
00971
00972 }
00973
00974
00975
00983
00984
00985 cpl_error_code
00986 xsh_frame_check_model_cfg_is_proper_for_sci(cpl_frame* model_config_frame,
00987 cpl_frame* sci_frame,
00988 xsh_instrument* instrument)
00989 {
00990
00991 cpl_propertylist* afc_plist=NULL;
00992 cpl_propertylist* sci_plist=NULL;
00993 const char* name=NULL;
00994 const char* sci_obs_targ_name=NULL;
00995 const char* afc_obs_targ_name=NULL;
00996 const char* afc_slit_value=NULL;
00997 const char* sci_slit_value=NULL;
00998 int sci_obs_id=0;
00999 int afc_obs_id=0;
01000
01001 check(name=cpl_frame_get_filename(model_config_frame));
01002 check(afc_plist=cpl_propertylist_load(name,0));
01003
01004 check(name=cpl_frame_get_filename(sci_frame));
01005 check(sci_plist=cpl_propertylist_load(name,0));
01006
01007 check(afc_slit_value=xsh_pfits_get_slit_value(afc_plist,instrument ));
01008
01009 check(sci_slit_value=xsh_pfits_get_slit_value(sci_plist,instrument ));
01010
01011 if(strstr(afc_slit_value,"Pin_0.5") == NULL) {
01012 xsh_msg_error("You have used uncorrect AFC corrected model cfg frame");
01013 xsh_msg_error("IFU AFC corrected model CFG must have INS.OPTIi.NAME='Pin_0.5'");
01014 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01015 }
01016
01017
01018 check(sci_obs_targ_name=xsh_pfits_get_obs_targ_name(sci_plist));
01019 check(afc_obs_targ_name=xsh_pfits_get_obs_targ_name(afc_plist));
01020 if(strcmp(sci_obs_targ_name,afc_obs_targ_name) != 0) {
01021 xsh_msg_error("Improper AFC corrected model cfg frame to reduce sci frame");
01022 xsh_msg_error("Their OBS.TARG.NAME values must match");
01023 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01024
01025 }
01026
01027 check(sci_obs_id=xsh_pfits_get_obs_id(sci_plist));
01028 check(afc_obs_id=xsh_pfits_get_obs_id(afc_plist));
01029
01030 if(sci_obs_id != afc_obs_id) {
01031 xsh_msg_error("Improper AFC corrected model cfg frame to reduce sci frame");
01032 xsh_msg_error("Their OBS.ID values must match");
01033 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01034 }
01035
01036 cleanup:
01037
01038 xsh_free_propertylist(&sci_plist);
01039 xsh_free_propertylist(&afc_plist);
01040
01041 return cpl_error_get_code();
01042 }
01043
01044
01045
01052
01053
01054 cpl_error_code
01055 xsh_frame_check_model_cfg_is_afc_corrected(cpl_frame* model_config_frame){
01056
01057 cpl_propertylist* plist=NULL;
01058 const char* name=NULL;
01059 const char* raw1_catg=NULL;
01060
01061 check(name=cpl_frame_get_filename(model_config_frame));
01062 check(plist=cpl_propertylist_load(name,0));
01063 check(raw1_catg=xsh_pfits_get_raw1catg(plist));
01064 if(strstr(raw1_catg,"AFC_ATT") == NULL) {
01065 xsh_msg_error("model cfg frame seems not to be AFC corrected");
01066 xsh_msg_error("Their PRO.REC1.RAW1..NAME values must contain AFC_ATT");
01067 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01068 }
01069
01070 cleanup:
01071
01072 xsh_free_propertylist(&plist);
01073
01074 return cpl_error_get_code();
01075
01076 }
01077
01078 static cpl_error_code
01079 xsh_add_correct_coeff(cpl_table** table, cpl_propertylist* plist,
01080 const char* prefix, const int row,
01081 const char* col12, const char* col32)
01082 {
01083 double coeff_t1=0;
01084 double coeff_t2=0;
01085 double coeff_t3=0;
01086 double diff_c12=0;
01087 double diff_c32=0;
01088 char key_name[40];
01089
01090
01091 sprintf(key_name,"%s_%s",prefix,"T1");
01092 check(coeff_t1=cpl_propertylist_get_double(plist,key_name));
01093
01094 sprintf(key_name,"%s_%s",prefix,"T2");
01095 coeff_t2=cpl_propertylist_get_double(plist,key_name);
01096
01097 sprintf(key_name,"%s_%s",prefix,"T3");
01098 coeff_t3=cpl_propertylist_get_double(plist,key_name);
01099 diff_c12=coeff_t1-coeff_t2;
01100 diff_c32=coeff_t3-coeff_t2;
01101
01102 cpl_table_set_double(*table,col12 ,row, diff_c12);
01103 cpl_table_set_double(*table,col32 ,row, diff_c32);
01104
01105 cleanup:
01106
01107 return cpl_error_get_code();
01108
01109 }
01110
01111 static cpl_frame*
01112 xsh_crea_correct_coeff(cpl_frame* qc_trace_merged_frame,xsh_instrument* inst)
01113 {
01114
01115 cpl_propertylist* plist=NULL;
01116 const char* fname=NULL;
01117 cpl_table* table=NULL;
01118 cpl_frame* result=NULL;
01119 char pname[80];
01120 char ptag[40];
01121
01122 fname=cpl_frame_get_filename(qc_trace_merged_frame);
01123 plist=cpl_propertylist_load(fname,0);
01124
01125 table=cpl_table_new(3);
01126 check(cpl_table_new_column(table,"DIFF_T12", CPL_TYPE_DOUBLE));
01127 check(cpl_table_new_column(table,"DIFF_T32", CPL_TYPE_DOUBLE));
01128
01129
01130 check(xsh_add_correct_coeff(&table,plist,XSH_QC_TRACE_FIT_C0,0,
01131 "DIFF_T12","DIFF_T32"));
01132
01133 check(xsh_add_correct_coeff(&table,plist,XSH_QC_TRACE_FIT_C1,1,
01134 "DIFF_T12","DIFF_T32"));
01135
01136 check(xsh_add_correct_coeff(&table,plist,XSH_QC_TRACE_FIT_C2,2,
01137 "DIFF_T12","DIFF_T32"));
01138
01139 sprintf(ptag,"IFU_CFG_COR_%s",xsh_instrument_arm_tostring(inst));
01140 sprintf(pname,"%s.fits",ptag);
01141 xsh_msg("tag=%s name=%s",ptag,pname);
01142
01143 check(cpl_table_save(table,plist,NULL,pname,CPL_IO_DEFAULT));
01144 result=xsh_frame_product(pname,ptag,CPL_FRAME_TYPE_TABLE,
01145 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01146
01147
01148 cleanup:
01149
01150 xsh_free_propertylist(&plist);
01151 xsh_free_table(&table);
01152
01153 return result;
01154 }
01155
01156 static cpl_frame*
01157 xsh_frame_build_sky_area(cpl_frame* slitmap_frame, const char* tag) {
01158
01159 char name_o[80];
01160 cpl_frame* result = NULL;
01161
01162 cpl_image* ima_slit = NULL;
01163 cpl_image* ima_sky = NULL;
01164 float* pslit = NULL;
01165 float* psky = NULL;
01166
01167 const char* name = NULL;
01168 cpl_propertylist* plist = NULL;
01169 int sx = 0;
01170 int sy = 0;
01171 int i = 0;
01172 int j = 0;
01173
01174 check(name = cpl_frame_get_filename(slitmap_frame));
01175 cpl_frame_dump(slitmap_frame, stdout);
01176 check(ima_slit = cpl_image_load(name, CPL_TYPE_FLOAT, 0, 0));
01177 check(plist = cpl_propertylist_load(name, 0));
01178 pslit = cpl_image_get_data_float(ima_slit);
01179
01180 sx = cpl_image_get_size_x(ima_slit);
01181 sy = cpl_image_get_size_y(ima_slit);
01182 ima_sky = cpl_image_new(sx, sy, CPL_TYPE_FLOAT);
01183 psky = cpl_image_get_data_float(ima_sky);
01184 for (j = 1; j < sy - 1; j++) {
01185 for (i = 1; i < sx - 1; i++) {
01186
01187 psky[j * sx + i] = 0.25 * (pslit[j * sx + i + 1] - pslit[j * sx + i - 1])
01188 * (pslit[(j + 1) * sx + i] - pslit[(j - 1) * sx + i]);
01189
01190 }
01191 }
01192
01193 sprintf(name_o, "%s.fits", tag);
01194 check(cpl_image_save(ima_sky, name_o, XSH_PRE_DATA_BPP, plist, CPL_IO_DEFAULT));
01195
01196 result = cpl_frame_duplicate(slitmap_frame);
01197 cpl_frame_set_filename(result, name_o);
01198 cpl_frame_set_tag(result, tag);
01199
01200 cleanup: xsh_free_propertylist(&plist);
01201 xsh_free_image(&ima_slit);
01202 xsh_free_image(&ima_sky);
01203 return result;
01204
01205 }
01206
01207 static cpl_frame*
01208 xsh_frame_build_sky_map(cpl_frame* slitmap_frame,const double value,const char* tag)
01209 {
01210
01211 char name_o[80];
01212 cpl_frame* result=NULL;
01213
01214 cpl_image* ima = NULL;
01215 const char* name = NULL;
01216 cpl_propertylist* plist = NULL;
01217
01218 check(name = cpl_frame_get_filename(slitmap_frame));
01219 check(ima = cpl_image_load(name, XSH_PRE_DATA_TYPE, 0, 0));
01220 check(plist = cpl_propertylist_load(name, 0));
01221
01222 check(cpl_image_add_scalar(ima,value));
01223 sprintf(name_o,"%s.fits",tag);
01224 check(cpl_image_save(ima, name_o, XSH_PRE_DATA_BPP, plist, CPL_IO_DEFAULT));
01225
01226 result = cpl_frame_duplicate(slitmap_frame);
01227 cpl_frame_set_filename(result, name_o);
01228 cpl_frame_set_tag(result, tag);
01229 xsh_add_temporary_file(name_o);
01230
01231 cleanup:
01232 xsh_free_propertylist(&plist);
01233 xsh_free_image(&ima);
01234
01235 return result;
01236
01237 }
01238
01248
01249 cpl_frame*
01250 xsh_build_ifu_map(cpl_frame* div_frame,
01251 cpl_frame* wavemap_frame,
01252 cpl_frame* slitmap_frame,
01253 cpl_frame* model_config_frame,
01254 xsh_instrument* instrument)
01255 {
01256
01257 cpl_frame* map = NULL;
01258 cpl_frame* ra_map = NULL;
01259 cpl_frame* dec_map = NULL;
01260 cpl_frame* sky_area = NULL;
01261
01262 char name_o[80];
01263 char tag_o[80];
01264
01265 cpl_image* ima = NULL;
01266 const char* name = NULL;
01267 cpl_propertylist* plist = NULL;
01268 double RA=0;
01269 double DEC=0;
01270
01271 name = cpl_frame_get_filename(div_frame);
01272 plist = cpl_propertylist_load(name, 0);
01273
01274 sprintf(tag_o,"%s_%s",XSH_IFU_MAP_SKY,xsh_instrument_arm_tostring(instrument));
01275 sprintf(name_o,"%s.fits",tag_o);
01276
01277
01278
01279
01280
01281 xsh_frame_image_save2ext(div_frame,name_o,0,0);
01282 xsh_frame_image_save2ext(wavemap_frame,name_o,0,1);
01283 xsh_frame_image_save2ext(slitmap_frame,name_o,0,2);
01284 xsh_frame_image_save2ext(slitmap_frame,name_o,1,3);
01285
01286
01287
01288
01289
01290
01291 RA=xsh_pfits_get_tel_targ_alpha(plist);
01292 DEC=xsh_pfits_get_tel_targ_delta(plist);
01293
01294
01295 RA=xsh_hms2deg(RA);
01296
01297 DEC=xsh_sess2deg(DEC);
01298
01299
01300 RA*=3600;
01301 DEC*=3600;
01302
01303
01304 sprintf(tag_o,"%s_%s","IFU_MAP_SKY_RA",xsh_instrument_arm_tostring(instrument));
01305 check(ra_map=xsh_frame_build_sky_map(slitmap_frame,RA,tag_o));
01306 check(xsh_frame_image_save2ext(ra_map,name_o,0,4));
01307
01308
01309 sprintf(tag_o,"%s_%s","IFU_MAP_SKY_DEC",xsh_instrument_arm_tostring(instrument));
01310 check(dec_map=xsh_frame_build_sky_map(slitmap_frame,DEC,tag_o));
01311 check(xsh_frame_image_save2ext(dec_map,name_o,0,5));
01312
01313
01314 sprintf(tag_o,"%s_%s","IFU_MAP_SKY_AREA",xsh_instrument_arm_tostring(instrument));
01315 check(sky_area=xsh_frame_build_sky_area(slitmap_frame,tag_o));
01316 check(xsh_frame_image_save2ext(sky_area,name_o,0,6));
01317 xsh_add_temporary_file(name_o);
01318
01319
01320
01321
01322
01323 map = xsh_frame_product(name_o, tag_o, CPL_FRAME_TYPE_IMAGE,
01324 CPL_FRAME_GROUP_PRODUCT, CPL_FRAME_LEVEL_FINAL);
01325
01326 cleanup:
01327 xsh_free_propertylist(&plist);
01328 xsh_free_image(&ima);
01329
01330 return map;
01331
01332 }
01333
01334
01335
01336
01337
01355
01356 cpl_error_code
01357 xsh_build_ifu_cube(cpl_frame* div_frame,
01358 cpl_frame* ifu_cfg_tab_frame,
01359 cpl_frame* ifu_cfg_cor_frame,
01360 cpl_frame* spectral_format_frame,
01361 cpl_frame* model_config_frame,
01362 cpl_frame* wavesol_frame,
01363 xsh_instrument* instrument,
01364 cpl_frameset* frameset,
01365 cpl_parameterlist* parameters,
01366 xsh_rectify_param * rectify_par,
01367 const char* recipe_id,
01368 const char* rec_prefix,
01369 const int frame_is_object)
01370 {
01371
01372
01373
01374
01375 cpl_image* data_img=NULL;
01376 cpl_image* errs_img=NULL;
01377 cpl_image* qual_img=NULL;
01378
01379 cpl_image* data_tmp=NULL;
01380 cpl_image* errs_tmp=NULL;
01381 cpl_image* qual_tmp=NULL;
01382 cpl_image* mask_tmp=NULL;
01383 cpl_frame* qc_trace_frame=NULL;
01384 cpl_frame* qc_trace_merged_frame=NULL;
01385 cpl_frame* frame_data_cube=NULL;
01386 cpl_frame* frame_errs_cube=NULL;
01387 cpl_frame* frame_qual_cube=NULL;
01388 cpl_frame* frame_merged_cube=NULL;
01389
01390
01391
01392
01393
01394 XSH_ARM arm;
01395 xsh_xs_3 model_config ;
01396 xsh_rec_list * rec_list = NULL ;
01397
01398 cpl_table* ifu_cfg_tab=NULL;
01399 cpl_table* sp_fmt_tab=NULL;
01400 cpl_vector* profile=NULL;
01401 cpl_imagelist* data_cube=NULL;
01402 cpl_imagelist* errs_cube=NULL;
01403 cpl_imagelist* qual_cube=NULL;
01404
01405 cpl_imagelist* data_cube_merge=NULL;
01406 cpl_imagelist* errs_cube_merge=NULL;
01407 cpl_imagelist* qual_cube_merge=NULL;
01408 cpl_imagelist* mask_data_merge=NULL;
01409 cpl_imagelist* mask_errs_merge=NULL;
01410 cpl_imagelist* mask_qual_merge=NULL;
01411
01412
01413 float* pima=NULL;
01414 float* perr=NULL;
01415 int* pqua=NULL;
01416 int* pmsk=NULL;
01417 int nord=0;
01418 int save_size_uvb=4;
01419 int save_size_vis=2;
01420 int save_size_nir=2;
01421 int save_size=0;
01422 const int peack_search_hsize=5;
01423 int method=0;
01424
01425
01426
01427 cpl_propertylist* data_plist=NULL;
01428 cpl_propertylist* errs_plist=NULL;
01429 cpl_propertylist* qual_plist=NULL;
01430
01431
01432 const char* sci_name=NULL;
01433 const char* sp_fmt_name=NULL;
01434 const char* ifu_cfg_name=NULL;
01435
01436 char name[256];
01437 char data_extid[40];
01438 char errs_extid[40];
01439 char qual_extid[40];
01440
01441 char qualifier[10];
01442 int binx=1;
01443 int biny=1;
01444
01445 double flux_upp=0;
01446 double flux_cen=0;
01447 double flux_low=0;
01448
01449
01450 double errs_upp=0;
01451 double errs_cen=0;
01452 double errs_low=0;
01453 double cube_wave_min=0;
01454 double cube_wave_max=0;
01455
01456
01457 int qual_upp=0;
01458 int qual_cen=0;
01459 int qual_low=0;
01460
01461 double cube_wmin=550;
01462 double cube_wmax=1000;
01463 double cube_wstep=0;
01464 double cube_sstep=0;
01465
01466 int is=0;
01467
01468 int ord=0;
01469 int ord_min=0;
01470 int ord_max=0;
01471
01472 double wave=0;
01473 double wave_min=0;
01474 double wave_max=0;
01475 double wave_step=0;
01476
01477 double wave_min_old=0;
01478 double wave_max_old=0;
01479
01480 int nstep_off=0;
01481
01482 double s=0;
01483 double s_min=-2;
01484 double s_max=2;
01485 double s_step=0;
01486
01487 double x=0;
01488 double y=0;
01489 double s_upp=0;
01490 double s_low=0;
01491
01492
01493 int radius=rectify_par->rectif_radius;
01494
01495 double confidence=0;
01496 int naxis1=0;
01497 int naxis2=0;
01498 int naxis3=0;
01499 int ik=0;
01500 double wave_cen=0;
01501
01502
01503
01504
01505
01506
01507 double w_low_coeff1=0;
01508 double w_low_coeff2=0;
01509
01510 double w_upp_coeff1=0;
01511 double w_upp_coeff2=0;
01512
01513 double w_low_coeff1_uvb=-0.002972;
01514 double w_low_coeff2_uvb=2.26497e-6+0.2e-6;
01515 double w_upp_coeff1_uvb=8.3355331e-5;
01516 double w_upp_coeff2_uvb=-1.0682e-6;
01517
01518
01519
01520 double w_low_coeff1_vis=-0.0016549569;
01521 double w_low_coeff2_vis=1.183805e-6+0.04e-6;
01522 double w_upp_coeff1_vis=-0.0016610719;
01523 double w_upp_coeff2_vis=1.0823013e-6+0.02e-6;
01524
01525 double w_low_coeff1_nir=-0.00010;
01526 double w_low_coeff2_nir=-5.0e-9;
01527
01528 double w_upp_coeff1_nir=0;
01529 double w_upp_coeff2_nir=0;
01530
01531
01532 double s_upp_off_uvb=4.0514429+0.16;
01533 double s_upp_off_vis=5.2504895+0.16-0.703;
01534 double s_upp_off_nir=3.5452+0.31;
01535 double s_upp_off=0;
01536
01537 double s_low_off_uvb=-3.4636662+0.16;
01538
01539 double s_low_off_vis=-2.9428071+0.16-0.645;
01540 double s_low_off_nir=-4.451682+0.21+0.31;
01541
01542 double s_low_off=0;
01543
01544 int w_step_fct=10;
01545
01546 int status=0;
01547 int mk=0;
01548 char tag[256];
01549 xsh_wavesol *wavesol=NULL;
01550 cpl_frame* trace_corr_tab=NULL;
01551 double s_upp_d0=0;
01552 double s_low_d0=0;
01553 double s_upp_d1=0;
01554 double s_low_d1=0;
01555 double s_upp_d2=0;
01556 double s_low_d2=0;
01557
01558 if(frame_is_object) {
01559 sprintf(qualifier,"OBJ");
01560 } else {
01561 sprintf(qualifier,"SKY");
01562 }
01563
01564
01565
01566
01567
01568
01569
01570
01571
01572
01573
01574
01575
01576
01577
01578
01579
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589 check(sci_name=cpl_frame_get_filename(div_frame));
01590 check(data_plist=cpl_propertylist_load(sci_name,0));
01591 errs_plist=cpl_propertylist_duplicate(data_plist);
01592 qual_plist=cpl_propertylist_duplicate(data_plist);
01593
01594 arm=instrument->arm;
01595
01596
01597 cube_wstep=rectify_par->rectif_bin_lambda;
01598 cube_sstep=rectify_par->rectif_bin_space;
01599
01600 if(arm == XSH_ARM_UVB) {
01601 cube_wmin=300;
01602 cube_wmax=550;
01603
01604 binx=xsh_pfits_get_binx(data_plist);
01605 biny=xsh_pfits_get_biny(data_plist);
01606
01607 s_low_off=s_low_off_uvb;
01608 s_upp_off=s_upp_off_uvb;
01609 w_low_coeff1=w_low_coeff1_uvb;
01610 w_low_coeff2=w_low_coeff2_uvb;
01611
01612 w_upp_coeff1=w_upp_coeff1_uvb;
01613 w_upp_coeff2=w_upp_coeff2_uvb;
01614 save_size=save_size_uvb;
01615
01616
01617
01618 } else if(arm == XSH_ARM_VIS) {
01619 cube_wmin=550;
01620 cube_wmax=1000;
01621
01622
01623 binx=xsh_pfits_get_binx(data_plist);
01624 biny=xsh_pfits_get_biny(data_plist);
01625
01626 s_low_off=s_low_off_vis;
01627 s_upp_off=s_upp_off_vis;
01628
01629 w_low_coeff1=w_low_coeff1_vis;
01630 w_low_coeff2=w_low_coeff2_vis;
01631
01632 w_upp_coeff1=w_upp_coeff1_vis;
01633 w_upp_coeff2=w_upp_coeff2_vis;
01634 save_size=save_size_vis;
01635
01636
01637 } else if(arm == XSH_ARM_NIR) {
01638 cube_wmin=1000;
01639 cube_wmax=2500;
01640
01641
01642
01643 s_low_off=s_low_off_nir;
01644 s_upp_off=s_upp_off_nir;
01645
01646
01647 w_low_coeff1=w_low_coeff1_nir;
01648 w_low_coeff2=w_low_coeff2_nir;
01649
01650 w_upp_coeff1=w_upp_coeff1_nir;
01651 w_upp_coeff2=w_upp_coeff2_nir;
01652 save_size=save_size_nir;
01653
01654 }
01655
01656
01657
01658 if(ifu_cfg_tab_frame) {
01659
01660 ifu_cfg_name=cpl_frame_get_filename(ifu_cfg_tab_frame);
01661 ifu_cfg_tab=cpl_table_load(ifu_cfg_name,1,0);
01662 s_upp_off=cpl_table_get_double(ifu_cfg_tab,"S_UPP_OFF",arm,&status);
01663 s_low_off=cpl_table_get_double(ifu_cfg_tab,"S_LOW_OFF",arm,&status);
01664
01665 w_upp_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF1",arm,&status);
01666 w_low_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF1",arm,&status);
01667 w_upp_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF2",arm,&status);
01668 w_low_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF2",arm,&status);
01669
01670 w_step_fct=cpl_table_get_int(ifu_cfg_tab,"W_STEP_FCT",arm,&status);
01671 xsh_msg("s_upp_off=%10.8g",s_upp_off);
01672 xsh_msg("s_low_off=%10.8g",s_low_off);
01673
01674
01675 xsh_msg("w_upp_coeff1=%10.8g",w_upp_coeff1);
01676 xsh_msg("w_upp_coeff2=%10.8g",w_upp_coeff2);
01677 xsh_msg("w_low_coeff1=%10.8g",w_low_coeff1);
01678 xsh_msg("w_low_coeff2=%10.8g",w_low_coeff2);
01679
01680 xsh_msg("w_step_fct=%d",w_step_fct);
01681
01682 }
01683
01684 if(ifu_cfg_cor_frame) {
01685 ifu_cfg_name=cpl_frame_get_filename(ifu_cfg_cor_frame);
01686 ifu_cfg_tab=cpl_table_load(ifu_cfg_name,1,0);
01687 s_upp_d0=cpl_table_get_double(ifu_cfg_tab,"DIFF_T12",0,&status);
01688 s_low_d0=cpl_table_get_double(ifu_cfg_tab,"DIFF_T32",0,&status);
01689 s_upp_d1=cpl_table_get_double(ifu_cfg_tab,"DIFF_T12",1,&status);
01690 s_low_d1=cpl_table_get_double(ifu_cfg_tab,"DIFF_T32",1,&status);
01691 s_upp_d2=cpl_table_get_double(ifu_cfg_tab,"DIFF_T12",2,&status);
01692 s_low_d2=cpl_table_get_double(ifu_cfg_tab,"DIFF_T32",2,&status);
01693 xsh_msg("upp_d0=%g upp_d1=%g upp_d2=%g low_d0=%g low_d1=%g low_d2=%g",
01694 s_upp_d0,s_upp_d1,s_upp_d2,s_low_d0,s_low_d1,s_low_d2);
01695
01696 }
01697 cube_wstep*=w_step_fct;
01698 if(model_config_frame!=NULL) {
01699 check( xsh_model_config_load_best( model_config_frame, &model_config));
01700 xsh_model_binxy(&model_config,binx,biny);
01701 } else {
01702 check( wavesol = xsh_wavesol_load( wavesol_frame, instrument));
01703 }
01704 check( rec_list = xsh_rec_list_create( instrument));
01705 check( profile = cpl_vector_new( CPL_KERNEL_DEF_SAMPLES));
01706 check(cpl_vector_fill_kernel_profile( profile,rectify_par->kernel_type,
01707 rectify_par->rectif_radius));
01708
01709 check(sci_name=cpl_frame_get_filename(div_frame));
01710 check(data_img=cpl_image_load(sci_name,XSH_PRE_DATA_TYPE,0,0));
01711 check(errs_img=cpl_image_load(sci_name,XSH_PRE_ERRS_TYPE,0,1));
01712 check(qual_img=cpl_image_load(sci_name,XSH_PRE_QUAL_TYPE,0,2));
01713
01714 check(sp_fmt_name=cpl_frame_get_filename(spectral_format_frame));
01715 check(sp_fmt_tab=cpl_table_load(sp_fmt_name,1,0));
01716 check(ord_min=cpl_table_get_column_min(sp_fmt_tab,"ORDER"));
01717 check(ord_max=cpl_table_get_column_max(sp_fmt_tab,"ORDER"));
01718 nord=ord_max-ord_min+1;
01719
01720 check(cube_wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",nord-1,&status));
01721 check(cube_wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",0,&status));
01722
01723 xsh_msg_debug("cube_wave_min=%10.8g cube_wave_max=%10.8g",cube_wave_min,cube_wave_max);
01724
01725
01726 naxis1=3;
01727 xsh_msg_debug("ord_min=%d ord_max=%d",ord_min,ord_max);
01728 wave_step= cube_wstep;
01729 s_step= cube_sstep;
01730 naxis2=(int)((s_max-s_min)/s_step+0.5)+1;
01731
01732
01733 xsh_free_imagelist(&data_cube_merge);
01734 xsh_free_imagelist(&errs_cube_merge);
01735 xsh_free_imagelist(&qual_cube_merge);
01736 xsh_free_imagelist(&mask_data_merge);
01737 xsh_free_imagelist(&mask_errs_merge);
01738 xsh_free_imagelist(&mask_qual_merge);
01739
01740 data_cube_merge=cpl_imagelist_new();
01741 errs_cube_merge=cpl_imagelist_new();
01742 qual_cube_merge=cpl_imagelist_new();
01743 mask_data_merge=cpl_imagelist_new();
01744 mask_errs_merge=cpl_imagelist_new();
01745 mask_qual_merge=cpl_imagelist_new();
01746
01747
01748 xsh_free_image(&data_tmp);
01749 xsh_free_image(&errs_tmp);
01750 xsh_free_image(&qual_tmp);
01751 xsh_free_image(&mask_tmp);
01752
01753 mk=0;
01754
01755 for( ord = ord_max; ord >= ord_min; ord-- ) {
01756
01757 xsh_free_imagelist(&data_cube);
01758 xsh_free_imagelist(&errs_cube);
01759 xsh_free_imagelist(&qual_cube);
01760
01761 data_cube=cpl_imagelist_new();
01762 errs_cube=cpl_imagelist_new();
01763 qual_cube=cpl_imagelist_new();
01764
01765
01766 data_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_DATA_TYPE);
01767 errs_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_ERRS_TYPE);
01768 qual_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
01769 mask_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
01770
01771 check(pima=cpl_image_get_data_float(data_tmp));
01772 check(perr=cpl_image_get_data_float(errs_tmp));
01773 check(pqua=cpl_image_get_data_int(qual_tmp));
01774 check(pmsk=cpl_image_get_data_int(mask_tmp));
01775
01776
01777
01778 check(wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",ord-ord_min,&status));
01779 check(wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",ord-ord_min,&status));
01780
01781 naxis3=(wave_max-wave_min)/wave_step+1;
01782 wave_cen=wave_min+naxis3/2*wave_step;
01783 xsh_msg_debug("order=%d naxis1=%d,naxis2=%d naxis3=%d, wave_min=%g wave_max=%g",
01784 ord,naxis1,naxis2,naxis3,wave_min,wave_max);
01785
01786
01787 if(ord<ord_max) {
01788 nstep_off=(int)((wave_min-wave_min_old)/wave_step+0.5);
01789 wave_min=wave_min_old+nstep_off*wave_step;
01790 }
01791
01792 ik=0;
01793 for( wave = wave_min; wave <= wave_max; wave+=wave_step) {
01794 is=0;
01795 for( s = s_min; s <= s_max; s+=s_step ) {
01796
01797
01798 s_upp=-s+s_upp_off+w_upp_coeff1*wave+w_upp_coeff2*wave*wave;
01799 s_upp-=(s_upp_d0+s_upp_d1*wave+s_upp_d2*wave*wave)*s_step;
01800 if(wavesol!=NULL) {
01801 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s_upp));
01802 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s_upp));
01803 } else {
01804
01805 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_upp,
01806 &x,&y));
01807 }
01808
01809
01810
01811 check(flux_upp=cpl_image_get_interpolated(data_img,x,y,
01812 profile,radius,
01813 profile,radius,
01814 &confidence));
01815 check(errs_upp=cpl_image_get_interpolated(errs_img,x,y,
01816 profile,radius,
01817 profile,radius,
01818 &confidence));
01819 check(qual_upp=cpl_image_get_interpolated(qual_img,x,y,
01820 profile,radius,
01821 profile,radius,
01822 &confidence));
01823
01824 pima[is*naxis1+0]=flux_upp;
01825 perr[is*naxis1+0]=errs_upp;
01826 pqua[is*naxis1+0]=qual_upp;
01827 pmsk[is*naxis1+0]=1;
01828
01829
01830 if(wavesol!=NULL) {
01831 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s));
01832 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s));
01833 } else {
01834 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s,
01835 &x,&y));
01836 }
01837 check(flux_cen=cpl_image_get_interpolated(data_img,x,y,
01838 profile,radius,
01839 profile,radius,
01840 &confidence));
01841
01842 check(errs_cen=cpl_image_get_interpolated(errs_img,x,y,
01843 profile,radius,
01844 profile,radius,
01845 &confidence));
01846
01847 check(qual_cen=cpl_image_get_interpolated(qual_img,x,y,
01848 profile,radius,
01849 profile,radius,
01850 &confidence));
01851
01852 pima[is*naxis1+1]=flux_cen;
01853 perr[is*naxis1+1]=errs_cen;
01854 pqua[is*naxis1+1]=qual_cen;
01855 pmsk[is*naxis1+1]=1;
01856
01857
01858 s_low=-s+s_low_off+w_low_coeff1*wave+w_low_coeff2*wave*wave;
01859 s_low-=(s_low_d0+s_low_d1*wave+s_low_d2*wave*wave)*s_step;
01860 if(wavesol!=NULL) {
01861 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s_low));
01862 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s_low));
01863 } else {
01864
01865 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_low,
01866 &x,&y));
01867 }
01868
01869 check(flux_low=cpl_image_get_interpolated(data_img,x,y,
01870 profile,radius,
01871 profile,radius,
01872 &confidence));
01873 check(errs_low=cpl_image_get_interpolated(errs_img,x,y,
01874 profile,radius,
01875 profile,radius,
01876 &confidence));
01877 check(qual_low=cpl_image_get_interpolated(qual_img,x,y,
01878 profile,radius,
01879 profile,radius,
01880 &confidence));
01881
01882 pima[is*naxis1+2]=flux_low;
01883 perr[is*naxis1+2]=errs_low;
01884 pqua[is*naxis1+2]=qual_low;
01885 pmsk[is*naxis1+2]=1;
01886
01887
01888 is++;
01889 }
01890 wave_max_old=wave_max;
01891 wave_min_old=wave_min;
01892
01893 check(cpl_imagelist_set(data_cube,cpl_image_duplicate(data_tmp),ik));
01894 check(cpl_imagelist_set(errs_cube,cpl_image_duplicate(errs_tmp),ik));
01895 check(cpl_imagelist_set(qual_cube,cpl_image_duplicate(qual_tmp),ik));
01896
01897 check(xsh_iml_merge_avg(&data_cube_merge,&mask_data_merge,
01898 data_tmp,mask_tmp,mk));
01899 check(xsh_iml_merge_avg(&errs_cube_merge,&mask_errs_merge,
01900 errs_tmp,mask_tmp,mk));
01901 check(xsh_iml_merge_avg(&qual_cube_merge,&mask_qual_merge,
01902 qual_tmp,mask_tmp,mk));
01903
01904
01905 ik++;
01906 if(ord==ord_max) {
01907 mk++;
01908 } else {
01909 mk=(int)((wave-cube_wave_min)/wave_step+0.5);
01910 }
01911 }
01912
01913 sprintf(tag,"%s_ORDER3D_DATA_%s_%s",rec_prefix,qualifier,
01914 xsh_arm_tostring(arm));
01915 sprintf(name,"%s.fits",tag);
01916
01917 xsh_cube_set_wcs(data_plist,wave_min,
01918 2.,s_step,wave_step,
01919 1,naxis2/2.,0);
01920
01921 xsh_cube_set_wcs(errs_plist,wave_min,
01922 2.,s_step,wave_step,
01923 1,naxis2/2.,0);
01924
01925 xsh_cube_set_wcs(qual_plist,wave_min,
01926 2.,s_step,wave_step,
01927 1,naxis2/2.,0);
01928
01929 sprintf(data_extid,"ORD%2.2d_FLUX",ord);
01930 xsh_pfits_set_extname (data_plist, data_extid);
01931 sprintf(errs_extid,"ORD%2.2d_ERRS",ord);
01932 xsh_pfits_set_extname (errs_plist, errs_extid);
01933 sprintf(qual_extid,"ORD%2.2d_QUAL",ord);
01934 xsh_pfits_set_extname (qual_plist, qual_extid);
01935
01936 xsh_plist_set_extra_keys(data_plist,"IMAGE","DATA","RMSE",
01937 data_extid,errs_extid,qual_extid,0);
01938
01939 xsh_plist_set_extra_keys(errs_plist,"IMAGE","ERROR","RMSE",
01940 data_extid,errs_extid,qual_extid,1);
01941
01942 xsh_plist_set_extra_keys(qual_plist,"IMAGE","QUALITY","FLAG32BIT",
01943 data_extid,errs_extid,qual_extid,2);
01944
01945
01946
01947 if(ord==ord_max) {
01948 xsh_pfits_set_pcatg(data_plist,tag);
01949 frame_data_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01950 cpl_imagelist_save(data_cube,name,XSH_SPECTRUM_DATA_BPP,data_plist,CPL_IO_DEFAULT);
01951 cpl_imagelist_save(errs_cube,name,XSH_SPECTRUM_ERRS_BPP,errs_plist,CPL_IO_EXTEND);
01952 cpl_imagelist_save(qual_cube,name,XSH_SPECTRUM_QUAL_BPP,qual_plist,CPL_IO_EXTEND);
01953 } else {
01954 cpl_imagelist_save(data_cube,name,XSH_SPECTRUM_DATA_BPP,data_plist,CPL_IO_EXTEND);
01955 cpl_imagelist_save(errs_cube,name,XSH_SPECTRUM_ERRS_BPP,errs_plist,CPL_IO_EXTEND);
01956 cpl_imagelist_save(qual_cube,name,XSH_SPECTRUM_QUAL_BPP,qual_plist,CPL_IO_EXTEND);
01957 }
01958
01959
01960 sprintf(name,"QC%s_%2.2d.fits",tag,ord);
01961
01962 xsh_free_image(&data_tmp);
01963 xsh_free_image(&errs_tmp);
01964 xsh_free_image(&qual_tmp);
01965 xsh_free_image(&mask_tmp);
01966
01967 }
01968
01969 sprintf(tag,"%s_MERGE3D_DATA_%s_%s",rec_prefix,qualifier,
01970 xsh_arm_tostring(arm));
01971 sprintf(name,"%s.fits",tag);
01972 xsh_cube_set_wcs(data_plist,cube_wave_min,
01973 2.,s_step,wave_step,
01974 1,naxis2/2.,0);
01975
01976 xsh_cube_set_wcs(errs_plist,cube_wave_min,
01977 2.,s_step,wave_step,
01978 1,naxis2/2.,0);
01979
01980 xsh_cube_set_wcs(qual_plist,cube_wave_min,
01981 2.,s_step,wave_step,
01982 1,naxis2/2.,0);
01983
01984 sprintf(data_extid,"FLUX");
01985 xsh_pfits_set_extname (data_plist, data_extid);
01986 sprintf(errs_extid,"ERRS");
01987 xsh_pfits_set_extname (errs_plist, errs_extid);
01988 sprintf(qual_extid,"QUAL");
01989 xsh_pfits_set_extname (qual_plist, qual_extid);
01990
01991 xsh_plist_set_extra_keys(data_plist,"IMAGE","DATA","RMSE",
01992 data_extid,errs_extid,qual_extid,0);
01993 xsh_plist_set_extra_keys(errs_plist,"IMAGE","ERROR","RMSE",
01994 data_extid,errs_extid,qual_extid,1);
01995 xsh_plist_set_extra_keys(qual_plist,"IMAGE","QUALITY","FLAG32BIT",
01996 data_extid,errs_extid,qual_extid,2);
01997 xsh_pfits_set_pcatg(data_plist,tag);
01998
01999
02000
02001 cpl_imagelist_save(data_cube_merge,name,XSH_SPECTRUM_DATA_BPP,data_plist,CPL_IO_DEFAULT);
02002
02003 cpl_imagelist_save(errs_cube_merge,name,XSH_SPECTRUM_ERRS_BPP,errs_plist,CPL_IO_EXTEND);
02004
02005 cpl_imagelist_save(qual_cube_merge,name,XSH_SPECTRUM_QUAL_BPP,qual_plist,CPL_IO_EXTEND);
02006
02007 xsh_msg_debug("merge cube size=%" CPL_SIZE_FORMAT "",cpl_imagelist_get_size(data_cube_merge));
02008
02009 frame_merged_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
02010 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
02011
02012
02013 check(xsh_add_product_imagelist(frame_merged_cube,frameset,parameters,
02014 recipe_id,instrument,NULL));
02015
02016
02017 if(frame_is_object) {
02018
02019 sprintf(tag,"%s_MERGE3D_%s",rec_prefix,qualifier);
02020
02021 check(qc_trace_merged_frame=xsh_cube_qc_trace_window(frame_merged_cube,
02022 instrument,
02023 "MERGE3D",rec_prefix,
02024 save_size+1,
02025 naxis2-save_size,
02026 peack_search_hsize,
02027 method,1));
02028
02029 trace_corr_tab=xsh_crea_correct_coeff(qc_trace_merged_frame,instrument);
02030
02031 check( xsh_add_product_table( qc_trace_merged_frame, frameset,
02032 parameters, recipe_id, instrument,
02033 NULL));
02034
02035 sprintf(tag,"%s_ORDER3D_DATA_%s",rec_prefix,qualifier);
02036
02037 check(xsh_add_product_imagelist(frame_data_cube,frameset,parameters,
02038 recipe_id,instrument,tag));
02039
02040
02041
02042
02043
02044
02045
02046 }
02047
02048 if(trace_corr_tab) {
02049 xsh_add_product_table( trace_corr_tab, frameset,
02050 parameters, recipe_id, instrument,NULL);
02051 }
02052
02053 cleanup:
02054
02055 xsh_free_propertylist(&data_plist);
02056 xsh_free_propertylist(&errs_plist);
02057 xsh_free_propertylist(&qual_plist);
02058 xsh_free_vector(&profile);
02059
02060 xsh_free_image(&data_tmp);
02061 xsh_free_image(&errs_tmp);
02062 xsh_free_image(&qual_tmp);
02063 xsh_free_image(&mask_tmp);
02064
02065 xsh_free_image(&data_img);
02066 xsh_free_image(&errs_img);
02067 xsh_free_image(&qual_img);
02068
02069
02070 xsh_free_table(&sp_fmt_tab);
02071
02072 xsh_free_imagelist(&data_cube);
02073 xsh_free_imagelist(&errs_cube);
02074 xsh_free_imagelist(&qual_cube);
02075
02076
02077 xsh_free_imagelist(&data_cube_merge);
02078 xsh_free_imagelist(&errs_cube_merge);
02079 xsh_free_imagelist(&qual_cube_merge);
02080 xsh_free_imagelist(&mask_data_merge);
02081 xsh_free_imagelist(&mask_errs_merge);
02082 xsh_free_imagelist(&mask_qual_merge);
02083
02084 xsh_rec_list_free(&rec_list);
02085 xsh_free_frame(&qc_trace_frame);
02086 xsh_free_frame(&qc_trace_merged_frame);
02087 xsh_free_frame(&trace_corr_tab);
02088 xsh_free_frame(&frame_data_cube);
02089 xsh_free_frame(&frame_errs_cube);
02090 xsh_free_frame(&frame_qual_cube);
02091 xsh_free_frame(&frame_merged_cube);
02092
02093
02094
02095
02096
02097 return cpl_error_get_code();
02098
02099 }
02100
02101
02102
02109 cpl_matrix * xsh_atrous( cpl_vector *spec, int nscales){
02110
02111 cpl_matrix *decomp = NULL;
02112 cpl_vector *filter = NULL;
02113 double orig_filter[3] = {3./8., 1./4., 1./16.};
02114 double *new_filter = NULL;
02115 int filter_size = 3;
02116
02117 int spec_size;
02118 cpl_vector *sp = NULL;
02119 cpl_vector *smooth = NULL;
02120 int i,k;
02121 cpl_vector *tmp_filter = NULL;
02122
02123
02124 XSH_ASSURE_NOT_NULL( spec);
02125 XSH_ASSURE_NOT_ILLEGAL( nscales >= 1);
02126
02127
02128 check( spec_size = cpl_vector_get_size( spec));
02129 check( decomp = cpl_matrix_new( nscales+1, spec_size));
02130 sp = cpl_vector_duplicate( spec);
02131 smooth = cpl_vector_duplicate( spec);
02132 check( filter = cpl_vector_wrap( filter_size, orig_filter));
02133
02134
02135 for(k=0; k< nscales; k++){
02136 xsh_msg_dbg_medium("scale %d use filter with size %d", k, filter_size);
02137
02138 check( cpl_wlcalib_xc_convolve( smooth, filter));
02139
02140
02141 for(i=0; i<spec_size; i++){
02142 double sp_val, smooth_val;
02143
02144 check( sp_val = cpl_vector_get( sp, i));
02145 check( smooth_val = cpl_vector_get( smooth, i));
02146 check( cpl_matrix_set( decomp, nscales-k, i, sp_val-smooth_val));
02147 }
02148
02149
02150 xsh_free_vector( &sp);
02151 check( sp = cpl_vector_duplicate(smooth));
02152
02153
02154 check( tmp_filter = cpl_vector_duplicate( filter));
02155 XSH_FREE( new_filter);
02156 filter_size = cpl_vector_get_size( tmp_filter);
02157 XSH_CALLOC( new_filter, double , filter_size*2-1);
02158
02159 for(i=0; i<filter_size; i++){
02160 new_filter[2*i] = cpl_vector_get( tmp_filter, i);
02161 }
02162 xsh_free_vector( &tmp_filter);
02163 xsh_unwrap_vector( &filter);
02164 check( filter = cpl_vector_wrap( filter_size*2-1, new_filter));
02165 filter_size = filter_size*2-1;
02166 }
02167
02168
02169
02170 for(i=0; i<spec_size; i++){
02171 double val;
02172
02173 check( val = cpl_vector_get( sp, i));
02174 check( cpl_matrix_set( decomp, 0, i, val));
02175 }
02176
02177 cleanup:
02178 if ( cpl_error_get_code() != CPL_ERROR_NONE){
02179 xsh_free_matrix( &decomp);
02180 }
02181 xsh_unwrap_vector( &filter);
02182 xsh_free_vector( &sp);
02183 xsh_free_vector( &smooth);
02184 XSH_FREE( new_filter);
02185 return decomp;
02186 }
02187
02188
02189
02190
02197 cpl_frameset * xsh_shift_offsettab( cpl_frameset *shiftifu_frameset,
02198 double offset_low, double offset_up)
02199 {
02200 cpl_frameset *result = NULL;
02201 cpl_frame *lo_frame = NULL;
02202 const char *lo_name = NULL;
02203 cpl_table *lo_table = NULL;
02204 double *lo_data = NULL;
02205 cpl_frame *up_frame = NULL;
02206 const char *up_name = NULL;
02207 cpl_table *up_table = NULL;
02208 double *up_data = NULL;
02209 int i, size;
02210 cpl_propertylist *lo_header = NULL;
02211 cpl_propertylist *up_header = NULL;
02212 cpl_frame *prod_frame = NULL;
02213
02214 lo_frame = cpl_frameset_get_frame(shiftifu_frameset,0);
02215 lo_name = cpl_frame_get_filename(lo_frame);
02216 xsh_msg("Name %s", lo_name);
02217 XSH_TABLE_LOAD( lo_table, lo_name);
02218 check( lo_data = cpl_table_get_data_double( lo_table,
02219 XSH_SHIFTIFU_COLNAME_SHIFTSLIT));
02220
02221 up_frame = cpl_frameset_get_frame(shiftifu_frameset,2);
02222 up_name = cpl_frame_get_filename(up_frame);
02223 xsh_msg("Name %s", up_name);
02224 XSH_TABLE_LOAD( up_table, up_name);
02225 check( up_data = cpl_table_get_data_double( up_table,
02226 XSH_SHIFTIFU_COLNAME_SHIFTSLIT));
02227
02228 size = cpl_table_get_nrow( lo_table);
02229
02230 for( i=0; i<size; i++){
02231 lo_data[i] += offset_low;
02232 up_data[i] += offset_up;
02233 }
02234
02235 lo_header = cpl_propertylist_load( lo_name,0);
02236 check( cpl_table_save( lo_table, lo_header, NULL, "tmp_OFFSET_TAB_LOW.fits", CPL_IO_DEFAULT));
02237 up_header = cpl_propertylist_load( up_name,0);
02238 check( cpl_table_save( up_table, up_header, NULL, "tmp_OFFSET_TAB_UP.fits", CPL_IO_DEFAULT));
02239
02240 result = cpl_frameset_new();
02241 check(prod_frame = xsh_frame_product( "tmp_OFFSET_TAB_LOW.fits",
02242 "OFFSET_TAB",
02243 CPL_FRAME_TYPE_TABLE,
02244 CPL_FRAME_GROUP_PRODUCT,
02245 CPL_FRAME_LEVEL_TEMPORARY));
02246 cpl_frameset_insert( result, prod_frame);
02247
02248 check(prod_frame = cpl_frame_duplicate( cpl_frameset_get_frame(shiftifu_frameset,1)));
02249 cpl_frameset_insert( result, prod_frame);
02250
02251 check(prod_frame = xsh_frame_product( "tmp_OFFSET_TAB_UP.fits",
02252 "OFFSET_TAB",
02253 CPL_FRAME_TYPE_TABLE,
02254 CPL_FRAME_GROUP_PRODUCT,
02255 CPL_FRAME_LEVEL_TEMPORARY));
02256 cpl_frameset_insert( result, prod_frame);
02257
02258 cleanup:
02259 if ( cpl_error_get_code() != CPL_ERROR_NONE){
02260 xsh_free_frameset( &result);
02261 }
02262 XSH_TABLE_FREE( lo_table);
02263 XSH_TABLE_FREE( up_table);
02264 xsh_free_propertylist( &lo_header);
02265 xsh_free_propertylist( &up_header);
02266 return result;
02267 }
02268