X-shooter Pipeline Reference Manual 3.8.15
test-xsh_compute_noise_map.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2013-01-31 08:05:57 $
23 * $Revision: 1.20 $
24 */
25#ifdef HAVE_CONFIG_H
26# include <config.h>
27#endif
28
29/*--------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------
43 Includes
44 ---------------------------------------------------------------------------*/
45
46
47#include <xsh_data_pre.h>
48#include <xsh_error.h>
49#include <xsh_msg.h>
50#include <xsh_data_instrument.h>
51#include <xsh_badpixelmap.h>
52#include <xsh_dfs.h>
53#include <xsh_pfits.h>
54#include <tests.h>
55#include <cpl.h>
56#include <math.h>
57#include <getopt.h>
58/*---------------------------------------------------------------------------
59 Defines
60 ---------------------------------------------------------------------------*/
61#define MODULE_ID "XSH_COMPUTE_NOISE_MAP"
62/*---------------------------------------------------------------------------
63 Functions prototypes
64 ---------------------------------------------------------------------------*/
65cpl_frameset * add_noisy_pixels( cpl_frameset *set,
67int verify_noisy( cpl_frame * frame, xsh_instrument* instrument);
68/*--------------------------------------------------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77cpl_frameset * add_noisy_pixels( cpl_frameset *set,
79{
80 /* Add a noisy pixel somewhere in the frame.
81 for a given pixel, take the avvg value of the frames
82 and add or subtract 1/2 of the value.
83 Should make a noisy pixel !
84 */
85 cpl_frameset * newset = NULL ;
86 int ix = 10, iy = 10 ;
87 int i = 0 ;
88 cpl_frame *current ;
89 float avg = 0., noise ;
90
91 cpl_frame_group group ;
92 /* Get the values at position ix,iy
93 Calculate average
94 Add noise
95 */
96 current = cpl_frameset_get_first( set ) ;
97 assure( current != NULL, cpl_error_get_code(),
98 "Cant get current" ) ;
99
100 group = cpl_frame_get_group( current ) ;
101
102 do {
103 xsh_pre * pre ;
104 int rej ;
105 float val ;
106
107 pre = xsh_pre_load( current, instrument ) ;
108 assure( pre != NULL, cpl_error_get_code(),
109 "Cant pre load" ) ;
111
112 val = cpl_image_get( pre->data, ix, iy, &rej ) ;
113 xsh_msg( "Current value (%d): %f", i, val ) ;
114 avg += val ;
115 xsh_pre_free( &pre ) ;
116 i++ ;
117 } while( (current = cpl_frameset_get_next( set )) != NULL ) ;
118 avg /= (float)i ;
119 xsh_msg( " avg: %f", avg ) ;
120
121 noise = avg/2. ;
122
123 current = cpl_frameset_get_first( set ) ;
124 newset = cpl_frameset_new();
125 assure( newset != NULL, cpl_error_get_code(),
126 "Cant create new framesey" ) ;
127 i = 0 ;
128 do {
129 xsh_pre * pre ;
130 char fname[128] ;
131 char tag[128] ;
132 cpl_frame * noisy = NULL ;
133
134 pre = xsh_pre_load( current, instrument ) ;
135 cpl_image_set( pre->data, ix, iy, noise ) ;
136 xsh_msg( " --> new value (%d): %f", i, noise ) ;
137 sprintf( fname, "noisy_%d.fits", i ) ;
138 sprintf( tag, "noisy_%d", i ) ;
139 xsh_msg( "Save frame %s", fname ) ;
140 noisy = xsh_pre_save( pre, fname, tag,1 ) ;
141 assure( noisy != NULL, cpl_error_get_code(),
142 "Cant save %s", fname ) ;
143 cpl_frame_set_filename( noisy, fname);
144 cpl_frame_set_group( noisy, group);
145 cpl_frame_set_tag( noisy, tag);
146 noise *= 2. ;
147 xsh_pre_free( &pre ) ;
148 i++ ;
149 //xsh_msg( "newset: %x - noisy: %x", newset, noisy ) ;
150 check_msg( cpl_frameset_insert( newset, noisy ),
151 "Cant insert noisy frame into newset" ) ;
152 } while( (current = cpl_frameset_get_next( set ) ) != NULL ) ;
153
154 cleanup:
155 return newset ;
156}
157
158static cpl_frame* create_frame(const char* name,int nx, int ny,
159 const char* tag, cpl_frame_group group,
161 int norm )
162{
163
164 XSH_INSTRCONFIG * iconfig = NULL ;
165 cpl_propertylist* header = NULL;
166 cpl_image* data = NULL;
167 cpl_frame *frame = NULL;
168
170 header = cpl_propertylist_new();
171 setHeader(header, iconfig, nx, ny, 1. );
172 xsh_msg("name=%s",name);
173 check(xsh_pfits_set_dit (header,1.));
174 check(xsh_pfits_set_ndit (header,1));
175 /* Special NIR */
176 check_msg( cpl_propertylist_append_double( header, XSH_DET_PXSPACE,
177 iconfig->pxspace),
178 "Cant append GAIN" ) ;
179
180 data = cpl_image_new(nx,ny,XSH_PRE_DATA_TYPE);
181 cpl_image_fill_gaussian(data,
182 nx / 3.0, ny / 3.0, /* center */
183 norm, /* norm */
184 nx, ny / 8.0); /* sigma */
185 cpl_image_save(data,name,XSH_PRE_DATA_BPP,header,
186 CPL_IO_DEFAULT);
187
188 /* Create test frame */
189 frame = cpl_frame_new();
190 cpl_frame_set_filename(frame,name);
191 cpl_frame_set_group(frame,group);
192 cpl_frame_set_tag(frame,tag);
193
194 cleanup:
195 xsh_free_propertylist(&header);
197
198 return frame;
199}
200
201int verify_noisy( cpl_frame * frame, xsh_instrument* instrument )
202{
203 /* One should find 1 noisy pixel in the Qual image of the frame */
204 xsh_pre *pre = NULL ;
205 int count = 0 ;
206
207 pre = xsh_pre_load( frame, instrument ) ;
208 assure( pre != NULL, cpl_error_get_code(),
209 "Cant pre load" ) ;
210 /* Get the QC parameter */
211 /* Check how many bad pixels in QUAL image */
212 count = xsh_bpmap_count( pre->qual, pre->nx, pre->ny ) ;
213 xsh_msg( "Number of bad pixels: %d (should be 1)", count ) ;
214 xsh_pre_free( &pre ) ;
215
216 cleanup:
217 if ( count == 1 ) return 0;
218 else return 1 ;
219}
220
221int main(void)
222{
223 xsh_instrument * instrument = NULL ;
224 cpl_frameset* set = NULL;
225 cpl_frameset *newset = NULL ;
226 cpl_frame* medFrame = NULL;
227 cpl_frame* Master = NULL;
228 cpl_frame* frame = NULL;
229#if defined(PICKUP_NOISE_HOT_PIXEL_MAP)
230 cpl_frame * noise_map = NULL ;
231#endif
233 xsh_clipping_param noise_clipping ;
234 cpl_imagelist *Liste ;
235 int i = 0;
236 int nx = 100, ny = 100 ;
237 int nframes = 3 ;
238 int norme = 48 ;
239 xsh_stack_param stack_param = {"median",5.,5.};
240 int decode_bp=QFLAG_OUTSIDE_DATA_RANGE;
241 /* Initialize libraries */
244
245 /* Create valid instrument */
250 /* Create three frames in frameset */
251 set = cpl_frameset_new();
252
253 for(i=0;i<nframes;i++){
254 char framename[256];
255 sprintf(framename,"frame%d.fits",i);
256 frame = create_frame(framename, nx, ny,
257 XSH_DARK_NIR,CPL_FRAME_GROUP_RAW, instrument,
258 norme );
259 cpl_frameset_insert(set,frame);
260 norme += 2 ;
261 }
262
263 /* USE prepare function (no BpMap) */
264 check(xsh_prepare(set, NULL, NULL,"PRE",instrument,0,CPL_FALSE));
265
266 /* Add a few noisy pixels in the frames */
267 newset = add_noisy_pixels( set, instrument ) ;
268
269 /* Set the crh_clipping parameters (default values ) */
270 crh_clipping.sigma = 5. ;
271 crh_clipping.niter = 2 ;
272 crh_clipping.frac = 0.7 ;
273
274 /* Set the noise_clipping parameters (default values ) */
275 noise_clipping.sigma = 5. ;
276 noise_clipping.niter = 3 ;
277 noise_clipping.frac = 0.7 ;
278 noise_clipping.diff = 0.7 ;
279
280 /* run remove_crh_multi (should not found any Cosmic !) */
281 xsh_msg("param method=%s",stack_param.stack_method);
282 check( medFrame = xsh_remove_crh_multiple( newset, "remove_crh.fits",
283 &stack_param,&crh_clipping,
285 &Liste,NULL,0 ) ) ;
286
287 /* TEST1 : test the compute_noise_map function */
288#if defined(PICKUP_NOISE_HOT_PIXEL_MAP)
289 check( Master = xsh_compute_noise_map( Liste, medFrame, &noise_clipping,
290 instrument,&noise_map ) ) ;
291#else
292 check( Master = xsh_compute_noise_map( Liste, medFrame, &noise_clipping,
293 instrument) ) ;
294#endif
295 /* Verify ==> check that the QUAL of the master contains the correct
296 number of noisy pixels
297 */
298 verify_noisy( Master, instrument ) ;
299
300 xsh_msg("Compute Noise OK");
301
302cleanup:
304 xsh_free_frameset(&set);
305 xsh_free_frameset(&newset);
306 xsh_free_frame(&medFrame);
307 xsh_free_frame( &Master ) ;
308 xsh_free_frame(&noise_map);
309 xsh_free_imagelist(&Liste);
311
313 if (cpl_error_get_code() != CPL_ERROR_NONE) {
314 xsh_error_dump(CPL_MSG_ERROR);
315 return 1;
316 } else {
317 return 0;
318 }
319}
320
static cpl_frame * create_frame(const char *name, int nx, int ny, const char *tag, cpl_frame_group group, xsh_instrument *instrument, int norm)
int main(void)
#define MODULE_ID
int verify_noisy(cpl_frame *frame, xsh_instrument *instrument)
cpl_frameset * add_noisy_pixels(cpl_frameset *set, xsh_instrument *instrument)
Unit test of XSH_SUBTRACT.
static xsh_instrument * instrument
static xsh_clipping_param crh_clipping
void setHeader(cpl_propertylist *header, XSH_INSTRCONFIG *iconfig, int nx, int ny, double exptime)
Definition: tests.c:288
int xsh_bpmap_count(cpl_image *bpmap, int nx, int ny)
cpl_frame * xsh_compute_noise_map(cpl_imagelist *dataList, cpl_frame *medFrame, xsh_clipping_param *noise_clipping, xsh_instrument *instr, cpl_frame **noisyFrame)
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
Definition: xsh_data_pre.c:849
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
void xsh_prepare(cpl_frameset *frames, cpl_frame *bpmap, cpl_frame *mbias, const char *prefix, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
This function transform RAW frames dataset in PRE frames dataset attaching the default bad pixel map ...
Definition: xsh_prepare.c:122
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_set_recipe_id(xsh_instrument *instrument, const char *recipe_id)
Set the recipe_id into the instrument structure.
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
XSH_INSTRCONFIG * xsh_instrument_get_config(xsh_instrument *i)
Get the instrument default set of keywords.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
xsh_instrument * xsh_instrument_new(void)
create new instrument structure
void xsh_instrument_set_decode_bp(xsh_instrument *i, const int decode_bp)
Set bad pixel code.
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_pfits_set_dit(cpl_propertylist *plist, double dit)
Write the DIT value.
Definition: xsh_pfits.c:1352
void xsh_pfits_set_ndit(cpl_propertylist *plist, int ndit)
Write the NDIT value.
Definition: xsh_pfits.c:1369
cpl_frame * xsh_remove_crh_multiple(cpl_frameset *rawFrames, const char *name, xsh_stack_param *stack_param, xsh_clipping_param *crh_clipping, xsh_instrument *inst, cpl_imagelist **, cpl_image **, const int save_tmp)
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
Definition: xsh_utils.c:2254
void xsh_free_temporary_files(void)
Free temprary files list.
Definition: xsh_utils.c:1451
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
Definition: xsh_utils.c:2164
cpl_image * qual
Definition: xsh_data_pre.h:71
cpl_image * data
Definition: xsh_data_pre.h:65
const char * stack_method
#define TESTS_CLEAN_WORKSPACE(DRL_ID)
Definition: tests.h:139
#define TESTS_INIT_WORKSPACE(DRL_ID)
Definition: tests.h:133
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
#define QFLAG_OUTSIDE_DATA_RANGE
@ XSH_ARM_NIR
@ XSH_MODE_IFU
#define XSH_PRE_DATA_TYPE
Definition: xsh_data_pre.h:42
#define XSH_PRE_DATA_BPP
Definition: xsh_data_pre.h:43
int nx
int ny
#define XSH_DARK_NIR
Definition: xsh_dfs.h:400
#define XSH_DET_PXSPACE
Definition: xsh_pfits.h:164