Created
March 25, 2020 17:24
-
-
Save jstanley0/67eed64f6d2bdbf14604c079e6e32ef0 to your computer and use it in GitHub Desktop.
create data for a scatter plot of aftershocks from the Magna, UT earthquake on 2020-03-18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'date' | |
require 'net/http' | |
QUERY = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=text&latitude=40.751&longitude=-112.078&maxradiuskm=50&starttime=2020-03-18" | |
data = Net::HTTP.get(URI(QUERY)).lines | |
header = data.shift.split('|') | |
ts_col = header.index("Time") | |
raise "Time column not found" unless ts_col | |
mag_col = header.index("Magnitude") | |
raise "Magnitude column not found" unless mag_col | |
x = [] | |
y = [] | |
min_date = nil | |
data.each do |line| | |
line = line.split('|') | |
y << line[mag_col].to_f | |
ts = DateTime.parse(line[ts_col]) | |
x << ts | |
min_date = [min_date, ts].compact.min | |
end | |
x = x.map { |ts| "%.3f" % ((ts - min_date) * 24).to_f } | |
y = y.map { |mag| "%.3f" % mag } | |
output = StringIO.new | |
output.puts "hours since quake,magnitude" | |
x.each_with_index do |ts, i| | |
magnitude = y[i] | |
output.puts "#{ts},#{magnitude}" | |
end | |
IO.popen('pbcopy', 'r+') do |clipboard| | |
clipboard.puts output.string | |
end | |
puts "#{data.size} data points copied for scatterplot.online" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment