IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 16, 2005, 6:41:16 PM (21 years ago)
Author:
eugene
Message:

cleaning include files, merging time/coord functions in libohana

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.data/convert.c

    r4697 r4805  
    33# include <time.h>
    44
    5 char *strptime(const char *s, const char *format, struct tm *tm);
    6 
    7  
     5/** additional time / coordinate conversions not supplied by libohana **/
     6
    87int hh_hms (double hh, int *hr, int *mn, double *sc) {
    98
     
    111110}
    112111
    113 /***** convert [-]00:00:00 to 0.0000 ****/
    114 int dms_to_ddd (double *Value, char *string) {
    115  
    116   int valid, neg, status;
    117   double tmp, value;
    118   char *p1, *p2, *px;
    119 
    120   valid = FALSE;
    121   neg = FALSE;
    122   stripwhite (string);
    123   p1 = string;
    124   px = string + strlen(string);
    125 
    126   if (string[0] == '-') {
    127     valid = TRUE;
    128     neg = TRUE;
    129     p1 = &string[1];
    130   }
    131   if (string[0] == '+') {
    132     valid = TRUE;
    133     neg = FALSE;
    134     p1 = &string[1];
    135   }
    136   if (isdigit(string[0])) {
    137     valid = TRUE;
    138     p1 = &string[0];
    139   }
    140   if (!valid) { return (FALSE); }
    141 
    142   status = 1;
    143   tmp = strtod (p1, &p2);
    144   if (p2 == p1) return (FALSE); /* entry not a number: +fred */
    145   value = tmp;
    146   if (p2 == px) goto escape;    /* entry only number: +1.0 */
    147   p1 = p2 + 1;
    148 
    149   tmp = strtod (p1, &p2);
    150   if (p2 == p1) goto escape;    /* entry not a number: +1:fred */
    151   status = 2;
    152   value += tmp / 60.0;
    153   if (p2 == px) goto escape;    /* entry only number: +1:1 */
    154   p1 = p2 + 1;
    155 
    156   tmp = strtod (p1, &p2);
    157   if (p2 == p1) goto escape;    /* entry not a number: +1:1:fred */
    158   value += tmp / 3600.0;
    159 
    160  escape:
    161   if (neg) {
    162     value *= -1;
    163   }
    164   *Value = value;
    165 
    166   return (status);
    167 }
    168 
    169112/***** convert 00:00:00 or 00:00 to 0 - 86400 ****/
    170113int hms_to_sec (char *string, time_t *second) {
     
    211154  if (*p) return (FALSE);
    212155  *second = time.tm_wday*86400 + time.tm_hour*3600 + time.tm_min*60 + time.tm_sec;
    213   return (TRUE);
    214 }
    215 
    216 /**********/
    217 int str_to_radec (double *ra, double *dec, char *str1, char *str2) {
    218 
    219   double Ra, Dec;
    220 
    221   *ra = *dec = 0;
    222   switch (dms_to_ddd (&Ra, str1)) {
    223   case 0:
    224     fprintf (stderr, "syntax error in RA\n");
    225     return (FALSE);
    226   case 1:
    227     break;
    228   case 2:
    229     Ra = Ra * 15;
    230     break;
    231   }
    232   switch (dms_to_ddd (&Dec, str2)) {
    233   case 0:
    234     fprintf (stderr, "syntax error in DEC\n");
    235     return (FALSE);
    236   case 1:
    237   case 2:
    238     break;
    239   }
    240   *ra = Ra;
    241   *dec = Dec;
    242156  return (TRUE);
    243157}
     
    302216}
    303217
    304 /**********/
    305 int chk_time (char *line) {
    306 
    307   char *p1, *p2;
    308   double tmp;
    309   int mode;
    310 
    311   p1 = line;
    312   tmp = strtod (p1, &p2);
    313   mode = TIME_DATE;
    314   if (p2 == p1 + strlen (p1) - 1) {
    315     if (*p2 == 'j') {
    316       mode = TIME_JD;
    317     }
    318     if (*p2 == 'J') {
    319       mode = TIME_MJD;
    320     }
    321     if (*p2 == 'd') {
    322       mode = TIME_DAYS;
    323     }
    324     if (*p2 == 'h') {
    325       mode = TIME_HOURS;
    326     }
    327     if (*p2 == 'm') {
    328       mode = TIME_MINUTES;
    329     }
    330     if (*p2 == 's') {
    331       mode = TIME_SECONDS;
    332     }
    333   }
    334   return (mode);
    335 }
    336 
    337 /**********/
    338 int str_to_time (char *line, time_t *second) {
    339  
    340   char *tmpline;
    341   struct tm *gmt;
    342   struct timeval now;
    343   double jd;
    344   time_t tsec;
    345 
    346   if (!strcasecmp (line, "NOW")) {
    347     gettimeofday (&now, (struct timezone *) NULL);
    348     *second = now.tv_sec;
    349     return (TRUE);
    350   }
    351    
    352   if (!strncasecmp (line, "TODAY", 5)) {
    353     gettimeofday (&now, (struct timezone *) NULL);
    354     if (line[5]) { /* line has extra data (ie, hh:mm:ss) */
    355       tsec = now.tv_sec;
    356       ALLOCATE (tmpline, char, 64);
    357       gmt   = gmtime (&tsec);
    358       sprintf (tmpline, "%04d/%02d/%02d,%s",
    359                1900 + gmt[0].tm_year, gmt[0].tm_mon+1, gmt[0].tm_mday, &line[6]);
    360       *second = date_to_sec (tmpline);
    361       free (tmpline);
    362       return (TRUE);
    363     } else {
    364       *second = 86400 * ((int)(now.tv_sec / 86400));
    365       return (TRUE);
    366     }
    367   }
    368    
    369   switch (chk_time (line)) {
    370   case 0:
    371     return (FALSE);
    372   case TIME_JD:
    373     jd = strtod (line, 0);
    374     *second = jd_to_sec (jd);
    375     return (TRUE);
    376   case TIME_MJD:
    377     jd = strtod (line, 0);
    378     *second = mjd_to_sec (jd);
    379     return (TRUE);
    380   case TIME_DATE:
    381     *second = date_to_sec (line);
    382     return (TRUE);
    383   case TIME_DAYS:
    384     *second = strtod (line, 0) * 86400.0;
    385     return (TRUE);
    386   case TIME_HOURS:
    387     *second = strtod (line, 0) * 3600.0;
    388     return (TRUE);
    389   case TIME_MINUTES:
    390     *second = strtod (line, 0) * 60.0;
    391     return (TRUE);
    392   case TIME_SECONDS:
    393     *second = strtod (line, 0);
    394     return (TRUE);
    395   }
    396   return (FALSE);
    397 }
    398 
    399 
    400 /**********/
    401 int str_to_dtime (char *line, double *second) {
    402  
    403   switch (chk_time (line)) {
    404   case 0:
    405   case TIME_JD:
    406   case TIME_MJD:
    407   case TIME_DATE:
    408     return (FALSE);
    409   case TIME_DAYS:
    410     *second = strtod (line, 0) * 86400.0;
    411     return (TRUE);
    412   case TIME_HOURS:
    413     *second = strtod (line, 0) * 3600.0;
    414     return (TRUE);
    415   case TIME_MINUTES:
    416     *second = strtod (line, 0) * 60.0;
    417     return (TRUE);
    418   case TIME_SECONDS:
    419     *second = strtod (line, 0);
    420     return (TRUE);
    421   }
    422   return (FALSE);
    423 }
    424 
    425 /**********/
    426 double sec_to_jd (time_t second) {
    427 
    428   double jd;
    429  
    430   jd = second/86400.0 + 2440587.5;
    431   return (jd);
    432 }
    433 
    434 /**********/
    435 time_t jd_to_sec (double jd) {
    436 
    437   time_t second;
    438 
    439   second = (jd - 2440587.5)*86400;
    440   return (second);
    441 }
    442 
    443 /**********/
    444 double sec_to_mjd (time_t second) {
    445 
    446   double mjd;
    447  
    448   mjd = second/86400.0 + 40587.0;
    449   return (mjd);
    450 }
    451 
    452 /**********/
    453 time_t mjd_to_sec (double mjd) {
    454 
    455   time_t second;
    456 
    457   second = (mjd - 40587.0)*86400;
    458   return (second);
    459 }
    460 
    461 /**********/
    462 char *sec_to_date (time_t second) {
    463  
    464   struct tm *gmt;
    465   char *line;
    466  
    467   ALLOCATE (line, char, 64);
    468   gmt   = gmtime (&second);
    469   sprintf (line, "%04d/%02d/%02d,%02d:%02d:%02d",
    470            1900 + gmt[0].tm_year, gmt[0].tm_mon+1, gmt[0].tm_mday,
    471            gmt[0].tm_hour, gmt[0].tm_min, gmt[0].tm_sec);
    472   return (line);
    473 
    474 }
    475 
    476 /***** date in format yyyy/mm/dd,hh:mm:ss *****/
    477 time_t date_to_sec (char *date) {
    478  
    479   time_t second;
    480   double tmp, jd;
    481   struct tm now;
    482   char *p1, *p2, *px;
    483  
    484   p1 = date;
    485   px = date + strlen(date);
    486   bzero (&now, sizeof(now));
    487 
    488   tmp = strtod (p1, &p2);
    489   if (p2 == p1) goto escape;
    490   now.tm_year = tmp;
    491   if (now.tm_year > 1000) now.tm_year -= 1900;
    492   if (now.tm_year <   50) now.tm_year += 100;
    493   if (p2 == px) goto escape; 
    494   p1 = p2 + 1;
    495 
    496   tmp = strtod (p1, &p2);
    497   if (p2 == p1) goto escape;
    498   now.tm_mon = tmp - 1; /* mon runs from 0 - 11 */
    499   if (p2 == px) goto escape; 
    500   p1 = p2 + 1;
    501 
    502   tmp = strtod (p1, &p2);
    503   if (p2 == p1) goto escape;
    504   now.tm_mday = tmp;
    505   if (p2 == px) goto escape; 
    506   p1 = p2 + 1;
    507 
    508   tmp = strtod (p1, &p2);
    509   if (p2 == p1) goto escape;
    510   p1 = p2 + 1;
    511   now.tm_hour = tmp;
    512   if (p2 == px) goto escape; 
    513 
    514   tmp = strtod (p1, &p2);
    515   if (p2 == p1) goto escape;
    516   now.tm_min = tmp;
    517   if (p2 == px) goto escape; 
    518   p1 = p2 + 1;
    519 
    520   tmp = strtod (p1, &p2);
    521   if (p2 == p1) goto escape;
    522   now.tm_sec = tmp;
    523   if (p2 == px) goto escape; 
    524   p1 = p2 + 1;
    525 
    526  escape:
    527   jd = now.tm_mday - 32075 + (int)(1461*(1900 + now.tm_year + 4800 + (int)(((now.tm_mon+1)-14)/12))/4)
    528     + (int)(367*((now.tm_mon+1) - 2 - (int)(((now.tm_mon+1) - 14)/12)*12)/12)
    529     - (int)(3*(int)((1900 + now.tm_year + 4900 + (int)(((now.tm_mon+1) - 14)/12))/100)/4) - 0.5;
    530  
    531   second = (jd - 2440587.5)*86400 + 3600.0*now.tm_hour + now.tm_min*60.0 + now.tm_sec;
    532 
    533   return (second);
    534 }
    535 
    536218/* convert UNIX time to a value referenced to the TimeReference in the given unit */
    537219double TimeValue (time_t time, time_t TimeReference, int TimeFormat) {
     
    599281
    600282/* times may be in forms as:
    601    20040200450s (N seconds since 1970.0)
    602    2440900.232j (julian date)
    603    99/02/23,03:22:18 (date string)
    604    (separators may be anything except space, +, -)
    605    99:02:15:12:23:30
    606    99:02:15:12h23m30s
    607    */
     283 * 20040200450s (N seconds since 1970.0)
     284 * 2440900.232j (julian date)
     285 * 99/02/23,03:22:18 (date string)
     286 * (separators may be anything except space, +, -)
     287 * 99:02:15:12:23:30
     288 * 99:02:15:12h23m30s
     289 */
     290
Note: See TracChangeset for help on using the changeset viewer.