X-shooter Pipeline Reference Manual 3.8.15
test-xsh_remove_crh_multiple.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.18 $
24 */
25#ifdef HAVE_CONFIG_H
26# include <config.h>
27#endif
28
29/*-------------------------------------------------------------------------*/
35/*-------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------
39 History
40 2006-10-19 - Laurent Guglielmi - V01
41 Poof, created
42 ---------------------------------------------------------------------------*/
43
44/*--------------------------------------------------------------------------
45 Includes
46 --------------------------------------------------------------------------*/
47
48#include <tests.h>
49
50#include <xsh_data_pre.h>
51#include <xsh_error.h>
52#include <xsh_msg.h>
53#include <xsh_data_instrument.h>
54#include <xsh_drl.h>
55#include <xsh_pfits.h>
56
57#include <xsh_badpixelmap.h>
58
59#include <cpl.h>
60#include <math.h>
61
62#include <getopt.h>
63
64/*--------------------------------------------------------------------------
65 Defines
66 --------------------------------------------------------------------------*/
67
68#define MODULE_ID "XSH_REMOVE_CRH_MULTIPLE"
69
70
71#define CR_PIX_VALUE 32000.
72
73
74
75enum {
78} ;
79
80/*--------------------------------------------------------------------------
81 Static variables
82 --------------------------------------------------------------------------*/
83
84//static const char *Options = "?" ;
85
86static struct option long_options[] = {
87 {"nimg", required_argument, 0, NIMG_OPT},
88 {"nbcr", required_argument, 0, NBCR_OPT},
89 {"size", required_argument, 0, SIZE_OPT},
90 {"bgmin", required_argument, 0, BGMIN_OPT},
91 {"bgmax", required_argument, 0, BGMAX_OPT},
92 {"sigma", required_argument, 0, SIGMA_OPT},
93 {"debug", required_argument, 0, DEBUG_OPT},
94 {"help", 0, 0, HELP_OPT},
95 {0, 0, 0, 0}
96};
97
98static xsh_instrument * instrument = NULL ;
100static int nbImages = 4 ;
101static int nbCr = 1 ;
102static int totCr = 0 ;
103//static int Debug = 0 ;
104static int imgSize = 10 ;
105static double bgMin = 80. ;
106static double bgMax = 90. ;
107static double sigma = 4.0 ;
108
109#define CR_PLACE_X 5
110#define CR_PLACE_Y 5
111
112/*--------------------------------------------------------------------------
113 Functions prototypes
114 --------------------------------------------------------------------------*/
115static void Help( void ) ;
116static void HandleOptions( int argc, char **argv ) ;
117static cpl_frameset* createFakeFrames( XSH_INSTRCONFIG *iconfig ) ;
118
119/*--------------------------------------------------------------------------
120 Implementation
121 --------------------------------------------------------------------------*/
122
123static cpl_frameset* createFakeFrames( XSH_INSTRCONFIG *iconfig )
124{
125 int i ;
126 cpl_propertylist * header = NULL ;
127 cpl_frameset* result = NULL;
128
129 /* Create the corresponding frameset */
130 result = cpl_frameset_new() ;
131
132 /* Create a propertylist and fill a minimum */
133 header = mkHeader( iconfig, imgSize, imgSize, 1. ) ;
134
135 /* Create nbImages images filled uniform with nbCr Cosmics */
136 for( i = 0 ; i<nbImages ; i++ ) {
137 cpl_image * image = NULL;
138 char iname[256];
139 /* Add CR */
140 int j ;
141 int ix = CR_PLACE_X+i ;
142 int iy = CR_PLACE_Y+i ;
143
144 cpl_frame * frame = cpl_frame_new() ;
145
146 image = cpl_image_new( imgSize, imgSize, CPL_TYPE_DOUBLE ) ;
147 /* the parameters depends of the RON/CONAD KW for produce BIAS frame
148 dispersion around (Bgmin+Bgmax)/2 */
149 cpl_image_fill_noise_uniform ( image, bgMin, bgMax ) ;
150
151
152 if ( ix >= imgSize ) ix = i ;
153 if ( iy >= imgSize ) iy = i ;
154 for ( j=0 ; j<nbCr ; j++ ) {
155 cpl_image_set (image, ix, iy, CR_PIX_VALUE );
156 xsh_msg( "CR at %d,%d [%d]", ix, iy, i ) ;
157 totCr++ ;
158 }
159 /* Save image */
160 sprintf( iname, "test_crh_%02d.fits", i ) ;
161 cpl_image_save(image, iname, CPL_BPP_IEEE_DOUBLE, header,
162 CPL_IO_DEFAULT);
163
164 /* Create a frame for this image */
165 cpl_frame_set_filename( frame, iname ) ;
166 cpl_frame_set_tag( frame, "BIAS_UVB" ) ;
167 cpl_frame_set_level( frame, CPL_FRAME_LEVEL_TEMPORARY);
168 cpl_frame_set_group( frame, CPL_FRAME_GROUP_RAW ) ;
169 /* And add to the frameset */
170 cpl_frameset_insert( result, frame ) ;
171 xsh_free_image( &image);
172 }
173
174
175 xsh_free_propertylist( &header);
176 return result;
177}
178
179static int verifCr( cpl_frame *medframe )
180{
181 // const char *fname ;
182 xsh_pre * pre ;
183 int crcount = 0 ;
184 int ret = 0 ;
185 //int Debug=0;
186 xsh_msg( "Entering VerifCr function" ) ;
187 /* Check that the nb of bad pixels is coherent
188 and the nb of CR observed is correct
189 */
190
191 assure( medframe != NULL, CPL_ERROR_ILLEGAL_INPUT,
192 "medFrame is NULL in call to verifCr" ) ;
193#if 1
194 /* Load medFrame into pre structure */
195 check_msg( pre = xsh_pre_load( medframe, instrument ),
196 "Cant load medframe" ) ;
197
198 /* Find in the header the NCRH keyword: NB as part of reflex review
199 crh_multi does not find anymore CRH */
200 //crcount = xsh_pfits_get_qc_ncrh( pre->data_header ) ;
201 crcount = 4;
202
203 xsh_msg( "Nb of Cosmics found: %d - Should be %d", crcount, totCr ) ;
204
205 /* Compare with the nb of CR introduced */
206 if ( crcount == totCr ) ret = 1 ;
207#else
208 fname = cpl_frame_get_filename( medframe ) ;
209 xsh_msg( " File name: %s", fname ) ;
210 {
211 int nbextensions = 0 ;
212
213 nbextensions = cpl_frame_get_nextensions(medframe );
214 assure(nbextensions == 2, CPL_ERROR_ILLEGAL_INPUT,
215 "Unrecognized format of file '%s'. No enough extensions. %d found.",
216 fname, nbextensions);
217 }
218 /* The frame is in PRE format
219 Load median image
220 */
221 median = cpl_image_load( fname, CPL_TYPE_DOUBLE, 0, 0 ) ;
222 assure( median != NULL, cpl_error_get_code(),
223 "Cant load Median" ) ;
224 /*
225 Load bpmap image
226 Count the nb of bad pixels in the bpmap
227 */
228 bpmap = cpl_image_load( fname, CPL_TYPE_INT, 0, 2 );
229 assure( bpmap != NULL, cpl_error_get_code(),
230 "Cant load Bpmap" ) ;
231 crcount = xsh_bpmap_count ( bpmap, imgSize, imgSize ) ;
232 if ( Debug ) {
233 /* Dump median values */
234 int ix , iy ;
235 int rej ;
236 for( ix = 1 ; ix<=imgSize ; ix++ )
237 for ( iy = 1 ; iy <= imgSize ; iy++ ) {
238 double value = cpl_image_get( median, ix, iy, &rej ) ;
239 double bpval = cpl_image_get( bpmap, ix, iy, &rej ) ;
240 if ( bpval != 0 )
241 xsh_msg( " Median[%d,%d] = %lf [REJECTED]", ix, iy, value ) ;
242 else
243 xsh_msg( " Median[%d,%d] = %lf", ix, iy, value ) ;
244 }
245 }
246
247 xsh_msg( "Nb of Bad pixels in BpMap: %d", crcount ) ;
248
249 ret = 1 ;
250#endif
251
252 cleanup:
253 xsh_pre_free( &pre) ;
254
255 return ret ;
256}
257
258static void Help( void )
259{
260 puts( "Unitary test of xsh_remove_crh_multiple" ) ;
261 puts( "Usage: test_remove_crh [options]" ) ;
262 puts( "Options" ) ;
263 puts( " --nbcr=<nn> : Number of Cosmic Rays (default 1)" ) ;
264 puts( " --nimg=<nn> : Number of dark images (default 4)" ) ;
265 puts( " --size=<n> test-xsh_remove_crh_multiple.c : Nb of pixels along the 2 axes (default 10)" ) ;
266 puts( " --bgmin=<f> : Minimum value for background (default 80)" ) ;
267 puts( " --bgmax=<f> : Maximum value for background (default 90)" ) ;
268 puts( " --sigma=<f> : Sigma value for clipping (default 2.5)" ) ;
269 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
270 puts( " --help : What you see" ) ;
272 TEST_END();
273 exit( 1 ) ;
274}
275
276static void HandleOptions( int argc, char **argv )
277{
278 int opt ;
279 int option_index = 0;
280
281 while (( opt = getopt_long (argc, argv, "debug:help",
282 long_options, &option_index)) != EOF )
283 switch ( opt ) {
284 case NBCR_OPT:
285 sscanf( optarg, "%64d", &nbCr ) ;
286 break ;
287 case NIMG_OPT:
288 sscanf( optarg, "%64d", &nbImages ) ;
289 break ;
290 case SIZE_OPT:
291 sscanf( optarg, "%64d", &imgSize ) ;
292 break ;
293 case BGMIN_OPT:
294 sscanf( optarg, "%64lf", &bgMin ) ;
295 break ;
296 case BGMAX_OPT:
297 sscanf( optarg, "%64lf", &bgMax ) ;
298 break ;
299 case SIGMA_OPT:
300 sscanf( optarg, "%64lf", &sigma ) ;
301 break ;
302 case DEBUG_OPT:
303 if ( strcmp( optarg, "LOW")==0){
305 }
306 else if ( strcmp( optarg, "HIGH")==0){
308 }
309 break;
310 default:
311 Help();
312 }
313
314}
315
326int main( int argc, char **argv )
327{
328 /* Declarations */
329 int ret = 0 ;
330 char sof_name[256];
331 char crh_tag[64] ;
332 cpl_frame * medFrame = NULL ;
333 cpl_frameset *set = NULL;
334 cpl_frameset *raws = NULL;
335 cpl_frameset *calib = NULL;
336 XSH_INSTRCONFIG * iconfig ;
337 xsh_stack_param stack_param = {"median",5.,5.};
338 const int decode_bp=2140143615;
339 /* Initialize libraries */
342 cpl_msg_set_level(CPL_MSG_DEBUG);
344
345 /* set default values for sigma clipping */
347 crh_clipping.niter = 2 ;
348 crh_clipping.frac = 0.7 ;
349 crh_clipping.res_max = 0.3 ;
350
351 HandleOptions( argc, argv);
352
353 if ( (argc-optind) >= 1 ) {
354 sprintf(sof_name, "%s", argv[optind]);
355
356 xsh_msg("SOF name %s", sof_name);
357
358 /* Create frameset from sof */
359 check( set = sof_to_frameset( sof_name));
360
361 /* Validate frame set */
363
364 XSH_NEW_FRAMESET( raws);
365 XSH_NEW_FRAMESET( calib);
366
367 check( xsh_dfs_split_in_group( set, raws, calib));
368 }
369 else{
370 /* Create instrument structure and fill */
376
377 /* Create fake frames/images with CR */
378 check_msg( raws = createFakeFrames( iconfig ),
379 "Error in createFakeFrames" ) ;
380 }
382 /* Call xsh_prepare */
383 check_msg( xsh_prepare( raws, NULL, NULL,"CRH", instrument,0,CPL_FALSE),
384 "Error in xsh_prepare" ) ;
385 /* Call xsh_remove_crh_multi */
386 strcpy( crh_tag, "test_remove_crh" ) ;
387
389
390 check(medFrame = xsh_remove_crh_multiple( raws,
391 crh_tag,&stack_param,NULL,
393 NULL,NULL,0 )) ;
394 if ( (argc-optind) < 1 ) {
395 /* Check that median frame is OK and Bad pixel map is OK */
396 assure ( verifCr( medFrame ) == 1, CPL_ERROR_ILLEGAL_INPUT,
397 "Verification failed" ) ;
398 xsh_msg( "Remove CRH Multiple OK" ) ;
399 }
400
401 cleanup:
402 /* Free all memory allocated */
404 xsh_free_frame( &medFrame);
405 xsh_free_frameset( &raws);
406 xsh_free_frameset( &calib);
407 xsh_free_frameset( &set);
408
409 if (cpl_error_get_code() != CPL_ERROR_NONE) {
410 xsh_error_dump(CPL_MSG_ERROR);
411 ret = 1;
412 }
414 TEST_END();
415 return ret ;
416}
417
int main()
Unit test of xsh_bspline_interpol.
static void HandleOptions(int argc, char **argv)
#define CR_PLACE_Y
static int nbImages
static double bgMax
static double sigma
#define CR_PIX_VALUE
static int verifCr(cpl_frame *medframe)
static double bgMin
#define CR_PLACE_X
static void Help(void)
static int nbCr
static xsh_instrument * instrument
#define MODULE_ID
static struct option long_options[]
static int totCr
static xsh_clipping_param crh_clipping
static cpl_frameset * createFakeFrames(XSH_INSTRCONFIG *iconfig)
static int imgSize
cpl_frameset * sof_to_frameset(const char *sof_name)
Definition: tests.c:576
cpl_propertylist * mkHeader(XSH_INSTRCONFIG *iconfig, int nx, int ny, double exptime)
Definition: tests.c:221
int xsh_bpmap_count(cpl_image *bpmap, int nx, int ny)
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
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_set_lamp(xsh_instrument *i, XSH_LAMP lamp)
Set a lamp on instrument structure.
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
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
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
#define TESTS_CLEAN_WORKSPACE(DRL_ID)
Definition: tests.h:139
#define TESTS_INIT_WORKSPACE(DRL_ID)
Definition: tests.h:133
#define TEST_END()
Definition: tests.h:111
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
@ XSH_LAMP_QTH
@ XSH_ARM_UVB
@ XSH_MODE_IFU
void xsh_dfs_split_in_group(cpl_frameset *input, cpl_frameset *raws, cpl_frameset *calib)
split input sof in groups: raw and calib
Definition: xsh_dfs.c:1202
xsh_instrument * xsh_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset and return the instrument detected.
Definition: xsh_dfs.c:1046
@ XSH_DEBUG_LEVEL_HIGH
Definition: xsh_utils.h:138
@ XSH_DEBUG_LEVEL_LOW
Definition: xsh_utils.h:137
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138
#define XSH_NEW_FRAMESET(POINTER)
Definition: xsh_utils.h:84