‘JMA’ web page consists of some layers. You can use different function for download each component.
I recommend to use existing data, which are already downloaded and cleaned up.
When you want to download links for climate data, use download_area_links() and download_links(). download_area_links() returns links for 6 areas. download_links() returns links for countries and stations.
For polite scraping, 5 sec interval is set in download_links(), it takes about 15 minutes to get all station links. Please use existing links by “data(station_links)”, if you do not need to renew links.
library(clidatajp)
library(tidyverse)
#> ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
#> ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
#> ✔ tibble 3.1.8 ✔ dplyr 1.0.10
#> ✔ tidyr 1.2.1 ✔ stringr 1.4.1
#> ✔ readr 2.1.2 ✔ forcats 0.5.1
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
# existing data
data(station_links)
%>%
station_links ::mutate("station" := stringi::stri_unescape_unicode(station)) %>%
dplyrprint() %>%
`$`("station") %>%
clean_station() %>%
::bind_cols(station_links["url"])
dplyr#> # A tibble: 3,444 × 3
#> no station url
#> <chr> <chr> <chr>
#> 1 1 アインセフラ - アルジェリア 緯度:32.77°N 経度:0.60°W … https:…
#> 2 2 アドラル - アルジェリア 緯度:27.88°N 経度:0.18°W 高度… https:…
#> 3 3 アルジェ - アルジェリア 緯度:36.77°N 経度:3.10°E 高度… https:…
#> 4 4 アンナバ - アルジェリア 緯度:36.83°N 経度:7.82°E 高度… https:…
#> 5 5 イナメナス - アルジェリア 緯度:28.05°N 経度:9.63°E 高… https:…
#> 6 6 イリジ - アルジェリア 緯度:26.50°N 経度:8.42°E 高度:5… https:…
#> 7 7 インゲザム - アルジェリア 緯度:19.57°N 経度:5.77°E 高… https:…
#> 8 8 インサラー - アルジェリア 緯度:27.23°N 経度:2.50°E 高… https:…
#> 9 9 ウェド - アルジェリア 緯度:33.50°N 経度:6.78°E 高度:6… https:…
#> 10 10 ウームエルブワギー - アルジェリア 緯度:35.87°N 経度:7.12… https:…
#> # … with 3,434 more rows
#> # A tibble: 3,444 × 8
#> station country latitude NS longi…¹ WE altit…² url
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
#> 1 アインセフラ_アルジェリア アルジ… 32.77 N 0.60 W 1058 http…
#> 2 アドラル_アルジェリア アルジ… 27.88 N 0.18 W 279 http…
#> 3 アルジェ_アルジェリア アルジ… 36.77 N 3.10 E 9 http…
#> 4 アンナバ_アルジェリア アルジ… 36.83 N 7.82 E 3 http…
#> 5 イナメナス_アルジェリア アルジ… 28.05 N 9.63 E 561 http…
#> 6 イリジ_アルジェリア アルジ… 26.50 N 8.42 E 543 http…
#> 7 インゲザム_アルジェリア アルジ… 19.57 N 5.77 E 400 http…
#> 8 インサラー_アルジェリア アルジ… 27.23 N 2.50 E 268 http…
#> 9 ウェド_アルジェリア アルジ… 33.50 N 6.78 E 64 http…
#> 10 ウームエルブワギー_アルジ… アルジ… 35.87 N 7.12 E 889 http…
#> # … with 3,434 more rows, and abbreviated variable names ¹longitude, ²altitude
# Download new data
# If you want links for all countries and all sations, remove head().
<- "https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/"
url <- gracefully_fail(url)
res if(!is.null(res)){
<- download_area_links()
area_links <- NULL
station_links <- head(area_links) # for test
area_links for(i in seq_along(area_links)){
print(stringr::str_c("area: ", i, " / ", length(area_links)))
<- download_links(area_links[i])
country_links <- head(country_links) # for test
country_links for(j in seq_along(country_links)){
print(stringr::str_c(" country: ", j, " / ", length(country_links)))
<- c(station_links, download_links(country_links[j]))
station_links
}
}<- tibble::tibble(url = station_links)
station_links
station_links
}#> [1] "area: 1 / 6"
#> [1] " country: 1 / 6"
#> [1] " country: 2 / 6"
#> [1] " country: 3 / 6"
#> [1] " country: 4 / 6"
#> [1] " country: 5 / 6"
#> [1] " country: 6 / 6"
#> [1] "area: 2 / 6"
#> [1] " country: 1 / 6"
#> [1] " country: 2 / 6"
#> [1] " country: 3 / 6"
#> [1] " country: 4 / 6"
#> [1] " country: 5 / 6"
#> [1] " country: 6 / 6"
#> [1] "area: 3 / 6"
#> [1] " country: 1 / 6"
#> [1] " country: 2 / 6"
#> [1] " country: 3 / 6"
#> [1] " country: 4 / 6"
#> [1] " country: 5 / 6"
#> [1] " country: 6 / 6"
#> [1] "area: 4 / 6"
#> [1] " country: 1 / 6"
#> [1] " country: 2 / 6"
#> [1] " country: 3 / 6"
#> [1] " country: 4 / 6"
#> [1] " country: 5 / 6"
#> [1] " country: 6 / 6"
#> [1] "area: 5 / 6"
#> [1] " country: 1 / 6"
#> [1] " country: 2 / 6"
#> [1] " country: 3 / 6"
#> [1] " country: 4 / 6"
#> [1] " country: 5 / 6"
#> [1] " country: 6 / 6"
#> [1] "area: 6 / 6"
#> [1] " country: 1 / 6"
#> [1] " country: 2 / 6"
#> [1] " country: 3 / 6"
#> [1] " country: 4 / 6"
#> [1] " country: 5 / 6"
#> [1] " country: 6 / 6"
#> Timeout was reached: [www.data.jma.go.jp] Operation timed out after 1008 milliseconds with 0 bytes received
#> # A tibble: 1,416 × 1
#> url
#> <chr>
#> 1 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60560
#> 2 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60620
#> 3 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60369
#> 4 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60360
#> 5 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60611
#> 6 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60640
#> 7 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60690
#> 8 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60630
#> 9 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60559
#> 10 https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/NrmMonth.php?stn=60421
#> # … with 1,406 more rows
I recommend to use existing data, which are already downloaded and cleaned up.
When you want to know how to prepare “data(japan_climate)”, please check url shown below.
https://github.com/matutosi/clidatajp/blob/main/data-raw/japan_climate.R
For polite scraping, 5 sec interval is set in download_climate(), it takes over 5 hours to get world climate data of all stations because of huge amount of data (3444 stations). Please use existing data by “data(world_climate)”, if you do not need to renew climate data.
# existing data
data(japan_climate)
%>%
japan_climate ::mutate_if(is.character, stringi::stri_unescape_unicode)
dplyr#> # A tibble: 3,768 × 14
#> no station month temperat…¹ preci…² snowf…³ insol…⁴ country period altit…⁵
#> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl>
#> 1 47401 稚内 1 -4.3 84.6 129 40.6 日本 1991-… 2.8
#> 2 47401 稚内 2 -4.3 60.6 105 74.7 日本 1991-… 2.8
#> 3 47401 稚内 3 -0.6 55.1 68 138. 日本 1991-… 2.8
#> 4 47401 稚内 4 4.5 50.3 9 174. 日本 1991-… 2.8
#> 5 47401 稚内 5 9.1 68.1 0 182. 日本 1991-… 2.8
#> 6 47401 稚内 6 13 65.8 NA 155. 日本 1991-… 2.8
#> 7 47401 稚内 7 17.2 101. NA 143. 日本 1991-… 2.8
#> 8 47401 稚内 8 19.5 123. NA 151. 日本 1991-… 2.8
#> 9 47401 稚内 9 17.2 137. NA 172. 日本 1991-… 2.8
#> 10 47401 稚内 10 11.3 130. 1 135. 日本 1991-… 2.8
#> # … with 3,758 more rows, 4 more variables: latitude <dbl>, longitude <dbl>,
#> # NS <chr>, WE <chr>, and abbreviated variable names ¹temperature,
#> # ²precipitation, ³snowfall, ⁴insolation, ⁵altitude
data(world_climate)
%>%
world_climate ::mutate_if(is.character, stringi::stri_unescape_unicode)
dplyr#> # A tibble: 41,328 × 11
#> no month temperature precipi…¹ station country latit…² NS longi…³ WE
#> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <chr> <dbl> <chr>
#> 1 1 1 7.1 14.9 アイン… アルジ… 32.8 N 0.6 W
#> 2 1 2 9.2 11.2 アイン… アルジ… 32.8 N 0.6 W
#> 3 1 3 12.9 15.9 アイン… アルジ… 32.8 N 0.6 W
#> 4 1 4 16.8 16.9 アイン… アルジ… 32.8 N 0.6 W
#> 5 1 5 21.5 15 アイン… アルジ… 32.8 N 0.6 W
#> 6 1 6 26.7 6.9 アイン… アルジ… 32.8 N 0.6 W
#> 7 1 7 31 4.1 アイン… アルジ… 32.8 N 0.6 W
#> 8 1 8 29.5 13.5 アイン… アルジ… 32.8 N 0.6 W
#> 9 1 9 24.4 21 アイン… アルジ… 32.8 N 0.6 W
#> 10 1 10 18.6 25.8 アイン… アルジ… 32.8 N 0.6 W
#> # … with 41,318 more rows, 1 more variable: altitude <dbl>, and abbreviated
#> # variable names ¹precipitation, ²latitude, ³longitude
# Download new data
# If you want links for all countries and all sations, remove head().
<- "https://www.data.jma.go.jp/gmd/cpd/monitor/nrmlist/"
url <- gracefully_fail(url)
res if(!is.null(res)){
<-
station_links %>%
station_links head() %>%
`$`("url")
<- list()
climate for(i in seq_along(station_links)){
print(stringr::str_c(i, " / ", length(station_links)))
<- download_climate(station_links[i])
climate[[i]]
}<- dplyr::bind_rows(climate)
world_climate
world_climate
}#> [1] "1 / 6"
#> [1] "2 / 6"
#> [1] "3 / 6"
#> [1] "4 / 6"
#> [1] "5 / 6"
#> [1] "6 / 6"
#> # A tibble: 72 × 11
#> station country latit…¹ NS longi…² WE altit…³ month tempe…⁴ preci…⁵
#> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 アインセフ… アルジ… 32.77 N 0.60 W 1058 1 7.1 14.9
#> 2 アインセフ… アルジ… 32.77 N 0.60 W 1058 2 9.2 11.2
#> 3 アインセフ… アルジ… 32.77 N 0.60 W 1058 3 12.9 15.9
#> 4 アインセフ… アルジ… 32.77 N 0.60 W 1058 4 16.8 16.9
#> 5 アインセフ… アルジ… 32.77 N 0.60 W 1058 5 21.5 15
#> 6 アインセフ… アルジ… 32.77 N 0.60 W 1058 6 26.7 6.9
#> 7 アインセフ… アルジ… 32.77 N 0.60 W 1058 7 31 4.1
#> 8 アインセフ… アルジ… 32.77 N 0.60 W 1058 8 29.5 13.5
#> 9 アインセフ… アルジ… 32.77 N 0.60 W 1058 9 24.4 21
#> 10 アインセフ… アルジ… 32.77 N 0.60 W 1058 10 18.6 25.8
#> # … with 62 more rows, 1 more variable: url <chr>, and abbreviated variable
#> # names ¹latitude, ²longitude, ³altitude, ⁴temperature, ⁵precipitation