| 1 |
/* Utility programs for unit conversions and calendar |
| 2 |
*/ |
| 3 |
|
| 4 |
/*double PI = 3.14159265358979323846;*/ |
| 5 |
#include "kep.h" |
| 6 |
#if __STDC__ |
| 7 |
double caltoj (long, int, double); |
| 8 |
#else |
| 9 |
double caltoj(); |
| 10 |
#endif |
| 11 |
|
| 12 |
char *intfmt = "%d"; |
| 13 |
char *lngfmt = "%ld"; |
| 14 |
char *dblfmt = "%lf"; |
| 15 |
char *strfmt = "%s"; |
| 16 |
|
| 17 |
/* Display Right Ascension and Declination |
| 18 |
* from input equatorial rectangular unit vector. |
| 19 |
* Output vector pol[] contains R.A., Dec., and radius. |
| 20 |
*/ |
| 21 |
int showrd( msg, p, pol ) |
| 22 |
char *msg; |
| 23 |
double p[], pol[]; |
| 24 |
{ |
| 25 |
double x, y, r; |
| 26 |
int i; |
| 27 |
|
| 28 |
r = 0.0; |
| 29 |
for( i=0; i<3; i++ ) |
| 30 |
{ |
| 31 |
x = p[i]; |
| 32 |
r += x * x; |
| 33 |
} |
| 34 |
r = sqrt(r); |
| 35 |
|
| 36 |
x = zatan2( p[0], p[1] ); |
| 37 |
pol[0] = x; |
| 38 |
|
| 39 |
y = asin( p[2]/r ); |
| 40 |
pol[1] = y; |
| 41 |
|
| 42 |
pol[2] = r; |
| 43 |
|
| 44 |
if (prtflg != 0) |
| 45 |
{ |
| 46 |
printf( "%s R.A. ", msg ); |
| 47 |
hms( x ); |
| 48 |
printf( "Dec. " ); |
| 49 |
dms( y ); |
| 50 |
printf( "\n" ); |
| 51 |
} |
| 52 |
return(0); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
/* Display magnitude of correction vector |
| 59 |
* in arc seconds |
| 60 |
*/ |
| 61 |
int showcor( strng, p, dp ) |
| 62 |
char *strng; |
| 63 |
double p[], dp[]; |
| 64 |
{ |
| 65 |
double p1[3], dr, dd; |
| 66 |
int i; |
| 67 |
|
| 68 |
if( prtflg == 0 ) |
| 69 |
return(0); |
| 70 |
|
| 71 |
for( i=0; i<3; i++ ) |
| 72 |
p1[i] = p[i] + dp[i]; |
| 73 |
|
| 74 |
deltap( p, p1, &dr, &dd ); |
| 75 |
printf( "%s dRA %.3fs dDec %.2f\"\n", strng, RTS*dr/15.0, RTS*dd ); |
| 76 |
return(0); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
/* Radians to degrees, minutes, seconds |
| 82 |
*/ |
| 83 |
int dms( x ) |
| 84 |
double x; |
| 85 |
{ |
| 86 |
double s; |
| 87 |
int d, m; |
| 88 |
|
| 89 |
s = x * RTD; |
| 90 |
if( s < 0.0 ) |
| 91 |
{ |
| 92 |
printf( " -" ); |
| 93 |
s = -s; |
| 94 |
} |
| 95 |
else |
| 96 |
printf( " " ); |
| 97 |
d = (int) s; |
| 98 |
s -= d; |
| 99 |
s *= 60; |
| 100 |
m = (int) s; |
| 101 |
s -= m; |
| 102 |
s *= 60; |
| 103 |
printf( "%3dd %02d\' %05.2f\" ", d, m, s ); |
| 104 |
return(0); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
/* Radians to hours, minutes, seconds |
| 111 |
*/ |
| 112 |
#define RTOH (12.0/PI) |
| 113 |
|
| 114 |
int hms( x ) |
| 115 |
double x; |
| 116 |
{ |
| 117 |
int h, m; |
| 118 |
long sint, sfrac; |
| 119 |
double s; |
| 120 |
|
| 121 |
s = x * RTOH; |
| 122 |
if( s < 0.0 ) |
| 123 |
s += 24.0; |
| 124 |
h = (int) s; |
| 125 |
s -= h; |
| 126 |
s *= 60; |
| 127 |
m = (int) s; |
| 128 |
s -= m; |
| 129 |
s *= 60; |
| 130 |
/* Handle shillings and pence roundoff. */ |
| 131 |
sfrac = (long) (1000.0 * s + 0.5); |
| 132 |
if( sfrac >= 60000L ) |
| 133 |
{ |
| 134 |
sfrac -= 60000L; |
| 135 |
m += 1; |
| 136 |
if( m >= 60 ) |
| 137 |
{ |
| 138 |
m -= 60; |
| 139 |
h += 1; |
| 140 |
} |
| 141 |
} |
| 142 |
sint = sfrac / 1000; |
| 143 |
sfrac -= sint * 1000; |
| 144 |
printf( "%3dh %02dm %02ld.%03lds ", h, m, sint, sfrac ); |
| 145 |
return(0); |
| 146 |
} |
| 147 |
|
| 148 |
/* julian.c |
| 149 |
* |
| 150 |
* This program calculates Julian day number from calendar |
| 151 |
* date, and the date and day of the week from Julian day. |
| 152 |
* The Julian date is double precision floating point |
| 153 |
* with the origin used by astronomers. The calendar output |
| 154 |
* converts fractions of a day into hours, minutes, and seconds. |
| 155 |
* There is no year 0. Enter B.C. years as negative; i.e., |
| 156 |
* 2 B.C. = -2. |
| 157 |
* |
| 158 |
* The approximate range of dates handled is 4713 B.C. to |
| 159 |
* 54,078 A.D. This should be adequate for most applications. |
| 160 |
* |
| 161 |
* B.C. dates are calculated by extending the Gregorian sequence |
| 162 |
* of leap years and century years into the past. This seems |
| 163 |
* the only sensible definition, but I don't know if it is |
| 164 |
* the official one. |
| 165 |
* |
| 166 |
* Note that the astronomical Julian day starts at noon on |
| 167 |
* the previous calendar day. Thus at midnight in the morning |
| 168 |
* of the present calendar day the Julian date ends in .5; |
| 169 |
* it rolls over to tomorrow at noon today. |
| 170 |
* |
| 171 |
* The month finding algorithm is attributed to Meeus. |
| 172 |
* |
| 173 |
* - Steve Moshier |
| 174 |
*/ |
| 175 |
|
| 176 |
|
| 177 |
char *months[12] = { |
| 178 |
"January", |
| 179 |
"February", |
| 180 |
"March", |
| 181 |
"April", |
| 182 |
"May", |
| 183 |
"June", |
| 184 |
"July", |
| 185 |
"August", |
| 186 |
"September", |
| 187 |
"October", |
| 188 |
"November", |
| 189 |
"December" |
| 190 |
}; |
| 191 |
|
| 192 |
char *days[7] = { |
| 193 |
"Sunday", |
| 194 |
"Monday", |
| 195 |
"Tuesday", |
| 196 |
"Wednesday", |
| 197 |
"Thursday", |
| 198 |
"Friday", |
| 199 |
"Saturday" |
| 200 |
}; |
| 201 |
|
| 202 |
long cyear = 1986L; |
| 203 |
extern long cyear; |
| 204 |
static int month = 1; |
| 205 |
static double day = 1.0; |
| 206 |
short yerend = 0; |
| 207 |
extern short yerend; |
| 208 |
|
| 209 |
double zgetdate() |
| 210 |
{ |
| 211 |
double J; |
| 212 |
|
| 213 |
/* Get operator to type in a date. |
| 214 |
*/ |
| 215 |
getnum( "Calendar date: Year", &cyear, lngfmt ); |
| 216 |
|
| 217 |
if( (cyear > 53994L) || (cyear < -4713L) ) |
| 218 |
{ |
| 219 |
printf( "Year out of range.\n" ); |
| 220 |
goto err; |
| 221 |
} |
| 222 |
if( cyear == 0 ) |
| 223 |
{ |
| 224 |
printf( "There is no year 0.\n" ); |
| 225 |
err: J = 0.0; |
| 226 |
goto pdate; |
| 227 |
} |
| 228 |
|
| 229 |
getnum( "Month (1-12)", &month, intfmt); |
| 230 |
getnum( "Day.fraction", &day, dblfmt ); |
| 231 |
|
| 232 |
/* Find the Julian day. */ |
| 233 |
J = caltoj(cyear,month,day); |
| 234 |
/*printf( "Julian day %.1f\n", J );*/ |
| 235 |
|
| 236 |
pdate: |
| 237 |
/* Convert back to calendar date. */ |
| 238 |
/* jtocal( J ); */ |
| 239 |
return(J); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
/* Calculate Julian day from Gregorian calendar date |
| 245 |
*/ |
| 246 |
double caltoj( year, month, day ) |
| 247 |
long year; |
| 248 |
int month; |
| 249 |
double day; |
| 250 |
{ |
| 251 |
long y, a, b, c, e, m; |
| 252 |
double J; |
| 253 |
|
| 254 |
|
| 255 |
/* The origin should be chosen to be a century year |
| 256 |
* that is also a leap year. We pick 4801 B.C. |
| 257 |
*/ |
| 258 |
y = year + 4800; |
| 259 |
if( year < 0 ) |
| 260 |
{ |
| 261 |
y += 1; |
| 262 |
} |
| 263 |
|
| 264 |
/* The following magic arithmetic calculates a sequence |
| 265 |
* whose successive terms differ by the correct number of |
| 266 |
* days per calendar month. It starts at 122 = March; January |
| 267 |
* and February come after December. |
| 268 |
*/ |
| 269 |
m = month; |
| 270 |
if( m <= 2 ) |
| 271 |
{ |
| 272 |
m += 12; |
| 273 |
y -= 1; |
| 274 |
} |
| 275 |
e = (306 * (m+1))/10; |
| 276 |
|
| 277 |
a = y/100; /* number of centuries */ |
| 278 |
if( year <= 1582L ) |
| 279 |
{ |
| 280 |
if( year == 1582L ) |
| 281 |
{ |
| 282 |
if( month < 10 ) |
| 283 |
goto julius; |
| 284 |
if( month > 10) |
| 285 |
goto gregor; |
| 286 |
if( day >= 15 ) |
| 287 |
goto gregor; |
| 288 |
} |
| 289 |
julius: |
| 290 |
printf( " Julian Calendar assumed.\n" ); |
| 291 |
b = -38; |
| 292 |
} |
| 293 |
else |
| 294 |
{ /* -number of century years that are not leap years */ |
| 295 |
gregor: |
| 296 |
b = (a/4) - a; |
| 297 |
} |
| 298 |
|
| 299 |
c = (36525L * y)/100; /* Julian calendar years and leap years */ |
| 300 |
|
| 301 |
/* Add up these terms, plus offset from J 0 to 1 Jan 4801 B.C. |
| 302 |
* Also fudge for the 122 days from the month algorithm. |
| 303 |
*/ |
| 304 |
J = b + c + e + day - 32167.5; |
| 305 |
return( J ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
/* Calculate month, day, and year from Julian date */ |
| 312 |
|
| 313 |
int jtocal( J ) |
| 314 |
double J; |
| 315 |
{ |
| 316 |
int month, day; |
| 317 |
long year, a, c, d, x, y, jd; |
| 318 |
int BC; |
| 319 |
double dd; |
| 320 |
|
| 321 |
if( J < 1721425.5 ) /* January 1.0, 1 A.D. */ |
| 322 |
BC = 1; |
| 323 |
else |
| 324 |
BC = 0; |
| 325 |
|
| 326 |
jd = (long) (J + 0.5); /* round Julian date up to integer */ |
| 327 |
|
| 328 |
/* Find the number of Gregorian centuries |
| 329 |
* since March 1, 4801 B.C. |
| 330 |
*/ |
| 331 |
a = (100*jd + 3204500L)/3652425L; |
| 332 |
|
| 333 |
/* Transform to Julian calendar by adding in Gregorian century years |
| 334 |
* that are not leap years. |
| 335 |
* Subtract 97 days to shift origin of JD to March 1. |
| 336 |
* Add 122 days for magic arithmetic algorithm. |
| 337 |
* Add four years to ensure the first leap year is detected. |
| 338 |
*/ |
| 339 |
c = jd + 1486; |
| 340 |
if( jd >= 2299160.5 ) |
| 341 |
c += a - a/4; |
| 342 |
else |
| 343 |
c += 38; |
| 344 |
/* Offset 122 days, which is where the magic arithmetic |
| 345 |
* month formula sequence starts (March 1 = 4 * 30.6 = 122.4). |
| 346 |
*/ |
| 347 |
d = (100*c - 12210L)/36525L; |
| 348 |
/* Days in that many whole Julian years */ |
| 349 |
x = (36525L * d)/100L; |
| 350 |
|
| 351 |
/* Find month and day. */ |
| 352 |
y = ((c-x)*100L)/3061L; |
| 353 |
day = (int) (c - x - ((306L*y)/10L)); |
| 354 |
month = (int) (y - 1); |
| 355 |
if( y > 13 ) |
| 356 |
month -= 12; |
| 357 |
|
| 358 |
/* Get the year right. */ |
| 359 |
year = d - 4715; |
| 360 |
if( month > 2 ) |
| 361 |
year -= 1; |
| 362 |
|
| 363 |
/* Day of the week. */ |
| 364 |
a = (jd + 1) % 7; |
| 365 |
|
| 366 |
/* Fractional part of day. */ |
| 367 |
dd = day + J - jd + 0.5; |
| 368 |
|
| 369 |
/* post the year. */ |
| 370 |
cyear = year; |
| 371 |
|
| 372 |
if( BC ) |
| 373 |
{ |
| 374 |
year = -year + 1; |
| 375 |
cyear = -year; |
| 376 |
if( prtflg ) |
| 377 |
printf( "%ld B.C. ", year ); |
| 378 |
} |
| 379 |
else |
| 380 |
{ |
| 381 |
if( prtflg ) |
| 382 |
printf( "%ld ", year ); |
| 383 |
} |
| 384 |
day = (int) dd; |
| 385 |
if( prtflg ) |
| 386 |
printf( "%s %d %s", months[month-1], day, days[(int) a] ); |
| 387 |
|
| 388 |
/* Flag last or first day of year */ |
| 389 |
if( ((month == 1) && (day == 1)) |
| 390 |
|| ((month == 12) && (day == 31)) ) |
| 391 |
yerend = 1; |
| 392 |
else |
| 393 |
yerend = 0; |
| 394 |
|
| 395 |
/* Display fraction of calendar day |
| 396 |
* as clock time. |
| 397 |
*/ |
| 398 |
a = (long) dd; |
| 399 |
dd = dd - a; |
| 400 |
if( prtflg ) |
| 401 |
{ |
| 402 |
hms( 2.0*PI*dd ); |
| 403 |
if( J == TDT ) |
| 404 |
printf( "TDT\n" ); /* Indicate Terrestrial Dynamical Time */ |
| 405 |
else if( J == UT ) |
| 406 |
printf( "UT\n" ); /* Universal Time */ |
| 407 |
else |
| 408 |
printf( "\n" ); |
| 409 |
} |
| 410 |
return(0); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
/* Reduce x modulo 360 degrees |
| 415 |
*/ |
| 416 |
double mod360(x) |
| 417 |
double x; |
| 418 |
{ |
| 419 |
long k; |
| 420 |
double y; |
| 421 |
|
| 422 |
k = (long) (x/360.0); |
| 423 |
y = x - k * 360.0; |
| 424 |
while( y < 0.0 ) |
| 425 |
y += 360.0; |
| 426 |
while( y > 360.0 ) |
| 427 |
y -= 360.0; |
| 428 |
return(y); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
/* Reduce x modulo 2 pi |
| 435 |
*/ |
| 436 |
#define TPI (2.0*PI) |
| 437 |
double modtp(x) |
| 438 |
double x; |
| 439 |
{ |
| 440 |
double y; |
| 441 |
|
| 442 |
y = floor( x/TPI ); |
| 443 |
y = x - y * TPI; |
| 444 |
while( y < 0.0 ) |
| 445 |
y += TPI; |
| 446 |
while( y >= TPI ) |
| 447 |
y -= TPI; |
| 448 |
return(y); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
/* Get operator to type in hours, minutes, and seconds |
| 453 |
*/ |
| 454 |
static int hours = 0; |
| 455 |
static int minutes = 0; |
| 456 |
static double seconds = 0.0; |
| 457 |
|
| 458 |
double gethms() |
| 459 |
{ |
| 460 |
double t; |
| 461 |
|
| 462 |
getnum( "Time: Hours", &hours, intfmt ); |
| 463 |
getnum( "Minutes", &minutes, intfmt ); |
| 464 |
getnum( "Seconds", &seconds, dblfmt ); |
| 465 |
t = (3600.0*hours + 60.0*minutes + seconds)/86400.0; |
| 466 |
return(t); |
| 467 |
} |
| 468 |
|
| 469 |
int getnum( msg, num, format ) |
| 470 |
char *msg; |
| 471 |
void *num; |
| 472 |
char *format; |
| 473 |
{ |
| 474 |
char s[40]; |
| 475 |
|
| 476 |
printf( "%s (", msg ); |
| 477 |
if( format == strfmt ) |
| 478 |
printf( format, (char *) num ); |
| 479 |
else if( format == dblfmt ) |
| 480 |
printf( format, *(double *)num ); |
| 481 |
else if( format == intfmt ) |
| 482 |
printf( format, *(int *)num ); |
| 483 |
else if( format == lngfmt ) |
| 484 |
printf( format, *(long *)num ); |
| 485 |
else |
| 486 |
printf( "Illegal input format\n" ); |
| 487 |
printf( ") ? "); |
| 488 |
gets(s); |
| 489 |
if( s[0] != '\0' ) |
| 490 |
sscanf( s, format, num ); |
| 491 |
return(0); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
/* |
| 497 |
* Convert change in rectangular coordinatates to change |
| 498 |
* in right ascension and declination. |
| 499 |
* For changes greater than about 0.1 degree, the |
| 500 |
* coordinates are converted directly to R.A. and Dec. |
| 501 |
* and the results subtracted. For small changes, |
| 502 |
* the change is calculated to first order by differentiating |
| 503 |
* tan(R.A.) = y/x |
| 504 |
* to obtain |
| 505 |
* dR.A./cos**2(R.A.) = dy/x - y dx/x**2 |
| 506 |
* where |
| 507 |
* cos**2(R.A.) = 1/(1 + (y/x)**2). |
| 508 |
* |
| 509 |
* The change in declination arcsin(z/R) is |
| 510 |
* d asin(u) = du/sqrt(1-u**2) |
| 511 |
* where u = z/R. |
| 512 |
* |
| 513 |
* p0 is the initial object - earth vector and |
| 514 |
* p1 is the vector after motion or aberration. |
| 515 |
* |
| 516 |
*/ |
| 517 |
|
| 518 |
int deltap( p0, p1, dr, dd ) |
| 519 |
double p0[], p1[]; |
| 520 |
double *dr, *dd; |
| 521 |
{ |
| 522 |
double dp[3], A, B, P, Q, x, y, z; |
| 523 |
int i; |
| 524 |
|
| 525 |
P = 0.0; |
| 526 |
Q = 0.0; |
| 527 |
z = 0.0; |
| 528 |
for( i=0; i<3; i++ ) |
| 529 |
{ |
| 530 |
x = p0[i]; |
| 531 |
y = p1[i]; |
| 532 |
P += x * x; |
| 533 |
Q += y * y; |
| 534 |
y = y - x; |
| 535 |
dp[i] = y; |
| 536 |
z += y*y; |
| 537 |
} |
| 538 |
|
| 539 |
A = sqrt(P); |
| 540 |
B = sqrt(Q); |
| 541 |
|
| 542 |
if( (A < 1.e-7) || (B < 1.e-7) || (z/(P+Q)) > 5.e-7 ) |
| 543 |
{ |
| 544 |
P = zatan2( p0[0], p0[1] ); |
| 545 |
Q = zatan2( p1[0], p1[1] ); |
| 546 |
Q = Q - P; |
| 547 |
while( Q < -PI ) |
| 548 |
Q += 2.0*PI; |
| 549 |
while( Q > PI ) |
| 550 |
Q -= 2.0*PI; |
| 551 |
*dr = Q; |
| 552 |
P = asin( p0[2]/A ); |
| 553 |
Q = asin( p1[2]/B ); |
| 554 |
*dd = Q - P; |
| 555 |
return(0); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
x = p0[0]; |
| 560 |
y = p0[1]; |
| 561 |
if( x == 0.0 ) |
| 562 |
{ |
| 563 |
*dr = 1.0e38; |
| 564 |
} |
| 565 |
else |
| 566 |
{ |
| 567 |
Q = y/x; |
| 568 |
Q = (dp[1] - dp[0]*y/x)/(x * (1.0 + Q*Q)); |
| 569 |
*dr = Q; |
| 570 |
} |
| 571 |
|
| 572 |
x = p0[2]/A; |
| 573 |
P = sqrt( 1.0 - x*x ); |
| 574 |
*dd = (p1[2]/B - x)/P; |
| 575 |
return(0); |
| 576 |
} |