Skip to content

Instantly share code, notes, and snippets.

@mickey24
Created March 14, 2011 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickey24/869400 to your computer and use it in GitHub Desktop.
Save mickey24/869400 to your computer and use it in GitHub Desktop.
奥村先生による福島原発のデータ fukushima*.csv ( http://oku.edu.mie-u.ac.jp/~okumura/stat/data/ ) をRなどで処理しやすい形に加工するスクリプトです.使い方についてはコメント欄を参照してください.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require "time"
puts %w[datetime mon day hour min place gamma neutron wind_dir wind_speed].join("\t")
lines = ARGF.each_line.to_a[2..-1]
date = ""
lines.each do |line|
list = line.split(",")
# date
date = list[0].empty? ? date : list[0]
date =~ /(\d+)\D+(\d+)/
mon = $1
day = $2
# time
time = list[1].gsub(/午後(\d+)/){$1.to_i + 12}
time =~ /(\d+)\D+(\d+)/
hour = $1
min = $2
# datetime
datetime = "2011/#{mon}/#{day} #{hour}:#{min}"
# place
place = list[2].gsub("付近", "")
# gamma
gamma = list[3].to_f * (list[3].include?("μ") ? 1 : 1.0/1000)
# neutron
neutron = list[4].to_f * (list[4].include?("μ") ? 1 : 1.0/1000)
# wind_dir
wind_dir = list[5]
# wind_speed
wind_speed = list[6].chomp
puts [datetime, mon, day, hour, min, place, gamma, neutron, wind_dir, wind_speed].join("\t")
end
@mickey24
Copy link
Author

コマンド例は以下の通りです.加工結果はtsvで標準出力に出力されます.
$ ruby convert_fukushima.rb fukushima1.csv

エラーが出てうまく実行できない時は,fukushima*.csvの文字コードや改行コードがおかしい可能性があります.この場合はあらかじめ以下のようにnkfを実行して文字コードや改行コードを変換してからconvert_fukushima.csvを実行すれば大丈夫だと思います.
$ nkf -Lw -c --overwrite fukushima1.csv

@kohske
Copy link

kohske commented Mar 15, 2011

被災された方々に、謹んでお見舞い申し上げます。
奥村先生の所から直接とってきて時系列プロットするスクリプトを貼り付けておいたので、必要であれば使ってください。
お役に立てば幸いです。

d <- read.csv('http://oku.edu.mie-u.ac.jp/~okumura/stat/data/fukushima1.csv', fileEncoding="sjis", skip=1, na="-", as.is=TRUE)
d <- d[,1:7]

for (i in 4:5) d[,i] <- as.numeric(str_replace(d[,i], "([0-9.]+).*", "\1")) * ifelse(grepl("n", d[,4]), 1/1000, 1)

iday<-which(d[,1]!="")
d[,1] <- rep(d[iday,1], diff(c(iday, nrow(d)+1)))

d$stime <- paste(d[,1],
ifelse(grepl("午前", d[,2]), "AM", "PM"),
as.integer(str_replace(d[,2], "..([0-9]).", "\1"))+1,
"/",
str_replace(d[,2], "..[0-9].([0-9]).*", "\1"))
d$time <- strptime(d$stime, "%m月%d日 %p %I / %M")

ggplot(d, aes(as.POSIXct(time), γ線, colour=計測場所)) + geom_line() + theme_grey(base_family="sans") + xlab("日時(日ー月)") + ylab("γ線(μSV/h)")

@kohske
Copy link

kohske commented Mar 15, 2011

あ、もうすでに時系列プロットも作ってたんですね。

@mickey24
Copy link
Author

おお,ありがとうございます!greplあたりは知らなかったので参考になりました.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment