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 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029
00039
00042
00043
00044
00045
00046
00047 #include <xsh_data_pre.h>
00048 #include <xsh_error.h>
00049 #include <xsh_msg.h>
00050 #include <xsh_data_instrument.h>
00051 #include <xsh_badpixelmap.h>
00052 #include <xsh_dfs.h>
00053 #include <xsh_pfits.h>
00054 #include <tests.h>
00055 #include <cpl.h>
00056 #include <math.h>
00057
00058
00059
00060
00061 #define MODULE_ID "XSH_COMPUTE_NOISE_MAP"
00062
00063
00064
00065 cpl_frameset * add_noisy_pixels( cpl_frameset *set,
00066 xsh_instrument* instrument);
00067 int verify_noisy( cpl_frame * frame, xsh_instrument* instrument);
00068
00075
00076
00077 cpl_frameset * add_noisy_pixels( cpl_frameset *set,
00078 xsh_instrument* instrument )
00079 {
00080
00081
00082
00083
00084
00085 cpl_frameset * newset = NULL ;
00086 int ix = 10, iy = 10 ;
00087 int i = 0 ;
00088 cpl_frame *current ;
00089 float avg = 0., noise ;
00090 const char* tag ;
00091 cpl_frame_group group ;
00092
00093
00094
00095
00096 current = cpl_frameset_get_first( set ) ;
00097 assure( current != NULL, cpl_error_get_code(),
00098 "Cant get current" ) ;
00099 tag = cpl_frame_get_tag( current ) ;
00100 group = cpl_frame_get_group( current ) ;
00101
00102 do {
00103 xsh_pre * pre ;
00104 int rej ;
00105 float val ;
00106
00107 pre = xsh_pre_load( current, instrument ) ;
00108 assure( pre != NULL, cpl_error_get_code(),
00109 "Cant pre load" ) ;
00110 xsh_instrument_set_recipe_id( instrument, "xsh_mdark" ) ;
00111
00112 val = cpl_image_get( pre->data, ix, iy, &rej ) ;
00113 xsh_msg( "Current value (%d): %f", i, val ) ;
00114 avg += val ;
00115 xsh_pre_free( &pre ) ;
00116 i++ ;
00117 } while( (current = cpl_frameset_get_next( set )) != NULL ) ;
00118 avg /= (float)i ;
00119 xsh_msg( " avg: %f", avg ) ;
00120
00121 noise = avg/2. ;
00122
00123 current = cpl_frameset_get_first( set ) ;
00124 newset = cpl_frameset_new();
00125 assure( newset != NULL, cpl_error_get_code(),
00126 "Cant create new framesey" ) ;
00127 i = 0 ;
00128 do {
00129 xsh_pre * pre ;
00130 char fname[128] ;
00131 char tag[128] ;
00132 cpl_frame * noisy = NULL ;
00133
00134 pre = xsh_pre_load( current, instrument ) ;
00135 cpl_image_set( pre->data, ix, iy, noise ) ;
00136 xsh_msg( " --> new value (%d): %f", i, noise ) ;
00137 sprintf( fname, "noisy_%d.fits", i ) ;
00138 sprintf( tag, "noisy_%d", i ) ;
00139 xsh_msg( "Save frame %s", fname ) ;
00140 noisy = xsh_pre_save( pre, fname, tag,1 ) ;
00141 assure( noisy != NULL, cpl_error_get_code(),
00142 "Cant save %s", fname ) ;
00143 cpl_frame_set_filename( noisy, fname);
00144 cpl_frame_set_group( noisy, group);
00145 cpl_frame_set_tag( noisy, tag);
00146 noise *= 2. ;
00147 xsh_pre_free( &pre ) ;
00148 i++ ;
00149
00150 check_msg( cpl_frameset_insert( newset, noisy ),
00151 "Cant insert noisy frame into newset" ) ;
00152 } while( (current = cpl_frameset_get_next( set ) ) != NULL ) ;
00153
00154 cleanup:
00155 return newset ;
00156 }
00157
00158 static cpl_frame* create_frame(const char* name,int nx, int ny,
00159 const char* tag, cpl_frame_group group,
00160 xsh_instrument* instrument,
00161 int norm )
00162 {
00163
00164 XSH_INSTRCONFIG * iconfig = NULL ;
00165 cpl_propertylist* header = NULL;
00166 cpl_image* data = NULL;
00167 cpl_frame *frame = NULL;
00168
00169 iconfig = xsh_instrument_get_config( instrument ) ;
00170 header = cpl_propertylist_new();
00171 setHeader(header, iconfig, nx, ny, 1. );
00172 xsh_msg("name=%s",name);
00173 check(xsh_pfits_set_dit (header,1.));
00174 check(xsh_pfits_set_ndit (header,1));
00175
00176 check_msg( cpl_propertylist_append_double( header, XSH_DET_PXSPACE,
00177 iconfig->pxspace),
00178 "Cant append GAIN" ) ;
00179
00180 data = cpl_image_new(nx,ny,XSH_PRE_DATA_TYPE);
00181 cpl_image_fill_gaussian(data,
00182 nx / 3.0, ny / 3.0,
00183 norm,
00184 nx, ny / 8.0);
00185 cpl_image_save(data,name,XSH_PRE_DATA_BPP,header,
00186 CPL_IO_DEFAULT);
00187
00188
00189 frame = cpl_frame_new();
00190 cpl_frame_set_filename(frame,name);
00191 cpl_frame_set_group(frame,group);
00192 cpl_frame_set_tag(frame,tag);
00193
00194 cleanup:
00195 xsh_free_propertylist(&header);
00196 xsh_free_image(&data);
00197
00198 return frame;
00199 }
00200
00201 int verify_noisy( cpl_frame * frame, xsh_instrument* instrument )
00202 {
00203
00204 xsh_pre *pre = NULL ;
00205 int count = 0 ;
00206
00207 pre = xsh_pre_load( frame, instrument ) ;
00208 assure( pre != NULL, cpl_error_get_code(),
00209 "Cant pre load" ) ;
00210
00211
00212 count = xsh_bpmap_count( pre->qual, pre->nx, pre->ny ) ;
00213 xsh_msg( "Number of bad pixels: %d (should be 1)", count ) ;
00214 xsh_pre_free( &pre ) ;
00215
00216 cleanup:
00217 if ( count == 1 ) return 0;
00218 else return 1 ;
00219 }
00220
00221 int main(void)
00222 {
00223 xsh_instrument * instrument = NULL ;
00224 cpl_frameset* set = NULL;
00225 cpl_frameset *newset = NULL ;
00226 cpl_frame* medFrame = NULL;
00227 cpl_frame* Master = NULL;
00228 cpl_frame* frame = NULL;
00229 #if defined(PICKUP_NOISE_HOT_PIXEL_MAP)
00230 cpl_frame * noise_map = NULL ;
00231 #endif
00232 xsh_clipping_param crh_clipping ;
00233 xsh_clipping_param noise_clipping ;
00234 cpl_imagelist *Liste ;
00235 int i = 0;
00236 int nx = 100, ny = 100 ;
00237 int nframes = 3 ;
00238 int norme = 48 ;
00239 xsh_stack_param stack_param = {"median",5.,5.};
00240 int decode_bp=QFLAG_OUTSIDE_DATA_RANGE;
00241
00242 TESTS_INIT(MODULE_ID);
00243
00244
00245 instrument = xsh_instrument_new() ;
00246 xsh_instrument_set_mode( instrument, XSH_MODE_IFU ) ;
00247 xsh_instrument_set_arm( instrument, XSH_ARM_NIR ) ;
00248 xsh_instrument_set_decode_bp( instrument, decode_bp ) ;
00249
00250 set = cpl_frameset_new();
00251
00252 for(i=0;i<nframes;i++){
00253 char framename[256];
00254 sprintf(framename,"frame%d.fits",i);
00255 frame = create_frame(framename, nx, ny,
00256 XSH_DARK_NIR,CPL_FRAME_GROUP_RAW, instrument,
00257 norme );
00258 cpl_frameset_insert(set,frame);
00259 norme += 2 ;
00260 }
00261
00262
00263 check(xsh_prepare(set, NULL, NULL,"PRE",instrument,0,CPL_FALSE));
00264
00265
00266 newset = add_noisy_pixels( set, instrument ) ;
00267
00268
00269 crh_clipping.sigma = 5. ;
00270 crh_clipping.niter = 2 ;
00271 crh_clipping.frac = 0.7 ;
00272
00273
00274 noise_clipping.sigma = 5. ;
00275 noise_clipping.niter = 3 ;
00276 noise_clipping.frac = 0.7 ;
00277 noise_clipping.diff = 0.7 ;
00278
00279
00280 xsh_msg("param method=%s",stack_param.stack_method);
00281 check( medFrame = xsh_remove_crh_multiple( newset, "remove_crh.fits",
00282 &stack_param,NULL,
00283 instrument,
00284 &Liste,NULL,0 ) ) ;
00285
00286
00287 #if defined(PICKUP_NOISE_HOT_PIXEL_MAP)
00288 check( Master = xsh_compute_noise_map( Liste, medFrame, &noise_clipping,
00289 instrument,&noise_map ) ) ;
00290 #else
00291 check( Master = xsh_compute_noise_map( Liste, medFrame, &noise_clipping,
00292 instrument) ) ;
00293 #endif
00294
00295
00296
00297 verify_noisy( Master, instrument ) ;
00298
00299 xsh_msg("Compute Noise OK");
00300
00301 cleanup:
00302 xsh_instrument_free(&instrument);
00303 xsh_free_frameset(&set);
00304 xsh_free_frameset(&newset);
00305 xsh_free_frame(&medFrame);
00306 xsh_free_frame( &Master ) ;
00307
00308 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00309 xsh_error_dump(CPL_MSG_ERROR);
00310 return 1;
00311 } else {
00312 return 0;
00313 }
00314 }
00315