Skip to content

Instantly share code, notes, and snippets.

@jstanley0
Created March 25, 2020 17:24
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 jstanley0/67eed64f6d2bdbf14604c079e6e32ef0 to your computer and use it in GitHub Desktop.
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
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