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
00039
00042
00043
00044
00045
00046 #include <math.h>
00047 #include <xsh_drl.h>
00048
00049 #include <xsh_badpixelmap.h>
00050 #include <xsh_data_pre.h>
00051 #include <xsh_dfs.h>
00052 #include <xsh_pfits.h>
00053 #include <xsh_error.h>
00054 #include <xsh_msg.h>
00055 #include <xsh_fit.h>
00056
00057 #include <xsh_compute_linearity.h>
00058
00059 #include <cpl.h>
00060
00061 #define USE_CPL_POLYNOMIAL
00062 #define OPTIMIZE_IMAGE_HANDLING
00063
00064
00065
00066
00067
00068
00069
00070
00071
00080 static int
00081 orderCompare (const void *one, const void *two)
00082 {
00083 TIME_FRAME *un = (TIME_FRAME *) one;
00084 TIME_FRAME *deux = (TIME_FRAME *) two;
00085
00086 if (un->exptime <= deux->exptime)
00087 return -1;
00088 else
00089 return 1;
00090 }
00091
00100 static int
00101 onoffCompare (const void *one, const void *two)
00102 {
00103 TIME_FRAME *un = (TIME_FRAME *) one;
00104 TIME_FRAME *deux = (TIME_FRAME *) two;
00105
00106 if (un->on_off <= deux->on_off)
00107 return 1;
00108 else
00109 return -1;
00110 }
00111
00120 cpl_frameset * xsh_subtract_on_off( cpl_frameset *set,
00121 xsh_instrument *instrument )
00122 {
00123 cpl_frame *currOn = NULL, *currOff = NULL;
00124 cpl_frameset * subSet = NULL ;
00125 int nframes, i ;
00126 xsh_pre * subtracted = NULL ;
00127 xsh_pre *preOn = NULL, * preOff = NULL ;
00128
00129
00130
00131
00132 xsh_msg( "===> Subtract on_off" ) ;
00133
00134 nframes = cpl_frameset_get_size( set ) ;
00135 subSet = cpl_frameset_new() ;
00136 assure( subSet != NULL, cpl_error_get_code(),
00137 "Cant create new frameset" ) ;
00138
00139 for( i = 0 ; i<nframes ; i+=2 ) {
00140 const char *filename = NULL;
00141
00142 if ( i == 0 )
00143 currOn = cpl_frameset_get_first (set);
00144 else currOn = cpl_frameset_get_next (set);
00145 assure( currOn != NULL, cpl_error_get_code(),
00146 "Cant get frame of frameset" ) ;
00147 filename = cpl_frame_get_filename (currOn);
00148 xsh_msg_dbg_low( " Subtracting ON : %s", filename ) ;
00149 check_msg( preOn = xsh_pre_load( currOn, instrument ),
00150 "Cant load PRE ON" ) ;
00151
00152 currOff = cpl_frameset_get_next (set);
00153 assure( currOff != NULL, cpl_error_get_code(),
00154 "Cant get frame of frameset" ) ;
00155 filename = cpl_frame_get_filename (currOff);
00156 xsh_msg_dbg_low( " OFF: %s", filename ) ;
00157 check_msg( preOff = xsh_pre_load( currOff, instrument ),
00158 "Cant load PRE frame OFF" ) ;
00159
00160
00161 subtracted = xsh_pre_duplicate(preOn);
00162 xsh_pre_subtract( subtracted, preOff ) ;
00163 assure( subtracted != NULL, cpl_error_get_code(),
00164 "Cant subtract images" ) ;
00165
00166 {
00167 cpl_frame * saved = NULL ;
00168 char outname[128] ;
00169 const char *tag = XSH_LINEARITY_NIR ;
00170
00171 sprintf( outname, "linear_sub_set_%d.fits", i ) ;
00172 saved = xsh_pre_save( subtracted, outname, tag,1 ) ;
00173 assure( saved != NULL, cpl_error_get_code(),
00174 "Cant save subtracted frame" ) ;
00175
00176 check_msg( cpl_frame_set_tag( saved, tag ),
00177 "Cant set frame tag" ) ;
00178
00179 check_msg( cpl_frameset_insert( subSet, saved ),
00180 "Cant insert frame %d into subSet", i ) ;
00181 }
00182
00183 xsh_pre_free( &preOn ) ;
00184 xsh_pre_free( &preOff ) ;
00185 xsh_pre_free( &subtracted ) ;
00186 }
00187
00188 cleanup:
00189 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
00190 xsh_free_frameset( &subSet ) ;
00191 subSet = NULL ;
00192 }
00193 xsh_pre_free( &preOn ) ;
00194 xsh_pre_free( &preOff ) ;
00195 xsh_pre_free( &subtracted ) ;
00196 return subSet ;
00197 }
00198
00212 int
00213 xsh_linear_group_by_exptime (cpl_frameset * raws, xsh_instrument* instrument,
00214 double exp_toler, cpl_frameset ** groupSet)
00215 {
00216
00217
00218
00219
00220
00221 int i, ngroups = 0;
00222 int nframes = 0;
00223 TIME_FRAME *expOrdered = NULL;
00224 TIME_FRAME *porder = NULL;
00225 cpl_frame *current = NULL;
00226 double temps = 0.0;
00227 int onoff = 0;
00228 cpl_frameset **pgrpset = NULL;
00229
00230 nframes = cpl_frameset_get_size (raws);
00231 xsh_msg( "Nb of Frames = %d", nframes ) ;
00232
00233 expOrdered = cpl_malloc (nframes * sizeof (TIME_FRAME));
00234 assure (expOrdered != NULL, cpl_error_get_code (),
00235 "Cant create tempory time+fframe array");
00236
00237 porder = expOrdered;
00238
00239 for ( i = 0 ; i<nframes ; i++, porder++) {
00240 const char *filename;
00241 cpl_propertylist *header;
00242 current = NULL ;
00243
00244 if ( i == 0 ) current = cpl_frameset_get_first (raws);
00245 else current = cpl_frameset_get_next (raws) ;
00246 assure( current != NULL, CPL_ERROR_ILLEGAL_INPUT,
00247 "Cant get Frame #%d from frameset", i ) ;
00248
00249 filename = cpl_frame_get_filename (current);
00250 xsh_msg_dbg_low ("load propertylist file %s\n", filename);
00251 check_msg (header = cpl_propertylist_load (filename, 0),
00252 "Could not load header of file '%s'", filename);
00253
00254
00255 if ( instrument->arm == XSH_ARM_NIR )
00256 porder->exptime = xsh_pfits_get_dit (header);
00257 else porder->exptime = xsh_pfits_get_exptime( header ) ;
00258
00259 porder->frame = current;
00260 if ( instrument->arm == XSH_ARM_NIR ) {
00261 bool onoffs = false;
00262 check(onoffs = xsh_pfits_get_lamp_on_off( header )) ;
00263 if ( onoffs ) porder->on_off = 1;
00264 else porder->on_off = 0 ;
00265 }
00266 xsh_msg_dbg_low ("Exptime[%d] = %lf", i, porder->exptime);
00267 xsh_free_propertylist( &header ) ;
00268 }
00269
00270
00271 xsh_msg_dbg_low ("==== Before qsort");
00272 qsort (expOrdered, nframes, sizeof (TIME_FRAME), orderCompare);
00273
00274
00275 {
00276 TIME_FRAME *first = expOrdered;
00277
00278 int nb = 0 ;
00279
00280 temps = expOrdered->exptime ;
00281
00282 for (i = 0, porder = expOrdered; i < nframes; i++, porder++) {
00283 if ( porder->exptime == temps ) {
00284
00285 nb++ ;
00286 }
00287 else {
00288 xsh_msg_dbg_low( "Call ON/OFF compare (%d frames)", nb ) ;
00289 qsort( first, nb, sizeof (TIME_FRAME), onoffCompare ) ;
00290 first = porder ;
00291 nb = 1 ;
00292 temps = porder->exptime ;
00293 }
00294 }
00295
00296 qsort( first, nb, sizeof (TIME_FRAME), onoffCompare ) ;
00297 }
00298
00299 {
00300 xsh_msg_dbg_low ("==== After qsort");
00301 for (i = 0, porder = expOrdered; i < nframes; i++, porder++)
00302 xsh_msg_dbg_low(" DIT[%d] = %lf (ON/OFF = %d)", i, porder->exptime,
00303 porder->on_off);
00304 }
00305
00306
00307 porder = expOrdered;
00308 temps = porder->exptime;
00309 onoff = porder->on_off ;
00310
00311 pgrpset = groupSet;
00312
00313 xsh_msg_dbg_low ("New frameset 0 starting at Frame 0" );
00314 *pgrpset = cpl_frameset_new ();
00315 check_msg (cpl_frameset_insert (*pgrpset, porder->frame),
00316 "Cant insert frame %d in group %d", i, ngroups);
00317 porder++;
00318 ngroups = 1;
00319 for (i = 1; i < nframes; i++, porder++) {
00320 if (fabs (porder->exptime - temps) > exp_toler ||
00321 porder->on_off != onoff ) {
00322 xsh_msg_dbg_low ("New frameset %d starting at Frame %d", ngroups, i);
00323 ngroups++;
00324 temps = porder->exptime;
00325 onoff = porder->on_off ;
00326 pgrpset++;
00327 *pgrpset = cpl_frameset_new ();
00328 assure( *pgrpset != NULL, cpl_error_get_code(),
00329 "Cant create new framest" ) ;
00330 }
00331 else
00332 xsh_msg_dbg_low ("Same group frame %d - %lf vs %lf", i,
00333 porder->exptime, temps);
00334 xsh_msg_dbg_low ("Add frame %d to frameset group %d", i, ngroups);
00335 check_msg (cpl_frameset_insert (*pgrpset, porder->frame),
00336 "Cant insert frame %d in group %d", i, ngroups);
00337 }
00338
00339 cleanup:
00340 if ( expOrdered != NULL ) cpl_free( expOrdered ) ;
00341 return ngroups;
00342 }
00343
00344 #if !defined(USE_CPL_POLYNOMIAL)
00345
00362 static cpl_imagelist *xsh_imagelist_fit_polynomial( cpl_imagelist * medlist,
00363 double *dit, int nframes,
00364 int nx, int ny,
00365 int degree )
00366 {
00367 int ll, ix, iy ;
00368 cpl_vector * pixvect = NULL;
00369 cpl_vector * temps = NULL;
00370 cpl_polynomial *poly = NULL;
00371 cpl_imagelist *poly_list = NULL,
00373 *final_list = NULL ;
00374 float **pixbuf = NULL ;
00375 cpl_binary **maskbuf = NULL ;
00377 assure( medlist != NULL, CPL_ERROR_NULL_INPUT,
00378 "Input list of median images is NUL !" ) ;
00379
00380 poly_list = cpl_imagelist_new() ;
00381 assure( poly_list != NULL, cpl_error_get_code(),
00382 "Cant create imagelist" ) ;
00383
00384
00385
00386 for ( ll = 0 ; ll<= degree ; ll++ ) {
00387 cpl_image *newone = NULL ;
00388
00389 newone = cpl_image_new( nx, ny, CPL_TYPE_DOUBLE ) ;
00390 assure( newone != NULL, cpl_error_get_code(),
00391 "Cant create new image" ) ;
00392 cpl_imagelist_set( poly_list, newone, ll ) ;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401
00402 xsh_msg( "Polynomial Fit - XSH version - nb of frames: %d", nframes ) ;
00403 #if 1
00404
00405
00406
00407
00408
00409
00410 pixbuf = cpl_malloc( nframes * sizeof(double *) ) ;
00411 assure( pixbuf != NULL, cpl_error_get_code(),
00412 "Cant allocate memory for pixel data" ) ;
00413 maskbuf = cpl_malloc( nframes * sizeof(cpl_binary *) ) ;
00414 assure( maskbuf != NULL, cpl_error_get_code(),
00415 "Cant allocate memory for BPM data" ) ;
00416
00417 for( ll = 0 ; ll < nframes ; ll++ ) {
00418 cpl_image *pcur = cpl_imagelist_get( medlist, ll ) ;
00419
00420 *(pixbuf+ll) = cpl_image_get_data( pcur ) ;
00421 *(maskbuf+ll) = cpl_mask_get_data( cpl_image_get_bpm( pcur ) ) ;
00422 }
00423
00424
00425 for ( iy = 0 ; iy < ny ; iy++ )
00426 for( ix = 0 ; ix < nx ; ix++ ) {
00427 cpl_binary ** pmask = maskbuf ;
00428 float ** cur = pixbuf ;
00429 int npix = 0 ;
00430
00431 pixvect = cpl_vector_new( nframes ) ;
00432 assure( pixvect != NULL, cpl_error_get_code(),
00433 "Cant create PixVect Vector" ) ;
00434 temps = cpl_vector_new( nframes ) ;
00435 assure( temps != NULL, cpl_error_get_code(),
00436 "Cant create Time Vector" ) ;
00437 xsh_msg_dbg_low( "++ Fit one pixel: %d,%d", ix, iy ) ;
00438
00439 for( ll = 0 ; ll<nframes ; ll++ , pmask++, cur++ ) {
00440 double pixval ;
00441 cpl_binary rej ;
00442 float *pcur = *cur ;
00443 cpl_binary *mask = *pmask ;
00444
00445 pixval = *(pcur + (iy*nx) + ix ) ;
00446 rej = *(mask + (iy*nx) + ix ) ;
00447 if ( rej == 0 ) {
00448 cpl_vector_set( temps, ll, *(dit+ll) ) ;
00449 cpl_vector_set( pixvect, ll, pixval ) ;
00450 npix++ ;
00451 }
00452 }
00453 xsh_msg_dbg_low( " Nb of pixels: %d", npix ) ;
00454
00455 if ( npix != nframes ) {
00456 cpl_vector_set_size( temps, npix ) ;
00457 cpl_vector_set_size( pixvect, npix ) ;
00458 }
00459
00460 poly = xsh_polynomial_fit_1d_create( temps, pixvect,
00461 degree, NULL ) ;
00462 xsh_free_vector( &temps ) ;
00463 xsh_free_vector( &pixvect ) ;
00464 assure( poly != NULL, CPL_ERROR_ILLEGAL_INPUT,
00465 "Error calling xsh_polynomial_fit_1d_create" ) ;
00466
00467 for( ll = 0 ; ll <= degree ; ll++ ) {
00468 cpl_image_set( cpl_imagelist_get( poly_list, ll ),
00469 ix+1, iy+1, cpl_polynomial_get_coeff( poly, &ll ) ) ;
00470 }
00471 xsh_free_polynomial( &poly ) ;
00472 }
00473 #else
00474 for ( iy = 1 ; iy <= ny ; iy++ )
00475 for( ix = 1 ; ix <= nx ; ix++ ) {
00476 int npix = 0 ;
00477
00478 pixvect = cpl_vector_new( nframes ) ;
00479 assure( pixvect != NULL, cpl_error_get_code(),
00480 "Cant create PixVect Vector" ) ;
00481 temps = cpl_vector_new( nframes ) ;
00482 assure( temps != NULL, cpl_error_get_code(),
00483 "Cant create Time Vector" ) ;
00484 xsh_msg_dbg_low( "++ Fit one pixel: %d,%d", ix, iy ) ;
00485 for( ll = 0 ; ll < nframes ; ll++ ) {
00486 double pixval ;
00487 int rej ;
00488 cpl_image *cur = NULL ;
00489 cur = cpl_imagelist_get( medlist, ll ) ;
00490 assure( cur != NULL, cpl_error_get_code(),
00491 "Cant get image #%d from liste" ) ;
00492
00493 pixval = cpl_image_get( cur, ix, iy, &rej ) ;
00494 xsh_msg_dbg_low( " Pixval: %lf", pixval ) ;
00495 if ( rej == 0 ) {
00496 cpl_vector_set( temps, ll, *(dit+ll) ) ;
00497 cpl_vector_set( pixvect, ll, pixval ) ;
00498 npix++ ;
00499 }
00500 }
00501 xsh_msg_dbg_low( " Nb of pixels: %d", npix ) ;
00502
00503 if ( npix != nframes ) {
00504 cpl_vector_set_size( temps, npix ) ;
00505 cpl_vector_set_size( pixvect, npix ) ;
00506 }
00507
00508 poly = xsh_polynomial_fit_1d_create( temps, pixvect,
00509 degree, NULL ) ;
00510 assure( poly != NULL, cpl_error_get_code(),
00511 "Error calling xsh_polynomial_fit_1d_create" ) ;
00512 xsh_free_vector( &temps ) ;
00513 xsh_free_vector( &pixvect ) ;
00514
00515
00516 for( ll = 0 ; ll <= degree ; ll++ ) {
00517 cpl_image_set( cpl_imagelist_get( poly_list, ll ),
00518 ix, iy, cpl_polynomial_get_coeff( poly, &ll ) ) ;
00519 }
00520 xsh_free_polynomial( &poly ) ;
00521 }
00522 #endif
00523
00524 final_list = cpl_imagelist_duplicate( poly_list ) ;
00525
00526 cleanup:
00527 xsh_free_imagelist( &poly_list ) ;
00528 xsh_free_polynomial( &poly ) ;
00529 xsh_free_vector( &temps ) ;
00530 xsh_free_vector( &pixvect ) ;
00531 xsh_free_imagelist( &poly_list ) ;
00532 cpl_free( pixbuf ) ;
00533 cpl_free( maskbuf ) ;
00534
00535 return final_list ;
00536 }
00537 #endif
00538
00549 cpl_frame *
00550 xsh_compute_linearity (cpl_frameset * medSet, xsh_instrument *instrument,
00551 xsh_clipping_param * lin_clipping,const int decode_bp)
00552 {
00553 cpl_image * resBpmap = NULL ;
00554 int nframes = 0 ;
00555 cpl_frame * resFrame = NULL ;
00556 int i = 0, ix = 0, iy = 0, nx = 0, ny = 0;
00557 int nbad, totbad = 0 ;
00558 cpl_imagelist *medList = NULL ;
00559 cpl_imagelist *bpmapList = NULL ;
00560 cpl_frame *current = NULL ;
00561 cpl_propertylist *bpmap_header = NULL ;
00563 double *Dit = NULL;
00564 #if defined(USE_CPL_POLYNOMIAL)
00565 cpl_vector * v_dit = NULL ;
00566
00567 #endif
00568 cpl_imagelist * polyList = NULL ;
00571 xsh_msg( "==> xsh_compute_linearity" ) ;
00572 assure (medSet != NULL, CPL_ERROR_ILLEGAL_INPUT, "Frameset is NULL");
00573 nframes = cpl_frameset_get_size (medSet);
00574
00575
00576
00577
00578
00579 medList = cpl_imagelist_new() ;
00580 assure( medList != NULL, cpl_error_get_code(),
00581 "Cant create medList" ) ;
00582 bpmapList = cpl_imagelist_new() ;
00583 assure( bpmapList != NULL, cpl_error_get_code(),
00584 "Cant create bpmapList" ) ;
00585
00586 current = cpl_frameset_get_first( medSet ) ;
00587 assure( current != NULL, cpl_error_get_code(),
00588 "Cant get current frame" ) ;
00589
00590
00591 Dit = cpl_malloc( nframes * sizeof(double) ) ;
00592 assure( Dit != NULL, cpl_error_get_code(),
00593 "Cant allocate Dit array" ) ;
00594 #if defined(USE_CPL_POLYNOMIAL)
00595 v_dit = cpl_vector_wrap( nframes, Dit ) ;
00596 assure( v_dit != NULL, cpl_error_get_code(),
00597 "Cant create v_dit vector" ) ;
00598 #endif
00599 xsh_msg( "Loop over all frames" ) ;
00600
00601 for( i = 0 ; i<nframes ; i++ ) {
00602 const char *filename ;
00603 cpl_propertylist *header = NULL ;
00604 cpl_image * curimage = NULL ;
00605 cpl_image *curbpmap = NULL ;
00606
00607 filename = cpl_frame_get_filename(current);
00608 assure( filename != NULL, cpl_error_get_code(),
00609 "Cant get filename from frame %d", i ) ;
00610
00611 curimage = cpl_image_load( filename, XSH_PRE_DATA_TYPE, 0, 0 ) ;
00612 assure( curimage != NULL, cpl_error_get_code(),
00613 "Cant get curimage from frame %d", i ) ;
00614
00615 cpl_imagelist_set( medList, curimage, i ) ;
00616 header = cpl_propertylist_load( filename, 0 ) ;
00617 assure( header != NULL, cpl_error_get_code(),
00618 "Cant get header of frame %d", i ) ;
00619
00620 xsh_msg_dbg_low( " Getting EXPTIME/DIT[%d]", i ) ;
00621
00622 if ( instrument->arm == XSH_ARM_NIR )
00623 *(Dit+i) = xsh_pfits_get_dit( header ) ;
00624 else *(Dit+i) = xsh_pfits_get_exptime( header ) ;
00625 cpl_propertylist_delete( header ) ;
00626
00627 if ( i == 0 ) {
00628 nx = cpl_image_get_size_x( curimage ) ;
00629 ny = cpl_image_get_size_y( curimage ) ;
00630 }
00631
00632
00633 xsh_msg_dbg_low( " Loading BpMap[%d]", i ) ;
00634
00635 curbpmap = cpl_image_load( filename, CPL_TYPE_INT, 0, 2 ) ;
00636 assure( curbpmap != NULL, cpl_error_get_code(),
00637 "Cant load bpmap frame #%d", i ) ;
00638 cpl_imagelist_set( bpmapList, curbpmap, i ) ;
00639 if ( i == 0 ) {
00640 bpmap_header = cpl_propertylist_load( filename, 2 ) ;
00641 assure( bpmap_header != NULL, cpl_error_get_code(),
00642 "Cant get bpmap header of frame %d", i ) ;
00643 }
00644 current = cpl_frameset_get_next( medSet ) ;
00645 xsh_msg_dbg_low( "-- Next Frame" ) ;
00646 }
00647
00648
00649 xsh_msg( "Collapsing BpMaps's" ) ;
00650
00651 resBpmap = xsh_bpmap_collapse_bpmap_create ( bpmapList,decode_bp ) ;
00652 assure( resBpmap != NULL, cpl_error_get_code(),
00653 "Cant create collapsed bpmap" ) ;
00654
00655 xsh_msg_dbg_low( "Freeing bpmapList" ) ;
00656 xsh_free_imagelist( &bpmapList ) ;
00657
00658
00659 for( i = 0 ; i<nframes ; i++ ) {
00660 xsh_set_image_cpl_bpmap ( cpl_imagelist_get( medList, i ),
00661 resBpmap, decode_bp ) ;
00662 }
00663
00664 #if defined(JUST_FOR_TEST)
00665
00666 {
00667 int ll = 0;
00668 cpl_vector * pixvect = NULL;
00669 cpl_vector * temps = NULL;
00670 cpl_polynomial *poly = NULL;
00671
00672
00673 pixvect = cpl_vector_new( nframes ) ;
00674 temps = cpl_vector_new( nframes ) ;
00675
00676 xsh_msg( "===== Fit polynome (1) sur valeurs moyennes des med frames" ) ;
00677 for( ll = 0 ; ll < nframes ; ll++ ) {
00678 cpl_image *cur = NULL ;
00679 cur = cpl_imagelist_get( medList, ll ) ;
00680 xsh_msg( " #%d: mean=%lf, DIT=%lf", ll, cpl_image_get_mean( cur ),
00681 *(Dit+ll) ) ;
00682 cpl_vector_set( pixvect, ll, cpl_image_get_mean( cur ) ) ;
00683 cpl_vector_set( temps, ll, *(Dit+ll) ) ;
00684 }
00685 poly = xsh_polynomial_fit_1d_create( temps, pixvect,
00686 LINEAR_POLYNOMIAL_DEGREE,
00687 NULL ) ;
00688 cpl_polynomial_dump( poly, stderr ) ;
00689 xsh_msg( "===== Ca ne ressemble en rien a ce qu'on obtient dans le" ) ;
00690 xsh_msg( " fit general. Bizarre n'est-il pas ?" ) ;
00691 for ( ll = 0 ; ll<=LINEAR_POLYNOMIAL_DEGREE ; ll++ )
00692 xsh_msg( ">>>>> %d : %lf\n", ll,
00693 cpl_polynomial_get_coeff( poly, &ll ) ) ;
00694 }
00695 #endif
00696 #if defined(USE_CPL_POLYNOMIAL)
00697
00698 xsh_msg( "Now fit polynomial - CPL version" ) ;
00699
00700
00701 polyList = xsh_fit_imagelist_polynomial( v_dit, medList, 0,
00702 LINEAR_POLYNOMIAL_DEGREE,
00703 CPL_FALSE, NULL ) ;
00704
00705
00706 assure( polyList != NULL, cpl_error_get_code(),
00707 "Cant fit polynomial - CPL version" ) ;
00708 xsh_msg( "Polynomial Fit Finished - CPL Version" ) ;
00709 #else
00710
00711
00712
00713
00714
00715 xsh_msg( "Now fit polynomial - XSH version" ) ;
00716 polyList = xsh_imagelist_fit_polynomial( medList, Dit, nframes, nx, ny,
00717 LINEAR_POLYNOMIAL_DEGREE ) ;
00718 assure( polyList != NULL, cpl_error_get_code(),
00719 "Cant fit polynomial - XSH version" ) ;
00720 xsh_msg( "Polynomial Fit Finished - XSH Version" ) ;
00721 #endif
00722
00723
00724
00725 for( i = 0 ; i<=LINEAR_POLYNOMIAL_DEGREE ; i++ ) {
00726 check(xsh_set_image_cpl_bpmap ( cpl_imagelist_get( polyList, i ),
00727 resBpmap, decode_bp )) ;
00728 }
00729
00730
00731 xsh_msg_dbg_low( "Clipping sigma: %f", lin_clipping->sigma ) ;
00732
00733 for( i=0; i <= LINEAR_POLYNOMIAL_DEGREE ; i++ ) {
00734
00735 cpl_image *pplane = NULL ;
00736 int iter = 0 ;
00737 double mean = 0.0, stdev = 0.0, med = 0.0;
00738
00739 pplane = cpl_imagelist_get( polyList, i ) ;
00740 assure( pplane != NULL, cpl_error_get_code(),
00741 "Gant get plane Nb %d", i ) ;
00742
00743
00744
00745
00746
00747 xsh_msg( "Sigma clipping on coefficient %d", i ) ;
00748 for( iter = 0 ; iter<lin_clipping->niter ; iter++ ) {
00749 #if defined(OPTIMIZE_IMAGE_HANDLING)
00750 double *pdata = NULL ;
00751 cpl_binary *pbpm = NULL ;
00752 #endif
00753 xsh_msg( " Iteration Nb %d/%d", iter+1, lin_clipping->niter ) ;
00754
00755 mean = cpl_image_get_mean( pplane ) ;
00756 stdev = cpl_image_get_stdev( pplane ) ;
00757 med = cpl_image_get_median( pplane ) ;
00758 xsh_msg( " Mean: %lf, stdev: %lf, Median: %lf",
00759 mean, stdev, med ) ;
00760
00761
00762
00763
00764 nbad = 0 ;
00765 #if defined(OPTIMIZE_IMAGE_HANDLING)
00766 pdata = cpl_image_get_data_double( pplane ) ;
00767 assure( pdata != NULL, cpl_error_get_code(),
00768 "Cant get pplane[%d] data", i ) ;
00769 pbpm = cpl_mask_get_data( cpl_image_get_bpm( pplane ) ) ;
00770 assure( pdata != NULL, cpl_error_get_code(),
00771 "Cant get pplane[%d] bpm", i ) ;
00772
00773 for( iy = 0; iy < ny ; iy++ )
00774 for( ix = 0 ; ix < nx ; ix++ ) {
00775 double coeff = *(pdata + ix + (iy*nx)) ;
00776 cpl_binary rej = *(pbpm + ix + (iy*nx)) ;
00777 if ( rej != 0 ) {
00778 xsh_msg_dbg_low( " **** rejected %d,%d", ix+1, iy+1 ) ;
00779 continue ;
00780 }
00781 #else
00782 for( iy = 1 ; iy <= ny ; iy++ )
00783 for( ix = 1 ; ix<=nx ; ix++ ) {
00784 int rej ;
00785 double coeff = cpl_image_get( pplane, ix, iy, &rej ) ;
00786 if ( rej != 0 ) {
00787 xsh_msg_dbg_low( " **** rejected %d,%d", ix, iy ) ;
00788 continue ;
00789 }
00790 #endif
00791
00792 if ( fabs( coeff - mean) > stdev*lin_clipping->sigma ) {
00793 xsh_msg_dbg_low( " BAD PIXEL at %d,%d - %lf vs %lf",
00794 ix, iy, mean, coeff ) ;
00795
00796 #if defined(OPTIMIZE_IMAGE_HANDLING)
00797 cpl_image_reject( pplane, ix+1, iy+1 ) ;
00798
00799 xsh_bpmap_set_bad_pixel( resBpmap, ix+1, iy+1,
00800 QFLAG_NON_LINEAR_PIXEL ) ;
00801 #else
00802 cpl_image_reject( pplane, ix, iy ) ;
00803
00804 xsh_bpmap_set_bad_pixel( resBpmap, ix, iy,
00805 QFLAG_NON_LINEAR_PIXEL ) ;
00806 #endif
00807 nbad++ ;
00808 }
00809 }
00810 xsh_msg( " Nbad: %d", nbad ) ;
00811 if ( nbad == 0 ) break ;
00812 totbad += nbad ;
00813 }
00814
00815 xsh_pfits_set_qc_multi( bpmap_header, (void *)&mean,
00816 XSH_QC_BP_MAP_LINi_MEAN, instrument, i ) ;
00817 xsh_msg_dbg_low( "%s (%d) = %lf", XSH_QC_BP_MAP_LINi_MEAN, i, mean ) ;
00818 xsh_pfits_set_qc_multi( bpmap_header, (void *)&med,
00819 XSH_QC_BP_MAP_LINi_MED, instrument, i ) ;
00820 xsh_msg_dbg_low( "%s (%d) = %lf", XSH_QC_BP_MAP_LINi_MED, i, med ) ;
00821 xsh_pfits_set_qc_multi( bpmap_header, (void *)&stdev,
00822 XSH_QC_BP_MAP_LINi_RMS, instrument, i ) ;
00823 xsh_msg_dbg_low( "%s (%d) = %lf", XSH_QC_BP_MAP_LINi_RMS, i, stdev ) ;
00824 }
00825 xsh_msg( "====> Total Non linear pixels: %d", totbad ) ;
00826
00827 {
00828 int nbadpix = cpl_image_count_rejected( resBpmap ) ;
00829 xsh_pfits_set_qc( bpmap_header, (void *)&nbadpix,
00830 XSH_QC_BP_MAP_NBADPIX, instrument ) ;
00831 xsh_msg_dbg_low( "%s = %d", XSH_QC_BP_MAP_NBADPIX, nbadpix ) ;
00832 }
00833
00834
00835
00836
00837 {
00838 char bpmapFname[132] ;
00839 const char *sarm = xsh_instrument_arm_tostring( instrument ) ;
00840
00841 sprintf( bpmapFname, "LINEARITY_BAD_PIXEL_MAP_%s.fits", sarm ) ;
00842 check_msg( cpl_image_save( resBpmap, bpmapFname, CPL_BPP_32_SIGNED,
00843 bpmap_header, CPL_IO_DEFAULT ),
00844 "Cant save resulting BpMap image" ) ;
00845
00846
00847
00848
00849 check(resFrame=xsh_frame_product(bpmapFname,
00850 XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_LIN,
00851 instrument),
00852 CPL_FRAME_TYPE_IMAGE,
00853 CPL_FRAME_GROUP_PRODUCT,
00854 CPL_FRAME_LEVEL_FINAL));
00855
00856 }
00857
00858 cleanup:
00859 if ( Dit != NULL ) cpl_free( Dit ) ;
00860 xsh_free_imagelist( &polyList ) ;
00861 xsh_free_imagelist( &bpmapList ) ;
00862 xsh_free_imagelist( &medList ) ;
00863 xsh_free_propertylist( &bpmap_header ) ;
00864 xsh_free_image( &resBpmap ) ;
00865
00866 return resFrame ;
00867
00868 }
00869
00870
00871