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