61#define USE_CPL_POLYNOMIAL
62#define OPTIMIZE_IMAGE_HANDLING
123 cpl_frame *currOn = NULL, *currOff = NULL;
124 cpl_frameset * subSet = NULL ;
127 xsh_pre *preOn = NULL, * preOff = NULL ;
132 xsh_msg(
"===> Subtract on_off" ) ;
134 nframes = cpl_frameset_get_size( set ) ;
135 subSet = cpl_frameset_new() ;
136 assure( subSet != NULL, cpl_error_get_code(),
137 "Cant create new frameset" ) ;
139 for( i = 0 ; i<nframes ; i+=2 ) {
140 const char *filename = NULL;
141 currOn = cpl_frameset_get_frame(set,i);
142 assure( currOn != NULL, cpl_error_get_code(),
143 "Cant get frame of frameset" ) ;
144 filename = cpl_frame_get_filename (currOn);
147 "Cant load PRE ON" ) ;
149 currOff = cpl_frameset_get_frame (set,i+1);
150 assure( currOff != NULL, cpl_error_get_code(),
151 "Cant get frame of frameset" ) ;
152 filename = cpl_frame_get_filename (currOff);
155 "Cant load PRE frame OFF" ) ;
160 assure( subtracted != NULL, cpl_error_get_code(),
161 "Cant subtract images" ) ;
164 cpl_frame * saved = NULL ;
168 sprintf( outname,
"linear_sub_set_%d.fits", i ) ;
170 assure( saved != NULL, cpl_error_get_code(),
171 "Cant save subtracted frame" ) ;
173 check_msg( cpl_frame_set_tag( saved, tag ),
174 "Cant set frame tag" ) ;
176 check_msg( cpl_frameset_insert( subSet, saved ),
177 "Cant insert frame %d into subSet", i ) ;
186 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
211 double exp_toler, cpl_frameset ** groupSet)
222 cpl_frame *current = NULL;
225 cpl_frameset **pgrpset = NULL;
227 nframes = cpl_frameset_get_size (raws);
228 xsh_msg(
"Nb of Frames = %d", nframes ) ;
230 expOrdered = cpl_malloc (nframes *
sizeof (
TIME_FRAME));
231 assure (expOrdered != NULL, cpl_error_get_code (),
232 "Cant create tempory time+fframe array");
236 for ( i = 0 ; i<nframes ; i++, porder++) {
237 const char *filename;
238 cpl_propertylist *header;
241 current = cpl_frameset_get_frame (raws,i);
243 assure( current != NULL, CPL_ERROR_ILLEGAL_INPUT,
244 "Cant get Frame #%d from frameset", i ) ;
246 filename = cpl_frame_get_filename (current);
248 check_msg (header = cpl_propertylist_load (filename, 0),
249 "Could not load header of file '%s'", filename);
256 porder->
frame = current;
260 if ( onoffs ) porder->
on_off = 1;
279 for (i = 0, porder = expOrdered; i < nframes; i++, porder++) {
280 if ( porder->
exptime == temps ) {
298 for (i = 0, porder = expOrdered; i < nframes; i++, porder++)
311 *pgrpset = cpl_frameset_new ();
313 "Cant insert frame %d in group %d", i, ngroups);
316 for (i = 1; i < nframes; i++, porder++) {
317 if (fabs (porder->
exptime - temps) > exp_toler ||
318 porder->
on_off != onoff ) {
324 *pgrpset = cpl_frameset_new ();
325 assure( *pgrpset != NULL, cpl_error_get_code(),
326 "Cant create new framest" ) ;
333 "Cant insert frame %d in group %d", i, ngroups);
337 if ( expOrdered != NULL ) cpl_free( expOrdered ) ;
341#if !defined(USE_CPL_POLYNOMIAL)
359static cpl_imagelist *xsh_imagelist_fit_polynomial( cpl_imagelist * medlist,
360 double *dit,
int nframes,
365 cpl_vector * pixvect = NULL;
366 cpl_vector * temps = NULL;
367 cpl_polynomial *poly = NULL;
368 cpl_imagelist *poly_list = NULL,
371 float **pixbuf = NULL ;
372 cpl_binary **maskbuf = NULL ;
374 assure( medlist != NULL, CPL_ERROR_NULL_INPUT,
375 "Input list of median images is NUL !" ) ;
377 poly_list = cpl_imagelist_new() ;
378 assure( poly_list != NULL, cpl_error_get_code(),
379 "Cant create imagelist" ) ;
383 for ( ll = 0 ; ll<=
degree ; ll++ ) {
384 cpl_image *newone = NULL ;
386 newone = cpl_image_new(
nx,
ny, CPL_TYPE_DOUBLE ) ;
387 assure( newone != NULL, cpl_error_get_code(),
388 "Cant create new image" ) ;
389 cpl_imagelist_set( poly_list, newone, ll ) ;
399 xsh_msg(
"Polynomial Fit - XSH version - nb of frames: %d", nframes ) ;
407 pixbuf = cpl_malloc( nframes *
sizeof(
double *) ) ;
408 assure( pixbuf != NULL, cpl_error_get_code(),
409 "Cant allocate memory for pixel data" ) ;
410 maskbuf = cpl_malloc( nframes *
sizeof(cpl_binary *) ) ;
411 assure( maskbuf != NULL, cpl_error_get_code(),
412 "Cant allocate memory for BPM data" ) ;
414 for( ll = 0 ; ll < nframes ; ll++ ) {
415 cpl_image *pcur = cpl_imagelist_get( medlist, ll ) ;
417 *(pixbuf+ll) = cpl_image_get_data( pcur ) ;
418 *(maskbuf+ll) = cpl_mask_get_data( cpl_image_get_bpm( pcur ) ) ;
422 for ( iy = 0 ; iy <
ny ; iy++ )
423 for( ix = 0 ; ix <
nx ; ix++ ) {
424 cpl_binary ** pmask = maskbuf ;
425 float ** cur = pixbuf ;
428 pixvect = cpl_vector_new( nframes ) ;
429 assure( pixvect != NULL, cpl_error_get_code(),
430 "Cant create PixVect Vector" ) ;
431 temps = cpl_vector_new( nframes ) ;
432 assure( temps != NULL, cpl_error_get_code(),
433 "Cant create Time Vector" ) ;
436 for( ll = 0 ; ll<nframes ; ll++ , pmask++, cur++ ) {
440 cpl_binary *mask = *pmask ;
442 pixval = *(pcur + (iy*
nx) + ix ) ;
443 rej = *(mask + (iy*
nx) + ix ) ;
445 cpl_vector_set( temps, ll, *(dit+ll) ) ;
446 cpl_vector_set( pixvect, ll, pixval ) ;
452 if ( npix != nframes ) {
453 cpl_vector_set_size( temps, npix ) ;
454 cpl_vector_set_size( pixvect, npix ) ;
461 assure( poly != NULL, CPL_ERROR_ILLEGAL_INPUT,
462 "Error calling xsh_polynomial_fit_1d_create" ) ;
464 for( ll = 0 ; ll <=
degree ; ll++ ) {
465 cpl_image_set( cpl_imagelist_get( poly_list, ll ),
466 ix+1, iy+1, cpl_polynomial_get_coeff( poly, &ll ) ) ;
471 for ( iy = 1 ; iy <=
ny ; iy++ )
472 for( ix = 1 ; ix <=
nx ; ix++ ) {
475 pixvect = cpl_vector_new( nframes ) ;
476 assure( pixvect != NULL, cpl_error_get_code(),
477 "Cant create PixVect Vector" ) ;
478 temps = cpl_vector_new( nframes ) ;
479 assure( temps != NULL, cpl_error_get_code(),
480 "Cant create Time Vector" ) ;
482 for( ll = 0 ; ll < nframes ; ll++ ) {
485 cpl_image *cur = NULL ;
486 cur = cpl_imagelist_get( medlist, ll ) ;
487 assure( cur != NULL, cpl_error_get_code(),
488 "Cant get image #%d from liste" ) ;
490 pixval = cpl_image_get( cur, ix, iy, &rej ) ;
493 cpl_vector_set( temps, ll, *(dit+ll) ) ;
494 cpl_vector_set( pixvect, ll, pixval ) ;
500 if ( npix != nframes ) {
501 cpl_vector_set_size( temps, npix ) ;
502 cpl_vector_set_size( pixvect, npix ) ;
507 assure( poly != NULL, cpl_error_get_code(),
508 "Error calling xsh_polynomial_fit_1d_create" ) ;
513 for( ll = 0 ; ll <=
degree ; ll++ ) {
514 cpl_image_set( cpl_imagelist_get( poly_list, ll ),
515 ix, iy, cpl_polynomial_get_coeff( poly, &ll ) ) ;
521 final_list = cpl_imagelist_duplicate( poly_list ) ;
530 cpl_free( maskbuf ) ;
550 cpl_image * resBpmap = NULL ;
552 cpl_frame * resFrame = NULL ;
553 int i = 0, ix = 0, iy = 0,
nx = 0,
ny = 0;
554 int nbad, totbad = 0 ;
555 cpl_imagelist *medList = NULL ;
556 cpl_imagelist *bpmapList = NULL ;
557 cpl_frame *current = NULL ;
558 cpl_propertylist *bpmap_header = NULL ;
561#if defined(USE_CPL_POLYNOMIAL)
562 cpl_vector * v_dit = NULL ;
565 cpl_imagelist * polyList = NULL ;
568 xsh_msg(
"==> xsh_compute_linearity" ) ;
569 assure (medSet != NULL, CPL_ERROR_ILLEGAL_INPUT,
"Frameset is NULL");
570 nframes = cpl_frameset_get_size (medSet);
576 medList = cpl_imagelist_new() ;
577 assure( medList != NULL, cpl_error_get_code(),
578 "Cant create medList" ) ;
579 bpmapList = cpl_imagelist_new() ;
580 assure( bpmapList != NULL, cpl_error_get_code(),
581 "Cant create bpmapList" ) ;
583 current = cpl_frameset_get_frame( medSet,0 ) ;
584 assure( current != NULL, cpl_error_get_code(),
585 "Cant get current frame" ) ;
588 Dit = cpl_malloc( nframes *
sizeof(
double) ) ;
589 assure( Dit != NULL, cpl_error_get_code(),
590 "Cant allocate Dit array" ) ;
591#if defined(USE_CPL_POLYNOMIAL)
592 v_dit = cpl_vector_wrap( nframes, Dit ) ;
593 assure( v_dit != NULL, cpl_error_get_code(),
594 "Cant create v_dit vector" ) ;
596 xsh_msg(
"Loop over all frames" ) ;
598 for( i = 0 ; i<nframes ; i++ ) {
599 const char *filename ;
600 cpl_propertylist *header = NULL ;
601 cpl_image * curimage = NULL ;
602 cpl_image *curbpmap = NULL ;
604 filename = cpl_frame_get_filename(current);
605 assure( filename != NULL, cpl_error_get_code(),
606 "Cant get filename from frame %d", i ) ;
609 assure( curimage != NULL, cpl_error_get_code(),
610 "Cant get curimage from frame %d", i ) ;
612 cpl_imagelist_set( medList, curimage, i ) ;
613 header = cpl_propertylist_load( filename, 0 ) ;
614 assure( header != NULL, cpl_error_get_code(),
615 "Cant get header of frame %d", i ) ;
622 cpl_propertylist_delete( header ) ;
625 nx = cpl_image_get_size_x( curimage ) ;
626 ny = cpl_image_get_size_y( curimage ) ;
632 curbpmap = cpl_image_load( filename, CPL_TYPE_INT, 0, 2 ) ;
633 assure( curbpmap != NULL, cpl_error_get_code(),
634 "Cant load bpmap frame #%d", i ) ;
635 cpl_imagelist_set( bpmapList, curbpmap, i ) ;
637 bpmap_header = cpl_propertylist_load( filename, 2 ) ;
638 assure( bpmap_header != NULL, cpl_error_get_code(),
639 "Cant get bpmap header of frame %d", i ) ;
641 current = cpl_frameset_get_frame( medSet,i+1 ) ;
646 xsh_msg(
"Collapsing BpMaps's" ) ;
649 assure( resBpmap != NULL, cpl_error_get_code(),
650 "Cant create collapsed bpmap" ) ;
656 for( i = 0 ; i<nframes ; i++ ) {
658 resBpmap, decode_bp ) ;
661#if defined(USE_CPL_POLYNOMIAL)
663 xsh_msg(
"Now fit polynomial - CPL version" ) ;
671 assure( polyList != NULL, cpl_error_get_code(),
672 "Cant fit polynomial - CPL version" ) ;
673 xsh_msg(
"Polynomial Fit Finished - CPL Version" ) ;
680 xsh_msg(
"Now fit polynomial - XSH version" ) ;
681 polyList = xsh_imagelist_fit_polynomial( medList, Dit, nframes,
nx,
ny,
683 assure( polyList != NULL, cpl_error_get_code(),
684 "Cant fit polynomial - XSH version" ) ;
685 xsh_msg(
"Polynomial Fit Finished - XSH Version" ) ;
692 resBpmap, decode_bp )) ;
700 cpl_image *pplane = NULL ;
702 double mean = 0.0, stdev = 0.0, med = 0.0;
704 pplane = cpl_imagelist_get( polyList, i ) ;
705 assure( pplane != NULL, cpl_error_get_code(),
706 "Gant get plane Nb %d", i ) ;
712 xsh_msg(
"Sigma clipping on coefficient %d", i ) ;
713 for( iter = 0 ; iter<lin_clipping->
niter ; iter++ ) {
714#if defined(OPTIMIZE_IMAGE_HANDLING)
715 double *pdata = NULL ;
716 cpl_binary *pbpm = NULL ;
718 xsh_msg(
" Iteration Nb %d/%d", iter+1, lin_clipping->
niter ) ;
720 mean = cpl_image_get_mean( pplane ) ;
721 stdev = cpl_image_get_stdev( pplane ) ;
722 med = cpl_image_get_median( pplane ) ;
723 xsh_msg(
" Mean: %lf, stdev: %lf, Median: %lf",
730#if defined(OPTIMIZE_IMAGE_HANDLING)
731 pdata = cpl_image_get_data_double( pplane ) ;
732 assure( pdata != NULL, cpl_error_get_code(),
733 "Cant get pplane[%d] data", i ) ;
734 pbpm = cpl_mask_get_data( cpl_image_get_bpm( pplane ) ) ;
735 assure( pdata != NULL, cpl_error_get_code(),
736 "Cant get pplane[%d] bpm", i ) ;
738 for( iy = 0; iy <
ny ; iy++ )
739 for( ix = 0 ; ix <
nx ; ix++ ) {
740 double coeff = *(pdata + ix + (iy*
nx)) ;
741 cpl_binary rej = *(pbpm + ix + (iy*
nx)) ;
747 for( iy = 1 ; iy <=
ny ; iy++ )
748 for( ix = 1 ; ix<=
nx ; ix++ ) {
750 double coeff = cpl_image_get( pplane, ix, iy, &rej ) ;
757 if ( fabs( coeff - mean) > stdev*lin_clipping->
sigma ) {
759 ix, iy, mean, coeff ) ;
761#if defined(OPTIMIZE_IMAGE_HANDLING)
762 cpl_image_reject( pplane, ix+1, iy+1 ) ;
767 cpl_image_reject( pplane, ix, iy ) ;
776 if ( nbad == 0 ) break ;
790 xsh_msg(
"====> Total Non linear pixels: %d", totbad ) ;
793 int nbadpix = cpl_image_count_rejected( resBpmap ) ;
803 char bpmapFname[132] ;
806 sprintf( bpmapFname,
"LINEARITY_BAD_PIXEL_MAP_%s.fits", sarm ) ;
807 check_msg( cpl_image_save( resBpmap, bpmapFname, CPL_BPP_32_SIGNED,
808 bpmap_header, CPL_IO_DEFAULT ),
809 "Cant save resulting BpMap image" ) ;
817 CPL_FRAME_TYPE_IMAGE,
818 CPL_FRAME_GROUP_PRODUCT,
819 CPL_FRAME_LEVEL_FINAL));
824 if ( Dit != NULL ) cpl_free( Dit ) ;
static xsh_instrument * instrument
cpl_image * xsh_bpmap_collapse_bpmap_create(cpl_imagelist *liste, const int decode_bp)
void xsh_bpmap_set_bad_pixel(cpl_image *bpmap, int ix, int iy, int flag)
void xsh_set_image_cpl_bpmap(cpl_image *image, cpl_image *bpmap, const int decode_bp)
static int onoffCompare(const void *one, const void *two)
cpl_frame * xsh_compute_linearity(cpl_frameset *medSet, xsh_instrument *instrument, xsh_clipping_param *lin_clipping, const int decode_bp)
int xsh_linear_group_by_exptime(cpl_frameset *raws, xsh_instrument *instrument, double exp_toler, cpl_frameset **groupSet)
static int orderCompare(const void *one, const void *two)
cpl_frameset * xsh_subtract_on_off(cpl_frameset *set, xsh_instrument *instrument)
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
void xsh_pre_subtract(xsh_pre *self, const xsh_pre *right)
Subtract one PRE image from another The data units are subtracted data = data1 - data2 The error imag...
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
#define assure(CONDITION, ERROR_CODE,...)
#define check_msg(COMMAND,...)
cpl_imagelist * xsh_fit_imagelist_polynomial(const cpl_vector *x_pos, const cpl_imagelist *values, int mindeg, int maxdeg, cpl_boolean is_eqdist, cpl_image *fiterror)
Fit a polynomial to each pixel in a list of images.
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
#define xsh_msg(...)
Print a message on info level.
#define xsh_msg_dbg_low(...)
bool xsh_pfits_get_lamp_on_off(const cpl_propertylist *plist)
find out the Lamp status (ON/OFF)
double xsh_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
void xsh_pfits_set_qc_multi(cpl_propertylist *plist, void *value, const char *kw, xsh_instrument *instrument, int idx)
void xsh_pfits_set_qc(cpl_propertylist *plist, void *value, const char *kw, xsh_instrument *instrument)
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
#define QFLAG_NON_LINEAR_PIXEL
#define LINEAR_POLYNOMIAL_DEGREE
#define XSH_PRE_DATA_TYPE
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
#define XSH_LINEARITY_NIR
#define XSH_QC_BP_MAP_LINi_RMS
#define XSH_QC_BP_MAP_LINi_MEAN
#define XSH_QC_BP_MAP_LINi_MED
#define XSH_QC_BP_MAP_NBADPIX
cpl_polynomial * xsh_polynomial_fit_1d_create(const cpl_vector *x_pos, const cpl_vector *values, int degree, double *mse)