fors_std_star.c

00001 /* $Id: fors_std_star.c,v 1.10 2008/01/29 13:28:28 cizzo Exp $
00002  *
00003  * This file is part of the FORS Library
00004  * Copyright (C) 2002-2006 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00019  */
00020 
00021 /*
00022  * $Author: cizzo $
00023  * $Date: 2008/01/29 13:28:28 $
00024  * $Revision: 1.10 $
00025  * $Name:  $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <fors_star.h>
00033 #include <fors_utils.h>
00034 
00035 #include <cpl.h>
00036 
00037 #include <math.h>
00038 #include <assert.h>
00039 #include <stdbool.h>
00040 #include <float.h>
00041 
00042 #undef cleanup
00043 #define cleanup \
00044 do { \
00045    cpl_table_delete(t); \
00046 } while(0)
00047 
00060 fors_std_star *
00061 fors_std_star_new(double ra, double dec, double m, double dm,
00062           double cat_m, double dcat_m,
00063           double col,
00064           const char *name)
00065 {
00066     fors_std_star *s = cpl_malloc(sizeof(*s));
00067 
00068     s->ra         = ra;
00069     s->dec        = dec;
00070     s->magnitude  = m;
00071     s->dmagnitude = dm;
00072     s->cat_magnitude  = cat_m;
00073     s->dcat_magnitude = dcat_m;
00074     s->color      = col;
00075 
00076     s->pixel     = fors_point_new(-1, -1);
00077     
00078     if (name != NULL) {
00079         s->name = cpl_strdup(name);
00080     }
00081     else {
00082         s->name = NULL;
00083     }
00084 
00085     return s;
00086 }
00087 
00092 void
00093 fors_std_star_delete(fors_std_star **s)
00094 {
00095     if (s && *s) {
00096         fors_point_delete(&(*s)->pixel);
00097         if ((*s)->name != NULL) {
00098             cpl_free((void *)(*s)->name); (*s)->name = NULL;
00099         }
00100         cpl_free(*s); *s = NULL;
00101     }
00102     return;
00103 }
00104 
00109 void
00110 fors_std_star_delete_const(const fors_std_star **s)
00111 {
00112     fors_std_star_delete((fors_std_star **)s);
00113     return;
00114 }
00115 
00116 
00117 #undef cleanup
00118 #define cleanup
00119 
00125 fors_std_star *
00126 fors_std_star_duplicate(const fors_std_star *s)
00127 {
00128     fors_std_star *d = NULL;
00129 
00130     assure( s != NULL, return NULL, NULL );
00131 
00132     d = cpl_malloc(sizeof(*d));
00133 
00134     d->ra             = s->ra;
00135     d->dec            = s->dec;
00136     d->magnitude      = s->magnitude;
00137     d->dmagnitude     = s->dmagnitude;
00138     d->cat_magnitude  = s->cat_magnitude;
00139     d->dcat_magnitude = s->dcat_magnitude;
00140     d->color          = s->color;
00141     
00142     d->pixel          = fors_point_duplicate(s->pixel);
00143     d->name           = s->name != NULL ? cpl_strdup(s->name) : NULL;
00144 
00145     return d;    
00146 }
00147 
00156 bool
00157 fors_std_star_equal(const fors_std_star *s,
00158             const fors_std_star *t)
00159 {
00160     assure( s != NULL && t != NULL, return true, NULL );
00161 
00162     return(fabs(s->ra  - t->ra ) < DBL_EPSILON &&
00163            fabs(s->dec - t->dec) < DBL_EPSILON);
00164 }
00165 
00166 #undef cleanup
00167 #define cleanup
00168 
00173 void
00174 fors_std_star_print(cpl_msg_severity level, const fors_std_star *star)
00175 {
00176     if (star == NULL) {
00177         fors_msg(level, "NULL std.star");
00178     }
00179     else {
00180         fors_msg(level, "(%7.4f, %7.4f): m = %g +- %g (col = %g), (%7.2f, %7.2f) %s",
00181                  star->ra, star->dec,
00182                  star->magnitude, star->dmagnitude,
00183          star->color,
00184                  star->pixel->x, star->pixel->y,
00185                  star->name != NULL ? star->name : "");
00186     }
00187 
00188     return;                  
00189 }
00190 
00191 
00199 bool
00200 fors_std_star_brighter_than(const fors_std_star *s,
00201                 const fors_std_star *t,
00202                 void *data)
00203 {
00204     data = data;
00205     return (s->magnitude < t->magnitude);
00206 }
00207 
00208 #undef cleanup
00209 #define cleanup
00210 
00215 void
00216 fors_std_star_print_list(cpl_msg_severity level, const fors_std_star_list *sl)
00217 {
00218     if (sl == NULL) fors_msg(level, "Null list");
00219     else {
00220         const fors_std_star *s;
00221         
00222         for (s = fors_std_star_list_first_const(sl);
00223              s != NULL;
00224              s = fors_std_star_list_next_const(sl)) {
00225             
00226             fors_std_star_print(level, s);
00227         }
00228     }
00229     return;
00230 }
00231 
00232 #undef cleanup
00233 #define cleanup
00234 
00240 double
00241 fors_std_star_dist_arcsec(const fors_std_star *s,
00242                 const fors_std_star *t)
00243 {
00244     assure( s != NULL, return -1, NULL );
00245     assure( t != NULL, return -1, NULL );
00246 
00247     /* Convert to radians, use stock formula for angular separation,
00248        convert */
00249     double s_ra  = s->ra  * 2*M_PI / 360;
00250     double s_dec = s->dec * 2*M_PI / 360;
00251     double t_ra  = t->ra  * 2*M_PI / 360;
00252     double t_dec = t->dec * 2*M_PI / 360;
00253     
00254     double cos_separation = 
00255         sin(s_dec)*sin(t_dec) +  
00256         cos(s_dec)*cos(t_dec) * cos(s_ra - t_ra);
00257     
00258     if (cos_separation < -1) cos_separation = -1;
00259     if (cos_separation >  1) cos_separation =  1;
00260     
00261     /* Note: result is always positive */
00262     return (acos(cos_separation) * 360 / (2*M_PI)) * 3600;
00263 }
00264 
00265 
00266 #define LIST_DEFINE
00267 #undef LIST_ELEM
00268 #define LIST_ELEM fors_std_star
00269 #include <list.h>
00270 

Generated on Wed Sep 10 07:31:52 2008 for FORS Pipeline Reference Manual by  doxygen 1.4.6