00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00037
00040
00041
00042
00043
00044 #include <math.h>
00045 #include <xsh_drl.h>
00046 #include <xsh_utils_wrappers.h>
00047 #include <xsh_data_order.h>
00048 #include <xsh_error.h>
00049 #include <xsh_utils.h>
00050 #include <xsh_utils_image.h>
00051 #include <xsh_msg.h>
00052 #include <xsh_data_pre.h>
00053 #include <cpl.h>
00054
00055
00056
00057
00058 #define HALF_SLIC_WINDOW 5
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00078 static cpl_table*
00079 xsh_compute_flat_edges(cpl_frame* frame,
00080 xsh_order_list* list,
00081 xsh_instrument* instrument,
00082 const char* method)
00083 {
00084
00085 cpl_image* mflat=NULL;
00086 const char* name=NULL;
00087 const char* name_o=NULL;
00088 char name_t[256];
00089 cpl_image* filter_x=NULL;
00090 cpl_propertylist* plist=NULL;
00091
00092 cpl_table* tbl=NULL;
00093 cpl_table* res=NULL;
00094
00095 cpl_vector* vsliclo=NULL;
00096 cpl_vector* vslicup=NULL;
00097 cpl_vector* vedgelo=NULL;
00098 cpl_vector* vedgeup=NULL;
00099 cpl_vector* vypos=NULL;
00100
00101 int* porder=NULL;
00102 int* pabsorder=NULL;
00103
00104 double* pcenterx=NULL;
00105 double* pcentery=NULL;
00106
00107
00108 double* pedgelo=NULL;
00109 double* pedgeup=NULL;
00110 double* psliceup=NULL;
00111 double* pslicelo=NULL;
00112 double* psliceup_resx=NULL;
00113 double* pslicelo_resx=NULL;
00114
00115 double* pedgeup_resx=NULL;
00116 double* pedgelo_resx=NULL;
00117
00118
00119 double* pedgelof=NULL;
00120 double* pedgeupf=NULL;
00121 double* psliceupf=NULL;
00122 double* pslicelof=NULL;
00123 double* psliceupthresf=NULL;
00124 double* pslicelothresf=NULL;
00125
00126 double* ypos=NULL;
00127
00128
00129 double* edgelo=NULL;
00130 double* edgeup=NULL;
00131 double* sliclo=NULL;
00132 double* slicup=NULL;
00133
00134 double max=0;
00135 int img_starty=0;
00136 int img_endy=0;
00137 int i=0;
00138 int is_ifu=0;
00139 int sx=0;
00140 int sy=0;
00141 int rad=6;
00142 int y=0;
00143 int llx=0;
00144 int urx=0;
00145 int nrows=0;
00146 int k=0;
00147 int num=0;
00148 int nsel=0;
00149
00150 cpl_polynomial* sliclopoly=NULL;
00151 cpl_polynomial* slicuppoly=NULL;
00152 cpl_polynomial* edgelopoly=NULL;
00153 cpl_polynomial* edgeuppoly=NULL;
00154
00155
00156 check(name=cpl_frame_get_filename(frame));
00157 check(mflat=cpl_image_load(name,CPL_TYPE_FLOAT,0,0));
00158
00159
00160
00161
00162
00163
00164 check(sx=cpl_image_get_size_x(mflat));
00165 check(sy=cpl_image_get_size_y(mflat));
00166
00167 if(strcmp(method,"sobel") == 0) {
00168 check(filter_x=xsh_sobel_lx(mflat));
00169 } else {
00170 check(filter_x=xsh_scharr_x(mflat));
00171 }
00172
00173
00174
00175 check(max=cpl_image_get_max(filter_x));
00176 check(cpl_image_divide_scalar(filter_x,max));
00177 check(cpl_image_abs(filter_x));
00178
00179 if(xsh_debug_level_get() >= XSH_DEBUG_LEVEL_MEDIUM) {
00180 if(strcmp(method,"sobel") == 0) {
00181 check(cpl_image_save(filter_x,"sobel_lx_n.fits",CPL_BPP_IEEE_FLOAT,
00182 NULL,CPL_IO_DEFAULT));
00183 } else {
00184 check(cpl_image_save(filter_x,"scharr_x_n.fits",CPL_BPP_IEEE_FLOAT,
00185 NULL,CPL_IO_DEFAULT));
00186 }
00187 }
00188
00189 nrows=sy*list->size;
00190 check(tbl=cpl_table_new(nrows));
00191 check(cpl_table_new_column(tbl,"ORDER",CPL_TYPE_INT));
00192 check(cpl_table_new_column(tbl,"ABSORDER",CPL_TYPE_INT));
00193
00194 check(cpl_table_new_column(tbl,"CENTERX",CPL_TYPE_DOUBLE));
00195 check(cpl_table_new_column(tbl,"CENTERY",CPL_TYPE_DOUBLE));
00196 check(cpl_table_new_column(tbl,"EDGELOX",CPL_TYPE_DOUBLE));
00197 check(cpl_table_new_column(tbl,"EDGEUPX",CPL_TYPE_DOUBLE));
00198
00199 check(cpl_table_fill_column_window_int(tbl,"ORDER",0,nrows,-1));
00200 check(cpl_table_fill_column_window_int(tbl,"ABSORDER",0,nrows,-1));
00201
00202 check(cpl_table_fill_column_window_double(tbl,"CENTERX",0,nrows,-1));
00203 check(cpl_table_fill_column_window_double(tbl,"CENTERY",0,nrows,-1));
00204 check(cpl_table_fill_column_window_double(tbl,"EDGELOX",0,nrows,-1));
00205 check(cpl_table_fill_column_window_double(tbl,"EDGEUPX",0,nrows,-1));
00206
00207
00208 check(porder=cpl_table_get_data_int(tbl,"ORDER"));
00209 check(pabsorder=cpl_table_get_data_int(tbl,"ABSORDER"));
00210
00211 check(pcenterx=cpl_table_get_data_double(tbl,"CENTERX"));
00212 check(pcentery=cpl_table_get_data_double(tbl,"CENTERY"));
00213 check(pedgelo=cpl_table_get_data_double(tbl,"EDGELOX"));
00214 check(pedgeup=cpl_table_get_data_double(tbl,"EDGEUPX"));
00215
00216
00217 is_ifu = xsh_instrument_get_mode( instrument ) == XSH_MODE_IFU ;
00218 if ( is_ifu){
00219 xsh_msg( "Detect Order Edges in IFU mode");
00220 check(cpl_table_new_column(tbl,"SLICELOX",CPL_TYPE_DOUBLE));
00221 check(cpl_table_new_column(tbl,"SLICEUPX",CPL_TYPE_DOUBLE));
00222 check(cpl_table_new_column(tbl,"SLICELOTHRESFX",CPL_TYPE_DOUBLE));
00223 check(cpl_table_new_column(tbl,"SLICEUPTHRESFX",CPL_TYPE_DOUBLE));
00224
00225 check(pslicelothresf=cpl_table_get_data_double(tbl,"SLICELOTHRESFX"));
00226 check(psliceupthresf=cpl_table_get_data_double(tbl,"SLICEUPTHRESFX"));
00227 check(cpl_table_fill_column_window_double(tbl,"SLICELOTHRESFX",0,nrows,-1));
00228 check(cpl_table_fill_column_window_double(tbl,"SLICEUPTHRESFX",0,nrows,-1));
00229
00230 check(pslicelo=cpl_table_get_data_double(tbl,"SLICELOX"));
00231 check(psliceup=cpl_table_get_data_double(tbl,"SLICEUPX"));
00232 check(cpl_table_fill_column_window_double(tbl,"SLICELOX",0,nrows,-1));
00233 check(cpl_table_fill_column_window_double(tbl,"SLICEUPX",0,nrows,-1));
00234
00235 }
00236 else {
00237 xsh_msg( "Detect Order Edges in SLIT mode");
00238 }
00239
00240
00241 for(i=0; i< list->size; i++){
00242 porder[k]=list->list[i].order;
00243 pabsorder[k]=list->list[i].absorder;
00244
00245 img_starty = list->list[i].starty/list->bin_y;
00246 img_endy=list->list[i].endy/list->bin_y;
00247
00248
00249
00250
00251
00252
00253 if((list->list[i].starty == -999) ||
00254 (list->list[i].endy==-999)){
00255 xsh_msg("PROBLEMS");
00256 method="fixed";
00257 break;
00258 }
00259
00260 xsh_msg_dbg_medium("start=%d end=%d",img_starty,img_endy);
00261 for(y=img_starty; y<img_endy; y++){
00262 double xl = 0, xu = 0, xc=0, xsl=0, xsu=0;
00263 cpl_size px, py;
00264 int yb=y*list->bin_y;
00265
00266 check( xu = xsh_order_list_eval_int( list, list->list[i].edguppoly, y));
00267 check( xl = xsh_order_list_eval_int( list, list->list[i].edglopoly, y));
00268
00269 llx=(int)xl-rad;
00270 urx=(int)xl+rad;
00271 if((llx>0) && (urx< sx)) {
00272
00273 check( cpl_image_get_maxpos_window( filter_x, llx, y, urx, y, &px, &py));
00274 pedgelo[k] = px;
00275 pedgelo[k]*=list->bin_x;
00276 porder[k]=list->list[i].order;
00277 pabsorder[k]=list->list[i].absorder;
00278 }
00279
00280 llx=(int)xu-rad;
00281 urx=(int)xu+rad;
00282
00283 if((llx>0) && (urx< sx)) {
00284
00285 check( cpl_image_get_maxpos_window( filter_x, llx,y,urx,y, &px, &py));
00286 pedgeup[k] = px;
00287 pedgeup[k]*=list->bin_x;
00288 porder[k]=list->list[i].order;
00289 pabsorder[k]=list->list[i].absorder;
00290 }
00291
00292
00293 check( xc = xsh_order_list_eval( list, list->list[i].cenpoly, y));
00294 pcenterx[k]=xc*list->bin_y;
00295 pcentery[k]=yb;
00296
00297
00298 if ( is_ifu) {
00299 if ( list->list[i].sliclopoly != NULL ) {
00300 check(xsl=cpl_polynomial_eval_1d(list->list[i].sliclopoly,yb,NULL));
00301
00302 xsl /= list->bin_x;
00303 pslicelothresf[k] = xsl;
00304 llx=(int)xsl-rad;
00305 urx=(int)xsl+rad;
00306
00307 if((llx>0) && (urx)< sx) {
00308
00309
00310 check( cpl_image_get_maxpos_window( filter_x, llx, y, urx, y, &px, &py));
00311 pslicelo[k]=px;
00312 pslicelo[k]*=list->bin_x;
00313 porder[k]=list->list[i].order;
00314 pabsorder[k]=list->list[i].absorder;
00315 }
00316 }
00317
00318 if ( list->list[i].slicuppoly != NULL ) {
00319 check(xsu=cpl_polynomial_eval_1d(list->list[i].slicuppoly,yb,NULL));
00320 xsu /= list->bin_x;
00321 psliceupthresf[k] = xsu;
00322 llx=(int)xsu-rad;
00323 urx=(int)xsu+rad;
00324
00325 if((llx>0) && (urx)< sx) {
00326
00327
00328 check( cpl_image_get_maxpos_window( filter_x, llx, y, urx, y, &px, &py));
00329 psliceup[k]=px;
00330 psliceup[k]*=list->bin_x;
00331 porder[k]=list->list[i].order;
00332 pabsorder[k]=list->list[i].absorder;
00333 }
00334 }
00335
00336
00337 }
00338
00339 k++;
00340 }
00341
00342 }
00343
00344 check(num=cpl_table_and_selected_int(tbl,"ORDER",CPL_GREATER_THAN,0));
00345 check(res=cpl_table_extract_selected(tbl));
00346
00347
00348 xsh_free_table(&tbl);
00349 cpl_table_select_all(res);
00350
00351
00352 check(cpl_table_new_column(res,"EDGELOFX",CPL_TYPE_DOUBLE));
00353 check(cpl_table_new_column(res,"EDGEUPFX",CPL_TYPE_DOUBLE));
00354 check(cpl_table_fill_column_window_double(res,"EDGELOFX",0,num,-1));
00355 check(cpl_table_fill_column_window_double(res,"EDGEUPFX",0,num,-1));
00356
00357 check(cpl_table_new_column(res,"EDGELO_RESX",CPL_TYPE_DOUBLE));
00358 check(cpl_table_new_column(res,"EDGEUP_RESX",CPL_TYPE_DOUBLE));
00359 check(cpl_table_fill_column_window_double(res,"EDGELO_RESX",0,num,-1));
00360 check(cpl_table_fill_column_window_double(res,"EDGEUP_RESX",0,num,-1));
00361
00362 check(pedgelof=cpl_table_get_data_double(res,"EDGELOFX"));
00363 check(pedgeupf=cpl_table_get_data_double(res,"EDGEUPFX"));
00364 check(pedgelo_resx=cpl_table_get_data_double(res,"EDGELO_RESX"));
00365 check(pedgeup_resx=cpl_table_get_data_double(res,"EDGEUP_RESX"));
00366
00367 if ( is_ifu ) {
00368 check(cpl_table_new_column(res,"SLICELOFX",CPL_TYPE_DOUBLE));
00369 check(cpl_table_new_column(res,"SLICEUPFX",CPL_TYPE_DOUBLE));
00370
00371 check(cpl_table_fill_column_window_double(res,"SLICELOFX",0,num,-1));
00372 check(cpl_table_fill_column_window_double(res,"SLICEUPFX",0,num,-1));
00373 check(pslicelof=cpl_table_get_data_double(res,"SLICELOFX"));
00374 check(psliceupf=cpl_table_get_data_double(res,"SLICEUPFX"));
00375
00376 check(cpl_table_new_column(res,"SLICELO_RESX",CPL_TYPE_DOUBLE));
00377 check(cpl_table_new_column(res,"SLICEUP_RESX",CPL_TYPE_DOUBLE));
00378 check(cpl_table_fill_column_window_double(res,"SLICELO_RESX",0,num,-1));
00379 check(cpl_table_fill_column_window_double(res,"SLICEUP_RESX",0,num,-1));
00380 check(pslicelo_resx=cpl_table_get_data_double(res,"SLICELO_RESX"));
00381 check(psliceup_resx=cpl_table_get_data_double(res,"SLICEUP_RESX"));
00382
00383
00384 }
00385
00386 k=0;
00387 for(i=0; i< list->size; i++){
00388
00389
00390
00391
00392
00393
00394 if((list->list[i].starty == -999) ||
00395 (list->list[i].endy==-999)){
00396 xsh_msg("PROBLEMS");
00397 method="fixed";
00398 break;
00399 }
00400
00401 check(nsel=cpl_table_and_selected_int(res,"ABSORDER",CPL_EQUAL_TO,
00402 list->list[i].absorder));
00403 if(nsel>0) {
00404 check(tbl=cpl_table_extract_selected(res));
00405
00406 check(ypos=cpl_table_get_data_double(tbl,"CENTERY"));
00407 check(vypos = cpl_vector_wrap( nsel, ypos ));
00408
00409
00410 check(edgelo=cpl_table_get_data_double(tbl,"EDGELOX"));
00411 check(edgeup=cpl_table_get_data_double(tbl,"EDGEUPX"));
00412
00413 check(vedgelo = cpl_vector_wrap( nsel, edgelo ));
00414 check(vedgeup = cpl_vector_wrap( nsel, edgeup ));
00415
00416 check( xsh_free_polynomial( &edgelopoly));
00417 check(edgelopoly=xsh_polynomial_fit_1d_create( vypos, vedgelo,
00418 list->list[i].pol_degree,
00419 NULL));
00420
00421 check( xsh_free_polynomial( &edgeuppoly));
00422 check(edgeuppoly=xsh_polynomial_fit_1d_create( vypos, vedgeup,
00423 list->list[i].pol_degree,
00424 NULL));
00425
00426
00427
00428
00429
00430 check( xsh_free_polynomial( &list->list[i].edglopoly));
00431 check(list->list[i].edglopoly=xsh_polynomial_fit_1d_create( vypos, vedgelo,
00432 list->list[i].pol_degree,
00433 NULL));
00434
00435 check( xsh_free_polynomial( &list->list[i].edguppoly));
00436 check(list->list[i].edguppoly=xsh_polynomial_fit_1d_create( vypos, vedgeup,
00437 list->list[i].pol_degree,
00438 NULL));
00439
00440
00441
00442 for(y=0; y< nsel; y++){
00443 check( pedgelof[k+y] = cpl_polynomial_eval_1d(edgelopoly,ypos[y],NULL));
00444 check( pedgeupf[k+y] = cpl_polynomial_eval_1d(edgeuppoly,ypos[y],NULL));
00445
00446 pedgelo_resx[k+y] = pedgelof[k+y] - edgelo[y];
00447 pedgeup_resx[k+y] = pedgeupf[k+y] - edgeup[y];
00448
00449 }
00450 check( cpl_vector_unwrap(vedgeup )) ;
00451 check( cpl_vector_unwrap(vedgelo )) ;
00452
00453
00454 if ( is_ifu ) {
00455 check(sliclo=cpl_table_get_data_double(tbl,"SLICELOX"));
00456 check(slicup=cpl_table_get_data_double(tbl,"SLICEUPX"));
00457 check( vslicup = cpl_vector_wrap( nsel, slicup )) ;
00458 check( vsliclo = cpl_vector_wrap( nsel, sliclo )) ;
00459
00460 check( xsh_free_polynomial( &sliclopoly));
00461 check(sliclopoly=xsh_polynomial_fit_1d_create( vypos, vsliclo,
00462 list->list[i].pol_degree,
00463 NULL));
00464
00465 check( xsh_free_polynomial( &slicuppoly));
00466 check(slicuppoly=xsh_polynomial_fit_1d_create( vypos, vslicup,
00467 list->list[i].pol_degree,
00468 NULL));
00469
00470
00471
00472
00473 check( xsh_free_polynomial( &list->list[i].sliclopoly));
00474 check(list->list[i].sliclopoly=xsh_polynomial_fit_1d_create( vypos, vsliclo,
00475 list->list[i].pol_degree,
00476 NULL));
00477
00478 check( xsh_free_polynomial( &list->list[i].slicuppoly));
00479 check(list->list[i].slicuppoly=xsh_polynomial_fit_1d_create( vypos, vslicup,
00480 list->list[i].pol_degree,
00481 NULL));
00482
00483
00484
00485 for(y=0; y< nsel; y++){
00486 check( pslicelof[k+y] = cpl_polynomial_eval_1d(sliclopoly,ypos[y],NULL));
00487 check( psliceupf[k+y] = cpl_polynomial_eval_1d(slicuppoly,ypos[y],NULL));
00488 pslicelo_resx[k+y] = pslicelof[k+y] - sliclo[y];
00489 psliceup_resx[k+y] = psliceupf[k+y] - slicup[y];
00490 }
00491 check( cpl_vector_unwrap(vslicup )) ;
00492 check( cpl_vector_unwrap(vsliclo )) ;
00493 }
00494 k+=nsel;
00495 xsh_free_table(&tbl);
00496 cpl_vector_unwrap(vypos );
00497
00498 }
00499 cpl_table_select_all(res);
00500 }
00501
00502 if(xsh_debug_level_get() >= XSH_DEBUG_LEVEL_MEDIUM) {
00503
00504
00505 if ( instrument->mode == XSH_MODE_IFU) {
00506 if (instrument->arm != XSH_ARM_UVB) {
00507 check(name_o=XSH_GET_TAG_FROM_ARM(XSH_MEASURE_FLAT_IFU_EDGES,instrument));
00508 } else {
00509 if(instrument->lamp == XSH_LAMP_QTH) {
00510 check(name_o=XSH_MEASURE_FLAT_QTH_IFU_EDGES_UVB);
00511 } else {
00512 check(name_o=XSH_MEASURE_FLAT_D2_IFU_EDGES_UVB);
00513 }
00514 }
00515 } else {
00516 if (instrument->arm != XSH_ARM_UVB) {
00517 check(name_o=XSH_GET_TAG_FROM_ARM(XSH_MEASURE_FLAT_SLIT_EDGES,instrument));
00518 } else {
00519 if(instrument->lamp == XSH_LAMP_QTH) {
00520 check(name_o=XSH_MEASURE_FLAT_QTH_SLIT_EDGES_UVB);
00521 } else {
00522 check(name_o=XSH_MEASURE_FLAT_D2_SLIT_EDGES_UVB);
00523 }
00524 }
00525 }
00526 sprintf(name_t,"%s%s",name_o,".fits");
00527 check(plist=cpl_propertylist_load(name,0));
00528 check(cpl_table_save(res,plist,NULL,name_t,CPL_IO_DEFAULT));
00529 }
00530
00531 cleanup:
00532 check( xsh_free_polynomial( &sliclopoly));
00533 check( xsh_free_polynomial( &slicuppoly));
00534 check( xsh_free_polynomial( &edgelopoly));
00535 check( xsh_free_polynomial( &edgeuppoly));
00536 xsh_free_propertylist(&plist);
00537 xsh_free_image(&mflat);
00538 xsh_free_image(&filter_x);
00539 xsh_free_table(&tbl);
00540 return res;
00541
00542 }
00543
00544
00545
00566 static void xsh_eval_y_avg_chunk(xsh_pre* pre, int xc, int yc,
00567 int chunk_y_hsize, int x_hsize, const int decode_bp, double *flux, double *noise)
00568 {
00569 int nx, ny;
00570 float *data = NULL, *errs = NULL;
00571 int *qual = NULL;
00572 int res_size=0;
00573 int i,j;
00574 int nb_good_pixels=0;
00575
00576
00577 XSH_ASSURE_NOT_NULL( pre);
00578 XSH_ASSURE_NOT_NULL( flux);
00579 XSH_ASSURE_NOT_NULL( noise);
00580
00581 nx = pre->nx;
00582 ny = pre->ny;
00583 res_size = x_hsize*2+1;
00584
00585 XSH_ASSURE_NOT_ILLEGAL( xc-x_hsize >= 0);
00586 XSH_ASSURE_NOT_ILLEGAL( xc+x_hsize < nx);
00587 XSH_ASSURE_NOT_ILLEGAL( yc-chunk_y_hsize >= 0);
00588 XSH_ASSURE_NOT_ILLEGAL( yc+chunk_y_hsize < ny);
00589
00590
00591 check( data = cpl_image_get_data_float( pre->data));
00592 check( errs = cpl_image_get_data_float( pre->errs));
00593 check( qual = cpl_image_get_data_int( pre->qual));
00594
00595 for(i=xc-x_hsize; i <= (xc+x_hsize); i++){
00596 for(j=yc-chunk_y_hsize; j < (yc+chunk_y_hsize); j++){
00597 if ( (qual[j*nx+i] & decode_bp) == 0){
00598 nb_good_pixels++;
00599 flux[i-xc+x_hsize]+=data[j*nx+i];
00600 noise[i-xc+x_hsize]+=errs[j*nx+i]*errs[j*nx+i];
00601 }
00602 }
00603 }
00604 for(i=0; i< res_size; i++){
00605 flux[i]/=nb_good_pixels;
00606 noise[i]= sqrt(noise[i])/nb_good_pixels;
00607 }
00608
00609 cleanup:
00610 return;
00611 }
00612
00625 static void
00626 xsh_detect_max_y( xsh_order_list* list,
00627 int chunk_hsize,
00628 cpl_polynomial *cen_poly,
00629 int cen_start,
00630 int cen_end,
00631 xsh_pre *pre,
00632 const int decode_bp,
00633 int* maxy)
00634 {
00635 int y;
00636 float flux_max;
00637 int *qual = NULL;
00638
00639
00640 XSH_ASSURE_NOT_NULL( cen_poly);
00641 XSH_ASSURE_NOT_NULL( pre);
00642 XSH_ASSURE_NOT_NULL( maxy);
00643 XSH_ASSURE_NOT_ILLEGAL( cen_start >=1);
00644 XSH_ASSURE_NOT_ILLEGAL( cen_end <= pre->ny);
00645
00646 check( qual = cpl_image_get_data_int( pre->qual));
00647 flux_max = 0.0;
00648 for(y=cen_start+chunk_hsize; y<= cen_end-chunk_hsize; y++){
00649 int x;
00650 double flux, noise;
00651
00652 check( x = xsh_order_list_eval_int( list, cen_poly, y));
00653 check( xsh_eval_y_avg_chunk( pre, x-1, y-1, chunk_hsize, 0,decode_bp,
00654 &flux, &noise));
00655 if ( ((qual[x-1+(y-1)*pre->nx] & decode_bp) == 0)
00656 && (flux > flux_max) ){
00657
00658 flux_max = flux;
00659 *maxy = y;
00660 }
00661 }
00662
00663 cleanup:
00664 return;
00665 }
00688 static void
00689 xsh_detect_edges( xsh_order_list* list,
00690 int y,
00691 cpl_polynomial *cen_poly,
00692 xsh_pre* pre,
00693 int window_hsize,
00694 double* res_flux,
00695 double* res_noise,
00696 int chunk_hsize,
00697 double min_snr,
00698 double flux_frac,
00699 int min_size_x,
00700 const int decode_bp,
00701 int *x_min,
00702 float *x_cen_sdivn,
00703 int *xup,
00704 int *xlow,
00705 float *min_low,
00706 float *min_up)
00707 {
00708 int x = 0;
00709 float min = 0.0, noise = 0.0, sdivn = 0.0;
00710 float threshold = 0.0;
00711 int j = 0, xmin = 0,xmax = 0;
00712 int window_size;
00713 int pos_min=0;
00714 int min_order_size_x =0;
00715
00716
00717 XSH_ASSURE_NOT_NULL( cen_poly);
00718 XSH_ASSURE_NOT_NULL( pre);
00719 XSH_ASSURE_NOT_NULL( res_flux);
00720 XSH_ASSURE_NOT_NULL( res_noise);
00721
00722 XSH_ASSURE_NOT_NULL( x_min);
00723 XSH_ASSURE_NOT_NULL( x_cen_sdivn);
00724 XSH_ASSURE_NOT_NULL( xup);
00725 XSH_ASSURE_NOT_NULL( xlow);
00726 XSH_ASSURE_NOT_NULL( min_low);
00727 XSH_ASSURE_NOT_NULL( min_up);
00728
00729 window_size = window_hsize*2+1;
00730 min_order_size_x = floor(min_size_x/2.0);
00731
00732 check( x = xsh_order_list_eval_int( list, cen_poly, y));
00733 xmin = x-window_hsize;
00734 *x_min= xmin;
00735 xmax = x+window_hsize;
00736
00737
00738 if (xmin >= 1 && xmax <= pre->nx) {
00739 check( xsh_eval_y_avg_chunk( pre, x-1, y-1, chunk_hsize, window_hsize,decode_bp,
00740 res_flux, res_noise));
00741
00742 noise = res_noise[window_hsize+1];
00743 sdivn = res_flux[window_hsize+1]/noise;
00744 *x_cen_sdivn = sdivn;
00745
00746 if ( sdivn > min_snr ){
00747
00748
00749
00750 min = res_flux[window_hsize+1];
00751 for(j = window_hsize+1; j<window_size; j++) {
00752 if (min > res_flux[j]){
00753 min = res_flux[j];
00754 }
00755 }
00756
00757 threshold = (res_flux[window_hsize+1]-min) * flux_frac;
00758
00759 j = window_hsize+1;
00760 while( (j < window_size-1) && ((res_flux[j]-min) >= threshold)){
00761 j++;
00762 }
00763 *xup = j;
00764 *min_up = min;
00765
00766 if ( (res_flux[j]-min) >= threshold){
00767 xsh_msg("WARNING UP edge not detected. Increase window size");
00768 *x_cen_sdivn = -1;
00769 }
00770
00771
00772 min = res_flux[window_hsize+1];
00773 pos_min = window_hsize+1;
00774
00775 for(j=0; j<=window_hsize+1; j++) {
00776 if (min > res_flux[j]){
00777 min = res_flux[j];
00778 pos_min = j;
00779 }
00780 }
00781
00782 threshold = (res_flux[window_hsize+1]-min) * flux_frac;
00783 j = window_hsize+1;
00784 while( (j > 0) && ( (res_flux[j]-min) >= threshold)){
00785 j--;
00786 }
00787 *xlow = (double)j;
00788 *min_low = min;
00789 if ( (res_flux[j]-min) >= threshold){
00790 xsh_msg("WARNING LOW edge not detected. Increase window size");
00791 *x_cen_sdivn = -1;
00792 }
00793 if ( (*xup-*xlow) < min_size_x){
00794 xsh_msg_dbg_medium("y %d : Size of order in cross dispersion "\
00795 "to small %d < %d", y, *xup-*xlow, min_size_x);
00796 *x_cen_sdivn = -1;
00797 }
00798 }
00799 else{
00800 xsh_msg_dbg_medium("y %d Invalid s/n : %f > %f", y, sdivn, min_snr);
00801 }
00802 }
00803 else{
00804 xsh_msg_dbg_medium("y %d Invalid xmin %d or xmax %d", y, xmin, xmax);
00805 *x_cen_sdivn = -1;
00806 }
00807 cleanup:
00808 return;
00809 }
00810
00811 static void xsh_detect_slitlet_ratio( double* res_flux, int window_hsize,
00812 double min_low, double min_up, int xlow, int xup,
00813 double *ratio_low, double *ratio_up)
00814 {
00815 double avg_cen = 0., avg_lo=0., avg_up=0.;
00816 int j;
00817 int size;
00818 int center;
00819
00820
00821
00822 XSH_ASSURE_NOT_NULL( res_flux);
00823 XSH_ASSURE_NOT_NULL( ratio_up);
00824 XSH_ASSURE_NOT_NULL( ratio_low);
00825
00826 size = (xup-xlow)/3;
00827
00828 center = window_hsize+1;
00829
00830 for( j=center-HALF_SLIC_WINDOW;
00831 j <= center+HALF_SLIC_WINDOW ; j++ ){
00832 avg_cen += res_flux[j];
00833 }
00834 avg_cen /= (HALF_SLIC_WINDOW*2)+1;
00835
00836
00837 center = window_hsize+1-size;
00838 for( j=center-HALF_SLIC_WINDOW;
00839 j <= center+HALF_SLIC_WINDOW ; j++ ){
00840 avg_lo += res_flux[j];
00841 }
00842 avg_lo /= (HALF_SLIC_WINDOW*2)+1;
00843
00844
00845 center = window_hsize+1+size;
00846 for( j=center-HALF_SLIC_WINDOW;
00847 j <= center+HALF_SLIC_WINDOW ; j++ ){
00848 avg_up += res_flux[j];
00849 }
00850 avg_up /= (HALF_SLIC_WINDOW*2)+1;
00851
00852 *ratio_up = (avg_up-min_up)/(avg_cen-min_up);
00853 *ratio_low = (avg_lo-min_low)/(avg_cen-min_low);
00854 cleanup:
00855 return;
00856 }
00873 cpl_frame* xsh_detect_order_edge( cpl_frame *frame,
00874 cpl_frame *cen_order_tab_frame,
00875 xsh_detect_order_param *detectorder_par,
00876 xsh_instrument *instrument)
00877 {
00878
00879 cpl_frame * result = NULL;
00880
00881 xsh_pre* pre = NULL;
00882 xsh_order_list* list = NULL;
00883 double* positions = NULL;
00884 double* uppervalues = NULL;
00885 double* lowervalues = NULL;
00886 double * slicup = NULL ;
00887 double * sliclow = NULL ;
00888 cpl_vector* xpos = NULL;
00889 cpl_vector* upper = NULL;
00890 cpl_vector* lower = NULL;
00891 cpl_vector * vslicup = NULL ;
00892 cpl_vector * vsliclow = NULL ;
00893
00894
00895 float* data = NULL;
00896 int y = 0;
00897 int size = 0,i = 0, k=0;
00898 int is_ifu = 0 ;
00899 char *fname = NULL ;
00900 const char * tag = NULL ;
00901 const char * arm_name = NULL ;
00902
00903 int window_size, min_order_size_x, chunk_hsize, window_hsize;
00904 double min_snr, slitlet_up_factor, slitlet_low_factor;
00905 double *res_flux = NULL, *res_noise = NULL;
00906 double flux_frac;
00907 int fixed_slice = 0;
00908 const char* method=NULL;
00909
00910 cpl_table* tab_edges_res=NULL;
00911 int bad_points=0;
00912
00913
00914 XSH_ASSURE_NOT_NULL(frame);
00915 XSH_ASSURE_NOT_NULL(cen_order_tab_frame);
00916 XSH_ASSURE_NOT_NULL(detectorder_par);
00917
00918 window_hsize = detectorder_par->search_window_hsize;
00919 window_size = window_hsize*2+1;
00920 min_order_size_x = detectorder_par->min_order_size_x ;
00921
00922
00923 flux_frac = detectorder_par->flux_thresh;
00924 min_snr = detectorder_par->min_sn;
00925 slitlet_low_factor = detectorder_par->slitlet_low_factor;
00926 slitlet_up_factor = detectorder_par->slitlet_up_factor;
00927 chunk_hsize = detectorder_par->chunk_hsize;
00928 fixed_slice = detectorder_par->fixed_slice;
00929 method=detectorder_par->method;
00930 assure(window_size > 0,CPL_ERROR_ILLEGAL_INPUT,
00931 "window_size=%d (window_size=window_hsize*2+1) < 0 "
00932 "parameter detectorder-edges-search-window-half-size=%d "
00933 "may have been set to a too large value",
00934 window_size,window_hsize);
00935
00936
00937 xsh_msg_dbg_medium("Parameters");
00938 xsh_msg_dbg_medium(" Window: %d,flux-Thresh: %.2f, S/N: %.2f",
00939 window_size, flux_frac, min_snr);
00940 xsh_msg_dbg_medium(" min-size-x: %d, Slitlet-low-factor: %.2f\
00941 Slitlet-up-factor: %.2f chunk-half-size %d",
00942 min_order_size_x, slitlet_low_factor, slitlet_up_factor, chunk_hsize);
00943 xsh_msg_dbg_medium(" fixed-slice = %s", BOOLEAN_TO_STRING(fixed_slice));
00944
00945
00946 check( pre = xsh_pre_load( frame, instrument));
00947
00948
00949 check(list = xsh_order_list_load( cen_order_tab_frame, instrument));
00950
00951 assure(min_order_size_x > 0 &&
00952 min_order_size_x < (floor) (pre->nx / list->size),
00953 CPL_ERROR_ILLEGAL_INPUT,
00954 "parameter detectorder-min-order-size-x=%d "
00955 "has been set to a too small or too large value", min_order_size_x);
00956
00957
00958
00959
00960
00961 XSH_REGDEBUG("binning %dx%d",list->bin_x, list->bin_y);
00962
00963 xsh_free_propertylist( &(list->header));
00964 XSH_NEW_PROPERTYLIST( list->header);
00965
00966
00967 check(data = cpl_image_get_data_float(pre->data));
00968 arm_name = xsh_instrument_arm_tostring( instrument ) ;
00969
00970 is_ifu = xsh_instrument_get_mode( instrument ) == XSH_MODE_IFU ;
00971 if ( is_ifu){
00972 xsh_msg( "Detect Order Edges in IFU mode");
00973 }
00974 else {
00975 xsh_msg( "Detect Order Edges in SLIT mode");
00976 }
00977
00978
00979 XSH_CALLOC( positions,double,pre->ny*list->bin_y);
00980 XSH_CALLOC( uppervalues,double,pre->ny*list->bin_y);
00981 XSH_CALLOC( lowervalues,double,pre->ny*list->bin_y);
00982 XSH_CALLOC( res_flux, double, window_size);
00983 XSH_CALLOC( res_noise, double, window_size);
00984
00985 XSH_CALLOC( slicup,double,pre->ny*list->bin_y);
00986 XSH_CALLOC( sliclow,double,pre->ny*list->bin_y);
00987
00988
00989
00990
00991 for(i=0; i< list->size; i++){
00992 int mean_size_up=0;
00993 int mean_size_low=0;
00994 int mean_size_slic_low=0;
00995 int mean_size_slic_up=0;
00996 double ratio_low=0., ratio_up=0.;
00997 int bin_starty;
00998 int bin_endy;
00999 int decode_bp=instrument->decode_bp;
01000 size = 0;
01001 check( bin_starty = xsh_order_list_get_starty( list, i));
01002 check( bin_endy = xsh_order_list_get_endy( list, i));
01003 xsh_msg_dbg_medium( "***** Order %d (%d-->%d)", i, bin_starty, bin_endy);
01004
01005
01006 if (is_ifu && !fixed_slice ){
01007 int max_y=0;
01008 float min_lo = 0.0,min_up=0.0, sdivn = 0.0;
01009 int xmin = 0;
01010 int upperval = 0,lowval=0;
01011
01012 check( xsh_detect_max_y( list, chunk_hsize, list->list[i].cenpoly,
01013 bin_starty, bin_endy, pre,decode_bp,
01014 &max_y));
01015 xsh_msg("Max at %d",max_y);
01016
01017
01018 check( xsh_detect_edges( list, max_y,
01019 list->list[i].cenpoly,
01020 pre,
01021 window_hsize, res_flux, res_noise,
01022 chunk_hsize,
01023 min_snr,
01024 flux_frac, min_order_size_x,decode_bp,
01025 &xmin, &sdivn, &upperval, &lowval,
01026 &min_lo, &min_up));
01027
01028
01029 xsh_msg_dbg_medium("order size at center %d", upperval-lowval);
01030 check( xsh_detect_slitlet_ratio( res_flux, window_hsize,
01031 min_lo, min_up, lowval, upperval, &ratio_low, &ratio_up));
01032 xsh_msg_dbg_medium("ratio low %f up %f", ratio_low, ratio_up);
01033 }
01034
01035 bad_points=0;
01036 for(y=1+chunk_hsize; y<= pre->ny-chunk_hsize; y++){
01037
01038
01039 float min_lo = 0.0,min_up=0.0, sdivn = 0.0;
01040 int j = 0, xmin = 0;
01041 int upperval = 0,lowval=0;
01042 double xslicup=0.;
01043 double xsliclow=0.;
01044 check( xsh_detect_edges( list, y, list->list[i].cenpoly,
01045 pre,
01046 window_hsize, res_flux, res_noise,
01047 chunk_hsize,
01048 min_snr,
01049 flux_frac, min_order_size_x,decode_bp,
01050 &xmin, &sdivn, &upperval, &lowval,
01051 &min_lo, &min_up));
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061 if ( (sdivn > min_snr) && is_ifu ) {
01062 if (fixed_slice){
01063 int slit_size =ceil( (upperval - lowval+1)/3.0);
01064
01065 xsliclow = lowval+slit_size;
01066 xslicup = upperval-slit_size;
01067 }
01068 else{
01069 double avg = 0. ;
01070 double max ;
01071
01072 for( j=window_hsize+1-HALF_SLIC_WINDOW;
01073 j <= window_hsize+1+HALF_SLIC_WINDOW ; j++ ) {
01074 avg += res_flux[j];
01075 }
01076 avg /= (HALF_SLIC_WINDOW*2)+1 ;
01077 max = (avg-min_up) * slitlet_up_factor * ratio_up;
01078 xslicup = 0 ;
01079 for ( j=window_hsize+1 ; j <= upperval ; j++){
01080 if ( (res_flux[j]-min_up) < max ) {
01081
01082 xslicup = j-1 ;
01083 break ;
01084 }
01085 }
01086 xsliclow = 0;
01087 max = (avg-min_lo) * slitlet_low_factor * ratio_low;
01088 for ( j=window_hsize+1; j >= lowval; j--){
01089 if ( (res_flux[j]-min_lo) < max ) {
01090
01091 xsliclow = j+1 ;
01092 break ;
01093 }
01094 }
01095 }
01096 }
01097
01098 if( (sdivn > min_snr) && (upperval - lowval) >= min_order_size_x) {
01099 if ( !is_ifu ) {
01100 positions[size] = y;
01101 lowervalues[size] = lowval+xmin;
01102 uppervalues[size] = upperval+xmin;
01103 mean_size_low += window_hsize+1-lowval;
01104 mean_size_up += upperval-(window_hsize+1);
01105 size++;
01106 }
01107 else if ( xslicup != 0 && xsliclow != 0) {
01108
01109 positions[size] = y;
01110 lowervalues[size] = lowval+xmin;
01111 uppervalues[size] = upperval+xmin;
01112 mean_size_low += window_hsize+1-lowval;
01113 mean_size_up += upperval-(window_hsize+1);
01114 slicup[size] = xslicup+xmin;
01115 sliclow[size] = xsliclow+xmin;
01116 mean_size_slic_low += window_hsize+1-xsliclow;
01117 mean_size_slic_up += xslicup-(window_hsize+1);
01118 size++;
01119 }
01120 } else {
01121 xsh_msg_dbg_medium("Order %d edge detection: sn (%g) < sn_threshold (%g) or order size (%d) < min order size min (%d)",
01122 list->list[i].absorder,sdivn,min_snr,
01123 (upperval - lowval),min_order_size_x);
01124 bad_points++;
01125 }
01126
01127
01128 }
01129 if(bad_points>1) {
01130 xsh_msg_dbg_medium("Order %d edge detection summary: ",
01131 list->list[i].absorder);
01132 xsh_msg_dbg_medium("%d out of %d not matching user defined min S/N (%g), or minimum order size",bad_points,pre->ny-2*chunk_hsize,min_snr);
01133 }
01134
01135
01136 if(size <= 3 && detectorder_par->qc_mode == CPL_TRUE) {
01137
01138
01139
01140
01141
01142
01143 list->list[i].starty=-999;
01144 list->list[i].endy=-999;
01145 xsh_msg("PROBLEMS");
01146
01147 method="fixed";
01148 xsh_msg_error("%d out of %d not matching user defined min S/N (%g), or minimum order size",bad_points,pre->ny-2*chunk_hsize,min_snr);
01149
01150 break;
01151
01152 } else {
01153 assure(size > 3,CPL_ERROR_ILLEGAL_INPUT,
01154 "can't fit polynomial solution of degree %d to only %d data points "
01155 "parameter detectorder-edges-search-window-half-size=%d "
01156 "may have a too small value "
01157 "or parameter detectorder-min-sn value may be too big "
01158 "or parameter detectorder-chunk-half-size=%d "
01159 "may have been set to a too small or too large large value "
01160 "or parameter detectorder-edges-flux-thresh=%g "
01161 "may have been set to a too small or too large large value",
01162 list->list[i].pol_degree,size,window_hsize,chunk_hsize,flux_frac);
01163
01164 list->list[i].starty = positions[0]-chunk_hsize;
01165 list->list[i].endy = positions[size-1]+chunk_hsize;
01166 }
01167
01168 mean_size_up = mean_size_up/size;
01169 mean_size_low = mean_size_low/size;
01170 xsh_msg_dbg_medium("mean size up %d low %d", mean_size_up, mean_size_low);
01171 if ( is_ifu ) {
01172 mean_size_slic_low=mean_size_slic_low/size;
01173 mean_size_slic_up=mean_size_slic_up/size;
01174 }
01175 xsh_msg_dbg_medium("mean size Slitlets up %d low %d",
01176 mean_size_slic_up, mean_size_slic_low);
01177
01178 xsh_msg_dbg_medium("starty %d endy %d",list->list[i].starty, list->list[i].endy);
01179
01180 for(y=1; y < list->list[i].starty; y++){
01181 int x_center;
01182
01183 check( x_center = xsh_order_list_eval_int( list,
01184 list->list[i].cenpoly, y));
01185 positions[size] = y;
01186 lowervalues[size] = x_center-mean_size_low;
01187 uppervalues[size] = x_center+mean_size_up;
01188 if (is_ifu){
01189 sliclow[size] = x_center-mean_size_slic_low;
01190 slicup[size] = x_center+mean_size_slic_up;
01191 }
01192 size++;
01193 }
01194 for(y=list->list[i].endy+1; y<= pre->ny; y++){
01195 int x_center;
01196
01197 check( x_center = xsh_order_list_eval_int( list,
01198 list->list[i].cenpoly, y));
01199 positions[size] = y;
01200 lowervalues[size] = x_center-mean_size_low;
01201 uppervalues[size] = x_center+mean_size_up;
01202
01203 if (is_ifu){
01204 sliclow[size] = x_center-mean_size_slic_low;
01205 slicup[size] = x_center+mean_size_slic_up;
01206 }
01207 size++;
01208 }
01209
01210
01211 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_MEDIUM) {
01212 FILE* debug_out = NULL;
01213 int newi = 0;
01214 char openmode[2];
01215
01216 if(i==0){
01217 sprintf(openmode,"w");
01218 }
01219 else{
01220 sprintf(openmode,"a");
01221 }
01222 XSH_NAME_LAMP_MODE_ARM( fname, "detect_edges_points", ".log", instrument);
01223 debug_out = fopen( fname,openmode);
01224
01225 for(newi=0;newi<size;newi++){
01226 fprintf(debug_out,"%f %f %f\n",lowervalues[newi],
01227 uppervalues[newi], positions[newi]);
01228 }
01229 fclose(debug_out);
01230 if ( is_ifu ) {
01231 XSH_NAME_LAMP_MODE_ARM( fname, "orders_slic", ".log", instrument);
01232 debug_out = fopen( fname,openmode);
01233
01234 for(newi=0 ; newi<size ; newi++){
01235 fprintf(debug_out,"%f %f %f\n", sliclow[newi],
01236 slicup[newi], positions[newi]);
01237 }
01238 fclose(debug_out);
01239 }
01240 }
01241
01242 list->list[i].starty *= list->bin_y;
01243 list->list[i].endy *= list->bin_y;
01244
01245 for(k=0; k < size; k++){
01246 lowervalues[k] *= list->bin_x;
01247 uppervalues[k] *= list->bin_x;
01248 positions[k] *= list->bin_y;
01249 if (is_ifu){
01250 sliclow[k] *= list->bin_x;
01251 slicup[k] *= list->bin_x;
01252 }
01253 }
01254
01255 check(xpos = cpl_vector_wrap(size,positions));
01256 check(upper = cpl_vector_wrap(size,uppervalues));
01257 check(lower = cpl_vector_wrap(size,lowervalues));
01258 if ( is_ifu ) {
01259 check( vslicup = cpl_vector_wrap( size, slicup )) ;
01260 check( vsliclow = cpl_vector_wrap( size, sliclow )) ;
01261 }
01262
01263
01264 check( xsh_free_polynomial( &list->list[i].edguppoly));
01265 check( list->list[i].edguppoly =
01266 xsh_polynomial_fit_1d_create( xpos, upper,
01267 list->list[i].pol_degree, NULL));
01268
01269 check( xsh_free_polynomial( &list->list[i].edglopoly));
01270 check( list->list[i].edglopoly =
01271 xsh_polynomial_fit_1d_create( xpos, lower,
01272 list->list[i].pol_degree, NULL));
01273
01274 if ( is_ifu) {
01275 check( xsh_free_polynomial( &list->list[i].sliclopoly));
01276 check( list->list[i].sliclopoly =
01277 xsh_polynomial_fit_1d_create( xpos, vsliclow,
01278 list->list[i].pol_degree, NULL));
01279 check( xsh_free_polynomial( &list->list[i].slicuppoly));
01280 check( list->list[i].slicuppoly =
01281 xsh_polynomial_fit_1d_create( xpos, vslicup,
01282 list->list[i].pol_degree,NULL));
01283 }
01284
01285
01286 if (xsh_debug_level_get() >= XSH_DEBUG_LEVEL_MEDIUM) {
01287 FILE* debug_out = NULL;
01288 int newi = 0;
01289 char openmode[2];
01290 int img_endy=0, img_starty;
01291
01292
01293 img_starty = list->list[i].starty/list->bin_y;
01294 img_endy=list->list[i].endy/list->bin_y;
01295 if(i==0){
01296 sprintf(openmode,"w");
01297 }
01298 else{
01299 sprintf(openmode,"a");
01300 }
01301
01302 XSH_NAME_LAMP_MODE_ARM( fname, "poly_edges", ".log", instrument);
01303 debug_out = fopen( fname ,openmode);
01304
01305 for(y=img_starty; y<img_endy; y++){
01306 float xl = 0, xu = 0 ;
01307
01308 check( xu = xsh_order_list_eval_int( list,
01309 list->list[i].edguppoly, y));
01310 check( xl = xsh_order_list_eval_int( list,
01311 list->list[i].edglopoly, y));
01312 fprintf(debug_out,"%f %f %d %d\n",xl, xu, y, i);
01313 }
01314 fclose(debug_out);
01315
01316 XSH_NAME_LAMP_MODE_ARM( fname, "poly_cen", ".log", instrument);
01317 debug_out = fopen( fname,openmode);
01318
01319 for(y=img_starty; y<img_endy; y++){
01320 float x = 0;
01321
01322 check( x = xsh_order_list_eval( list,
01323 list->list[i].cenpoly, y));
01324 fprintf(debug_out,"%f %d %f\n",x,y, i);
01325 }
01326 fclose(debug_out);
01327
01328
01329 if ( is_ifu ) {
01330 XSH_NAME_LAMP_MODE_ARM( fname, "orders_slic", ".log", instrument);
01331 debug_out = fopen( fname,openmode);
01332
01333 for(newi=0 ; newi<size ; newi++){
01334 fprintf(debug_out,"%f %f %f %d\n",
01335 cpl_vector_get(vsliclow,newi),cpl_vector_get(vslicup,newi),
01336 cpl_vector_get(xpos,newi), i);
01337 }
01338 fclose(debug_out);
01339 XSH_NAME_LAMP_MODE_ARM( fname, "poly_slic", ".log", instrument);
01340 debug_out = fopen( fname,openmode);
01341
01342 for(y=img_starty;y<img_endy;y++){
01343 float xl = 0, xu = 0;
01344
01345 check( xu = xsh_order_list_eval( list,
01346 list->list[i].edguppoly, y));
01347 check( xl = xsh_order_list_eval( list,
01348 list->list[i].edglopoly, y));
01349 fprintf(debug_out,"%f %f %d %d\n",xl, xu, y, i);
01350 }
01351 fclose(debug_out);
01352 }
01353 }
01354
01355
01356 check( xsh_unwrap_vector(&xpos));
01357 check( xsh_unwrap_vector(&upper));
01358 check( xsh_unwrap_vector(&lower));
01359 if ( is_ifu){
01360 check( xsh_unwrap_vector( &vslicup));
01361 check( xsh_unwrap_vector( &vsliclow));
01362 }
01363
01364
01365
01366 }
01367
01368 if(strcmp(method,"fixed") != 0) {
01369 check(tab_edges_res=xsh_compute_flat_edges(frame,list,instrument,method));
01370 }
01371
01372 tag = XSH_GET_TAG_FROM_LAMP( XSH_ORDER_TAB_EDGES,instrument);
01373 XSH_REGDEBUG( "tag %s", tag);
01374 XSH_NAME_LAMP_MODE_ARM( fname, XSH_ORDER_TAB_EDGES, ".fits", instrument);
01375 check( result = xsh_order_list_save( list, instrument, fname, tag, pre->ny*list->bin_y));
01376
01377 if(strcmp(method,"fixed") != 0) {
01378 check(cpl_table_save(tab_edges_res, NULL, NULL, fname, CPL_IO_EXTEND));
01379 }
01380
01381 xsh_msg_dbg_high( " Created %s", fname);
01382
01383 cleanup:
01384 if ( cpl_error_get_code () != CPL_ERROR_NONE) {
01385 xsh_unwrap_vector( &xpos);
01386 xsh_unwrap_vector( &upper);
01387 xsh_unwrap_vector( &lower);
01388 xsh_unwrap_vector( &vslicup);
01389 xsh_unwrap_vector( &vsliclow);
01390 }
01391 xsh_pre_free( &pre);
01392 xsh_free_table( &tab_edges_res );
01393
01394 xsh_order_list_free( &list);
01395 XSH_FREE( positions);
01396 XSH_FREE( uppervalues);
01397 XSH_FREE( lowervalues);
01398 XSH_FREE( res_flux);
01399 XSH_FREE( res_noise);
01400 XSH_FREE( slicup);
01401 XSH_FREE( sliclow);
01402 XSH_FREE( fname);
01403 return result;
01404 }
01405
01406
01407