Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRiseAndSetAtSolarAngle method #135

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ Adds a custom time when the sun reaches the given angle to results returned by `

`SunCalc.times` property contains all currently defined times.

```javascript
SunCalc.getRiseAndSetAtSolarAngle(/*Date*/ timeAndDate, /*Number*/ angle, /*Number*/ latitude, /*Number*/ longitude, /*Number (default=0)*/ elevation)
```

Returns an object containing the times at which the sun will reach the given angle at the given
location and elevation. The object contains the following properties (each is a `Date` object):

| Property | Description |
| --------------- | ------------------------------------------------------------------------ |
| `rise` | The time at which the sun will reach the given angle when it is rising |
| `set` | The time at which the sun will reach the given angle when it is setting |

### Sun position

Expand Down
40 changes: 40 additions & 0 deletions suncalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,46 @@ SunCalc.getTimes = function (date, lat, lng, height) {
return result;
};

/**
* Get the time at which the sun will have a given apparent angle when rising and when setting.
*
* @param {Date} date The date to get the times for. Only the date part is important.
* @param {number} angle The angle of the sun relative to the Earth's horizon.
* @param {number} lat The latitude.
* @param {number} lng The longitude.
* @param {number} [elevation=0] The elevation of the observer in meters.
* @return {{set: Date, rise: Date}}
*/
SunCalc.getRiseAndSetAtSolarAngle = function (date, angle, lat, lng, elevation) {

elevation = elevation || 0;
angle = angle - astroRefraction(angle) * 100;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea why I need to multiply by 100?


var lw = rad * -lng,
phi = rad * lat,

dh = observerAngle(elevation),

d = toDays(date),
n = julianCycle(d, lw),
ds = approxTransit(0, lw, n),

M = solarMeanAnomaly(ds),
L = eclipticLongitude(M),
dec = declination(L, 0),

Jnoon = solarTransitJ(ds, M, L),
h0 = (angle + dh) * rad,

Jset = getSetJ(h0, lw, phi, dec, n, M, L),
Jrise = Jnoon - (Jset - Jnoon);

return {
rise: fromJulian(Jrise),
set: fromJulian(Jset),
};
};


// moon calculations, based on http://aa.quae.nl/en/reken/hemelpositie.html formulas

Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ t.test('getTimes adjusts sun phases when additionally given the observer height'
t.end();
});

t.test('getRiseAndSetAtSolarAngle returns the correct time for the given date and location', function (t) {
var times = SunCalc.getRiseAndSetAtSolarAngle(date, 0, lat, lng);

t.equal(new Date(times.rise).toString(), new Date(testTimes.sunrise).toString());
t.equal(times.set.toString(), new Date(testTimes.sunset).toString());
t.end();
});

t.test('getRiseAndSetAtAngle adjusts sun phases when additionally given the observer\'s elevation', function (t) {
var times = SunCalc.getRiseAndSetAtSolarAngle(date, 0, lat, lng, height);

t.equal(new Date(heightTestTimes.sunrise).toUTCString(), times.rise.toUTCString());
t.equal(new Date(heightTestTimes.sunset).toUTCString(), times.set.toUTCString());

t.end();
});

t.test('getMoonPosition returns moon position data given time and location', function (t) {
var moonPos = SunCalc.getMoonPosition(date, lat, lng);

Expand Down