Files

91 lines
5.4 KiB
TypeScript

// 해돋이 지도 데이터·계산 (웹 frontend/src/b/sunrise.ts 가 쓴다). 지도 그림·배치 계산은 단풍 지도(shared/lib/foliage.ts)와 같이 쓴다
import data from '../../frontend/seo/sunrise_spots.json'
import { kst } from '.'
import { firstSnowInSeason } from './firstSnow'
import { foliageToday, toXY } from './foliage'
export type Coast = 'east' | 'south' | 'west' | 'inland'
type Raw = { name: string; coast: Coast; region: string; lat: number; lon: number; top?: boolean }
/** x·y 는 지도 그림 안 위치(0~1), rise·dawn 은 그날 0시부터 센 분(한국 시각), az 는 해 뜨는 방향(북 0° 시계 방향), top 은 대표 명소 */
export type SunSpot = { name: string; coast: Coast; region: string; top: boolean; x: number; y: number; rise: number; dawn: number; az: number }
/** next 는 다가오는 해돋이(아침 9시 전이면 오늘, 지나면 내일), newyear 는 1월 1일 */
export type SunDayKey = 'next' | 'newyear'
export type SunDay = { key: SunDayKey; date: string; label: string }
export const COAST: Record<Coast, string> = { east: '동해', south: '남해·제주', west: '서해', inland: '산·도심' }
// 해 뜨는 시각: 해 윗부분이 수평선에 닿는 때(대기 굴절 포함 -0.833°). 여명: 해 고도 -6°(시민박명), 이때부터 하늘이 밝아진다
const SUN_ALTITUDE = -0.833
const CIVIL_ALTITUDE = -6
const KST_MIN = 540
// 이 시각이 지나면 오늘 해돋이는 끝난 것으로 보고 내일 것을 보여 준다
const NEXT_FROM_HOUR = 9
const SEASON_MONTHS = [12, 1, 2]
const DOKDO = { lat: 37.2426, lon: 131.8672 }
const DIRS = ['북', '북동', '동', '남동', '남', '남서', '서', '북서']
const DAY = 86_400_000
const rad = (d: number) => (d * Math.PI) / 180
const deg = (r: number) => (r * 180) / Math.PI
/** NOAA 근사식(backend/services/calc/sun.py 와 같은 식). 태양 적위(라디안)와 남중 시각(UTC 분) */
function solar(iso: string, lon: number) {
const [y, m, d] = iso.split('-').map(Number)
const g = ((2 * Math.PI) / 365) * Math.round((Date.UTC(y, m - 1, d) - Date.UTC(y, 0, 1)) / DAY)
const eqtime = 229.18 * (0.000075 + 0.001868 * Math.cos(g) - 0.032077 * Math.sin(g) - 0.014615 * Math.cos(2 * g) - 0.040849 * Math.sin(2 * g))
const decl =
0.006918 - 0.399912 * Math.cos(g) + 0.070257 * Math.sin(g) - 0.006758 * Math.cos(2 * g) + 0.000907 * Math.sin(2 * g) - 0.002697 * Math.cos(3 * g) + 0.00148 * Math.sin(3 * g)
return { decl, noon: 720 - 4 * lon - eqtime }
}
/** 해가 그 고도에 있을 때부터 남중까지 걸리는 분 */
function untilNoon(lat: number, decl: number, altitude: number) {
const cosH = (Math.sin(rad(altitude)) - Math.sin(rad(lat)) * Math.sin(decl)) / (Math.cos(rad(lat)) * Math.cos(decl))
return deg(Math.acos(Math.max(-1, Math.min(1, cosH)))) * 4
}
/** 그날 그 자리의 해 뜨는 시각·여명 시작(0시부터 센 분, 한국 시각)과 해 뜨는 방향. 해발 0m 기준 */
export function sunriseAt(iso: string, lat: number, lon: number) {
const { decl, noon } = solar(iso, lon)
return {
rise: noon - untilNoon(lat, decl, SUN_ALTITUDE) + KST_MIN,
dawn: noon - untilNoon(lat, decl, CIVIL_ALTITUDE) + KST_MIN,
az: Math.round(deg(Math.acos(Math.max(-1, Math.min(1, Math.sin(decl) / Math.cos(rad(lat))))))),
}
}
/** 0시부터 센 분 → '7:31' */
export function clock(min: number) {
const m = Math.round(min)
return `${Math.floor(m / 60)}:${String(m % 60).padStart(2, '0')}`
}
/** 118° → '남동' */
export const direction = (az: number) => DIRS[Math.round(az / 45) % 8]
/** 고를 수 있는 날: 12월엔 새해 첫날이 먼저, 그다음 다가오는 해돋이. 다가오는 해돋이가 1월 1일이면 새해 첫날 하나만 */
export function sunriseDays(now: Date = new Date()): SunDay[] {
const t = kst(now)
const today = t.h < NEXT_FROM_HOUR
const date = new Date(Date.UTC(t.y, t.mo - 1, t.d + (today ? 0 : 1))).toISOString().slice(0, 10)
if (date.endsWith('-01-01')) return [{ key: 'newyear', date, label: '새해 첫날' }]
const next: SunDay = { key: 'next', date, label: today ? '오늘' : '내일' }
return t.mo === 12 ? [{ key: 'newyear', date: `${t.y + 1}-01-01`, label: '새해 첫날' }, next] : [next]
}
/** 지금이 해돋이철인지 (오늘 아이콘 줄의 해돋이 지도 칸·피드 해돋이 카드를 보일지). 12~2월 가운데 첫눈 지도가 끝난 다음 날부터: 같은 자리를 첫눈 지도가 먼저 쓴다 */
export const sunriseInSeason = (now: Date = new Date()) => SEASON_MONTHS.includes(kst(now).mo) && !firstSnowInSeason(foliageToday(now))
/** 그날 명소별 해 뜨는 시각, 이른 순(같으면 이름 순) */
export function sunriseOn(date: string): SunSpot[] {
return (data.spots as Raw[])
.map(({ name, coast, region, lat, lon, top }) => ({ name, coast, region, top: !!top, ...toXY(lat, lon), ...sunriseAt(date, lat, lon) }))
.sort((a, b) => Math.round(a.rise) - Math.round(b.rise) || a.name.localeCompare(b.name))
}
/** 우리나라에서 해가 가장 먼저 뜨는 독도의 그날 해 뜨는 시각 (지도 그림 밖이라 명소 목록에는 없다) */
export const dokdoRise = (date: string) => sunriseAt(date, DOKDO.lat, DOKDO.lon).rise
/** '새해 첫 해는 간절곶 7:31부터' — spots 는 이른 순 */
export const sunriseHeadline = (day: SunDay, spots: SunSpot[]) => `${day.key === 'newyear' ? '새해' : day.label} 첫 해는 ${spots[0].name} ${clock(spots[0].rise)}부터`