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_pfits.h>
00037 #include <xsh_model_io.h>
00038 #include <xsh_data_rec.h>
00039 #include <xsh_dfs.h>
00040
00041 #define PI_NUMB (3.1415926535897932384626433832795)
00042
00043 static double
00044 xsh_interpol(double x,double x1,double x2,double y1,double y2)
00045 {
00046 return y1+(y2-y1)/(x2-x1)*(x-x1);
00047 }
00048
00049
00050 static double
00051 xsh_sess2deg(const double sess);
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 microns") ;
00181
00182 cpl_propertylist_erase_regexp(*plist, "^CRPIX3",0);
00183 cpl_propertylist_insert_after_int(*plist,"CTYPE3", "CRPIX3", crpix3 ) ;
00184 cpl_propertylist_set_comment(*plist, "CRPIX3", "Reference pixel in z") ;
00185
00186 cpl_propertylist_erase_regexp(*plist, "^CRVAL3",0);
00187 cpl_propertylist_insert_after_double(*plist,"CRPIX3", "CRVAL3", crval3) ;
00188 cpl_propertylist_set_comment(*plist, "CRVAL3", "central wavelength") ;
00189
00190 cpl_propertylist_erase_regexp(*plist, "^CDELT3",0);
00191
00192 cpl_propertylist_insert_after_double(*plist,"CRVAL3","CDELT3",cdelt3) ;
00193 cpl_propertylist_set_comment(*plist, "CDELT3", "microns per pixel") ;
00194
00195 cpl_propertylist_erase_regexp(*plist, "^CUNIT3",0);
00196 cpl_propertylist_insert_after_string(*plist,"CDELT3", "CUNIT3", "mum" ) ;
00197 cpl_propertylist_set_comment(*plist, "CUNIT3", "spectral unit" ) ;
00198
00199 }
00200
00201
00212 static void
00213 xsh_plist_set_cd_matrix2(cpl_propertylist** plist,
00214 const double cd1_1,
00215 const double cd1_2,
00216 const double cd2_1,
00217 const double cd2_2)
00218 {
00219
00220 check(cpl_propertylist_erase_regexp(*plist, "^CD1_1",0));
00221 check(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
00222 "CD1_1", cd1_1 )) ;
00223 check(cpl_propertylist_set_comment(*plist, "CD1_1",
00224 "CD rotation matrix" )) ;
00225
00226 check(cpl_propertylist_erase_regexp(*plist, "^CD1_2",0));
00227 check(cpl_propertylist_insert_after_double(*plist, "CD1_1",
00228 "CD1_2", cd1_2 )) ;
00229 check(cpl_propertylist_set_comment(*plist, "CD1_2",
00230 "CD rotation matrix" )) ;
00231
00232 check(cpl_propertylist_erase_regexp(*plist, "^CD2_1",0));
00233 check(cpl_propertylist_insert_after_double(*plist, "CD1_2",
00234 "CD2_1", cd2_1 )) ;
00235 check(cpl_propertylist_set_comment(*plist, "CD2_1",
00236 "CD rotation matrix" )) ;
00237
00238 check(cpl_propertylist_erase_regexp(*plist, "^CD2_2",0));
00239 check(cpl_propertylist_insert_after_double(*plist, "CD2_1",
00240 "CD2_2", cd2_2 )) ;
00241 check(cpl_propertylist_set_comment(*plist, "CD2_2",
00242 "CD rotation matrix" )) ;
00243
00244 cleanup:
00245 return;
00246
00247
00248 }
00249
00250
00263 static void
00264 xsh_plist_set_cd_matrix3(cpl_propertylist** plist,
00265 const double cd1_3,
00266 const double cd2_3,
00267 const double cd3_1,
00268 const double cd3_2,
00269 const double cd3_3)
00270 {
00271
00272
00273 check(cpl_propertylist_erase_regexp(*plist, "^CD1_3",0));
00274 check(cpl_propertylist_insert_after_double(*plist,"EXPTIME",
00275 "CD1_3", cd1_3 )) ;
00276 check(cpl_propertylist_set_comment(*plist, "CD1_3",
00277 "CD rotation matrix" )) ;
00278
00279
00280 check(cpl_propertylist_erase_regexp(*plist, "^CD2_3",0));
00281 check(cpl_propertylist_insert_after_double(*plist,"CD1_3",
00282 "CD2_3", cd2_3 )) ;
00283 check(cpl_propertylist_set_comment(*plist, "CD2_3",
00284 "CD rotation matrix" )) ;
00285
00286
00287
00288 check(cpl_propertylist_erase_regexp(*plist, "^CD3_1",0));
00289 check(cpl_propertylist_insert_after_double(*plist,"CD2_3",
00290 "CD3_1", cd3_1 )) ;
00291 check(cpl_propertylist_set_comment(*plist, "CD3_1",
00292 "CD rotation matrix" )) ;
00293
00294 check(cpl_propertylist_erase_regexp(*plist, "^CD3_2",0));
00295 check(cpl_propertylist_insert_after_double(*plist, "CD3_1",
00296 "CD3_2", cd3_2 )) ;
00297 check(cpl_propertylist_set_comment(*plist, "CD3_2",
00298 "CD rotation matrix" )) ;
00299
00300 check(cpl_propertylist_erase_regexp(*plist, "^CD3_3",0));
00301 check(cpl_propertylist_insert_after_double(*plist, "CD3_2",
00302 "CD3_3", cd3_3 )) ;
00303 check(cpl_propertylist_set_comment(*plist, "CD3_3",
00304 "CD rotation matrix" )) ;
00305
00306 cleanup:
00307 return;
00308
00309
00310 }
00311
00318 static double
00319 xsh_hms2deg(const double hms)
00320 {
00321 int hrs=0;
00322 int min=0;
00323 double sec=0;
00324 double deg=0;
00325 double rest=hms;
00326 int sign=1;
00327
00328
00329
00330 if(hms<0) {
00331 sign=-1;
00332 rest=-hms;
00333 }
00334
00335
00336
00337 hrs=(int)(rest/10000.);
00338
00339
00340 rest=rest-(double)(hrs*10000.);
00341 min=(int)(rest/100.);
00342
00343
00344 sec=rest-(double)(min*100.);
00345
00346
00347 deg=hrs*15+(double)(min/4.)+(double)(sec/240.);
00348
00349
00350 deg=sign*deg;
00351
00352
00353 return deg;
00354
00355 }
00356
00364 static double
00365 xsh_sess2deg(const double sess)
00366 {
00367 int grad=0;
00368 int min=0;
00369 double sec=0;
00370 double deg=0;
00371 double rest=sess;
00372 int sign=1;
00373
00374
00375
00376 if(sess<0) {
00377 sign=-1;
00378 rest=-sess;
00379 }
00380
00381
00382
00383 grad=(int)(rest/10000.);
00384
00385
00386 rest=rest-(double)(grad*10000.);
00387 min=(int)(rest/100.);
00388
00389
00390 sec=rest-(double)(min*100.);
00391
00392
00393 deg=grad+(double)(min/60.)+(double)(sec/3600.);
00394
00395
00396 deg=sign*deg;
00397
00398
00399 return deg;
00400
00401 }
00402
00403
00404
00405
00417 cpl_error_code
00418 xsh_cube_set_wcs(cpl_propertylist * plist,
00419 float cenLambda,
00420 float dispersion,
00421 int center_z,
00422 float center_x,
00423 float center_y )
00424 {
00425
00426 float pixelscale ;
00427 double ra ;
00428 double dec ;
00429 double angle ;
00430 float radangle ;
00431 double cd1_1, cd1_2, cd2_1, cd2_2 ;
00432 int sign_swap = -1;
00433
00434
00435 double cdelt1=0;
00436 double cdelt2=0;
00437 double cdelt3=dispersion;
00438
00439 double crpix1=center_x;
00440 double crpix2=center_y;
00441 int crpix3=center_z;
00442
00443 double crval1=0;
00444 double crval2=0;
00445 double crval3=cenLambda;
00446
00447
00448
00449 pixelscale = xsh_pfits_get_pixscale(plist)/2. ;
00450 ra = xsh_pfits_get_ra(plist) ;
00451 dec = xsh_pfits_get_dec(plist) ;
00452
00453
00454 ra=xsh_pfits_get_tel_targ_alpha(plist);
00455 dec=xsh_pfits_get_tel_targ_delta(plist);
00456
00457
00458 ra=xsh_hms2deg(ra);
00459 dec=xsh_sess2deg(dec);
00460
00461
00462
00463 crval1=ra;
00464 crval2=dec;
00465
00466 angle = xsh_pfits_get_posangle(plist) ;
00467
00468 if(cpl_error_get_code() != CPL_ERROR_NONE) {
00469 cpl_error_reset();
00470 }
00471 cdelt1=sign_swap*pixelscale / 3600.;
00472 cdelt2= +pixelscale / 3600.;
00473
00474
00475 radangle = angle * PI_NUMB / 180. ;
00476 cd1_1 = +cdelt1*cos(radangle);
00477 cd1_2 = -cdelt2*sin(radangle);
00478 cd2_1 = +cdelt1*sin(radangle);
00479 cd2_2 = +cdelt2*cos(radangle);
00480
00481
00482 xsh_plist_set_coord1(&plist,crpix1,crval1,cdelt1);
00483 xsh_plist_set_coord2(&plist,crpix2,crval2,cdelt2);
00484 xsh_plist_set_coord3(&plist,crpix3,crval3,cdelt3);
00485 xsh_plist_set_cd_matrix2(&plist,cd1_1,cd1_2,cd2_1,cd2_2);
00486 xsh_plist_set_cd_matrix3(&plist,0,0,0,0,dispersion);
00487
00488 return cpl_error_get_code();
00489 }
00490
00491
00492
00493
00494
00495
00496
00499
00510
00511
00512
00513 void
00514 xsh_edge_check(const int px,const int nx,const int rad_x,
00515 int* llx,int* urx)
00516 {
00517
00518
00519 *llx=px-rad_x;
00520 *urx=px+rad_x;
00521 *llx=(*llx>1) ? *llx : 1;
00522 *urx=(*urx<nx) ? *urx : nx;
00523 return;
00524
00525 }
00526
00527
00528
00543
00544
00545 void
00546 xsh_convert_xy_to_ws(double x_centroid,
00547 double* p_obj_cen,
00548 double* p_slit,
00549 double* p_wave,
00550 const int lly,
00551 const int nx,
00552 const int row,
00553 double* p_obj_cen_s,
00554 double* p_obj_cen_w)
00555 {
00556 int x_ceil=0;
00557 int x_floor=0;
00558 double s_ceil=0;
00559 double s_floor=0;
00560 double w_ceil=0;
00561 double w_floor=0;
00562
00563 p_obj_cen[row]=x_centroid;
00564 x_ceil=ceil(x_centroid);
00565 x_floor=floor(x_centroid);
00566
00567 s_ceil =p_slit[lly*nx+x_ceil];
00568 s_floor=p_slit[lly*nx+x_floor];
00569
00570 w_ceil =p_wave[lly*nx+x_ceil];
00571 w_floor=p_wave[lly*nx+x_floor];
00572
00573 p_obj_cen_s[row]=xsh_interpol(x_centroid,x_floor,x_ceil,s_floor,s_ceil);
00574 p_obj_cen_w[row]=xsh_interpol(x_centroid,x_floor,x_ceil,w_floor,w_ceil);
00575
00576 return;
00577 }
00578
00579
00580
00587
00588
00589 cpl_error_code
00590 xsh_table_edges_swap_low_upp(cpl_table** tab)
00591 {
00592
00593 cpl_table_duplicate_column(*tab,"OBJ_LOW_S_TMP",*tab,"OBJ_LOW_S");
00594 cpl_table_duplicate_column(*tab,"OBJ_UPP_S_TMP",*tab,"OBJ_UPP_S");
00595
00596 cpl_table_erase_column(*tab,"OBJ_LOW_S");
00597 cpl_table_erase_column(*tab,"OBJ_UPP_S");
00598
00599 cpl_table_duplicate_column(*tab,"OBJ_UPP_S",*tab,"OBJ_LOW_S_TMP");
00600 cpl_table_duplicate_column(*tab,"OBJ_LOW_S",*tab,"OBJ_UPP_S_TMP");
00601
00602 cpl_table_erase_column(*tab,"OBJ_LOW_S_TMP");
00603 cpl_table_erase_column(*tab,"OBJ_UPP_S_TMP");
00604
00605 cpl_table_duplicate_column(*tab,"OBJ_LOW_W_TMP",*tab,"OBJ_LOW_W");
00606 cpl_table_duplicate_column(*tab,"OBJ_UPP_W_TMP",*tab,"OBJ_UPP_W");
00607
00608 cpl_table_erase_column(*tab,"OBJ_LOW_W");
00609 cpl_table_erase_column(*tab,"OBJ_UPP_W");
00610
00611 cpl_table_duplicate_column(*tab,"OBJ_UPP_W",*tab,"OBJ_LOW_W_TMP");
00612 cpl_table_duplicate_column(*tab,"OBJ_LOW_W",*tab,"OBJ_UPP_W_TMP");
00613
00614 cpl_table_erase_column(*tab,"OBJ_LOW_W_TMP");
00615 cpl_table_erase_column(*tab,"OBJ_UPP_W_TMP");
00616
00617
00618 return cpl_error_get_code();
00619 }
00620
00621
00622
00623
00630
00631
00632 cpl_table*
00633 xsh_table_edge_prepare(const char* name)
00634 {
00635 cpl_table* tab=NULL;
00636 int nrow=0;
00637
00638 check(tab=cpl_table_load(name,2,0));
00639
00640 nrow=cpl_table_get_nrow(tab);
00641
00642 cpl_table_new_column(tab,"OBJ_LOW_X",CPL_TYPE_DOUBLE);
00643 cpl_table_new_column(tab,"OBJ_CEN_X",CPL_TYPE_DOUBLE);
00644 cpl_table_new_column(tab,"OBJ_UPP_X",CPL_TYPE_DOUBLE);
00645
00646 cpl_table_fill_column_window(tab,"OBJ_LOW_X",0,nrow,-1);
00647 cpl_table_fill_column_window(tab,"OBJ_CEN_X",0,nrow,-1);
00648 cpl_table_fill_column_window(tab,"OBJ_UPP_X",0,nrow,-1);
00649
00650 cpl_table_new_column(tab,"OBJ_LOW_S",CPL_TYPE_DOUBLE);
00651 cpl_table_new_column(tab,"OBJ_LOW_W",CPL_TYPE_DOUBLE);
00652 cpl_table_new_column(tab,"OBJ_CEN_S",CPL_TYPE_DOUBLE);
00653 cpl_table_new_column(tab,"OBJ_CEN_W",CPL_TYPE_DOUBLE);
00654 cpl_table_new_column(tab,"OBJ_UPP_S",CPL_TYPE_DOUBLE);
00655 cpl_table_new_column(tab,"OBJ_UPP_W",CPL_TYPE_DOUBLE);
00656
00657 cpl_table_fill_column_window(tab,"OBJ_LOW_S",0,nrow,-1);
00658 cpl_table_fill_column_window(tab,"OBJ_LOW_W",0,nrow,-1);
00659 cpl_table_fill_column_window(tab,"OBJ_CEN_S",0,nrow,-1);
00660 cpl_table_fill_column_window(tab,"OBJ_CEN_W",0,nrow,-1);
00661 cpl_table_fill_column_window(tab,"OBJ_UPP_S",0,nrow,-1);
00662 cpl_table_fill_column_window(tab,"OBJ_UPP_W",0,nrow,-1);
00663
00664 cleanup:
00665 return tab;
00666
00667 }
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00784
00785
00786 cpl_error_code
00787 xsh_ifu_trace_object_calibrate(const char* ifu_object_ff_name,
00788 const char* order_tab_edges_ifu_name,
00789 const char* slit_map_name,
00790 const char* wave_map_name)
00791 {
00792 cpl_image* ifu_object_ff_ima=NULL;
00793 cpl_image* wave_map_ima=NULL;
00794 cpl_image* slit_map_ima=NULL;
00795 cpl_image* ratio_ima=NULL;
00796 cpl_table* tab=NULL;
00797
00798 int sx=0;
00799 int sy=0;
00800
00801 int nrow=0;
00802
00803 double* p_edge_lo_x=NULL;
00804 double* p_edge_up_x=NULL;
00805
00806 double* p_slice_lo_x=NULL;
00807 double* p_slice_up_x=NULL;
00808
00809 double* p_center_y=NULL;
00810
00811 double* p_obj_low=NULL;
00812 double* p_obj_cen=NULL;
00813 double* p_obj_upp=NULL;
00814
00815 double* p_obj_low_s=NULL;
00816 double* p_obj_low_w=NULL;
00817 double* p_obj_cen_s=NULL;
00818 double* p_obj_cen_w=NULL;
00819 double* p_obj_upp_s=NULL;
00820 double* p_obj_upp_w=NULL;
00821
00822 double* p_slit=NULL;
00823 double* p_wave=NULL;
00824
00825 double x_centroid=0;
00826
00827 int llx=0;
00828 int lly=0;
00829 int urx=0;
00830 int ury=0;
00831
00832 int row=0;
00833
00834 double range1_w_min=0;
00835 double range1_w_max=0;
00836 double range2_w_min=0;
00837 double range2_w_max=0;
00838
00839 double range_ks_w_min=0;
00840 double range_ks_w_max=0;
00841
00842 int nx=0;
00843 int ny=0;
00844 cpl_propertylist* plist=NULL;
00845 XSH_ARM arm=XSH_ARM_UNDEFINED;
00846 const char* pcatg=NULL;
00847 char tag[25];
00848 char name[80];
00849 int px=0;
00850 int py=0;
00851
00852 int rad_x=10;
00853
00854
00855
00856
00857 int binx=1;
00858 int biny=1;
00859
00860 typedef enum {centroid, gaussian} xsh_fit_method;
00861 xsh_fit_method fit_method = centroid;
00862
00863
00864 check(ifu_object_ff_ima=cpl_image_load(ifu_object_ff_name,CPL_TYPE_DOUBLE,0,0));
00865 plist=cpl_propertylist_load(ifu_object_ff_name,0);
00866 pcatg=xsh_pfits_get_pcatg(plist);
00867
00868 xsh_msg("pcatg=%s",pcatg);
00869
00870 if(strstr(pcatg,"UVB") != NULL) {
00871 arm=XSH_ARM_UVB;
00872 range1_w_min=300;
00873 range1_w_max=500;
00874 range_ks_w_min=380;
00875 range_ks_w_max=550;
00876 binx=xsh_pfits_get_binx(plist);
00877 biny=xsh_pfits_get_biny(plist);
00878
00879
00880 } else if(strstr(pcatg,"VIS") != NULL) {
00881 arm=XSH_ARM_VIS;
00882 range1_w_min=600;
00883 range1_w_max=950;
00884
00885 range_ks_w_min=600;
00886 range_ks_w_max=900;
00887 binx=xsh_pfits_get_binx(plist);
00888 biny=xsh_pfits_get_biny(plist);
00889
00890 } else if(strstr(pcatg,"NIR") != NULL) {
00891 arm=XSH_ARM_NIR;
00892 range1_w_min=1050;
00893 range1_w_max=1250;
00894 range2_w_min=2000;
00895 range2_w_max=2200;
00896
00897 range_ks_w_min=1100;
00898 range_ks_w_max=2200;
00899
00900
00901 }
00902
00903 check(slit_map_ima=cpl_image_load(slit_map_name,CPL_TYPE_DOUBLE,0,0));
00904 check(wave_map_ima=cpl_image_load(wave_map_name,CPL_TYPE_DOUBLE,0,0));
00905 sx=cpl_image_get_size_x(ifu_object_ff_ima);
00906 sy=cpl_image_get_size_y(ifu_object_ff_ima);
00907
00908 check(tab=xsh_table_edge_prepare(order_tab_edges_ifu_name));
00909 nrow=cpl_table_get_nrow(tab);
00910
00911 p_edge_lo_x=cpl_table_get_data_double(tab,"EDG_LO_X");
00912 p_slice_lo_x=cpl_table_get_data_double(tab,"SLIC_LO_X");
00913 p_obj_low=cpl_table_get_data_double(tab,"OBJ_LOW_X");
00914 p_obj_low_s=cpl_table_get_data_double(tab,"OBJ_LOW_S");
00915 p_obj_low_w=cpl_table_get_data_double(tab,"OBJ_LOW_W");
00916
00917 p_edge_up_x=cpl_table_get_data_double(tab,"EDG_UP_X");
00918 p_slice_up_x=cpl_table_get_data_double(tab,"SLIC_UP_X");
00919 p_obj_upp=cpl_table_get_data_double(tab,"OBJ_UPP_X");
00920 p_obj_upp_s=cpl_table_get_data_double(tab,"OBJ_UPP_S");
00921 p_obj_upp_w=cpl_table_get_data_double(tab,"OBJ_UPP_W");
00922
00923 p_center_y=cpl_table_get_data_double(tab,"CENTER_Y");
00924 p_obj_cen=cpl_table_get_data_double(tab,"OBJ_CEN_X");
00925 p_obj_cen_s=cpl_table_get_data_double(tab,"OBJ_CEN_S");
00926 p_obj_cen_w=cpl_table_get_data_double(tab,"OBJ_CEN_W");
00927
00928 p_slit=cpl_image_get_data_double(slit_map_ima);
00929 p_wave=cpl_image_get_data_double(wave_map_ima);
00930 nx=cpl_image_get_size_x(wave_map_ima);
00931 ny=cpl_image_get_size_y(wave_map_ima);
00932
00933
00934 for(row=0;row<nrow;row++) {
00935
00936 lly=(int)p_center_y[row]/biny;
00937 ury=(int)p_center_y[row]/biny;
00938
00939
00940 if((llx<nx) && (ury<ny) ) {
00941
00942
00943 llx=floor(p_slice_lo_x[row]);
00944 urx=ceil(p_slice_up_x[row]);
00945
00946 check(cpl_image_get_maxpos_window(ifu_object_ff_ima,
00947 llx,lly,urx,ury,&px,&py));
00948
00949 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00950 if(fit_method==centroid) {
00951 x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00952 llx,lly,urx,ury);
00953
00954 } else {
00955 check(x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00956 llx,urx,lly));
00957 }
00958
00959
00960 p_obj_cen[row]=x_centroid;
00961
00962 if(urx<nx) {
00963 check(xsh_convert_xy_to_ws(x_centroid,p_obj_cen,p_slit,p_wave,
00964 lly,nx,row,p_obj_cen_s,p_obj_cen_w));
00965 }
00966
00967
00968 llx=floor(p_edge_lo_x[row]);
00969 urx=ceil(p_slice_lo_x[row]);
00970 cpl_image_get_maxpos_window(ifu_object_ff_ima,
00971 llx,lly,urx,ury,&px,&py);
00972
00973 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00974 if(fit_method==centroid) {
00975 check(x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00976 llx,lly,urx,ury));
00977 } else {
00978 x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
00979 llx,urx,lly);
00980 }
00981
00982
00983
00984 if(urx<nx) {
00985 check(xsh_convert_xy_to_ws(x_centroid,p_obj_low,p_slit,p_wave,
00986 lly,nx,row,p_obj_low_s,p_obj_low_w));
00987 }
00988
00989
00990 llx=floor(p_slice_up_x[row]);
00991 urx=ceil(p_edge_up_x[row]);
00992 cpl_image_get_maxpos_window(ifu_object_ff_ima,
00993 llx,lly,urx,ury,&px,&py);
00994
00995 xsh_edge_check(px,nx,rad_x,&llx,&urx);
00996 if(fit_method==centroid) {
00997 x_centroid=cpl_image_get_centroid_x_window(ifu_object_ff_ima,
00998 llx,lly,urx,ury);
00999 } else {
01000 x_centroid=xsh_image_fit_gaussian_max_pos_x_window(ifu_object_ff_ima,
01001 llx,urx,lly);
01002 }
01003
01004 p_obj_upp[row]=x_centroid;
01005
01006 if(urx<nx) {
01007 check(xsh_convert_xy_to_ws(x_centroid,p_obj_upp,p_slit,p_wave,
01008 lly,nx,row,p_obj_upp_s,p_obj_upp_w));
01009 }
01010 }
01011 }
01012
01013 if(arm==XSH_ARM_VIS) {
01014 xsh_table_edges_swap_low_upp(&tab);
01015 }
01016 cpl_table_duplicate_column(tab,"OBJ_CEN_PLUS_UPP_S",tab,"OBJ_CEN_S");
01017
01018 cpl_table_duplicate_column(tab,"OBJ_CEN_PLUS_LOW_S",tab,"OBJ_CEN_S");
01019
01020 cpl_table_add_columns(tab,"OBJ_CEN_PLUS_UPP_S","OBJ_UPP_S");
01021 cpl_table_add_columns(tab,"OBJ_CEN_PLUS_LOW_S","OBJ_LOW_S");
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045 sprintf(tag,"TRACE_OBJ_%s",xsh_arm_tostring(arm));
01046 sprintf(name,"%s.fits",tag);
01047 xsh_pfits_set_pcatg(plist,tag);
01048 check(cpl_table_save(tab,plist,NULL,name,CPL_IO_DEFAULT));
01049
01050
01051
01052 cleanup:
01053 xsh_free_image(&ifu_object_ff_ima);
01054 xsh_free_image(&slit_map_ima);
01055 xsh_free_image(&wave_map_ima);
01056 xsh_free_image(&ratio_ima);
01057 xsh_free_propertylist(&plist);
01058 return cpl_error_get_code();
01059
01060 }
01061
01062
01063
01071
01072
01073 cpl_error_code
01074 xsh_frame_check_model_cfg_is_proper_for_sci(cpl_frame* model_config_frame,
01075 cpl_frame* sci_frame,
01076 xsh_instrument* instrument)
01077 {
01078
01079 cpl_propertylist* afc_plist=NULL;
01080 cpl_propertylist* sci_plist=NULL;
01081 const char* name=NULL;
01082 const char* sci_obs_targ_name=NULL;
01083 const char* afc_obs_targ_name=NULL;
01084 const char* afc_slit_value=NULL;
01085 const char* sci_slit_value=NULL;
01086 int sci_obs_id=0;
01087 int afc_obs_id=0;
01088
01089 check(name=cpl_frame_get_filename(model_config_frame));
01090 check(afc_plist=cpl_propertylist_load(name,0));
01091
01092 check(name=cpl_frame_get_filename(sci_frame));
01093 check(sci_plist=cpl_propertylist_load(name,0));
01094
01095 check(afc_slit_value=xsh_pfits_get_slit_value(afc_plist,instrument ));
01096
01097 check(sci_slit_value=xsh_pfits_get_slit_value(sci_plist,instrument ));
01098
01099 if(strstr(afc_slit_value,"Pin_0.5") == NULL) {
01100 xsh_msg_error("You have used uncorrect AFC corrected model cfg frame");
01101 xsh_msg_error("IFU AFC corrected model CFG must have INS.OPTIi.NAME='Pin_0.5'");
01102 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01103 }
01104
01105
01106 check(sci_obs_targ_name=xsh_pfits_get_obs_targ_name(sci_plist));
01107 check(afc_obs_targ_name=xsh_pfits_get_obs_targ_name(afc_plist));
01108 if(strcmp(sci_obs_targ_name,afc_obs_targ_name) != 0) {
01109 xsh_msg_error("Improper AFC corrected model cfg frame to reduce sci frame");
01110 xsh_msg_error("Their OBS.TARG.NAME values must match");
01111 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01112
01113 }
01114
01115 check(sci_obs_id=xsh_pfits_get_obs_id(sci_plist));
01116 check(afc_obs_id=xsh_pfits_get_obs_id(afc_plist));
01117
01118 if(sci_obs_id != afc_obs_id) {
01119 xsh_msg_error("Improper AFC corrected model cfg frame to reduce sci frame");
01120 xsh_msg_error("Their OBS.ID values must match");
01121 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01122 }
01123
01124 cleanup:
01125
01126 xsh_free_propertylist(&sci_plist);
01127 xsh_free_propertylist(&afc_plist);
01128
01129 return cpl_error_get_code();
01130 }
01131
01132
01133
01140
01141
01142 cpl_error_code
01143 xsh_frame_check_model_cfg_is_afc_corrected(cpl_frame* model_config_frame){
01144
01145 cpl_propertylist* plist=NULL;
01146 const char* name=NULL;
01147 const char* raw1_catg=NULL;
01148
01149 check(name=cpl_frame_get_filename(model_config_frame));
01150 check(plist=cpl_propertylist_load(name,0));
01151 check(raw1_catg=xsh_pfits_get_raw1catg(plist));
01152 if(strstr(raw1_catg,"AFC_ATT") == NULL) {
01153 xsh_msg_error("model cfg frame seems not to be AFC corrected");
01154 xsh_msg_error("Their PRO.REC1.RAW1..NAME values must contain AFC_ATT");
01155 cpl_error_set(cpl_func,CPL_ERROR_ILLEGAL_INPUT);
01156 }
01157
01158 cleanup:
01159
01160 xsh_free_propertylist(&plist);
01161
01162 return cpl_error_get_code();
01163
01164 }
01165
01166
01167
01168
01185
01186 cpl_error_code
01187 xsh_build_ifu_cube(cpl_frame* div_frame,
01188 cpl_frame* ifu_config_frame,
01189 cpl_frame* spectral_format_frame,
01190 cpl_frame* model_config_frame,
01191 cpl_frame* wavesol_frame,
01192 xsh_instrument* instrument,
01193 cpl_frameset* frameset,
01194 cpl_parameterlist* parameters,
01195 xsh_rectify_param * rectify_par,
01196 const char* recipe_id,
01197 const char* rec_prefix,
01198 const int frame_is_object)
01199 {
01200
01201
01202
01203
01204 cpl_image* data_img=NULL;
01205 cpl_image* errs_img=NULL;
01206 cpl_image* qual_img=NULL;
01207
01208 cpl_image* data_tmp=NULL;
01209 cpl_image* errs_tmp=NULL;
01210 cpl_image* qual_tmp=NULL;
01211 cpl_image* mask_tmp=NULL;
01212 cpl_frame* qc_trace_frame=NULL;
01213 cpl_frame* qc_trace_merged_frame=NULL;
01214 cpl_frame* frame_data_cube=NULL;
01215 cpl_frame* frame_errs_cube=NULL;
01216 cpl_frame* frame_qual_cube=NULL;
01217 cpl_frame* frame_merged_cube=NULL;
01218
01219
01220
01221
01222
01223 XSH_ARM arm;
01224 xsh_xs_3 model_config ;
01225 xsh_rec_list * rec_list = NULL ;
01226
01227 cpl_table* ifu_cfg_tab=NULL;
01228 cpl_table* sp_fmt_tab=NULL;
01229 cpl_vector* profile=NULL;
01230 cpl_imagelist* data_cube=NULL;
01231 cpl_imagelist* errs_cube=NULL;
01232 cpl_imagelist* qual_cube=NULL;
01233
01234 cpl_imagelist* data_cube_merge=NULL;
01235 cpl_imagelist* errs_cube_merge=NULL;
01236 cpl_imagelist* qual_cube_merge=NULL;
01237 cpl_imagelist* mask_cube_merge=NULL;
01238
01239
01240 float* pima=NULL;
01241 float* perr=NULL;
01242 int* pqua=NULL;
01243 int* pmsk=NULL;
01244 int nord=0;
01245 int save_size_uvb=4;
01246 int save_size_vis=2;
01247 int save_size_nir=2;
01248 int save_size=0;
01249 const int peack_search_hsize=5;
01250 int method=0;
01251
01252
01253
01254 cpl_propertylist* data_plist=NULL;
01255 cpl_propertylist* errs_plist=NULL;
01256 cpl_propertylist* qual_plist=NULL;
01257
01258
01259 const char* sci_name=NULL;
01260 const char* sp_fmt_name=NULL;
01261 const char* ifu_cfg_name=NULL;
01262
01263 char name[256];
01264 char qualifier[10];
01265 int binx=1;
01266 int biny=1;
01267
01268 double flux_upp=0;
01269 double flux_cen=0;
01270 double flux_low=0;
01271
01272
01273 double errs_upp=0;
01274 double errs_cen=0;
01275 double errs_low=0;
01276 double cube_wave_min=0;
01277 double cube_wave_max=0;
01278
01279
01280 int qual_upp=0;
01281 int qual_cen=0;
01282 int qual_low=0;
01283
01284 double cube_wmin=550;
01285 double cube_wmax=1000;
01286 double cube_wstep=0;
01287 double cube_sstep=0;
01288
01289 int is=0;
01290
01291 int ord=0;
01292 int ord_min=0;
01293 int ord_max=0;
01294
01295 double wave=0;
01296 double wave_min=0;
01297 double wave_max=0;
01298 double wave_step=0;
01299
01300 double wave_min_old=0;
01301 double wave_max_old=0;
01302
01303 int nstep_off=0;
01304
01305 double s=0;
01306 double s_min=-2;
01307 double s_max=2;
01308 double s_step=0;
01309
01310 double x=0;
01311 double y=0;
01312 double s_upp=0;
01313 double s_low=0;
01314
01315
01316 int radius=rectify_par->rectif_radius;
01317
01318 double confidence=0;
01319 int naxis1=0;
01320 int naxis2=0;
01321 int naxis3=0;
01322 int ik=0;
01323 double wave_cen=0;
01324
01325
01326
01327
01328
01329
01330 double w_low_coeff1=0;
01331 double w_low_coeff2=0;
01332
01333 double w_upp_coeff1=0;
01334 double w_upp_coeff2=0;
01335
01336 double w_low_coeff1_uvb=-0.002972;
01337 double w_low_coeff2_uvb=2.26497e-6+0.2e-6;
01338 double w_upp_coeff1_uvb=8.3355331e-5;
01339 double w_upp_coeff2_uvb=-1.0682e-6;
01340
01341
01342
01343 double w_low_coeff1_vis=-0.0016549569;
01344 double w_low_coeff2_vis=1.183805e-6+0.04e-6;
01345 double w_upp_coeff1_vis=-0.0016610719;
01346 double w_upp_coeff2_vis=1.0823013e-6+0.02e-6;
01347
01348 double w_low_coeff1_nir=-0.00010;
01349 double w_low_coeff2_nir=-5.0e-9;
01350
01351 double w_upp_coeff1_nir=0;
01352 double w_upp_coeff2_nir=0;
01353
01354
01355 double s_upp_off_uvb=4.0514429+0.16;
01356 double s_upp_off_vis=5.2504895+0.16-0.703;
01357 double s_upp_off_nir=3.5452+0.31;
01358 double s_upp_off=0;
01359
01360 double s_low_off_uvb=-3.4636662+0.16;
01361
01362 double s_low_off_vis=-2.9428071+0.16-0.645;
01363 double s_low_off_nir=-4.451682+0.21+0.31;
01364
01365 double s_low_off=0;
01366
01367 int w_step_fct=10;
01368
01369 int status=0;
01370 int mk=0;
01371 char tag[256];
01372 xsh_wavesol *wavesol=NULL;
01373
01374 if(frame_is_object) {
01375 sprintf(qualifier,"OBJ");
01376 } else {
01377 sprintf(qualifier,"SKY");
01378 }
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405 check(sci_name=cpl_frame_get_filename(div_frame));
01406 check(data_plist=cpl_propertylist_load(sci_name,0));
01407 arm=instrument->arm;
01408
01409
01410 cube_wstep=rectify_par->rectif_bin_lambda;
01411 cube_sstep=rectify_par->rectif_bin_space;
01412
01413 if(arm == XSH_ARM_UVB) {
01414 cube_wmin=300;
01415 cube_wmax=550;
01416
01417 binx=xsh_pfits_get_binx(data_plist);
01418 biny=xsh_pfits_get_biny(data_plist);
01419
01420 s_low_off=s_low_off_uvb;
01421 s_upp_off=s_upp_off_uvb;
01422 w_low_coeff1=w_low_coeff1_uvb;
01423 w_low_coeff2=w_low_coeff2_uvb;
01424
01425 w_upp_coeff1=w_upp_coeff1_uvb;
01426 w_upp_coeff2=w_upp_coeff2_uvb;
01427 save_size=save_size_uvb;
01428
01429
01430
01431 } else if(arm == XSH_ARM_VIS) {
01432 cube_wmin=550;
01433 cube_wmax=1000;
01434
01435
01436 binx=xsh_pfits_get_binx(data_plist);
01437 biny=xsh_pfits_get_biny(data_plist);
01438
01439 s_low_off=s_low_off_vis;
01440 s_upp_off=s_upp_off_vis;
01441
01442 w_low_coeff1=w_low_coeff1_vis;
01443 w_low_coeff2=w_low_coeff2_vis;
01444
01445 w_upp_coeff1=w_upp_coeff1_vis;
01446 w_upp_coeff2=w_upp_coeff2_vis;
01447 save_size=save_size_vis;
01448
01449
01450 } else if(arm == XSH_ARM_NIR) {
01451 cube_wmin=1000;
01452 cube_wmax=2500;
01453
01454
01455
01456 s_low_off=s_low_off_nir;
01457 s_upp_off=s_upp_off_nir;
01458
01459
01460 w_low_coeff1=w_low_coeff1_nir;
01461 w_low_coeff2=w_low_coeff2_nir;
01462
01463 w_upp_coeff1=w_upp_coeff1_nir;
01464 w_upp_coeff2=w_upp_coeff2_nir;
01465 save_size=save_size_nir;
01466
01467 }
01468
01469
01470
01471 if(ifu_config_frame) {
01472
01473 ifu_cfg_name=cpl_frame_get_filename(ifu_config_frame);
01474 ifu_cfg_tab=cpl_table_load(ifu_cfg_name,1,0);
01475 s_upp_off=cpl_table_get_double(ifu_cfg_tab,"S_UPP_OFF",arm,&status);
01476 s_low_off=cpl_table_get_double(ifu_cfg_tab,"S_LOW_OFF",arm,&status);
01477
01478 w_upp_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF1",arm,&status);
01479 w_low_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF1",arm,&status);
01480 w_upp_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF2",arm,&status);
01481 w_low_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF2",arm,&status);
01482
01483 w_step_fct=cpl_table_get_int(ifu_cfg_tab,"W_STEP_FCT",arm,&status);
01484 xsh_msg("s_upp_off=%10.8g",s_upp_off);
01485 xsh_msg("s_low_off=%10.8g",s_low_off);
01486
01487
01488 xsh_msg("w_upp_coeff1=%10.8g",w_upp_coeff1);
01489 xsh_msg("w_upp_coeff2=%10.8g",w_upp_coeff2);
01490 xsh_msg("w_low_coeff1=%10.8g",w_low_coeff1);
01491 xsh_msg("w_low_coeff2=%10.8g",w_low_coeff2);
01492
01493 xsh_msg("w_step_fct=%d",w_step_fct);
01494
01495 }
01496 cube_wstep*=w_step_fct;
01497 if(model_config_frame!=NULL) {
01498 check( xsh_model_config_load_best( model_config_frame, &model_config));
01499 xsh_model_binxy(&model_config,binx,biny);
01500 } else {
01501 check( wavesol = xsh_wavesol_load( wavesol_frame, instrument));
01502 }
01503 check( rec_list = xsh_rec_list_create( instrument));
01504 check( profile = cpl_vector_new( CPL_KERNEL_DEF_SAMPLES));
01505 check(cpl_vector_fill_kernel_profile( profile,rectify_par->kernel_type,
01506 rectify_par->rectif_radius));
01507
01508 check(sci_name=cpl_frame_get_filename(div_frame));
01509 check(data_img=cpl_image_load(sci_name,XSH_PRE_DATA_TYPE,0,0));
01510 check(errs_img=cpl_image_load(sci_name,XSH_PRE_ERRS_TYPE,0,1));
01511 check(qual_img=cpl_image_load(sci_name,XSH_PRE_QUAL_TYPE,0,2));
01512
01513 check(sp_fmt_name=cpl_frame_get_filename(spectral_format_frame));
01514 check(sp_fmt_tab=cpl_table_load(sp_fmt_name,1,0));
01515 check(ord_min=cpl_table_get_column_min(sp_fmt_tab,"ORDER"));
01516 check(ord_max=cpl_table_get_column_max(sp_fmt_tab,"ORDER"));
01517 nord=ord_max-ord_min+1;
01518
01519 check(cube_wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",nord-1,&status));
01520 check(cube_wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",0,&status));
01521
01522 xsh_msg_debug("cube_wave_min=%10.8g cube_wave_max=%10.8g",cube_wave_min,cube_wave_max);
01523
01524
01525 naxis1=3;
01526 xsh_msg_debug("ord_min=%d ord_max=%d",ord_min,ord_max);
01527 wave_step= cube_wstep;
01528 s_step= cube_sstep;
01529 naxis2=(int)((s_max-s_min)/s_step+0.5)+1;
01530
01531
01532 xsh_free_imagelist(&data_cube_merge);
01533 xsh_free_imagelist(&errs_cube_merge);
01534 xsh_free_imagelist(&qual_cube_merge);
01535 xsh_free_imagelist(&mask_cube_merge);
01536
01537 data_cube_merge=cpl_imagelist_new();
01538 errs_cube_merge=cpl_imagelist_new();
01539 qual_cube_merge=cpl_imagelist_new();
01540 mask_cube_merge=cpl_imagelist_new();
01541
01542
01543 xsh_free_image(&data_tmp);
01544 xsh_free_image(&errs_tmp);
01545 xsh_free_image(&qual_tmp);
01546 xsh_free_image(&mask_tmp);
01547
01548 mk=0;
01549
01550 for( ord = ord_max; ord >= ord_min; ord-- ) {
01551
01552 xsh_free_imagelist(&data_cube);
01553 xsh_free_imagelist(&errs_cube);
01554 xsh_free_imagelist(&qual_cube);
01555
01556 data_cube=cpl_imagelist_new();
01557 errs_cube=cpl_imagelist_new();
01558 qual_cube=cpl_imagelist_new();
01559
01560
01561 data_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_DATA_TYPE);
01562 errs_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_ERRS_TYPE);
01563 qual_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
01564 mask_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
01565
01566 check(pima=cpl_image_get_data_float(data_tmp));
01567 check(perr=cpl_image_get_data_float(errs_tmp));
01568 check(pqua=cpl_image_get_data_int(qual_tmp));
01569 check(pmsk=cpl_image_get_data_int(mask_tmp));
01570
01571
01572
01573 check(wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",ord-ord_min,&status));
01574 check(wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",ord-ord_min,&status));
01575
01576 naxis3=(wave_max-wave_min)/wave_step+1;
01577 wave_cen=wave_min+naxis3/2*wave_step;
01578 xsh_msg_debug("order=%d naxis1=%d,naxis2=%d naxis3=%d, wave_min=%g wave_max=%g",
01579 ord,naxis1,naxis2,naxis3,wave_min,wave_max);
01580
01581
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600 if(ord<ord_max) {
01601
01602
01603
01604
01605 nstep_off=(int)((wave_min-wave_min_old)/wave_step+0.5);
01606
01607
01608
01609
01610
01611 wave_min=wave_min_old+nstep_off*wave_step;
01612
01613
01614
01615
01616
01617
01618 }
01619
01620 ik=0;
01621
01622 for( wave = wave_min; wave <= wave_max; wave+=wave_step) {
01623 is=0;
01624 for( s = s_min; s <= s_max; s+=s_step ) {
01625
01626
01627
01628 s_upp=-s+s_upp_off+w_upp_coeff1*wave+w_upp_coeff2*wave*wave;
01629 if(wavesol!=NULL) {
01630 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s_upp));
01631 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s_upp));
01632 } else {
01633
01634 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_upp,
01635 &x,&y));
01636 }
01637
01638 check(flux_upp=cpl_image_get_interpolated(data_img,x,y,
01639 profile,radius,
01640 profile,radius,
01641 &confidence));
01642 check(errs_upp=cpl_image_get_interpolated(errs_img,x,y,
01643 profile,radius,
01644 profile,radius,
01645 &confidence));
01646 check(qual_upp=cpl_image_get_interpolated(qual_img,x,y,
01647 profile,radius,
01648 profile,radius,
01649 &confidence));
01650
01651 pima[is*naxis1+0]=flux_upp;
01652 perr[is*naxis1+0]=errs_upp;
01653 pqua[is*naxis1+0]=qual_upp;
01654 pmsk[is*naxis1+0]=1;
01655
01656
01657
01658
01659
01660 if(wavesol!=NULL) {
01661 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s));
01662 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s));
01663 } else {
01664 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s,
01665 &x,&y));
01666 }
01667 check(flux_cen=cpl_image_get_interpolated(data_img,x,y,
01668 profile,radius,
01669 profile,radius,
01670 &confidence));
01671
01672 check(errs_cen=cpl_image_get_interpolated(errs_img,x,y,
01673 profile,radius,
01674 profile,radius,
01675 &confidence));
01676
01677 check(qual_cen=cpl_image_get_interpolated(qual_img,x,y,
01678 profile,radius,
01679 profile,radius,
01680 &confidence));
01681
01682 pima[is*naxis1+1]=flux_cen;
01683 perr[is*naxis1+1]=errs_cen;
01684 pqua[is*naxis1+1]=qual_cen;
01685 pmsk[is*naxis1+1]=1;
01686
01687
01688
01689
01690
01691
01692
01693
01694
01695 s_low=-s+s_low_off+w_low_coeff1*wave+w_low_coeff2*wave*wave;
01696 if(wavesol!=NULL) {
01697 check( x= xsh_wavesol_eval_polx( wavesol,wave, ord,s_low));
01698 check( y= xsh_wavesol_eval_poly( wavesol,wave, ord,s_low));
01699 } else {
01700
01701 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_low,
01702 &x,&y));
01703 }
01704
01705 check(flux_low=cpl_image_get_interpolated(data_img,x,y,
01706 profile,radius,
01707 profile,radius,
01708 &confidence));
01709 check(errs_low=cpl_image_get_interpolated(errs_img,x,y,
01710 profile,radius,
01711 profile,radius,
01712 &confidence));
01713 check(qual_low=cpl_image_get_interpolated(qual_img,x,y,
01714 profile,radius,
01715 profile,radius,
01716 &confidence));
01717
01718 pima[is*naxis1+2]=flux_low;
01719 perr[is*naxis1+2]=errs_low;
01720 pqua[is*naxis1+2]=qual_low;
01721 pmsk[is*naxis1+2]=1;
01722
01723
01724 is++;
01725 }
01726 wave_max_old=wave_max;
01727 wave_min_old=wave_min;
01728
01729 check(cpl_imagelist_set(data_cube,cpl_image_duplicate(data_tmp),ik));
01730 check(cpl_imagelist_set(errs_cube,cpl_image_duplicate(errs_tmp),ik));
01731 check(cpl_imagelist_set(qual_cube,cpl_image_duplicate(qual_tmp),ik));
01732
01733
01734
01735
01736 check(xsh_iml_merge_avg(&data_cube_merge,&mask_cube_merge,
01737 data_tmp,mask_tmp,mk));
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747
01748
01749
01750
01751
01752
01753
01754
01755
01756 ik++;
01757 if(ord==ord_max) {
01758 mk++;
01759 } else {
01760 mk=(int)((wave-cube_wave_min)/wave_step+0.5);
01761 }
01762 }
01763
01764 sprintf(tag,"%s_ORDER3D_DATA_%s_%s",rec_prefix,qualifier,
01765 xsh_arm_tostring(arm));
01766
01767 sprintf(name,"%s.fits",tag);
01768
01769 xsh_pfits_set_crval3(data_plist,wave_min);
01770 xsh_pfits_set_cdelt3(data_plist,wave_step);
01771
01772
01773 if(ord==ord_max) {
01774 cpl_imagelist_save(data_cube,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_DEFAULT);
01775 frame_data_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01776 } else {
01777 cpl_imagelist_save(data_cube,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_EXTEND);
01778 }
01779
01780 xsh_free_frame(&qc_trace_frame);
01781 sprintf(tag,"ORDER%2.2d",ord);
01782 check(qc_trace_frame=xsh_cube_qc_trace_window(frame_data_cube,
01783 instrument,tag,rec_prefix,
01784 save_size+1,
01785 naxis2-save_size,
01786 peack_search_hsize,
01787 method,0));
01788
01789
01790
01791
01792
01793
01794 sprintf(tag,"%s_ORDER3D_ERRS_%s_%s",rec_prefix,qualifier,
01795 xsh_arm_tostring(arm));
01796
01797 sprintf(name,"%s.fits",tag);
01798
01799 if(ord==ord_max) {
01800 cpl_imagelist_save(errs_cube,name,CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
01801 frame_errs_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01802 } else {
01803 cpl_imagelist_save(errs_cube,name,CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_EXTEND);
01804 }
01805
01806 sprintf(tag,"%s_ORDER3D_QUAL_%s_%s",rec_prefix,qualifier,
01807 xsh_arm_tostring(arm));
01808
01809 sprintf(name,"%s.fits",tag);
01810
01811 if(ord==ord_max) {
01812 cpl_imagelist_save(qual_cube,name,CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
01813 frame_qual_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01814 } else {
01815 cpl_imagelist_save(qual_cube,name,CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_EXTEND);
01816 }
01817
01818
01819 sprintf(name,"QC%s_%2.2d.fits",tag,ord);
01820
01821 xsh_free_image(&data_tmp);
01822 xsh_free_image(&errs_tmp);
01823 xsh_free_image(&qual_tmp);
01824 xsh_free_image(&mask_tmp);
01825
01826 }
01827
01828 sprintf(tag,"%s_MERGE3D_DATA_%s_%s",rec_prefix,qualifier,
01829 xsh_arm_tostring(arm));
01830 sprintf(name,"%s.fits",tag);
01831 xsh_pfits_set_crval3(data_plist,cube_wave_min);
01832 xsh_pfits_set_cdelt3(data_plist,wave_step);
01833
01834
01835 cpl_imagelist_save(data_cube_merge,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_DEFAULT);
01836
01837
01838 xsh_msg_debug("merge cube size=%d",cpl_imagelist_get_size(data_cube_merge));
01839
01840 frame_merged_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
01841 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
01842
01843 check(xsh_add_product_imagelist(frame_merged_cube,frameset,parameters,
01844 recipe_id,instrument,tag));
01845
01846 sprintf(tag,"%s_MERGE3D_%s",rec_prefix,qualifier);
01847
01848 check(qc_trace_merged_frame=xsh_cube_qc_trace_window(frame_merged_cube,
01849 instrument,
01850 "MERGE3D",rec_prefix,
01851 save_size+1,
01852 naxis2-save_size,
01853 peack_search_hsize,
01854 method,1));
01855
01856 if(frame_is_object) {
01857 check( xsh_add_product_table_multi( qc_trace_merged_frame, frameset,
01858 parameters, recipe_id, instrument));
01859
01860
01861 check(xsh_add_product_imagelist(frame_data_cube,frameset,parameters,
01862 recipe_id,
01863 instrument,
01864 cpl_frame_get_tag(frame_data_cube)));
01865
01866 check(xsh_add_product_imagelist(frame_errs_cube,frameset,parameters,
01867 recipe_id,
01868 instrument,
01869 cpl_frame_get_tag(frame_errs_cube)));
01870
01871 check(xsh_add_product_imagelist(frame_qual_cube,frameset,parameters,
01872 recipe_id,
01873 instrument,
01874 cpl_frame_get_tag(frame_qual_cube)));
01875 }
01876
01877
01878
01879
01880
01881
01882
01883
01884
01885
01886
01887
01888
01889
01890
01891
01892
01893
01894
01895
01896
01897
01898
01899
01900
01901
01902
01903
01904
01905
01906
01907 cleanup:
01908
01909 xsh_free_propertylist(&data_plist);
01910 xsh_free_propertylist(&errs_plist);
01911 xsh_free_propertylist(&qual_plist);
01912 xsh_free_vector(&profile);
01913
01914 xsh_free_image(&data_tmp);
01915 xsh_free_image(&errs_tmp);
01916 xsh_free_image(&qual_tmp);
01917 xsh_free_image(&mask_tmp);
01918
01919 xsh_free_image(&data_img);
01920 xsh_free_image(&errs_img);
01921 xsh_free_image(&qual_img);
01922
01923
01924 xsh_free_table(&sp_fmt_tab);
01925
01926 xsh_free_imagelist(&data_cube);
01927 xsh_free_imagelist(&errs_cube);
01928 xsh_free_imagelist(&qual_cube);
01929
01930
01931 xsh_free_imagelist(&data_cube_merge);
01932 xsh_free_imagelist(&errs_cube_merge);
01933 xsh_free_imagelist(&qual_cube_merge);
01934 xsh_free_imagelist(&mask_cube_merge);
01935
01936 xsh_rec_list_free(&rec_list);
01937 xsh_free_frame(&qc_trace_frame);
01938 xsh_free_frame(&qc_trace_merged_frame);
01939 xsh_free_frame(&frame_data_cube);
01940 xsh_free_frame(&frame_errs_cube);
01941 xsh_free_frame(&frame_qual_cube);
01942 xsh_free_frame(&frame_merged_cube);
01943
01944
01945
01946
01947
01948 return cpl_error_get_code();
01949
01950 }
01951