X-shooter Pipeline Reference Manual 3.8.15
test-xsh_detect_line_pos2.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-06-13 15:08:52 $
23 * $Revision: 1.1 $
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#include <xsh_utils.h>
43#include <xsh_error.h>
44#include <xsh_msg.h>
45#include <xsh_data_instrument.h>
46#include <xsh_data_pre.h>
47#include <xsh_fit.h>
48#include <xsh_pfits.h>
49#include <tests.h>
50#include <cpl.h>
51#include <math.h>
52#include <time.h>
53#include <sys/time.h>
54#include <xsh_cpl_size.h>
55/*---------------------------------------------------------------------------
56 Defines
57 ---------------------------------------------------------------------------*/
58#define MODULE_ID "XSH_DETECT_LINE_POS2"
59
60static cpl_error_code
61xsh_add_fits_key_min_set(cpl_propertylist* plist)
62{
63 cpl_propertylist_append_string(plist,XSH_DPR_TYPE,"TEST");
64 cpl_propertylist_append_double(plist,XSH_EXPTIME,10.);
65 cpl_propertylist_append_double(plist,XSH_RON,1.);
66 cpl_propertylist_append_double(plist,XSH_CONAD,1.);
67 cpl_propertylist_append_double(plist,XSH_DET_GAIN,1.);
68 cpl_propertylist_append_int(plist,XSH_WIN_BINX,1);
69 cpl_propertylist_append_int(plist,XSH_WIN_BINY,1);
70 cpl_propertylist_append_double(plist,XSH_PSZX,15.);
71 cpl_propertylist_append_double(plist,XSH_PSZY,15.);
72 //Only for NIR:
73 cpl_propertylist_append_double(plist,XSH_DET_PXSPACE,1.800e-05);
74 cpl_propertylist_append_int(plist,XSH_CHIP_NY,2048);
75
76
77 return cpl_error_get_code();
78
79}
80
81/*--------------------------------------------------------------------------*/
87/*--------------------------------------------------------------------------*/
88int main()
89{
90 int ret = 0;
91 int sx=4096;
92 int sy=4096;
93
94 //char *image_name = NULL;
95 cpl_image* image = NULL;
96 cpl_image* gauss = NULL;
97 cpl_image* img_raw = NULL;
98 cpl_image* img_err = NULL;
99 cpl_image* img_bias = NULL;
100
101
102
103 double min_noise=-10;
104 double max_noise=10;
105 double mean_level=100;
106
107 double gauss_a=1.e5;
108 double gauss_sx=4;
109 double gauss_sy=4;
110 //const int dim=2;
111 cpl_size xpos=sx/2;
112 cpl_size ypos=sy/2;
113 int size=1+2*(gauss_sx+gauss_sy);
114
115 double norm=0;
116 double cen_x=0;
117 double cen_y=0;
118 double sig_x=0;
119 double sig_y=0;
120 double fwhm_x=0;
121 double fwhm_y=0;
122 cpl_frame* frm_raw=NULL;
123 cpl_frame* frm_bias=NULL;
125 const char* name_bias="bias.fits";
126 const char* name_raw="raw.fits";
127 xsh_pre* pre = NULL;
128 cpl_propertylist* plist=NULL;
129
132 cpl_msg_set_level( CPL_MSG_DEBUG);
134
137
138 check(img_bias=cpl_image_new(sx, sy,CPL_TYPE_FLOAT));
139 check(cpl_image_fill_noise_uniform(img_bias,min_noise,max_noise));
140 check(cpl_image_add_scalar(img_bias,mean_level));
141 check(img_raw=cpl_image_duplicate(img_bias));
142
143 plist=cpl_propertylist_new();
145
146
147 check(cpl_image_save(img_bias,name_bias,
148 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
149
150 xsh_free_propertylist(&plist);
151
152 check(frm_bias=xsh_frame_product(name_bias,"BIAS",
153 CPL_FRAME_TYPE_IMAGE,
154 CPL_FRAME_GROUP_RAW,
155 CPL_FRAME_LEVEL_FINAL));
156
157
158 check(gauss=cpl_image_new(sx,sy,CPL_TYPE_FLOAT));
159 check(cpl_image_fill_gaussian(gauss,xpos,ypos,gauss_a,gauss_sx,gauss_sy));
160 check(cpl_image_add(img_raw,gauss));
161
162 plist=cpl_propertylist_new();
164
165 check(cpl_image_save(img_raw,name_raw,
166 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
167
168 check(frm_raw=xsh_frame_product(name_raw,"RAW",
169 CPL_FRAME_TYPE_IMAGE,
170 CPL_FRAME_GROUP_RAW,
171 CPL_FRAME_LEVEL_FINAL));
172
173 xsh_free_propertylist(&plist);
174
175 xsh_msg("Predicted Pos: [%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "], Amp: %f Sigma: [%f,%f]",
176 xpos,ypos,gauss_a,gauss_sx,gauss_sy);
177
178 xsh_msg("Fit box params: pos=[%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "] size=%d",xpos,ypos,size);
179 img_err=cpl_image_duplicate(img_raw);
180 cpl_image_power(img_err,0.5);
181 int i=0;
182 cpl_array* parameters=cpl_array_new(7, CPL_TYPE_DOUBLE);
183 cpl_array* err_params=cpl_array_new(7, CPL_TYPE_DOUBLE);
184 cpl_array* fit_params=cpl_array_new(7, CPL_TYPE_INT);
185
186 /*All parameter should be fitted*/
187 for (i = 0; i < 7; i++)
188 cpl_array_set(fit_params, i, 1);
189
190 double rms=0;
191 double red_chisq=0;
192 cpl_matrix* covariance=NULL;
193 cpl_matrix* phys_cov=NULL;
194 double major=0;
195 double minor=0;
196 double angle=0;
197 const char *p[7] = { /* Parameter names */
198 "Background ",
199 "Normalisation ",
200 "Correlation ",
201 "Center position x",
202 "Center position y",
203 "Sigma x ",
204 "Sigma y "};
205
206
207
208 check(cpl_fit_image_gaussian(img_raw, img_err, xpos, ypos,size,size,
209 parameters,err_params,fit_params,
210 &rms,&red_chisq,&covariance,
211 &major,&minor,&angle,&phys_cov));
212/*
213 &norm, &cen_x, &cen_y,
214 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
215*/
216
217
218 for (i = 0; i < 7; i++){
219 cpl_msg_info(cpl_func,"%s: %f",
220 p[i], cpl_array_get(parameters,i,NULL));
221 }
222
223 xsh_msg("G Results: rms: %f red_chisq: %f major: %f minor: %f angle: %f",
224 rms,red_chisq,major,minor,angle);
225
226 cen_x=cpl_array_get(parameters,3,NULL);
227 cen_y=cpl_array_get(parameters,4,NULL);
228 sig_x=cpl_array_get(parameters,5,NULL);
229 sig_y=cpl_array_get(parameters,6,NULL);
230
231 xsh_msg("G Results: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
232 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
233
234 check(xsh_image_find_barycenter(img_raw,xpos,ypos,size,&norm,&cen_x,&cen_y,
235 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
236
237 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
238 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
239
240
241 check(pre=xsh_pre_create(frm_raw,NULL,img_bias,instrument,0,CPL_FALSE));
242
243 check(cpl_image_save(pre->data,"pre_ima_raw.fits",
244 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
245
246 check(cpl_image_get_maxpos(pre->data,&xpos,&ypos));
247
248 xsh_msg("Pos Max: [%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "]",xpos,ypos);
249
250 check(cpl_image_fit_gaussian(pre->data, xpos, ypos,size,&norm, &cen_x, &cen_y,
251 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
252
253 xsh_msg("G Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
254 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
255
256
257
258 check(xsh_image_find_barycenter(pre->data,xpos,ypos,size,&norm,&cen_x,&cen_y,
259 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
260
261 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
262 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
263
264 cleanup:
265
266 cpl_array_delete(parameters);
267 cpl_array_delete(err_params);
268 cpl_array_delete(fit_params);
269 cpl_matrix_delete(covariance);
270 cpl_matrix_delete(phys_cov);
271 xsh_pre_free(&pre);
272 xsh_free_image(&img_err);
273 xsh_free_image(&image);
274 xsh_free_image(&gauss);
275 xsh_free_image(&img_raw);
276 xsh_free_image(&img_bias);
277 xsh_free_frame(&frm_raw);
278 xsh_free_frame(&frm_bias);
279 xsh_free_propertylist(&plist);
281
283 if (cpl_error_get_code() != CPL_ERROR_NONE) {
284 xsh_error_dump(CPL_MSG_ERROR);
285 ret = 1;
286 }
287 return ret;
288}
289
int main()
Unit test of xsh_bspline_interpol.
static xsh_instrument * instrument
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
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
cpl_error_code xsh_image_find_barycenter(const cpl_image *im, int xpos, int ypos, int size, double *norm, double *xcen, double *ycen, double *sig_x, double *sig_y, double *fwhm_x, double *fwhm_y)
Apply a gaussian fit on an image sub window.
Definition: xsh_fit.c:696
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
xsh_instrument * xsh_instrument_new(void)
create new instrument structure
int size
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define MODULE_ID
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_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_image * data
Definition: xsh_data_pre.h:65
static cpl_error_code xsh_add_fits_key_min_set(cpl_propertylist *plist)
#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
@ XSH_ARM_UVB
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
#define XSH_PSZY
Definition: xsh_pfits.h:171
#define XSH_EXPTIME
Definition: xsh_pfits.h:125
#define XSH_CHIP_NY
Definition: xsh_pfits.h:152
#define XSH_PSZX
Definition: xsh_pfits.h:170
#define XSH_DET_PXSPACE
Definition: xsh_pfits.h:164
#define XSH_WIN_BINY
Definition: xsh_pfits.h:156
#define XSH_WIN_BINX
Definition: xsh_pfits.h:155
#define XSH_RON
Definition: xsh_pfits.h:161
#define XSH_DPR_TYPE
Definition: xsh_pfits.h:147
#define XSH_DET_GAIN
Definition: xsh_pfits.h:165
#define XSH_CONAD
Definition: xsh_pfits.h:162
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138