/// <summary>
        /// Retrieve historic marginal emissions results between the given dates and store them in the database
        /// </summary>
        /// <param name="startDateTime">Optional StartDateTime. If not supplied, a default value will be used</param>
        /// <param name="endDateTime">Optional endDateTime. If not supplied, a default value will be used</param>
        /// <param name="regionWattTimeName">Abbreviation for the required region (e.g. "PJM"). See https://api.watttime.org/faq/#where </param>
        /// <param name="regionId">regionId of this region in the application's database</param>
        public void MineHistoricMarginalCarbonResults(
            DateTime?startDateTime,
            DateTime?endDateTime,
            string regionWattTimeName,
            int regionId)
        {
            var historicStartDateTime = startDateTime ?? DateTime.UtcNow.AddDays(-2);
            var historicEndDateTime   = endDateTime ?? DateTime.UtcNow.AddMinutes(15);

            try
            {
                var results = this.wattTimeEmissionsInteraction.GetObservedMarginalCarbonResults(
                    wattTimeApiUrl,
                    regionWattTimeName,
                    historicStartDateTime,
                    historicEndDateTime,
                    null,
                    wattTimeApiKey);

                Logger.Information(
                    $"Received {results.Count} HistoricMarginalCarbonResults Results for {regionWattTimeName} from WattTime. Inserting them into the database",
                    "CarbonEmissionsMiner.MineHistoricMarginalCarbonResults()");

                // Insert results in the database
                using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString))
                {
                    foreach (var res in results)
                    {
                        var dateTime = res.timestamp;

                        var marginalCarbon = res.marginal_carbon.value;
                        var units          = res.marginal_carbon.units;

                        if (marginalCarbon != null)
                        {
                            var marginalCarbonMetric = units == "lb/MW"
                                                           ? this.wattTimeEmissionsInteraction
                                                       .ConvertLbsPerMWhTo_GCo2PerkWh((double)marginalCarbon)
                                                           : marginalCarbon;

                            _objectModel.InsertOrUpdateCarbonEmissionsDataPoints(
                                regionId,
                                dateTime,
                                null,
                                false,
                                marginalCarbonMetric,
                                false);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(
                    $"CarbonEmissionsMiner: MineHistoricMarginalCarbonResults(): Exception encountered while emissions Data figures for {regionWattTimeName} between {historicStartDateTime} and {historicEndDateTime}.",
                    "CarbonEmissionsMiner.MineHistoricMarginalCarbonResults()",
                    null,
                    e);
            }
        }
Exemple #2
0
        /// <summary>
        /// Retrieve forecast marginal emissions results between the given dates and store them in the database
        /// </summary>
        /// <param name="startDateTime">Optional StartDateTime. If not supplied, a default value will be used</param>
        /// <param name="endDateTime">Optional endDateTime. If not supplied, a default value will be used</param>
        /// <param name="regionWattTimeName">Abbreviation for the required region (e.g. "PJM"). See https://api.watttime.org/faq/#where </param>
        /// <param name="regionId">regionId of this region in the application's database</param>
        public void MineForecastMarginalCarbonResults(
            DateTime?startDateTime,
            DateTime?endDateTime,
            string regionWattTimeName,
            int regionId)
        {
            var historicStartDateTime = startDateTime ?? DateTime.UtcNow.AddDays(-2);
            var historicEndDateTime   = endDateTime ?? DateTime.UtcNow.AddMinutes(15);

            try
            {
                Logger.Information(
                    $"Mining Forecast Marginal Carbon Results for {regionWattTimeName} from WattTime URL {this.wattTimeApiUrl}.",
                    "CarbonEmissionsMiner.MineForecastMarginalCarbonResults()");

                var results = this.wattTimeEmissionsInteraction.GetForecastMarginalCarbonResults(
                    WattTimeV2ApiUrl,
                    regionWattTimeName,
                    WattTimeUsername,
                    WattTimePassword,
                    historicStartDateTime,
                    historicEndDateTime,
                    null);

                Logger.Information(
                    $"Received {results.Count} ForecastMarginalCarbonResults Results for {regionWattTimeName} from WattTime. Inserting them into the database",
                    "CarbonEmissionsMiner.ForecastMarginalCarbonResults()");

                // Insert results in the database
                using (var _objectModel = new SmartEnergyOM(this.DatabaseConnectionString))
                {
                    foreach (var res in results)
                    {
                        var dateTime = res.point_time;

                        var marginalCarbon = res.value;

                        var marginalCarbonMetric = this.wattTimeEmissionsInteraction
                                                   .ConvertLbsPerMWhTo_GCo2PerkWh((double)marginalCarbon);

                        _objectModel.InsertOrUpdateCarbonEmissionsDataPoints(
                            regionId,
                            dateTime,
                            null,
                            null,
                            null,
                            marginalCarbonMetric);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(
                    $"CarbonEmissionsMiner: MineForecastMarginalCarbonResults(): Exception encountered while emissions Data figures for {regionWattTimeName} between {historicStartDateTime} and {historicEndDateTime}.",
                    "CarbonEmissionsMiner.MineForecastMarginalCarbonResults()",
                    null,
                    e);
            }
        }
Exemple #3
0
        /// <summary>
        /// Take a CSV File containing CarbonEmissionsDataPoint values, and add them as CarbonEmissionsDataPoint to the database
        /// </summary>
        /// <param name="folderContainingCsv"></param>
        /// <param name="csvFileName"></param>
        /// <param name="regionId">RegionId to which the CarbonEmissionsDataPoint values pertain</param>
        /// <param name="dateTimeFormat">DateTime format to be passed to DateTime.ParseExact(). E.g. "MM/dd/yyyy HH:mm:ss"</param>
        /// <param name="ignoreParsingErrors">True to ignore errors parsing individual rows. False to rethrow the exceptions.</param>
        /// <returns></returns>
        public void ImportCarbonResultsToDatabaseFromCsv(
            string folderContainingCsv,
            string csvFileName,
            int regionId,
            string dateTimeFormat    = "MM/dd/yyyy HH:mm:ss",
            bool ignoreParsingErrors = true)
        {
            try
            {
                Logger.Information(
                    $"Importing Historic Carbon Results for RegionId {regionId} from CSV {csvFileName}.",
                    "CarbonEmissionsMiner.ImportCarbonResultsToDatabaseFromCsv()");

                var results =
                    ImportCarbonResultsFromCsv(folderContainingCsv, csvFileName, regionId, dateTimeFormat, ignoreParsingErrors);

                using (var objectModel = new SmartEnergyOM(this.DatabaseConnectionString))
                {
                    foreach (var result in results)
                    {
                        // Insert the value into the database
                        objectModel.InsertOrUpdateCarbonEmissionsDataPoints(
                            regionId,
                            result.DateTimeUTC,
                            result.SystemWideCO2Intensity_gCO2kWh,
                            null,
                            result.MarginalCO2Intensity_gCO2kWh,
                            null);
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(
                    $"CarbonEmissionsMiner: ImportCarbonResultsToDatabaseFromCsv(): Exception encountered importing emissions Data from CSV with name {csvFileName} in folder {folderContainingCsv} and inserting them to the database.",
                    "CarbonEmissionsMiner.ImportCarbonResultsToDatabaseFromCsv()",
                    null,
                    e);
            }
        }