X-shooter Pipeline Reference Manual 3.8.15
test-xsh_extract_clean_sym.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: 2012-12-08 18:36:38 $
23 * $Revision: 1.18 $
24 */
25
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*-------------------------------------------------------------------------*/
36/*-------------------------------------------------------------------------*/
39/*--------------------------------------------------------------------------
40 Includes
41 --------------------------------------------------------------------------*/
42
43#include <tests.h>
44
45#include <xsh_data_pre.h>
46#include <xsh_error.h>
47#include <xsh_msg.h>
48#include <xsh_data_instrument.h>
49#include <xsh_data_rec.h>
51#include <xsh_drl.h>
52#include <xsh_pfits.h>
53
54#include <xsh_badpixelmap.h>
55#include <xsh_parameters.h>
56
57#include <cpl.h>
58#include <math.h>
59
60#include <getopt.h>
61
62/*--------------------------------------------------------------------------
63 Defines
64 --------------------------------------------------------------------------*/
65
66#define MODULE_ID "XSH_EXTRACT_CLEAN_SYM"
67
68enum {
70} ;
71
72static struct option long_options[] = {
73 {"decode-bp", required_argument, 0, DECODEBP_OPT},
74 {"method", required_argument, 0, METHOD_OPT},
75 {"debug", required_argument, 0, DEBUG_OPT},
76 {"help", 0, 0, HELP_OPT},
77 {0, 0, 0, 0}
78};
79
80static void Help( void )
81{
82 puts( "Unitary test of xsh_extract_clean");
83 puts( "Usage: test_xsh_extract_clean [options] <input_files>");
84
85 puts( "Options" ) ;
86 puts( " --decode-bp=<n> : Integer representation of the bits to be considered bad when decoding the bad pixel mask pixel values.");
87 puts( " --method=<n> : method for extraction CLEAN | LOCALIZATION | FULL | NOD");
88 puts( " --debug=<n> : Level of debug LOW | MEDIUM | HIGH [MEDIUM]" );
89 puts( " --help : What you see" ) ;
90 puts( "\nInput Files" ) ;
91 puts( "The input files argument MUST be in this order:" ) ;
92 puts( " 1. Rectified frame 2D" ) ;
93 puts( " 2. Localization table");
95 TEST_END();
96 exit(0);
97}
98
99static void HandleOptions( int argc, char **argv,
100 xsh_extract_param *extract_par,int* decode_bp)
101{
102 int opt ;
103 int option_index = 0;
104
105 while (( opt = getopt_long (argc, argv, "slit_position:slit_height:method",
106 long_options, &option_index)) != EOF ){
107
108 switch ( opt ) {
109 case METHOD_OPT:
110 if ( strcmp(optarg, EXTRACT_METHOD_PRINT( LOCALIZATION_METHOD)) == 0){
111 extract_par->method = LOCALIZATION_METHOD;
112 }
113 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( FULL_METHOD)) == 0){
114 extract_par->method = FULL_METHOD;
115 }
116 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( CLEAN_METHOD)) == 0){
117 extract_par->method = CLEAN_METHOD;
118 }
119 else if ( strcmp(optarg, EXTRACT_METHOD_PRINT( NOD_METHOD)) == 0){
120 extract_par->method = NOD_METHOD;
121 }
122 else{
123 xsh_msg("WRONG method %s", optarg);
124 exit(-1);
125 }
126 break ;
127 case DEBUG_OPT:
128 if ( strcmp( optarg, "LOW")==0){
130 }
131 else if ( strcmp( optarg, "HIGH")==0){
133 }
134 break;
135 case DECODEBP_OPT:
136 *decode_bp=atoi(optarg);
137 break;
138
139 case HELP_OPT:
140 Help();
141 break;
142 default:
143 Help();
144 break;
145 }
146 }
147 return;
148}
149
150cpl_error_code xsh_set_wcs(cpl_propertylist* header, const double crpix1,
151 const double crval1, const double cdelt1, const double crpix2,
152 const double crval2, const double cdelt2) {
153 cpl_propertylist_append_double(header, "CRPIX1", crpix1);
154 cpl_propertylist_append_double(header, "CRVAL1", crval1);
155 cpl_propertylist_append_double(header, "CDELT1", cdelt1);
156 cpl_propertylist_append_string(header, "CTYPE1", "LINEAR");
157
158 cpl_propertylist_append_double(header, "CRPIX2", crpix2);
159 cpl_propertylist_append_double(header, "CRVAL2", crval2);
160 cpl_propertylist_append_double(header, "CDELT2", cdelt2);
161 cpl_propertylist_append_string(header, "CTYPE2", "LINEAR");
162
163 xsh_pfits_set_cd11(header, cdelt1);
164 xsh_pfits_set_cd12(header, 0);
165 xsh_pfits_set_cd21(header, 0);
166 xsh_pfits_set_cd22(header, cdelt2);
167
168 return cpl_error_get_code();
169}
170/*--------------------------------------------------------------------------
171 Implementation
172 --------------------------------------------------------------------------*/
173
174static cpl_frame*
176{
177 cpl_frame* extracted=NULL;
178 //float* prof = 0;
179 //int nbad=0;
180 //int ngood=0;
181 //int islit=0;
182 int nslit=0;
183 //int idx=0;
184 int nlambda=0;
185 int ilambda=0;
186 int mask_hsize=3;
187 int mask_size=mask_hsize*2+1;
188 //int m=0;
189 //int mdx=0;
190
191 int* qual=NULL;
192 float* flux=NULL;
193 float* errs=NULL;
194
195 //int* sub_qual=NULL;
196 //float* sub_flux=NULL;
197 //float* sub_errs=NULL;
198
199 int* res_qual=NULL;
200 double* res_flux=NULL;
201 double* res_errs=NULL;
202 double* res_lambda=NULL;
203
204 double* lambda=NULL;
205
206 double fluxval=0;
207 double errval=0;
208
209
210 int qualval=0;
211 //float num_fct=0;
212 //float den_fct=0;
213 //float num_add=0;
214 //float den_add=0;
215
216 //double ipol_fct=0;
217 const char* fname=NULL;
218 cpl_image* ima_data=NULL;
219 cpl_image* ima_errs=NULL;
220 cpl_image* ima_qual=NULL;
221
222 //cpl_image* sub_ima_data=NULL;
223 //cpl_image* sub_ima_errs=NULL;
224 //cpl_image* sub_ima_qual=NULL;
225 cpl_propertylist* header=NULL;
226 //cpl_propertylist* plist=NULL;
227 //double crpix1 = 1;
228 double crval1 = 533.68;
229 double cdelt1 = 0.04;
230 //double crpix2 = 1;
231
232 const char* res_name="extracted_signal.fits";
233
234 cpl_table* res_table=NULL;
235
236 int slit_border=6;
237 int hslit=8;
238 int slit_min=slit_border;
239 int slit_max=slit_min+2*hslit;
240
241 //int nbad_sub=0;
242 //int mslit=0;
243
244 fname=cpl_frame_get_filename(frame2D);
245 ima_data=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
246 ima_errs=cpl_image_load(fname,CPL_TYPE_FLOAT,0,1);
247 ima_qual=cpl_image_load(fname,CPL_TYPE_INT,0,2);
248 header=cpl_propertylist_load(fname,0);
249
250 nlambda=xsh_pfits_get_naxis1(header);
251 nslit=xsh_pfits_get_naxis2(header);
252
253 crval1=xsh_pfits_get_crval1(header);
254 cdelt1=xsh_pfits_get_cdelt1(header);
255
256
257
258 flux=cpl_image_get_data_float(ima_data);
259 errs=cpl_image_get_data_float(ima_errs);
260 check(qual=cpl_image_get_data_int(ima_qual));
261
262 res_table=cpl_table_new(nlambda);
263 cpl_table_new_column(res_table,"wave",CPL_TYPE_DOUBLE);
264 cpl_table_new_column(res_table,"flux",CPL_TYPE_DOUBLE);
265 cpl_table_new_column(res_table,"errs",CPL_TYPE_DOUBLE);
266 cpl_table_new_column(res_table,"qual",CPL_TYPE_INT);
267
268 cpl_table_fill_column_window_double(res_table,"wave",0,nlambda,0.);
269 cpl_table_fill_column_window_double(res_table,"flux",0,nlambda,0.);
270 cpl_table_fill_column_window_double(res_table,"errs",0,nlambda,0.);
271 cpl_table_fill_column_window_int(res_table,"qual",0,nlambda,0);
272
273 check(res_lambda=cpl_table_get_data_double(res_table,"wave"));
274 res_flux=cpl_table_get_data_double(res_table,"flux");
275 res_errs=cpl_table_get_data_double(res_table,"errs");
276 res_qual=cpl_table_get_data_int(res_table,"qual");
277
278 lambda = cpl_malloc(nlambda * sizeof(double));
279
280
281 slit_min = nslit / 2 - hslit;
282 slit_max = nslit / 2 + hslit;
283
284
285 xsh_msg_debug("mask size=%d", mask_size);
286 float* pima;
287 float* fima;
288 float* rima;
289 for (ilambda = 0; ilambda < nlambda; ilambda++) {
290 fluxval = 0;
291 errval = 0;
292 qualval = 0;
293 lambda[ilambda] = (float) (crval1 + cdelt1 * ilambda);
294
295 xsh_extract_clean_slice(flux,errs,qual,lambda,ilambda,instrument,slit_min, slit_max,nlambda,nslit,mask_hsize,&fluxval, &errval,&qualval, pima, fima, rima);
296
297 res_lambda[ilambda] = lambda[ilambda];
298 res_flux[ilambda] = fluxval;
299 res_errs[ilambda] = sqrt(errval);
300 res_qual[ilambda] = qualval;
301
302
303 }
304
305
306 check(cpl_table_save(res_table,NULL,NULL,res_name,CPL_IO_DEFAULT));
307
308 extracted = cpl_frame_new();
309 cpl_frame_set_filename(extracted, res_name);
310 cpl_frame_set_level(extracted, CPL_FRAME_LEVEL_TEMPORARY);
311 cpl_frame_set_group(extracted, CPL_FRAME_GROUP_RAW ) ;
312 cpl_frame_set_tag(extracted,"TEST");
313
314cleanup:
315
316 XSH_FREE(lambda);
317 xsh_free_propertylist(&header);
318 xsh_free_image(&ima_data);
319 xsh_free_image(&ima_errs);
320 xsh_free_image(&ima_qual);
321 xsh_free_table(&res_table);
322
323 return extracted;
324}
325
333int main( int argc, char **argv)
334{
335 /* Declarations */
336 int ret = 0 ;
338 cpl_propertylist *header= NULL;
339 const char *tag = "TELL_SLIT_ORDER2D_VIS";
340 cpl_frame* result = NULL;
341 //cpl_frame* result_eso = NULL;
342
343 char* rec_name = "sym_extract2d.fits";
344 cpl_frame* rec_frame = NULL;
345
346 int decode_bp=DECODE_BP_FLAG_DEF;
347 int sx=300;
348 int sy=68;
349 //int sy=20;
350 const double crpix1=1.;
351 const double crval1=533.68;
352 const double cdelt1=0.04;
353 xsh_extract_param extract_obj = { CLEAN_METHOD};
354 const double crpix2=1.;
355 const double crval2=-5.3;
356 const double cdelt2=0.16;
357 cpl_image* data=NULL;
358 cpl_image* errs=NULL;
359 cpl_image* qual=NULL;
360 float* pdata=NULL;
361 int* pqual=NULL;
362 XSH_ARM arm = XSH_ARM_VIS;
363 int i=0;
364 int j=0;
365 int k=0;
366 int sc=sy/2;
367 float* val=NULL;
368 const int rad=3;
369 const int nval=2*rad+1;
370 const float crh=10000.;
371
372 /* Initialize libraries */
375
376 cpl_msg_set_level(CPL_MSG_DEBUG);
378
379
380 /* Analyse parameters */
381 HandleOptions( argc, argv, &extract_obj,&decode_bp);
382 xsh_msg("argc=%d optind=%d",argc,optind);
383 /*
384 if ( (argc - optind) > 0 ) {
385 rec_name = argv[optind];
386 if ( (argc - optind) > 1){
387 loc_name = argv[optind+1];
388 }
389 }
390 else{
391 Help();
392 }
393 */
394
395 /* generate image */
396 data=cpl_image_new(sx,sy,CPL_TYPE_FLOAT);
397 //cpl_image_add_scalar(data,1);
398 qual=cpl_image_new(sx,sy,CPL_TYPE_INT);
399 pdata=cpl_image_get_data_float(data);
400 pqual=cpl_image_get_data_int(qual);
401
402 /* fill a central trace object ad hoc (with values that are squares to get proper errors: sum=83 */
403 val = cpl_malloc(nval * sizeof(float));
404 val[0]=4.;
405 val[1]=9.;
406 val[2]=16.;
407 val[3]=25.;
408 val[4]=16.;
409 val[5]=9.;
410 val[6]=4.;
411
412 for (k = -rad; k <= rad; k++) {
413 j = sc - k;
414 for (i = 0; i < sx; i++) {
415 pdata[j * sx + i] = val[k + rad];
416 }
417 }
418
419
420
421 /* add a CRH */
422 // isolated
423
424 j=sc;
425 i=12;
426 //cpl_image_set(data,i,j,crh);
427 //cpl_image_set(qual,i,j,crh);
428 pdata[j * sx + i]=crh;
429 pqual[j * sx + i]=1;
430 xsh_msg_debug("ingest CRH at pix[%d,%d]=pix[%g,%g]",i,j,crval1+i*cdelt1,crval2+j*cdelt2);
431
432
433
434 // double on Y
435 j=sc-1;
436 i=52;
437 pdata[j * sx + i]=crh;
438 pqual[j * sx + i]=1;
439
440 j=sc+1;
441 pdata[j * sx + i]=crh;
442 pqual[j * sx + i]=1;
443
444 // double on X
445 j=sc;
446 i=102;
447 pdata[j * sx + i]=crh;
448 pqual[j * sx + i]=1;
449 i=103;
450 pdata[j * sx + i]=crh;
451 pqual[j * sx + i]=1;
452
453
454 // longer than extraction slit
455 j=sc;
456 for (i = 106; i <= 116; i++) {
457 pdata[j * sx + i]=crh;
458 pqual[j * sx + i]=1;
459 }
460
461 // L shaped
462 j=sc+2;
463 i=152;
464 pdata[j * sx + i]=crh;
465 pqual[j * sx + i]=1;
466
467 j=sc+1;
468 i=152;
469 pdata[j * sx + i]=crh;
470 pqual[j * sx + i]=1;
471
472 j=sc;
473 i=152;
474 pdata[j * sx + i]=crh;
475 pqual[j * sx + i]=1;
476 j=sc-1;
477
478 i=152;
479 pdata[j * sx + i]=crh;
480 pqual[j * sx + i]=1;
481
482 j=sc-1;
483 i=153;
484 pdata[j * sx + i]=crh;
485 pqual[j * sx + i]=1;
486
487
488 // filling all the object: does not work?
489 for (k = -rad; k <= rad; k++) {
490 j = sc - k;
491 i=202;
492 pdata[j * sx + i] = crh;
493 pqual[j * sx + i]=1;
494 }
495
496
497 // filling all the extraction slit: strange mask
498 for (j= 0; j < sy; j++) {
499 i=252;
500 pdata[j * sx + i] = crh;
501 pqual[j * sx + i]=1;
502 }
503
504
505
506 //cpl_image_add_scalar(data,100.);
507 errs=cpl_image_duplicate(data);
508 cpl_image_power(errs,0.5);
509 cpl_image_add_scalar(errs,1);
510
511 header=cpl_propertylist_new();
512 xsh_set_wcs(header, crpix1,crval1, cdelt1, crpix2,crval2, cdelt2);
513 cpl_image_save(data, rec_name, XSH_PRE_DATA_BPP, header, CPL_IO_DEFAULT);
514 cpl_image_save(errs, rec_name, XSH_PRE_ERRS_BPP, NULL, CPL_IO_EXTEND);
515 cpl_image_save(qual, rec_name, XSH_PRE_QUAL_BPP, NULL, CPL_IO_EXTEND);
516
517 rec_frame = cpl_frame_new();
518 XSH_ASSURE_NOT_NULL (rec_frame);
519 cpl_frame_set_filename( rec_frame, rec_name) ;
520
521 cpl_frame_set_level( rec_frame, CPL_FRAME_LEVEL_TEMPORARY);
522 cpl_frame_set_group( rec_frame, CPL_FRAME_GROUP_RAW ) ;
523 cpl_frame_set_tag( rec_frame, tag);
524
525 /* Create instrument structure and fill */
531 //xsh_msg("decode_bp=%d",decode_bp);
532
533
534 check(result = xsh_extract_local_clean(rec_frame,instrument));
535
536 cleanup:
537
538 XSH_FREE(val);
540 xsh_free_image( &errs);
541 xsh_free_image( &qual);
542
544 xsh_free_frame( &rec_frame);
545 xsh_free_frame( &result);
546 xsh_free_propertylist( &header);
547
548 if (cpl_error_get_code() != CPL_ERROR_NONE) {
549 xsh_error_dump(CPL_MSG_ERROR);
550 ret= 1;
551 }
553 TEST_END();
554 return ret ;
555}
556
int main()
Unit test of xsh_bspline_interpol.
cpl_error_code xsh_set_wcs(cpl_propertylist *header, const double crpix1, const double crval1, const double cdelt1, const double crpix2, const double crval2, const double cdelt2)
#define MODULE_ID
static struct option long_options[]
static cpl_frame * xsh_extract_local_clean(cpl_frame *frame2D, xsh_instrument *instrument)
static xsh_instrument * instrument
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
cpl_error_code xsh_extract_clean_slice(const float *flux, const float *errs, int *qual, const double *lambda, const int ilambda, xsh_instrument *instrument, const int slit_min, const int slit_max, const int nlambda, const int nslit, const int mask_hsize, double *fluxval, double *errval, int *qualval, float *, float *, float *)
Definition: xsh_extract.c:346
void xsh_instrument_set_mode(xsh_instrument *i, XSH_MODE mode)
Set a mode on instrument structure.
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
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_debug(...)
Print a debug message.
Definition: xsh_msg.h:99
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_pfits_set_cd22(cpl_propertylist *plist, double value)
Write the CD2_2 value.
Definition: xsh_pfits.c:2369
void xsh_pfits_set_cd21(cpl_propertylist *plist, double value)
Write the CD2_1 value.
Definition: xsh_pfits.c:2354
double xsh_pfits_get_cdelt1(const cpl_propertylist *plist)
find out the cdelt1
Definition: xsh_pfits.c:2196
int xsh_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
Definition: xsh_pfits.c:227
double xsh_pfits_get_crval1(const cpl_propertylist *plist)
find out the crval1
Definition: xsh_pfits.c:1907
void xsh_pfits_set_cd12(cpl_propertylist *plist, double value)
Write the CD1_2 value.
Definition: xsh_pfits.c:2339
void xsh_pfits_set_cd11(cpl_propertylist *plist, double value)
Write the CD1_1 value.
Definition: xsh_pfits.c:2324
int xsh_pfits_get_naxis2(const cpl_propertylist *plist)
find out the NAXIS2 value
Definition: xsh_pfits.c:244
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
int xsh_debug_level_set(int level)
set debug level
Definition: xsh_utils.c:3125
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
enum extract_method method
static void HandleOptions(int argc, char **argv, xsh_extract_param *extract_par, int *decode_bp)
static void Help(void)
#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_VIS
@ XSH_MODE_IFU
#define XSH_PRE_ERRS_BPP
Definition: xsh_data_pre.h:45
#define XSH_PRE_QUAL_BPP
Definition: xsh_data_pre.h:47
#define XSH_PRE_DATA_BPP
Definition: xsh_data_pre.h:43
#define DECODE_BP_FLAG_DEF
#define EXTRACT_METHOD_PRINT(method)
@ FULL_METHOD
@ NOD_METHOD
@ LOCALIZATION_METHOD
@ CLEAN_METHOD
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
@ 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