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

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