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 #include <xsh_cpl_size.h>
00031
00038
00041
00042
00043
00044 #include <math.h>
00045 #include <xsh_utils.h>
00046 #include <xsh_drl.h>
00047 #include <xsh_pfits.h>
00048 #include <xsh_error.h>
00049 #include <xsh_msg.h>
00050 #include <xsh_badpixelmap.h>
00051 #include <xsh_utils_image.h>
00052 #include <xsh_data_order.h>
00053 #include <xsh_data_grid.h>
00054
00055
00056
00057
00058
00059
00060
00061
00062 static cpl_error_code
00063 subtract_background(cpl_image *image, cpl_image *background_im,
00064 const polynomial *background_pol);
00065
00066 static cpl_image*
00067 xsh_background_poly(cpl_image* image, xsh_grid* grid,xsh_background_param* back_par);
00068
00069
00070
00071
00072
00073
00092 cpl_frame*
00093 xsh_subtract_bias( cpl_frame *frame,
00094 cpl_frame *bias,
00095 xsh_instrument* instr,
00096 const char * type,
00097 const int pre_overscan_corr,
00098 const int save_tmp)
00099 {
00100
00101 cpl_frame * result = NULL;
00102
00103 xsh_pre* xframe = NULL;
00104 xsh_pre* xbias = NULL;
00105
00106 char resultname[256];
00107 int binx=0;
00108 int biny=0;
00109 char tag[80];
00110 double avg=0;
00111
00112
00113 XSH_ASSURE_NOT_NULL( frame);
00114 XSH_ASSURE_NOT_NULL( bias);
00115 XSH_ASSURE_NOT_NULL( instr);
00116
00117
00118 check( xframe = xsh_pre_load( frame, instr));
00119 check( xbias = xsh_pre_load( bias, instr));
00120
00121 check(binx=xsh_pfits_get_binx(xbias->data_header));
00122 check(biny=xsh_pfits_get_biny(xbias->data_header));
00123
00124 if(pre_overscan_corr==0) {
00125 check( xsh_pre_subtract( xframe, xbias));
00126 } else {
00127 check(avg=cpl_image_get_mean(xbias->data));
00128 check(xsh_pre_subtract_scalar(xbias,avg));
00129 check( xsh_pre_subtract( xframe, xbias));
00130 }
00131
00132 sprintf(tag,"%sON_%s",type,xsh_instrument_arm_tostring (instr));
00133
00134
00135
00136
00137 sprintf(resultname,"%s.fits",tag);
00138
00139 check( xsh_pfits_set_pcatg( xframe->data_header, tag));
00140 check( result = xsh_pre_save ( xframe, resultname, tag,save_tmp));
00141 check( cpl_frame_set_tag( result,tag));
00142
00143 cleanup:
00144 if ( cpl_error_get_code() != CPL_ERROR_NONE) {
00145 xsh_free_frame( &result);
00146 }
00147 xsh_pre_free( &xframe);
00148 xsh_pre_free( &xbias);
00149 return result;
00150 }
00151
00152
00153
00154
00163
00164 cpl_frameset* xsh_subtract_nir_on_off(cpl_frameset* on, cpl_frameset* off,
00165 xsh_instrument* instr){
00166
00167 cpl_frameset * result = NULL;
00168
00169 cpl_frame* res = NULL;
00170
00171 char resultname[256];
00172 int i = 0, size_on = 0, size_off = 0;
00173
00174
00175 XSH_ASSURE_NOT_NULL(on);
00176 XSH_ASSURE_NOT_NULL(off);
00177 XSH_ASSURE_NOT_NULL(instr);
00178
00179 check(size_on = cpl_frameset_get_size(on));
00180 check(size_off = cpl_frameset_get_size(off));
00181
00182 XSH_ASSURE_NOT_ILLEGAL(size_on == size_off);
00183
00184 XSH_NEW_FRAMESET(result);
00185
00186
00187 for ( i=0; i< size_on; i++) {
00188 cpl_frame* on_f = NULL;
00189 cpl_frame* off_f = NULL;
00190
00191 check( on_f = cpl_frameset_get_frame(on,i));
00192 check( off_f = cpl_frameset_get_frame(off,i));
00193 sprintf(resultname, "ON-OFF_%d.fits",i);
00194
00195 check( res = xsh_subtract_dark(on_f, off_f, resultname, instr));
00196 check(cpl_frameset_insert(result, res));
00197 xsh_add_temporary_file(resultname);
00198 }
00199
00200 cleanup:
00201 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00202 xsh_free_frameset(&result);
00203 xsh_free_frame(&res);
00204 }
00205 return result;
00206 }
00207
00216
00217 cpl_frame * xsh_subtract_dark(cpl_frame * frame,cpl_frame * dark,
00218 const char* filename, xsh_instrument* instr)
00219 {
00220
00221 cpl_frame * result = NULL;
00222
00223 xsh_pre* xframe = NULL;
00224 xsh_pre* xdark = NULL;
00225
00226 double exptime = 0.0;
00227 const char* tag=NULL;
00228
00229
00230 XSH_ASSURE_NOT_NULL(frame);
00231 XSH_ASSURE_NOT_NULL(dark);
00232 XSH_ASSURE_NOT_NULL(filename);
00233 XSH_ASSURE_NOT_NULL(instr);
00234
00235
00236 check(xframe = xsh_pre_load(frame, instr));
00237 check(xdark = xsh_pre_load(dark, instr));
00238
00239
00240 if (xsh_instrument_get_arm(instr) != XSH_ARM_NIR) {
00241 exptime = xframe->exptime;
00242 assure(exptime > 0,CPL_ERROR_ILLEGAL_INPUT,
00243 "EXPTIME must be greater than 0 : %f",exptime);
00244 check(xsh_pre_multiply_scalar(xdark,exptime));
00245 }
00246
00247 check(xsh_pre_subtract(xframe,xdark));
00248
00249 tag=cpl_frame_get_tag(frame);
00250 check(result = xsh_pre_save (xframe, filename, tag,0));
00251 check(cpl_frame_set_tag (result,tag));
00252
00253 cleanup:
00254 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00255 xsh_free_frame(&result);
00256 }
00257 xsh_pre_free(&xframe);
00258 xsh_pre_free(&xdark);
00259 return result;
00260 }
00261
00262
00281
00282 cpl_frame* xsh_subtract_background( cpl_frame *frame,
00283 cpl_frame *edges_order_tab_frame,
00284 xsh_background_param *background_par,
00285 xsh_instrument *instr,
00286 const char *prefix,
00287 cpl_frame **grid_frame,
00288 cpl_frame **backg_frame,
00289 const int save_tmp)
00290 {
00291
00292 cpl_frame *result = NULL;
00293
00294 xsh_order_list *orderlist = NULL;
00295 xsh_pre *pre = NULL;
00296 xsh_grid *grid = NULL;
00297 cpl_image *background_img = NULL;
00298
00299 float *data = NULL;
00300 int incr = 0, i = 0, y =0, lost = 0;
00301 int grid_size_x =0, grid_size_y =0;
00302 double *tab = NULL;
00303 int x = 0;
00304 float x1 = 0, x2 =0, dx = 0;
00305 double medflux = 0.0;
00306 char *fname = NULL;
00307 char *pcatg = NULL;
00308 cpl_image *background_smo = NULL;
00309 int smooth_x = -1;
00310 int smooth_y = -1;
00311 cpl_propertylist *plist = NULL;
00312 cpl_table *grid_tbl = NULL;
00313 cpl_image* ima=NULL;
00314
00315
00316 XSH_ASSURE_NOT_NULL( instr);
00317 XSH_ASSURE_NOT_NULL( frame);
00318 XSH_ASSURE_NOT_NULL( edges_order_tab_frame);
00319 XSH_ASSURE_NOT_NULL( background_par);
00320
00321
00322
00323
00324
00325
00326 assure(background_par->sampley > 0,CPL_ERROR_ILLEGAL_INPUT,
00327 "parameter background-nb-y=%d "
00328 "must be set to a positive value",
00329 background_par->sampley);
00330 XSH_ASSURE_NOT_ILLEGAL(background_par->radius_x >= 0);
00331 XSH_ASSURE_NOT_ILLEGAL(background_par->radius_y >= 0);
00332
00333
00334
00335
00336 check( pre = xsh_pre_load(frame, instr));
00337 check( orderlist = xsh_order_list_load( edges_order_tab_frame, instr));
00338 xsh_order_list_set_bin_x( orderlist, pre->binx);
00339 xsh_order_list_set_bin_y( orderlist, pre->biny);
00340 check( data = cpl_image_get_data_float(pre->data));
00341
00342
00343 grid_size_x = orderlist->size+5;
00344 grid_size_y = background_par->sampley+2;
00345
00346 assure( grid_size_y > 0 && grid_size_y < pre->ny, CPL_ERROR_ILLEGAL_INPUT,
00347 "grid_size_y=%d must be in (0,%d) range "
00348 "parameter background-nb-y=%d "
00349 "may have been set to a too large or too small value",
00350 grid_size_y,pre->ny,background_par->sampley);
00351
00352 smooth_x = background_par->smooth_x;
00353 smooth_y = background_par->smooth_y;
00354
00355 check( grid = xsh_grid_create( grid_size_x*grid_size_y));
00356
00357
00358 assure(background_par->radius_x<pre->nx,CPL_ERROR_ILLEGAL_INPUT,
00359 "parameter -background-radius-x (%d) must be > 0 and < %d",
00360 background_par->radius_x,pre->nx);
00361 assure(background_par->radius_y<pre->ny,CPL_ERROR_ILLEGAL_INPUT,
00362 "parameter -background-radius-y (%d) must be > 0 and < %d",
00363 background_par->radius_y,pre->ny);
00364
00365
00366 XSH_MALLOC(tab,double,(2*background_par->radius_x+1)*(2*background_par->radius_y+1));
00367
00368
00369 incr = (int)ceil((float)pre->ny/(float)background_par->sampley);
00370
00371
00372 for( y=1; y <= (pre->ny-1+incr); y += incr){
00373 int sizey = 2 * background_par->radius_y+1;
00374 int oy = -background_par->radius_y;
00375
00376
00377 if( (y+incr) > pre->ny){
00378 y = pre->ny;
00379 }
00380
00381
00382 if((y == 1) || (y == pre->ny)){
00383 sizey = background_par->radius_y+1;
00384 oy = 0;
00385 }
00386
00387
00388 check( medflux = xsh_pre_data_window_sample_flux_pa(pre,
00389 1, y+oy,
00390 2 * background_par->radius_x+1,
00391 sizey,
00392 tab,
00393 background_par->method,
00394 background_par->min_frac_grid));
00395
00396 if ( xsh_instrument_get_arm(instr) == XSH_ARM_NIR){
00397 if ( (y <= orderlist->list[orderlist->size-2].starty) ||
00398 y >= orderlist->list[orderlist->size-2].endy){
00399
00400 check( xsh_grid_add(grid, 1, y, medflux));
00401 }
00402 }
00403 else{
00404 check( xsh_grid_add(grid, 1, y, medflux));
00405 }
00406
00407
00408 for( i=orderlist->size-1; i > 0; i--){
00409 int starty, endy;
00410
00411 starty = orderlist->list[i-1].starty;
00412 endy = orderlist->list[i-1].endy;
00413
00414
00415 if(starty == -999 ) {
00416
00417
00418
00419
00420
00421 xsh_msg("PROBLEMS");
00422 check(xsh_grid_add(grid, 0, y, 0.));
00423 background_par->method="poly";
00424 lost++;
00425 break;
00426 }
00427
00428 check( x1 = xsh_order_list_eval( orderlist,
00429 orderlist->list[i].edguppoly, y));
00430 check( x2 = xsh_order_list_eval( orderlist,
00431 orderlist->list[i-1].edglopoly, y));
00432
00433 x = floor( (x1+x2)/2.0+0.5);
00434 dx = (x2-x1)/2.0;
00435 xsh_msg_dbg_high("x %d dx %f", x, dx);
00436
00437 if ( xsh_instrument_get_arm(instr) == XSH_ARM_NIR){
00438 if ( y <= starty || y >= endy){
00439 if (x >1 && x <= pre->nx){
00440 check(xsh_grid_add(grid, x, y, 0.0));
00441 }
00442 continue;
00443 }
00444 }
00445
00446 if ( (x > (background_par->radius_x+1)) && ((2*background_par->radius_x+1)< dx)){
00447
00448 check(medflux = xsh_pre_data_window_sample_flux_pa(pre,
00449 x-background_par->radius_x,
00450 y+oy ,
00451 2*background_par->radius_x+1,
00452 sizey,
00453 tab,
00454 background_par->method,
00455 background_par->min_frac_grid));
00456 check( xsh_grid_add(grid, x, y, medflux));
00457 }
00458 else{
00459 lost++;
00460 }
00461
00462 if ( i == (orderlist->size-1)){
00463 check( x2 = xsh_order_list_eval( orderlist,
00464 orderlist->list[i].edglopoly, y));
00465
00466 x = x2-dx;
00467
00468 if(dx<0) {
00469 xsh_msg("Monitor dx=%d x=%g y=%d",x,dx,y);
00470 }
00471
00472
00473 if ( (x > (background_par->radius_x+1)) ){
00474 check(medflux = xsh_pre_data_window_sample_flux_pa(pre,
00475 x-background_par->radius_x,
00476 y+oy ,
00477 2*background_par->radius_x+1,
00478 sizey,
00479 tab,
00480 background_par->method,
00481 background_par->min_frac_grid));
00482 check(xsh_grid_add(grid, x, y, medflux));
00483 }
00484 }
00485 else{
00486 lost++;
00487 }
00488
00489 if ( i == 1){
00490 check( x1 = xsh_order_list_eval( orderlist,
00491 orderlist->list[0].edguppoly, y));
00492
00493 if ( xsh_instrument_get_arm(instr) == XSH_ARM_VIS){
00494 check( x2 = xsh_order_list_eval( orderlist,
00495 orderlist->list[0].edglopoly, y));
00496 x = x1+(x1-x2)*1.2;
00497 }
00498 else{
00499 x=x1+dx;
00500 }
00501 if ( (x > (background_par->radius_x+1)) && (x+background_par->radius_x < pre->nx) ){
00502 check(medflux = xsh_pre_data_window_sample_flux_pa(pre,
00503 x-background_par->radius_x,
00504 y+oy ,
00505 2*background_par->radius_x+1,
00506 sizey,
00507 tab,
00508 background_par->method,
00509 background_par->min_frac_grid));
00510 check(xsh_grid_add(grid, x, y, medflux));
00511 }
00512 else {
00513 lost++;
00514 }
00515 }
00516
00517 }
00518
00519 check(medflux = xsh_pre_data_window_sample_flux_pa(pre,
00520 pre->nx-background_par->radius_x,
00521 y+oy,
00522 background_par->radius_x+1,
00523 sizey,
00524 tab,
00525 background_par->method,
00526 background_par->min_frac_grid));
00527
00528 check(xsh_grid_add(grid, pre->nx, y, medflux));
00529 }
00530
00531 xsh_msg_dbg_low("Nb of lost points %d ",lost);
00532 xsh_msg_dbg_low("Nb of grid points %d ",xsh_grid_get_index(grid));
00533
00534
00535 check( xsh_grid_sort(grid));
00536
00537
00538 if(strcmp(background_par->method,"minimum")==0) {
00539
00540 xsh_msg("generate background image: spline");
00541 check( background_img = cpl_image_new( pre->nx, pre->ny, CPL_TYPE_DOUBLE));
00542 check(xsh_image_fit_spline(background_img, grid));
00543 } else if(strcmp(background_par->method,"median")==0) {
00544
00545 xsh_msg("generate background image: spline");
00546 check( background_img = cpl_image_new( pre->nx, pre->ny, CPL_TYPE_DOUBLE));
00547 check(xsh_image_fit_spline(background_img, grid));
00548 } else if (strcmp(background_par->method,"poly")==0) {
00549 xsh_msg("generate background image: poly");
00550 check(ima=cpl_image_cast(pre->data,CPL_TYPE_DOUBLE));
00551 check( background_img = xsh_background_poly( ima, grid,background_par));
00552 xsh_free_image(&ima);
00553
00554
00555
00556
00557 }
00558
00559 if( smooth_y>0) {
00560 check( background_smo = xsh_image_smooth_mean_y( background_img,
00561 smooth_y));
00562
00563
00564
00565
00566 xsh_free_image( &background_img);
00567 background_img= cpl_image_duplicate( background_smo);
00568 xsh_free_image( &background_smo);
00569 }
00570
00571 if( smooth_x>0) {
00572 check(background_smo = xsh_image_smooth_mean_x( background_img,
00573 smooth_x));
00574
00575
00576
00577
00578 xsh_free_image( &background_img);
00579 background_img = cpl_image_duplicate( background_smo);
00580 xsh_free_image( &background_smo);
00581 }
00582
00583
00584 XSH_NAME_PREFIX_LAMP_MODE_ARM( pcatg, prefix, "_BACK", "", instr);
00585 XSH_NAME_PREFIX_LAMP_MODE_ARM( fname, prefix, "_BACK", ".fits", instr);
00586 plist=cpl_propertylist_new();
00587 check( xsh_pfits_set_pcatg( plist, pcatg));
00588
00589 check( cpl_image_save( background_img, fname, CPL_BPP_IEEE_FLOAT,
00590 plist, CPL_IO_DEFAULT));
00591 if(!save_tmp) {
00592 xsh_add_temporary_file(fname);
00593 }
00594
00595 xsh_free_propertylist( &plist);
00596
00597 check( *backg_frame = xsh_frame_product( fname, pcatg,
00598 CPL_FRAME_TYPE_IMAGE, CPL_FRAME_GROUP_CALIB,
00599 CPL_FRAME_LEVEL_FINAL));
00600
00601
00602 check( cpl_image_subtract( pre->data, background_img));
00603
00604 XSH_FREE( fname);
00605 XSH_FREE( pcatg);
00606 XSH_NAME_PREFIX_LAMP_MODE_ARM( fname, prefix, "_SUB_BACK", ".fits", instr);
00607 XSH_NAME_PREFIX_LAMP_MODE_ARM( pcatg, prefix, "_SUB_BACK", "", instr);
00608
00609 check( xsh_pfits_set_pcatg( pre->data_header, pcatg));
00610 if(save_tmp==0) {
00611 check( result = xsh_pre_save( pre, fname, pcatg, 1));
00612 } else {
00613 check( result = xsh_pre_save( pre, fname, pcatg, 0));
00614 }
00615 check(cpl_frame_set_tag (result, pcatg));
00616 check(cpl_frame_set_type (result, CPL_FRAME_TYPE_IMAGE));
00617 check(cpl_frame_set_group (result, CPL_FRAME_GROUP_CALIB));
00618 check(cpl_frame_set_level (result, CPL_FRAME_LEVEL_FINAL));
00619
00620 grid_tbl = xsh_grid2table(grid);
00621
00622
00623
00624
00625
00626 XSH_FREE( fname);
00627 XSH_FREE( pcatg);
00628 XSH_NAME_PREFIX_LAMP_MODE_ARM( fname, prefix, "_GRID_BACK", ".fits", instr);
00629 XSH_NAME_PREFIX_LAMP_MODE_ARM( pcatg, prefix, "_GRID_BACK", "", instr);
00630
00631 plist = cpl_propertylist_new();
00632 check( xsh_pfits_set_pcatg( plist, pcatg));
00633 check( cpl_table_save( grid_tbl, plist, NULL, fname, CPL_IO_DEFAULT));
00634 xsh_free_propertylist( &plist);
00635 xsh_free_table( &grid_tbl);
00636 if(xsh_instrument_get_arm(instr) == XSH_ARM_UVB) {
00637 xsh_add_temporary_file(fname);
00638 }
00639
00640 if(save_tmp==0) {
00641 xsh_add_temporary_file(fname);
00642 }
00643
00644
00645
00646 *grid_frame =xsh_frame_product(fname,pcatg,CPL_FRAME_TYPE_TABLE,
00647 CPL_FRAME_GROUP_CALIB,CPL_FRAME_LEVEL_FINAL);
00648
00649 cleanup:
00650 if (cpl_error_get_code () != CPL_ERROR_NONE) {
00651 xsh_free_frame(&result);
00652 }
00653 xsh_free_propertylist(&plist);
00654 xsh_order_list_free( &orderlist);
00655 XSH_FREE( tab);
00656 XSH_FREE( fname);
00657 XSH_FREE( pcatg);
00658 xsh_grid_free( &grid);
00659 xsh_pre_free( &pre);
00660 xsh_free_image( &background_img);
00661 return result;
00662 }
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00685
00686
00687 static cpl_error_code
00688 subtract_background(cpl_image *image, cpl_image *background_im,
00689 const polynomial *background_pol)
00690 {
00691 int nx, ny;
00692 int x, y;
00693
00694 double *image_data;
00695 double *background_data = NULL;
00696 passure(image != NULL, " ");
00697
00698
00699
00700
00701 assure(cpl_image_count_rejected(image) == 0,
00702 CPL_ERROR_UNSUPPORTED_MODE, "Input image contains bad pixels");
00703 assure(cpl_image_get_type(image) == CPL_TYPE_DOUBLE,
00704 CPL_ERROR_UNSUPPORTED_MODE,
00705 "Input image is of type %s. double expected",
00706 xsh_tostring_cpl_type(cpl_image_get_type(image)));
00707
00708
00709 image_data = cpl_image_get_data_double(image);
00710 background_data = cpl_image_get_data_double(background_im);
00711
00712 nx = cpl_image_get_size_x(image);
00713 ny = cpl_image_get_size_y(image);
00714
00715 for (y = 1; y <= ny; y++)
00716 {
00717 for (x = 1; x <= nx; x++)
00718 {
00719 double back;
00720 double flux, new_flux;
00721
00722
00723
00724 back = xsh_polynomial_evaluate_2d(background_pol,x,y);
00725
00726
00727 flux = image_data[(x-1) + (y-1) * nx];
00728 new_flux = flux-back;
00729 image_data[(x-1) + (y-1) * nx] = new_flux;
00730 background_data[(x-1)+(y-1)*nx]=xsh_max_double(0, flux - new_flux);
00731
00732 }
00733 }
00734
00735 cleanup:
00736 return cpl_error_get_code();
00737 }
00738
00739 static cpl_image*
00740 xsh_background_poly(cpl_image* image, xsh_grid* grid,xsh_background_param* background_par)
00741 {
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751 polynomial* background=NULL;
00752 double mse=0, rmse=0;
00753 cpl_size total_clipped = 0;
00754 cpl_table* t=NULL;
00755 cpl_image* bkg_img=NULL;
00756
00757 int degx = background_par->poly_deg_x;
00758 int degy = background_par->poly_deg_y;
00759 double kappa = background_par->poly_kappa;
00760 xsh_msg("degx=%d degy=%d kappa=%g",degx,degy,kappa);
00761
00762 check(t=xsh_grid2table(grid));
00763
00764
00765 {
00766 cpl_size n_clipped;
00767 do {
00768 cpl_size deg_xy=(degx + 1)*(degy + 1);
00769 assure( cpl_table_get_nrow(t) > (degx + 1)*(degy + 1),
00770 CPL_ERROR_ILLEGAL_OUTPUT,
00771 "Too few sample points available (%" CPL_SIZE_FORMAT " point(s)) to make the fit "
00772 "(more than %" CPL_SIZE_FORMAT " points needed). "
00773 "Increase number of sample points or increase kappa",
00774 cpl_table_get_nrow(t), deg_xy);
00775
00776
00777 xsh_polynomial_delete(&background);
00778 check_msg( background = xsh_polynomial_regression_2d(
00779 t, "X", "Y", "INT", NULL,
00780 degx, degy, "INTfit", NULL, NULL, &mse,
00781 NULL, NULL, -1, -1),
00782 "Error fitting polynomial");
00783
00784
00785 cpl_table_duplicate_column(t, "Residual", t, "INT");
00786 cpl_table_subtract_columns(t, "Residual", "INTfit");
00787
00788
00789
00790
00791
00792
00793 cpl_table_subtract_scalar(t, "Residual",
00794 cpl_table_get_column_median(t, "Residual"));
00795 rmse = cpl_table_get_column_stdev(t, "Residual");
00796 xsh_msg_dbg_medium("rmse=%g",rmse);
00797
00798 if (kappa > 0)
00799 {
00800 check_msg( n_clipped = xsh_select_table_rows(
00801 t, "Residual", CPL_GREATER_THAN, kappa * rmse),
00802 "Error selecting rows");
00803 }
00804 else
00805 {
00806 n_clipped = 0;
00807 }
00808
00809 total_clipped += n_clipped;
00810
00811 xsh_msg_dbg_medium("RMS = %f. %" CPL_SIZE_FORMAT " of %" CPL_SIZE_FORMAT " points rejected in kappa-sigma clipping",
00812 rmse, n_clipped, cpl_table_get_nrow(t));
00813
00814 cpl_table_erase_selected(t);
00815
00816 if (n_clipped > 0)
00817 {
00818 cpl_table_erase_column(t, "INTfit");
00819 cpl_table_erase_column(t, "Residual");
00820 }
00821
00822 } while (n_clipped > 0);
00823 }
00824
00825
00826
00827
00828
00829 {
00830 double percentage =
00831 100.0 * ( (double)total_clipped ) / (total_clipped + cpl_table_get_nrow(t));
00832
00833 if (kappa > 0) {
00834 xsh_msg_dbg_medium("%" CPL_SIZE_FORMAT " of %" CPL_SIZE_FORMAT " points (%.2f %%) were rejected in "
00835 "kappa-sigma clipping. RMS = %.2f ADU",
00836 total_clipped,
00837 cpl_table_get_nrow(t) + total_clipped,
00838 percentage,
00839 sqrt(mse));
00840 }
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862 }
00863 bkg_img=cpl_image_duplicate(image);
00864
00865 check_msg( subtract_background(image,bkg_img, background),
00866 "Error subtracting background polynomial");
00867
00868 cleanup:
00869 xsh_free_table( &t);
00870 xsh_polynomial_delete(&background);
00871 return bkg_img;
00872
00873 }
00874
00875
00876