Class: JMACode::AreaForecastLocal

Inherits:
Struct
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.dataObject

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_alertObject

Returns the value of attribute belonging_local_code_in_tornado_alert

Returns:

  • (Object)

    the current value of 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_alertObject

Returns the value of attribute belonging_local_code_in_weather_alert

Returns:

  • (Object)

    the current value of 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

#codeObject

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



5
6
7
# File 'lib/jma_code/area_forecast_local.rb', line 5

def code
  @code
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/jma_code/area_forecast_local.rb', line 5

def name
  @name
end

#name_phoneticObject

Returns the value of attribute name_phonetic

Returns:

  • (Object)

    the current value of name_phonetic



5
6
7
# File 'lib/jma_code/area_forecast_local.rb', line 5

def name_phonetic
  @name_phonetic
end

#used_byObject

Returns the value of attribute used_by

Returns:

  • (Object)

    the current value of 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

.build_tree(areas = nil, cities = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jma_code/area_forecast_local.rb', line 96

def build_tree(areas=nil, cities=nil)
  areas ||= get
  cities ||= JMACode::AreaInformationCity.get

  toplevels, areas = areas.partition{|a| a.any_belonging_locals.blank?}
  pref_areas = areas.group_by(&:prefecture_code)
  pref_cities = cities.group_by(&:prefecture_code)
  toplevels.group_by(&:prefecture).map{|pref, pref_toplevels|
    current_areas = pref_areas[pref.code]
    current_cities = pref_cities[pref.code]

    [
      block_given? ? yield(pref) : pref, 
      pref_toplevels.map{|t|
        secondlevels = (current_areas+current_cities).select{|a| a.child_of?(t)}
        secondlevels_children = secondlevels.map{|s|
          thirdlevels = (current_areas+current_cities).select{|a| a.child_of?(s)}
          thirdlevels_children = thirdlevels.map{|th| 
            forthlevels = current_cities.select{|c| c.child_of?(th)}
            [
              block_given? ? yield(th) : th, 
              forthlevels.map{|f|
                [block_given? ? yield(f) : f, nil]
              }
            ]
          }
          [
            block_given? ? yield(s) : s,
            thirdlevels_children
          ]
        }
        [
          block_given? ? yield(t) : t,
          secondlevels_children
        ]
      }
    ]

  }
end

.getObject



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

.walk_tree(tree, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/jma_code/area_forecast_local.rb', line 137

def walk_tree(tree, &block)
  tree.map do |area, children|
    a = block.call(area)
    c = if children.is_a?(Array) and children.present?
      walk_tree(children, &block)
    else
      children
    end
    [a, c]
  end
end

Instance Method Details

#any_ancestry_localsObject



180
181
182
183
184
185
186
187
# File 'lib/jma_code/area_forecast_local.rb', line 180

def any_ancestry_locals
  @any_ancestry_locals ||= [
    belonging_local_in_weather_alert, 
    belonging_local_in_tornado_alert, 
    belonging_local_in_weather_alert&.belonging_local_in_weather_alert, 
    belonging_local_in_tornado_alert&.belonging_local_in_tornado_alert, 
  ].compact.uniq(&:code)
end

#any_belonging_localsObject



176
177
178
# File 'lib/jma_code/area_forecast_local.rb', line 176

def any_belonging_locals
  @any_belonging_locals ||= [belonging_local_in_weather_alert, belonging_local_in_tornado_alert].compact.uniq(&:code)
end

#area_information_citiesObject



158
159
160
# File 'lib/jma_code/area_forecast_local.rb', line 158

def area_information_cities
  @area_information_cities ||= AreaInformationCity.get.select{|x| x.area_forecast_local_code == code}
end

#belonging_local_in_tornado_alertObject



169
170
171
172
173
174
# File 'lib/jma_code/area_forecast_local.rb', line 169

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_alertObject



162
163
164
165
166
167
# File 'lib/jma_code/area_forecast_local.rb', line 162

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

#child_of?(area_or_city) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/jma_code/area_forecast_local.rb', line 189

def child_of?(area_or_city)
  any_belonging_locals.map(&:code).include?(area_or_city.code)
end

#prefectureObject



154
155
156
# File 'lib/jma_code/area_forecast_local.rb', line 154

def prefecture
  @prefecture ||= Prefecture.get.find{|pref| pref.code == prefecture_code}
end

#prefecture_codeObject



150
151
152
# File 'lib/jma_code/area_forecast_local.rb', line 150

def prefecture_code
  @prefecture_code ||= code[0, 2]
end

#to_csv_rowObject



193
194
195
196
197
198
199
# File 'lib/jma_code/area_forecast_local.rb', line 193

def to_csv_row
  HEADERS.map do |k|
    respond_to?(k) ?
      public_send(k) :
      nil
  end
end