A lot of astronomy is based on mapping positions of objects in either the celestial sphere, or ground based positions (ie longitude and latitude). Because of this, almost every astronomical calculation uses the coordinates – expressed in degrees – of objects to calculate their various attributes.

Fortunately, most people are used to working with degrees, although, the problem is that degrees are not exactly that easy to work with in calculations.

Degrees are made up of 60 minutes, which in turn are made up of 60 seconds per minute. With all the calculations in the application, we need a decimal format of the degree value so that we can easily work with it.

Before I move on to further topics covering coodinate system, let me introduce you to a class that works with degrees, and allows easy conversion between DMS and decimal formats.

The UraniaDMS class encapsulates all the functionality required to handle degrees.

Most of the class is pretty much self-explanatory. There are functions to set and set the degrees, minutes and seconds individually, as well as a whole. The class stores the values for degree, minute and second separately, as well as a separate value for the sign.

The most complicated function is the function that sets the value as a decimal – setDec(). It sets the degrees, minutes and seconds in turn, each time taking the whole part nad then multiplying the fractional part by 60.

There is also a getString() function that outputs a nice friendly representation of the value.

using System;

namespace UraniaLib
{
	/// 
	/// Summary description for UraniaDMS.
	/// 
	public class UraniaDMS
	{
		private long glDegrees;
		private long glMinutes;
		private double gfSeconds;
		private long glSign;
	
		public UraniaDMS()
		{

		}

		public void setSign(double pfNum)
		{
			if (pfNum < 0)
			{
				glSign = -1;
			}
			else
			{
				glSign = 1;
			}
		}

		public int IsPositive()
		{
			if (glSign == -1)
			{
				return 0;
			}
			else
			{
				return 1;
			}
		}

		public void setDegree(long plDegrees)
		{
			setSign(plDegrees);
			glDegrees = Math.Abs(plDegrees);
		}

		public void setMinutes(long plMinutes)
		{
			if (glDegrees == 0)
			{
				setSign(plMinutes);
			}
			
			glMinutes = Math.Abs(plMinutes);
			while (glMinutes >= 60)
			{
				glMinutes = glMinutes - 60;
				glDegrees = glDegrees + 1;
			}
		}

		public void setSeconds(double pfSeconds)
		{
			if ((glDegrees == 0) && (glMinutes == 0)) 
			{
				setSign(pfSeconds);
			}
			gfSeconds = Math.Abs(pfSeconds);

			while (gfSeconds >= 60)
			{
				gfSeconds = gfSeconds - 60;
				glMinutes = glMinutes + 1;
			}
			
			while (glMinutes >= 60)
			{
				glMinutes = glMinutes - 60;
				glDegrees = glDegrees + 1;
			}
		}

		public void setDMS(long plDegrees, long plMinutes, double pfSeconds)
		{
			setSign(plDegrees);
			glDegrees = Math.Abs(plDegrees);
			glMinutes = Math.Abs(plMinutes);
			gfSeconds = Math.Abs(pfSeconds);
			
			while (gfSeconds >= 60)
			{
				gfSeconds = gfSeconds - 60;
				glMinutes = glMinutes + 1;
			}
			while (glMinutes >= 60)
			{
				glMinutes = glMinutes - 60;
				glDegrees = glDegrees + 1;
			}
		}

		public void setDec(double pfDec)
		{
			double fTmp;
			setSign(pfDec);
		
			glDegrees = (long)Math.Floor(Math.Abs(pfDec));
			fTmp = (Math.Abs(pfDec) - glDegrees) * 60;
			glMinutes = (long)Math.Floor(fTmp);
			fTmp = (fTmp - glMinutes) * 60;
			gfSeconds = fTmp;
			while (gfSeconds >= 60)
			{
				gfSeconds = gfSeconds - 60;
				glMinutes = glMinutes + 1;
			}
			while (glMinutes >= 60)
			{
				glMinutes = glMinutes - 60;
				glDegrees = glDegrees + 1;
			}
		}

		public long getDegrees()
		{
			return glDegrees * glSign;
		}

		public long getMinutes()
		{
			if (glDegrees == 0)
			{
				return glMinutes * glSign;
			}
			else
			{
				return glMinutes;
			}
		}

		public double getSeconds()
		{
			if ((glDegrees == 0) && (glMinutes == 0))
			{
				return gfSeconds * glSign;
			}
			else
			{
				return gfSeconds;
			}
		}

		public double getDecFormat()
		{
			double fDec;
			fDec = ((double)glDegrees + (glMinutes / 60.0) + ((gfSeconds / 60.0 / 60.0))) * (double)glSign;
			return fDec;
		}

		public string getString(string sFormat)
		{
			string sTmp;
			
			sTmp = glDegrees.ToString("00") + "° " + glMinutes.ToString("00") + "' " + (gfSeconds.ToString(sFormat)) + "\"";

			if (glSign == -1)
			{
			sTmp = "-" + sTmp;
			}
			return sTmp;
		}
	}
}

Share