View Full Version here: : Help - any C# programmers knowing celestial coordinates?
erick
04-07-2007, 02:42 PM
Both as a handy little tool and an exercise for my recently graduated computer programmer son, I thought it would be neat to have a little utility for converting a target's RA/Dec to Alt/Az for a given location/time.
The utility is written and is quite neat - a few fancy enhancements over what I wanted (eg. drop down editable lists of locations and targets). A bit of debugging to go, but one minor problem - it gives wrong answers, but not always?
It was based on this article:-
http://www.stargazing.net/kepler/altaz.html
My son decided not to try to program the spherical trigonometry, but rather found and inserted a routine someone else had developed and made available.
On testing against the example used in the article, the utility produces the given answer:-
"At 2310 UT on 10th August 1998, from Birmingham UK, M13 ( RA = 16 h 41.7 min, DEC = 36 d 28 min) will be found at an altitude of 49.2 degrees, and an azimuth of 269.1 degrees."
Great! So I tried it against some objects in our sky at present - the answers are obviously wrong? Finally I went back to the example of M13 observed from Birmingham in 1998 and plugged all the data into Stellarium and got the right answer (once I got the time right - Stellarium seems to want to always use local computer time, regardless of the chosen observing location - probably I haven't found how to change that as yet!)
We've tried everything we can think of, without success.
So, is there any kind C# programmer out there with a knowledge of or interest in celestial coordinates who would be willing to have a look and advise, please PM me.
As soon as we get this resolved, I'll be happy to make the program available. It's a very small windows utility. I did an earlier search and didn't find anything similar. I do know that what we are doing is embedded in most star chart programs.
Thanks, Eric :)
Galactic G
04-07-2007, 11:05 PM
I'll bite.....
Please show us the code!!:D
erick
04-07-2007, 11:41 PM
Thanks, I have an offer to look at it and another waiting in case expert #1 don't solve it ( ;) ). When all is fixed, we'll share the code and the executable.
Barrykgerdes
05-07-2007, 11:37 AM
The local time problem in Stellarium is known and is in the to do list. It could be fixed in the next release.
I don't know how to program in C,or C++ but I wrote a program in quick basic many years ago to do such a conversion and compiled it so it runs in Windows XP as a command line program. The maths is pretty straight forward and all the algorithms and formulae came from Peter Duffet Smith's book.
Barry
erick
05-07-2007, 12:18 PM
Thanks Barry. I'm using your landscape in Stellarium - nice panoroma. :)
Barrykgerdes
05-07-2007, 03:37 PM
Hi Eric
I remade that panorama last Monday in a high resolution form from the original photos. The file is now about 13MB but it displays the landscape at 28 pixels/deg. Great for watching stars, planets and nebulae setting through the trees.
Barry
Rob_K
05-07-2007, 03:46 PM
Contacted my brother who's a surveyor & programmer re this Eric. He says he's got some old calculator programmes and maybe some vb code that do it, but nothing in C or C++. Says there's nothing too hard about spherical trigonometry, so your problems must be to do with time and position. He may post at a later time if you don't sort it out, but he's got to head off for several days. Good luck!
Cheers -
erick
05-07-2007, 04:21 PM
Beauty, Barry! I'll go and grab it again and reload. :)
Edit: Barry, still only 3.7 MByte file on www.stellarium.org site?
erick
05-07-2007, 04:24 PM
Thanks Rob, see how we go.
I had expected my son to program the spherical trigonometry himself, but, like any efficient programmer, he just looked to see whether there was some open source code available from someone who had solved it previously. Methinks: "Whither innovation"!!
Eric
74tuc
06-07-2007, 02:26 AM
Hi Erick,
Just had a look at the web site quoted and his formulae work quite well.
Before delving into the C code check the programmers maths. There is alot of unnecessary messing about.
In the article it is stated;
"note how we added 360 to LST to bring the number into the range
0 to 360 degrees"
What should be done is to invoke the modulus function (n Mod m ) ie.
lst = lst mod 360 ... this will resolve ambiguities - if needed.
In following : HA = LST - RA it is OK for lst to have a negative value
(-55.19 degrees) all this means that the angle is measured in a CW direction
This is not needed:
"If HA negative, then add 360 to bring in range 0 to 360"
An hour angle of approx. -55 - 250 = -305 degrees is the same as (90 - 35) or 55 degrees. the maths functions sin and cos should evaluate sin(-305) and cos(-305) correctly.
1. Possible problem as to why your code works sometimes may lie in the way sines and cosines are handled by the programmer.
2. Check the calculation of local sidereal time (lst) for various conditions.
erick
06-07-2007, 08:33 AM
Thanks 74tuc, for these suggestions/hints. Eric :)
Barrykgerdes
06-07-2007, 11:32 AM
Sorry eric its too big for my website. I can email it in two bits if you give me an address.
Barry
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.
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.
vBulletin® v3.8.7, Copyright ©2000-2025, vBulletin Solutions, Inc.