I am making a Javascript program and on of the functions is to convert altitude/azimuth to right-ascension/declination given a time and latitude. My code can find the declination fairly accurately, I checked with Stellarium. I have been using this site to help me with the math.
My program gives me the wrong value for horizontal ascension which I plan on using to find right ascension (I already have a function to find local sidereal time which works). Here is my code for the equation
var ha = asin(-sin(az)*cos(alt) / cos(dec)) * (180 / Math.PI);
this code is in Javascript but I defined custom sin/cos/asin functions that take degrees as input and return radians because that is the form my data is in.
The site I use also says that this equation should give the same result
var ha = acos((sin(alt) - sin(lat)*sin(dec)) / (cos(lat) * cos(dec))) * (180 / Math.PI);
but, the two equations give different results and neither are correct according to stellarium. I have checked that all the variables I am putting in are correct and I am almost certain I entered the equation correct. Here is the full code on github. I need help figuring out how to fix this problem.
–Note this can be run with node js with no libraries
The result I get is { ra: [ 23, 57, 37.9 ], dec: [ -5, 24, 38.88 ] }
It should be getting { ra: [ 5, 36, 22.6 ], dec: [ -5, 24, 38.88 ] it does not have to be exact, I only really care about the first number of ra (right ascension). It is also formatted in HMS format. The datetime is hardcoded to Febuary 1st 2022 12:00:00 so that is what you should set stellarium to if you are testing this out.
Thank you – CR.