示例#1
0
        /// <summary>
        /// 读取头部信息
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Sp3Header Read(string path)
        {
            Sp3Header header = new Sp3Header();

            header.SourceName = Path.GetFileName(path);
            using (TextReader r = new StreamReader(path))
            {
                string line = null;

                string symbol = null;
                line   = r.ReadLine();
                symbol = line.Substring(0, 1);
                if (symbol != LineSymbols[0])
                {
                    throw new Exception("SP3文件不合法");
                }

                header.VersionId        = line.Substring(1, 1);
                header.P_V_ModeFlag     = line.Substring(2, 1);
                header.StartTime        = Time.Parse(line.Substring(3, 28));
                header.NumberOfEpochs   = int.Parse(line.Substring(32, 7));
                header.DataUsed         = line.Substring(40, 5);
                header.CoordinateSystem = line.Substring(46, 5);
                header.OrbitType        = line.Substring(52, 3);
                header.AgencyName       = line.Substring(56, 4);


                int lineIndex                  = 0;
                int sartLineIndexOfSat         = 0;
                int sartLineIndexOfSatAccuracy = 0;
                do
                {
                    line = r.ReadLine();
                    lineIndex++;

                    switch (FirstTwoCharToMarker(line))
                    {
                    case  Sp3HeaderLineMarker.Time:
                        header.GPSWeek           = int.Parse(line.Substring(3, 4));
                        header.SecondsOfWeek     = double.Parse(line.Substring(9, 15));
                        header.EpochInterval     = double.Parse(line.Substring(24, 14));
                        header.ModJulianDayStart = int.Parse(line.Substring(39, 5));
                        header.FractionalDay     = double.Parse(line.Substring(45, 15));
                        break;

                    case Sp3HeaderLineMarker.SatelliteCount:
                        sartLineIndexOfSat        = lineIndex;
                        header.NumberOfSatellites = int.Parse(line.Substring(3, 3));

                        int len = header.NumberOfSatellites >= 17 ? 17 : header.NumberOfSatellites;

                        header.PRNs = SatelliteNumber.ParsePRNs(line.Substring(9, len * 3));
                        break;

                    //都用于存储卫星号。
                    case  Sp3HeaderLineMarker.SatelliteNumber:
                        int prevSatCount = 17 * (lineIndex - sartLineIndexOfSat);
                        int satLen       = header.NumberOfSatellites >= prevSatCount + 17 ? 17 : header.NumberOfSatellites - prevSatCount;

                        if (satLen > 0)
                        {
                            header.PRNs.AddRange(SatelliteNumber.ParsePRNs(line.Substring(9, satLen * 3)));
                        }
                        break;

                    //用于表示卫星的精度,1表示极佳, 99表示不要用,0表示未知。
                    case  Sp3HeaderLineMarker.Accuracy:
                        if (sartLineIndexOfSatAccuracy == 0)
                        {
                            sartLineIndexOfSatAccuracy = lineIndex;
                        }
                        if (header.SatAccuraces == null)
                        {
                            header.SatAccuraces = new Dictionary <SatelliteNumber, int>();
                        }

                        int prevSatAccuCount = 17 * (lineIndex - sartLineIndexOfSatAccuracy);
                        int satAccLen        = header.NumberOfSatellites >= prevSatAccuCount + 17 ? 17 : header.NumberOfSatellites - prevSatAccuCount;
                        if (satAccLen > 0)
                        {
                            List <string> sts = Utils.StringUtil.Split(line.Substring(9, satAccLen * 3), 3);
                            for (int i = prevSatAccuCount, j = 0; j < satAccLen; i++, j++)
                            {
                                header.SatAccuraces.Add(header.PRNs[i], int.Parse(sts[j]));
                            }
                        }
                        break;

                    case  Sp3HeaderLineMarker.Char:
                        if (header.Characters == null)
                        {
                            header.Characters = new List <string>();
                        }
                        header.Characters.AddRange(new List <String>(line.Substring(3, 57).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)));
                        break;

                    case  Sp3HeaderLineMarker.Float:
                        if (header.Floats == null)
                        {
                            header.Floats = new List <double>();
                        }
                        List <String> list = new List <String>(line.Substring(3, 57).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                        foreach (string Str in list)
                        {
                            header.Floats.Add(double.Parse(Str));
                        }
                        break;

                    case  Sp3HeaderLineMarker.Int:
                        if (header.Ints == null)
                        {
                            header.Ints = new List <int>();
                        }
                        List <String> listINts = new List <String>(line.Substring(3, 57).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                        foreach (string Str in listINts)
                        {
                            header.Ints.Add(int.Parse(Str));
                        }
                        break;

                    case  Sp3HeaderLineMarker.Comment:
                        if (header.Comments == null)
                        {
                            header.Comments = new List <string>();
                        }
                        if (line.Length > 3)
                        {
                            header.Comments.Add(line.Substring(3));
                        }
                        break;

                    case  Sp3HeaderLineMarker.End:
                        header.LineNumber = lineIndex;
                        return(header);

                    // break;
                    default:
                        throw new ApplicationException("天啊!你是不可能看到我的!除非出错了,难道是版本变了?");
                    }
                } while (String.Compare(line.Substring(0, 2), "* ", true) != 0);
                header.LineNumber = lineIndex + 1;
            }
            return(header);
        }