Do not use -1 for sun calc error

master
Kenny Levinsen 2020-10-19 00:37:02 +02:00
parent c7c8dad01d
commit 5c497febb7
2 changed files with 37 additions and 55 deletions

View File

@ -45,8 +45,7 @@ static double sun_hour_angle(double latitude, double declination, double target_
static time_t hour_angle_to_time(double longitude, double eqtime, double hour_angle) { static time_t hour_angle_to_time(double longitude, double eqtime, double hour_angle) {
// https://www.esrl.noaa.gov/gmd/grad/solcalc/solareqns.PDF // https://www.esrl.noaa.gov/gmd/grad/solcalc/solareqns.PDF
return isnan(hour_angle) ? -1 : return DEGREES((4.0 * M_PI - 4 * (longitude + hour_angle) - eqtime) * 60);
DEGREES((4.0 * M_PI - 4 * (longitude + hour_angle) - eqtime) * 60);
} }
static enum sun_condition condition(double latitude_rad, double sun_declination) { static enum sun_condition condition(double latitude_rad, double sun_declination) {

89
main.c
View File

@ -290,47 +290,31 @@ static void set_temperature(struct wl_list *outputs, int temp, int gamma) {
} }
} }
static const char *sun_condition_str[] = { static void print_trajectory(struct context *ctx) {
"normal", fprintf(stderr, "calculated sun trajectory: ");
"midnight sun", struct tm dawn, sunrise, sunset, dusk;
"polar night", switch (ctx->condition) {
"invalid", case NORMAL:
NULL, localtime_r(&ctx->sun.dawn, &dawn);
}; localtime_r(&ctx->sun.sunrise, &sunrise);
localtime_r(&ctx->sun.sunset, &sunset);
static void print_trajectory(struct context *ctx, time_t day) { localtime_r(&ctx->sun.dusk, &dusk);
fprintf(stderr, "calculated sun trajectory:"); fprintf(stderr,
"dawn %02d:%02d, sunrise %02d:%02d, sunset %02d:%02d, dusk %02d:%02d\n",
struct tm tm; dawn.tm_hour, dawn.tm_min,
if (ctx->sun.dawn >= day) { sunrise.tm_hour, sunrise.tm_min,
localtime_r(&ctx->sun.dawn, &tm); sunset.tm_hour, sunset.tm_min,
fprintf(stderr, " dawn %02d:%02d,", tm.tm_hour, tm.tm_min); dusk.tm_hour, dusk.tm_min);
} else { break;
fprintf(stderr, " dawn N/A,"); case MIDNIGHT_SUN:
fprintf(stderr, "midnight sun\n");
return;
case POLAR_NIGHT:
fprintf(stderr, "polar night\n");
return;
default:
abort();
} }
if (ctx->sun.sunrise >= day) {
localtime_r(&ctx->sun.sunrise, &tm);
fprintf(stderr, " sunrise %02d:%02d,", tm.tm_hour, tm.tm_min);
} else {
fprintf(stderr, " sunrise N/A,");
}
if (ctx->sun.sunset >= day) {
localtime_r(&ctx->sun.sunset, &tm);
fprintf(stderr, " sunset %02d:%02d,", tm.tm_hour, tm.tm_min);
} else {
fprintf(stderr, " sunset N/A,");
}
if (ctx->sun.dusk >= day) {
localtime_r(&ctx->sun.dusk, &tm);
fprintf(stderr, " dusk %02d:%02d,", tm.tm_hour, tm.tm_min);
} else {
fprintf(stderr, " dusk N/A,");
}
fprintf(stderr, " condition: %s\n", sun_condition_str[ctx->condition]);
} }
static int anim_kelvin_step = 25; static int anim_kelvin_step = 25;
@ -350,12 +334,18 @@ static void recalc_stops(struct context *ctx, time_t now) {
switch (cond) { switch (cond) {
case NORMAL: case NORMAL:
ctx->state = STATE_NORMAL;
ctx->sun.dawn = sun.dawn + day;
ctx->sun.sunrise = sun.sunrise + day;
ctx->sun.sunset = sun.sunset + day;
ctx->sun.dusk = sun.dusk + day;
if (ctx->condition == MIDNIGHT_SUN) { if (ctx->condition == MIDNIGHT_SUN) {
// Yesterday had no sunset, so remove our sunrise. // Yesterday had no sunset, so remove our sunrise.
sun.dawn = -1; ctx->sun.dawn = day;
sun.sunrise = -1; ctx->sun.sunrise = day;
} }
ctx->state = STATE_NORMAL;
break; break;
case MIDNIGHT_SUN: case MIDNIGHT_SUN:
if (ctx->condition == POLAR_NIGHT) { if (ctx->condition == POLAR_NIGHT) {
@ -368,10 +358,8 @@ static void recalc_stops(struct context *ctx, time_t now) {
} }
// Borrow yesterday's sunrise to animate into the midnight sun // Borrow yesterday's sunrise to animate into the midnight sun
sun.dawn = sun.dawn != -1 ? sun.dawn : sun.dawn = ctx->sun.dawn - last_day + day;
ctx->sun.dawn - last_day; sun.sunrise = ctx->sun.sunrise - last_day + day;
sun.sunrise = sun.sunrise != -1 ? sun.sunrise :
ctx->sun.sunrise - last_day;
ctx->state = STATE_TRANSITION; ctx->state = STATE_TRANSITION;
break; break;
case POLAR_NIGHT: case POLAR_NIGHT:
@ -385,16 +373,11 @@ static void recalc_stops(struct context *ctx, time_t now) {
} }
ctx->condition = cond; ctx->condition = cond;
ctx->sun.dawn = sun.dawn + day;
ctx->sun.sunrise = sun.sunrise + day;
ctx->sun.sunset = sun.sunset + day;
ctx->sun.dusk = sun.dusk + day;
int temp_diff = ctx->config.high_temp - ctx->config.low_temp; int temp_diff = ctx->config.high_temp - ctx->config.low_temp;
ctx->dawn_step_time = (ctx->sun.sunrise - ctx->sun.dawn) * anim_kelvin_step / temp_diff; ctx->dawn_step_time = (ctx->sun.sunrise - ctx->sun.dawn) * anim_kelvin_step / temp_diff;
ctx->dusk_step_time = (ctx->sun.dusk - ctx->sun.sunset) * anim_kelvin_step / temp_diff; ctx->dusk_step_time = (ctx->sun.dusk - ctx->sun.sunset) * anim_kelvin_step / temp_diff;
print_trajectory(ctx, day); print_trajectory(ctx);
} }
static int interpolate_temperature(time_t now, time_t start, time_t stop, int temp_start, int temp_stop) { static int interpolate_temperature(time_t now, time_t start, time_t stop, int temp_start, int temp_stop) {