IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 11, 2004, 3:34:09 PM (22 years ago)
Author:
desonia
Message:

adjusted the psTime functions to conform to SDRS and made psGrommitAlloc
set all the inputs to slaAoppa function.

File:
1 edited

Legend:

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

    r2032 r2048  
    1111 *  @author Ross Harman, MHPCC
    1212 *
    13  *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
    14  *  @date $Date: 2004-10-09 00:17:14 $
     13 *  @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2004-10-12 01:34:09 $
    1515 *
    1616 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2727#include "psAbort.h"
    2828#include "psImage.h"
     29#include "psCoord.h"
    2930
    3031#include "psAstronomyErrors.h"
     32
     33#ifndef SER7_FILE
     34#define SER7_FILE "../../data/ser7.dat"
     35#endif
     36
     37#ifndef TAIUTC_FILE
     38#define TAIUTC_FILE "../../data/tai-utc.dat"
     39#endif
    3140
    3241/** Sidereal angular conversion from seconds to radians for GMST in seconds (i.e. pi/(180*240)) */
     
    6170
    6271                /** Preprocessor macro to check for null psTime struct */
    63                 #define CHECK_NULL_TIME(TIME,RETURN)                                                                         \
    64                 if(TIME == NULL) {                                                                                           \
    65                     psError(__func__,"NULL value not allowed");                                                              \
    66                     return RETURN;                                                                                           \
     72                #define CHECK_NULL_TIME(TIME,RETURN) \
     73                if(TIME == NULL) { \
     74                    psError(__func__,"NULL value not allowed"); \
     75                    return RETURN; \
    6776                }
    6877
    6978                /** Preprocessor macro to allocate psTime struct */
    70                 #define ALLOC_TIME(TIME)                                                                                     \
    71                 TIME = (psTime*)psAlloc(sizeof(psTime));                                                                     \
    72 if (TIME == NULL) {                                                                                          \
    73     psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__);                                   \
    74 }                                                                                                            \
    75 TIME->sec = 0;                                                                                               \
    76 TIME->usec = 0;                                                                                              \
     79                #define ALLOC_TIME(TIME) \
     80                TIME = (psTime*)psAlloc(sizeof(psTime)); \
     81if (TIME == NULL) { \
     82    psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); \
     83} \
     84TIME->sec = 0; \
     85TIME->usec = 0; \
    7786TIME->type = PS_TIME_TAI;
    7887
    79 
    80 psImage* readTaiUtcFile(char *fileName)
     88/** Read TAI/UTC data file.
     89 *
     90 *  Reads TAI/UTC data file.
     91 *
     92 *  @return  psImage*: Two-dimensional array with table data.
     93 */
     94static psImage* readTaiUtcFile(
     95    const char *fileName                      ///< Name of file to read.
     96);
     97
     98
     99/** Read SER7 data file.
     100 *
     101 *  Reads SER7 data file.
     102 *
     103 *  @return  psImage*: Two-dimensional array with table data.
     104 */
     105static psImage* readSer7File(
     106    const char *fileName                      ///< Name of file to read.
     107);
     108
     109
     110/** Read finals data file.
     111 *
     112 *  Reads finals data file.
     113 *
     114 *  @return  psImage*: Two-dimensional array with table data.
     115 */
     116/* not yet used
     117static psImage* readFinalsFile(
     118    char *fileName                      ///< Name of file to read.
     119);
     120*/
     121
     122/** Read EOPC data file.
     123 *
     124 *  Reads EOPC data file.
     125 *
     126 *  @return  psImage*: Two-dimensional array with table data.
     127 */
     128/* not yet used
     129static psImage* readEopcFile(
     130    char *fileName                      ///< Name of file to read.
     131);
     132*/
     133
     134/** Lookup and interpolate TAI-UTC data
     135 *
     136 *  Interpolates TAI-UTC data, given time in MJD format.
     137 *
     138 *  @return  double: Interpolated value.
     139 */
     140static double lookupTaiUtcTable(
     141    const psTime *time                        ///< time to lookup.
     142);
     143
     144/** Lookup and interpolate polar or UT1-UTC data
     145 *
     146 *  Interpolates TAI-UT1 or polar coordinate data, given time in MJD format.
     147 *
     148 *  @return  double: Interpolated value.
     149 */
     150static double lookupSer7Table(
     151    const psTime *time,                        ///< time to lookup.
     152    int col                              ///< Column to lookup.
     153);
     154
     155static psImage* readTaiUtcFile(const char *fileName)
    81156{
    82157    char line[LINESIZE];
     
    113188    *(unsigned int *)&table->numRows = j;
    114189
     190    p_psMemSetPersistent(table,true);
     191    p_psMemSetPersistent(table->data.V,true);
     192    p_psMemSetPersistent(table->rawDataBuffer,true);
     193
    115194    return table;
    116195}
    117196
    118 psImage* readSer7File(char *fileName)
     197static psImage* readSer7File(const char *fileName)
    119198{
    120199    bool beginRecord = false;
     
    160239    *(unsigned int *)&table->numRows = j;
    161240
     241    p_psMemSetPersistent(table,true);
     242    p_psMemSetPersistent(table->data.V,true);
     243    p_psMemSetPersistent(table->rawDataBuffer,true);
     244
    162245    return table;
    163246}
    164247
    165 psImage* readEopcFile(char *fileName)
     248/* Not yet used...
     249static psImage* readEopcFile(char *fileName)
    166250{
    167251    char line[LINESIZE];
     
    171255    psImage *table = NULL;
    172256    FILE *fd = NULL;
    173 
    174 
     257 
     258 
    175259    fd=fopen(fileName, "r");
    176260    if(fd==NULL) {
     
    178262        return NULL ;
    179263    }
    180 
     264 
    181265    // Table shouldn't be larger than 2500 rows. All columns are read.
    182266    table  = psImageAlloc(11, maxLines, PS_TYPE_F64);
     
    186270        }
    187271    }
    188 
     272 
    189273    while(fgets(line, LINESIZE, fd) != NULL) {
    190274        if(j>=maxLines) {
     
    197281               ptr, ptr+1, ptr+2, ptr+3, ptr+4, ptr+5, ptr+6, ptr+7, ptr+8, ptr+9, ptr+10);
    198282    }
    199 
     283 
    200284    *(unsigned int *)&table->numRows = j;
    201 
     285 
    202286    return table;
    203287}
    204 
    205 psImage* readFinalsFile(char *fileName)
     288 
     289static psImage* readFinalsFile(char *fileName)
    206290{
    207291    char line[LINESIZE];
     
    210294    psImage *table = NULL;
    211295    FILE *fd = NULL;
    212 
    213 
     296 
     297 
    214298    fd=fopen(fileName, "r");
    215299    if(fd==NULL) {
     
    217301        return NULL ;
    218302    }
    219 
     303 
    220304    // Table shouldn't be larger than 15000 rows. Select columns are read.
    221305    table  = psImageAlloc(6, maxLines, PS_TYPE_F64);
     
    226310            psFree(table);
    227311        }
    228 
     312 
    229313        psF64 *ptr = table->data.F64[j++];
    230314        ptr = table->data.F64[j++];
    231 
     315 
    232316        // Sometimes there are blank entries in this table, so check for a space to determine if there is no entry
    233317        if(*(line+7) == ' ') {                   // MJD
     
    236320            *ptr = atof(line+7);
    237321        }
    238 
     322 
    239323        if(*(line+18) == ' ') {                  // Polar motion X
    240324            *(ptr+1) = 0.0;
     
    242326            *(ptr+1) = atof(line+18);
    243327        }
    244 
     328 
    245329        if(*(line+27) == ' ') {                  // Polar motion X error
    246330            *(ptr+2) = 0.0;
     
    248332            *(ptr+2) = atof(line+27);
    249333        }
    250 
     334 
    251335        if(*(line+37) == ' ') {                  // Polar motion Y
    252336            *(ptr+3) = 0.0;
     
    254338            *(ptr+3) = atof(line+37);
    255339        }
    256 
     340 
    257341        if(*(line+46) == ' ') {                  // Polar motion Y error
    258342            *(ptr+4) = 0.0;
     
    260344            *(ptr+4) = atof(line+46);
    261345        }
    262 
     346 
    263347        if(*(line+58) == ' ') {                  // UT1-UTC
    264348            *(ptr+5) = 0.0;
     
    267351        }
    268352    }
    269 
     353 
    270354    *(unsigned int *)&table->numRows = j;
    271 
     355 
    272356    return table;
    273357}
    274 
    275 
    276 double lookupTaiUtcTable(psImage *table, psTime *time)
     358*/
     359
     360static double lookupTaiUtcTable(const psTime *time)
    277361{
    278362    int hiIdx = 0;
     
    286370    double const2 = 0.0;
    287371    double const3 = 0.0;
    288 
     372    static psImage* table = NULL;
     373
     374    if (table == NULL) {
     375        table = readTaiUtcFile(TAIUTC_FILE);
     376        if (table == NULL) {
     377            psAbort(__func__,"Failed to read tai-utc.dat file");
     378        }
     379    }
    289380
    290381    // Number of rows is the number of rows of data read from the data file
     
    337428}
    338429
    339 double lookupSer7Table(psImage *table, psTime *time, int col)
     430static double lookupSer7Table(const psTime *time, int col)
    340431{
    341432    int hiIdx = 0;
     
    345436    double denom = 0.0;
    346437    double mjdUtc = 0.0;
    347 
     438    static psImage* table = NULL;
     439
     440    if (table == NULL) {
     441        table = readSer7File(SER7_FILE);
     442        if (table == NULL) {
     443            psAbort(__func__,"Failed to read the ser7.dat file.");
     444        }
     445    }
    348446
    349447    // Number of rows is the number of rows of data read from the data file
     
    404502    // Add most leapseconds to UTC time to get TAI time if necessary
    405503    if(type == PS_TIME_TAI) {
    406         time->sec += psGetTAIDelta(time);
     504        time->sec += psTimeGetTAIDelta(time);
    407505    }
    408506
     
    420518    }
    421519
    422     delta = psGetTAIDelta(time);
     520    delta = psTimeGetTAIDelta(time);
    423521
    424522    if(type == PS_TIME_UTC) {
     
    461559        taiTime = psMemIncrRefCounter(time);
    462560        ALLOC_TIME(utcTime);
    463         utcTime->sec = taiTime->sec - psGetTAIDelta(time);
     561        utcTime->sec = taiTime->sec - psTimeGetTAIDelta(time);
    464562        utcTime->usec = taiTime->usec;
    465563        utcTime->type = PS_TIME_UTC;
     
    467565        utcTime = psMemIncrRefCounter(time);
    468566        ALLOC_TIME(taiTime);
    469         taiTime->sec = utcTime->sec + psGetTAIDelta(time);
     567        taiTime->sec = utcTime->sec + psTimeGetTAIDelta(time);
    470568        taiTime->usec = utcTime->usec;
    471569        taiTime->type = PS_TIME_TAI;
     
    475573    ALLOC_TIME(ut1UtcDelta);
    476574    ut1UtcDelta->type = PS_TIME_UTC;
    477     ut1UtcDelta->usec = psGetUT1Delta(utcTime)*1e6;
     575    ut1UtcDelta->usec = psTimeGetUT1Delta(utcTime)*1e6;
    478576    ut1Time = psTimeAdd(utcTime, ut1UtcDelta);
    479577
     
    516614}
    517615
    518 double psGetUT1Delta(psTime *time)
    519 {
    520     static psImage* iersTable = NULL;
    521     double ut1UtcDeltaUsec = 0.0;
    522 
    523 
    524     if (iersTable == NULL) {
    525         iersTable = readSer7File("../../data/ser7.dat");
    526         p_psMemSetPersistent(iersTable,true);
    527         p_psMemSetPersistent(iersTable->data.V,true);
    528         p_psMemSetPersistent(iersTable->rawDataBuffer,true);
    529     }
    530 
    531     ut1UtcDeltaUsec = lookupSer7Table(iersTable, time, 6)*1.0e6;
    532 
    533     return ut1UtcDeltaUsec;
    534 }
    535 
    536 double psGetTAIDelta(psTime *time)
    537 {
    538     double delta = 0.0;
    539     static psImage* taiUtcTable = NULL;
    540 
    541 
    542     if (taiUtcTable == NULL) {
    543         taiUtcTable = readTaiUtcFile("../../data/tai-utc.dat");
    544         p_psMemSetPersistent(taiUtcTable,true);
    545         p_psMemSetPersistent(taiUtcTable->data.V,true);
    546         p_psMemSetPersistent(taiUtcTable->rawDataBuffer,true);
    547     }
    548     delta = lookupTaiUtcTable(taiUtcTable, time);
    549 
    550     return delta;
    551 }
    552 
    553 psF64 psTimeLeapseconds(const psTime *time1, const psTime *time2)
    554 {
    555     psF64 diff = 0.0;
     616double psTimeGetUT1Delta(const psTime *time)
     617{
     618    return lookupSer7Table(time, 6);
     619}
     620
     621struct psSphere* psTimeGetPoleCoords(const psTime* time)
     622{
     623
     624    double x = lookupSer7Table((psTime*)time, 4);
     625    double y = lookupSer7Table((psTime*)time, 5);
     626
     627    struct psSphere* output = psAlloc(sizeof(psSphere));
     628    output->r = x * M_PI / 648000.0; // x in arcsec converted to radians, i.e., x/60/60*M_PI/180
     629    output->d = y * M_PI / 648000.0;
     630
     631    return output;
     632}
     633
     634double psTimeGetTAIDelta(const psTime *time)
     635{
     636    return lookupTaiUtcTable(time);
     637}
     638
     639long psTimeLeapseconds(const psTime *time1, const psTime *time2)
     640{
     641    long diff = 0;
    556642
    557643    // NULL error checks
     
    559645    CHECK_NULL_TIME(time2,0);
    560646
    561     diff = fabs(psGetTAIDelta((psTime*)time1)-psGetTAIDelta((psTime*)time2));
     647    diff = abs(psTimeGetTAIDelta((psTime*)time1)-psTimeGetTAIDelta((psTime*)time2));
    562648
    563649    return diff;
    564650}
    565651
    566 double psTimeToJD(psTime *time)
     652double psTimeToJD(const psTime *time)
    567653{
    568654    double jd = 0.0;
     
    582668}
    583669
    584 double psTimeToMJD(psTime *time)
     670double psTimeToMJD(const psTime *time)
    585671{
    586672    double mjd = 0.0;
     
    600686}
    601687
    602 char* psTimeToISO(psTime *time)
     688char* psTimeToISOTime(const psTime *time)
    603689{
    604690    int ms = 0;
     
    634720}
    635721
    636 struct timeval psTimeToTimeval(psTime *time)
     722struct timeval psTimeToTimeval(const psTime *time)
    637723{
    638724    struct timeval timevalTime;
     
    648734}
    649735
    650 struct tm* psTimeToTM(psTime *time)
     736struct tm* psTimeToTM(const psTime *time)
    651737{
    652738    long cent = 0;
     
    711797}
    712798
    713 psTime* psJDToTime(double time)
     799psTime* psTimeFromJD(double time)
    714800{
    715801    double days = 0.0;
     
    734820}
    735821
    736 psTime* psMJDToTime(double time)
     822psTime* psTimeFromMJD(double time)
    737823{
    738824    double days = 0.0;
     
    757843}
    758844
    759 psTime* psISOToTime(char *time)
     845psTime* psTimeFromISOTime(const char *time)
    760846{
    761847    char tempString[MAX_TIME_STRING_LENGTH];
     
    826912
    827913    // Convert tm time to psTime
    828     outTime = psTMToTime(&tmTime);
     914    outTime = psTimeFromTM(&tmTime);
    829915    outTime->usec = millisecond * 1000;
    830916
     
    832918}
    833919
    834 psTime* psTimevalToTime(struct timeval *time)
     920psTime* psTimeFromTimeval(const struct timeval *time)
    835921{
    836922    psTime *outTime = NULL;
     
    850936}
    851937
    852 psTime* psTMToTime(struct tm* time)
     938psTime* psTimeFromTM(const struct tm* time)
    853939{
    854940    long year;
     
    905991}
    906992
    907 psTime* psTimeAdd(psTime *tai1, psTime *tai2)
     993psTime* psTimeAdd(const psTime *tai1, const psTime *tai2)
    908994{
    909995    psTime *outTime = NULL;
     
    9291015}
    9301016
    931 psTime* psTimeSub(psTime *tai1, psTime *tai2)
     1017psTime* psTimeSubtract(const psTime *tai1, const psTime *tai2)
    9321018{
    9331019    psTime *outTime = NULL;
     
    9531039}
    9541040
    955 psTime* psTimeDelta(psTime *tai1, psTime *tai2)
     1041psTime* psTimeDelta(const psTime *tai1, const psTime *tai2)
    9561042{
    9571043    psTime *outTime = NULL;
Note: See TracChangeset for help on using the changeset viewer.