Пример #1
0
        protected override void RunChartMaking(ScenarioSliceParameters slice)
        {
            var dbProfileExport = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.HouseProfiles);
            var sa = SaveableEntry <Prosumer> .GetSaveableEntry(dbProfileExport, SaveableEntryTableType.HouseLoad, Services.Logger);

            var            dbHouses          = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);
            var            houses            = dbHouses.Fetch <House>();
            var            house             = houses.Single(x => x.ComplexName == "Kornhausgasse 16");
            List <Profile> relevantProsumers = new List <Profile>();

            foreach (var prosumer in sa.ReadEntireTableDBAsEnumerable())
            {
                if (prosumer.HouseGuid == house.Guid)
                {
                    if (prosumer.Profile == null)
                    {
                        throw new FlaException("profile was null");
                    }
                    relevantProsumers.Add(new Profile(prosumer.Name + " " + prosumer.ProfileSourceName, prosumer.Profile.Values, prosumer.Profile.EnergyOrPower));
                }
            }

            var fn = MakeAndRegisterFullFilename("ExportedProfiles.xlsx", slice);

            XlsxDumper.DumpProfilesToExcel(fn, slice.DstYear, 15, new ProfileWorksheetContent("kornhausgasse 16", "Last", relevantProsumers));
        }
        private void ProcessOneSumTypeWithXls([NotNull] ScenarioSliceParameters slice, [NotNull] string fn, SumType sumType)
        {
            var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
            var saHouses  = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedLoadsForAnalysis, Services.Logger);

            var entries         = saHouses.LoadAllOrMatching();
            var providerentries = entries.Where(x => x.Key.SumType == sumType).ToList();
            var dbRaw           = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var bkwJ            = dbRaw.Fetch <BkwProfile>();

            foreach (var ae in providerentries)
            {
                if (sumType == SumType.ByProvider)
                {
                    ae.Profile.Name = ae.Key.ProviderType + " " + ae.GenerationOrLoad;
                }
                else if (sumType == SumType.ByProfileSource)
                {
                    ae.Profile.Name = ae.Key.ProfileSource ?? throw new InvalidOperationException();
                }
                else
                {
                    throw new FlaException("Unknown sum type");
                }
            }
            var profiles = providerentries.Select(x => x.Profile).ToList();
            var bkw      = new Profile(bkwJ[0].Profile);

            bkw.Name = "Messung 2017";
            profiles.Add(bkw);
            XlsxDumper.DumpProfilesToExcel(fn, 2017, 15, new ProfileWorksheetContent("Profile", "Leistung [MW]", bkw.Name, profiles));
        }
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            var dbProfileExport = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.HouseProfiles);
            var sa = SaveableEntry <Prosumer> .GetSaveableEntry(dbProfileExport, SaveableEntryTableType.HouseLoad, Services.Logger);

            int       count = 0;
            Stopwatch sw    = Stopwatch.StartNew();

            foreach (var prosumer in sa.ReadEntireTableDBAsEnumerable())
            {
                var keys = GenerateKeysFromProsumer(prosumer);
                ProcessProsumer(keys, prosumer);
                count++;
                if (count % 1000 == 0)
                {
                    Info("Subtotal processing load count: " + count + " in a total of " + sw.Elapsed);
                }
            }

            count = 0;
            sw    = Stopwatch.StartNew();
            var saGen = SaveableEntry <Prosumer> .GetSaveableEntry(dbProfileExport, SaveableEntryTableType.HouseGeneration, Services.Logger);

            foreach (var prosumer in saGen.ReadEntireTableDBAsEnumerable())
            {
                var keys = GenerateKeysFromProsumer(prosumer);
                ProcessProsumer(keys, prosumer);
                count++;
                if (count % 1000 == 0)
                {
                    Info("Subtotal processing generation count: " + count + " in a total of " + sw.Elapsed);
                }
            }
            Debug("summed up count: " + _entriesByKey.Count);
            var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
            var saArchive = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedLoadsForAnalysis, Services.Logger);

            saArchive.MakeCleanTableForListOfFields(false);
            foreach (var value in _entriesByKey.Values)
            {
                saArchive.AddRow(value);
            }
            saArchive.SaveDictionaryToDatabase(MyLogger);
            Debug("Finished saving prosumer loads to the db");

            if (Services.RunningConfig.MakeHouseSums)
            {
                //make house sums
                var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);
                var houses   = dbHouses.Fetch <House>();
                Dictionary <string, string> houseNames = new Dictionary <string, string>();
                foreach (var house in houses)
                {
                    houseNames.Add(house.Guid, house.ComplexName);
                }

                MakeHouseSums(dbArchive, sa, houseNames);
            }
        }
        protected override void RunActualProcess([NotNull][ItemNotNull] List <ScenarioSliceParameters> allSlices, [NotNull] AnalysisRepository analysisRepo)
        {
            Info("starting to make trafostation results");
            MultiyearTrend myt = new MultiyearTrend();

            foreach (var slice in allSlices)
            {
                Info("Processing slice " + slice);
                var dbArchiving    = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileAnalysis, slice, DatabaseCode.Smartgrid);
                var saArchiveEntry = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchiving, SaveableEntryTableType.Smartgrid, Services.Logger);

                var sgis = dbArchiving.Fetch <SmartGridInformation>();
                if (sgis.Count != 1)
                {
                    throw new FlaException("invalid count");
                }
                var sgi = sgis[0];
                myt[slice].AddValue("Gesamtspeichergrösse [GWh]", sgi.TotalStorageSize, DisplayUnit.GWh);
                double avgreduction = sgi.SummedReductionFactor / sgi.NumberOfReductionFactors;
                myt[slice].AddValue("Average Reduction Factor", avgreduction, DisplayUnit.Stk);
                myt[slice].AddValue("Number of Prosumers", sgi.NumberOfProsumers, DisplayUnit.Stk);
                var aes = saArchiveEntry.LoadAllOrMatching();
                {
                    var cityload = aes.Single(x => x.Name == SummedLoadType.CityLoad.ToString());
                    myt[slice].AddValue("Energiebedarf Gesamt [GWh]", cityload.Profile.EnergySum(), DisplayUnit.GWh);
                    var cityGen1    = aes.Single(x => x.Name == SummedLoadType.CityGeneration.ToString());
                    var cityGenProf = cityGen1.Profile.MultiplyWith(-1, "Energieerzeugung");
                    if (cityGenProf.EnergySum() > 0)
                    {
                        throw new FlaException("Positive energy sum while generationg");
                    }
                    myt[slice].AddValue("Energieerzeugung Gesamt [GWh]", cityGenProf.EnergySum(), DisplayUnit.GWh);
                    var citySum = cityload.Profile.Add(cityGenProf, "sum");

                    myt[slice].AddValue("Netto-Energiebedarf [GWh]", citySum.EnergySum(), DisplayUnit.GWh);
                    myt[slice].AddValue("Maximale Last am Umspannwerk [MW]", citySum.MaxPower(), DisplayUnit.Mw);
                    myt[slice].AddValue("Minimale Last am Umspannwerk [MW]", citySum.MinPower(), DisplayUnit.Mw);
                }

                var smartcityload = aes.Single(x => x.Name == SummedLoadType.SmartCityLoad.ToString());
                myt[slice].AddValue("Energiebedarf Gesamt (smart) [GWh]", smartcityload.Profile.EnergySum(), DisplayUnit.GWh);
                var smartcityGen = aes.Single(x => x.Name == SummedLoadType.SmartCityGeneration.ToString());
                myt[slice].AddValue("Energieerzeugung Gesamt (smart) [GWh[", smartcityGen.Profile.EnergySum(), DisplayUnit.GWh);
                var smartcitySum = smartcityload.Profile.Add(smartcityGen.Profile, "sum");

                myt[slice].AddValue("Netto-Energiebedarf (smart) [GWh]", smartcitySum.EnergySum(), DisplayUnit.GWh);
                myt[slice].AddValue("Maximale Last am Umspannwerk (smart) [MW]", smartcitySum.MaxPower(), DisplayUnit.Mw);
                myt[slice].AddValue("Minimale Last am Umspannwerk (smart) [MW]", smartcitySum.MinPower(), DisplayUnit.Mw);
            }
            var filename3 = MakeAndRegisterFullFilename("SmartGridOverview.xlsx", Constants.PresentSlice);

            Info("Writing results to " + filename3);
            XlsxDumper.DumpMultiyearTrendToExcel(filename3, myt);
            SaveToPublicationDirectory(filename3, Constants.PresentSlice, "4.5");
        }
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            if (!Services.RunningConfig.MakeExcelPerTrafokreis)
            {
                return;
            }

            var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
            var saHouses  = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedHouseProfiles, Services.Logger);

            string         currentTrafokreis = "";
            ExcelWorksheet ws;
            int            columnIdx = 1;
            ExcelPackage   p         = new ExcelPackage();

            ws = p.Workbook.Worksheets.Add("sheet1");
            foreach (var house in saHouses.ReadEntireTableDBAsEnumerable("Trafokreis"))
            {
                if (currentTrafokreis != house.Trafokreis && !string.IsNullOrWhiteSpace(currentTrafokreis))
                {
                    var fn = MakeAndRegisterFullFilename(FilenameHelpers.CleanFileName(currentTrafokreis) + ".xlsx", slice);
                    // ReSharper disable once PossibleNullReferenceException
                    p.SaveAs(new FileInfo(fn));
                    SaveToArchiveDirectory(fn, RelativeDirectory.Trafokreise, slice);
                    p.Dispose();
                    p         = new ExcelPackage();
                    ws        = p.Workbook.Worksheets.Add(currentTrafokreis);
                    columnIdx = 2;
                }

                currentTrafokreis = house.Trafokreis;
                // ReSharper disable once PossibleNullReferenceException
                ws.Cells[1, columnIdx].Value = house.Name;
                int rowIdx = 2;
                for (int i = 0; i < house.Profile.Values.Count; i++)
                {
                    ws.Cells[rowIdx, columnIdx].Value = house.Profile.Values[i];
                    rowIdx++;
                }

                columnIdx++;
            }

            var fn2 = MakeAndRegisterFullFilename(FilenameHelpers.CleanFileName(currentTrafokreis) + ".xlsx", slice);

            // ReSharper disable once PossibleNullReferenceException
            p.SaveAs(new FileInfo(fn2));
            SaveToArchiveDirectory(fn2, RelativeDirectory.Trafokreise, slice);

            p.Dispose();
        }
        public PVProfileProvider([NotNull] ServiceRepository services, [NotNull] ScenarioSliceParameters slice,
                                 [NotNull] DBDto dbDto) : base(nameof(PVProfileProvider), services, slice)
        {
            _dbDto = dbDto;
            DevelopmentStatus.Add("use the correct weather year profile for the generation");
            DevelopmentStatus.Add("instead of commenting out, check if all angles & right weather file based on key. If not right, clear table and regenerate");
            Directory.SetCurrentDirectory(Services.RunningConfig.Directories.SamDirectory);
            Info("SSC Version number = " + API.Version());
            Info("SSC bBuild Information = " + API.BuildInfo());
            var dbPV = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, Slice, DatabaseCode.PvProfiles);

            _saveableEntries = SaveableEntry <Profile> .GetSaveableEntry(dbPV, SaveableEntryTableType.PVGeneration, MyLogger);

            _saveableEntries.MakeTableForListOfFieldsIfNotExists(true);
        }
        public ElectricCarProvider([NotNull] ServiceRepository services,
                                   [NotNull] ScenarioSliceParameters slice,
                                   [NotNull] DBDto dbDto,
                                   [NotNull][ItemNotNull] List <HouseCreationAndCalculationJob> housesToBeCreated,
                                   [NotNull] CachingLPGProfileLoader lpgProfileLoader) : base(nameof(ElectricCarProvider), services, slice)
        {
            _dbDto             = dbDto;
            _housesToBeCreated = housesToBeCreated;
            _lpgProfileLoader  = lpgProfileLoader;
            var profileCacheDb = services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.EvProfiles);

            _saveableEntry = SaveableEntry <Profile> .GetSaveableEntry(profileCacheDb, SaveableEntryTableType.EvProfile, services.Logger);

            _saveableEntry.MakeTableForListOfFieldsIfNotExists(true);
        }
        public HouseholdLoadProfileProvider([NotNull] ServiceRepository services,
                                            [NotNull] ScenarioSliceParameters slice,
                                            [NotNull][ItemNotNull] List <HouseCreationAndCalculationJob> districtsToBeCreated,
                                            [NotNull] SLPProvider slpProvider,
                                            [NotNull] DBDto dbDto,
                                            [NotNull] CachingLPGProfileLoader lpgloader) : base(nameof(HouseholdLoadProfileProvider), services, slice)
        {
            _housesToBeCreated = districtsToBeCreated ?? throw new ArgumentNullException(nameof(districtsToBeCreated));
            _slpProvider       = slpProvider;
            _dbDto             = dbDto;
            _lpgloader         = lpgloader;
            var profileCacheDb = services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.HouseholdProfiles);

            _saveableEntry = SaveableEntry <Profile> .GetSaveableEntry(profileCacheDb, SaveableEntryTableType.LPGProfile, services.Logger);

            _saveableEntry.MakeTableForListOfFieldsIfNotExists(true);
        }
Пример #9
0
        public void TestArchiveEntry()
        {
            Profile      p  = Profile.MakeConstantProfile(10000, "profilename", Profile.ProfileResolution.QuarterHour);
            ArchiveEntry ae = new ArchiveEntry("name", new AnalysisKey("tk", "pt", SumType.ByTrafokreisAndProvider,
                                                                       GenerationOrLoad.Generation, "housename",
                                                                       "profilesource", "household"), p, GenerationOrLoad.Load, "trafokreis");
            // ReSharper disable twice AssignNullToNotNullAttribute
            ServiceRepository services = new ServiceRepository(null, null, Logger, Config, new Random());
            var db = services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Testing, Constants.PresentSlice);
            var sa = SaveableEntry <ArchiveEntry> .GetSaveableEntry(db, SaveableEntryTableType.HouseLoad, Logger);

            sa.MakeCleanTableForListOfFields(false);
            sa.AddRow(ae);
            sa.SaveDictionaryToDatabase(Logger);
            var loadedae = sa.LoadAllOrMatching();

            loadedae[0].Should().BeEquivalentTo(ae);
        }
Пример #10
0
        private List <HouseEnergyValue> GetEnergyInProfiles([NotNull] ScenarioSliceParameters parameters)
        {
            var dbArchive =
                Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, parameters, DatabaseCode.SummedLoadForAnalysis);
            var saHouses = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedHouseProfiles, Services.Logger);

            var results = new List <HouseEnergyValue>();

            foreach (var house in saHouses.ReadEntireTableDBAsEnumerable())
            {
                if (house.Key.GenerationOrLoad == GenerationOrLoad.Load)
                {
                    double energySum = house.Profile.EnergySum();
                    results.Add(new HouseEnergyValue(house.Name, energySum));
                }
            }

            return(results);
        }
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
            var saHouses  = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedLoadsForAnalysis, Services.Logger);

            var           entries         = saHouses.LoadAllOrMatching();
            var           providerentries = entries.Where(x => x.Key.SumType == SumType.ByTrafokreisAndProvider).ToList();
            RowCollection rc = new RowCollection("energy", "energy");
            Dictionary <string, RowBuilder> rbsByTk = new Dictionary <string, RowBuilder>();

            foreach (var providerentry in providerentries)
            {
                var        tk = providerentry.Key.Trafokreis;
                RowBuilder rb;
                if (!rbsByTk.ContainsKey(tk ?? throw new InvalidOperationException()))
                {
                    rb = RowBuilder.Start("Trafokreis", providerentry.Key.Trafokreis);
                    rbsByTk.Add(tk, rb);
                }
        public void Run()
        {
            Random rnd = new Random();
            // ReSharper disable twice AssignNullToNotNullAttribute
            ServiceRepository services          = new ServiceRepository(null, null, Logger, Config, rnd);
            ProsumerComponentResultArchiver pra = new ProsumerComponentResultArchiver(Stage.Testing, Constants.PresentSlice, HouseProcessor.ProcessingMode.Collecting,
                                                                                      services);
            Prosumer prosumer = new Prosumer("houseguid", "housename", HouseComponentType.Household, "sourceguid", 1,
                                             "hausanschlussguid", "haussanschlusskey", GenerationOrLoad.Load, "trafokreis", "providername", "profileSource");
            Stopwatch sw = Stopwatch.StartNew();
            var       createdProsumers = new List <Prosumer>();

            for (int i = 0; i < 200; i++)
            {
                Profile prof = Profile.MakeRandomProfile(rnd, "name", Profile.ProfileResolution.QuarterHour);
                prosumer.Profile = prof;
                pra.Archive(prosumer);
                createdProsumers.Add(prosumer);
            }
            Info("Creating and queueing: " + sw.Elapsed.ToString());
            pra.FinishSavingEverything();
            pra.Dispose();
            sw.Stop();
            Info("total: " + sw.Elapsed);
            var loadSa = SaveableEntry <Prosumer> .GetSaveableEntry(services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Testing, Constants.PresentSlice, DatabaseCode.HouseProfiles),
                                                                    SaveableEntryTableType.HouseLoad, Logger);

            var loadedProsumers = loadSa.LoadAllOrMatching();

            Info("checking profile counts");
            foreach (var loadedProsumer in loadedProsumers)
            {
                loadedProsumer.Profile?.Values.Count.Should().Be(8760 * 4);
            }
            Info("profile counts were ok, checking prosumers");
            sw = Stopwatch.StartNew();
            for (int i = 0; i < createdProsumers.Count && i < 1; i++)
            {
                loadedProsumers[i].Should().BeEquivalentTo(createdProsumers[i]);
            }
            sw.Stop();
            Info("checking prosumers took " + sw.Elapsed.ToString());
        }
        // ReSharper disable once NotNullMemberIsNotInitialized
        public ProsumerComponentResultArchiver(Stage myStage, [NotNull] ScenarioSliceParameters parameters,
                                               HouseProcessor.ProcessingMode processingMode,
                                               [NotNull] ServiceRepository services)
        {
            _myStage  = myStage;
            _services = services;
            if (processingMode == HouseProcessor.ProcessingMode.Collecting)
            {
                _generationSa = SaveableEntry <Prosumer> .GetSaveableEntry(_services.SqlConnectionPreparer.GetDatabaseConnection(myStage, parameters, DatabaseCode.HouseProfiles),
                                                                           SaveableEntryTableType.HouseGeneration,
                                                                           _services.Logger);

                _generationSa.MakeCleanTableForListOfFields(false);
                _loadSa = SaveableEntry <Prosumer> .GetSaveableEntry(_services.SqlConnectionPreparer.GetDatabaseConnection(myStage, parameters, DatabaseCode.HouseProfiles),
                                                                     SaveableEntryTableType.HouseLoad,
                                                                     _services.Logger);

                _loadSa.MakeCleanTableForListOfFields(false);
            }
            _myThread = ThreadProvider.Get().MakeThreadAndStart(SafeRun, "Archiver");
        }
        public void TestCreationSavingAndKeyCheck()
        {
            SqlConnectionPreparer ms = new SqlConnectionPreparer(Config);
            var     db = ms.GetDatabaseConnection(Stage.Testing, Constants.PresentSlice);
            Profile p  = new Profile("myProfile", new List <double>().AsReadOnly(), EnergyOrPower.Power);
            SaveableEntry <Profile> sa = SaveableEntry <Profile> .GetSaveableEntry(db, SaveableEntryTableType.PVGeneration, Logger);

            sa.MakeCleanTableForListOfFields(true);
            sa.MakeTableForListOfFieldsIfNotExists(true);
            string myKey = p.Name;

            if (sa.CheckForName(myKey, Logger))
            {
                throw new FlaException("Key already exists in cleared db");
            }

            sa.AddRow(p);
            sa.SaveDictionaryToDatabase(Logger);
            if (!sa.CheckForName(myKey, Logger))
            {
                throw new FlaException("Saving failed. Key not in db");
            }
        }
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            var fn        = MakeAndRegisterFullFilename(FilenameHelpers.CleanFileName("SummedLoadProfileExport.xlsx"), slice);
            var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
            var saHouses  = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedLoadsForAnalysis, Services.Logger);

            var            dbRaw           = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var            bkwArr          = dbRaw.Fetch <BkwProfile>();
            var            bkwjSonProfile  = bkwArr[0];
            var            entries         = saHouses.LoadAllOrMatching();
            var            providerentries = entries.Where(x => x.Key.SumType == SumType.ByProvider).ToList();
            List <Profile> profiles        = new List <Profile>();

            foreach (var providerentry in providerentries)
            {
                providerentry.Profile.Name = (providerentry.Key.ProviderType ?? throw new FlaException()) + " " + providerentry.Key.GenerationOrLoad;
                if (providerentry.Key.GenerationOrLoad == GenerationOrLoad.Load)
                {
                    profiles.Add(providerentry.Profile);
                }
                else
                {
                    profiles.Add(providerentry.Profile.MultiplyWith(-1, providerentry.Profile.Name));
                }
            }

            profiles = MergeProfiles(profiles);
            var bkwProfile = new Profile(bkwjSonProfile.Profile);

            bkwProfile.Name = "Messung 2017 [kW]";
            profiles.Add(bkwProfile);
            XlsxDumper.DumpProfilesToExcel(fn, slice.DstYear, 15, new ProfileWorksheetContent("Profiles", "Last [kW]", bkwProfile.Name, profiles));
            SaveToArchiveDirectory(fn, RelativeDirectory.Report, slice);
            SaveToPublicationDirectory(fn, slice, "4.4");
            SaveToPublicationDirectory(fn, slice, "5");
            Info("saved " + fn);
        }
Пример #16
0
        protected override void RunActualProcess([NotNull][ItemNotNull] List <ScenarioSliceParameters> allSlices, [NotNull] AnalysisRepository analysisRepo)
        {
            List <ScenarioSliceParameters> missingSlices = new List <ScenarioSliceParameters>();

            foreach (var slice in allSlices)
            {
                Info("Checking for slice " + slice);
                var db = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice);
                var fi = new FileInfo(db.DBFilename);
                if (!fi.Exists)
                {
                    missingSlices.Add(slice);
                }
            }
            if (missingSlices.Count > 0)
            {
                var    missingSliceNames = missingSlices.Select(x => x.ToString()).ToList();
                string missingSlicesStr  = string.Join("\n", missingSliceNames);
                throw new FlaException("Missing Profile Slices: " + missingSlicesStr);
            }

            Info("starting to make trafostation results");
            MultiyearMultiVariableTrend mytProviders  = new MultiyearMultiVariableTrend();
            MultiyearMultiVariableTrend mytComponents = new MultiyearMultiVariableTrend();

            foreach (var slice in allSlices)
            {
                var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
                var saHouses  = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedLoadsForAnalysis, Services.Logger);

                var entries          = saHouses.LoadAllOrMatching();
                var providerentries1 = entries.Where(x => x.Key.SumType == SumType.ByProvider).ToList();
                providerentries1.Sort((x, y) => String.Compare(x.Key.ProviderType, y.Key.ProviderType, StringComparison.Ordinal));
                Dictionary <Tuple <string, GenerationOrLoad>, double> energyByName = new Dictionary <Tuple <string, GenerationOrLoad>, double>();
                foreach (var entry in providerentries1)
                {
                    double energy       = entry.Profile.EnergySum();
                    string providertype = (entry.Key.ProviderType ?? throw new FlaException("No provider set")) + " " + entry.GenerationOrLoad;
                    Info("Providertype: " + providertype);
                    var friendlyName = ChartHelpers.GetFriendlyProviderName(providertype);
                    var key          = new Tuple <string, GenerationOrLoad>(friendlyName, entry.GenerationOrLoad);
                    if (!energyByName.ContainsKey(key))
                    {
                        energyByName.Add(key, 0);
                    }
                    energyByName[key] += energy;
                }

                foreach (var pair in energyByName)
                {
                    if (pair.Key.Item2 == GenerationOrLoad.Load)
                    {
                        mytProviders[slice].AddValue("Stromlast [GWh]", pair.Key.Item1, pair.Value, DisplayUnit.GWh);
                    }
                    else
                    {
                        mytProviders[slice].AddValue("Erzeugung [GWh]", pair.Key.Item1, pair.Value, DisplayUnit.GWh);
                    }
                }

                var componentEntries = entries.Where(x => x.Key.SumType == SumType.ByHouseholdComponentType).ToList();
                componentEntries.Sort((x, y) => String.Compare(x.Key.HouseComponentType, y.Key.HouseComponentType, StringComparison.Ordinal));
                foreach (var entry in componentEntries)
                {
                    double energy             = entry.Profile.EnergySum();
                    string houseComponentType = entry.Key.HouseComponentType ?? throw new FlaException("No provider set");
                    Info("HouseComponentType: " + houseComponentType);
                    if (entry.Key.GenerationOrLoad == GenerationOrLoad.Load)
                    {
                        mytComponents[slice].AddValue("ComponentLoad", houseComponentType, energy, DisplayUnit.GWh);
                    }
                    else
                    {
                        mytComponents[slice].AddValue("ComponentGeneration", houseComponentType, energy, DisplayUnit.GWh);
                    }
                }
            }
            var filename3 = MakeAndRegisterFullFilename("EnergieProVerbraucher.xlsx", Constants.PresentSlice);

            Info("Writing results to " + filename3);
            XlsxDumper.DumpMultiyearMultiVariableTrendToExcel(filename3, mytProviders);
            SaveToPublicationDirectory(filename3, Constants.PresentSlice, "5");
            var filename4 = MakeAndRegisterFullFilename("ComponentProfileEnergyCharts.xlsx", Constants.PresentSlice);

            Info("Writing results to " + filename4);
            XlsxDumper.DumpMultiyearMultiVariableTrendToExcel(filename4, mytComponents);
            SaveToArchiveDirectory(filename4, RelativeDirectory.Report, Constants.PresentSlice);
        }
        public void RunTest()
        {
            CompositeResolver.RegisterAndSetAsDefault(NativeDateTimeResolver.Instance, StandardResolver.Instance);
            PrepareUnitTest();
            Config.Directories.ResultStorageDirectory = WorkingDirectory.Dir;
            Config.Directories.CalcServerLpgDirectory = WorkingDirectory.Dir;

            // ReSharper disable twice AssignNullToNotNullAttribute

            HouseCreationAndCalculationJob hcj = new HouseCreationAndCalculationJob(Scenario.Present().ToString(), "2017", "trafokreis");
            HouseData     hd  = new HouseData("houseguid", "HT01", 1000, 1000, "houseName");
            HouseholdData hhd = new HouseholdData("householdguid",
                                                  2000,
                                                  ElectricCarUse.UseElectricCar,
                                                  "householdname",
                                                  ElectricCarProvider.ChargingStationSet,
                                                  ElectricCarProvider.TransportationDevicesOneCar,
                                                  ElectricCarProvider.TravelRouteSet,
                                                  new List <TransportationDistanceModifier>(),
                                                  HouseholdDataSpecifictionType.ByPersons);

            hd.Households.Add(hhd);
            hhd.UseElectricCar          = ElectricCarUse.UseElectricCar;
            hhd.TransportationDeviceSet = ElectricCarProvider.TransportationDevicesOneCar;
            hhd.TravelRouteSet          = ElectricCarProvider.TravelRouteSet;
            hhd.ChargingStationSet      = ElectricCarProvider.ChargingStationSet;

            hhd.HouseholdDataPersonSpecification = new HouseholdDataPersonSpecification(new List <PersonData> {
                new PersonData(30, Gender.Male)
            });
            hcj.House = hd;

            List <HouseCreationAndCalculationJob> houseJobs = new List <HouseCreationAndCalculationJob>();

            houseJobs.Add(hcj);
            FileHelpers.CopyRec(Config.Directories.LPGReleaseDirectory, WorkingDirectory.Dir, Logger, true);
            var endTime = new DateTime(Constants.PresentSlice.DstYear, 1, 10);
            ProfileGenerationRo pgro = new ProfileGenerationRo();

            HouseProcessor.WriteDistrictsForLPG(houseJobs,
                                                WorkingDirectory.DirDi,
                                                Logger,
                                                Constants.PresentSlice,
                                                endTime,
                                                pgro);
            string districtsDir = WorkingDirectory.Combine("Districts");
            var    districtsDi  = new DirectoryInfo(districtsDir);
            var    files        = districtsDi.GetFiles("*.json");

            void RunOneFile(FileInfo myfi)
            {
                ProcessStartInfo psi = new ProcessStartInfo();

                psi.FileName         = WorkingDirectory.Combine("simulationengine.exe");
                psi.UseShellExecute  = true;
                psi.WorkingDirectory = WorkingDirectory.Dir;
                psi.Arguments        = "ProcessHouseJob  -j \"" + myfi.FullName + "\"";
                Info("running " + psi.FileName + " " + psi.Arguments);
                using (Process p = new Process()) {
                    p.StartInfo = psi;
                    p.Start();
                    p.WaitForExit();
                }
            }

            foreach (var housejob in files)
            {
                RunOneFile(housejob);
            }

            DBDto dbDto = new DBDto(new List <House>(), new List <Hausanschluss>(), new List <Car>(), new List <Household>(), new List <RlmProfile>());
            CachingLPGProfileLoader ca = new CachingLPGProfileLoader(Logger, dbDto);
            List <int> isns            = new List <int>();

            isns.Add(10);
            CarDistanceEntry cde = new CarDistanceEntry("houseguid",
                                                        "householdguid",
                                                        "carguid",
                                                        20,
                                                        20,
                                                        isns,
                                                        10,
                                                        "haguid",
                                                        "sourceguid",
                                                        "cdename",
                                                        CarType.Electric);
            HouseComponentRo      hcro = new HouseComponentRo("housecomponent", "componeenttype", 1000, 200, "processingstatus", "isns", "standort", 0);
            ProviderParameterDto  ppd  = new ProviderParameterDto(cde, WorkingDirectory.Dir, hcro);
            SqlConnectionPreparer scp  = new SqlConnectionPreparer(Config);
            MyDb db = scp.GetDatabaseConnection(Stage.Testing, Constants.PresentSlice);
            SaveableEntry <Profile> sa = SaveableEntry <Profile> .GetSaveableEntry(db, SaveableEntryTableType.LPGProfile, Logger);

            sa.MakeTableForListOfFieldsIfNotExists(true);
            string dstDir = Path.Combine(WorkingDirectory.Dir, hcj.Trafokreis, hcj.House.Name);

            FileHelpers.CopyRec(WorkingDirectory.Combine("Results"), dstDir, Logger, true);

            //normal electricity test and cache test
            Info("================== ");
            Info("electricity");
            Info("================== ");
            var profElec1 = ca.LoadLPGProfile(ppd,
                                              hcj.Trafokreis,
                                              "Electricity",
                                              sa,
                                              hhd.HouseholdGuid,
                                              out var profsource,
                                              hcj.House.Name,
                                              Config,
                                              true);

            Info("Source: " + profsource);
            Assert.NotNull(profElec1);
            Assert.NotNull(profsource);

            var profElecCache = ca.LoadLPGProfile(ppd,
                                                  hcj.Trafokreis,
                                                  "Electricity",
                                                  sa,
                                                  hhd.HouseholdGuid,
                                                  out var profsourceCache,
                                                  hcj.House.Name,
                                                  Config,
                                                  true);

            Info("Source 2: " + profsourceCache);
            Assert.NotNull(profsourceCache);
            Assert.NotNull(profsource);
            profElec1.Should().BeEquivalentTo(profElecCache, options => options.Excluding(ctx => ctx.SelectedMemberPath.EndsWith("BinaryProfile")));


            //Car Charging Electricity electricity test and cache test
            Info("================== ");
            Info("Car Charging Electricity electricity");
            Info("================== ");
            SaveableEntry <Profile> sa2 = SaveableEntry <Profile> .GetSaveableEntry(db, SaveableEntryTableType.EvProfile, Logger);

            sa2.MakeCleanTableForListOfFields(true);
            var prof2 = ca.LoadLPGProfile(ppd,
                                          hcj.Trafokreis,
                                          "Car Charging Electricity",
                                          sa2,
                                          hhd.HouseholdGuid,
                                          out var profsource2,
                                          hcj.House.Name,
                                          Config,
                                          true);

            Info("Source Wp 1: " + profsource2);
            Assert.NotNull(prof2);
            Assert.NotNull(profsource2);

            var prof3 = ca.LoadLPGProfile(ppd,
                                          hcj.Trafokreis,
                                          "Car Charging Electricity",
                                          sa2,
                                          hhd.HouseholdGuid,
                                          out var profsource3,
                                          hcj.House.Name,
                                          Config,
                                          true);

            Info("Source Wp 2: " + profsource3);
            Assert.NotNull(prof3);
            Assert.NotNull(profsource3);

            prof2.Should().BeEquivalentTo(prof3, options => options.Excluding(ctx =>
                                                                              ctx.SelectedMemberPath.EndsWith("BinaryProfile")));
        }
Пример #18
0
        protected override void RunActualProcess(ScenarioSliceParameters slice)
        {
            var dbProfileExport = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.HouseProfiles);
            var saLoad          = SaveableEntry <Prosumer> .GetSaveableEntry(dbProfileExport, SaveableEntryTableType.HouseLoad, Services.Logger);

            var saGeneration = SaveableEntry <Prosumer> .GetSaveableEntry(dbProfileExport, SaveableEntryTableType.HouseGeneration, Services.Logger);

            var dbArchiving = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileAnalysis, slice, DatabaseCode.Smartgrid);

            dbArchiving.RecreateTable <SmartGridInformation>();
            var saArchiveEntry = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchiving, SaveableEntryTableType.Smartgrid, Services.Logger);

            int count      = 0;
            var smartSlice = slice.CopyThisSlice();

            smartSlice.SmartGridEnabled = true;
            RowCollection prosumerCollection = new RowCollection("Prosumers", "Prosumers");
            Stopwatch     sw      = Stopwatch.StartNew();
            const string  tkfield = "Trafokreis";
            Stopwatch     swIdx   = Stopwatch.StartNew();

            saLoad.CreateIndexIfNotExists(tkfield);
            saGeneration.CreateIndexIfNotExists(tkfield);
            Info("Creating the index took " + swIdx.Elapsed);
            var trafos = saLoad.SelectSingleDistinctField <string>(tkfield);

            Info("Reading trafokreise took " + sw.Elapsed);
            Dictionary <string, Profile> trafokreiseLoad       = new Dictionary <string, Profile>();
            Dictionary <string, Profile> trafokreiseGeneration = new Dictionary <string, Profile>();
            ChangableProfile             cityload       = ChangableProfile.FromProfile(Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour));
            ChangableProfile             cityGeneration =
                ChangableProfile.FromProfile(Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour));
            ChangableProfile smartCityLoad =
                ChangableProfile.FromProfile(Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour));
            ChangableProfile smartCityGeneration =
                ChangableProfile.FromProfile(Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour));
            RowCollection        smartGridPointValues = new RowCollection("SmartGridHASummaries", "SmartGridHASummaries");
            SmartGridInformation smartGridInformation = new SmartGridInformation();

            foreach (var trafo in trafos)
            {
                //if (count > 500) {
                //continue;
                //}
                Dictionary <string, List <Prosumer> > prosumersByHa = new Dictionary <string, List <Prosumer> >();
                ChangableProfile trafoLoadSum =
                    ChangableProfile.FromProfile(Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour));
                foreach (var prosumer in saLoad.ReadSubsetOfTableDBAsEnumerable(tkfield, trafo))
                {
                    RowBuilder rb = RowBuilder.Start("Name", prosumer.Name);
                    rb.Add("Energy", prosumer.Profile?.EnergySum());
                    rb.Add("HA", prosumer.HausanschlussKey);
                    rb.Add("Trafokreis", prosumer.TrafoKreis);
                    prosumerCollection.Add(rb);
                    count++;
                    if (count % 50 == 0)
                    {
                        Info("Processing Prosumers Load: " + count + " " + sw.Elapsed);
                    }

                    if (!string.IsNullOrWhiteSpace(prosumer.HausanschlussKey))
                    {
                        if (!prosumersByHa.ContainsKey(prosumer.HausanschlussKey))
                        {
                            prosumersByHa.Add(prosumer.HausanschlussKey, new List <Prosumer>());
                        }

                        prosumersByHa[prosumer.HausanschlussKey].Add(prosumer);
                    }

                    trafoLoadSum.Add(prosumer.Profile ?? throw new FlaException("empty profile"));
                }

                AnalysisKey  keyload = new AnalysisKey(trafo, null, SumType.ByTrafokreis, GenerationOrLoad.Load, null, null, null);
                ArchiveEntry aeload  = new ArchiveEntry("Trafokreis " + trafo, keyload, trafoLoadSum.ToProfile(), GenerationOrLoad.Load, trafo);
                saArchiveEntry.AddRow(aeload);

                trafokreiseLoad.Add(trafo, trafoLoadSum.ToProfile());
                var trafoGenerationSum = Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour);
                foreach (var prosumer in saGeneration.ReadSubsetOfTableDBAsEnumerable(tkfield, trafo))
                {
                    RowBuilder rb = RowBuilder.Start("Name", prosumer.Name);
                    rb.Add("Energy", prosumer.Profile?.EnergySum());
                    rb.Add("HA", prosumer.HausanschlussKey);
                    rb.Add("Trafokreis", prosumer.TrafoKreis);
                    prosumerCollection.Add(rb);
                    if (count % 50 == 0)
                    {
                        Info("Processing Prosumers Generation: " + count + " " + sw.Elapsed);
                    }

                    if (!string.IsNullOrWhiteSpace(prosumer.HausanschlussKey))
                    {
                        if (!prosumersByHa.ContainsKey(prosumer.HausanschlussKey))
                        {
                            prosumersByHa.Add(prosumer.HausanschlussKey, new List <Prosumer>());
                        }

                        prosumersByHa[prosumer.HausanschlussKey].Add(prosumer);
                    }

                    //var powerLimited = prosumer.Profile?.LimitToPercentageOfMax(0.5)?? throw new FlaException("blub");
                    //if(powerLimited.Values.Count ==0) { throw new FlaException("huch?");}
                    trafoGenerationSum = trafoGenerationSum.Add(prosumer.Profile ?? throw new FlaException(), "Sum");
                }

                AnalysisKey  key = new AnalysisKey(trafo, null, SumType.ByTrafokreis, GenerationOrLoad.Generation, null, null, null);
                ArchiveEntry ae  = new ArchiveEntry("Trafokreis " + trafo, key, trafoGenerationSum, GenerationOrLoad.Generation, trafo);
                saArchiveEntry.AddRow(ae);
                trafokreiseGeneration.Add(trafo, trafoGenerationSum);
                cityload.Add(trafoLoadSum);
                cityGeneration.Add(trafoGenerationSum);
                //if (count > 16000) {
                ApplySmartGridstuff(prosumersByHa, trafo, smartCityLoad, smartCityGeneration, smartGridPointValues, smartGridInformation, smartSlice);
                //}
            }

            var addedSmart       = smartCityLoad.ToProfile().Add(smartCityGeneration.ToProfile(), "Netto-Last (smart)");
            var addedSerializeFn = MakeAndRegisterFullFilename("addedProfile.lz4", slice);

            dbArchiving.BeginTransaction();
            dbArchiving.Save(smartGridInformation);
            dbArchiving.CompleteTransaction();
            SaveCityProfile(cityload, saArchiveEntry, SummedLoadType.CityLoad, GenerationOrLoad.Load);
            SaveCityProfile(cityGeneration, saArchiveEntry, SummedLoadType.CityGeneration, GenerationOrLoad.Generation);
            SaveCityProfile(smartCityGeneration, saArchiveEntry, SummedLoadType.SmartCityGeneration, GenerationOrLoad.Generation);
            SaveCityProfile(smartCityLoad, saArchiveEntry, SummedLoadType.SmartCityLoad, GenerationOrLoad.Load);
            saArchiveEntry.MakeCleanTableForListOfFields(false);
            saArchiveEntry.SaveDictionaryToDatabase(MyLogger);

            FileStream fs     = new FileStream(addedSerializeFn, FileMode.Create);
            var        added  = cityload.ToProfile().Subtract(cityGeneration.ToProfile(), "Netto-Last (konventionell)");
            var        lz4Arr = LZ4MessagePackSerializer.Serialize(added);

            fs.Write(lz4Arr, 0, lz4Arr.Length);
            fs.Close();
            MakePlotlyTrafostationBoxPlots("Boxplot_Load_OhneMV.html", trafokreiseLoad, slice, true);
            MakePlotlyTrafostationBoxPlots("Boxplot_Gen_OhneMV.html", trafokreiseGeneration, slice, true);

            var fn = MakeAndRegisterFullFilename("ProsumerDump.xlsx", slice);

            XlsxDumper.DumpProfilesToExcel(fn,
                                           slice.DstYear,
                                           15,
                                           new RowWorksheetContent(prosumerCollection),
                                           new RowWorksheetContent(smartGridPointValues));
            var           fnProf             = MakeAndRegisterFullFilename("SmartGridProfiles_distributedStorage.xlsx", slice);
            RowCollection rc                 = new RowCollection("StatusInfo", "Status");
            double        avgReductionFactor = smartGridInformation.SummedReductionFactor / smartGridInformation.NumberOfReductionFactors;

            rc.Add(RowBuilder.Start("Total storage size", smartGridInformation.TotalStorageSize)
                   .Add("Number of Prosumers", smartGridInformation.NumberOfProsumers)
                   .Add("Citywide Reduction", avgReductionFactor)
                   .Add("MinimumLoadBefore", added.MinPower())
                   .Add("MinLoadSmart", addedSmart.MinPower())
                   .Add("MaxLoadBefore", added.MaxPower())
                   .Add("MaxLoadSmart", addedSmart.MaxPower())
                   );

            XlsxDumper.DumpProfilesToExcel(fnProf,
                                           slice.DstYear,
                                           15,
                                           new RowWorksheetContent(rc),
                                           new ProfileWorksheetContent("load", "Last [MW]", 240, cityload.ToProfile()),
                                           new ProfileWorksheetContent("generation", "Erzeugung [MW]", 240, cityGeneration.ToProfile()),
                                           new ProfileWorksheetContent("added", "Netto-Last [kW]", 240, added),
                                           new ProfileWorksheetContent("smartload", "Last (smart) [MW]", 240, smartCityLoad.ToProfile()),
                                           new ProfileWorksheetContent("smartgeneration", "Erzeugung (smart) [MW]", 240, smartCityGeneration.ToProfile()),
                                           new ProfileWorksheetContent("smartadded", "Netto-Last [kW]", 240, added, addedSmart));
            SaveToArchiveDirectory(fnProf, RelativeDirectory.Report, smartSlice);
            SaveToPublicationDirectory(fnProf, slice, "4.5");
            SaveToPublicationDirectory(fnProf, slice, "5");
        }
        private void ProcessOneSumType([NotNull] ScenarioSliceParameters slice, [NotNull] string fn, SumType sumType)
        {
            var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
            var saHouses  = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedLoadsForAnalysis, Services.Logger);

            int columnIdx = 2;

            using (ExcelPackage p = new ExcelPackage()) {
                var entries         = saHouses.LoadAllOrMatching();
                var providerentries = entries.Where(x => x.Key.SumType == sumType).ToList();
                var ws     = p.Workbook.Worksheets.Add("sheet1");
                int sumCol = providerentries.Count + 4;
                foreach (var entry in providerentries)
                {
                    string colLabel;
                    if (sumType == SumType.ByProvider)
                    {
                        colLabel = entry.Key.ProviderType;
                    }
                    else if (sumType == SumType.ByProfileSource)
                    {
                        colLabel = entry.Key.ProfileSource;
                    }
                    else
                    {
                        throw new FlaException("Unknown sum type");
                    }

                    ws.Cells[1, columnIdx].Value = colLabel;
                    int    rowIdx     = 2;
                    var    vals       = entry.Profile.ConvertFromEnergyToPower();
                    double multiplier = 1;
                    if (entry.GenerationOrLoad == GenerationOrLoad.Generation)
                    {
                        multiplier = -1;
                    }

                    for (int i = 0; i < vals.Values.Count; i++)
                    {
                        ws.Cells[rowIdx, columnIdx].Value = vals.Values[i] * multiplier;
                        rowIdx++;
                    }

                    ws.Cells[columnIdx, sumCol].Value = colLabel;
                    string colletter = XlsxDumper.GetExcelColumnName(columnIdx);
                    ws.Cells[columnIdx, sumCol + 1].Formula = "=Sum(" + colletter + ":" + colletter + ")/1000000/4";

                    columnIdx++;
                }

                WriteBkwColumn(ws, columnIdx, sumCol);

                //winter chart
                int startrow = 2;
                int endrow   = startrow + 24 * 4 * 14;
                int chartRow = columnIdx + 2;
                MakeSingleChart(ws, chartRow, columnIdx, providerentries, startrow, endrow, "Winter");

                //sommer chart
                startrow  = 24 * 4 * (7 + 130);
                endrow    = startrow + 24 * 4 * 7;
                chartRow += 30;

                MakeSingleChart(ws, chartRow, columnIdx, providerentries, startrow, endrow, "Sommer");

                //sommer chart
                startrow  = 10000;
                endrow    = 11000;
                chartRow += 30;

                MakeSingleChart(ws, chartRow, columnIdx, providerentries, startrow, endrow, "Frühjahr");
                p.SaveAs(new FileInfo(fn));
                SaveToArchiveDirectory(fn, RelativeDirectory.Report, slice);
            }

            Info("saved " + fn);
        }
        private void MakeHouseSums([NotNull] MyDb dbArchive, [NotNull] SaveableEntry <Prosumer> sa, [NotNull] Dictionary <string, string> houseNames)
        {
            Debug("started summing up the houses");
            ArchiveEntry currentAeLoad = new ArchiveEntry("",
                                                          new AnalysisKey(null, null, SumType.ByHouse, GenerationOrLoad.Load, "", null, null),
                                                          Profile.MakeConstantProfile(0, "", Profile.ProfileResolution.QuarterHour),
                                                          GenerationOrLoad.Load, "");
            ArchiveEntry currentAeGen = new ArchiveEntry("",
                                                         new AnalysisKey(null, null, SumType.ByHouse, GenerationOrLoad.Generation, "", null, null),
                                                         Profile.MakeConstantProfile(0, "", Profile.ProfileResolution.QuarterHour),
                                                         GenerationOrLoad.Generation, "");
            var saHouses = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedHouseProfiles, Services.Logger);

            saHouses.MakeCleanTableForListOfFields(false);
            int           count = 0;
            List <string> processedHouseNames = new List <string>();

            foreach (var prosumer in sa.ReadEntireTableDBAsEnumerable("HouseGuid"))
            {
                count++;
                string houseName = houseNames[prosumer.HouseGuid];
                if (houseName != currentAeLoad.Key.HouseName && !string.IsNullOrWhiteSpace(currentAeLoad.Key.HouseName))
                {
                    saHouses.AddRow(currentAeLoad);
                    saHouses.AddRow(currentAeGen);
                    if (saHouses.RowEntries.Count > 50)
                    {
                        saHouses.SaveDictionaryToDatabase(MyLogger);
                    }
                    if (processedHouseNames.Contains(houseName))
                    {
                        throw new FlaException("Duplicate house name");
                    }
                    processedHouseNames.Add(houseName);
                }

                if (houseName != currentAeLoad.Key.HouseName)
                {
                    currentAeLoad = new ArchiveEntry(houseName,
                                                     new AnalysisKey(null, null, SumType.ByHouse, GenerationOrLoad.Load, houseName, null, null),
                                                     Profile.MakeConstantProfile(0, houseName, Profile.ProfileResolution.QuarterHour),
                                                     GenerationOrLoad.Load, prosumer.TrafoKreis ?? throw new FlaException("No trafokreis"));
                    currentAeGen = new ArchiveEntry(houseName,
                                                    new AnalysisKey(null, null, SumType.ByHouse, GenerationOrLoad.Generation, houseName, null, null),
                                                    Profile.MakeConstantProfile(0, houseName, Profile.ProfileResolution.QuarterHour),
                                                    GenerationOrLoad.Generation, prosumer.TrafoKreis);
                }

                switch (prosumer.GenerationOrLoad)
                {
                case GenerationOrLoad.Generation:
                    currentAeGen.Profile = currentAeGen.Profile.Add(prosumer.Profile ?? throw new FlaException(), currentAeGen.Name);
                    break;

                case GenerationOrLoad.Load:
                    currentAeLoad.Profile = currentAeLoad.Profile.Add(prosumer.Profile ?? throw new FlaException(), currentAeGen.Name);
                    break;

                default: throw new FlaException("Forgotten type");
                }
                if (count % 1000 == 0)
                {
                    Info("Subtotal house Processing count: " + count);
                }
            }

            saHouses.SaveDictionaryToDatabase(MyLogger);
            Debug("Finished saving the remaining houses");
        }
Пример #21
0
        protected override void RunChartMaking(ScenarioSliceParameters slice)
        {
            var dbArchive = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
            var saArchive = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive, SaveableEntryTableType.SummedLoadsForAnalysis, Services.Logger);

            var archiveEntries = saArchive.LoadAllOrMatching();
            var dbSrcProfiles  = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var bkwRaw         = dbSrcProfiles.Fetch <BkwProfile>();

            MakePlotlyLineCharts();
            MakePlotlyTrafostationBoxPlots();

            MakeStackedTrafokreise();
            MakeEnergyProTrafokreis();

            void MakeEnergyProTrafokreis()
            {
                var            trafoKreise     = archiveEntries.Where(x => x.Key.SumType == SumType.ByTrafokreis).ToList();
                BarSeriesEntry bseEnergy       = new BarSeriesEntry("Energy [GWh]");
                BarSeriesEntry bseAveragePower = new BarSeriesEntry("Average Power [kW]");
                List <string>  labels          = new List <string>();

                foreach (var tk in trafoKreise)
                {
                    double energySum = tk.Profile.EnergySum();
                    bseEnergy.Values.Add(energySum / 1_000_000);
                    bseAveragePower.Values.Add(energySum / 8760);
                    labels.Add(tk.Key.Trafokreis);
                }

                var bses = new List <BarSeriesEntry>();

                bses.Add(bseEnergy);
                var fn = MakeAndRegisterFullFilename("EnergyPerTrafokreis.png", slice);

                Services.PlotMaker.MakeBarChart(fn, bseEnergy.Name, bses, labels);

                var bsesPower = new List <BarSeriesEntry>();

                bsesPower.Add(bseAveragePower);
                var fn2 = MakeAndRegisterFullFilename("DurchschnittsleistungPerTrafokreis.png", slice);

                Services.PlotMaker.MakeBarChart(fn2, bseAveragePower.Name, bsesPower, labels);
            }

            void MakePlotlyTrafostationBoxPlots()
            {
                var trafoKreise          = archiveEntries.Where(x => x.Key.SumType == SumType.ByTrafokreis).ToList();
                var fn2                  = MakeAndRegisterFullFilename("Boxplots.html", slice);
                List <BoxplotTrace> bpts = new List <BoxplotTrace>();
                int height               = 100;

                foreach (var entry in trafoKreise)
                {
                    BoxplotTrace bpt = new BoxplotTrace(entry.Key.Trafokreis, entry.Profile.ConvertFromEnergyToPower().Get5BoxPlotValues());
                    bpts.Add(bpt);
                    height += 25;
                }

                var layout = new PlotlyLayout {
                    Title  = "Leistungen pro Trafostation",
                    Height = height,
                    Margin = new Margin {
                        Left = 200
                    }
                };
                FlaPlotlyPlot fpp = new FlaPlotlyPlot();

                fpp.RenderToFile(bpts, layout, null, fn2);
            }

            void MakePlotlyLineCharts()
            {
                var           providers = archiveEntries.Where(x => x.Key.SumType == SumType.ByProvider).ToList();
                List <double> timestep  = new List <double>();

                for (int i = 0; i < 8760 * 24 * 4; i++)
                {
                    timestep.Add(i);
                }

                FlaPlotlyPlot fpp = new FlaPlotlyPlot();

                foreach (var entry in providers)
                {
                    var fn2 = MakeAndRegisterFullFilename("LineChartsPerProvider." + "." + entry.GenerationOrLoad + "." + entry.Key.ProviderType +
                                                          ".html",
                                                          slice);
                    LineplotTrace        bpt = new LineplotTrace(entry.Key.Trafokreis, timestep, entry.Profile.Values.ToList());
                    List <LineplotTrace> lpt = new List <LineplotTrace>();
                    lpt.Add(bpt);
                    fpp.RenderToFile(lpt, null, null, fn2);
                }
            }

            void MakeStackedTrafokreise()
            {
                var trafoKreise = archiveEntries.Where(x => x.Key.SumType == SumType.ByTrafokreis).ToList();
                List <LineSeriesEntry> lines = new List <LineSeriesEntry>();
                var runningProfile           = Profile.MakeConstantProfile(0, "base", Profile.ProfileResolution.QuarterHour);

                foreach (var entry in trafoKreise)
                {
                    runningProfile = runningProfile.Add(entry.Profile.ConvertFromEnergyToPower(), entry.Name);
                    lines.Add(runningProfile.GetLineSeriesEntry());
                }

                Profile bkw = new Profile(bkwRaw[0].Profile);

                lines.Add(bkw.GetLineSeriesEntry());
                var fn = MakeAndRegisterFullFilename("TrafokreiseStacked.png", slice);

                Services.PlotMaker.MakeLineChart(fn, "energy", lines, new List <AnnotationEntry>());
            }
        }
Пример #22
0
        protected override void RunActualProcess([NotNull][ItemNotNull] List <ScenarioSliceParameters> allSlices,
                                                 [NotNull] AnalysisRepository analysisRepo)
        {
            Info("starting to make trafostation results");
            MultiyearMultiVariableTrend myt = new MultiyearMultiVariableTrend();

            foreach (var slice in allSlices)
            {
                var dbArchive =
                    Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ProfileGeneration, slice, DatabaseCode.SummedLoadForAnalysis);
                var saHouses = SaveableEntry <ArchiveEntry> .GetSaveableEntry(dbArchive,
                                                                              SaveableEntryTableType.SummedLoadsForAnalysis,
                                                                              Services.Logger);

                var entries          = saHouses.LoadAllOrMatching();
                var providerentries1 = entries.Where(x => x.Key.SumType == SumType.ByProvider).ToList();

                double electricitySum = 0;
                foreach (var entry in providerentries1)
                {
                    if (entry.GenerationOrLoad == GenerationOrLoad.Generation)
                    {
                        continue;
                    }

                    double energy       = entry.Profile.EnergySum();
                    string providertype = (entry.Key.ProviderType ?? throw new FlaException("No provider set")) + " " + entry.GenerationOrLoad;
                    Info("Providertype: " + providertype);
                    electricitySum += energy;
                }

                HouseComponentRepository hcr = new HouseComponentRepository(analysisRepo, slice);
                var houses = analysisRepo.GetSlice(slice).Fetch <House>();
                Dictionary <string, double> loadPerEnergyType = new Dictionary <string, double>();
                Dictionary <string, double> genPerEnergyType  = new Dictionary <string, double>();
                foreach (var house in houses)
                {
                    var components = house.CollectHouseComponents(hcr);
                    foreach (var component in components)
                    {
                        if (component.HausAnschlussGuid == null)
                        {
                            continue;
                        }

                        string energyType = component.EnergyType.ToString();
                        if (component.GenerationOrLoad == GenerationOrLoad.Load)
                        {
                            if (!loadPerEnergyType.ContainsKey(energyType))
                            {
                                loadPerEnergyType.Add(energyType, 0);
                            }

                            loadPerEnergyType[energyType] += component.EffectiveEnergyDemand;
                        }
                        else if (component.GenerationOrLoad == GenerationOrLoad.Generation)
                        {
                            if (!genPerEnergyType.ContainsKey(energyType))
                            {
                                genPerEnergyType.Add(energyType, 0);
                            }

                            genPerEnergyType[energyType] += component.EffectiveEnergyDemand;
                        }
                        else
                        {
                            throw new FlaException("invalid type");
                        }
                    }
                }

                foreach (var pair in loadPerEnergyType)
                {
                    if (pair.Key == "Strom")
                    {
                        myt[slice].AddValue("Jahresenergiebedarf [GWh]",
                                            ChartHelpers.GetFriendlyEnergTypeName(pair.Key),
                                            electricitySum,
                                            DisplayUnit.GWh);
                    }
                    else
                    {
                        myt[slice].AddValue("Jahresenergiebedarf [GWh]",
                                            ChartHelpers.GetFriendlyEnergTypeName(pair.Key),
                                            pair.Value,
                                            DisplayUnit.GWh);
                    }
                }

                foreach (var pair in genPerEnergyType)
                {
                    if (pair.Key == "Strom")
                    {
                        myt[slice].AddValue("GenerationPerEnergyType",
                                            ChartHelpers.GetFriendlyEnergTypeName(pair.Key),
                                            electricitySum,
                                            DisplayUnit.GWh);
                    }
                    else
                    {
                        myt[slice].AddValue("GenerationPerEnergyType", ChartHelpers.GetFriendlyEnergTypeName(pair.Key), pair.Value, DisplayUnit.GWh);
                    }
                }
            }

            var filename3 = MakeAndRegisterFullFilename("EnergieProEnergieträger.xlsx", Constants.PresentSlice);

            Info("Writing results to " + filename3);
            XlsxDumper.DumpMultiyearMultiVariableTrendToExcel(filename3, myt);
            SaveToPublicationDirectory(filename3, Constants.PresentSlice, "5");
        }