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

feat: Added getNight() to night module in @observerly/astrometry. #106

Merged
merged 1 commit into from
Sep 4, 2023
Merged
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
43 changes: 43 additions & 0 deletions src/night.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,46 @@ export const getSolarTransit = (
}

/*****************************************************************************************************************/

/**
*
* getNight()
*
* Calculates the start and end of the next night for the observer at the given date.
*
* @param date - The date to calculate the extent of the next night for.
* @param observer - The geographic coordinates of the observer.
* @param horizon - The horizon altitude to use for the calculation.
* @param temperature - The temperature to use for the calculation.
* @param pressure - The pressure to use for the calculation.
* @returns The start and end of the next night for the observer.
*
*/
export const getNight = (
datetime: Date,
observer: GeographicCoordinate,
horizon: number = -12,
temperature: number = 288.15,
pressure: number = 101325
): {
start: Date | null
end: Date | null
} => {
const { sunset } = getSolarTransit(datetime, observer, horizon, temperature, pressure)

const { sunrise } = getSolarTransit(
new Date(datetime.getTime() + 60000 * 60 * 24),
observer,
horizon,
temperature,
pressure
)

// The observer could be in perpetual daylight or perpetual night, e.g., the North Pole or South Pole:
return {
start: sunset,
end: sunrise
}
}

/*****************************************************************************************************************/
27 changes: 26 additions & 1 deletion tests/night.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { describe, expect, it } from 'vitest'

/*****************************************************************************************************************/

import { getSolarTransit } from '../src'
import { getSolarTransit, getNight } from '../src'

/*****************************************************************************************************************/

Expand Down Expand Up @@ -67,3 +67,28 @@ describe('getSolarTransit', () => {
})

/*****************************************************************************************************************/

describe('getNight', () => {
it('should be defined', () => {
expect(getNight).toBeDefined()
})

it('should return the correct night for the observer at a horizon of 0 degrees', () => {
const { start, end } = getNight(
datetime,
{
latitude,
longitude
},
0
)

expect(start).toBeInstanceOf(Date)
expect(end).toBeInstanceOf(Date)

expect(start?.toISOString()).toBe('2021-05-14T19:56:00.000Z')
expect(end?.toISOString()).toBe('2021-05-15T04:45:00.000Z')
})
})

/*****************************************************************************************************************/
Loading