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 if(file != NULL) {
00173 fclose(file);
00174 }
00175
00176 check( pos_vect = cpl_vector_wrap( size, xpos));
00177 check( data_vect = cpl_vector_wrap( size, ypos));
00178
00179 cpl_vector_fit_gaussian( pos_vect, NULL, data_vect,
00180 NULL,CPL_FIT_ALL,&cpl_x0,&cpl_sigma,&cpl_area,&cpl_offset,NULL,NULL,NULL);
00181
00182 printf( "CPL FIT area %f x0 %f sigma %f offset %f\n", cpl_area, cpl_x0, cpl_sigma, cpl_offset);
00183
00184 xsh_gsl_init_gaussian_fit( pos_vect, data_vect, init_par);
00185 check( xsh_gsl_fit_gaussian( pos_vect, data_vect, 0, init_par, errs_par, &status));
00186
00187
00188 printf ("area = %.5f +/- %.5f\n", init_par[0], errs_par[0]);
00189 printf ("a = %.5f +/- %.5f\n", init_par[1], errs_par[1]);
00190 printf ("b = %.5f +/- %.5f\n", init_par[2], errs_par[2]);
00191 printf ("c = %.5f +/- %.5f\n", init_par[3], errs_par[3]);
00192 printf ("x0 = %.5f +/- %.5f\n", init_par[4], errs_par[4]);
00193 printf ("sigma = %.5f +/- %.5f\n", init_par[5], errs_par[5]);
00194
00195 sprintf( res_name, "GSL_FIT_%s.dat", file_name);
00196
00197 file = fopen( res_name, "w+");
00198 fprintf( file, "#x y\n");
00199
00200 for( i=0; i< size; i++){
00201 double step = xpos[1]-xpos[0];
00202 double j;
00203
00204 for (j=0; j< step; j+=step/10.){
00205 double t = xpos[i]+j;
00206
00207 double area = init_par[0];
00208 double a = init_par[1];
00209 double b = init_par[2];
00210 double c = init_par[3];
00211 double x0 = init_par[4];
00212 double sigma =init_par[5];
00213 double height = area/sqrt(2*M_PI*sigma*sigma);
00214
00215 double W = ((t-x0)*(t-x0))/(2*sigma*sigma);
00216
00217 double Yi = height*exp(-W)+a+b*t+c*t*t;
00218
00219
00220 fprintf( file, "%f %f\n", t, Yi);
00221 }
00222 }
00223 fclose(file);
00224
00225 sprintf( res_name, "CPL_FIT_%s.dat", file_name);
00226
00227 file = fopen( res_name, "w+");
00228 fprintf( file, "#x y\n");
00229
00230 for( i=0; i< size; i++){
00231 double step = xpos[1]-xpos[0];
00232 double j;
00233
00234 for (j=0; j< step; j+=step/10.){
00235 double t = xpos[i]+j;
00236
00237 double area = cpl_area;
00238 double a = cpl_offset;
00239 double b = 0;
00240 double c = 0;
00241 double x0 = cpl_x0;
00242 double sigma = cpl_sigma;
00243 double height = area/sqrt(2*M_PI*sigma*sigma);
00244
00245 double W = ((t-x0)*(t-x0))/(2*sigma*sigma);
00246
00247 double Yi = height*exp(-W)+a+b*t+c*t*t;
00248
00249
00250 fprintf( file, "%f %f\n", t, Yi);
00251 }
00252 }
00253 fclose(file);
00254
00255
00256
00257
00258 cleanup:
00259 if (cpl_error_get_code() != CPL_ERROR_NONE) {
00260 xsh_error_dump(CPL_MSG_ERROR);
00261 ret=1;
00262 }
00263 xsh_unwrap_vector( &pos_vect);
00264 xsh_unwrap_vector( &data_vect);
00265 TEST_END();
00266 return ret ;
00267 }
00268