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
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_raw = NULL;
00098 cpl_image* img_err = NULL;
00099 cpl_image* img_bias = NULL;
00100
00101
00102
00103 double min_noise=-10;
00104 double max_noise=10;
00105 double mean_level=100;
00106
00107 double gauss_a=1.e5;
00108 double gauss_sx=4;
00109 double gauss_sy=4;
00110
00111 cpl_size xpos=sx/2;
00112 cpl_size ypos=sy/2;
00113 int size=1+2*(gauss_sx+gauss_sy);
00114
00115 double norm=0;
00116 double cen_x=0;
00117 double cen_y=0;
00118 double sig_x=0;
00119 double sig_y=0;
00120 double fwhm_x=0;
00121 double fwhm_y=0;
00122 cpl_frame* frm_raw=NULL;
00123 cpl_frame* frm_bias=NULL;
00124 xsh_instrument* instrument=NULL;
00125 const char* name_bias="bias.fits";
00126 const char* name_raw="raw.fits";
00127 xsh_pre* pre = NULL;
00128 cpl_propertylist* plist=NULL;
00129
00130
00131 TESTS_INIT( MODULE_ID);
00132 cpl_msg_set_level( CPL_MSG_DEBUG);
00133 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM) ;
00134
00135 instrument=xsh_instrument_new();
00136 xsh_instrument_set_arm(instrument,XSH_ARM_UVB);
00137
00138 check(img_bias=cpl_image_new(sx, sy,CPL_TYPE_FLOAT));
00139 check(cpl_image_fill_noise_uniform(img_bias,min_noise,max_noise));
00140 check(cpl_image_add_scalar(img_bias,mean_level));
00141 check(img_raw=cpl_image_duplicate(img_bias));
00142
00143 plist=cpl_propertylist_new();
00144 xsh_add_fits_key_min_set(plist);
00145
00146
00147 check(cpl_image_save(img_bias,name_bias,
00148 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
00149
00150 xsh_free_propertylist(&plist);
00151
00152 check(frm_bias=xsh_frame_product(name_bias,"BIAS",
00153 CPL_FRAME_TYPE_IMAGE,
00154 CPL_FRAME_GROUP_RAW,
00155 CPL_FRAME_LEVEL_FINAL));
00156
00157
00158 check(gauss=cpl_image_new(sx,sy,CPL_TYPE_FLOAT));
00159 check(cpl_image_fill_gaussian(gauss,xpos,ypos,gauss_a,gauss_sx,gauss_sy));
00160 check(cpl_image_add(img_raw,gauss));
00161
00162 plist=cpl_propertylist_new();
00163 xsh_add_fits_key_min_set(plist);
00164
00165 check(cpl_image_save(img_raw,name_raw,
00166 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
00167
00168 check(frm_raw=xsh_frame_product(name_raw,"RAW",
00169 CPL_FRAME_TYPE_IMAGE,
00170 CPL_FRAME_GROUP_RAW,
00171 CPL_FRAME_LEVEL_FINAL));
00172
00173 xsh_free_propertylist(&plist);
00174
00175 xsh_msg("Predicted Pos: [%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "], Amp: %f Sigma: [%f,%f]",
00176 xpos,ypos,gauss_a,gauss_sx,gauss_sy);
00177
00178 xsh_msg("Fit box params: pos=[%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "] size=%d",xpos,ypos,size);
00179 img_err=cpl_image_duplicate(img_raw);
00180 cpl_image_power(img_err,0.5);
00181 int i=0;
00182 cpl_array* parameters=cpl_array_new(7, CPL_TYPE_DOUBLE);
00183 cpl_array* err_params=cpl_array_new(7, CPL_TYPE_DOUBLE);
00184 const cpl_array* fit_params=cpl_array_new(7, CPL_TYPE_INT);
00185
00186
00187 for (i = 0; i < 7; i++)
00188 cpl_array_set(fit_params, i, 1);
00189
00190 double rms=0;
00191 double red_chisq=0;
00192 cpl_matrix* covariance=NULL;
00193 cpl_matrix* phys_cov=NULL;
00194 double major=0;
00195 double minor=0;
00196 double angle=0;
00197 const char *p[7] = {
00198 "Background ",
00199 "Normalisation ",
00200 "Correlation ",
00201 "Center position x",
00202 "Center position y",
00203 "Sigma x ",
00204 "Sigma y "};
00205
00206
00207
00208 check(cpl_fit_image_gaussian(img_raw, img_err, xpos, ypos,size,size,
00209 parameters,err_params,fit_params,
00210 &rms,&red_chisq,&covariance,
00211 &major,&minor,&angle,&phys_cov));
00212
00213
00214
00215
00216
00217
00218 for (i = 0; i < 7; i++){
00219 cpl_msg_info(cpl_func,"%s: %f",
00220 p[i], cpl_array_get(parameters,i,NULL));
00221 }
00222
00223 xsh_msg("G Results: rms: %f red_chisq: %f major: %f minor: %f angle: %f",
00224 rms,red_chisq,major,minor,angle);
00225
00226 cen_x=cpl_array_get(parameters,3,NULL);
00227 cen_y=cpl_array_get(parameters,4,NULL);
00228 sig_x=cpl_array_get(parameters,5,NULL);
00229 sig_y=cpl_array_get(parameters,6,NULL);
00230
00231 xsh_msg("G Results: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00232 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00233
00234 check(xsh_image_find_barycenter(img_raw,xpos,ypos,size,&norm,&cen_x,&cen_y,
00235 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
00236
00237 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00238 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00239
00240
00241 check(pre=xsh_pre_create(frm_raw,NULL,img_bias,instrument,0,CPL_FALSE));
00242
00243 check(cpl_image_save(pre->data,"pre_ima_raw.fits",
00244 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
00245
00246 check(cpl_image_get_maxpos(pre->data,&xpos,&ypos));
00247
00248 xsh_msg("Pos Max: [%" CPL_SIZE_FORMAT ",%" CPL_SIZE_FORMAT "]",xpos,ypos);
00249
00250 check(cpl_image_fit_gaussian(pre->data, xpos, ypos,size,&norm, &cen_x, &cen_y,
00251 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
00252
00253 xsh_msg("G Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00254 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00255
00256
00257
00258 check(xsh_image_find_barycenter(pre->data,xpos,ypos,size,&norm,&cen_x,&cen_y,
00259 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
00260
00261 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00262 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00263
00264 cleanup:
00265
00266 xsh_free_image(&image);
00267 xsh_free_image(&gauss);
00268 xsh_free_image(&img_raw);
00269 xsh_free_image(&img_bias);
00270 xsh_free_frame(&frm_raw);
00271 xsh_free_frame(&frm_bias);
00272 xsh_free_propertylist(&plist);
00273
00274
00275 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00276 xsh_error_dump(CPL_MSG_ERROR);
00277 ret = 1;
00278 }
00279 return ret;
00280 }
00281