uves_physmod_center_gauss.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 /*
00022  * $Author: amodigli $
00023  * $Date: 2010/09/24 09:32:06 $
00024  * $Revision: 1.14 $
00025  * $Name: uves-5_0_0 $
00026  * $Log: uves_physmod_center_gauss.c,v $
00027  * Revision 1.14  2010/09/24 09:32:06  amodigli
00028  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
00029  *
00030  * Revision 1.12  2007/08/21 13:08:26  jmlarsen
00031  * Removed irplib_access module, largely deprecated by CPL-4
00032  *
00033  * Revision 1.11  2007/06/06 08:17:33  amodigli
00034  * replace tab with 4 spaces
00035  *
00036  * Revision 1.10  2007/03/19 13:48:54  jmlarsen
00037  * Added some error handling
00038  *
00039  * Revision 1.9  2006/11/15 15:02:14  jmlarsen
00040  * Implemented const safe workarounds for CPL functions
00041  *
00042  * Revision 1.7  2006/11/15 14:04:08  jmlarsen
00043  * Removed non-const version of parameterlist_get_first/last/next which is already in CPL, added const-safe wrapper, unwrapper and deallocator functions
00044  *
00045  * Revision 1.6  2006/11/06 15:19:41  jmlarsen
00046  * Removed unused include directives
00047  *
00048  * Revision 1.5  2006/08/23 15:41:06  amodigli
00049  * removed warning from checks on line length
00050  *
00051  * Revision 1.4  2006/08/07 11:30:55  jmlarsen
00052  * Added debugging statments
00053  *
00054  * Revision 1.3  2006/06/20 10:56:56  amodigli
00055  * cleaned output, added units
00056  *
00057  * Revision 1.2  2006/02/15 13:19:15  jmlarsen
00058  * Reduced source code max. line length
00059  *
00060  * Revision 1.1  2006/02/03 07:46:30  jmlarsen
00061  * Moved recipe implementations to ./uves directory
00062  *
00063  * Revision 1.8  2006/01/19 08:47:24  jmlarsen
00064  * Inserted missing doxygen end tag
00065  *
00066  * Revision 1.7  2006/01/13 13:43:15  jmlarsen
00067  * Removed memory leak
00068  *
00069  * Revision 1.6  2006/01/13 09:54:42  amodigli
00070  * Fixed some bugs: improved agreement with MIDAS version
00071  *
00072  * Revision 1.5  2006/01/05 14:29:59  jmlarsen
00073  * Removed newline characters from output strings
00074  *
00075  * Revision 1.4  2005/12/20 08:11:44  jmlarsen
00076  * Added CVS  entry
00077  *
00078  */
00079 
00080 /*----------------------------------------------------------------------------*/
00084 /*----------------------------------------------------------------------------*/
00089 #ifdef HAVE_CONFIG_H
00090 #  include <config.h>
00091 #endif
00092 
00093 /*-----------------------------------------------------------------------------
00094                                 Includes
00095  -----------------------------------------------------------------------------*/
00096 #include <uves_physmod_center_gauss.h>
00097 
00098 #include <uves_physmod_cstacen.h>
00099 #include <uves_utils_wrappers.h>
00100 #include <uves_error.h>
00101 #include <uves_msg.h>
00102 
00103 #include <cpl.h>
00104 
00105 /*-----------------------------------------------------------------------------
00106                                 Defines
00107  -----------------------------------------------------------------------------*/
00108 /*-----------------------------------------------------------------------------
00109                             Functions prototypes
00110  ----------------------------------------------------------------------------*/
00111 /*-----------------------------------------------------------------------------
00112                             Static variables
00113  -----------------------------------------------------------------------------*/
00114 
00115 /*-----------------------------------------------------------------------------
00116                             Functions code
00117  -----------------------------------------------------------------------------*/
00118 
00119 /*----------------------------------------------------------------------------*/
00138 /*----------------------------------------------------------------------------*/
00139 
00140 int
00141 uves_physmod_center_gauss(const cpl_image * raw_image,cpl_table** m_tbl)
00142 {
00143 
00144   /* Derived from MIDAS center.for, only in case input is ima,table and
00145      method is Gauss
00146   */
00147   int nraw=0;
00148   int i=0;
00149 
00150   double px_start=0;
00151   double py_start=0;
00152   double px_end=0;
00153   double py_end=0;
00154 
00155   int img_llx=0;
00156   int img_lly=0;
00157   int img_urx=0;
00158   int img_ury=0;
00159 
00160   int img_sx=0;
00161   int img_sy=0;
00162 
00163 
00164   double tmp_val=0;
00165 
00166   double* x_mod=NULL;
00167   double* y_mod=NULL;
00168   int status=0;
00169   double* x_cen=NULL;
00170   double* y_cen=NULL;
00171 
00172   double* x_err=NULL;
00173   double* y_err=NULL;
00174 
00175   double* x_start=NULL;
00176   double* y_start=NULL;
00177   double* x_end=NULL;
00178   double* y_end=NULL;
00179   double* icent=0;
00180 
00181 
00182   double* x_sig=NULL;
00183   double* y_sig=NULL;
00184   double* x_fwhm=NULL;
00185   double* y_fwhm=NULL;
00186 
00187   int img_ofx=0;
00188   int img_ofy=0;
00189   int img_buf1=0;
00190   int img_buf2=0;
00191   int img_buf3=0;
00192   
00193   int img_szx=0;
00194   int img_szy=0;
00195 
00196   float xout=0;
00197   float yout=0;
00198   float xerr=0;
00199   float yerr=0;
00200   float xsig=0;
00201   float ysig=0;
00202   float amp=0;
00203   double xfwhm=0;
00204   double yfwhm=0;
00205   double ang=0.;
00206   double ang_sig=0.;
00207   double rv=0.;
00208   int kstat=0;
00209   int cpix[5]={0,0,0,0,0};
00210   double STEP[3]={1.0,1.0,1.0};
00211   int NPIX=0;
00212 
00213   float* sima;
00214   cpl_image* img_sub=NULL;
00215   cpl_image* img_cst=NULL;
00216 
00217   int ok=0;
00218   int nok=0;
00219   int nf=0;
00220 
00221 
00222 
00223   check(nraw = cpl_table_get_nrow(*m_tbl), "Error getting nraw");
00224 
00225   cpl_table_new_column(*m_tbl,"XCEN",CPL_TYPE_DOUBLE);
00226   cpl_table_new_column(*m_tbl,"YCEN",CPL_TYPE_DOUBLE);
00227   cpl_table_new_column(*m_tbl,"ICENT",CPL_TYPE_DOUBLE);
00228   cpl_table_new_column(*m_tbl,"XSIG",CPL_TYPE_DOUBLE);
00229   cpl_table_new_column(*m_tbl,"YSIG",CPL_TYPE_DOUBLE);
00230   cpl_table_new_column(*m_tbl,"XFWHM",CPL_TYPE_DOUBLE);
00231   cpl_table_new_column(*m_tbl,"YFWHM",CPL_TYPE_DOUBLE);
00232   cpl_table_new_column(*m_tbl,"XERR",CPL_TYPE_DOUBLE);
00233   cpl_table_new_column(*m_tbl,"YERR",CPL_TYPE_DOUBLE);
00234 
00235 
00236   icent = cpl_table_get_data_double(*m_tbl,"ICENT");
00237 
00238   x_mod = cpl_table_get_data_double(*m_tbl,"XMOD");
00239   y_mod = cpl_table_get_data_double(*m_tbl,"YMOD");
00240   x_err = cpl_table_get_data_double(*m_tbl,"XERR");
00241   y_err = cpl_table_get_data_double(*m_tbl,"YERR");
00242 
00243   x_start = cpl_table_get_data_double(*m_tbl,"XSTART");
00244   y_start = cpl_table_get_data_double(*m_tbl,"YSTART");
00245   x_end   = cpl_table_get_data_double(*m_tbl,"XEND");
00246   y_end   = cpl_table_get_data_double(*m_tbl,"YEND");
00247 
00248   x_cen = cpl_table_get_data_double(*m_tbl,"XCEN");
00249   y_cen = cpl_table_get_data_double(*m_tbl,"YCEN");
00250   x_sig = cpl_table_get_data_double(*m_tbl,"XSIG");
00251   y_sig = cpl_table_get_data_double(*m_tbl,"YSIG");
00252   x_fwhm = cpl_table_get_data_double(*m_tbl,"XFWHM");
00253   y_fwhm = cpl_table_get_data_double(*m_tbl,"YFWHM");
00254   
00255 
00256   cpl_table_new_column(*m_tbl,"STATUS",CPL_TYPE_INT);
00257   check_nomsg( cpl_table_set_column_invalid(*m_tbl,"STATUS",0,
00258                                             cpl_table_get_nrow(*m_tbl)));
00259 
00260   cpl_table_set_column_invalid(*m_tbl,"ICENT",0,
00261                             cpl_table_get_nrow(*m_tbl));
00262 
00263   cpl_table_set_column_invalid(*m_tbl,"XCEN",0,
00264                             cpl_table_get_nrow(*m_tbl));
00265   cpl_table_set_column_invalid(*m_tbl,"YCEN",0,
00266                             cpl_table_get_nrow(*m_tbl));
00267 
00268   cpl_table_set_column_invalid(*m_tbl,"XSIG",0,
00269                             cpl_table_get_nrow(*m_tbl));
00270   cpl_table_set_column_invalid(*m_tbl,"YSIG",0,
00271                             cpl_table_get_nrow(*m_tbl));
00272 
00273   cpl_table_set_column_invalid(*m_tbl,"XFWHM",0,
00274                             cpl_table_get_nrow(*m_tbl));
00275   cpl_table_set_column_invalid(*m_tbl,"YFWHM",0,
00276                             cpl_table_get_nrow(*m_tbl));
00277 
00278 
00279   /* We loop over each table raw and get box edges */
00280   /* one should skip selected raws */
00281   nf=0;
00282   for(i=0;i<nraw;i++) { 
00283     /* get box edges */
00284     px_start=cpl_table_get_double(*m_tbl,"XSTART",i,&status);
00285     py_start=cpl_table_get_double(*m_tbl,"YSTART",i,&status);
00286     px_end=cpl_table_get_double(*m_tbl,"XEND",i,&status);
00287     py_end=cpl_table_get_double(*m_tbl,"YEND",i,&status);
00288 
00289 
00290     img_llx=floor(px_start+0.5);
00291     img_lly=floor(py_start+0.5);
00292     img_urx=floor(px_end+0.5);
00293     img_ury=floor(py_end+0.5);
00294 
00295     img_szx=cpl_image_get_size_x(raw_image);
00296     img_szy=cpl_image_get_size_y(raw_image);
00297 
00298     /*
00299     if(i==167 || i==485) {
00300       uves_msg(">>>>>>>>>>>>>>>>i=%d",i);
00301     }
00302     */
00303     if(img_llx < 1 || img_urx > img_szx || 
00304        img_lly < 1 || img_ury > img_szy) {
00305       /* 
00306      uves_msg("SSSSSSSSSSSSSSSSSSi=%d",i);
00307       */
00308       continue;
00309 
00310       }
00311     /*
00312     img_llx=( img_llx >= 1 ) ? img_llx : 1;
00313     img_lly=( img_lly >= 1 ) ? img_lly : 1;
00314     img_urx=( img_urx <= img_szx ) ? img_urx : img_szx;
00315     img_ury=( img_ury <= img_szy ) ? img_ury : img_szy;
00316     */
00317 
00318     img_sx=img_urx-img_llx+1;
00319     img_sy=img_ury-img_lly+1;
00320     img_ofx=img_llx-1;
00321     img_ofy=img_lly-1;
00322     /* we get NPIX */
00323     img_buf1=NPIX;
00324     img_buf2=img_sy;
00325     img_buf3=1;
00326 
00327 
00328     /* initialize centering algorithm */
00329     cpix[0]=1;
00330     cpix[1]=img_sx;
00331     cpix[2]=1;
00332     cpix[3]=img_sy;
00333 
00334 
00335     uves_msg_debug("Box %d %d %d %d %d",nf,img_llx,img_urx,img_lly,img_ury);
00336     /*
00337     check(img_sub =cpl_image_extract(raw_image,img_llx,img_lly,img_urx,img_ury);
00338       img_cst = cpl_image_cast(img_sub,CPL_TYPE_FLOAT),
00339       sima = cpl_image_get_data_float(img_cst),
00340     "error img_sub");
00341     */
00342     uves_free_image(&img_sub);
00343     img_sub =cpl_image_extract(raw_image,img_llx,img_lly,img_urx,img_ury);
00344     uves_free_image(&img_cst);
00345     img_cst = cpl_image_cast(img_sub,CPL_TYPE_FLOAT);
00346     sima = cpl_image_get_data_float(img_cst);
00347 
00348       /*
00349  for(i=0;kk<img_sx*img_sy;kk++) {
00350     uves_msg("Image[%d]=%f",kk,sima[kk]);
00351  }
00352       */
00353     
00354     uves_msg_debug("stacen nf=%d cpix=%d %d %d %d",nf,cpix[0],cpix[1],cpix[2],cpix[3]);
00355     uves_physmod_stacen(sima,img_sx,img_sy,'G',cpix,
00356             &xout,&yout,&xerr,&yerr,&xsig,&ysig,&amp,&kstat);
00357     
00358     uves_msg_debug("nf=%d,xout=%f,yout=%f,xerr=%f,yerr=%f",nf,xout,yout,xerr,yerr);
00359     uves_msg_debug("xsig=%f,ysig=%f,xfwhm=%f,yfwhm=%f",xsig,ysig,xfwhm,yfwhm);
00360 
00361     uves_msg_debug("amp=%f,kstat=%d",amp,kstat);
00362     
00363     rv=0.;
00364     ang=0.;
00365     ang_sig=0.;
00366 
00367     
00368     if (i<0) {
00369     uves_msg("Min=%f,Max=%f",cpl_image_get_min(img_sub),
00370                              cpl_image_get_max(img_sub));
00371     uves_msg("llx=%d,lly=%d,urx=%d,ury=%d",img_llx,img_lly,img_urx,img_ury);
00372     uves_msg("sx=%d,sy=%d,ofx=%d,ofy=%d",img_sx,img_sy,img_ofx,img_ofy);
00373     uves_msg("img_sx=%d,img_sy=%d,cpix[0]=%d,cpix[1]=%d,cpix[2]=%d,cpix[3]=%d",
00374            img_sx,img_sy,cpix[0],cpix[1],cpix[2],cpix[3]);
00375 
00376     uves_msg("i=%d,xout=%f,yout=%f,xerr=%f,yerr=%f",nf,xout,yout,xerr,yerr);
00377     uves_msg("xsig=%f,ysig=%f,xfwhm=%f,yfwhm=%f,amp=%f,kstat=%d",xsig,ysig,xfwhm,yfwhm,amp,kstat);
00378    
00379     }
00380     
00381     /* check status */
00382     if(kstat == 0) {
00383 
00384       xerr=xerr*fabs(STEP[1]);
00385       xsig=xsig*fabs(STEP[1]);
00386       xfwhm=xsig*TWOSQRT2LN2;  /* MIDAS: 2.35482 */
00387 
00388       yerr=yerr*fabs(STEP[2]);
00389       ysig=ysig*fabs(STEP[2]);
00390       yfwhm=ysig*TWOSQRT2LN2;  /* MIDAS: 2.35482 */
00391       ok++;
00392       tmp_val=cpl_table_get_double(*m_tbl,"IDENT",i,&status);
00393       tmp_val-=1;
00394       tmp_val/=1000.;
00395 
00396     } else {
00397 
00398       xerr=0.;
00399       xsig=0.;
00400       xfwhm=0.;
00401 
00402       yerr=0.;
00403       ysig=0.;
00404       yfwhm=0.;
00405       nok++;
00406       /* we assume that not using IQUE function kstatus >=0 */
00407     }
00408 
00409 
00410     uves_msg_debug("nf=%d %f %f %f %f %f %d",nf,xout,yout,xsig,ysig,amp,kstat);
00411     
00412     /* we write results in table */
00413 
00414 
00415     /* we check if FWHM makes sense */
00416     if( (xfwhm > img_sx *fabs(STEP[1])) || (yfwhm > img_sy *fabs(STEP[2])) ) {
00417       kstat = 4;
00418     }
00419     if ( (xfwhm < 0.0) || (yfwhm < 0.0) ) {
00420       kstat = 5;
00421     }
00422 
00423 
00424    
00425     /* to debug: 0-->NN>0 */
00426     if (i<0) {
00427     uves_msg("Min=%f,Max=%f",cpl_image_get_min(img_sub),
00428                              cpl_image_get_max(img_sub));
00429     uves_msg("llx=%d,lly=%d,urx=%d,ury=%d",img_llx,img_lly,img_urx,img_ury);
00430     uves_msg("sx=%d,sy=%d,ofx=%d,ofy=%d",img_sx,img_sy,img_ofx,img_ofy);
00431     uves_msg("img_sx=%d,img_sy=%d,cpix[0]=%d,cpix[1]=%d,cpix[2]=%d,cpix[3]=%d",
00432            img_sx,img_sy,cpix[0],cpix[1],cpix[2],cpix[3]);
00433 
00434     uves_msg("i=%d,xout=%f,yout=%f,xerr=%f,yerr=%f",i,xout,yout,xerr,yerr);
00435     uves_msg("xsig=%f,ysig=%f,xfwhm=%f,yfwhm=%f,amp=%f,kstat=%d",xsig,ysig,xfwhm,yfwhm,amp,kstat);
00436    
00437     }
00438     
00439 
00440      cpl_table_set_double(*m_tbl,"IDENT",i,tmp_val);
00441      cpl_table_set_double(*m_tbl,"ICENT",i,(double)amp);
00442      cpl_table_set_double(*m_tbl,"XERR",i,(double)xerr);
00443      cpl_table_set_double(*m_tbl,"YERR",i,(double)yerr);
00444 
00445      cpl_table_set_double(*m_tbl,"XCEN",i,(double)(xout+img_ofx));
00446      cpl_table_set_double(*m_tbl,"YCEN",i,(double)(yout+img_ofy));
00447      cpl_table_set_double(*m_tbl,"XSIG",i,(double)xsig);
00448      cpl_table_set_double(*m_tbl,"YSIG",i,(double)ysig);
00449      cpl_table_set_double(*m_tbl,"XFWHM",i,(double)xfwhm);
00450      cpl_table_set_double(*m_tbl,"YFWHM",i,(double)yfwhm);
00451      cpl_table_set_int   (*m_tbl,"STATUS",i,kstat);
00452      nf=nf+1;
00453 
00454   }
00455 
00456   if (false)   /* Debugging... */
00457       {
00458       cpl_table *dum = cpl_table_new(cpl_table_get_nrow(*m_tbl));
00459       
00460       cpl_table_duplicate_column(dum,"STAT",*m_tbl,"STATUS");
00461       
00462       uves_msg_warning("xcen = %f", cpl_table_get_column_mean(*m_tbl, "XCEN"));
00463       uves_msg_warning("ycen = %f", cpl_table_get_column_mean(*m_tbl, "YCEN"));
00464       uves_msg_warning("xerr = %f", cpl_table_get_column_mean(*m_tbl, "XERR"));
00465       uves_msg_warning("yerr = %f", cpl_table_get_column_mean(*m_tbl, "YERR"));
00466       uves_msg_warning("xsig = %f", cpl_table_get_column_mean(*m_tbl, "XSIG"));
00467       uves_msg_warning("ysig = %f", cpl_table_get_column_mean(*m_tbl, "YSIG"));
00468       uves_msg_warning("xfwhm = %f", cpl_table_get_column_mean(*m_tbl, "XFWHM"));
00469       uves_msg_warning("yfwhm = %f", cpl_table_get_column_mean(*m_tbl, "YFWHM"));
00470       uves_msg_warning("icent = %f", cpl_table_get_column_mean(*m_tbl, "ICENT"));
00471       uves_msg_warning("status = %f", cpl_table_get_column_mean(*m_tbl, "STATUS"));
00472       
00473 //    cpl_table_dump(dum, 0, cpl_table_get_nrow(*m_tbl), stdout);
00474       }
00475 
00476   uves_msg_debug("nok=%d ok=%d",nok,ok);
00477 
00478   cleanup:
00479   uves_free_image(&img_sub);
00480   uves_free_image(&img_cst);
00481 
00482   return 0;
00483 }

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