solunatus 0.4.0

High-precision astronomical calculation library and CLI for sun/moon positions, rise/set times, and lunar phases
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/// Optimized calendar generation with parallelization and batch processing
///
/// Key optimizations:
/// 1. Rayon parallelization: Process multiple days in parallel across P-cores
/// 2. Batch moonrise/moonset: Use moon_batch_optimized for 3-4x faster event calculation
/// 3. Reduced DateTime clones: Create once, reuse throughout
/// 4. Preallocated buffers: Reduce memory allocations
use crate::astro::{moon, moon_batch_optimized, sun, Location};
use anyhow::{anyhow, Context, Result};
use chrono::{Datelike, Duration, NaiveDate, TimeZone, Utc, Weekday};
use chrono_tz::Tz;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use serde::Serialize;
use std::collections::BTreeMap;

const MIN_YEAR: i32 = -999;
const MAX_YEAR: i32 = 3000;

// Rayon thread pool configuration for calendar generation
#[cfg(feature = "parallel")]
const CALENDAR_CHUNK_SIZE: usize = 365; // Process 1 year per thread

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CalendarFormat {
    Html,
    Json,
}

#[derive(Debug, Clone)]
struct DailyRecord {
    date: NaiveDate,
    weekday: Weekday,
    sunrise: Option<chrono::DateTime<Tz>>,
    sunset: Option<chrono::DateTime<Tz>>,
    solar_noon: Option<chrono::DateTime<Tz>>,
    civil_dawn: Option<chrono::DateTime<Tz>>,
    civil_dusk: Option<chrono::DateTime<Tz>>,
    moonrise: Option<chrono::DateTime<Tz>>,
    moonset: Option<chrono::DateTime<Tz>>,
    illumination: f64,
    phase_angle: f64,
    phase_name: String,
    phase_emoji: String,
}

#[derive(Debug, Serialize)]
struct CalendarMetadata<'a> {
    latitude: f64,
    longitude: f64,
    timezone: String,
    city: Option<&'a str>,
    range_start: String,
    range_end: String,
    generated_at_utc: String,
}

#[derive(Debug, Serialize)]
struct CalendarDayJson {
    date: String,
    weekday: String,
    solar: SolarBlockJson,
    lunar: LunarBlockJson,
}

#[derive(Debug, Serialize)]
struct SolarBlockJson {
    sunrise: Option<String>,
    sunset: Option<String>,
    solar_noon: Option<String>,
    civil_dawn: Option<String>,
    civil_dusk: Option<String>,
    day_length_minutes: Option<f64>,
}

#[derive(Debug, Serialize)]
struct LunarBlockJson {
    moonrise: Option<String>,
    moonset: Option<String>,
    illumination_percent: f64,
    phase_angle_degrees: f64,
    phase_name: String,
    phase_emoji: String,
}

#[derive(Debug, Serialize)]
struct CalendarJson<'a> {
    metadata: CalendarMetadata<'a>,
    days: Vec<CalendarDayJson>,
}

/// Generate optimized calendar with parallelization
pub fn generate_calendar_optimized(
    location: &Location,
    timezone: &Tz,
    city_name: Option<&str>,
    start: NaiveDate,
    end: NaiveDate,
    format: CalendarFormat,
) -> Result<String> {
    validate_range(start, end)?;

    // Phase 1: Parallel collection of daily records
    let records = collect_records_parallel(location, timezone, start, end)?;

    match format {
        CalendarFormat::Html => Ok(render_html(
            location, timezone, city_name, start, end, &records,
        )),
        CalendarFormat::Json => render_json(location, timezone, city_name, start, end, &records),
    }
}

fn validate_range(start: NaiveDate, end: NaiveDate) -> Result<()> {
    if start > end {
        return Err(anyhow!("Calendar start date must be before end date"));
    }

    let start_year = start.year();
    let end_year = end.year();

    if start_year < MIN_YEAR || end_year > MAX_YEAR {
        return Err(anyhow!(
            "Calendar range must fall between astronomical years {} (1000 BCE) and {} (3000 CE)",
            MIN_YEAR,
            MAX_YEAR
        ));
    }
    Ok(())
}

/// Parallel collection of daily records using rayon
///
/// Strategy: Divide date range into chunks, process each chunk in parallel,
/// then sort by date to maintain chronological order.
#[cfg(feature = "parallel")]
fn collect_records_parallel(
    location: &Location,
    timezone: &Tz,
    start: NaiveDate,
    end: NaiveDate,
) -> Result<Vec<DailyRecord>> {
    let total_days = (end - start).num_days() as usize + 1;

    // Create chunks of dates (one chunk per logical work unit)
    let chunks: Vec<(NaiveDate, NaiveDate)> = (0..=(total_days / CALENDAR_CHUNK_SIZE))
        .map(|chunk_idx| {
            let chunk_start = start + Duration::days((chunk_idx * CALENDAR_CHUNK_SIZE) as i64);
            let chunk_end = if chunk_idx == total_days / CALENDAR_CHUNK_SIZE {
                end
            } else {
                chunk_start + Duration::days((CALENDAR_CHUNK_SIZE - 1) as i64)
            };
            (chunk_start, chunk_end.min(end))
        })
        .collect();

    // Process chunks in parallel
    let chunk_results: Result<Vec<Vec<DailyRecord>>> = chunks
        .into_par_iter()
        .map(|(chunk_start, chunk_end)| {
            collect_records_sequential(location, timezone, chunk_start, chunk_end)
        })
        .collect();

    // Flatten and sort by date (should already be sorted, but ensure it)
    let mut records = chunk_results?
        .into_iter()
        .flat_map(|v| v.into_iter())
        .collect::<Vec<_>>();

    records.sort_by_key(|r| r.date);

    Ok(records)
}

/// Sequential collection (fallback when parallel feature is disabled)
#[cfg(not(feature = "parallel"))]
fn collect_records_parallel(
    location: &Location,
    timezone: &Tz,
    start: NaiveDate,
    end: NaiveDate,
) -> Result<Vec<DailyRecord>> {
    // Without parallel feature, just use sequential processing
    collect_records_sequential(location, timezone, start, end)
}

/// Sequential collection for a chunk of dates
fn collect_records_sequential(
    location: &Location,
    timezone: &Tz,
    start: NaiveDate,
    end: NaiveDate,
) -> Result<Vec<DailyRecord>> {
    let mut records = Vec::new();
    let mut current = start;

    while current <= end {
        let record = build_record_optimized(location, timezone, current)
            .with_context(|| format!("Failed to compute ephemerides for {}", current))?;
        records.push(record);
        current = current
            .checked_add_signed(Duration::days(1))
            .ok_or_else(|| anyhow!("Date overflow when iterating calendar range"))?;
    }

    Ok(records)
}

/// Build a daily record with optimization: batch moonrise/moonset
#[inline]
fn build_record_optimized(
    location: &Location,
    timezone: &Tz,
    date: NaiveDate,
) -> Result<DailyRecord> {
    let local_midday = resolve_midday(timezone, date)?;

    // Solar events (unchanged - these are fast enough)
    let sunrise = sun::solar_event_time(location, &local_midday, sun::SolarEvent::Sunrise);
    let sunset = sun::solar_event_time(location, &local_midday, sun::SolarEvent::Sunset);
    let solar_noon = sun::solar_event_time(location, &local_midday, sun::SolarEvent::SolarNoon);
    let civil_dawn = sun::solar_event_time(location, &local_midday, sun::SolarEvent::CivilDawn);
    let civil_dusk = sun::solar_event_time(location, &local_midday, sun::SolarEvent::CivilDusk);

    // Lunar events: Use batch optimization for moonrise + moonset
    // This is the critical path - moonrise/moonset are expensive
    let threshold = -0.834;
    let batch_result =
        moon_batch_optimized::batch_search_rise_and_set(location, &local_midday, threshold);
    let moonrise = batch_result.moonrise;
    let moonset = batch_result.moonset;

    // Lunar position
    let lunar_position = moon::lunar_position(location, &local_midday);

    Ok(DailyRecord {
        date,
        weekday: local_midday.weekday(),
        sunrise,
        sunset,
        solar_noon,
        civil_dawn,
        civil_dusk,
        moonrise,
        moonset,
        illumination: lunar_position.illumination,
        phase_angle: lunar_position.phase_angle,
        phase_name: moon::phase_name(lunar_position.phase_angle).to_string(),
        phase_emoji: moon::phase_emoji(lunar_position.phase_angle).to_string(),
    })
}

fn resolve_midday(timezone: &Tz, date: NaiveDate) -> Result<chrono::DateTime<Tz>> {
    match timezone.with_ymd_and_hms(date.year(), date.month(), date.day(), 12, 0, 0) {
        chrono::LocalResult::Single(dt) => Ok(dt),
        chrono::LocalResult::Ambiguous(first, _second) => Ok(first),
        chrono::LocalResult::None => timezone
            .with_ymd_and_hms(date.year(), date.month(), date.day(), 13, 0, 0)
            .single()
            .ok_or_else(|| anyhow!("Unable to resolve local midday for {}", date)),
    }
}

fn render_json(
    location: &Location,
    timezone: &Tz,
    city_name: Option<&str>,
    start: NaiveDate,
    end: NaiveDate,
    records: &[DailyRecord],
) -> Result<String> {
    let metadata = CalendarMetadata {
        latitude: location.latitude.value(),
        longitude: location.longitude.value(),
        timezone: timezone.name().to_string(),
        city: city_name,
        range_start: start.to_string(),
        range_end: end.to_string(),
        generated_at_utc: Utc::now().format("%Y-%m-%d %H:%M:%S UTC").to_string(),
    };

    let days = records
        .iter()
        .map(|record| CalendarDayJson {
            date: record.date.to_string(),
            weekday: record.weekday.to_string(),
            solar: SolarBlockJson {
                sunrise: record.sunrise.map(format_time),
                sunset: record.sunset.map(format_time),
                solar_noon: record.solar_noon.map(format_time),
                civil_dawn: record.civil_dawn.map(format_time),
                civil_dusk: record.civil_dusk.map(format_time),
                day_length_minutes: day_length_minutes(record),
            },
            lunar: LunarBlockJson {
                moonrise: record.moonrise.map(format_time),
                moonset: record.moonset.map(format_time),
                illumination_percent: (record.illumination * 100.0 * 10.0).round() / 10.0,
                phase_angle_degrees: (record.phase_angle * 10.0).round() / 10.0,
                phase_name: record.phase_name.clone(),
                phase_emoji: record.phase_emoji.clone(),
            },
        })
        .collect();

    Ok(serde_json::to_string_pretty(&CalendarJson {
        metadata,
        days,
    })?)
}

fn render_html(
    location: &Location,
    timezone: &Tz,
    city_name: Option<&str>,
    start: NaiveDate,
    end: NaiveDate,
    records: &[DailyRecord],
) -> String {
    let mut by_month: BTreeMap<(i32, u32), Vec<&DailyRecord>> = BTreeMap::new();
    for record in records {
        by_month
            .entry((record.date.year(), record.date.month()))
            .or_default()
            .push(record);
    }

    let mut html = String::with_capacity(1024 * 100); // Pre-allocate for efficiency
    html.push_str("<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"/>");
    html.push_str("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>");
    html.push_str("<title>Solunatus Calendar</title>");
    html.push_str("<style>");
    html.push_str("body{font-family:'Helvetica Neue',Arial,sans-serif;background:#0d1117;color:#e6edf3;margin:0;padding:0 0 3rem 0;}");
    html.push_str("header{padding:2.5rem 1.5rem 1.5rem;background:linear-gradient(135deg,#1f6feb,#8b949e);color:#fff;}");
    html.push_str("header h1{margin:0;font-size:2.2rem;font-weight:700;}");
    html.push_str(".meta{margin-top:0.5rem;font-size:1rem;opacity:0.9;}");
    html.push_str(".container{max-width:1200px;margin:0 auto;padding:1.5rem;}");
    html.push_str("h2{margin:2rem 0 0.75rem;font-size:1.6rem;color:#58a6ff;}");
    html.push_str("table{width:100%;border-collapse:collapse;background:#161b22;border-radius:0.75rem;overflow:hidden;box-shadow:0 20px 45px rgba(0,0,0,0.35);}");
    html.push_str(
        "thead{background:rgba(88,166,255,0.12);text-transform:uppercase;letter-spacing:0.05em;}",
    );
    html.push_str("th,td{padding:0.9rem 1rem;text-align:left;white-space:nowrap;}");
    html.push_str("tbody tr:nth-child(even){background:rgba(88,166,255,0.04);}");
    html.push_str("tbody tr:hover{background:rgba(88,166,255,0.14);}");
    html.push_str(".date{font-weight:600;font-size:1rem;}");
    html.push_str(".weekday{font-size:0.9rem;opacity:0.75;display:block;margin-top:0.25rem;text-transform:uppercase;}");
    html.push_str(".emoji{font-size:1.2rem;margin-right:0.5rem;}");
    html.push_str(".illum{font-size:0.9rem;opacity:0.8;}");
    html.push_str(".badge{display:inline-block;padding:0.25rem 0.65rem;border-radius:999px;font-size:0.8rem;background:rgba(88,166,255,0.2);color:#58a6ff;margin-left:0.4rem;}");
    html.push_str(".location{font-size:1rem;margin-top:0.25rem;}");
    html.push_str(".footer-note{margin-top:3rem;font-size:0.85rem;opacity:0.7;text-align:center;}");
    html.push_str("</style></head><body>");

    html.push_str("<header>");
    html.push_str("<h1>Solunatus Astronomical Calendar</h1>");
    html.push_str("<div class=\"meta\">");
    if let Some(city) = city_name {
        html.push_str(&escape_html(city));
        html.push_str("");
    }
    html.push_str(&format!(
        "{}{}{}",
        format_lat(location.latitude.value()),
        format_lon(location.longitude.value()),
        timezone.name()
    ));
    html.push_str("</div>");
    html.push_str("<div class=\"meta\">");
    html.push_str(&format!(
        "Range: {}{} • Generated {}",
        start,
        end,
        Utc::now().format("%Y-%m-%d %H:%M UTC")
    ));
    html.push_str("</div>");
    html.push_str("</header>");

    html.push_str("<div class=\"container\">");

    for ((year, month), days) in by_month {
        let month_name = month_name(month);
        html.push_str(&format!("<h2>{} {}</h2>", month_name, year));
        html.push_str("<table><thead><tr>");
        html.push_str("<th>Date</th><th>Sunrise</th><th>Sunset</th><th>Daylight</th>");
        html.push_str("<th>Civil Dawn</th><th>Civil Dusk</th>");
        html.push_str("<th>Moonrise</th><th>Moonset</th><th>Lunar Phase</th>");
        html.push_str("</tr></thead><tbody>");

        for record in days {
            html.push_str("<tr>");

            html.push_str("<td class=\"date\">");
            html.push_str(&format!(
                "{} <span class=\"weekday\">{}</span>",
                record.date.format("%b %d"),
                record.weekday
            ));
            html.push_str("</td>");

            html.push_str(&format!(
                "<td>{}</td>",
                record.sunrise.map_or("".to_string(), format_time)
            ));
            html.push_str(&format!(
                "<td>{}</td>",
                record.sunset.map_or("".to_string(), format_time)
            ));
            html.push_str(&format!(
                "<td>{}</td>",
                day_length_minutes(record)
                    .map(|mins| format!(
                        "{:02} h {:02} m",
                        (mins / 60.0).floor() as i64,
                        (mins % 60.0).round() as i64
                    ))
                    .unwrap_or_else(|| "".to_string())
            ));
            html.push_str(&format!(
                "<td>{}</td>",
                record.civil_dawn.map_or("".to_string(), format_time)
            ));
            html.push_str(&format!(
                "<td>{}</td>",
                record.civil_dusk.map_or("".to_string(), format_time)
            ));
            html.push_str(&format!(
                "<td>{}</td>",
                record.moonrise.map_or("".to_string(), format_time)
            ));
            html.push_str(&format!(
                "<td>{}</td>",
                record.moonset.map_or("".to_string(), format_time)
            ));
            let illum = (record.illumination * 1000.0).round() / 10.0;
            html.push_str("<td>");
            html.push_str(&format!(
                "<span class=\"emoji\">{}</span>{}<span class=\"badge\">{:.1}%</span>",
                record.phase_emoji,
                escape_html(&record.phase_name),
                illum
            ));
            html.push_str("</td>");

            html.push_str("</tr>");
        }

        html.push_str("</tbody></table>");
    }

    html.push_str("<div class=\"footer-note\">Times shown in local apparent clock time. Daylight savings and high-latitude edge cases are handled gracefully; missing events are marked with an em dash.</div>");
    html.push_str("</div></body></html>");

    html
}

fn format_time(dt: chrono::DateTime<Tz>) -> String {
    dt.format("%H:%M").to_string()
}

fn day_length_minutes(record: &DailyRecord) -> Option<f64> {
    match (record.sunrise, record.sunset) {
        (Some(rise), Some(set)) => {
            let diff = set.signed_duration_since(rise);
            if diff.num_seconds() >= 0 {
                Some(diff.num_seconds() as f64 / 60.0)
            } else {
                None
            }
        }
        _ => None,
    }
}

fn month_name(month: u32) -> &'static str {
    match month {
        1 => "January",
        2 => "February",
        3 => "March",
        4 => "April",
        5 => "May",
        6 => "June",
        7 => "July",
        8 => "August",
        9 => "September",
        10 => "October",
        11 => "November",
        12 => "December",
        _ => "Unknown",
    }
}

fn escape_html(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&#39;")
}

fn format_lat(lat: f64) -> String {
    if lat >= 0.0 {
        format!("{:.4}° N", lat)
    } else {
        format!("{:.4}° S", -lat)
    }
}

fn format_lon(lon: f64) -> String {
    if lon >= 0.0 {
        format!("{:.4}° E", lon)
    } else {
        format!("{:.4}° W", -lon)
    }
}