| SOCR ≫ | DSPA ≫ | DSPA2 Topics ≫ |
Expanding on other DSPA2 appendices), this DSPA2 appendix covers a discussion of building a mathematical model the length/duration of daylight and demonstrating supporting data science analytics for predicting the exact length of daylight (sunset to sunrise) for a given day, longitude, latitude, and altitude.
Note: This Part 2 of the DSPA Appendix 13 (Mathematical Modeling and Data Science Analytics of the Duration of Daylight) is intentionally separated from the main Part 1 of the DSPA Appendix 13 (Mathematical Modeling and Data Science Analytics of the Duration of Daylight). It includes large data and JavaScript code supporting the plotly app. It includes the interactive 3D temporal animation app that allows manual navigation across longitude, latitude, altitude and time (daily).
# Load necessary libraries
library(dplyr)
library(purrr)
library(plotly)
library(tidyr)
# Assume location_data is the pre-existing data frame with columns: latitude, longitude, altitude
# Example structure of location_data:
# location_data <- data.frame(
# latitude = c(-90, -80, -70, -60, -50),
# longitude = c(-180, -180, -180, -180, -180),
# altitude = c(0, 0, 0, 0, 0)
# )
location_data <- # download the low geospatial resolution lon-lat-alt data
read.csv("https://umich.instructure.com/files/37840607/download?download_frd=1",
header = TRUE)
# Define the days of the year
days <- 1:365
# Function to calculate daylight duration, including altitude
daylight_duration <- function(latitude, day_of_year, altitude) {
if (is.na(latitude) || is.na(day_of_year) || is.na(altitude)) {
return(NA) # Return NA if any input is missing
}
R <- 6371000 # Earth's radius in meters
# Solar declination angle
declination <- 23.44 * sin((2 * pi / 365) * (day_of_year - 81))
# Altitude correction in radians, only for positive altitudes
if (altitude > 0) {
altitude_correction <- -sqrt(2 * altitude / R) * (180 / pi)
} else {
altitude_correction <- 0 # No correction for sea level or below
}
# Calculate hour angle argument, clamped within [-1, 1]
hour_angle_arg <- -tan(latitude * pi / 180) * tan(declination * pi / 180) -
sin(altitude_correction * pi / 180)
hour_angle_arg <- pmax(pmin(hour_angle_arg, 1), -1)
# Calculate hour angle
hour_angle <- acos(hour_angle_arg)
# Calculate daylight duration in hours
daylight_hours <- (2 * hour_angle) / (pi / 12) # Convert radians to hours
# Handle cases of 24-hour daylight or darkness
if (hour_angle_arg <= -1) { daylight_hours <- 24 # Continuous daylight
} else if (hour_angle_arg >= 1) { daylight_hours <- 0 # Continuous night
}
return(daylight_hours)
}
# Calculate daylight duration for each (latitude, longitude, day) combination
daylight_data <- location_data %>% rowwise() %>%
mutate(daylight = list(sapply(days, function(day)
daylight_duration(latitude, day, altitude)))) %>%
unnest_wider(daylight, names_sep = "_") %>% ungroup()
# Convert days of the year to actual dates
# Define the starting date (e.g., January 1, 2024)
start_date <- as.Date("2024-01-01")
# Convert days of the year to actual dates
daylight_data_long <- daylight_data %>%
pivot_longer(cols = starts_with("daylight_"), names_to = "day",
values_to = "daylight_hours") %>%
mutate(day = as.numeric(gsub("daylight_", "", day))) # Extract numeric day
# daylight_data_long <- daylight_data_long %>%
# mutate(date = start_date + (day - 1)) # Convert day number to actual date
# Create a combined label for "Day X - Date"
daylight_data_long <- daylight_data_long %>%
mutate(day_label = paste("Day", day, "-", format(date, "%B %d, %Y"))) # Combine day number and date
# Define the starting date (e.g., January 1, 2024)
start_date <- as.Date("2024-01-01")
# Convert days of the year to actual dates
daylight_data_long <- daylight_data_long %>%
mutate(date = start_date + (day - 1)) # Convert day number to actual date
# Create a combined label for "Day X - Date"
daylight_data_long <- daylight_data_long %>%
mutate(day_label = paste("Day", day, "-", format(date, "%B %d, %Y"))) # Combine day number and date
# Format the date as "Month Day, Year" for display in the tooltip
daylight_data_long <- daylight_data_long %>%
mutate(date_text = format(date, "%B %d, %Y")) # Format as "Month Day, Year"
g2 <- list(projection = list(type = 'orthographic'),
showland = TRUE, landcolor = "lightgray", lakecolor = "lightblue")
# Plotly code with updated frame to display day and date in slider
fig <- plot_ly(data = daylight_data_long, type = 'scattergeo', mode = 'markers',
lat=~latitude, lon=~longitude, frame=~day_label, # Use combined day and date label for frame
marker = list(size = 6, opacity = 0.95), color = ~daylight_hours, colors = "RdYlBu",
customdata = ~altitude, # Altitude data for tooltip
hovertemplate = paste('<i>Latitude</i>: %{lat:.2f}', '<br><i>Longitude</i>: %{lon:.2f}',
'<br><i>Altitude (m)</i>: %{customdata:.2f}',
'<br><i>Date</i>: %{text}', # Display formatted date
'<br><i>Daylight Hours</i>: %{marker.color:.2f}',
'<extra></extra>'),
text = ~date_text, # Assign date_text to the `text` property for hovertemplate
showlegend = FALSE) %>%
layout(geo = g2,
title = list(text = "Daylight Length Over Earth Throughout the Year"), # Set title in list format
coloraxis = list(colorbar = list(title = "Daylight Hours")) # Adjust coloraxis for colorbar
)
fig # Show the plot