X-shooter Pipeline Reference Manual 3.8.15
test-xsh_flux.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-01-16 21:09:42 $
23 * $Revision: 1.6 $
24 */
25#ifdef HAVE_CONFIG_H
26# include <config.h>
27#endif
28
29/*--------------------------------------------------------------------------*/
35/*--------------------------------------------------------------------------*/
38/*---------------------------------------------------------------------------
39 Includes
40 ---------------------------------------------------------------------------*/
41
42
43#include <cpl.h>
44#include <xsh_data_instrument.h>
45#include <xsh_pfits.h>
46#include <xsh_msg.h>
47#include <xsh_utils.h>
48#include <xsh_utils_image.h>
49#include <xsh_data_order.h>
50#include <tests.h>
51#include <math.h>
52#include <xsh_cpl_size.h>
53
54static const double poly0_coeff[] = {
55 -256., 1., 0.001 } ;
56static const double poly1_coeff[] = {
57 -256., 1., 0.001 } ;
58/*---------------------------------------------------------------------------
59 Defines
60 ---------------------------------------------------------------------------*/
61#define MODULE_ID "XSH_FLUX"
62/*---------------------------------------------------------------------------
63 Functions prototypes
64 ---------------------------------------------------------------------------*/
65static double
66derivative_x(const double a1, const double a2, const double x){
67
68 return a1+2*a2*x;
69
70}
71
72static double
73derivative_y(const double a1, const double a2, const double y){
74
75 return a1+2*a2*y;
76
77}
78
79int main(void)
80{
81 cpl_image* ima_orig=NULL;
82 cpl_image* ima_warp=NULL;
83 cpl_image* ima_gaus=NULL;
84 cpl_image* ima_corr=NULL;
85 cpl_image* dXdx=NULL;
86 cpl_image* dYdy=NULL;
87 cpl_image* dXdy=NULL;
88 cpl_image* dYdx=NULL;
89
90
91 cpl_polynomial* poly_u=NULL;
92 cpl_polynomial* poly_v=NULL;
93 cpl_vector* xprofile=NULL;
94 cpl_vector* yprofile=NULL;
95
96 int nx=2048;
97 int ny=2048;
98 int sx=256;
99 int sy=256;
100 double sigx=16;
101 double sigy=16;
102 int xc=sx/2;
103 int yc=sy/2;
104 double flux=5.e4;
105 const int dim=2;
106 cpl_size pows[dim];
107
108
109 double f_new=0;
110 double f_org=0;
111
112 float* pdXdx=NULL;
113 float* pdYdy=NULL;
114 float* pdXdy=NULL;
115 float* pdYdx=NULL;
116 float* pcor=NULL;
117
118
119 int i=0;
120 int j=0;
121
124 xsh_msg("detect_continuum");
125
126 ima_orig=cpl_image_new(nx,ny,CPL_TYPE_FLOAT);
127 check(ima_gaus=cpl_image_new(sx,sy,CPL_TYPE_FLOAT));
128
129 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,flux,sigx,sigy));
130 cpl_image_copy(ima_orig,ima_gaus,1*(nx/4),1*(ny/4));
131
132 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,2*flux,sigx,sigy));
133 cpl_image_copy(ima_orig,ima_gaus,3*(nx/4),1*(ny/4));
134
135 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,3*flux,sigx,sigy));
136 cpl_image_copy(ima_orig,ima_gaus,2*(nx/4),2*(ny/4));
137
138 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,4*flux,sigx,sigy));
139 cpl_image_copy(ima_orig,ima_gaus,1*(nx/4),3*(ny/4));
140
141 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,5*flux,sigx,sigy));
142 cpl_image_copy(ima_orig,ima_gaus,3*(nx/4),3*(ny/4));
143 //cpl_image_add_scalar(ima_orig,bias);
144
145 cpl_image_save( ima_orig,"ima_orig.fits", CPL_BPP_IEEE_FLOAT, NULL,
146 CPL_IO_DEFAULT);
147 check(f_org=cpl_image_get_flux(ima_orig));
148
149 // Now warps the image
150 poly_u=cpl_polynomial_new(2);
151 poly_v=cpl_polynomial_new(2);
152
153
154 pows[0]=0;
155 pows[1]=0;
156 cpl_polynomial_set_coeff(poly_v,pows,poly0_coeff[0]);
157
158
159 pows[0]=0;
160 pows[1]=1;
161 cpl_polynomial_set_coeff(poly_v,pows,poly0_coeff[1]);
162
163
164 pows[0]=0;
165 pows[1]=2;
166 cpl_polynomial_set_coeff(poly_v,pows,poly0_coeff[2]);
167
168
169/* old version */
170 pows[0]=1;
171 pows[1]=0;
172 cpl_polynomial_set_coeff(poly_u,pows,1.);
173
174/*
175
176 pows[0]=0;
177 pows[1]=0;
178 cpl_polynomial_set_coeff(poly_u,pows,poly1_coeff[0]);
179
180
181 pows[0]=0;
182 pows[1]=1;
183 cpl_polynomial_set_coeff(poly_u,pows,poly1_coeff[1]);
184
185
186 pows[0]=0;
187 pows[1]=2;
188 cpl_polynomial_set_coeff(poly_u,pows,poly1_coeff[2]);
189*/
190 cpl_polynomial_dump(poly_u,stdout);
191 cpl_polynomial_dump(poly_v,stdout);
192
193
194 check(ima_warp=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
195 check(xprofile=cpl_vector_new(nx));
196 check(yprofile=cpl_vector_new(ny));
197 cpl_vector_fill_kernel_profile(xprofile, CPL_KERNEL_DEFAULT,
198 CPL_KERNEL_DEF_WIDTH);
199 cpl_vector_fill_kernel_profile(yprofile, CPL_KERNEL_DEFAULT,
200 CPL_KERNEL_DEF_WIDTH);
201
202 check(cpl_image_warp_polynomial(ima_warp,ima_orig,poly_u,poly_v,
203 xprofile,CPL_KERNEL_DEF_WIDTH,
204 yprofile,CPL_KERNEL_DEF_WIDTH));
205
206 check(ima_corr=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
207 // The following only with CPL4.5!!!
208 //check(xsh_image_warp_polynomial_scale(ima_corr,poly_u,poly_v));
209 cpl_image_save( ima_warp,"ima_warp.fits", CPL_BPP_IEEE_FLOAT, NULL,
210 CPL_IO_DEFAULT);
211 check(f_new=cpl_image_get_flux(ima_warp));
212 xsh_msg("Flux start: %g",f_org);
213 xsh_msg("Flux end: %g",f_new);
214
215
216
217 // Construct derivatives images
218 check(dXdx=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
219 check(dYdy=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
220 check(dXdy=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
221 check(dYdx=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
222
223 check(pdXdx=cpl_image_get_data_float(dXdx));
224 check(pdYdy=cpl_image_get_data_float(dYdy));
225 check(pdXdy=cpl_image_get_data_float(dXdy));
226 check(pdYdx=cpl_image_get_data_float(dYdx));
227 check(pcor=cpl_image_get_data_float(ima_corr));
228
229 //check(sup=cpl_vector_new(2));
230 //check(psup=cpl_vector_get_data(sup));
231 //check(inf=cpl_vector_new(2));
232 //check(pinf=cpl_vector_get_data(inf));
233
234 //Fill images with derivatives
235 for(j=1;j<ny-1;j++) {
236 for(i=1;i<nx-1;i++) {
237 //psup[0]=(double)(i+1);
238 //psup[1]=(double)j;
239
240 //pinf[0]=(double)(i-1);
241 //pinf[1]=(double)j;
242
243 pdXdx[i+j*nx]=1.;
244
245 pdYdx[i+j*nx]=derivative_x(poly0_coeff[1],poly0_coeff[2],(double)i);
246
247 //psup[0]=(double)i;
248 //psup[1]=(double)(j+1);
249
250 //pinf[0]=(double)i;
251 //pinf[1]=(double)(j-1);
252
253
254 pdXdy[i+j*nx]=0.;
255
256 pdYdy[i+j*nx]=derivative_y(poly0_coeff[1],poly0_coeff[2],(double)j);
257
258
259 //xsh_msg("i=%d j=%d pdYdx=%g pdXdy=%g",i,j,pdYdx[i+j*nx],pdXdy[i+j*nx]);
260
261 pcor[i+j*nx]=fabs(pdXdx[i+j*nx]*pdYdy[i+j*nx]-
262 pdXdy[i+j*nx]*pdYdx[i+j*nx]);
263
264 }
265 }
266
267
268 cpl_image_save( ima_corr,"ima_corr.fits", CPL_BPP_IEEE_FLOAT, NULL,
269 CPL_IO_DEFAULT);
270
271 cpl_image_multiply(ima_warp,ima_corr);
272
273 cpl_image_save( ima_warp,"ima_end.fits", CPL_BPP_IEEE_FLOAT, NULL,
274 CPL_IO_DEFAULT);
275
276
277 check(f_new=cpl_image_get_flux(ima_warp));
278 xsh_msg("Flux corr: %g",f_new);
279
280
281
282
283
284 cleanup:
285 xsh_free_image(&ima_orig);
286 xsh_free_image(&ima_warp);
287 xsh_free_image(&ima_gaus);
288 xsh_free_image(&ima_corr);
289 xsh_free_image(&dXdx);
290 xsh_free_image(&dYdy);
291 xsh_free_image(&dXdy);
292 xsh_free_image(&dYdx);
293
294
295 xsh_free_polynomial(&poly_u);
296 xsh_free_polynomial(&poly_v);
297 xsh_free_vector(&xprofile);
298 xsh_free_vector(&yprofile);
299
301 if (cpl_error_get_code() != CPL_ERROR_NONE) {
302 xsh_error_dump(CPL_MSG_ERROR);
303 return 1;
304 }
305 else {
306 return 0;
307 }
308
309}
310
static double derivative_x(const double a1, const double a2, const double x)
Definition: test-xsh_flux.c:66
static const double poly1_coeff[]
Definition: test-xsh_flux.c:56
int main(void)
Definition: test-xsh_flux.c:79
#define MODULE_ID
Definition: test-xsh_flux.c:61
static double derivative_y(const double a1, const double a2, const double y)
Definition: test-xsh_flux.c:73
static const double poly0_coeff[]
Definition: test-xsh_flux.c:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
int * y
int * x
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_free_polynomial(cpl_polynomial **p)
Deallocate a polynomial and set the pointer to NULL.
Definition: xsh_utils.c:2194
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
#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
int nx
int ny