Skip to content

Commit

Permalink
[api][desktop]: Fix Sky Atlas manual date & time ephemeris
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Aug 22, 2024
1 parent 092d803 commit ba84104
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
24 changes: 12 additions & 12 deletions api/src/main/kotlin/nebulosa/api/atlas/SkyAtlasController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ class SkyAtlasController(
@GetMapping("sun/altitude-points")
fun altitudePointsOfSun(
@LocationParam location: Location,
@DateAndTimeParam date: LocalDate,
@DateAndTimeParam dateTime: LocalDateTime,
@RequestParam(required = false, defaultValue = "1") @Valid @Min(1) stepSize: Int,
@RequestParam(required = false, defaultValue = "false") fast: Boolean,
) = skyAtlasService.altitudePointsOfSun(location, date, stepSize, fast)
) = skyAtlasService.altitudePointsOfSun(location, dateTime, stepSize, fast)

@GetMapping("moon/position")
fun positionOfMoon(
Expand All @@ -53,10 +53,10 @@ class SkyAtlasController(
@GetMapping("moon/altitude-points")
fun altitudePointsOfMoon(
@LocationParam location: Location,
@DateAndTimeParam date: LocalDate,
@DateAndTimeParam dateTime: LocalDateTime,
@RequestParam(required = false, defaultValue = "1") @Valid @Min(1) stepSize: Int,
@RequestParam(required = false, defaultValue = "false") fast: Boolean,
) = skyAtlasService.altitudePointsOfMoon(location, date, stepSize, fast)
) = skyAtlasService.altitudePointsOfMoon(location, dateTime, stepSize, fast)

@GetMapping("planets/{code}/position")
fun positionOfPlanet(
Expand All @@ -70,10 +70,10 @@ class SkyAtlasController(
fun altitudePointsOfPlanet(
@PathVariable code: String,
@LocationParam location: Location,
@DateAndTimeParam date: LocalDate,
@DateAndTimeParam dateTime: LocalDateTime,
@RequestParam(required = false, defaultValue = "1") @Valid @Min(1) stepSize: Int,
@RequestParam(required = false, defaultValue = "false") fast: Boolean,
) = skyAtlasService.altitudePointsOfPlanet(location, code, date, stepSize, fast)
) = skyAtlasService.altitudePointsOfPlanet(location, code, dateTime, stepSize, fast)

@GetMapping("minor-planets")
fun searchMinorPlanet(@RequestParam @Valid @NotBlank text: String) = skyAtlasService.searchMinorPlanet(text)
Expand All @@ -96,9 +96,9 @@ class SkyAtlasController(
fun altitudePointsOfSkyObject(
@PathVariable id: Long,
@LocationParam location: Location,
@DateAndTimeParam date: LocalDate,
@DateAndTimeParam dateTime: LocalDateTime,
@RequestParam(required = false, defaultValue = "1") @Valid @Min(1) stepSize: Int,
) = skyAtlasService.altitudePointsOfSkyObject(location, id, date, stepSize)
) = skyAtlasService.altitudePointsOfSkyObject(location, id, dateTime, stepSize)

@GetMapping("sky-objects")
fun searchSkyObject(
Expand Down Expand Up @@ -130,9 +130,9 @@ class SkyAtlasController(
fun altitudePointsOfSatellite(
satellite: SatelliteEntity,
@LocationParam location: Location,
@DateAndTimeParam date: LocalDate,
@DateAndTimeParam dateTime: LocalDateTime,
@RequestParam(required = false, defaultValue = "1") @Valid @Min(1) stepSize: Int,
) = skyAtlasService.altitudePointsOfSatellite(location, satellite, date, stepSize)
) = skyAtlasService.altitudePointsOfSatellite(location, satellite, dateTime, stepSize)

@GetMapping("satellites")
fun searchSatellites(
Expand All @@ -144,9 +144,9 @@ class SkyAtlasController(
@GetMapping("twilight")
fun twilight(
@LocationParam location: Location,
@DateAndTimeParam date: LocalDate,
@DateAndTimeParam dateTime: LocalDateTime,
@RequestParam(required = false, defaultValue = "false") fast: Boolean,
) = skyAtlasService.twilight(location, date, fast)
) = skyAtlasService.twilight(location, dateTime, fast)

@GetMapping("moon/phase")
fun moonPhase(
Expand Down
24 changes: 12 additions & 12 deletions api/src/main/kotlin/nebulosa/api/atlas/SkyAtlasService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class SkyAtlasService(
return satelliteRepository.search(text.ifBlank { null }, groups, id)
}

fun twilight(location: GeographicCoordinate, date: LocalDate, fast: Boolean = false): Twilight {
fun twilight(location: GeographicCoordinate, dateTime: LocalDateTime, fast: Boolean = false): Twilight {
val civilDusk = doubleArrayOf(0.0, 0.0)
val nauticalDusk = doubleArrayOf(0.0, 0.0)
val astronomicalDusk = doubleArrayOf(0.0, 0.0)
Expand All @@ -133,7 +133,7 @@ class SkyAtlasService(
val nauticalDawn = doubleArrayOf(0.0, 0.0)
val civilDawn = doubleArrayOf(0.0, 0.0)

val ephemeris = bodyEphemeris(if (fast) VSOP87E.SUN else SUN, location, LocalDateTime.of(date, LocalTime.now(SystemClock)), true)
val ephemeris = bodyEphemeris(if (fast) VSOP87E.SUN else SUN, location, dateTime, true)
val range = evenlySpacedNumbers(0.0, (ephemeris.size - 1).toDouble(), ephemeris.size)
val result = findDiscrete(range, TwilightDiscreteFunction(ephemeris), 1.0)

Expand All @@ -158,35 +158,35 @@ class SkyAtlasService(
)
}

fun altitudePointsOfSun(location: GeographicCoordinate, date: LocalDate, stepSize: Int, fast: Boolean = false): List<DoubleArray> {
val ephemeris = bodyEphemeris(if (fast) VSOP87E.SUN else SUN, location, LocalDateTime.of(date, LocalTime.now(SystemClock)), true)
fun altitudePointsOfSun(location: GeographicCoordinate, dateTime: LocalDateTime, stepSize: Int, fast: Boolean = false): List<DoubleArray> {
val ephemeris = bodyEphemeris(if (fast) VSOP87E.SUN else SUN, location, dateTime, true)
return altitudePointsOfBody(ephemeris, stepSize)
}

fun altitudePointsOfMoon(location: GeographicCoordinate, date: LocalDate, stepSize: Int, fast: Boolean = false): List<DoubleArray> {
val ephemeris = bodyEphemeris(if (fast) FAST_MOON else MOON, location, LocalDateTime.of(date, LocalTime.now(SystemClock)), true)
fun altitudePointsOfMoon(location: GeographicCoordinate, dateTime: LocalDateTime, stepSize: Int, fast: Boolean = false): List<DoubleArray> {
val ephemeris = bodyEphemeris(if (fast) FAST_MOON else MOON, location, dateTime, true)
return altitudePointsOfBody(ephemeris, stepSize)
}

fun altitudePointsOfPlanet(
location: GeographicCoordinate, code: String, date: LocalDate,
location: GeographicCoordinate, code: String, dateTime: LocalDateTime,
stepSize: Int, fast: Boolean = false
): List<DoubleArray> {
val target: Any = VSOP87E.entries.takeIf { fast }?.find { "${it.target}" == code } ?: code
val ephemeris = bodyEphemeris(target, location, LocalDateTime.of(date, LocalTime.now(SystemClock)), true)
val ephemeris = bodyEphemeris(target, location, dateTime, true)
return altitudePointsOfBody(ephemeris, stepSize)
}

fun altitudePointsOfSkyObject(location: GeographicCoordinate, id: Long, date: LocalDate, stepSize: Int): List<DoubleArray> {
fun altitudePointsOfSkyObject(location: GeographicCoordinate, id: Long, dateTime: LocalDateTime, stepSize: Int): List<DoubleArray> {
val target = cachedSimbadEntities[id] ?: simbadEntityRepository.find(id)
?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "Cannot found sky object: [$id]")
cachedSimbadEntities[id] = target
val ephemeris = bodyEphemeris(target, location, LocalDateTime.of(date, LocalTime.now(SystemClock)), true)
val ephemeris = bodyEphemeris(target, location, dateTime, true)
return altitudePointsOfBody(ephemeris, stepSize)
}

fun altitudePointsOfSatellite(location: GeographicCoordinate, satellite: SatelliteEntity, date: LocalDate, stepSize: Int): List<DoubleArray> {
val ephemeris = bodyEphemeris("TLE@${satellite.tle}", location, LocalDateTime.of(date, LocalTime.now(SystemClock)), true)
fun altitudePointsOfSatellite(location: GeographicCoordinate, satellite: SatelliteEntity, dateTime: LocalDateTime, stepSize: Int): List<DoubleArray> {
val ephemeris = bodyEphemeris("TLE@${satellite.tle}", location, dateTime, true)
return altitudePointsOfBody(ephemeris, stepSize)
}

Expand Down
4 changes: 2 additions & 2 deletions desktop/src/app/atlas/atlas.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,8 @@
<div class="col-7 flex-column gap-2">
<p-calendar
[disabled]="refreshing || !dateTimeAndLocation.manual"
[(ngModel)]="dateTimeAndLocation.dateTime"
(ngModelChange)="dateTimeChanged(true)"
[ngModel]="dateTimeAndLocation.dateTime"
(ngModelChange)="dateTimeChanged(true, $event)"
[inline]="true"
[showIcon]="true"
[showWeek]="true"
Expand Down
10 changes: 8 additions & 2 deletions desktop/src/app/atlas/atlas.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,13 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit,
}
}

protected async dateTimeChanged(dateChanged: boolean) {
protected async dateTimeChanged(dateChanged: boolean, date?: Date) {
if (date) {
this.dateTimeAndLocation.dateTime.setFullYear(date.getFullYear())
this.dateTimeAndLocation.dateTime.setMonth(date.getMonth())
this.dateTimeAndLocation.dateTime.setDate(date.getDate())
}

this.savePreference()
await this.refreshTab(dateChanged, true)
}
Expand Down Expand Up @@ -689,7 +695,7 @@ export class AtlasComponent implements OnInit, AfterContentInit, AfterViewInit,
}
// Moon.
else if (this.tab === BodyTabType.MOON) {
void this.api.moonPhase(dateTime).then((res) => this.moon.phase = res)
void this.api.moonPhase(dateTime).then((res) => (this.moon.phase = res))
const position = await this.api.positionOfMoon(dateTime, location)
Object.assign(this.moon.position, position)
}
Expand Down
20 changes: 10 additions & 10 deletions desktop/src/shared/services/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,8 @@ export class ApiService {
}

altitudePointsOfSun(dateTime: Date, location?: Location, fast: boolean = false) {
const date = extractDate(dateTime)
const query = this.http.query({ date, fast, hasLocation: location?.id || true })
const [date, time] = extractDateTime(dateTime)
const query = this.http.query({ date, time, fast, hasLocation: location?.id || true })
return this.http.get<[number, number][]>(`sky-atlas/sun/altitude-points?${query}`)
}

Expand All @@ -464,8 +464,8 @@ export class ApiService {
}

altitudePointsOfMoon(dateTime: Date, location?: Location, fast: boolean = false) {
const date = extractDate(dateTime)
const query = this.http.query({ date, fast, hasLocation: location?.id || true })
const [date, time] = extractDateTime(dateTime)
const query = this.http.query({ date, time, fast, hasLocation: location?.id || true })
return this.http.get<[number, number][]>(`sky-atlas/moon/altitude-points?${query}`)
}

Expand All @@ -476,8 +476,8 @@ export class ApiService {
}

altitudePointsOfPlanet(code: string, dateTime: Date, location?: Location, fast: boolean = false) {
const date = extractDate(dateTime)
const query = this.http.query({ date, fast, hasLocation: location?.id || true })
const [date, time] = extractDateTime(dateTime)
const query = this.http.query({ date, time, fast, hasLocation: location?.id || true })
return this.http.get<[number, number][]>(`sky-atlas/planets/${encodeURIComponent(code)}/altitude-points?${query}`)
}

Expand All @@ -488,8 +488,8 @@ export class ApiService {
}

altitudePointsOfSkyObject(simbad: DeepSkyObject, dateTime: Date, location?: Location) {
const date = extractDate(dateTime)
const query = this.http.query({ date, hasLocation: location?.id || true })
const [date, time] = extractDateTime(dateTime)
const query = this.http.query({ date, time, hasLocation: location?.id || true })
return this.http.get<[number, number][]>(`sky-atlas/sky-objects/${simbad.id}/altitude-points?${query}`)
}

Expand All @@ -511,8 +511,8 @@ export class ApiService {
}

altitudePointsOfSatellite(satellite: Satellite, dateTime: Date, location?: Location) {
const date = extractDate(dateTime)
const query = this.http.query({ date, hasLocation: location?.id || true })
const [date, time] = extractDateTime(dateTime)
const query = this.http.query({ date, time, hasLocation: location?.id || true })
return this.http.get<[number, number][]>(`sky-atlas/satellites/${satellite.id}/altitude-points?${query}`)
}

Expand Down

0 comments on commit ba84104

Please sign in to comment.