| Conditions | 16 |
| Total Lines | 230 |
| Code Lines | 97 |
| Lines | 64 |
| Ratio | 27.83 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like data.datasets.sanity_checks.sanitycheck_eGon2035_electricity() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """ |
||
| 24 | residential_electricity_annual_sum, |
||
| 25 | residential_electricity_hh_refinement, |
||
| 26 | }, |
||
| 27 | ) |
||
| 28 | |||
| 29 | |||
| 30 | def etrago_eGon2035_electricity(): |
||
| 31 | """Execute basic sanity checks. |
||
| 32 | |||
| 33 | Returns print statements as sanity checks for the electricity sector in |
||
| 34 | the eGon2035 scenario. |
||
| 35 | |||
| 36 | Parameters |
||
| 37 | ---------- |
||
| 38 | None |
||
| 39 | |||
| 40 | Returns |
||
| 41 | ------- |
||
| 42 | None |
||
| 43 | """ |
||
| 44 | |||
| 45 | scn = "eGon2035" |
||
| 46 | |||
| 47 | # Section to check generator capacities |
||
| 48 | print(f"Sanity checks for scenario {scn}") |
||
| 49 | print( |
||
| 50 | "For German electricity generators the following deviations between " |
||
| 51 | "the inputs and outputs can be observed:" |
||
| 52 | ) |
||
| 53 | |||
| 54 | carriers_electricity = [ |
||
| 55 | "other_non_renewable", |
||
| 56 | "other_renewable", |
||
| 57 | "reservoir", |
||
| 58 | "run_of_river", |
||
| 59 | "oil", |
||
| 60 | "wind_onshore", |
||
| 61 | "wind_offshore", |
||
| 62 | "solar", |
||
| 63 | "solar_rooftop", |
||
| 64 | "biomass", |
||
| 65 | ] |
||
| 66 | |||
| 67 | for carrier in carriers_electricity: |
||
| 68 | |||
| 69 | if carrier == "biomass": |
||
| 70 | sum_output = db.select_dataframe( |
||
| 71 | """SELECT scn_name, SUM(p_nom::numeric) as output_capacity_mw |
||
| 72 | FROM grid.egon_etrago_generator |
||
| 73 | WHERE bus IN ( |
||
| 74 | SELECT bus_id FROM grid.egon_etrago_bus |
||
| 75 | WHERE scn_name = 'eGon2035' |
||
| 76 | AND country = 'DE') |
||
| 77 | AND carrier IN ('biomass', 'industrial_biomass_CHP', |
||
| 78 | 'central_biomass_CHP') |
||
| 79 | GROUP BY (scn_name); |
||
| 80 | """, |
||
| 81 | warning=False, |
||
| 82 | ) |
||
| 83 | |||
| 84 | else: |
||
| 85 | sum_output = db.select_dataframe( |
||
| 86 | f"""SELECT scn_name, |
||
| 87 | SUM(p_nom::numeric) as output_capacity_mw |
||
| 88 | FROM grid.egon_etrago_generator |
||
| 89 | WHERE scn_name = '{scn}' |
||
| 90 | AND carrier IN ('{carrier}') |
||
| 91 | AND bus IN |
||
| 92 | (SELECT bus_id |
||
| 93 | FROM grid.egon_etrago_bus |
||
| 94 | WHERE scn_name = 'eGon2035' |
||
| 95 | AND country = 'DE') |
||
| 96 | GROUP BY (scn_name); |
||
| 97 | """, |
||
| 98 | warning=False, |
||
| 99 | ) |
||
| 100 | |||
| 101 | sum_input = db.select_dataframe( |
||
| 102 | f"""SELECT carrier, SUM(capacity::numeric) as input_capacity_mw |
||
| 103 | FROM supply.egon_scenario_capacities |
||
| 104 | WHERE carrier= '{carrier}' |
||
| 105 | AND scenario_name ='{scn}' |
||
| 106 | GROUP BY (carrier); |
||
| 107 | """, |
||
| 108 | warning=False, |
||
| 109 | ) |
||
| 110 | |||
| 111 | View Code Duplication | if ( |
|
|
|
|||
| 112 | sum_output.output_capacity_mw.sum() == 0 |
||
| 113 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 114 | ): |
||
| 115 | print( |
||
| 116 | f"No capacity for carrier '{carrier}' needed to be" |
||
| 117 | f" distributed. Everything is fine" |
||
| 118 | ) |
||
| 119 | |||
| 120 | elif ( |
||
| 121 | sum_input.input_capacity_mw.sum() > 0 |
||
| 122 | and sum_output.output_capacity_mw.sum() == 0 |
||
| 123 | ): |
||
| 124 | print( |
||
| 125 | f"Error: Capacity for carrier '{carrier}' was not distributed " |
||
| 126 | f"at all!" |
||
| 127 | ) |
||
| 128 | |||
| 129 | elif ( |
||
| 130 | sum_output.output_capacity_mw.sum() > 0 |
||
| 131 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 132 | ): |
||
| 133 | print( |
||
| 134 | f"Error: Eventhough no input capacity was provided for carrier" |
||
| 135 | f"'{carrier}' a capacity got distributed!" |
||
| 136 | ) |
||
| 137 | |||
| 138 | else: |
||
| 139 | sum_input["error"] = ( |
||
| 140 | (sum_output.output_capacity_mw - sum_input.input_capacity_mw) |
||
| 141 | / sum_input.input_capacity_mw |
||
| 142 | ) * 100 |
||
| 143 | g = sum_input["error"].values[0] |
||
| 144 | |||
| 145 | print(f"{carrier}: " + str(round(g, 2)) + " %") |
||
| 146 | |||
| 147 | # Section to check storage units |
||
| 148 | |||
| 149 | print(f"Sanity checks for scenario {scn}") |
||
| 150 | print( |
||
| 151 | "For German electrical storage units the following deviations between" |
||
| 152 | "the inputs and outputs can be observed:" |
||
| 153 | ) |
||
| 154 | |||
| 155 | carriers_electricity = ["pumped_hydro"] |
||
| 156 | |||
| 157 | for carrier in carriers_electricity: |
||
| 158 | |||
| 159 | sum_output = db.select_dataframe( |
||
| 160 | f"""SELECT scn_name, SUM(p_nom::numeric) as output_capacity_mw |
||
| 161 | FROM grid.egon_etrago_storage |
||
| 162 | WHERE scn_name = '{scn}' |
||
| 163 | AND carrier IN ('{carrier}') |
||
| 164 | AND bus IN |
||
| 165 | (SELECT bus_id |
||
| 166 | FROM grid.egon_etrago_bus |
||
| 167 | WHERE scn_name = 'eGon2035' |
||
| 168 | AND country = 'DE') |
||
| 169 | GROUP BY (scn_name); |
||
| 170 | """, |
||
| 171 | warning=False, |
||
| 172 | ) |
||
| 173 | |||
| 174 | sum_input = db.select_dataframe( |
||
| 175 | f"""SELECT carrier, SUM(capacity::numeric) as input_capacity_mw |
||
| 176 | FROM supply.egon_scenario_capacities |
||
| 177 | WHERE carrier= '{carrier}' |
||
| 178 | AND scenario_name ='{scn}' |
||
| 179 | GROUP BY (carrier); |
||
| 180 | """, |
||
| 181 | warning=False, |
||
| 182 | ) |
||
| 183 | |||
| 184 | View Code Duplication | if ( |
|
| 185 | sum_output.output_capacity_mw.sum() == 0 |
||
| 186 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 187 | ): |
||
| 188 | print( |
||
| 189 | f"No capacity for carrier '{carrier}' needed to be " |
||
| 190 | f"distributed. Everything is fine" |
||
| 191 | ) |
||
| 192 | |||
| 193 | elif ( |
||
| 194 | sum_input.input_capacity_mw.sum() > 0 |
||
| 195 | and sum_output.output_capacity_mw.sum() == 0 |
||
| 196 | ): |
||
| 197 | print( |
||
| 198 | f"Error: Capacity for carrier '{carrier}' was not distributed" |
||
| 199 | f" at all!" |
||
| 200 | ) |
||
| 201 | |||
| 202 | elif ( |
||
| 203 | sum_output.output_capacity_mw.sum() > 0 |
||
| 204 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 205 | ): |
||
| 206 | print( |
||
| 207 | f"Error: Eventhough no input capacity was provided for carrier" |
||
| 208 | f" '{carrier}' a capacity got distributed!" |
||
| 209 | ) |
||
| 210 | |||
| 211 | else: |
||
| 212 | sum_input["error"] = ( |
||
| 213 | (sum_output.output_capacity_mw - sum_input.input_capacity_mw) |
||
| 214 | / sum_input.input_capacity_mw |
||
| 215 | ) * 100 |
||
| 216 | g = sum_input["error"].values[0] |
||
| 217 | |||
| 218 | print(f"{carrier}: " + str(round(g, 2)) + " %") |
||
| 219 | |||
| 220 | # Section to check loads |
||
| 221 | |||
| 222 | print( |
||
| 223 | "For German electricity loads the following deviations between the" |
||
| 224 | " input and output can be observed:" |
||
| 225 | ) |
||
| 226 | |||
| 227 | output_demand = db.select_dataframe( |
||
| 228 | """SELECT a.scn_name, a.carrier, SUM((SELECT SUM(p) |
||
| 229 | FROM UNNEST(b.p_set) p))/1000000::numeric as load_twh |
||
| 230 | FROM grid.egon_etrago_load a |
||
| 231 | JOIN grid.egon_etrago_load_timeseries b |
||
| 232 | ON (a.load_id = b.load_id) |
||
| 233 | JOIN grid.egon_etrago_bus c |
||
| 234 | ON (a.bus=c.bus_id) |
||
| 235 | AND b.scn_name = 'eGon2035' |
||
| 236 | AND a.scn_name = 'eGon2035' |
||
| 237 | AND a.carrier = 'AC' |
||
| 238 | AND c.scn_name= 'eGon2035' |
||
| 239 | AND c.country='DE' |
||
| 240 | GROUP BY (a.scn_name, a.carrier); |
||
| 241 | |||
| 242 | """, |
||
| 243 | warning=False, |
||
| 244 | )["load_twh"].values[0] |
||
| 245 | |||
| 246 | input_cts_ind = db.select_dataframe( |
||
| 247 | """SELECT scenario, |
||
| 248 | SUM(demand::numeric/1000000) as demand_mw_regio_cts_ind |
||
| 249 | FROM demand.egon_demandregio_cts_ind |
||
| 250 | WHERE scenario= 'eGon2035' |
||
| 251 | AND year IN ('2035') |
||
| 252 | GROUP BY (scenario); |
||
| 253 | |||
| 254 | """, |
||
| 575 |