Skip to content

Instantly share code, notes, and snippets.

@izumogeiger
Last active May 18, 2020 22:38
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 izumogeiger/533e85ae2cd77a0affbebacdb9943eee to your computer and use it in GitHub Desktop.
Save izumogeiger/533e85ae2cd77a0affbebacdb9943eee to your computer and use it in GitHub Desktop.
require 'pp'
require 'date'
require 'tempfile'
require 'open3'
=begin
## data file path
#{DATADIR}/YYYY/MM/DD.txt
## data file
2020-05-01T00:00:13+09:00,13.704,
2020-05-01T00:02:14+09:00,27.927,
2020-05-01T00:04:15+09:00,22.078,
2020-05-01T00:06:15+09:00,19.938,
2020-05-01T00:08:16+09:00,22.040,
2020-05-01T00:10:16+09:00,18.368,
=end
DATADIR = "/home/pi/monitor_log/gc10"
OUTDIR = "/home/pi/git/monitor/gc10"
def concat_file(path,io)
File.readlines(path, chomp: true).each do |line|
io.puts(line)
end
end
def cut4day(path)
Tempfile.open("cut4day") do |fp|
last = nil
File.readlines(path, chomp: true).each do |line|
last = line
end
t0 = DateTime.parse(last.split(",")[0]) -1 # 1day before
File.readlines(path, chomp: true).each do |line|
t = DateTime.parse(line.split(",")[0])
if t > t0
fp.puts(line)
else
# do nothing
end
end
fp.close
yield fp.path
end
end
def gnuplot(mode, path)
out = File.join(OUTDIR,"#{mode}.png")
gpin = <<-EOS
set datafile separator ','
set xdata time
set timefmt "%Y-%m-%d\124%H:%M:%S"
set mxtics 2
set mytics 2
set grid xtics ytics mxtics mytics
set key outside
set title 'radiation #{mode}'
set term png size 800,480
set ylabel 'cpm'
set xlabel 'time'
set output '#{out}'
p '#{path}' u 1:2 w l lc rgb "gray" t "cpm", "" u 1:2 smooth lw 2 lc rgb "red" t "cpm"
set output
EOS
o,e,s = Open3.capture3("gnuplot",:stdin_data => gpin)
unless s.success?
puts o,e,s
end
end
def usage
puts "#{$0} <month|week|day>"
exit 1
end
def prepare_data_file(mode)
today = Date.today
case mode
when "month"
diff = (today >> 1) - today
days = (0..diff).map do |i| today - i end
when "week"
days = (0..6).map do |i| today - i end
when "day"
days = (0..1).map do |i| today - i end
end
days.reverse!
Tempfile.open("gc10data") do |fp|
days.each do |d|
file = File.join(DATADIR,d.year.to_s,format("%02d",d.month),format("%02d.txt",d.day))
concat_file(file,fp)
end
fp.close
case mode
when "day"
cut4day(fp.path) do |path|
yield path
end
when "week","month"
yield fp.path
end
end
end
# main
usage if ARGV.size < 1
mode = ARGV[0].downcase
case mode
when "month"
when "week"
when "day"
else
usage
end
prepare_data_file(mode) do |path|
gnuplot(mode, path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment