import { ReactNode } from "react"; // —- import { ErrorBoundary } from "@/components/ErrorBoundary"; import { Tooltip } from "@/components/Tooltip"; import { useAppSelector } from "@/redux/index"; import type { EmissionsReplacements } from "@/redux/reducers/results"; import type { CombinedSectorsEmissionsData } from "@/calculations/emissions"; import { type EmptyObject } from "@/utilities"; type AnnualMonthlyData = ReturnType; /** * Round number to the nearest 10 and conditionally display '—' if number is * within 10 of zero. */ function formatNumber(number: number) { if (number !== 0 && number < 10 && number > -10) return "—"; const result = Math.round(number / 10) * 10; return result.toLocaleString(); } /** * Calculate numerator ÷ denominator, formatted to three decimal places */ function calculateFraction(numerator: number, denominator: number) { return denominator !== 0 ? (numerator / denominator).toFixed(3) : ""; } /** * Sum the the total annual original, post-EERE, and impacts (difference between * the two) values from the monthly values for each field (pollutant). * * NOTE: normally we'd just use the annual data from each field, but we need * to use the monthly data for ozone season generation and ozone season nox, so * we might as well build up every field from their monthly values. */ function setAnnualMonthlyData( combinedSectorsEmissionsData: CombinedSectorsEmissionsData, ) { if (!combinedSectorsEmissionsData) { return { generation: { original: 0, postEere: 0, impacts: 0 }, ozoneGeneration: { original: 0, postEere: 0, impacts: 0 }, so2: { original: 0, postEere: 0, impacts: 0 }, nox: { original: 0, postEere: 0, impacts: 0 }, ozoneNox: { original: 0, postEere: 0, impacts: 0 }, co2: { original: 0, postEere: 0, impacts: 0 }, pm25: { original: 0, postEere: 0, impacts: 0 }, vocs: { original: 0, postEere: 0, impacts: 0 }, nh3: { original: 0, postEere: 0, impacts: 0 }, }; } const result = Object.entries(combinedSectorsEmissionsData.total).reduce( (object, [key, value]) => { const field = key as keyof typeof combinedSectorsEmissionsData.total; const totalPowerData = value.power; if (totalPowerData) { Object.entries(totalPowerData.monthly).forEach( ([monthlyKey, monthlyData]) => { const month = Number(monthlyKey); const { original, postEere } = monthlyData; /** * Build up ozone season generation and ozone season nox * (Ozone season is between May and September) */ if (month >= 5 && month <= 9) { if (field === "generation") { object.ozoneGeneration.original += original; object.ozoneGeneration.postEere += postEere; object.ozoneGeneration.impacts += postEere - original; } if (field === "nox") { object.ozoneNox.original += original; object.ozoneNox.postEere += postEere; object.ozoneNox.impacts += postEere - original; } } object[field].original += original; object[field].postEere += postEere; object[field].impacts += postEere - original; }, ); } return object; }, { generation: { original: 0, postEere: 0, impacts: 0 }, ozoneGeneration: { original: 0, postEere: 0, impacts: 0 }, so2: { original: 0, postEere: 0, impacts: 0 }, nox: { original: 0, postEere: 0, impacts: 0 }, ozoneNox: { original: 0, postEere: 0, impacts: 0 }, co2: { original: 0, postEere: 0, impacts: 0 }, pm25: { original: 0, postEere: 0, impacts: 0 }, vocs: { original: 0, postEere: 0, impacts: 0 }, nh3: { original: 0, postEere: 0, impacts: 0 }, }, ); return result; } /** * If "replacement" is needed for a pollutant, we'll change the calculated * `original` value for that pollutant to the pollutant's replacement value for * the region (found in the config file), and change the `postEere` value to be * the sum of the replaced `original` value and the calculated `impacts` value. */ function applyEmissionsReplacement(options: { annualMonthlyData: AnnualMonthlyData; emissionsReplacements: EmissionsReplacements | EmptyObject; }) { const { annualMonthlyData, emissionsReplacements } = options; const result = { ...annualMonthlyData }; if (Object.keys(emissionsReplacements).length === 0) return result; Object.entries(emissionsReplacements).forEach(([key, replacementValue]) => { const pollutant = key as keyof typeof annualMonthlyData; const pollutantData = annualMonthlyData[pollutant]; if (pollutantData) { pollutantData.original = replacementValue; pollutantData.postEere = replacementValue + pollutantData.impacts; } }); return result; } function EmissionsReplacementTooltip(props: { field: "generation" | "so2" | "nox" | "co2"; }) { const { field } = props; // prettier-ignore const fieldMarkup = new Map<'generation' | 'so2' | 'nox' | 'co2', ReactNode>() .set('generation', <>Generation) .set('so2', <>SO2) .set('nox', <>NOX) .set('co2', <>CO2); return (

This region features one or more power plants with an infrequent{" "} {fieldMarkup.get(field)} emissions event. {fieldMarkup.get(field)}{" "} emissions changes from these plants are not included in this analysis. See Section 2 of the{" "} AVERT User Manual {" "} for more information.

); } function PowerEmissionsTableContent() { const inputs = useAppSelector(({ impacts }) => impacts.inputs); const combinedSectorsEmissionsData = useAppSelector( ({ results }) => results.combinedSectorsEmissionsData, ); const emissionsReplacements = useAppSelector( ({ results }) => results.emissionsReplacements, ); const annualMonthlyData = setAnnualMonthlyData(combinedSectorsEmissionsData); const data = applyEmissionsReplacement({ annualMonthlyData, emissionsReplacements, }); const { constantMwh, annualGwh, broadProgram, reduction, topHours, onshoreWind, offshoreWind, utilitySolar, rooftopSolar, batteryEVs, hybridEVs, transitBuses, schoolBuses, } = inputs; const eeInputsEmpty = (constantMwh === "" || constantMwh === "0") && (annualGwh === "" || annualGwh === "0") && (broadProgram === "" || broadProgram === "0") && (reduction === "" || reduction === "0") && (topHours === "" || topHours === "0"); const reInputsEmpty = (onshoreWind === "" || onshoreWind === "0") && (offshoreWind === "" || offshoreWind === "0") && (utilitySolar === "" || utilitySolar === "0") && (rooftopSolar === "" || rooftopSolar === "0"); const evInputsEmpty = (batteryEVs === "" || batteryEVs === "0") && (hybridEVs === "" || hybridEVs === "0") && (transitBuses === "" || transitBuses === "0") && (schoolBuses === "" || schoolBuses === "0"); const eereAndEvInputsEntered = (!eeInputsEmpty || !reInputsEmpty) && !evInputsEmpty; const { generation, ozoneGeneration, so2, nox, ozoneNox, co2, pm25, vocs, nh3, } = data; if (!combinedSectorsEmissionsData) return null; return ( <>
  Original Post Change Change
Generation (MWh)  {Object.hasOwn(emissionsReplacements, "generation") && ( )} {formatNumber(generation.original)} {formatNumber(generation.postEere)} {formatNumber(generation.impacts)}
Total Emissions from Fossil Generation Fleet
SO2 (lb)  {Object.hasOwn(emissionsReplacements, "so2") && ( )} {formatNumber(so2.original)} {formatNumber(so2.postEere)} {formatNumber(so2.impacts)}
NOX (lb)  {Object.hasOwn(emissionsReplacements, "nox") && ( )} {formatNumber(nox.original)} {formatNumber(nox.postEere)} {formatNumber(nox.impacts)}
Ozone season NOX (lb){" "}

Ozone season is defined as May 1 — September 30. Ozone season emissions are a subset of annual emissions.

{formatNumber(ozoneNox.original)} {formatNumber(ozoneNox.postEere)} {formatNumber(ozoneNox.impacts)}
CO2 (tons)  {Object.hasOwn(emissionsReplacements, "co2") && ( )} {formatNumber(co2.original)} {formatNumber(co2.postEere)} {formatNumber(co2.impacts)}
PM2.5 (lb) {formatNumber(pm25.original)} {formatNumber(pm25.postEere)} {formatNumber(pm25.impacts)}
VOCs (lb) {formatNumber(vocs.original)} {formatNumber(vocs.postEere)} {formatNumber(vocs.impacts)}
NH3 (lb) {formatNumber(nh3.original)} {formatNumber(nh3.postEere)} {formatNumber(nh3.impacts)}
AVERT-derived Emission Rates: Average Fossil   Marginal Fossil
SO2 (lb/MWh) {calculateFraction(so2.original, generation.original)}   {eereAndEvInputsEntered ? ( Ø ) : ( calculateFraction(so2.impacts, generation.impacts) )}
NOX (lb/MWh) {calculateFraction(nox.original, generation.original)}   {eereAndEvInputsEntered ? ( Ø ) : ( calculateFraction(nox.impacts, generation.impacts) )}
Ozone season NOX (lb/MWh){" "}

Ozone season is defined as May 1 — September 30. Ozone season emissions are a subset of annual emissions.

{calculateFraction( ozoneNox.original, ozoneGeneration.original, )}   {eereAndEvInputsEntered ? ( Ø ) : ( calculateFraction(ozoneNox.impacts, ozoneGeneration.impacts) )}
CO2 (tons/MWh) {calculateFraction(co2.original, generation.original)}   {eereAndEvInputsEntered ? ( Ø ) : ( calculateFraction(co2.impacts, generation.impacts) )}
PM2.5 (lb/MWh) {calculateFraction(pm25.original, generation.original)}   {eereAndEvInputsEntered ? ( Ø ) : ( calculateFraction(pm25.impacts, generation.impacts) )}
VOCs (lb/MWh) {calculateFraction(vocs.original, generation.original)}   {eereAndEvInputsEntered ? ( Ø ) : ( calculateFraction(vocs.impacts, generation.impacts) )}
NH3 (lb/MWh) {calculateFraction(nh3.original, generation.original)}   {eereAndEvInputsEntered ? ( Ø ) : ( calculateFraction(nh3.impacts, generation.impacts) )}

{/* NOTE: hidden paragraph is intentional to get around EPA's * `ul:last-child { margin-bottom: revert; }` style */}

); } export function PowerEmissionsTable() { return ( Error loading power sector emissions table. Please contact AVERT support at{" "} avert@epa.gov } > ); }