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