earth 0.7.0 → 0.11.0

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 (80) hide show
  1. data/certification_changelog.markdown +21 -0
  2. data/lib/earth/air.rb +0 -2
  3. data/lib/earth/air/aircraft.rb +77 -27
  4. data/lib/earth/air/aircraft/data_miner.rb +12 -19
  5. data/lib/earth/air/aircraft_instance.rb +2 -0
  6. data/lib/earth/air/aircraft_instance_seat_class.rb +1 -0
  7. data/lib/earth/air/airport/data_miner.rb +1 -1
  8. data/lib/earth/air/data_miner.rb +0 -2
  9. data/lib/earth/air/flight_distance_class.rb +2 -3
  10. data/lib/earth/air/flight_distance_class/data_miner.rb +0 -13
  11. data/lib/earth/air/flight_distance_class_seat_class.rb +3 -2
  12. data/lib/earth/air/flight_seat_class.rb +0 -6
  13. data/lib/earth/air/flight_seat_class/data_miner.rb +0 -7
  14. data/lib/earth/air/flight_segment.rb +47 -53
  15. data/lib/earth/air/flight_segment/data_miner.rb +2 -2
  16. data/lib/earth/automobile.rb +4 -3
  17. data/lib/earth/automobile/automobile_fuel.rb +56 -119
  18. data/lib/earth/automobile/automobile_fuel/data_miner.rb +17 -4
  19. data/lib/earth/automobile/automobile_make.rb +1 -16
  20. data/lib/earth/automobile/automobile_make/data_miner.rb +25 -25
  21. data/lib/earth/automobile/automobile_make_model.rb +0 -26
  22. data/lib/earth/automobile/automobile_make_model/data_miner.rb +12 -13
  23. data/lib/earth/automobile/automobile_make_model_year.rb +6 -37
  24. data/lib/earth/automobile/automobile_make_model_year/data_miner.rb +34 -18
  25. data/lib/earth/automobile/automobile_make_model_year_variant.rb +27 -49
  26. data/lib/earth/automobile/automobile_make_model_year_variant/data_miner.rb +111 -140
  27. data/lib/earth/automobile/automobile_make_year.rb +0 -12
  28. data/lib/earth/automobile/automobile_make_year/data_miner.rb +22 -23
  29. data/lib/earth/automobile/automobile_make_year_fleet.rb +11 -0
  30. data/lib/earth/automobile/{automobile_make_fleet_year → automobile_make_year_fleet}/data_miner.rb +1 -2
  31. data/lib/earth/automobile/automobile_model.rb +5 -0
  32. data/lib/earth/automobile/automobile_model/data_miner.rb +19 -0
  33. data/lib/earth/automobile/automobile_size_class.rb +1 -0
  34. data/lib/earth/automobile/automobile_size_class/data_miner.rb +30 -4
  35. data/lib/earth/automobile/automobile_type_fuel_year.rb +1 -1
  36. data/lib/earth/automobile/automobile_type_fuel_year/data_miner.rb +30 -19
  37. data/lib/earth/automobile/automobile_type_fuel_year_age.rb +0 -3
  38. data/lib/earth/automobile/automobile_type_fuel_year_age/data_miner.rb +13 -15
  39. data/lib/earth/automobile/automobile_type_fuel_year_control.rb +0 -4
  40. data/lib/earth/automobile/automobile_type_fuel_year_control/data_miner.rb +0 -8
  41. data/lib/earth/automobile/automobile_type_year.rb +2 -1
  42. data/lib/earth/automobile/automobile_type_year/data_miner.rb +12 -10
  43. data/lib/earth/automobile/automobile_year.rb +5 -0
  44. data/lib/earth/automobile/automobile_year/data_miner.rb +15 -0
  45. data/lib/earth/automobile/data_miner.rb +5 -3
  46. data/lib/earth/automobile/dependencies.txt +45 -0
  47. data/lib/earth/bus/bus_class.rb +1 -87
  48. data/lib/earth/bus/bus_class/data_miner.rb +0 -1
  49. data/lib/earth/bus/bus_fuel/data_miner.rb +12 -19
  50. data/lib/earth/bus/bus_fuel_control/data_miner.rb +8 -12
  51. data/lib/earth/conversions_ext.rb +1 -0
  52. data/lib/earth/fuel/fuel/data_miner.rb +28 -38
  53. data/lib/earth/fuel/fuel_year/data_miner.rb +36 -47
  54. data/lib/earth/hospitality/lodging_class/data_miner.rb +12 -6
  55. data/lib/earth/locality.rb +1 -0
  56. data/lib/earth/locality/country.rb +1 -1
  57. data/lib/earth/locality/country/data_miner.rb +36 -28
  58. data/lib/earth/locality/data_miner.rb +1 -0
  59. data/lib/earth/locality/egrid_subregion/data_miner.rb +19 -14
  60. data/lib/earth/locality/urbanity/data_miner.rb +1 -3
  61. data/lib/earth/locality/zip_code/data_miner.rb +1 -1
  62. data/lib/earth/rail/national_transit_database_record/data_miner.rb +10 -14
  63. data/lib/earth/rail/rail_fuel.rb +2 -6
  64. data/lib/earth/residence/air_conditioner_use/data_miner.rb +1 -3
  65. data/lib/earth/residence/clothes_machine_use/data_miner.rb +1 -3
  66. data/lib/earth/residence/dishwasher_use/data_miner.rb +1 -3
  67. data/lib/earth/residence/residence_class/data_miner.rb +1 -3
  68. data/lib/earth/residence/residential_energy_consumption_survey_response/data_miner.rb +3 -1
  69. data/lib/earth/utils.rb +15 -5
  70. data/lib/earth/version.rb +1 -1
  71. data/spec/earth_spec.rb +4 -4
  72. metadata +9 -11
  73. data/features/automobile_type_fuel_age.feature +0 -55
  74. data/lib/earth/air/aircraft_class.rb +0 -59
  75. data/lib/earth/air/aircraft_class/data_miner.rb +0 -14
  76. data/lib/earth/air/aircraft_fuel_use_equation.rb +0 -33
  77. data/lib/earth/air/aircraft_fuel_use_equation/data_miner.rb +0 -13
  78. data/lib/earth/automobile/automobile_make_fleet_year.rb +0 -46
  79. data/lib/earth/automobile/automobile_type_fuel_age.rb +0 -65
  80. data/lib/earth/automobile/automobile_type_fuel_age/data_miner.rb +0 -149
@@ -0,0 +1,21 @@
1
+ # Material changes
2
+ - Add energy content to AutomobileFuel (c0edd440e89bb201c37b449dbf0db8f3f5e69726)
3
+ - Add hybridity and fuel code to AutomobileMakeModelYear (8d619575bc399b26abef16d4a033abb94fb9b60b)
4
+ - Add European rail data (82d4a301f58012d38ca66a93a23703a41f5c2f94)
5
+ - New seat class data (3e1bd6f4b41961f420a17aac566244054b0a790b)
6
+ - Update curated airlines (4ded9ce3ce1485e1051da4b313b61e18d760229d)
7
+ - Add US average eGRID region loss factor (a6c0176d9bb1b51c695d101513d1c32940c86e89)
8
+ - Air improvements: ICAO data in CM1, fuzzy matching aircraft -> flight segment, fuel use data in AircraftFuelUseEquation, improve Airport and Aircraft import errata (6e2becd74c625905ae3ca40f0c0bd9b9fb652939)
9
+
10
+ # Immaterial changes
11
+ - New zipcode eGRID subregion data (ebbb08a5ffcc118f311d2fca61b7ce9e0c4d4c2d)
12
+ - Allow users to input automobile 'make', 'model', and 'year' (97d793ea3de14a4e7eef0b0b4ca883011f52038e)
13
+ - New 2010 FEG file (2c678d7d452da97b41bdf5311c399f7a84efe9cf)
14
+ - New zipcode eGRID subregion data (a64add803285c836c03ed3f66f70bf6d3e4affc5)
15
+ - Add 2011 FEG data (7c94ad50e53c6a064c862715c0afd5fdbd183873)
16
+ - Revise computation data (4ced1779d2e45948143a84b2fc1699ef8d298d63)
17
+ - New zipcode eGRID subregion data (af754e84468b47fa8fe7847072bd884a0c6a2b27)
18
+ - Move some automobile data and fallbacks to Country (253215c0f914bc8bab31f64acdbfd3d7cecdd644)
19
+
20
+ # Latest 3rd-party review
21
+ f08c4cad8f30542bda5d49d0a4bbafffc451edaa
@@ -1,6 +1,4 @@
1
1
  require 'earth/air/aircraft'
2
- require 'earth/air/aircraft_class'
3
- require 'earth/air/aircraft_fuel_use_equation'
4
2
  require 'earth/air/aircraft_instance'
5
3
  require 'earth/air/aircraft_instance_seat_class'
6
4
  require 'earth/air/airline'
@@ -1,25 +1,9 @@
1
1
  class Aircraft < ActiveRecord::Base
2
2
  set_primary_key :icao_code
3
3
 
4
- belongs_to :aircraft_class, :foreign_key => 'class_code', :primary_key => 'code'
5
- belongs_to :fuel_use_equation, :foreign_key => 'fuel_use_code', :primary_key => 'code', :class_name => 'AircraftFuelUseEquation'
6
-
7
- col :icao_code
8
- col :manufacturer_name
9
- col :model_name
10
- col :description
11
- col :aircraft_type
12
- col :engine_type
13
- col :engines, :type => :integer
14
- col :weight_class
15
- col :class_code
16
- col :fuel_use_code
17
- col :seats, :type => :float
18
- col :passengers, :type => :float
19
-
20
- # Enable aircraft.flight_segments
4
+ # Fuzzy association with FlightSegment
21
5
  cache_loose_tight_dictionary_matches_with :flight_segments, :primary_key => :description, :foreign_key => :aircraft_description
22
-
6
+
23
7
  class << self
24
8
  # set up a loose_tight_dictionary for matching Aircraft description with FlightSegment aircraft_description
25
9
  def loose_tight_dictionary
@@ -31,18 +15,49 @@ class Aircraft < ActiveRecord::Base
31
15
  :must_match_blocking => true,
32
16
  :first_blocking_decides => true)
33
17
  end
34
-
18
+
35
19
  # FIXME TODO do we want to restrict this to certain years?
36
20
  # Derive some average characteristics from flight segments
37
21
  def update_averages!
22
+ # Setup fuzzy matches with FlightSegment
38
23
  manually_cache_flight_segments!
39
- find_each do |aircraft|
40
- aircraft.seats = aircraft.flight_segments.weighted_average :seats_per_flight, :weighted_by => :passengers
41
- aircraft.passengers = aircraft.flight_segments.sum :passengers
42
- aircraft.save!
24
+
25
+ # Calculate seats and passengers for each aircraft based on associated flight_segments
26
+ find_each do |a|
27
+ if a.seats = a.flight_segments.weighted_average(:seats_per_flight, :weighted_by => :passengers)
28
+ a.seats_specificity = 'aircraft'
29
+ end
30
+ if (passengers = a.flight_segments.sum(:passengers)) > 0
31
+ a.passengers = passengers
32
+ end
33
+ a.save!
34
+ end
35
+
36
+ # Calculate seats for any aircraft that don't have any flight_segments by averaging across all aircraft with flight segments in the aircraft class
37
+ where(:seats => nil).find_each do |a|
38
+ if a.seats = where(:class_code => a.class_code, :seats_specificity => 'aircraft').weighted_average(:seats, :weighted_by => :passengers)
39
+ a.seats_specificity = 'aircraft_class'
40
+ a.save!
41
+ end
42
+ end
43
+
44
+ # Calculate any missing fuel use coefficients by averaging across all aircraft with fuel use coefficients in the same aircraft class
45
+ where(:m3 => nil).find_each do |a|
46
+ a.m3 = where(:class_code => a.class_code, :fuel_use_specificity => 'aircraft').weighted_average(:m3, :weighted_by => :passengers)
47
+ a.m2 = where(:class_code => a.class_code, :fuel_use_specificity => 'aircraft').weighted_average(:m2, :weighted_by => :passengers)
48
+ a.m1 = where(:class_code => a.class_code, :fuel_use_specificity => 'aircraft').weighted_average(:m1, :weighted_by => :passengers)
49
+ a.b = where(:class_code => a.class_code, :fuel_use_specificity => 'aircraft').weighted_average(:b, :weighted_by => :passengers)
50
+ if a.valid_fuel_use_equation?
51
+ a.m3_units = 'kilograms_per_cubic_nautical_mile'
52
+ a.m2_units = 'kilograms_per_square_nautical_mile'
53
+ a.m1_units = 'kilograms_per_nautical_mile'
54
+ a.b_units = 'kilograms'
55
+ a.fuel_use_specificity = 'aircraft_class'
56
+ a.save!
57
+ end
43
58
  end
44
59
  end
45
-
60
+
46
61
  # Cache fuzzy matches between FlightSegment aircraft_description and Aircraft description
47
62
  def manually_cache_flight_segments!
48
63
  FlightSegment.run_data_miner!
@@ -56,16 +71,16 @@ class Aircraft < ActiveRecord::Base
56
71
  # Pull out the complete first aircraft description
57
72
  # e.g. 'boeing 747-100'
58
73
  first_description = original_description.split('/')[0]
59
-
74
+
60
75
  # Pull out the root of the description - the text up to and including the last ' ' or '-'
61
76
  # e.g. 'boeing 747-'
62
77
  root_length = first_description.rindex(/[ \-]/)
63
78
  root = first_description.slice(0..root_length)
64
-
79
+
65
80
  # Pull out the suffixes - the text separated by forward slashes
66
81
  # e.g. ['100', '200']
67
82
  suffixes = original_description.split(root)[1].split('/')
68
-
83
+
69
84
  # Create an array of synthesized descriptions by appending each suffix to the root
70
85
  # e.g. ['boeing 747-100', 'boeing 747-200']
71
86
  suffixes.map{ |suffix| root + suffix }.each do |synthesized_description|
@@ -91,4 +106,39 @@ class Aircraft < ActiveRecord::Base
91
106
  end
92
107
  end
93
108
  end
109
+
110
+ def valid_fuel_use_equation?
111
+ [m3, m2, m1, b].all?(&:present?) and [m3, m2, m1, b].any?(&:nonzero?)
112
+ end
113
+
114
+ falls_back_on :m3 => lambda { weighted_average(:m3, :weighted_by => :passengers) }, # 9.73423082858437e-08 r7110: 8.6540464368905e-8 r6972: 8.37e-8
115
+ :m2 => lambda { weighted_average(:m2, :weighted_by => :passengers) }, # -0.000134350543484608 r7110: -0.00015337661447817 r6972: -4.09e-5
116
+ :m1 => lambda { weighted_average(:m1, :weighted_by => :passengers) }, # 6.7728101555467 r7110: 4.7781966869412 r6972: 7.85
117
+ :b => lambda { weighted_average(:b, :weighted_by => :passengers) }, # 1527.81790006167 r7110: 1065.3476555284 r6972: 1.72e3
118
+ :m3_units => 'kilograms_per_cubic_nautical_mile',
119
+ :m2_units => 'kilograms_per_square_nautical_mile',
120
+ :m1_units => 'kilograms_per_nautical_mile',
121
+ :b_units => 'kilograms'
122
+
123
+ col :icao_code
124
+ col :manufacturer_name
125
+ col :model_name
126
+ col :description
127
+ col :aircraft_type
128
+ col :engine_type
129
+ col :engines, :type => :integer
130
+ col :weight_class
131
+ col :class_code
132
+ col :passengers, :type => :float
133
+ col :seats, :type => :float
134
+ col :seats_specificity
135
+ col :m3, :type => :float
136
+ col :m3_units
137
+ col :m2, :type => :float
138
+ col :m2_units
139
+ col :m1, :type => :float
140
+ col :m1_units
141
+ col :b, :type => :float
142
+ col :b_units
143
+ col :fuel_use_specificity
94
144
  end
@@ -51,34 +51,27 @@ Aircraft.class_eval do
51
51
  store 'weight_class'
52
52
  end
53
53
 
54
- import "a curated list of aircraft fuel use codes",
55
- :url => 'https://spreadsheets.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdGxqRjFJdDlQWVVLYS11NnJVcDZsYWc&output=csv' do
54
+ import "aircraft fuel use equations derived from EMEP/EEA and ICAO",
55
+ :url => 'https://docs.google.com/spreadsheet/pub?key=0AoQJbWqPrREqdEhYenF3dGt1T0Y1cTdneUNsNjV0dEE&output=csv' do
56
56
  key 'icao_code'
57
- store 'fuel_use_code'
57
+ store 'm3', :units_field_name => 'm3_units'
58
+ store 'm2', :units_field_name => 'm2_units'
59
+ store 'm1', :units_field_name => 'm1_units'
60
+ store 'b', :units_field_name => 'b_units'
61
+ store 'fuel_use_specificity', :static => 'aircraft'
58
62
  end
59
63
 
60
64
  process "Synthesize description from manufacturer name and model name" do
61
- find_each do |aircraft|
62
- aircraft.update_attribute :description, [aircraft.manufacturer_name, aircraft.model_name].join(' ').downcase
63
- end
65
+ update_all "description = LOWER(manufacturer_name || ' ' || model_name)"
64
66
  end
65
67
 
66
68
  process "Synthesize class code from engine type and weight class" do
67
- find_each do |aircraft|
68
- size = case aircraft.weight_class
69
- when 'Small', 'Small+', 'Light'
70
- 'Light'
71
- when 'Large', 'Medium'
72
- 'Medium'
73
- else
74
- 'Heavy'
75
- end
76
- aircraft.update_attribute :class_code, [size, aircraft.engines.to_s, 'engine', aircraft.engine_type].join(' ')
77
- end
69
+ where(:weight_class => ['Small', 'Small+', 'Light']).update_all "class_code = 'Light ' || engines || ' engine ' || engine_type"
70
+ where(:weight_class => ['Medium', 'Large'] ).update_all "class_code = 'Medium ' || engines || ' engine ' || engine_type"
71
+ where(:weight_class => 'Heavy' ).update_all "class_code = 'Heavy ' || engines || ' engine ' || engine_type"
78
72
  end
79
73
 
74
+ # Calculate seats and passengers from flight_segments
80
75
  process :update_averages!
81
-
82
- # FIXME TODO verify this
83
76
  end
84
77
  end
@@ -6,4 +6,6 @@ class AircraftInstance < ActiveRecord::Base
6
6
  col :serial_number
7
7
  col :aircraft_description
8
8
  col :airline_name
9
+ add_index :registration
10
+ add_index :serial_number
9
11
  end
@@ -9,4 +9,5 @@ class AircraftInstanceSeatClass < ActiveRecord::Base
9
9
  col :seat_pitch_units
10
10
  col :seat_width, :type => :float
11
11
  col :seat_width_units
12
+ add_index :aircraft_instance_id
12
13
  end
@@ -46,7 +46,7 @@ Airport.class_eval do
46
46
  process "Fill in blank country codes" do
47
47
  Country.find_each do |country|
48
48
  next unless country.name.present? and country.iso_3166_code.present?
49
- update_all({ :country_iso_3166_code => country.iso_3166_code }, [ "country_name LIKE ?", country.name ])
49
+ where(["country_name LIKE ?", country.name]).update_all :country_iso_3166_code => country.iso_3166_code
50
50
  end
51
51
  end
52
52
 
@@ -1,6 +1,4 @@
1
1
  require 'earth/air/aircraft/data_miner'
2
- require 'earth/air/aircraft_class/data_miner'
3
- require 'earth/air/aircraft_fuel_use_equation/data_miner'
4
2
  require 'earth/air/airline/data_miner'
5
3
  require 'earth/air/airport/data_miner'
6
4
  require 'earth/air/bts_aircraft/data_miner'
@@ -2,7 +2,7 @@ class FlightDistanceClass < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
4
  def self.find_by_distance(distance)
5
- first :conditions => arel_table[:min_distance].lteq(distance).and(arel_table[:max_distance].gt(distance))
5
+ first :conditions => arel_table[:min_distance].lteq(distance.to_f).and(arel_table[:max_distance].gt(distance.to_f))
6
6
  end
7
7
 
8
8
  col :name
@@ -12,5 +12,4 @@ class FlightDistanceClass < ActiveRecord::Base
12
12
  col :min_distance_units
13
13
  col :max_distance, :type => :float
14
14
  col :max_distance_units
15
- col :passengers, :type => :float
16
- end
15
+ end
@@ -11,18 +11,5 @@ FlightDistanceClass.class_eval do
11
11
  # FIXME TODO verify that min_distance >= 0
12
12
  # FIXME TODO verify that max_distance > 0
13
13
  # FIXME TODO verify that distance class distance bounds don't overlap
14
-
15
- process "Ensure FlightSegment is populated" do
16
- FlightSegment.run_data_miner!
17
- end
18
-
19
- process "Calculate passengers for each distance class" do
20
- find_each do |distance_class|
21
- distance_class.passengers = FlightSegment.distances_between(distance_class.min_distance, distance_class.max_distance).sum(:passengers)
22
- distance_class.save!
23
- end
24
- end
25
-
26
- # FIXME TODO verify this
27
14
  end
28
15
  end
@@ -1,10 +1,11 @@
1
1
  class FlightDistanceClassSeatClass < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
- belongs_to :distance_class, :foreign_key => 'distance_class_name', :primary_key => 'name', :class_name => 'FlightDistanceClass'
4
+ falls_back_on :name => 'fallback',
5
+ :multiplier => 1.0
5
6
 
6
7
  col :name
7
8
  col :distance_class_name
8
9
  col :seat_class_name
9
10
  col :multiplier, :type => :float
10
- end
11
+ end
@@ -1,11 +1,5 @@
1
1
  class FlightSeatClass < ActiveRecord::Base
2
2
  set_primary_key :name
3
3
 
4
- has_many :distance_class_seat_classes, :foreign_key => 'seat_class_name', :primary_key => 'name', :class_name => 'FlightDistanceClassSeatClass'
5
-
6
- falls_back_on :name => 'fallback',
7
- :multiplier => 1
8
-
9
4
  col :name
10
- col :multiplier, :type => :float
11
5
  end
@@ -9,12 +9,5 @@ FlightSeatClass.class_eval do
9
9
  find_or_create_by_name seat_class_name
10
10
  end
11
11
  end
12
-
13
- process "Calculate multipliers from FlightDistanceClassSeatClass and FlightDistanceClass" do
14
- find_each do |seat_class|
15
- seat_class.multiplier = seat_class.distance_class_seat_classes.weighted_average(:multiplier, :weighted_by => [:distance_class, :passengers])
16
- seat_class.save!
17
- end
18
- end
19
12
  end
20
13
  end
@@ -14,41 +14,57 @@ class FlightSegment < ActiveRecord::Base
14
14
  # Enable flight_segment.aircraft
15
15
  cache_loose_tight_dictionary_matches_with :aircraft, :primary_key => :aircraft_description, :foreign_key => :description
16
16
 
17
+ class << self
18
+ def update_averages!
19
+ # Derive load factor, which is passengers divided by available seats
20
+ where('seats > 0').update_all 'load_factor = 1.0 * passengers / seats'
21
+
22
+ # Assume a load factor of 1 where passengers > available seats
23
+ where('passengers > seats AND seats > 0').update_all 'load_factor = 1'
24
+
25
+ # FIXME TODO: what is 90.718474
26
+ # Derive freight share as a fraction of the total weight carried
27
+ where('(freight + mail + passengers) > 0').update_all 'freight_share = 1.0 * (freight + mail) / (freight + mail + (passengers * 90.718474))'
28
+
29
+ # Derive average seats per flight
30
+ where('flights > 0').update_all 'seats_per_flight = 1.0 * seats / flights'
31
+ end
32
+ end
33
+
17
34
  falls_back_on :distance => lambda { weighted_average(:distance, :weighted_by => :passengers) }, # 2077.1205 data1 10-12-2010
18
35
  :seats_per_flight => lambda { weighted_average(:seats_per_flight, :weighted_by => :passengers) }, # 144.15653537046 data1 10-12-2010
19
36
  :load_factor => lambda { weighted_average(:load_factor, :weighted_by => :passengers) }, # 0.78073233770097 data1 10-12-2010
20
37
  :freight_share => lambda { weighted_average(:freight_share, :weighted_by => :passengers) } # 0.022567224170157 data1 10-12-2010
21
38
 
22
- col :row_hash # auto-generated primary key
23
- col :origin_airport_iata_code # iata code
24
- col :origin_airport_city # city
25
- col :origin_country_iso_3166_code # iso code
26
- col :destination_airport_iata_code # iata code
27
- col :destination_airport_city # city
28
- col :destination_country_iso_3166_code # iso code
29
- col :airline_bts_code # bts code
30
- col :airline_icao_code # icao code
31
- col :airline_name # text description derived from bts or icao code
32
- col :aircraft_bts_code # bts code
33
- col :aircraft_description # text description derived from BTS T100 or ICAO TFS
34
- col :flights, :type => :integer # number of flights over month or year
35
- col :passengers, :type => :integer # total passengers on all flights
36
- col :seats, :type => :integer # total seats on all flights
37
- col :seats_per_flight, :type => :float # average seats per flight; make this a float
38
- col :load_factor, :type => :float # passengers / seats
39
- col :freight_share, :type => :float # (freight + mail) / (freight + mail + (passengers * average passenger weight))
40
- col :distance, :type => :float # flight distance
41
- col :distance_units # 'kilometres'
42
- col :payload_capacity, :type => :float # aircraft maximum payload capacity rating; float b/c unit conversion
43
- col :payload_capacity_units # 'kilograms'
44
- col :freight, :type => :float # total freight on all flights performed; float b/c unit conversion
45
- col :freight_units # 'kilograms'
46
- col :mail, :type => :float # total mail on all flights performed; float b/c unit conversion
47
- col :mail_units # 'kilograms'
48
- col :month, :type => :integer # month of flight
49
- col :year, :type => :integer # year of flight
50
- col :approximate_date, :type => :date # assumed 14th day of month
51
- col :source # 'BTS T100' or 'ICAO TFS'
39
+ col :row_hash # auto-generated primary key
40
+ col :origin_airport_iata_code # iata code
41
+ col :origin_airport_city # city
42
+ col :origin_country_iso_3166_code # iso code
43
+ col :destination_airport_iata_code # iata code
44
+ col :destination_airport_city # city
45
+ col :destination_country_iso_3166_code # iso code
46
+ col :airline_bts_code # bts code
47
+ col :airline_icao_code # icao code
48
+ col :airline_name # text description derived from bts or icao code
49
+ col :aircraft_bts_code # bts code
50
+ col :aircraft_description # text description derived from BTS T100 or ICAO TFS
51
+ col :flights, :type => :integer # number of flights over month or year
52
+ col :passengers, :type => :integer # total passengers on all flights
53
+ col :seats, :type => :integer # total seats on all flights
54
+ col :seats_per_flight, :type => :float # average seats per flight; make this a float
55
+ col :load_factor, :type => :float # passengers / seats
56
+ col :freight_share, :type => :float # (freight + mail) / (freight + mail + (passengers * average passenger weight))
57
+ col :distance, :type => :float # flight distance
58
+ col :distance_units # 'kilometres'
59
+ col :payload_capacity, :type => :float # aircraft maximum payload capacity rating; float b/c unit conversion
60
+ col :payload_capacity_units # 'kilograms'
61
+ col :freight, :type => :float # total freight on all flights performed; float b/c unit conversion
62
+ col :freight_units # 'kilograms'
63
+ col :mail, :type => :float # total mail on all flights performed; float b/c unit conversion
64
+ col :mail_units # 'kilograms'
65
+ col :month, :type => :integer # month of flight
66
+ col :year, :type => :integer # year of flight
67
+ col :source # 'BTS T100' or 'ICAO TFS'
52
68
  add_index :origin_airport_iata_code
53
69
  add_index :origin_airport_city
54
70
  add_index :destination_airport_iata_code
@@ -59,26 +75,4 @@ class FlightSegment < ActiveRecord::Base
59
75
  add_index :aircraft_bts_code
60
76
  add_index :aircraft_description
61
77
  add_index :year
62
-
63
- def self.distances_between(floor, ceiling)
64
- where arel_table[:distance].gteq(floor).and(arel_table[:distance].lt(ceiling))
65
- end
66
-
67
- def self.update_averages!
68
- # Derive load factor, which is passengers divided by available seats
69
- update_all 'load_factor = 1.0 * passengers / seats', 'seats > 0'
70
-
71
- # Assume a load factor of 1 where passengers > available seats
72
- update_all 'load_factor = 1', 'passengers > seats AND seats > 0'
73
-
74
- # TODO: what is 90.718474
75
- # Derive freight share as a fraction of the total weight carried
76
- update_all 'freight_share = 1.0 * (freight + mail) / (freight + mail + (passengers * 90.718474))', '(freight + mail + passengers) > 0'
77
-
78
- # Derive average seats per flight
79
- update_all 'seats_per_flight = 1.0 * seats / flights', 'flights > 0'
80
-
81
- # Add a useful date field
82
- update_all %{approximate_date = DATE(year || '-' || month || '-' || '14')}, 'month IS NOT NULL'
83
- end
84
- end
78
+ end
@@ -222,7 +222,7 @@ FlightSegment.class_eval do
222
222
  process "Look up airline name based on BTS code" do
223
223
  connection.select_values("SELECT DISTINCT airline_bts_code FROM flight_segments WHERE airline_bts_code IS NOT NULL").each do |bts_code|
224
224
  if airline = Airline.find_by_bts_code(bts_code)
225
- update_all({ :airline_name => airline.name }, :airline_bts_code => bts_code)
225
+ where(:airline_bts_code => bts_code).update_all :airline_name => airline.name
226
226
  end
227
227
  end
228
228
  end
@@ -230,7 +230,7 @@ FlightSegment.class_eval do
230
230
  process "Look up aircraft description based on BTS code" do
231
231
  connection.select_values("SELECT DISTINCT aircraft_bts_code FROM flight_segments WHERE aircraft_bts_code IS NOT NULL").each do |bts_code|
232
232
  if aircraft = BtsAircraft.find_by_bts_code(bts_code)
233
- update_all({ :aircraft_description => aircraft.description.downcase }, :aircraft_bts_code => bts_code)
233
+ where(:aircraft_bts_code => bts_code).update_all :aircraft_description => aircraft.description.downcase
234
234
  end
235
235
  end
236
236
  end