X-shooter Pipeline Reference Manual 3.8.15
test-xsh_data_pre.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: 2011-12-02 14:13:14 $
23 * $Revision: 1.8 $
24 * $Name: not supported by cvs2svn $
25 */
26#ifdef HAVE_CONFIG_H
27# include <config.h>
28#endif
29
30/*--------------------------------------------------------------------------*/
36/*--------------------------------------------------------------------------*/
39/*---------------------------------------------------------------------------
40 Includes
41 ---------------------------------------------------------------------------*/
42
43
44#include <xsh_data_pre.h>
45#include <xsh_error.h>
46#include <xsh_msg.h>
47#include <xsh_badpixelmap.h>
48#include <xsh_data_instrument.h>
49#include <xsh_dfs.h>
50#include <xsh_pfits.h>
51#include <tests.h>
52
53#include <cpl.h>
54#include <math.h>
55
56/*---------------------------------------------------------------------------
57 Defines
58 ---------------------------------------------------------------------------*/
59#define MODULE_ID "XSH_DATA_PRE"
60
61#define SYNTAX "Test the pre frame\n"\
62 "use : ./test_xsh_data_pre PRE \n"\
63 "PRE => the pre frame\n"
64
65/*--------------------------------------------------------------------------*/
72/*--------------------------------------------------------------------------*/
73int main( int argc, char** argv)
74{
75 int ret = 0;
76 xsh_instrument * instrument = NULL ;
77 char *pre_name = NULL;
78 cpl_frame *pre_frame = NULL;
79 xsh_pre *pre = NULL;
80
81 cpl_propertylist* header = NULL;
82 int naxis1;
83 cpl_image *errs_img = NULL;
84 cpl_image* qual_img = NULL;
85
86 int *qual = NULL;
87 FILE* pre_file = NULL;
88 int i, j;
89 double data_mean=0.0, data_stdev=0.0, data_median=0.0;
90 int binx;
91
92 /* Initialize libraries */
94 cpl_msg_set_level( CPL_MSG_DEBUG);
96
97 /* Analyse parameters */
98 if (argc > 1){
99 pre_name = argv[1];
100 }
101 else{
102 printf(SYNTAX);
103 TEST_END();
104 return 0;
105 }
106
107 XSH_ASSURE_NOT_NULL( pre_name);
108 check (header = cpl_propertylist_load( pre_name, 0));
109 check ( naxis1 = xsh_pfits_get_naxis1( header));
110 check ( binx = xsh_pfits_get_binx( header));
111
112 /* Create instrument structure and fill */
115 if ( naxis1 == (2048/binx) ){
117 }
118 else if (naxis1 == (2044/binx) ){
120 }
121 else if (naxis1 == (1024/binx) ){
123 }
124 else{
125 xsh_msg(" Invalid naxis1 size %d", naxis1);
127 }
129
130 /* Create frame */
131 pre_frame = cpl_frame_new();
132 cpl_frame_set_filename( pre_frame, pre_name) ;
133 cpl_frame_set_level( pre_frame, CPL_FRAME_LEVEL_TEMPORARY);
134 cpl_frame_set_group( pre_frame, CPL_FRAME_GROUP_RAW );
135 cpl_frame_set_tag( pre_frame, "BIAS_UVB");
136
137 check( pre = xsh_pre_load( pre_frame, instrument));
138
139 {
140 int a=0, b=0;
142 3, 1, &a, &b);
143 xsh_msg("find max at %d %d",a,b);
144 }
145 check( xsh_pre_median_mean_stdev( pre, &data_mean, &data_median, &data_stdev));
146
147 xsh_msg(" Statistics on DATA: median %f mean %f stdev %f",
148 data_median, data_mean, data_stdev);
149
150 check( errs_img = xsh_pre_get_errs( pre));
151 check( qual_img = xsh_pre_get_qual( pre));
152 check ( qual = cpl_image_get_data_int( qual_img));
153 xsh_msg("Save order tab in BADPIXEL.reg");
154 pre_file = fopen( "BADPIXEL.reg", "w");
155 fprintf( pre_file, "# Region file format: DS9 version 4.0\n"\
156 "global color=red font=\"helvetica 4 normal\""\
157 "select=1 highlite=1 edit=1 move=1 delete=1 include=1 fixed=0 "\
158 "source\nimage\n");
159 xsh_msg( "XSH_GOOD_PIXEL_LEVEL %d",XSH_GOOD_PIXEL_LEVEL);
160 for(j=0; j< pre->ny; j++){
161 for(i=0; i<pre->nx; i++){
162 if ( qual[i+j*pre->nx] > XSH_GOOD_PIXEL_LEVEL){
163 fprintf( pre_file, "point(%f,%f) #point=cross color=red font="\
164 "\"helvetica 4 normal\"\n", i+1.0, j+1.0);
165 }
166 else if ( (qual[i+j*pre->nx] & 16) == 16){
167 fprintf( pre_file, "point(%f,%f) #point=cross color=green font="\
168 "\"helvetica 4 normal\"\n", i+1.0, j+1.0);
169 }
170 }
171 }
172 fclose( pre_file);
173
174 cleanup:
175 xsh_free_frame( &pre_frame);
176 xsh_pre_free( &pre);
178 xsh_free_propertylist( &header);
179
180 if (cpl_error_get_code() != CPL_ERROR_NONE) {
181 xsh_error_dump(CPL_MSG_ERROR);
182 ret = 1;
183 }
184 TEST_END();
185 return ret;
186
187#if 0
188 /* Create FITS data */
189 frame = xsh_test_create_frame(dataname, nx, ny,
190 XSH_LINEARITY_UVB,CPL_FRAME_GROUP_RAW, instrument);
191 /* TEST 1 Create PRE */
192 check(pre1 = xsh_pre_create(frame,NULL,NULL, instrument));
193 xsh_msg("Create PRE %s OK",dataname);
194
195 /* TEST 2 : Save PRE */
196 check(preframe = xsh_pre_save(pre1,pre_name, "PRE_TAG",1 ));
197 xsh_msg("Save PRE %s OK",pre_name);
198
199 /* DFS : assign tag and group */
200 cpl_frame_set_tag(preframe,XSH_LINEARITY_UVB);
201 cpl_frame_set_group(preframe,CPL_FRAME_GROUP_RAW);
202
203 /* TEST 3 : Load PRE */
204 check(pre2 = xsh_pre_load(preframe,instrument));
205 xsh_msg("Load PRE %s OK",dataname);
206
207 /* TEST 4 : Check FITS header */
208 check_msg(conad = xsh_pfits_get_conad(pre2->data_header),
209 "Can't find conad in header");
210 check_msg(ron = xsh_pfits_get_ron(pre2->data_header),
211 "Can't find ron in header");
212
213 xsh_msg("Header PRE OK");
214
215 /* TEST 5 : Check format */
216 assure(xsh_pre_get_nx(pre2) == nx,CPL_ERROR_ILLEGAL_OUTPUT,
217 "wrong nx in PRE format");
218 assure(xsh_pre_get_ny(pre2) == ny,CPL_ERROR_ILLEGAL_OUTPUT,
219 "wrong ny in PRE format");
220 assure(xsh_pre_get_group(pre2) == CPL_FRAME_GROUP_RAW,
221 CPL_ERROR_ILLEGAL_OUTPUT,"wrong group in PRE format");
222 assure(pre2->data != NULL,CPL_ERROR_ILLEGAL_OUTPUT,
223 "PRE data is NULL");
224 assure(pre2->errs != NULL,CPL_ERROR_ILLEGAL_OUTPUT,
225 "PRE errs is NULL");
226 assure(pre2->qual != NULL,CPL_ERROR_ILLEGAL_OUTPUT,
227 "PRE qual is NULL");
228
229 xsh_msg("Format PRE OK");
230
231 /* TEST 6 : Check data */
232 tdata = cpl_image_get_data_float(pre2->data);
233 terrs = cpl_image_get_data_float(pre2->errs);
234 for(i = 0; i < nx * ny; i++){
235 assure((terrs[i] - sqrt(fabs(tdata[i])+ron*ron) < XSH_FLOAT_PRECISION),
236 CPL_ERROR_ILLEGAL_OUTPUT,"WRONG errs part");
237 }
238 xsh_msg("Data PRE OK");
239
240 /* TEST 7 : Subtract two PRE frames */
241 pre3 = xsh_pre_duplicate(pre2);
242 residual = xsh_pre_duplicate(pre2);
243 check(xsh_pre_subtract(residual, pre3));
244
245 avg_residual = cpl_image_get_stdev(xsh_pre_get_data(residual));
246 avg_err = cpl_image_get_stdev(xsh_pre_get_errs(residual));
247
248 xsh_msg("avg residual = %f ; avg uncertainty = %f",
249 avg_residual, avg_err);
250
251 assure(avg_residual <= avg_err, CPL_ERROR_ILLEGAL_OUTPUT,
252 "Residual is not zero");
253
254 /* For each pixel, check that the residual is zero (withing
255 uncertainty) */
256 tdata = cpl_image_get_data_float(residual->data);
257 terrs = cpl_image_get_data_float(residual->errs);
258 tqual = cpl_image_get_data_int(residual->qual);
259 tdata1 = cpl_image_get_data_float(pre2->data);
260 terrs1 = cpl_image_get_data_float(pre2->errs);
261 tqual1 = cpl_image_get_data_int(pre2->qual);
262 tdata2 = cpl_image_get_data_float(pre3->data);
263 terrs2 = cpl_image_get_data_float(pre3->errs);
264 tqual2 = cpl_image_get_data_int(pre3->qual);
265 for (i = 0; i < nx * ny; i++) {
266 assure(tdata[i] <= terrs[i],CPL_ERROR_ILLEGAL_OUTPUT,
267 "Residual is non-zero at %d : ",i);
268 assure(terrs[i] -(sqrt(pow(terrs1[i],2)+pow(terrs2[i],2))) <
270 CPL_ERROR_ILLEGAL_OUTPUT,
271 "Wrong errs at %d : ",i);
272 assure(tqual[i] == (tqual1[i]+tqual2[i]),CPL_ERROR_ILLEGAL_OUTPUT,
273 "Wrong qual part");
274 }
275 xsh_msg("subtract two PRE OK");
276
277 /* TEST 8 : Add two PRE image */
278 check(addtest = xsh_pre_duplicate(pre2));
279 check(xsh_pre_add(addtest, pre3));
280 tdata = cpl_image_get_data_float(addtest->data);
281 terrs = cpl_image_get_data_float(addtest->errs);
282 tqual = cpl_image_get_data_int(addtest->qual);
283 tdata1 = cpl_image_get_data_float(pre2->data);
284 terrs1 = cpl_image_get_data_float(pre2->errs);
285 tqual1 = cpl_image_get_data_int(pre2->qual);
286 tdata2 = cpl_image_get_data_float(pre3->data);
287 terrs2 = cpl_image_get_data_float(pre3->errs);
288 tqual2 = cpl_image_get_data_int(pre3->qual);
289
290 for (i = 0; i < nx * ny; i++) {
291 assure(tdata[i] -(tdata1[i]+tdata2[i]) < XSH_FLOAT_PRECISION,
292 CPL_ERROR_ILLEGAL_OUTPUT,
293 "Wrong data at %d : ",i);
294 assure(terrs[i] -(sqrt(pow(terrs1[i],2)+pow(terrs2[i],2))) <
296 CPL_ERROR_ILLEGAL_OUTPUT,
297 "Wrong errs at %d : ",i);
298 assure(tqual[i] == (tqual1[i]+tqual2[i]),CPL_ERROR_ILLEGAL_OUTPUT,
299 "Wrong qual part");
300 }
301 xsh_msg("Add two PRE OK");
302
303cleanup:
305 xsh_pre_free(&pre1);
306 xsh_pre_free(&pre2);
307 xsh_pre_free(&pre3);
308 xsh_pre_free(&residual);
309 xsh_pre_free(&addtest);
310 xsh_free_frame(&frame);
311 xsh_free_frame(&preframe);
312
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#endif
320}
321
int main()
Unit test of xsh_bspline_interpol.
static xsh_instrument * instrument
int binx
cpl_frame * xsh_test_create_frame(const char *name, int nx, int ny, const char *tag, cpl_frame_group group, xsh_instrument *instrument)
Definition: tests.c:119
void xsh_pre_add(xsh_pre *self, const xsh_pre *right)
Add two PRE images.
cpl_image * xsh_pre_get_data(xsh_pre *pre)
Get data.
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
Definition: xsh_data_pre.c:953
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
cpl_frame_group xsh_pre_get_group(const xsh_pre *pre)
Get ny of pre structure.
cpl_image * xsh_pre_get_qual(xsh_pre *pre)
Get qual.
int xsh_pre_get_ny(const xsh_pre *pre)
Get ny of pre structure.
int xsh_pre_window_best_median_flux_pos(xsh_pre *pre, int xcen, int ycen, int search_window_hsize, int running_median_hsize, int *xadj, int *yadj)
Search pixel position of best running median flux in the search window.
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
xsh_pre * xsh_pre_create(cpl_frame *raw, cpl_frame *bpmap, cpl_image *bias_data, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
Create a XSH_PRE from a raw frame.
Definition: xsh_data_pre.c:450
int xsh_pre_get_nx(const xsh_pre *pre)
Get nx of pre structure.
void xsh_pre_subtract(xsh_pre *self, const xsh_pre *right)
Subtract one PRE image from another The data units are subtracted data = data1 - data2 The error imag...
cpl_image * xsh_pre_get_errs(xsh_pre *pre)
Get errs.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
void xsh_pre_median_mean_stdev(xsh_pre *preFrame, double *mean, double *median, double *stdev)
#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
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
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
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
double xsh_pfits_get_conad(const cpl_propertylist *plist)
find out the CONAD value
Definition: xsh_pfits.c:692
int xsh_pfits_get_binx(const cpl_propertylist *plist)
find out the BINX value
Definition: xsh_pfits.c:289
double xsh_pfits_get_ron(const cpl_propertylist *plist)
find out the RON value
Definition: xsh_pfits.c:459
int xsh_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
Definition: xsh_pfits.c:227
#define MODULE_ID
#define SYNTAX
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_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
#define XSH_FLOAT_PRECISION
Definition: tests.h:150
#define TEST_END()
Definition: tests.h:111
#define TESTS_INIT(DRL_ID)
Definition: tests.h:105
#define XSH_GOOD_PIXEL_LEVEL
@ XSH_LAMP_QTH
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
@ XSH_MODE_IFU
int nx
int ny
#define XSH_LINEARITY_UVB
Definition: xsh_dfs.h:383
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138