IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 20, 2004, 11:21:58 AM (22 years ago)
Author:
harman
Message:

Replaced ALLOC_TIME with psTimeAlloc
Added more error conditionals

File:
1 edited

Legend:

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

    r2179 r2181  
    1111 *  @author Ross Harman, MHPCC
    1212 *
    13  *  @version $Revision: 1.35 $ $Name: not supported by cvs2svn $
    14  *  @date $Date: 2004-10-20 20:05:51 $
     13 *  @version $Revision: 1.36 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2004-10-20 21:21:58 $
    1515 *
    1616 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    7272                #define LINESIZE 1024
    7373
    74                 /** Preprocessor macro to check for null psTime struct */
    75                 #define CHECK_NULL_TIME(TIME,RETURN) \
    76                 if(TIME == NULL) { \
    77                     psError(__func__,"NULL value not allowed"); \
    78                     return RETURN; \
    79                 }
    80 
    81                 /** Preprocessor macro to allocate psTime struct */
    82                 #define ALLOC_TIME(TIME) \
    83                 TIME = (psTime*)psAlloc(sizeof(psTime)); \
    84 if (TIME == NULL) { \
    85     psAbort(__func__, " : Line %d - Failed to allocate memory", __LINE__); \
    86 } \
    87 TIME->sec = 0; \
    88 TIME->usec = 0; \
    89 TIME->type = PS_TIME_TAI;
    90 
    91 /** Read TAI/UTC data file.
    92  *
    93  *  Reads TAI/UTC data file.
    94  *
    95  *  @return  psImage*: Two-dimensional array with table data.
    96  */
    97 static psImage* readTaiUtcFile(
    98     const char *fileName                      ///< Name of file to read.
    99 );
     74                /** Read TAI/UTC data file.
     75                 *
     76                 *  Reads TAI/UTC data file.
     77                 *
     78                 *  @return  psImage*: Two-dimensional array with table data.
     79                 */
     80                static psImage* readTaiUtcFile(
     81                    const char *fileName                      ///< Name of file to read.
     82                );
    10083
    10184
     
    488471    psTime *outTime = NULL;
    489472
     473
     474    // Error checks
    490475    if(type!=PS_TIME_TAI && type!=PS_TIME_UTC) {
    491476        psError(__func__, " : Line %d - Incorrect type. Type: %d", __LINE__, type);
     
    512497
    513498    // Allocate psTime struct
    514     ALLOC_TIME(time);
     499    time = psTimeAlloc(type);
    515500
    516501    if (gettimeofday(&now, (struct timezone *)0) == -1) {
     
    522507    time->sec = now.tv_sec;
    523508    time->usec = now.tv_usec;
    524     time->type = type;
    525509
    526510    // Add most leapseconds to UTC time to get TAI time if necessary
     
    537521
    538522
    539     if(time->type == type) {
    540         psError(__func__, " : Line %d - Time is already in the correct psTime format. Time type: %d", __LINE__, type);
     523    // Error checks
     524    if(time == NULL) {
     525        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     526        return NULL;
     527    } else if(time->usec >= 1e6) {
     528        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     529        return NULL;
     530    } else if(time->type == type) {
     531        psError(__func__, " : Line %d - Time already in correct psTime type. Time type: %d", __LINE__, type);
    541532        return NULL;
    542533    }
     
    545536
    546537    if(type == PS_TIME_UTC) {
    547         time->sec = time->sec - delta;    // User wants UTC time. Remove leapseconds.
     538        time->sec = time->sec - delta;    // User wants UTC time. Subtract leapseconds.
    548539    } else if(type == PS_TIME_TAI) {
    549540        time->sec = time->sec + delta;    // User wants TAI time. Add leapseconds.
     
    580571
    581572
     573    // Error checks
     574    if(time == NULL) {
     575        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     576        return 0;
     577    } else if(time->usec >= 1e6) {
     578        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     579        return 0;
     580    }
     581
    582582    // Calculate TAI or UTC time based on type of time user passes
    583583    if(time->type == PS_TIME_TAI) {
    584584        taiTime = psMemIncrRefCounter(time);
    585         ALLOC_TIME(utcTime);
     585        utcTime = psTimeAlloc(PS_TIME_UTC);
    586586        utcTime->sec = taiTime->sec - psTimeGetTAIDelta(time);
    587587        utcTime->usec = taiTime->usec;
    588         utcTime->type = PS_TIME_UTC;
    589588    } else if(time->type == PS_TIME_UTC) {
    590589        utcTime = psMemIncrRefCounter(time);
    591         ALLOC_TIME(taiTime);
     590        taiTime = psTimeAlloc(PS_TIME_TAI);
    592591        taiTime->sec = utcTime->sec + psTimeGetTAIDelta(time);
    593592        taiTime->usec = utcTime->usec;
    594         taiTime->type = PS_TIME_TAI;
    595593    }
    596594
    597595    // Convert Universal Time (UTC) to  UT1
    598     ALLOC_TIME(ut1UtcDelta);
    599     ut1UtcDelta->type = PS_TIME_UTC;
     596    ut1UtcDelta = psTimeAlloc(PS_TIME_UTC);
    600597
    601598    ut1UtcDbl = psTimeGetUT1Delta(utcTime)*1e6;
     
    614611
    615612    // Calculate Terrestial Dynamical Time (TDT)
    616     ALLOC_TIME(tdtDelta);
     613    tdtDelta = psTimeAlloc(PS_TIME_TAI);
    617614    tdtDelta->sec = 32;
    618615    tdtDelta->usec = 184000;
     
    648645double psTimeGetUT1Delta(const psTime *time)
    649646{
     647
     648    // Error checks
     649    if(time == NULL) {
     650        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     651        return 0.0;
     652    } else if(time->usec >= 1e6) {
     653        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     654        return 0.0;
     655    }
     656
    650657    return lookupSer7Table(time, 6);
    651658}
     
    653660struct psSphere* psTimeGetPoleCoords(const psTime* time)
    654661{
    655 
    656     double x = lookupSer7Table((psTime*)time, 4);
    657     double y = lookupSer7Table((psTime*)time, 5);
    658 
    659     struct psSphere* output = psAlloc(sizeof(psSphere));
     662    double x = 0.0;
     663    double y = 0.0;
     664    struct psSphere* output = NULL;
     665
     666
     667    // Error checks
     668    if(time == NULL)
     669    {
     670        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     671        return NULL;
     672    } else if(time->usec >= 1e6)
     673    {
     674        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     675        return NULL;
     676    }
     677
     678    x = lookupSer7Table((psTime*)time, 4);
     679    y = lookupSer7Table((psTime*)time, 5);
     680
     681    output = psAlloc(sizeof(psSphere));
    660682    output->r = x * M_PI / 648000.0; // x in arcsec converted to radians, i.e., x/60/60*M_PI/180
    661683    output->d = y * M_PI / 648000.0;
     
    666688double psTimeGetTAIDelta(const psTime *time)
    667689{
     690
     691    // Error checks
     692    if(time == NULL) {
     693        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     694        return 0.0;
     695    } else if(time->usec >= 1e6) {
     696        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     697        return 0.0;
     698    }
     699
    668700    return lookupTaiUtcTable(time);
    669701}
     
    673705    long diff = 0;
    674706
    675     // NULL error checks
    676     CHECK_NULL_TIME(time1,0);
    677     CHECK_NULL_TIME(time2,0);
     707
     708    // Error checks
     709    if(time1 == NULL) {
     710        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
     711        return 0;
     712    } else if(time1->usec >= 1e6) {
     713        psError(__func__, " : Line %d - First input time microseconds too big. Value %u", __LINE__, time1->usec);
     714        return 0;
     715    } else if(time2 == NULL) {
     716        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
     717        return 0;
     718    } else if(time2->usec >= 1e6) {
     719        psError(__func__, " : Line %d - First input time microseconds too big. Value %u", __LINE__, time2->usec);
     720        return 0;
     721    }
    678722
    679723    diff = abs(psTimeGetTAIDelta((psTime*)time1)-psTimeGetTAIDelta((psTime*)time2));
     
    687731
    688732
    689     // NULL error check
    690     CHECK_NULL_TIME(time,0.0);
     733    // Error checks
     734    if(time == NULL) {
     735        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     736        return 0.0;
     737    } else if(time->usec >= 1e6) {
     738        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     739        return 0.0;
     740    }
    691741
    692742    // Julian date conversion
     
    705755
    706756
    707     // NULL error check
    708     CHECK_NULL_TIME(time,0.0);
     757    // Error checks
     758    if(time == NULL) {
     759        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     760        return 0.0;
     761    } else if(time->usec >= 1e6) {
     762        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     763        return 0.0;
     764    }
    709765
    710766    // Modified Julian date conversion
     
    727783
    728784
    729     // NULL error check
    730     CHECK_NULL_TIME(time,NULL);
     785    // Error checks
     786    if(time == NULL) {
     787        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     788        return NULL;
     789    } else if(time->usec >= 1e6) {
     790        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     791        return NULL;
     792    }
    731793
    732794    tempString = psAlloc(MAX_TIME_STRING_LENGTH);
     
    757819
    758820
    759     // NULL error check
    760     CHECK_NULL_TIME(time,timevalTime);
     821    // Error checks
     822    if(time == NULL)
     823    {
     824        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     825        return timevalTime;
     826    } else if(time->usec >= 1e6)
     827    {
     828        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     829        return timevalTime;
     830    }
    761831
    762832    timevalTime.tv_sec = time->sec;
     
    779849
    780850
     851    // Error checks
     852    if(time == NULL)
     853    {
     854        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     855        return NULL;
     856    } else if(time->usec >= 1e6)
     857    {
     858        psError(__func__, " : Line %d - Input time microseconds too big. Value %u", __LINE__, time->usec);
     859        return NULL;
     860    }
     861
    781862    seconds = time->sec%60;
    782863    minute = time->sec/60%60;
     
    837918
    838919    // Allocate psTime struct
    839     ALLOC_TIME(outTime);
     920    outTime = psTimeAlloc(PS_TIME_TAI);
    840921
    841922    // Julian date conversion courtesy of Eugene Magnier
     
    849930    outTime->sec = seconds;
    850931
     932    // Error check
     933    if(outTime->usec >= 1e6) {
     934        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
     935    }
     936
    851937    return outTime;
    852938}
     
    860946
    861947    // Allocate psTime struct
    862     ALLOC_TIME(outTime);
     948    outTime = psTimeAlloc(PS_TIME_TAI);
    863949
    864950    // Modified Julian date conversion courtesy of Eugene Magnier
     
    871957    }
    872958    outTime->sec = seconds;
     959
     960    // Error check
     961    if(outTime->usec >= 1e6) {
     962        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
     963    }
    873964
    874965    return outTime;
     
    9471038    outTime->usec = millisecond * 1000;
    9481039
     1040    // Error check
     1041    if(outTime->usec >= 1e6) {
     1042        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
     1043    }
     1044
    9491045    return outTime;
    9501046}
     
    9551051
    9561052
    957     // NULL error check
    958     CHECK_NULL_TIME(time,NULL);
     1053    // Error check
     1054    if(time == NULL)
     1055    {
     1056        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     1057        return NULL;
     1058    }
    9591059
    9601060    // Allocate psTime struct
    961     ALLOC_TIME(outTime)
     1061    outTime = psTimeAlloc(PS_TIME_TAI);
    9621062
    9631063    // Convert to psTime
    9641064    outTime->sec = time->tv_sec;
    9651065    outTime->usec = time->tv_usec;
     1066
     1067    // Error check
     1068    if(outTime->usec >= 1e6)
     1069    {
     1070        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
     1071    }
    9661072
    9671073    return outTime;
     
    9791085    psTime *outTime = NULL;
    9801086
    981     // NULL error check
    982     CHECK_NULL_TIME(time,NULL);
     1087
     1088    // Error check
     1089    if(time == NULL)
     1090    {
     1091        psError(__func__, " : Line %d - NULL value not allowed", __LINE__);
     1092        return NULL;
     1093    }
    9831094
    9841095    // Allocate psTime struct
    985     ALLOC_TIME(outTime);
     1096    outTime = psTimeAlloc(PS_TIME_TAI);
    9861097
    9871098    // Extract data from TM struct
     
    10201131    outTime->usec = 0;
    10211132
     1133    // Error check
     1134    if(outTime->usec >= 1e6)
     1135    {
     1136        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
     1137    }
     1138
    10221139    return outTime;
    10231140}
     
    10301147
    10311148
     1149    // Error checks
    10321150    if(tai1->type != tai2->type) {
    10331151        psError(__func__, " : Line %d - Argument types inconsistent for time addition", __LINE__);
    10341152        return NULL;
     1153    } else if(tai1 == NULL) {
     1154        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
     1155        return NULL;
    10351156    } else if(tai1->usec >= 1e6) {
    10361157        psError(__func__, " : Line %d - Argument 1 microseconds too big. Value %u", __LINE__, tai1->usec);
    10371158        return NULL;
     1159    } else if(tai2 == NULL) {
     1160        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
     1161        return NULL;
    10381162    } else if(tai2->usec >= 1e6) {
    10391163        psError(__func__, " : Line %d - Argument 2 microseconds too big. Value %u", __LINE__, tai2->usec);
     
    10411165    }
    10421166
    1043     ALLOC_TIME(outTime);
    1044     outTime->type = tai1->type;
     1167    outTime = psTimeAlloc(tai1->type);
    10451168    outTime->sec = tai1->sec + tai2->sec;
    10461169    deltaUsec = tai1->usec + tai2->usec;
     
    10581181    }
    10591182
     1183    // Error check
     1184    if(outTime->usec >= 1e6) {
     1185        psError(__func__, " : Line %d - Output time microseconds too big. Value %u", __LINE__, outTime->usec);
     1186    }
     1187
    10601188    return outTime;
    10611189}
     
    10681196
    10691197
     1198    // Error checks
    10701199    if(tai1->type != tai2->type) {
    10711200        psError(__func__, " : Line %d - Argument types inconsistent for time subtraction", __LINE__);
    10721201        return NULL;
     1202    } else if(tai1 == NULL) {
     1203        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
     1204        return NULL;
    10731205    } else if(tai1->usec >= 1e6) {
    10741206        psError(__func__, " : Line %d - Argument 1 microseconds too big. Value %u", __LINE__, tai1->usec);
    10751207        return NULL;
     1208    } else if(tai2 == NULL) {
     1209        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
     1210        return NULL;
    10761211    } else if(tai2->usec >= 1e6) {
    10771212        psError(__func__, " : Line %d - Argument 2 microseconds too big. Value %u", __LINE__, tai2->usec);
     
    10791214    }
    10801215
    1081     ALLOC_TIME(outTime);
    1082     outTime->type = tai1->type;
     1216    outTime = psTimeAlloc(tai1->type);
    10831217    deltaSec = tai1->sec - tai2->sec;
    10841218    deltaUsec = (long)tai1->usec - (long)tai2->usec;
     
    11081242
    11091243
     1244    // Error checks
    11101245    if(tai1->type != tai2->type) {
    11111246        psError(__func__, " : Line %d - Argument types inconsistent for time subtraction", __LINE__);
    11121247        return NULL;
     1248    } else if(tai1 == NULL) {
     1249        psError(__func__, " : Line %d - NULL value not allowed for first input", __LINE__);
     1250        return NULL;
    11131251    } else if(tai1->usec >= 1e6) {
    11141252        psError(__func__, " : Line %d - Argument 1 microseconds too big. Value %u", __LINE__, tai1->usec);
    11151253        return NULL;
     1254    } else if(tai2 == NULL) {
     1255        psError(__func__, " : Line %d - NULL value not allowed for second input", __LINE__);
     1256        return NULL;
    11161257    } else if(tai2->usec >= 1e6) {
    11171258        psError(__func__, " : Line %d - Argument 2 microseconds too big. Value %u", __LINE__, tai2->usec);
     
    11191260    }
    11201261
    1121     ALLOC_TIME(outTime);
    1122     outTime->type = tai1->type;
     1262    outTime = psTimeAlloc(tai1->type);
    11231263    deltaSec = tai1->sec - tai2->sec;
    11241264    deltaUsec = (long)tai1->usec - (long)tai2->usec;
Note: See TracChangeset for help on using the changeset viewer.