Beispiel #1
0
        /// <summary>
        /// Read records
        /// </summary>
        public IEnumerable <DeltaTRecord> GetRecords()
        {
            var file = StreamProvider.LoadFile("swe_deltat.txt")
                       ?? StreamProvider.LoadFile("sedeltat.txt");

            if (file != null)
            {
                using (var reader = new StreamReader(file))
                {
                    String line;
                    Regex  reg = new Regex(@"^(\d{4})\s+(\d+\.\d+)$");
                    while ((line = reader.ReadLine()) != null)
                    {
                        line = line.Trim(' ', '\t');
                        if (String.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
                        {
                            continue;
                        }
                        var match = reg.Match(line);
                        if (!match.Success)
                        {
                            continue;
                        }
                        int y;
                        if (!int.TryParse(match.Groups[1].Value, out y))
                        {
                            continue;
                        }
                        double v;
                        if (!double.TryParse(match.Groups[2].Value, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out v))
                        {
                            continue;
                        }
                        yield return(new DeltaTRecord {
                            Year = y, Value = v
                        });
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Find the name of an asteroid
        /// </summary>
        /// <remarks>
        /// The file seasnam.txt may contain comments starting with '#'.
        /// There must be at least two columns:
        /// 1. asteroid catalog number
        /// 2. asteroid name
        /// The asteroid number may or may not be in brackets
        /// </remarks>
        /// <param name="id">Id of the asteroid</param>
        /// <returns>Name of the asteroid or null if not found</returns>
        public String FindAsteroidName(int id)
        {
            var file = StreamProvider.LoadFile(AsteroidFileName);

            if (file != null)
            {
                using (var reader = new StreamReader(file))
                {
                    Regex  reg = new Regex(@"^[\s\(\{\[]*(\d+)[\s\)\}\]]*(.+)$");
                    String line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        line = line.TrimStart(' ', '\t', '(', '[', '{');
                        if (String.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
                        {
                            continue;
                        }

                        // Parse line
                        var match = reg.Match(line);
                        if (!match.Success)
                        {
                            continue;
                        }

                        // Read id planet
                        int idPlan = int.Parse(match.Groups[1].Value);

                        // Retourne name if match
                        if (idPlan == id)
                        {
                            return(match.Groups[2].Value);
                        }
                    }
                }
            }
            return(null);
        }
        /// <summary>
        /// Find an element
        /// </summary>
        public OsculatingElement FindElement(int idPlanet, double julianDay, ref int fict_ifl)
        {
            OsculatingElement result = null;
            var file = StreamProvider.LoadFile(FictitiousFile);

            if (file != null)
            {
                using (var reader = new StreamReader(file))
                {
                    // Find the element in the file
                    int    iLine = 0, iPlanet = -1;
                    String line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        iLine++;
                        // Pass unused lines
                        line = line.Trim(' ', '\t');
                        if (String.IsNullOrWhiteSpace(line) || line.StartsWith("#"))
                        {
                            continue;
                        }
                        // Remove comments ending the line
                        int iTmp = line.IndexOf('#');
                        if (iTmp >= 0)
                        {
                            line = line.Substring(0, iTmp);
                        }
                        // Split parts
                        var parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        //            serri = C.sprintf("error in file %s, line %7.0f:", SwissEph.SE_FICTFILE, (double)iline);
                        if (parts.Length < 9)
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorNineElementsRequired);
                        }
                        iPlanet++;
                        if (iPlanet != idPlanet)
                        {
                            continue;
                        }

                        result = new OsculatingElement();

                        // epoch of elements
                        String sp = parts[0].ToLower();
                        if (sp.StartsWith("j2000"))
                        {
                            result.Epoch = SweDate.J2000;
                        }
                        else if (sp.StartsWith("b1950"))
                        {
                            result.Epoch = SweDate.B1950;
                        }
                        else if (sp.StartsWith("j1900"))
                        {
                            result.Epoch = SweDate.J1900;
                        }
                        else if (sp.StartsWith("j") || sp.StartsWith("b"))
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorInvalidEpoch);
                        }
                        else
                        {
                            result.Epoch = double.Parse(sp, CultureInfo.InvariantCulture);
                        }
                        var tt = julianDay - result.Epoch;

                        // equinox
                        sp = parts[1].TrimStart(' ', '\t').ToLower();
                        if (sp.StartsWith("j2000"))
                        {
                            result.Equinox = SweDate.J2000;
                        }
                        else if (sp.StartsWith("b1950"))
                        {
                            result.Equinox = SweDate.B1950;
                        }
                        else if (sp.StartsWith("j1900"))
                        {
                            result.Equinox = SweDate.J1900;
                        }
                        else if (sp.StartsWith("jdate"))
                        {
                            result.Equinox = julianDay;
                        }
                        else if (sp.StartsWith("j") || sp.StartsWith("b"))
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorInvalidEquinox);
                        }
                        else
                        {
                            result.Equinox = double.Parse(sp, CultureInfo.InvariantCulture);
                        }

                        // mean anomaly t0
                        double dTmp;
                        var    retc = CheckTTerms(tt, parts[2], out dTmp);
                        if (retc < 0)
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorMeanAnomalyValueInvalid);
                        }
                        result.MeanAnomaly = SweLib.DegNorm(dTmp);
                        // if mean anomaly has t terms (which happens with fictitious planet Vulcan),
                        // we set epoch = tjd, so that no motion will be added anymore equinox = tjd
                        if (retc == 1)
                        {
                            result.Epoch = julianDay;
                        }
                        result.MeanAnomaly *= SweLib.DEGTORAD;

                        // semi-axis
                        retc = CheckTTerms(tt, parts[3], out dTmp);
                        if (dTmp <= 0 || retc < 0)
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorSemiAxisValueInvalid);
                        }
                        result.SemiAxis = dTmp;

                        // eccentricity
                        retc = CheckTTerms(tt, parts[4], out dTmp);
                        if (dTmp >= 1 || dTmp < 0 || retc < 0)
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorEccentricityValueInvalid);
                        }
                        result.Eccentricity = dTmp;

                        // perihelion argument
                        retc = CheckTTerms(tt, parts[5], out dTmp);
                        if (retc < 0)
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorPerihelionArgumentValueInvalid);
                        }
                        result.Perihelion  = SweLib.DegNorm(dTmp);
                        result.Perihelion *= SweLib.DEGTORAD;

                        // node
                        retc = CheckTTerms(tt, parts[6], out dTmp);
                        if (retc < 0)
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorNodeValueInvalid);
                        }
                        result.AscendingNode  = SweLib.DegNorm(dTmp);
                        result.AscendingNode *= SweLib.DEGTORAD;

                        // Inclination
                        retc = CheckTTerms(tt, parts[7], out dTmp);
                        if (retc < 0)
                        {
                            throw new SweError(Locales.LSR.Error_ReadingFile, FictitiousFile, iLine, Locales.LSR.Fictitious_ErrorInclinationValueInvalid);
                        }
                        result.Inclination  = SweLib.DegNorm(dTmp);
                        result.Inclination *= SweLib.DEGTORAD;

                        // planet name
                        result.Name = parts[8].Trim(' ', '\t');

                        // geocentric
                        if (parts.Length > 9)
                        {
                            parts[9] = parts[9].ToLower();
                            if (parts[9].Contains("geo"))
                            {
                                fict_ifl |= FictitiousGeo;
                            }
                        }
                        break;
                    }
                    if (result == null)
                    {
                        throw new SweError(
                                  Locales.LSR.Error_ReadingFile, FictitiousFile, iLine,
                                  String.Format(Locales.LSR.Fictitious_ErrorElementsNotFound, idPlanet)
                                  );
                    }
                }
            }
            return(result);
        }