alexdum commited on
Commit
3a07883
·
1 Parent(s): 9aa300d

feat: Apply date range filtering to DWD index file selection and parsed dataframes for improved data loading.

Browse files
Files changed (2) hide show
  1. fun/parse_dwd.R +9 -7
  2. server.R +32 -3
fun/parse_dwd.R CHANGED
@@ -108,13 +108,15 @@ read_dwd_data <- function(zip_path, start_date = NULL, end_date = NULL) {
108
  # Filter valid dates
109
  df <- df[!is.na(df$datetime), ]
110
 
111
- # Window Filter - REMOVED to allow caller to inspect full range
112
- # if (!is.null(start_date)) {
113
- # df <- df[df$datetime >= (as.POSIXct(start_date) - days(1)), ]
114
- # }
115
- # if (!is.null(end_date)) {
116
- # df <- df[df$datetime <= (as.POSIXct(end_date) + days(1)), ]
117
- # }
 
 
118
 
119
  # Column Mapping
120
  weather_cols <- c(
 
108
  # Filter valid dates
109
  df <- df[!is.na(df$datetime), ]
110
 
111
+ # Window Filter - Discard rows outside requested window immediately to save memory/processing
112
+ if (!is.null(start_date)) {
113
+ s_limit <- as.POSIXct(start_date)
114
+ df <- df[df$datetime >= s_limit, ]
115
+ }
116
+ if (!is.null(end_date)) {
117
+ e_limit <- as.POSIXct(end_date) + days(1) # Include the end date fully
118
+ df <- df[df$datetime <= e_limit, ]
119
+ }
120
 
121
  # Column Mapping
122
  weather_cols <- c(
server.R CHANGED
@@ -548,11 +548,40 @@ server <- function(input, output, session) {
548
  # Freeze UI during download and parsing - include station name
549
  session$sendCustomMessage("freezeUI", list(text = msg, station = station_name))
550
 
551
- # Generate Filtered Index
552
- targets <- current_index() %>% filter(id == station_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
553
 
554
  if (nrow(targets) == 0) {
555
- reset_fetch("No data found in index for this station.")
 
 
 
 
 
 
 
 
 
556
  return()
557
  }
558
 
 
548
  # Freeze UI during download and parsing - include station name
549
  session$sendCustomMessage("freezeUI", list(text = msg, station = station_name))
550
 
551
+ # Generate Filtered Index - Optimize based on Date Range
552
+ req_start_date <- as.Date(window_debounced()$start)
553
+ req_end_date <- as.Date(window_debounced()$end)
554
+
555
+ # Helper to convert YYYYMMDD string to Date
556
+ parse_dwd_date <- function(x) {
557
+ as.Date(x, format = "%Y%m%d")
558
+ }
559
+
560
+ targets <- current_index() %>%
561
+ filter(id == station_id) %>%
562
+ filter(
563
+ # Logic: Keep file if (FileStart <= ReqEnd) AND (FileEnd >= ReqStart)
564
+ # Handle cases where start/end might be NA or "recent" type implies checking
565
+ type == "recent" | # Always check recent files as they might have fresh data
566
+ type == "solar" | # Solar usually has one file or simple structure, keep safe
567
+ (
568
+ !is.na(start_date) & !is.na(end_date) &
569
+ parse_dwd_date(start_date) <= req_end_date &
570
+ parse_dwd_date(end_date) >= req_start_date
571
+ )
572
+ )
573
 
574
  if (nrow(targets) == 0) {
575
+ # Fallback/Debug: check if we filtered out everything
576
+ raw_targets <- current_index() %>% filter(id == station_id)
577
+ msg <- "No data found in index for this station."
578
+
579
+ if (nrow(raw_targets) > 0) {
580
+ # Data exists but was filtered out by date range
581
+ msg <- "No data found for the selected date range."
582
+ }
583
+
584
+ reset_fetch(msg)
585
  return()
586
  }
587