uves_physmod_regress_echelle.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: 2011/12/08 14:06:33 $
00023  * $Revision: 1.18 $
00024  * $Name: uves-5_0_0 $
00025  */
00026 
00027 /*----------------------------------------------------------------------------*/
00031 /*----------------------------------------------------------------------------*/
00032 
00033 #ifdef HAVE_CONFIG_H
00034 #  include <config.h>
00035 #endif
00036 
00037 /*-----------------------------------------------------------------------------
00038                                 Includes
00039  -----------------------------------------------------------------------------*/
00040 #include <uves_physmod_regress_echelle.h>
00041 
00042 #include <uves_physmod_necregr.h>
00043 #include <uves_utils_polynomial.h>
00044 #include <uves_utils_wrappers.h>
00045 #include <uves_pfits.h>
00046 #include <uves_error.h>
00047 #include <uves_msg.h>
00048 
00049 #include <cpl.h>
00050 
00051 /*-----------------------------------------------------------------------------
00052                                 Defines
00053  -----------------------------------------------------------------------------*/
00054 #define SELECT_FUNC_DOUBLE cpl_table_and_selected_double
00055 #define SELECT_FUNC_INT    cpl_table_and_selected_int
00056 /* Selected rows are ignored by many CPL functions (unlike MIDAS)
00057 Therefore SELECT/TABLE t SELECT.and. ...... 
00058 has different effect from cpl_table_and_selected_double(t, ...)
00059 
00060 To remove selected rows (which is also not exactly what MIDAS does):
00061 #define SELECT_FUNC_DOUBLE uves_extract_table_rows_local
00062 #define SELECT_FUNC_INT    uves_extract_table_rows_local
00063 (but this did not really make a difference)
00064 */
00066 /*-----------------------------------------------------------------------------
00067                             Functions prototypes
00068  ----------------------------------------------------------------------------*/
00069 /*-----------------------------------------------------------------------------
00070                             Static variables
00071  -----------------------------------------------------------------------------*/
00072 
00073 /*-----------------------------------------------------------------------------
00074                             Functions code
00075  -----------------------------------------------------------------------------*/
00076 
00077 /*----------------------------------------------------------------------------*/
00096 /*----------------------------------------------------------------------------*/
00097 
00098 int uves_physmod_regress_echelle(const uves_propertylist *raw_header,
00099                  enum uves_chip chip,
00100                  const char *recipe_id,
00101                  const cpl_parameterlist* parameters,
00102                  cpl_table** o_tbl, 
00103                  int num_outliers, 
00104                  double tol, 
00105                  double kappa,
00106                  cpl_table** s_tbl,
00107                  cpl_table** w_tbl)
00108 {
00109 
00110 
00111   int  CNT =0;
00112   double THRESH=0.;
00113   int ORDER=0;
00114   float RMSMAX=0.;
00115   float RMS=0.;
00116   int REJECT[300];
00117   int NBREJ=0;
00118   double start1=0;
00119   double start2=0;
00120   double step1=0;
00121   double step2=0;
00122   int scan1=0;
00123   int scan2=0;
00124   int naxis1=0;
00125   int naxis2=0;
00126 
00127   int def_pol1=0;
00128   int def_pol2=0;
00129 
00130   double inpr1=0;
00131   double inpr2=0;
00132   double inpr3=0;
00133   char OUTMODE='V';
00134   int ECHORD_1=0;
00135   int INPUTI_1=0;
00136   int INPUTI_2=0;
00137   int imsize1=0;
00138   int nraw=0;
00139   int ord_min=0;
00140   int ord_max=0;
00141   int status=0;
00142   double mean_sq_err_y=0;
00143   double outputd[10][10];
00144   polynomial* poly2d_y=NULL;
00145 
00146   uves_propertylist* plist=NULL;
00147 
00148 
00149 
00150 
00151   check (start1 = uves_pfits_get_crval1(raw_header),
00152      "Could not read start factor from input header");
00153 
00154   check (start2 = uves_pfits_get_crval2(raw_header),
00155      "Could not read start factor from input header");
00156   
00157   check (step1 = uves_pfits_get_cdelt1(raw_header),
00158      "Could not read step factor from input header");
00159   check (step2 = uves_pfits_get_cdelt2(raw_header),
00160      "Could not read step factor from input header");
00161   
00162   /* AMO */
00163   /* here should be arm dependent */
00164   check (scan2 = uves_pfits_get_nx(raw_header,chip),
00165      "Could not read scan1 factor from input header");
00166   check (scan1 = uves_pfits_get_ny(raw_header,chip),
00167      "Could not read scan2 factor from input header");
00168   /*
00169   check (scan2 = uves_pfits_get_out1nx(raw_header), 
00170        "Could not read scan1 factor from input header");
00171 
00172   check (scan1 = uves_pfits_get_out1ny(raw_header), 
00173        "Could not read scan1 factor from input header");
00174   */
00175   /* scan1 is allways 1 */
00176   scan1=1;
00177   check (naxis1 = uves_pfits_get_naxis1(raw_header),
00178      "Could not read naxis1 from input header");
00179   check (naxis2 = uves_pfits_get_naxis2(raw_header),
00180      "Could not read naxis2 from input header");
00181 
00182   uves_msg_debug("start=%f %f step=%f %f naxis=%d %d",
00183           start1,start2,step1,step2,naxis1,naxis2);
00184 
00185   imsize1=naxis2;
00186 
00187 
00188   /* for some reason in MIDAS prg STEP=START */
00189   step1=start1;
00190   step2=start2;
00191 
00192 
00193   check( uves_get_parameter(parameters, NULL, recipe_id, 
00194            "def_pol1", CPL_TYPE_INT, &def_pol1 ),
00195        "Could not read parameter");
00196 
00197   check( uves_get_parameter(parameters, NULL, recipe_id, 
00198            "def_pol2", CPL_TYPE_INT, &def_pol2 ),
00199        "Could not read parameter");
00200 
00201   uves_msg_debug("Polynomial %d %d",def_pol1,def_pol2);
00202 
00203 
00204   uves_msg_debug("Display orders positions...");
00205 
00206   inpr1 = start1;
00207   inpr2 = start1+(imsize1-1)*step1;
00208  
00209 
00210 if (inpr2 < inpr1) { 
00211   inpr3 = inpr1;
00212   inpr1 = inpr2;
00213   inpr2 = inpr3;
00214 }
00215 
00216 
00217 /* select in the table values which are in the range covered by the
00218    actual formatcheck frame */
00219   uves_msg_debug("1st select: inputr=%f %f",inpr1,inpr2);
00220   uves_msg_debug("ord Xmin=%f Xmax=%f",
00221           cpl_table_get_column_min(*o_tbl,"X"),
00222       cpl_table_get_column_max(*o_tbl,"X"));
00223 
00224   check(nraw=SELECT_FUNC_DOUBLE(*o_tbl,"X",CPL_NOT_LESS_THAN,inpr1),"Selection on X failed");
00225 
00226   uves_msg_debug("nraw=%d",nraw);
00227 
00228   check(nraw=SELECT_FUNC_DOUBLE(*o_tbl,"X",CPL_NOT_GREATER_THAN,inpr2),"Selection on X failed");
00229   uves_msg_debug("nraw=%d",nraw);
00230 
00231   /* scan1=1, scan2=naxis2*/
00232   inpr1 = start2+(scan1-1)*step2;
00233   inpr2 = start2+(scan2-1)*step2;
00234 
00235   
00236   if (inpr2 < inpr1) {
00237     inpr3 = inpr1;
00238     inpr1 = inpr2;
00239     inpr2 = inpr3;
00240   }
00241 
00242   uves_msg_debug("2nd select: inputr=%f %f",inpr1,inpr2);
00243 
00244   check(nraw=SELECT_FUNC_DOUBLE(*o_tbl,"Y",CPL_NOT_LESS_THAN,inpr1),"Selection on Y failed");
00245   uves_msg_debug("nraw=%d",nraw);
00246 
00247   check(nraw=SELECT_FUNC_DOUBLE(*o_tbl,"Y",CPL_NOT_GREATER_THAN,inpr2),"Selection on Y failed");
00248   uves_msg_debug("nraw=%d",nraw);
00249 
00250   /* this table has slightly different values with respect to MIDAS */
00251   
00252   if(-1 == uves_physmod_necregr(o_tbl,w_tbl)) {
00253     uves_msg_error("Error using uves_necregr");
00254     return -1;
00255   }
00256 
00257 *s_tbl=cpl_table_duplicate(*w_tbl);
00258 
00259 
00260 /* we sort the table */
00261   plist=uves_propertylist_new();
00262   uves_propertylist_append_bool(plist,"RMS",0);  /* 0 for ascending order */
00263   uves_table_sort(*s_tbl,plist);
00264   uves_free_propertylist(&plist);
00265 
00266   ord_min=cpl_table_get_column_min(*o_tbl,"ORDER");
00267   ord_max=cpl_table_get_column_max(*o_tbl,"ORDER");
00268   ECHORD_1=(ord_max-ord_min+1);
00269   ORDER=(ord_max-ord_min+1)/2;
00270   ORDER=uves_max_int(1,ORDER);
00271 
00272 
00273 
00274   RMSMAX=cpl_table_get_double(*s_tbl,"RMS",ORDER,&status);
00275 
00276   RMSMAX=3.5*RMSMAX;
00277   uves_msg_debug("RMSMAX=%f",RMSMAX);
00278  if (RMSMAX < 0.05)  RMSMAX = 0.05;
00279   uves_msg_debug("RMSMAX=%f",RMSMAX);
00280  
00281 
00282 
00283 
00284 
00285 if( OUTMODE != 'S') {
00286   uves_msg("Maximum admissible rms: %f pixels",RMSMAX);
00287 }
00288 
00289  
00290  for (ORDER = 0; ORDER<ECHORD_1; ORDER++){
00291  RMS = cpl_table_get_double(*w_tbl,"RMS",ORDER,&status);
00292  /*
00293  uves_msg_debug("RMS=%f",RMS);
00294  */
00295  if (RMS > RMSMAX){ 
00296    if (OUTMODE != 'S') {
00297      uves_msg_warning("Rejected order number %d RMS = %f pixels",ORDER,RMS);
00298    }
00299    REJECT[NBREJ] = ORDER;
00300    NBREJ = NBREJ + 1;
00301  }
00302 
00303 }
00304 
00305 
00306    for(ORDER = 1; ORDER<NBREJ; ORDER++){
00307        check(SELECT_FUNC_INT(*o_tbl,"ORDER",
00308          CPL_NOT_EQUAL_TO,REJECT[NBREJ]),"Error1 selecting ORDER");
00309 
00310    }
00311   INPUTI_1 = def_pol2 + 1;      /* Degree Y + 1 */
00312   INPUTI_2 = ECHORD_1 - NBREJ;  /* Number of orders minus rejected orders */
00313 if (INPUTI_1 > INPUTI_2) {
00314   uves_msg("*****************************************************");
00315   uves_msg("**** Warning : Number of selected orders {INPUTI(2)}");
00316   uves_msg("**** is too small for the current value of echelle");
00317   uves_msg("**** parameter DEFPOL(2)=%d", def_pol2);
00318   uves_msg("*****************************************************");
00319 
00320 
00321 }
00322 
00323   check(poly2d_y=uves_polynomial_regression_2d(*o_tbl,"X","ORDER","Y",
00324                          NULL,def_pol1,def_pol2,"YFIT",
00325                                              NULL,NULL,
00326                          &mean_sq_err_y,NULL,NULL,-1,-1),
00327                                          "Fitting YFIT failed");
00328   cpl_table_set_column_unit(*o_tbl,"YFIT","pix");
00329   cpl_table_set_column_unit(*o_tbl,"X","pix");
00330   cpl_table_set_column_unit(*o_tbl,"Y","pix");
00331 
00332   outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_y,0,0);
00333   outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_y,1,0);
00334   outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_y,0,1);
00335   outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_y,1,1);
00336   outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_y,2,0);
00337   outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_y,0,2);
00338   outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_y,1,2);
00339   outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_y,2,1);
00340   outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_y,2,2);
00341 
00342   cpl_table_duplicate_column(*o_tbl,"RESIDUAL",*o_tbl,"Y");
00343   cpl_table_subtract_columns(*o_tbl,"RESIDUAL","YFIT");
00344   cpl_table_set_column_unit(*o_tbl,"RESIDUAL","pix");
00345 
00346    uves_msg_debug("Error %f",sqrt(mean_sq_err_y));
00347   THRESH = kappa * sqrt(mean_sq_err_y);
00348   if (THRESH > tol)   THRESH = tol;
00349 
00350   /* do a selection on the absolute value of RESIDUAL */
00351   check(SELECT_FUNC_DOUBLE(*o_tbl,"RESIDUAL",
00352          CPL_LESS_THAN,THRESH),"Error1 selecting RESIDUAL");
00353 
00354   check(SELECT_FUNC_DOUBLE(*o_tbl,"RESIDUAL",
00355          CPL_GREATER_THAN,-THRESH),"Error2 selecting RESIDUAL");
00356 
00357   if (num_outliers >= 1)
00358       {
00359   
00360       uves_msg_debug("Kappa-Sigma clipping...");
00361       for(CNT = 1; CNT<num_outliers; CNT++) {
00362       
00363           /*
00364         REGRE/POLY  {ORDTAB} :Y :X,:ORDER {DEFPOL(1)},{DEFPOL(2)}  KEYLONG 
00365         SAVE/REGR   {ORDTAB} COEFF   KEYLONG
00366         COMPUTE/REGR   {ORDTAB} :YFIT = COEFF 
00367         COMPUTE/TABLE    {ORDTAB} :RESIDUAL = :Y - :YFIT;
00368           */
00369           
00370           uves_polynomial_delete(&poly2d_y);
00371           check(poly2d_y=uves_polynomial_regression_2d(*o_tbl,"X","ORDER","Y",
00372                                NULL,def_pol1,def_pol2,"YFIT",
00373                                NULL,NULL,
00374                                &mean_sq_err_y,NULL,NULL,-1,-1),
00375             "Fitting YFIT failed");
00376           
00377           if (OUTMODE != 'S') {
00378           uves_msg_debug("Ndata = %" CPL_SIZE_FORMAT " - Rms = %f pixels",
00379                cpl_table_get_nrow(*o_tbl),sqrt(mean_sq_err_y));
00380           }
00381           
00382           cpl_table_duplicate_column(*o_tbl,"RESIDUAL",*o_tbl,"Y");
00383           cpl_table_subtract_columns(*o_tbl,"RESIDUAL","YFIT");
00384           uves_msg_debug("error %f",sqrt(mean_sq_err_y));
00385           THRESH = kappa * sqrt(mean_sq_err_y);
00386           if (THRESH > tol)   THRESH = tol;
00387           /* do a selection on the absolute value of RESIDUAL */
00388           check(SELECT_FUNC_DOUBLE(*o_tbl,"RESIDUAL",
00389           CPL_LESS_THAN,THRESH),"Error3 selecting RESIDUAL");
00390           check(SELECT_FUNC_DOUBLE(*o_tbl,"RESIDUAL",
00391           CPL_GREATER_THAN,-THRESH),"Error4 selecting RESIDUAL");
00392           
00393       }
00394       }
00395 
00396 
00397   cleanup:
00398   uves_polynomial_delete(&poly2d_y);
00399   uves_free_propertylist(&plist);
00400   return 0;
00401 
00402 }

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1