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_dfs.h>
00052 #include <xsh_pfits.h>
00053 #include <tests.h>
00054 #include <cpl.h>
00055 #include <math.h>
00056
00057
00058
00059
00060 #define MODULE_ID "XSH_COMPUTE_NOISE_MAP"
00061
00062
00063
00064 cpl_frameset * add_noisy_pixels( cpl_frameset *set,
00065 xsh_instrument* instrument);
00066 int verify_noisy( cpl_frame * frame, xsh_instrument* instrument);
00067
00074
00075
00076 cpl_frameset * add_noisy_pixels( cpl_frameset *set,
00077 xsh_instrument* instrument )
00078 {
00079
00080
00081
00082
00083
00084 cpl_frameset * newset = NULL ;
00085 int ix = 10, iy = 10 ;
00086 int i = 0 ;
00087 cpl_frame *current ;
00088 float avg = 0., noise ;
00089 const char* tag ;
00090 cpl_frame_group group ;
00091
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
00175 check_msg( cpl_propertylist_append_double( header, XSH_DET_PXSPACE,
00176 iconfig->pxspace),
00177 "Cant append GAIN" ) ;
00178
00179 data = cpl_image_new(nx,ny,XSH_PRE_DATA_TYPE);
00180 cpl_image_fill_gaussian(data,
00181 nx / 3.0, ny / 3.0,
00182 norm,
00183 nx, ny / 8.0);
00184 cpl_image_save(data,name,XSH_PRE_DATA_BPP,header,
00185 CPL_IO_DEFAULT);
00186
00187
00188 frame = cpl_frame_new();
00189 cpl_frame_set_filename(frame,name);
00190 cpl_frame_set_group(frame,group);
00191 cpl_frame_set_tag(frame,tag);
00192
00193 cleanup:
00194 xsh_free_propertylist(&header);
00195 xsh_free_image(&data);
00196
00197 return frame;
00198 }
00199
00200 int verify_noisy( cpl_frame * frame, xsh_instrument* instrument )
00201 {
00202
00203 xsh_pre *pre = NULL ;
00204 int count = 0 ;
00205
00206 pre = xsh_pre_load( frame, instrument ) ;
00207 assure( pre != NULL, cpl_error_get_code(),
00208 "Cant pre load" ) ;
00209
00210
00211 count = xsh_bpmap_count( pre->qual, pre->nx, pre->ny ) ;
00212 xsh_msg( "Number of bad pixels: %d (should be 1)", count ) ;
00213 xsh_pre_free( &pre ) ;
00214
00215 cleanup:
00216 if ( count == 1 ) return 0;
00217 else return 1 ;
00218 }
00219
00220 int main(void)
00221 {
00222 xsh_instrument * instrument = NULL ;
00223 cpl_frameset* set = NULL;
00224 cpl_frameset *newset = NULL ;
00225 cpl_frame* medFrame = NULL;
00226 cpl_frame* Master = NULL;
00227 cpl_frame* frame = NULL;
00228 #if defined(PICKUP_NOISE_HOT_PIXEL_MAP)
00229 cpl_frame * noise_map = NULL ;
00230 #endif
00231 xsh_clipping_param crh_clipping ;
00232 xsh_clipping_param noise_clipping ;
00233 cpl_imagelist *Liste ;
00234 int i = 0;
00235 int nx = 100, ny = 100 ;
00236 int nframes = 3 ;
00237 int norme = 48 ;
00238
00239
00240 TESTS_INIT(MODULE_ID);
00241
00242
00243 instrument = xsh_instrument_new() ;
00244 xsh_instrument_set_mode( instrument, XSH_MODE_IFU ) ;
00245 xsh_instrument_set_arm( instrument, XSH_ARM_NIR ) ;
00246
00247
00248 set = cpl_frameset_new();
00249
00250 for(i=0;i<nframes;i++){
00251 char framename[256];
00252 sprintf(framename,"frame%d.fits",i);
00253 frame = create_frame(framename, nx, ny,
00254 XSH_DARK_NIR,CPL_FRAME_GROUP_RAW, instrument,
00255 norme );
00256 cpl_frameset_insert(set,frame);
00257 norme += 2 ;
00258 }
00259
00260
00261 check(xsh_prepare(set, NULL, NULL,"PRE",instrument,0));
00262
00263
00264 newset = add_noisy_pixels( set, instrument ) ;
00265
00266
00267 crh_clipping.sigma = 5. ;
00268 crh_clipping.niter = 2 ;
00269 crh_clipping.frac = 0.7 ;
00270
00271
00272 noise_clipping.sigma = 5. ;
00273 noise_clipping.niter = 3 ;
00274 noise_clipping.frac = 0.7 ;
00275 noise_clipping.diff = 0.7 ;
00276
00277
00278 check( medFrame = xsh_remove_crh_multiple( newset, "remove_crh.fits",
00279 &crh_clipping, instrument,
00280 &Liste,NULL ) ) ;
00281
00282
00283 #if defined(PICKUP_NOISE_HOT_PIXEL_MAP)
00284 check( Master = xsh_compute_noise_map( Liste, medFrame, &noise_clipping,
00285 instrument, &noise_map ) ) ;
00286 #else
00287 check( Master = xsh_compute_noise_map( Liste, medFrame, &noise_clipping,
00288 instrument ) ) ;
00289 #endif
00290
00291
00292
00293 verify_noisy( Master, instrument ) ;
00294
00295 xsh_msg("Compute Noise OK");
00296
00297 cleanup:
00298 xsh_instrument_free(&instrument);
00299 xsh_free_frameset(&set);
00300 xsh_free_frameset(&newset);
00301 xsh_free_frame(&medFrame);
00302 xsh_free_frame( &Master ) ;
00303
00304 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00305 xsh_error_dump(CPL_MSG_ERROR);
00306 return 1;
00307 } else {
00308 return 0;
00309 }
00310 }
00311