UVES Pipeline Reference Manual  5.4.6
uves_physmod_center_gauss.c
1 /* *
2  * This file is part of the ESO UVES Pipeline *
3  * Copyright (C) 2004,2005 European Southern Observatory *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18  * */
19 
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2010-09-24 09:32:06 $
24  * $Revision: 1.14 $
25  * $Name: not supported by cvs2svn $
26  * $Log: not supported by cvs2svn $
27  * Revision 1.12 2007/08/21 13:08:26 jmlarsen
28  * Removed irplib_access module, largely deprecated by CPL-4
29  *
30  * Revision 1.11 2007/06/06 08:17:33 amodigli
31  * replace tab with 4 spaces
32  *
33  * Revision 1.10 2007/03/19 13:48:54 jmlarsen
34  * Added some error handling
35  *
36  * Revision 1.9 2006/11/15 15:02:14 jmlarsen
37  * Implemented const safe workarounds for CPL functions
38  *
39  * Revision 1.7 2006/11/15 14:04:08 jmlarsen
40  * Removed non-const version of parameterlist_get_first/last/next which is already in CPL, added const-safe wrapper, unwrapper and deallocator functions
41  *
42  * Revision 1.6 2006/11/06 15:19:41 jmlarsen
43  * Removed unused include directives
44  *
45  * Revision 1.5 2006/08/23 15:41:06 amodigli
46  * removed warning from checks on line length
47  *
48  * Revision 1.4 2006/08/07 11:30:55 jmlarsen
49  * Added debugging statments
50  *
51  * Revision 1.3 2006/06/20 10:56:56 amodigli
52  * cleaned output, added units
53  *
54  * Revision 1.2 2006/02/15 13:19:15 jmlarsen
55  * Reduced source code max. line length
56  *
57  * Revision 1.1 2006/02/03 07:46:30 jmlarsen
58  * Moved recipe implementations to ./uves directory
59  *
60  * Revision 1.8 2006/01/19 08:47:24 jmlarsen
61  * Inserted missing doxygen end tag
62  *
63  * Revision 1.7 2006/01/13 13:43:15 jmlarsen
64  * Removed memory leak
65  *
66  * Revision 1.6 2006/01/13 09:54:42 amodigli
67  * Fixed some bugs: improved agreement with MIDAS version
68  *
69  * Revision 1.5 2006/01/05 14:29:59 jmlarsen
70  * Removed newline characters from output strings
71  *
72  * Revision 1.4 2005/12/20 08:11:44 jmlarsen
73  * Added CVS entry
74  *
75  */
76 
77 /*----------------------------------------------------------------------------*/
81 /*----------------------------------------------------------------------------*/
86 #ifdef HAVE_CONFIG_H
87 # include <config.h>
88 #endif
89 
90 /*-----------------------------------------------------------------------------
91  Includes
92  -----------------------------------------------------------------------------*/
93 #include <uves_physmod_center_gauss.h>
94 
95 #include <uves_physmod_cstacen.h>
96 #include <uves_utils_wrappers.h>
97 #include <uves_error.h>
98 #include <uves_msg.h>
99 
100 #include <cpl.h>
101 
102 /*-----------------------------------------------------------------------------
103  Defines
104  -----------------------------------------------------------------------------*/
105 /*-----------------------------------------------------------------------------
106  Functions prototypes
107  ----------------------------------------------------------------------------*/
108 /*-----------------------------------------------------------------------------
109  Static variables
110  -----------------------------------------------------------------------------*/
111 
112 /*-----------------------------------------------------------------------------
113  Functions code
114  -----------------------------------------------------------------------------*/
115 
116 /*----------------------------------------------------------------------------*/
135 /*----------------------------------------------------------------------------*/
136 
137 int
138 uves_physmod_center_gauss(const cpl_image * raw_image,cpl_table** m_tbl)
139 {
140 
141  /* Derived from MIDAS center.for, only in case input is ima,table and
142  method is Gauss
143  */
144  int nraw=0;
145  int i=0;
146 
147  double px_start=0;
148  double py_start=0;
149  double px_end=0;
150  double py_end=0;
151 
152  int img_llx=0;
153  int img_lly=0;
154  int img_urx=0;
155  int img_ury=0;
156 
157  int img_sx=0;
158  int img_sy=0;
159 
160 
161  double tmp_val=0;
162 
163  double* x_mod=NULL;
164  double* y_mod=NULL;
165  int status=0;
166  double* x_cen=NULL;
167  double* y_cen=NULL;
168 
169  double* x_err=NULL;
170  double* y_err=NULL;
171 
172  double* x_start=NULL;
173  double* y_start=NULL;
174  double* x_end=NULL;
175  double* y_end=NULL;
176  double* icent=0;
177 
178 
179  double* x_sig=NULL;
180  double* y_sig=NULL;
181  double* x_fwhm=NULL;
182  double* y_fwhm=NULL;
183 
184  int img_ofx=0;
185  int img_ofy=0;
186  int img_buf1=0;
187  int img_buf2=0;
188  int img_buf3=0;
189 
190  int img_szx=0;
191  int img_szy=0;
192 
193  float xout=0;
194  float yout=0;
195  float xerr=0;
196  float yerr=0;
197  float xsig=0;
198  float ysig=0;
199  float amp=0;
200  double xfwhm=0;
201  double yfwhm=0;
202  double ang=0.;
203  double ang_sig=0.;
204  double rv=0.;
205  int kstat=0;
206  int cpix[5]={0,0,0,0,0};
207  double STEP[3]={1.0,1.0,1.0};
208  int NPIX=0;
209 
210  float* sima;
211  cpl_image* img_sub=NULL;
212  cpl_image* img_cst=NULL;
213 
214  int ok=0;
215  int nok=0;
216  int nf=0;
217 
218 
219 
220  check(nraw = cpl_table_get_nrow(*m_tbl), "Error getting nraw");
221 
222  cpl_table_new_column(*m_tbl,"XCEN",CPL_TYPE_DOUBLE);
223  cpl_table_new_column(*m_tbl,"YCEN",CPL_TYPE_DOUBLE);
224  cpl_table_new_column(*m_tbl,"ICENT",CPL_TYPE_DOUBLE);
225  cpl_table_new_column(*m_tbl,"XSIG",CPL_TYPE_DOUBLE);
226  cpl_table_new_column(*m_tbl,"YSIG",CPL_TYPE_DOUBLE);
227  cpl_table_new_column(*m_tbl,"XFWHM",CPL_TYPE_DOUBLE);
228  cpl_table_new_column(*m_tbl,"YFWHM",CPL_TYPE_DOUBLE);
229  cpl_table_new_column(*m_tbl,"XERR",CPL_TYPE_DOUBLE);
230  cpl_table_new_column(*m_tbl,"YERR",CPL_TYPE_DOUBLE);
231 
232 
233  icent = cpl_table_get_data_double(*m_tbl,"ICENT");
234 
235  x_mod = cpl_table_get_data_double(*m_tbl,"XMOD");
236  y_mod = cpl_table_get_data_double(*m_tbl,"YMOD");
237  x_err = cpl_table_get_data_double(*m_tbl,"XERR");
238  y_err = cpl_table_get_data_double(*m_tbl,"YERR");
239 
240  x_start = cpl_table_get_data_double(*m_tbl,"XSTART");
241  y_start = cpl_table_get_data_double(*m_tbl,"YSTART");
242  x_end = cpl_table_get_data_double(*m_tbl,"XEND");
243  y_end = cpl_table_get_data_double(*m_tbl,"YEND");
244 
245  x_cen = cpl_table_get_data_double(*m_tbl,"XCEN");
246  y_cen = cpl_table_get_data_double(*m_tbl,"YCEN");
247  x_sig = cpl_table_get_data_double(*m_tbl,"XSIG");
248  y_sig = cpl_table_get_data_double(*m_tbl,"YSIG");
249  x_fwhm = cpl_table_get_data_double(*m_tbl,"XFWHM");
250  y_fwhm = cpl_table_get_data_double(*m_tbl,"YFWHM");
251 
252 
253  cpl_table_new_column(*m_tbl,"STATUS",CPL_TYPE_INT);
254  check_nomsg( cpl_table_set_column_invalid(*m_tbl,"STATUS",0,
255  cpl_table_get_nrow(*m_tbl)));
256 
257  cpl_table_set_column_invalid(*m_tbl,"ICENT",0,
258  cpl_table_get_nrow(*m_tbl));
259 
260  cpl_table_set_column_invalid(*m_tbl,"XCEN",0,
261  cpl_table_get_nrow(*m_tbl));
262  cpl_table_set_column_invalid(*m_tbl,"YCEN",0,
263  cpl_table_get_nrow(*m_tbl));
264 
265  cpl_table_set_column_invalid(*m_tbl,"XSIG",0,
266  cpl_table_get_nrow(*m_tbl));
267  cpl_table_set_column_invalid(*m_tbl,"YSIG",0,
268  cpl_table_get_nrow(*m_tbl));
269 
270  cpl_table_set_column_invalid(*m_tbl,"XFWHM",0,
271  cpl_table_get_nrow(*m_tbl));
272  cpl_table_set_column_invalid(*m_tbl,"YFWHM",0,
273  cpl_table_get_nrow(*m_tbl));
274 
275 
276  /* We loop over each table raw and get box edges */
277  /* one should skip selected raws */
278  nf=0;
279  for(i=0;i<nraw;i++) {
280  /* get box edges */
281  px_start=cpl_table_get_double(*m_tbl,"XSTART",i,&status);
282  py_start=cpl_table_get_double(*m_tbl,"YSTART",i,&status);
283  px_end=cpl_table_get_double(*m_tbl,"XEND",i,&status);
284  py_end=cpl_table_get_double(*m_tbl,"YEND",i,&status);
285 
286 
287  img_llx=floor(px_start+0.5);
288  img_lly=floor(py_start+0.5);
289  img_urx=floor(px_end+0.5);
290  img_ury=floor(py_end+0.5);
291 
292  img_szx=cpl_image_get_size_x(raw_image);
293  img_szy=cpl_image_get_size_y(raw_image);
294 
295  /*
296  if(i==167 || i==485) {
297  uves_msg(">>>>>>>>>>>>>>>>i=%d",i);
298  }
299  */
300  if(img_llx < 1 || img_urx > img_szx ||
301  img_lly < 1 || img_ury > img_szy) {
302  /*
303  uves_msg("SSSSSSSSSSSSSSSSSSi=%d",i);
304  */
305  continue;
306 
307  }
308  /*
309  img_llx=( img_llx >= 1 ) ? img_llx : 1;
310  img_lly=( img_lly >= 1 ) ? img_lly : 1;
311  img_urx=( img_urx <= img_szx ) ? img_urx : img_szx;
312  img_ury=( img_ury <= img_szy ) ? img_ury : img_szy;
313  */
314 
315  img_sx=img_urx-img_llx+1;
316  img_sy=img_ury-img_lly+1;
317  img_ofx=img_llx-1;
318  img_ofy=img_lly-1;
319  /* we get NPIX */
320  img_buf1=NPIX;
321  img_buf2=img_sy;
322  img_buf3=1;
323 
324 
325  /* initialize centering algorithm */
326  cpix[0]=1;
327  cpix[1]=img_sx;
328  cpix[2]=1;
329  cpix[3]=img_sy;
330 
331 
332  uves_msg_debug("Box %d %d %d %d %d",nf,img_llx,img_urx,img_lly,img_ury);
333  /*
334  check(img_sub =cpl_image_extract(raw_image,img_llx,img_lly,img_urx,img_ury);
335  img_cst = cpl_image_cast(img_sub,CPL_TYPE_FLOAT),
336  sima = cpl_image_get_data_float(img_cst),
337  "error img_sub");
338  */
339  uves_free_image(&img_sub);
340  img_sub =cpl_image_extract(raw_image,img_llx,img_lly,img_urx,img_ury);
341  uves_free_image(&img_cst);
342  img_cst = cpl_image_cast(img_sub,CPL_TYPE_FLOAT);
343  sima = cpl_image_get_data_float(img_cst);
344 
345  /*
346  for(i=0;kk<img_sx*img_sy;kk++) {
347  uves_msg("Image[%d]=%f",kk,sima[kk]);
348  }
349  */
350 
351  uves_msg_debug("stacen nf=%d cpix=%d %d %d %d",nf,cpix[0],cpix[1],cpix[2],cpix[3]);
352  uves_physmod_stacen(sima,img_sx,img_sy,'G',cpix,
353  &xout,&yout,&xerr,&yerr,&xsig,&ysig,&amp,&kstat);
354 
355  uves_msg_debug("nf=%d,xout=%f,yout=%f,xerr=%f,yerr=%f",nf,xout,yout,xerr,yerr);
356  uves_msg_debug("xsig=%f,ysig=%f,xfwhm=%f,yfwhm=%f",xsig,ysig,xfwhm,yfwhm);
357 
358  uves_msg_debug("amp=%f,kstat=%d",amp,kstat);
359 
360  rv=0.;
361  ang=0.;
362  ang_sig=0.;
363 
364 
365  if (i<0) {
366  uves_msg("Min=%f,Max=%f",cpl_image_get_min(img_sub),
367  cpl_image_get_max(img_sub));
368  uves_msg("llx=%d,lly=%d,urx=%d,ury=%d",img_llx,img_lly,img_urx,img_ury);
369  uves_msg("sx=%d,sy=%d,ofx=%d,ofy=%d",img_sx,img_sy,img_ofx,img_ofy);
370  uves_msg("img_sx=%d,img_sy=%d,cpix[0]=%d,cpix[1]=%d,cpix[2]=%d,cpix[3]=%d",
371  img_sx,img_sy,cpix[0],cpix[1],cpix[2],cpix[3]);
372 
373  uves_msg("i=%d,xout=%f,yout=%f,xerr=%f,yerr=%f",nf,xout,yout,xerr,yerr);
374  uves_msg("xsig=%f,ysig=%f,xfwhm=%f,yfwhm=%f,amp=%f,kstat=%d",xsig,ysig,xfwhm,yfwhm,amp,kstat);
375 
376  }
377 
378  /* check status */
379  if(kstat == 0) {
380 
381  xerr=xerr*fabs(STEP[1]);
382  xsig=xsig*fabs(STEP[1]);
383  xfwhm=xsig*TWOSQRT2LN2; /* MIDAS: 2.35482 */
384 
385  yerr=yerr*fabs(STEP[2]);
386  ysig=ysig*fabs(STEP[2]);
387  yfwhm=ysig*TWOSQRT2LN2; /* MIDAS: 2.35482 */
388  ok++;
389  tmp_val=cpl_table_get_double(*m_tbl,"IDENT",i,&status);
390  tmp_val-=1;
391  tmp_val/=1000.;
392 
393  } else {
394 
395  xerr=0.;
396  xsig=0.;
397  xfwhm=0.;
398 
399  yerr=0.;
400  ysig=0.;
401  yfwhm=0.;
402  nok++;
403  /* we assume that not using IQUE function kstatus >=0 */
404  }
405 
406 
407  uves_msg_debug("nf=%d %f %f %f %f %f %d",nf,xout,yout,xsig,ysig,amp,kstat);
408 
409  /* we write results in table */
410 
411 
412  /* we check if FWHM makes sense */
413  if( (xfwhm > img_sx *fabs(STEP[1])) || (yfwhm > img_sy *fabs(STEP[2])) ) {
414  kstat = 4;
415  }
416  if ( (xfwhm < 0.0) || (yfwhm < 0.0) ) {
417  kstat = 5;
418  }
419 
420 
421 
422  /* to debug: 0-->NN>0 */
423  if (i<0) {
424  uves_msg("Min=%f,Max=%f",cpl_image_get_min(img_sub),
425  cpl_image_get_max(img_sub));
426  uves_msg("llx=%d,lly=%d,urx=%d,ury=%d",img_llx,img_lly,img_urx,img_ury);
427  uves_msg("sx=%d,sy=%d,ofx=%d,ofy=%d",img_sx,img_sy,img_ofx,img_ofy);
428  uves_msg("img_sx=%d,img_sy=%d,cpix[0]=%d,cpix[1]=%d,cpix[2]=%d,cpix[3]=%d",
429  img_sx,img_sy,cpix[0],cpix[1],cpix[2],cpix[3]);
430 
431  uves_msg("i=%d,xout=%f,yout=%f,xerr=%f,yerr=%f",i,xout,yout,xerr,yerr);
432  uves_msg("xsig=%f,ysig=%f,xfwhm=%f,yfwhm=%f,amp=%f,kstat=%d",xsig,ysig,xfwhm,yfwhm,amp,kstat);
433 
434  }
435 
436 
437  cpl_table_set_double(*m_tbl,"IDENT",i,tmp_val);
438  cpl_table_set_double(*m_tbl,"ICENT",i,(double)amp);
439  cpl_table_set_double(*m_tbl,"XERR",i,(double)xerr);
440  cpl_table_set_double(*m_tbl,"YERR",i,(double)yerr);
441 
442  cpl_table_set_double(*m_tbl,"XCEN",i,(double)(xout+img_ofx));
443  cpl_table_set_double(*m_tbl,"YCEN",i,(double)(yout+img_ofy));
444  cpl_table_set_double(*m_tbl,"XSIG",i,(double)xsig);
445  cpl_table_set_double(*m_tbl,"YSIG",i,(double)ysig);
446  cpl_table_set_double(*m_tbl,"XFWHM",i,(double)xfwhm);
447  cpl_table_set_double(*m_tbl,"YFWHM",i,(double)yfwhm);
448  cpl_table_set_int (*m_tbl,"STATUS",i,kstat);
449  nf=nf+1;
450 
451  }
452 
453  if (false) /* Debugging... */
454  {
455  cpl_table *dum = cpl_table_new(cpl_table_get_nrow(*m_tbl));
456 
457  cpl_table_duplicate_column(dum,"STAT",*m_tbl,"STATUS");
458 
459  uves_msg_warning("xcen = %f", cpl_table_get_column_mean(*m_tbl, "XCEN"));
460  uves_msg_warning("ycen = %f", cpl_table_get_column_mean(*m_tbl, "YCEN"));
461  uves_msg_warning("xerr = %f", cpl_table_get_column_mean(*m_tbl, "XERR"));
462  uves_msg_warning("yerr = %f", cpl_table_get_column_mean(*m_tbl, "YERR"));
463  uves_msg_warning("xsig = %f", cpl_table_get_column_mean(*m_tbl, "XSIG"));
464  uves_msg_warning("ysig = %f", cpl_table_get_column_mean(*m_tbl, "YSIG"));
465  uves_msg_warning("xfwhm = %f", cpl_table_get_column_mean(*m_tbl, "XFWHM"));
466  uves_msg_warning("yfwhm = %f", cpl_table_get_column_mean(*m_tbl, "YFWHM"));
467  uves_msg_warning("icent = %f", cpl_table_get_column_mean(*m_tbl, "ICENT"));
468  uves_msg_warning("status = %f", cpl_table_get_column_mean(*m_tbl, "STATUS"));
469 
470 // cpl_table_dump(dum, 0, cpl_table_get_nrow(*m_tbl), stdout);
471  }
472 
473  uves_msg_debug("nok=%d ok=%d",nok,ok);
474 
475  cleanup:
476  uves_free_image(&img_sub);
477  uves_free_image(&img_cst);
478 
479  return 0;
480 }
#define uves_msg_warning(...)
Print an warning message.
Definition: uves_msg.h:87
#define check_nomsg(CMD)
Definition: uves_error.h:204
int uves_physmod_stacen(float *p_img, int dimx, int dimy, char meth, int *image, float *xout, float *yout, float *xerr, float *yerr, float *xsig, float *ysig, float *xyval, int *stat)
Routines used to do Gaussian fit to a line.
#define uves_msg(...)
Print a message on 'info' or 'debug' level.
Definition: uves_msg.h:119
int uves_physmod_center_gauss(const cpl_image *raw_image, cpl_table **m_tbl)
Fit the image line X and Y distributions with a Gaussian in a box.
#define uves_msg_debug(...)
Print a debug message.
Definition: uves_msg.h:97
#define check(CMD,...)
Definition: uves_error.h:198