00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00043
00044
00045
00046 static
00047 double _get_optional_table_value( const cpl_table *tab,
00048 unsigned int row,
00049 const char *colname);
00050 static
00051 double _square( double a);
00052
00053
00054
00055
00056
00057
00058 #undef cleanup
00059 #define cleanup
00060
00066
00067 static
00068 double _get_optional_table_value( const cpl_table *tab,
00069 unsigned int row,
00070 const char *colname)
00071 {
00072 int null;
00073 cpl_errorstate errstat = cpl_errorstate_get();
00074 cpl_error_code errc;
00075
00076 if (colname != NULL && colname[0] != '\0')
00077 {
00078 double d;
00079 d = cpl_table_get( tab, colname, row, &null);
00080 if (!cpl_errorstate_is_equal(errstat))
00081 {
00082 switch (errc = cpl_error_get_code())
00083 {
00084 case CPL_ERROR_DATA_NOT_FOUND:
00085 cpl_error_set_message( cpl_func,
00086 errc,
00087 "Column \"%s\" not found",
00088 colname);
00089 break;
00090 case CPL_ERROR_INVALID_TYPE:
00091 cpl_error_set_message( cpl_func,
00092 errc,
00093 "Column \"%s\" is not numeric",
00094 colname);
00095 break;
00096 default: break;
00097 }
00098 cleanup;
00099 }
00100 else
00101 {
00102 return d;
00103 }
00104 }
00105
00106 {
00107 int n;
00108 union _nant {
00109 unsigned char bytes[8];
00110 double val;
00111 } nan;
00112 for (n = 0; n < 8; n++)
00113 nan.bytes[n] = (unsigned char)255;
00114 return nan.val;
00115 }
00116 }
00117
00118
00124
00125 static
00126 double _square( double a)
00127 {
00128 return a*a;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138 #undef cleanup
00139 #define cleanup \
00140 do { \
00141 cpl_table_delete(t); \
00142 } while(0)
00143
00144
00161
00162 fors_std_star *fors_std_star_new( double ra, double dec,
00163 double m, double dm,
00164 double cat_m, double dcat_m,
00165 double col, double dcol,
00166 double cov_catm_col,
00167 const char *name)
00168 {
00169 fors_std_star *s = cpl_malloc(sizeof(*s));
00170
00171 s->ra = ra;
00172 s->dec = dec;
00173 s->magnitude = m;
00174 s->dmagnitude = dm;
00175 s->cat_magnitude = cat_m;
00176 s->dcat_magnitude = dcat_m;
00177 s->color = col;
00178 s->dcolor = dcol;
00179 s->cov_catm_color = cov_catm_col;
00180
00181 s->pixel = fors_point_new(-1, -1);
00182
00183 if (name != NULL) {
00184 s->name = cpl_strdup(name);
00185 }
00186 else {
00187 s->name = NULL;
00188 }
00189
00190 s->trusted = true;
00191
00192 return s;
00193 }
00194
00195
00196 #undef cleanup
00197 #define cleanup fors_std_star_delete(&s)
00198
00228
00229 fors_std_star *fors_std_star_new_from_table(
00230 const cpl_table *tab,
00231 unsigned int row,
00232 const char *ra_col,
00233 const char *dec_col,
00234 const char *mag_col,
00235 const char *dmag_col,
00236 const char *catmag_col,
00237 const char *dcatmag_col,
00238 const char *color_col,
00239 const char *dcolor_col,
00240 const char *cov_catm_color_col,
00241 const char *x_col,
00242 const char *y_col,
00243 const char *name_col)
00244 {
00245 cpl_errorstate errstat = cpl_errorstate_get();
00246 double x,
00247 y;
00248
00249 fors_std_star *s = cpl_malloc(sizeof(*s));
00250 s->name = NULL;
00251
00252 s->ra = _get_optional_table_value(tab, row, ra_col);
00253 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00254 s->dec = _get_optional_table_value(tab, row, dec_col);
00255 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00256 s->magnitude = _get_optional_table_value(tab, row, mag_col);
00257 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00258 s->dmagnitude = _get_optional_table_value(tab, row, dmag_col);
00259 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00260 s->cat_magnitude = _get_optional_table_value(tab, row, catmag_col);
00261 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00262 s->dcat_magnitude = _get_optional_table_value(tab, row, dcatmag_col);
00263 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00264 s->color = _get_optional_table_value(tab, row, color_col);
00265 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00266 s->dcolor = _get_optional_table_value(tab, row, dcolor_col);
00267 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00268 s->cov_catm_color = _get_optional_table_value(tab, row,
00269 cov_catm_color_col);
00270 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00271
00272 x = _get_optional_table_value(tab, row, x_col);
00273 y = _get_optional_table_value(tab, row, y_col);
00274 s->pixel = fors_point_new( (isnan(x) ? -1 : x),
00275 (isnan(y) ? -1 : y));
00276 assure(cpl_errorstate_is_equal(errstat), return s, NULL);
00277 if (s->pixel->x < 1) s->pixel->x = -1;
00278 if (s->pixel->y < 1) s->pixel->y = -1;
00279
00280 s->name = NULL;
00281 if (name_col != NULL)
00282 {
00283 const char *str;
00284 str = cpl_table_get_string(tab, name_col, row);
00285 if (!cpl_errorstate_is_equal(errstat))
00286 {
00287 cpl_error_code errc;
00288 switch (errc = cpl_error_get_code())
00289 {
00290 case CPL_ERROR_DATA_NOT_FOUND:
00291 cpl_error_set_message( cpl_func,
00292 errc,
00293 "Column \"%s\" not found",
00294 name_col);
00295 break;
00296 case CPL_ERROR_INVALID_TYPE:
00297 cpl_error_set_message( cpl_func,
00298 errc,
00299 "Column \"%s\" is not string type",
00300 name_col);
00301 break;
00302 default: break;
00303 }
00304 cleanup;
00305 return s;
00306 }
00307 if (str != NULL)
00308 s->name = cpl_strdup(str);
00309 }
00310
00311 s->trusted = true;
00312
00313 return s;
00314 }
00315
00316
00321
00322 void fors_std_star_delete( fors_std_star **s){
00323 if (s && *s) {
00324 fors_point_delete(&(*s)->pixel);
00325 if ((*s)->name != NULL) {
00326 cpl_free((void *)(*s)->name); (*s)->name = NULL;
00327 }
00328 cpl_free(*s); *s = NULL;
00329 }
00330 return;
00331 }
00332
00333
00338
00339 void fors_std_star_delete_const( const fors_std_star **s){
00340 fors_std_star_delete((fors_std_star **)s);
00341 return;
00342 }
00343
00344
00345 #undef cleanup
00346 #define cleanup
00347
00352
00353 fors_std_star *fors_std_star_duplicate( const fors_std_star *s)
00354 {
00355 fors_std_star *d = NULL;
00356
00357 assure( s != NULL, return NULL, NULL );
00358
00359 d = cpl_malloc(sizeof(*d));
00360
00361 d->ra = s->ra;
00362 d->dec = s->dec;
00363 d->magnitude = s->magnitude;
00364 d->dmagnitude = s->dmagnitude;
00365 d->cat_magnitude = s->cat_magnitude;
00366 d->dcat_magnitude = s->dcat_magnitude;
00367 d->color = s->color;
00368 d->dcolor = s->dcolor;
00369 d->cov_catm_color = s->cov_catm_color;
00370
00371 d->pixel = fors_point_duplicate(s->pixel);
00372 d->name = s->name != NULL ? cpl_strdup(s->name) : NULL;
00373
00374 d->trusted = s->trusted;
00375
00376 return d;
00377 }
00378
00379
00380 #undef cleanup
00381 #define cleanup
00382
00388
00389 void fors_std_star_set_name( fors_std_star *s,
00390 const char *name)
00391 {
00392 assure( s != NULL, return, NULL );
00393
00394 cpl_free(s->name);
00395 s->name = (name != NULL) ? cpl_strdup(name) : NULL;
00396
00397 return;
00398 }
00399
00400
00409
00410 bool fors_std_star_equal( const fors_std_star *s,
00411 const fors_std_star *t)
00412 {
00413 assure( s != NULL && t != NULL, return true, NULL );
00414
00415 return( s->trusted && t->trusted &&
00416 fabs(s->ra - t->ra ) < DBL_EPSILON &&
00417 fabs(s->dec - t->dec) < DBL_EPSILON);
00418 }
00419
00420
00421 #undef cleanup
00422 #define cleanup
00423
00428
00429 void fors_std_star_print( cpl_msg_severity level,
00430 const fors_std_star *star)
00431 {
00432 if (star == NULL) {
00433 fors_msg(level, "NULL std.star");
00434 }
00435 else {
00436 fors_msg(level, "(%7.4f, %7.4f): %sm = %g +- %g "
00437 "(col = %g +- %g)%s, (x=%7.2f, y=%7.2f) %s",
00438 star->ra, star->dec,
00439 (star->trusted ? "" : "untrusted magnitude (values are: "),
00440 star->magnitude, star->dmagnitude,
00441 star->color, star->dcolor,
00442 (star->trusted ? "" : ")"),
00443 star->pixel->x, star->pixel->y,
00444 ((star->name != NULL) ? star->name : ""));
00445 }
00446
00447 return;
00448 }
00449
00450
00451 #undef cleanup
00452 #define cleanup
00453
00458
00459 void fors_std_star_print_list( cpl_msg_severity level,
00460 const fors_std_star_list *sl)
00461 {
00462 if (sl == NULL) fors_msg(level, "Null list");
00463 else {
00464 const fors_std_star *s;
00465
00466 for (s = fors_std_star_list_first_const(sl);
00467 s != NULL;
00468 s = fors_std_star_list_next_const(sl)) {
00469
00470 fors_std_star_print(level, s);
00471 }
00472 }
00473 return;
00474 }
00475
00476
00484
00485 bool fors_std_star_brighter_than(const fors_std_star *s,
00486 const fors_std_star *t,
00487 void *data){
00488 data = data;
00489 return (s->trusted && t->trusted &&
00490 s->magnitude < t->magnitude);
00491 }
00492
00493
00494 #undef cleanup
00495 #define cleanup
00496
00502
00503 double fors_std_star_dist_arcsec( const fors_std_star *s,
00504 const fors_std_star *t)
00505 {
00506 assure( s != NULL, return -1, NULL );
00507 assure( t != NULL, return -1, NULL );
00508
00509
00510
00511 double s_ra = s->ra * 2*M_PI / 360;
00512 double s_dec = s->dec * 2*M_PI / 360;
00513 double t_ra = t->ra * 2*M_PI / 360;
00514 double t_dec = t->dec * 2*M_PI / 360;
00515
00516 double cos_separation =
00517 sin(s_dec)*sin(t_dec) +
00518 cos(s_dec)*cos(t_dec) * cos(s_ra - t_ra);
00519
00520 if (cos_separation < -1) cos_separation = -1;
00521 if (cos_separation > 1) cos_separation = 1;
00522
00523
00524 return (acos(cos_separation) * 360 / (2*M_PI)) * 3600;
00525 }
00526
00527
00537
00538 void fors_std_star_compute_corrected_mag(
00539 fors_std_star *s,
00540 double color_term,
00541 double dcolor_term)
00542 {
00543 cassure( s != NULL, CPL_ERROR_NULL_INPUT, return, NULL);
00544
00545 s->magnitude = s->cat_magnitude - color_term * s->color;
00546 s->dmagnitude = sqrt( _square(s->dcat_magnitude)
00547 - 2.0 * color_term * s->cov_catm_color
00548 + _square(color_term * s->dcolor)
00549 + _square(s->color * dcolor_term));
00550 }
00551
00552
00553 #define LIST_DEFINE
00554 #undef LIST_ELEM
00555 #define LIST_ELEM fors_std_star
00556 #include <list.h>
00557