home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / fblnk224.zip / telescope.c < prev    next >
Text File  |  1999-01-28  |  15KB  |  550 lines

  1. /*  telescope.c  */
  2. /*  part of the fitsblink program  */
  3. /*  Control panel for a telescope and CCD camera */
  4. /*  Jure Skvarc                                   */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <string.h>
  9. #include <math.h>
  10.  
  11. #include <forms.h>
  12.  
  13. #include "formblink.h"
  14. #include "functs.h"
  15.  
  16. extern STATE state;
  17. extern BLINK_FRAME *frame;
  18. extern TELESCOPE telescope;
  19. extern OBSERVATORY observatory;
  20. extern int daylight;
  21. extern long int timezone;
  22.  
  23.  
  24. /*=========================================================*/
  25. /*  Execute a command by append string a to a tx command   */
  26. /*=========================================================*/
  27. void
  28. execute(char *a)
  29.  
  30. {
  31.   char *b;
  32.  
  33.   b = (char *) myalloc(sizeof(char) * (strlen(a) + 4), "execute", "b");
  34.   strcpy(b, "tx ");
  35.   strcat(b, a);
  36.   fl_exe_command(b, 1);
  37.   free(b);
  38. }
  39.  
  40. /* Calculate altitude and azimuth from the given ra and dec and location */
  41. void
  42. equtoaltaz(TELESCOPE *tel, OBSERVATORY *obs)
  43.  
  44. {
  45.   double sr, ha, fi;
  46.   double jd;
  47.   double sfi, cfi, del, cha;
  48.   struct tm *gmt;
  49.   time_t t;
  50.  
  51.   t = time(NULL);
  52.   gmt = gmtime(&t);   /* Current gmt  */
  53.   /*  calculate Julian day */
  54.   jd = julian_day(1900 + gmt->tm_year, gmt->tm_mon + 1, (double) gmt->tm_mday
  55.           + gmt->tm_hour / 24.0 + gmt->tm_min / 1440.0 + 
  56.           gmt->tm_sec / 86400.0);
  57.   /*  Calculate local hour angle for tel->ra  */
  58.   sr = fmod(sidereal(jd, obs->longitude), 360.0);
  59.   if (sr < 0) {
  60.     sr = (360 + sr);
  61.   }
  62.   ha = fmod(sr - tel->ra, 360.0);
  63.   if (ha < 0) {
  64.     ha = DEGRAD * (360 + ha);
  65.   }
  66.   else {
  67.     ha = DEGRAD * ha;
  68.   }
  69.   cha = cos(ha);
  70.   fi = obs->latitude * DEGRAD;
  71.   sfi = sin(fi); cfi = cos(fi);
  72.   del = tel->dec * DEGRAD;
  73.   tel->az = RADDEG * atan2(sin(ha), cha * sfi - tan(del) * cfi) + 180.0;
  74.   tel->alt = RADDEG * asin(sfi * sin(del) + cfi * cos(del) * cha);
  75.   tel->jd = jd;
  76.   tel->sr = sr;
  77. }
  78.  
  79. /* Calculate ra and dec from the given altitude and azimuth and location */
  80. void
  81. altaztoequ(TELESCOPE *tel, OBSERVATORY *obs)
  82.  
  83. {
  84.   double sr, ha, fi;
  85.   double jd;
  86.   double sfi, cfi, del, cha;
  87.   struct tm *gmt;
  88.   time_t t;
  89.  
  90.   t = time(NULL);
  91.   gmt = gmtime(&t);   /* Current gmt  */
  92.   /*  calculate Julian day */
  93.   jd = julian_day(1900 + gmt->tm_year, gmt->tm_mon + 1, (double) gmt->tm_mday + 
  94.           gmt->tm_hour / 24.0 + gmt->tm_min / 1440.0 + 
  95.           gmt->tm_sec / 86400.0);
  96.   sr = fmod(sidereal(jd, obs->longitude), 360.0);
  97.   if (sr < 0) {
  98.     sr = (360 + sr);
  99.   }
  100.   /*  Calculate local hour angle for tel->ra  */
  101.   ha = DEGRAD * fmod(sr - tel->ra, 360.0);
  102.   if (ha < 0) {
  103.     ha = DEGRAD * (360 + ha);
  104.   }
  105.   else {
  106.     ha = DEGRAD * ha;
  107.   }
  108.   cha = cos(DEGRAD * tel->az);
  109.   fi = obs->latitude * DEGRAD;
  110.   sfi = sin(fi); cfi = cos(fi);
  111.   del = tel->alt * DEGRAD;
  112.   tel->ra = RADDEG * atan2(sin(DEGRAD * tel->az), cha * sfi + tan(del) * cfi);
  113.   tel->ra = RADDEG * (ha - tel->ra);
  114.   if (tel->ra < 0) tel->ra += 360.0;
  115.   tel->dec = RADDEG * asin(sfi * sin(del) - cfi * cos(del) * cha);
  116.   tel->jd = jd;
  117.   tel->sr = sr;
  118. }
  119.  
  120. void
  121. update_position(XEvent *xev, void *t)
  122.  
  123. {
  124.   char a[20];
  125.   equtoaltaz(&telescope, &observatory);
  126.   fl_set_input(state.telescope->telaltW, format_dec(telescope.alt + 1E-9));
  127.   fl_set_input(state.telescope->telaziW, format_az(telescope.az + 1E-9));
  128.   sprintf(a, "%.5f", telescope.jd);
  129.   fl_set_input(state.telescope->teljulianW, a);
  130.   fl_set_input(state.telescope->telsiderealW, format_ra(telescope.sr + 1E-9));
  131.   state.timeout = fl_add_timeout(500, update_position, NULL);
  132. }
  133.  
  134. void
  135. teldecC(FL_OBJECT *obj, long val)
  136.  
  137. {
  138.   double dec;
  139.  
  140.   dec = parse_dec_c((char *)fl_get_input(obj));
  141.   fl_set_input(obj, format_dec(dec + 1E-9));
  142.   telescope.dec = dec;
  143. }
  144.  
  145. void
  146. telraC(FL_OBJECT *obj, long val)
  147.  
  148. {
  149.   double ra;
  150.  
  151.   ra = parse_ra_c((char *)fl_get_input(obj));
  152.   fl_set_input(obj, format_ra(ra + 1E-9));
  153.   telescope.ra = ra;
  154. }
  155.  
  156. void
  157. telaziC(FL_OBJECT *obj, long val)
  158.  
  159. {
  160. }
  161.  
  162. void
  163. telaltC(FL_OBJECT *obj, long val)
  164.  
  165. {
  166. }
  167.  
  168. void
  169. telbrowsecatC(FL_OBJECT *obj, long val)
  170.  
  171. {
  172.   char a[256], *p;
  173.   char ra[20], dec[20];
  174.  
  175.   strncpy(a, fl_get_browser_line(obj, fl_get_browser(obj)), 255);
  176.   /*  Extract entries no 4 and 5  */
  177.   p = a;
  178.   p = strtok(p, " \t");
  179.   p = strtok(NULL, " \t");
  180.   p = strtok(NULL, " \t");
  181.   p = strtok(NULL, " \t");
  182.   strcpy(ra, p);
  183.   p = strtok(NULL, " \t");
  184.   strcpy(dec, p);
  185.   telescope.ra = parse_ra_c(ra);
  186.   telescope.dec = parse_dec_c(dec);
  187.   fl_set_input(state.telescope->telraW, format_ra(telescope.ra + 1E-9));
  188.   fl_set_input(state.telescope->teldecW, format_dec(telescope.dec + 1E-9));
  189. }
  190.  
  191. void
  192. teltempC(FL_OBJECT *obj, long val)
  193.  
  194. {
  195.   double t;
  196.   char a[10];
  197.  
  198.   t = strtod(fl_get_input(obj), NULL);
  199.   if (fabs(t) > 270) {
  200.     t = 0;
  201.   }
  202.   sprintf(a, "%.2f", t);
  203.   telescope.temp = t;
  204.   fl_set_input(obj, a);
  205. }
  206.  
  207. void
  208. telreadtempC(FL_OBJECT *obj, long val)
  209.  
  210. {
  211.   FD_CMDLOG *cmdlog;
  212.   int m;
  213.   char *a, b[20];
  214.  
  215.   fl_deactivate_form(state.telescope->Telescope);
  216.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  217.   execute(telescope.gettemp);
  218.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  219.   fl_activate_form(state.telescope->Telescope);
  220.   cmdlog = fl_get_command_log_fdstruct();
  221.   m = fl_get_browser_maxline(cmdlog->browser) - 3;
  222.   a = strdup((char *) fl_get_browser_line(cmdlog->browser, m));
  223.   if (a == NULL) return;
  224.   sscanf(a, "ccd done temp=%lf\n", &telescope.temp);
  225.   sprintf(b, "%.2f", telescope.temp);
  226.   fl_set_input(state.telescope->teltempW, b);
  227.   free(a);
  228. }
  229.  
  230. void
  231. telsettempC(FL_OBJECT *obj, long val)
  232.  
  233. {
  234.   char a[270], *p, b[20];
  235.  
  236.   strcpy(a, telescope.settemp);
  237.   p = strstr(a, "TEMP");
  238.   if (p == NULL) {
  239.     fl_show_alert("Missing TEMP template in the set temperature  command!", "", "", 1);
  240.     return;
  241.   }
  242.   *p = '\0';
  243.   sprintf(b, "%.2f", telescope.temp);
  244.   strcat(a, b);
  245.   strcat(a, (char *) telescope.settemp + (p - a) + 4);
  246.   fl_deactivate_form(state.telescope->Telescope);
  247.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  248.   execute(a);
  249.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  250.   fl_activate_form(state.telescope->Telescope);
  251. }
  252.  
  253. /*========================*/
  254. /*   Set exposure time    */
  255. /*========================*/
  256. void
  257. telexposureC(FL_OBJECT *obj, long val)
  258.  
  259. {
  260.   double t;
  261.   char a[10];
  262.  
  263.   t = strtod(fl_get_input(obj), NULL);
  264.   if (t < 0) {
  265.     t = 0;
  266.   }
  267.   sprintf(a, "%.2f", t);
  268.   telescope.exptime = t;
  269.   fl_set_input(obj, a);
  270.  
  271. }
  272.  
  273. /*==========================*/
  274. /*  Make a CCD exposure     */
  275. /*==========================*/
  276. void
  277. telstartexposureC(FL_OBJECT *obj, long val)
  278.  
  279. {
  280.   char a[270], *p, b[270];
  281.   
  282.   strcpy(a, telescope.expose);
  283.   p = strstr(a, "FILE");
  284.   if (p == NULL) {
  285.     fl_show_alert("Missing FILE template in the expose command!", "", "", 1);
  286.     return;
  287.   }
  288.   *p = '\0';
  289.   if (telescope.filename == NULL || strlen(telescope.filename) == 0) {
  290.     fl_show_alert("Please enter file name!", "", "", 1);
  291.     return;
  292.   }
  293.   sprintf(b, "%s", telescope.filename);
  294.   strcat(a, b);
  295.   strcat(a, (char *) telescope.expose + (p - a) + 4);
  296.   p = strstr(p + 1, "TIME");
  297.   if (p == NULL) {
  298.     fl_show_alert("Missing TIME template in the expose command!", "", "", 1);
  299.     return;
  300.   }
  301.   *p = '\0';
  302.   sprintf(b, "%.3f %s ", telescope.exptime, (telescope.dark) ? "dark" : "");
  303.   strcat(a, b);
  304.   strcat(a, (char *) telescope.expose + 
  305.      (strstr(telescope.expose, "TIME") - telescope.expose) + 4);
  306.   fl_deactivate_form(state.telescope->Telescope);
  307.   /*  fl_show_object(state.telescope->exponW); */
  308.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  309.   execute(a);
  310.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  311.   /*fl_hide_object(state.telescope->exponW);*/
  312.   fl_activate_form(state.telescope->Telescope);
  313. }
  314.  
  315. /*================================*/
  316. /*  Center star in the CCD field  */
  317. /*================================*/
  318. void
  319. telcenterC(FL_OBJECT *obj, long val)
  320.  
  321. {
  322.   char a[270], *p, b[270];
  323.   
  324.   strcpy(a, telescope.center);
  325.   p = strstr(a, "TIME");
  326.   if (p == NULL) {
  327.     fl_show_alert("Missing TIME template in the center command!", "", "", 1);
  328.     return;
  329.   }
  330.   *p = '\0';
  331.   sprintf(b, "%.3f", telescope.exptime);
  332.   strcat(a, b);
  333.   strcat(a, (char *) telescope.center + 
  334.      (strstr(telescope.center, "TIME") - telescope.center) + 4);
  335.   fl_deactivate_form(state.telescope->Telescope);
  336.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  337.   execute(a);
  338.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  339.   fl_activate_form(state.telescope->Telescope);
  340. }
  341.  
  342.  
  343.  
  344. void
  345. telfileC(FL_OBJECT *obj, long val)
  346.  
  347. {
  348.   if (telescope.filename) free(telescope.filename);
  349.   telescope.filename = strdup((char *) fl_get_input(obj));
  350. }
  351.  
  352. void
  353. telfbrowseC(FL_OBJECT *obj, long val)
  354.  
  355. {
  356. }
  357.  
  358. void
  359. teldarkC(FL_OBJECT *obj, long val)
  360.  
  361. {
  362.   telescope.dark = fl_get_button(obj);
  363. }
  364.  
  365.  
  366. void
  367. telstartC(FL_OBJECT *obj, long val)
  368.  
  369. {
  370.   fl_deactivate_form(state.telescope->Telescope);
  371.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  372.   execute(telescope.start);
  373.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  374.   fl_activate_form(state.telescope->Telescope);
  375. }
  376.  
  377.  
  378. void
  379. telparkC(FL_OBJECT *obj, long val)
  380.  
  381. {
  382.   fl_deactivate_form(state.telescope->Telescope);
  383.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  384.   execute(telescope.park);
  385.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  386.   fl_activate_form(state.telescope->Telescope);
  387. }
  388.  
  389. /*===============================================*/
  390. /*  Read and display current telescope position  */
  391. /*===============================================*/
  392. void
  393. telreadposC(FL_OBJECT *obj, long val)
  394.  
  395. {
  396.   FD_CMDLOG *cmdlog;
  397.   int m;
  398.   char *a;
  399.   char ra[20], dec[20];
  400.  
  401.   fl_deactivate_form(state.telescope->Telescope);
  402.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  403.   execute(telescope.where);
  404.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  405.   fl_activate_form(state.telescope->Telescope);
  406.   cmdlog = fl_get_command_log_fdstruct();
  407.   m = fl_get_browser_maxline(cmdlog->browser) - 3;
  408.   a = strdup((char *) fl_get_browser_line(cmdlog->browser, m));
  409.   if (a == NULL) return;
  410.   sscanf(a, "where done ra=%s dec=%s\n", ra, dec);
  411.   telescope.ra = parse_ra_c(ra);
  412.   telescope.dec = parse_dec_c(dec);
  413.   fl_set_input(state.telescope->telraW, format_ra(telescope.ra + 1E-9));
  414.   fl_set_input(state.telescope->teldecW, format_dec(telescope.dec + 1E-9));
  415.   free(a);
  416. }
  417.  
  418.  
  419. /*===========================================*/
  420. /*  Point telescope to the desired position  */
  421. /*===========================================*/
  422. void
  423. telgoC(FL_OBJECT *obj, long val)
  424.  
  425. {
  426.   char a[270], *p;
  427.  
  428.   strcpy(a, telescope.point);
  429.   p = strstr(a, "RA");
  430.   if (p == NULL) {
  431.     fl_show_alert("Missing RA template in the point command!", "", "", 1);
  432.     return;
  433.   }
  434.   *p = '\0';
  435.   strcat(a, format_ra(telescope.ra + 1E-9));
  436.   strcat(a, (char *) telescope.point + (p - a) + 2);
  437.   p = strstr(p + 1, "DEC");
  438.   if (p == NULL) {
  439.     fl_show_alert("Missing DEC template in the point command!", "", "", 1);
  440.     return;
  441.   }
  442.   *p = '\0';
  443.   strcat(a, format_dec(telescope.dec + 1E-9));
  444.   strcat(a, (char *) telescope.point + 
  445.      (strstr(telescope.point, "DEC") - telescope.point) + 3);
  446.   fl_deactivate_form(state.telescope->Telescope);
  447.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  448.   execute(a);
  449.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  450.   fl_activate_form(state.telescope->Telescope);
  451. }
  452.  
  453. /*=======================================*/
  454. /*  Set telescope position to RA, DEC    */
  455. /*=======================================*/
  456. void
  457. telzeroC(FL_OBJECT *obj, long val)
  458.  
  459. {
  460.   char a[270], *p;
  461.  
  462.   strcpy(a, telescope.zero);
  463.   p = strstr(a, "RA");
  464.   if (p == NULL) {
  465.     fl_show_alert("Missing RA template in the zero command!", "", "", 1);
  466.     return;
  467.   }
  468.   *p = '\0';
  469.   strcat(a, format_ra(telescope.ra + 1E-9));
  470.   strcat(a, (char *) telescope.zero + (p - a) + 2);
  471.   p = strstr(p + 1, "DEC");
  472.   if (p == NULL) {
  473.     fl_show_alert("Missing DEC template in the zero command!", "", "", 1);
  474.     return;
  475.   }
  476.   *p = '\0';
  477.   strcat(a, format_dec(telescope.dec + 1E-9));
  478.   strcat(a, (char *) telescope.zero + 
  479.      (strstr(telescope.zero, "DEC") - telescope.zero) + 3);
  480.   fl_deactivate_form(state.telescope->Telescope);
  481.   fl_set_object_color(obj, FL_RED, FL_BUTTON_COL2);
  482.   execute(a);
  483.   fl_set_object_color(obj, FL_BUTTON_COL1, FL_BUTTON_COL2);
  484.   fl_activate_form(state.telescope->Telescope);
  485. }
  486.  
  487.  
  488. void
  489. telchoosecatC(FL_OBJECT *obj, long val)
  490.  
  491. {
  492.   const char *fn;
  493.   static char *curdir = "";
  494.   static char *pattern = "*.lst";
  495.   static char *name = "";
  496.  
  497.   fl_use_fselector(0);
  498.   fl_invalidate_fselector_cache();
  499.   fn = fl_show_fselector("Choose a catalog", curdir, pattern, name);
  500.   if (fn == NULL) return;
  501.   /*  Save the current directory  */
  502.   curdir = (char *) fl_get_directory();
  503.   /*  Save the current pattern */
  504.   pattern = (char *) fl_get_pattern();
  505.   name = (char *) fl_get_filename();
  506.   fl_load_browser(state.telescope->telbrowsecatW, fn);
  507. }
  508. /*  Show the telescope control window */
  509. void
  510. show_telescope_control(void)
  511.  
  512. {
  513.   int n;
  514.   time_t t;
  515.   char a[20];
  516.  
  517.   /*  The window in not created yet, do it now  */
  518.   if (state.telescope == NULL) {
  519.     state.telescope = create_form_Telescope();
  520.     t = time(NULL);
  521.     localtime(&t);
  522.     /* fl_set_clock_adjustment(state.telescope->telutW, timezone); */
  523.     fl_set_browser_fontsize(state.telescope->telbrowsecatW, 14);
  524.     fl_set_browser_fontstyle(state.telescope->telbrowsecatW, FL_FIXED_STYLE);
  525.     fl_deactivate_object(state.telescope->teljulianW);
  526.     fl_deactivate_object(state.telescope->telsiderealW);
  527.     fl_deactivate_object(state.telescope->telaltW);
  528.     fl_deactivate_object(state.telescope->telaziW);
  529.   }
  530.   fl_show_form(state.telescope->Telescope,  FL_FIX_SIZE, FL_FULLBORDER | FL_PLACE_FREE_CENTER, "Telescope control");
  531.   state.timeout = fl_add_timeout(500, update_position, NULL);
  532.   /*  Write current parameter values into the form */
  533.   n = state.control[state.control_num];
  534.  
  535.   fl_set_button(state.telescope->teldarkW, telescope.dark);
  536.   fl_set_input(state.telescope->telraW, format_ra(telescope.ra));
  537.   fl_set_input(state.telescope->teldecW, format_dec(telescope.dec));
  538.   sprintf(a, "%.2f\n", telescope.temp);
  539.   fl_set_input(state.telescope->teltempW, a);
  540.   sprintf(a, "%.3f\n", telescope.exptime);
  541.   fl_set_input(state.telescope->telexposureW, a);
  542.   equtoaltaz(&telescope, &observatory);
  543.   fl_set_input(state.telescope->telfileW, telescope.filename);
  544. }
  545.  
  546.  
  547.  
  548.  
  549.  
  550.