Class: JMACode::AreaForecastLocal
- Inherits:
-
Struct
- Object
- Struct
- JMACode::AreaForecastLocal
- Defined in:
- lib/jma_code/area_forecast_local.rb
Constant Summary collapse
- CSV_ROW_SEP =
"\r\n"- NUM_HEADER_ROWS =
4- HEADERS =
%i( code name name_phonetic used_by_area_forecast_type3_in_weather_alert used_by_area_forecast_type2_in_weather_alert used_by_area_forecast_type1_in_weather_alert used_by_weather_forecast used_by_weather_description used_by_next_weather_forecast used_by_landslide_alert used_by_area_forecast_type3_in_tornado_alert used_by_area_forecast_type2_in_tornado_alert used_by_area_forecast_type1_in_tornado_alert used_by_recorded_heavy_rain_alert used_by_flood_alert used_by_area_forecast_type3_in_typhoon_probability used_by_area_forecast_type1_in_typhoon_probability belonging_local_code_in_weather_alert belonging_local_code_in_tornado_alert )
Class Attribute Summary collapse
-
.data ⇒ Object
Returns the value of attribute data.
Instance Attribute Summary collapse
-
#belonging_local_code_in_tornado_alert ⇒ Object
Returns the value of attribute belonging_local_code_in_tornado_alert.
-
#belonging_local_code_in_weather_alert ⇒ Object
Returns the value of attribute belonging_local_code_in_weather_alert.
-
#code ⇒ Object
Returns the value of attribute code.
-
#name ⇒ Object
Returns the value of attribute name.
-
#name_phonetic ⇒ Object
Returns the value of attribute name_phonetic.
-
#used_by ⇒ Object
Returns the value of attribute used_by.
Class Method Summary collapse
- .build_by_csv_row(row) ⇒ Object
- .get ⇒ Object
- .load(**args) ⇒ Object
- .load_csv(version: "20240216-completed") ⇒ Object
- .load_relation(version: "20240216") ⇒ Object
Instance Method Summary collapse
- #any_belonging_locals ⇒ Object
- #area_information_cities ⇒ Object
- #belonging_local_in_tornado_alert ⇒ Object
- #belonging_local_in_weather_alert ⇒ Object
- #to_csv_row ⇒ Object
Class Attribute Details
.data ⇒ Object
Returns the value of attribute data.
37 38 39 |
# File 'lib/jma_code/area_forecast_local.rb', line 37 def data @data end |
Instance Attribute Details
#belonging_local_code_in_tornado_alert ⇒ Object
Returns the value of attribute belonging_local_code_in_tornado_alert
5 6 7 |
# File 'lib/jma_code/area_forecast_local.rb', line 5 def belonging_local_code_in_tornado_alert @belonging_local_code_in_tornado_alert end |
#belonging_local_code_in_weather_alert ⇒ Object
Returns the value of attribute belonging_local_code_in_weather_alert
5 6 7 |
# File 'lib/jma_code/area_forecast_local.rb', line 5 def belonging_local_code_in_weather_alert @belonging_local_code_in_weather_alert end |
#code ⇒ Object
Returns the value of attribute code
5 6 7 |
# File 'lib/jma_code/area_forecast_local.rb', line 5 def code @code end |
#name ⇒ Object
Returns the value of attribute name
5 6 7 |
# File 'lib/jma_code/area_forecast_local.rb', line 5 def name @name end |
#name_phonetic ⇒ Object
Returns the value of attribute name_phonetic
5 6 7 |
# File 'lib/jma_code/area_forecast_local.rb', line 5 def name_phonetic @name_phonetic end |
#used_by ⇒ Object
Returns the value of attribute used_by
5 6 7 |
# File 'lib/jma_code/area_forecast_local.rb', line 5 def used_by @used_by end |
Class Method Details
.build_by_csv_row(row) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/jma_code/area_forecast_local.rb', line 83 def build_by_csv_row(row) used_by_fields = HEADERS.select{|n| n.to_s.start_with?('used_by_')} new( code: row[:code], name: row[:name], name_phonetic: row[:name_phonetic], belonging_local_code_in_weather_alert: row[:belonging_local_code_in_weather_alert], belonging_local_code_in_tornado_alert: row[:belonging_local_code_in_tornado_alert], used_by: used_by_fields.select{|f| row[f] == '1'}.map{|f| f.to_s.sub(/\Aused_by_/, '').to_sym} ) end |
.get ⇒ Object
55 56 57 |
# File 'lib/jma_code/area_forecast_local.rb', line 55 def get @data ||= load end |
.load(**args) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/jma_code/area_forecast_local.rb', line 47 def load(**args) load_csv(**args) do |csv| csv.drop(NUM_HEADER_ROWS).map do |row| build_by_csv_row(row) end end end |
.load_csv(version: "20240216-completed") ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/jma_code/area_forecast_local.rb', line 39 def load_csv(version: "20240216-completed") path = File.join(File.dirname(__FILE__), "../../data/#{version}_AreaInformationCity-AreaForecastLocalM/AreaForecastLocalM(コード表).csv") File.open(path) do |f| csv = CSV.new(f, headers: HEADERS, row_sep: CSV_ROW_SEP) yield(csv) end end |
.load_relation(version: "20240216") ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jma_code/area_forecast_local.rb', line 59 def load_relation(version: "20240216") headers = %i(code_type3 name_type3 code_type2 name_type2 code_type1 name_type1) path1 = File.join(File.dirname(__FILE__), "../../data/#{version}_AreaInformationCity-AreaForecastLocalM/AreaForecastLocalM(関係表 警報・注意報.csv") relation1 = File.open(path1) do |f| csv = CSV.new(f, headers: headers, row_sep: CSV_ROW_SEP) csv.drop(3).map do |row| [row[:code_type3], row[:code_type2], row[:code_type1]] end end path2 = File.join(File.dirname(__FILE__), "../../data/#{version}_AreaInformationCity-AreaForecastLocalM/AreaForecastLocalM(関係表 竜巻注意情報.csv") relation2 = File.open(path2) do |f| csv = CSV.new(f, headers: headers, row_sep: CSV_ROW_SEP) csv.drop(3).map do |row| [row[:code_type3], row[:code_type2], row[:code_type1]] end end Struct.new(:relation_weather_alert, :relation_tornado_alert, keyword_init: true).new({ relation_weather_alert: relation1, relation_tornado_alert: relation2 }) end |
Instance Method Details
#any_belonging_locals ⇒ Object
115 116 117 |
# File 'lib/jma_code/area_forecast_local.rb', line 115 def any_belonging_locals [belonging_local_in_weather_alert, belonging_local_in_tornado_alert].compact.uniq(&:code) end |
#area_information_cities ⇒ Object
97 98 99 |
# File 'lib/jma_code/area_forecast_local.rb', line 97 def area_information_cities @area_information_cities ||= AreaInformationCity.get.select{|x| x.area_forecast_local_code == code} end |
#belonging_local_in_tornado_alert ⇒ Object
108 109 110 111 112 113 |
# File 'lib/jma_code/area_forecast_local.rb', line 108 def belonging_local_in_tornado_alert return nil if belonging_local_code_in_tornado_alert.blank? @belonging_local_in_tornado_alert ||= begin AreaForecastLocal.get.find{|a| a.code == belonging_local_code_in_tornado_alert} end end |
#belonging_local_in_weather_alert ⇒ Object
101 102 103 104 105 106 |
# File 'lib/jma_code/area_forecast_local.rb', line 101 def belonging_local_in_weather_alert return nil if belonging_local_code_in_weather_alert.blank? @belonging_local_in_weather_alert ||= begin AreaForecastLocal.get.find{|a| a.code == belonging_local_code_in_weather_alert} end end |
#to_csv_row ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/jma_code/area_forecast_local.rb', line 119 def to_csv_row HEADERS.map do |k| respond_to?(k) ? public_send(k) : nil end end |