/* Utility programs for unit conversions and calendar */ static double PI = 3.14159265358979323846; #include "/usr/local/wiro/tracking/wirotypes.h" #include "/usr/local/wiro/tracking/wiro.h" extern struct wiro_memory *tinfo; char *intfmt = "%d"; char *lngfmt = "%ld"; char *dblfmt = "%lf"; char *strfmt = "%s"; /* julian.c * * This program calculates Julian day number from calendar * date, and the date and day of the week from Julian day. * The Julian date is double precision floating point * with the origin used by astronomers. The calendar output * converts fractions of a day into hours, minutes, and seconds. * There is no year 0. Enter B.C. years as negative; i.e., * 2 B.C. = -2. * * The approximate range of dates handled is 4713 B.C. to * 54,078 A.D. This should be adequate for most applications. * * B.C. dates are calculated by extending the Gregorian sequence * of leap years and century years into the past. This seems * the only sensible definition, but I don't know if it is * the official one. * * Note that the astronomical Julian day starts at noon on * the previous calendar day. Thus at midnight in the morning * of the present calendar day the Julian date ends in .5; * it rolls over to tomorrow at noon today. * * The month finding algorithm is attributed to Meeus. * * - Steve Moshier */ char *months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; char *days[7] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; long cyear = 1986; extern long cyear; static int month = 1; static double day = 1.0; short yerend = 0; extern short yerend; double getdate() { double J; double caltoj(); /* Get operator to type in a date. */ cyear = YEAR; if( (cyear > 53994) || (cyear < -4713) ) { printf( "Year out of range.\n" ); goto err; } if( cyear == 0 ) { printf( "There is no year 0.\n" ); err: J = 0.0; } month = MONTH; day = DAY; /* Find the Julian day. */ J = caltoj(cyear,month,day); printf( "Julian day %.1f\n", J ); return(J); } /* Calculate Julian day from Gregorian calendar date */ double caltoj( year, month, day ) long year; int month; double day; { long y, a, b, c, e, m; int BC; double J; BC = 0; /* The origin should be chosen to be a century year * that is also a leap year. We pick 4801 B.C. */ y = year + 4800; if( year < 0 ) { BC = 1; y += 1; } /* The following magic arithmetic calculates a sequence * whose successive terms differ by the correct number of * days per calendar month. It starts at 122 = March; January * and February come after December. */ m = month; if( m <= 2 ) { m += 12; y -= 1; } e = (306 * (m+1))/10; a = y/100; /* number of centuries */ if( year <= 1582L ) { if( year == 1582L ) { if( month < 10 ) goto julius; if( month > 10) goto gregor; if( day >= 15 ) goto gregor; } julius: printf( " Julian Calendar assumed.\n" ); b = -38; } else { /* -number of century years that are not leap years */ gregor: b = (a/4) - a; } c = (36525 * y)/100; /* Julian calendar years and leap years */ /* Add up these terms, plus offset from J 0 to 1 Jan 4801 B.C. * Also fudge for the 122 days from the month algorithm. */ J = b + c + e + day - 32167.5; return( J ); } /* Reduce x modulo 360 degrees */ double mod360(x) double x; { long k; double y; k = x/360.0; y = x - k * 360.0; while( y < 0.0 ) y += 360.0; while( y > 360.0 ) y -= 360.0; return(y); } /* Reduce x modulo 2 pi */ #define TPI (2.0*PI) double modtp(x) double x; { int k; double y; y = floor( x/TPI ); y = x - y * TPI; while( y < 0.0 ) y += TPI; while( y >= TPI ) y -= TPI; return(y); } /* Get operator to type in hours, minutes, and seconds */ static int hours = 0; static int minutes = 0; static double seconds = 0.0; double gethms() { char s[40]; double t, x; /* getnum( "Time: Hours", &hours, intfmt ); getnum( "Minutes", &minutes, intfmt ); getnum( "Seconds", &seconds, dblfmt ); t = (3600.0*hours + 60.0*minutes + seconds)/86400.0; */ t = UT_TIME / 24.0 ; printf ("gethms returning %lf days \n",t); return(t); } getnum( msg, num, format ) char *msg; double *num; char *format; { char s[40]; printf( "%s (", msg ); if( format == strfmt ) printf( format, num ); else if( format == dblfmt ) printf( format, *(double *)num ); else if( format == intfmt ) printf( format, *(int *)num ); else if( format == lngfmt ) printf( format, *(long *)num ); else printf( "Illegal input format\n" ); printf( ") ? "); gets(s); if( s[0] != '\0' ) sscanf( s, format, num ); }