uves_physmod_msrawxy.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:05:02 $
00023  * $Revision: 1.21 $
00024  * $Name: uves-5_0_0 $
00025  */
00026 
00027 /*----------------------------------------------------------------------------*/
00031 /*----------------------------------------------------------------------------*/
00034 #ifdef HAVE_CONFIG_H
00035 #  include <config.h>
00036 #endif
00037 /*-----------------------------------------------------------------------------
00038                                 Includes
00039  -----------------------------------------------------------------------------*/
00040 #include <uves_physmod_msrawxy.h>
00041 
00042 #include <uves_physmod_center_gauss.h>
00043 #include <uves_pfits.h>
00044 #include <uves_utils_polynomial.h>
00045 #include <uves_utils_wrappers.h>
00046 #include <uves_msg.h>
00047 #include <uves_error.h>
00048 
00049 /*-----------------------------------------------------------------------------
00050                             Functions code
00051  -----------------------------------------------------------------------------*/
00052 
00053 /*----------------------------------------------------------------------------*/
00083 /*----------------------------------------------------------------------------*/
00084 
00085 
00086 int uves_physmod_msrawxy(const cpl_image *raw_image, 
00087              const uves_propertylist *raw_header, 
00088              const char *recipe_id,
00089              const cpl_parameterlist* parameters, 
00090              cpl_table* mod_tbl,
00091              cpl_table** l_tbl,
00092              cpl_table** m_tbl,
00093              cpl_table** p_tbl,
00094              cpl_table** rline_tbl,
00095              cpl_table** mline_tbl,
00096              cpl_table** npline_tbl)
00097     
00098 {
00099   double scl_fct=0;
00100   double add_fct=0;
00101   double      X_AXIS_SCALE=0;    
00102   double      Y_AXIS_SCALE=0;
00103   double      OFFSET_X=0;    
00104   double      OFFSET_Y=0;
00105 
00106 
00107   int status=0;
00108 
00109 
00110   double limit[4]={0.,0.,0.,0.};
00111   int      MBOX_X=0;
00112   int      MBOX_Y=0;
00113  
00114   double tmp_x=0;
00115   double tmp_y=0;
00116   double tmp_val_x=0;
00117   double tmp_val_y=0;
00118 
00119 
00120 
00121   polynomial* poly2d_x=NULL;
00122   polynomial* poly2d_y=NULL;
00123   double mean_err_xdif=0;
00124   double mean_err_ydif=0;
00125  
00126   double binx=0;
00127   double biny=0;
00128 
00129   double mbox_x=0;
00130   double mbox_y=0;
00131   double outputd[3][3];
00132   int k=0;
00133 
00134   cpl_table * tmp_tbl=NULL;
00135   cpl_table * tmp_tbl1=NULL;
00136   cpl_table * tmp_tbl2=NULL;
00137   cpl_table * tmp_tbl3=NULL;
00138   cpl_table * tmp_tbl4=NULL;
00139  
00140   uves_msg_debug("start msrawxy");
00141 
00142   check (biny = uves_pfits_get_binx(raw_header),
00143      "Could not read x binning factor from input header");
00144 
00145   check (binx = uves_pfits_get_biny(raw_header), 
00146      "Could not read x binning factor from input header");
00147 
00148   check( uves_get_parameter(parameters, NULL, recipe_id, 
00149            "mbox_x", CPL_TYPE_INT, &MBOX_X )  , 
00150        "Could not read parameter");
00151 
00152   check( uves_get_parameter(parameters, NULL, recipe_id, 
00153            "mbox_y", CPL_TYPE_INT, &MBOX_Y )  , 
00154        "Could not read parameter");
00155 
00156   mbox_x=MBOX_X/binx;
00157   mbox_y=MBOX_Y/biny;
00158 
00159     /* IN MIDAS we always use the default offset=0,0
00160     check( uves_get_parameter(parameters, NULL, recipe_id, 
00161            "offset_x", CPL_TYPE_DOUBLE, &OFFSET_X )  , 
00162        "Could not read parameter");
00163 
00164     check( uves_get_parameter(parameters, NULL, recipe_id, 
00165            "offset_y", CPL_TYPE_DOUBLE, &OFFSET_Y )  ,
00166        "Could not read parameter");
00167 
00168     */
00169     /* msrawxy specific pars */
00170     /* Removed params x_axis_scale,y_axis_scale, always set to 0 */
00171     /*
00172     check( uves_get_parameter(parameters, NULL, recipe_id, 
00173            "x_axis_scale", CPL_TYPE_DOUBLE, &X_AXIS_SCALE )  , 
00174            "Could not read parameter");
00175 
00176     check( uves_get_parameter(parameters, NULL, recipe_id, 
00177            "y_axis_scale", CPL_TYPE_DOUBLE, &Y_AXIS_SCALE ),
00178        "Could not read parameter");
00179     */
00180 
00181     check(cpl_table_and_selected_double(mod_tbl,"IDENT",CPL_GREATER_THAN,0),
00182           "Error selecting IDENT");
00183   *l_tbl=cpl_table_extract_selected(mod_tbl);
00184 
00185   uves_msg_debug("xs=%f ofx=%f bx=%f",(1.+X_AXIS_SCALE),OFFSET_X,mbox_x/2);
00186   uves_msg_debug("ys=%f ofy=%f by=%f",(1.+Y_AXIS_SCALE),OFFSET_Y,mbox_y/2);
00187 
00188   scl_fct=(1.+X_AXIS_SCALE);
00189   cpl_table_erase_column(*l_tbl,"XSTART");
00190   cpl_table_duplicate_column(*l_tbl,"XSTART",*l_tbl,"XMOD");
00191   cpl_table_multiply_scalar(*l_tbl,"XSTART",scl_fct);
00192   add_fct=OFFSET_X-(double)mbox_x/2;
00193 
00194   uves_msg_debug("add_fct=%f",add_fct);
00195   cpl_table_add_scalar(*l_tbl,"XSTART",add_fct);
00196 
00197   cpl_table_erase_column(*l_tbl,"XEND");
00198   cpl_table_duplicate_column(*l_tbl,"XEND",*l_tbl,"XMOD");
00199   cpl_table_multiply_scalar(*l_tbl,"XEND",scl_fct);
00200   add_fct=OFFSET_X+(double)mbox_x/2;
00201   uves_msg_debug("add_fct=%f",add_fct);
00202   cpl_table_add_scalar(*l_tbl,"XEND",add_fct);
00203 
00204   scl_fct=(1.+Y_AXIS_SCALE);
00205   cpl_table_erase_column(*l_tbl,"YSTART");
00206   cpl_table_duplicate_column(*l_tbl,"YSTART",*l_tbl,"YMOD");
00207   cpl_table_multiply_scalar(*l_tbl,"YSTART",scl_fct);
00208   add_fct=OFFSET_Y-(double)mbox_y/2;
00209   uves_msg_debug("add_fct=%f",add_fct);
00210   cpl_table_add_scalar(*l_tbl,"YSTART",add_fct);
00211 
00212   cpl_table_erase_column(*l_tbl,"YEND");
00213   cpl_table_duplicate_column(*l_tbl,"YEND",*l_tbl,"YMOD");
00214   cpl_table_multiply_scalar(*l_tbl,"YEND",scl_fct);
00215   add_fct=OFFSET_Y+(double)mbox_y/2;
00216   uves_msg_debug("add_fct=%f",add_fct);
00217   cpl_table_add_scalar(*l_tbl,"YEND",add_fct);
00218  
00219 
00220  
00221   limit[2] = mbox_x/2;  /* limit(3) */
00222   check(tmp_tbl1=uves_extract_table_rows(*l_tbl,"XSTART",CPL_GREATER_THAN,
00223           limit[2]),"Error selecting XSTART");
00224 
00225   check_nomsg(limit[0] = cpl_image_get_size_x(raw_image)-mbox_x/2); /* limit(1) */
00226 
00227   check(tmp_tbl2=uves_extract_table_rows(tmp_tbl1,"XSTART",CPL_LESS_THAN,
00228           limit[0]),"Error selecting XSTART");
00229 
00230   limit[3] = mbox_y/2; /* limit(4) */
00231 
00232   check(tmp_tbl3=uves_extract_table_rows(tmp_tbl2,"YSTART",CPL_GREATER_THAN,
00233           limit[3]),"Error selecting YSTART");
00234 
00235   check_nomsg(limit[1] = cpl_image_get_size_y(raw_image)-mbox_y/2); /* limit(2) */
00236 
00237   check(tmp_tbl4=uves_extract_table_rows(tmp_tbl3,"YSTART",CPL_LESS_THAN,
00238           limit[1]),"Error selecting YSTART");
00239 
00240   uves_msg_debug("limits %f %f %f %f",
00241                limit[0],limit[1],limit[2],limit[3]);
00242   check_nomsg(*m_tbl     = cpl_table_extract_selected(tmp_tbl4));
00243   check_nomsg(*rline_tbl = cpl_table_extract_selected(tmp_tbl4));
00244 
00245   /* this is to finally get in l_tbl the result of the selection */
00246   check_nomsg(tmp_tbl=cpl_table_extract_selected(*l_tbl));
00247   uves_free_table(l_tbl);
00248   *l_tbl=cpl_table_duplicate(tmp_tbl);
00249   uves_free_table(&tmp_tbl);
00250 
00251 
00252   assure( !cpl_table_has_invalid(*m_tbl,"XMOD") &&
00253       !cpl_table_has_invalid(*m_tbl,"YMOD"),
00254       CPL_ERROR_ILLEGAL_INPUT, 
00255       "Invalid content in table m_tbl");
00256 
00257   uves_msg_debug("min=%f max=%f std=%f",
00258           cpl_image_get_min(raw_image),
00259           cpl_image_get_max(raw_image),
00260           cpl_image_get_stdev(raw_image));
00261 
00262   check_nomsg( uves_physmod_center_gauss(raw_image,m_tbl) );
00263   /*
00264   cpl_table_dump(*m_tbl,0,20,stdout);
00265   */
00266 
00267 
00268   check_nomsg(
00269       (cpl_table_duplicate_column(*rline_tbl,"XMES",*m_tbl,"XCEN"),
00270        cpl_table_duplicate_column(*rline_tbl,"YMES",*m_tbl,"YCEN"),
00271        cpl_table_duplicate_column(*rline_tbl,"STATUS",*m_tbl,"STATUS")));
00272 
00273   /* save measured positions without check for quality check */
00274   *mline_tbl=cpl_table_duplicate(*rline_tbl);
00275 
00276   uves_msg_debug("nraw=%" CPL_SIZE_FORMAT "",
00277      cpl_table_and_selected_int(*rline_tbl,"STATUS",CPL_EQUAL_TO,0.));
00278 
00279 
00280   check_nomsg( *p_tbl=cpl_table_extract_selected(*rline_tbl) );
00281   /* the following two lines to keep in *rline_tbl the result of selection */
00282   uves_free_table(rline_tbl);
00283   *rline_tbl=cpl_table_duplicate(*p_tbl);
00284 
00285   uves_msg_debug("middump nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*p_tbl));
00286 
00287   cpl_table_select_all(*rline_tbl);
00288   uves_free_table(rline_tbl);
00289   *rline_tbl=cpl_table_duplicate(*p_tbl);
00290   uves_msg_debug("rline nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00291 
00292 
00293   check_nomsg( (cpl_table_duplicate_column(*rline_tbl,"XDIF",*rline_tbl,"XMES"),
00294                 cpl_table_subtract_columns(*rline_tbl,"XDIF","XMOD")) );
00295   uves_msg_debug("rline1 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00296 
00297 
00298   cpl_table_duplicate_column(*rline_tbl,"YDIF",*rline_tbl,"YMES");
00299   cpl_table_subtract_columns(*rline_tbl,"YDIF","YMOD");
00300 
00301   uves_msg_debug("rline2 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00302   /* Fits the residual differences */
00303   /* Now we do the polynomial fit */
00304   check(poly2d_x=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","XDIF",
00305                          NULL,2,2,"XDIFREG",NULL,NULL,
00306                          &mean_err_xdif,NULL,NULL,-1,-1),
00307         "2D poly regression to fit XDIF failed. Try to increase mbox_x value, "
00308         "and possibly the mbox_y value");
00309 
00310   outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_x,0,0);
00311   outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_x,1,0);
00312   outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_x,0,1);
00313   outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_x,1,1);
00314   outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_x,2,0);
00315   outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_x,0,2);
00316   outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_x,1,2);
00317   outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_x,2,1);
00318   outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_x,2,2);
00319 
00320   check(poly2d_y=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","YDIF",
00321                          NULL,2,2,"YDIFREG",NULL,NULL,
00322                          &mean_err_ydif,NULL,NULL,-1,-1),
00323         "2D poly regression to fit YDIF failed. Try to increase mbox_y value");
00324 
00325 
00326   outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_y,0,0);
00327   outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_y,1,0);
00328   outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_y,0,1);
00329   outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_y,1,1);
00330   outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_y,2,0);
00331   outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_y,0,2);
00332   outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_y,1,2);
00333   outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_y,2,1);
00334   outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_y,2,2);
00335 
00336 
00337   uves_msg_debug("rline3 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00338    cpl_table_duplicate_column(*rline_tbl,"XMODREG",*rline_tbl,"XMOD");
00339    cpl_table_add_columns(*rline_tbl,"XMODREG","XDIFREG");
00340 
00341    cpl_table_duplicate_column(*rline_tbl,"YMODREG",*rline_tbl,"YMOD");
00342    cpl_table_add_columns(*rline_tbl,"YMODREG","YDIFREG");
00343 
00344   uves_msg_debug("rline4 nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*rline_tbl));
00345   /*
00346    cpl_table_dump(*rline_tbl,0,10,stdout);
00347   */
00348 
00349 
00350   /* was   */
00351   *npline_tbl=cpl_table_extract_selected(mod_tbl);
00352 
00353   /* we now unselect */
00354   cpl_table_select_all(mod_tbl); 
00355   /*
00356   *npline_tbl=cpl_table_extract_selected(mod_tbl);
00357   */
00358   uves_msg_debug("npline nraw=%" CPL_SIZE_FORMAT "",cpl_table_get_nrow(*npline_tbl));
00359   /* to prevent a problem of duplicating columns from tables 
00360      with different size we use a temporary table and extend 
00361      the size */ 
00362   tmp_tbl=cpl_table_new(cpl_table_get_nrow(*rline_tbl));
00363   cpl_table_duplicate_column(tmp_tbl,"XDIF",*rline_tbl,"XDIF");
00364   cpl_table_duplicate_column(tmp_tbl,"YDIF",*rline_tbl,"YDIF");
00365   
00366   cpl_table_set_size(tmp_tbl,cpl_table_get_nrow(*npline_tbl));
00367 
00368   cpl_table_duplicate_column(*npline_tbl,"XDIF",tmp_tbl,"XDIF");
00369   cpl_table_duplicate_column(*npline_tbl,"YDIF",tmp_tbl,"YDIF");
00370   uves_free_table(&tmp_tbl);
00371 
00372   cpl_table_new_column(*npline_tbl,"XDIFREG",CPL_TYPE_DOUBLE);
00373   cpl_table_new_column(*npline_tbl,"YDIFREG",CPL_TYPE_DOUBLE);
00374  
00375    for (k=0;k<cpl_table_get_nrow(*npline_tbl);k++) {
00376 
00377      tmp_x=cpl_table_get_double(*npline_tbl,"XMOD",k,&status);
00378      tmp_y=cpl_table_get_double(*npline_tbl,"YMOD",k,&status);
00379 
00380      tmp_val_x=uves_polynomial_evaluate_2d(poly2d_x,tmp_x,tmp_y);
00381      tmp_val_y=uves_polynomial_evaluate_2d(poly2d_y,tmp_x,tmp_y);
00382 
00383      cpl_table_set_double(*npline_tbl,"XDIFREG",k,tmp_val_x);
00384      cpl_table_set_double(*npline_tbl,"YDIFREG",k,tmp_val_y);
00385      
00386    }
00387 
00388    cpl_table_add_columns(*npline_tbl,"XMOD","XDIFREG");
00389    cpl_table_add_columns(*npline_tbl,"YMOD","YDIFREG");
00390 
00391 
00392    cleanup:
00393    uves_polynomial_delete(&poly2d_x);
00394    uves_polynomial_delete(&poly2d_y);
00395    uves_free_table(&tmp_tbl);
00396    uves_free_table(&tmp_tbl1);
00397    uves_free_table(&tmp_tbl2);
00398    uves_free_table(&tmp_tbl3);
00399    uves_free_table(&tmp_tbl4);
00400    
00401     uves_msg_debug("end msrawxy");
00402   return 0;
00403 }

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