uves_physmod_calmap.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019  
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2010/09/24 09:32:06 $
00023  * $Revision: 1.20 $
00024  * $Name: uves-4_9_1 $
00025  */
00026 /*----------------------------------------------------------------------------*/
00030 /*----------------------------------------------------------------------------*/
00033 #ifdef HAVE_CONFIG_H
00034 #  include <config.h>
00035 #endif
00036 
00037 #include <uves_physmod_calmap.h>
00038 
00039 #include <uves_physmod_regress_echelle.h>
00040 #include <uves_wavecal_utils.h>
00041 #include <uves_utils_wrappers.h>
00042 #include <uves_msg.h>
00043 #include <uves_error.h>
00044 
00068 int uves_physmod_calmap(const uves_propertylist *raw_header, 
00069             enum uves_chip chip,
00070             const char *recipe_id,
00071             const cpl_parameterlist* parameters, 
00072             cpl_table* npline_tbl,
00073             cpl_table** ord_tbl,
00074             cpl_table** lin_tbl,
00075             cpl_table** w_tbl,
00076             cpl_table** s_tbl,
00077             int* abs_ord_min,
00078             int* abs_ord_max,
00079             polynomial** poly2d)
00080 {
00081   int rel_ord=0;
00082   int ord_min=0;
00083   int ord_max=0;
00084   int ord_ech=0;
00085   double dis_avg=0;
00086 
00087   uves_propertylist* plist=NULL;
00088   polynomial* poly1d=NULL;
00089 
00090   double mean_err_aux=0;
00091   double mean_err_order=0;
00092 
00093   int num_outliers=0;
00094   double tol=0;
00095   double kappa=0.;
00096   double outputd[3];
00097   uves_msg_debug("start calmap");
00098 
00099 
00100 
00101   check(uves_get_parameter(parameters,NULL,recipe_id,"kappa",CPL_TYPE_DOUBLE,&kappa), 
00102     "Could not read parameter");
00103   check(uves_get_parameter(parameters,NULL,recipe_id,"tol",CPL_TYPE_DOUBLE,&tol), 
00104     "Could not read parameter");
00105 
00106   cpl_table_erase_invalid_rows(npline_tbl);
00107   uves_msg_debug("nraw=%d",cpl_table_get_nrow(npline_tbl));
00108   *ord_tbl=cpl_table_new(cpl_table_get_nrow(npline_tbl));
00109     
00110   cpl_table_duplicate_column(*ord_tbl,"ABS_ORDER",npline_tbl,"ORDER");
00111   cpl_table_duplicate_column(*ord_tbl,"ORDER",npline_tbl,"ORDER");
00112   cpl_table_duplicate_column(*ord_tbl,"X",npline_tbl,"XMOD");
00113   cpl_table_duplicate_column(*ord_tbl,"Y",npline_tbl,"YMOD");
00114  
00115   /* 
00116   cpl_table_dump(*ord_tbl,0,20,stdout);
00117   OLD VERSION: (WRONG)
00118   rel_ord = cpl_table_get_int(*ord_tbl,"ORDER",0,&status);
00119   */
00120   rel_ord = cpl_table_get_column_max(*ord_tbl,"ORDER");
00121 
00122   uves_msg_debug("relative order=%d",rel_ord);
00123   cpl_table_multiply_scalar(*ord_tbl,"ORDER",-1);
00124   cpl_table_add_scalar(*ord_tbl,"ORDER",rel_ord);
00125   cpl_table_add_scalar(*ord_tbl,"ORDER",+1);
00126 
00127 
00128   plist=uves_propertylist_new();
00129   uves_propertylist_append_bool(plist,"ORDER",0);  /* 0 for ascending order */
00130   uves_propertylist_append_bool(plist,"X",0);      /* 1 for descending order */
00131   uves_table_sort(*ord_tbl,plist);
00132   uves_free_propertylist(&plist);
00133   *abs_ord_min=cpl_table_get_column_min(*ord_tbl,"ORDER");
00134   *abs_ord_max=cpl_table_get_column_max(*ord_tbl,"ORDER");
00135 
00136   ord_ech=*abs_ord_max-*abs_ord_min+1;
00137   uves_msg_debug("Orders: max %d min %d  No %d",
00138            *abs_ord_max,*abs_ord_min,(*abs_ord_max-*abs_ord_min+1));
00139 
00140 
00141 
00142   uves_physmod_regress_echelle(raw_header,chip,
00143                    recipe_id,parameters,
00144                                ord_tbl,num_outliers, 
00145                                tol, kappa,s_tbl,w_tbl);
00146 
00147 
00148   /* we skip  prepare/background */
00149 
00150 
00151 
00152   uves_free_table(lin_tbl);
00153   *lin_tbl=cpl_table_new(cpl_table_get_nrow(npline_tbl));
00154 
00155 
00156   cpl_table_duplicate_column(*lin_tbl,"X",npline_tbl,"XMOD");
00157   cpl_table_duplicate_column(*lin_tbl,"Y",npline_tbl,"ORDER");
00158   cpl_table_duplicate_column(*lin_tbl,"PEAK",npline_tbl,"ORDER");
00159   cpl_table_duplicate_column(*lin_tbl,"Ident",npline_tbl,"IDENT");
00160   cpl_table_duplicate_column(*lin_tbl,"YNEW",npline_tbl,"YMOD");
00161   cpl_table_duplicate_column(*lin_tbl,"Order",npline_tbl,"ORDER");
00162   cpl_table_duplicate_column(*lin_tbl,"WAVEC",npline_tbl,"IDENT");
00163 
00164   cpl_table_duplicate_column(*lin_tbl,"AUX",npline_tbl,"ORDER");
00165   cpl_table_cast_column(*lin_tbl,"AUX","Aux",CPL_TYPE_DOUBLE);
00166   cpl_table_multiply_columns(*lin_tbl,"Aux","Ident");
00167   cpl_table_erase_column(*lin_tbl,"AUX");
00168 
00169   cpl_table_set_column_unit(*lin_tbl,"WAVEC","Ang");
00170   cpl_table_set_column_unit(*lin_tbl,"YNEW","pix");
00171   cpl_table_set_column_unit(*lin_tbl,"Ident","Ang");
00172   /* we check that the product Aux=ORDER*Ident=m*lambda=d*sin(alpha) is a
00173      constant as is expected in a grating. A non constant product is an 
00174      indication of detector rotation (this is the case: the product is 
00175      not exacly constant).
00176   */
00177   check(poly1d=uves_polynomial_regression_1d(*lin_tbl,"X","Aux",NULL,
00178                          3,"XREG",NULL,
00179                          &mean_err_aux,-1),
00180                                          "Fitting YDIF failed");
00181 
00182   cpl_table_set_column_unit(*lin_tbl,"XREG","pix");
00183   cpl_table_set_column_unit(*lin_tbl,"X","pix");
00184 
00185   /* NOTE THAT XREG is NOT an output column from MIDAS: should we remove it? */
00186 
00187   outputd[0]=uves_polynomial_get_coeff_1d(poly1d,0);
00188   outputd[1]=uves_polynomial_get_coeff_1d(poly1d,1);
00189   outputd[2]=uves_polynomial_get_coeff_1d(poly1d,2);
00190 
00191   ord_min=cpl_table_get_column_min(*lin_tbl,"Order");
00192   ord_max=cpl_table_get_column_max(*lin_tbl,"Order");
00193 
00194   uves_msg_debug("output0=%f output1=%f output2=%f",
00195            outputd[0],outputd[1],outputd[2]);
00196 
00197   uves_msg_debug("ord_max=%d ord_min=%d",ord_max,ord_min);
00198 
00199   cpl_table_duplicate_column(*lin_tbl,LINETAB_PIXELSIZE,*lin_tbl,"X");
00200   cpl_table_multiply_scalar(*lin_tbl,LINETAB_PIXELSIZE,2*outputd[2]);
00201   cpl_table_add_scalar(*lin_tbl,LINETAB_PIXELSIZE,outputd[1]);
00202   cpl_table_divide_columns(*lin_tbl,LINETAB_PIXELSIZE,"Order");
00203  
00204 
00205    dis_avg=cpl_table_get_column_mean(*lin_tbl,LINETAB_PIXELSIZE);
00206    uves_msg("Average pixel size: %f wav. units",dis_avg);
00207  
00208  
00209   check(*poly2d=uves_polynomial_regression_2d(*lin_tbl,"X","YNEW","Order",
00210                          NULL,4,4,"RORD",NULL,NULL,
00211                          &mean_err_order,NULL,NULL,-1,-1),
00212                                          "Fitting Order failed");
00213 
00214 
00215 
00216   /* NOTE THAT RORD is NOT an output column from MIDAS: should we remove it? */
00217 
00218 
00219   /*
00220 save/regr    {LINTAB} RORD KEYLONG
00221   */
00222 
00223   /* SHOULD WE SAVE RORD somewhere? */
00224 
00225   /* This saves the current section and parameters and tables: generates
00226      {P3}LINE.tbl {P3}ORDE.tbl {P3}back.tbl
00227 
00228 save/echelle  {P3} >Null
00229   */
00230 
00231   /*
00232 set/echelle   wlcmtd=guess guess={P3} 
00233   */
00234 
00235 
00236   uves_msg_debug("Line Table: ncol=%d",cpl_table_get_ncol(*lin_tbl));
00237   cleanup:
00238   uves_free_propertylist(&plist);
00239   uves_polynomial_delete(&poly1d);
00240   
00241     uves_msg_debug("end calmap");
00242   return 0;
00243 }

Generated on 8 Mar 2011 for UVES Pipeline Reference Manual by  doxygen 1.6.1