earth 0.5.0 → 0.5.2

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.
Files changed (173) hide show
  1. data/Gemfile +4 -5
  2. data/README.markdown +0 -8
  3. data/Rakefile +14 -10
  4. data/bin/earth_tester.rb +12 -12
  5. data/earth.gemspec +2 -1
  6. data/features/automobile_make_fleet_year.feature +9 -19
  7. data/features/automobile_make_model_year.feature +4 -4
  8. data/features/support/env.rb +11 -7
  9. data/features/support/imports/automobile_make_fleet_year_bad.csv +1 -1
  10. data/features/support/imports/automobile_make_fleet_year_good.csv +1 -1
  11. data/features/support/imports/automobile_make_model_year_bad.csv +1 -1
  12. data/features/support/imports/automobile_make_model_year_good.csv +1 -1
  13. data/lib/earth/air/aircraft/data_miner.rb +63 -15
  14. data/lib/earth/air/aircraft.rb +0 -11
  15. data/lib/earth/air/aircraft_class/data_miner.rb +0 -13
  16. data/lib/earth/air/aircraft_class.rb +12 -3
  17. data/lib/earth/air/aircraft_fuel_use_equation/data_miner.rb +0 -13
  18. data/lib/earth/air/aircraft_fuel_use_equation.rb +12 -3
  19. data/lib/earth/air/airline/data_miner.rb +0 -7
  20. data/lib/earth/air/airline.rb +6 -3
  21. data/lib/earth/air/airport/data_miner.rb +5 -14
  22. data/lib/earth/air/airport.rb +9 -7
  23. data/lib/earth/air/bts_aircraft/data_miner.rb +0 -5
  24. data/lib/earth/air/bts_aircraft.rb +3 -3
  25. data/lib/earth/air/data_miner.rb +2 -3
  26. data/lib/earth/air/flight_distance_class/data_miner.rb +0 -6
  27. data/lib/earth/air/flight_distance_class.rb +4 -3
  28. data/lib/earth/air/flight_seat_class/data_miner.rb +0 -7
  29. data/lib/earth/air/flight_seat_class.rb +6 -3
  30. data/lib/earth/air/flight_segment/data_miner.rb +30 -112
  31. data/lib/earth/air/flight_segment.rb +47 -13
  32. data/lib/earth/air.rb +2 -1
  33. data/lib/earth/automobile/automobile_fuel/data_miner.rb +1 -25
  34. data/lib/earth/automobile/automobile_fuel.rb +24 -8
  35. data/lib/earth/automobile/automobile_make/data_miner.rb +3 -16
  36. data/lib/earth/automobile/automobile_make.rb +5 -3
  37. data/lib/earth/automobile/automobile_make_fleet_year/data_miner.rb +16 -30
  38. data/lib/earth/automobile/automobile_make_fleet_year.rb +10 -7
  39. data/lib/earth/automobile/automobile_make_model/data_miner.rb +1 -11
  40. data/lib/earth/automobile/automobile_make_model.rb +9 -7
  41. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +4 -17
  42. data/lib/earth/automobile/automobile_make_model_year.rb +11 -6
  43. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +37 -65
  44. data/lib/earth/automobile/automobile_make_model_year_variant.rb +36 -6
  45. data/lib/earth/automobile/automobile_make_year/data_miner.rb +3 -15
  46. data/lib/earth/automobile/automobile_make_year.rb +8 -7
  47. data/lib/earth/automobile/automobile_size_class/data_miner.rb +0 -15
  48. data/lib/earth/automobile/automobile_size_class.rb +13 -2
  49. data/lib/earth/automobile/automobile_size_class_year/data_miner.rb +0 -11
  50. data/lib/earth/automobile/automobile_size_class_year.rb +10 -3
  51. data/lib/earth/automobile/automobile_type_fuel_age/data_miner.rb +0 -12
  52. data/lib/earth/automobile/automobile_type_fuel_age.rb +10 -2
  53. data/lib/earth/automobile/automobile_type_fuel_control/data_miner.rb +0 -11
  54. data/lib/earth/automobile/automobile_type_fuel_control.rb +9 -3
  55. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +1 -17
  56. data/lib/earth/automobile/automobile_type_fuel_year.rb +14 -6
  57. data/lib/earth/automobile/automobile_type_fuel_year_age/data_miner.rb +0 -13
  58. data/lib/earth/automobile/automobile_type_fuel_year_age.rb +12 -7
  59. data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +0 -11
  60. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +9 -6
  61. data/lib/earth/automobile/automobile_type_year/data_miner.rb +0 -10
  62. data/lib/earth/automobile/automobile_type_year.rb +9 -3
  63. data/lib/earth/bus/bus_class/data_miner.rb +0 -29
  64. data/lib/earth/bus/bus_class.rb +27 -2
  65. data/lib/earth/bus/bus_fuel/data_miner.rb +1 -16
  66. data/lib/earth/bus/bus_fuel.rb +15 -8
  67. data/lib/earth/bus/bus_fuel_control/data_miner.rb +0 -10
  68. data/lib/earth/bus/bus_fuel_control.rb +8 -3
  69. data/lib/earth/bus/bus_fuel_year_control/data_miner.rb +1 -10
  70. data/lib/earth/bus/bus_fuel_year_control.rb +8 -7
  71. data/lib/earth/bus/data_miner.rb +2 -0
  72. data/lib/earth/bus.rb +2 -0
  73. data/lib/earth/computation/computation_carrier/data_miner.rb +0 -5
  74. data/lib/earth/computation/computation_carrier.rb +3 -2
  75. data/lib/earth/computation/computation_carrier_instance_class/data_miner.rb +0 -8
  76. data/lib/earth/computation/computation_carrier_instance_class.rb +6 -6
  77. data/lib/earth/computation/computation_carrier_region/data_miner.rb +0 -7
  78. data/lib/earth/computation/computation_carrier_region.rb +6 -7
  79. data/lib/earth/computation/data_miner.rb +2 -0
  80. data/lib/earth/computation.rb +2 -0
  81. data/lib/earth/diet/diet_class/data_miner.rb +0 -16
  82. data/lib/earth/diet/diet_class.rb +16 -4
  83. data/lib/earth/diet/food_group/data_miner.rb +0 -9
  84. data/lib/earth/diet/food_group.rb +7 -2
  85. data/lib/earth/fuel/fuel/data_miner.rb +0 -16
  86. data/lib/earth/fuel/fuel.rb +14 -2
  87. data/lib/earth/fuel/fuel_price/data_miner.rb +0 -6
  88. data/lib/earth/fuel/fuel_price.rb +4 -6
  89. data/lib/earth/fuel/fuel_type/data_miner.rb +0 -14
  90. data/lib/earth/fuel/fuel_type.rb +14 -4
  91. data/lib/earth/fuel/fuel_year/data_miner.rb +0 -16
  92. data/lib/earth/fuel/fuel_year.rb +14 -2
  93. data/lib/earth/fuel/greenhouse_gas/data_miner.rb +0 -9
  94. data/lib/earth/fuel/greenhouse_gas.rb +10 -5
  95. data/lib/earth/hospitality/lodging_class/data_miner.rb +0 -12
  96. data/lib/earth/hospitality/lodging_class.rb +10 -2
  97. data/lib/earth/industry/data_miner.rb +0 -0
  98. data/lib/earth/industry/industry.rb +3 -10
  99. data/lib/earth/industry/industry_product.rb +6 -13
  100. data/lib/earth/industry/industry_product_line.rb +5 -12
  101. data/lib/earth/industry/industry_sector.rb +5 -12
  102. data/lib/earth/industry/merchant.rb +4 -11
  103. data/lib/earth/industry/merchant_category.rb +3 -10
  104. data/lib/earth/industry/merchant_category_industry.rb +5 -12
  105. data/lib/earth/industry/product_line.rb +5 -12
  106. data/lib/earth/industry/product_line_industry_product.rb +5 -12
  107. data/lib/earth/industry/sector.rb +5 -12
  108. data/lib/earth/locality/census_division/data_miner.rb +0 -23
  109. data/lib/earth/locality/census_division.rb +21 -6
  110. data/lib/earth/locality/census_region/data_miner.rb +0 -5
  111. data/lib/earth/locality/census_region.rb +3 -2
  112. data/lib/earth/locality/climate_division/data_miner.rb +0 -7
  113. data/lib/earth/locality/climate_division.rb +5 -7
  114. data/lib/earth/locality/country/data_miner.rb +16 -29
  115. data/lib/earth/locality/country.rb +15 -4
  116. data/lib/earth/locality/data_miner.rb +3 -0
  117. data/lib/earth/locality/egrid_region/data_miner.rb +0 -5
  118. data/lib/earth/locality/egrid_region.rb +5 -8
  119. data/lib/earth/locality/egrid_subregion/data_miner.rb +1 -20
  120. data/lib/earth/locality/egrid_subregion.rb +17 -6
  121. data/lib/earth/locality/petroleum_administration_for_defense_district/data_miner.rb +4 -13
  122. data/lib/earth/locality/petroleum_administration_for_defense_district.rb +8 -4
  123. data/lib/earth/locality/state/data_miner.rb +0 -8
  124. data/lib/earth/locality/state.rb +6 -7
  125. data/lib/earth/locality/urbanity/data_miner.rb +0 -4
  126. data/lib/earth/locality/urbanity.rb +2 -2
  127. data/lib/earth/locality/zip_code/data_miner.rb +0 -10
  128. data/lib/earth/locality/zip_code.rb +8 -6
  129. data/lib/earth/locality.rb +3 -0
  130. data/lib/earth/pet/breed/data_miner.rb +0 -7
  131. data/lib/earth/pet/breed.rb +5 -6
  132. data/lib/earth/pet/breed_gender/data_miner.rb +0 -8
  133. data/lib/earth/pet/breed_gender.rb +6 -6
  134. data/lib/earth/pet/gender/data_miner.rb +0 -3
  135. data/lib/earth/pet/gender.rb +2 -2
  136. data/lib/earth/pet/species/data_miner.rb +0 -17
  137. data/lib/earth/pet/species.rb +17 -4
  138. data/lib/earth/rail/rail_class/data_miner.rb +0 -14
  139. data/lib/earth/rail/rail_class.rb +12 -3
  140. data/lib/earth/residence/air_conditioner_use/data_miner.rb +0 -6
  141. data/lib/earth/residence/air_conditioner_use.rb +5 -3
  142. data/lib/earth/residence/clothes_machine_use/data_miner.rb +0 -6
  143. data/lib/earth/residence/clothes_machine_use.rb +4 -2
  144. data/lib/earth/residence/data_miner.rb +2 -0
  145. data/lib/earth/residence/dishwasher_use/data_miner.rb +0 -6
  146. data/lib/earth/residence/dishwasher_use.rb +5 -3
  147. data/lib/earth/residence/residence_appliance/data_miner.rb +0 -6
  148. data/lib/earth/residence/residence_appliance.rb +4 -2
  149. data/lib/earth/residence/residence_class/data_miner.rb +0 -4
  150. data/lib/earth/residence/residence_class.rb +2 -2
  151. data/lib/earth/residence/residence_fuel_price/data_miner.rb +0 -14
  152. data/lib/earth/residence/residence_fuel_price.rb +12 -7
  153. data/lib/earth/residence/residence_fuel_type/data_miner.rb +0 -8
  154. data/lib/earth/residence/residence_fuel_type.rb +6 -2
  155. data/lib/earth/residence/residential_energy_consumption_survey_response/data_miner.rb +0 -94
  156. data/lib/earth/residence/residential_energy_consumption_survey_response.rb +92 -6
  157. data/lib/earth/residence.rb +2 -0
  158. data/lib/earth/shipping/carrier/data_miner.rb +0 -10
  159. data/lib/earth/shipping/carrier.rb +8 -2
  160. data/lib/earth/shipping/carrier_mode/data_miner.rb +0 -10
  161. data/lib/earth/shipping/carrier_mode.rb +9 -3
  162. data/lib/earth/shipping/shipment_mode/data_miner.rb +0 -7
  163. data/lib/earth/shipping/shipment_mode.rb +5 -2
  164. data/lib/earth/version.rb +1 -1
  165. data/lib/earth.rb +92 -84
  166. data/spec/earth/air/aircraft_spec.rb +1 -1
  167. data/spec/earth_spec.rb +15 -15
  168. data/spec/spec_helper.rb +12 -9
  169. metadata +175 -171
  170. data/lib/earth/active_record_ext.rb +0 -9
  171. data/lib/earth/air/aircraft_manufacturer/data_miner.rb +0 -21
  172. data/lib/earth/air/aircraft_manufacturer.rb +0 -7
  173. data/lib/earth/base.rb +0 -7
@@ -1,7 +1,15 @@
1
- # need this to run flight_segment.cache_aircraft!
2
- require 'loose_tight_dictionary/cached_result'
3
-
4
1
  FlightSegment.class_eval do
2
+ # For import errata
3
+ class FlightSegment::Guru
4
+ def in_may_2009?(row)
5
+ row ['MONTH'] == 5 and row['YEAR'] == 2009
6
+ end
7
+
8
+ def in_july_2009?(row)
9
+ row ['MONTH'] == 7 and row['YEAR'] == 2009
10
+ end
11
+ end
12
+
5
13
  URL = 'http://www.transtats.bts.gov/DownLoad_Table.asp?Table_ID=293&Has_Group=3&Is_Zipped=0'
6
14
  FORM_DATA = %{
7
15
  UserTableName=T_100_Segment__All_Carriers&
@@ -162,51 +170,8 @@ FlightSegment.class_eval do
162
170
  }.gsub /[\s]+/,''
163
171
 
164
172
  data_miner do
165
- schema Earth.database_options do
166
- string 'row_hash' # auto-generated primary key
167
- string 'origin_airport_iata_code' # iata code
168
- string 'origin_airport_city' # city
169
- string 'origin_country_iso_3166_code' # iso code
170
- string 'destination_airport_iata_code' # iata code
171
- string 'destination_airport_city' # city
172
- string 'destination_country_iso_3166_code' # iso code
173
- string 'airline_bts_code' # bts code
174
- string 'airline_icao_code' # icao code
175
- string 'airline_name' # text description derived from bts or icao code
176
- string 'aircraft_bts_code' # bts code
177
- string 'aircraft_description' # text description derived from BTS T100 or ICAO TFS
178
- integer 'flights' # number of flights over month or year
179
- integer 'passengers' # total passengers on all flights
180
- integer 'seats' # total seats on all flights
181
- float 'seats_per_flight' # average seats per flight; make this a float
182
- float 'load_factor' # passengers / seats
183
- float 'freight_share' # (freight + mail) / (freight + mail + (passengers * average passenger weight))
184
- float 'distance' # flight distance
185
- string 'distance_units' # 'kilometres'
186
- float 'payload_capacity' # aircraft maximum payload capacity rating; float b/c unit conversion
187
- string 'payload_capacity_units' # 'kilograms'
188
- float 'freight' # total freight on all flights performed; float b/c unit conversion
189
- string 'freight_units' # 'kilograms'
190
- float 'mail' # total mail on all flights performed; float b/c unit conversion
191
- string 'mail_units' # 'kilograms'
192
- integer 'month' # month of flight
193
- integer 'year' # year of flight
194
- date 'approximate_date' # assumed 14th day of month
195
- string 'source' # 'BTS T100' or 'ICAO TFS'
196
- index 'origin_airport_iata_code' # index for faster lookup by origin airport
197
- index 'origin_airport_city' # index for faster lookup by origin city
198
- index 'destination_airport_iata_code' # index for faster lookup by destination airport
199
- index 'destination_airport_city' # index for faster lookup by destination city
200
- index 'airline_bts_code' # index for faster lookup by airline bts code
201
- index 'airline_icao_code' # index for faster lookup by airline icao code
202
- index 'airline_name' # index for faster lookup by airline name
203
- index 'aircraft_bts_code' # index for faster lookup by aircraft bts code
204
- index 'aircraft_description' # index for faster lookup by aircraft
205
- index 'year' # index for faster lookup by year
206
- end
207
-
208
173
  months = Hash.new
209
- (2009..2010).each do |year|
174
+ (2009..2011).each do |year|
210
175
  (1..12).each do |month|
211
176
  time = Time.gm year, month
212
177
  form_data = FORM_DATA.dup
@@ -223,6 +188,8 @@ FlightSegment.class_eval do
223
188
  :form_data => form_data,
224
189
  :compression => :zip,
225
190
  :glob => '/*.csv',
191
+ # FIXME TODO 6/6/2011 the errata doesn't work - still doesn't fill in missing airline in May and July 2009
192
+ :errata => { :url => 'https://spreadsheets.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdGxpYU1qWFR3d0syTVMyQVVOaDd0V3c&output=csv', :responder => FlightSegment::Guru.new },
226
193
  :select => lambda { |record| record['DEPARTURES_PERFORMED'].to_i > 0 } do
227
194
  key 'row_hash'
228
195
  store 'origin_airport_iata_code', :field_name => 'ORIGIN'
@@ -244,33 +211,24 @@ FlightSegment.class_eval do
244
211
  end
245
212
  end
246
213
 
247
- # verify origin_airport_iata_code is in airports
248
- # verify destination_airport_iata_code is in airports
249
- # verify origin_country_iso_3166_code is in countries
250
- # verify destination_country_iso_3166_code is in countries
251
- # verify airline_bts_code appears in airlines
252
- # verify aircraft_description is never missing
253
- # verify year is never missing
254
-
255
- process "Ensure Airline is populated" do
214
+ process "Ensure Airline and BtsAircraft are populated" do
256
215
  Airline.run_data_miner!
216
+ BtsAircraft.run_data_miner!
257
217
  end
258
218
 
259
219
  process "Look up airline name based on BTS code" do
260
220
  connection.select_values("SELECT DISTINCT airline_bts_code FROM flight_segments WHERE airline_bts_code IS NOT NULL").each do |bts_code|
261
- name = Airline.find_by_bts_code(bts_code).name
262
- update_all %{ airline_name = "#{name}" }, %{ airline_bts_code = "#{bts_code}" }
221
+ if airline = Airline.find_by_bts_code(bts_code)
222
+ update_all %{ airline_name = "#{airline.name}" }, %{ airline_bts_code = "#{bts_code}" }
223
+ end
263
224
  end
264
225
  end
265
226
 
266
- process "Ensure BtsAircraft is populated" do
267
- BtsAircraft.run_data_miner!
268
- end
269
-
270
227
  process "Look up aircraft description based on BTS code" do
271
228
  connection.select_values("SELECT DISTINCT aircraft_bts_code FROM flight_segments WHERE aircraft_bts_code IS NOT NULL").each do |bts_code|
272
- description = BtsAircraft.find_by_bts_code(bts_code).description.downcase
273
- update_all %{ aircraft_description = "#{description}" }, %{ aircraft_bts_code = "#{bts_code}" }
229
+ if aircraft = BtsAircraft.find_by_bts_code(bts_code)
230
+ update_all %{ aircraft_description = "#{aircraft.description.downcase}" }, %{ aircraft_bts_code = "#{bts_code}" }
231
+ end
274
232
  end
275
233
  end
276
234
 
@@ -316,56 +274,16 @@ FlightSegment.class_eval do
316
274
  update_all 'approximate_date = DATE(CONCAT_WS("-", year, month, "14"))', 'month IS NOT NULL'
317
275
  end
318
276
 
319
- process "Ensure Aircraft is populated" do
277
+ process "Data mine Aircraft to cache fuzzy matches" do
320
278
  Aircraft.run_data_miner!
321
279
  end
322
280
 
323
- process "Cache fuzzy matches between FlightSegment aircraft_description and Aircraft description" do
324
- LooseTightDictionary::CachedResult.setup
325
- FlightSegment.find_by_sql("SELECT DISTINCT aircraft_description FROM flight_segments WHERE aircraft_description IS NOT NULL").each do |flight_segment|
326
- original_description = flight_segment.aircraft_description
327
-
328
- # If the flight segment's aircraft_description contains '/' then it describes multiple aircraft.
329
- # We need to synthesize descriptions for those aircraft, find all Aircraft that fuzzily match the
330
- # synthesized descriptions, and associate those Aircraft with the original aircraft_description.
331
- # e.g. boeing 747-100/200
332
- if original_description.include?("/")
333
- # Pull out the complete first aircraft description
334
- # e.g. 'boeing 747-100'
335
- first_description = original_description.split('/')[0]
336
-
337
- # Pull out the root of the description - the text up to and including the last ' ' or '-'
338
- # e.g. 'boeing 747-'
339
- root_length = first_description.rindex('-')
340
- root = first_description.slice(0..root_length)
341
-
342
- # Pull out the suffixes - the text separated by forward slashes
343
- # e.g. ['100', '200']
344
- suffixes = original_description.split(root)[1].split('/')
345
-
346
- # Create an array of synthesized descriptions by appending each suffix to the root
347
- # e.g. ['boeing 747-100', 'boeing 747-200']
348
- suffixes.map{ |suffix| root + suffix }.each do |synthesized_description|
349
- # Look up the Aircraft that match each synthesized description and associate
350
- # them with the original flight segment aircraft_description
351
- Aircraft.loose_tight_dictionary.find_all(synthesized_description).each do |aircraft|
352
- attrs = {
353
- :a_class => "Aircraft",
354
- :a => aircraft.description,
355
- :b_class => "FlightSegment",
356
- :b => original_description
357
- }
358
- unless ::LooseTightDictionary::CachedResult.exists? attrs
359
- ::LooseTightDictionary::CachedResult.create! attrs
360
- end
361
- end
362
- end
363
- # If the flight segment's aircraft_description doesn't contain '/' we can use
364
- # a method provided by loose_tight_dictionary to associate it with Aircraft
365
- else
366
- flight_segment.cache_aircraft!
367
- end
368
- end
369
- end
281
+ # verify origin_airport_iata_code is in airports
282
+ # verify destination_airport_iata_code is in airports
283
+ # verify origin_country_iso_3166_code is in countries
284
+ # verify destination_country_iso_3166_code is in countries
285
+ # verify airline_name is never missing
286
+ # verify aircraft_description is never missing
287
+ # verify year is never missing
370
288
  end
371
289
  end
@@ -1,16 +1,15 @@
1
- # need this for association with Aircraft through loose_tight_dictionary_cached_results
2
- require 'loose_tight_dictionary/cached_result'
3
-
4
1
  class FlightSegment < ActiveRecord::Base
5
2
  set_primary_key :row_hash
6
3
 
7
4
  extend CohortScope
8
5
  self.minimum_cohort_size = 1
9
6
 
10
- # If airport iata code is missing, associate with all airports in a city
11
- # We need this to calculate distance when importing ICAO segments - see cm1 flight_segment.rb
12
- has_many :origin_city_airports, :foreign_key => 'city', :primary_key => 'origin_airport_city', :class_name => 'Airport'
13
- has_many :destination_city_airports, :foreign_key => 'city', :primary_key => 'destination_airport_city', :class_name => 'Airport'
7
+ # Cutting this for now because if iata code is missing we have to look up airports using both city and country; don't know how to do this with ActiveRecord
8
+ # - Ian 6/12/2011
9
+ # # If airport iata code is missing, associate with all airports in a city
10
+ # # We need this to calculate distance when importing ICAO segments - see cm1 flight_segment.rb
11
+ # has_many :origin_city_airports, :foreign_key => 'city', :primary_key => 'origin_airport_city', :class_name => 'Airport'
12
+ # has_many :destination_city_airports, :foreign_key => 'city', :primary_key => 'destination_airport_city', :class_name => 'Airport'
14
13
 
15
14
  # Enable flight_segment.aircraft
16
15
  cache_loose_tight_dictionary_matches_with :aircraft, :primary_key => :aircraft_description, :foreign_key => :description
@@ -20,11 +19,46 @@ class FlightSegment < ActiveRecord::Base
20
19
  :load_factor => lambda { weighted_average(:load_factor, :weighted_by => :passengers) }, # 0.78073233770097 data1 10-12-2010
21
20
  :freight_share => lambda { weighted_average(:freight_share, :weighted_by => :passengers) } # 0.022567224170157 data1 10-12-2010
22
21
 
23
- data_miner do
24
- tap "Brighter Planet's sanitized flight segment data", Earth.taps_server
25
-
26
- process "Pull dependencies" do
27
- run_data_miner_on_belongs_to_associations
28
- end
22
+ create_table do
23
+ string 'row_hash' # auto-generated primary key
24
+ string 'origin_airport_iata_code' # iata code
25
+ string 'origin_airport_city' # city
26
+ string 'origin_country_iso_3166_code' # iso code
27
+ string 'destination_airport_iata_code' # iata code
28
+ string 'destination_airport_city' # city
29
+ string 'destination_country_iso_3166_code' # iso code
30
+ string 'airline_bts_code' # bts code
31
+ string 'airline_icao_code' # icao code
32
+ string 'airline_name' # text description derived from bts or icao code
33
+ string 'aircraft_bts_code' # bts code
34
+ string 'aircraft_description' # text description derived from BTS T100 or ICAO TFS
35
+ integer 'flights' # number of flights over month or year
36
+ integer 'passengers' # total passengers on all flights
37
+ integer 'seats' # total seats on all flights
38
+ float 'seats_per_flight' # average seats per flight; make this a float
39
+ float 'load_factor' # passengers / seats
40
+ float 'freight_share' # (freight + mail) / (freight + mail + (passengers * average passenger weight))
41
+ float 'distance' # flight distance
42
+ string 'distance_units' # 'kilometres'
43
+ float 'payload_capacity' # aircraft maximum payload capacity rating; float b/c unit conversion
44
+ string 'payload_capacity_units' # 'kilograms'
45
+ float 'freight' # total freight on all flights performed; float b/c unit conversion
46
+ string 'freight_units' # 'kilograms'
47
+ float 'mail' # total mail on all flights performed; float b/c unit conversion
48
+ string 'mail_units' # 'kilograms'
49
+ integer 'month' # month of flight
50
+ integer 'year' # year of flight
51
+ date 'approximate_date' # assumed 14th day of month
52
+ string 'source' # 'BTS T100' or 'ICAO TFS'
53
+ index 'origin_airport_iata_code' # index for faster lookup by origin airport
54
+ index 'origin_airport_city' # index for faster lookup by origin city
55
+ index 'destination_airport_iata_code' # index for faster lookup by destination airport
56
+ index 'destination_airport_city' # index for faster lookup by destination city
57
+ index 'airline_bts_code' # index for faster lookup by airline bts code
58
+ index 'airline_icao_code' # index for faster lookup by airline icao code
59
+ index 'airline_name' # index for faster lookup by airline name
60
+ index 'aircraft_bts_code' # index for faster lookup by aircraft bts code
61
+ index 'aircraft_description' # index for faster lookup by aircraft
62
+ index 'year' # index for faster lookup by year
29
63
  end
30
64
  end
data/lib/earth/air.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require 'earth/air/aircraft'
2
2
  require 'earth/air/aircraft_class'
3
3
  require 'earth/air/aircraft_fuel_use_equation'
4
- require 'earth/air/aircraft_manufacturer'
5
4
  require 'earth/air/airline'
6
5
  require 'earth/air/airport'
7
6
  require 'earth/air/bts_aircraft'
8
7
  require 'earth/air/flight_distance_class'
9
8
  require 'earth/air/flight_seat_class'
10
9
  require 'earth/air/flight_segment'
10
+
11
+ require 'earth/locality'
@@ -1,29 +1,5 @@
1
1
  AutomobileFuel.class_eval do
2
2
  data_miner do
3
- schema Earth.database_options do
4
- string 'name'
5
- string 'code'
6
- string 'base_fuel_name'
7
- string 'blend_fuel_name'
8
- float 'blend_portion' # the portion of the blend that is the blend fuel
9
- string 'distance_key' # used to look up annual distance from AutomobileTypeFuelYear
10
- string 'ef_key' # used to look up ch4 n2o and hfc emission factors from AutomobileTypeFuelYear
11
- float 'annual_distance'
12
- string 'annual_distance_units'
13
- float 'co2_emission_factor'
14
- string 'co2_emission_factor_units'
15
- float 'co2_biogenic_emission_factor'
16
- string 'co2_biogenic_emission_factor_units'
17
- float 'ch4_emission_factor'
18
- string 'ch4_emission_factor_units'
19
- float 'n2o_emission_factor'
20
- string 'n2o_emission_factor_units'
21
- float 'hfc_emission_factor'
22
- string 'hfc_emission_factor_units'
23
- float 'emission_factor' # DEPRECATED but motorcycle needs this
24
- string 'emission_factor_units' # DEPRECATED but motorcycle needs this
25
- end
26
-
27
3
  import "a list of pure automobile fuels",
28
4
  :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdE9xTEdueFM2R0diNTgxUlk1QXFSb2c&gid=0&output=html' do
29
5
  key 'name'
@@ -44,7 +20,7 @@ AutomobileFuel.class_eval do
44
20
  store 'ef_key'
45
21
  end
46
22
 
47
- process "Ensure necessary datasets are imported" do
23
+ process "Ensure AutomobileTypeFuelYearAge, AutomobileTypeYear, Fuel, and GreenhouseGas are populated" do
48
24
  AutomobileTypeFuelYearAge.run_data_miner!
49
25
  AutomobileTypeYear.run_data_miner!
50
26
  Fuel.run_data_miner!
@@ -97,15 +97,31 @@ class AutomobileFuel < ActiveRecord::Base
97
97
  :n2o_emission_factor_units => lambda { AutomobileFuel.fallback_n2o_emission_factor_units },
98
98
  :hfc_emission_factor => lambda { AutomobileFuel.fallback_hfc_emission_factor },
99
99
  :hfc_emission_factor_units => lambda { AutomobileFuel.fallback_hfc_emission_factor_units }
100
-
101
- data_miner do
102
- tap "Brighter Planet's sanitized automobile fuel data", Earth.taps_server
103
-
104
- process "pull dependencies" do
105
- run_data_miner_on_belongs_to_associations
106
- end
100
+
101
+ create_table do
102
+ string 'name'
103
+ string 'code'
104
+ string 'base_fuel_name'
105
+ string 'blend_fuel_name'
106
+ float 'blend_portion' # the portion of the blend that is the blend fuel
107
+ string 'distance_key' # used to look up annual distance from AutomobileTypeFuelYear
108
+ string 'ef_key' # used to look up ch4 n2o and hfc emission factors from AutomobileTypeFuelYear
109
+ float 'annual_distance'
110
+ string 'annual_distance_units'
111
+ float 'co2_emission_factor'
112
+ string 'co2_emission_factor_units'
113
+ float 'co2_biogenic_emission_factor'
114
+ string 'co2_biogenic_emission_factor_units'
115
+ float 'ch4_emission_factor'
116
+ string 'ch4_emission_factor_units'
117
+ float 'n2o_emission_factor'
118
+ string 'n2o_emission_factor_units'
119
+ float 'hfc_emission_factor'
120
+ string 'hfc_emission_factor_units'
121
+ float 'emission_factor' # DEPRECATED but motorcycle needs this
122
+ string 'emission_factor_units' # DEPRECATED but motorcycle needs this
107
123
  end
108
-
124
+
109
125
  CODES = {
110
126
  :electricity => 'El',
111
127
  :diesel => 'D'
@@ -1,17 +1,12 @@
1
1
  AutomobileMake.class_eval do
2
2
  data_miner do
3
3
  process "Start from scratch" do
4
- connection.drop_table table_name
4
+ delete_all
5
5
  end
6
6
 
7
- schema Earth.database_options do
8
- string 'name'
9
- float 'fuel_efficiency'
10
- string 'fuel_efficiency_units'
11
- end
12
-
13
- process "Ensure AutomobileMakeModelYearVariant is populated" do
7
+ process "Ensure AutomobileMakeModelYearVariant and AutomobileMakeFleetYear are populated" do
14
8
  AutomobileMakeModelYearVariant.run_data_miner!
9
+ AutomobileMakeFleetYear.run_data_miner!
15
10
  end
16
11
 
17
12
  process "Derive manufacturer names from automobile make model year variants" do
@@ -23,10 +18,6 @@ AutomobileMake.class_eval do
23
18
  }
24
19
  end
25
20
 
26
- process "Ensure AutomobileMakeFleetYear is populated" do
27
- AutomobileMakeFleetYear.run_data_miner!
28
- end
29
-
30
21
  # sabshere 1/31/11 add Avanti, DaimlerChrysler, IHC, Tesla, etc.
31
22
  process "Derive extra manufacturer names from CAFE data" do
32
23
  INSERT_IGNORE %{INTO automobile_makes(name)
@@ -35,10 +26,6 @@ AutomobileMake.class_eval do
35
26
  }
36
27
  end
37
28
 
38
- process "Ensure AutomobileMakeFleetYear is populated" do
39
- AutomobileMakeFleetYear.run_data_miner!
40
- end
41
-
42
29
  # FIXME TODO make this a method on AutomobileMake?
43
30
  process "Calculate fuel efficiency from automobile make fleet years for makes with CAFE data" do
44
31
  make_fleet_years = AutomobileMakeFleetYear.arel_table
@@ -5,8 +5,10 @@ class AutomobileMake < ActiveRecord::Base
5
5
  has_many :models, :class_name => 'AutomobileMakeModel', :foreign_key => 'make_name'
6
6
  has_many :fleet_years, :class_name => 'AutomobileMakeFleetYear', :foreign_key => 'make_name'
7
7
  has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_name'
8
-
9
- data_miner do
10
- tap "Brighter Planet's make year data", Earth.taps_server
8
+
9
+ create_table do
10
+ string 'name'
11
+ float 'fuel_efficiency'
12
+ string 'fuel_efficiency_units'
11
13
  end
12
14
  end
@@ -1,16 +1,5 @@
1
1
  AutomobileMakeFleetYear.class_eval do
2
2
  data_miner do
3
- schema Earth.database_options do
4
- string 'name'
5
- string 'make_year_name'
6
- string 'make_name'
7
- string 'fleet'
8
- integer 'year'
9
- float 'fuel_efficiency'
10
- string 'fuel_efficiency_units'
11
- integer 'volume'
12
- end
13
-
14
3
  import "annual corporate average fuel economy data for domestic and imported vehicle fleets from the NHTSA",
15
4
  :url => 'https://spreadsheets.google.com/pub?key=0AoQJbWqPrREqdEdXWXB6dkVLWkowLXhYSFVUT01sS2c&hl=en&gid=0&output=csv',
16
5
  :errata => { 'url' => 'http://static.brighterplanet.com/science/data/transport/automobiles/make_fleet_years/errata.csv' },
@@ -24,34 +13,31 @@ AutomobileMakeFleetYear.class_eval do
24
13
  store 'volume'
25
14
  end
26
15
 
27
- verify "Year should be from 1978 to 2009" do
28
- AutomobileMakeFleetYear.all.each do |fleet_year|
29
- unless fleet_year.year > 1977 and fleet_year.year < 2010
30
- raise "Invalid year for AutomobileMakeFleetYear #{fleet_year.name}: #{fleet_year.year} (should be from 1978 to 2009)"
16
+ verify "Year should be from 1978 to 2010" do
17
+ connection.select_values("SELECT DISTINCT year FROM automobile_make_fleet_years").each do |year|
18
+ unless year > 1977 and year < 2011
19
+ raise "Invalid year in automobile_make_fleet_years: #{year} is not from 1978 to 2010"
31
20
  end
32
21
  end
33
22
  end
34
23
 
35
- verify "Fuel efficiency should be greater than zero" do
36
- AutomobileMakeFleetYear.all.each do |fleet_year|
37
- unless fleet_year.fuel_efficiency > 0
38
- raise "Invalid fuel efficiency for AutomobileMakeFleetYear #{fleet_year.name}: #{fleet_year.fuel_efficiency} (should be > 0)"
24
+ verify "Fuel efficiency and volume should be greater than zero" do
25
+ [:fuel_efficiency, :volume].each do |field|
26
+ if AutomobileMakeFleetYear.where(field => nil).any?
27
+ raise "Invalid #{field} in automobile_make_fleet_years: nil is not > 0"
28
+ else
29
+ min = AutomobileMakeFleetYear.minimum(field)
30
+ unless min > 0
31
+ raise "Invalid #{field} in automobile_make_fleet_years: #{min} is not > 0"
32
+ end
39
33
  end
40
34
  end
41
35
  end
42
36
 
43
37
  verify "Fuel efficiency units should be kilometres per litre" do
44
- AutomobileMakeFleetYear.all.each do |fleet_year|
45
- unless fleet_year.fuel_efficiency_units == "kilometres_per_litre"
46
- raise "Invalid fuel efficiency units for AutomobileMakeFleetYear #{fleet_year.name}: #{fleet_year.fuel_efficiency_units} (should be kilometres_per_litre)"
47
- end
48
- end
49
- end
50
-
51
- verify "Volume should be greater than zero" do
52
- AutomobileMakeFleetYear.all.each do |fleet_year|
53
- unless fleet_year.volume > 0
54
- raise "Invalid volume for AutomobileMakeFleetYear #{fleet_year.name}: #{fleet_year.volume} (should be > 0)"
38
+ connection.select_values("SELECT DISTINCT fuel_efficiency_units FROM automobile_make_fleet_years").each do |units|
39
+ unless units == "kilometres_per_litre"
40
+ raise "Invalid fuel efficiency units in automobile_make_fleet_years: #{units} is not 'kilometres_per_litre'"
55
41
  end
56
42
  end
57
43
  end
@@ -3,12 +3,15 @@ class AutomobileMakeFleetYear < ActiveRecord::Base
3
3
 
4
4
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
5
  belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
6
-
7
- data_miner do
8
- tap "Brighter Planet's sanitized auto make fleet year data", Earth.taps_server
9
-
10
- process "Pull dependencies" do
11
- run_data_miner_on_belongs_to_associations
12
- end
6
+
7
+ create_table do
8
+ string 'name'
9
+ string 'make_year_name'
10
+ string 'make_name'
11
+ string 'fleet'
12
+ integer 'year'
13
+ float 'fuel_efficiency'
14
+ string 'fuel_efficiency_units'
15
+ integer 'volume'
13
16
  end
14
17
  end
@@ -1,17 +1,7 @@
1
1
  AutomobileMakeModel.class_eval do
2
2
  data_miner do
3
3
  process "Start from scratch" do
4
- connection.drop_table table_name
5
- end
6
-
7
- schema Earth.database_options do
8
- string 'name' # make + model
9
- string 'make_name'
10
- string 'model_name' # model only
11
- float 'fuel_efficiency_city'
12
- string 'fuel_efficiency_city_units'
13
- float 'fuel_efficiency_highway'
14
- string 'fuel_efficiency_highway_units'
4
+ delete_all
15
5
  end
16
6
 
17
7
  process "Ensure AutomobileMakeModelYearVariant is populated" do
@@ -3,12 +3,14 @@ class AutomobileMakeModel < ActiveRecord::Base
3
3
 
4
4
  belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
5
  has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_model_name'
6
-
7
- data_miner do
8
- tap "Brighter Planet's auto model data", Earth.taps_server
9
-
10
- process "Pull dependencies" do
11
- run_data_miner_on_belongs_to_associations
12
- end
6
+
7
+ create_table do
8
+ string 'name' # make + model
9
+ string 'make_name'
10
+ string 'model_name' # model only
11
+ float 'fuel_efficiency_city'
12
+ string 'fuel_efficiency_city_units'
13
+ float 'fuel_efficiency_highway'
14
+ string 'fuel_efficiency_highway_units'
13
15
  end
14
16
  end
@@ -1,20 +1,7 @@
1
1
  AutomobileMakeModelYear.class_eval do
2
2
  data_miner do
3
3
  process "Start from scratch" do
4
- connection.drop_table table_name
5
- end
6
-
7
- schema Earth.database_options do
8
- string 'name' # make + model + year
9
- string 'make_name'
10
- string 'model_name'
11
- string 'make_model_name'
12
- integer 'year'
13
- string 'make_year_name'
14
- float 'fuel_efficiency_city'
15
- string 'fuel_efficiency_city_units'
16
- float 'fuel_efficiency_highway'
17
- string 'fuel_efficiency_highway_units'
4
+ delete_all
18
5
  end
19
6
 
20
7
  process "Ensure AutomobileMakeModelYearVariant is populated" do
@@ -45,10 +32,10 @@ AutomobileMakeModelYear.class_eval do
45
32
  end
46
33
  end
47
34
 
48
- verify "Year should be from 1985 to 2010" do
35
+ verify "Year should be from 1985 to 2011" do
49
36
  AutomobileMakeModelYear.all.each do |model_year|
50
- unless model_year.year.to_i > 1984 and model_year.year.to_i < 2011
51
- raise "Invalid year for AutomobileMakeModelYear #{model_year.name}: #{model_year.year} (should be from 1985 to 2010)"
37
+ unless model_year.year.to_i > 1984 and model_year.year.to_i < 2012
38
+ raise "Invalid year for AutomobileMakeModelYear #{model_year.name}: #{model_year.year} (should be from 1985 to 2011)"
52
39
  end
53
40
  end
54
41
  end
@@ -4,11 +4,16 @@ class AutomobileMakeModelYear < ActiveRecord::Base
4
4
  belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
5
5
  has_many :make_model_year_variants, :class_name => 'AutomobileMakeModelYearVariant', :foreign_key => 'make_model_year_name'
6
6
 
7
- data_miner do
8
- tap "Brighter Planet's model year data", Earth.taps_server
9
-
10
- process "pull dependencies" do
11
- run_data_miner_on_belongs_to_associations
12
- end
7
+ create_table do
8
+ string 'name' # make + model + year
9
+ string 'make_name'
10
+ string 'model_name'
11
+ string 'make_model_name'
12
+ integer 'year'
13
+ string 'make_year_name'
14
+ float 'fuel_efficiency_city'
15
+ string 'fuel_efficiency_city_units'
16
+ float 'fuel_efficiency_highway'
17
+ string 'fuel_efficiency_highway_units'
13
18
  end
14
19
  end