Geeks With Blogs
Bill Osuch - Random geek notes

Part 1 of my attempt at converting the code in Astronomy With Your Personal Computer.

MINSEC - converting decimal degrees/hours into degrees/hours, minutes, and seconds (and vice-versa).

Most astronomy calculations are going to be expressed as degrees, minutes and seconds of arc, but computers need to work in decimal degrees. These methods will allow you to convert from one to the other and back.

To convert from decimal to degrees/minutes/seconds:

  1. The whole units of degrees will remain the same (i.e. for 238.91945°, start with 238°).
  2. Multiply the decimal by 60 (i.e. .91945 * 60 = 55.167) - the whole number becomes the minutes (55').
  3. Take the remaining decimal and multiply by 60. (i.e. .167 * 60 = 10.02) - the resulting number becomes the seconds (10.02"). Seconds can remain as a decimal.
  4. Take your three sets of numbers and put them together, using the symbols for degrees (°), minutes (‘), and seconds (") (i.e. 238° 55' 10.02").

And to convert from degrees/minutes/seconds to decimal, it's just one step:

degrees + (minutes * 1/60) + (seconds * 1/60 * 1/60)

I decided to represent the degrees/minutes/seconds as a struct:

        public struct dms
        {
            public decimal degrees, minutes, seconds;
            public string dmsString;
        }

So to convert from decimal to dms:

        private dms DecimalToDegMinSec(decimal decimalNumber)
        {
            dms myDMS = new dms();
            myDMS.degrees = Convert.ToInt32(Math.Truncate(decimalNumber));
            myDMS.minutes = Convert.ToInt32(Math.Truncate((decimalNumber - myDMS.degrees) * 60));
            myDMS.seconds = (((decimalNumber - myDMS.degrees) * 60) - myDMS.minutes) * 60;
            myDMS.dmsString = myDMS.degrees.ToString() + "° " + myDMS.minutes.ToString() + "' " + ((double)myDMS.seconds).ToString() + "\"";
            return myDMS;
        }

and to convert dms to decimal:

        private decimal DegMinSecToDecimal(dms myDMS)
        {
            return myDMS.degrees + (myDMS.minutes * (1m / 60m)) + (myDMS.seconds * (1m / 60m) * (1m / 60m));
        }

Try it and see:

            dms myDMS = new dms();
            myDMS = DecimalToDegMinSec(238.91945m);
            Console.WriteLine("Converting 238.91945 to DMS: " + myDMS.dmsString);
            myDMS.dmsString = "";
            Console.WriteLine("Converting back to decimal: " + DegMinSecToDecimal(myDMS).ToString("G28"));
            Console.ReadLine();

Technorati Tags:

Posted on Wednesday, October 5, 2011 12:52 PM C# , Astronomy | Back to top


Comments on this post: Converting decimal degrees to degrees/minutes/seconds (Astronomy With Your Personal Computer)

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Bill Osuch | Powered by: GeeksWithBlogs.net