Example #1
0
        public void LoadModel(MagneticModelSet modelSet)
        {
            _Models = null;

            if (modelSet == null)
            {
                throw new GeoMagExceptionFileNotFound("Error coefficient file name not specified");
            }

            _Models = modelSet;
        }
Example #2
0
        public void LoadModel(string modelFile, string svFile)
        {
            _Models = null;

            if (string.IsNullOrEmpty(modelFile))
            {
                throw new GeoMagExceptionFileNotFound("Error coefficient file name not specified");
            }

            _Models = ModelReader.Read(modelFile, svFile);
        }
        public MagneticModelSet(MagneticModelSet other)
        {
            ID          = other.ID;
            Type        = other.Type;
            MinDate     = other.MinDate;
            MaxDate     = other.MaxDate;
            EarthRadius = other.EarthRadius;

            FileNames = new List <string>();
            if (other.FileNames.Any())
            {
                FileNames.AddRange(other.FileNames);
            }

            Models = new List <MagneticModel>();
            if (other.Models.Any())
            {
                Models.AddRange(other.Models);
            }
        }
Example #4
0
 public GeoMag()
 {
     _Models = null;
 }
        private static MagneticModelSet COFreader(string modelFile)
        {
            var outModels = new MagneticModelSet();

            outModels.FileNames.Add(Path.GetFileName(modelFile));

            double tempDbl = 0;

            using (var stream = new StreamReader(modelFile))
            {
                string inbuff;

                Int32 mModelIdx = -1;                             /* First model will be 0 */

                Int32 eModelIdx = -1;                             /* First model will be 0 */

                Int32 lineNumber = 0;

                while ((inbuff = stream.ReadLine()) != null)
                {
                    lineNumber++;

                    inbuff = inbuff.Trim();

                    if (!string.IsNullOrEmpty(inbuff))
                    {
                        var lineParase = inbuff.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                        if (lineNumber.Equals(1))
                        {
                            outModels.Type = inbuff.CheckStringForModel();
                        }

                        if (!inbuff.CheckStringForModel().Equals(knownModels.NONE))
                        {
                            /* New model */
                            if (outModels.Type.Equals(knownModels.EMM))
                            {
                                double.TryParse(lineParase[0], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                            }
                            else
                            {
                                double.TryParse(lineParase[1], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                            }

                            outModels.AddModel(new MagneticModel
                            {
                                Type = @"M",
                                Year = tempDbl
                            });

                            mModelIdx = outModels.GetModels.Count() - 1;

                            outModels.AddModel(new MagneticModel
                            {
                                Type = @"S",
                                Year = tempDbl
                            });

                            eModelIdx = outModels.GetModels.Count() - 1;
                        }
                        else if (mModelIdx > -1)
                        {
                            if (outModels.Type.Equals(knownModels.EMM))
                            {
                                #region Split File Line Reader
                                Int32 lineDegree = -1;

                                Int32 lineOrder = -1;

                                for (Int32 itemIdx = 0; itemIdx < lineParase.Count(); itemIdx++)
                                {
                                    switch (itemIdx)
                                    {
                                    //Degree(n) (int)
                                    case 0:
                                        Int32.TryParse(lineParase[itemIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out lineDegree);
                                        break;

                                    //Order(m) (int)
                                    case 1:
                                        Int32.TryParse(lineParase[itemIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out lineOrder);
                                        break;

                                    //g1 (double)
                                    case 2:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        outModels.AddCoefficients(mModelIdx, tempDbl);
                                        break;

                                    //h1 (double)
                                    case 3:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        if (lineOrder > 0)
                                        {
                                            outModels.AddCoefficients(mModelIdx, tempDbl);
                                        }
                                        break;

                                    //g2 (double)
                                    case 4:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        outModels.AddCoefficients(eModelIdx, tempDbl);
                                        break;

                                    //h2 (double)
                                    case 5:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        if (lineOrder > 0)
                                        {
                                            outModels.AddCoefficients(eModelIdx, tempDbl);
                                        }
                                        break;

                                    //irat (string)
                                    case 6:
                                        //coeffLine.Model = lineParase[itemIdx];
                                        break;

                                    //LineNum (int)
                                    case 7:
                                        //Int32.TryParse(lineParase[itemIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out tempInt);
                                        //coeffLine.LineNum = tempInt;
                                        break;
                                    }
                                }
                                #endregion
                            }
                            else
                            {
                                #region Single File Line Reader
                                Int32 lineDegree = -1;

                                Int32 lineOrder = -1;

                                for (Int32 itemIdx = 0; itemIdx < lineParase.Count(); itemIdx++)
                                {
                                    switch (itemIdx)
                                    {
                                    //Degree(n) (int)
                                    case 0:
                                        Int32.TryParse(lineParase[itemIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out lineDegree);
                                        break;

                                    //Order(m) (int)
                                    case 1:
                                        Int32.TryParse(lineParase[itemIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out lineOrder);
                                        break;

                                    //g1 (double)
                                    case 2:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        outModels.AddCoefficients(mModelIdx, tempDbl);
                                        break;

                                    //h1 (double)
                                    case 3:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        if (lineOrder > 0)
                                        {
                                            outModels.AddCoefficients(mModelIdx, tempDbl);
                                        }
                                        break;

                                    //g2 (double)
                                    case 4:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        outModels.AddCoefficients(eModelIdx, tempDbl);
                                        break;

                                    //h2 (double)
                                    case 5:
                                        double.TryParse(lineParase[itemIdx], NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);
                                        if (lineOrder > 0)
                                        {
                                            outModels.AddCoefficients(eModelIdx, tempDbl);
                                        }
                                        break;

                                    //irat (string)
                                    case 6:
                                        //coeffLine.Model = lineParase[itemIdx];
                                        break;

                                    //LineNum (int)
                                    case 7:
                                        //Int32.TryParse(lineParase[itemIdx], NumberStyles.Integer, CultureInfo.InvariantCulture, out tempInt);
                                        //coeffLine.LineNum = tempInt;
                                        break;
                                    }
                                }
                                #endregion
                            }
                        }
                    }
                }
            }

            //Add 5 years to the start date of the final model
            outModels.MaxDate += 5;

            if (outModels.NumberOfModels.Equals(0))
            {
                throw new GeoMagExceptionModelNotLoaded(string.Format("Error: No models were detected in the specified file{0}File Name: {1}",
                                                                      Environment.NewLine, Path.GetFileName(modelFile)));
            }

            return(outModels);
        }
        private static MagneticModelSet DATreader(string modelFile)
        {
            var outModels = new MagneticModelSet();

            outModels.FileNames.Add(Path.GetFileName(modelFile));

            double tempDbl = 0;

            double earthRadius = 6371.001;                 /* old (default) reference radius */

            using (var stream = new StreamReader(modelFile))
            {
                string inbuff;

                Int32 modelI = -1;                             /* First model will be 0 */

                Int32 lineNumber = 0;

                while ((inbuff = stream.ReadLine()) != null)
                {
                    lineNumber++;

                    inbuff = inbuff.Trim();

                    if (!string.IsNullOrEmpty(inbuff))
                    {
                        var lineParase = inbuff.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

                        if (lineNumber.Equals(1))
                        {
                            //Min Year
                            double.TryParse(inbuff, NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);

                            outModels.MinDate = tempDbl;
                        }
                        else if (lineNumber.Equals(2))
                        {
                            //Max Year
                            double.TryParse(inbuff, NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);

                            outModels.MaxDate = tempDbl;
                        }
                        else if (inbuff.IndexOf("N", StringComparison.OrdinalIgnoreCase).Equals(0))
                        {
                            /* ignore the rest of the line */
                            earthRadius = Constants.EarthsRadiusInKm;   /* new reference radius */
                        }
                        else if (inbuff.IndexOf("M", StringComparison.OrdinalIgnoreCase).Equals(0) ||
                                 inbuff.IndexOf("S", StringComparison.OrdinalIgnoreCase).Equals(0) ||
                                 inbuff.IndexOf("E", StringComparison.OrdinalIgnoreCase).Equals(0)) /* If 1st 3 chars are spaces */
                        {
                            modelI++;                                                               /* New model */

                            double.TryParse(lineParase.Last(), NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);

                            outModels.AddModel(new MagneticModel
                            {
                                Type = lineParase.First(),
                                Year = tempDbl
                            });
                        }
                        else if (modelI > -1)
                        {
                            /* read in more values for this era */

                            foreach (var ptr in lineParase)
                            {
                                double.TryParse(ptr, NumberStyles.Float, CultureInfo.InvariantCulture, out tempDbl);

                                outModels.AddCoefficients(modelI, tempDbl);
                            }
                        }
                    }
                }
            }

            if (outModels.NumberOfModels.Equals(0))
            {
                throw new GeoMagExceptionModelNotLoaded(string.Format("Error: No models were detected in the specified file{0}File Name: {1}",
                                                                      Environment.NewLine, Path.GetFileName(modelFile)));
            }

            outModels.EarthRadius = earthRadius;

            return(outModels);
        }
Example #7
0
        /// <summary>
        /// calculate main field and field change at a 'spot' in time and space
        /// </summary>
        /// <param name="CalculationOptions">details of the time and space to for which to calculate</param>
        /// <param name="dateOfCalc">The DateTime object for the calculation date</param>
        /// <param name="magModels">the models loaded by the Model Reader</param>
        /// <param name="internalSH">internal coeffiecients for a particular date</param>
        /// <param name="externalSH">external coeffiecients for a particular date</param>
        /// <param name="earthRadius">Optional: Radius of the earth used by the model</param>
        /// <returns>The result of the magnetic calculation</returns>
        public static MagneticCalculations SpotCalculation(CalculationOptions CalculationOptions, DateTime dateOfCalc, MagneticModelSet magModels,
                                                           Coefficients internalSH, Coefficients externalSH, double earthRadius = Constants.EarthsRadiusInKm)
        {
            /* call procedure GETFIELD - values returned in geomagfield*/
            var fieldCalculations = GetField(CalculationOptions, internalSH, externalSH, earthRadius);

            GeoMagVector svCalculations = null;

            if (CalculationOptions.SecularVariation)
            {
                double date1 = -1;
                double date2 = -1;

                CalculateDatesForVariation(CalculationOptions.StartDate.ToDecimal(), magModels.MinDate, magModels.MaxDate, out date1, out date2);

                /* get coefficients and field for date1 */
                var SVintSH = new Coefficients();
                var SVextSH = new Coefficients();

                magModels.GetIntExt(date1, out SVintSH, out SVextSH);

                var geomagfield1 = GetField(CalculationOptions, SVintSH, SVextSH, earthRadius);

                /* get coefficients and field for date2 */

                magModels.GetIntExt(date2, out SVintSH, out SVextSH);

                var geomagfield2 = GetField(CalculationOptions, SVintSH, SVextSH, earthRadius);

                /* calculate changes - difference between
                 * fields 1 & 2 returned in fielddiffs */

                svCalculations = geomagfield2.Subtract(geomagfield1);
            } /* end of secular variation calc. for SPOT */

            return(new MagneticCalculations(dateOfCalc, fieldCalculations, svCalculations));
        }