IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 22678


Ignore:
Timestamp:
Feb 26, 2009, 8:37:23 AM (17 years ago)
Author:
Paul Price
Message:

Cleaning up

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

Legend:

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

    r20595 r22678  
    99 *
    1010 *  @author Ross Harman, MHPCC
     11 *  @author Paul Price, IfA
    1112 *
    12  *  @version $Revision: 1.117 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2008-11-09 00:30:07 $
    14  *
    15  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     13 *  Copyright 2004-2009 Institute for Astronomy, University of Hawaii
    1614 */
    1715
    1816#ifdef HAVE_CONFIG_H
    19 # include "config.h"
     17#include "config.h"
    2018#endif
    2119
     
    3937#include "psAssert.h"
    4038
    41 #define MAX_STRING_LENGTH 256
    42 
    43 /** Sidereal angular conversion from seconds to radians for GMST in seconds (i.e. pi/(180*240)) */
    44 #define S2R (7.272205216643039903848711535369e-5)
    45 
    46 /** Two times pi with double precision accuracy */
    47 #define TWOPI (2.0*M_PI)
    48 
    49 /** Conversion from radians to degrees */
    50 #define R2DEG = (180.0/M_PI)
    51 
    52 /** Maximum length of time string */
    53 #define MAX_TIME_STRING_LENGTH 256
    54 
    55 /** Seconds per minute */
    56 #define  SEC_PER_MINUTE 60.0
    57 
    58 /** Seconds per hour */
    59 #define  SEC_PER_HOUR (60.0*SEC_PER_MINUTE)
    60 
    61 /** Seconds per day */
    62 #define  SEC_PER_DAY (24.0*SEC_PER_HOUR)
    63 
    64 /** Seconds per year */
    65 #define  SEC_PER_YEAR (365.0*SEC_PER_DAY)
    66 
    67 /** Microseconds per day */
    68 #define NSEC_PER_DAY 86400000000000.0
    69 
    70 // Time config file path
    71 static char *timeConfig = NULL;
    72 
    73 /** Time metadata read from config file */
    74 static psMetadata *timeMetadata = NULL;
    75 
    76 // Offset to convert terrestrial time(TT) to international atomic time(TAI)
    77 #define  TAI_TT_OFFSET_SECONDS        32
    78 #define  TAI_TT_OFFSET_NANOSECONDS    184000000
    79 
    80 // Offset from converting to MJD
    81 #define  MJD_EPOCH_OFFSET             40587.0
    82 
    83 // Offset for converting to JD
    84 #define  JD_EPOCH_OFFSET              2440587.5
    85 
    86 // Offset of year 0000 from epoch
    87 #define YEAR_0000_SEC                 -62125920000
    88 
    89 // Offset of year 9999 from epoch
    90 #define YEAR_9999_SEC                 253202544000
     39
     40#define S2R (M_PI / (180.0 * 240.0))    /// Sidereal conversion: GMST in seconds to radians
     41#define R2DEG = (180.0/M_PI)            /// Conversion from radians to degrees
     42#define MAX_STRING_LENGTH 256           /// Maximum length of string
     43#define MAX_TIME_STRING_LENGTH 256      /// Maximum length of time string
     44#define SEC_PER_MINUTE 60.0             /// Seconds per minute
     45#define SEC_PER_HOUR (60.0*SEC_PER_MINUTE) /// Seconds per hour
     46#define SEC_PER_DAY (24.0*SEC_PER_HOUR) /// Seconds per day
     47#define SEC_PER_YEAR (365.0*SEC_PER_DAY) /// Seconds per year
     48#define NSEC_PER_DAY (SEC_PER_DAY * 1000000000.0) /// Nanoseconds per day
     49
     50#define MJD_EPOCH_OFFSET 40587.0        // Offset from converting to MJD
     51#define JD_EPOCH_OFFSET 2440587.5       // Offset for converting to JD
     52#define YEAR_0000_SEC -62125920000      // Offset of year 0000 from epoch
     53#define YEAR_9999_SEC 253202544000      // Offset of year 9999 from epoch
     54
     55// Offset to convert terrestrial time (TT) to international atomic time (TAI)
     56#define TAI_TT_OFFSET_SECONDS        32
     57#define TAI_TT_OFFSET_NANOSECONDS    184000000
     58
     59
     60// Static global variables
     61static char *timeConfig = NULL;         // Time config file path
     62static psMetadata *timeMetadata = NULL; // Time metadata read from config file
     63
    9164
    9265/** Static function prototypes */
     
    9972static psTime* convertTimeUTCUT1(psTime* time);
    10073
    101 static bool p_psTimeInit(const char *fileName);
     74static bool timeInit(const char *fileName);
    10275
    10376/** Removes leading and trailing whitespace and # characters from a string. The cleaned string is a new null
    10477 *  terminated copy of the original input string. */
    105 static char *cleanString(char *inString,
    106                          int sLen)
    107 {
    108     char *ptrB = NULL;
    109     char *ptrE = NULL;
    110     char *cleaned = NULL;
    111 
    112     ptrB = inString;
     78static char *cleanString(char *inString,// Input string
     79                         int sLen       // Length of string
     80                         )
     81{
     82    char *ptrB = inString;              // Pointer to start
    11383
    11484    // Skip over leading # or whitespace
    115     while (isspace(*ptrB) || *ptrB=='#') {
     85    while (isspace(*ptrB) || *ptrB == '#') {
    11686        ptrB++;
    11787    }
    11888
    11989    // Skip over trailing whitespace, null terminators, and # characters
    120     ptrE = inString + sLen - 1;
    121     while(isspace(*ptrE) || *ptrE=='\0' || *ptrE=='#') {
     90    char *ptrE = inString + sLen - 1;   // Pointer to end
     91    while (isspace(*ptrE) || *ptrE == '\0' || *ptrE == '#') {
    12292        ptrE--;
    12393    }
     
    12797
    12898    // Adds '\0' to end of string and +1 to sLen
    129     cleaned = psStringNCopy(ptrB, sLen);
    130 
    131     return cleaned;
     99    return psStringNCopy(ptrB, sLen);
    132100}
    133101
    134102/** Returns cleaned token based on delimiter, but not including delimiter. Also changes the pointer location
    135103 * the beginning of the string. Tokens are newly allocated null terminated strings. */
    136 static char* getToken(char **inString,
    137                       char *delimiter,
    138                       psParseErrorType *status)
    139 {
    140     char *cleanToken = NULL;
    141     int sLen = 0;
    142 
     104static char* getToken(char **inString,  // Input string
     105                      char *delimiter,  // Delimiter
     106                      psParseErrorType *status // Parsing status, returned
     107                      )
     108{
    143109    // Skip over leading whitespace
    144     while(isspace(**inString)) {
     110    while (isspace(**inString)) {
    145111        (*inString)++;
    146112    }
    147113
    148     // Length of token, not including delimiter
    149     sLen = strcspn(*inString, delimiter);
    150 
    151     if(sLen) {
    152 
     114    int sLen = strcspn(*inString, delimiter); // Length of token, not including delimiter
     115    char *cleanToken = NULL;            // Token, cleaned of delimiters
     116    if (sLen) {
    153117        // Create new, cleaned, and null terminated token
    154118        cleanToken = cleanString(*inString, sLen);
    155119
    156120        // Move to end of token
    157         //        (*inString) += (sLen+1);
    158121        (*inString) += sLen;
    159122        if (**inString != '\0' ) {
    160123            (*inString)++;
    161124        }
    162 
    163     } else if(**inString!='\0' && sLen==0) {
     125    } else if (**inString != '\0' && sLen == 0) {
    164126        *status = PS_PARSE_ERROR_GENERAL;
    165127    }
     
    210172
    211173    // Check if psTime tables are already loaded
    212     if(!p_psTimeInit(p_psTimeConfigFilename(NULL))) {
     174    if (!timeInit(p_psTimeConfigFilename(NULL))) {
    213175        *status = PS_LOOKUP_ERROR;
    214176        return NAN;
     
    225187
    226188        // Check if table not a metadata item
    227         if(tableMetadataItem == NULL) {
     189        if (tableMetadataItem == NULL) {
    228190            psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
    229191                    tableName);
     
    239201
    240202        // Check if index within to/from range
    241         if(index >= table->validFrom ) {
    242             if(index <= table->validTo) {
     203        if (index >= table->validFrom ) {
     204            if (index <= table->validTo) {
    243205                // Attempt to interpolate table
    244206                result = psLookupTableInterpolate(table, index, column);
    245207                *status = PS_LOOKUP_SUCCESS;
    246                 if(!isnan(result)) {
     208                if (!isnan(result)) {
    247209                    break;
    248210                }
     
    258220}
    259221
    260 bool p_psTimeInit(const char *fileName)
     222static bool timeInit(const char *fileName)
    261223{
    262224    psS32 numLines = 0;
     
    270232    char *tableName = NULL;
    271233    char *tableFormat = NULL;
    272     char *fullTableName = NULL;
    273     psS32 i = 0;
    274     psS32 j = 0;
    275234    psS32 numTables = 0;
    276235    psU32 nFail = 0;
     
    283242    char metadataTableNames[4][MAX_STRING_LENGTH] = {"daily", "eopc",  "finals", "tai"};
    284243
    285     // Check if the p_psTimeInit has already been ran
    286     if (timeMetadata != NULL) {
     244    // Check if the timeInit has already been run
     245    if (timeMetadata) {
    287246        return true;
    288247    }
    289248
     249    // All memory allocated below is "persistent"
    290250    // XXX this is not thread safe as the persistence setting is global
    291     const bool initialPersistence =
    292         p_psMemAllocatePersistent(true); // All memory allocated below is "persistent"
     251    const bool initialPersistence = p_psMemAllocatePersistent(true); // Initial setting of persistence
    293252
    294253    // Read config file
    295254    timeMetadata = psMetadataConfigRead(timeMetadata, &nFail, fileName, true);
    296     if(timeMetadata == NULL) {
     255    if (!timeMetadata) {
     256        psError(PS_ERR_IO, false, "Unable to read time configuration file %s", fileName);
    297257        return false;
    298     } else if(nFail != 0) {
     258    } else if (nFail != 0) {
     259        psError(PS_ERR_IO, false, "Failed to parse %d lines reading time configuration file %s",
     260                nFail, fileName);
    299261        return false;
    300262    }
     
    302264    // Get number of tables
    303265    metadataItem = psMetadataLookup(timeMetadata, "psLib.time.tables.n");
    304     if(metadataItem == NULL) {
     266    if (metadataItem == NULL) {
    305267        p_psMemAllocatePersistent(initialPersistence);
    306268        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
     
    312274    // Get lower range of tables
    313275    metadataItem = psMetadataLookup(timeMetadata, "psLib.time.tables.from");
    314     if(metadataItem == NULL) {
     276    if (metadataItem == NULL) {
    315277        p_psMemAllocatePersistent(initialPersistence);
    316278        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
     
    319281    }
    320282    tablesFrom = psVectorCopy(tablesFrom, metadataItem->data.V, PS_TYPE_F64);
    321     if(tablesFrom->n != numTables) {
     283    if (tablesFrom->n != numTables) {
    322284        p_psMemAllocatePersistent(initialPersistence);
    323285        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Incorrect vector size. Size: %ld, Expected %d."), tablesFrom->n, numTables);
     
    328290    // Get upper range of tables
    329291    metadataItem = psMetadataLookup(timeMetadata, "psLib.time.tables.to");
    330     if(metadataItem == NULL) {
     292    if (metadataItem == NULL) {
    331293        p_psMemAllocatePersistent(initialPersistence);
    332294        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
     
    336298    }
    337299    tablesTo = psVectorCopy(tablesTo, metadataItem->data.V, PS_TYPE_F64);
    338     if(tablesTo->n != numTables) {
     300    if (tablesTo->n != numTables) {
    339301        p_psMemAllocatePersistent(initialPersistence);
    340302        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Incorrect vector size. Size: %ld, Expected %d."), tablesTo->n, numTables);
     
    346308    // Get index columns for the tables
    347309    metadataItem = psMetadataLookup(timeMetadata, "psLib.time.tables.index");
    348     if(metadataItem == NULL) {
     310    if (metadataItem == NULL) {
    349311        p_psMemAllocatePersistent(initialPersistence);
    350312        psError(PS_ERR_BAD_PARAMETER_VALUE,true,_("Failed find '%s' in time metadata."),
     
    355317    }
    356318    tablesIndex = psVectorCopy(tablesIndex, metadataItem->data.V, PS_TYPE_S32);
    357     if(tablesIndex->n != numTables) {
     319    if (tablesIndex->n != numTables) {
    358320        p_psMemAllocatePersistent(initialPersistence);
    359321        psError(PS_ERR_BAD_PARAMETER_VALUE,true,_("Incorrect vector size. Size: %ld, Expected %d."),tablesIndex->n,numTables);
     
    366328    // Get path to time data files
    367329    metadataItem = psMetadataLookup(timeMetadata, "psLib.time.tables.dir");
    368     if(metadataItem == NULL) {
     330    if (metadataItem == NULL) {
    369331        p_psMemAllocatePersistent(initialPersistence);
    370332        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
     
    379341    // Table file names
    380342    metadataItem = psMetadataLookup(timeMetadata, "psLib.time.tables.files");
    381     if(metadataItem == NULL) {
     343    if (metadataItem == NULL) {
    382344        p_psMemAllocatePersistent(initialPersistence);
    383345        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
     
    394356    // Get table format strings
    395357    metadataItem = psMetadataLookup(timeMetadata, "psLib.time.tables.format");
    396     if(metadataItem == NULL) {
     358    if (metadataItem == NULL) {
    397359        p_psMemAllocatePersistent(initialPersistence);
    398360        psError(PS_ERR_BAD_PARAMETER_VALUE,true, _("Failed find '%s' in time metadata."),
     
    411373    bool no_problem = true;  // True if we've detected no errors
    412374    namesPtr = tableNames;
    413     while((tableName=getToken(&namesPtr, " ", &status)) != NULL) {
    414 
    415         // Form path with table name, adding one to length for last '/' that may not occur
    416         // in string in cong file
    417         fullTableName = (char*)psAlloc(strlen(tableDir)+strlen(tableName)+1+1);
    418 
    419         // Old strings may come back from psAlloc(), so set initial position to EOL
    420         fullTableName[0]='\0';
    421         strcat(fullTableName, tableDir);
    422         strcat(fullTableName, "/");
    423         strcat(fullTableName, tableName);
     375    int i;                              // Iterator
     376    for (i = 0; (tableName = getToken(&namesPtr, " ", &status)) != NULL; i++) {
     377        psString fullTableName = NULL;  // Full path for table
     378        psStringAppend(&fullTableName, "%s/%s", tableDir, tableName);
    424379
    425380        // Get table format
    426         tableFormat = getToken(&formatPtr,",",&status);
    427         if(tableFormat == NULL) {
    428             psError(PS_ERR_BAD_PARAMETER_VALUE,no_problem,_("Failed find '%s' in time metadata."),
     381        tableFormat = getToken(&formatPtr, ",", &status);
     382        if (!tableFormat) {
     383            psError(PS_ERR_BAD_PARAMETER_VALUE, no_problem, _("Failed find '%s' in time metadata."),
    429384                    "psLib.time.tables.format");
    430385            no_problem = false;
     
    432387
    433388        // Create and read table
    434         if(i < numTables) {
     389        if (i < numTables) {
    435390            table = psLookupTableAlloc(fullTableName, (const char*)tableFormat, tablesIndex->data.S32[i]);
    436391            numLines = psLookupTableRead(table);
    437392        } else {
    438393            psError(PS_ERR_BAD_PARAMETER_VALUE, no_problem,
    439                     _("Incorrect number of table files entered. Found: %d. Expected: %d."), i+1, numTables);
     394                    _("Incorrect number of table files entered. Found: %d. Expected: %d."), i + 1, numTables);
    440395            no_problem = false;
    441396        }
     
    443398        // Place tables into metadata slightly altered names as keys to create consistent naming conventions
    444399        foundTable = false;
    445         for(j=0; j<numTables; j++) {
     400        for (int j = 0; j < numTables; j++) {
    446401            metadataNamesPtr = strstr(tableName, metadataTableNames[j]);
    447             if(metadataNamesPtr != NULL) {
     402            if (metadataNamesPtr != NULL) {
    448403                psMetadataAdd(timeMetadata, PS_LIST_TAIL, strcat(metadataTableNames[j], "Table"),
    449404                              PS_DATA_LOOKUPTABLE, NULL, table);
    450405                foundTable = true;
    451             } else if(foundTable==false && j==numTables-1) {
     406            } else if (foundTable == false && j == numTables - 1) {
    452407                psError(PS_ERR_BAD_PARAMETER_VALUE, no_problem,
    453408                        _("Incorrect number of table files entered. Found: %d. Expected: %d."), j, numTables);
     
    460415        psFree(tableFormat);
    461416        psFree(table);
    462         i++;
    463417    }
    464418
    465419    p_psMemAllocatePersistent(initialPersistence);
    466420
    467     if(numTables != i) {
    468         psError(PS_ERR_BAD_PARAMETER_VALUE, no_problem, _("Incorrect number of table files entered. Found: %d. Expected: %d."), i, numTables);
     421    if (numTables != i) {
     422        psError(PS_ERR_BAD_PARAMETER_VALUE, no_problem,
     423                _("Incorrect number of table files entered. Found: %d. Expected: %d."),
     424                i, numTables);
    469425    }
    470426
     
    481437bool p_psTimeFinalize(void)
    482438{
    483     if(timeMetadata != NULL) {
     439    if (timeMetadata != NULL) {
    484440        psFree(timeMetadata);
    485441        timeMetadata = NULL;
     
    496452psTime* psTimeAlloc(psTimeType type)
    497453{
    498     psTime *outTime = NULL;
    499 
    500454    // Error checks
    501     if(type!=PS_TIME_TAI && type!=PS_TIME_UTC && type!=PS_TIME_UT1 &&
    502             type!=PS_TIME_TT) {
    503         psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    504                 _("Specified type, %d, is not supported."),
    505                 type);
    506         return NULL;
     455    switch (type) {
     456      case PS_TIME_TAI:
     457      case PS_TIME_UTC:
     458      case PS_TIME_UT1:
     459      case PS_TIME_TT:
     460        // No action
     461        break;
     462      default:
     463        // Since we can never return NULL from an allocator
     464        psAbort("Specified type, %d, is not supported.", type);
    507465    }
    508466
    509467    // Allocate memory for structure
    510     outTime = (psTime*)psAlloc(sizeof(psTime));
     468    psTime *outTime = psAlloc(sizeof(psTime));
    511469    psMemSetDeallocator(outTime, (psFreeFunc)timeFree);
     470
    512471    // Initialize members
    513472    outTime->sec = 0;
     
    522481bool psMemCheckTime(psPtr ptr)
    523482{
    524     return ( psMemGetDeallocator(ptr) == (psFreeFunc)timeFree );
     483    return (psMemGetDeallocator(ptr) == (psFreeFunc)timeFree);
    525484}
    526485
     
    528487psTime* psTimeGetNow(psTimeType type)
    529488{
    530     struct timeval now;
    531     psTime *time = NULL;
    532 
    533     // Allocate psTime struct
    534     time = psTimeAlloc(type);
    535 
    536     // Verify time structure allocated
    537     if(time == NULL) {
    538         return NULL;
    539     }
    540 
    541489    // Get the system time
    542     //    if (gettimeofday(&now, (struct timezone *)0) == -1) {
     490    struct timeval now;                 // Current time
    543491    if (gettimeofday(&now, 0) == -1) {
    544492        psError(PS_ERR_OS_CALL_FAILED, true,
     
    547495    }
    548496
     497    // Allocate psTime struct
     498    psTime *time = psTimeAlloc(type);   // Container for current time
     499
    549500    // Convert timeval time to psTime
    550501    time->sec = now.tv_sec;
    551     time->nsec = now.tv_usec*1000;
     502    time->nsec = now.tv_usec * 1000;
    552503
    553504    // Add most leapseconds to UTC time to get TAI time if necessary
    554     if(type == PS_TIME_TAI) {
     505    if (type == PS_TIME_TAI) {
    555506        time->sec += p_psTimeGetTAIDelta(time);
    556507    }
     
    575526
    576527    // Check for underflow in nsec
    577     if(deltaNsec > time->nsec) {
     528    if (deltaNsec > time->nsec) {
    578529        // Borrow second
    579530        time->nsec += 1e9;
     
    585536
    586537    // Check for overflow in nsec
    587     if(time->nsec >= 1e9) {
     538    if (time->nsec >= 1e9) {
    588539        time->nsec -= 1e9;
    589540        time->sec++;
     
    595546    // Check if leapsecond present in delta
    596547    deltaUTC = p_psTimeGetTAIDelta(time);
    597     if(fabs(deltaTAI-deltaUTC) >= 1.0) {
     548    if (fabs(deltaTAI-deltaUTC) >= 1.0) {
    598549        time->sec++;
    599550    }
     
    621572
    622573    // Check for overflow in nsec
    623     if(time->nsec >= 1e9) {
     574    if (time->nsec >= 1e9) {
    624575        time->nsec -= 1e9;
    625576        time->sec++;
     
    641592
    642593    // Check for overflow in nsec
    643     if(time->nsec >= 1e9) {
     594    if (time->nsec >= 1e9) {
    644595        time->nsec -= 1e9;
    645596        time->sec++;
     
    658609
    659610    // Check for nsec underflow
    660     if(TAI_TT_OFFSET_NANOSECONDS > time->nsec) {
     611    if (TAI_TT_OFFSET_NANOSECONDS > time->nsec) {
    661612        // Borrow second
    662613        time->sec--;
     
    666617
    667618    // Check for overflow in nsec
    668     if(time->nsec >= 1e9) {
     619    if (time->nsec >= 1e9) {
    669620        time->nsec -= 1e9;
    670621        time->sec++;
     
    685636
    686637    // Since UTC is within 0.9 sec of UT1 then nsec member is the member affected
    687     if((ut1utc < 0) && (abs(ut1utc) > time->nsec)) {
     638    if ((ut1utc < 0) && (abs(ut1utc) > time->nsec)) {
    688639        // Borrow from sec
    689640        time->sec--;
    690         if(time->leapsecond) {
     641        if (time->leapsecond) {
    691642            time->leapsecond = false;
    692643        } else {
     
    699650
    700651    // Check for overflow in nsec
    701     if(time->nsec >= 1e9) {
     652    if (time->nsec >= 1e9) {
    702653        time->nsec -= 1e9;
    703654        time->sec++;
    704         if(time->leapsecond) {
     655        if (time->leapsecond) {
    705656            time->leapsecond = false;
    706657            time->sec--;
     
    724675
    725676    // If the input type is UT1 then return time and generate error message
    726     if(time->type == PS_TIME_UT1) {
     677    if (time->type == PS_TIME_UT1) {
    727678        psError(PS_ERR_BAD_PARAMETER_VALUE,true,"Cannot convert from UT1 time type");
    728679        return NULL;
     
    735686
    736687    // Convert from TAI to UTC, TT, UT1
    737     if(time->type == PS_TIME_TAI) {
     688    if (time->type == PS_TIME_TAI) {
    738689        // Convert from TAI to UTC
    739         if(type == PS_TIME_UTC) {
     690        if (type == PS_TIME_UTC) {
    740691            time = convertTimeTAIUTC(time);
    741692            // Convert from TAI to TT
    742         } else if(type == PS_TIME_TT) {
     693        } else if (type == PS_TIME_TT) {
    743694            time = convertTimeTAITT(time);
    744695            // Convert from TAI to UT1
    745         } else if(type == PS_TIME_UT1) {
     696        } else if (type == PS_TIME_UT1) {
    746697            // Convert to UTC first
    747698            time = convertTimeTAIUTC(time);
     
    754705        }
    755706        // Convert from TT to TAI, UTC, UT1
    756     } else if(time->type == PS_TIME_TT) {
     707    } else if (time->type == PS_TIME_TT) {
    757708        // Convert from TT to UTC
    758         if(type == PS_TIME_UTC) {
     709        if (type == PS_TIME_UTC) {
    759710            // Convert to TAI time first
    760711            time = convertTimeTTTAI(time);
     
    762713            time = convertTimeTAIUTC(time);
    763714            // Convert from TT to TAI
    764         } else if(type == PS_TIME_TAI) {
     715        } else if (type == PS_TIME_TAI) {
    765716            time = convertTimeTTTAI(time);
    766717            // Convert from TT to UT1
    767         } else if(type == PS_TIME_UT1) {
     718        } else if (type == PS_TIME_UT1) {
    768719            // Convert to UTC first
    769720            // Convert to TAI time first
     
    779730        }
    780731        // Convert from UTC to TAI, TT, UT1
    781     } else if(time->type == PS_TIME_UTC) {
     732    } else if (time->type == PS_TIME_UTC) {
    782733        // Convert UTC to TAI
    783         if(type == PS_TIME_TAI) {
     734        if (type == PS_TIME_TAI) {
    784735            time = convertTimeUTCTAI(time);
    785736            // Convert UTC to TT
    786         } else if(type == PS_TIME_TT) {
     737        } else if (type == PS_TIME_TT) {
    787738            // Convert to TAI time first
    788739            time = convertTimeUTCTAI(time);
     
    790741            time = convertTimeTAITT(time);
    791742            // Convert UTC to UT1
    792         } else if(type == PS_TIME_UT1) {
     743        } else if (type == PS_TIME_UT1) {
    793744            time = convertTimeUTCUT1(time);
    794745            // Convert UTC to unknown time type
     
    831782
    832783    // Verify input time is not in UT1 seconds
    833     if(time->type == PS_TIME_UT1) {
     784    if (time->type == PS_TIME_UT1) {
    834785        psError(PS_ERR_BAD_PARAMETER_VALUE,true,_("Specified type, %d, is incorrect."),time->type);
    835786        return NAN;
     
    864815    // Calculate Greenwich Mean Sidereal Time (GMST) in radians.
    865816    // Equation set up to minimize multiplications.
    866     gmstRad = fracDays*TWOPI
    867               + (const1+const2*tu+t*(const3+t*(const4+t*(const5+const6*t))))*S2R;
     817    gmstRad = fracDays * 2 * M_PI +
     818        (const1 + const2 * tu + t * (const3 + t * (const4 + t * (const5 + const6 * t)))) * S2R;
    868819
    869820    // Place GMST between 0 and 2*pi
    870     gmstRad = fmod(gmstRad, TWOPI);
     821    gmstRad = fmod(gmstRad, 2 * M_PI);
    871822
    872823    // Calculate Local Mean Sidereal Time (LMST) in radians
     
    899850
    900851    // Check for invalid bulletin specified
    901     if((bulletin != PS_IERS_A) && (bulletin != PS_IERS_B)) {
     852    if ((bulletin != PS_IERS_A) && (bulletin != PS_IERS_B)) {
    902853        psError(PS_ERR_BAD_PARAMETER_VALUE,true,"Invalid bulletin specified %d",bulletin);
    903854        return NAN;
     
    905856
    906857    // Check if psTime tables are already loaded
    907     if(!p_psTimeInit(p_psTimeConfigFilename(NULL))) {
     858    if (!timeInit(p_psTimeConfigFilename(NULL))) {
    908859        psError(PS_ERR_UNKNOWN, true, "failed to init time tables.");
    909860        return NAN;
     
    911862
    912863    // Set lookup table column based on Bullentin
    913     if(bulletin == PS_IERS_A) {
     864    if (bulletin == PS_IERS_A) {
    914865        tableColumn = 3;
    915866    } else {
     
    922873
    923874    // Value could not be found through table lookup and interpolation
    924     if(status == PS_LOOKUP_PAST_TOP) {
     875    if (status == PS_LOOKUP_PAST_TOP) {
    925876
    926877        // Date too early for tables. Get default time delta value from metadata, and issue warning.
     
    929880        // Lookup value from time metadata loaded from pslib.config
    930881        tableMetadataItem = psMetadataLookup(timeMetadata, "psLib.time.before.dut");
    931         if(tableMetadataItem == NULL) {
     882        if (tableMetadataItem == NULL) {
    932883            psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
    933884                    "psLib.time.before.dut");
     
    936887        result = tableMetadataItem->data.F64;
    937888
    938     } else if(status == PS_LOOKUP_PAST_BOTTOM) {
     889    } else if (status == PS_LOOKUP_PAST_BOTTOM) {
    939890        /* Date too late for tables. Issue warning and use following formulae for predicting
    940891           ahead of the most recent available table entry.
     
    949900        // Lookup values to calculate prediction
    950901        tableMetadataItem = psMetadataLookup(timeMetadata, "psLib.time.predict.dut");
    951         if(tableMetadataItem == NULL) {
     902        if (tableMetadataItem == NULL) {
    952903            psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
    953904                    "psLib.time.predict.dut");
     
    958909
    959910        // Calculate predication of future UT1-UTC
    960         t = 2000.0 + (mjd - 51544.03)/365.2422;
    961         dut2ut1 = 0.022*sin(TWOPI*t) - 0.012*cos(TWOPI*t) - 0.006*sin(4.0*M_PI*t) + 0.007*cos(4.0*M_PI*t);
    962         result = dut->data.F64[0] + dut->data.F64[1]*(mjd - dut->data.F64[2]) - dut2ut1;
    963 
    964     } else if(status != PS_LOOKUP_SUCCESS) {
     911        t = 2000.0 + (mjd - 51544.03) / 365.2422;
     912        dut2ut1 = 0.022 * sin(2 * M_PI * t) - 0.012 * cos(2 * M_PI * t) -
     913            0.006 * sin(4.0 * M_PI * t) + 0.007 * cos(4.0 * M_PI * t);
     914        result = dut->data.F64[0] + dut->data.F64[1] * (mjd - dut->data.F64[2]) - dut2ut1;
     915
     916    } else if (status != PS_LOOKUP_SUCCESS) {
    965917        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed time table interpolation."));
    966918        return NAN;
     
    10861038    PS_ASSERT_INT_WITHIN_RANGE(time->nsec,0,(psU32)((1e9)-1),NULL);
    10871039
    1088     if(time->type != PS_TIME_TAI) {
     1040    if (time->type != PS_TIME_TAI) {
    10891041        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Specified type, %d, is incorrect."), time->type);
    10901042        return NULL;
     
    10921044
    10931045    // Check if psTime tables are already loaded
    1094     if(!p_psTimeInit(p_psTimeConfigFilename(NULL))) {
     1046    if (!timeInit(p_psTimeConfigFilename(NULL))) {
    10951047        psError(PS_ERR_UNKNOWN, true, "failed to init time tables.");
    10961048        return NULL;
     
    11051057
    11061058    // Value could not be found through table lookup and interpolation
    1107     if(xStatus==PS_LOOKUP_PAST_TOP && yStatus==PS_LOOKUP_PAST_TOP) {
     1059    if (xStatus==PS_LOOKUP_PAST_TOP && yStatus==PS_LOOKUP_PAST_TOP) {
    11081060
    11091061        // Date too earlier for tables. Get default polar coodinate values from metadata, and issue warning.
     
    11171069
    11181070        tableMetadataItem = psMetadataLookup(timeMetadata, "psLib.time.before.xp");
    1119         if(tableMetadataItem == NULL) {
     1071        if (tableMetadataItem == NULL) {
    11201072            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    11211073                    _("Failed find '%s' in time metadata."), "psLib.time.before.xp");
     
    11251077
    11261078        tableMetadataItem = psMetadataLookup(timeMetadata, "psLib.time.before.yp");
    1127         if(tableMetadataItem == NULL) {
     1079        if (tableMetadataItem == NULL) {
    11281080            psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."), "psLib.time.before.yp");
    11291081            return NULL;
     
    11311083        y = tableMetadataItem->data.F64;
    11321084
    1133     } else if(xStatus==PS_LOOKUP_PAST_BOTTOM && yStatus==PS_LOOKUP_PAST_BOTTOM) {
     1085    } else if (xStatus==PS_LOOKUP_PAST_BOTTOM && yStatus==PS_LOOKUP_PAST_BOTTOM) {
    11341086
    11351087        /* Date too late for tables. Issue warning and use following formulae for predicting
     
    11541106        // Get predicted MJD
    11551107        tableMetadataItem = psMetadataLookup(timeMetadata, "psLib.time.predict.mjd");
    1156         if(tableMetadataItem == NULL) {
     1108        if (tableMetadataItem == NULL) {
    11571109            psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."),
    11581110                    "psLib.time.predict.mjd");
     
    11631115        // Get xp
    11641116        tableMetadataItem = psMetadataLookup(timeMetadata, "psLib.time.predict.xp");
    1165         if(tableMetadataItem == NULL) {
     1117        if (tableMetadataItem == NULL) {
    11661118            psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."), "psLib.time.predict.xp");
    11671119            return NULL;
     
    11721124        // Get yp
    11731125        tableMetadataItem = psMetadataLookup(timeMetadata, "psLib.time.predict.yp");
    1174         if(tableMetadataItem == NULL) {
     1126        if (tableMetadataItem == NULL) {
    11751127            psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."), "psLib.time.predict.yp");
    11761128            return NULL;
     
    11801132
    11811133        // Calculate "a" and "c" constants
    1182         a = TWOPI*(mjd - mjdPred)/365.25;
    1183         c = TWOPI*(mjd - mjdPred)/435.0;
     1134        a = 2 * M_PI * (mjd - mjdPred) / 365.25;
     1135        c = 2 * M_PI * (mjd - mjdPred) / 435.0;
    11841136
    11851137        // Calculate x and y polar coordinates
     
    11961148            yp->data.F64[4]*sin(c);
    11971149
    1198     } else if(xStatus!=PS_LOOKUP_SUCCESS || yStatus!=PS_LOOKUP_SUCCESS) {
     1150    } else if (xStatus!=PS_LOOKUP_SUCCESS || yStatus!=PS_LOOKUP_SUCCESS) {
    11991151        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed time table interpolation."));
    12001152        return NULL;
     
    12261178
    12271179    // Check if psTime tables are loaded/loadable
    1228     if (!p_psTimeInit(p_psTimeConfigFilename(NULL))) {
     1180    if (!timeInit(p_psTimeConfigFilename(NULL))) {
    12291181        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed to open file %s."),
    12301182                p_psTimeConfigFilename(NULL));
     
    12341186    // Get table from metadata
    12351187    tableMetadataItem = psMetadataLookup(timeMetadata, "taiTable");
    1236     if(tableMetadataItem == NULL) {
     1188    if (tableMetadataItem == NULL) {
    12371189        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed find '%s' in time metadata."), "taiTable");
    12381190        return NAN;
     
    12421194
    12431195    // Determine Julian and modified Julian dates used in table lookup and time delta calculation
    1244     if(time->sec < 0) {
     1196    if (time->sec < 0) {
    12451197        // psTime earlier than epoch
    12461198        jd = time->sec / SEC_PER_DAY - time->nsec / NSEC_PER_DAY + JD_EPOCH_OFFSET;
     
    12531205
    12541206    // Set ceiling of the julian date to the last entry in the lookup table
    1255     if(table->validTo < jd) {
     1207    if (table->validTo < jd) {
    12561208        jd = table->validTo;
    12571209    }
     
    12611213
    12621214    // Check for successful interpolation
    1263     if(results == NULL) {
     1215    if (results == NULL) {
    12641216        psError(PS_ERR_BAD_PARAMETER_VALUE, true, _("Failed time table interpolation."));
    12651217        return NAN;
     
    12721224
    12731225    // If const3 not equal to zero solve for difference else floor of const1
    1274     if(fabs(const3-0.0) > FLT_EPSILON) {
     1226    if (fabs(const3-0.0) > FLT_EPSILON) {
    12751227        out = const1 + (mjd - const2) * const3;
    12761228    } else {
     
    13061258
    13071259    // Verify time is UTC type
    1308     if(utc->type != PS_TIME_UTC) {
     1260    if (utc->type != PS_TIME_UTC) {
    13091261        psError(PS_ERR_BAD_PARAMETER_VALUE,true,_("Specified type, %d, is incorrect."),utc->type);
    13101262        return false;
     
    13161268
    13171269    // Check the absolute difference between the two times for leapsecond
    1318     if(psTimeLeapSecondDelta(utc,prevUtc) >= 1.0) {
     1270    if (psTimeLeapSecondDelta(utc,prevUtc) >= 1.0) {
    13191271        returnValue = true;
    13201272    } else {
     
    13431295
    13441296    // Julian date conversion
    1345     if(time2->sec < 0) {
     1297    if (time2->sec < 0) {
    13461298        // psTime earlier than epoch
    13471299        jd = time2->sec / SEC_PER_DAY - time2->nsec / NSEC_PER_DAY + JD_EPOCH_OFFSET;
     
    13691321
    13701322    // Modified Julian date conversion
    1371     if(time2->sec < 0) {
     1323    if (time2->sec < 0) {
    13721324        // psTime earlier than epoch
    13731325        mjd = time2->sec / SEC_PER_DAY - time2->nsec / NSEC_PER_DAY + MJD_EPOCH_OFFSET;
     
    14161368
    14171369    // Check if time is UTC and leapsecond
    1418     if(((time->type==PS_TIME_UTC)||(time->type==PS_TIME_UT1)) && (time->leapsecond)) {
     1370    if (((time->type==PS_TIME_UTC)||(time->type==PS_TIME_UT1)) && (time->leapsecond)) {
    14191371        // Modify second to be 60
    14201372        tempString[17] = '6';
     
    14871439    days = jd - 2440587.5;
    14881440    seconds = days * SEC_PER_DAY;
    1489     if(seconds < 0.0) {
     1441    if (seconds < 0.0) {
    14901442        outTime->nsec = (seconds - (psS64)seconds) * -1000000000.0;  // psTime earlier than epoch
    14911443    } else {
     
    15131465    seconds = days * SEC_PER_DAY;
    15141466
    1515     if(seconds < 0.0) {
     1467    if (seconds < 0.0) {
    15161468        outTime->nsec = (seconds - (psS64)seconds) * -1000000000.0;  // psTime earlier than epoch
    15171469    } else {
     
    17011653
    17021654    // Make month in range 3..14 (treat Jan & Feb as months 13..14 of prev year)
    1703     if( month <= 2 )
     1655    if ( month <= 2 )
    17041656    {
    17051657        temp = (14 - month) / 12;
     
    17071659        year -= temp;
    17081660        month += 12 * temp;
    1709     } else if(month > 14)
     1661    } else if (month > 14)
    17101662    {
    17111663        temp = (month - 3) / 12;
     
    18121764
    18131765    // Convert time to TAI if necessary, but without changing input arguments
    1814     if(time->type == PS_TIME_UTC) {
     1766    if (time->type == PS_TIME_UTC) {
    18151767        tempTime = psTimeAlloc(PS_TIME_UTC);
    18161768        tempTime->sec = time->sec;
     
    18331785
    18341786    // Convert result to same time type as input
    1835     if(time->type == PS_TIME_UTC) {
     1787    if (time->type == PS_TIME_UTC) {
    18361788        outTime = psTimeConvert(outTime, PS_TIME_UTC);
    18371789    }
     
    18581810
    18591811    // Verify both times of the same type
    1860     if(time1->type != time2->type) {
     1812    if (time1->type != time2->type) {
    18611813        psError(PS_ERR_BAD_PARAMETER_VALUE,true,_("Specified type, %d, is incorrect."),time1->type);
    18621814        return NAN;
     
    18641816
    18651817    // Convert time to TAI if necessary, but without changing input arguments
    1866     if(time1->type == PS_TIME_UTC) {
     1818    if (time1->type == PS_TIME_UTC) {
    18671819        tempTime1 = psTimeAlloc(PS_TIME_UTC);
    18681820        tempTime1->sec = time1->sec;
     
    18721824        tempTime1 = psMemIncrRefCounter((psTime*)time1);
    18731825    }
    1874     if(time2->type == PS_TIME_UTC) {
     1826    if (time2->type == PS_TIME_UTC) {
    18751827        tempTime2 = psTimeAlloc(PS_TIME_UTC);
    18761828        tempTime2->sec = time2->sec;
  • trunk/psLib/src/astro/psTime.h

    r17023 r22678  
    474474);
    475475
    476 // used by p_psEOCInit()
    477 const char *p_psTimeConfigFilename(const char *filename);
     476/// Return the name of the configuration file used for time
     477///
     478/// The name is derived first from the environment variable PS_CONFIG_FILE if set; next from a previously used
     479/// configuration file if available; finally from the default which is set at install time.
     480const char *p_psTimeConfigFilename(const char *filename // Filename to use, or NULL
     481    );
    478482
    479483/// @}
Note: See TracChangeset for help on using the changeset viewer.