FORS Pipeline Reference Manual  5.0.9
fors_star.c
1 /* $Id: fors_star.c,v 1.29 2010-09-14 07:49:30 cizzo Exp $
2  *
3  * This file is part of the FORS Library
4  * Copyright (C) 2002-2010 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author: cizzo $
23  * $Date: 2010-09-14 07:49:30 $
24  * $Revision: 1.29 $
25  * $Name: not supported by cvs2svn $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 #include <fors_star.h>
33 
34 #include <fors_utils.h>
35 
36 #include <cpl.h>
37 
38 #include <math.h>
39 #include <assert.h>
40 
49 /*-----------------------------------------------------------------------------
50  Prototypes
51  -----------------------------------------------------------------------------*/
52 
53 static
54 double _get_optional_table_value( const cpl_table *tab,
55  unsigned int row,
56  const char *colname);
57 
58 /*-----------------------------------------------------------------------------
59  Private Implementation
60  -----------------------------------------------------------------------------*/
61 
62 /*----------------------------------------------------------------------------*/
63 #undef cleanup
64 #define cleanup
65 
71 /*----------------------------------------------------------------------------*/
72 static
73 double _get_optional_table_value( const cpl_table *tab,
74  unsigned int row,
75  const char *colname)
76 {
77  int null;
78  if (colname != NULL && colname[0] != '\0')
79  {
80  double d;
81  d = cpl_table_get( tab, colname, row, &null);
82  assure( !cpl_error_get_code(),
83  return d,
84  "Missing column: %s",
85  colname);
86  return d;
87 
88  }
89  else
90  return 0.0;
91 }
92 
93 /*-----------------------------------------------------------------------------
94  Implementation
95  -----------------------------------------------------------------------------*/
96 
97 /*----------------------------------------------------------------------------*/
98 #undef cleanup
99 #define cleanup
100 
113 /*----------------------------------------------------------------------------*/
114 fors_star *fors_star_new(double x, double y,
115  double fwhm,
116  double smajor, double sminor,
117  double orientation,
118  double m, double dm,
119  double si)
120 {
121  assure( smajor >= sminor && sminor >= 0, return NULL,
122  "Illegal semi major/minor axes: %g, %g",
123  smajor, sminor );
124 
125  assure( 0 <= si && si <= 1, return NULL,
126  "Stellarity index must be between 0 and 1, is %f",
127  si);
128 
129  assure( fwhm >= 0, return NULL,
130  "Star FWHM must be non-negative, is %f",
131  fwhm);
132 
133  fors_star *s = cpl_malloc(sizeof(*s));
134 
135  s->pixel = fors_point_new(x, y);
136  s->fwhm = fwhm;
137  s->semi_major = smajor;
138  s->semi_minor = sminor;
139  s->stellarity_index = si;
140  s->orientation = orientation;
141  s->magnitude = m;
142  s->dmagnitude = dm;
143  s->magnitude_corr = 0;
144  s->dmagnitude_corr = 0;
145  s->id = NULL;
146  s->weight = 0;
147 
148  return s;
149 }
150 
151 /*----------------------------------------------------------------------------*/
152 #undef cleanup
153 #define cleanup fors_star_delete(&s)
154 
169 /*----------------------------------------------------------------------------*/
170 fors_star *fors_star_new_from_table(const cpl_table *tab,
171  unsigned int row,
172  const char *x_col,
173  const char *y_col,
174  const char *fwhm_col,
175  const char *smaj_col,
176  const char *smin_col,
177  const char *theta_col,
178  const char *mag_col,
179  const char *dmag_col,
180  const char *stlndx_col)
181 {
182  int null;
183  fors_star *s = cpl_malloc(sizeof(*s));
184 
185  s->pixel = fors_point_new( _get_optional_table_value(tab, row, x_col),
186  _get_optional_table_value(tab, row, y_col));
187  assure(!cpl_error_get_code(), return s, NULL);
188 
189  s->fwhm = _get_optional_table_value(tab, row, fwhm_col);
190  assure(!cpl_error_get_code(), return s, NULL);
191 
192  s->semi_major = _get_optional_table_value(tab, row, smaj_col);
193  assure(!cpl_error_get_code(), return s, NULL);
194 
195  s->semi_minor = _get_optional_table_value(tab, row, smin_col);
196  assure(!cpl_error_get_code(), return s, NULL);
197 
198  s->stellarity_index = _get_optional_table_value(tab, row, stlndx_col);
199  assure(!cpl_error_get_code(), return s, NULL);
200 
201  s->orientation = _get_optional_table_value(tab, row, theta_col);
202  assure(!cpl_error_get_code(), return s, NULL);
203 
204  s->magnitude = _get_optional_table_value(tab, row, mag_col);
205  assure(!cpl_error_get_code(), return s, NULL);
206 
207  s->dmagnitude = _get_optional_table_value(tab, row, dmag_col);
208  assure(!cpl_error_get_code(), return s, NULL);
209 
210  s->magnitude_corr = 0;
211  s->dmagnitude_corr = 0;
212  s->id = NULL;
213  s->weight = 0;
214 
215  return s;
216 }
217 
218 /*----------------------------------------------------------------------------*/
219 #undef cleanup
220 #define cleanup
221 
226 /*----------------------------------------------------------------------------*/
228 {
229  bool success = 1;
230 
231  success &= (star != NULL);
232  success &= (star->semi_major >= star->semi_minor);
233  success &= (0.0 <= star->stellarity_index && star->stellarity_index <= 1.0);
234  success &= (star->fwhm > 0.0);
235 
236  return success;
237 }
238 
239 /*----------------------------------------------------------------------------*/
240 #undef cleanup
241 #define cleanup
242 
247 /*----------------------------------------------------------------------------*/
249 {
250  fors_star *d;
251 
252  assure( star != NULL, return NULL, NULL );
253 
254  d = cpl_malloc(sizeof(*d));
255  *d = *star; /* Simple copy of all members, next do
256  deep copy of pointers */
257 
258  d->pixel = fors_point_duplicate(star->pixel);
259 
260  if (star->id != NULL) {
261  d->id = fors_std_star_duplicate(star->id);
262  }
263 
264  return d;
265 }
266 
267 /*----------------------------------------------------------------------------*/
272 /*----------------------------------------------------------------------------*/
274 {
275  if (star && *star) {
276  fors_point_delete(&(*star)->pixel);
277  if ((*star)->id != NULL) {
278  fors_std_star_delete_const(&((*star)->id));
279  }
280  cpl_free(*star); *star = NULL;
281  }
282  return;
283 }
284 
285 /*----------------------------------------------------------------------------*/
290 /*----------------------------------------------------------------------------*/
292 {
293  if (star && *star) {
294  fors_point_delete(&(*star)->pixel);
295  cpl_free(*star); *star = NULL;
296  }
297  return;
298 }
299 
300 /*----------------------------------------------------------------------------*/
307 /*----------------------------------------------------------------------------*/
308 bool
310  const fors_star *t)
311 {
312  assure( s != NULL && t != NULL, return true, NULL );
313 
314  return (fors_point_equal(s->pixel, t->pixel));
315 }
316 
317 /*----------------------------------------------------------------------------*/
318 #undef clenaup
319 #define cleanup
320 
327 /*----------------------------------------------------------------------------*/
328 bool
330  const fors_star *s2,
331  void *data)
332 {
333  data = data;
334  return (s1->magnitude < s2->magnitude);
335 }
336 
337 /*----------------------------------------------------------------------------*/
338 #undef cleanup
339 #define cleanup
340 
346 /*----------------------------------------------------------------------------*/
347 double fors_star_distsq(const fors_star *s, const fors_star *t)
348 {
349  assure( s != NULL, return 0, NULL );
350  assure( t != NULL, return 0, NULL );
351 
352  return fors_point_distsq(s->pixel, t->pixel);
353 }
354 
355 /*----------------------------------------------------------------------------*/
356 #undef cleanup
357 #define cleanup
358 
364 /*----------------------------------------------------------------------------*/
365 double fors_star_extension(const fors_star *s, void *data)
366 {
367  assure( s != NULL, return -1, NULL );
368  data = data;
369 
370  /* return sqrt(s->semi_major * s->semi_minor); */
371  return s->fwhm / TWOSQRT2LN2;
372 }
373 
374 /*----------------------------------------------------------------------------*/
375 #undef cleanup
376 #define cleanup
377 
383 /*----------------------------------------------------------------------------*/
384 double fors_star_stellarity(const fors_star *s, void *data)
385 {
386  assure( s != NULL, return -1, NULL );
387  data = data;
388 
389  return s->stellarity_index;
390 }
391 
392 /*----------------------------------------------------------------------------*/
393 #undef cleanup
394 #define cleanup
395 
401 /*----------------------------------------------------------------------------*/
402 double fors_star_ellipticity(const fors_star *s, void *data)
403 {
404  assure( s != NULL, return -1, NULL );
405  data = data;
406 
407  if (s->semi_major <= 0) return 1;
408  else return 1 - (s->semi_minor / s->semi_major);
409 }
410 
411 /*----------------------------------------------------------------------------*/
417 /*----------------------------------------------------------------------------*/
418 void fors_star_print(cpl_msg_severity level, const fors_star *s)
419 {
420  if (s == NULL) {
421  fors_msg(level, "[NULL]");
422  }
423  else {
424  fors_msg(level, "at (%7.2f, %7.2f): m = %g +- %g (mc = %g +- %g), "
425  "shape: (%g, %g, %g)",
426  s->pixel->x, s->pixel->y,
427  s->magnitude, s->dmagnitude,
428  s->magnitude_corr, s->dmagnitude_corr,
429  s->orientation, s->semi_major, s->semi_minor);
430  }
431 
432  return;
433 }
434 
435 /*----------------------------------------------------------------------------*/
441 /*----------------------------------------------------------------------------*/
442 void
443 fors_star_print_list(cpl_msg_severity level, const fors_star_list *sl)
444 {
445  if (sl == NULL) fors_msg(level, "Null list");
446  else {
447  const fors_star *s;
448 
449  for (s = fors_star_list_first_const(sl);
450  s != NULL;
451  s = fors_star_list_next_const(sl)) {
452  fors_star_print(level, s);
453  }
454  }
455  return;
456 }
457 
458 /*----------------------------------------------------------------------------*/
465 /*----------------------------------------------------------------------------*/
466 double
467 fors_star_get_x(const fors_star *s, void *data)
468 {
469  assure( s != NULL, return -1, NULL );
470 
471  data = data;
472 
473  return s->pixel->x;
474 }
475 
476 /*----------------------------------------------------------------------------*/
483 /*----------------------------------------------------------------------------*/
484 double
485 fors_star_get_y(const fors_star *s, void *data)
486 {
487  assure( s != NULL, return -1, NULL );
488 
489  data = data;
490 
491  return s->pixel->y;
492 }
493 
494 
495 /*----------------------------------------------------------------------------*/
502 /*----------------------------------------------------------------------------*/
503 double
504 fors_star_get_zeropoint(const fors_star *s, void *data)
505 {
506  assure( s != NULL, return 0, NULL );
507  assure( s->id != NULL, return 0, NULL );
508  data = data;
509 
510  return (s->id->magnitude - s->magnitude_corr);
511 }
512 
513 /*----------------------------------------------------------------------------*/
520 /*----------------------------------------------------------------------------*/
521 double
523 {
524  assure( s != NULL, return 0, NULL );
525  assure( s->id != NULL, return 0, NULL );
526  data = data;
527 
528  return sqrt(s->dmagnitude_corr * s->dmagnitude_corr +
529  s->id->dmagnitude * s->id->dmagnitude);
530 }
531 
532 /*----------------------------------------------------------------------------*/
539 /*----------------------------------------------------------------------------*/
540 bool
541 fors_star_is_identified(const fors_star *s, void *data)
542 {
543  data = data;
544  assure( s != NULL, return 0, NULL );
545  return (s->id != NULL && s->id->trusted);
546 }
547 
548 #define LIST_DEFINE
549 #undef LIST_ELEM
550 #define LIST_ELEM fors_star
551 #include <list.h>
552 
fors_point * fors_point_new(double x, double y)
Constructor.
Definition: fors_point.c:53
double fors_star_get_x(const fors_star *s, void *data)
Get position.
Definition: fors_star.c:467
void fors_star_print_list(cpl_msg_severity level, const fors_star_list *sl)
Print list of stars.
Definition: fors_star.c:443
double fors_star_stellarity(const fors_star *s, void *data)
Get star stellarity.
Definition: fors_star.c:384
double fors_star_ellipticity(const fors_star *s, void *data)
Get star ellipticity.
Definition: fors_star.c:402
bool fors_star_equal(const fors_star *s, const fors_star *t)
Test for equality.
Definition: fors_star.c:309
void fors_star_delete(fors_star **star)
Delete object and set pointer to NULL.
Definition: fors_star.c:273
double fors_star_distsq(const fors_star *s, const fors_star *t)
Get distance between stars.
Definition: fors_star.c:347
void fors_star_print(cpl_msg_severity level, const fors_star *s)
Print object.
Definition: fors_star.c:418
static double _get_optional_table_value(const cpl_table *tab, unsigned int row, const char *colname)
Get a double value from a table.
Definition: fors_star.c:73
#define assure(EXPR)
Definition: list.c:101
void fors_point_delete(fors_point **p)
Destructor.
Definition: fors_point.c:87
fors_star * fors_star_new(double x, double y, double fwhm, double smajor, double sminor, double orientation, double m, double dm, double si)
Constructor.
Definition: fors_star.c:114
bool fors_star_is_identified(const fors_star *s, void *data)
Determine if star was identified.
Definition: fors_star.c:541
fors_star * fors_star_duplicate(const fors_star *star)
Copy constructor.
Definition: fors_star.c:248
double fors_star_get_y(const fors_star *s, void *data)
Get position.
Definition: fors_star.c:485
double fors_star_get_zeropoint_err(const fors_star *s, void *data)
Get zeropoint error.
Definition: fors_star.c:522
double fors_star_extension(const fors_star *s, void *data)
Get star size.
Definition: fors_star.c:365
bool fors_star_brighter_than(const fors_star *s1, const fors_star *s2, void *data)
Compare star brightness.
Definition: fors_star.c:329
fors_point * fors_point_duplicate(const fors_point *p)
Copy constructor.
Definition: fors_point.c:70
bool fors_point_equal(const fors_point *p, const fors_point *q)
Equality.
Definition: fors_point.c:120
bool fors_star_check_values(const fors_star *star)
Copy constructor.
Definition: fors_star.c:227
void fors_star_delete_but_standard(fors_star **star)
Delete object and set pointer to NULL - but ignore the standard star.
Definition: fors_star.c:291
double fors_point_distsq(const fors_point *p, const fors_point *q)
Metric.
Definition: fors_point.c:103
double fors_star_get_zeropoint(const fors_star *s, void *data)
Get zeropoint.
Definition: fors_star.c:504
fors_star * fors_star_new_from_table(const cpl_table *tab, unsigned int row, const char *x_col, const char *y_col, const char *fwhm_col, const char *smaj_col, const char *smin_col, const char *theta_col, const char *mag_col, const char *dmag_col, const char *stlndx_col)
Create a star from a table WITHOUT checking.
Definition: fors_star.c:170