Пример #1
0
        /// <summary>
        /// Decode one report line
        /// </summary>
        /// <param name="csvLine">The Station report line</param>
        /// <param name="lookup">An item lookup table</param>
        /// <returns>A MetarData record</returns>
        private static MetarTafData DecodeCSVLine(string csvLine, IDictionary <Fields, int> lookup)
        {
            var rec = new MetarTafData();

            try {
                string[] e = csvLine.Split(new char[] { ',' });

                rec.RAW  = e[lookup[Fields.raw_text]].Trim( );
                rec.Data = MDEC.MTData.Decode(rec.RAW);
                if (double.TryParse(e[lookup[Fields.latitude]], out double dData))
                {
                    rec.Lat = dData;
                }
                if (double.TryParse(e[lookup[Fields.longitude]], out dData))
                {
                    rec.Lon = dData;
                }
                if (float.TryParse(e[lookup[Fields.elevation_m]], out float fData))
                {
                    rec.Elevation_m = fData;
                }
                rec.Valid = true;
            }
            catch {
                rec.Error = $"Unrecognizable TAF record\n{csvLine}";
                rec.Valid = false;
            }
            return(rec);
        }
Пример #2
0
        /// <summary>
        /// Decodes a METAR / TAF message received as CSV
        /// Returns one or more MetarTafData Records as List
        /// </summary>
        /// <param name="csvData">The Metar/Taf Message</param>
        /// <returns>A list of MetarTafData</returns>
        internal static MetarTafDataList DecodeCSV(string csvData)
        {
            // arrives as

            /*
             * No errors
             * No warnings
             * 3 ms
             * data source=tafs
             * N results
             * header line
             * value line
             */
            var ret = new MetarTafDataList();

            if (string.IsNullOrEmpty(csvData))
            {
                var rec = new MetarTafData(); ret.Add(rec);
                rec.Error = $"Empty TAF record received";
                rec.Valid = false;
                return(ret);
            }
            using (var sr = new StringReader(csvData)) {
                try {
                    var line = sr.ReadLine(); // errors
                    if (!line.StartsWith("No errors"))
                    {
                        var rec = new MetarTafData(); ret.Add(rec);
                        rec.Error = line;
                        rec.Valid = false;
                        return(ret);
                    }
                    line = sr.ReadLine( );                       // warnings
                    line = sr.ReadLine( );                       // response time
                    line = sr.ReadLine( );                       // data sources
                    line = sr.ReadLine( );                       // N results
                    string[] e = line.Split(new char[] { ' ' }); // get the N
                    if (!int.TryParse(e[0], out int nRec))
                    {
                        var rec = new MetarTafData(); ret.Add(rec);
                        rec.Error = $"TAF cannot derive number of records\n{line}";
                        rec.Valid = false;
                        return(ret);
                    }
                    if (nRec <= 0)
                    {
                        var rec = new MetarTafData(); ret.Add(rec);
                        rec.Error = $"TAF contains 0 records (not a known station?)\n{line}";
                        rec.Valid = false;
                        return(ret);
                    }

                    line = sr.ReadLine( ); // headers
                    if (!line.StartsWith($"{Fields.raw_text},"))
                    {
                        var rec = new MetarTafData(); ret.Add(rec);
                        rec.Error = $"Unrecognizable TAF record header format\n{line}";
                        rec.Valid = false;
                        return(ret);
                    }
                    // Create an item Lookup table
                    var hList  = line.Split(new char[] { ',' }).ToList(); // the list of items from the header
                    var lookup = CreateLookup(line);                      // a lookup where the value is the item index in the list (and CSV line)
                    if (lookup.Count == 0)
                    {
                        var rec = new MetarTafData(); ret.Add(rec);
                        rec.Error = $"Field(s) not found in TAF record header format\n{line}";
                        rec.Valid = false;
                        return(ret);
                    }

                    // get all station reports
                    for (int i = 0; i < nRec; i++)
                    {
                        line = sr.ReadLine( ); // result line
                        ret.Add(DecodeCSVLine(line, lookup));
                    }
                }
#pragma warning disable CS0168 // Variable is declared but never used
                catch (Exception e) {
#pragma warning restore CS0168 // Variable is declared but never used
                    var rec = new MetarTafData(); ret.Add(rec);
                    rec.Error = $"Unrecognizable TAF record\n{csvData}";
                    rec.Valid = false;
                }

                return(ret);
            }
        }