Beispiel #1
0
        public static List <SkybotObject> GetObjects(EquatorialPoint Location, double Radius, DateTime Time)
        {
            /* Prepares the query */
            string url    = GenerateSCSUrl(Location, Radius, Time);
            string result = string.Empty;

            /* Tries querying the SkyBoT server, returining nothing if it fails. */
            try
            {
                result = (new WebClient()).DownloadString(url);
            }
            catch
            {
                return(new List <SkybotObject>());
            }
            /* Parsing Skybot output: specific selection of vot:TABLEDATA and namespace cleanup */
            int sel1 = result.IndexOf("<vot:TABLEDATA>", StringComparison.InvariantCulture);

            if (sel1 == -1)
            {
                return(new List <SkybotObject>());
            }
            string data = result.Substring(sel1);
            int    sel2 = data.IndexOf("</vot:TABLEDATA>", StringComparison.InvariantCulture);

            data = data.Substring(0, sel2 + 16);
            data = data.Replace("vot:", "");
            /* Parsing xml output */
            XDocument           xdoc = XDocument.Parse(data);
            List <SkybotObject> objs = new List <SkybotObject>();
            XElement            key  = xdoc.Elements().ToArray()[0];

            foreach (XElement xe in key.Elements())
            {
                var          z       = xe.Elements().ToArray();
                string       permdes = z[0].Value;
                string       name    = z[1].Value;
                string       ra      = z[2].Value;
                string       dec     = z[3].Value;
                string       cls     = z[4].Value;
                int?         pd      = int.TryParse(permdes, out int pdi) ? (int?)pdi : null;
                SkybotObject oj      = new SkybotObject(name, ra + " " + dec, Time, pd, cls, 0, 0);
                objs.Add(oj);
            }
            return(objs);
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves the list of objects from the given url.
        /// </summary>
        /// <remarks>
        /// If the function returned false, but the object list is non-null, then requried fields in the VOTable were missing.
        /// </remarks>
        /// <returns><c>true</c>, if succeded in obtaining, <c>false</c> otherwise.</returns>
        /// <param name="Url">The URL from which to retrieve objects.</param>
        /// <param name="Time">Time corresponding to the given URL.</param>
        /// <param name="Objects">The resulting list of objects.</param>
        public static bool GetObjects(string Url, DateTime Time, out List <SkybotObject> Objects)
        {
            string result = string.Empty;

            /* Tries querying the SkyBoT server, returining false if it fails. */
            using (WebClient client = new WebClient())
                try
                { client.Proxy = null; result = client.DownloadString(Url); }
                catch
                { Objects = null; return(false); }

            XmlNamespaceManager xmgr = new XmlNamespaceManager(new NameTable());

            xmgr.AddNamespace("vot", VOTxmlns);
            XDocument Doc  = XDocument.Parse(result);
            XElement  Root = Doc.Root;
            /* Parse column headers */
            var Columns = Root.XPathSelectElements("/vot:VOTABLE/vot:RESOURCE/vot:TABLE/vot:FIELD", xmgr);
            Dictionary <string, int> Cset = new Dictionary <string, int>();
            int csn = 0;

            foreach (var clmn in Columns)
            {
                Cset.Add(clmn.Attribute("name").Value, csn++);
            }

            /* Parse table */
            var Rows = Root.XPathSelectElements("/vot:VOTABLE/vot:RESOURCE/vot:TABLE/vot:DATA/vot:TABLEDATA/vot:TR", xmgr);

            Objects = new List <SkybotObject>();
            foreach (var Row in Rows)
            {
                List <string> Values = Row.Elements().Select((x) => x.Value).ToList();

                try
                {
                    int?pd = int.TryParse(Values[Cset["Num"]], out int pdi) ? (int?)pdi : null;
                    double.TryParse(Values[Cset["Mv"]], out double magV);
                    double.TryParse(Values[Cset["ErrPos"]], out double ErrPos);
                    SkybotObject sko = new SkybotObject(Values[Cset["Name"]], Values[Cset["RA"]] + " " + Values[Cset["DEC"]], Time, pd, Values[Cset["Class"]], magV, ErrPos);
                    Objects.Add(sko);
                }
                catch (KeyNotFoundException) { return(false); }
            }
            return(true);
        }