00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029
00035
00038
00039
00040
00041
00042
00043 #include <cpl.h>
00044 #include <xsh_data_instrument.h>
00045 #include <xsh_pfits.h>
00046 #include <xsh_msg.h>
00047 #include <xsh_utils.h>
00048 #include <xsh_utils_image.h>
00049 #include <xsh_data_order.h>
00050 #include <tests.h>
00051 #include <math.h>
00052 #include <xsh_cpl_size.h>
00053
00054 static const double poly0_coeff[] = {
00055 -256., 1., 0.001 } ;
00056 static const double poly1_coeff[] = {
00057 -256., 1., 0.001 } ;
00058
00059
00060
00061 #define MODULE_ID "XSH_FLUX"
00062
00063
00064
00065 static double
00066 derivative_x(const double a1, const double a2, const double x){
00067
00068 return a1+2*a2*x;
00069
00070 }
00071
00072 static double
00073 derivative_y(const double a1, const double a2, const double y){
00074
00075 return a1+2*a2*y;
00076
00077 }
00078
00079 int main(void)
00080 {
00081 cpl_image* ima_orig=NULL;
00082 cpl_image* ima_warp=NULL;
00083 cpl_image* ima_gaus=NULL;
00084 cpl_image* ima_corr=NULL;
00085 cpl_image* dXdx=NULL;
00086 cpl_image* dYdy=NULL;
00087 cpl_image* dXdy=NULL;
00088 cpl_image* dYdx=NULL;
00089
00090
00091 cpl_polynomial* poly_u=NULL;
00092 cpl_polynomial* poly_v=NULL;
00093 cpl_vector* xprofile=NULL;
00094 cpl_vector* yprofile=NULL;
00095
00096 int nx=2048;
00097 int ny=2048;
00098 int sx=256;
00099 int sy=256;
00100 double sigx=16;
00101 double sigy=16;
00102 int xc=sx/2;
00103 int yc=sy/2;
00104 double flux=5.e4;
00105 const int dim=2;
00106 cpl_size pows[dim];
00107
00108
00109 double f_new=0;
00110 double f_org=0;
00111
00112 float* pdXdx=NULL;
00113 float* pdYdy=NULL;
00114 float* pdXdy=NULL;
00115 float* pdYdx=NULL;
00116 float* pcor=NULL;
00117
00118
00119 int i=0;
00120 int j=0;
00121
00122 TESTS_INIT(MODULE_ID);
00123 xsh_msg("detect_continuum");
00124
00125 ima_orig=cpl_image_new(nx,ny,CPL_TYPE_FLOAT);
00126 check(ima_gaus=cpl_image_new(sx,sy,CPL_TYPE_FLOAT));
00127
00128 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,flux,sigx,sigy));
00129 cpl_image_copy(ima_orig,ima_gaus,1*(nx/4),1*(ny/4));
00130
00131 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,2*flux,sigx,sigy));
00132 cpl_image_copy(ima_orig,ima_gaus,3*(nx/4),1*(ny/4));
00133
00134 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,3*flux,sigx,sigy));
00135 cpl_image_copy(ima_orig,ima_gaus,2*(nx/4),2*(ny/4));
00136
00137 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,4*flux,sigx,sigy));
00138 cpl_image_copy(ima_orig,ima_gaus,1*(nx/4),3*(ny/4));
00139
00140 check(cpl_image_fill_gaussian(ima_gaus,xc,yc,5*flux,sigx,sigy));
00141 cpl_image_copy(ima_orig,ima_gaus,3*(nx/4),3*(ny/4));
00142
00143
00144 cpl_image_save( ima_orig,"ima_orig.fits", CPL_BPP_IEEE_FLOAT, NULL,
00145 CPL_IO_DEFAULT);
00146 check(f_org=cpl_image_get_flux(ima_orig));
00147
00148
00149 poly_u=cpl_polynomial_new(2);
00150 poly_v=cpl_polynomial_new(2);
00151
00152
00153 pows[0]=0;
00154 pows[1]=0;
00155 cpl_polynomial_set_coeff(poly_v,pows,poly0_coeff[0]);
00156
00157
00158 pows[0]=0;
00159 pows[1]=1;
00160 cpl_polynomial_set_coeff(poly_v,pows,poly0_coeff[1]);
00161
00162
00163 pows[0]=0;
00164 pows[1]=2;
00165 cpl_polynomial_set_coeff(poly_v,pows,poly0_coeff[2]);
00166
00167
00168
00169 pows[0]=1;
00170 pows[1]=0;
00171 cpl_polynomial_set_coeff(poly_u,pows,1.);
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 cpl_polynomial_dump(poly_u,stdout);
00190 cpl_polynomial_dump(poly_v,stdout);
00191
00192
00193 check(ima_warp=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00194 check(xprofile=cpl_vector_new(nx));
00195 check(yprofile=cpl_vector_new(ny));
00196 cpl_vector_fill_kernel_profile(xprofile, CPL_KERNEL_DEFAULT,
00197 CPL_KERNEL_DEF_WIDTH);
00198 cpl_vector_fill_kernel_profile(yprofile, CPL_KERNEL_DEFAULT,
00199 CPL_KERNEL_DEF_WIDTH);
00200
00201 check(cpl_image_warp_polynomial(ima_warp,ima_orig,poly_u,poly_v,
00202 xprofile,CPL_KERNEL_DEF_WIDTH,
00203 yprofile,CPL_KERNEL_DEF_WIDTH));
00204
00205 check(ima_corr=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00206
00207
00208 cpl_image_save( ima_warp,"ima_warp.fits", CPL_BPP_IEEE_FLOAT, NULL,
00209 CPL_IO_DEFAULT);
00210 check(f_new=cpl_image_get_flux(ima_warp));
00211 xsh_msg("Flux start: %g",f_org);
00212 xsh_msg("Flux end: %g",f_new);
00213
00214
00215
00216
00217 check(dXdx=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00218 check(dYdy=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00219 check(dXdy=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00220 check(dYdx=cpl_image_new(nx,ny,CPL_TYPE_FLOAT));
00221
00222 check(pdXdx=cpl_image_get_data_float(dXdx));
00223 check(pdYdy=cpl_image_get_data_float(dYdy));
00224 check(pdXdy=cpl_image_get_data_float(dXdy));
00225 check(pdYdx=cpl_image_get_data_float(dYdx));
00226 check(pcor=cpl_image_get_data_float(ima_corr));
00227
00228
00229
00230
00231
00232
00233
00234 for(j=1;j<ny-1;j++) {
00235 for(i=1;i<nx-1;i++) {
00236
00237
00238
00239
00240
00241
00242 pdXdx[i+j*nx]=1.;
00243
00244 pdYdx[i+j*nx]=derivative_x(poly0_coeff[1],poly0_coeff[2],(double)i);
00245
00246
00247
00248
00249
00250
00251
00252
00253 pdXdy[i+j*nx]=0.;
00254
00255 pdYdy[i+j*nx]=derivative_y(poly0_coeff[1],poly0_coeff[2],(double)j);
00256
00257
00258
00259
00260 pcor[i+j*nx]=fabs(pdXdx[i+j*nx]*pdYdy[i+j*nx]-
00261 pdXdy[i+j*nx]*pdYdx[i+j*nx]);
00262
00263 }
00264 }
00265
00266
00267 cpl_image_save( ima_corr,"ima_corr.fits", CPL_BPP_IEEE_FLOAT, NULL,
00268 CPL_IO_DEFAULT);
00269
00270 cpl_image_multiply(ima_warp,ima_corr);
00271
00272 cpl_image_save( ima_warp,"ima_end.fits", CPL_BPP_IEEE_FLOAT, NULL,
00273 CPL_IO_DEFAULT);
00274
00275
00276 check(f_new=cpl_image_get_flux(ima_warp));
00277 xsh_msg("Flux corr: %g",f_new);
00278
00279
00280
00281
00282
00283 cleanup:
00284 xsh_free_image(&ima_orig);
00285 xsh_free_image(&ima_warp);
00286 xsh_free_image(&ima_gaus);
00287 xsh_free_image(&ima_corr);
00288 xsh_free_image(&dXdx);
00289 xsh_free_image(&dYdy);
00290 xsh_free_image(&dXdy);
00291 xsh_free_image(&dYdx);
00292
00293
00294 xsh_free_polynomial(&poly_u);
00295 xsh_free_polynomial(&poly_v);
00296 xsh_free_vector(&xprofile);
00297 xsh_free_vector(&yprofile);
00298
00299 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00300 xsh_error_dump(CPL_MSG_ERROR);
00301 return 1;
00302 }
00303 else {
00304 return 0;
00305 }
00306
00307 }
00308