home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2006 September / SAN CD 9-2006 CD-ROM 16.iso / pc / Software / Network Telescope Control / NTC-Setup.Exe / Source / ntc_server_object.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2006-03-24  |  12.4 KB  |  715 lines

  1. unit ntc_server_object;
  2. {
  3.     Copyright (C) 2004 - 2006 Andrew Sprott
  4.  
  5.     http://astronomy.crysania.co.uk
  6.     astro@trefach.co.uk
  7.  
  8.     This program is free software; you can redistribute it and/or
  9.     modify it under the terms of the GNU General Public License
  10.     as published by the Free Software Foundation; either version 2
  11.     of the License, or (at your option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. }
  22.  
  23. interface
  24.  
  25. uses
  26.     SysUtils,
  27.  
  28.     ntc_server_observer;
  29.  
  30. const
  31.     no_value=999;
  32.  
  33. type
  34.     hms=record
  35.         hours,
  36.         minutes,
  37.         seconds,
  38.         tens:integer;
  39.         high,
  40.         low:string;
  41.     end;
  42.  
  43.     dms=record
  44.         degrees,
  45.         minutes,
  46.         seconds,
  47.         tens:integer;
  48.         high,
  49.         low:string;
  50.     end;
  51.  
  52.     ams=record
  53.         degrees,
  54.         minutes,
  55.         seconds,
  56.         tens:integer;
  57.         high,
  58.         low:string;
  59.     end;
  60.  
  61.     tscope_object=class(tobject)
  62.  
  63.         constructor create;
  64.  
  65.     private
  66.         { private declarations }
  67.     public
  68.         { Public declarations }
  69.         name:string;
  70.         magnitude,
  71.         magnification:integer;
  72.         { parameters to control scope }
  73.         ra,
  74.         dec:double;
  75.         az,
  76.         alt:double;
  77.         speed,
  78.         dir:string[16];
  79.         info:string;
  80.         moving:boolean;
  81.         { debug }
  82.         input:string;
  83.         { end debug }
  84.  
  85.         { binary to text }
  86.         procedure right_ascension_str(
  87.                     h,m,s:integer;
  88.             var ra_rec:hms); overload;
  89.  
  90.         procedure right_ascension_str(
  91.                     f:double;
  92.             var ra_rec:hms); overload;
  93.  
  94.         procedure declination_str(
  95.                     f:double;
  96.                     d,m,s:integer;
  97.             var dec_rec:dms); overload;
  98.  
  99.         procedure declination_str(
  100.                     f:double;
  101.             var dec_rec:dms); overload;
  102.  
  103.         procedure azimuth_str(
  104.                     f:double;
  105.             var az_rec:dms); overload;
  106.  
  107.         procedure altitude_str(
  108.                     f:double;
  109.             var alt_rec:ams); overload;
  110.  
  111.         procedure right_ascension_str(
  112.             var ra_rec:hms); overload;
  113.  
  114.         procedure declination_str(
  115.             var dec_rec:dms); overload;
  116.  
  117.         procedure azimuth_str(
  118.             var az_rec:dms); overload;
  119.  
  120.         procedure altitude_str(
  121.             var alt_rec:ams); overload;
  122.  
  123.         { text to binary }
  124.         function str_right_ascension(
  125.             var f:double;
  126.             var ra_rec:hms)
  127.                     :boolean; overload;
  128.  
  129.         function str_declination(
  130.             var f:double;
  131.             var dec_rec:dms)
  132.                     :boolean; overload;
  133.  
  134.         function str_azimuth(
  135.             var f:double;
  136.             var az_rec:dms)
  137.                     :boolean; overload;
  138.  
  139.         function str_altitude(
  140.             var f:double;
  141.             var alt_rec:ams)
  142.                     :boolean; overload;
  143.  
  144.         function str_right_ascension(
  145.             var ra_rec:hms)
  146.                     :boolean; overload;
  147.  
  148.         function str_declination(
  149.             var dec_rec:dms)
  150.                     :boolean; overload;
  151.  
  152.         function str_azimuth(
  153.             var az_rec:dms)
  154.                     :boolean; overload;
  155.  
  156.         function str_altitude(
  157.             var alt_rec:ams)
  158.                     :boolean; overload;
  159.  
  160.     end;
  161.  
  162. var
  163.     current_object:tscope_object;
  164.  
  165. implementation
  166.  
  167. uses
  168.     ntc_server_comms,
  169.     ntc_server_config,
  170.     ntc_server_form;
  171.  
  172.     { -------------
  173.         form handling
  174.         ------------- }
  175.  
  176. constructor tscope_object.create;
  177. begin
  178.     name:='';
  179.     ra:=4.7;
  180.     dec:=51.5;
  181.     alt:=51.38;
  182.     az:=4.08;
  183.     magnitude:=1;
  184.     magnification:=1;
  185.     moving:=false;
  186. end;
  187.  
  188. procedure sinc(
  189.     var s:string;
  190.             t:string);
  191. begin
  192.     s:=s+t;
  193. end;
  194.  
  195.     { --------------
  196.         binary to text
  197.         -------------- }
  198.  
  199. { HH:MM:SS
  200.     HH:MM.T }
  201. procedure tscope_object.right_ascension_str(
  202.             h,m,s:integer;
  203.     var ra_rec:hms);
  204. begin
  205.     with ra_rec do
  206.         begin
  207.             hours:=h;
  208.             seconds:=s;
  209.             minutes:=m;
  210.             tens:=seconds div 10;
  211.             high:='';
  212.             if hours<10 then
  213.                 high:='0';
  214.             sinc(high,inttostr(hours)+':');
  215.             if minutes<10 then
  216.                 sinc(high,'0');
  217.             sinc(high,inttostr(minutes));
  218.             if not scope_config.seconds_bug then
  219.                 begin
  220.                     sinc(high,':');
  221.                     low:=high;
  222.                     if seconds<10 then
  223.                         sinc(high,'0');
  224.                     sinc(high,inttostr(seconds));
  225.                     sinc(low,'.'+inttostr(seconds div 10));
  226.                 end
  227.             else
  228.                 low:=high;
  229.         end;
  230. end;
  231.  
  232. { HH:MM:SS
  233.     HH:MM.T }
  234. procedure tscope_object.right_ascension_str(
  235.             f:double;
  236.     var ra_rec:hms);
  237. var
  238.     m:double;
  239. begin
  240.     ra:=f;
  241.     with ra_rec do
  242.         begin
  243.             hours:=trunc(f);
  244.             m:=frac(f)*60;
  245.             seconds:=trunc(frac(m)*60);
  246.             minutes:=trunc(m);
  247.             right_ascension_str(hours,minutes,seconds,ra_rec);
  248.         end;
  249. end;
  250.  
  251. { sDDflMM:SS
  252.     sDDflMM }
  253. procedure tscope_object.declination_str(
  254.             f:double;
  255.             d,m,s:integer;
  256.     var dec_rec:dms);
  257. begin
  258.     with dec_rec do
  259.         begin
  260.             degrees:=d;
  261.             seconds:=s;
  262.             minutes:=m;
  263.             tens:=seconds div 10;
  264.             if f<0 then
  265.                 high:='-'
  266.             else
  267.                 high:='+';
  268.             if degrees<10 then
  269.                 sinc(high,'0');
  270.             sinc(high,inttostr(degrees)+'fl');
  271.             if minutes<10 then
  272.                 sinc(high,'0');
  273.             sinc(high,inttostr(minutes));
  274.             low:=high;
  275.             if not scope_config.seconds_bug then
  276.                 begin
  277.                     sinc(high,':');
  278.                     if seconds<10 then
  279.                         sinc(high,'0');
  280.                     sinc(high,inttostr(seconds));
  281.                 end;
  282.         end;
  283. end;
  284.  
  285. { sDDflMM:SS
  286.     sDDflMM }
  287. procedure tscope_object.declination_str(
  288.             f:double;
  289.     var dec_rec:dms);
  290. var
  291.     m:double;
  292. begin
  293.     dec:=f;
  294.     with dec_rec do
  295.         begin
  296.             degrees:=trunc(f);
  297.             m:=abs(frac(f)*60);
  298.             seconds:=trunc(frac(m)*60);
  299.             minutes:=trunc(m);
  300.             declination_str(f,degrees,minutes,seconds,dec_rec);
  301.         end;
  302. end;
  303.  
  304. { DDDflMM:SS
  305.     DDDflMM.T }
  306. procedure tscope_object.azimuth_str(
  307.             f:double;
  308.     var az_rec:dms);
  309. var
  310.     m:double;
  311. begin
  312.     az:=f;
  313.     with az_rec do
  314.         begin
  315.             degrees:=trunc(f);
  316.             m:=frac(f)*60;
  317.             if frac(f)<m/60 then
  318.                 m:=m+1;
  319.             seconds:=trunc(frac(m)*60);
  320.             minutes:=trunc(m);
  321.             tens:=seconds div 10;
  322.             if degrees<10 then
  323.                 high:='00'
  324.             else if degrees<100 then
  325.                 high:='0'
  326.             else
  327.                 high:='';
  328.             sinc(high,inttostr(degrees)+'fl');
  329.             if minutes<10 then
  330.                 sinc(high,'0');
  331.             sinc(high,inttostr(minutes));
  332.             if not scope_config.seconds_bug then
  333.                 begin
  334.                     low:=high+'.'+inttostr(tens);
  335.                     sinc(high,':');
  336.                     if seconds<10 then
  337.                         sinc(high,'0');
  338.                     sinc(high,inttostr(seconds));
  339.                 end
  340.             else
  341.                 low:=high;
  342.         end;
  343. end;
  344.  
  345. { sDDflMM:SS
  346.     sDDflMM }
  347. procedure tscope_object.altitude_str(
  348.             f:double;
  349.     var alt_rec:ams);
  350. var
  351.     t_dec:double;
  352. begin
  353.     t_dec:=dec;
  354.     alt:=f;
  355.     declination_str(f,dms(alt_rec));
  356.     dec:=t_dec;
  357. end;
  358.  
  359. procedure tscope_object.right_ascension_str(
  360.     var ra_rec:hms);
  361. begin
  362.     right_ascension_str(ra,ra_rec);
  363. end;
  364.  
  365. procedure tscope_object.declination_str(
  366.     var dec_rec:dms);
  367. begin
  368.     declination_str(dec,dec_rec);
  369. end;
  370.  
  371. procedure tscope_object.azimuth_str(
  372.     var az_rec:dms);
  373. begin
  374.     azimuth_str(az,az_rec);
  375. end;
  376.  
  377. procedure tscope_object.altitude_str(
  378.     var alt_rec:ams);
  379. begin
  380.     altitude_str(alt,alt_rec);
  381. end;
  382.  
  383.     { --------------
  384.         text to binary
  385.         -------------- }
  386.  
  387. { HH:MM:SS
  388.     HH:MM.T }
  389. function tscope_object.str_right_ascension(
  390.     var f:double;
  391.     var ra_rec:hms)
  392.             :boolean;
  393. var
  394.     s,r:string;
  395.     i:integer;
  396.     h,n:boolean;
  397. begin
  398.     result:=false;
  399.     with ra_rec do
  400.         begin
  401.             if high<>'' then
  402.                 r:=high
  403.             else
  404.                 r:=low;
  405.             if r='#' then
  406.                 exit;
  407.             input:=r;
  408.             while r[length(r)]='#' do
  409.                 r:=copy(r,1,length(r)-1);
  410.             i:=pos(':',r);
  411.             if i=0 then
  412.                 exit;
  413.             s:=copy(r,1,i-1);
  414.             hours:=strtointdef(s,no_value);
  415.             if hours=no_value then
  416.                 exit;
  417.             r:=copy(r,i+1,length(r)-i);
  418.             i:=pos(':',r);
  419.             if i=0 then
  420.                 begin
  421.                     i:=pos('.',r);
  422.                     h:=false;
  423.                     if i=0 then
  424.                         begin
  425.                             i:=length(r)+1;
  426.                             n:=true;
  427.                         end
  428.                     else
  429.                         begin
  430.                             low:=high;
  431.                             n:=false;
  432.                         end;
  433.                 end
  434.             else
  435.                 begin
  436.                     low:='';
  437.                     h:=true;
  438.                     n:=false;
  439.                 end;
  440.             s:=copy(r,1,i-1);
  441.             minutes:=strtointdef(s,no_value);
  442.             if minutes=no_value then
  443.                 exit;
  444.             if n then
  445.                 begin
  446.                     tens:=0;
  447.                     seconds:=0;
  448.                 end
  449.             else
  450.                 begin
  451.                     r:=copy(r,i+1,length(r)-i);
  452.                     if not h then
  453.                         begin
  454.                             tens:=strtointdef(r,no_value);
  455.                             if tens=no_value then
  456.                                 exit;
  457.                             seconds:=tens*10;
  458.                         end
  459.                     else
  460.                         begin
  461.                             seconds:=strtointdef(r,no_value);
  462.                             if seconds=no_value then
  463.                                 exit;
  464.                             tens:=seconds div 10;
  465.                         end;
  466.                 end;
  467.             f:=seconds/60;
  468.             f:=(f+minutes)/60;
  469.             f:=f+hours;
  470.         end;
  471.     ra:=f;
  472.     result:=true;
  473. end;
  474.  
  475. { sDDflMM:SS
  476.     sDDflMM }
  477. function tscope_object.str_declination(
  478.     var f:double;
  479.     var dec_rec:dms)
  480.             :boolean;
  481. var
  482.     s:string;
  483.     i,j:integer;
  484.     r:string;
  485. begin
  486.     result:=false;
  487.     with dec_rec do
  488.         begin
  489.             if high<>'' then
  490.                 r:=high
  491.             else
  492.                 r:=low;
  493.             if r='#' then
  494.                 exit;
  495.             input:=r;
  496.             while r[length(r)]='#' do
  497.                 r:=copy(r,1,length(r)-1);
  498.             i:=pos('fl',r);
  499.             if i=0 then
  500.                 exit;
  501.             j:=pos(':',r);
  502.             if j>0 then
  503.                 low:=''
  504.             else
  505.                 low:=high;
  506.             s:=copy(r,1,i-1);
  507.             degrees:=strtointdef(s,no_value);
  508.             if degrees=no_value then
  509.                 exit;
  510.             if j>0 then
  511.                 s:=copy(r,i+1,j-i-1)
  512.             else
  513.                 s:=copy(r,i+1,length(r)-i);
  514.             minutes:=strtointdef(s,no_value);
  515.             if minutes=no_value then
  516.                 exit;
  517.             if j>0 then
  518.                 begin
  519.                     s:=copy(r,j+1,length(r)-j);
  520.                     seconds:=strtointdef(s,no_value);
  521.                     if seconds=no_value then
  522.                         exit;
  523.                     tens:=seconds div 10;
  524.                 end
  525.             else
  526.                 begin
  527.                     tens:=0;
  528.                     seconds:=0;
  529.                 end;
  530.             f:=seconds/60;
  531.             f:=(f+minutes)/60;
  532.             f:=f+degrees;
  533.         end;
  534.     dec:=f;
  535.     result:=true;
  536. end;
  537.  
  538. { DDDMM:SS
  539.     DDDflMM.T }
  540. function tscope_object.str_azimuth(
  541.     var f:double;
  542.     var az_rec:dms)
  543.             :boolean;
  544. var
  545.     s,r:string;
  546.     t,u:char;
  547.     i,j:integer;
  548.     h,n:boolean;
  549.  
  550.     procedure bug;
  551.     begin
  552.         if copy(s,1,1)='0' then
  553.             s:=copy(s,2,length(s)-1);
  554.         if length(s)>1 then
  555.             begin
  556.                 t:=s[1];
  557.                 u:=s[2];
  558.             end
  559.         else if length(s)>0 then
  560.             begin
  561.                 t:='0';
  562.                 u:=s[1];
  563.             end
  564.         else
  565.             begin
  566.                 az_rec.degrees:=0;
  567.                 exit;
  568.             end;
  569.         j:=(ord(t)-ord('0'))*10;
  570.         az_rec.degrees:=ord(u)-ord('0')+j;
  571.     end;
  572.  
  573. begin
  574.     result:=false;
  575.     with az_rec do
  576.         begin
  577.             if high<>'' then
  578.                 r:=high
  579.             else
  580.                 r:=low;
  581.             if r='#' then
  582.                 exit;
  583.             input:=r;
  584.             while r[length(r)]='#' do
  585.                 r:=copy(r,1,length(r)-1);
  586.             if pos('.',high)>0 then
  587.                 low:=high
  588.             else
  589.                 low:='';
  590.             i:=pos('fl',r);
  591.             if i=0 then
  592.                 exit;
  593.             s:=copy(r,1,i-1);
  594.             with scope_config do
  595.                  if (model_list.itemindex+1=autostar_type) and
  596.                         (century_bug) then
  597.                 begin
  598.                     if (i>2) and (strtointdef(s,no_value)<>no_value) then
  599.                         degrees:=strtoint(s)
  600.                     else bug;
  601.                 end
  602.             else if (strtointdef(s,no_value)=no_value) then
  603.                 bug
  604.             else
  605.                 begin
  606.                     degrees:=strtointdef(s,no_value);
  607.                     if degrees=no_value then
  608.                         exit;
  609.                 end;
  610.             r:=copy(r,i+1,length(r)-i);
  611.             i:=pos(':',r);
  612.             if i=0 then
  613.                 begin
  614.                     i:=pos('.',r);
  615.                     h:=false;
  616.                     if i=0 then
  617.                         begin
  618.                             i:=length(r)+1;
  619.                             n:=true;
  620.                         end
  621.                     else
  622.                         begin
  623.                             low:=high;
  624.                             n:=false;
  625.                         end;
  626.                 end
  627.             else
  628.                 begin
  629.                     low:='';
  630.                     h:=true;
  631.                     n:=false;
  632.                 end;
  633.             s:=copy(r,1,i-1);
  634.             minutes:=strtointdef(s,no_value);
  635.             if minutes=no_value then
  636.                 exit;
  637.             if n then
  638.                 begin
  639.                     tens:=0;
  640.                     seconds:=0;
  641.                 end
  642.             else
  643.                 begin
  644.                     r:=copy(r,i+1,length(r)-i);
  645.                     if not h then
  646.                         begin
  647.                             tens:=strtointdef(r,no_value);
  648.                             if tens=no_value then
  649.                                 exit;
  650.                             seconds:=tens*10;
  651.                         end
  652.                     else
  653.                         begin
  654.                             seconds:=strtointdef(r,no_value);
  655.                             if seconds=no_value then
  656.                                 exit;
  657.                             tens:=seconds div 10;
  658.                         end;
  659.                 end;
  660.             f:=seconds/60;
  661.             f:=(f+minutes)/60;
  662.             f:=f+degrees;
  663.         end;
  664.     az:=f;
  665.     result:=true;
  666. end;
  667.  
  668. { sDDflMM:SS
  669.     sDDflMM }
  670. function tscope_object.str_altitude(
  671.     var f:double;
  672.     var alt_rec:ams)
  673.             :boolean;
  674. var
  675.     t_dec:double;
  676. begin
  677.     t_dec:=dec;
  678.     result:=str_declination(f,dms(alt_rec));
  679.     alt:=f;
  680.     dec:=t_dec;
  681. end;
  682.  
  683. function tscope_object.str_right_ascension(
  684.     var ra_rec:hms)
  685.             :boolean;
  686. begin
  687.     result:=str_right_ascension(ra,ra_rec);
  688. end;
  689.  
  690. function tscope_object.str_declination(
  691.     var dec_rec:dms)
  692.             :boolean;
  693. begin
  694.     result:=str_declination(dec,dec_rec);
  695. end;
  696.  
  697. function tscope_object.str_azimuth(
  698.     var az_rec:dms)
  699.             :boolean;
  700. begin
  701.     result:=str_azimuth(az,az_rec);
  702. end;
  703.  
  704. function tscope_object.str_altitude(
  705.     var alt_rec:ams)
  706.             :boolean;
  707. begin
  708.     result:=str_altitude(alt,alt_rec);
  709. end;
  710.  
  711. begin
  712.     current_object:=nil;
  713.     current_object:=tscope_object.create;
  714. end.
  715.