fors_star.c

00001 /* $Id: fors_star.c,v 1.29 2010/09/14 07:49:30 cizzo Exp $
00002  *
00003  * This file is part of the FORS Library
00004  * Copyright (C) 2002-2010 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: 2010/09/14 07:49:30 $
00024  * $Revision: 1.29 $
00025  * $Name: fors-4_8_6 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 #include <fors_star.h>
00033 
00034 #include <fors_utils.h>
00035 
00036 #include <cpl.h>
00037 
00038 #include <math.h>
00039 #include <assert.h>
00040 
00049 /*-----------------------------------------------------------------------------
00050     Prototypes
00051  -----------------------------------------------------------------------------*/
00052 
00053 static
00054 double  _get_optional_table_value(  const cpl_table *tab,
00055                                     unsigned int    row,
00056                                     const char      *colname);
00057 
00058 /*-----------------------------------------------------------------------------
00059     Private Implementation
00060  -----------------------------------------------------------------------------*/
00061 
00062 /*----------------------------------------------------------------------------*/
00063 #undef cleanup
00064 #define cleanup
00065 
00071 /*----------------------------------------------------------------------------*/
00072 static
00073 double  _get_optional_table_value(  const cpl_table *tab,
00074                                     unsigned int    row,
00075                                     const char      *colname)
00076 {
00077     int null;
00078     if (colname != NULL && colname[0] != '\0')
00079     {
00080         double  d;
00081         d = cpl_table_get(          tab, colname, row, &null);
00082         assure(                     !cpl_error_get_code(),
00083                                     return d,
00084                                     "Missing column: %s",
00085                                         colname);
00086         return d;
00087             
00088     }
00089     else
00090         return 0.0;
00091 }
00092 
00093 /*-----------------------------------------------------------------------------
00094     Implementation
00095  -----------------------------------------------------------------------------*/
00096 
00097 /*----------------------------------------------------------------------------*/
00098 #undef cleanup
00099 #define cleanup
00100 
00113 /*----------------------------------------------------------------------------*/
00114 fors_star *fors_star_new(double x, double y,
00115                          double fwhm,
00116                          double smajor, double sminor,
00117                          double orientation,
00118                          double m, double dm,
00119                          double si)
00120 {
00121     assure( smajor >= sminor && sminor >= 0, return NULL,
00122             "Illegal semi major/minor axes: %g, %g",
00123             smajor, sminor );
00124 
00125     assure( 0 <= si && si <= 1, return NULL,
00126             "Stellarity index must be between 0 and 1, is %f",
00127             si);
00128 
00129     assure( fwhm >= 0, return NULL,
00130             "Star FWHM must be non-negative, is %f",
00131             fwhm);
00132 
00133     fors_star *s = cpl_malloc(sizeof(*s));
00134 
00135     s->pixel = fors_point_new(x, y);
00136     s->fwhm = fwhm;
00137     s->semi_major = smajor;
00138     s->semi_minor = sminor;
00139     s->stellarity_index = si;
00140     s->orientation = orientation;
00141     s->magnitude = m;
00142     s->dmagnitude = dm;
00143     s->magnitude_corr = 0;
00144     s->dmagnitude_corr = 0;
00145     s->id = NULL;
00146     s->weight = 0;
00147 
00148     return s;
00149 }
00150 
00151 /*----------------------------------------------------------------------------*/
00152 #undef cleanup
00153 #define cleanup fors_star_delete(&s)
00154 
00169 /*----------------------------------------------------------------------------*/
00170 fors_star *fors_star_new_from_table(const cpl_table *tab,
00171                                     unsigned int    row,
00172                                     const char      *x_col,
00173                                     const char      *y_col,
00174                                     const char      *fwhm_col,
00175                                     const char      *smaj_col,
00176                                     const char      *smin_col,
00177                                     const char      *theta_col,
00178                                     const char      *mag_col,
00179                                     const char      *dmag_col,
00180                                     const char      *stlndx_col)
00181 {
00182     int null;
00183     fors_star *s = cpl_malloc(sizeof(*s));
00184 
00185     s->pixel = fors_point_new(  _get_optional_table_value(tab, row, x_col),
00186                                 _get_optional_table_value(tab, row, y_col));
00187     assure(!cpl_error_get_code(), return s, NULL);
00188     
00189     s->fwhm =                   _get_optional_table_value(tab, row, fwhm_col);
00190     assure(!cpl_error_get_code(), return s, NULL);
00191     
00192     s->semi_major =             _get_optional_table_value(tab, row, smaj_col);
00193     assure(!cpl_error_get_code(), return s, NULL);
00194     
00195     s->semi_minor =             _get_optional_table_value(tab, row, smin_col);
00196     assure(!cpl_error_get_code(), return s, NULL);
00197     
00198     s->stellarity_index =       _get_optional_table_value(tab, row, stlndx_col);
00199     assure(!cpl_error_get_code(), return s, NULL);
00200     
00201     s->orientation =            _get_optional_table_value(tab, row, theta_col);
00202     assure(!cpl_error_get_code(), return s, NULL);
00203     
00204     s->magnitude =              _get_optional_table_value(tab, row, mag_col);
00205     assure(!cpl_error_get_code(), return s, NULL);
00206     
00207     s->dmagnitude =             _get_optional_table_value(tab, row, dmag_col);
00208     assure(!cpl_error_get_code(), return s, NULL);
00209     
00210     s->magnitude_corr = 0;
00211     s->dmagnitude_corr = 0;
00212     s->id = NULL;
00213     s->weight = 0;
00214     
00215     return s;
00216 }
00217 
00218 /*----------------------------------------------------------------------------*/
00219 #undef cleanup
00220 #define cleanup
00221 
00226 /*----------------------------------------------------------------------------*/
00227 bool fors_star_check_values(        const fors_star *star)
00228 {
00229     bool    success = 1;
00230     
00231     success &= (star != NULL);
00232     success &= (star->semi_major >= star->semi_minor);
00233     success &= (0.0 <= star->stellarity_index && star->stellarity_index <= 1.0);
00234     success &= (star->fwhm > 0.0);
00235     
00236     return success;
00237 }
00238 
00239 /*----------------------------------------------------------------------------*/
00240 #undef cleanup
00241 #define cleanup
00242 
00247 /*----------------------------------------------------------------------------*/
00248 fors_star *fors_star_duplicate(const fors_star *star)
00249 {
00250     fors_star *d;
00251 
00252     assure( star != NULL, return NULL, NULL );
00253 
00254     d = cpl_malloc(sizeof(*d));
00255     *d = *star;  /* Simple copy of all members, next do
00256                     deep copy of pointers */
00257     
00258     d->pixel = fors_point_duplicate(star->pixel);
00259 
00260     if (star->id != NULL) {
00261         d->id = fors_std_star_duplicate(star->id);
00262     }
00263     
00264     return d;
00265 }
00266 
00267 /*----------------------------------------------------------------------------*/
00272 /*----------------------------------------------------------------------------*/
00273 void fors_star_delete(fors_star **star)
00274 {
00275     if (star && *star) {
00276         fors_point_delete(&(*star)->pixel);
00277         if ((*star)->id != NULL) {
00278             fors_std_star_delete_const(&((*star)->id));
00279         }
00280         cpl_free(*star); *star = NULL;
00281     }
00282     return;
00283 }
00284 
00285 /*----------------------------------------------------------------------------*/
00290 /*----------------------------------------------------------------------------*/
00291 void fors_star_delete_but_standard(fors_star **star)
00292 {
00293     if (star && *star) {
00294         fors_point_delete(&(*star)->pixel);
00295         cpl_free(*star); *star = NULL;
00296     }
00297     return;
00298 }
00299 
00300 /*----------------------------------------------------------------------------*/
00307 /*----------------------------------------------------------------------------*/
00308 bool
00309 fors_star_equal(const fors_star *s,
00310                 const fors_star *t)
00311 {
00312     assure( s != NULL && t != NULL, return true, NULL );
00313     
00314     return (fors_point_equal(s->pixel, t->pixel));
00315 }
00316 
00317 /*----------------------------------------------------------------------------*/
00318 #undef clenaup
00319 #define cleanup
00320 
00327 /*----------------------------------------------------------------------------*/
00328 bool
00329 fors_star_brighter_than(const fors_star *s1,
00330                         const fors_star *s2,
00331                         void *data)
00332 {
00333     data = data;
00334     return (s1->magnitude < s2->magnitude);
00335 }
00336 
00337 /*----------------------------------------------------------------------------*/
00338 #undef cleanup
00339 #define cleanup
00340 
00346 /*----------------------------------------------------------------------------*/
00347 double fors_star_distsq(const fors_star *s, const fors_star *t)
00348 {
00349     assure( s != NULL, return 0, NULL );
00350     assure( t != NULL, return 0, NULL );
00351 
00352     return fors_point_distsq(s->pixel, t->pixel);
00353 }
00354 
00355 /*----------------------------------------------------------------------------*/
00356 #undef cleanup
00357 #define cleanup
00358 
00364 /*----------------------------------------------------------------------------*/
00365 double fors_star_extension(const fors_star *s, void *data)
00366 {
00367     assure( s != NULL, return -1, NULL );
00368     data = data;
00369 
00370     /* return sqrt(s->semi_major * s->semi_minor); */
00371     return s->fwhm / TWOSQRT2LN2;
00372 }
00373 
00374 /*----------------------------------------------------------------------------*/
00375 #undef cleanup
00376 #define cleanup
00377 
00383 /*----------------------------------------------------------------------------*/
00384 double fors_star_stellarity(const fors_star *s, void *data)
00385 {
00386     assure( s != NULL, return -1, NULL );
00387     data = data;
00388 
00389     return s->stellarity_index;
00390 }
00391 
00392 /*----------------------------------------------------------------------------*/
00393 #undef cleanup
00394 #define cleanup
00395 
00401 /*----------------------------------------------------------------------------*/
00402 double fors_star_ellipticity(const fors_star *s, void *data)
00403 {
00404     assure( s != NULL, return -1, NULL );
00405     data = data;
00406     
00407     if (s->semi_major <= 0) return 1;
00408     else return 1 - (s->semi_minor / s->semi_major);
00409 }
00410 
00411 /*----------------------------------------------------------------------------*/
00417 /*----------------------------------------------------------------------------*/
00418 void fors_star_print(cpl_msg_severity level, const fors_star *s)
00419 {
00420     if (s == NULL) {
00421         fors_msg(level, "[NULL]");
00422     }
00423     else {
00424         fors_msg(level, "at (%7.2f, %7.2f): m = %g +- %g (mc = %g +- %g), "
00425                  "shape: (%g, %g, %g)",
00426                  s->pixel->x, s->pixel->y,
00427                  s->magnitude, s->dmagnitude,
00428                  s->magnitude_corr, s->dmagnitude_corr,
00429                  s->orientation, s->semi_major, s->semi_minor);
00430     }
00431 
00432     return;
00433 }
00434 
00435 /*----------------------------------------------------------------------------*/
00441 /*----------------------------------------------------------------------------*/
00442 void
00443 fors_star_print_list(cpl_msg_severity level, const fors_star_list *sl)
00444 {
00445     if (sl == NULL) fors_msg(level, "Null list");
00446     else {
00447         const fors_star *s;
00448         
00449         for (s = fors_star_list_first_const(sl);
00450              s != NULL;
00451              s = fors_star_list_next_const(sl)) {
00452             fors_star_print(level, s);
00453         }
00454     }
00455     return;
00456 }
00457 
00458 /*----------------------------------------------------------------------------*/
00465 /*----------------------------------------------------------------------------*/
00466 double
00467 fors_star_get_x(const fors_star *s, void *data)
00468 {
00469     assure( s != NULL, return -1, NULL );
00470 
00471     data = data;
00472 
00473     return s->pixel->x;
00474 }
00475 
00476 /*----------------------------------------------------------------------------*/
00483 /*----------------------------------------------------------------------------*/
00484 double
00485 fors_star_get_y(const fors_star *s, void *data)
00486 {
00487     assure( s != NULL, return -1, NULL );
00488 
00489     data = data;
00490 
00491     return s->pixel->y;
00492 }
00493 
00494 
00495 /*----------------------------------------------------------------------------*/
00502 /*----------------------------------------------------------------------------*/
00503 double
00504 fors_star_get_zeropoint(const fors_star *s, void *data)
00505 {
00506     assure( s     != NULL, return 0, NULL );
00507     assure( s->id != NULL, return 0, NULL );
00508     data = data;
00509 
00510     return (s->id->magnitude - s->magnitude_corr);
00511 }
00512 
00513 /*----------------------------------------------------------------------------*/
00520 /*----------------------------------------------------------------------------*/
00521 double
00522 fors_star_get_zeropoint_err(const fors_star *s, void *data)
00523 {
00524     assure( s     != NULL, return 0, NULL );
00525     assure( s->id != NULL, return 0, NULL );
00526     data = data;
00527     
00528     return sqrt(s->dmagnitude_corr * s->dmagnitude_corr +
00529                 s->id->dmagnitude * s->id->dmagnitude);
00530 }
00531 
00532 /*----------------------------------------------------------------------------*/
00539 /*----------------------------------------------------------------------------*/
00540 bool
00541 fors_star_is_identified(const fors_star *s, void *data)
00542 {
00543     data = data;
00544     assure( s     != NULL, return 0, NULL );
00545     return (s->id != NULL && s->id->trusted);
00546 }
00547 
00548 #define LIST_DEFINE
00549 #undef LIST_ELEM
00550 #define LIST_ELEM fors_star
00551 #include <list.h>
00552 

Generated on Fri Mar 4 09:46:01 2011 for FORS Pipeline Reference Manual by  doxygen 1.4.7