X-shooter Pipeline Reference Manual 3.8.15
test-xsh_detect_line_file.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-19 06:36:29 $
23 * $Revision: 1.3 $
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 <getopt.h>
55#include <xsh_cpl_size.h>
56/*---------------------------------------------------------------------------
57 Defines
58 ---------------------------------------------------------------------------*/
59#define MODULE_ID "XSH_DETECT_LINE_FILE"
60
61static cpl_error_code
62xsh_add_fits_key_min_set(cpl_propertylist* plist)
63{
64
65 cpl_propertylist_append_double(plist,XSH_EXPTIME,10.);
66 cpl_propertylist_append_double(plist,XSH_RON,1.);
67 cpl_propertylist_append_double(plist,XSH_CONAD,1.);
68 cpl_propertylist_append_double(plist,XSH_DET_GAIN,1.);
69 cpl_propertylist_append_int(plist,XSH_WIN_BINX,1);
70 cpl_propertylist_append_int(plist,XSH_WIN_BINY,1);
71 cpl_propertylist_append_double(plist,XSH_PSZX,15.);
72 cpl_propertylist_append_double(plist,XSH_PSZY,15.);
73 //Only for NIR:
74 cpl_propertylist_append_double(plist,XSH_DET_PXSPACE,1.800e-05);
75 cpl_propertylist_append_int(plist,XSH_CHIP_NY,2048);
76
77
78 return cpl_error_get_code();
79
80}
81#define PI_NUMB (3.1415926535897932384626433832795)
82/*--------------------------------------------------------------------------*/
88/*--------------------------------------------------------------------------*/
89int main( int argc, char** argv)
90{
91 int ret = 0;
92 int sx=4096;
93 int sy=4096;
94
95 //char *image_name = NULL;
96 cpl_image* image = NULL;
97 cpl_image* gauss = NULL;
98 cpl_image* img_err = NULL;
99 cpl_image* img_line = 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=8;
109 double gauss_sy=8;
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_line=NULL;
125 const char* name_out="line.fits";
126 //const char* name_raw="raw.fits";
127 //xsh_pre* pre = NULL;
128 cpl_propertylist* plist=NULL;
129 const char* name_line = NULL;
130 int nb_frames=0;
131 double back=0;
132
134 cpl_msg_set_level( CPL_MSG_DEBUG);
136
137
138
139 nb_frames = argc - optind;
140 if ( nb_frames == 1 ) {
141 name_line = argv[optind];
142 }
143 else{
144 xsh_msg( "********** NOT ENOUGH INPUT FRAMES **********" ) ;
145 exit(0);
146 }
147
148
149 img_line = cpl_image_load(name_line,CPL_TYPE_DOUBLE,0,0);
150 sx=cpl_image_get_size_x(img_line);
151 sy=cpl_image_get_size_y(img_line);
152 xpos=sx/2;
153 ypos=sy/2;
154
157 plist=cpl_propertylist_new();
159
160
161 check(cpl_image_save(img_line,name_out,
162 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
163
164 xsh_free_propertylist(&plist);
165
166 check(frm_line=xsh_frame_product(name_out,"BIAS",
167 CPL_FRAME_TYPE_IMAGE,
168 CPL_FRAME_GROUP_RAW,
169 CPL_FRAME_LEVEL_FINAL));
170
171
172 img_err=cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
173 cpl_image_add_scalar(img_err,1.);
174 //cpl_image_power(img_err,0.5);
175
176 cpl_size i=0;
177 cpl_array* parameters=cpl_array_new(7, CPL_TYPE_DOUBLE);
178 cpl_array* err_params=cpl_array_new(7, CPL_TYPE_DOUBLE);
179 cpl_array* fit_params=cpl_array_new(7, CPL_TYPE_INT);
180
181 /*All parameter should be fitted*/
182 for (i = 0; i < 7; i++)
183 cpl_array_set(fit_params, i, 1);
184
185 double rms=0;
186 double red_chisq=0;
187 cpl_matrix* covariance=NULL;
188 cpl_matrix* phys_cov=NULL;
189 double major=0;
190 double minor=0;
191 double angle=0;
192 /* Parameter names
193 const char *p[7] = {
194 "Background ",
195 "Normalisation ",
196 "Correlation ",
197 "Center position x",
198 "Center position y",
199 "Sigma x ",
200 "Sigma y "};
201 */
202
203
204 check(cpl_fit_image_gaussian(img_line, img_err, xpos, ypos,size,size,
205 parameters,err_params,fit_params,
206 &rms,&red_chisq,&covariance,
207 &major,&minor,&angle,&phys_cov));
208
209 /*
210 for (i = 0; i < 7; i++){
211 cpl_msg_info(cpl_func,"%s: %f",
212 p[i], cpl_array_get(parameters,i,NULL));
213 }
214 */
215 /*
216 xsh_msg("G Results: rms: %f red_chisq: %f major: %f minor: %f angle: %f",
217 rms,red_chisq,major,minor,angle);
218 */
219
220 double rho=0;
221 back=cpl_array_get(parameters,0,NULL);
222 norm=cpl_array_get(parameters,1,NULL);
223 rho=cpl_array_get(parameters,2,NULL);
224
225 cen_x=cpl_array_get(parameters,3,NULL);
226 cen_y=cpl_array_get(parameters,4,NULL);
227 sig_x=cpl_array_get(parameters,5,NULL);
228 sig_y=cpl_array_get(parameters,6,NULL);
229 fwhm_x=sig_x*CPL_MATH_FWHM_SIG;
230 fwhm_y=sig_y*CPL_MATH_FWHM_SIG;
231
232 double sig_xy=sig_x*sig_x;
233 double sig_x2=sig_x*sig_x;
234 double sig_y2=sig_y*sig_y;
235 double rho2=rho*rho;
236 double theta=0.5*atan( 2.*rho * (sig_xy) / (sig_x2 - sig_y2) );
237 double a=sig_xy*sqrt( 2.*(1.-rho2) * cos(2.*theta)/(sig_x2+sig_y2) * cos(2.*theta) + sig_y2 - sig_x2);
238 double b=sig_xy*sqrt( 2.*(1.-rho2) * cos(2.*theta)/(sig_x2+sig_y2) * cos(2.*theta) - sig_y2 + sig_x2);
239
240 xsh_msg("%s R: [%f,%f], Back: %f Norm: %f Sigma: [%f,%f] FWHM [%f,%f] theta,a,b: [%f(%f),%f,%f]",
241 name_line,cen_x,cen_y,back,norm,sig_x,sig_y,fwhm_x,fwhm_y,theta,theta*180./PI_NUMB,a,b);
242 /*
243 check(xsh_image_find_barycenter(img_line,xpos,ypos,size,&norm,&cen_x,&cen_y,
244 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
245
246 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
247 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
248
249
250 check(pre=xsh_pre_create(frm_raw,NULL,img_line,instrument,0,CPL_FALSE));
251
252 check(cpl_image_save(pre->data,"pre_ima_raw.fits",
253 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
254
255 check(cpl_image_get_maxpos(pre->data,&xpos,&ypos));
256
257 xsh_msg("Pos Max: [%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "]",xpos,ypos);
258
259 check(cpl_image_fit_gaussian(pre->data, xpos, ypos,size,&norm, &cen_x, &cen_y,
260 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
261
262 xsh_msg("G Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
263 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
264
265
266
267 check(xsh_image_find_barycenter(pre->data,xpos,ypos,size,&norm,&cen_x,&cen_y,
268 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
269
270 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
271 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
272 */
273 cleanup:
274
275 xsh_free_image(&image);
276 xsh_free_image(&gauss);
277 xsh_free_image(&img_line);
278 xsh_free_frame(&frm_raw);
279 xsh_free_frame(&frm_line);
280 xsh_free_propertylist(&plist);
281
282
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
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
void xsh_instrument_set_arm(xsh_instrument *i, XSH_ARM arm)
Set an arm on 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
static cpl_error_code xsh_add_fits_key_min_set(cpl_propertylist *plist)
#define MODULE_ID
#define PI_NUMB
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
#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_DET_GAIN
Definition: xsh_pfits.h:165
#define XSH_CONAD
Definition: xsh_pfits.h:162
@ XSH_DEBUG_LEVEL_MEDIUM
Definition: xsh_utils.h:138