FORS Pipeline Reference Manual  5.0.9
fors_std_star.c
1 /* $Id: fors_std_star.c,v 1.20 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.20 $
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 #include <fors_utils.h>
34 
35 #include <cpl.h>
36 
37 #include <math.h>
38 #include <assert.h>
39 #include <stdbool.h>
40 #include <float.h>
41 
42 /*-----------------------------------------------------------------------------
43  Prototypes
44  -----------------------------------------------------------------------------*/
45 
46 static
47 double _get_optional_table_value( const cpl_table *tab,
48  unsigned int row,
49  const char *colname);
50 static
51 double _square( double a);
52 
53 /*-----------------------------------------------------------------------------
54  Private Implementation
55  -----------------------------------------------------------------------------*/
56 
57 /*----------------------------------------------------------------------------*/
58 #undef cleanup
59 #define cleanup
60 
66 /*----------------------------------------------------------------------------*/
67 static
68 double _get_optional_table_value( const cpl_table *tab,
69  unsigned int row,
70  const char *colname)
71 {
72  int null;
73  cpl_errorstate errstat = cpl_errorstate_get();
74  cpl_error_code errc;
75 
76  if (colname != NULL && colname[0] != '\0')
77  {
78  double d;
79  d = cpl_table_get( tab, colname, row, &null);
80  if (!cpl_errorstate_is_equal(errstat))
81  {
82  switch (errc = cpl_error_get_code())
83  {
84  case CPL_ERROR_DATA_NOT_FOUND:
85  cpl_error_set_message( cpl_func,
86  errc,
87  "Column \"%s\" not found",
88  colname);
89  break;
90  case CPL_ERROR_INVALID_TYPE:
91  cpl_error_set_message( cpl_func,
92  errc,
93  "Column \"%s\" is not numeric",
94  colname);
95  break;
96  default: break;
97  }
98  cleanup;
99  }
100  else
101  {
102  return d;
103  }
104  }
105 
106  {
107  int n;
108  union _nant {
109  unsigned char bytes[8];
110  double val;
111  } nan;
112  for (n = 0; n < 8; n++)
113  nan.bytes[n] = (unsigned char)255;
114  return nan.val;
115  }
116 }
117 
118 /*----------------------------------------------------------------------------*/
124 /*----------------------------------------------------------------------------*/
125 static
126 double _square( double a)
127 {
128  return a*a;
129 }
130 
131 
132 /*-----------------------------------------------------------------------------
133  Implementation
134  -----------------------------------------------------------------------------*/
135 
136 /*----------------------------------------------------------------------------*/
137 /*----------------------------------------------------------------------------*/
138 #undef cleanup
139 #define cleanup \
140 do { \
141  cpl_table_delete(t); \
142 } while(0)
143 
144 /*----------------------------------------------------------------------------*/
161 /*----------------------------------------------------------------------------*/
162 fors_std_star *fors_std_star_new( double ra, double dec,
163  double m, double dm,
164  double cat_m, double dcat_m,
165  double col, double dcol,
166  double cov_catm_col,
167  const char *name)
168 {
169  fors_std_star *s = cpl_malloc(sizeof(*s));
170 
171  s->ra = ra;
172  s->dec = dec;
173  s->magnitude = m;
174  s->dmagnitude = dm;
175  s->cat_magnitude = cat_m;
176  s->dcat_magnitude = dcat_m;
177  s->color = col;
178  s->dcolor = dcol;
179  s->cov_catm_color = cov_catm_col;
180 
181  s->pixel = fors_point_new(-1, -1);
182 
183  if (name != NULL) {
184  s->name = cpl_strdup(name);
185  }
186  else {
187  s->name = NULL;
188  }
189 
190  s->trusted = true;
191 
192  return s;
193 }
194 
195 /*----------------------------------------------------------------------------*/
196 #undef cleanup
197 #define cleanup fors_std_star_delete(&s)
198 
228 /*----------------------------------------------------------------------------*/
229 fors_std_star *fors_std_star_new_from_table(
230  const cpl_table *tab,
231  unsigned int row,
232  const char *ra_col,
233  const char *dec_col,
234  const char *mag_col,
235  const char *dmag_col,
236  const char *catmag_col,
237  const char *dcatmag_col,
238  const char *color_col,
239  const char *dcolor_col,
240  const char *cov_catm_color_col,
241  const char *x_col,
242  const char *y_col,
243  const char *name_col)
244 {
245  cpl_errorstate errstat = cpl_errorstate_get();
246  double x,
247  y;
248 
249  fors_std_star *s = cpl_malloc(sizeof(*s));
250  s->name = NULL; /* assign this before calling assure (that calls delete) */
251 
252  s->ra = _get_optional_table_value(tab, row, ra_col);
253  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
254  s->dec = _get_optional_table_value(tab, row, dec_col);
255  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
256  s->magnitude = _get_optional_table_value(tab, row, mag_col);
257  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
258  s->dmagnitude = _get_optional_table_value(tab, row, dmag_col);
259  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
260  s->cat_magnitude = _get_optional_table_value(tab, row, catmag_col);
261  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
262  s->dcat_magnitude = _get_optional_table_value(tab, row, dcatmag_col);
263  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
264  s->color = _get_optional_table_value(tab, row, color_col);
265  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
266  s->dcolor = _get_optional_table_value(tab, row, dcolor_col);
267  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
268  s->cov_catm_color = _get_optional_table_value(tab, row,
269  cov_catm_color_col);
270  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
271 
272  x = _get_optional_table_value(tab, row, x_col);
273  y = _get_optional_table_value(tab, row, y_col);
274  s->pixel = fors_point_new( (isnan(x) ? -1 : x),
275  (isnan(y) ? -1 : y));
276  assure(cpl_errorstate_is_equal(errstat), return s, NULL);
277  if (s->pixel->x < 1) s->pixel->x = -1;
278  if (s->pixel->y < 1) s->pixel->y = -1;
279 
280  s->name = NULL;
281  if (name_col != NULL)
282  {
283  const char *str;
284  str = cpl_table_get_string(tab, name_col, row);
285  if (!cpl_errorstate_is_equal(errstat))
286  {
287  cpl_error_code errc;
288  switch (errc = cpl_error_get_code())
289  {
290  case CPL_ERROR_DATA_NOT_FOUND:
291  cpl_error_set_message( cpl_func,
292  errc,
293  "Column \"%s\" not found",
294  name_col);
295  break;
296  case CPL_ERROR_INVALID_TYPE:
297  cpl_error_set_message( cpl_func,
298  errc,
299  "Column \"%s\" is not string type",
300  name_col);
301  break;
302  default: break;
303  }
304  cleanup;
305  return s;
306  }
307  if (str != NULL)
308  s->name = cpl_strdup(str);
309  }
310 
311  s->trusted = true;
312 
313  return s;
314 }
315 
316 /*----------------------------------------------------------------------------*/
321 /*----------------------------------------------------------------------------*/
322 void fors_std_star_delete( fors_std_star **s){
323  if (s && *s) {
324  fors_point_delete(&(*s)->pixel);
325  if ((*s)->name != NULL) {
326  cpl_free((void *)(*s)->name); (*s)->name = NULL;
327  }
328  cpl_free(*s); *s = NULL;
329  }
330  return;
331 }
332 
333 /*----------------------------------------------------------------------------*/
338 /*----------------------------------------------------------------------------*/
339 void fors_std_star_delete_const( const fors_std_star **s){
340  fors_std_star_delete((fors_std_star **)s);
341  return;
342 }
343 
344 /*----------------------------------------------------------------------------*/
345 #undef cleanup
346 #define cleanup
347 
352 /*----------------------------------------------------------------------------*/
353 fors_std_star *fors_std_star_duplicate( const fors_std_star *s)
354 {
355  fors_std_star *d = NULL;
356 
357  assure( s != NULL, return NULL, NULL );
358 
359  d = cpl_malloc(sizeof(*d));
360 
361  d->ra = s->ra;
362  d->dec = s->dec;
363  d->magnitude = s->magnitude;
364  d->dmagnitude = s->dmagnitude;
365  d->cat_magnitude = s->cat_magnitude;
366  d->dcat_magnitude = s->dcat_magnitude;
367  d->color = s->color;
368  d->dcolor = s->dcolor;
369  d->cov_catm_color = s->cov_catm_color;
370 
371  d->pixel = fors_point_duplicate(s->pixel);
372  d->name = s->name != NULL ? cpl_strdup(s->name) : NULL;
373 
374  d->trusted = s->trusted;
375 
376  return d;
377 }
378 
379 /*----------------------------------------------------------------------------*/
380 #undef cleanup
381 #define cleanup
382 
388 /*----------------------------------------------------------------------------*/
389 void fors_std_star_set_name( fors_std_star *s,
390  const char *name)
391 {
392  assure( s != NULL, return, NULL );
393 
394  cpl_free(s->name);
395  s->name = (name != NULL) ? cpl_strdup(name) : NULL;
396 
397  return;
398 }
399 
400 /*----------------------------------------------------------------------------*/
409 /*----------------------------------------------------------------------------*/
410 bool fors_std_star_equal( const fors_std_star *s,
411  const fors_std_star *t)
412 {
413  assure( s != NULL && t != NULL, return true, NULL );
414 
415  return( s->trusted && t->trusted &&
416  fabs(s->ra - t->ra ) < DBL_EPSILON &&
417  fabs(s->dec - t->dec) < DBL_EPSILON);
418 }
419 
420 /*----------------------------------------------------------------------------*/
421 #undef cleanup
422 #define cleanup
423 
428 /*----------------------------------------------------------------------------*/
429 void fors_std_star_print( cpl_msg_severity level,
430  const fors_std_star *star)
431 {
432  if (star == NULL) {
433  fors_msg(level, "NULL std.star");
434  }
435  else {
436  fors_msg(level, "(%7.4f, %7.4f): %sm = %g +- %g "
437  "(col = %g +- %g)%s, (x=%7.2f, y=%7.2f) %s",
438  star->ra, star->dec,
439  (star->trusted ? "" : "untrusted magnitude (values are: "),
440  star->magnitude, star->dmagnitude,
441  star->color, star->dcolor,
442  (star->trusted ? "" : ")"),
443  star->pixel->x, star->pixel->y,
444  ((star->name != NULL) ? star->name : ""));
445  }
446 
447  return;
448 }
449 
450 /*----------------------------------------------------------------------------*/
451 #undef cleanup
452 #define cleanup
453 
458 /*----------------------------------------------------------------------------*/
459 void fors_std_star_print_list( cpl_msg_severity level,
460  const fors_std_star_list *sl)
461 {
462  if (sl == NULL) fors_msg(level, "Null list");
463  else {
464  const fors_std_star *s;
465 
466  for (s = fors_std_star_list_first_const(sl);
467  s != NULL;
468  s = fors_std_star_list_next_const(sl)) {
469 
470  fors_std_star_print(level, s);
471  }
472  }
473  return;
474 }
475 
476 /*----------------------------------------------------------------------------*/
484 /*----------------------------------------------------------------------------*/
485 bool fors_std_star_brighter_than(const fors_std_star *s,
486  const fors_std_star *t,
487  void *data){
488  data = data;
489  return (s->trusted && t->trusted &&
490  s->magnitude < t->magnitude);
491 }
492 
493 /*----------------------------------------------------------------------------*/
494 #undef cleanup
495 #define cleanup
496 
502 /*----------------------------------------------------------------------------*/
503 double fors_std_star_dist_arcsec( const fors_std_star *s,
504  const fors_std_star *t)
505 {
506  assure( s != NULL, return -1, NULL );
507  assure( t != NULL, return -1, NULL );
508 
509  /* Convert to radians, use stock formula for angular separation,
510  convert */
511  double s_ra = s->ra * 2*M_PI / 360;
512  double s_dec = s->dec * 2*M_PI / 360;
513  double t_ra = t->ra * 2*M_PI / 360;
514  double t_dec = t->dec * 2*M_PI / 360;
515 
516  double cos_separation =
517  sin(s_dec)*sin(t_dec) +
518  cos(s_dec)*cos(t_dec) * cos(s_ra - t_ra);
519 
520  if (cos_separation < -1) cos_separation = -1;
521  if (cos_separation > 1) cos_separation = 1;
522 
523  /* Note: result is always positive */
524  return (acos(cos_separation) * 360 / (2*M_PI)) * 3600;
525 }
526 
527 /*----------------------------------------------------------------------------*/
537 /*----------------------------------------------------------------------------*/
538 void fors_std_star_compute_corrected_mag(
539  fors_std_star *s,
540  double color_term,
541  double dcolor_term)
542 {
543  cassure( s != NULL, CPL_ERROR_NULL_INPUT, return, NULL);
544 
545  s->magnitude = s->cat_magnitude - color_term * s->color;
546  s->dmagnitude = sqrt( _square(s->dcat_magnitude)
547  - 2.0 * color_term * s->cov_catm_color
548  + _square(color_term * s->dcolor)
549  + _square(s->color * dcolor_term));
550 }
551 
552 /*----------------------------------------------------------------------------*/
553 #define LIST_DEFINE
554 #undef LIST_ELEM
555 #define LIST_ELEM fors_std_star
556 #include <list.h>
557 
fors_point * fors_point_new(double x, double y)
Constructor.
Definition: fors_point.c:53
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_point * fors_point_duplicate(const fors_point *p)
Copy constructor.
Definition: fors_point.c:70