Last active
May 31, 2024 06:59
-
-
Save igproj-fusion/5813ea53ee518ca8802935ff11c4122c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################### | |
######################################################### | |
# | |
# 気象庁>各種データ・資料 > 過去の気象データ検索 | |
# https://www.data.jma.go.jp/obd/stats/etrn/index.php | |
# | |
######################################################### | |
pacman::p_load( | |
conflicted, | |
tidyverse, | |
rvest, | |
polite, | |
janitor, | |
RcppRoll, | |
here) | |
# | |
# functions | |
# | |
get_temp_data <- function(URL) { | |
URL |> | |
bow() |> | |
scrape(content = "text/html; charset=UTF-8") |> | |
html_table() |> | |
pluck(6) |> | |
clean_names() |> | |
select(ri, jiang_shui_liang, | |
qi_wen, qi_wen_2, qi_wen_3, ri_zhao_shi_jian_h) |> | |
slice(-(1:2)) |> | |
dplyr::mutate_all(~gsub(.,pattern="]",replacement = "")) |> | |
dplyr::mutate_all(~gsub(.,pattern=")",replacement = "")) |> | |
mutate( | |
day = ri |> as.integer(), | |
precipitation = jiang_shui_liang |> as.double(), | |
mean_temp = qi_wen |> as.double(), | |
max_temp = qi_wen_2 |> as.double(), | |
min_temp = qi_wen_3 |> as.double(), | |
sunshine = ri_zhao_shi_jian_h |> as.double(), | |
.keep = "none") | |
} | |
multiple_years <- function(base_url, year_range, month_range) { | |
year_month_range <- crossing(year_range, month_range) | |
paste0(base_url, year_month_range$year_range, "&month=", year_month_range$month_range, "&day=&view=p1") |> | |
map(\(url) get_temp_data(url), .progress = TRUE) |> | |
list_rbind(names_to = "rowid") |> | |
left_join( | |
year_month_range |> | |
rowid_to_column(), | |
by = join_by(rowid)) |> | |
select(!rowid) |> | |
mutate(age = paste0(floor(year_range / 10) * 10, "s")) |> | |
rename(year = year_range, month = month_range) | |
} | |
######################################################################## | |
# | |
# Example: 千葉県 坂畑(block_no=1241) 1978年1月~ | |
# | |
url1 <- "https://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=45" | |
url2 <- "&block_no=1241" | |
url3 <- "&year=" | |
base_url <- paste0(url1, url2, url3) | |
year_range <- 1978:2023 | |
month_range <- 1:12 | |
sakahata_1978_2023 <- multiple_years(base_url, year_range, month_range) | |
save(sakahata_1978_2023, | |
file = here("data", "sakahata_1978_2023.RData")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment