IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

Added new time math functions

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.