seis_ruby 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README.rdoc +23 -4
  2. data/Rakefile +10 -0
  3. data/bin/seis_ruby +1 -1
  4. data/lib/seis_ruby/application.rb +76 -0
  5. data/lib/seis_ruby/command.rb +15 -0
  6. data/lib/seis_ruby/core_ext/file.rb +7 -0
  7. data/lib/seis_ruby/core_ext.rb +5 -0
  8. data/lib/seis_ruby/data/cmtsolution.rb +84 -64
  9. data/lib/seis_ruby/data/sac/ascii/head.rb +49 -0
  10. data/lib/seis_ruby/data/sac/ascii.rb +61 -0
  11. data/lib/seis_ruby/data/sac/binary/head.rb +5 -0
  12. data/lib/seis_ruby/data/sac/binary.rb +26 -0
  13. data/lib/seis_ruby/data/sac/body.rb +62 -0
  14. data/lib/seis_ruby/data/sac/head.rb +162 -0
  15. data/lib/seis_ruby/data/sac.rb +99 -3
  16. data/lib/seis_ruby/data.rb +6 -5
  17. data/lib/seis_ruby/database/global_cmt_catalog_search/cmtsolution_format.rb +30 -0
  18. data/lib/seis_ruby/database/global_cmt_catalog_search.rb +44 -0
  19. data/lib/seis_ruby/database.rb +3 -0
  20. data/lib/seis_ruby/version.rb +1 -1
  21. data/lib/seis_ruby.rb +12 -3
  22. data/seis_ruby.gemspec +8 -14
  23. data/test/data/000000001.SAC +0 -0
  24. data/test/data/000000001.SAC_ascii +32 -0
  25. data/test/seis_ruby/command_test.rb +19 -0
  26. data/test/seis_ruby/core_ext/file_test.rb +7 -0
  27. data/test/seis_ruby/data/cmtsolution_test.rb +63 -0
  28. data/test/seis_ruby/data/sac/ascii_test.rb +15 -0
  29. data/test/seis_ruby/data/sac/binary_test.rb +15 -0
  30. data/test/seis_ruby/data/sac/body_test.rb +27 -0
  31. data/test/seis_ruby/data/sac/head_test.rb +78 -0
  32. data/test/seis_ruby/data/sac_test.rb +52 -0
  33. data/test/seis_ruby/database/global_cmt_catalog_search/cmtsolution_format_test.rb +142 -0
  34. data/test/seis_ruby/database/global_cmt_catalog_search_test.rb +10 -0
  35. data/test/seis_ruby_test.rb +0 -0
  36. data/test/test_helper.rb +7 -0
  37. data/test.watchr +13 -0
  38. metadata +79 -54
  39. data/lib/seis_ruby/bin/seis_ruby_runner.rb +0 -58
  40. data/lib/seis_ruby/bin.rb +0 -6
  41. data/lib/seis_ruby/io/gcmt_catalog/custom_html_parser.rb +0 -16
  42. data/lib/seis_ruby/io/gcmt_catalog.rb +0 -50
  43. data/lib/seis_ruby/io.rb +0 -6
  44. data/rakefile +0 -8
  45. data/spec/seis_ruby/data/cmtsolution_spec.rb +0 -83
  46. data/spec/seis_ruby/io/gcmt_catalog_spec.rb +0 -19
  47. data/spec/seis_ruby/io/scrape.yaml +0 -3972
  48. data/spec/spec_helper.rb +0 -18
@@ -1,6 +1,102 @@
1
- module SeisRuby
2
- module Data
3
- module Sac
1
+ # @see http://www.iris.edu/software/sac/manual/file_format.html SAC User Manual/SAC Data Format
2
+ class ::SeisRuby::Data::Sac < ::SeisRuby::Data
3
+ require 'seis_ruby/data/sac/head'
4
+ require 'seis_ruby/data/sac/body'
5
+ require 'seis_ruby/data/sac/binary'
6
+ require 'seis_ruby/data/sac/ascii'
7
+
8
+ def self.load_file(file)
9
+ raw_data = File.read_uri(file)
10
+ type = type_from_file(file)
11
+ meta_data = {file: file}
12
+
13
+ load(raw_data, type, meta_data)
14
+ end
15
+
16
+ def self.load(raw_data, type = :binary, meta_data = {})
17
+ new(raw_data, type, meta_data)
18
+ end
19
+
20
+ def self.type_from_file(file)
21
+ if ::SeisRuby::Data::Sac::Binary.uri_for_self?(file)
22
+ :binary
23
+ elsif ::SeisRuby::Data::Sac::Ascii.uri_for_self?(file)
24
+ :ascii
25
+ else
26
+ raise ArgumentError, "Unsupported extension: #{ext}"
27
+ end
28
+ end
29
+
30
+ def self.uri_for_self?(uri)
31
+ ::SeisRuby::Data::Sac::Binary.uri_for_self?(uri) || ::SeisRuby::Data::Sac::Ascii.uri_for_self?(uri)
32
+ end
33
+
34
+ def initialize(raw_data, type = :binary, meta_data = {})
35
+ @raw_data = raw_data
36
+ @meta_data = meta_data
37
+ parse!(type)
38
+ end
39
+
40
+ attr_accessor :head # Header
41
+ attr_accessor :body # Wave
42
+
43
+ ::SeisRuby::Data::Sac::Head::NAMES.each{|name|
44
+ define_method(name){@head[name]}
45
+ define_method("#{name}="){|val| @head[name] = val}}
46
+
47
+ def dump_file(file)
48
+ open(file, 'wb'){|io|
49
+ io.write(module_from_type(type_from_file(file)).dump(@head, @body))
50
+ io.flush
51
+ }
52
+ end
53
+
54
+ def dump(type = :binary)
55
+ module_from_type(type).dump(@head, @body)
56
+ end
57
+
58
+ def eql?(other)
59
+ self.class == other.class\
60
+ && @meta_data.eql?(other.meta_data)\
61
+ && @raw_data.eql?(other.raw_data)\
62
+ && @head.eql?(other.head)\
63
+ && @body.eql?(other.body)
64
+ end
65
+
66
+ def hash
67
+ [self.class, @meta_data, @raw_data, @head, @body].hash
68
+ end
69
+
70
+ def ==(other)
71
+ @head == other.head\
72
+ && @body == other.body
73
+ end
74
+
75
+ private
76
+
77
+ def type_from_file(file)
78
+ self.class.type_from_file(file)
79
+ end
80
+
81
+ def module_from_type(type)
82
+ case type
83
+ when :binary
84
+ ::SeisRuby::Data::Sac::Binary
85
+ when :ascii
86
+ ::SeisRuby::Data::Sac::Ascii
87
+ else
88
+ raise ArgumentError, "Unsupported type: #{type}"
4
89
  end
5
90
  end
91
+
92
+ def parse!(type)
93
+ @head, @body = module_from_type(type).parse(@raw_data)
94
+ check_consistency
95
+ end
96
+
97
+ private
98
+
99
+ def check_consistency
100
+
101
+ end
6
102
  end
@@ -1,6 +1,7 @@
1
- module SeisRuby
2
- module Data
3
- require 'ruby_patch'
4
- extend ::RubyPatch::AutoLoad
5
- end
1
+ class ::SeisRuby::Data
2
+ require 'seis_ruby/data/sac'
3
+ require 'seis_ruby/data/cmtsolution'
4
+
5
+ attr_accessor :meta_data # Meta data about raw data.
6
+ attr_accessor :raw_data # Raw data.
6
7
  end
@@ -0,0 +1,30 @@
1
+ module ::SeisRuby::Database::GlobalCmtCatalogSearch::CmtsolutionFormat
2
+ require 'mechanize'
3
+
4
+ class << self
5
+ def load_file(uri)
6
+ agent = Mechanize.new{|a|
7
+ a.pluggable_parser.html = CustomHtmlParser
8
+ }
9
+ agent.get(uri)
10
+
11
+ events\
12
+ = agent.page.search('pre')\
13
+ .find{|e| e.text =~ /event name:/}\
14
+ .text.split("\n\n")\
15
+ .delete_if{|t| t.strip.empty?}\
16
+ .map{|text| ::SeisRuby::Data::Cmtsolution.load(text, {file: uri})}
17
+
18
+ unless next_link = agent.page.links.find{|elem| elem.text == 'More solutions'}
19
+ return events
20
+ end
21
+ events.concat(load_file(next_link.uri))
22
+ end
23
+ end
24
+
25
+ class CustomHtmlParser < ::Mechanize::Page
26
+ def initialize(uri = nil, response = nil, body = nil, code = nil, mech = nil)
27
+ super(uri, response, body.gsub(/<=/, '&lt;='), code, mech)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,44 @@
1
+ module ::SeisRuby::Database::GlobalCmtCatalogSearch
2
+ require 'seis_ruby/database/global_cmt_catalog_search/cmtsolution_format'
3
+
4
+ # Examples of target URI
5
+ # 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&lts=-9999&uts=9999&lpe1=0&upe1=90&lpe2=0&upe2=90&list=4
6
+ # http://www.ldeo.columbia.edu/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&lts=-9999&uts=9999&lpe1=0&upe1=90&lpe2=0&upe2=90&list=4&start=99
7
+ URI_FOR_SELF_REG = /\Ahttp:\/\/www\.(?:globalcmt\.org|ldeo\.columbia\.edu)\/cgi-bin\/globalcmt-cgi-bin\/CMT4\/form\?.*list=([0-5]+).*/
8
+
9
+ class << self
10
+ # @param [String] uri
11
+ # @return [Boolean]
12
+ def uri_for_self?(uri)
13
+ uri =~ URI_FOR_SELF_REG
14
+ end
15
+
16
+ # @param [String] uri URI of a first page of Global CMT Catalog Search result.
17
+ # @return [Array<Hash>] {::SeisRuby::Data::Cmtsolution#parse}
18
+ def load_file(uri)
19
+ raise ArgumentError, "Not a URI of #{self}: #{uri}" unless uri =~ URI_FOR_SELF_REG
20
+ module_from_list_number(Regexp.last_match(1).to_i).load_file(uri)
21
+ end
22
+
23
+ private
24
+
25
+ def module_from_list_number(list_number)
26
+ case list_number
27
+ when 0
28
+ raise NotImplementedError, "Standard"
29
+ when 1
30
+ raise NotImplementedError, "List of event names"
31
+ when 2
32
+ raise NotImplementedError, "GMT psvelomeca input"
33
+ when 3
34
+ raise NotImplementedError, "GMT psmeca input"
35
+ when 4
36
+ ::SeisRuby::Database::GlobalCmtCatalogSearch::CmtsolutionFormat
37
+ when 5
38
+ raise NotImplementedError, "Full format"
39
+ else
40
+ raise ArgumentError, "Unsupported list number: #{list_number}"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module ::SeisRuby::Database
2
+ require 'seis_ruby/database/global_cmt_catalog_search'
3
+ end
@@ -1,3 +1,3 @@
1
1
  module SeisRuby
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/seis_ruby.rb CHANGED
@@ -1,5 +1,14 @@
1
- module SeisRuby
1
+ # @see ::SeisRuby::Command Module functions.
2
+ module ::SeisRuby
3
+ AUTHOR = 'Kasahara Amato'
4
+ LICENSE = 'GNU GENERAL PUBLIC LICENSE Version 3'
5
+
6
+ require 'seis_ruby/core_ext'
2
7
  require 'seis_ruby/version'
3
- require 'ruby_patch'
4
- extend ::RubyPatch::AutoLoad
8
+ require 'seis_ruby/data'
9
+ require 'seis_ruby/database'
10
+ require 'seis_ruby/command'
11
+ require 'seis_ruby/application'
12
+
13
+ extend ::SeisRuby::Command
5
14
  end
data/seis_ruby.gemspec CHANGED
@@ -1,5 +1,4 @@
1
1
  gem_name = File.basename(Dir.pwd)
2
- require 'rainbow'
3
2
  require 'ruby_patch'
4
3
 
5
4
  dir = File.join(__DIR__, 'lib')
@@ -11,29 +10,24 @@ Gem::Specification.new do |s|
11
10
  s.name = gem_name
12
11
  s.summary = "Ruby library for earthquake science."
13
12
  s.version = SeisRuby::VERSION
14
- s.add_development_dependency 'rspec', '~> 2.10'
15
- s.add_development_dependency 'simplecov', '~> 0.6'
16
- s.add_runtime_dependency 'rainbow', '~> 1.1'
13
+ s.add_development_dependency 'watchr', '~> 0.7'
17
14
  s.add_runtime_dependency 'thor', '~> 0.15'
18
15
  s.add_runtime_dependency 'mechanize', '~> 2.5'
19
- s.add_runtime_dependency 'ruby_patch', '~> 1.1'
16
+ s.add_development_dependency 'pry', '>= 0.9.10'
17
+ s.add_development_dependency 'ruby_patch', '>= 1.1.0'
20
18
  s.author = 'kshramt'
21
- s.description = "Ruby library for earthquake science. Provides Parser for various data formats used in seismology."
19
+ s.description = "Ruby library for earthquake science."
22
20
  s.executables << 'seis_ruby'
23
- s.post_install_message = (<<-EOS
21
+ s.post_install_message = <<-EOS
24
22
 
25
23
 
26
- SeisRuby #{::SeisRuby::VERSION}
27
- Thank you for installing.
28
24
  Please execute following command:
29
25
 
30
- #{::SeisRuby::Bin::SeisRubyRunner::COMMAND_NAME} generate_completion
31
-
32
- Enjoy!
26
+ #{::SeisRuby::Application::COMMAND} generate_completion
33
27
 
34
28
 
35
29
  EOS
36
- ).color(:green)
37
- s.required_ruby_version = '~> 1.9'
30
+ s.required_ruby_version = '>= 1.9.3'
38
31
  s.test_files.concat `git ls-files spec`.split.select{|path| path =~ /_spec\.rb/}
32
+ s.test_files.concat `git ls-files test`.split.select{|path| path =~ /_test\.rb/}
39
33
  end
Binary file
@@ -0,0 +1,32 @@
1
+ 0.02500000 -12345.00 -12345.00 1.000000e+10 -12345.00
2
+ 0.0005380000 725.6500 -292.0595 -12345.00 2.000000
3
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
4
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
5
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
6
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
7
+ -12345.00 0.000000 90.00000 1000.000 2.000000
8
+ 0.000000 0.000000 -12345.00 10000.00 7.000000
9
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
10
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
11
+ 10018.76 90.00000 270.0000 90.00000 -12345.00
12
+ -12345.00 -12345.00 0.000000 0.000000 -12345.00
13
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
14
+ -12345.00 -12345.00 -12345.00 -12345.00 -12345.00
15
+ 2012 100 1 2 3
16
+ 4 6 0 0 9
17
+ -12345 -12345 -12345 -12345 -12345
18
+ 1 5 -12345 -12345 -12345
19
+ -12345 -12345 -12345 -12345 -12345
20
+ -12345 -12345 -12345 -12345 -12345
21
+ -12345 -12345 -12345 -12345 -12345
22
+ 1 0 0 1 0
23
+ TEST -12345
24
+ 10 -12345 -12345
25
+ -12345 -12345 -12345
26
+ -12345 -12345 -12345
27
+ -12345 -12345 -12345
28
+ -12345 -12345 -12345
29
+ -12345 -12345 BHZ
30
+ XX -12345 -12345
31
+ -24370.00 -29946.00 -24963.00 -26414.00 -27634.00
32
+ -23068.00 -20123.00 -24239.00 -21714.00
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class ::CommandTest < ::MiniTest::Unit::TestCase
4
+ SAC_ASCII_FILE = "test/data/000000001.SAC_ascii"
5
+ SAC_BINARY_FILE = "test/data/000000001.SAC"
6
+
7
+ def test_load_file_sac_binary
8
+ assert(::SeisRuby.load_file(SAC_BINARY_FILE))
9
+ end
10
+
11
+ def test_load_file_sac_ascii
12
+ assert(::SeisRuby.load_file(SAC_ASCII_FILE))
13
+ end
14
+
15
+ def test_load_file_global_cmt_catalog_search
16
+ assert(::SeisRuby.load_file('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&lts=-9999&uts=9999&lpe1=0&upe1=90&lpe2=0&upe2=90&list=4'))
17
+ assert(::SeisRuby.load_file('http://www.ldeo.columbia.edu/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&lts=-9999&uts=9999&lpe1=0&upe1=90&lpe2=0&upe2=90&list=4&start=99'))
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class FileTest_ < ::MiniTest::Unit::TestCase
4
+ def test_read_uri
5
+ assert(::File.read_uri('http://www.google.com'))
6
+ end
7
+ end
@@ -0,0 +1,63 @@
1
+ require 'test_helper'
2
+
3
+ class ::CmtsolutionTest < ::MiniTest::Unit::TestCase
4
+ RAW_DATA = <<-EOS
5
+
6
+
7
+
8
+ MLI 1976 1 2 3 29 39.60 -28.6100 -177.6400 59.0 6.2 0.0 KERMADEC ISLANDS REGION
9
+ event name: 010176A
10
+ time shift: 13.8000
11
+ half duration: 9.4000
12
+ latitude: -29.2500
13
+ longitude: -176.9600
14
+ depth: 47.8000
15
+ Mrr: 7.680000e+26
16
+ Mtt: 9.000000e+24
17
+ Mpp: -7.770000e+26
18
+ Mrt: 1.390000e+26
19
+ Mrp: 4.520000e+26
20
+ Mtp: -3.260000e+26
21
+
22
+
23
+ EOS
24
+
25
+ def test_load
26
+ cmtsolution = ::SeisRuby::Data::Cmtsolution.load(RAW_DATA, {file: '(test)'})
27
+ hypocenter_table = {
28
+ data_source: 'MLI',
29
+ year: 1976,
30
+ month: 1,
31
+ day: 2,
32
+ hour: 3,
33
+ minute: 29,
34
+ second: 39.6,
35
+ latitude: -28.61,
36
+ longitude: -177.64,
37
+ depth: 59.0,
38
+ mb: 6.2,
39
+ ms: 0.0,
40
+ region_name: 'KERMADEC ISLANDS REGION',
41
+ }
42
+ centroid_table = {
43
+ event_name: '010176A',
44
+ time_shift: 13.8,
45
+ half_duration: 9.4,
46
+ latitude: -29.25,
47
+ longitude: -176.96,
48
+ depth: 47.8,
49
+ mrr: 7.68e+26,
50
+ mtt: 9.0e+24,
51
+ mpp: -7.77e+26,
52
+ mrt: 1.39e+26,
53
+ mrp: 4.52e+26,
54
+ mtp: -3.26e+26,
55
+ }
56
+ assert_equal(hypocenter_table, cmtsolution.hypocenter.table)
57
+ assert_equal(centroid_table, cmtsolution.centroid.table)
58
+ end
59
+
60
+ def test_time
61
+ assert_equal(Time.gm(1976, 1, 2, 3, 29, 39, 600000), ::SeisRuby::Data::Cmtsolution.load(RAW_DATA, {file: '(test)'}).hypocenter.origin_time)
62
+ end
63
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class ::AsciiTest < ::MiniTest::Unit::TestCase
4
+ RAW_DATA = File.read "test/data/000000001.SAC_ascii"
5
+
6
+ def test_parse
7
+ assert_equal(
8
+ [Hash, Array],
9
+ ::SeisRuby::Data::Sac::Ascii.parse(RAW_DATA).map(&:class))
10
+ end
11
+
12
+ def test_dump
13
+ assert(::SeisRuby::Data::Sac::Binary.dump({iftype: :itime}, []))
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class ::BinaryTest < ::MiniTest::Unit::TestCase
4
+ RAW_DATA = File.read("test/data/000000001.SAC")
5
+
6
+ def test_parse
7
+ assert_equal(
8
+ [Hash, Array],
9
+ ::SeisRuby::Data::Sac::Binary.parse(RAW_DATA).map(&:class))
10
+ end
11
+
12
+ def test_dump
13
+ assert(::SeisRuby::Data::Sac::Binary.dump({iftype: :itime}, []))
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class ::BodyTest < ::MiniTest::Unit::TestCase
4
+ T = ::SeisRuby::Data::Sac::Body
5
+
6
+ def test_shape_body
7
+ [
8
+ [[1, 2, 3, 4], {iftype: :itime}, [1, 2, 3, 4]],
9
+ [[[3, 1], [4, 2]], {iftype: :ixy}, [1, 2, 3, 4]],
10
+ [[[3, 1], [4, 2]], {iftype: :iamph}, [1, 2, 3, 4]],
11
+ [[Complex(1, 3), Complex(2, 4)], {iftype: :irlim}, [1, 2, 3, 4]],
12
+ [[[1, 3, 5], [2, 4, 6]], {iftype: :ixyz, nxsize: 2}, [1, 2, 3, 4, 5, 6]],
13
+ ].each{|expect, head, body|
14
+ assert_equal(expect, T.shape_body(body, head))}
15
+ end
16
+
17
+ def test_body_for_dump
18
+ [
19
+ [[1, 2, 3, 4], {iftype: :itime}, [1, 2, 3, 4]],
20
+ [[1, 2, 3, 4], {iftype: :ixy}, [[3, 1], [4, 2]]],
21
+ [[1, 2, 3, 4], {iftype: :iamph}, [[3, 1], [4, 2]]],
22
+ [[1, 2, 3, 4], {iftype: :irlim}, [Complex(1, 3), Complex(2, 4)]],
23
+ [[1, 2, 3, 4, 5, 6], {iftype: :ixyz, nxsize: 2}, [[1, 3, 5], [2, 4, 6]]],
24
+ ].each{|expect, head, body|
25
+ assert_equal(expect, T.body_for_dump(body, head))}
26
+ end
27
+ end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+
3
+ class HeadTest < ::MiniTest::Unit::TestCase
4
+ require 'ostruct'
5
+
6
+ # undefined, valid, invalid
7
+ # :f -12345.0, 42.0, :sym
8
+ # :n -12345, 42, :sym
9
+ # :i -12445, :sym, (-1, 0, 87, 88)
10
+ # :l -12345, (1, 0), /
11
+ # :k "-12345", "42", /
12
+
13
+ T = ::SeisRuby::Data::Sac::Head
14
+
15
+ F_FROM = T::CONVERTER_FROM_HEAD_FLOAT
16
+ N_FROM = T::CONVERTER_FROM_HEAD_INTEGER
17
+ I_FROM = T::CONVERTER_FROM_HEAD_ENUMERATED_VALUE
18
+ L_FROM = T::CONVERTER_FROM_HEAD_LOGICAL
19
+ K_FROM = T::CONVERTER_FROM_HEAD_STRING
20
+
21
+ def test_convert_from_head_undefined
22
+ assert_nil(F_FROM.call(-12345.0))
23
+ assert_nil(N_FROM.call(-12345))
24
+ assert_nil(I_FROM.call(-12345))
25
+ assert_nil(K_FROM.call("-12345"))
26
+ end
27
+
28
+ def test_convert_from_head_valid
29
+ assert_equal(42.0, F_FROM.call(42.0))
30
+ assert_equal(42, N_FROM.call(42))
31
+ assert_equal(:itime, I_FROM.call(1))
32
+ assert_equal(:ipostq, I_FROM.call(42))
33
+ assert_equal(:iu, I_FROM.call(86))
34
+ assert_equal(true, L_FROM.call(1))
35
+ assert_equal(false, L_FROM.call(0))
36
+ assert_equal("42", K_FROM.call("42"))
37
+ end
38
+
39
+ def test_convert_from_head_invalid
40
+ assert_raises(ArgumentError, NoMethodError){F_FROM.call(:'1.0')}
41
+ assert_raises(ArgumentError, NoMethodError){N_FROM.call(:'1')}
42
+ assert_raises(ArgumentError, NoMethodError){I_FROM.call(-1)}
43
+ assert_raises(ArgumentError, NoMethodError){I_FROM.call(0)}
44
+ assert_raises(ArgumentError, NoMethodError){I_FROM.call(87)}
45
+ assert_raises(ArgumentError, NoMethodError){I_FROM.call(88)}
46
+ end
47
+
48
+ F_TO = T::CONVERTER_TO_HEAD_FLOAT
49
+ N_TO = T::CONVERTER_TO_HEAD_INTEGER
50
+ I_TO = T::CONVERTER_TO_HEAD_ENUMERATED_VALUE
51
+ L_TO = T::CONVERTER_TO_HEAD_LOGICAL
52
+ K_TO = T::CONVERTER_TO_HEAD_STRING
53
+
54
+ def test_convert_to_head_undefined
55
+ assert_equal(-12345.0, F_TO.call(nil))
56
+ assert_equal(-12345, N_TO.call(nil))
57
+ assert_equal(-12345, I_TO.call(nil))
58
+ assert_equal(0, L_TO.call(nil))
59
+ assert_equal("-12345", K_TO.call(nil))
60
+ end
61
+
62
+ def test_convert_to_head_valid
63
+ assert_equal(42.0, F_TO.call(42.0))
64
+ assert_equal(42, N_TO.call(42))
65
+ assert_equal(1, I_TO.call(:itime))
66
+ assert_equal(42, I_TO.call(:ipostq))
67
+ assert_equal(86, I_TO.call(:iu))
68
+ assert_equal(1, L_TO.call(true))
69
+ assert_equal(0, L_TO.call(false))
70
+ assert_equal("42", K_TO.call("42"))
71
+ end
72
+
73
+ def test_convert_to_head_invalid
74
+ assert_raises(ArgumentError, NoMethodError){F_TO.call(:"42.0")}
75
+ assert_raises(ArgumentError, NoMethodError){N_TO.call(:"42")}
76
+ assert_raises(ArgumentError, NoMethodError){I_TO.call(:this_is_not_exist)}
77
+ end
78
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class ::SacTest < ::MiniTest::Unit::TestCase
4
+ ASCII_FILE = "test/data/000000001.SAC_ascii"
5
+ BINARY_FILE = "test/data/000000001.SAC"
6
+ def test_new_binary
7
+ assert(::SeisRuby::Data::Sac.new(File.read(BINARY_FILE), :binary))
8
+ end
9
+
10
+ def test_new_ascii
11
+ assert(::SeisRuby::Data::Sac.new(File.read(ASCII_FILE), :ascii))
12
+ end
13
+
14
+ def test_load
15
+ assert(::SeisRuby::Data::Sac.load(File.read(BINARY_FILE), :binary))
16
+ end
17
+
18
+ def test_parse_binary
19
+ sac = ::SeisRuby::Data::Sac.load(File.read(BINARY_FILE), :binary)
20
+ assert(sac.head)
21
+ assert_equal(Hash, sac.head.class)
22
+ assert(sac.body)
23
+ assert_equal(sac.body.class, Array)
24
+ end
25
+
26
+ def test_load_file
27
+ assert(::SeisRuby::Data::Sac.load_file(BINARY_FILE))
28
+ end
29
+
30
+ def test_dump
31
+ sac = ::SeisRuby::Data::Sac.load_file(BINARY_FILE)
32
+ assert(sac.dump(:binary))
33
+ end
34
+
35
+ def test_dump_file
36
+ assert(::SeisRuby::Data::Sac.load_file(BINARY_FILE).dump_file(BINARY_FILE + '.sac'))
37
+ end
38
+
39
+ def test_binary_not_break_file
40
+ sac_binary_original = ::SeisRuby::Data::Sac.load_file(BINARY_FILE)
41
+ sac_binary_original.dump_file(BINARY_FILE + '.sac')
42
+ sac_binary_dumped = ::SeisRuby::Data::Sac.load_file(BINARY_FILE + '.sac')
43
+ assert_equal(sac_binary_original, sac_binary_dumped)
44
+ end
45
+
46
+ def test_ascii_not_break_file
47
+ sac_ascii_original = ::SeisRuby::Data::Sac.load_file(ASCII_FILE)
48
+ sac_ascii_original.dump_file(ASCII_FILE + '.sac_ascii')
49
+ sac_ascii_dumped = ::SeisRuby::Data::Sac.load_file(ASCII_FILE + '.sac_ascii')
50
+ assert_equal(sac_ascii_original, sac_ascii_dumped)
51
+ end
52
+ end