22
33package com .digitalpebble .spruce .modules .electricitymaps ;
44
5- import com .digitalpebble .spruce .* ;
6-
7- import static com .digitalpebble .spruce .SpruceColumn .* ;
8-
5+ import com .digitalpebble .spruce .Column ;
6+ import com . digitalpebble . spruce . EnrichmentModule ;
7+ import com .digitalpebble .spruce .Provider ;
8+ import com . digitalpebble . spruce . Utils ;
99import com .digitalpebble .spruce .modules .realtimecloud .RegionMappings ;
1010import org .apache .spark .sql .Row ;
11+ import org .slf4j .Logger ;
12+ import org .slf4j .LoggerFactory ;
1113
1214import java .io .IOException ;
1315import java .util .HashMap ;
1416import java .util .List ;
1517import java .util .Map ;
1618
19+ import static com .digitalpebble .spruce .SpruceColumn .*;
20+
1721/**
1822 * Populate the CARBON_INTENSITY field using ElecticityMaps' 2024 datasets
1923 * for rows where energy usage has been estimated.
2024 **/
2125public class AverageCarbonIntensity implements EnrichmentModule {
2226
23- private final Map < String , Double > average_intensities = new HashMap <>( );
27+ private static final Logger log = LoggerFactory . getLogger ( AverageCarbonIntensity . class );
2428
2529 private final static String DEFAULT_RESOURCE_LOCATION = "electricitymaps/averages_2024.csv" ;
30+ private final Map <String , Double > average_intensities = new HashMap <>();
2631
2732 public void init (Map <String , Object > params ) {
2833 // load the averages for each EM IDs
@@ -53,11 +58,16 @@ public Column[] columnsNeeded() {
5358 }
5459
5560 /**
56- Get the average intensity for the given region ID
57- in gCO2perKWH
61+ * Get the average intensity for the given region ID
62+ * in gCO2perKWH
63+ * or null if the region does not exist
5864 */
5965 protected Double getAverageIntensity (Provider provider , String regionId ) {
6066 String emRegionId = RegionMappings .getEMRegion (provider , regionId );
67+ if (emRegionId == null ) {
68+ log .info ("Region unknown {} for {}" , regionId , provider );
69+ return null ;
70+ }
6171 return average_intensities .get (emRegionId );
6272 }
6373
@@ -79,16 +89,11 @@ public Row process(Row row) {
7989 }
8090
8191 // get intensity for the location
82- try {
83- final double coeff = getAverageIntensity (Provider .AWS , locationCode );
84- if (coeff == 0.0d ) {
85- // if the coefficient is 0 it means that the region is not supported
86- return row ;
87- }
88- return EnrichmentModule .withUpdatedValue (row , CARBON_INTENSITY , coeff );
89- } catch (Exception exception ) {
90- // if the region is not supported, we cannot compute the carbon intensity
92+ Double coeff = getAverageIntensity (Provider .AWS , locationCode );
93+ if (coeff == null ) {
94+ // if the coefficient is 0 it means that the region is not supported
9195 return row ;
9296 }
97+ return EnrichmentModule .withUpdatedValue (row , CARBON_INTENSITY , coeff );
9398 }
9499}
0 commit comments