eew_parser 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/eew_parser.gemspec +1 -1
  3. data/lib/eew_parser.rb +110 -103
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 21123397596c23d603c3effd0d986bce0ced93206de76c2d14e560f5137045db
4
- data.tar.gz: eece41255984ffe5d4f5b7a2f789f32257ea362b2701b6fa762d485f99e7e422
3
+ metadata.gz: debf68a60210ffc8a9aea6eee399711a8907c6662cdfb02bc93923d6a37f24d6
4
+ data.tar.gz: c4b6303e6dba4fb27cc1623f7e02a3ed605a2aeb397b5089001d63c50bd63cfe
5
5
  SHA512:
6
- metadata.gz: fd0cde626f2c1d2e9919f7ac9ee096595079a1b177883cee6a5016ad3b182f1a71fb87bd155dbc631546ab4ac6d8263cfa0879e92a128788afd04e207ef51238
7
- data.tar.gz: a487e06a2191dbc8f769df9f1ed043c9e2a38c3da6a03ad9b03c9b52a0ef80a1bff243034a98a438ec894a229220ede3ff2d634ea8d9aa55748bdc703878ba9b
6
+ metadata.gz: 25d5e66cc6ff58a553cff6f1254a2ecb81a557a23be2d8d199de02e17a2a606ef128667fc3308edcdfb2600bbee6461d1afed4d5741f76af0b76e60bd1f77dd4
7
+ data.tar.gz: 87b0e325685d853eb8447399034efa4804fa4d24d9e1fe7408ec1d0326ebd2c7e9867365da8bc08b952f4cb12723d68f614f5cb0d40d9ab4c74b90f638b7a1e7
data/eew_parser.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "eew_parser"
3
- spec.version = "0.2.0"
3
+ spec.version = "0.2.1"
4
4
  spec.authors = ["Masaki Matsushita"]
5
5
  spec.email = ["glass.saga@gmail.com"]
6
6
  spec.summary = %q{Parser for Earthquake Early Warning from JMA}
data/lib/eew_parser.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require "time"
4
+
3
5
  require_relative "epicenter_code"
4
6
  require_relative "area_code"
5
7
 
@@ -30,110 +32,39 @@ module EEW
30
32
  @fastcast = str.dup
31
33
  @fastcast.force_encoding(Encoding::ASCII)
32
34
  @fastcast.freeze
33
- raise Error, "電文の形式が不正です" if @fastcast.bytesize < 135
35
+ raise Error, "電文のサイズが不正です" if @fastcast.bytesize < 135
34
36
  end
35
37
 
36
38
  attr_reader :fastcast
37
39
 
38
- # initializeに与えられた電文を返します。
39
- def to_s
40
- @fastcast.dup
41
- end
42
-
43
- # 電文のサイズを返します。
44
- def size
45
- @fastcast.bytesize
40
+ def inspect
41
+ "#<EEWParser:#{id} (第#{number}報) #{epicenter} 震度#{seismic_intensity}>"
46
42
  end
47
43
 
48
- # 緊急地震速報の内容をテキストで出力します。
49
- def print
50
- return @print.dup if @print
51
-
52
- @print = <<-EOS
53
- 緊急地震速報 (第#{number}報)
54
- 電文種別: #{type}
55
- 発信官署: #{from}
56
- 訓練等の識別符: #{drill_type}
57
- 電文の発表時刻: #{report_time.strftime("%F %T")}
58
- 電文がこの電文を含め何通あるか: #{number_of_telegram}
59
- コードが続くかどうか: #{continue?}
60
- 地震発生時刻もしくは地震検知時刻: #{earthquake_time.strftime("%F %T")}
61
- 地震識別番号: #{id}
62
- 発表状況(訂正等)の指示: #{status}
63
- 発表する高度利用者向け緊急地震速報の番号(地震単位での通番): #{number}
64
- 震央地名: #{epicenter}
65
- 震央の位置: #{position}
66
- 震源の深さ(単位 km)(不明・未設定時,キャンセル時:///): #{depth}
67
- マグニチュード(不明・未設定時、キャンセル時:///): #{magnitude}
68
- 最大予測震度(不明・未設定時、キャンセル時://): #{seismic_intensity}
69
- 震央の確からしさ: #{probability_of_position}
70
- 震源の深さの確からしさ: #{probability_of_depth}
71
- マグニチュードの確からしさ: #{probability_of_magnitude}
72
- 震央の確からしさ(気象庁の部内システムでの利用): #{probability_of_position_jma}
73
- 震源の深さの確からしさ(気象庁の部内システムでの利用): #{probability_of_depth_jma}
74
- 震央位置の海陸判定: #{land_or_sea}
75
- 警報を含む内容かどうか: #{warning?}
76
- 予測手法: #{prediction_method}
77
- 最大予測震度の変化: #{change}
78
- 最大予測震度の変化の理由: #{reason_of_change}
79
- EOS
80
-
81
- if has_ebi?
82
- @print << "\n地域毎の警報の判別、最大予測震度及び主要動到達予測時刻(EBI):\n"
83
- ebi.each do |local|
84
- arrival_time = local[:arrival] ? "すでに到達" : local[:arrival_time]&.strftime("%T")
85
- @print << "#{local[:area_name]} 最大予測震度: #{local[:intensity]} 予想到達時刻: #{arrival_time} 警報: #{local[:warning]}\n"
86
- end
87
- end
88
-
89
- @print.freeze
90
- return @print.dup
44
+ def ==(other)
45
+ @fastcast == other.fastcast
91
46
  end
92
47
 
93
- Attributes = [
94
- :type, :from, :drill_type, :report_time, :number_of_telegram, :continue?, :earthquake_time, :id, :status, :final?, :number, :epicenter, :position, :depth,
95
- :magnitude, :seismic_intensity, :observation_points_of_magnitude, :probability_of_depth, :probability_of_magnitude, :probability_of_position, :probability_of_depth_jma,
96
- :land_or_sea, :warning?, :prediction_method, :change, :reason_of_change, :ebi
97
- ].freeze
98
-
99
- # 電文を解析した結果をHashで返します。
100
- def to_hash
101
- unless @hash
102
- @hash = {}
103
- Attributes.each do |attribute|
104
- @hash[attribute] = __send__(attribute)
105
- end
106
- @hash.freeze
107
- end
108
- return @hash.dup
48
+ def <=>(other)
49
+ id + number <=> other.id + other.number
109
50
  end
110
51
 
111
- # 正しい電文であるかを返します
112
- def valid?
113
- unless @valid
114
- begin
115
- Attributes.each do |attribute|
116
- __send__(attribute)
117
- end
118
- rescue Error
119
- @valid = false
120
- else
121
- @valid = true
122
- end
123
- end
124
- return @valid
52
+ def eql?(other)
53
+ @fastcast.eql?(other.fastcast)
125
54
  end
126
55
 
127
- def inspect
128
- "#<EEWParser:#{id} (第#{number}報) #{epicenter} 震度#{seismic_intensity}>"
56
+ def hash
57
+ @fastcast.hash
129
58
  end
130
59
 
131
- def ==(other)
132
- fastcast == other.fastcast
60
+ # initializeに与えられた電文を返します。
61
+ def to_s
62
+ @fastcast.dup
133
63
  end
134
64
 
135
- def <=>(other)
136
- __id__ <=> other.__id__
65
+ # 電文のサイズを返します。
66
+ def size
67
+ @fastcast.bytesize
137
68
  end
138
69
 
139
70
  # 電文種別コード
@@ -212,7 +143,7 @@ module EEW
212
143
 
213
144
  # 電文の発表時刻のTimeオブジェクトを返します。
214
145
  def report_time
215
- Time.local("20" + @fastcast[9, 2], @fastcast[11, 2], @fastcast[13, 2], @fastcast[15, 2], @fastcast[17, 2], @fastcast[19, 2])
146
+ Time.strptime(@fastcast[9, 12], "%y%m%d%H%M%S")
216
147
  rescue ArgumentError
217
148
  raise Error, "電文の形式が不正です (発表時刻: #{@fastcast[9, 12]})"
218
149
  end
@@ -239,25 +170,21 @@ module EEW
239
170
 
240
171
  # 地震発生時刻もしくは地震検知時刻のTimeオブジェクトを返します。
241
172
  def earthquake_time
242
- Time.local("20" + @fastcast[26, 2], @fastcast[28, 2], @fastcast[30, 2], @fastcast[32, 2], @fastcast[34, 2], @fastcast[36, 2])
173
+ Time.strptime(@fastcast[26, 12], "%y%m%d%H%M%S")
243
174
  rescue ArgumentError
244
175
  raise Error, "電文の形式が不正です (地震発生時刻: #{@fastcast[26, 12]})"
245
176
  end
246
177
 
247
178
  # 地震識別番号(String)
248
179
  def id
249
- id = @fastcast[41, 14]
250
- Integer(id, 10) # verify
251
- return id
180
+ return @id if @id
181
+ @id = @fastcast[41, 14]
182
+ Integer(@id, 10) # verify
183
+ return @id
252
184
  rescue ArgumentError
253
185
  raise Error, "電文の形式が不正です(地震識別番号: #{id})"
254
186
  end
255
187
 
256
- # 地震識別番号 + 通番
257
- def __id__
258
- (id * 10) + number
259
- end
260
-
261
188
  # 発表状況(訂正等)の指示
262
189
  def status
263
190
  case @fastcast[59]
@@ -370,6 +297,7 @@ module EEW
370
297
  "6+" => "6強",
371
298
  "07" => "7"
372
299
  }.freeze
300
+ private_constant :SeismicIntensity
373
301
 
374
302
  # 最大予測震度
375
303
  def seismic_intensity
@@ -388,6 +316,7 @@ module EEW
388
316
  "9" => "予備",
389
317
  "/" =>"不明又は未設定"
390
318
  }.freeze
319
+ private_constant :OriginProbability
391
320
 
392
321
  # 震央の確からしさ
393
322
  def probability_of_position
@@ -453,7 +382,7 @@ module EEW
453
382
  end
454
383
  end
455
384
 
456
- # 後方互換性のため
385
+ # 互換性のため
457
386
  alias probability_of_position_jma observation_points_of_magnitude
458
387
 
459
388
  # 震源の深さの確からしさ(※気象庁の部内システムでの利用)
@@ -610,6 +539,85 @@ module EEW
610
539
  return @ebi.dup
611
540
  end
612
541
 
542
+ Attributes = [
543
+ :type, :from, :drill_type, :report_time, :number_of_telegram, :continue?, :earthquake_time, :id, :status, :final?, :number, :epicenter, :position, :depth,
544
+ :magnitude, :seismic_intensity, :observation_points_of_magnitude, :probability_of_depth, :probability_of_magnitude, :probability_of_position, :probability_of_depth_jma,
545
+ :land_or_sea, :warning?, :prediction_method, :change, :reason_of_change, :ebi
546
+ ].freeze
547
+ private_constant :Attributes
548
+
549
+ # 電文を解析した結果をHashで返します。
550
+ def to_hash
551
+ unless @hash
552
+ @hash = {}
553
+ Attributes.each do |attribute|
554
+ @hash[attribute] = __send__(attribute)
555
+ end
556
+ @hash.freeze
557
+ end
558
+ return @hash.dup
559
+ end
560
+
561
+ # 正しい電文であるかを返します
562
+ def valid?
563
+ unless @valid
564
+ begin
565
+ Attributes.each do |attribute|
566
+ __send__(attribute)
567
+ end
568
+ rescue Error
569
+ @valid = false
570
+ else
571
+ @valid = true
572
+ end
573
+ end
574
+ return @valid
575
+ end
576
+
577
+ # 緊急地震速報の内容をテキストで出力します。
578
+ def print
579
+ return @print.dup if @print
580
+
581
+ @print = <<-EOS
582
+ 緊急地震速報 (第#{number}報)
583
+ 電文種別: #{type}
584
+ 発信官署: #{from}
585
+ 訓練等の識別符: #{drill_type}
586
+ 電文の発表時刻: #{report_time.strftime("%F %T")}
587
+ 電文がこの電文を含め何通あるか: #{number_of_telegram}
588
+ コードが続くかどうか: #{continue?}
589
+ 地震発生時刻もしくは地震検知時刻: #{earthquake_time.strftime("%F %T")}
590
+ 地震識別番号: #{id}
591
+ 発表状況(訂正等)の指示: #{status}
592
+ 発表する高度利用者向け緊急地震速報の番号(地震単位での通番): #{number}
593
+ 震央地名: #{epicenter}
594
+ 震央の位置: #{position}
595
+ 震源の深さ(単位 km)(不明・未設定時,キャンセル時:///): #{depth}
596
+ マグニチュード(不明・未設定時、キャンセル時:///): #{magnitude}
597
+ 最大予測震度(不明・未設定時、キャンセル時://): #{seismic_intensity}
598
+ 震央の確からしさ: #{probability_of_position}
599
+ 震源の深さの確からしさ: #{probability_of_depth}
600
+ マグニチュードの確からしさ: #{probability_of_magnitude}
601
+ 震源の深さの確からしさ(気象庁の部内システムでの利用): #{probability_of_depth_jma}
602
+ 震央位置の海陸判定: #{land_or_sea}
603
+ 警報を含む内容かどうか: #{warning?}
604
+ 予測手法: #{prediction_method}
605
+ 最大予測震度の変化: #{change}
606
+ 最大予測震度の変化の理由: #{reason_of_change}
607
+ EOS
608
+
609
+ if has_ebi?
610
+ @print << "\n地域毎の警報の判別、最大予測震度及び主要動到達予測時刻(EBI):\n"
611
+ ebi.each do |local|
612
+ arrival_time = local[:arrival] ? "すでに到達" : local[:arrival_time]&.strftime("%T")
613
+ @print << "#{local[:area_name].ljust(10)} 最大予測震度: #{local[:intensity].ljust(2)} 予想到達時刻: #{arrival_time} 警報: #{local[:warning]}\n"
614
+ end
615
+ end
616
+
617
+ @print.freeze
618
+ return @print.dup
619
+ end
620
+
613
621
  private
614
622
 
615
623
  # 電文フォーマットの震度を文字列に変換
@@ -639,8 +647,9 @@ module EEW
639
647
 
640
648
  # 予想到達時刻
641
649
  def ebi_arrival_time(local_str)
642
- return nil if local_str[10, 6] == "//////"
643
- return Time.local("20" + @fastcast[26, 2], @fastcast[28, 2], @fastcast[30, 2], local_str[10, 2], local_str[12, 2], local_str[14, 2])
650
+ arrival_time = local_str[10, 6]
651
+ return nil if arrival_time == "//////"
652
+ return Time.strptime(arrival_time, "%H%M%S")
644
653
  rescue ArgumentError
645
654
  raise Error, "電文の形式が不正です (EBI: 地震発生時刻)"
646
655
  end
@@ -673,8 +682,6 @@ module EEW
673
682
  end
674
683
  end
675
684
 
676
- EEWParser = EEW::Parser
677
-
678
685
  if __FILE__ == $PROGRAM_NAME # テスト
679
686
  str = <<EOS #テスト用の電文(EBIを含む)
680
687
  37 03 00 110415233453 C11
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eew_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Matsushita