|
| 1 | +class NaphthaMigration < ActiveRecord::Migration[7.1] |
| 2 | + include ETEngine::ScenarioMigration |
| 3 | + |
| 4 | + RelativeChange = Struct.new(:input_oil, :input_naphtha, :default_key, :naphtha_default_key) do |
| 5 | + def skip?(scenario) |
| 6 | + !scenario.user_values.key?(input_oil) |
| 7 | + end |
| 8 | + |
| 9 | + # Calculates and sets the inputs for crude oil and naphtha on the scenario. |
| 10 | + # Uses the old and new defaults (start values) of the inputs. |
| 11 | + def set_new_values(scenario, old_defaults, new_defaults) |
| 12 | + if change = relative_change(scenario, old_defaults) |
| 13 | + # If a change can be calculated: the inputs are set to the |
| 14 | + # relative change times the new default for both oil and naptha |
| 15 | + scenario.user_values[input_oil] = new_defaults[default_key] * change |
| 16 | + scenario.user_values[input_naphtha] = new_defaults[naphtha_default_key] * change |
| 17 | + else |
| 18 | + # If change can't be calculated, set naphtha to zero, oil stays |
| 19 | + # unchanged |
| 20 | + scenario.user_values[input_naphtha] = 0 |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + # Returns the relative change of the input, based on the old default |
| 25 | + # of the input, and what it was set to by the user |
| 26 | + def relative_change(scenario, defaults) |
| 27 | + unless defaults[default_key].zero? |
| 28 | + return scenario.user_values[input_oil] / defaults[default_key] |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + # Inputs that need to be recalculated when they were set by the user. |
| 34 | + CHANGES = [ |
| 35 | + # Normal energetic |
| 36 | + RelativeChange.new( |
| 37 | + input_oil: 'industry_chemicals_other_burner_crude_oil_share', |
| 38 | + input_naphtha: 'industry_chemicals_other_burner_naphtha_share', |
| 39 | + default_key: 'industry_chemicals_other_burner_crude_oil_share', |
| 40 | + naphtha_default_key: 'industry_chemicals_other_burner_naphtha_share' |
| 41 | + ), |
| 42 | + # Normal non-energetic |
| 43 | + RelativeChange.new( |
| 44 | + input_oil: 'industry_chemicals_other_crude_oil_non_energetic_share', |
| 45 | + input_naphtha: 'industry_chemicals_other_naphtha_non_energetic_share', |
| 46 | + default_key: 'industry_useful_demand_for_chemical_other_crude_oil_non_energetic_share', |
| 47 | + naphtha_default_key: 'industry_useful_demand_for_chemical_other_naphtha_non_energetic_share' |
| 48 | + ), |
| 49 | + # External coupling energetic |
| 50 | + RelativeChange.new( |
| 51 | + input_oil: 'external_coupling_industry_chemical_other_burner_crude_oil_share', |
| 52 | + input_naphtha: 'external_coupling_industry_chemical_other_burner_naphtha_share', |
| 53 | + default_key: 'industry_chemicals_other_burner_crude_oil_share', # same start value |
| 54 | + naphtha_default_key: 'industry_chemicals_other_burner_naphtha_share' |
| 55 | + ), |
| 56 | + # External coupling non-energetic |
| 57 | + RelativeChange.new( |
| 58 | + input_oil: 'external_coupling_industry_chemical_other_non_energetic_crude_oil_share', |
| 59 | + input_naphtha: 'external_coupling_industry_chemical_other_non_energetic_naphtha_share', |
| 60 | + default_key: 'industry_useful_demand_for_chemical_other_crude_oil_non_energetic_share', # same start value |
| 61 | + naphtha_default_key: 'industry_useful_demand_for_chemical_other_naphtha_non_energetic_share' |
| 62 | + ) |
| 63 | + ].freeze |
| 64 | + |
| 65 | + RENAME = %w[fertilizers refineries other].to_h do |sector| |
| 66 | + [ |
| 67 | + "external_coupling_energy_chemical_#{sector}_transformation_external_coupling_node_crude_oil_output_share", |
| 68 | + "external_coupling_energy_chemical_#{sector}_transformation_external_coupling_node_naphtha_output_share" |
| 69 | + ] |
| 70 | + end.freeze |
| 71 | + |
| 72 | + def up |
| 73 | + # Load up defaults from dumps |
| 74 | + old_defaults = load_defaults('old') |
| 75 | + new_defaults = load_defaults('new') |
| 76 | + |
| 77 | + migrate_scenarios do |scenario| |
| 78 | + CHANGES.each do |change| |
| 79 | + next if change.skip?(scenario) |
| 80 | + # NOTE: Some datasets do not work yet on the naphtha branch, skip them |
| 81 | + next unless new_defaults.key?(scenario.area_code) |
| 82 | + |
| 83 | + change.set_new_values( |
| 84 | + scenario, |
| 85 | + old_defaults[scenario.area_code], |
| 86 | + new_defaults[scenario.area_code] |
| 87 | + ) |
| 88 | + end |
| 89 | + RENAME.each do |old_input, new_input| |
| 90 | + if scenario.user_values.key?(old_input) |
| 91 | + scenario.user_values[new_input] = scenario.user_values.delete(old_input) |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + def load_defaults(tag) |
| 98 | + JSON.load(File.read( |
| 99 | + Rails.root.join( |
| 100 | + "db/migrate/#{File.basename(__FILE__, '.rb')}/dataset_values_#{tag}.json" |
| 101 | + ) |
| 102 | + )) |
| 103 | + end |
| 104 | +end |
0 commit comments