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
00055
00056
00057 #define MODULE_ID "XSH_DETECT_LINE_POS"
00058
00059 static cpl_error_code
00060 xsh_add_fits_key_min_set(cpl_propertylist* plist)
00061 {
00062
00063 cpl_propertylist_append_double(plist,XSH_EXPTIME,10.);
00064 cpl_propertylist_append_double(plist,XSH_RON,1.);
00065 cpl_propertylist_append_double(plist,XSH_CONAD,1.);
00066 cpl_propertylist_append_double(plist,XSH_DET_GAIN,1.);
00067 cpl_propertylist_append_int(plist,XSH_WIN_BINX,1);
00068 cpl_propertylist_append_int(plist,XSH_WIN_BINY,1);
00069 cpl_propertylist_append_double(plist,XSH_PSZX,15.);
00070 cpl_propertylist_append_double(plist,XSH_PSZY,15.);
00071
00072 cpl_propertylist_append_double(plist,XSH_DET_PXSPACE,1.800e-05);
00073 cpl_propertylist_append_int(plist,XSH_CHIP_NY,2048);
00074
00075
00076 return cpl_error_get_code();
00077
00078 }
00079
00080
00086
00087 int main( int argc, char** argv)
00088 {
00089 int ret = 0;
00090 int sx=4096;
00091 int sy=4096;
00092
00093
00094 cpl_image* image = NULL;
00095 cpl_image* gauss = NULL;
00096 cpl_image* img_raw = NULL;
00097 cpl_image* img_bias = NULL;
00098
00099
00100
00101 double min_noise=-10;
00102 double max_noise=10;
00103 double mean_level=100;
00104
00105 double gauss_a=1.e5;
00106 double gauss_sx=4;
00107 double gauss_sy=4;
00108 const int dim=2;
00109 int xpos=sx/2;
00110 int ypos=sy/2;
00111 int size=1+2*(gauss_sx+gauss_sy);
00112
00113 double norm=0;
00114 double cen_x=0;
00115 double cen_y=0;
00116 double sig_x=0;
00117 double sig_y=0;
00118 double fwhm_x=0;
00119 double fwhm_y=0;
00120 cpl_frame* frm_raw=NULL;
00121 cpl_frame* frm_bias=NULL;
00122 cpl_frame* frm_mask=NULL;
00123 xsh_instrument* instrument=NULL;
00124 const char* name_bias="bias.fits";
00125 const char* name_raw="raw.fits";
00126 const char* name_mask="mask.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: [%d,%d], Amp: %f Sigma: [%f,%f]",
00176 xpos,ypos,gauss_a,gauss_sx,gauss_sy);
00177
00178 xsh_msg("Fit box params: pos=[%d,%d] size=%d",xpos,ypos,size);
00179
00180 check(cpl_image_fit_gaussian(img_raw, xpos, ypos,size,&norm, &cen_x, &cen_y,
00181 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
00182
00183 xsh_msg("G Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00184 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00185
00186 check(xsh_image_find_barycenter(img_raw,xpos,ypos,size,&norm,&cen_x,&cen_y,
00187 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
00188
00189 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00190 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00191
00192
00193 check(pre=xsh_pre_create(frm_raw,NULL,img_bias,instrument,0));
00194
00195 check(cpl_image_save(pre->data,"pre_ima_raw.fits",
00196 CPL_BPP_IEEE_FLOAT,plist,CPL_IO_DEFAULT));
00197
00198 check(cpl_image_get_maxpos(pre->data,&xpos,&ypos));
00199
00200 xsh_msg("Pos Max: [%d,%d]",xpos,ypos);
00201
00202 check(cpl_image_fit_gaussian(pre->data, xpos, ypos,size,&norm, &cen_x, &cen_y,
00203 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
00204
00205 xsh_msg("G Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00206 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00207
00208
00209
00210 check(xsh_image_find_barycenter(pre->data,xpos,ypos,size,&norm,&cen_x,&cen_y,
00211 &sig_x, &sig_y, &fwhm_x, &fwhm_y));
00212
00213 xsh_msg("B Measured Pos: [%f,%f], Amp: %f Sigma: [%f,%f] FWHM: [%f,%f]",
00214 cen_x,cen_y,norm,sig_x,sig_y,fwhm_x,fwhm_y);
00215
00216 cleanup:
00217
00218 xsh_free_image(&image);
00219 xsh_free_image(&gauss);
00220 xsh_free_image(&img_raw);
00221 xsh_free_image(&img_bias);
00222 xsh_free_frame(&frm_raw);
00223 xsh_free_frame(&frm_bias);
00224 xsh_free_propertylist(&plist);
00225
00226
00227 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00228 xsh_error_dump(CPL_MSG_ERROR);
00229 ret = 1;
00230 }
00231 return ret;
00232 }
00233