File size: 1,653 Bytes
3fc14e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# tests/debug_parse.R
library(dplyr)
library(tibble)

file <- "produkt_klima_tag_20240710_20260110_00011.txt"
if (!file.exists(file)) unzip("debug_daily.zip", files = file)

message("Reading table...")
df <- read.table(
    file,
    sep = ";",
    header = TRUE,
    stringsAsFactors = FALSE,
    strip.white = TRUE,
    fill = TRUE
)

names(df)

tryCatch(
    {
        df <- df %>%
            rename_with(~ case_when(
                . == "TT_TU" ~ "temp",
                . == "RF_TU" ~ "rh",
                . == "R1" ~ "precip",
                . == "F" ~ "wind_speed",
                . == "D" ~ "wind_dir",
                . == "P" ~ "pressure",
                . == "P0" ~ "station_pressure",
                . %in% c("N_8", "V_N") ~ "cloud_cover",
                . %in% c("FX_10", "FX_911") ~ "wind_gust_max",
                . %in% c("FG_LBERG", "FG_STRAHL") ~ "solar_global",
                . %in% c("SD_LBERG", "SD_STRAHL", "SD_SO") ~ "sunshine_duration",

                # Daily Mappings
                . %in% c("TMK", "TM_K", "TNK", "TXK") ~ "temp",
                . %in% c("RSK", "RS_K") ~ "precip",
                . == "FM" ~ "wind_speed",
                . == "FX" ~ "wind_gust_max",
                . %in% c("SDK", "SD_SO") ~ "sunshine_duration",
                . == "UPM" ~ "rh",
                . == "PM" ~ "pressure",
                . == "NM" ~ "cloud_cover",
                TRUE ~ .
            ))

        message("Rename complete.")
        print(names(df))
    },
    error = function(e) {
        message("ERROR CAUGHT:")
        print(e)
        message("Message: ", conditionMessage(e))
    }
)

file.remove(file)