Index: /trunk/doc/pslib/psLibADD.tex
===================================================================
--- /trunk/doc/pslib/psLibADD.tex	(revision 1617)
+++ /trunk/doc/pslib/psLibADD.tex	(revision 1618)
@@ -1,3 +1,3 @@
-%%% $Id: psLibADD.tex,v 1.30 2004-08-24 19:32:18 eugene Exp $
+%%% $Id: psLibADD.tex,v 1.31 2004-08-25 04:36:56 price Exp $
 \documentclass[panstarrs]{panstarrs}
 
@@ -694,17 +694,227 @@
 
 Correct time representation is critical in astronomical software.
-PSLib uses the \code{psTime} structure to represent all time values.
-This structure represents a time which is consists of seconds and
-fractions of seconds in a time system defined by the \code{psTimeType}
-element \code{type}.  Two possible time systems are currently
-available: TAI and UTC.  Both are defined in terms of the reference
-epoch 1970-01-01T00:00:00Z, but with minor modifications for
-leap-seconds as needed.  The first represenatation, TAI (International
-Atomic Time), has seconds of uniform length and no leap seconds.  The
-exact zero reference is 1970/01/01,00:00:10 UTC.  The second
-representations is UTC, which has seconds of uniform length and
-leap-seconds as needed to adjust it to remain within 0.9 seconds of
-the Earth's rotation.  It has a zero-point of exactly
-1970/01/01,00:00:00 UTC.
+PSLib uses the \code{psTime} structure to represent time values.  This
+structure represents a time which is consists of seconds and fractions
+of seconds in a time system defined by the \code{psTimeType} element
+\code{type}.  Two possible time systems are currently available: TAI
+and UTC.  Both are defined in terms of the reference epoch
+1970-01-01T00:00:00Z, but with minor modifications for leap seconds as
+needed.  The first represenatation, TAI (International Atomic Time),
+has seconds of uniform length and no leap seconds.  The exact zero
+reference is 1970/01/01,00:00:10 UTC.  The second representation is
+UTC, which has seconds of uniform length and leap seconds as needed to
+adjust it to remain within 0.9 seconds of the Earth's rotation.  It
+has a zero-point of exactly 1970/01/01,00:00:00 UTC.
+
+
+The conversion from a time and longitude to local mean sidereal time
+is performed using the SLALib function \code{sla_GMST}.  
+\paragraph{Coordinated Universal Time (UTC)}
+
+Coordinated Univeral Time (UTC) is a system of time with SI length
+seconds but attempts to stay within 1s of UT1.  This is done by the
+insertion of leap second whenever UTC-UT1 $\ge$ 0.9s.  By definition
+UTC-TAI is an integer number of seconds.  UTC went into effect on
+"1972-01-01T00:00:00" and is defined as being UTC-TAI = 10s on that
+date.  For dates prior to 1972-01-01 a fixed offset of +10s relative
+to TAI will be assumed.
+
+\begin{equation}
+UTC = TAI + 10s + leapseconds
+\end{equation}
+
+Leapseconds are declared by the International Earth Rotation and
+Reference Systems Service (IERS).  Leapseconds only occur in the UTC
+time system and cannot be accurately predicted due to variations in
+the Earth's rotational period.  To determine the number of leapsecond
+in a given UTC date a table of leapseconds as annouced by the IERS
+must be consulted.  This table will have to be updated each time a new
+leapsecond occurs.
+
+For ease of conversion, UTC should be represented as the number of
+seconds since the UNIX epoch of "1970-01-01T00:00:00".
+
+\paragraph{International Atomic Time (TAI)}
+
+International Atomic Time or Temps Atomique International (TAI) is a
+system of time defined by the Bureau International des Poids et
+Mesures (BIPM) with SI length seconds as measured at sea level.  To
+convert from UTC to TAI subtract the base delta of $10s$ and all of
+the accumulated leapsecons since 1972-01-01 up until the UTC date
+being converted.
+
+\begin{equation}
+{\rm TAI} = {\rm UTC} - 10{\rm s} - {\rm leapseconds}
+\end{equation}
+
+For ease of conversion, TAI should be represented as the number of
+seconds since the UNIX epoch of "1970-01-01T00:00:00".
+
+\paragraph{Leap seconds}
+
+Leap seconds keep UTC within 0.9s of UT1.  The offset between TAI and
+UTC must be looked up from tables.  Jumps in the offset correspond to
+leap seconds.
+
+\begin{verbatim}
+ 1972 JUL  1 =JD 2441499.5  TAI-UTC=  11.0       S + (MJD - 41317.) X 0.0 S
+ 1973 JAN  1 =JD 2441683.5  TAI-UTC=  12.0       S + (MJD - 41317.) X 0.0 S
+ 1974 JAN  1 =JD 2442048.5  TAI-UTC=  13.0       S + (MJD - 41317.) X 0.0 S
+ 1975 JAN  1 =JD 2442413.5  TAI-UTC=  14.0       S + (MJD - 41317.) X 0.0 S
+ 1976 JAN  1 =JD 2442778.5  TAI-UTC=  15.0       S + (MJD - 41317.) X 0.0 S
+ 1977 JAN  1 =JD 2443144.5  TAI-UTC=  16.0       S + (MJD - 41317.) X 0.0 S
+ 1978 JAN  1 =JD 2443509.5  TAI-UTC=  17.0       S + (MJD - 41317.) X 0.0 S
+ 1979 JAN  1 =JD 2443874.5  TAI-UTC=  18.0       S + (MJD - 41317.) X 0.0 S
+ 1980 JAN  1 =JD 2444239.5  TAI-UTC=  19.0       S + (MJD - 41317.) X 0.0 S
+ 1981 JUL  1 =JD 2444786.5  TAI-UTC=  20.0       S + (MJD - 41317.) X 0.0 S
+ 1982 JUL  1 =JD 2445151.5  TAI-UTC=  21.0       S + (MJD - 41317.) X 0.0 S
+ 1983 JUL  1 =JD 2445516.5  TAI-UTC=  22.0       S + (MJD - 41317.) X 0.0 S
+ 1985 JUL  1 =JD 2446247.5  TAI-UTC=  23.0       S + (MJD - 41317.) X 0.0 S
+ 1988 JAN  1 =JD 2447161.5  TAI-UTC=  24.0       S + (MJD - 41317.) X 0.0 S
+ 1990 JAN  1 =JD 2447892.5  TAI-UTC=  25.0       S + (MJD - 41317.) X 0.0 S
+ 1991 JAN  1 =JD 2448257.5  TAI-UTC=  26.0       S + (MJD - 41317.) X 0.0 S
+ 1992 JUL  1 =JD 2448804.5  TAI-UTC=  27.0       S + (MJD - 41317.) X 0.0 S
+ 1993 JUL  1 =JD 2449169.5  TAI-UTC=  28.0       S + (MJD - 41317.) X 0.0 S
+ 1994 JUL  1 =JD 2449534.5  TAI-UTC=  29.0       S + (MJD - 41317.) X 0.0 S
+ 1996 JAN  1 =JD 2450083.5  TAI-UTC=  30.0       S + (MJD - 41317.) X 0.0 S
+ 1997 JUL  1 =JD 2450630.5  TAI-UTC=  31.0       S + (MJD - 41317.) X 0.0 S
+ 1999 JAN  1 =JD 2451179.5  TAI-UTC=  32.0       S + (MJD - 41317.) X 0.0 S
+\end{verbatim}
+
+For the present time, it should be assumed that this table resides on
+local disk in a known location (i.e., there is no need that it is
+downloaded from the internet by PSLib).  Later, the location of this
+file will be made configurable.
+
+This data is available from
+\code{http://hpiers.obspm.fr/eop-pc/earthor/utc/TAI-UTC_tab.html}
+
+\paragraph{Gregorian dates to seconds}
+
+The below algorithm converts from Gregorian-formatted dates to
+seconds since the UNIX epoch.
+
+\begin{verbatim}
+    Given year, month, day.
+
+    ### Make month in range 3..14 (treat Jan & Feb as months 13..14 of prev year):
+    if ( month <= 2 )
+    {
+        year -= ( temp = ( 14 - month ) / 12 )
+        month += 12 * temp
+    }
+    else if ( month > 14 )
+    {
+        year += ( temp = ( month - 3 ) / 12 )
+        month -= 12 * temp
+    }
+ 
+    ### make year positive
+    if ( year < 0 )
+    {
+        day -= 146097 * ( temp = ( 399 - year ) / 400 )
+        year += 400 * temp
+    }
+ 
+    ### add: day of month, days of previous 0-11 month period that began
+    ### w/March, days of previous 0-399 year period that began w/March
+    ### of a 400-multiple year), days of any 400-year periods before
+    ### that, and 306 days to adjust from Mar 1, year 0-relative to Jan
+    ### 1, year 1-relative
+    day += ( month * 367 - 1094 ) / 12 + year % 100 * 1461 / 4 +
+          ( year / 100 * 36524 + year / 400 ) - 306
+
+    unix = ( ( day - 1 ) * 86400 ) - 62135596800
+    utc = unix - leapseconds(unix)
+\end{verbatim}
+
+To go the other way:
+
+\begin{verbatim}
+    unix = utc + leapseconds(utc)
+    day = ( unix + 62135596800 ) / 86400
+    temp = 0
+ 
+    ### add 306 days to make relative to Mar 1, 0; also adjust day to be
+    ### within a range (1..2**28-1) where our calculations will work
+    ### with 32bit ints
+    if ( day > 2**28 - 307 )
+    {
+        ### avoid overflow if day close to maxint
+        temp = ( day - 146097 + 306 ) / 146097 + 1
+        day -= temp * 146097 - 306
+    }
+    else if ( ( day += 306 ) <= 0 )
+    {
+        temp = -( -day / 146097 + 1 )  ### avoid ambiguity in C division of negatives
+        day -= temp * 146097
+    }
+ 
+    cent = ( day * 4 - 1 ) / 146097    ### calc number of centuries day is after 29 Feb of yr 0
+    day -= cent * 146097 / 4           ### (4 centuries = 146097 days)
+    year = ( day * 4 - 1 ) / 1461      ### calc number of years into the century,
+    day -= year * 1461 / 4             ### again March-based (4 yrs =~ 146[01] days)
+    month = ( day * 12 + 1093 ) / 367  ### get the month (3..14 represent March through
+    day -= ( month * 367 - 1094 ) / 12 ### February of following year)
+    year += cent * 100 + temp * 400    ### get the real year, which is off by
+    if ( month > 12 )                  ### one if month is January or February
+    {
+        year++
+	month -= 12
+    }
+
+
+    Output year, month, day.
+\end{verbatim}
+
+(Above taken from \code{DateTime.pm} (C) 2003 Dave Rolsky, available
+from \code{datetime.perl.org}.)
+
+
+
+\paragraph{Universal Time (UT1)}
+\label{sec:ut1}
+
+Univseral Time is a measure of the rotation angle of the Earth.  When
+corrected for polar motion it is referred to as UT1.  This is distict
+from UT0 which does not involve corrections for polar motion.
+
+The offset of UTC from UT1, $\Delta$ UT1 = UTC - UT1, may be
+determined from the following site in real time:
+
+\code{ftp://maia.usno.navy.mil/ser7/finals.all}
+
+\noindent with explanatory guide at
+
+\code{ftp://maia.usno.navy.mil/ser7/readme.finals}
+
+See also the web page \code{http://maia.usno.navy.mil/}.  The most
+significant accuracy requirements are for the current value when
+calculating the LST.  For this purpose, the table above
+(\code{ser7.dat}), which provides predictions over a 2 month period,
+must be made available locally to PSLib and updated regularly.
+
+For the present time, it should be assumed that this table resides on
+local disk in a known location (i.e., there is no need that it is
+downloaded from the internet by PSLib).  Later, the location of this
+file will be made configurable.
+
+For dates within the range of the table, the value for the offset
+between UTC and UT1 shall be derived from linear interpolation between
+the nearest entries in the table.  For dates earlier the range of the
+above table, a warning shall be generated, and the values calculated
+from a different table (an estimate, instead of observations),
+obtained from:
+
+\code{http://hpiers.obspm.fr/eoppc/eop/eopc01/eopc01.1900-2004}
+
+Dates outside the ranges of the above tables shall generate an error.
+
+These tables shall be read in only when required by the user, and
+shall remain in memory until the termination of the program.  An
+additional function, \code{psTimeTableReset} should be provided in
+order to force the reloading of the time tables.
+
+\paragraph{Julian Day and Modified Julian Day}
 
 Julian Day (JD) and Modified Julian Day (MJD) are both continuous time
@@ -716,65 +926,87 @@
 
 \begin{verbatim}
-mjd = psTime.tv_sec/86400.0 + psTime.tv_usec/86400000000.0 + 40587.0;
- jd = psTime.tv_sec/86400.0 + psTime.tv_usec/86400000000.0 + 2440587.5;
+mjd = psTime.sec/86400.0 + psTime.usec/86400000000.0 + 40587.0;
+ jd = psTime.sec/86400.0 + psTime.usec/86400000000.0 + 2440587.5;
 \end{verbatim}
 
-The entry below gives the current relationship between JD, MJD, UTC,
-and TAI, and comes from the reference at
-\code{http://tycho.usno.navy.mil/leapsec.html}
-
-\begin{verbatim}
- 1961 JAN  1 =JD 2437300.5  TAI-UTC=   1.4228180 S + (MJD - 37300.) X 0.001296 S
- 1961 AUG  1 =JD 2437512.5  TAI-UTC=   1.3728180 S + (MJD - 37300.) X 0.001296 S
- 1962 JAN  1 =JD 2437665.5  TAI-UTC=   1.8458580 S + (MJD - 37665.) X 0.0011232S
- 1963 NOV  1 =JD 2438334.5  TAI-UTC=   1.9458580 S + (MJD - 37665.) X 0.0011232S
- 1964 JAN  1 =JD 2438395.5  TAI-UTC=   3.2401300 S + (MJD - 38761.) X 0.001296 S
- 1964 APR  1 =JD 2438486.5  TAI-UTC=   3.3401300 S + (MJD - 38761.) X 0.001296 S
- 1964 SEP  1 =JD 2438639.5  TAI-UTC=   3.4401300 S + (MJD - 38761.) X 0.001296 S
- 1965 JAN  1 =JD 2438761.5  TAI-UTC=   3.5401300 S + (MJD - 38761.) X 0.001296 S
- 1965 MAR  1 =JD 2438820.5  TAI-UTC=   3.6401300 S + (MJD - 38761.) X 0.001296 S
- 1965 JUL  1 =JD 2438942.5  TAI-UTC=   3.7401300 S + (MJD - 38761.) X 0.001296 S
- 1965 SEP  1 =JD 2439004.5  TAI-UTC=   3.8401300 S + (MJD - 38761.) X 0.001296 S
- 1966 JAN  1 =JD 2439126.5  TAI-UTC=   4.3131700 S + (MJD - 39126.) X 0.002592 S
- 1968 FEB  1 =JD 2439887.5  TAI-UTC=   4.2131700 S + (MJD - 39126.) X 0.002592 S
- 1972 JAN  1 =JD 2441317.5  TAI-UTC=  10.0       S + (MJD - 41317.) X 0.0      S
- 1972 JUL  1 =JD 2441499.5  TAI-UTC=  11.0       S + (MJD - 41317.) X 0.0      S
- 1973 JAN  1 =JD 2441683.5  TAI-UTC=  12.0       S + (MJD - 41317.) X 0.0      S
- 1974 JAN  1 =JD 2442048.5  TAI-UTC=  13.0       S + (MJD - 41317.) X 0.0      S
- 1975 JAN  1 =JD 2442413.5  TAI-UTC=  14.0       S + (MJD - 41317.) X 0.0      S
- 1976 JAN  1 =JD 2442778.5  TAI-UTC=  15.0       S + (MJD - 41317.) X 0.0      S
- 1977 JAN  1 =JD 2443144.5  TAI-UTC=  16.0       S + (MJD - 41317.) X 0.0      S
- 1978 JAN  1 =JD 2443509.5  TAI-UTC=  17.0       S + (MJD - 41317.) X 0.0      S
- 1979 JAN  1 =JD 2443874.5  TAI-UTC=  18.0       S + (MJD - 41317.) X 0.0      S
- 1980 JAN  1 =JD 2444239.5  TAI-UTC=  19.0       S + (MJD - 41317.) X 0.0      S
- 1981 JUL  1 =JD 2444786.5  TAI-UTC=  20.0       S + (MJD - 41317.) X 0.0      S
- 1982 JUL  1 =JD 2445151.5  TAI-UTC=  21.0       S + (MJD - 41317.) X 0.0      S
- 1983 JUL  1 =JD 2445516.5  TAI-UTC=  22.0       S + (MJD - 41317.) X 0.0      S
- 1985 JUL  1 =JD 2446247.5  TAI-UTC=  23.0       S + (MJD - 41317.) X 0.0      S
- 1988 JAN  1 =JD 2447161.5  TAI-UTC=  24.0       S + (MJD - 41317.) X 0.0      S
- 1990 JAN  1 =JD 2447892.5  TAI-UTC=  25.0       S + (MJD - 41317.) X 0.0      S
- 1991 JAN  1 =JD 2448257.5  TAI-UTC=  26.0       S + (MJD - 41317.) X 0.0      S
- 1992 JUL  1 =JD 2448804.5  TAI-UTC=  27.0       S + (MJD - 41317.) X 0.0      S
- 1993 JUL  1 =JD 2449169.5  TAI-UTC=  28.0       S + (MJD - 41317.) X 0.0      S
- 1994 JUL  1 =JD 2449534.5  TAI-UTC=  29.0       S + (MJD - 41317.) X 0.0      S
- 1996 JAN  1 =JD 2450083.5  TAI-UTC=  30.0       S + (MJD - 41317.) X 0.0      S
- 1997 JUL  1 =JD 2450630.5  TAI-UTC=  31.0       S + (MJD - 41317.) X 0.0      S
- 1999 JAN  1 =JD 2451179.5  TAI-UTC=  32.0       S + (MJD - 41317.) X 0.0      S
-\end{verbatim}
-
-The conversion from a time and longitude to local mean sidereal time
-is performed using the SLA Lib function \code{sla_GMST}.  This
-function requires the value $\Delta$ UT1 = UTC - UT1.  The value of
-$\Delta$ UT1 may be determined from the following site in real time:
-
-\code{ftp://maia.usno.navy.mil/ser7/ser7.dat}
-
-In addition, the long-term values may be determined from the table
-found at: \code{ftp://maia.usno.navy.mil/ser7/finals.all}.  See also
-the web page \code{http://maia.usno.navy.mil/}.  The most significant
-accuracy requirements are for the current value when calculating the
-LST.  For this purpose, the table above (\code{ser7.dat}), which
-provides predictions over a 2 month period, must be made available
-locally to PSLib and updated regularly. 
+$2451545.0$ JD $= 51544.5$ MJD is equivalent to "2000-01-01T00:00:00".
+
+\begin{equation}
+{\rm JD} = {\rm MJD} + 2400000.5
+\end{equation}
+
+\paragraph{Terrestrial Dynamical Time (TDT)}
+
+Terrestrial Dynamical Time (TDT) is defined as a fixed offset from
+TAI.  Its only purpose as far as we are concerned is for its utility
+in obtaining the GMST.
+
+\begin{equation}
+{\rm TDT} = {\rm TAI} + 32.184{\rm s}
+\end{equation}
+
+\paragraph{TDT as Julian Centuries since J2000.0}
+
+The algorithm for calulating GMST requires TDT formated in Julian centruies
+since the J2000.0 epoch.
+
+\begin{equation}
+t_u = \frac{{\rm JD} - 2451545.0}{36525}
+\end{equation}
+
+\paragraph{UT1 as Julian Centuries since J2000.0}
+
+The algorithm for calulating GMST requires UT1 be formated in Julian
+centuries since the J2000.0 epoch.
+
+\begin{equation}
+t = \frac{{\rm JD} - 2451545.0}{36525}
+\end{equation}
+
+\paragraph{Greenwich Mean Sidereal Time (GMST)}
+
+Greenwich Mean Sidereal Time (GMST) is caclulated from UT1 and TDT.
+This algorithm requires that both time inputs are expressed as Julian
+centuries since J2000.0.
+
+Here $t_u$ is UT1 expressed in Julian centuries since J2000.0, and $t$
+is TDT expressed in Julian centuries since J2000.0.
+
+\begin{eqnarray}
+{\rm GMST00}(t_u, t) & = & UT1 + 24110.5493771\\
+& & + 8639877.3173760\, t_u + 307.4771600\, t\\
+& & + 0.0931118\, t^2 - 0.0000062\, t^3\\
+& & + 0.0000013\, t^4
+\end{eqnarray}
+
+Gives $GMST00$ in seconds.
+
+\paragraph{Longitude}
+
+Longitudes are often expressed in the form of decimal degrees while the
+algorithm for calculating GMST outputs seconds.
+
+\begin{equation}
+1\degree = 240s
+\end{equation}
+
+\paragraph{Local Mean Sidereal Time (LMST)}
+
+Local Mean Sidereal Time (LMST) is Greenwich Mean Sideral Time (GMST)
+plus the observer's location in East longitude. Calculating LMST
+requires the input of Universal Time (UT1), Terrestrial Dynamical Time
+(TDT) and a longitude (measured East of Greenwich).
+
+\begin{equation}
+LMST = GMST00(t_u, t) + longitude
+\end{equation}
+
+Gives $LMST$ in seconds.
+
+\paragraph{Polar Coordinates}
+
+The polar coordinates, $x_p$ and $y_p$, required for \code{SLA_AOPPA}
+(and hence the \code{psGrommit}s), may be calculated in a similar
+manner as for the offset of UT1 from UTC (\S\ref{sec:ut1}).
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
