IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2725 for trunk/psLib/src/astro


Ignore:
Timestamp:
Dec 16, 2004, 8:53:35 AM (22 years ago)
Author:
harman
Message:

Added new time math functions

Location:
trunk/psLib/src/astro
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/astro/psTime.c

    r2607 r2725  
    1010 *  @author Ross Harman, MHPCC
    1111 *
    12  *  @version $Revision: 1.46 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-12-03 23:15:00 $
     12 *  @version $Revision: 1.47 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-12-16 18:52:41 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    151151    // Check time metadata. Function call reports errors.
    152152    if(timeMetadata == NULL) {
    153         if(!psTimeInit(TIME_CONFIG_FILE))
     153        if(!p_psTimeInit(TIME_CONFIG_FILE))
    154154            return 0.0;
    155155    }
     
    184184}
    185185
    186 bool psTimeInit(const char *fileName)
     186bool p_psTimeInit(const char *fileName)
    187187{
    188188    bool foundTable = false;
     
    326326}
    327327
    328 bool psTimeFinalize(void)
     328bool p_psTimeFinalize(void)
    329329{
    330330    if(timeMetadata != NULL) {
     
    405405        time->sec = time->sec + delta;    // User wants TAI time. Add leapseconds.
    406406    } else {
    407         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    408                 PS_ERRORTEXT_psTime_TYPE_UNKNOWN,
    409                 type);
     407        psError(PS_ERR_BAD_PARAMETER_VALUE, true, PS_ERRORTEXT_psTime_TYPE_UNKNOWN, type);
    410408    }
    411409
     
    431429    psF64  const6       =  0.0000013;
    432430    psTime *ut1UtcDelta = NULL;
    433     psTime *tdtDelta    = NULL;
    434431    psTime *tdtTime     = NULL;
    435432    psTime *taiTime     = NULL;
     
    456453
    457454    // Convert Universal Time (UTC) to  UT1
    458     ut1UtcDelta = psTimeAlloc(PS_TIME_UTC);
    459     ut1UtcDbl = psTimeGetUT1Delta(taiTime)*1e6;
    460     ut1UtcDelta->usec = (psU32)fabs(ut1UtcDbl);
    461     if(ut1UtcDbl<0) {
    462         ut1Time = psTimeSubtract(utcTime, ut1UtcDelta);
    463     } else {
    464         ut1Time = psTimeAdd(utcTime, ut1UtcDelta);
    465     }
     455    ut1UtcDbl = psTimeGetUT1Delta(taiTime);
     456    utcTime->type = PS_TIME_TAI; // Don't allow addition of leapseconds to ut1Time
     457    ut1Time = psTimeMath(utcTime, ut1UtcDbl);
     458    utcTime->type = PS_TIME_UTC;
    466459
    467460    // Calculate UT1 as Julian Centuries since J2000.0
     
    471464
    472465    // Calculate Terrestial Dynamical Time (TDT)
    473     tdtDelta = psTimeAlloc(PS_TIME_TAI);
    474     tdtDelta->sec = 32;
    475     tdtDelta->usec = 184000;
    476     tdtTime = psTimeAdd(taiTime, tdtDelta);
     466    tdtTime = psTimeMath(taiTime, 32.184);
    477467
    478468    // Calculate TDT as Julian centuries since J2000.0
     
    496486    psFree(ut1Time);
    497487    psFree(tdtTime);
    498     psFree(tdtDelta);
    499488    psFree(utcTime);
    500489    psFree(taiTime);
     
    718707    // Check time metadata
    719708    if(timeMetadata == NULL) {
    720         if(!psTimeInit(TIME_CONFIG_FILE))
     709        if(!p_psTimeInit(TIME_CONFIG_FILE)) {
    721710            psError(PS_ERR_BAD_PARAMETER_VALUE, true, PS_ERRORTEXT_psTime_FILE_NOT_FOUND, "psTime.config");
    722         return 0.0;
     711            return 0.0;
     712        }
    723713    }
    724714
     
    741731    // Check for successful interpolation
    742732    for(i=0; i<stats->n; i++) {
    743         if(stats->data.U32[i]!=PS_LOOKUP_PAST_BOTTOM && stats->data.U32[i]!=PS_LOOKUP_PAST_TOP) {
     733        if(stats->data.U32[i] == PS_LOOKUP_ERROR) {
    744734            psError(PS_ERR_BAD_PARAMETER_VALUE, true, PS_ERRORTEXT_psTime_INTERPOLATION_FAILED);
    745735        }
     
    835825    // Converts psTime to YYYY-MM-DDThh:mm:ss.sss in string form
    836826    if (!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y-%m-%dT%H:%M:%S", tmTime)) {
    837         psError(PS_ERR_OS_CALL_FAILED, true,
    838                 PS_ERRORTEXT_psTime_CONVERT_TIME_TO_STRING_FAILED);
     827        psError(PS_ERR_OS_CALL_FAILED, true, PS_ERRORTEXT_psTime_CONVERT_TIME_TO_STRING_FAILED);
    839828    }
    840829
    841830    if (snprintf(timeString, MAX_TIME_STRING_LENGTH, "%s.%3.3dZ", tempString, ms) < 0) {
    842         psError(PS_ERR_OS_CALL_FAILED, true,
    843                 PS_ERRORTEXT_psTime_APPEND_MSEC_FAILED);
     831        psError(PS_ERR_OS_CALL_FAILED, true, PS_ERRORTEXT_psTime_APPEND_MSEC_FAILED);
    844832    }
    845833    psFree(tempString);
     
    987975
    988976    // Convert YYYY-MM-DDThh:mm:ss.sss in string form to tm time
    989     if (sscanf(time, "%d-%d-%dT%d:%d:%d.%d",
    990                &tmTime.tm_year,&tmTime.tm_mon,&tmTime.tm_mday,
    991                &tmTime.tm_hour,&tmTime.tm_min,&tmTime.tm_sec,&millisecond) < 7) {
    992         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    993                 PS_ERRORTEXT_psTime_ISOTIME_MALFORMED,
    994                 time);
     977    if (sscanf(time, "%d-%d-%dT%d:%d:%d.%d", &tmTime.tm_year, &tmTime.tm_mon, &tmTime.tm_mday,
     978               &tmTime.tm_hour, &tmTime.tm_min, &tmTime.tm_sec,&millisecond) < 7) {
     979        psError(PS_ERR_BAD_PARAMETER_VALUE, true, PS_ERRORTEXT_psTime_ISOTIME_MALFORMED, time);
    995980        return NULL;
    996981    }
     
    10951080}
    10961081
    1097 psTime* psTimeAdd(const psTime *tai1, const psTime *tai2)
    1098 {
    1099     psF64 deltaSec = 0;
    1100     psS64 deltaUsec = 0;
     1082psTime* psTimeMath(const psTime *time, psF64 delta)
     1083{
     1084    psS64 deltaSec = 0;
     1085    psF64 uSecTimeInput = 0.0;
    11011086    psTime *outTime = NULL;
    1102 
    1103 
    1104     // Error checks
    1105     PS_PTR_CHECK_NULL(tai1,NULL);
    1106     PS_PTR_CHECK_NULL(tai2,NULL);
    1107     PS_INT_CHECK_RANGE(tai1->usec,0,1e6-1,NULL);
    1108     PS_INT_CHECK_RANGE(tai2->usec,0,1e6-1,NULL);
    1109 
    1110     if(tai1->type != tai2->type) {
    1111         psError(PS_ERR_BAD_PARAMETER_TYPE, true,
    1112                 PS_ERRORTEXT_psTime_TYPE_MISMATCH);
    1113         return NULL;
    1114     }
    1115 
    1116     outTime = psTimeAlloc(tai1->type);
    1117     outTime->sec = tai1->sec + tai2->sec;
    1118     deltaUsec = tai1->usec + tai2->usec;
    1119 
    1120     // Adjust time in case of microsecond overflow after addition
    1121     if(deltaUsec >= 1e6) {
    1122         if(deltaSec < 0 ) {
    1123             outTime->sec--; // Negative seconds - make more negative
    1124         } else {
    1125             outTime->sec++; // Positive seconds - make more positive
    1126         }
    1127         outTime->usec = deltaUsec - 1e6;
     1087    psTime *tempTime = NULL;
     1088
     1089
     1090    // Error checks
     1091    PS_PTR_CHECK_NULL(time,NULL);
     1092    PS_INT_CHECK_RANGE(time->usec,0,1e6-1,NULL);
     1093
     1094    // Convert time to TAI if necessary, but without changing input arguments
     1095    if(time->type == PS_TIME_UTC) {
     1096        tempTime = psTimeAlloc(PS_TIME_UTC);
     1097        tempTime->sec = time->sec;
     1098        tempTime->usec = time->usec;
     1099        tempTime = psTimeConvert(tempTime, PS_TIME_TAI);
    11281100    } else {
    1129         outTime->usec = deltaUsec;
    1130     }
     1101        tempTime = psMemIncrRefCounter((psTime*)time);
     1102    }
     1103
     1104    uSecTimeInput = tempTime->sec >= 0 ? 1.0 : -1.0;
     1105    uSecTimeInput = uSecTimeInput*tempTime->usec/1e6;
     1106    delta = delta + uSecTimeInput;
     1107    deltaSec = (psS64)delta;
     1108
     1109    // Create output time
     1110    outTime = psTimeAlloc(PS_TIME_TAI);
     1111    outTime->sec = delta + (psF64)tempTime->sec;
     1112    outTime->usec = fabs((delta-(psF64)deltaSec)*1e6);
    11311113
    11321114    // Error check
    11331115    PS_INT_CHECK_RANGE(outTime->usec,0,1e6-1,outTime);
    11341116
     1117    // Convert result to same time type as input
     1118    if(time->type == PS_TIME_UTC) {
     1119        outTime = psTimeConvert(outTime, PS_TIME_UTC);
     1120    }
     1121
     1122    psFree(tempTime);
     1123
    11351124    return outTime;
    11361125}
    11371126
    1138 psTime* psTimeSubtract(const psTime *tai1, const psTime *tai2)
    1139 {
    1140     psS64 deltaSec = 0;
    1141     psS64 deltaUsec = 0;
    1142     psTime *outTime = NULL;
    1143 
    1144 
    1145     // Error checks
    1146     PS_PTR_CHECK_NULL(tai1,NULL);
    1147     PS_PTR_CHECK_NULL(tai2,NULL);
    1148     PS_INT_CHECK_RANGE(tai1->usec,0,1e6-1,NULL);
    1149     PS_INT_CHECK_RANGE(tai2->usec,0,1e6-1,NULL);
    1150     if(tai1->type != tai2->type) {
    1151         psError(PS_ERR_BAD_PARAMETER_TYPE, true,
    1152                 PS_ERRORTEXT_psTime_TYPE_MISMATCH);
    1153         return NULL;
    1154     }
    1155 
    1156     outTime = psTimeAlloc(tai1->type);
    1157     deltaSec = tai1->sec - tai2->sec;
    1158     deltaUsec = (psS64)tai1->usec - (psS64)tai2->usec;
    1159 
    1160     // Adjust time in case of microsecond underflow after subtraction
    1161     if(deltaUsec < 0) {
    1162         if(deltaSec < 0) {
    1163             deltaSec++; // Negative seconds - make less negative
    1164         } else {
    1165             deltaSec--; // Positive seconds - make less positive
    1166         }
    1167         outTime->usec = 1e6+deltaUsec;
     1127psF64 psTimeDelta(const psTime *time1, const psTime *time2)
     1128{
     1129    psF64 out = 0.0;
     1130    psF64 uSec1 = 0.0;
     1131    psF64 uSec2 = 0.0;
     1132    psTime *tempTime1 = NULL;
     1133    psTime *tempTime2 = NULL;
     1134
     1135
     1136    // Error checks
     1137    PS_PTR_CHECK_NULL(time1,0.0);
     1138    PS_INT_CHECK_RANGE(time1->usec,0,1e6-1,0.0);
     1139    PS_PTR_CHECK_NULL(time2,0.0);
     1140    PS_INT_CHECK_RANGE(time2->usec,0,1e6-1,0.0);
     1141
     1142    // Convert time to TAI if necessary, but without changing input arguments
     1143    if(time1->type == PS_TIME_UTC) {
     1144        tempTime1 = psTimeAlloc(PS_TIME_UTC);
     1145        tempTime1->sec = time1->sec;
     1146        tempTime1->usec = time1->usec;
     1147        tempTime1 = psTimeConvert(tempTime1, PS_TIME_TAI);
    11681148    } else {
    1169         outTime->usec = deltaUsec;
    1170     }
    1171 
    1172     outTime->sec = deltaSec;
    1173 
    1174     return outTime;
    1175 }
    1176 
    1177 psTime* psTimeDelta(const psTime *tai1, const psTime *tai2)
    1178 {
    1179     psS64 deltaSec = 0;
    1180     psS64 deltaUsec = 0;
    1181     psTime *outTime = NULL;
    1182 
    1183 
    1184     // Error checks
    1185     // Error checks
    1186     PS_PTR_CHECK_NULL(tai1,NULL);
    1187     PS_PTR_CHECK_NULL(tai2,NULL);
    1188     PS_INT_CHECK_RANGE(tai1->usec,0,1e6-1,NULL);
    1189     PS_INT_CHECK_RANGE(tai2->usec,0,1e6-1,NULL);
    1190     if(tai1->type != tai2->type) {
    1191         psError(PS_ERR_BAD_PARAMETER_TYPE, true,
    1192                 PS_ERRORTEXT_psTime_TYPE_MISMATCH);
    1193         return NULL;
    1194     }
    1195 
    1196     outTime = psTimeAlloc(tai1->type);
    1197     deltaSec = tai1->sec - tai2->sec;
    1198     deltaUsec = (psS64)tai1->usec - (psS64)tai2->usec;
    1199 
    1200     // Adjust time in case of microsecond underflow after subtraction
    1201     if(deltaUsec < 0) {
    1202         if(deltaSec < 0) {
    1203             deltaSec++; // Negative seconds - make less negative
    1204         } else {
    1205             deltaSec--; // Positive seconds - make less positive
    1206         }
    1207         outTime->usec = 1e6+deltaUsec;
     1149        tempTime1 = psMemIncrRefCounter((psTime*)time1);
     1150    }
     1151    if(time2->type == PS_TIME_UTC) {
     1152        tempTime2 = psTimeAlloc(PS_TIME_UTC);
     1153        tempTime2->sec = time2->sec;
     1154        tempTime2->usec = time2->usec;
     1155        tempTime2 = psTimeConvert(tempTime2, PS_TIME_TAI);
    12081156    } else {
    1209         outTime->usec = deltaUsec;
    1210     }
    1211 
    1212     outTime->sec = abs(deltaSec);
    1213 
    1214     return outTime;
    1215 }
     1157        tempTime2 = psMemIncrRefCounter((psTime*)time2);
     1158    }
     1159
     1160    uSec1 = tempTime1->sec >= 0 ? 1.0 : -1.0;
     1161    uSec1 = uSec1*tempTime1->usec/1e6;
     1162    uSec2 = tempTime2->sec >= 0 ? 1.0 : -1.0;
     1163    uSec2 = uSec2*tempTime2->usec/1e6;
     1164    out = (tempTime1->sec-tempTime2->sec) + (uSec1-uSec2);
     1165
     1166    psFree(tempTime1);
     1167    psFree(tempTime2);
     1168
     1169    return out;
     1170}
     1171
  • trunk/psLib/src/astro/psTime.h

    r2318 r2725  
    55 *
    66 *  A collection of functions are required by psLib to manipulate time data. These functions primarily consist
    7  *  of conversions between specific time formats.  PSLib currently uses the UNIX timeval time system as the
    8  *  base upon which International Atomic Time (TAI) and Universal Time Coordinated (UTC) are calculated. TAI
    9  *  time varies over time due to the earth's rotation and the movement of the continental plates. It is
    10  *  currentls 32 seconds faster than UTC/timeval time. The conversion between TAI and UTC time for other dates
    11  *  is given in the table below, which comes from:
    12  *
    13  *  ftp://maia.usno.navy.mil/ser7/tai-utc.dat
    14  *
    15  *  This table is placed into a static array and used by the psTime functions for conversions.
    16  *
    17  *   1961 JAN  1 =JD 2437300.5  TAI-UTC=   1.4228180 S + (MJD - 37300.) X 0.001296 S
    18  *   1961 AUG  1 =JD 2437512.5  TAI-UTC=   1.3728180 S + (MJD - 37300.) X 0.001296 S
    19  *   1962 JAN  1 =JD 2437665.5  TAI-UTC=   1.8458580 S + (MJD - 37665.) X 0.0011232S
    20  *   1963 NOV  1 =JD 2438334.5  TAI-UTC=   1.9458580 S + (MJD - 37665.) X 0.0011232S
    21  *   1964 JAN  1 =JD 2438395.5  TAI-UTC=   3.2401300 S + (MJD - 38761.) X 0.001296 S
    22  *   1964 APR  1 =JD 2438486.5  TAI-UTC=   3.3401300 S + (MJD - 38761.) X 0.001296 S
    23  *   1964 SEP  1 =JD 2438639.5  TAI-UTC=   3.4401300 S + (MJD - 38761.) X 0.001296 S
    24  *   1965 JAN  1 =JD 2438761.5  TAI-UTC=   3.5401300 S + (MJD - 38761.) X 0.001296 S
    25  *   1965 MAR  1 =JD 2438820.5  TAI-UTC=   3.6401300 S + (MJD - 38761.) X 0.001296 S
    26  *   1965 JUL  1 =JD 2438942.5  TAI-UTC=   3.7401300 S + (MJD - 38761.) X 0.001296 S
    27  *   1965 SEP  1 =JD 2439004.5  TAI-UTC=   3.8401300 S + (MJD - 38761.) X 0.001296 S
    28  *   1966 JAN  1 =JD 2439126.5  TAI-UTC=   4.3131700 S + (MJD - 39126.) X 0.002592 S
    29  *   1968 FEB  1 =JD 2439887.5  TAI-UTC=   4.2131700 S + (MJD - 39126.) X 0.002592 S
    30  *   1972 JAN  1 =JD 2441317.5  TAI-UTC=  10.0       S + (MJD - 41317.) X 0.0      S
    31  *   1972 JUL  1 =JD 2441499.5  TAI-UTC=  11.0       S + (MJD - 41317.) X 0.0      S
    32  *   1973 JAN  1 =JD 2441683.5  TAI-UTC=  12.0       S + (MJD - 41317.) X 0.0      S
    33  *   1974 JAN  1 =JD 2442048.5  TAI-UTC=  13.0       S + (MJD - 41317.) X 0.0      S
    34  *   1975 JAN  1 =JD 2442413.5  TAI-UTC=  14.0       S + (MJD - 41317.) X 0.0      S
    35  *   1976 JAN  1 =JD 2442778.5  TAI-UTC=  15.0       S + (MJD - 41317.) X 0.0      S
    36  *   1977 JAN  1 =JD 2443144.5  TAI-UTC=  16.0       S + (MJD - 41317.) X 0.0      S
    37  *   1978 JAN  1 =JD 2443509.5  TAI-UTC=  17.0       S + (MJD - 41317.) X 0.0      S
    38  *   1979 JAN  1 =JD 2443874.5  TAI-UTC=  18.0       S + (MJD - 41317.) X 0.0      S
    39  *   1980 JAN  1 =JD 2444239.5  TAI-UTC=  19.0       S + (MJD - 41317.) X 0.0      S
    40  *   1981 JUL  1 =JD 2444786.5  TAI-UTC=  20.0       S + (MJD - 41317.) X 0.0      S
    41  *   1982 JUL  1 =JD 2445151.5  TAI-UTC=  21.0       S + (MJD - 41317.) X 0.0      S
    42  *   1983 JUL  1 =JD 2445516.5  TAI-UTC=  22.0       S + (MJD - 41317.) X 0.0      S
    43  *   1985 JUL  1 =JD 2446247.5  TAI-UTC=  23.0       S + (MJD - 41317.) X 0.0      S
    44  *   1988 JAN  1 =JD 2447161.5  TAI-UTC=  24.0       S + (MJD - 41317.) X 0.0      S
    45  *   1990 JAN  1 =JD 2447892.5  TAI-UTC=  25.0       S + (MJD - 41317.) X 0.0      S
    46  *   1991 JAN  1 =JD 2448257.5  TAI-UTC=  26.0       S + (MJD - 41317.) X 0.0      S
    47  *   1992 JUL  1 =JD 2448804.5  TAI-UTC=  27.0       S + (MJD - 41317.) X 0.0      S
    48  *   1993 JUL  1 =JD 2449169.5  TAI-UTC=  28.0       S + (MJD - 41317.) X 0.0      S
    49  *   1994 JUL  1 =JD 2449534.5  TAI-UTC=  29.0       S + (MJD - 41317.) X 0.0      S
    50  *   1996 JAN  1 =JD 2450083.5  TAI-UTC=  30.0       S + (MJD - 41317.) X 0.0      S
    51  *   1997 JUL  1 =JD 2450630.5  TAI-UTC=  31.0       S + (MJD - 41317.) X 0.0      S
    52  *   1999 JAN  1 =JD 2451179.5  TAI-UTC=  32.0       S + (MJD - 41317.) X 0.0      S
     7 *  of conversions between specific time formats.  They use the UNIX timeval time system as the
     8 *  base upon which International Atomic Time (TAI) and Universal Time Coordinated (UTC) are calculated.
    539 *
    5410 *  @author Ross Harman, MHPCC
    5511 *
    56  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    57  *  @date $Date: 2004-11-10 18:27:08 $
     12 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-12-16 18:52:51 $
    5814 *
    5915 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    6016 */
     17
    6118
    6219#ifndef PSTIME_H
     
    10764 * @return  bool: True for success, false for failure.
    10865 */
    109 psBool psTimeInit(const char *fileName);
     66psBool p_psTimeInit(const char *fileName);
    11067
    11168/** Free memory persistant time data.
     
    11572 * @return  void: void.
    11673 */
    117 psBool psTimeFinalize(void);
     74psBool p_psTimeFinalize(void);
    11875
    11976/** Allocate time struct.
     
    305262);
    306263
    307 /** Add two TAI times.
    308  *
    309  *  Adds two TAI times. This function does not add or subtract leapseconds.
     264/** Adds delta to time. Result is in TAI time.
     265 *
     266 *  Adds delta to time. Input time is converted to TAI format if necessary.
    310267 *
    311268 *  @return  psTime*: time.
    312269 */
    313 psTime* psTimeAdd(
    314     const psTime *tai1,                 ///< First TAI time.
    315     const psTime *tai2                  ///< Second TAI time.
    316 );
    317 
    318 /** Subtract two TAI times.
    319  *
    320  *  Subtracts two TAI times. This function does not add or subtract leapseconds.
    321  *
    322  *  @return  psTime*: time.
    323  */
    324 psTime* psTimeSubtract(
    325     const psTime *tai1,                 ///< First TAI time.
    326     const psTime *tai2                  ///< Second TAI time.
    327 );
    328 
    329 
    330 /** Find delta of two TAI times.
    331  *
    332  *  Finds the absolute difference of two TAI times. This function does not add or subtract leapseconds.
    333  *
    334  *  @return  psTime*: time.
    335  */
    336 psTime* psTimeDelta(
    337     const psTime *tai1,                 ///< First TAI time.
    338     const psTime *tai2                  ///< Second TAI time.
     270psTime* psTimeMath(
     271    const psTime *time,                 ///< Time.
     272    psF64 delta                         ///< Time delta.
     273);
     274
     275/** Determine difference between two times. Result is in TAI time.
     276 *
     277 *  Determine difference between two times. Input times are converted to TAI format if necessary.
     278 *
     279 *  @return psF64: Time difference.
     280 */
     281psF64 psTimeDelta(
     282    const psTime *time1,                ///< First time.
     283    const psTime *time2                 ///< Second time.
    339284);
    340285
Note: See TracChangeset for help on using the changeset viewer.