<p>The calculations are not trivial, but they are encapsulated in software libraries such as pyephem, which has <a href="http://rhodesmill.org/pyephem/rise-set.html" rel="nofollow">examples of finding rise, set and transit</a>
If you want to understand how these are calculated, you can readnthe source, which is based on the xephem application. </p>

<p>One detail which complicates the calculation is the refraction caused by the atmosphere, which can change the moment of setting by several minutes.</p>

CC BY-SA 3.0
2016-04-10T18:23:18.767Z
Thanks. I'll take a look at pyephem, but my python is weak.  Do you know of any C or .NET based libraries?
W. Christopher Moses
CC BY-SA 3.0
2016-04-10T20:09:36.490Z
Pyephem is based on xephem, which is in C. Xephem is a graphical application  which can calculate all the same things that pyephem does.
James K
CC BY-SA 4.0
2018-06-16T16:38:24.280Z
@W.ChristopherMoses: `pyephem` seems to be just a slim wrapper around `xephem`, whose source code is included.
serv-inc
CC BY-SA 4.0
2018-07-03T19:40:16.527Z
@W.ChristopherMoses: make that libastro
serv-inc
CC BY-SA 3.0
2016-04-09T23:20:04.880Z
2016-04-09T23:20:04.880Z
James K
<p>If you want a website rather than a software package like James suggests, I recommend use the INGT <a href="http://catserver.ing.iac.es/staralt/index.php" rel="nofollow noreferrer">object visibility tool</a> which has all 3 inputs you request and produces an elevation plot with rise and set times, maximum altitude etc. An example is shown below:</p>

<p><a href="https://i.sstatic.net/wLQKd.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wLQKd.gif" alt="enter image description here"></a></p>

CC BY-SA 3.0
2016-04-10T18:34:45.943Z
Glancing at the source, it does not look like code is available on the page.  Do you know of other sites which produce such a nice plot and have the code embedded?
W. Christopher Moses
CC BY-SA 3.0
2016-04-10T20:21:21.683Z
There are these ones on the bottom of the page under the "More" tab : iObserve, astronomy tools, JSkyCalc, obstools. They might have code embedded if you look through them, however you might like to try Astroplan (https://astroplan.readthedocs.org/en/latest/tutorials/plots.html) its an Astropy affiliated package that looks very promising as an astronomy planning tool, it should have all the source code on there, although I believe its still in development.
Dean
CC BY-SA 3.0
2016-04-09T23:48:39.253Z
2016-04-09T23:48:39.253Z
Dean
<p>Meeus provides a fairly simple algorithm in Astronomical Algorithms.  An example implementation is available in <a href="http://astrogreg.com/risesetalgorithm.html" rel="nofollow noreferrer">Rise and Set Algorithm</a>.  The code is fairly simple and I have reproduced it below.</p>
<p>A couple of warnings.  The first is that the algorithm assumes longitudes to the West are positive, which is the opposite of how most GPS APIs return it.  And, if the object moves considerably through the day, you may want to recompute the position for the given times, then recompute the rise and set times again.</p>
<pre><code>//By Greg Miller gmiller@gregmiller.net www.astrogreg.com
//Released as public domain
const toRad=Math.PI/180.0;
const toDeg=180.0/Math.PI;

//Corrects values to make them between 0 and 1
function constrain(v){
    if(v&lt;0){return v+1;}
    if(v&gt;1){return v-1;}
    return v;
}

//All angles must be in radians
//Outputs are times in hours GMT (not accounting for daylight saving time)
//From Meeus Page 101
function getRiseSet(jd,lat,lon,ra,dec){
    const h0=-0.8333 //For Sun
    //const h0=-0.5667 //For stars and planets
    //const h0=0.125   //For Moon

    const cosH=(Math.sin(h0*Math.PI/180.0)-Math.sin(lat)*Math.sin(dec)) / (Math.cos(lat)*Math.cos(dec));
    const H0=Math.acos(cosH)*180.0/Math.PI;

    const gmst=GMST(Math.floor(jd)+.5);

    const transit=(ra*toDeg+lon*toDeg-gmst)/360.0;
    const rise=transit-(H0/360.0);
    const set=transit+(H0/360.0);

    return [constrain(transit)*24.0,constrain(rise)*24.0,constrain(set)*24.0];
}

//Greenwhich mean sidreal time from Meeus page 87
//Input is julian date, does not have to be 0h
//Output is angle in degrees
function GMST(jd){
    const T=(jd-2451545.0)/36525.0;
    let st=280.46061837+360.98564736629*(jd-2451545.0)+0.000387933*T*T - T*T*T/38710000.0;
    st=st%360;
    if(st&lt;0){st+=360;}

    return st;
    //return st*Math.PI/180.0;
}
</code></pre>

CC BY-SA 4.0
2020-08-31T00:17:11.147Z
2020-08-31T00:17:11.147Z
Greg Miller
<p>I've been looking, unsuccessfully, for hours for a simple set of equations:</p>

<p><strong>Input</strong>: <br>
RA and Dec of an object<br>
Observers Lat and Lng<br>
Current Time<br></p>

<p><strong>Output</strong>: <br>
Rise Time <br>
Transit Time<br>
Set Time<br>
(and, hopefully) max elevation at transit <br>
(and, hopefully) current alt and az of object</p>

<p>It seems like this should be a simple relationship, but I haven't been able to find it.<br>
If an equation simply gives the current alt/az for an object, I guess I could solve for az and then find the maximum for a given time range. <br></p>

<p><strong>Questions</strong>: <br>
1. Anybody know of such equations? Or, even better, some sample code. <br>
2. If not, can anybody point me in the right direction?</p>

<p>Thanks</p>

CC BY-SA 3.0
2016-04-10T23:09:24.593Z
Yes! I'm only interested in "fixed" objects on the celestial sphere. By "simple," I mean simple for an experienced programmer to implement. Of course, if it is simple to understand, all the better.  It does seem like it should be "simple" all around, but I would rather not try to create it all from scratch.  Any references or pointers would be greatly appreciated
W. Christopher Moses
<coordinate><celestial-mechanics><declination>
Need Simple equation for Rise, Transit, and Set time
W. Christopher Moses
# Need Simple equation for Rise, Transit, and Set time

(Asked by: username_0 on 2016-04-09)

I've been looking, unsuccessfully, for hours for a simple set of equations:

**Input**:   
RA and Dec of an object  
Observers Lat and Lng  
Current Time

**Output**:   
Rise Time   
Transit Time  
Set Time  
(and, hopefully) max elevation at transit   
(and, hopefully) current alt and az of object

It seems like this should be a simple relationship, but I haven't been able to find it.  
If an equation simply gives the current alt/az for an object, I guess I could solve for az and then find the maximum for a given time range.

**Questions**:   
1. Anybody know of such equations? Or, even better, some sample code.   
2. If not, can anybody point me in the right direction?

Thanks

**Tags:** coordinate, celestial-mechanics, declination

> **Comments:**
> **username_0**: Yes! I'm only interested in "fixed" objects on the celestial sphere. By "simple," I mean simple for an experienced programmer to implement. Of course, if it is simple to understand, all the better.  It does seem like it should be "simple" all around, but I would rather not try to create it all from scratch.  Any references or pointers would be greatly appreciated  

---

## 3 answers

### Answer by username_1 on 2016-04-09. Score: 3

The calculations are not trivial, but they are encapsulated in software libraries such as pyephem, which has [examples of finding rise, set and transit](http://rhodesmill.org/pyephem/rise-set.html)
If you want to understand how these are calculated, you can readnthe source, which is based on the xephem application.

One detail which complicates the calculation is the refraction caused by the atmosphere, which can change the moment of setting by several minutes.

> **Comments:**
> **username_0**: Thanks. I'll take a look at pyephem, but my python is weak.  Do you know of any C or .NET based libraries?  
> **username_1**: Pyephem is based on xephem, which is in C. Xephem is a graphical application  which can calculate all the same things that pyephem does.  
> **username_2**: @username_0: `pyephem` seems to be just a slim wrapper around `xephem`, whose source code is included.  
> **username_2**: @username_0: make that libastro  

---

### Answer by username_3 on 2016-04-09. Score: 2

If you want a website rather than a software package like username_1 suggests, I recommend use the INGT [object visibility tool](http://catserver.ing.iac.es/staralt/index.php) which has all 3 inputs you request and produces an elevation plot with rise and set times, maximum altitude etc. An example is shown below:

[![enter image description here](https://i.sstatic.net/wLQKd.gif)](https://i.sstatic.net/wLQKd.gif)

> **Comments:**
> **username_0**: Glancing at the source, it does not look like code is available on the page.  Do you know of other sites which produce such a nice plot and have the code embedded?  
> **username_3**: There are these ones on the bottom of the page under the "More" tab : iObserve, astronomy tools, JSkyCalc, obstools. They might have code embedded if you look through them, however you might like to try Astroplan (https://astroplan.readthedocs.org/en/latest/tutorials/plots.html) its an Astropy affiliated package that looks very promising as an astronomy planning tool, it should have all the source code on there, although I believe its still in development.  

---

### Answer by username_4 on 2020-08-31. Score: 0

Meeus provides a fairly simple algorithm in Astronomical Algorithms. An example implementation is available in [Rise and Set Algorithm](http://astrogreg.com/risesetalgorithm.html). The code is fairly simple and I have reproduced it below.

A couple of warnings. The first is that the algorithm assumes longitudes to the West are positive, which is the opposite of how most GPS APIs return it. And, if the object moves considerably through the day, you may want to recompute the position for the given times, then recompute the rise and set times again.

```
//By username_4 username_4 gmiller@username_4.net www.astrogreg.com
//Released as public domain
const toRad=Math.PI/180.0;
const toDeg=180.0/Math.PI;

//Corrects values to make them between 0 and 1
function constrain(v){
    if(v<0){return v+1;}
    if(v>1){return v-1;}
    return v;
}

//All angles must be in radians
//Outputs are times in hours GMT (not accounting for daylight saving time)
//From Meeus Page 101
function getRiseSet(jd,lat,lon,ra,dec){
    const h0=-0.8333 //For Sun
    //const h0=-0.5667 //For stars and planets
    //const h0=0.125   //For Moon

    const cosH=(Math.sin(h0*Math.PI/180.0)-Math.sin(lat)*Math.sin(dec)) / (Math.cos(lat)*Math.cos(dec));
    const H0=Math.acos(cosH)*180.0/Math.PI;

    const gmst=GMST(Math.floor(jd)+.5);

    const transit=(ra*toDeg+lon*toDeg-gmst)/360.0;
    const rise=transit-(H0/360.0);
    const set=transit+(H0/360.0);

    return [constrain(transit)*24.0,constrain(rise)*24.0,constrain(set)*24.0];
}

//Greenwhich mean sidreal time from Meeus page 87
//Input is julian date, does not have to be 0h
//Output is angle in degrees
function GMST(jd){
    const T=(jd-2451545.0)/36525.0;
    let st=280.46061837+360.98564736629*(jd-2451545.0)+0.000387933*T*T - T*T*T/38710000.0;
    st=st%360;
    if(st<0){st+=360;}

    return st;
    //return st*Math.PI/180.0;
}

```


---

