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

Generated on Tue Jun 19 14:39:16 2007 for UVES Pipeline Reference Manual by  doxygen 1.4.6