IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1407 for trunk/psLib/src/astro


Ignore:
Timestamp:
Aug 6, 2004, 2:06:06 PM (22 years ago)
Author:
desonia
Message:

another attempt to get astyle to get it right.

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

Legend:

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

    r1406 r1407  
     1
    12/** @file  psCoord.c
    23*
     
    1011*  @author George Gusciora, MHPCC
    1112*
    12 *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2004-08-06 22:34:05 $
     13*  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2004-08-07 00:06:06 $
    1415*
    1516*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2728#include <float.h>
    2829
    29 static float cot( float x );
    30 static float arg( float x, float y );
     30static float cot(float x);
     31static float arg(float x, float y);
    3132
    3233// This is the only function in this file which I understand.
    33 psPlane *psPlaneTransformApply( psPlane *out,
    34                                 const psPlaneTransform *transform,
    35                                 const psPlane *coords )
    36 {
    37     if ( out == NULL ) {
    38         out = ( psPlane * ) psAlloc( sizeof( psPlane ) );
    39     }
    40     out->x = transform->x->coeff[ 0 ][ 0 ] +
    41              ( transform->x->coeff[ 1 ][ 0 ] * coords->x ) +
    42              ( transform->x->coeff[ 0 ][ 1 ] * coords->y );
    43 
    44     out->y = transform->y->coeff[ 0 ][ 0 ] +
    45              ( transform->y->coeff[ 1 ][ 0 ] * coords->x ) +
    46              ( transform->y->coeff[ 0 ][ 1 ] * coords->y );
    47 
    48     return ( out );
    49 }
    50 
     34psPlane *psPlaneTransformApply(psPlane * out, const psPlaneTransform * transform, const psPlane * coords)
     35{
     36    if (out == NULL) {
     37        out = (psPlane *) psAlloc(sizeof(psPlane));
     38    }
     39    out->x = transform->x->coeff[0][0] +
     40             (transform->x->coeff[1][0] * coords->x) + (transform->x->coeff[0][1] * coords->y);
     41
     42    out->y = transform->y->coeff[0][0] +
     43             (transform->y->coeff[1][0] * coords->x) + (transform->y->coeff[0][1] * coords->y);
     44
     45    return (out);
     46}
    5147
    5248// This transformation takes into account parameters beyond an objects
    5349// spatial coordinates: term3 and term4.
    54 psPlane *psPlaneDistortApply( psPlane *out,
    55                               const psPlaneDistort *transform,
    56                               const psPlane *coords,
    57                               float term3,
    58                               float term4 )
    59 {
    60     if ( out == NULL ) {
    61         out = ( psPlane * ) psAlloc( sizeof( psPlane ) );
    62     }
    63 
    64     out->x = transform->x->coeff[ 0 ][ 0 ][ 0 ][ 0 ] +
    65              ( transform->x->coeff[ 1 ][ 0 ][ 0 ][ 0 ] * coords->x ) +
    66              ( transform->x->coeff[ 0 ][ 1 ][ 0 ][ 0 ] * coords->y ) +
    67              ( transform->x->coeff[ 0 ][ 0 ][ 1 ][ 0 ] * term3 ) +
    68              ( transform->x->coeff[ 0 ][ 0 ][ 0 ][ 1 ] * term4 );
    69 
    70     out->y = transform->y->coeff[ 0 ][ 0 ][ 0 ][ 0 ] +
    71              ( transform->y->coeff[ 1 ][ 0 ][ 0 ][ 0 ] * coords->x ) +
    72              ( transform->y->coeff[ 0 ][ 1 ][ 0 ][ 0 ] * coords->y ) +
    73              ( transform->y->coeff[ 0 ][ 0 ][ 1 ][ 0 ] * term3 ) +
    74              ( transform->y->coeff[ 0 ][ 0 ][ 0 ][ 1 ] * term4 );
    75 
    76     return ( out );
    77 }
    78 
     50psPlane *psPlaneDistortApply(psPlane * out,
     51                             const psPlaneDistort * transform,
     52                             const psPlane * coords, float term3, float term4)
     53{
     54    if (out == NULL) {
     55        out = (psPlane *) psAlloc(sizeof(psPlane));
     56    }
     57
     58    out->x = transform->x->coeff[0][0][0][0] +
     59             (transform->x->coeff[1][0][0][0] * coords->x) +
     60             (transform->x->coeff[0][1][0][0] * coords->y) +
     61             (transform->x->coeff[0][0][1][0] * term3) + (transform->x->coeff[0][0][0][1] * term4);
     62
     63    out->y = transform->y->coeff[0][0][0][0] +
     64             (transform->y->coeff[1][0][0][0] * coords->x) +
     65             (transform->y->coeff[0][1][0][0] * coords->y) +
     66             (transform->y->coeff[0][0][1][0] * term3) + (transform->y->coeff[0][0][0][1] * term4);
     67
     68    return (out);
     69}
    7970
    8071// This function prototype has been modified since the SDRS.
    81 psSphereTransform *psSphereTransformAlloc( double NPlat,
    82         double Xo,
    83         double xo )
    84 {
    85     psSphereTransform * tmp = ( psSphereTransform * ) psAlloc( sizeof( psSphereTransform ) );
    86 
    87     tmp->sinPhi = sin( NPlat );
    88     tmp->cosPhi = cos( NPlat );
     72psSphereTransform *psSphereTransformAlloc(double NPlat, double Xo, double xo)
     73{
     74    psSphereTransform *tmp = (psSphereTransform *) psAlloc(sizeof(psSphereTransform));
     75
     76    tmp->sinPhi = sin(NPlat);
     77    tmp->cosPhi = cos(NPlat);
    8978    tmp->Xo = Xo;
    9079    tmp->xo = xo;
    9180
    92     return ( tmp );
     81    return (tmp);
    9382}
    9483
     
    10089// there are no typo's.
    10190
    102 psSphere *psSphereTransformApply( psSphere *out,
    103                                   const psSphereTransform *transform,
    104                                   const psSphere *coord )
     91psSphere *psSphereTransformApply(psSphere * out, const psSphereTransform * transform, const psSphere * coord)
    10592{
    10693    double sinY = 0.0;
     
    11299    double dx = 0.0;
    113100
    114     if ( out == NULL ) {
    115         out = ( psSphere * ) psAlloc( sizeof( psSphere ) );
     101    if (out == NULL) {
     102        out = (psSphere *) psAlloc(sizeof(psSphere));
    116103    }
    117104
     
    119106    y = coord->d;
    120107    dx = x - transform->xo;
    121     sinY = cos( y ) * sin( dx ) * transform->sinPhi + sin( y ) * transform->cosPhi;
    122     cosY = sqrt( 1.0 - sinY * sinY );
    123     sinX = ( cos( y ) * sin( dx ) * transform->cosPhi - sin( y ) * transform->sinPhi ) /
    124            cos( y );
    125     cosX = cos( y ) * cos( dx ) / cos( y );
    126 
    127     out->r = atan2( sinX, cosX ) + transform->Xo;
    128     out->d = atan2( sinY, cosY );
    129 
    130     return ( out );
    131 }
    132 
    133 psSphereTransform *psSphereTransformICRStoEcliptic( psTime time )
    134 {
    135     struct tm * tmTime = psTimeToTM( time );
    136     double year = ( double ) ( 1900 + tmTime->tm_year );
     108    sinY = cos(y) * sin(dx) * transform->sinPhi + sin(y) * transform->cosPhi;
     109    cosY = sqrt(1.0 - sinY * sinY);
     110    sinX = (cos(y) * sin(dx) * transform->cosPhi - sin(y) * transform->sinPhi) / cos(y);
     111    cosX = cos(y) * cos(dx) / cos(y);
     112
     113    out->r = atan2(sinX, cosX) + transform->Xo;
     114    out->d = atan2(sinY, cosY);
     115
     116    return (out);
     117}
     118
     119psSphereTransform *psSphereTransformICRStoEcliptic(psTime time)
     120{
     121    struct tm *tmTime = psTimeToTM(time);
     122    double year = (double)(1900 + tmTime->tm_year);
    137123    double T = year / 100.0;
    138124    double phi = -23.452294 + 0.013013 * T + 0.000001639 * T * T - 0.000000503 * T * T * T;
     
    140126    double xo = 0.0;
    141127
    142     return ( psSphereTransformAlloc( phi, Xo, xo ) );
    143 }
    144 
    145 psSphereTransform *psSphereTransformEcliptictoICRS( psTime time )
    146 {
    147     struct tm * tmTime = psTimeToTM( time );
    148     double year = ( double ) ( 1900 + tmTime->tm_year );
     128    return (psSphereTransformAlloc(phi, Xo, xo));
     129}
     130
     131psSphereTransform *psSphereTransformEcliptictoICRS(psTime time)
     132{
     133    struct tm *tmTime = psTimeToTM(time);
     134    double year = (double)(1900 + tmTime->tm_year);
    149135    double T = year / 100.0;
    150     double phi = + 23.452294 - 0.013013 * T - 0.000001639 * T * T + 0.000000503 * T * T * T;
     136    double phi = +23.452294 - 0.013013 * T - 0.000001639 * T * T + 0.000000503 * T * T * T;
    151137    double Xo = 0.0;
    152138    double xo = 0.0;
    153139
    154     return ( psSphereTransformAlloc( phi, Xo, xo ) );
    155 }
    156 
    157 psSphereTransform *psSphereTransformICRStoGalatic( void )
    158 {
    159     return ( psSphereTransformAlloc( 62.6, 282.25, 33.0 ) );
    160 }
    161 
    162 psSphereTransform *psSphereTransformGalatictoICRS( void )
    163 {
    164     return ( psSphereTransformAlloc( -62.6, 33.0, 282.25 ) );
     140    return (psSphereTransformAlloc(phi, Xo, xo));
     141}
     142
     143psSphereTransform *psSphereTransformICRStoGalatic(void)
     144{
     145    return (psSphereTransformAlloc(62.6, 282.25, 33.0));
     146}
     147
     148psSphereTransform *psSphereTransformGalatictoICRS(void)
     149{
     150    return (psSphereTransformAlloc(-62.6, 33.0, 282.25));
    165151}
    166152
    167153// XXX: Is this the correct way to calculate this?
    168 float cot( float x )
    169 {
    170     return ( 1.0 / atan( x ) );
     154float cot(float x)
     155{
     156    return (1.0 / atan(x));
    171157}
    172158
    173159// This is some kind of arc tan function.
    174 float arg( float x, float y )
    175 {
    176     if ( x > 0 ) {
    177         return ( atan( y / x ) );
    178     } else
    179         if ( ( x == 0 ) && ( y == 0 ) ) {
    180             return ( 0.5 * M_PI );
    181         } else
    182             if ( ( x == 0 ) && ( y == 0 ) ) {
    183                 return ( -0.5 * M_PI );
    184             } else
    185                 if ( ( x == 0 ) && ( y == 0 ) ) {
    186                     return ( M_PI + atan( y / x ) );
    187                 } else
    188                     if ( ( x == 0 ) && ( y == 0 ) ) {
    189                         return ( -M_PI + atan( y / x ) );
    190                     }
    191 
    192     psAbort( __func__, "Unacceptable range for (arg(%f, %f).\n", x, y );
    193     return ( 0.0 );
     160float arg(float x, float y)
     161{
     162    if (x > 0) {
     163        return (atan(y / x));
     164    } else if ((x == 0) && (y == 0)) {
     165        return (0.5 * M_PI);
     166    } else if ((x == 0) && (y == 0)) {
     167        return (-0.5 * M_PI);
     168    } else if ((x == 0) && (y == 0)) {
     169        return (M_PI + atan(y / x));
     170    } else if ((x == 0) && (y == 0)) {
     171        return (-M_PI + atan(y / x));
     172    }
     173
     174    psAbort(__func__, "Unacceptable range for (arg(%f, %f).\n", x, y);
     175    return (0.0);
    194176}
    195177
    196178// XXX: Waiting for the definition of the PS_PROJ_PAR projection.
    197179// XXX: Waiting for the definition of the PS_PROJ_GLS projection.
    198 psPlane *psProject( const psSphere *coord,
    199                     const psProjection *projection )
     180psPlane *psProject(const psSphere * coord, const psProjection * projection)
    200181{
    201182    float R = 0.0;
    202183    float alpha = 0.0;
    203     psPlane *tmp = ( psPlane * ) psAlloc( sizeof( psPlane ) );
    204 
    205     if ( projection->type == PS_PROJ_TAN ) {
    206         R = cot( coord->r ) * ( 180.0 / M_PI );
    207         tmp->x = R * sin( coord->d );
    208         tmp->y = R * cos( coord->d );
    209 
    210     } else
    211         if ( projection->type == PS_PROJ_SIN ) {
    212             R = cos( coord->r ) * ( 180.0 / M_PI );
    213             tmp->x = R * sin( coord->d );
    214             tmp->y = R * cos( coord->d );
    215 
    216         } else
    217             if ( projection->type == PS_PROJ_CAR ) {
    218                 tmp->x = coord->d;
    219                 tmp->y = coord->r;
    220 
    221             } else
    222                 if ( projection->type == PS_PROJ_MER ) {
    223                     tmp->x = coord->d;
    224                     tmp->y = log( tan( 45.0 + ( 0.5 * coord->r ) ) ) * 180.0 / M_PI;
    225 
    226                 } else
    227                     if ( projection->type == PS_PROJ_AIT ) {
    228                         alpha = 1.0 / ( ( 180.0 / M_PI ) *
    229                                         sqrt( 1.0 + ( cos( coord->r ) * cos( 0.5 * coord->d ) * 0.5 ) ) );
    230 
    231                         tmp->x = 2.0 * alpha * cos( coord->r ) * sin( 0.5 * coord->d );
    232                         tmp->y = alpha * sin( coord->d );
    233 
    234                     } else
    235                         if ( projection->type == PS_PROJ_PAR ) {
    236                             psAbort( __func__, "The projection type PS_PROJ_PAR is undefined.\n" );
    237 
    238                         } else
    239                             if ( projection->type == PS_PROJ_GLS ) {
    240                                 psAbort( __func__, "The projection type PS_PROJ_GLS is undefined.\n" );
    241                             }
    242 
    243     return ( tmp );
     184    psPlane *tmp = (psPlane *) psAlloc(sizeof(psPlane));
     185
     186    if (projection->type == PS_PROJ_TAN) {
     187        R = cot(coord->r) * (180.0 / M_PI);
     188        tmp->x = R * sin(coord->d);
     189        tmp->y = R * cos(coord->d);
     190
     191    } else if (projection->type == PS_PROJ_SIN) {
     192        R = cos(coord->r) * (180.0 / M_PI);
     193        tmp->x = R * sin(coord->d);
     194        tmp->y = R * cos(coord->d);
     195
     196    } else if (projection->type == PS_PROJ_CAR) {
     197        tmp->x = coord->d;
     198        tmp->y = coord->r;
     199
     200    } else if (projection->type == PS_PROJ_MER) {
     201        tmp->x = coord->d;
     202        tmp->y = log(tan(45.0 + (0.5 * coord->r))) * 180.0 / M_PI;
     203
     204    } else if (projection->type == PS_PROJ_AIT) {
     205        alpha = 1.0 / ((180.0 / M_PI) * sqrt(1.0 + (cos(coord->r) * cos(0.5 * coord->d) * 0.5)));
     206
     207        tmp->x = 2.0 * alpha * cos(coord->r) * sin(0.5 * coord->d);
     208        tmp->y = alpha * sin(coord->d);
     209
     210    } else if (projection->type == PS_PROJ_PAR) {
     211        psAbort(__func__, "The projection type PS_PROJ_PAR is undefined.\n");
     212
     213    } else if (projection->type == PS_PROJ_GLS) {
     214        psAbort(__func__, "The projection type PS_PROJ_GLS is undefined.\n");
     215    }
     216
     217    return (tmp);
    244218}
    245219
    246220// XXX: Waiting for the definition of the PS_PROJ_PAR projection.
    247221// XXX: Waiting for the definition of the PS_PROJ_GLS projection.
    248 psSphere *psDeproject( const psPlane *coord,
    249                        const psProjection *projection )
     222psSphere *psDeproject(const psPlane * coord, const psProjection * projection)
    250223{
    251224    float R = 0.0;
     
    253226    float chu1 = 0.0;
    254227    float chu2 = 0.0;
    255     psSphere *tmp = ( psSphere * ) psAlloc( sizeof( psSphere ) );
    256 
    257     if ( projection->type == PS_PROJ_TAN ) {
    258         R = sqrt( ( coord->x * coord->x ) + ( coord->y * coord->y ) );
    259         tmp->d = arg( -coord->y, coord->x );
    260         tmp->r = atan( 180.0 / ( R * M_PI ) );
    261 
    262     } else
    263         if ( projection->type == PS_PROJ_SIN ) {
    264             R = sqrt( ( coord->x * coord->x ) + ( coord->y * coord->y ) );
    265             tmp->d = arg( -coord->y, coord->x );
    266             tmp->r = acos( ( R * M_PI ) / 180.0 );
    267 
    268         } else
    269             if ( projection->type == PS_PROJ_CAR ) {
    270                 tmp->d = coord->x;
    271                 tmp->r = coord->y;
    272 
    273             } else
    274                 if ( projection->type == PS_PROJ_MER ) {
    275                     tmp->d = coord->x;
    276                     tmp->r = ( 2.0 * atan( exp( ( coord->y * M_PI / 180.0 ) ) ) ) - 180.0;
    277 
    278                 } else
    279                     if ( projection->type == PS_PROJ_AIT ) {
    280                         chu1 = ( coord->x * M_PI ) / 720.0;
    281                         chu1 *= chu1;
    282                         chu2 = ( coord->y * M_PI ) / 360.0;
    283                         chu2 *= chu2;
    284                         chu = sqrt( 1.0 - chu1 - chu2 );
    285                         tmp->d = 2.0 * arg( ( 2.0 * chu * chu ) - 1.0,
    286                                             ( coord->x * chu * M_PI ) / 360.0 );
    287                         tmp->r = asin( ( coord->y * chu * M_PI ) / 180.0 );
    288 
    289                     } else
    290                         if ( projection->type == PS_PROJ_PAR ) {
    291                             psAbort( __func__, "The projection type PS_PROJ_PAR is undefined.\n" );
    292 
    293                         } else
    294                             if ( projection->type == PS_PROJ_GLS ) {
    295                                 psAbort( __func__, "The projection type PS_PROJ_GLG is undefined.\n" );
    296                             }
    297 
    298     return ( tmp );
     228    psSphere *tmp = (psSphere *) psAlloc(sizeof(psSphere));
     229
     230    if (projection->type == PS_PROJ_TAN) {
     231        R = sqrt((coord->x * coord->x) + (coord->y * coord->y));
     232        tmp->d = arg(-coord->y, coord->x);
     233        tmp->r = atan(180.0 / (R * M_PI));
     234
     235    } else if (projection->type == PS_PROJ_SIN) {
     236        R = sqrt((coord->x * coord->x) + (coord->y * coord->y));
     237        tmp->d = arg(-coord->y, coord->x);
     238        tmp->r = acos((R * M_PI) / 180.0);
     239
     240    } else if (projection->type == PS_PROJ_CAR) {
     241        tmp->d = coord->x;
     242        tmp->r = coord->y;
     243
     244    } else if (projection->type == PS_PROJ_MER) {
     245        tmp->d = coord->x;
     246        tmp->r = (2.0 * atan(exp((coord->y * M_PI / 180.0)))) - 180.0;
     247
     248    } else if (projection->type == PS_PROJ_AIT) {
     249        chu1 = (coord->x * M_PI) / 720.0;
     250        chu1 *= chu1;
     251        chu2 = (coord->y * M_PI) / 360.0;
     252        chu2 *= chu2;
     253        chu = sqrt(1.0 - chu1 - chu2);
     254        tmp->d = 2.0 * arg((2.0 * chu * chu) - 1.0, (coord->x * chu * M_PI) / 360.0);
     255        tmp->r = asin((coord->y * chu * M_PI) / 180.0);
     256
     257    } else if (projection->type == PS_PROJ_PAR) {
     258        psAbort(__func__, "The projection type PS_PROJ_PAR is undefined.\n");
     259
     260    } else if (projection->type == PS_PROJ_GLS) {
     261        psAbort(__func__, "The projection type PS_PROJ_GLG is undefined.\n");
     262    }
     263
     264    return (tmp);
    299265}
    300266
    301267// XXX: Do I need to check for unacceptable transformation parameters?
    302268// Maybe, if the points are on the North/South Pole, etc?
    303 psSphere *psSphereGetOffset( const psSphere *restrict position1,
    304                              const psSphere *restrict position2,
    305                              psSphereOffsetMode mode,
    306                              psSphereOffsetUnit unit )
    307 {
    308     //    psPlane *lin;
     269psSphere *psSphereGetOffset(const psSphere * restrict position1,
     270                            const psSphere * restrict position2,
     271                            psSphereOffsetMode mode, psSphereOffsetUnit unit)
     272{
     273    // psPlane *lin;
    309274    psProjection proj;
    310275    psSphere *tmp;
     
    312277    double tmpD = 0.0;
    313278
    314     if ( mode == PS_LINEAR ) {
     279    if (mode == PS_LINEAR) {
    315280        // XXX: I have no idea how to construct this.  Maybe project both
    316281        // sperical positions onto the plane, set the origin at one of the
     
    318283
    319284        // XXX: Do I need to somehow scale this projection?
    320         // project position1?  Will it project to (0.0, 0.0)?
     285        // project position1? Will it project to (0.0, 0.0)?
    321286        proj.R = position1->r;
    322287        proj.D = position1->d;
     
    325290        proj.type = PS_PROJ_TAN;
    326291
    327         //        lin = psProject(position2, proj);
    328         //        tmp = psDeproject(lin, proj);
     292        // lin = psProject(position2, proj);
     293        // tmp = psDeproject(lin, proj);
    329294
    330295        // XXX: Do we need to convert units in tmp?
    331         return ( tmp );
    332     } else
    333         if ( mode == PS_SPHERICAL ) {
    334             tmpR = position2->r - position1->r;
    335             tmpD = position2->d - position1->d;
    336 
    337             if ( unit == PS_ARCSEC ) {
    338                 tmpR = ( tmpR * 180.0 * 60.0 * 60.0 ) / M_PI;
    339                 tmpD = ( tmpR * 180.0 * 60.0 * 60.0 ) / M_PI;
    340             } else
    341                 if ( unit == PS_ARCMIN ) {
    342                     tmpR = ( tmpR * 180.0 * 60.0 ) / M_PI;
    343                     tmpD = ( tmpR * 180.0 * 60.0 ) / M_PI;
    344                 } else
    345                     if ( unit == PS_DEGREE ) {
    346                         tmpR = ( tmpR * 180.0 ) / M_PI;
    347                         tmpD = ( tmpR * 180.0 ) / M_PI;
    348                     } else
    349                     if ( unit == PS_RADIAN ) {}
    350                         else {
    351                             psAbort( __func__, "Unknown offset unit: 0x%x\n", unit );
    352                         }
    353 
    354             tmp = ( psSphere * ) psAlloc( sizeof( psSphere ) );
    355             tmp->r = tmpR;
    356             tmp->d = tmpD;
    357             tmp->rErr = 0.0;
    358             tmp->dErr = 0.0;
    359             // XXX: Do we need to wrap these to an acceptable range?
    360             return ( tmp );
     296        return (tmp);
     297    } else if (mode == PS_SPHERICAL) {
     298        tmpR = position2->r - position1->r;
     299        tmpD = position2->d - position1->d;
     300
     301        if (unit == PS_ARCSEC) {
     302            tmpR = (tmpR * 180.0 * 60.0 * 60.0) / M_PI;
     303            tmpD = (tmpR * 180.0 * 60.0 * 60.0) / M_PI;
     304        } else if (unit == PS_ARCMIN) {
     305            tmpR = (tmpR * 180.0 * 60.0) / M_PI;
     306            tmpD = (tmpR * 180.0 * 60.0) / M_PI;
     307        } else if (unit == PS_DEGREE) {
     308            tmpR = (tmpR * 180.0) / M_PI;
     309            tmpD = (tmpR * 180.0) / M_PI;
     310        } else if (unit == PS_RADIAN) {}
     311        else {
     312            psAbort(__func__, "Unknown offset unit: 0x%x\n", unit);
    361313        }
    362     psAbort( __func__, "Unrecognized offset mode\n" );
    363     return ( NULL );
    364 }
    365 
     314
     315        tmp = (psSphere *) psAlloc(sizeof(psSphere));
     316        tmp->r = tmpR;
     317        tmp->d = tmpD;
     318        tmp->rErr = 0.0;
     319        tmp->dErr = 0.0;
     320        // XXX: Do we need to wrap these to an acceptable range?
     321        return (tmp);
     322    }
     323    psAbort(__func__, "Unrecognized offset mode\n");
     324    return (NULL);
     325}
    366326
    367327// XXX: Do I need to check for unacceptable transformation parameters?
    368328// Maybe, if the points are on the North/South Pole, etc?
    369329// XXX: I copied the algorithm from the ADD exactly.
    370 psSphere *psSphereSetOffset( const psSphere *restrict position,
    371                              const psSphere *restrict offset,
    372                              psSphereOffsetMode mode,
    373                              psSphereOffsetUnit unit )
     330psSphere *psSphereSetOffset(const psSphere * restrict position,
     331                            const psSphere * restrict offset,
     332                            psSphereOffsetMode mode, psSphereOffsetUnit unit)
    374333{
    375334    psPlane lin;
     
    379338    double tmpD = 0.0;
    380339
    381     if ( mode == PS_LINEAR ) {
     340    if (mode == PS_LINEAR) {
    382341        proj.R = position->r;
    383342        proj.D = position->d;
     
    389348        lin.y = offset->d;
    390349
    391         tmp = psDeproject( &lin, &proj );
    392         return ( tmp );
    393 
    394     } else
    395         if ( mode == PS_SPHERICAL ) {
    396             if ( unit == PS_ARCSEC ) {
    397                 tmpR = ( M_PI * offset->r ) / ( 180.0 * 60.0 * 60.0 );
    398                 tmpD = ( M_PI * offset->d ) / ( 180.0 * 60.0 * 60.0 );
    399             } else
    400                 if ( unit == PS_ARCMIN ) {
    401                     tmpR = ( M_PI * offset->r ) / ( 180.0 * 60.0 );
    402                     tmpD = ( M_PI * offset->d ) / ( 180.0 * 60.0 );
    403                 } else
    404                     if ( unit == PS_DEGREE ) {
    405                         tmpR = ( M_PI * offset->r ) / ( 180.0 );
    406                         tmpD = ( M_PI * offset->d ) / ( 180.0 );
    407                     } else
    408                         if ( unit == PS_RADIAN ) {
    409                             tmpR = offset->r;
    410                             tmpD = offset->d;
    411                         } else {
    412                             psAbort( __func__, "Unknown offset unit: 0x%x\n", unit );
    413                         }
    414 
    415             tmp = ( psSphere * ) psAlloc( sizeof( psSphere ) );
    416             tmp->r = position->r + tmpR;
    417             tmp->r = position->d + tmpD;
    418             tmp->rErr = 0.0;
    419             tmp->dErr = 0.0;
    420 
    421             // XXX: wrap tmp->r and tmp->d to the allowed range (-PI to PI)
    422             // and (0 to 2*PI).
    423             return ( tmp );
     350        tmp = psDeproject(&lin, &proj);
     351        return (tmp);
     352
     353    } else if (mode == PS_SPHERICAL) {
     354        if (unit == PS_ARCSEC) {
     355            tmpR = (M_PI * offset->r) / (180.0 * 60.0 * 60.0);
     356            tmpD = (M_PI * offset->d) / (180.0 * 60.0 * 60.0);
     357        } else if (unit == PS_ARCMIN) {
     358            tmpR = (M_PI * offset->r) / (180.0 * 60.0);
     359            tmpD = (M_PI * offset->d) / (180.0 * 60.0);
     360        } else if (unit == PS_DEGREE) {
     361            tmpR = (M_PI * offset->r) / (180.0);
     362            tmpD = (M_PI * offset->d) / (180.0);
     363        } else if (unit == PS_RADIAN) {
     364            tmpR = offset->r;
     365            tmpD = offset->d;
     366        } else {
     367            psAbort(__func__, "Unknown offset unit: 0x%x\n", unit);
    424368        }
    425     psAbort( __func__, "Unrecognized offset mode\n" );
    426     return ( NULL );
    427 }
     369
     370        tmp = (psSphere *) psAlloc(sizeof(psSphere));
     371        tmp->r = position->r + tmpR;
     372        tmp->r = position->d + tmpD;
     373        tmp->rErr = 0.0;
     374        tmp->dErr = 0.0;
     375
     376        // XXX: wrap tmp->r and tmp->d to the allowed range (-PI to PI)
     377        // and (0 to 2*PI).
     378        return (tmp);
     379    }
     380    psAbort(__func__, "Unrecognized offset mode\n");
     381    return (NULL);
     382}
  • trunk/psLib/src/astro/psCoord.h

    r1393 r1407  
     1
    12/** @file  psCoord.h
    23*
     
    1011*  @author George Gusciora, MHPCC
    1112*
    12 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2004-08-05 19:38:51 $
     13*  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2004-08-07 00:06:06 $
    1415*
    1516*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1617*/
    1718
    18 # ifndef PS_COORD_H
    19 # define PS_COORD_H
    20 
    21 #include "psType.h"
    22 #include "psImage.h"
    23 #include "psArray.h"
    24 #include "psList.h"
    25 #include "psFunctions.h"
    26 #include "psTime.h"
     19#ifndef PS_COORD_H
     20#    define PS_COORD_H
     21
     22#    include "psType.h"
     23#    include "psImage.h"
     24#    include "psArray.h"
     25#    include "psList.h"
     26#    include "psFunctions.h"
     27#    include "psTime.h"
    2728
    2829/// @addtogroup CoordinateTransform
     
    3839typedef struct
    3940{
    40     double x;      ///< x position
    41     double y;      ///< y position
    42     double xErr;   ///< Error in x position
    43     double yErr;   ///< Error in y position
     41    double x;                   // /< x position
     42    double y;                   // /< y position
     43    double xErr;                // /< Error in x position
     44    double yErr;                // /< Error in y position
    4445}
    4546psPlane;
     
    5455typedef struct
    5556{
    56     double r;      ///< RA
    57     double d;      ///< Dec
    58     double rErr;   ///< Error in RA
    59     double dErr;   ///< Error in Dec
     57    double r;                   // /< RA
     58    double d;                   // /< Dec
     59    double rErr;                // /< Error in RA
     60    double dErr;                // /< Error in Dec
    6061}
    6162psSphere;
     
    7172typedef struct
    7273{
    73     psDPolynomial2D *x;                ///< 2D polynomial transform of X coordinates
    74     psDPolynomial2D *y;                ///< 2D polynomial transform of Y coordinates
     74    psDPolynomial2D *x;         // /< 2D polynomial transform of X coordinates
     75    psDPolynomial2D *y;         // /< 2D polynomial transform of Y coordinates
    7576}
    7677psPlaneTransform;
     
    9091typedef struct
    9192{
    92     psDPolynomial4D *x;                ///< 4D polynomial transform of X coordinates
    93     psDPolynomial4D *y;                ///< 4D polynomial transform of Y coordinates
     93    psDPolynomial4D *x;         // /< 4D polynomial transform of X coordinates
     94    psDPolynomial4D *y;         // /< 4D polynomial transform of Y coordinates
    9495}
    9596psPlaneDistort;
     
    107108typedef struct
    108109{
    109     double sinPhi;                    ///< sin of North Pole lattitude
    110     double cosPhi;                    ///< cos of North Pole lattitude
    111     double Xo;                        ///< First PT of Ares lon
    112     double xo;                        ///< First PT of Ares equiv lon
     110    double sinPhi;              // /< sin of North Pole lattitude
     111    double cosPhi;              // /< cos of North Pole lattitude
     112    double Xo;                  // /< First PT of Ares lon
     113    double xo;                  // /< First PT of Ares equiv lon
    113114}
    114115psSphereTransform;
     
    120121 */
    121122typedef enum {
    122     PS_PROJ_TAN,        ///< Tangent projection
    123     PS_PROJ_SIN,        ///< Sine projection
    124     PS_PROJ_AIT,        ///< Aitoff projection
    125     PS_PROJ_PAR,        ///< Par projection
    126     PS_PROJ_GLS,        ///< GLS projection
    127     PS_PROJ_CAR,        ///< CAR projection
    128     PS_PROJ_MER,        ///< MER projection
    129     PS_PROJ_NTYPE      ///< Number of types; must be last.
     123    PS_PROJ_TAN,                // /< Tangent projection
     124    PS_PROJ_SIN,                // /< Sine projection
     125    PS_PROJ_AIT,                // /< Aitoff projection
     126    PS_PROJ_PAR,                // /< Par projection
     127    PS_PROJ_GLS,                // /< GLS projection
     128    PS_PROJ_CAR,                // /< CAR projection
     129    PS_PROJ_MER,                // /< MER projection
     130    PS_PROJ_NTYPE               // /< Number of types; must be last.
    130131} psProjectionType;
    131132
     
    137138typedef struct
    138139{
    139     double R;     ///< Coordinates of projection center
    140     double D;     ///< Coordinates of projection center
    141     double Xs;    ///< plate-scale in X direction
    142     double Ys;    ///< plate-scale in Y direction
    143     psProjectionType type;  ///< Projection type
     140    double R;                   // /< Coordinates of projection center
     141    double D;                   // /< Coordinates of projection center
     142    double Xs;                  // /< plate-scale in X direction
     143    double Ys;                  // /< plate-scale in Y direction
     144    psProjectionType type;      // /< Projection type
    144145}
    145146psProjection;
     
    151152 */
    152153typedef enum {
    153     PS_SPHERICAL,                      ///< offset corresponds to an angular offset
    154     PS_LINEAR                          ///< offset corresponds to a linear offset
     154    PS_SPHERICAL,               // /< offset corresponds to an angular offset
     155    PS_LINEAR                   // /< offset corresponds to a linear offset
    155156} psSphereOffsetMode;
    156157
     
    161162 */
    162163typedef enum {
    163     PS_ARCSEC,                         ///< Arcseconds
    164     PS_ARCMIN,                         ///< Arcminutes
    165     PS_DEGREE,                         ///< Degrees
    166     PS_RADIAN                          ///< Radians
     164    PS_ARCSEC,                  // /< Arcseconds
     165    PS_ARCMIN,                  // /< Arcminutes
     166    PS_DEGREE,                  // /< Degrees
     167    PS_RADIAN                   // /< Radians
    167168} psSphereOffsetUnit;
    168169
     
    170171 *
    171172 */
    172 psPlane *psPlaneTransformApply(
    173     psPlane *out,                      ///< a psPlane to recycle.  If NULL, a new one is generated.
    174     const psPlaneTransform *transform, ///< the transform to apply
    175     const psPlane *coords              ///< the coordinate to apply the transform above.
    176 );
     173psPlane *psPlaneTransformApply(psPlane * out,   // /< a psPlane to recycle.  If NULL, a new one is generated.
     174                               const psPlaneTransform * transform,      // /< the transform to apply
     175                               const psPlane * coords   // /< the coordinate to apply the transform above.
     176                              );
    177177
    178178/** Applies the psPlaneDistort transform to a specified coordinate
    179179 *
    180180 */
    181 psPlane *psPlaneDistortApply(
    182     psPlane *out,                      ///< a psPlane to recycle.  If NULL, a new one is generated.
    183     const psPlaneDistort *transform,   ///< the transform to apply
    184     const psPlane *coords,             ///< the coordinate to apply the transform above.
    185     float term3,                       ///< third term -- maybe magnitude
    186     float term4                        ///< forth term -- maybe color
    187 );
     181psPlane *psPlaneDistortApply(psPlane * out,     // /< a psPlane to recycle.  If NULL, a new one is generated.
     182                             const psPlaneDistort * transform,  // /< the transform to apply
     183                             const psPlane * coords,    // /< the coordinate to apply the transform above.
     184                             float term3,       // /< third term -- maybe magnitude
     185                             float term4        // /< forth term -- maybe color
     186                            );
    188187
    189188/** Allocator for psSphereTransform
    190189 *
    191190 */
    192 psSphereTransform *psSphereTransformAlloc(
    193     double NPlat,                      ///< north pole latitude
    194     double Xo,                         ///< First PT of Ares lon
    195     double xo                          ///< First PT of Ares equiv lon
    196 );
    197 
     191psSphereTransform *psSphereTransformAlloc(double NPlat, // /< north pole latitude
     192        double Xo,    // /< First PT of Ares lon
     193        double xo     // /< First PT of Ares equiv lon
     194                                         );
    198195
    199196/** Applies the psSphereTransform transform for a specified coordinate
    200197 *
    201198 */
    202 psSphere *psSphereTransformApply(
    203     psSphere *out,                     ///< a psSphere to recycle.  If NULL, a new one is generated.
    204     const psSphereTransform *transform,///< the transform to apply
    205     const psSphere *coord              ///< the coordinate to apply the transform above.x
    206 );
    207 
    208 psSphereTransform *psSphereTransformICRStoEcliptic(
    209     psTime time
    210 );
    211 
    212 psSphereTransform *psSphereTransformEcliptictoICRS(
    213     psTime time
    214 );
    215 
    216 psSphereTransform *psSphereTransformICRStoGalatic( void );
    217 
    218 psSphereTransform *psSphereTransformGalatictoICRS( void );
    219 
    220 psPlane *psProject(
    221     const psSphere *coord,
    222     const psProjection *projection
    223 );
    224 
    225 psSphere *psDeproject(
    226     const psPlane *coord,
    227     const psProjection *projection
    228 );
    229 
    230 psSphere *psSphereGetOffset(
    231     const psSphere *restrict position1,
    232     const psSphere *restrict position2,
    233     psSphereOffsetMode mode,
    234     psSphereOffsetUnit unit
    235 );
    236 
    237 psSphere *psSphereSetOffset(
    238     const psSphere *restrict position,
    239     const psSphere *restrict offset,
    240     psSphereOffsetMode mode,
    241     psSphereOffsetUnit unit
    242 );
     199psSphere *psSphereTransformApply(psSphere * out,        // /< a psSphere to recycle.  If NULL, a new one is
     200                                 // generated.
     201                                 const psSphereTransform * transform,   // /< the transform to apply
     202                                 const psSphere * coord // /< the coordinate to apply the transform above.x
     203                                );
     204
     205psSphereTransform *psSphereTransformICRStoEcliptic(psTime time);
     206
     207psSphereTransform *psSphereTransformEcliptictoICRS(psTime time);
     208
     209psSphereTransform *psSphereTransformICRStoGalatic(void);
     210
     211psSphereTransform *psSphereTransformGalatictoICRS(void);
     212
     213psPlane *psProject(const psSphere * coord, const psProjection * projection);
     214
     215psSphere *psDeproject(const psPlane * coord, const psProjection * projection);
     216
     217psSphere *psSphereGetOffset(const psSphere * restrict position1,
     218                            const psSphere * restrict position2,
     219                            psSphereOffsetMode mode, psSphereOffsetUnit unit);
     220
     221psSphere *psSphereSetOffset(const psSphere * restrict position,
     222                            const psSphere * restrict offset,
     223                            psSphereOffsetMode mode, psSphereOffsetUnit unit);
    243224
    244225/// @}
  • trunk/psLib/src/astro/psTime.c

    r1406 r1407  
     1
    12/** @file  psTime.c
    23 *
     
    1213 *  @author Ross Harman, MHPCC
    1314 *
    14  *  @version $Revision: 1.13 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2004-08-06 22:34:05 $
     15 *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2004-08-07 00:06:06 $
    1617 *
    1718 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1819 */
    1920
    20 
    2121/******************************************************************************/
     22
    2223/*  INCLUDE FILES                                                             */
     24
    2325/******************************************************************************/
    2426
     
    3234
    3335/******************************************************************************/
     36
    3437/*  DEFINE STATEMENTS                                                         */
     38
    3539/******************************************************************************/
    3640
     
    7377}                                                                                                            \
    7478
     79
    7580/******************************************************************************/
     81
    7682/*  TYPE DEFINITIONS                                                          */
     83
    7784/******************************************************************************/
    7885
     
    8087
    8188/*****************************************************************************/
     89
    8290/*  GLOBAL VARIABLES                                                         */
     91
    8392/*****************************************************************************/
    8493
     
    8695
    8796/*****************************************************************************/
     97
    8898/*  FILE STATIC VARIABLES                                                    */
     99
    89100/*****************************************************************************/
    90101
     
    119130
    120131// Table for Julian date of leapsecond update and current total number of leapseconds at that date
    121 static double leapseconds[NUM_LEAPSECOND_UPDATES][2] =
    122     {
    123         {
    124             2441317.5, 10.0
    125         },
    126         {
    127             2441499.5, 11.0
    128         },
    129         {
    130             2441683.5, 12.0
    131         },
    132         {
    133             2442048.5, 13.0
    134         },
    135         {
    136             2442413.5, 14.0
    137         },
    138         {
    139             2442778.5, 15.0
    140         },
    141         {
    142             2443144.5, 16.0
    143         },
    144         {
    145             2443509.5, 17.0
    146         },
    147         {
    148             2443874.5, 18.0
    149         },
    150         {
    151             2444239.5, 19.0
    152         },
    153         {
    154             2444786.5, 20.0
    155         },
    156         {
    157             2445151.5, 21.0
    158         },
    159         {
    160             2445516.5, 22.0
    161         },
    162         {
    163             2446247.5, 23.0
    164         },
    165         {
    166             2447161.5, 24.0
    167         },
    168         {
    169             2447892.5, 25.0
    170         },
    171         {
    172             2448257.5, 26.0
    173         },
    174         {
    175             2448804.5, 27.0
    176         },
    177         {
    178             2449169.5, 28.0
    179         },
    180         {
    181             2449534.5, 29.0
    182         },
    183         {
    184             2450083.5, 30.0
    185         },
    186         {
    187             2450630.5, 31.0
    188         },
    189         {
    190             2451179.5, 32.0
    191         }
    192     };
    193 
    194 /*****************************************************************************/
     132static double leapseconds[NUM_LEAPSECOND_UPDATES][2] = {
     133            {
     134                2441317.5, 10.0},
     135            {
     136                2441499.5, 11.0},
     137            {
     138                2441683.5, 12.0},
     139            {
     140                2442048.5, 13.0},
     141            {
     142                2442413.5, 14.0},
     143            {
     144                2442778.5, 15.0},
     145            {
     146                2443144.5, 16.0},
     147            {
     148                2443509.5, 17.0},
     149            {
     150                2443874.5, 18.0},
     151            {
     152                2444239.5, 19.0},
     153            {
     154                2444786.5, 20.0},
     155            {
     156                2445151.5, 21.0},
     157            {
     158                2445516.5, 22.0},
     159            {
     160                2446247.5, 23.0},
     161            {
     162                2447161.5, 24.0},
     163            {
     164                2447892.5, 25.0},
     165            {
     166                2448257.5, 26.0},
     167            {
     168                2448804.5, 27.0},
     169            {
     170                2449169.5, 28.0},
     171            {
     172                2449534.5, 29.0},
     173            {
     174                2450083.5, 30.0},
     175            {
     176                2450630.5, 31.0},
     177            {
     178                2451179.5, 32.0}
     179        };
     180
     181/*****************************************************************************/
     182
    195183/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
     184
    196185/*****************************************************************************/
    197186
     
    199188
    200189/*****************************************************************************/
     190
    201191/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     192
    202193/*****************************************************************************/
    203194
     
    210201    time.tv_usec = 0;
    211202
    212     if(gettimeofday(&now,(struct timezone *) 0) == -1) {
     203    if (gettimeofday(&now, (struct timezone *)0) == -1) {
    213204        psError(__func__, " : Line %d - Failed to get time", __LINE__);
    214205        return time;
    215206    }
    216 
    217207    // Convert timeval time to psTime
    218208    time.tv_sec = now.tv_sec;
     
    220210
    221211    // Add most current leapseconds value to UTC time to get TAI time
    222     time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES-1][1];
     212    time.tv_sec += leapseconds[NUM_LEAPSECOND_UPDATES - 1][1];
    223213
    224214    return time;
    225215}
    226216
    227 char* psTimeToISO(psTime time)
     217char *psTimeToISO(psTime time)
    228218{
    229219    int ms = 0;
     
    232222    struct tm *tmTime = NULL;
    233223
    234     CHECK_NEGATIVE_TIME_STRUCT(time,NULL);
     224    CHECK_NEGATIVE_TIME_STRUCT(time, NULL);
    235225
    236226    tempString = psAlloc(MAX_TIME_STRING_LENGTH);
     
    238228
    239229    // Converts psTime to YYYY/MM/DD,HH:MM:SS.SSS in string form
    240     ms = time.tv_usec/1000;
     230    ms = time.tv_usec / 1000;
    241231
    242232    // tmTime variable is statically allocated, no need to free
    243233    tmTime = gmtime(&time.tv_sec);
    244     if(!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) {
     234    if (!strftime(tempString, MAX_TIME_STRING_LENGTH, "%Y/%m/%d,%H:%M:%S", tmTime)) {
    245235        psError(__func__, " : Line %d - Failed strftime conversion", __LINE__);
    246236    }
    247237
    248     if(snprintf(timeString,MAX_TIME_STRING_LENGTH,"%s.%3.3d", tempString, ms) < 0) {
     238    if (snprintf(timeString, MAX_TIME_STRING_LENGTH, "%s.%3.3d", tempString, ms) < 0) {
    249239        psError(__func__, " : Line %d - Failed snprintf conversion", __LINE__);
    250240    }
     
    262252    psTime outTime;
    263253
    264     CHECK_NEGATIVE_TIME_STRUCT(time,outTime);
     254    CHECK_NEGATIVE_TIME_STRUCT(time, outTime);
    265255
    266256    // Find leapseconds to subtract from psTime to get UTC time
    267257    jd = psTimeToJD(time);
    268258    jdTable = leapseconds[0];
    269     for(i=0; i<NUM_LEAPSECOND_UPDATES; i++, jdTable+=2) {
    270         if(jd > *jdTable) {
     259    for (i = 0; i < NUM_LEAPSECOND_UPDATES; i++, jdTable += 2) {
     260        if (jd > *jdTable) {
    271261            ls = *(jdTable + 1);
    272262        }
     
    276266    outTime.tv_usec = time.tv_usec;
    277267
    278     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
     268    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
    279269
    280270    return outTime;
     
    285275    double mjd = 0.0;
    286276
    287     CHECK_NEGATIVE_TIME_STRUCT(time,mjd);
     277    CHECK_NEGATIVE_TIME_STRUCT(time, mjd);
    288278
    289279    // Modified Julian date conversion courtesy of Eugene Magnier
    290     mjd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 40587.0;
     280    mjd = time.tv_sec / SEC_PER_DAY + time.tv_usec / USEC_PER_DAY + 40587.0;
    291281
    292282    return mjd;
     
    297287    double jd = 0.0;
    298288
    299     CHECK_NEGATIVE_TIME_STRUCT(time,jd);
     289    CHECK_NEGATIVE_TIME_STRUCT(time, jd);
    300290
    301291    // Julian date conversion courtesy of Eugene Magnier
    302     jd = time.tv_sec/SEC_PER_DAY + time.tv_usec/USEC_PER_DAY + 2440587.5;
     292    jd = time.tv_sec / SEC_PER_DAY + time.tv_usec / USEC_PER_DAY + 2440587.5;
    303293
    304294    return jd;
     
    309299    struct timeval timevalTime;
    310300
    311     CHECK_NEGATIVE_TIME_STRUCT(time,timevalTime);
     301    CHECK_NEGATIVE_TIME_STRUCT(time, timevalTime);
    312302    timevalTime.tv_sec = time.tv_sec;
    313303    timevalTime.tv_usec = time.tv_usec;
     
    316306}
    317307
    318 struct tm* psTimeToTM(psTime time)
     308struct tm *psTimeToTM(psTime time)
    319309{
    320310    struct tm *tmTime = NULL;
    321311
    322     CHECK_NEGATIVE_TIME_STRUCT(time,tmTime);
     312    CHECK_NEGATIVE_TIME_STRUCT(time, tmTime);
    323313    tmTime = gmtime(&time.tv_sec);
    324314
     
    343333    // Convert YYYY/MM/DD,HH:MM:SS.SSS in string form to tm time
    344334    year = atoi(strtok(tempString, "/"));
    345     if(year < 1900) {
    346         psError(__func__,"Years less than 1900 not allowed. Value: %d", year);
     335    if (year < 1900) {
     336        psError(__func__, "Years less than 1900 not allowed. Value: %d", year);
    347337        return outTime;
    348338    }
    349339
    350340    month = atoi(strtok(NULL, "/"));
    351     if(month<1 || month>12) {
    352         psError(__func__,"Month must have a value from 1 to 12. Value: %d", month);
     341    if (month < 1 || month > 12) {
     342        psError(__func__, "Month must have a value from 1 to 12. Value: %d", month);
    353343        return outTime;
    354344    }
    355345
    356346    day = atoi(strtok(NULL, ","));
    357     if(day<1 || day>31) {
    358         psError(__func__,"Day must have a value from 1 to 31. Value: %d", day);
     347    if (day < 1 || day > 31) {
     348        psError(__func__, "Day must have a value from 1 to 31. Value: %d", day);
    359349        return outTime;
    360350    }
    361351
    362352    hour = atoi(strtok(NULL, ":"));
    363     if(hour<0 || hour>23) {
    364         psError(__func__,"Hour must have a value from 0 to 23. Value: %d", hour);
     353    if (hour < 0 || hour > 23) {
     354        psError(__func__, "Hour must have a value from 0 to 23. Value: %d", hour);
    365355        return outTime;
    366356    }
    367357
    368358    minute = atoi(strtok(NULL, ":"));
    369     if(minute<0 || minute>59) {
    370         psError(__func__,"Minute must have a value from 0 to 59. Value: %d", minute);
     359    if (minute < 0 || minute > 59) {
     360        psError(__func__, "Minute must have a value from 0 to 59. Value: %d", minute);
    371361        return outTime;
    372362    }
    373363
    374364    second = atoi(strtok(NULL, "."));
    375     if(second<0 || second>59) {
    376         psError(__func__,"Second must have a value from 0 to 59. Value: %d", second);
     365    if (second < 0 || second > 59) {
     366        psError(__func__, "Second must have a value from 0 to 59. Value: %d", second);
    377367        return outTime;
    378368    }
    379369
    380370    millisecond = atoi(strtok(NULL, "X"));
    381     if(millisecond<0 || millisecond>1000) {
    382         psError(__func__,"Millisecond must have a value from 0 to 999. Value: %d", millisecond);
     371    if (millisecond < 0 || millisecond > 1000) {
     372        psError(__func__, "Millisecond must have a value from 0 to 999. Value: %d", millisecond);
    383373        return outTime;
    384374    }
     
    394384    // Convert tm time to psTime
    395385    outTime = psTMToTime(&tmTime);
    396     outTime.tv_usec = millisecond*1000;
     386    outTime.tv_usec = millisecond * 1000;
    397387
    398388    return outTime;
     
    403393    psTime outTime;
    404394
    405     CHECK_NEGATIVE_TIME_STRUCT(time,outTime);
     395    CHECK_NEGATIVE_TIME_STRUCT(time, outTime);
    406396
    407397    // Convert UTC time to psTime/TAI
    408     outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES-1][1];
     398    outTime.tv_sec = time.tv_sec + leapseconds[NUM_LEAPSECOND_UPDATES - 1][1];
    409399    outTime.tv_usec = time.tv_usec;
    410     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
     400    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
    411401
    412402    return outTime;
     
    419409    double seconds = 0.0;
    420410
    421     CHECK_NEGATIVE_TIME(time,outTime);
     411    CHECK_NEGATIVE_TIME(time, outTime);
    422412
    423413    // Modified Julian date conversion courtesy of Eugene Magnier
     
    425415
    426416    // Convert to psTime/TAI
    427     seconds = days*SEC_PER_DAY;
    428     outTime.tv_usec = (seconds -(long)seconds)*1000000.0;
     417    seconds = days * SEC_PER_DAY;
     418    outTime.tv_usec = (seconds - (long)seconds) * 1000000.0;
    429419    outTime.tv_sec = seconds;
    430420
    431     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
     421    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
    432422
    433423    return outTime;
     
    440430    psTime outTime;
    441431
    442     CHECK_NEGATIVE_TIME(time,outTime);
     432    CHECK_NEGATIVE_TIME(time, outTime);
    443433
    444434    // Julian date conversion courtesy of Eugene Magnier
     
    446436
    447437    // Convert to psTime/TAI
    448     seconds = days*SEC_PER_DAY;
     438    seconds = days * SEC_PER_DAY;
    449439    outTime.tv_sec = seconds;
    450     outTime.tv_usec = (seconds -(long)seconds)*1000000.0;
    451 
    452     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
    453 
    454     return outTime;
    455 }
    456 
    457 psTime psTimevalToTime(struct timeval *time)
    458 {
    459     psTime outTime;
    460     if(time == NULL)
    461     {
    462         psError(__func__,"Null value for timeval arg not allowed");
    463         return outTime;
    464     } else
    465         if(time->tv_sec < 0)
    466         {
    467             psError(__func__,"Negative seconds are not allowed: %ld", time->tv_sec);
    468             return outTime;
    469         } else
    470             if(time->tv_usec<0)
    471             {
    472                 psError(__func__,"Negative microseconds are not allowed: %ld", time->tv_usec);
    473                 return outTime;
    474             }
    475 
     440    outTime.tv_usec = (seconds - (long)seconds) * 1000000.0;
     441
     442    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
     443
     444    return outTime;
     445}
     446
     447psTime psTimevalToTime(struct timeval * time)
     448{
     449    psTime outTime;
     450
     451    if (time == NULL)
     452    {
     453        psError(__func__, "Null value for timeval arg not allowed");
     454        return outTime;
     455    } else if (time->tv_sec < 0)
     456    {
     457        psError(__func__, "Negative seconds are not allowed: %ld", time->tv_sec);
     458        return outTime;
     459    } else if (time->tv_usec < 0)
     460    {
     461        psError(__func__, "Negative microseconds are not allowed: %ld", time->tv_usec);
     462        return outTime;
     463    }
    476464    // Convert to psTime/TAI
    477465    outTime.tv_sec = time->tv_sec;
    478466    outTime.tv_usec = time->tv_usec;
    479467
    480     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
    481 
    482     return outTime;
    483 }
    484 
    485 
    486 psTime psTMToTime(struct tm *time)
     468    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
     469
     470    return outTime;
     471}
     472
     473psTime psTMToTime(struct tm * time)
    487474{
    488475    int i;
    489476    int n;
    490477    int y;
    491     int mon [] =
    492         {
    493             31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    494         };
     478    int mon[] = {
     479                    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
     480                };
    495481    long epoch;
    496482    psTime outTime;
    497483
    498     i= 0;
     484    i = 0;
    499485    n = 0;
    500486    y = 0;
    501487    epoch = 0;
    502488
    503     if(time == NULL)
    504     {
    505         psError(__func__,"Null value for tm arg not allowed");
    506         return outTime;
    507     } else
    508         if(time->tm_year < 70)
    509         {
    510             psError(__func__,"Input times earlier than 1970 not allowed. Value: %d", time->tm_year+1900);
    511             return outTime;
    512         } else
    513             if(time->tm_mon<0 || time->tm_mon>11)
    514             {
    515                 psError(__func__,"Month must have a value from 0 to 11. Value: %d", time->tm_mon);
    516                 return outTime;
    517             } else
    518                 if(time->tm_mday<1 || time->tm_mday>31)
    519                 {
    520                     psError(__func__,"Day must have a value from 1 to 31. Value: %d", time->tm_mday);
    521                     return outTime;
    522                 } else
    523                     if(time->tm_hour<0 || time->tm_hour>23)
    524                     {
    525                         psError(__func__,"Hour must have a value from 0 to 23. Value: %d", time->tm_hour);
    526                         return outTime;
    527                     } else
    528                         if(time->tm_min<0 || time->tm_min>59)
    529                         {
    530                             psError(__func__,"Minute must have a value from 0 to 59. Value: %d", time->tm_min);
    531                             return outTime;
    532                         } else
    533                             if(time->tm_sec<0 || time->tm_sec>59)
    534                             {
    535                                 psError(__func__,"Second must have a value from 0 to 59. Value: %d", time->tm_sec);
    536                                 return outTime;
    537                             }
     489    if (time == NULL)
     490    {
     491        psError(__func__, "Null value for tm arg not allowed");
     492        return outTime;
     493    } else if (time->tm_year < 70)
     494    {
     495        psError(__func__, "Input times earlier than 1970 not allowed. Value: %d", time->tm_year + 1900);
     496        return outTime;
     497    } else if (time->tm_mon < 0 || time->tm_mon > 11)
     498    {
     499        psError(__func__, "Month must have a value from 0 to 11. Value: %d", time->tm_mon);
     500        return outTime;
     501    } else if (time->tm_mday < 1 || time->tm_mday > 31)
     502    {
     503        psError(__func__, "Day must have a value from 1 to 31. Value: %d", time->tm_mday);
     504        return outTime;
     505    } else if (time->tm_hour < 0 || time->tm_hour > 23)
     506    {
     507        psError(__func__, "Hour must have a value from 0 to 23. Value: %d", time->tm_hour);
     508        return outTime;
     509    } else if (time->tm_min < 0 || time->tm_min > 59)
     510    {
     511        psError(__func__, "Minute must have a value from 0 to 59. Value: %d", time->tm_min);
     512        return outTime;
     513    } else if (time->tm_sec < 0 || time->tm_sec > 59)
     514    {
     515        psError(__func__, "Second must have a value from 0 to 59. Value: %d", time->tm_sec);
     516        return outTime;
     517    }
    538518
    539519    n = time->tm_year + 1900 - 1;
    540     epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n/4 - n/100 + n/400) -
    541             (1969/4 - 1969/100 + 1969/400)) * SEC_PER_DAY;
     520    epoch = (time->tm_year - 70) * SEC_PER_YEAR + ((n / 4 - n / 100 + n / 400) -
     521            (1969 / 4 - 1969 / 100 + 1969 / 400)) * SEC_PER_DAY;
    542522
    543523    y = time->tm_year + 1900;
    544524
    545525    // Adjust for leap years
    546     for(i = 0; i<time->tm_mon; i++)
    547     {
    548         epoch += mon [i] * SEC_PER_DAY;
    549         if(i == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
     526    for (i = 0; i < time->tm_mon; i++)
     527    {
     528        epoch += mon[i] * SEC_PER_DAY;
     529        if (i == 1 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) {
    550530            epoch += SEC_PER_DAY;
    551531        }
     
    554534    // Add everything
    555535    epoch += (time->tm_mday - 1) * SEC_PER_DAY;
    556     epoch += time->tm_hour *SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec;
     536    epoch += time->tm_hour * SEC_PER_HOUR + time->tm_min * SEC_PER_MINUTE + time->tm_sec;
    557537
    558538    // Create psTime
     
    560540    outTime.tv_sec = epoch;
    561541
    562     CHECK_NEGATIVE_TIME_STRUCT(outTime,outTime);
    563 
    564     return outTime;
    565 }
     542    CHECK_NEGATIVE_TIME_STRUCT(outTime, outTime);
     543
     544    return outTime;
     545}
  • trunk/psLib/src/astro/psTime.h

    r1406 r1407  
     1
    12/** @file  psTime.h
    23 *
     
    1213 *  @author Ross Harman, MHPCC
    1314 *
    14  *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2004-08-06 22:34:05 $
     15 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2004-08-07 00:06:06 $
    1617 *
    1718 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1920
    2021#ifndef PSTIME_H
    21 #define PSTIME_H
    22 
    23 #include <time.h>
    24 #include <sys/types.h>
    25 #include <sys/time.h>
    26 
    27 #include "psType.h"
     22#    define PSTIME_H
     23
     24#    include <time.h>
     25#    include <sys/types.h>
     26#    include <sys/time.h>
     27
     28#    include "psType.h"
    2829
    2930/// @addtogroup Time
     
    3132
    3233/******************************************************************************/
     34
    3335/*  TYPE DEFINITIONS                                                          */
     36
    3437/******************************************************************************/
    3538
     
    4245typedef struct
    4346{
    44     time_t        tv_sec;    /**< Seconds since epoch, Jan 1, 1970. */
    45     suseconds_t   tv_usec;   /**< Microseconds since last second. */
     47
     48    time_t tv_sec;           /**< Seconds since epoch, Jan 1, 1970. */
     49
     50    suseconds_t tv_usec;     /**< Microseconds since last second. */
    4651}
    4752psTime;
    4853
    4954/*****************************************************************************/
     55
    5056/* FUNCTION PROTOTYPES                                                       */
     57
    5158/*****************************************************************************/
    5259
     
    5764 *  @return  psTime: Struct with current time.
    5865 */
    59 psTime psTimeGetTime(
    60     void   /** No argument. */
    61 );
     66
     67psTime psTimeGetTime(void
     68                     /** No argument. */
     69                    );
    6270
    6371/** Convert psTime to ISO time in TAI units.
     
    6876 *  @return  char*: Pointer null terminated array of chars in ISO time.
    6977 */
    70 char* psTimeToISO(
    71     psTime time     /** Input time to be converted. */
    72 );
     78
     79char *psTimeToISO(psTime time
     80                  /** Input time to be converted. */
     81                 );
    7382
    7483/** Convert psTime to UTC time.
     
    8089 *  @return  psTime: UTC time psTime format.
    8190 */
    82 psTime psTimeToUTC(
    83     psTime time    /** Input time to be converted. */
    84 );
     91
     92psTime psTimeToUTC(psTime time
     93                   /** Input time to be converted. */
     94                  );
    8595
    8696/** Convert psTime to modified Julian date time.
     
    91101 *  @return  double: Modified Julian Days (MJD) time.
    92102 */
    93 double psTimeToMJD(
    94     psTime time    /** Input time to be converted. */
    95 );
     103
     104double psTimeToMJD(psTime time
     105                   /** Input time to be converted. */
     106                  );
    96107
    97108/** Convert psTime to Julian date time.
     
    102113 *  @return  double: Julian Date (JD) time.
    103114 */
    104 double psTimeToJD(
    105     psTime time    /** Input time to be converted. */
    106 );
     115
     116double psTimeToJD(psTime time
     117                  /** Input time to be converted. */
     118                 );
    107119
    108120/** Convert psTime to timeval time.
     
    113125 *  @return  timeval: timeval struct time.
    114126 */
    115 struct timeval psTimeToTimeval(
    116                 psTime time    /** Input time to be converted. */
    117             );
     127
     128struct timeval psTimeToTimeval(psTime time
     129                                           /** Input time to be converted. */
     130                                          );
    118131
    119132/** Convert psTime to tm time.
     
    124137 *  @return  tm: tm struct time.
    125138 */
    126 struct tm* psTimeToTM(
    127                 psTime time    /** Input time to be converted. */
    128             );
     139
     140struct tm *psTimeToTM(psTime time
     141                                  /** Input time to be converted. */
     142                                 );
    129143
    130144/** Convert ISO to psTime.
     
    135149 *  @return  psTime: time
    136150 */
    137 psTime psISOToTime(
    138     char *time  /** Input time to be converted. */
    139 );
     151
     152psTime psISOToTime(char *time
     153                   /** Input time to be converted. */
     154                  );
    140155
    141156/** Convert UTC to psTime.
     
    146161 *  @return  psTime: time in TAI units.
    147162 */
    148 psTime psUTCToTime(
    149     psTime time /** Input time to be converted. */
    150 );
     163
     164psTime psUTCToTime(psTime time
     165                   /** Input time to be converted. */
     166                  );
    151167
    152168/** Convert MJD to psTime.
     
    157173 *  @return  psTime: time.
    158174 */
    159 psTime psMJDToTime(
    160     double time /** Input time to be converted. */
    161 );
     175
     176psTime psMJDToTime(double time
     177                   /** Input time to be converted. */
     178                  );
    162179
    163180/** Convert JD to psTime.
     
    168185 *  @return  psTime: time.
    169186 */
    170 psTime psJDToTime(
    171     double time /** Input time to be converted. */
    172 );
     187
     188psTime psJDToTime(double time
     189                  /** Input time to be converted. */
     190                 );
    173191
    174192/** Convert timeval to psTime.
     
    179197 *  @return  psTime: time.
    180198 */
    181 psTime psTimevalToTime(
    182     struct timeval *time    /** Input time to be converted. */
    183 );
     199
     200psTime psTimevalToTime(struct timeval *time
     201                       /** Input time to be converted. */
     202                      );
    184203
    185204/** Convert tm time to psTime.
     
    190209 *  @return  psTime: time.
    191210 */
    192 psTime psTMToTime(
    193     struct tm *time /** Input time to be converted. */
    194 );
     211
     212psTime psTMToTime(struct tm *time
     213                  /** Input time to be converted. */
     214                 );
     215
    195216/// @}
    196217
Note: See TracChangeset for help on using the changeset viewer.