sinfo_utl_table_test.c

00001 /* $Id: sinfo_utl_table_test.c,v 1.6 2012/03/03 10:38:03 amodigli Exp $
00002  *
00003  * This file is part of the SINFONI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2012/03/03 10:38:03 $
00024  * $Revision: 1.6 $
00025  * $Name: HEAD $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 #include <math.h>
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  ----------------------------------------------------------------------------*/
00035 #include <stdlib.h>
00036 /* cpl */
00037 #include <cpl.h>
00038 /* irplib */
00039 #include <irplib_utils.h>
00040 
00041 #include <sinfo_tpl_utils.h>
00042 #include <sinfo_pfits.h>
00043 #include <sinfo_tpl_dfs.h>
00044 #include <sinfo_msg.h>
00045 #include <sinfo_error.h>
00046 #include <sinfo_utils_wrappers.h>
00047 #include <sinfo_globals.h>
00048 #include <sinfo_recipes.h>
00049 #include <sinfo_function_1d.h>
00050 #include <sinfo_functions.h>
00051 #include <sinfo_fit.h>
00052 /*-----------------------------------------------------------------------------
00053                             Functions prototypes
00054  ----------------------------------------------------------------------------*/
00055 
00056 static int sinfo_utl_table_test_create(cpl_plugin *) ;
00057 static int sinfo_utl_table_test_exec(cpl_plugin *) ;
00058 static int sinfo_utl_table_test_destroy(cpl_plugin *) ;
00059 static int sinfo_utl_table_test_shift(cpl_parameterlist *, cpl_frameset *) ;
00060 
00061 static int
00062 sinfo_utl_table_test_amoeba_poly(cpl_parameterlist   *   parlist, 
00063              cpl_frameset        *   framelist);
00064 static int
00065 sinfo_utl_table_test_amoeba_boltzmann(cpl_parameterlist   *   parlist, 
00066              cpl_frameset        *   framelist);
00067 
00068 static cpl_vector* sa_vx=NULL;
00069 static cpl_vector* sa_vy=NULL;
00070 
00071 static double
00072 sinfo_fit_boltzmann(double p[]);
00073 
00074 
00075 static int 
00076 sinfo_fitbkg(const double x[], 
00077              const double a[], 
00078              double *result);
00079 
00080 static double
00081 sinfo_fac(const double x, const double t);
00082 
00083 static double
00084 sinfo_fit_poly(double p[]);;
00085 
00086 static cpl_table*
00087 sinfo_table_shift_column_spline3(cpl_table* t, 
00088                                  const char* col, 
00089                                  const double s);
00090 
00091 static cpl_table*
00092 sinfo_table_shift_column_poly(cpl_table* t, 
00093                               const char* col, 
00094                               const double s,
00095                               const int order);
00096 
00097 static cpl_table*
00098 sinfo_table_shift_column_int(const cpl_table* t, 
00099                              const char* col, 
00100                              const double s,
00101                                    double* r);
00102 
00103 static cpl_table*
00104 sinfo_table_shift_simple(cpl_table* inp, 
00105                          const char* col, 
00106                          const double shift);
00107 
00108 #define NPOINT 1000
00109 /*-----------------------------------------------------------------------------
00110                             Static variables
00111  ----------------------------------------------------------------------------*/
00112 
00113 static char sinfo_utl_table_test_description[] =
00114 "This recipe perform cubes combination.\n"
00115 "The input files are several cubeses\n"
00116 "their associated tags should be CUBE.\n"
00117 "The output is a cube PRO_CUBE resulting from the input cubes\n"
00118 "accurding to the value of op where op indicates the operation to be \n"
00119 "performed specified by the parameter sinfoni.sinfo_utl_table_test.op\n"
00120 "Information on relevant parameters can be found with\n"
00121 "esorex --params sinfo_utl_table_test\n"
00122 "esorex --help sinfo_utl_table_test\n"
00123 "\n";
00124 
00125 /*-----------------------------------------------------------------------------
00126                                 Functions code
00127  ----------------------------------------------------------------------------*/
00129 /*---------------------------------------------------------------------------*/
00133 /*---------------------------------------------------------------------------*/
00134 
00135 /*---------------------------------------------------------------------------*/
00143 /*---------------------------------------------------------------------------*/
00144 int cpl_plugin_get_info(cpl_pluginlist * list)
00145 {
00146     cpl_recipe  *   recipe = cpl_calloc(1, sizeof *recipe ) ;
00147     cpl_plugin  *   plugin = &recipe->interface ;
00148 
00149     cpl_plugin_init(plugin,
00150                     CPL_PLUGIN_API,
00151                     SINFONI_BINARY_VERSION,
00152                     CPL_PLUGIN_TYPE_RECIPE,
00153                     "sinfo_utl_table_test",
00154                     "Combines a cube list in an output cube",
00155                     sinfo_utl_table_test_description,
00156                     "Andrea Modigliani",
00157                     "Andrea.Modigliani@eso.org",
00158                     sinfo_get_license(),
00159                     sinfo_utl_table_test_create,
00160                     sinfo_utl_table_test_exec,
00161                     sinfo_utl_table_test_destroy) ;
00162 
00163     cpl_pluginlist_append(list, plugin) ;
00164     
00165     return 0;
00166 }
00167 
00168 /*---------------------------------------------------------------------------*/
00177 /*---------------------------------------------------------------------------*/
00178 static int sinfo_utl_table_test_create(cpl_plugin * plugin)
00179 {
00180     cpl_recipe      * recipe ;
00181     cpl_parameter   * p ;
00182 
00183     /* Get the recipe out of the plugin */
00184     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00185         recipe = (cpl_recipe *)plugin ;
00186     else return -1 ;
00187     cpl_error_reset();
00188     irplib_reset();
00189 
00190     /* Create the parameters list in the cpl_recipe object */
00191     recipe->parameters = cpl_parameterlist_new() ; 
00192 
00193     /* Fill the parameters list */
00194     /* --stropt */
00195     p = cpl_parameter_new_value("sinfoni.sinfo_utl_table_test.method", 
00196                                 CPL_TYPE_STRING, 
00197                                 "Output filename", 
00198                                 "sinfoni.sinfo_utl_table_test",
00199                                 "sinfo_clean_mean");
00200     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "method") ;
00201     cpl_parameterlist_append(recipe->parameters, p) ;
00202 
00203     /* --doubleopt */
00204     p = cpl_parameter_new_value("sinfoni.sinfo_utl_table_test.wshift", 
00205                                 CPL_TYPE_DOUBLE, 
00206                                 "Shift", 
00207                                 "sinfoni.sinfo_utl_table_test", 
00208                                 0.01) ;
00209 
00210     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wshift") ;
00211     cpl_parameterlist_append(recipe->parameters, p) ;
00212 
00213     /* --doubleopt */
00214     p = cpl_parameter_new_value("sinfoni.sinfo_utl_table_test.wstart", 
00215                                 CPL_TYPE_DOUBLE, 
00216                                 "Shift", 
00217                                 "sinfoni.sinfo_utl_table_test", 
00218                                 1.45) ;
00219 
00220 
00221     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wstart") ;
00222     cpl_parameterlist_append(recipe->parameters, p) ;
00223 
00224 
00225     /* --doubleopt */
00226     p = cpl_parameter_new_value("sinfoni.sinfo_utl_table_test.wend", 
00227                                 CPL_TYPE_DOUBLE, 
00228                                 "End Wavelength", 
00229                                 "sinfoni.sinfo_utl_table_test", 
00230                                 2.45) ;
00231 
00232 
00233     cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "wend") ;
00234     cpl_parameterlist_append(recipe->parameters, p) ;
00235 
00236 
00237     /* Return */
00238     return 0;
00239 }
00240 
00241 /*---------------------------------------------------------------------------*/
00247 /*---------------------------------------------------------------------------*/
00248 static int sinfo_utl_table_test_exec(cpl_plugin * plugin)
00249 {
00250     cpl_recipe  *   recipe ;
00251     int status=0;
00252     cpl_errorstate initial_errorstate = cpl_errorstate_get();
00253 
00254     /* Get the recipe out of the plugin */
00255     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00256         recipe = (cpl_recipe *)plugin ;
00257     else return -1 ;
00258     status+=sinfo_utl_table_test_shift(recipe->parameters, recipe->frames);
00259     status+=sinfo_utl_table_test_amoeba_poly(recipe->parameters, 
00260                                              recipe->frames);
00261     status+=sinfo_utl_table_test_amoeba_boltzmann(recipe->parameters, 
00262                                                   recipe->frames);
00263    if (!cpl_errorstate_is_equal(initial_errorstate)) {                      
00264         /* Dump the error history since recipe execution start.                
00265            At this point the recipe cannot recover from the error */           
00266         cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);              
00267     } 
00268     return status ;
00269 }
00270 
00271 /*---------------------------------------------------------------------------*/
00277 /*---------------------------------------------------------------------------*/
00278 static int sinfo_utl_table_test_destroy(cpl_plugin * plugin)
00279 {
00280     cpl_recipe  *   recipe ;
00281     
00282     /* Get the recipe out of the plugin */
00283     if (cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE) 
00284         recipe = (cpl_recipe *)plugin ;
00285     else return -1 ;
00286 
00287     cpl_parameterlist_delete(recipe->parameters) ; 
00288     return 0 ;
00289 }
00290 
00291 
00292 /*---------------------------------------------------------------------------*/
00299 /*---------------------------------------------------------------------------*/
00300 static int
00301 sinfo_utl_table_test_shift(cpl_parameterlist   *   parlist, 
00302             cpl_frameset        *   framelist)
00303 {
00304 
00305   cpl_table* t=NULL;
00306   cpl_table* t_shift=NULL;
00307   int np=NPOINT;
00308   cpl_parameter* p=NULL;
00309   const char* op=NULL;
00310   double ws=0;
00311   double we=0;
00312   double wd=0;
00313   double wshift=0;
00314   double pshift=0;
00315 
00316   double* pw=NULL;
00317   double* pi=NULL;
00318 
00319   int i=0;
00320 
00321   double ra=0;
00322   const double pg=3.1415926535897932384626433832795;
00323   
00324 
00325   check(sinfo_dfs_set_groups(framelist),
00326          "Cannot identify RAW and CALIB frames") ;
00327 
00328   // read input parameters
00329   check_nomsg(p=cpl_parameterlist_find(parlist, 
00330                 "sinfoni.sinfo_utl_table_test.method"));
00331   check_nomsg(op=cpl_parameter_get_string(p));
00332 
00333 
00334   check_nomsg(p=cpl_parameterlist_find(parlist,
00335                 "sinfoni.sinfo_utl_table_test.wstart"));
00336   check_nomsg(ws=cpl_parameter_get_double(p)) ;
00337 
00338 
00339   wd=(we-ws)/np;
00340   check_nomsg(p=cpl_parameterlist_find(parlist,
00341                 "sinfoni.sinfo_utl_table_test.wend"));
00342   check_nomsg(we=cpl_parameter_get_double(p)) ;
00343 
00344   check_nomsg(p=cpl_parameterlist_find(parlist,
00345                 "sinfoni.sinfo_utl_table_test.wshift"));
00346   check_nomsg(wshift=cpl_parameter_get_double(p)) ;
00347   
00348 
00349 
00350   // create the table
00351   check_nomsg(t=cpl_table_new(np));
00352   check_nomsg(cpl_table_new_column(t,"WAVE",CPL_TYPE_DOUBLE));
00353   check_nomsg(cpl_table_new_column(t,"INT",CPL_TYPE_DOUBLE));
00354   check_nomsg(cpl_table_fill_column_window(t,"WAVE",0,np,0));
00355   check_nomsg(cpl_table_fill_column_window(t,"INT",0,np,0));
00356 
00357   check_nomsg(pw=cpl_table_get_data_double(t,"WAVE"));
00358   check_nomsg(pi=cpl_table_get_data_double(t,"INT"));
00359 
00360 
00361 
00362   for(i=0;i<np;i++) {
00363     pw[i]=ws+i*wd;
00364     ra=(double)i/(double)np*6.*pg;
00365     pi[i]=cos(ra);
00366   }
00367   pshift=wshift/wd;
00368   check_nomsg(cpl_table_save(t, NULL, NULL, "out_cosine.fits", 0));
00369   /*
00370   sinfo_msg("shift1");
00371   cknull_nomsg(t_shift=sinfo_table_shift_column_int(t, "INT", pshift,&wrest));
00372   check_nomsg(cpl_table_save(t_shift,NULL,NULL,"out_cosine_shift1.fits", 0));
00373   sinfo_msg("shift2");
00374   sinfo_free_table(&t_shift);
00375   cknull_nomsg(t_shift=sinfo_table_shift_column_poly(t,"INT", pshift,2));
00376   check_nomsg(cpl_table_save(t_shift,NULL,NULL,"out_cosine_shift2.fits", 0));
00377   sinfo_msg("shift3");
00378   sinfo_free_table(&t_shift);
00379   cknull_nomsg(t_shift=sinfo_table_shift_column_spline3(t_shift,"INT",pshift));
00380 
00381   */
00382 
00383   check_nomsg(t_shift=sinfo_table_shift_simple(t,"INT",pshift));
00384   check_nomsg(cpl_table_save(t_shift,NULL,NULL, "out_cosine_shift3.fits", 0));
00385   sinfo_free_table(&t);
00386 
00387   return -1;
00388  cleanup:
00389 
00390   sinfo_free_table(&t);
00391   return -1;
00392 
00393 
00394 }
00395 
00396 
00397 
00398 /*---------------------------------------------------------------------------*/
00405 /*---------------------------------------------------------------------------*/
00406 static int
00407 sinfo_utl_table_test_amoeba_poly(cpl_parameterlist   *   parlist, 
00408             cpl_frameset        *   framelist)
00409 {
00410 
00411   cpl_table* t=NULL;
00412   int np=NPOINT;
00413   cpl_parameter* p=NULL;
00414   const char* op=NULL;
00415   double ws=0;
00416   double we=0;
00417   double wd=0;
00418   double wshift=0;
00419 
00420   double* pw=NULL;
00421   double* pi=NULL;
00422   double* pf=NULL;
00423   int i=0;
00424   int j=0;
00425   double a[3];
00426   double p0[3];
00427 
00428   
00429   // Amoeba fit:
00430   const int MP=4;
00431   const int NP=3;
00432   double y[MP];
00433   double** ap=NULL;
00434   double FTOL=2e-6;
00435   int nfunc=0;
00436 
00437   check(sinfo_dfs_set_groups(framelist),
00438          "Cannot identify RAW and CALIB frames") ;
00439 
00440   // read input parameters
00441   check_nomsg(p=cpl_parameterlist_find(parlist, 
00442                 "sinfoni.sinfo_utl_table_test.method"));
00443   check_nomsg(op=cpl_parameter_get_string(p));
00444 
00445 
00446   check_nomsg(p=cpl_parameterlist_find(parlist,
00447                 "sinfoni.sinfo_utl_table_test.wstart"));
00448   check_nomsg(ws=cpl_parameter_get_double(p)) ;
00449 
00450 
00451   wd=(we-ws)/np;
00452   check_nomsg(p=cpl_parameterlist_find(parlist,
00453                 "sinfoni.sinfo_utl_table_test.wend"));
00454   check_nomsg(we=cpl_parameter_get_double(p)) ;
00455 
00456   check_nomsg(p=cpl_parameterlist_find(parlist,
00457                 "sinfoni.sinfo_utl_table_test.wshift"));
00458   check_nomsg(wshift=cpl_parameter_get_double(p)) ;
00459   
00460 
00461   //Prepare a template function (polynomial)
00462   check_nomsg(t=cpl_table_new(np));
00463   check_nomsg(cpl_table_new_column(t,"WAVE",CPL_TYPE_DOUBLE));
00464   check_nomsg(cpl_table_new_column(t,"INT",CPL_TYPE_DOUBLE));
00465   check_nomsg(cpl_table_fill_column_window(t,"WAVE",0,np,0));
00466   check_nomsg(cpl_table_fill_column_window(t,"INT",0,np,0));
00467 
00468   check_nomsg(pw=cpl_table_get_data_double(t,"WAVE"));
00469   check_nomsg(pi=cpl_table_get_data_double(t,"INT"));
00470   ws=-2.;
00471   we=+4.;
00472   a[0]=+1.;
00473   a[1]=-2.;
00474   a[2]=1.;
00475   wd=(we-ws)/np;
00476   for(i=0;i<np;i++) {
00477     pw[i]=ws+i*wd;
00478     //pi[i]=(pw[i]-a[0])*(pw[i]-a[0])+1.*rand()/RAND_MAX;
00479     pi[i]=a[0]+a[1]*pw[i]+a[2]*pw[i]*pw[i]+1.*rand()/RAND_MAX;
00480   }
00481 
00482 
00483   sa_vx=cpl_vector_wrap(np,cpl_table_get_data_double(t,"WAVE"));
00484   sa_vy=cpl_vector_wrap(np,cpl_table_get_data_double(t,"INT"));
00485   // Amoeba part
00486   ap=(double**) cpl_calloc(MP,sizeof(double*));
00487   for(i=0;i<MP;i++) {
00488     ap[i]=cpl_calloc(NP,sizeof(double));
00489   }
00490 
00491   for(i=0;i<MP;i++) {
00492     for(j=0;j<NP;j++) {
00493       ap[i][j]=0;
00494     }
00495   }
00496   //ap[0][0]=-5;
00497   //ap[1][0]=-2;
00498 
00499   ap[0][0]=-3; ap[0][1]=-3; ap[0][2]=-3;
00500   ap[1][0]=+3; ap[1][1]=-3; ap[1][2]=-3;
00501   ap[2][0]=+3; ap[2][1]=+3; ap[2][2]=-3;
00502   ap[3][0]=+3; ap[3][1]=-3; ap[3][2]=+3;
00503 
00504   sinfo_msg("Before amoeba fit");
00505   for(i=0;i<MP;i++) {
00506     for(j=0;j<NP;j++) {
00507       sinfo_msg("ap[%d][%d]=%g",i,j,ap[i][j]);
00508     }
00509   }
00510 
00511   for(i=0;i<MP;i++) {
00512     p0[0]=ap[i][0]; 
00513     p0[1]=ap[i][1]; 
00514     p0[2]=ap[i][2];
00515     y[i]=sinfo_fit_poly(p0);
00516   }
00517 
00518   sinfo_fit_amoeba(ap,y,NP,FTOL,sinfo_fit_poly,&nfunc);
00519 
00520   sinfo_msg("After amoeba fit");
00521   for(i=0;i<MP;i++) {
00522     for(j=0;j<NP;j++) {
00523       sinfo_msg("ap[%d][%d]=%g",i,j,ap[i][j]);
00524     }
00525   }
00526 
00527   check_nomsg(cpl_table_new_column(t,"FIT",CPL_TYPE_DOUBLE));
00528   check_nomsg(cpl_table_fill_column_window(t,"FIT",0,np,0));
00529   check_nomsg(pf=cpl_table_get_data_double(t,"FIT"));
00530 
00531   wd=(we-ws)/np;
00532   for(i=0;i<np;i++) {
00533     pw[i]=ws+i*wd;
00534     pf[i]=ap[0][0]+ap[0][1]*pw[i]+ap[0][2]*pw[i]*pw[i];
00535   }
00536 
00537   check_nomsg(cpl_table_save(t,NULL,NULL, "out_amoeba_poly.fits", 0));
00538   cpl_vector_unwrap(sa_vx);
00539   cpl_vector_unwrap(sa_vy);
00540 
00541   sinfo_free_table(&t);
00542   return -1;
00543  cleanup:
00544   cpl_vector_unwrap(sa_vx);
00545   cpl_vector_unwrap(sa_vy);
00546 
00547   sinfo_free_table(&t);
00548   return -1;
00549 
00550 
00551 }
00552 
00553 /*---------------------------------------------------------------------------*/
00560 /*---------------------------------------------------------------------------*/
00561 static int
00562 sinfo_utl_table_test_amoeba_boltzmann(cpl_parameterlist   *   parlist, 
00563                        cpl_frameset        *   framelist)
00564 {
00565 
00566   cpl_table* t=NULL;
00567   int np=NPOINT;
00568   cpl_parameter* p=NULL;
00569   const char* op=NULL;
00570   double ws=0;
00571   double we=0;
00572   double wd=0;
00573   double wshift=0;
00574 
00575   double* pw=NULL;
00576   double* pi=NULL;
00577   double* pf=NULL;
00578   int i=0;
00579   int j=0;
00580   double a[3];
00581   double p0[3];
00582 
00583   
00584   // Amoeba fit:
00585   const int MP=4;
00586   const int NP=3;
00587   double y[MP];
00588   double** ap=NULL;
00589   double FTOL=2e-6;
00590   int nfunc=0;
00591   double max=0;
00592 
00593   double bkg_min=0;
00594   double bkg_max=0;
00595   double p0_min=0;
00596   double p0_max=0;
00597   double p1_min=0;
00598   double p1_max=0;
00599   double p2_min=0;
00600   double p2_max=0;
00601 
00602 
00603   check(sinfo_dfs_set_groups(framelist),
00604          "Cannot identify RAW and CALIB frames") ;
00605 
00606   // read input parameters
00607   check_nomsg(p=cpl_parameterlist_find(parlist, 
00608                 "sinfoni.sinfo_utl_table_test.method"));
00609   check_nomsg(op=cpl_parameter_get_string(p));
00610 
00611 
00612   check_nomsg(p=cpl_parameterlist_find(parlist,
00613                 "sinfoni.sinfo_utl_table_test.wstart"));
00614   check_nomsg(ws=cpl_parameter_get_double(p)) ;
00615 
00616 
00617   wd=(we-ws)/np;
00618   check_nomsg(p=cpl_parameterlist_find(parlist,
00619                 "sinfoni.sinfo_utl_table_test.wend"));
00620   check_nomsg(we=cpl_parameter_get_double(p)) ;
00621 
00622   check_nomsg(p=cpl_parameterlist_find(parlist,
00623                 "sinfoni.sinfo_utl_table_test.wshift"));
00624   check_nomsg(wshift=cpl_parameter_get_double(p)) ;
00625   
00626 
00627   //Prepare a template function (polynomial)
00628   check_nomsg(t=cpl_table_new(np));
00629   check_nomsg(cpl_table_new_column(t,"WAVE",CPL_TYPE_DOUBLE));
00630   check_nomsg(cpl_table_new_column(t,"INT",CPL_TYPE_DOUBLE));
00631   check_nomsg(cpl_table_fill_column_window(t,"WAVE",0,np,0));
00632   check_nomsg(cpl_table_fill_column_window(t,"INT",0,np,0));
00633 
00634   check_nomsg(pw=cpl_table_get_data_double(t,"WAVE"));
00635   check_nomsg(pi=cpl_table_get_data_double(t,"INT"));
00636   ws=1.4;
00637   we=2.5;
00638   wd=(we-ws)/np;
00639   a[2]=280;
00640   a[1]=55.817665;
00641   a[0]=548.77802;
00642   for(i=0;i<np;i++) {
00643     pw[i]=ws+i*wd;
00644     pi[i]=sinfo_fac(pw[i],a[2])*(1.+0.1*rand()/RAND_MAX);
00645   }
00646   check_nomsg(max=cpl_table_get_column_max(t,"INT"));
00647   check_nomsg(cpl_table_duplicate_column(t,"THERMAL",t,"INT"));
00648   check_nomsg(cpl_table_divide_scalar(t,"THERMAL",max));
00649   check_nomsg(cpl_table_multiply_scalar(t,"THERMAL",a[1]));
00650   check_nomsg(cpl_table_add_scalar(t,"THERMAL",a[0]));
00651 
00652   check_nomsg(sa_vx=cpl_vector_wrap(np,cpl_table_get_data_double(t,"WAVE")));
00653   check_nomsg(sa_vy=cpl_vector_wrap(np,cpl_table_get_data_double(t,"THERMAL")));
00654   // Amoeba part
00655   ap=(double**) cpl_calloc(MP,sizeof(double*));
00656   for(i=0;i<MP;i++) {
00657     ap[i]=cpl_calloc(NP,sizeof(double));
00658   }
00659 
00660   for(i=0;i<MP;i++) {
00661     for(j=0;j<NP;j++) {
00662       ap[i][j]=0;
00663     }
00664   }
00665   //ap[0][0]=-5;
00666   //ap[1][0]=-2;
00667   bkg_min=cpl_table_get_column_min(t,"THERMAL");
00668   bkg_max=cpl_table_get_column_max(t,"THERMAL");
00669 
00670   p0_min=bkg_min/2;
00671   p0_max=bkg_min*2;
00672   p1_min=bkg_min/2.;
00673   p1_max=bkg_max*2;
00674   p1_min=200;
00675   p2_max=300;
00676 
00677   ap[0][0]=p0_min; ap[0][1]=p1_min; ap[0][2]=p2_min;
00678   ap[1][0]=p0_max; ap[1][1]=p1_min; ap[1][2]=p2_min;
00679   ap[2][0]=p0_min; ap[2][1]=p1_max; ap[2][2]=p2_min;
00680   ap[3][0]=p0_min; ap[3][1]=p1_min; ap[3][2]=p2_max;
00681 
00682   sinfo_msg("Before amoeba fit");
00683   for(i=0;i<MP;i++) {
00684     for(j=0;j<NP;j++) {
00685       sinfo_msg("ap[%d][%d]=%g",i,j,ap[i][j]);
00686     }
00687   }
00688 
00689   for(i=0;i<MP;i++) {
00690     p0[0]=ap[i][0]; 
00691     p0[1]=ap[i][1]; 
00692     p0[2]=ap[i][2];
00693     y[i]=sinfo_fit_boltzmann(p0);
00694   }
00695 
00696   sinfo_fit_amoeba(ap,y,NP,FTOL,sinfo_fit_boltzmann,&nfunc);
00697 
00698   sinfo_msg("After amoeba fit");
00699   for(i=0;i<MP;i++) {
00700     for(j=0;j<NP;j++) {
00701       sinfo_msg("ap[%d][%d]=%g",i,j,ap[i][j]);
00702     }
00703   }
00704 
00705   check_nomsg(cpl_table_new_column(t,"FIT",CPL_TYPE_DOUBLE));
00706   check_nomsg(cpl_table_fill_column_window(t,"FIT",0,np,0));
00707   check_nomsg(pf=cpl_table_get_data_double(t,"FIT"));
00708 
00709   wd=(we-ws)/np;
00710   for(i=0;i<np;i++) {
00711     pw[i]=ws+i*wd;
00712     pf[i]=sinfo_fac(pw[i],ap[0][2]);
00713   }
00714   check_nomsg(max=cpl_table_get_column_max(t,"FIT"));
00715   check_nomsg(cpl_table_divide_scalar(t,"FIT",max));
00716   check_nomsg(cpl_table_multiply_scalar(t,"FIT",ap[0][1]));
00717   check_nomsg(cpl_table_add_scalar(t,"FIT",ap[0][0]));
00718 
00719   check_nomsg(cpl_table_save(t,NULL,NULL, "out_amoeba_boltzmann.fits", 0));
00720 
00721 
00722  cleanup:
00723 
00724   cpl_vector_unwrap(sa_vx);
00725   cpl_vector_unwrap(sa_vy);
00726   sinfo_free_table(&t);
00727 
00728   if (cpl_error_get_code() != CPL_ERROR_NONE) {
00729     return -1;
00730   } else {
00731     return 0;
00732   }
00733 
00734 
00735 
00736 }
00737 
00738 
00739 static cpl_table*
00740 sinfo_table_shift_column_int(const cpl_table* t, 
00741                              const char* col, 
00742                              const double s, 
00743                                    double* r)
00744 {
00745   cpl_table* out=NULL;
00746   int is=(int)s;
00747   int nrow=0;
00748   int i=0;
00749 
00750   double* pi=NULL;
00751   double* po=NULL;
00752 
00753   cknull(t,"null input table");
00754   out=cpl_table_duplicate(t);
00755   *r=s-is;
00756   nrow=cpl_table_get_nrow(t);
00757   pi=cpl_table_get_data_double(t,col);
00758   po=cpl_table_get_data_double(out,col);
00759   sinfo_msg("shifting of  %d pixels",is);
00760   if(is > 0 ) {
00761     for(i=0;i<nrow;i++) {
00762       if( ((i-is) >=0) && ((i-is) < nrow)) {
00763     po[i-is]=pi[i];
00764       }
00765     }
00766   } else {
00767     for(i=nrow-1;i>-1;i--) {
00768       if( ((i-is) >=0) && ((i-is) < nrow)) {
00769     po[i-is]=pi[i];
00770       }
00771     }
00772   }
00773   return out;
00774  cleanup:
00775   sinfo_free_table(&out);
00776   return NULL;
00777 
00778 }
00779 
00780 
00781 
00782 static cpl_table*
00783 sinfo_table_shift_column_poly(cpl_table* t, 
00784                               const char* col, 
00785                               const double shift,
00786                               const int order)
00787 {
00788   cpl_table* out=NULL;
00789   int nrow=0;
00790   int i=0;
00791   int flag=0;
00792   int n_points=0;
00793   int firstpos=0;
00794   int z=0;
00795   float eval=0;
00796   float sum=0;
00797   float new_sum=0;
00798   float* pi=NULL;
00799   float* po=NULL;
00800   float* spec=NULL ;
00801   float* corrected_spec=NULL ;
00802   float* xnum=NULL ;
00803   float* tableptr=NULL;
00804 
00805   cknull(t,"null input table");
00806   if ( order <= 0 ) {
00807     sinfo_msg_error("wrong order of interpolation polynom given!") ;
00808     goto cleanup;
00809   }
00810 
00811   out=cpl_table_duplicate(t);
00812 
00813   nrow=cpl_table_get_nrow(t);
00814   cpl_table_cast_column(t,col,"FINT",CPL_TYPE_FLOAT);
00815   cpl_table_cast_column(out,col,"FINT",CPL_TYPE_FLOAT);
00816   pi=cpl_table_get_data_float(t,"FINT");
00817   po=cpl_table_get_data_float(out,"FINT");
00818 
00819   n_points = order + 1 ;
00820   if ( n_points % 2 == 0 ) {
00821     firstpos = (int)(n_points/2) - 1 ;
00822   } else {
00823     firstpos = (int)(n_points/2) ;
00824   }
00825   spec=cpl_calloc(nrow,sizeof(float)) ;
00826   corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
00827   xnum=cpl_calloc(order+1,sizeof(float)) ;
00828   /* fill the xa[] array for the polint function */
00829   for ( i = 0 ; i < n_points ; i++ ) {
00830     xnum[i] = i ;
00831   }
00832 
00833 
00834   for(i=0;i<nrow;i++) {
00835     corrected_spec[i] = 0. ;
00836   }
00837 
00838   sum = 0. ;
00839   for ( z = 0 ; z < nrow ; z++ ) {
00840     spec[z] = pi[z] ;
00841     if (isnan(spec[z]) ) {
00842       spec[z] = 0. ;
00843                   
00844       for ( i = z - firstpos ; i < z-firstpos+n_points ; i++ ) {
00845     if ( i < 0 ) continue ;
00846     if ( i >= nrow) continue  ;
00847     corrected_spec[i] = ZERO ;
00848       }
00849     }
00850     if ( z != 0 && z != nrow - 1 ) {
00851       sum += spec[z] ;
00852     }
00853   }
00854 
00855   new_sum = 0. ;
00856   for ( z = 0 ; z < nrow ; z++ ) {
00857     /* ---------------------------------------------------------------
00858      * now determine the arrays of size n_points with which the
00859      * polynom is determined and determine the position eval
00860      * where the polynom is evaluated in polynomial interpolation.
00861      * Take care of the points near the row edges!
00862      */
00863     if (isnan(corrected_spec[z])) continue ;
00864     if ( z - firstpos < 0 ) {
00865       tableptr = &spec[0] ;
00866       eval     = shift + z ;
00867     } else if ( z - firstpos + n_points >= nrow ) {
00868       tableptr = &spec[nrow - n_points] ;
00869       eval     = shift + z + n_points - nrow ;
00870     } else {
00871       tableptr = &spec[z-firstpos] ;
00872       eval     = shift + firstpos ;
00873     }
00874 
00875     flag=0;
00876     corrected_spec[z]=sinfo_new_nev_ille(xnum,tableptr,order,eval,&flag);
00877     if ( z != 0 && z != nrow - 1 ) {
00878       new_sum += corrected_spec[z] ;
00879     }
00880   }
00881 
00882   /* fill the output spectrum */
00883   for (z = 0 ; z < nrow ; z++ ) {
00884     if ( new_sum == 0. ) {
00885       new_sum = 1. ;
00886     }
00887     if ( z == 0 ) {
00888       po[z] = ZERO ;
00889     } else if ( z == nrow - 1 ) {
00890       po[z] = ZERO ;
00891     } else if ( isnan(corrected_spec[z]) ) {
00892       po[z] = ZERO ;
00893     } else {
00894       corrected_spec[z] *= sum / new_sum ;
00895       po[z] = corrected_spec[z] ;
00896     }
00897   }
00898   check_nomsg(cpl_table_erase_column(t,"FINT"));
00899   check_nomsg(cpl_table_erase_column(out,col));
00900   check_nomsg(cpl_table_cast_column(out,"FINT",col,CPL_TYPE_DOUBLE));
00901   check_nomsg(cpl_table_erase_column(out,"FINT"));
00902 
00903   sinfo_free_float(&spec) ;
00904   sinfo_free_float(&corrected_spec) ;
00905   sinfo_free_float(&xnum) ;
00906 
00907   return out;
00908  cleanup:
00909 
00910 
00911   sinfo_free_float(&spec) ;
00912   sinfo_free_float(&corrected_spec) ;
00913   sinfo_free_float(&xnum) ;
00914   sinfo_free_table(&out);
00915   return NULL;
00916 
00917 
00918 }
00919 
00920 
00921 
00922 
00923 static cpl_table*
00924 sinfo_table_shift_column_spline3(cpl_table* t, 
00925                                  const char* col, 
00926                                  const double shift)
00927 {
00928   cpl_table* out=NULL;
00929   int nrow=0;
00930   int i=0;
00931   int z=0;
00932 
00933   float sum=0;
00934   float new_sum=0;
00935 
00936   float* pi=NULL;
00937   float* po=NULL;
00938   float* eval=NULL;
00939   float* xnum=NULL;
00940   float* spec=NULL;
00941   float* corrected_spec=NULL;
00942 
00943   cknull(t,"null input table");
00944   out=cpl_table_duplicate(t);
00945 
00946   nrow=cpl_table_get_nrow(t);
00947   check_nomsg(cpl_table_cast_column(t,col,"FINT",CPL_TYPE_FLOAT));
00948   check_nomsg(cpl_table_cast_column(out,col,"FINT",CPL_TYPE_FLOAT));
00949   pi=cpl_table_get_data_float(t,"FINT");
00950   po=cpl_table_get_data_float(out,"FINT");
00951   
00952 
00953 
00954   xnum=cpl_calloc(nrow,sizeof(float)) ;
00955   /* fill the xa[] array for the spline function */
00956   for ( i = 0 ; i < nrow ; i++ ) {
00957     xnum[i] = i ;
00958   }
00959 
00960   spec=cpl_calloc(nrow,sizeof(float)) ;
00961   corrected_spec=cpl_calloc(nrow,sizeof(float)) ;
00962   eval=cpl_calloc(nrow,sizeof(float)) ;
00963 
00964   sum = 0. ;
00965   for ( z = 0 ; z < nrow ; z++ ) {
00966     spec[z] = pi[z] ;
00967     if (isnan(spec[z]) ) {
00968       for ( i = z-1 ; i <= z+1 ; i++ ) {
00969     if ( i < 0 ) continue ;
00970     if ( i >= nrow) continue ;
00971     corrected_spec[i] = ZERO ;
00972       }
00973       spec[z] = 0. ;
00974     }
00975     sum += spec[z] ;
00976     eval[z] = (float)shift+(float)z ;
00977   }
00978   /* now we do the spline interpolation*/
00979   if ( -1 == sinfo_function1d_natural_spline(xnum,spec, nrow, 
00980                                              eval,corrected_spec, nrow))
00981     {
00982       sinfo_msg_error("error in spline interpolation!") ;
00983       goto cleanup;
00984     }
00985         
00986   new_sum = 0. ;
00987   for ( z = 0 ; z < nrow ; z++ ) {
00988     if ( isnan(corrected_spec[z]) ) {
00989       continue ;
00990     }
00991     new_sum += corrected_spec[z] ;
00992   }
00993   /* fill output imagelist */
00994   for ( z = 0 ; z < nrow ; z++ ) {
00995     if ( new_sum == 0. ) new_sum =1. ; 
00996     {
00997       if ( isnan(corrected_spec[z]) ) {
00998     po[z] = ZERO ;
00999       } else {
01000     corrected_spec[z] *= sum / new_sum ;
01001     po[z] = corrected_spec[z] ;
01002       }
01003     }
01004   }
01005 
01006   sinfo_free_float(&xnum);
01007   sinfo_free_float(&spec) ;
01008   sinfo_free_float(&corrected_spec) ;
01009   sinfo_free_float(&eval) ;
01010 
01011   check_nomsg(cpl_table_erase_column(t,"FINT"));
01012   check_nomsg(cpl_table_erase_column(out,col));
01013   check_nomsg(cpl_table_cast_column(out,"FINT",col,CPL_TYPE_DOUBLE));
01014   check_nomsg(cpl_table_erase_column(out,"FINT"));
01015 
01016   return out;
01017  cleanup:
01018 
01019   sinfo_free_float(&xnum);
01020   sinfo_free_float(&spec) ;
01021   sinfo_free_float(&corrected_spec) ;
01022   sinfo_free_float(&eval) ;
01023   sinfo_free_table(&out);
01024   return NULL;
01025 
01026 
01027 }
01028 
01029 
01030 static cpl_table*
01031 sinfo_table_shift_simple(cpl_table* inp, 
01032                          const char* col, 
01033                          const double shift)
01034 {
01035 
01036   int nrow=0;
01037   cpl_table* out=NULL;
01038   int is=(int)shift;
01039   double ds=shift-is;
01040   double* pi=NULL;
01041   double* po=NULL;
01042   double m=0;
01043   int i=0;
01044   cknull(inp,"null input table");
01045 
01046   check_nomsg(nrow=cpl_table_get_nrow(inp));
01047   check_nomsg(out=cpl_table_duplicate(inp));
01048   check_nomsg(cpl_table_fill_column_window(out,col,0,nrow,0));
01049   check_nomsg(pi=cpl_table_get_data_double(inp,col));
01050   check_nomsg(po=cpl_table_get_data_double(out,col));
01051 
01052 
01053   for(i=0;i<nrow;i++) {
01054     if((i+is)>0 && (i+is+1) < nrow) {
01055       m=pi[i+is+1]-pi[i+is];
01056       po[i]=pi[i+is]+m*ds;
01057     }
01058   }
01059   return out;
01060   cleanup:
01061   sinfo_free_table(&out);
01062   return NULL;
01063 
01064 }
01065 
01066 
01067 /*-------------------------------------------------------------------------*/
01074 /*--------------------------------------------------------------------------*/
01075 
01076 static double
01077 sinfo_fit_poly(double p[])
01078 
01079 {
01080 
01081   double* px=NULL;
01082   double* py=NULL; 
01083 
01084   int i=0;
01085   int np=0;
01086  
01087   double fy=0;  
01088   double chi2=0;
01089 
01090   check_nomsg(px= cpl_vector_get_data(sa_vx));
01091   check_nomsg(py= cpl_vector_get_data(sa_vy));
01092   check_nomsg(np= cpl_vector_get_size(sa_vx));
01093 
01094   for(i=0;i<np;i++) {
01095     //fy=(px[i]-p[0])*(px[i]-p[0]);
01096     fy=p[0]+p[1]*px[i]+p[2]*px[i]*px[i];
01097     chi2+=(py[i]-fy)*(py[i]-fy);
01098   } 
01099   
01100   return chi2;
01101  cleanup:
01102   return -1;
01103 
01104 }
01105 
01106 static double
01107 sinfo_fit_boltzmann(double p[])
01108 
01109 {
01110 
01111   double* px=NULL;
01112   double* py=NULL; 
01113   double* pv=NULL; 
01114   cpl_vector* vtmp=NULL;
01115   double max=0;
01116   int i=0;
01117   int np=0;
01118  
01119   double chi2=0;
01120 
01121   check_nomsg(px= cpl_vector_get_data(sa_vx));
01122   check_nomsg(py= cpl_vector_get_data(sa_vy));
01123   check_nomsg(np= cpl_vector_get_size(sa_vx));
01124   check_nomsg(vtmp=cpl_vector_duplicate(sa_vy));
01125   check_nomsg(pv=cpl_vector_get_data(vtmp));
01126    
01127   for(i=0;i<np;i++) {
01128     pv[i]=sinfo_fac(px[i],p[2]);
01129     //sinfo_msg("x=%g p=%g",px[i],pv[i]);
01130   }
01131   check_nomsg(max=cpl_vector_get_max(vtmp));
01132   if(max> 0) {
01133     check_nomsg(cpl_vector_divide_scalar(vtmp,max));
01134     check_nomsg(cpl_vector_multiply_scalar(vtmp,p[1]));
01135     check_nomsg(cpl_vector_add_scalar(vtmp,p[0]));
01136   }
01137 
01138 
01139   for(i=0;i<np;i++) {
01140     chi2+=(py[i]-pv[i])*(py[i]-pv[i]);
01141   } 
01142   
01143   return chi2;
01144  cleanup:
01145   return -1;
01146 
01147 }
01148 
01149 static int 
01150 sinfo_fitbkg(const double x[], 
01151              const double a[], 
01152              double *result)
01153 {
01154 
01155   double fac  = sinfo_fac(x[0],a[2]);
01156   *result = a[0]+a[1]*fac;
01157 
01158   return 0;
01159 }
01160 
01161 static double
01162 sinfo_fac(const double x, const double t)
01163 {
01164   
01165   double c=14387.7;
01166 
01167   return pow(x,-5.)/(exp(c/(x*fabs(t)))-1.);
01168 }
01169 
01170 

Generated on 3 Mar 2013 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1