| Conditions | 16 |
| Total Lines | 243 |
| Code Lines | 97 |
| Lines | 70 |
| Ratio | 28.81 % |
| 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.etrago_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 | """ |
||
| 37 | def etrago_eGon2035_electricity(): |
||
| 38 | """Execute basic sanity checks. |
||
| 39 | |||
| 40 | Returns print statements as sanity checks for the electricity sector in |
||
| 41 | the eGon2035 scenario. |
||
| 42 | |||
| 43 | Parameters |
||
| 44 | ---------- |
||
| 45 | None |
||
| 46 | |||
| 47 | Returns |
||
| 48 | ------- |
||
| 49 | None |
||
| 50 | """ |
||
| 51 | |||
| 52 | scn = "eGon2035" |
||
| 53 | |||
| 54 | # Section to check generator capacities |
||
| 55 | logger.info(f"Sanity checks for scenario {scn}") |
||
| 56 | logger.info( |
||
| 57 | "For German electricity generators the following deviations between " |
||
| 58 | "the inputs and outputs can be observed:" |
||
| 59 | ) |
||
| 60 | |||
| 61 | carriers_electricity = [ |
||
| 62 | "other_non_renewable", |
||
| 63 | "other_renewable", |
||
| 64 | "reservoir", |
||
| 65 | "run_of_river", |
||
| 66 | "oil", |
||
| 67 | "wind_onshore", |
||
| 68 | "wind_offshore", |
||
| 69 | "solar", |
||
| 70 | "solar_rooftop", |
||
| 71 | "biomass", |
||
| 72 | ] |
||
| 73 | |||
| 74 | for carrier in carriers_electricity: |
||
| 75 | |||
| 76 | if carrier == "biomass": |
||
| 77 | sum_output = db.select_dataframe( |
||
| 78 | """SELECT scn_name, SUM(p_nom::numeric) as output_capacity_mw |
||
| 79 | FROM grid.egon_etrago_generator |
||
| 80 | WHERE bus IN ( |
||
| 81 | SELECT bus_id FROM grid.egon_etrago_bus |
||
| 82 | WHERE scn_name = 'eGon2035' |
||
| 83 | AND country = 'DE') |
||
| 84 | AND carrier IN ('biomass', 'industrial_biomass_CHP', |
||
| 85 | 'central_biomass_CHP') |
||
| 86 | GROUP BY (scn_name); |
||
| 87 | """, |
||
| 88 | warning=False, |
||
| 89 | ) |
||
| 90 | |||
| 91 | else: |
||
| 92 | sum_output = db.select_dataframe( |
||
| 93 | f"""SELECT scn_name, |
||
| 94 | SUM(p_nom::numeric) as output_capacity_mw |
||
| 95 | FROM grid.egon_etrago_generator |
||
| 96 | WHERE scn_name = '{scn}' |
||
| 97 | AND carrier IN ('{carrier}') |
||
| 98 | AND bus IN |
||
| 99 | (SELECT bus_id |
||
| 100 | FROM grid.egon_etrago_bus |
||
| 101 | WHERE scn_name = 'eGon2035' |
||
| 102 | AND country = 'DE') |
||
| 103 | GROUP BY (scn_name); |
||
| 104 | """, |
||
| 105 | warning=False, |
||
| 106 | ) |
||
| 107 | |||
| 108 | sum_input = db.select_dataframe( |
||
| 109 | f"""SELECT carrier, SUM(capacity::numeric) as input_capacity_mw |
||
| 110 | FROM supply.egon_scenario_capacities |
||
| 111 | WHERE carrier= '{carrier}' |
||
| 112 | AND scenario_name ='{scn}' |
||
| 113 | GROUP BY (carrier); |
||
| 114 | """, |
||
| 115 | warning=False, |
||
| 116 | ) |
||
| 117 | |||
| 118 | View Code Duplication | if ( |
|
|
|
|||
| 119 | sum_output.output_capacity_mw.sum() == 0 |
||
| 120 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 121 | ): |
||
| 122 | logger.info( |
||
| 123 | f"No capacity for carrier '{carrier}' needed to be" |
||
| 124 | f" distributed. Everything is fine" |
||
| 125 | ) |
||
| 126 | |||
| 127 | elif ( |
||
| 128 | sum_input.input_capacity_mw.sum() > 0 |
||
| 129 | and sum_output.output_capacity_mw.sum() == 0 |
||
| 130 | ): |
||
| 131 | logger.info( |
||
| 132 | f"Error: Capacity for carrier '{carrier}' was not distributed " |
||
| 133 | f"at all!" |
||
| 134 | ) |
||
| 135 | |||
| 136 | elif ( |
||
| 137 | sum_output.output_capacity_mw.sum() > 0 |
||
| 138 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 139 | ): |
||
| 140 | logger.info( |
||
| 141 | f"Error: Eventhough no input capacity was provided for carrier" |
||
| 142 | f"'{carrier}' a capacity got distributed!" |
||
| 143 | ) |
||
| 144 | |||
| 145 | else: |
||
| 146 | sum_input["error"] = ( |
||
| 147 | (sum_output.output_capacity_mw - sum_input.input_capacity_mw) |
||
| 148 | / sum_input.input_capacity_mw |
||
| 149 | ) * 100 |
||
| 150 | g = sum_input["error"].values[0] |
||
| 151 | |||
| 152 | logger.info(f"{carrier}: " + str(round(g, 2)) + " %") |
||
| 153 | |||
| 154 | # Section to check storage units |
||
| 155 | |||
| 156 | logger.info(f"Sanity checks for scenario {scn}") |
||
| 157 | logger.info( |
||
| 158 | "For German electrical storage units the following deviations between" |
||
| 159 | "the inputs and outputs can be observed:" |
||
| 160 | ) |
||
| 161 | |||
| 162 | carriers_electricity = ["pumped_hydro"] |
||
| 163 | |||
| 164 | for carrier in carriers_electricity: |
||
| 165 | |||
| 166 | sum_output = db.select_dataframe( |
||
| 167 | f"""SELECT scn_name, SUM(p_nom::numeric) as output_capacity_mw |
||
| 168 | FROM grid.egon_etrago_storage |
||
| 169 | WHERE scn_name = '{scn}' |
||
| 170 | AND carrier IN ('{carrier}') |
||
| 171 | AND bus IN |
||
| 172 | (SELECT bus_id |
||
| 173 | FROM grid.egon_etrago_bus |
||
| 174 | WHERE scn_name = 'eGon2035' |
||
| 175 | AND country = 'DE') |
||
| 176 | GROUP BY (scn_name); |
||
| 177 | """, |
||
| 178 | warning=False, |
||
| 179 | ) |
||
| 180 | |||
| 181 | sum_input = db.select_dataframe( |
||
| 182 | f"""SELECT carrier, SUM(capacity::numeric) as input_capacity_mw |
||
| 183 | FROM supply.egon_scenario_capacities |
||
| 184 | WHERE carrier= '{carrier}' |
||
| 185 | AND scenario_name ='{scn}' |
||
| 186 | GROUP BY (carrier); |
||
| 187 | """, |
||
| 188 | warning=False, |
||
| 189 | ) |
||
| 190 | |||
| 191 | View Code Duplication | if ( |
|
| 192 | sum_output.output_capacity_mw.sum() == 0 |
||
| 193 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 194 | ): |
||
| 195 | logger.info( |
||
| 196 | f"No capacity for carrier '{carrier}' needed to be " |
||
| 197 | f"distributed. Everything is fine" |
||
| 198 | ) |
||
| 199 | |||
| 200 | elif ( |
||
| 201 | sum_input.input_capacity_mw.sum() > 0 |
||
| 202 | and sum_output.output_capacity_mw.sum() == 0 |
||
| 203 | ): |
||
| 204 | logger.info( |
||
| 205 | f"Error: Capacity for carrier '{carrier}' was not distributed" |
||
| 206 | f" at all!" |
||
| 207 | ) |
||
| 208 | |||
| 209 | elif ( |
||
| 210 | sum_output.output_capacity_mw.sum() > 0 |
||
| 211 | and sum_input.input_capacity_mw.sum() == 0 |
||
| 212 | ): |
||
| 213 | logger.info( |
||
| 214 | f"Error: Eventhough no input capacity was provided for carrier" |
||
| 215 | f" '{carrier}' a capacity got distributed!" |
||
| 216 | ) |
||
| 217 | |||
| 218 | else: |
||
| 219 | sum_input["error"] = ( |
||
| 220 | (sum_output.output_capacity_mw - sum_input.input_capacity_mw) |
||
| 221 | / sum_input.input_capacity_mw |
||
| 222 | ) * 100 |
||
| 223 | g = sum_input["error"].values[0] |
||
| 224 | |||
| 225 | logger.info(f"{carrier}: " + str(round(g, 2)) + " %") |
||
| 226 | |||
| 227 | # Section to check loads |
||
| 228 | |||
| 229 | logger.info( |
||
| 230 | "For German electricity loads the following deviations between the" |
||
| 231 | " input and output can be observed:" |
||
| 232 | ) |
||
| 233 | |||
| 234 | output_demand = db.select_dataframe( |
||
| 235 | """SELECT a.scn_name, a.carrier, SUM((SELECT SUM(p) |
||
| 236 | FROM UNNEST(b.p_set) p))/1000000::numeric as load_twh |
||
| 237 | FROM grid.egon_etrago_load a |
||
| 238 | JOIN grid.egon_etrago_load_timeseries b |
||
| 239 | ON (a.load_id = b.load_id) |
||
| 240 | JOIN grid.egon_etrago_bus c |
||
| 241 | ON (a.bus=c.bus_id) |
||
| 242 | AND b.scn_name = 'eGon2035' |
||
| 243 | AND a.scn_name = 'eGon2035' |
||
| 244 | AND a.carrier = 'AC' |
||
| 245 | AND c.scn_name= 'eGon2035' |
||
| 246 | AND c.country='DE' |
||
| 247 | GROUP BY (a.scn_name, a.carrier); |
||
| 248 | |||
| 249 | """, |
||
| 250 | warning=False, |
||
| 251 | )["load_twh"].values[0] |
||
| 252 | |||
| 253 | input_cts_ind = db.select_dataframe( |
||
| 254 | """SELECT scenario, |
||
| 255 | SUM(demand::numeric/1000000) as demand_mw_regio_cts_ind |
||
| 256 | FROM demand.egon_demandregio_cts_ind |
||
| 257 | WHERE scenario= 'eGon2035' |
||
| 258 | AND year IN ('2035') |
||
| 259 | GROUP BY (scenario); |
||
| 260 | |||
| 261 | """, |
||
| 262 | warning=False, |
||
| 263 | )["demand_mw_regio_cts_ind"].values[0] |
||
| 264 | |||
| 265 | input_hh = db.select_dataframe( |
||
| 266 | """SELECT scenario, SUM(demand::numeric/1000000) as demand_mw_regio_hh |
||
| 267 | FROM demand.egon_demandregio_hh |
||
| 268 | WHERE scenario= 'eGon2035' |
||
| 269 | AND year IN ('2035') |
||
| 270 | GROUP BY (scenario); |
||
| 271 | """, |
||
| 272 | warning=False, |
||
| 273 | )["demand_mw_regio_hh"].values[0] |
||
| 274 | |||
| 275 | input_demand = input_hh + input_cts_ind |
||
| 276 | |||
| 277 | e = round((output_demand - input_demand) / input_demand, 2) * 100 |
||
| 278 | |||
| 279 | logger.info(f"electricity demand: {e} %") |
||
| 280 | |||
| 638 |