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: 2007/06/06 08:17:33 $
00023  * $Revision: 1.14 $
00024  * $Name: uves-4_2_2 $
00025  * $Log: uves_physmod_calmap.c,v $
00026  * Revision 1.14  2007/06/06 08:17:33  amodigli
00027  * replace tab with 4 spaces
00028  *
00029  * Revision 1.13  2007/04/24 12:50:29  jmlarsen
00030  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00031  *
00032  * Revision 1.12  2007/04/10 07:08:50  jmlarsen
00033  * Changed interface of polynomial_regression_2d()
00034  *
00035  * Revision 1.11  2006/11/06 15:19:41  jmlarsen
00036  * Removed unused include directives
00037  *
00038  * Revision 1.10  2006/10/24 14:12:16  jmlarsen
00039  * Parametrized recipe id to support FLAMES recipe
00040  *
00041  * Revision 1.9  2006/10/10 11:20:11  jmlarsen
00042  * Renamed line table columns to match MIDAS
00043  *
00044  * Revision 1.8  2006/10/05 11:16:41  jmlarsen
00045  * Declared parameter list const
00046  *
00047  * Revision 1.7  2006/08/23 15:41:06  amodigli
00048  * removed warning from checks on line length
00049  *
00050  * Revision 1.6  2006/06/20 10:56:56  amodigli
00051  * cleaned output, added units
00052  *
00053  * Revision 1.5  2006/06/07 12:45:19  jmlarsen
00054  * Added missing doxygen endmarker
00055  *
00056  * Revision 1.4  2006/06/01 14:21:51  amodigli
00057  * added Doxigen doc
00058  *
00059  * Revision 1.3  2006/04/24 09:22:16  jmlarsen
00060  * Added doxygen tag: addtogroup
00061  *
00062  * Revision 1.2  2006/03/08 13:25:39  amodigli
00063  * Aux column in linae tab has now proper values
00064  *
00065  * Revision 1.1  2006/02/03 07:46:30  jmlarsen
00066  * Moved recipe implementations to ./uves directory
00067  *
00068  * Revision 1.11  2006/01/16 08:01:57  amodigli
00069  *
00070  * Added stability check
00071  *
00072  * Revision 1.10  2006/01/13 13:43:15  jmlarsen
00073  * Removed memory leak
00074  *
00075  * Revision 1.9  2006/01/05 14:29:59  jmlarsen
00076  * Removed newline characters from output strings
00077  *
00078  * Revision 1.8  2006/01/05 09:32:33  amodigli
00079  * Fixed warning
00080  *
00081  * Revision 1.7  2006/01/05 09:29:37  amodigli
00082  * Aux col is now CPL_TYPE_DOUBLE
00083  *
00084  * Revision 1.6  2006/01/03 09:20:38  amodigli
00085  *
00086  * Added ABS_ORDER column to order table
00087  *
00088  * Revision 1.5  2005/12/22 14:37:43  amodigli
00089  * Fixed a problem of mismatch between CPL and MIDAS results (also MIDAS need same change)
00090  *
00091  * Revision 1.4  2005/12/22 10:11:48  amodigli
00092  * Fixed bug in Pixelsize computation
00093  *
00094  * Revision 1.3  2005/12/20 08:11:44  jmlarsen
00095  * Added CVS  entry
00096  *
00097  */
00098 /*----------------------------------------------------------------------------*/
00102 /*----------------------------------------------------------------------------*/
00105 #ifdef HAVE_CONFIG_H
00106 #  include <config.h>
00107 #endif
00108 
00109 #include <uves_physmod_calmap.h>
00110 
00111 #include <uves_physmod_regress_echelle.h>
00112 #include <uves_wavecal_utils.h>
00113 #include <uves_utils_wrappers.h>
00114 #include <uves_msg.h>
00115 #include <uves_error.h>
00116 
00140 int uves_physmod_calmap(const uves_propertylist *raw_header, 
00141             enum uves_chip chip,
00142             const char *recipe_id,
00143             const cpl_parameterlist* parameters, 
00144             cpl_table* npline_tbl,
00145             cpl_table** ord_tbl,
00146             cpl_table** lin_tbl,
00147             cpl_table** w_tbl,
00148             cpl_table** s_tbl,
00149             int* abs_ord_min,
00150             int* abs_ord_max,
00151             polynomial** poly2d)
00152 {
00153   int rel_ord=0;
00154   int ord_min=0;
00155   int ord_max=0;
00156   int ord_ech=0;
00157   double dis_avg=0;
00158 
00159   uves_propertylist* plist=NULL;
00160   polynomial* poly1d=NULL;
00161 
00162   double mean_err_aux=0;
00163   double mean_err_order=0;
00164 
00165   int num_outliers=0;
00166   double tol=0;
00167   double kappa=0.;
00168   double outputd[3];
00169     uves_msg_debug("start calmap");
00170 
00171 
00172 
00173     check(uves_get_parameter(parameters,NULL,recipe_id,"kappa",CPL_TYPE_DOUBLE,&kappa), 
00174        "Could not read parameter");
00175     check(uves_get_parameter(parameters,NULL,recipe_id,"tol",CPL_TYPE_DOUBLE,&tol), 
00176        "Could not read parameter");
00177 
00178   cpl_table_erase_invalid_rows(npline_tbl);
00179   uves_msg_debug("nraw=%d",cpl_table_get_nrow(npline_tbl));
00180   *ord_tbl=cpl_table_new(cpl_table_get_nrow(npline_tbl));
00181     
00182   cpl_table_duplicate_column(*ord_tbl,"ABS_ORDER",npline_tbl,"ORDER");
00183   cpl_table_duplicate_column(*ord_tbl,"ORDER",npline_tbl,"ORDER");
00184   cpl_table_duplicate_column(*ord_tbl,"X",npline_tbl,"XMOD");
00185   cpl_table_duplicate_column(*ord_tbl,"Y",npline_tbl,"YMOD");
00186  
00187   /* 
00188   cpl_table_dump(*ord_tbl,0,20,stdout);
00189   OLD VERSION: (WRONG)
00190   rel_ord = cpl_table_get_int(*ord_tbl,"ORDER",0,&status);
00191   */
00192   rel_ord = cpl_table_get_column_max(*ord_tbl,"ORDER");
00193 
00194   uves_msg_debug("relative order=%d",rel_ord);
00195   cpl_table_multiply_scalar(*ord_tbl,"ORDER",-1);
00196   cpl_table_add_scalar(*ord_tbl,"ORDER",rel_ord);
00197   cpl_table_add_scalar(*ord_tbl,"ORDER",+1);
00198 
00199 
00200   plist=uves_propertylist_new();
00201   uves_propertylist_append_bool(plist,"ORDER",0);  /* 0 for ascending order */
00202   uves_propertylist_append_bool(plist,"X",0);      /* 1 for descending order */
00203   uves_table_sort(*ord_tbl,plist);
00204   uves_free_propertylist(&plist);
00205   *abs_ord_min=cpl_table_get_column_min(*ord_tbl,"ORDER");
00206   *abs_ord_max=cpl_table_get_column_max(*ord_tbl,"ORDER");
00207 
00208   ord_ech=*abs_ord_max-*abs_ord_min+1;
00209   uves_msg_debug("Orders: max %d min %d  No %d",
00210            *abs_ord_max,*abs_ord_min,(*abs_ord_max-*abs_ord_min+1));
00211 
00212 
00213 
00214   uves_physmod_regress_echelle(raw_header,chip,
00215                    recipe_id,parameters,
00216                                ord_tbl,num_outliers, 
00217                                tol, kappa,s_tbl,w_tbl);
00218 
00219 
00220   /* we skip  prepare/background */
00221 
00222 
00223 
00224   uves_free_table(lin_tbl);
00225   *lin_tbl=cpl_table_new(cpl_table_get_nrow(npline_tbl));
00226 
00227 
00228   cpl_table_duplicate_column(*lin_tbl,"X",npline_tbl,"XMOD");
00229   cpl_table_duplicate_column(*lin_tbl,"Y",npline_tbl,"ORDER");
00230   cpl_table_duplicate_column(*lin_tbl,"PEAK",npline_tbl,"ORDER");
00231   cpl_table_duplicate_column(*lin_tbl,"Ident",npline_tbl,"IDENT");
00232   cpl_table_duplicate_column(*lin_tbl,"YNEW",npline_tbl,"YMOD");
00233   cpl_table_duplicate_column(*lin_tbl,"Order",npline_tbl,"ORDER");
00234   cpl_table_duplicate_column(*lin_tbl,"WAVEC",npline_tbl,"IDENT");
00235 
00236   cpl_table_duplicate_column(*lin_tbl,"AUX",npline_tbl,"ORDER");
00237   cpl_table_cast_column(*lin_tbl,"AUX","Aux",CPL_TYPE_DOUBLE);
00238   cpl_table_multiply_columns(*lin_tbl,"Aux","Ident");
00239   cpl_table_erase_column(*lin_tbl,"AUX");
00240 
00241 
00242 
00243   check(poly1d=uves_polynomial_regression_1d(*lin_tbl,"X","Aux",NULL,
00244                          3,"XREG",NULL,
00245                          &mean_err_aux,-1),
00246                                          "Fitting YDIF failed");
00247 
00248   /* NOTE THAT XREG is NOT an output column from MIDAS: should we remove it? */
00249 
00250   outputd[0]=uves_polynomial_get_coeff_1d(poly1d,0);
00251   outputd[1]=uves_polynomial_get_coeff_1d(poly1d,1);
00252   outputd[2]=uves_polynomial_get_coeff_1d(poly1d,2);
00253 
00254   ord_min=cpl_table_get_column_min(*lin_tbl,"Order");
00255   ord_max=cpl_table_get_column_max(*lin_tbl,"Order");
00256 
00257   uves_msg_debug("output0=%f output1=%f output2=%f",
00258            outputd[0],outputd[1],outputd[2]);
00259 
00260   uves_msg_debug("ord_max=%d ord_min=%d",ord_max,ord_min);
00261 
00262   cpl_table_duplicate_column(*lin_tbl,LINETAB_PIXELSIZE,*lin_tbl,"X");
00263   cpl_table_multiply_scalar(*lin_tbl,LINETAB_PIXELSIZE,2*outputd[2]);
00264   cpl_table_add_scalar(*lin_tbl,LINETAB_PIXELSIZE,outputd[1]);
00265   cpl_table_divide_columns(*lin_tbl,LINETAB_PIXELSIZE,"Order");
00266  
00267 
00268    dis_avg=cpl_table_get_column_mean(*lin_tbl,LINETAB_PIXELSIZE);
00269    uves_msg("Average pixel size: %f wav. units",dis_avg);
00270  
00271  
00272   check(*poly2d=uves_polynomial_regression_2d(*lin_tbl,"X","YNEW","Order",
00273                          NULL,4,4,"RORD",NULL,NULL,
00274                          &mean_err_order,NULL,NULL,-1,-1),
00275                                          "Fitting Order failed");
00276 
00277 
00278 
00279   /* NOTE THAT RORD is NOT an output column from MIDAS: should we remove it? */
00280 
00281 
00282   /*
00283 save/regr    {LINTAB} RORD KEYLONG
00284   */
00285 
00286   /* SHOULD WE SAVE RORD somewhere? */
00287 
00288   /* This saves the current section and parameters and tables: generates
00289      {P3}LINE.tbl {P3}ORDE.tbl {P3}back.tbl
00290 
00291 save/echelle  {P3} >Null
00292   */
00293 
00294   /*
00295 set/echelle   wlcmtd=guess guess={P3} 
00296   */
00297 
00298 
00299   uves_msg_debug("Line Table: ncol=%d",cpl_table_get_ncol(*lin_tbl));
00300   cleanup:
00301   uves_free_propertylist(&plist);
00302   uves_polynomial_delete(&poly1d);
00303   
00304     uves_msg_debug("end calmap");
00305   return 0;
00306 }

Generated on Mon Apr 21 10:56:55 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1