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: 2007/06/25 16:34:29 $
00023  * $Revision: 1.15 $
00024  * $Name: uves-3_4_5 $
00025  * $Log: uves_physmod_msrawxy.c,v $
00026  * Revision 1.15  2007/06/25 16:34:29  amodigli
00027  * added more checks
00028  *
00029  * Revision 1.14  2007/06/06 08:17:33  amodigli
00030  * replace tab with 4 spaces
00031  *
00032  * Revision 1.13  2007/04/24 12:50:29  jmlarsen
00033  * Replaced cpl_propertylist -> uves_propertylist which is much faster
00034  *
00035  * Revision 1.12  2007/04/10 07:08:50  jmlarsen
00036  * Changed interface of polynomial_regression_2d()
00037  *
00038  * Revision 1.11  2007/03/19 13:48:39  jmlarsen
00039  * Added some error handling
00040  *
00041  * Revision 1.10  2007/02/22 15:52:01  jmlarsen
00042  * Added some error checking
00043  *
00044  * Revision 1.9  2006/11/06 15:19:41  jmlarsen
00045  * Removed unused include directives
00046  *
00047  * Revision 1.8  2006/10/24 14:12:16  jmlarsen
00048  * Parametrized recipe id to support FLAMES recipe
00049  *
00050  * Revision 1.7  2006/10/05 11:17:20  jmlarsen
00051  * Declared parameter list const
00052  *
00053  * Revision 1.6  2006/08/16 13:58:10  amodigli
00054  * fixed bug affecting ORDMAX value
00055  *
00056  * Revision 1.5  2006/08/07 11:34:08  jmlarsen
00057  * Fixed == comparison of floating point values
00058  *
00059  * Revision 1.4  2006/07/28 14:51:26  amodigli
00060  * fixed some bugs on improper table selection
00061  *
00062  * Revision 1.3  2006/07/14 12:54:45  jmlarsen
00063  * Doc. update
00064  *
00065  * Revision 1.2  2006/06/20 10:56:56  amodigli
00066  * cleaned output, added units
00067  *
00068  * Revision 1.1  2006/02/03 07:46:30  jmlarsen
00069  * Moved recipe implementations to ./uves directory
00070  *
00071  * Revision 1.12  2006/01/20 10:05:49  jmlarsen
00072  * Inserted missing doxygen end tag
00073  *
00074  * Revision 1.11  2006/01/16 13:52:58  jmlarsen
00075  * Removed memory leak
00076  *
00077  * Revision 1.10  2006/01/16 08:01:57  amodigli
00078  *
00079  * Added stability check
00080  *
00081  * Revision 1.9  2006/01/13 13:43:15  jmlarsen
00082  * Removed memory leak
00083  *
00084  * Revision 1.8  2006/01/13 09:54:42  amodigli
00085  * Fixed some bugs: improved agreement with MIDAS version
00086  *
00087  * Revision 1.7  2006/01/09 14:05:42  amodigli
00088  * Fixed doxigen warnings
00089  *
00090  * Revision 1.6  2006/01/05 14:29:59  jmlarsen
00091  * Removed newline characters from output strings
00092  *
00093  * Revision 1.5  2005/12/21 13:50:40  amodigli
00094  * Fixed bug (seg fault on HP-UX)
00095  *
00096  * Revision 1.4  2005/12/20 08:11:44  jmlarsen
00097  * Added CVS  entry
00098  *
00099  */
00100 
00101 /*----------------------------------------------------------------------------*/
00105 /*----------------------------------------------------------------------------*/
00108 #ifdef HAVE_CONFIG_H
00109 #  include <config.h>
00110 #endif
00111 /*-----------------------------------------------------------------------------
00112                                 Includes
00113  -----------------------------------------------------------------------------*/
00114 #include <uves_physmod_msrawxy.h>
00115 
00116 #include <uves_physmod_center_gauss.h>
00117 #include <uves_pfits.h>
00118 #include <uves_utils_polynomial.h>
00119 #include <uves_utils_wrappers.h>
00120 #include <uves_msg.h>
00121 #include <uves_error.h>
00122 
00123 /*-----------------------------------------------------------------------------
00124                             Functions code
00125  -----------------------------------------------------------------------------*/
00126 
00127 /*----------------------------------------------------------------------------*/
00157 /*----------------------------------------------------------------------------*/
00158 
00159 
00160 int uves_physmod_msrawxy(const cpl_image *raw_image, 
00161              const uves_propertylist *raw_header, 
00162              const char *recipe_id,
00163              const cpl_parameterlist* parameters, 
00164              cpl_table* mod_tbl,
00165              cpl_table** l_tbl,
00166              cpl_table** m_tbl,
00167              cpl_table** p_tbl,
00168              cpl_table** rline_tbl,
00169              cpl_table** mline_tbl,
00170              cpl_table** npline_tbl)
00171     
00172 {
00173   double scl_fct=0;
00174   double add_fct=0;
00175   double      X_AXIS_SCALE=0;    
00176   double      Y_AXIS_SCALE=0;
00177   double      OFFSET_X=0;    
00178   double      OFFSET_Y=0;
00179 
00180 
00181   int status=0;
00182 
00183 
00184   double limit[4]={0.,0.,0.,0.};
00185   int      MBOX_X=0;
00186   int      MBOX_Y=0;
00187  
00188   double tmp_x=0;
00189   double tmp_y=0;
00190   double tmp_val_x=0;
00191   double tmp_val_y=0;
00192 
00193 
00194 
00195   polynomial* poly2d_x=NULL;
00196   polynomial* poly2d_y=NULL;
00197   double mean_err_xdif=0;
00198   double mean_err_ydif=0;
00199  
00200   double binx=0;
00201   double biny=0;
00202 
00203   double mbox_x=0;
00204   double mbox_y=0;
00205   double outputd[3][3];
00206   int k=0;
00207 
00208   cpl_table * tmp_tbl=NULL;
00209   cpl_table * tmp_tbl1=NULL;
00210   cpl_table * tmp_tbl2=NULL;
00211   cpl_table * tmp_tbl3=NULL;
00212   cpl_table * tmp_tbl4=NULL;
00213  
00214   uves_msg_debug("start msrawxy");
00215 
00216   check (biny = uves_pfits_get_binx(raw_header),
00217      "Could not read x binning factor from input header");
00218 
00219   check (binx = uves_pfits_get_biny(raw_header), 
00220      "Could not read x binning factor from input header");
00221 
00222   check( uves_get_parameter(parameters, NULL, recipe_id, 
00223            "mbox_x", CPL_TYPE_INT, &MBOX_X )  , 
00224        "Could not read parameter");
00225 
00226   check( uves_get_parameter(parameters, NULL, recipe_id, 
00227            "mbox_y", CPL_TYPE_INT, &MBOX_Y )  , 
00228        "Could not read parameter");
00229 
00230   mbox_x=MBOX_X/binx;
00231   mbox_y=MBOX_Y/biny;
00232 
00233     /* IN MIDAS we always use the default offset=0,0
00234     check( uves_get_parameter(parameters, NULL, recipe_id, 
00235            "offset_x", CPL_TYPE_DOUBLE, &OFFSET_X )  , 
00236        "Could not read parameter");
00237 
00238     check( uves_get_parameter(parameters, NULL, recipe_id, 
00239            "offset_y", CPL_TYPE_DOUBLE, &OFFSET_Y )  ,
00240        "Could not read parameter");
00241 
00242     */
00243     /* msrawxy specific pars */
00244     check( uves_get_parameter(parameters, NULL, recipe_id, 
00245            "x_axis_scale", CPL_TYPE_DOUBLE, &X_AXIS_SCALE )  , 
00246            "Could not read parameter");
00247 
00248     check( uves_get_parameter(parameters, NULL, recipe_id, 
00249            "y_axis_scale", CPL_TYPE_DOUBLE, &Y_AXIS_SCALE ),
00250        "Could not read parameter");
00251 
00252     check(cpl_table_and_selected_double(mod_tbl,"IDENT",CPL_GREATER_THAN,0),
00253           "Error selecting IDENT");
00254   *l_tbl=cpl_table_extract_selected(mod_tbl);
00255 
00256   uves_msg_debug("xs=%f ofx=%f bx=%f",(1.+X_AXIS_SCALE),OFFSET_X,mbox_x/2);
00257   uves_msg_debug("ys=%f ofy=%f by=%f",(1.+Y_AXIS_SCALE),OFFSET_Y,mbox_y/2);
00258 
00259   scl_fct=(1.+X_AXIS_SCALE);
00260   cpl_table_erase_column(*l_tbl,"XSTART");
00261   cpl_table_duplicate_column(*l_tbl,"XSTART",*l_tbl,"XMOD");
00262   cpl_table_multiply_scalar(*l_tbl,"XSTART",scl_fct);
00263   add_fct=OFFSET_X-(double)mbox_x/2;
00264 
00265   uves_msg_debug("add_fct=%f",add_fct);
00266   cpl_table_add_scalar(*l_tbl,"XSTART",add_fct);
00267 
00268   cpl_table_erase_column(*l_tbl,"XEND");
00269   cpl_table_duplicate_column(*l_tbl,"XEND",*l_tbl,"XMOD");
00270   cpl_table_multiply_scalar(*l_tbl,"XEND",scl_fct);
00271   add_fct=OFFSET_X+(double)mbox_x/2;
00272   uves_msg_debug("add_fct=%f",add_fct);
00273   cpl_table_add_scalar(*l_tbl,"XEND",add_fct);
00274 
00275   scl_fct=(1.+Y_AXIS_SCALE);
00276   cpl_table_erase_column(*l_tbl,"YSTART");
00277   cpl_table_duplicate_column(*l_tbl,"YSTART",*l_tbl,"YMOD");
00278   cpl_table_multiply_scalar(*l_tbl,"YSTART",scl_fct);
00279   add_fct=OFFSET_Y-(double)mbox_y/2;
00280   uves_msg_debug("add_fct=%f",add_fct);
00281   cpl_table_add_scalar(*l_tbl,"YSTART",add_fct);
00282 
00283   cpl_table_erase_column(*l_tbl,"YEND");
00284   cpl_table_duplicate_column(*l_tbl,"YEND",*l_tbl,"YMOD");
00285   cpl_table_multiply_scalar(*l_tbl,"YEND",scl_fct);
00286   add_fct=OFFSET_Y+(double)mbox_y/2;
00287   uves_msg_debug("add_fct=%f",add_fct);
00288   cpl_table_add_scalar(*l_tbl,"YEND",add_fct);
00289  
00290 
00291  
00292   limit[2] = mbox_x/2;  /* limit(3) */
00293   check(tmp_tbl1=uves_extract_table_rows(*l_tbl,"XSTART",CPL_GREATER_THAN,
00294           limit[2]),"Error selecting XSTART");
00295 
00296   check_nomsg(limit[0] = cpl_image_get_size_x(raw_image)-mbox_x/2); /* limit(1) */
00297 
00298   check(tmp_tbl2=uves_extract_table_rows(tmp_tbl1,"XSTART",CPL_LESS_THAN,
00299           limit[0]),"Error selecting XSTART");
00300 
00301   limit[3] = mbox_y/2; /* limit(4) */
00302 
00303   check(tmp_tbl3=uves_extract_table_rows(tmp_tbl2,"YSTART",CPL_GREATER_THAN,
00304           limit[3]),"Error selecting YSTART");
00305 
00306   check_nomsg(limit[1] = cpl_image_get_size_y(raw_image)-mbox_y/2); /* limit(2) */
00307 
00308   check(tmp_tbl4=uves_extract_table_rows(tmp_tbl3,"YSTART",CPL_LESS_THAN,
00309           limit[1]),"Error selecting YSTART");
00310 
00311   uves_msg_debug("limits %f %f %f %f",
00312                limit[0],limit[1],limit[2],limit[3]);
00313   check_nomsg(*m_tbl     = cpl_table_extract_selected(tmp_tbl4));
00314   check_nomsg(*rline_tbl = cpl_table_extract_selected(tmp_tbl4));
00315 
00316   /* this is to finally get in l_tbl the result of the selection */
00317   check_nomsg(tmp_tbl=cpl_table_extract_selected(*l_tbl));
00318   uves_free_table(l_tbl);
00319   *l_tbl=cpl_table_duplicate(tmp_tbl);
00320   uves_free_table(&tmp_tbl);
00321 
00322 
00323   assure( !cpl_table_has_invalid(*m_tbl,"XMOD") &&
00324       !cpl_table_has_invalid(*m_tbl,"YMOD"),
00325       CPL_ERROR_ILLEGAL_INPUT, 
00326       "Invalid content in table m_tbl");
00327 
00328   uves_msg_debug("min=%f max=%f std=%f",
00329           cpl_image_get_min(raw_image),
00330           cpl_image_get_max(raw_image),
00331           cpl_image_get_stdev(raw_image));
00332 
00333   check_nomsg( uves_physmod_center_gauss(raw_image,m_tbl) );
00334   /*
00335   cpl_table_dump(*m_tbl,0,20,stdout);
00336   */
00337 
00338 
00339   check_nomsg(
00340       (cpl_table_duplicate_column(*rline_tbl,"XMES",*m_tbl,"XCEN"),
00341        cpl_table_duplicate_column(*rline_tbl,"YMES",*m_tbl,"YCEN"),
00342        cpl_table_duplicate_column(*rline_tbl,"STATUS",*m_tbl,"STATUS")));
00343 
00344   /* save measured positions without check for quality check */
00345   *mline_tbl=cpl_table_duplicate(*rline_tbl);
00346 
00347   uves_msg_debug("nraw=%d",
00348      cpl_table_and_selected_int(*rline_tbl,"STATUS",CPL_EQUAL_TO,0.));
00349 
00350 
00351   check_nomsg( *p_tbl=cpl_table_extract_selected(*rline_tbl) );
00352   /* the following two lines to keep in *rline_tbl the result of selection */
00353   uves_free_table(rline_tbl);
00354   *rline_tbl=cpl_table_duplicate(*p_tbl);
00355 
00356   uves_msg_debug("middump nraw=%d",cpl_table_get_nrow(*p_tbl));
00357 
00358   cpl_table_select_all(*rline_tbl);
00359   uves_free_table(rline_tbl);
00360   *rline_tbl=cpl_table_duplicate(*p_tbl);
00361   uves_msg_debug("rline nraw=%d",cpl_table_get_nrow(*rline_tbl));
00362 
00363 
00364   check_nomsg( (cpl_table_duplicate_column(*rline_tbl,"XDIF",*rline_tbl,"XMES"),
00365                 cpl_table_subtract_columns(*rline_tbl,"XDIF","XMOD")) );
00366   uves_msg_debug("rline1 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00367 
00368 
00369   cpl_table_duplicate_column(*rline_tbl,"YDIF",*rline_tbl,"YMES");
00370   cpl_table_subtract_columns(*rline_tbl,"YDIF","YMOD");
00371 
00372   uves_msg_debug("rline2 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00373   /* Fits the residual differences */
00374   /* Now we do the polynomial fit */
00375   check(poly2d_x=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","XDIF",
00376                          NULL,2,2,"XDIFREG",NULL,NULL,
00377                          &mean_err_xdif,NULL,NULL,-1,-1),
00378                                          "Fitting XDIF failed");
00379 
00380   outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_x,0,0);
00381   outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_x,1,0);
00382   outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_x,0,1);
00383   outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_x,1,1);
00384   outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_x,2,0);
00385   outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_x,0,2);
00386   outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_x,1,2);
00387   outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_x,2,1);
00388   outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_x,2,2);
00389 
00390   check(poly2d_y=uves_polynomial_regression_2d(*rline_tbl,"XMOD","YMOD","YDIF",
00391                          NULL,2,2,"YDIFREG",NULL,NULL,
00392                          &mean_err_ydif,NULL,NULL,-1,-1),
00393                                          "Fitting YDIF failed");
00394 
00395 
00396   outputd[0][0]=uves_polynomial_get_coeff_2d(poly2d_y,0,0);
00397   outputd[1][0]=uves_polynomial_get_coeff_2d(poly2d_y,1,0);
00398   outputd[0][1]=uves_polynomial_get_coeff_2d(poly2d_y,0,1);
00399   outputd[1][1]=uves_polynomial_get_coeff_2d(poly2d_y,1,1);
00400   outputd[2][0]=uves_polynomial_get_coeff_2d(poly2d_y,2,0);
00401   outputd[0][2]=uves_polynomial_get_coeff_2d(poly2d_y,0,2);
00402   outputd[1][2]=uves_polynomial_get_coeff_2d(poly2d_y,1,2);
00403   outputd[2][1]=uves_polynomial_get_coeff_2d(poly2d_y,2,1);
00404   outputd[2][2]=uves_polynomial_get_coeff_2d(poly2d_y,2,2);
00405 
00406 
00407   uves_msg_debug("rline3 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00408    cpl_table_duplicate_column(*rline_tbl,"XMODREG",*rline_tbl,"XMOD");
00409    cpl_table_add_columns(*rline_tbl,"XMODREG","XDIFREG");
00410 
00411    cpl_table_duplicate_column(*rline_tbl,"YMODREG",*rline_tbl,"YMOD");
00412    cpl_table_add_columns(*rline_tbl,"YMODREG","YDIFREG");
00413 
00414   uves_msg_debug("rline4 nraw=%d",cpl_table_get_nrow(*rline_tbl));
00415   /*
00416    cpl_table_dump(*rline_tbl,0,10,stdout);
00417   */
00418 
00419 
00420   /* was   */
00421   *npline_tbl=cpl_table_extract_selected(mod_tbl);
00422 
00423   /* we now unselect */
00424   cpl_table_select_all(mod_tbl); 
00425   /*
00426   *npline_tbl=cpl_table_extract_selected(mod_tbl);
00427   */
00428   uves_msg_debug("npline nraw=%d",cpl_table_get_nrow(*npline_tbl));
00429   /* to prevent a problem of duplicating columns from tables 
00430      with different size we use a temporary table and extend 
00431      the size */ 
00432   tmp_tbl=cpl_table_new(cpl_table_get_nrow(*rline_tbl));
00433   cpl_table_duplicate_column(tmp_tbl,"XDIF",*rline_tbl,"XDIF");
00434   cpl_table_duplicate_column(tmp_tbl,"YDIF",*rline_tbl,"YDIF");
00435   
00436   cpl_table_set_size(tmp_tbl,cpl_table_get_nrow(*npline_tbl));
00437 
00438   cpl_table_duplicate_column(*npline_tbl,"XDIF",tmp_tbl,"XDIF");
00439   cpl_table_duplicate_column(*npline_tbl,"YDIF",tmp_tbl,"YDIF");
00440   uves_free_table(&tmp_tbl);
00441 
00442   cpl_table_new_column(*npline_tbl,"XDIFREG",CPL_TYPE_DOUBLE);
00443   cpl_table_new_column(*npline_tbl,"YDIFREG",CPL_TYPE_DOUBLE);
00444  
00445    for (k=0;k<cpl_table_get_nrow(*npline_tbl);k++) {
00446 
00447      tmp_x=cpl_table_get_double(*npline_tbl,"XMOD",k,&status);
00448      tmp_y=cpl_table_get_double(*npline_tbl,"YMOD",k,&status);
00449 
00450      tmp_val_x=uves_polynomial_evaluate_2d(poly2d_x,tmp_x,tmp_y);
00451      tmp_val_y=uves_polynomial_evaluate_2d(poly2d_y,tmp_x,tmp_y);
00452 
00453      cpl_table_set_double(*npline_tbl,"XDIFREG",k,tmp_val_x);
00454      cpl_table_set_double(*npline_tbl,"YDIFREG",k,tmp_val_y);
00455      
00456    }
00457 
00458    cpl_table_add_columns(*npline_tbl,"XMOD","XDIFREG");
00459    cpl_table_add_columns(*npline_tbl,"YMOD","YDIFREG");
00460 
00461 
00462    cleanup:
00463    uves_polynomial_delete(&poly2d_x);
00464    uves_polynomial_delete(&poly2d_y);
00465    uves_free_table(&tmp_tbl);
00466    uves_free_table(&tmp_tbl1);
00467    uves_free_table(&tmp_tbl2);
00468    uves_free_table(&tmp_tbl3);
00469    uves_free_table(&tmp_tbl4);
00470    
00471     uves_msg_debug("end msrawxy");
00472   return 0;
00473 }

Generated on Thu Nov 15 14:32:29 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1