# Sun elevation and moon phase

**URL:** <https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753>\
**Category:** Astro/Space\
**Tags:** question\
**Created:** [January 17, 2022, 11:06am UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753 "2022-01-17T11:06:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [January 17, 2022, 11:06am UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753/1 "2022-01-17T11:06:22Z")

</div>

I need to know what the:

1. sun elevation (and sunrise)
2. moon phase
3. moon elevation

is as a function of date, time, and coordinates.

I tried looking for an existing package in JuliaAstro, and it truly seems like all the pieces are there, but I couldn’t find anything that directly mentions any of this.

Does anyone know of a straight forward way to do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 17, 2022, 11:17am UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753/2 "2022-01-17T11:17:03Z")

</div>

> [@yakir12](#):
>
> but I couldn’t find anything that directly mentions any of this.

Really?

[http://juliaastro.github.io/AstroLib.jl/stable/ref/#Moon-and-sun](http://juliaastro.github.io/AstroLib.jl/stable/ref/#Moon-and-sun)

An example of putting everything together (for the Sun):

> **[Drawing the analemma with Julia](https://giordano.github.io/blog/2017-11-12-analemma/)**
>
> You may know that if you check the position of the Sun every day in the same place at the same time (accounting for daylight saving time if necessary), you’ll find that it slightly moves. This is a combination of the tilt of the Earth’s axis and the...

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 17, 2022, 2:21pm UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753/3 "2022-01-17T14:21:10Z")

</div>

> [@giordano](#):
>
> [Drawing the analemma with Julia](https://giordano.github.io/blog/2017-11-12-analemma/)

We learn something new here every day.

Linking a related scientific article on how to [_Build Your Own Analemma_](https://arxiv.org/pdf/1302.0765.pdf).

---

<div class="post-metadata">

**Author:** ![rbl100](https://avatars.discourse-cdn.com/v4/letter/r/94ad74/32.png) [@rbl100](https://discourse.julialang.org/u/rbl100)\
**Post date:** [December 24, 2023, 4:22pm UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753/4 "2023-12-24T16:22:54Z")

</div>

First, Merry Christmas. Second, I don’t see documentation for AstroLib.sunpos(). Is there any? Lastly, I’m doing something wrong in trying to get the azimuth and elevation of the sun. All pointers appreciated.  
“”"  
B5 = Observatory(“Bldg5”, 29.574166, -95.114444, 1, -6)  
jd=jdcnv(“2024-01-28T07:15:00”)  
ra,dec=sunpos(jd)  
az, el = eq2hor(ra, dec)  
“”"  
The values I get are nowhere near the values other sources have. Even if I use a 6 hour offset for my time vs UTC.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 24, 2023, 6:37pm UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753/5 "2023-12-24T18:37:08Z")

</div>

Just a curiosity: what is the day on which the curve intertwines?  
What characteristic does it have (same hours of dark and light or something like that)?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [December 25, 2023, 10:49am UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753/6 "2023-12-25T10:49:19Z")

</div>

You don’t supply details or expected output, but a quick test here (Julia v1.10rc, AstroLib v0.4.2) suggests it works OK.

```julia
using AstroLib
lat, lon = (51.477811, -0.001475)
jd = jdcnv("2023-12-25T10:27:00")
ra, dec = sunpos(jd)
alti, azi, ha = eq2hor(ra, dec, jd, lat, lon)

```

giving altitude and azimuth `(12.389058355400406, 157.92698445374288)`. This is close to [NOAA](https://gml.noaa.gov/grad/solcalc/)’s calculations:

 ![Screenshot 2023-12-25 at 10.38.25](https://global.discourse-cdn.com/julialang/original/3X/b/0/b0e59c928af8ee7f1556f453c46e047190776f02.png)

but I don’t know how accurate AstroLib’s results are expected to be - is `0.071` difference good or bad…

I think the documentation issue for `sunpos` has already been addressed by a [helpful contributor](https://github.com/JuliaAstro/AstroLib.jl/pull/81)! As it says in the README:

> Check [TODO.md](https://github.com/JuliaAstro/AstroLib.jl/blob/master/TODO.md) out to see how you can help. Volunteers are welcome!

But - see [here](https://discourse.julialang.org/t/calculate-positions-of-astronomical-objects-from-a-terrestrial-observatory/107772) for more recent developments.

Perhaps talk to the Julia/Astronomy folks?

---

<div class="post-metadata">

**Author:** ![CosmicSpittle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmicspittle/32/24028_2.png) [@CosmicSpittle](https://discourse.julialang.org/u/CosmicSpittle)\
**Post date:** [January 16, 2024, 11:28pm UTC](https://discourse.julialang.org/t/sun-elevation-and-moon-phase/74753/7 "2024-01-16T23:28:26Z")

</div>

Hi - I am also trying calculate the local sunrise/sunset/moonPhase using the functions in AstroLib.jl. I have the following example code.

I read the article linked in the solution to this question, but I’m not able to take it that final step for times (either a code problem or lack of understanding the process).

```julia
using AstroLib
using Dates

dateTime = DateTime("2024-01-13")
lat, lon = (38.88, -77.03)
jd = jdcnv(dateTime)
gmst = ct2lst(lon, -5, dateTime) # local sideral time in hours

## find local coords and hour angle (in degrees)
ra, dec = sunpos(jd) # get sun ra and dec
alt, az, ha = eq2hor(ra, dec, jd, lat, lon) # local alt, azi, and hour angle in degrees

## calculate transit
transit = ra/360 + lon/360 - gmst
if (transit < 0) transit += 24 end
transit_time = DateTime(year(dateTime), month(dateTime), day(dateTime), sixty(transit)[1], sixty(transit)[2])
# transit time is close, according to NOAA web site - 17:17 UTC (12:17 EST (local))

## Calculation of sunrise from transit and hour angle
sunrise = transit - ha/360

## convert to dateTime
sunrise_time = DateTime(year(dateTime), month(dateTime), day(dateTime), sixty(sunrise)[1], sixty(sunrise)[2])
## According to the NOAA calculator, sunrise should be at 12:26 UTC

```

It seems to have calculated the transit correctly (close to NOAA’s spreadsheet); however, I am not getting the sunrise to be even close (off by several hours). I assume I have botched units or a transform.

Any astronomy people out there familiar with AstroLib and might quickly see what is wrong?

Side question - Is Astrolib.jl considered fast/slow/similar compared to something like SunCalc? I typically need a few hundred days calculated at a time.

Thank you in advance!
