View Single Post
  #13  
Old 02-08-2007, 01:02 PM
naim
Registered User

naim is offline
 
Join Date: Aug 2007
Location: Melbourne
Posts: 1
Hello one and all!

I am Naim the son of your friendly neighbourhood poster, 'erick'.

I am writing the small app on which this thread is based. I woke up this morning and decided that the lack of progress on this app had gone on long enough, so here is the troublesome code for everyone to take a look at.

Code:
 
public enum LongitudeDirection{
      Ignore,
      East,
      West
}
public enum LatitudeDirection{
      Ignore,
      North,
      South
}
 
/**
* The variables used in parsing the user input
*/ 
public struct ParseData
{
      public double   ra_hr, ra_min, ra_sec;         // right ascension details
      public double   dec_deg, dec_min, dec_sec;     // declination details
      public double   lat_deg, lat_min, lat_sec,     // latitude details
                          lon_deg, lon_min, lon_sec;     // longitude details
      public int        day, month, year,              // date
                          hour, min, sec;                // time
      public LongitudeDirection lon_dir;             // The direction for longitude
      public LatitudeDirection lat_dir;              // The direction for latitude
}
 
/* Simply an event for the 'Calculate' button on the form */
private void Button_CalculateClick(object sender, EventArgs e)
{
      ParseData input; // storage for the parsed data from the form
      try
      {
            ParseFormData(out input); // parse the input data from the form
            try
            {
                  // have the DateTime throw an exception if the date/time input is invalid
                  DateTime t = new DateTime(input.year, input.month, input.day, input.hour, input.min, input.sec);
            }
            catch (System.Exception ex)
            {
                  throw new Exception("Invalid date and/or time.");
            }
      }
      catch(System.Exception ex)
      {
            MessageBox.Show(ex.Message);
            return;
      }
 
      double ra = RAToDouble(input.ra_hr, input.ra_min, input.ra_sec);
      double dec = DegMinSecToDouble(input.dec_deg, input.dec_min, input.dec_sec);
      double lat = DegMinSecToDouble(input.lat_deg, input.lat_min, input.lat_sec);
      double lon = DegMinSecToDouble(input.lon_deg, input.lon_min, input.lon_sec);
      DateTime time = new DateTime(input.year, input.month, input.day, input.hour, input.min, input.sec);
 
      if (input.lat_dir == LatitudeDirection.South)
           lat *= -1;
      if (input.lon_dir == LongitudeDirection.West)
           lon *= -1;
 
      Horizon result = CoordToHorizon(ref time, ra, dec, lat, lon);
      TextBox_Altitude.Text = result.Altitude.ToString();
      TextBox_Azimuth.Text = result.Azimuth.ToString();
}
 
/**
* Convert a date,time, right ascension, declination, latitude and longitude into an Altitude and Azimuth
*/ 
Horizon CoordToHorizon(ref DateTime time, double ra, double dec, double lat, double lon)
{
      Horizon res;
      double ha, sin_alt, cos_az, alt, az;
 
      // compute hour angle in degrees
      ha = CalcSidrealTime(ref time, lon) - ra;
      if (ha < 0)
            ha += 360;
 
      // convert degrees to radians
      ha = ha * System.Math.PI / 180.0;
      dec = dec * System.Math.PI / 180.0;
      lat = lat * System.Math.PI / 180.0;
      // compute altitude in radians
      sin_alt = System.Math.Sin(dec) * System.Math.Sin(lat) + 
  System.Math.Cos(dec) * System.Math.Cos(lat) * System.Math.Cos(ha);
      alt = System.Math.Asin(sin_alt);
 
      // compute azimuth in radians
      // we get a divide by zero error at the poles, or if alt == 90 degrees
      cos_az = (System.Math.Sin(dec) - System.Math.Sin(alt) * System.Math.Sin(lat)) / 
       (System.Math.Cos(alt) * System.Math.Cos(lat));
      az = System.Math.Acos(cos_az);
 
      // convert radians to degrees
      res.Altitude = alt * 180.0 / System.Math.PI;
      res.Azimuth = az * 180.0 / System.Math.PI;
 
      // choose the hemisphere
      if (System.Math.Sin(ha) > 0)
            res.Azimuth = 360.0 - res.Azimuth;
 
      return res;
}
 
/**
 * Calc the sidreal time for a specific longitude and a date and time.
 */ 
double CalcSidrealTime(ref DateTime time, double longitude)
{
      int year = time.Year;
      int month = time.Month;
      int day = time.Day;
      int hour = time.Hour;
      int min = time.Minute;
      int second = time.Second;
 
      if (month == 1 || month == 2)
      {
            year--;
            month += 12;
      }
 
      int a, b, c, d;
      a = (int)System.Math.Floor(year / 100.0);
      b = 2 - a + (int)System.Math.Floor(a / 4.0);
      c = (int)System.Math.Floor(365.25 * year);
      d = (int)System.Math.Floor(30.6001 * (month + 1));
 
      double jd, jt, mst=0;
      // calc the number of Julian days since J2000.0
      jd = b + c + d - 730550.5 + day + HourMinSecToDouble(hour, min, second) / 24.0;
 
      // calc the number of Julian centuries since J2000.0
      jt = jd / 36525.0;
 
      return mst;
}
I hope the code is clear. It seems that this forums decided that all the tabs that were formatting the code before pasting here would be converted to spaces, and I can't get the hand of the 'Increase Indent' function during posting. It seems to do very strange things with blocks of text, even when they haven't been highlighted.

As soon as this problem is figured out I'll get my dad to release the app, and it might be useful to some of you. It can save viewing positions (places on the earth where you are looking into your telescope), and the coords of celestial objects, so if you have objects that you frequently look at, you don't have to continually be typing in the same coords over and over again.

Edit:: You may wish to just copy and paste the code into you're favorite IDE, as code is always more easily understandable with syntax highlighting.

Also, the 'Ignore' in the LongitudeDirection and LatitudeDirection enums are just in case someone has a long or lat coord that isn't marked with a N,S,E or W, but might have a negative sign in front. I don't do anything special for it. For example, with the latitude, if the coord is South, the calculations need the latitude degree to be negative, and the same for longitude and West.

If the user chooses 'Ignore', it's just like choosing North or East.
Reply With Quote