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

Generated on Fri Apr 18 14:11:42 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1