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