5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rで過去天気を自動で取り込んだ|気象庁のサイト

Last updated at Posted at 2019-04-14

目的

野鳥の音声を自動で録音した時の降水量を調べられないかと思い気象庁から過去の天気を自動で読んで見ました。あまりズバリの記事がなかったので今日の備忘録として残します[変数名が違うなどあったので微修正しました:2019-04-21]。

方法

環境

各ソフトのインストールはたくさんのページを参照させて頂きました(冗長になるのでここでは割愛いたしますね)。

  • MacOSX HighSierra 10.13.6
  • safari 12.1
  • R 3.4.3
  • java version "12" 2019-03-19
  • selenium-server-standalone 3.141.59

注意点

  • safariは"環境設定" > "詳細" の"メニュバーに開発メニューを表示"をチェックしてください。
  • "開発"メニューから"リモートオートメーションを許可"をチェックしてください

例として2018年04月03日の常陸太田市の降水量を調べます。

まず、Seleniumを立ち上げます。terminalで以下を入力します。

shell-command
$ java -jar /usr/local/Cellar/selenium-server-standalone/3.141.59/libexec/selenium-server-standalone-3.141.59.jar

ここからRのスクリプトです。

使用するRパッケージ

getweather.r
library(RSelenium)
library(tidyverse)
library(rvest)

変数定義

getweather.r
year  <- "2018"; # 調べつ年
month <- "04";   # 調べる月
day   <- "03";   # 調べる日
date.aya <- paste0(year, month, "-", day);

1時間ごとに過去気象を調べるのは、気象庁のサイトを使いました。手作業で常陸太田の場所をあらかじめ調べておきます。そして月と日をRから設定します。

getweather.r
url <- paste0("http://www.data.jma.go.jp/obd/stats/etrn/view/hourly_a1.php?prec_no=40&block_no=1331&year=", year, "&month=", month,"&day=", day,"&view=a1");

Rでhtmlのページを読み込みます(スクレイピング)

やっていることは指定したページをselenium経由でページを読み込むことです。ブラウザのcloseも忘ずに閉じておきましょう。browserNameを変えればchomeもfirefoxもOKでしょう(未検証)。

getweather.r
remDr <- remoteDriver(
  remoteServerAddr = "localhost",
  port = 4444,
  browserName = "safari"
); # 最初の2行のパラメータは多分デフォルトでいいでしょうが念のため。
remDr$open(); #seleniumのリモートドライバをオープン
remDr$navigate(url); # ページを表示
res.page <- remDr$getPageSource(); #そのページを丸ごと読み込む
remDr$close(); #クローズ

一旦デバック用にhtmlファイルに書いておきました。htmlの構造なんかを調べるのはファイルに落とした方がが簡単ですし。

file.html <- "~/Desktop/weather.html";
write(unlist(res.page), file.html); 
res <- read_html(file.html)
res.table <- res %>% rvest::html_table(fill = TRUE); #tableを読む

読み込んだ気象データは5番目のテーブルにありました。そして、1行目のデータはサブのヘッダなのでこれをドロップしました。ヘッダにダブりがあるので風速・風向(m/s)、これを整えます。つまりダブりがないようにヘッダを変更します。

getweather.r
res.weather <- res.table[[5]][-1,]
names(res.weather)[4] <- "風速(m/s)"
names(res.weather)[5] <- "風向"
names(res.weather)[6] <- "日照時間(h)"
names(res.weather)[7] <- "降雪(cm)"
names(res.weather)[8] <- "積雪(cm)"

後の処理で時間情報を使えるようにPOSIXctに変換しておきます。

getweather.r
res.weather %>% mutate(time = as.POSIXct(strptime(paste0(date.aya,as.numeric() - 1,":00:00"), "%Y-%m-%d %H:%M:%S")))

最後の行はR内部の時間を取り扱うクラスに変更するためのものです。

結果

結果は以下の通りです。うまくいきました。

   時 降水量(mm) 気温(℃) 風速(m/s)   風向 日照時間(h) 降雪(cm) 積雪(cm)             time
1   1        0.0     10.3       1.1 北北西               ///    /// 2018-04-03 00:00:00
2   2        0.0     10.2       0.6     北               ///    /// 2018-04-03 01:00:00
3   3        0.0      9.6       0.6     北               ///    /// 2018-04-03 02:00:00
4   4        0.0      8.6       0.5     北               ///    /// 2018-04-03 03:00:00
5   5        0.0      8.7       0.8 北北西               ///    /// 2018-04-03 04:00:00
6   6        0.0      8.6       1.1     北      0.0      ///    /// 2018-04-03 05:00:00
7   7        0.0     10.5       1.2 北北西      0.0      ///    /// 2018-04-03 06:00:00
8   8        0.0     14.5       1.0   南東      0.8      ///    /// 2018-04-03 07:00:00
9   9        0.0     16.4       1.0 南南西      0.8      ///    /// 2018-04-03 08:00:00
10 10        0.0     17.9       1.1     西      1.0      ///    /// 2018-04-03 09:00:00
11 11        0.0     19.5       1.6 南南西      0.9      ///    /// 2018-04-03 10:00:00
12 12        0.0     21.3       1.9     南      1.0      ///    /// 2018-04-03 11:00:00
13 13        0.0     21.5       1.6 南南東      1.0      ///    /// 2018-04-03 12:00:00
14 14        0.0     22.3       1.9 南南東      1.0      ///    /// 2018-04-03 13:00:00
15 15        0.0     23.5       1.6     南      1.0      ///    /// 2018-04-03 14:00:00
16 16        0.0     22.4       3.2     南      0.5      ///    /// 2018-04-03 15:00:00
17 17        0.0     21.5       2.8 南南東      0.2      ///    /// 2018-04-03 16:00:00
18 18        0.0     19.5       1.0 東南東      0.0      ///    /// 2018-04-03 17:00:00
19 19        0.0     16.1       1.2     北               ///    /// 2018-04-03 18:00:00
20 20        0.0     14.6       1.3     北               ///    /// 2018-04-03 19:00:00
21 21        0.0     13.2       0.8     北               ///    /// 2018-04-03 20:00:00
22 22        0.0     12.8       1.4     北               ///    /// 2018-04-03 21:00:00
23 23        0.0     12.0       1.0     北               ///    /// 2018-04-03 22:00:00
24 24        0.0     11.4       0.6     北               ///    /// 2018-04-03 23:00:00

ここで注意は時間は(多分)計測終了時刻です。1時なら0時から1時までの気象測定結果でしょうが、単純にstrptimeで変換すると24時はその日の0時になってしまうので、パラメータtimeを1時間引いて測定開始時刻にしておきます。

monthとdayを変えれば任意の日の天気を自動取り出せます。日時をぐるぐる回せば、毎晩録音した時刻の降水量がゲットできます。もっとも場所は最寄りの気象観測所になりますが。このサイトはそのほか10分毎のデータもありより精度を高めることもできそうです。

皆様の何かのお役に立てれば嬉しいです。

参考サイト

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?