seis_ruby 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +5 -0
- data/bin/seis_ruby +2 -15
- data/lib/seis_ruby/bin/seis_ruby_runner.rb +58 -0
- data/lib/seis_ruby/bin.rb +6 -0
- data/lib/seis_ruby/data/cmtsolution.rb +23 -9
- data/lib/seis_ruby/data.rb +2 -2
- data/lib/seis_ruby/io/gcmt_catalog/custom_html_parser.rb +3 -0
- data/lib/seis_ruby/io/gcmt_catalog.rb +21 -13
- data/lib/seis_ruby/io.rb +2 -1
- data/lib/seis_ruby/version.rb +1 -1
- data/lib/seis_ruby.rb +2 -2
- data/seis_ruby.gemspec +27 -7
- data/spec/seis_ruby/data/cmtsolution_spec.rb +2 -17
- data/spec/seis_ruby/io/gcmt_catalog_spec.rb +0 -1
- data/spec/seis_ruby/io/scrape.yaml +3 -1
- data/spec/spec_helper.rb +2 -0
- metadata +36 -24
data/README.rdoc
ADDED
data/bin/seis_ruby
CHANGED
@@ -1,17 +1,4 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require '
|
4
|
-
|
5
|
-
require 'seis_ruby'
|
6
|
-
require 'ruby_patch'
|
7
|
-
require 'yaml'
|
8
|
-
|
9
|
-
desc 'gcmt_catalog_scrape URL', 'scrape cmtsolution data from GCMT catalog search result pages.'
|
10
|
-
def gcmt_catalog_scrape(url)
|
11
|
-
file = "#{__METHOD__}_#{Time.now.ymdhms}.yaml"
|
12
|
-
open(file, 'w').write(SeisRuby::Io::GcmtCatalog.scrape(url).to_yaml)
|
13
|
-
puts file
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
SeisRubyRunner.start
|
3
|
+
require 'seis_ruby'
|
4
|
+
::SeisRuby::Bin::SeisRubyRunner.start
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module SeisRuby
|
2
|
+
module Bin
|
3
|
+
require 'thor'
|
4
|
+
|
5
|
+
class SeisRubyRunner < Thor
|
6
|
+
require 'yaml'
|
7
|
+
require 'fileutils'
|
8
|
+
require 'ruby_patch'
|
9
|
+
extend ::RubyPatch::AutoLoad
|
10
|
+
|
11
|
+
COMMAND_NAME = 'seis_ruby'
|
12
|
+
|
13
|
+
desc 'gcmt_catalog_scrape URL', 'scrape cmtsolution data from GCMT catalog search result pages.'
|
14
|
+
def gcmt_catalog_scrape(url)
|
15
|
+
file = "#{__METHOD__}_#{Time.now.ymdhms}.yaml"
|
16
|
+
open(file, 'w').write(SeisRuby::Io::GcmtCatalog.scrape(url).to_yaml)
|
17
|
+
puts file
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "generate_completion", "Generate bash completion of seis_ruby's commands."
|
21
|
+
def generate_completion
|
22
|
+
commands = `#{COMMAND_NAME} --help`.split("\n").select{|l| l =~ / *#{COMMAND_NAME} /}.map{|l| l.split[1]}
|
23
|
+
function = <<-EOS
|
24
|
+
_#{COMMAND_NAME}()
|
25
|
+
{
|
26
|
+
local commands current previous
|
27
|
+
commands="#{commands.join(' ')}"
|
28
|
+
current="${COMP_WORDS[COMP_CWORD]}"
|
29
|
+
previous="${COMP_WORDS[COMP_CWORD-1]}"
|
30
|
+
|
31
|
+
case "${previous}" in
|
32
|
+
#{COMMAND_NAME}|help)
|
33
|
+
COMPREPLY=( $(compgen -W "${commands}" ${current}) );;
|
34
|
+
* )
|
35
|
+
COMPREPLY=( $(compgen -f ${current}) );;
|
36
|
+
esac
|
37
|
+
}
|
38
|
+
complete -F _#{COMMAND_NAME} #{COMMAND_NAME}
|
39
|
+
EOS
|
40
|
+
|
41
|
+
file = "#{ENV['HOME']}/.#{COMMAND_NAME}.d/completion.bash"
|
42
|
+
FileUtils.mkdir_p(File.dirname(file))
|
43
|
+
FileUtils.cp(file, "#{file}.#{Time.now.ymdhms}.bak") if File.file?(file)
|
44
|
+
open(file, 'w').write(function)
|
45
|
+
|
46
|
+
tilde_file = file.sub(/\A#{ENV['HOME']}/, '~')
|
47
|
+
puts <<-EOS
|
48
|
+
Please add following code to your ~/.bashrc if necessary.
|
49
|
+
|
50
|
+
# enable seis_ruby completion.
|
51
|
+
if [ -f #{tilde_file} ]; then
|
52
|
+
. #{tilde_file}
|
53
|
+
fi
|
54
|
+
EOS
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module SeisRuby
|
2
2
|
module Data
|
3
3
|
module Cmtsolution
|
4
|
-
require '
|
5
|
-
|
4
|
+
require 'ruby_patch'
|
5
|
+
extend ::RubyPatch::AutoLoad
|
6
6
|
|
7
7
|
ParseError = Class.new(StandardError)
|
8
8
|
|
@@ -42,20 +42,34 @@ module SeisRuby
|
|
42
42
|
h
|
43
43
|
end
|
44
44
|
|
45
|
-
def
|
45
|
+
def _parse_one_event(text_for_one_event)
|
46
46
|
begin
|
47
47
|
lines = _preprocess_for_parse(text_for_one_event)
|
48
48
|
_parse_lines(lines)
|
49
49
|
rescue
|
50
|
-
raise ParseError, text_for_one_event
|
51
|
-
.split("\n")\
|
52
|
-
.map{|line| line.quote('|'.color(:red))}\
|
53
|
-
.join("\n")
|
50
|
+
raise ParseError, text_for_one_event
|
54
51
|
end
|
55
52
|
end
|
56
53
|
|
57
|
-
|
58
|
-
|
54
|
+
# @param [String, Array<String>] events List of CMTSOLUTION format texts.
|
55
|
+
# @return [Array<Hash>] You can access :event_name, :hypocenter, :centroid.
|
56
|
+
def parse(*events)
|
57
|
+
h = events.flatten.map{|ev|
|
58
|
+
begin
|
59
|
+
_parse_one_event(ev)
|
60
|
+
rescue ParseError => e
|
61
|
+
e
|
62
|
+
end
|
63
|
+
}.classify{|o| o.exception?}
|
64
|
+
unless (error_list = h[true]).empty?
|
65
|
+
error_message = error_list.map{|e| e.message}.join("\n\n")
|
66
|
+
raise ParseError, <<-EOS
|
67
|
+
Could not parse following events:
|
68
|
+
#{error_message}
|
69
|
+
EOS
|
70
|
+
end
|
71
|
+
|
72
|
+
h[false]
|
59
73
|
end
|
60
74
|
end
|
61
75
|
end
|
data/lib/seis_ruby/data.rb
CHANGED
@@ -4,6 +4,9 @@ module SeisRuby
|
|
4
4
|
require 'mechanize'
|
5
5
|
|
6
6
|
class CustomHtmlParser < ::Mechanize::Page
|
7
|
+
require 'ruby_patch'
|
8
|
+
extend ::RubyPatch::AutoLoad
|
9
|
+
|
7
10
|
def initialize(uri = nil, response = nil, body = nil, code = nil, mech = nil)
|
8
11
|
super(uri, response, body.gsub(/<=/, '<='), code, mech)
|
9
12
|
end
|
@@ -3,30 +3,38 @@ module SeisRuby
|
|
3
3
|
module GcmtCatalog
|
4
4
|
require 'rainbow'
|
5
5
|
require 'mechanize'
|
6
|
-
require '
|
7
|
-
|
6
|
+
require 'ruby_patch'
|
7
|
+
extend ::RubyPatch::AutoLoad
|
8
8
|
|
9
9
|
module_function
|
10
10
|
|
11
|
+
# @param [String] url URL of a first page of Global CMT Catalog search result.
|
12
|
+
# @return [Array<Hash>] {::SeisRuby::Data::Cmtsolution#parse}
|
11
13
|
def scrape(url)
|
12
14
|
agent = Mechanize.new{|a|
|
13
15
|
a.pluggable_parser.html = ::SeisRuby::Io::GcmtCatalog::CustomHtmlParser
|
14
16
|
}
|
15
17
|
|
16
|
-
|
18
|
+
result = {
|
19
|
+
query: url,
|
20
|
+
result: [],
|
21
|
+
}
|
22
|
+
|
17
23
|
while url
|
18
24
|
agent.get(url)
|
19
|
-
agent.page.search('pre')\
|
25
|
+
event_text_list = agent.page.search('pre')\
|
20
26
|
.find{|e| e.text =~ /event name:/}\
|
21
27
|
.text.split("\n\n")\
|
22
|
-
.
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
.delete_if{|t| t.strip.empty?}
|
29
|
+
|
30
|
+
begin
|
31
|
+
event_list = ::SeisRuby::Data::Cmtsolution.parse(event_text_list)
|
32
|
+
rescue ::SeisRuby::Data::Cmtsolution::ParseError => e
|
33
|
+
$stderr.puts e.class.to_s.color(:red)
|
34
|
+
$stderr.puts e.message
|
35
|
+
ensure
|
36
|
+
result[:result].concat event_list
|
37
|
+
end
|
30
38
|
|
31
39
|
url = nil
|
32
40
|
if next_link = agent.page.links\
|
@@ -35,7 +43,7 @@ module SeisRuby
|
|
35
43
|
end
|
36
44
|
end
|
37
45
|
|
38
|
-
|
46
|
+
result
|
39
47
|
end
|
40
48
|
end
|
41
49
|
end
|
data/lib/seis_ruby/io.rb
CHANGED
data/lib/seis_ruby/version.rb
CHANGED
data/lib/seis_ruby.rb
CHANGED
data/seis_ruby.gemspec
CHANGED
@@ -1,19 +1,39 @@
|
|
1
1
|
gem_name = File.basename(Dir.pwd)
|
2
|
-
require
|
2
|
+
require 'rainbow'
|
3
|
+
require 'ruby_patch'
|
4
|
+
|
5
|
+
dir = File.join(__DIR__, 'lib')
|
6
|
+
$LOAD_PATH.unshift(File.join(dir)) unless $LOAD_PATH.include?(dir)
|
7
|
+
require "#{gem_name}"
|
3
8
|
|
4
9
|
Gem::Specification.new do |s|
|
5
10
|
s.files = `git ls-files`.split
|
6
11
|
s.name = gem_name
|
7
12
|
s.summary = "Ruby library for earthquake science."
|
8
13
|
s.version = SeisRuby::VERSION
|
9
|
-
s.add_development_dependency 'rspec', '~> 2'
|
10
|
-
s.add_development_dependency 'simplecov', '~> 0'
|
11
|
-
s.add_runtime_dependency 'rainbow', '~>1'
|
12
|
-
s.add_runtime_dependency '
|
13
|
-
s.add_runtime_dependency '
|
14
|
+
s.add_development_dependency 'rspec', '~> 2.10'
|
15
|
+
s.add_development_dependency 'simplecov', '~> 0.6'
|
16
|
+
s.add_runtime_dependency 'rainbow', '~> 1.1'
|
17
|
+
s.add_runtime_dependency 'thor', '~> 0.15'
|
18
|
+
s.add_runtime_dependency 'mechanize', '~> 2.5'
|
19
|
+
s.add_runtime_dependency 'ruby_patch', '~> 0.0'
|
14
20
|
s.author = 'kshramt'
|
15
21
|
s.description = "Ruby library for earthquake science. Provides Parser for various data formats used in seismology."
|
16
22
|
s.executables << 'seis_ruby'
|
17
|
-
s.
|
23
|
+
s.post_install_message = (<<-EOS
|
24
|
+
|
25
|
+
|
26
|
+
SeisRuby #{::SeisRuby::VERSION}
|
27
|
+
Thank you for installing.
|
28
|
+
Please execute following command:
|
29
|
+
|
30
|
+
#{::SeisRuby::Bin::SeisRubyRunner::COMMAND_NAME} generate_completion
|
31
|
+
|
32
|
+
Enjoy!
|
33
|
+
|
34
|
+
|
35
|
+
EOS
|
36
|
+
).color(:green)
|
37
|
+
s.required_ruby_version = '~> 1.9'
|
18
38
|
s.test_files.concat `git ls-files spec`.split.select{|path| path =~ /_spec\.rb/}
|
19
39
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'seis_ruby/data/cmtsolution'
|
3
2
|
|
4
3
|
describe ::SeisRuby::Data::Cmtsolution do
|
5
4
|
before :each do
|
@@ -71,27 +70,13 @@ Mtp: -1.491000e+24
|
|
71
70
|
describe '.parse' do
|
72
71
|
context 'normal case' do
|
73
72
|
it do
|
74
|
-
::SeisRuby::Data::Cmtsolution.parse(@valid_data).should == @parsed_valid_data
|
73
|
+
::SeisRuby::Data::Cmtsolution.parse(@valid_data, @valid_data, [@valid_data, @valid_data]).should == [@parsed_valid_data, @parsed_valid_data, @parsed_valid_data, @parsed_valid_data]
|
75
74
|
end
|
76
75
|
end
|
77
76
|
|
78
77
|
context 'wrong case' do
|
79
78
|
it do
|
80
|
-
lambda{::SeisRuby::Data::Cmtsolution.parse(@wrong_data)}.should raise_error(::SeisRuby::Data::Cmtsolution::ParseError)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe '.parse_list' do
|
86
|
-
context 'normal case' do
|
87
|
-
it do
|
88
|
-
::SeisRuby::Data::Cmtsolution.parse_list([@valid_data, @valid_data]).should == [@parsed_valid_data, @parsed_valid_data]
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context 'wrong case' do
|
93
|
-
it do
|
94
|
-
lambda{::SeisRuby::Data::Cmtsolution.parse_list([@valid_data, @wrong_data])}.should raise_error(::SeisRuby::Data::Cmtsolution::ParseError)
|
79
|
+
lambda{::SeisRuby::Data::Cmtsolution.parse([@valid_data, @wrong_data])}.should raise_error(::SeisRuby::Data::Cmtsolution::ParseError)
|
95
80
|
end
|
96
81
|
end
|
97
82
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
---
|
2
|
+
:query: http://www.globalcmt.org/cgi-bin/globalcmt-cgi-bin/CMT4/form?itype=ymd&yr=1976&mo=1&day=1&oyr=1976&omo=1&oday=1&jyr=1976&jday=1&ojyr=1&ojday=1&otype=nd&nday=400&lmw=0&umw=10&lms=0&ums=10&lmb=0&umb=10&llat=-90&ulat=90&llon=-180&ulon=180&lhd=0&uhd=1000<s=-9999&uts=9999&lpe1=0&upe1=90&lpe2=0&upe2=90&list=4
|
3
|
+
:result:
|
2
4
|
- :hypocenter:
|
3
5
|
:data_source: MLI
|
4
6
|
:year: 1976
|
@@ -157,7 +159,7 @@
|
|
157
159
|
:mrr: 4.78e+27
|
158
160
|
:mtt: -4.9e+26
|
159
161
|
:mpp: -4.3e+27
|
160
|
-
:mrt:
|
162
|
+
:mrt: 8.3e+26
|
161
163
|
:mrp: 3.62e+27
|
162
164
|
:mtp: -1.32e+27
|
163
165
|
:event_name: 011476A
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seis_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,66 +9,74 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &86475830 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '2'
|
21
|
+
version: '2.10'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *86475830
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: simplecov
|
27
|
-
requirement: &
|
27
|
+
requirement: &86475580 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '0'
|
32
|
+
version: '0.6'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *86475580
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rainbow
|
38
|
-
requirement: &
|
38
|
+
requirement: &86475350 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
42
42
|
- !ruby/object:Gem::Version
|
43
|
-
version: '1'
|
43
|
+
version: '1.1'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *86475350
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: thor
|
49
|
+
requirement: &86475120 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.15'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *86475120
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: mechanize
|
49
|
-
requirement: &
|
60
|
+
requirement: &86474890 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ~>
|
53
64
|
- !ruby/object:Gem::Version
|
54
|
-
version: '2'
|
65
|
+
version: '2.5'
|
55
66
|
type: :runtime
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *86474890
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: ruby_patch
|
60
|
-
requirement: &
|
71
|
+
requirement: &86474660 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
version: 0.1.0
|
66
|
-
- - <
|
74
|
+
- - ~>
|
67
75
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
76
|
+
version: '0.0'
|
69
77
|
type: :runtime
|
70
78
|
prerelease: false
|
71
|
-
version_requirements: *
|
79
|
+
version_requirements: *86474660
|
72
80
|
description: Ruby library for earthquake science. Provides Parser for various data
|
73
81
|
formats used in seismology.
|
74
82
|
email:
|
@@ -77,8 +85,11 @@ executables:
|
|
77
85
|
extensions: []
|
78
86
|
extra_rdoc_files: []
|
79
87
|
files:
|
88
|
+
- README.rdoc
|
80
89
|
- bin/seis_ruby
|
81
90
|
- lib/seis_ruby.rb
|
91
|
+
- lib/seis_ruby/bin.rb
|
92
|
+
- lib/seis_ruby/bin/seis_ruby_runner.rb
|
82
93
|
- lib/seis_ruby/data.rb
|
83
94
|
- lib/seis_ruby/data/cmtsolution.rb
|
84
95
|
- lib/seis_ruby/data/sac.rb
|
@@ -94,16 +105,17 @@ files:
|
|
94
105
|
- spec/spec_helper.rb
|
95
106
|
homepage:
|
96
107
|
licenses: []
|
97
|
-
post_install_message:
|
108
|
+
post_install_message: ! "\e[32m\n\nSeisRuby 0.1.0\nThank you for installing.\nPlease
|
109
|
+
execute following command:\n\nseis_ruby generate_completion\n\nEnjoy!\n\n\n\e[0m"
|
98
110
|
rdoc_options: []
|
99
111
|
require_paths:
|
100
112
|
- lib
|
101
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
114
|
none: false
|
103
115
|
requirements:
|
104
|
-
- -
|
116
|
+
- - ~>
|
105
117
|
- !ruby/object:Gem::Version
|
106
|
-
version: 1.9
|
118
|
+
version: '1.9'
|
107
119
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
120
|
none: false
|
109
121
|
requirements:
|