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
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00036
00039
00040
00041
00042 #include <xsh_utils.h>
00043 #include <xsh_error.h>
00044 #include <xsh_msg.h>
00045 #include <xsh_data_instrument.h>
00046 #include <xsh_data_pre.h>
00047 #include <xsh_fit.h>
00048 #include <xsh_pfits.h>
00049 #include <tests.h>
00050 #include <cpl.h>
00051 #include <math.h>
00052 #include <time.h>
00053 #include <sys/time.h>
00054 #include <xsh_cpl_size.h>
00055
00056
00057
00058 #define MODULE_ID "XSH_DETECT_LINE_POS2"
00059
00060 static cpl_error_code
00061 xsh_add_fits_key_min_set(cpl_propertylist* plist)
00062 {
00063
00064 cpl_propertylist_append_double(plist,XSH_EXPTIME,10.);
00065 cpl_propertylist_append_double(plist,XSH_RON,1.);
00066 cpl_propertylist_append_double(plist,XSH_CONAD,1.);
00067 cpl_propertylist_append_double(plist,XSH_DET_GAIN,1.);
00068 cpl_propertylist_append_int(plist,XSH_WIN_BINX,1);
00069 cpl_propertylist_append_int(plist,XSH_WIN_BINY,1);
00070 cpl_propertylist_append_double(plist,XSH_PSZX,15.);
00071 cpl_propertylist_append_double(plist,XSH_PSZY,15.);
00072
00073 cpl_propertylist_append_double(plist,XSH_DET_PXSPACE,1.800e-05);
00074 cpl_propertylist_append_int(plist,XSH_CHIP_NY,2048);
00075
00076
00077 return cpl_error_get_code();
00078
00079 }
00080 #define PI_NUMB (3.1415926535897932384626433832795)
00081
00087
00088 int main( int argc, char** argv)
00089 {
00090 int ret = 0;
00091 int sx=4096;
00092 int sy=4096;
00093
00094
00095 cpl_image* image = NULL;
00096 cpl_image* gauss = NULL;
00097 cpl_image* img_err = NULL;
00098 cpl_image* img_line = NULL;
00099
00100
00101
00102 double min_noise=-10;
00103 double max_noise=10;
00104 double mean_level=100;
00105
00106 double gauss_a=1.e5;
00107 double gauss_sx=8;
00108 double gauss_sy=8;
00109
00110 cpl_size xpos=sx/2;
00111 cpl_size ypos=sy/2;
00112 int size=1+2*(gauss_sx+gauss_sy);
00113
00114 double norm=0;
00115 double cen_x=0;
00116 double cen_y=0;
00117 double sig_x=0;
00118 double sig_y=0;
00119 double fwhm_x=0;
00120 double fwhm_y=0;
00121 cpl_frame* frm_raw=NULL;
00122 cpl_frame* frm_line=NULL;
00123 xsh_instrument* instrument=NULL;
00124 const char* name_out="line.fits";
00125 const char* name_raw="raw.fits";
00126 xsh_pre* pre = NULL;
00127 cpl_propertylist* plist=NULL;
00128 const char* name_line = NULL;
00129 int nb_frames=0;
00130 double back=0;
00131
00132 TESTS_INIT( MODULE_ID);
00133 cpl_msg_set_level( CPL_MSG_DEBUG);
00134 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM) ;
00135
00136
00137
00138 nb_frames = argc - optind;
00139 if ( nb_frames == 1 ) {
00140 name_line = argv[optind];
00141 }
00142 else{
00143 xsh_msg( "********** NOT ENOUGH INPUT FRAMES **********" ) ;
00144 exit(0);
00145 }
00146
00147
00148 img_line = cpl_image_load(name_line,CPL_TYPE_DOUBLE,0,0);
00149 sx=cpl_image_get_size_x(img_line);
00150 sy=cpl_image_get_size_y(img_line);
00151 xpos=sx/2;
00152 ypos=sy/2;
00153
00154 instrument=xsh_instrument_new();
00155 xsh_instrument_set_arm(instrument,XSH_ARM_UVB);
00156 plist=cpl_propertylist_new();
00157 xsh_add_fits_key_min_set(plist);
00158
00159
00160 check(cpl_image_save(img_line,name_out,
00161 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
00162
00163 xsh_free_propertylist(&plist);
00164
00165 check(frm_line=xsh_frame_product(name_out,"BIAS",
00166 CPL_FRAME_TYPE_IMAGE,
00167 CPL_FRAME_GROUP_RAW,
00168 CPL_FRAME_LEVEL_FINAL));
00169
00170
00171 img_err=cpl_image_new(sx,sy,CPL_TYPE_DOUBLE);
00172 cpl_image_add_scalar(img_err,1.);
00173
00174
00175 int i=0;
00176 cpl_array* parameters=cpl_array_new(7, CPL_TYPE_DOUBLE);
00177 cpl_array* err_params=cpl_array_new(7, CPL_TYPE_DOUBLE);
00178 const cpl_array* fit_params=cpl_array_new(7, CPL_TYPE_INT);
00179
00180
00181 for (i = 0; i < 7; i++)
00182 cpl_array_set(fit_params, i, 1);
00183
00184 double rms=0;
00185 double red_chisq=0;
00186 cpl_matrix* covariance=NULL;
00187 cpl_matrix* phys_cov=NULL;
00188 double major=0;
00189 double minor=0;
00190 double angle=0;
00191 const char *p[7] = {
00192 "Background ",
00193 "Normalisation ",
00194 "Correlation ",
00195 "Center position x",
00196 "Center position y",
00197 "Sigma x ",
00198 "Sigma y "};
00199
00200
00201
00202 check(cpl_fit_image_gaussian(img_line, img_err, xpos, ypos,size,size,
00203 parameters,err_params,fit_params,
00204 &rms,&red_chisq,&covariance,
00205 &major,&minor,&angle,&phys_cov));
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 double rho=0;
00219 back=cpl_array_get(parameters,0,NULL);
00220 norm=cpl_array_get(parameters,1,NULL);
00221 rho=cpl_array_get(parameters,2,NULL);
00222
00223 cen_x=cpl_array_get(parameters,3,NULL);
00224 cen_y=cpl_array_get(parameters,4,NULL);
00225 sig_x=cpl_array_get(parameters,5,NULL);
00226 sig_y=cpl_array_get(parameters,6,NULL);
00227 fwhm_x=sig_x*CPL_MATH_FWHM_SIG;
00228 fwhm_y=sig_y*CPL_MATH_FWHM_SIG;
00229
00230 double sig_xy=sig_x*sig_x;
00231 double sig_x2=sig_x*sig_x;
00232 double sig_y2=sig_y*sig_y;
00233 double rho2=rho*rho;
00234 double theta=0.5*atan( 2.*rho * (sig_xy) / (sig_x2 - sig_y2) );
00235 double a=sig_xy*sqrt( 2.*(1.-rho2) * cos(2.*theta)/(sig_x2+sig_y2) * cos(2.*theta) + sig_y2 - sig_x2);
00236 double b=sig_xy*sqrt( 2.*(1.-rho2) * cos(2.*theta)/(sig_x2+sig_y2) * cos(2.*theta) - sig_y2 + sig_x2);
00237
00238 xsh_msg("%s R: [%f,%f], Back: %f Norm: %f Sigma: [%f,%f] FWHM [%f,%f] theta,a,b: [%f(%f),%f,%f]",
00239 name_line,cen_x,cen_y,back,norm,sig_x,sig_y,fwhm_x,fwhm_y,theta,theta*180./PI_NUMB,a,b);
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 cleanup:
00272
00273 xsh_free_image(&image);
00274 xsh_free_image(&gauss);
00275 xsh_free_image(&img_line);
00276 xsh_free_frame(&frm_raw);
00277 xsh_free_frame(&frm_line);
00278 xsh_free_propertylist(&plist);
00279
00280
00281 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00282 xsh_error_dump(CPL_MSG_ERROR);
00283 ret = 1;
00284 }
00285 return ret;
00286 }
00287