home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / fblnk224.zip / midexposure.c < prev    next >
C/C++ Source or Header  |  1999-01-28  |  6KB  |  234 lines

  1. /*  midexposure.c  */
  2. /*  part of the fitsblink program  */
  3. /*  routines which calculate the time and date of mid-exposure */
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #include "functs.h"
  8.  
  9. int
  10. days_per_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  11.  
  12.  
  13. /*  Increment date for one day  */
  14. /*==============================*/
  15. void
  16. incr_date(int *year, int *month, int *day)
  17.  
  18. {
  19.   int leap = 0;
  20.  
  21.   if (*month == 2 && *year % 4 == 0 && (*year % 100 != 0 || *year % 400 == 0)) {
  22.     leap = 1;
  23.   }
  24.   (*day)++;
  25.   if (*day > days_per_month[*month] + leap) {
  26.     *day = 1;  /* First day of month  */
  27.     (*month)++;  /* Next month  */
  28.     if (*month == 13) {
  29.       *month = 1;
  30.       (*year)++;
  31.     }
  32.   }
  33. }
  34.  
  35. /*  convert time in decimal hours to hour, minute second format  */
  36. /*===============================================================*/
  37. void
  38. dechour_to_hms(double dechour, int *h, int *m, float *s)
  39.  
  40. {
  41.   *h = (int) dechour;
  42.   dechour -= *h;
  43.   dechour *= 60.0;
  44.   dechour += 1e-9;
  45.   *m = (int) dechour;
  46.   dechour -= *m;
  47.   *s = dechour * 60.0;
  48. }
  49.  
  50.  
  51. /*  Parse date which is either in format dd/mm/yy or dd/mm/yyyy or yyyy-mm-dd */
  52. void
  53. parse_date(char *date, int *year, int *month, int *day)
  54.  
  55. {
  56.   int i;
  57.   char sep;
  58.   char *p;
  59.   char *tdate;
  60.  
  61.   tdate = strdup(date);
  62.   /*  Check for separator  */
  63.   i = 0;
  64.   p = tdate;
  65.   sep = 0;
  66.   while (p[i] != '\0') {
  67.     if (p[i] == '/') {
  68.       sep = '/';
  69.       break;
  70.     }
  71.     else if (p[i] == '-') {
  72.       sep = '-';
  73.       break;
  74.     }
  75.     i++;
  76.   }
  77.   p = tdate;
  78.   /*  Skip ' sign if present */
  79.   if (*p == '\'') p++;
  80.   switch (sep) {
  81.   case '/':
  82.     p = strtok(p, "/");
  83.     *day = strtol(p, NULL, 10);
  84.     p = strtok(NULL, "/");
  85.     *month = strtol(p, NULL, 10);
  86.     p = strtok(NULL, "/");
  87.     *year = strtol(p, NULL, 10);
  88.     if (*year < 50) *year += 2000;
  89.     else if (*year < 100) *year += 1900;
  90.     break;
  91.   case '-':
  92.     p = strtok(p, "-");
  93.     *year = strtol(p, NULL, 10);
  94.     p = strtok(NULL, "-");
  95.     *month = strtol(p, NULL, 10);
  96.     p = strtok(NULL, "-");
  97.     *day = strtol(p, NULL, 10);
  98.     if (*day > 31) {
  99.       int t = *day;
  100.       *day = *year;
  101.       *year = t;
  102.     }
  103.     break;
  104.   default:
  105.     *year = 0;
  106.     *month = 0;
  107.     *day = 0;
  108.   }
  109.   free(tdate);
  110. }
  111.  
  112.  
  113. /*  Parse time writen in format hh:mm:ss.sss */
  114. void
  115. parse_time(char *time, int *hour, int *minute, float *second)
  116.  
  117. {
  118.   char *p, *ttime;
  119.  
  120.   ttime = strdup(time);
  121.   p = ttime;
  122.   /*  Skip ' sign if present */
  123.   if (*p == '\'') p++;
  124.   p = strtok(p, ":");
  125.   *hour = strtol(p, NULL, 10);
  126.   p = strtok(NULL, ":.");
  127.   *minute = strtol(p, NULL, 10);
  128.   p = strtok(NULL, "':");
  129.   *second = strtod(p, NULL);
  130.   free(ttime);
  131. }
  132.  
  133. /*  Calculate the time of midexposure  */
  134. /*=====================================*/
  135. void
  136. calculate_midexposure(FITSFILE *f, int *year, int *month, int *day,
  137.               int *hour, int *minute, float *second)
  138.  
  139. {
  140.   int h, m, h2, m2;
  141.   float s, s2;
  142.   double d1 = 0.0, d2 = 0.0;
  143.  
  144.   /*  If the DATE-OBS keyword is present, set the date  */
  145.   if (f->is_date_obs) {
  146.     parse_date(f->date_obs, year, month, day);
  147.   }
  148.   else {
  149.     /*  Set date to a default  */
  150.     *year = 2000;
  151.     *month = 1;
  152.     *day = 1;
  153.   }
  154.  
  155.   /*  For the time od midexposure, first try the easy way: check if */
  156.   /*  UT-CENT keyword is present  */
  157.   if (f->is_ut_cent) {
  158.     parse_time(f->ut_cent, hour, minute, second);
  159.     /* Now, check if a correction of date is needed  */
  160.     if (f->is_ut_start) {
  161.       /*  Read start of exposure  */
  162.       parse_time(f->ut_start, &h, &m, &s);
  163.       if (h > *hour) {
  164.     /* exposure is over midnight, adjust the date */
  165.     incr_date(year, month, day);
  166.       }
  167.     }
  168.     else {
  169.       if (f->is_exposure) {  /*  This situation is already weird:  */
  170.                 /*  there is UT-CENT but no UT-START  */
  171.     /*  Nevertheless, we happen to have the EXPOSURE keyword  */
  172.     /*  Convert UT-CENT to seconds and subtract half of exposure  */
  173.     d1 = (double) *hour + (double) *minute / 60.0 + *second / 3600.0 - 
  174.       f->exposure / 7200.0;
  175.     if (d1 > 0) {  /* Increment date for one day  */
  176.       incr_date(year, month, day);
  177.     }
  178.       }
  179.     }
  180.   }
  181.   else if (f->is_ut_start) {
  182.     /*  There is no UT-CENT keyword, so we have to calculate it by our own  */
  183.     /*  First, check for UT_START  */
  184.     parse_time(f->ut_start, &h, &m, &s);
  185.     d1 = (double) h + (double) m / 60.0 + s / 3600.0;
  186.     if (f->is_exposure) {
  187.       /*  Add half of the exposure time to the start of exposure  */
  188.       d1 += f->exposure / 7200.0;
  189.       if (d1 > 24.0) {
  190.     incr_date(year, month, day);
  191.     d1 -= 24;
  192.       }
  193.       dechour_to_hms(d1, hour, minute, second);
  194.     } 
  195.     else if (f->is_ut_end) {
  196.       /* There is no EXPOSURE keyword but there is UT_END  */
  197.       parse_time(f->ut_end, &h2, &m2, &s2);
  198.       d2 = (double) h2 + (double) m2 / 60.0 + s2 / 3600.0;
  199.       printf("d1=%f, d2=%f\n", d1, d2);
  200.       d1 = 0.5 * (d1 + d2);
  201.       if (d1 > 24.0) {
  202.     incr_date(year, month, day);
  203.     d1 -= 24;
  204.       }
  205.       printf("d1=%f, d2=%f\n", d1, d2);
  206.       dechour_to_hms(d1, hour, minute, second);
  207.     }
  208.   }
  209.   else  if (f->is_time_beg) {     /*  Last hope: check for TIME-BEG */
  210.     parse_time(f->time_beg, &h, &m, &s);
  211.     d1 = (double) h + (double) m / 60.0 + s / 3600.0;
  212.     if (f->is_exposure) {
  213.       /*  Add half of the exposure time to the start of exposure  */
  214.       d1 += f->exposure / 7200.0;
  215.       if (d1 > 24.0) {
  216.     incr_date(year, month, day);
  217.     d1 -= 24;
  218.       }
  219.       dechour_to_hms(d1, hour, minute, second);
  220.     } 
  221.     else if (f->is_time_end) {
  222.       /* There is no EXPOSURE keyword but there is TIME-END  */
  223.       parse_time(f->time_end, &h2, &m2, &s2);
  224.       d2 = (double) h2 + (double) m2 / 60.0 + s2 / 3600.0;
  225.       d1 = 0.5 * (d1 + d2);
  226.       if (d1 > 24.0) {
  227.     incr_date(year, month, day);
  228.     d1 -= 24;
  229.       }
  230.       dechour_to_hms(d1, hour, minute, second);
  231.     }
  232.   }
  233. }
  234.