| | library(dplyr) |
| | library(readr) |
| | library(jsonlite) |
| | library(stringr) |
| |
|
| | |
| | amedas_raw <- fromJSON("https://www.jma.go.jp/bosai/amedas/const/amedastable.json") |
| |
|
| | to_decimal_degrees <- function(deg, minutes) { |
| | ifelse(is.na(deg) | is.na(minutes), NA_real_, |
| | ifelse(deg < 0, deg - minutes / 60, deg + minutes / 60)) |
| | } |
| |
|
| | parse_amedas_coord <- function(coord) { |
| | if (is.null(coord) || length(coord) < 2) return(NA_real_) |
| | to_decimal_degrees(as.numeric(coord[1]), as.numeric(coord[2])) |
| | } |
| |
|
| | normalize_name <- function(name) { |
| | name %>% |
| | str_replace_all("[\\((].*?[\\))]", "") %>% |
| | str_squish() |
| | } |
| |
|
| | coord_round <- function(x) round(x, 4) |
| |
|
| | amedas_entries <- imap_dfr(amedas_raw, function(entry, station_id) { |
| | name_jp <- if (!is.null(entry$kjName) && nzchar(entry$kjName)) entry$kjName else station_id |
| | name_en <- if (!is.null(entry$enName) && nzchar(entry$enName)) entry$enName else NA_character_ |
| | tibble( |
| | ID = as.character(station_id), |
| | Name_JP = name_jp, |
| | Name_EN = name_en, |
| | Lat = parse_amedas_coord(entry$lat), |
| | Lon = parse_amedas_coord(entry$lon) |
| | ) |
| | }) |
| |
|
| | |
| | |
| | |
| | stations <- read_csv("data/jma_stations_full.csv", show_col_types = FALSE) |
| |
|
| | |
| | |
| | mappings <- stations %>% |
| | filter(!is.na(prec_no)) %>% |
| | select(Name_JP, prec_no) %>% |
| | distinct() |
| |
|
| | stations_fixed <- stations %>% |
| | left_join(mappings, by = "Name_JP") %>% |
| | mutate(prec_no = ifelse(is.na(prec_no.x), prec_no.y, prec_no.x)) %>% |
| | select(-prec_no.x, -prec_no.y) |
| |
|
| | |
| | |
| | stations_final <- stations_fixed %>% |
| | arrange(Name_JP, desc(Type)) %>% |
| | distinct(Name_JP, .keep_all = TRUE) |
| |
|
| | |
| | amedas_match <- amedas_entries %>% |
| | mutate( |
| | Name_JP_norm = normalize_name(Name_JP), |
| | Lat_round = coord_round(Lat), |
| | Lon_round = coord_round(Lon) |
| | ) %>% |
| | distinct(Name_JP_norm, Lat_round, Lon_round, .keep_all = TRUE) |
| |
|
| | stations_final <- stations_final %>% |
| | left_join( |
| | amedas_entries %>% |
| | select(ID, Name_EN) %>% |
| | rename(Name_EN_json = Name_EN), |
| | by = "ID" |
| | ) %>% |
| | mutate( |
| | Name_JP_norm = normalize_name(Name_JP), |
| | Lat_round = coord_round(Lat), |
| | Lon_round = coord_round(Lon) |
| | ) %>% |
| | left_join( |
| | amedas_match %>% |
| | select(Name_JP_norm, Lat_round, Lon_round, Name_EN) %>% |
| | rename(Name_EN_match = Name_EN), |
| | by = c("Name_JP_norm", "Lat_round", "Lon_round") |
| | ) %>% |
| | mutate(Name_EN = coalesce(Name_EN, Name_EN_json, Name_EN_match)) %>% |
| | select(-Name_EN_json, -Name_EN_match, -Name_JP_norm, -Lat_round, -Lon_round) |
| |
|
| | |
| | write_csv(stations_final, "data/jma_stations_full.csv") |
| | message(sprintf("Final station count: %d", nrow(stations_final))) |
| |
|