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
00043 #include <tests.h>
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046
00047
00048 #include <xsh_data_pre.h>
00049 #include <xsh_error.h>
00050 #include <xsh_msg.h>
00051 #include <xsh_data_instrument.h>
00052 #include <xsh_data_spectrum.h>
00053 #include <xsh_data_localization.h>
00054 #include <xsh_drl.h>
00055 #include <xsh_pfits.h>
00056 #include <xsh_parameters.h>
00057 #include <xsh_badpixelmap.h>
00058 #include <xsh_utils_ifu.h>
00059 #include <xsh_utils.h>
00060 #include <cpl.h>
00061 #include <math.h>
00062
00063 #include <string.h>
00064 #include <getopt.h>
00065
00066
00067
00068
00069
00070 #define MODULE_ID "XSH_GAUSSIAN_FIT"
00071
00072 enum {
00073 HELP_OPT
00074 } ;
00075
00076 static struct option long_options[] = {
00077 {"help", 0, 0, HELP_OPT},
00078 {0, 0, 0, 0}
00079 };
00080
00081 static void Help( void )
00082 {
00083 puts( "Unitary test of xsh_gaussian_fit");
00084 puts( "Usage: test_xsh_gaussian_fit [options] DATA_FILE");
00085
00086 puts( "Options" ) ;
00087 puts( " --help : What you see" ) ;
00088 puts( "\nInput Files" ) ;
00089 puts( "DATA_FILE :ASCII x,y file y column is use for decomposition");
00090 TEST_END();
00091 }
00092
00093
00094 static void HandleOptions( int argc, char **argv)
00095 {
00096 int opt ;
00097 int option_index = 0;
00098
00099 while (( opt = getopt_long (argc, argv,
00100 "help",
00101 long_options, &option_index)) != EOF ){
00102
00103 switch ( opt ) {
00104 default:
00105 Help(); exit(-1);
00106 }
00107 }
00108 return;
00109 }
00110
00111
00112 int main( int argc, char **argv)
00113 {
00114
00115 int ret = 0 ;
00116 const char *file_name = NULL;
00117 FILE* file = NULL;
00118 char line[200];
00119 double xpos[10000];
00120 double ypos[10000];
00121 int i, size=0;
00122 int max = 10000;
00123 char res_name[256];
00124
00125
00126 double cpl_area=0, cpl_sigma=0, cpl_x0=0, cpl_offset=0;
00127 int status;
00128
00129 cpl_vector *pos_vect = NULL;
00130 cpl_vector *data_vect = NULL;
00131
00132
00133 double init_par[6];
00134 double errs_par[6];
00135
00136
00137 TESTS_INIT(MODULE_ID);
00138
00139 cpl_msg_set_level(CPL_MSG_DEBUG);
00140 xsh_debug_level_set(XSH_DEBUG_LEVEL_MEDIUM);
00141
00142
00143 HandleOptions( argc, argv);
00144
00145 if ( (argc - optind) > 0 ) {
00146 file_name = argv[optind];
00147 }
00148 else {
00149 Help();
00150 exit( 0);
00151 }
00152
00153 xsh_msg("---Input Files");
00154 xsh_msg("File : %s ", file_name);
00155
00156
00157 file = fopen( file_name, "r");
00158 if (file != NULL){
00159 size=0;
00160 while ( size < max && fgets( line, 200, file)){
00161 char col1[20];
00162 char col2[20];
00163
00164 if ( line[0] != '#'){
00165 sscanf(line,"%s %s", col1, col2);
00166 xpos[size] = atof(col1);
00167 ypos[size] = atof(col2);
00168 size++;
00169 }
00170 }
00171 }
00172 fclose(file);
00173
00174 check( pos_vect = cpl_vector_wrap( size, xpos));
00175 check( data_vect = cpl_vector_wrap( size, ypos));
00176
00177 cpl_vector_fit_gaussian( pos_vect, NULL, data_vect,
00178 NULL,CPL_FIT_ALL,&cpl_x0,&cpl_sigma,&cpl_area,&cpl_offset,NULL,NULL,NULL);
00179
00180 printf( "CPL FIT area %f x0 %f sigma %f offset %f\n", cpl_area, cpl_x0, cpl_sigma, cpl_offset);
00181
00182 xsh_gsl_init_gaussian_fit( pos_vect, data_vect, init_par);
00183 check( xsh_gsl_fit_gaussian( pos_vect, data_vect, 0, init_par, errs_par, &status));
00184
00185
00186 printf ("area = %.5f +/- %.5f\n", init_par[0], errs_par[0]);
00187 printf ("a = %.5f +/- %.5f\n", init_par[1], errs_par[1]);
00188 printf ("b = %.5f +/- %.5f\n", init_par[2], errs_par[2]);
00189 printf ("c = %.5f +/- %.5f\n", init_par[3], errs_par[3]);
00190 printf ("x0 = %.5f +/- %.5f\n", init_par[4], errs_par[4]);
00191 printf ("sigma = %.5f +/- %.5f\n", init_par[5], errs_par[5]);
00192
00193 sprintf( res_name, "GSL_FIT_%s.dat", file_name);
00194
00195 file = fopen( res_name, "w+");
00196 fprintf( file, "#x y\n");
00197
00198 for( i=0; i< size; i++){
00199 double step = xpos[1]-xpos[0];
00200 double j;
00201
00202 for (j=0; j< step; j+=step/10.){
00203 double t = xpos[i]+j;
00204
00205 double area = init_par[0];
00206 double a = init_par[1];
00207 double b = init_par[2];
00208 double c = init_par[3];
00209 double x0 = init_par[4];
00210 double sigma =init_par[5];
00211 double height = area/sqrt(2*M_PI*sigma*sigma);
00212
00213 double W = ((t-x0)*(t-x0))/(2*sigma*sigma);
00214
00215 double Yi = height*exp(-W)+a+b*t+c*t*t;
00216
00217
00218 fprintf( file, "%f %f\n", t, Yi);
00219 }
00220 }
00221 fclose(file);
00222
00223 sprintf( res_name, "CPL_FIT_%s.dat", file_name);
00224
00225 file = fopen( res_name, "w+");
00226 fprintf( file, "#x y\n");
00227
00228 for( i=0; i< size; i++){
00229 double step = xpos[1]-xpos[0];
00230 double j;
00231
00232 for (j=0; j< step; j+=step/10.){
00233 double t = xpos[i]+j;
00234
00235 double area = cpl_area;
00236 double a = cpl_offset;
00237 double b = 0;
00238 double c = 0;
00239 double x0 = cpl_x0;
00240 double sigma = cpl_sigma;
00241 double height = area/sqrt(2*M_PI*sigma*sigma);
00242
00243 double W = ((t-x0)*(t-x0))/(2*sigma*sigma);
00244
00245 double Yi = height*exp(-W)+a+b*t+c*t*t;
00246
00247
00248 fprintf( file, "%f %f\n", t, Yi);
00249 }
00250 }
00251 fclose(file);
00252
00253
00254
00255
00256 cleanup:
00257 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00258 xsh_error_dump(CPL_MSG_ERROR);
00259 ret=1;
00260 }
00261 xsh_unwrap_vector( &pos_vect);
00262 xsh_unwrap_vector( &data_vect);
00263 TEST_END();
00264 return ret ;
00265 }
00266