public static AltAzm RaDec2AltAzm(Coordinates coord, LatLon location, DateTime time, double elevation)
        {
            var utils = new ASCOM.Astrometry.AstroUtils.AstroUtils();
            var MJDdate = utils.CalendarToMJD(time.Day, time.Month, time.Year);
            MJDdate += time.TimeOfDay.TotalDays;

            var tfm = new ASCOM.Astrometry.Transform.Transform();
            tfm.JulianDateTT = MJDdate;
            tfm.SiteElevation = elevation * 1000;
            tfm.SiteLatitude = location.Lat;
            tfm.SiteLongitude = location.Lon;
            tfm.SiteTemperature = 0;
            tfm.SetJ2000(coord.Ra, coord.Dec);
            tfm.Refresh();

            var res = new AltAzm(tfm.ElevationTopocentric, tfm.AzimuthTopocentric);
            return res;
        }
        public static Coordinates AltAzm2RaDec(AltAzm altAzm, LatLon location, DateTime time, double elevation)
        {
            var utils = new ASCOM.Astrometry.AstroUtils.AstroUtils();
            var MJDdate = utils.CalendarToMJD(time.Day, time.Month, time.Year);
            MJDdate += time.TimeOfDay.TotalDays;

            var tfm = new ASCOM.Astrometry.Transform.Transform();
            tfm.JulianDateTT = MJDdate;
            tfm.SiteElevation = elevation * 1000;
            tfm.SiteLatitude = location.Lat;
            tfm.SiteLongitude = location.Lon;
            tfm.SiteTemperature = 0;
            tfm.SetAzimuthElevation(altAzm.Azm, altAzm.Alt);
            tfm.Refresh();
            var res = new Coordinates(tfm.RAJ2000, tfm.DecJ2000);
            return res;
        }
 public static double NowLST(LatLon location)
 {
     var nov = new Astrometry.NOVAS.NOVAS31();
     var ast = new Astrometry.AstroUtils.AstroUtils();
     var currJD = ast.JulianDateUT1(0);
     double lstNow = 0;
     var res = nov.SiderealTime(
         currJD, 0d, 0, GstType.GreenwichApparentSiderealTime, Method.EquinoxBased, Accuracy.Full, ref lstNow);
     if (res != 0) throw new InvalidValueException("Error getting Local Apparent Sidereal time");
     return lstNow;
 }
        public byte[] exchange(byte[] input)
        {
            var ans = new List<byte>();
            if(input == null || !input.Any()) throw new Exception("Empty transfer");

            var t = input[0];
            switch (t)
            {
                case (byte)'V':
                    return this.makeVersion();
                case (byte)'Z':
                    return "12AB,4000#".ToBytes();
                case (byte)'z':
                    return "12AB0500,40000500#".ToBytes();
                case (byte)'E':
                    return "34AB,12CE#".ToBytes();
                case (byte)'e':
                    return "34AB0500,12CE0500#".ToBytes();
                case (byte)'S': //Sync
                case (byte)'s':
                case (byte)'B': //Slew AltAzm
                case (byte)'b':
                case (byte)'R': //Slew RaDec
                case (byte)'r':
                    return "#".ToBytes();
                case (byte)'t':
                    return new byte[]{(byte)_tracking, (byte)(char)GeneralCommands.TERMINATOR};
                case (byte)'T':
                    _tracking = (TrackingMode)input[1];
                    return new byte[] { (byte)(char)GeneralCommands.TERMINATOR };
                case (byte)'P':
                    return this.SendCommand(input[1], (DeviceID)input[2], (DeviceCommands)input[3], input.Skip(4).ToArray());
                case (byte)'w':
                    {
                        var lat = Location.Lat.ToDMS();
                        var lon = Location.Lon.ToDMS();

                        return new byte[]
                                   {
                                       (byte)lat.D, (byte)lat.M, (byte)lat.S, (byte)(lat.Sign > 0 ? 0 : 1), (byte)lon.D,
                                       (byte)lon.M, (byte)lon.S, (byte)(lon.Sign > 0 ? 0 : 1), (byte)(char)GeneralCommands.TERMINATOR
                                   };
                    }
                case (byte)'W':
                    {
                        var lat = new DMS(input[1], input[2], input[3], input[4] > 0 ? -1 : 1);
                        var lon = new DMS(input[5], input[6], input[7], input[8] > 0 ? -1 : 1);
                        Location = new LatLon(lat.Deg, lon.Deg);
                        return "#".ToBytes();
                    }
                case (byte)'h':
                    {
                        var tm = DateTime.Now;
                        var tz = (int)(TimeZone.CurrentTimeZone.GetUtcOffset(tm).TotalHours + 0.5);
                        var dlst = TimeZone.CurrentTimeZone.IsDaylightSavingTime(DateTime.Now) ? 1 : 0;
                        return new byte[]
                                   {
                                       (byte)tm.Hour, (byte)tm.Minute, (byte)tm.Second, (byte)tm.Month, (byte)tm.Day,
                                       (byte)(tm.Year - 2000), (byte)tz, (byte)dlst, (byte)(char)GeneralCommands.TERMINATOR
                                   };
                    }
                case (byte)'H':
                    return "#".ToBytes();
                default:
                    return "#".ToBytes();
            }

            ans.Add((byte)(char)GeneralCommands.TERMINATOR);
            return ans.ToArray();
        }