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, *last ;
00277 int nb = 0 ;
00278
00279 temps = expOrdered->exptime ;
00280
00281 for (i = 0, porder = expOrdered; i < nframes; i++, porder++) {
00282 if ( porder->exptime == temps ) {
00283 last = porder ;
00284 nb++ ;
00285 }
00286 else {
00287 xsh_msg_dbg_low( "Call ON/OFF compare (%d frames)", nb ) ;
00288 qsort( first, nb, sizeof (TIME_FRAME), onoffCompare ) ;
00289 first = porder ;
00290 nb = 1 ;
00291 temps = porder->exptime ;
00292 }
00293 }
00294
00295 qsort( first, nb, sizeof (TIME_FRAME), onoffCompare ) ;
00296 }
00297
00298 {
00299 xsh_msg_dbg_low ("==== After qsort");
00300 for (i = 0, porder = expOrdered; i < nframes; i++, porder++)
00301 xsh_msg_dbg_low(" DIT[%d] = %lf (ON/OFF = %d)", i, porder->exptime,
00302 porder->on_off);
00303 }
00304
00305
00306 porder = expOrdered;
00307 temps = porder->exptime;
00308 onoff = porder->on_off ;
00309
00310 pgrpset = groupSet;
00311
00312 xsh_msg_dbg_low ("New frameset 0 starting at Frame 0" );
00313 *pgrpset = cpl_frameset_new ();
00314 check_msg (cpl_frameset_insert (*pgrpset, porder->frame),
00315 "Cant insert frame %d in group %d", i, ngroups);
00316 porder++;
00317 ngroups = 1;
00318 for (i = 1; i < nframes; i++, porder++) {
00319 if (fabs (porder->exptime - temps) > exp_toler ||
00320 porder->on_off != onoff ) {
00321 xsh_msg_dbg_low ("New frameset %d starting at Frame %d", ngroups, i);
00322 ngroups++;
00323 temps = porder->exptime;
00324 onoff = porder->on_off ;
00325 pgrpset++;
00326 *pgrpset = cpl_frameset_new ();
00327 assure( *pgrpset != NULL, cpl_error_get_code(),
00328 "Cant create new framest" ) ;
00329 }
00330 else
00331 xsh_msg_dbg_low ("Same group frame %d - %lf vs %lf", i,
00332 porder->exptime, temps);
00333 xsh_msg_dbg_low ("Add frame %d to frameset group %d", i, ngroups);
00334 check_msg (cpl_frameset_insert (*pgrpset, porder->frame),
00335 "Cant insert frame %d in group %d", i, ngroups);
00336 }
00337
00338 cleanup:
00339 if ( expOrdered != NULL ) cpl_free( expOrdered ) ;
00340 return ngroups;
00341 }
00342
00343 #if !defined(USE_CPL_POLYNOMIAL)
00344
00361 static cpl_imagelist *xsh_imagelist_fit_polynomial( cpl_imagelist * medlist,
00362 double *dit, int nframes,
00363 int nx, int ny,
00364 int degree )
00365 {
00366 int ll, ix, iy ;
00367 cpl_vector * pixvect = NULL;
00368 cpl_vector * temps = NULL;
00369 cpl_polynomial *poly = NULL;
00370 cpl_imagelist *poly_list = NULL,
00372 *final_list = NULL ;
00373 float **pixbuf = NULL ;
00374 cpl_binary **maskbuf = NULL ;
00376 assure( medlist != NULL, CPL_ERROR_NULL_INPUT,
00377 "Input list of median images is NUL !" ) ;
00378
00379 poly_list = cpl_imagelist_new() ;
00380 assure( poly_list != NULL, cpl_error_get_code(),
00381 "Cant create imagelist" ) ;
00382
00383
00384
00385 for ( ll = 0 ; ll<= degree ; ll++ ) {
00386 cpl_image *newone = NULL ;
00387
00388 newone = cpl_image_new( nx, ny, CPL_TYPE_DOUBLE ) ;
00389 assure( newone != NULL, cpl_error_get_code(),
00390 "Cant create new image" ) ;
00391 cpl_imagelist_set( poly_list, newone, ll ) ;
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401 xsh_msg( "Polynomial Fit - XSH version - nb of frames: %d", nframes ) ;
00402 #if 1
00403
00404
00405
00406
00407
00408
00409 pixbuf = cpl_malloc( nframes * sizeof(double *) ) ;
00410 assure( pixbuf != NULL, cpl_error_get_code(),
00411 "Cant allocate memory for pixel data" ) ;
00412 maskbuf = cpl_malloc( nframes * sizeof(cpl_binary *) ) ;
00413 assure( maskbuf != NULL, cpl_error_get_code(),
00414 "Cant allocate memory for BPM data" ) ;
00415
00416 for( ll = 0 ; ll < nframes ; ll++ ) {
00417 cpl_image *pcur = cpl_imagelist_get( medlist, ll ) ;
00418
00419 *(pixbuf+ll) = cpl_image_get_data( pcur ) ;
00420 *(maskbuf+ll) = cpl_mask_get_data( cpl_image_get_bpm( pcur ) ) ;
00421 }
00422
00423
00424 for ( iy = 0 ; iy < ny ; iy++ )
00425 for( ix = 0 ; ix < nx ; ix++ ) {
00426 cpl_binary ** pmask = maskbuf ;
00427 float ** cur = pixbuf ;
00428 int npix = 0 ;
00429
00430 pixvect = cpl_vector_new( nframes ) ;
00431 assure( pixvect != NULL, cpl_error_get_code(),
00432 "Cant create PixVect Vector" ) ;
00433 temps = cpl_vector_new( nframes ) ;
00434 assure( temps != NULL, cpl_error_get_code(),
00435 "Cant create Time Vector" ) ;
00436 xsh_msg_dbg_low( "++ Fit one pixel: %d,%d", ix, iy ) ;
00437
00438 for( ll = 0 ; ll<nframes ; ll++ , pmask++, cur++ ) {
00439 double pixval ;
00440 cpl_binary rej ;
00441 float *pcur = *cur ;
00442 cpl_binary *mask = *pmask ;
00443
00444 pixval = *(pcur + (iy*nx) + ix ) ;
00445 rej = *(mask + (iy*nx) + ix ) ;
00446 if ( rej == 0 ) {
00447 cpl_vector_set( temps, ll, *(dit+ll) ) ;
00448 cpl_vector_set( pixvect, ll, pixval ) ;
00449 npix++ ;
00450 }
00451 }
00452 xsh_msg_dbg_low( " Nb of pixels: %d", npix ) ;
00453
00454 if ( npix != nframes ) {
00455 cpl_vector_set_size( temps, npix ) ;
00456 cpl_vector_set_size( pixvect, npix ) ;
00457 }
00458
00459 poly = xsh_polynomial_fit_1d_create( temps, pixvect,
00460 degree, NULL ) ;
00461 xsh_free_vector( &temps ) ;
00462 xsh_free_vector( &pixvect ) ;
00463 assure( poly != NULL, CPL_ERROR_ILLEGAL_INPUT,
00464 "Error calling xsh_polynomial_fit_1d_create" ) ;
00465
00466 for( ll = 0 ; ll <= degree ; ll++ ) {
00467 cpl_image_set( cpl_imagelist_get( poly_list, ll ),
00468 ix+1, iy+1, cpl_polynomial_get_coeff( poly, &ll ) ) ;
00469 }
00470 xsh_free_polynomial( &poly ) ;
00471 }
00472 #else
00473 for ( iy = 1 ; iy <= ny ; iy++ )
00474 for( ix = 1 ; ix <= nx ; ix++ ) {
00475 int npix = 0 ;
00476
00477 pixvect = cpl_vector_new( nframes ) ;
00478 assure( pixvect != NULL, cpl_error_get_code(),
00479 "Cant create PixVect Vector" ) ;
00480 temps = cpl_vector_new( nframes ) ;
00481 assure( temps != NULL, cpl_error_get_code(),
00482 "Cant create Time Vector" ) ;
00483 xsh_msg_dbg_low( "++ Fit one pixel: %d,%d", ix, iy ) ;
00484 for( ll = 0 ; ll < nframes ; ll++ ) {
00485 double pixval ;
00486 int rej ;
00487 cpl_image *cur = NULL ;
00488 cur = cpl_imagelist_get( medlist, ll ) ;
00489 assure( cur != NULL, cpl_error_get_code(),
00490 "Cant get image #%d from liste" ) ;
00491
00492 pixval = cpl_image_get( cur, ix, iy, &rej ) ;
00493 xsh_msg_dbg_low( " Pixval: %lf", pixval ) ;
00494 if ( rej == 0 ) {
00495 cpl_vector_set( temps, ll, *(dit+ll) ) ;
00496 cpl_vector_set( pixvect, ll, pixval ) ;
00497 npix++ ;
00498 }
00499 }
00500 xsh_msg_dbg_low( " Nb of pixels: %d", npix ) ;
00501
00502 if ( npix != nframes ) {
00503 cpl_vector_set_size( temps, npix ) ;
00504 cpl_vector_set_size( pixvect, npix ) ;
00505 }
00506
00507 poly = xsh_polynomial_fit_1d_create( temps, pixvect,
00508 degree, NULL ) ;
00509 assure( poly != NULL, cpl_error_get_code(),
00510 "Error calling xsh_polynomial_fit_1d_create" ) ;
00511 xsh_free_vector( &temps ) ;
00512 xsh_free_vector( &pixvect ) ;
00513
00514
00515 for( ll = 0 ; ll <= degree ; ll++ ) {
00516 cpl_image_set( cpl_imagelist_get( poly_list, ll ),
00517 ix, iy, cpl_polynomial_get_coeff( poly, &ll ) ) ;
00518 }
00519 xsh_free_polynomial( &poly ) ;
00520 }
00521 #endif
00522
00523 final_list = cpl_imagelist_duplicate( poly_list ) ;
00524
00525 cleanup:
00526 xsh_free_imagelist( &poly_list ) ;
00527 xsh_free_polynomial( &poly ) ;
00528 xsh_free_vector( &temps ) ;
00529 xsh_free_vector( &pixvect ) ;
00530 xsh_free_imagelist( &poly_list ) ;
00531 cpl_free( pixbuf ) ;
00532 cpl_free( maskbuf ) ;
00533
00534 return final_list ;
00535 }
00536 #endif
00537
00548 cpl_frame *
00549 xsh_compute_linearity (cpl_frameset * medSet, xsh_instrument *instrument,
00550 xsh_clipping_param * lin_clipping)
00551 {
00552 cpl_image * resBpmap = NULL ;
00553 int nframes = 0 ;
00554 cpl_frame * resFrame = NULL ;
00555 int i = 0, ix = 0, iy = 0, nx = 0, ny = 0;
00556 int nbad, totbad = 0 ;
00557 cpl_imagelist *medList = NULL ;
00558 cpl_imagelist *bpmapList = NULL ;
00559 cpl_frame *current = NULL ;
00560 cpl_propertylist *bpmap_header = NULL ;
00562 double *Dit = NULL;
00563 #if defined(USE_CPL_POLYNOMIAL)
00564 cpl_vector * v_dit = NULL ;
00565
00566 #endif
00567 cpl_imagelist * polyList = NULL ;
00570 xsh_msg( "==> xsh_compute_linearity" ) ;
00571 assure (medSet != NULL, CPL_ERROR_ILLEGAL_INPUT, "Frameset is NULL");
00572 nframes = cpl_frameset_get_size (medSet);
00573
00574
00575
00576
00577
00578 medList = cpl_imagelist_new() ;
00579 assure( medList != NULL, cpl_error_get_code(),
00580 "Cant create medList" ) ;
00581 bpmapList = cpl_imagelist_new() ;
00582 assure( bpmapList != NULL, cpl_error_get_code(),
00583 "Cant create bpmapList" ) ;
00584
00585 current = cpl_frameset_get_first( medSet ) ;
00586 assure( current != NULL, cpl_error_get_code(),
00587 "Cant get current frame" ) ;
00588
00589
00590 Dit = cpl_malloc( nframes * sizeof(double) ) ;
00591 assure( Dit != NULL, cpl_error_get_code(),
00592 "Cant allocate Dit array" ) ;
00593 #if defined(USE_CPL_POLYNOMIAL)
00594 v_dit = cpl_vector_wrap( nframes, Dit ) ;
00595 assure( v_dit != NULL, cpl_error_get_code(),
00596 "Cant create v_dit vector" ) ;
00597 #endif
00598 xsh_msg( "Loop over all frames" ) ;
00599
00600 for( i = 0 ; i<nframes ; i++ ) {
00601 const char *filename ;
00602 cpl_propertylist *header = NULL ;
00603 cpl_image * curimage = NULL ;
00604 cpl_image *curbpmap = NULL ;
00605
00606 filename = cpl_frame_get_filename(current);
00607 assure( filename != NULL, cpl_error_get_code(),
00608 "Cant get filename from frame %d", i ) ;
00609
00610 curimage = cpl_image_load( filename, XSH_PRE_DATA_TYPE, 0, 0 ) ;
00611 assure( curimage != NULL, cpl_error_get_code(),
00612 "Cant get curimage from frame %d", i ) ;
00613
00614 cpl_imagelist_set( medList, curimage, i ) ;
00615 header = cpl_propertylist_load( filename, 0 ) ;
00616 assure( header != NULL, cpl_error_get_code(),
00617 "Cant get header of frame %d", i ) ;
00618
00619 xsh_msg_dbg_low( " Getting EXPTIME/DIT[%d]", i ) ;
00620
00621 if ( instrument->arm == XSH_ARM_NIR )
00622 *(Dit+i) = xsh_pfits_get_dit( header ) ;
00623 else *(Dit+i) = xsh_pfits_get_exptime( header ) ;
00624 cpl_propertylist_delete( header ) ;
00625
00626 if ( i == 0 ) {
00627 nx = cpl_image_get_size_x( curimage ) ;
00628 ny = cpl_image_get_size_y( curimage ) ;
00629 }
00630
00631
00632 xsh_msg_dbg_low( " Loading BpMap[%d]", i ) ;
00633
00634 curbpmap = cpl_image_load( filename, CPL_TYPE_INT, 0, 2 ) ;
00635 assure( curbpmap != NULL, cpl_error_get_code(),
00636 "Cant load bpmap frame #%d", i ) ;
00637 cpl_imagelist_set( bpmapList, curbpmap, i ) ;
00638 if ( i == 0 ) {
00639 bpmap_header = cpl_propertylist_load( filename, 2 ) ;
00640 assure( bpmap_header != NULL, cpl_error_get_code(),
00641 "Cant get bpmap header of frame %d", i ) ;
00642 }
00643 current = cpl_frameset_get_next( medSet ) ;
00644 xsh_msg_dbg_low( "-- Next Frame" ) ;
00645 }
00646
00647
00648 xsh_msg( "Collapsing BpMaps's" ) ;
00649
00650 resBpmap = xsh_bpmap_collapse_bpmap_create ( bpmapList ) ;
00651 assure( resBpmap != NULL, cpl_error_get_code(),
00652 "Cant create collapsed bpmap" ) ;
00653
00654 xsh_msg_dbg_low( "Freeing bpmapList" ) ;
00655 xsh_free_imagelist( &bpmapList ) ;
00656
00657
00658 for( i = 0 ; i<nframes ; i++ ) {
00659 xsh_set_image_cpl_bpmap ( cpl_imagelist_get( medList, i ),
00660 resBpmap, nx, ny ) ;
00661 }
00662
00663 #if defined(JUST_FOR_TEST)
00664
00665 {
00666 int ll = 0;
00667 cpl_vector * pixvect = NULL;
00668 cpl_vector * temps = NULL;
00669 cpl_polynomial *poly = NULL;
00670
00671
00672 pixvect = cpl_vector_new( nframes ) ;
00673 temps = cpl_vector_new( nframes ) ;
00674
00675 xsh_msg( "===== Fit polynome (1) sur valeurs moyennes des med frames" ) ;
00676 for( ll = 0 ; ll < nframes ; ll++ ) {
00677 cpl_image *cur = NULL ;
00678 cur = cpl_imagelist_get( medList, ll ) ;
00679 xsh_msg( " #%d: mean=%lf, DIT=%lf", ll, cpl_image_get_mean( cur ),
00680 *(Dit+ll) ) ;
00681 cpl_vector_set( pixvect, ll, cpl_image_get_mean( cur ) ) ;
00682 cpl_vector_set( temps, ll, *(Dit+ll) ) ;
00683 }
00684 poly = xsh_polynomial_fit_1d_create( temps, pixvect,
00685 LINEAR_POLYNOMIAL_DEGREE,
00686 NULL ) ;
00687 cpl_polynomial_dump( poly, stderr ) ;
00688 xsh_msg( "===== Ca ne ressemble en rien a ce qu'on obtient dans le" ) ;
00689 xsh_msg( " fit general. Bizarre n'est-il pas ?" ) ;
00690 for ( ll = 0 ; ll<=LINEAR_POLYNOMIAL_DEGREE ; ll++ )
00691 xsh_msg( ">>>>> %d : %lf\n", ll,
00692 cpl_polynomial_get_coeff( poly, &ll ) ) ;
00693 }
00694 #endif
00695 #if defined(USE_CPL_POLYNOMIAL)
00696
00697 xsh_msg( "Now fit polynomial - CPL version" ) ;
00698
00699
00700 polyList = xsh_fit_imagelist_polynomial( v_dit, medList, 0,
00701 LINEAR_POLYNOMIAL_DEGREE,
00702 CPL_FALSE, NULL ) ;
00703
00704
00705 assure( polyList != NULL, cpl_error_get_code(),
00706 "Cant fit polynomial - CPL version" ) ;
00707 xsh_msg( "Polynomial Fit Finished - CPL Version" ) ;
00708 #else
00709
00710
00711
00712
00713
00714 xsh_msg( "Now fit polynomial - XSH version" ) ;
00715 polyList = xsh_imagelist_fit_polynomial( medList, Dit, nframes, nx, ny,
00716 LINEAR_POLYNOMIAL_DEGREE ) ;
00717 assure( polyList != NULL, cpl_error_get_code(),
00718 "Cant fit polynomial - XSH version" ) ;
00719 xsh_msg( "Polynomial Fit Finished - XSH Version" ) ;
00720 #endif
00721
00722
00723
00724 for( i = 0 ; i<=LINEAR_POLYNOMIAL_DEGREE ; i++ ) {
00725 check(xsh_set_image_cpl_bpmap ( cpl_imagelist_get( polyList, i ),
00726 resBpmap, nx, ny )) ;
00727 }
00728
00729
00730 xsh_msg_dbg_low( "Clipping sigma: %f", lin_clipping->sigma ) ;
00731
00732 for( i=0; i <= LINEAR_POLYNOMIAL_DEGREE ; i++ ) {
00733
00734 cpl_image *pplane = NULL ;
00735 int iter = 0 ;
00736 double mean = 0.0, stdev = 0.0, med = 0.0;
00737
00738 pplane = cpl_imagelist_get( polyList, i ) ;
00739 assure( pplane != NULL, cpl_error_get_code(),
00740 "Gant get plane Nb %d", i ) ;
00741
00742
00743
00744
00745
00746 xsh_msg( "Sigma clipping on coefficient %d", i ) ;
00747 for( iter = 0 ; iter<lin_clipping->niter ; iter++ ) {
00748 #if defined(OPTIMIZE_IMAGE_HANDLING)
00749 double *pdata = NULL ;
00750 cpl_binary *pbpm = NULL ;
00751 #endif
00752 xsh_msg( " Iteration Nb %d/%d", iter+1, lin_clipping->niter ) ;
00753
00754 mean = cpl_image_get_mean( pplane ) ;
00755 stdev = cpl_image_get_stdev( pplane ) ;
00756 med = cpl_image_get_median( pplane ) ;
00757 xsh_msg( " Mean: %lf, stdev: %lf, Median: %lf",
00758 mean, stdev, med ) ;
00759
00760
00761
00762
00763 nbad = 0 ;
00764 #if defined(OPTIMIZE_IMAGE_HANDLING)
00765 pdata = cpl_image_get_data_double( pplane ) ;
00766 assure( pdata != NULL, cpl_error_get_code(),
00767 "Cant get pplane[%d] data", i ) ;
00768 pbpm = cpl_mask_get_data( cpl_image_get_bpm( pplane ) ) ;
00769 assure( pdata != NULL, cpl_error_get_code(),
00770 "Cant get pplane[%d] bpm", i ) ;
00771
00772 for( iy = 0; iy < ny ; iy++ )
00773 for( ix = 0 ; ix < nx ; ix++ ) {
00774 double coeff = *(pdata + ix + (iy*nx)) ;
00775 cpl_binary rej = *(pbpm + ix + (iy*nx)) ;
00776 if ( rej != 0 ) {
00777 xsh_msg_dbg_low( " **** rejected %d,%d", ix+1, iy+1 ) ;
00778 continue ;
00779 }
00780 #else
00781 for( iy = 1 ; iy <= ny ; iy++ )
00782 for( ix = 1 ; ix<=nx ; ix++ ) {
00783 int rej ;
00784 double coeff = cpl_image_get( pplane, ix, iy, &rej ) ;
00785 if ( rej != 0 ) {
00786 xsh_msg_dbg_low( " **** rejected %d,%d", ix, iy ) ;
00787 continue ;
00788 }
00789 #endif
00790
00791 if ( fabs( coeff - mean) > stdev*lin_clipping->sigma ) {
00792 xsh_msg_dbg_low( " BAD PIXEL at %d,%d - %lf vs %lf",
00793 ix, iy, mean, coeff ) ;
00794
00795 #if defined(OPTIMIZE_IMAGE_HANDLING)
00796 cpl_image_reject( pplane, ix+1, iy+1 ) ;
00797
00798 xsh_bpmap_set_bad_pixel( resBpmap, ix+1, iy+1,
00799 QFLAG_NON_LINEAR_PIXEL ) ;
00800 #else
00801 cpl_image_reject( pplane, ix, iy ) ;
00802
00803 xsh_bpmap_set_bad_pixel( resBpmap, ix, iy,
00804 QFLAG_NON_LINEAR_PIXEL ) ;
00805 #endif
00806 nbad++ ;
00807 }
00808 }
00809 xsh_msg( " Nbad: %d", nbad ) ;
00810 if ( nbad == 0 ) break ;
00811 totbad += nbad ;
00812 }
00813
00814 xsh_pfits_set_qc_multi( bpmap_header, (void *)&mean,
00815 XSH_QC_BP_MAP_LINi_MEAN, instrument, i ) ;
00816 xsh_msg_dbg_low( "%s (%d) = %lf", XSH_QC_BP_MAP_LINi_MEAN, i, mean ) ;
00817 xsh_pfits_set_qc_multi( bpmap_header, (void *)&med,
00818 XSH_QC_BP_MAP_LINi_MED, instrument, i ) ;
00819 xsh_msg_dbg_low( "%s (%d) = %lf", XSH_QC_BP_MAP_LINi_MED, i, med ) ;
00820 xsh_pfits_set_qc_multi( bpmap_header, (void *)&stdev,
00821 XSH_QC_BP_MAP_LINi_RMS, instrument, i ) ;
00822 xsh_msg_dbg_low( "%s (%d) = %lf", XSH_QC_BP_MAP_LINi_RMS, i, stdev ) ;
00823 }
00824 xsh_msg( "====> Total Non linear pixels: %d", totbad ) ;
00825
00826 {
00827 int nbadpix = cpl_image_count_rejected( resBpmap ) ;
00828 xsh_pfits_set_qc( bpmap_header, (void *)&nbadpix,
00829 XSH_QC_BP_MAP_NBADPIX, instrument ) ;
00830 xsh_msg_dbg_low( "%s = %d", XSH_QC_BP_MAP_NBADPIX, nbadpix ) ;
00831 }
00832
00833
00834
00835
00836 {
00837 char bpmapFname[132] ;
00838 const char *sarm = xsh_instrument_arm_tostring( instrument ) ;
00839
00840 sprintf( bpmapFname, "LINEARITY_BAD_PIXEL_MAP_%s.fits", sarm ) ;
00841 check_msg( cpl_image_save( resBpmap, bpmapFname, CPL_BPP_32_SIGNED,
00842 bpmap_header, CPL_IO_DEFAULT ),
00843 "Cant save resulting BpMap image" ) ;
00844 xsh_add_temporary_file(bpmapFname);
00845
00846
00847
00848 check(resFrame=xsh_frame_product(bpmapFname,
00849 XSH_GET_TAG_FROM_ARM(XSH_BP_MAP_LIN,
00850 instrument),
00851 CPL_FRAME_TYPE_IMAGE,
00852 CPL_FRAME_GROUP_PRODUCT,
00853 CPL_FRAME_LEVEL_FINAL));
00854
00855 }
00856
00857 cleanup:
00858 if ( Dit != NULL ) cpl_free( Dit ) ;
00859 xsh_free_imagelist( &polyList ) ;
00860 xsh_free_imagelist( &bpmapList ) ;
00861 xsh_free_imagelist( &medList ) ;
00862 xsh_free_propertylist( &bpmap_header ) ;
00863 xsh_free_image( &resBpmap ) ;
00864
00865 return resFrame ;
00866
00867 }
00868
00869
00870