예제 #1
0
        private void ApplySmartGridstuff([JetBrains.Annotations.NotNull] Dictionary <string, List <Prosumer> > dictionary,
                                         [JetBrains.Annotations.NotNull] string trafokreis,
                                         [JetBrains.Annotations.NotNull] ChangableProfile smartLoad,
                                         [JetBrains.Annotations.NotNull] ChangableProfile smartGeneration,
                                         [JetBrains.Annotations.NotNull] RowCollection smartGridPointValues,
                                         [JetBrains.Annotations.NotNull] SmartGridInformation summedSgi,
                                         [JetBrains.Annotations.NotNull] ScenarioSliceParameters smartSlice)
        {
            trafokreis = FilenameHelpers.CleanUmlaute(trafokreis);
            var loadFilename       = MakeAndRegisterFullFilename(trafokreis + ".load.csv", smartSlice, false);
            var generationFilename = MakeAndRegisterFullFilename(trafokreis + ".generation.csv", smartSlice, false);

            using (StreamWriter loadsw = new StreamWriter(loadFilename)) {
                using (StreamWriter generationsw = new StreamWriter(generationFilename)) {
                    foreach (var ha in dictionary)
                    {
                        var loadsum = ChangableProfile.FromProfile(Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour));
                        var gensum  = ChangableProfile.FromProfile(Profile.MakeConstantProfile(0, "Sum", Profile.ProfileResolution.QuarterHour));

                        foreach (var prosumer in ha.Value)
                        {
                            if (prosumer.GenerationOrLoad == GenerationOrLoad.Load)
                            {
                                loadsum.Add(prosumer.Profile ?? throw new FlaException());
                            }
                            else
                            {
                                gensum.Subtract(prosumer.Profile ?? throw new FlaException());
                            }
                        }

                        RowBuilder rb = RowBuilder.Start("Hausanschluss", ha.Key);
                        smartGridPointValues.Add(rb);
                        var loadProfile = loadsum.ToProfile();
                        rb.Add("Load Energy Sum [kWh]", loadProfile.EnergySum());
                        var genProfile = gensum.ToProfile();
                        rb.Add("Generation Energy Sum [kWh]", genProfile.EnergySum());
                        rb.Add("Number of Prosumers", ha.Value.Count);

                        var    sum             = loadProfile.Add(genProfile, "sum");
                        double storagesizeLoad = //Math.Abs(sum.MakeDailyAverages().Values.Max()) * 24 * 4 * 1.5;
                                                 loadProfile.EnergySum() / 1000 * 4;
                        double storageSizeGen = Math.Abs(genProfile.EnergySum()) / 1000 * 4;
                        double storagesize    = Math.Max(storageSizeGen, storagesizeLoad);
                        sum = ProfileSmoothing.FindBestPowerReductionRatio(sum, storagesize, out _, out var reductionFactor, 0.7);
                        //if (reductionFactor > 0.99) {
//                            storagesize = 0;
                        //}
                        summedSgi.NumberOfReductionFactors++;
                        summedSgi.SummedReductionFactor += reductionFactor;
                        summedSgi.TotalStorageSize      += storagesize;
                        summedSgi.NumberOfProsumers++;
                        rb.Add("Trafokreis", trafokreis);
                        rb.Add("Faktor", reductionFactor);
                        rb.Add("Storage Size", storagesize);
                        var positive = sum.GetOnlyPositive("positive");
                        smartLoad.Add(positive);
                        var negative = sum.GetOnlyNegative("negative");
                        smartGeneration.Add(negative);
                        rb.Add("Energy Sum  Size", storagesize);
                        string csvhakey = ha.Value.First().Isn + ";SM-Pros;MAXMEASUREDVALUE;" + ha.Key + ";";
                        if (positive.EnergySum() > 0)
                        {
                            loadsw.WriteLine(csvhakey + positive.GetCSVLine());
                        }

                        if (negative.EnergySum() < 0)
                        {
                            var generation = negative.MultiplyWith(-1, "generation");
                            generationsw.WriteLine(csvhakey + generation.GetCSVLine());
                        }

                        //positive.GetCSVLine()
                    }

                    loadsw.Close();
                    generationsw.Close();
                }
            }

            if (smartSlice.DstYear == 2050)
            {
                FileInfo lfn = new FileInfo(loadFilename);
                if (lfn.Length > 0)
                {
                    SaveToArchiveDirectory(loadFilename, RelativeDirectory.Load, smartSlice);
                }

                var gfn = new FileInfo(generationFilename);
                if (gfn.Length > 0)
                {
                    SaveToArchiveDirectory(generationFilename, RelativeDirectory.Generation, smartSlice);
                }
            }
        }
예제 #2
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");
        }
        protected override void RunActualProcess()
        {
            var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouses.RecreateTable <DHWHeaterEntry>();
            var houses          = dbHouses.Fetch <House>();
            var households      = dbHouses.Fetch <Household>();
            var houseHeatings   = dbHouses.Fetch <HouseHeating>();
            var hausanschlusses = dbHouses.Fetch <Hausanschluss>();

            if (houseHeatings.All(x => x.KantonDhwMethods.Count == 0))
            {
                throw new Exception("not a single  dhw heating method was set");
            }

            if (houseHeatings.All(x => x.KantonHeatingMethods.Count == 0))
            {
                throw new Exception("not a single  space heating method was set");
            }

            RowCollection rc = new RowCollection("Sheet1", "Sheet1");

            dbHouses.BeginTransaction();
            foreach (var house in houses)
            {
                var householdInHouse = households.Where(x => x.HouseGuid == house.Guid).ToList();
                var occupantsInHouse = householdInHouse.SelectMany(x => x.Occupants).ToList();
                var dhwHeaterEntry   = new DHWHeaterEntry(house.Guid, Guid.NewGuid().ToString(), "DHW@" + house.ComplexName);
                var houseHeating     = houseHeatings.Single(x => x.HouseGuid == house.Guid);
                var heatingMethod    = HeatingSystemType.Unbekannt;
                if (houseHeating.KantonDhwMethods.Count > 0)
                {
                    heatingMethod = houseHeating.GetDominantDhwHeatingMethod();
                }

                var        peopleInHouse = occupantsInHouse.Count;
                RowBuilder rb            = RowBuilder.Start("House", house.ComplexName);
                rc.Add(rb);
                rb.Add("Households", households.Count);
                rb.Add("Persons", occupantsInHouse.Count);

                rb.Add("Ebbe DHW Estimate", houseHeating.KantonWarmwasserEnergyDemand);
                switch (heatingMethod)
                {
                case HeatingSystemType.Electricity:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    // electricity at night
                    break;

                case HeatingSystemType.SolarThermal:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.Gas:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Gasheating;
                    break;

                case HeatingSystemType.Heatpump:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.Fernwärme:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.DistrictHeating;
                    break;

                case HeatingSystemType.Other:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.None:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.None;
                    break;

                case HeatingSystemType.Öl:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.OilHeating;
                    break;

                case HeatingSystemType.Holz:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.Unbekannt:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    break;

                case HeatingSystemType.GasheatingLocalnet:
                    dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Gasheating;
                    break;

                case HeatingSystemType.FernwärmeLocalnet:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                case HeatingSystemType.FeuerungsstättenOil:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                case HeatingSystemType.FeuerungsstättenGas:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                case HeatingSystemType.Kohle:
                    throw new Exception("Unknown heating method: " + heatingMethod);

                default: throw new Exception("Unknown heating method: " + heatingMethod);
                }

                rb.Add("Heating Method", dhwHeaterEntry.DhwHeatingSystemType);

                double        totalEnergy = 0;
                double        dhwEnergy   = 0;
                Hausanschluss hausanschluss;
                string        hausanschlussGuid = null;
                string        standort          = null;
                foreach (var hh in householdInHouse)
                {
                    double personEnergy = hh.EffectiveEnergyDemand / hh.Occupants.Count;
                    if (personEnergy > 1800)
                    {
                        dhwHeaterEntry.DhwHeatingSystemType = DhwHeatingSystem.Electricity;
                    }
                }

                foreach (var hh in householdInHouse)
                {
                    double hhdhwEnergy  = 0;
                    double personEnergy = hh.EffectiveEnergyDemand / hh.Occupants.Count;

                    if (dhwHeaterEntry.DhwHeatingSystemType == DhwHeatingSystem.Electricity)
                    {
                        hhdhwEnergy = CalculateDHWEnergy(personEnergy) * hh.Occupants.Count;
                    }

                    var rbhh = RowBuilder.Start("household", hh.Name);
                    rbhh.Add("Persons", hh.Occupants.Count);
                    rbhh.Add("Energy Per Person in Household [kWh]", hhdhwEnergy);
                    rbhh.Add("Household DHW heating method", dhwHeaterEntry.DhwHeatingSystemType);
                    rbhh.Add("Household Energy", hh.EffectiveEnergyDemand);
                    rc.Add(rbhh);
                    if (Math.Abs(hh.EffectiveEnergyDemand - hh.LocalnetLowVoltageYearlyTotalElectricityUse) > 0.1)
                    {
                        if (Math.Abs(hh.EffectiveEnergyDemand - (hh.LocalnetLowVoltageYearlyTotalElectricityUse + dhwEnergy)) > 0.1)
                        {
                            throw new FlaException("Energy use does not fit");
                        }
                    }

                    hh.SetEnergyReduction("DHW", hhdhwEnergy);
                    if (hh.EffectiveEnergyDemand < 0)
                    {
                        throw new FlaException("Effective Energy demand was null");
                    }

                    totalEnergy      += hh.EffectiveEnergyDemand;
                    hausanschlussGuid = hh.HausAnschlussGuid;
                    standort          = hh.Standort;
                    dbHouses.Save(hh);
                    dhwEnergy += hhdhwEnergy;
                }

                if (hausanschlussGuid != null)
                {
                    hausanschluss = hausanschlusses.Single(x => x.Guid == hausanschlussGuid);
                }
                else
                {
                    hausanschluss = house.GetHausanschlussByIsn(new List <int>(), null, hausanschlusses, MyLogger) ??
                                    throw new FlaException("no hausanschluss");
                }

                dhwHeaterEntry.Standort              = standort;
                dhwHeaterEntry.HausAnschlussGuid     = hausanschluss.Guid;
                dhwHeaterEntry.EffectiveEnergyDemand = dhwEnergy;
                if (totalEnergy < 0)
                {
                    throw new FlaException("Negative total energy");
                }

                rb.Add("Total Energy Originally [kWh]", totalEnergy);
                rb.Add("Total Energy DHW [kWh]", dhwEnergy);
                rb.Add("DHW Power [kWh]", dhwEnergy / 365 / 2);
                rb.Add("Total Energy After Dhw [kWh]", totalEnergy - dhwEnergy);
                rb.Add("Energy Per Person [kWh]", totalEnergy / peopleInHouse);

                dbHouses.Save(dhwHeaterEntry);
            }

            dbHouses.CompleteTransaction();
            var hhdump = MakeAndRegisterFullFilename("Households.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(hhdump, rc);
        }
예제 #4
0
        internal Worksheet(Workbook wb, BoundSheetRecord sheet, SortedList<long, Biff> records)
            : base(wb)
        {
            _name = sheet.Name;

            int idx = records.IndexOfKey((long)sheet.BofPos);

            _hyperlinks = new HyperLinkCollection(wb);

            for (int i = idx + 1; i < records.Count; ++i)
            {
                Biff biff = records.Values[i];
                if (biff is HyperLinkRecord)
                    _hyperlinks.Add((HyperLinkRecord)biff);
                else if (biff is EofRecord)
                    break;
            }

            BofRecord bof = (BofRecord)records.Values[idx++];

            Biff seeker = records.Values[idx++];

            while (!(seeker is IndexRecord))
                seeker = records.Values[idx++];

            IndexRecord index = (IndexRecord)seeker;

            _rows = new RowCollection(wb);
            foreach (uint indexPos in index.Rows)
            {
                long dbCellPos = indexPos;
                int dbCellIdx = records.IndexOfKey(dbCellPos);
                DbCellRecord dbCell = (DbCellRecord)records[dbCellPos];

                if (dbCell.RowOffset > 0)
                {
                    long rowPos = dbCellPos - dbCell.RowOffset;
                    int recIndex = records.IndexOfKey(rowPos);
                    Debug.Assert(recIndex != -1);

                    Biff record = records.Values[recIndex++];
                    while (record is RowRecord)
                    {
                        RowRecord row = (RowRecord)record;
                        Row currentRow = new Row(Workbook, row);
                        _rows.Add(row.RowNumber, currentRow);

                        record = records.Values[recIndex++];
                    }

                    while (recIndex <= dbCellIdx)
                    {
                        if (!(record is CellRecord))
                        {
                            record = records.Values[recIndex++];
                            continue;
                        }

                        CellRecord thecell = (CellRecord)record;
                        Row currentRow = _rows[thecell.Row];

                        if (thecell is SingleColCellRecord)
                        {
                            SingleColCellRecord cell = (SingleColCellRecord)thecell;
                            object val = cell.Value;

                            Cell newCell = new Cell(Workbook, val);
                            if (cell is RowColXfCellRecord)
                            {
                                RowColXfCellRecord xfCell = (RowColXfCellRecord)cell;

                                Style style = Workbook.Styles[xfCell.Xf];
                                Debug.Assert(style != null);
                                newCell.Style = style;
                            }
                            currentRow.Cells.Add((byte)cell.Col, newCell);
                        }
                        else
                        {
                            MultipleColCellRecord cells = (MultipleColCellRecord)thecell;
                            for (ushort i = cells.FirstCol; i <= cells.LastCol; ++i)
                            {
                                object val = cells.GetValue(i);
                                if (val != null)
                                {
                                    Cell newCell = null;
                                    if (val is RkRec)
                                    {
                                        RkRec rk = (RkRec)val;

                                        newCell = new Cell(Workbook, rk.Value);
                                        Style style = Workbook.Styles[rk.Xf];
                                        Debug.Assert(style != null);
                                        newCell.Style = style;
                                    }
                                    else
                                        newCell = new Cell(Workbook, val);

                                    currentRow.Cells.Add((byte)i, newCell);
                                }
                            }
                        }

                        record = records.Values[recIndex++];
                    }
                }
            }
        }
        public MainWindowViewModel()
        {
            this.RowColumnValues   = new MappedValueCollection();
            this.ColumnsCollection = new ObservableCollection <ColumnsViewModel>();
            this.RowCollection     = new ObservableCollection <RowViewModel>();

            // Columns

            #region Columns
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "01"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "02"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "03"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "04"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "05"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "06"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "07"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "08"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "09"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "10"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "11"
            }));
            this.ColumnsCollection.Add(new ColumnsViewModel(new Column()
            {
                Currency = "12"
            }));

            #endregion

            #region Rows
            // Rows
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "A"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "B"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "C"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "D"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "E"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "F"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "G"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "H"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "I"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "J"
            }));
            RowCollection.Add(new RowViewModel(new Row()
            {
                Name = "K"
            }));
            #endregion

            #region Cells
            // Cell Values
            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 8", IsCellHit = true
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("02")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("B")).FirstOrDefault()
            });

            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 1"
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("05")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("C")).FirstOrDefault()
            });


            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 2"
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("06")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("D")).FirstOrDefault()
            });

            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 3"
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("08")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("E")).FirstOrDefault()
            });

            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 4"
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("08")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("C")).FirstOrDefault()
            });

            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 5"
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("08")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("H")).FirstOrDefault()
            });

            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 6"
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("08")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("F")).FirstOrDefault()
            });

            RowColumnValues.Add(new MappedValue
            {
                Value = new CellData()
                {
                    ColorName = "Data 7"
                },
                ColumnBinding = ColumnsCollection.Where(x => x.Currency.Equals("08")).FirstOrDefault(),
                RowBinding    = RowCollection.Where(x => x.Name.Equals("A")).FirstOrDefault()
            });

            #endregion
        }
예제 #6
0
파일: Table.cs 프로젝트: ChrisMoreton/Test3
		internal void redimTable(int cols, int rows, bool copy,
			int cpCol, int cpRow, bool cpInserted,
			int colsAffected, int rowsAffected, bool undo)
		{
			resetCoveredCells();
			hasHeaderRows = false;

			if (undo) flowChart.UndoManager.onRedimTable(this);

			// remove arrows attached to deleted rows
			if (!cpInserted && (rowsAffected != 0) && (rowsList != null))
			{
				ArrayList arrowsToRemove = new ArrayList();
				for (int i = cpRow; i < cpRow + rowsAffected && i < this.rowsCount; ++i)
				{
					Row r = (Row)this.rowsList[i];
					for (int k = 0; k < r.IncomingArrows.Count; ++k)
						arrowsToRemove.Add(r.IncomingArrows[k]);
					for (int k = 0; k < r.OutgoingArrows.Count; ++k)
						arrowsToRemove.Add(r.OutgoingArrows[k]);
				}
				for (int j = 0; j < arrowsToRemove.Count; ++j)
					flowChart.DeleteObject((ChartObject)arrowsToRemove[j]);
				arrowsToRemove.Clear();
			}

			if (undo) flowChart.UndoManager.onRedimArrowsDeleted(this);

			CellCollection oldData = cells;
			RowCollection oldRowsDsc = rowsList;
			ColumnCollection oldColsDsc = colsList;
			int oldCols = this.columnsCount;
			int oldRows = this.rowsCount;
			bool copyC, copyR;
			copyC = copyR = copy;

			// resize the table
			this.columnsCount = cols;
			this.rowsCount = rows;

			// resize the columns array
			colsList = new ColumnCollection();
			if (this.columnsCount > 0)
			{
				for (int c = 0; c < this.columnsCount; ++c)
				{
					Column col = new Column(this);
					col.width = columnWidth;
					colsList.Add(col);
				}
			}
			else
			{
				copyC = false;
			}

			// resize the rows array
			rowsList = new RowCollection();
			if (this.rowsCount > 0)
			{
				for (int rr = 0; rr < this.rowsCount; ++rr)
				{
					Row row = new Row(this);
					row.Height = rowHeight;
					rowsList.Add(row);
				}
			}
			else
			{
				copyR = false;
			}

			// resize the cells array
			cells = new CellCollection();
			if (this.columnsCount > 0 && this.rowsCount > 0)
			{
				for (int i = 0; i < this.columnsCount * this.rowsCount; ++i)
					cells.Add(new Cell(this));
			}
			else
			{
				copy = false;
			}

			// copy data from the old cells array to the new one
			if (copy && (cells != null) && (oldData != null))
			{
				for (int rr = 0; rr < this.rowsCount; ++rr)
				{
					for (int c = 0; c < this.columnsCount; ++c)
					{
						int cr = rr;
						int cc = c;
						bool cp = true;
						if (cpInserted)
						{
							if (c >= cpCol && c < cpCol + colsAffected) cp = false;
							if (rr >= cpRow && rr < cpRow + rowsAffected) cp = false;
							if (c >= cpCol + colsAffected) cc -= colsAffected;
							if (rr >= cpRow + rowsAffected) cr -= rowsAffected;
						}
						else
						{
							if (c >= cpCol) cc += colsAffected;
							if (rr >= cpRow) cr += rowsAffected;
						}
						if (cp)
						{
							cells[rr * this.columnsCount + c] = oldData[cr * oldCols + cc];
							oldData[cr * oldCols + cc] = null;
						}
						else
							cells[rr * this.columnsCount + c] = new Cell(this);
					}
				}
			}

			if (oldData != null)
			{
				for (int oc = 0; oc < oldData.Count; ++oc)
				{
					if (oldData[oc] != null)
						oldData[oc].freeResources();
				}
			}

			// copy data from the old rows array to the new one
			if (copyR && (rowsList != null) && (oldRowsDsc != null))
			{
				for (int rr = 0; rr < this.rowsCount; ++rr)
				{
					int cr = rr;
					bool cp = true;
					if (cpInserted)
					{
						if (rr >= cpRow && rr < cpRow + rowsAffected) cp = false;
						if (rr >= cpRow + rowsAffected) cr -= rowsAffected;
					}
					else
					{
						if (rr >= cpRow) cr += rowsAffected;
					}
					if (cp)
						rowsList[rr] = oldRowsDsc[cr];
				}
			}

			// copy data from the old columns array to the new one
			if (copyC && (colsList != null) && (oldColsDsc != null))
			{
				for (int c = 0; c < this.columnsCount; ++c)
				{
					int cc = c;
					bool cp = true;
					if (cpInserted)
					{
						if (c >= cpCol && c < cpCol + colsAffected) cp = false;
						if (c >= cpCol + colsAffected) cc -= colsAffected;
					}
					else
					{
						if (c >= cpCol) cc += colsAffected;
					}
					if (cp)
						colsList[c] = oldColsDsc[cc];
				}
			}

			if (rowsAffected > 0)
			{
				updateLinksIndices();
				updateArrowsPos(cpRow);
			}

			if (copy && rowsAffected != 0)
				checkForHeaderRows();

			if (undo) flowChart.UndoManager.onCompleteRedim();
		}
예제 #7
0
        protected override void MakeVisualization([NotNull] ScenarioSliceParameters slice, bool isPresent)
        {
            var          dbHouse         = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);
            List <House> houses1         = dbHouse.Fetch <House>();
            var          heatingsystems1 = dbHouse.Fetch <HeatingSystemEntry>();
            Dictionary <string, HeatingSystemEntry> heatingsystemsByGuid = new Dictionary <string, HeatingSystemEntry>();

            foreach (var hse in heatingsystems1)
            {
                heatingsystemsByGuid.Add(hse.HouseGuid, hse);
            }

            MakeHeatingSystemAnalysis();
            EnergyIntensityHistogram();
            MakeHeatingTypeIntensityMap();
            MakeHeatingTypeMap();
            MakeHeatingSystemSankey();
            HeatingSystemCountHistogram();
            MakeHeatingSystemMapError();
            MakeHeatingSystemMap();
            MakeFernwärmeTypeMap();
            if (isPresent)
            {
                PresentOnlyVisualisations(heatingsystemsByGuid, slice, houses1, dbHouse);
            }

            void MakeHeatingSystemAnalysis()
            {
                var filename = MakeAndRegisterFullFilename("analysis.csv", slice);
                var sw       = new StreamWriter(filename);

                sw.WriteLine("Original Heating system - Anzahl");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.OriginalHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Count);
                }

                sw.WriteLine("");
                sw.WriteLine("");
                sw.WriteLine("Original Heating system - Summe");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.OriginalHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Sum(x => x.EffectiveEnergyDemand));
                }

                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("Target Anzahl");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.SynthesizedHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Count);
                }

                sw.WriteLine("");
                sw.WriteLine("");

                sw.WriteLine("Target Summe");
                foreach (HeatingSystemType hst in Enum.GetValues(typeof(HeatingSystemType)))
                {
                    var fs1 = heatingsystems1.Where(x => x.SynthesizedHeatingSystemType == hst).ToList();
                    sw.WriteLine(hst + ";" + fs1.Sum(x => x.EffectiveEnergyDemand));
                }

                sw.Close();
            }

            void EnergyIntensityHistogram()
            {
                var filename  = MakeAndRegisterFullFilename("EnergyIntensityHistogram.png", slice);
                var ages      = heatingsystems1.Select(x => x.CalculatedAverageHeatingEnergyDemandDensity).Where(y => y > 0).ToList();
                var barSeries = new List <BarSeriesEntry>();
                var h         = new Histogram(ages, 100);

                barSeries.Add(BarSeriesEntry.MakeBarSeriesEntry(h, out var colnames, "F0"));
                Services.PlotMaker.MakeBarChart(filename, "EnergyIntensityHistogram", barSeries, colnames);
                var xlsfilename    = MakeAndRegisterFullFilename("EnergyIntensity.xlsx", slice);
                var heatingSystems = heatingsystems1.Where(y => y.CalculatedAverageHeatingEnergyDemandDensityWithNulls != null).ToList();
                var rbDict         = new Dictionary <string, RowBuilder>();

                foreach (var hs in heatingSystems)
                {
                    int        bucket = (int)(Math.Floor(hs.CalculatedAverageHeatingEnergyDemandDensityWithNulls / 50 ?? 0) * 50);
                    string     key    = bucket.ToString();
                    string     et     = hs.EnergyType.ToString();
                    RowBuilder rb;
                    if (rbDict.ContainsKey(key))
                    {
                        rb = rbDict[key];
                    }
                    else
                    {
                        rb = RowBuilder.Start("Energieträger", key);
                        rbDict.Add(key, rb);
                    }

                    rb.AddToPossiblyExisting(et, 1);
                }
                RowCollection rc = new RowCollection("energy", "energy");

                foreach (var builder in rbDict)
                {
                    rc.Add(builder.Value);
                }
                RowCollection rc2 = new RowCollection("raw", "raw");

                foreach (var heatingSystemEntry in heatingsystems1)
                {
                    RowBuilder rb = RowBuilder.Start("Träger", heatingSystemEntry.EnergyType)
                                    .Add("Effektiv", heatingSystemEntry.EffectiveEnergyDemand)
                                    .Add("EBF", heatingSystemEntry.Ebf);
                    rc2.Add(rb);
                }
                XlsxDumper.WriteToXlsx(xlsfilename, rc, rc2);
            }

            void MakeHeatingSystemSankey()
            {
                var ssa1 = new SingleSankeyArrow("HouseHeatingSystems", 1500, MyStage, SequenceNumber, Name, slice, Services);

                ssa1.AddEntry(new SankeyEntry("Houses", houses1.Count, 5000, Orientation.Straight));
                var ssa2 = new SingleSankeyArrow("EnergyBySystems", 1500, MyStage, SequenceNumber, Name, slice, Services);

                ssa2.AddEntry(new SankeyEntry("Houses", heatingsystems1.Sum(x => x.EffectiveEnergyDemand) / 1000000, 5000, Orientation.Straight));
                var counts = new Dictionary <HeatingSystemType, int>();
                var energy = new Dictionary <HeatingSystemType, double>();

                foreach (var entry in heatingsystems1)
                {
                    if (!counts.ContainsKey(entry.SynthesizedHeatingSystemType))
                    {
                        counts.Add(entry.SynthesizedHeatingSystemType, 0);
                        energy.Add(entry.SynthesizedHeatingSystemType, 0);
                    }

                    counts[entry.SynthesizedHeatingSystemType]++;
                    energy[entry.SynthesizedHeatingSystemType] += entry.EffectiveEnergyDemand;
                }

                var i = 1;

                foreach (var pair in counts)
                {
                    ssa1.AddEntry(new SankeyEntry(pair.Key.ToString(), pair.Value * -1, 2000 * i, Orientation.Up));
                    i++;
                }

                i = 1;
                foreach (var pair in energy)
                {
                    ssa2.AddEntry(new SankeyEntry(pair.Key.ToString(), pair.Value * -1 / 1000000, 2000 * i, Orientation.Up));
                    i++;
                }

                Services.PlotMaker.MakeSankeyChart(ssa1);
                Services.PlotMaker.MakeSankeyChart(ssa2);
            }

            void HeatingSystemCountHistogram()
            {
                var counts = new Dictionary <HeatingSystemType, int>();

                foreach (var entry in heatingsystems1)
                {
                    if (!counts.ContainsKey(entry.SynthesizedHeatingSystemType))
                    {
                        counts.Add(entry.SynthesizedHeatingSystemType, 0);
                    }

                    counts[entry.SynthesizedHeatingSystemType]++;
                }

                var filename  = MakeAndRegisterFullFilename("HeatingSystemHistogram.png", slice);
                var names     = new List <string>();
                var barSeries = new List <BarSeriesEntry>();
                var column    = 0;

                foreach (var pair in counts)
                {
                    names.Add(pair.Value.ToString());
                    var count = pair.Value;
                    barSeries.Add(BarSeriesEntry.MakeBarSeriesEntry(pair.Key.ToString(), count, column));
                    column++;
                }

                Services.PlotMaker.MakeBarChart(filename, "HeatingSystemHistogram", barSeries, names);
            }

            void MakeHeatingSystemMapError()
            {
                RGB GetColor(House h)
                {
                    var hse = heatingsystemsByGuid[h.Guid];

                    if (hse.OriginalHeatingSystemType == HeatingSystemType.Fernwärme)
                    {
                        return(Constants.Red);
                    }

                    if (hse.OriginalHeatingSystemType == HeatingSystemType.Gas)
                    {
                        return(Constants.Orange);
                    }

                    if (hse.OriginalHeatingSystemType == HeatingSystemType.FeuerungsstättenGas)
                    {
                        return(Constants.Orange);
                    }

                    return(Constants.Black);
                }

                var mapPoints = houses1.Select(x => x.GetMapPoint(GetColor)).ToList();

                var filename      = MakeAndRegisterFullFilename("KantonHeatingSystemErrors.svg", slice);
                var legendEntries = new List <MapLegendEntry> {
                    new MapLegendEntry("Kanton Fernwärme", Constants.Red),
                    new MapLegendEntry("Kanton Gas", Constants.Orange)
                };

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
            }

            void MakeHeatingSystemMap()
            {
                var rgbs = new Dictionary <HeatingSystemType, RGB>();
                var hs   = heatingsystems1.Select(x => x.SynthesizedHeatingSystemType).Distinct().ToList();
                var idx  = 0;

                foreach (var type in hs)
                {
                    rgbs.Add(type, ColorGenerator.GetRGB(idx++));
                }

                RGB GetColor(House h)
                {
                    var hse = heatingsystemsByGuid[h.Guid];

                    return(rgbs[hse.SynthesizedHeatingSystemType]);
                }

                var mapPoints = houses1.Select(x => x.GetMapPoint(GetColor)).ToList();

                var filename      = MakeAndRegisterFullFilename("HeatingSystemMap.svg", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in rgbs)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
            }

            void MakeHeatingTypeMap()
            {
                var maxEnergy             = heatingsystems1.Max(x => x.EffectiveEnergyDemand);
                var colorsByHeatingSystem = new Dictionary <HeatingSystemType, RGB> {
                    {
                        HeatingSystemType.Gas, Constants.Orange
                    }, {
                        HeatingSystemType.Öl, Constants.Black
                    }, {
                        HeatingSystemType.Electricity, Constants.Green
                    }, {
                        HeatingSystemType.Heatpump, Constants.Green
                    }, {
                        HeatingSystemType.Fernwärme, Constants.Blue
                    }, {
                        HeatingSystemType.Other, Constants.Türkis
                    }, {
                        HeatingSystemType.None, Constants.Yellow
                    }
                };

                RGBWithSize GetColorWithSize(House h)
                {
                    var s      = heatingsystemsByGuid[h.Guid];
                    var energy = Math.Log(s.EffectiveEnergyDemand / maxEnergy * 100) * 50;

                    if (energy < 10)
                    {
                        energy = 10;
                    }

                    if (!colorsByHeatingSystem.ContainsKey(s.SynthesizedHeatingSystemType))
                    {
                        throw new Exception("undefined color for " + s.SynthesizedHeatingSystemType);
                    }

                    var rgb = colorsByHeatingSystem[s.SynthesizedHeatingSystemType];

                    return(new RGBWithSize(rgb.R, rgb.G, rgb.B, (int)energy));
                }

                RGB GetColor(House h)
                {
                    var s = heatingsystemsByGuid[h.Guid];

                    if (!colorsByHeatingSystem.ContainsKey(s.SynthesizedHeatingSystemType))
                    {
                        throw new Exception("undefined color for " + s.SynthesizedHeatingSystemType);
                    }

                    var rgb = colorsByHeatingSystem[s.SynthesizedHeatingSystemType];

                    return(new RGB(rgb.R, rgb.G, rgb.B));
                }

                var mapPoints     = houses1.Select(x => x.GetMapPointWithSize(GetColorWithSize)).ToList();
                var filename      = MakeAndRegisterFullFilename("MapHeatingTypeAndSystemPerHousehold.svg", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in colorsByHeatingSystem)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
                var filenameOsm = MakeAndRegisterFullFilename("MapHeatingTypeAndSystemPerHouseholdOsm.png", slice);

                legendEntries.Add(new MapLegendEntry("Nicht Stadtgebiet", Constants.Red));
                var mceh = houses1.Select(x => x.GetMapColorForHouse(GetColor)).ToList();

                Services.PlotMaker.MakeOsmMap("HeatingTypeMap", filenameOsm, mceh, new List <WgsPoint>(), legendEntries, new List <LineEntry>());
            }

            void MakeFernwärmeTypeMap()
            {
                RGBWithLabel GetColor(House h)
                {
                    var s = heatingsystemsByGuid[h.Guid];

                    if (s.SynthesizedHeatingSystemType == HeatingSystemType.Fernwärme)
                    {
                        return(new RGBWithLabel(Constants.Green, ""));
                    }

                    return(new RGBWithLabel(Constants.Blue, "")); //h.ComplexName
                }

                var legendEntries = new List <MapLegendEntry>();
                var filenameOsm   = MakeAndRegisterFullFilename("FernwärmeOSM.png", slice);

                legendEntries.Add(new MapLegendEntry("Nicht Stadtgebiet", Constants.Red));
                legendEntries.Add(new MapLegendEntry("Nicht Fernwärme", Constants.Blue));
                legendEntries.Add(new MapLegendEntry("Fernwärme", Constants.Green));
                var mceh = houses1.Select(x => x.GetMapColorForHouse(GetColor)).ToList();

                Services.PlotMaker.MakeOsmMap("HeatingTypeMap", filenameOsm, mceh, new List <WgsPoint>(), legendEntries, new List <LineEntry>());
            }

            void MakeHeatingTypeIntensityMap()
            {
                var colorsByHeatingSystem = new Dictionary <HeatingSystemType, RGB> {
                    {
                        HeatingSystemType.Gas, Constants.Orange
                    }, {
                        HeatingSystemType.Öl, Constants.Black
                    }, {
                        HeatingSystemType.Electricity, Constants.Green
                    }, {
                        HeatingSystemType.Heatpump, Constants.Green
                    }, {
                        HeatingSystemType.Fernwärme, Constants.Blue
                    }, {
                        HeatingSystemType.Other, Constants.Türkis
                    }, {
                        HeatingSystemType.None, Constants.Türkis
                    }
                };

                RGBWithSize GetColor(House h)
                {
                    var s      = heatingsystemsByGuid[h.Guid];
                    var energy = s.CalculatedAverageHeatingEnergyDemandDensity / 10;

                    if (energy < 10)
                    {
                        energy = 10;
                    }

                    if (!colorsByHeatingSystem.ContainsKey(s.SynthesizedHeatingSystemType))
                    {
                        throw new Exception("undefined color for " + s.SynthesizedHeatingSystemType);
                    }

                    var rgb = colorsByHeatingSystem[s.SynthesizedHeatingSystemType];

                    return(new RGBWithSize(rgb.R, rgb.G, rgb.B, (int)energy));
                }

                var mapPoints     = houses1.Select(x => x.GetMapPointWithSize(GetColor)).ToList();
                var filename      = MakeAndRegisterFullFilename("MapHeatingTypeAndIntensityPerHouse.svg", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in colorsByHeatingSystem)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeMapDrawer(filename, Name, mapPoints, legendEntries);
            }
        }
        protected override void RunActualProcess()
        {
            var dbComplex = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Complexes, Constants.PresentSlice);
            //var dbRaw = SqlConnection.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var buildingcomplexes = dbComplex.Fetch <BuildingComplex>();

            var dbComplexEnergy = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.ComplexEnergyData, Constants.PresentSlice);
            var complexdata     = dbComplexEnergy.Fetch <ComplexBuildingData>();
            var dbHouse         = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouse.RecreateTable <House>();
            dbHouse.RecreateTable <PotentialHousehold>();
            //var sonnendach = dbRaw.Fetch<SonnenDach>();
            dbHouse.BeginTransaction();

            List <string> complexesToIgnore = new List <string> {
                "EGID191357110", "EGID1306724", "EGID1305755", "Fischermätteliweg nn"
            };
            //complexesToIgnore.Add("Bernstrasse 113a");
            //complexesToIgnore.Add("Finkhubelweg 8");
            //complexesToIgnore.Add("Friedhof 4");
            var manualCoordinates = ReadManualCoordinatesList();

            foreach (var complex in buildingcomplexes)
            {
                if (complexesToIgnore.Contains(complex.ComplexName))
                {
                    continue;
                }
                //haus anlegen
                var h = new House(complex.ComplexName,
                                  complex.ComplexGuid,
                                  Guid.NewGuid().ToString()
                                  )
                {
                    ComplexID = complex.ComplexID,
                };
                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                // ReSharper disable once HeuristicUnreachableCode
                if (h.Guid == null)
                {
                    // ReSharper disable once HeuristicUnreachableCode
                    throw new Exception("House guid was null");
                }

                // ReSharper disable once ConditionIsAlwaysTrueOrFalse
                // ReSharper disable once HeuristicUnreachableCode
                if (h.ComplexGuid == null)
                {
                    // ReSharper disable once HeuristicUnreachableCode
                    throw new Exception("Complex guid was null");
                }

                //gebäudeobjektids zuordnen

                foreach (var complexGebäudeObjectID in complex.GebäudeObjectIDs)
                {
                    h.GebäudeObjectIDs.Add(complexGebäudeObjectID);
                }

                // egids zuordnen
                foreach (var eGid in complex.EGids)
                {
                    h.EGIDs.Add((int)eGid);
                }

                //geo coords zuordnen
                foreach (var coord in complex.Coords)
                {
                    h.WgsGwrCoords.Add(WgsPoint.ConvertKoordsToLonLat(coord.X, coord.Y));
                }

                var manualCoord = manualCoordinates.Where(x => x.Name == h.ComplexName).ToList();
                if (manualCoord.Any())
                {
                    foreach (var manualComplexCoordinatese in manualCoord)
                    {
                        h.WgsGwrCoords.Add(new WgsPoint(manualComplexCoordinatese.Lon, manualComplexCoordinatese.Lat));
                    }
                }

                foreach (var coord in complex.LocalnetCoords)
                {
                    h.LocalWgsPoints.Add(WgsPoint.ConvertKoordsToLonLat(coord.X, coord.Y));
                }

                //addressen zuordnen
                if (complex.Adresses.Count == 0)
                {
                    h.Adress = "Unknown";
                }
                else
                {
                    h.Adress = complex.Adresses[0];
                }

                if (complex.TrafoKreise.Count > 0)
                {
                    foreach (var erzId in complex.ErzeugerIDs)
                    {
                        h.ErzeugerIDs.Add(erzId);
                    }
                }


                //assign household data
                var thiscomplexdata = complexdata.FirstOrDefault(x => x.ComplexName == complex.ComplexName);
                if (thiscomplexdata == null)
                {
                    h.Area = 0;
                    h.Appartments.Add(new AppartmentEntry(Guid.NewGuid().ToString(), 0, false, Constants.PresentSlice.DstYear));
                }
                else
                {
                    if (thiscomplexdata.AnzahlWohnungenBern > 0)
                    {
                        double avgArea = thiscomplexdata.TotalEnergieBezugsfläche / thiscomplexdata.AnzahlWohnungenBern;
                        for (int i = 0; i < thiscomplexdata.AnzahlWohnungenBern; i++)
                        {
                            h.Appartments.Add(new AppartmentEntry(Guid.NewGuid().ToString(), avgArea, true, Constants.PresentSlice.DstYear));
                        }
                    }
                    else
                    {
                        h.Appartments.Add(new AppartmentEntry(Guid.NewGuid().ToString(), thiscomplexdata.TotalEnergieBezugsfläche, false, Constants.PresentSlice.DstYear));
                    }
                    h.Area += thiscomplexdata.TotalArea;
                    h.AverageBuildingAge = thiscomplexdata.BuildingAges.Average();
                }

                dbHouse.Save(h);
            }

            dbHouse.CompleteTransaction();
            var           allHouses = dbHouse.FetchAsRepo <House>();
            RowCollection rc        = new RowCollection("houses", "houses");

            foreach (var house in allHouses)
            {
                RowBuilder rb = RowBuilder.Start("age", house.AverageBuildingAge);
                rc.Add(rb);
            }

            var fn = MakeAndRegisterFullFilename("HouseAges.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
        public void DumpToExcel([NotNull] string dstPath, XlsResultOutputMode mode)
        {
            RowCollection rc = new RowCollection("GeneratedLoadProfiles", "GeneratedLoadProfiles");

            if (mode == XlsResultOutputMode.ByTrafoStationTree)
            {
                var trafostationen = Houses.SelectMany(x => x.HausAnschlussList).Select(y => y.Trafokreis).Distinct().ToList();
                foreach (var trafostation in trafostationen)
                {
                    rc.Add(RowBuilder.Start("Trafostation", trafostation));
                    foreach (var houseRo in Houses)
                    {
                        if (houseRo.HausAnschlussList.Any(x => x.Trafokreis == trafostation))
                        {
                            rc.Add(houseRo.ToRowBuilder());
                            foreach (HausAnschlussRo hausAnschlussRo in houseRo.HausAnschlussList)
                            {
                                if (hausAnschlussRo.Trafokreis == trafostation)
                                {
                                    rc.Add(hausAnschlussRo.ToRowBuilder(houseRo, mode));
                                    foreach (HouseComponentRo component in hausAnschlussRo.HouseComponents)
                                    {
                                        rc.Add(component.ToRowBuilder(houseRo, hausAnschlussRo, mode));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (mode == XlsResultOutputMode.ByTrafoStationHausanschlussTree)
            {
                var trafostationen = Houses.SelectMany(x => x.HausAnschlussList).Select(y => y.Trafokreis).Distinct().ToList();
                var haros          = Houses.SelectMany(x => x.HausAnschlussList).Distinct().ToList();
                haros.Sort((x, y) => String.Compare(x.ObjektID, y.ObjektID, StringComparison.Ordinal));
                foreach (var trafostation in trafostationen)
                {
                    rc.Add(RowBuilder.Start("Trafostation", trafostation));
                    var filteredHaros = haros.Where(x => x.Trafokreis == trafostation);
                    foreach (var anschlussRo in filteredHaros)
                    {
                        rc.Add(anschlussRo.ToRowBuilder(null, XlsResultOutputMode.ByTrafoStationHausanschlussTree));
                        var housesForAnschluss = Houses.Where(x => x.HausAnschlussList.Contains(anschlussRo)).ToList();
                        foreach (var houseRo in housesForAnschluss)
                        {
                            rc.Add(houseRo.ToRowBuilder());
                            foreach (HouseComponentRo component in anschlussRo.HouseComponents)
                            {
                                rc.Add(component.ToRowBuilder(houseRo, anschlussRo, mode));
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (HouseRo house in Houses)
                {
                    if (mode == XlsResultOutputMode.Tree)
                    {
                        rc.Add(house.ToRowBuilder());
                    }

                    foreach (var anschlussRo in house.HausAnschlussList)
                    {
                        if (mode == XlsResultOutputMode.Tree)
                        {
                            rc.Add(anschlussRo.ToRowBuilder(house, mode));
                        }

                        foreach (var component in anschlussRo.HouseComponents)
                        {
                            rc.Add(component.ToRowBuilder(house, anschlussRo, mode));
                        }
                    }
                }
            }

            XlsxDumper.WriteToXlsx(dstPath, rc);
        }
        protected override void RunActualProcess()
        {
            var dbHouses  = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);
            var dbComplex = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Complexes, Constants.PresentSlice);

            dbHouses.RecreateTable <HouseHeating>();
            var           buildingcomplexes       = dbComplex.Fetch <BuildingComplex>();
            var           dbRaw                   = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var           energieBedarfsDatenBern = dbRaw.Fetch <EnergiebedarfsdatenBern>();
            var           houses                  = dbHouses.Fetch <House>();
            var           potentialHeatingSystems = dbHouses.Fetch <PotentialHeatingSystemEntry>();
            RowCollection rc = new RowCollection("Heating", "Heizung");

            dbHouses.BeginTransaction();
            foreach (var h in houses)
            {
                RowBuilder rb = RowBuilder.Start("House", h.ComplexName);
                rc.Add(rb);
                var c            = buildingcomplexes.First(x => x.ComplexID == h.ComplexID);
                var hs           = potentialHeatingSystems.Where(x => x.HouseGuid == h.Guid).ToList();
                var houseHeating = new HouseHeating {
                    LocalnetFernwärmeEnergyUse = hs.Sum(x => x.YearlyFernwärmeDemand),
                    LocalnetGasEnergyUse       = hs.Sum(x => x.YearlyGasDemand),
                    HouseGuid = h.Guid,
                    LocalnetHeatingSystemEntryCount = hs.Count
                };
                rb.Add("Fernwärme", houseHeating.LocalnetFernwärmeEnergyUse).Add("LocalnetEntries", hs.Count);
                rb.Add("Standort", string.Join(",", hs.Select(x => x.Standort).Distinct()));
                rb.Add("Gas", houseHeating.LocalnetGasEnergyUse);
                rb.Add("EBF", h.EnergieBezugsFläche);
                rb.Add("Abrechnung", JsonConvert.SerializeObject(hs, Formatting.Indented));
                //collect ebbe daten
                foreach (var eGid in c.EGids)
                {
                    var ebdbs = energieBedarfsDatenBern.Where(x => x.egid == eGid).ToList();
                    if (ebdbs.Count > 1)
                    {
                        throw new Exception("too many ebdb");
                    }

                    if (ebdbs.Count == 1)
                    {
                        var eb = ebdbs[0];
                        houseHeating.KantonHeatingMethods.Add(GetHeatingMethodString((int)eb.upd_genhz));
                        houseHeating.KantonTotalEnergyDemand      += eb.calc_ehzww;
                        houseHeating.KantonHeizungEnergyDemand    += eb.calc_ehz;
                        houseHeating.KantonWarmwasserEnergyDemand += eb.calc_eww;
                        houseHeating.KantonDhwMethods.Add(GetHeatingMethodString(eb.upd_genww));
                    }
                }

                houseHeating.LocalnetCombinedEnergyDemand = houseHeating.LocalnetFernwärmeEnergyUse + houseHeating.LocalnetGasEnergyUse;
                houseHeating.LocalnetHeatingEnergyDensity = houseHeating.LocalnetAdjustedHeatingDemand / h.EnergieBezugsFläche;
                if (houseHeating.LocalnetHeatingEnergyDensity > 500)
                {
                    houseHeating.LocalnetHeatingEnergyDensity       = 250;
                    houseHeating.EnergyDensityIndustrialApplication = houseHeating.LocalnetHeatingEnergyDensity - 250;
                }

                houseHeating.KantonHeatingEnergyDensity = houseHeating.KantonTotalEnergyDemand / h.EnergieBezugsFläche;
                if (Math.Abs(houseHeating.LocalnetHeatingEnergyDensity) > 0.001)
                {
                    houseHeating.HeatingEnergyDifference = houseHeating.LocalnetHeatingEnergyDensity - houseHeating.KantonHeatingEnergyDensity;
                    if (double.IsNaN(houseHeating.HeatingEnergyDifference) || double.IsInfinity(houseHeating.HeatingEnergyDifference))
                    {
                        houseHeating.HeatingEnergyDifference = 0;
                    }
                }

                if (houseHeating.LocalnetHeatingEnergyDensity > 0)
                {
                    houseHeating.MergedHeatingEnergyDensity = houseHeating.LocalnetHeatingEnergyDensity;
                }
                else
                {
                    houseHeating.MergedHeatingEnergyDensity = houseHeating.KantonHeatingEnergyDensity;
                }

                if (houseHeating.MergedHeatingEnergyDensity < 0)
                {
                    throw new Exception("Negative heating intensity");
                }

                houseHeating.MergedHeatingDemand = 0;
                if (houseHeating.LocalnetCombinedEnergyDemand > 1)
                {
                    houseHeating.MergedHeatingDemand = houseHeating.LocalnetCombinedEnergyDemand;
                }
                else
                {
                    houseHeating.MergedHeatingDemand = houseHeating.KantonTotalEnergyDemand;
                }

                dbHouses.Save(h);
                dbHouses.Save(houseHeating);
            }

            dbHouses.CompleteTransaction();
            var fn = MakeAndRegisterFullFilename("heatingCalcsDump.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
예제 #11
0
        private void Initialize(IAttributeSet attrs = null)
        {
            MaxHeight = double.PositiveInfinity;
            Columns   = new ColumnCollection();
            Rows      = new RowCollection();

            ChildViewAdded   -= GridBase_ChildViewAdded;
            ChildViewAdded   += GridBase_ChildViewAdded;
            ChildViewRemoved -= GridBase_ChildViewRemoved;
            ChildViewRemoved += GridBase_ChildViewRemoved;

            if (attrs == null)
            {
                return;
            }
            var rowCountString = attrs.GetAttributeValue(ElementExtensions.XmlNamespace, "rows");
            var rows           = rowCountString?.Split(',');

            if (rows?.Length == 1 && int.TryParse(rows[0], out int rowCount))
            {
                this.SetRows(rowCount);
            }
            else if (rows?.Length > 0)
            {
                foreach (var row in rows)
                {
                    if (row.Contains('*'))
                    {
                        double.TryParse(row.Replace("*", string.Empty), out double height);
                        Rows.Add(new Row(height == 0 ? 1 : height, LayoutUnitType.Star));
                    }
                    else if (row.ToUpper().StartsWith("A"))
                    {
                        Rows.Add(Row.AutoSized);
                    }
                    else
                    {
                        var height = double.Parse(row);
                        Rows.Add(new Row(height, LayoutUnitType.Absolute));
                    }
                }
            }
            var columns = attrs.GetAttributeValue(ElementExtensions.XmlNamespace, "columns")?.Split(',');

            if (columns?.Length == 1 && int.TryParse(columns[0], out int columnCount))
            {
                this.SetColumns(columnCount);
            }
            else if (columns?.Length > 0)
            {
                foreach (var column in columns)
                {
                    if (column.Contains('*'))
                    {
                        double.TryParse(column.Replace("*", string.Empty), out double width);
                        Columns.Add(new Column(width == 0 ? 1 : width, LayoutUnitType.Star));
                    }
                    else if (column.ToUpper().StartsWith("A"))
                    {
                        Columns.Add(Column.AutoSized);
                    }
                    else
                    {
                        var width = double.Parse(column);
                        Columns.Add(new Column(width, LayoutUnitType.Absolute));
                    }
                }
            }
            string padding = attrs.GetAttributeValue(ElementExtensions.XmlNamespace, "padding");

            if (!string.IsNullOrEmpty(padding))
            {
                var padValues = padding.Split(',').Select(p => p.TryParseDouble()).ToList();
                switch (padValues.Count)
                {
                case 1:
                    Padding = new Thickness(padValues[0]);
                    break;

                case 2:
                    Padding = new Thickness(padValues[0], padValues[1]);
                    break;

                case 4:
                    Padding = new Thickness(padValues[0], padValues[1], padValues[2], padValues[3]);
                    break;

                default:
                    throw new FormatException("Invalid padding format: " + padding);
                }
            }

            var left   = ElementExtensions.ParseThickness(attrs, "paddingLeft", (int)Padding.Left);
            var top    = ElementExtensions.ParseThickness(attrs, "paddingTop", (int)Padding.Top);
            var right  = ElementExtensions.ParseThickness(attrs, "paddingRight", (int)Padding.Right);
            var bottom = ElementExtensions.ParseThickness(attrs, "paddingBottom", (int)Padding.Bottom);

            Padding = new Thickness(left, top, right, bottom);
        }
        private void ChangeHeatingSystemsForOneType([NotNull] ScenarioSliceParameters slice,
                                                    [NotNull][ItemNotNull] List <HeatingSystemEntry> allPotentialSystemsToChange,
                                                    HeatingSystemType heatingSystemType,
                                                    double sumToSwitch,
                                                    [NotNull] RowCollection rc,
                                                    [NotNull] Dictionary <string, House> housesByGuid)
        {
            if (Math.Abs(sumToSwitch) < 0.1)
            {
                return;
            }

            var rb = RowBuilder.Start("Type to Change", heatingSystemType.ToString());

            rc.Add(rb);
            var matchingPotentialSystemsToChange =
                allPotentialSystemsToChange.Where(x => x.SynthesizedHeatingSystemType == heatingSystemType).ToList();

            rb.Add("Planned Sum", sumToSwitch);
            rb.Add("Energy Demand before across all systems", matchingPotentialSystemsToChange.Select(x => x.OriginalHeatDemand2017).Sum());
            WeightedRandomAllocator <HeatingSystemEntry> wra = new WeightedRandomAllocator <HeatingSystemEntry>(Services.Rnd, MyLogger);

            var pickedHeatingSystems = wra.PickObjectUntilLimit(matchingPotentialSystemsToChange,
                                                                WeighingFunctionForSwitchingToHeatpump,
                                                                x => x.OriginalHeatDemand2017,
                                                                sumToSwitch,
                                                                false);
            double changedEnergy = 0;

            foreach (var pickedHeatingSystem in pickedHeatingSystems)
            {
                pickedHeatingSystem.Age = 0;
                pickedHeatingSystem.SynthesizedHeatingSystemType = HeatingSystemType.Heatpump;
                pickedHeatingSystem.ProvideProfile = true;
                changedEnergy += pickedHeatingSystem.OriginalHeatDemand2017;
                House house = housesByGuid[pickedHeatingSystem.HouseGuid];
                var   rb1   = RowBuilder.Start("House", house.ComplexName);
                rb1.Add("Changed Energy", pickedHeatingSystem.EffectiveEnergyDemand);
                rb1.Add("Heating System", heatingSystemType);
                rc.Add(rb1);
            }

            rb.Add("Changed Sum", changedEnergy);
            Info("Changed " + pickedHeatingSystems.Count + " from " + heatingSystemType + " to heatpump for a total of " + changedEnergy / 1_000_000 +
                 " gwh");
            double overSubscribed = sumToSwitch - changedEnergy;

            rb.Add("Oversubscribed", overSubscribed);
            if (slice.DstYear != 2050)
            {
                if (overSubscribed > 0)
                {
                    throw new FlaException("Problem: tried to allocate " + sumToSwitch / Constants.GWhFactor +
                                           "gwh to heat pumps, but could only switch " + changedEnergy / Constants.GWhFactor + " gwh in the year " +
                                           slice.DstYear + " and scenario " + slice.DstScenario + " from type " + heatingSystemType);
                }

                //im letzten jahr ist oversubscribe ok.
                //overSubscribed.Should().BeLessOrEqualTo(0);
            }

            var leftoveroldSystems = matchingPotentialSystemsToChange.Where(x => x.SynthesizedHeatingSystemType == heatingSystemType).ToList();

            rb.Add("Energy Demand after across all systems", leftoveroldSystems.Select(x => x.EffectiveEnergyDemand).Sum());
        }
예제 #13
0
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters parameters)
        {
            Services.SqlConnection.RecreateTable <HausAnschlussExportEntry>(Stage.ProfileExport, parameters);
            MyDb dbProfileExport = PrepareProfileExport(parameters);

            //prepare profile export

            //load previous data
            var dbProfileGeneration = LoadPreviousData(parameters,
                                                       out var houses, out var households,
                                                       out var businesses);
            //trafokreise
            var trafokreise = houses.SelectMany(x => x.Hausanschluss).Select(x => x.Trafokreis).Distinct().ToList();

            //int trafokreiscount = 1;

            dbProfileGeneration.Database.BeginTransaction();
            RowCollection rc = new RowCollection();

            foreach (var trafokreis in trafokreise)
            {
                //prepare
                if (string.IsNullOrWhiteSpace(trafokreis))
                {
                    continue;
                }
                //  if (trafokreiscount > 3) {
                //    continue;
                //}
                //  trafokreiscount++;

                //merge
                var houseExportEntries = MergeAllEntriesForTrafokreis(houses, dbProfileGeneration, trafokreis,
                                                                      households, businesses);

                //validate
                CheckHouseExportEntryIfAllHouseholdsAreCovered(households, houseExportEntries);

                //save
                var saLoad       = Prosumer.GetSaveableEntry(dbProfileExport, TableType.HouseLoad);
                var saGeneration = Prosumer.GetSaveableEntry(dbProfileExport, TableType.HouseGeneration);
                foreach (var houseExportEntry in houseExportEntries)
                {
                    var rb = RowBuilder.Start("Trafokreis", trafokreis);
                    //load
                    var ps = new Prosumer(houseExportEntry.HouseGuid,
                                          houseExportEntry.HouseName, ProsumerType.HouseLoad,
                                          houseExportEntry.HouseGuid, houseExportEntry.Isn, houseExportEntry.Trafokreis,
                                          houseExportEntry.GetSumProfile(HausAnschlussExportEntry.TypeOfSum.Load), houseExportEntry.HausanschlussGuid, houseExportEntry.HausAnschlussKeyString);
                    double profileEnergy = ps.Profile?.EnergySum() ?? throw new Exception("Invalid profile");
                    rb.Add("Housename", houseExportEntry.HouseName)
                    .Add("Load in Profile", profileEnergy).Add("Total Energy Load Planned", houseExportEntry.TotalEnergyLoad);
                    if (Math.Abs(profileEnergy - houseExportEntry.TotalEnergyLoad) > 1)
                    {
                        throw new Exception("Wrong energy");
                    }
                    saLoad.AddRow(ps);
                    //generation
                    if (houseExportEntry.TotalEnergyGeneration > 1)
                    {
                        var psGen = new Prosumer(houseExportEntry.HouseGuid, houseExportEntry.HouseName, ProsumerType.HouseLoad, houseExportEntry.HouseGuid, houseExportEntry.Isn,
                                                 houseExportEntry.Trafokreis, houseExportEntry.GetSumProfile(HausAnschlussExportEntry.TypeOfSum.Generation), houseExportEntry.HausanschlussGuid,
                                                 houseExportEntry.HausAnschlussKeyString);
                        double profileEnergyGen = psGen.Profile?.EnergySum() ?? throw new Exception("Invalid profile");
                        if (Math.Abs(profileEnergyGen - houseExportEntry.TotalEnergyGeneration) > 1)
                        {
                            throw new Exception("Wrong energy");
                        }
                        rb.Add("Generation Profile Energy", profileEnergyGen)
                        .Add("Total Energy Generation Planned", houseExportEntry.TotalEnergyGeneration);
                        saGeneration.AddRow(psGen);
                    }

                    dbProfileExport.Database.Save(houseExportEntry);
                    rc.Add(rb);
                }
                saLoad.SaveDictionaryToDatabase();
                if (saGeneration.RowEntries.Count > 0)
                {
                    saGeneration.SaveDictionaryToDatabase();
                }
            }
            var fnLoad = MakeAndRegisterFullFilename("MergedStuff.xlsx", Name, "", parameters);

            XlsxDumper.WriteToXlsx(rc, fnLoad, "ProsumersMerged");
        }
        public static RowCollection MakeRowCollectionForSingleSheet <T>(int year, int timeStepWidthInMinutes, [NotNull] IValueProvider wsc)
        {
            //dump to csv
            RowCollection rc          = new RowCollection(wsc.SheetName, wsc.YAxisName);
            DateTime      dt          = new DateTime(year, 1, 1);
            int           columnCount = wsc.GetColumnCount();
            List <ReadOnlyCollection <T> > columns = new List <ReadOnlyCollection <T> >();

            for (int i = 0; i < columnCount; i++)
            {
                columns.Add(wsc.GetValues <T>(i));
            }

            int maxProfileCount = columns.Max(x => x.Count);
            var names           = wsc.GetColumnNames();

            if (names.Count != names.Distinct().Count())
            {
                string s = "";
                foreach (var name in names)
                {
                    var count = names.Count(x => x == name);
                    if (count > 1)
                    {
                        s += name + ": " + count + "\n";
                    }
                }

                throw new FlaException("Profile names need to be unique:" + s);
            }

            if (wsc.GetColumnCount() == 0)
            {
                throw new FlaException("no profiles");
            }

            foreach (var profile in columns)
            {
                if (profile.Count == 0)
                {
                    throw new FlaException("no values in profile");
                }
            }

            for (int row = 0; row < maxProfileCount; row++)
            {
                RowBuilder rb = RowBuilder.Start("Idx", row);
                rc.Add(rb);
                rb.Add("Time", dt.ToString("ddd, dd.MM.yyyy") + "\n" + dt.ToString("HH:mm"));
                if (row > 5)
                {
                    rb.Add("Time (date only)", dt.ToString("dd.MM.yyyy"));
                }
                else
                {
                    rb.Add("Time (date only)", null);
                }

                dt = dt.AddMinutes(timeStepWidthInMinutes);
                for (int col = 0; col < columnCount; col++)
                {
                    if (columns[col].Count > row)
                    {
                        rb.Add(names[col], columns[col][row]);
                    }
                }
            }

            return(rc);
        }
예제 #15
0
        protected override void RunChartMaking()
        {
            var slice = Constants.PresentSlice;
            //var dbHouse = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);
            var ap               = new AnalysisRepository(Services.RunningConfig);
            var dbHouse          = ap.GetSlice(Constants.PresentSlice);
            var houses           = dbHouse.Fetch <House>();
            var houseTypeEntries = dbHouse.Fetch <HouseTypeEntry>();

            MakeHouseTypeEntries();
            MakeHouseTypeMap();
            MakeEnergyUseXls();
            void MakeEnergyUseXls()
            {
                var        households   = dbHouse.Fetch <Household>();
                var        businesses   = dbHouse.Fetch <BusinessEntry>();
                var        infra        = dbHouse.Fetch <BuildingInfrastructure>();
                var        light        = dbHouse.Fetch <StreetLightingEntry>();
                var        dhw          = dbHouse.Fetch <DHWHeaterEntry>();
                var        rc           = new RowCollection("energy", "energy");
                RowBuilder rb           = RowBuilder.Start("Haushalte", households.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                var        bigcustomers = businesses.Where(x => x.EffectiveEnergyDemand > 20000);
                var        small        = businesses.Where(x => x.EffectiveEnergyDemand <= 20000);

                rb.Add("Geschäftskunden > 20 MWh", bigcustomers.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                rb.Add("Geschäftskunden < 20 MWh", small.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                rb.Add("Gebäudeinfrastruktur", infra.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);
                rb.Add("Strassenbeleuchtung", light.Sum(x => x.YearlyElectricityUse) / Constants.GWhFactor);
                rb.Add("Elektroboiler", dhw.Sum(x => x.EffectiveEnergyDemand) / Constants.GWhFactor);

                rc.Add(rb);
                var fn = MakeAndRegisterFullFilename("EnergyTreeMap.xlsx", Constants.PresentSlice);

                XlsxDumper.WriteToXlsx(fn, rc);
                SaveToPublicationDirectory(fn, Constants.PresentSlice, "4.2");
            }

            void MakeHouseTypeEntries()
            {
                var ssa = new SingleSankeyArrow("HouseTypeEntries", 1500, MyStage,
                                                SequenceNumber, Name, slice, Services);

                ssa.AddEntry(new SankeyEntry("Houses", houses.Count, 5000, Orientation.Straight));
                var countsPerType = new Dictionary <HouseType, int>();

                foreach (var entry in houseTypeEntries)
                {
                    if (!countsPerType.ContainsKey(entry.HouseType))
                    {
                        countsPerType.Add(entry.HouseType, 0);
                    }

                    countsPerType[entry.HouseType]++;
                }

                foreach (var pair in countsPerType)
                {
                    ssa.AddEntry(new SankeyEntry(pair.Key.ToString(), pair.Value * -1, 2000, Orientation.Up));
                }

                Services.PlotMaker.MakeSankeyChart(ssa);
            }

            void MakeHouseTypeMap()
            {
                var rgbs = new Dictionary <HouseType, RGB>();
                var hs   = houseTypeEntries.Select(x => x.HouseType).Distinct().ToList();
                var idx  = 0;

                foreach (var type in hs)
                {
                    var rgb = ColorGenerator.GetRGB(idx++);
                    if (rgb.R == 255 && rgb.B == 255 && rgb.G == 255)
                    {
                        rgb = new RGB(200, 200, 200);
                    }

                    if (rgb.R == 255 && rgb.B == 0 && rgb.G == 0)
                    {
                        rgb = Constants.Orange;
                    }

                    rgbs.Add(type, rgb);
                }

                RGB GetColor(House h)
                {
                    var hse = houseTypeEntries.Single(x => x.HouseGuid == h.Guid);

                    return(rgbs[hse.HouseType]);
                }

                var mapPoints = houses.Select(x => x.GetMapColorForHouse(GetColor)).ToList();

                var filename      = MakeAndRegisterFullFilename("HouseTypeMap.png", slice);
                var legendEntries = new List <MapLegendEntry>();

                foreach (var pair in rgbs)
                {
                    legendEntries.Add(new MapLegendEntry(pair.Key.ToString(), pair.Value));
                }

                Services.PlotMaker.MakeOsmMap(Name, filename, mapPoints, new List <WgsPoint>(), legendEntries, new List <LineEntry>());
            }
        }
        private static List <RowCollection> MakeRowCollectionForFromMultiTrendVariable([NotNull] MultiyearMultiVariableTrend trend,
                                                                                       [NotNull] string variable,
                                                                                       out int categoryCount,
                                                                                       out int yearCount)
        {
            var scenarios  = trend.Dict.Keys.Select(x => x.DstScenario).Distinct().ToList();
            var categories = trend.Dict.Values.SelectMany(x => x.Values).Where(x => x.VariableName == variable).SelectMany(y => y.Values.Keys)
                             .Distinct().ToList();

            categoryCount = categories.Count;
            var years = trend.Dict.Keys.Select(x => x.DstYear).Distinct().OrderBy(x => x).ToList();

            yearCount = years.Count;
            List <RowCollection> rows = new List <RowCollection>();
            var presentSliceValues    = trend.Dict[Constants.PresentSlice];

            foreach (var scenario in scenarios)
            {
                if (scenario == Scenario.Present())
                {
                    continue;
                }

                RowCollection rc = new RowCollection(scenario.ShortName, scenario.Name);
                rows.Add(rc);
                foreach (var year in years)
                {
                    var rb = RowBuilder.Start("Year", year);
                    if (year == 2017)
                    {
                        foreach (var category in categories)
                        {
                            object o = null;
                            if (presentSliceValues != null)
                            {
                                o = presentSliceValues.GetSliceValueByName(variable, category);
                            }

                            rb.Add(category, o);
                        }
                    }
                    else
                    {
                        foreach (var category in categories)
                        {
                            var    slicevalues = trend.Dict.Values.FirstOrDefault(x => x.Slice.DstYear == year && x.Slice.DstScenario == scenario);
                            object o           = null;
                            if (slicevalues != null)
                            {
                                o = slicevalues.GetSliceValueByName(variable, category);
                            }

                            rb.Add(category, o);
                        }
                    }

                    rc.Add(rb);
                }
            }

            return(rows);
        }
예제 #17
0
        protected override void RunActualProcess([NotNull] ScenarioSliceParameters slice)
        {
            var dbSrcHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice.PreviousSliceNotNull);
            var dbDstHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);

            dbDstHouses.RecreateTable <PvSystemEntry>();
            dbDstHouses.RecreateTable <PVPotential>();
            var srcHouses          = dbSrcHouses.Fetch <House>();
            var srcPVPotentials    = dbSrcHouses.Fetch <PVPotential>();
            var srcPvSystemEntries = dbSrcHouses.Fetch <PvSystemEntry>();
            var hausanschlusses    = dbDstHouses.Fetch <Hausanschluss>();

            dbDstHouses.BeginTransaction();
            foreach (var potential in srcPVPotentials)
            {
                potential.ID = 0;
                dbDstHouses.Save(potential);
            }

            //copy pv systems from previous slice
            HashSet <string>            houseGuidsForSystemsWithPV = new HashSet <string>();
            Dictionary <string, double> pvPotentialByHouseGuid     = new Dictionary <string, double>();

            foreach (var pvpot in srcPVPotentials)
            {
                if (!houseGuidsForSystemsWithPV.Contains(pvpot.HouseGuid))
                {
                    houseGuidsForSystemsWithPV.Add(pvpot.HouseGuid);
                    pvPotentialByHouseGuid.Add(pvpot.HouseGuid, 0);
                }

                pvPotentialByHouseGuid[pvpot.HouseGuid] += pvpot.SonnendachStromErtrag;
            }

            var potentialhousesForPvSystems = srcHouses.Where(x => houseGuidsForSystemsWithPV.Contains(x.Guid)).ToList();

            foreach (var entry in srcPvSystemEntries)
            {
                var toremove = potentialhousesForPvSystems.FirstOrDefault(x => x.Guid == entry.HouseGuid);
                if (toremove != null)
                {
                    potentialhousesForPvSystems.Remove(toremove);
                }

                if (entry.PVAreas.Count == 0)
                {
                    throw new FlaException("No PV System areas defined.");
                }

                entry.ID = 0;
                dbDstHouses.Save(entry);
            }

            var  pvToInstallInkWh   = slice.PvPowerToInstallInGwh * 1_000_000;
            bool continueAllocation = true;
            int  pvSystemCount      = 0;

            while (pvToInstallInkWh > 0 && continueAllocation)
            {
                //make ranges
                var rangeEntries = SetRanges(potentialhousesForPvSystems, pvPotentialByHouseGuid);
                if (rangeEntries.Count == 0)
                {
                    continueAllocation = false;
                    continue;
                }

                //randomly pick
                var max        = rangeEntries.Max(x => x.EndRange);
                var pick       = Services.Rnd.NextDouble() * max;
                var rangeEntry = rangeEntries.Single(x => pick >= x.StartRange && pick <= x.EndRange);
                //remove house
                potentialhousesForPvSystems.Remove(rangeEntry.House);
                //save pvsystementry
                var pvPotenial = pvPotentialByHouseGuid[rangeEntry.House.Guid];
                pvSystemCount++;
                string erzeugerid    = "PV-" + slice.DstYear + "-" + pvSystemCount;
                var    hausanschlsus = rangeEntry.House.GetHausanschlussByIsn(new List <int>(), erzeugerid, hausanschlusses, MyLogger) ??
                                       throw new FlaException("no hausanschluss");
                if (hausanschlsus.ObjectID.ToLower().Contains("leuchte"))
                {
                    throw new FlaException("pv an leuchte in " + slice.DstYear + " " + hausanschlsus.ObjectID);
                }

                var pvSystemEntry = new PvSystemEntry(rangeEntry.House.Guid,
                                                      Guid.NewGuid().ToString(),
                                                      hausanschlsus.Guid,
                                                      rangeEntry.House.ComplexName,
                                                      erzeugerid,
                                                      slice.DstYear)
                {
                    EffectiveEnergyDemand = pvPotenial
                };
                var areas = srcPVPotentials.Where(x => x.HouseGuid == rangeEntry.House.Guid).ToList();
                foreach (var area in areas)
                {
                    pvSystemEntry.PVAreas.Add(new PVSystemArea(area.Ausrichtung, area.Neigung, area.SonnendachStromErtrag));
                }

                if (pvSystemEntry.PVAreas.Count == 0)
                {
                    throw new FlaException("No PV System areas defined.");
                }

                pvToInstallInkWh       -= pvSystemEntry.EffectiveEnergyDemand;
                pvSystemEntry.BuildYear = slice.DstYear;
                dbDstHouses.Save(pvSystemEntry);
                //deduct from pvtoinstall
            }

            dbDstHouses.CompleteTransaction();
            var           newPVs = dbDstHouses.FetchAsRepo <PvSystemEntry>();
            RowCollection rc     = new RowCollection("pv", "pv");

            foreach (var pv in newPVs)
            {
                foreach (var area in pv.PVAreas)
                {
                    RowBuilder rb = RowBuilder.Start("HA", pv.HausAnschlussGuid);
                    rc.Add(rb);
                    rb.Add("Azimut", area.Azimut);
                    rb.Add("Tilt", area.Tilt);
                    rb.Add("Energy", area.Energy);
                }
            }

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

            XlsxDumper.WriteToXlsx(fn, rc);
        }
예제 #18
0
        //Override the OnPrintPage to provide the printing logic for the document
        protected override void OnPrintPage(PrintPageEventArgs ev)
        {
            float lpp         = 0;
            float yPos        = 0;
            int   count       = 0;
            float leftMargin  = ev.MarginBounds.Left;
            float rightMargin = ev.MarginBounds.Right;
            float topMargin   = ev.MarginBounds.Top;

            //ev.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

            if (rc == null)
            {
                Document.ParseAll();
                Document.ParseAll(true);


                rc = new RowCollection();
                foreach (Row r in Document)
                {
                    bool  hasbreak = false;
                    float x        = leftMargin;
                    Row   newRow   = new Row();
                    rc.Add(newRow);
                    foreach (Word w in r)
                    {
                        Font f = fontNormal;
                        if (w.Style != null)
                        {
                            FontStyle fs = 0;

                            if (w.Style.Bold)
                            {
                                fs |= FontStyle.Bold;
                            }

                            if (w.Style.Italic)
                            {
                                fs |= FontStyle.Italic;
                            }

                            if (w.Style.Underline)
                            {
                                fs |= FontStyle.Underline;
                            }

                            f = new Font("Courier new", 8, fs);
                        }
                        SizeF sf = ev.Graphics.MeasureString(w.Text, f);
                        if (x + sf.Width > rightMargin)
                        {
                            char chr = (char)0xbf;
                            Word br  = new Word();
                            br.Text    = chr + "";
                            br.InfoTip = "break char";
                            newRow.Add(br);
                            hasbreak = true;



                            newRow = new Row();
                            rc.Add(newRow);
                            x = leftMargin;
                        }
                        x += sf.Width;
                        newRow.Add(w);
                    }
                    if (hasbreak)
                    {
                        rc.Add(new Row());
                    }
                }
            }
            //------------------------------------------------------

            base.OnPrintPage(ev);



            lpp = ev.MarginBounds.Height / fontNormal.GetHeight(ev.Graphics);



            while (count < lpp && (RowIndex < rc.Count))
            {
                float x = leftMargin;
                yPos = topMargin + (count * fontNormal.GetHeight(ev.Graphics));

                Row r = rc[RowIndex];

                foreach (Word w in r)
                {
                    if (w.InfoTip != null && w.InfoTip == "break char")
                    {
                        ev.Graphics.DrawString(w.Text, fontBreak, Brushes.Black, x, yPos, new StringFormat());
                    }
                    else
                    {
                        SizeF sf = ev.Graphics.MeasureString(w.Text, fontNormal);

                        if (w.Text != null && (".,:;".IndexOf(w.Text) >= 0))
                        {
                            sf.Width = 6;
                            x       -= 4;
                        }
                        if (w.Text == "\t")
                        {
                            sf.Width = ev.Graphics.MeasureString("...", fontNormal).Width;
                        }


                        Color c = Color.Black;
                        Font  f = fontNormal;
                        if (w.Style != null)
                        {
                            c = w.Style.ForeColor;
                            FontStyle fs = 0;

                            if (w.Style.Bold)
                            {
                                fs |= FontStyle.Bold;
                            }

                            if (w.Style.Italic)
                            {
                                fs |= FontStyle.Italic;
                            }

                            if (w.Style.Underline)
                            {
                                fs |= FontStyle.Underline;
                            }

                            f = new Font("Courier new", 8, fs);

                            if (!w.Style.Transparent)
                            {
                                Color bg = w.Style.BackColor;
                                ev.Graphics.FillRectangle(new SolidBrush(bg), x, yPos, sf.Width, fontNormal.GetHeight(ev.Graphics));
                            }
                        }

                        c = Color.FromArgb(c.R, c.G, c.B);



                        ev.Graphics.DrawString(w.Text, f, new SolidBrush(c), x, yPos, new StringFormat());
                        x += sf.Width;
                    }
                }

                count++;
                RowIndex++;
            }

            //If we have more lines then print another page
            if (RowIndex < rc.Count)
            {
                ev.HasMorePages = true;
            }
            else
            {
                ev.HasMorePages = false;
            }
        }
예제 #19
0
 private static RowCollection CloneRows(GridPanel panel)
 {
     RowCollection rows = new RowCollection(panel, (int) panel.Rows.DefaultSize);
     using (rows.DeferNotifications())
     {
         foreach (Row row in panel.Rows)
         {
             rows.Add(row);
             row.List = panel.Rows;
         }
     }
     rows.MinSize = panel.Rows.MinSize;
     return rows;
 }
        protected override void RunActualProcess(ScenarioSliceParameters slice)
        {
            if (!slice.Equals(Constants.PresentSlice))
            {
                return;
            }

            var dbHouses = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, slice);

            Info("using house db in  " + dbHouses.ConnectionString);
            var houses = dbHouses.Fetch <House>();
            HouseComponentRepository hcr = new HouseComponentRepository(dbHouses);
            var hausanschlusses          = dbHouses.Fetch <Hausanschluss>();
            var dbRaw                   = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var suppIsn                 = dbRaw.Fetch <HausanschlussImportSupplement>();
            var standortDict            = new Dictionary <string, string>();
            var rlms                    = dbRaw.Fetch <RlmProfile>();
            List <AssignmentEntry> ases = new List <AssignmentEntry>();

            foreach (var supplement in suppIsn)
            {
                if (!string.IsNullOrWhiteSpace(supplement.TargetStandort))
                {
                    if (standortDict.ContainsKey(supplement.TargetStandort))
                    {
                        throw new FlaException("Already contains standort " + supplement.TargetStandort);
                    }

                    standortDict.Add(supplement.TargetStandort, supplement.HaObjectid);
                }

                AssignmentEntry ase = ases.FirstOrDefault(x => x.ObjectId == supplement.HaObjectid);
                if (ase == null)
                {
                    ase = new AssignmentEntry(supplement.HaObjectid);
                    ases.Add(ase);
                }

                ase.Targets.Add(supplement.TargetStandort);
                ase.TargetCount++;
            }

            List <string>        checkedStandorte       = new List <string>();
            var                  lgzs                   = ReadZuordnungLastgänge();
            var                  successfullAssignments = new List <LastgangZuordnung>();
            RowCollection        rc = new RowCollection("sheet", "Sheet1");
            List <PvSystemEntry> assignedPVsystems = new List <PvSystemEntry>();
            List <PvSystemEntry> otherPVsystems    = new List <PvSystemEntry>();

            foreach (House house in houses)
            {
                var houseComponents = house.CollectHouseComponents(hcr);
                foreach (var component in houseComponents)
                {
                    if (component.Standort == null)
                    {
                        continue;
                    }

                    if (standortDict.ContainsKey(component.Standort))
                    {
                        Hausanschluss ha             = hausanschlusses.Single(x => x.Guid == component.HausAnschlussGuid);
                        string        targetObjectID = standortDict[component.Standort];
                        if (ha.ObjectID != targetObjectID)
                        {
                            throw new FlaException("Incorrect hausanschluss for " + component.Name + ": was supposed to be " + targetObjectID +
                                                   " but instead was " + ha.ObjectID + " (standort: " + component.Standort);
                        }

                        var ase = ases.Single(x => x.ObjectId == ha.ObjectID);
                        ase.AssignedCount++;
                        ase.Assigned.Add(component.Standort);
                        checkedStandorte.Add(component.Standort);
                    }

                    if (component.HouseComponentType == HouseComponentType.BusinessWithLastgangHighVoltage)
                    {
                        var businessEntry = (BusinessEntry)component;
                        var rlmfn         = businessEntry.RlmProfileName ?? throw new FlaException("No file");
                        var ha            = hausanschlusses.First(x => x.Guid == businessEntry.HausAnschlussGuid);
                        var rlm           = rlms.Single(x => x.Name == businessEntry.RlmProfileName);
                        var energysum     = new Profile(rlm.Profile).EnergySum();
                        LogAssignment(lgzs,
                                      rlmfn,
                                      ha,
                                      successfullAssignments,
                                      rc,
                                      businessEntry.Name,
                                      businessEntry.Standort,
                                      suppIsn,
                                      "HS",
                                      businessEntry.EffectiveEnergyDemand,
                                      energysum,
                                      house.ComplexName,
                                      businessEntry.FinalIsn);
                    }

                    if (component.HouseComponentType == HouseComponentType.BusinessWithLastgangLowVoltage)
                    {
                        var businessEntry = (BusinessEntry)component;
                        var rlmfn         = businessEntry.RlmProfileName ?? throw new FlaException("no file?");
                        var ha            = hausanschlusses.First(x => x.Guid == businessEntry.HausAnschlussGuid);
                        var rlm           = rlms.Single(x => x.Name == businessEntry.RlmProfileName);
                        var energysum     = new Profile(rlm.Profile).EnergySum();
                        LogAssignment(lgzs,
                                      rlmfn,
                                      ha,
                                      successfullAssignments,
                                      rc,
                                      businessEntry.Name,
                                      businessEntry.Standort,
                                      suppIsn,
                                      "NS",
                                      businessEntry.EffectiveEnergyDemand,
                                      energysum,
                                      house.ComplexName,
                                      businessEntry.FinalIsn);
                    }

                    if (component.HouseComponentType == HouseComponentType.Kwkw)
                    {
                        var kwkw      = (KleinWasserkraft)component;
                        var rlmfn     = kwkw.RlmProfileName;
                        var ha        = hausanschlusses.First(x => x.Guid == kwkw.HausAnschlussGuid);
                        var rlm       = rlms.Single(x => x.Name == kwkw.RlmProfileName);
                        var energysum = new Profile(rlm.Profile).EnergySum();
                        LogAssignment(lgzs,
                                      rlmfn,
                                      ha,
                                      successfullAssignments,
                                      rc,
                                      kwkw.Name,
                                      kwkw.Standort,
                                      suppIsn,
                                      "WKW",
                                      kwkw.EffectiveEnergyDemand,
                                      energysum,
                                      house.ComplexName,
                                      kwkw.FinalIsn);
                    }

                    if (component.HouseComponentType == HouseComponentType.Photovoltaik)
                    {
                        var ha = hausanschlusses.First(x => x.Guid == component.HausAnschlussGuid);
                        if (lgzs.Any(x => string.Equals(x.Knoten.ToLower(), ha.ObjectID.ToLower(), StringComparison.InvariantCultureIgnoreCase)))
                        {
                            assignedPVsystems.Add((PvSystemEntry)component);
                        }
                        else
                        {
                            otherPVsystems.Add((PvSystemEntry)component);
                        }
                    }
                }
            }

            foreach (var zuordnung in lgzs)
            {
                if (!successfullAssignments.Contains(zuordnung))
                {
                    RowBuilder rb         = RowBuilder.Start("Lastgang", zuordnung.FileName).Add("Diren Profilename", zuordnung.Knoten);
                    var        has        = hausanschlusses.Where(x => x.ObjectID == zuordnung.Knoten).ToList();
                    var        haguids    = has.Select(x => x.Guid).Distinct().ToList();
                    var        pvs        = assignedPVsystems.Where(x => haguids.Contains(x.HausAnschlussGuid)).ToList();
                    var        houseGuids = has.Select(x => x.HouseGuid).Distinct().ToList();
                    var        otherPV    = otherPVsystems.Where(x => houseGuids.Contains(x.HouseGuid));
                    rb.Add("PV Systems", string.Join(";", pvs.Select(x => x.Name)));
                    rb.Add("Other PV Systems@house", string.Join(";", otherPV.Select(x => x.Name)));
                    var rlm = rlms.Where(x => x.Name.Contains(zuordnung.FileName)).ToList();
                    for (int i = 0; i < rlm.Count; i++)
                    {
                        var energysum = new Profile(rlm[i].Profile).EnergySum();
                        rb.Add("Profilesumme " + i, energysum);
                    }

                    rc.Add(rb);
                    if (has.Count > 0)
                    {
                        List <string> housenames = new List <string>();
                        foreach (var ha in has)
                        {
                            var house = houses.First(x => x.Guid == ha.HouseGuid);
                            housenames.Add(house.ComplexName);
                        }

                        rb.Add("Hausname", string.Join(",", housenames.Distinct()));
                    }
                }
            }

            foreach (var pVsystem in otherPVsystems)
            {
                House house = houses.First(x => x.Guid == pVsystem.HouseGuid);
                var   rb    = RowBuilder.Start("Hausname", house.ComplexName);
                rb.Add("Effective Energy", pVsystem.EffectiveEnergyDemand);
                rb.Add("Planned Energy", pVsystem.EffectiveEnergyDemand);
                var ha = hausanschlusses.First(x => x.Guid == pVsystem.HausAnschlussGuid);
                rb.Add("Zugeordnete ObjektID", ha.ObjectID);
                rc.Add(rb);
            }

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

            XlsxDumper.WriteToXlsx(fn, rc);

            var missingAssigments = lgzs.Where(x => !successfullAssignments.Contains(x)).ToList();

            if (missingAssigments.Count > 0)
            {
                Info("Missing assignments: " + string.Join("\n", missingAssigments.Select(x => x.FileName)));
            }

            foreach (var pair in standortDict)
            {
                if (!checkedStandorte.Contains(pair.Key))
                {
                    //throw new FlaException("Didn't find supplemental standort " + pair.Value + " in the list of components. Typo?");
                }
            }

            foreach (var ase in ases)
            {
                foreach (var target in ase.Targets)
                {
                    if (!ase.Assigned.Contains(target))
                    {
                        //  throw new FlaException("Missing " + target + " from the list of assigned standorts");
                    }
                }

                foreach (var assigned in ase.Assigned)
                {
                    if (!ase.Targets.Contains(assigned))
                    {
                        //throw new FlaException("Missing " + assigned + " from the list of target standorts");
                    }
                }
            }
        }
        protected override void RunActualProcess()
        {
            var dbHouse = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);

            dbHouse.RecreateTable <BusinessEntry>();
            //load data
            var                  potentialBusinesses = dbHouse.Fetch <PotentialBusinessEntry>();
            var                  hausanschlusses     = dbHouse.Fetch <Hausanschluss>();
            var                  houses           = dbHouse.Fetch <House>();
            var                  validIsns        = houses.SelectMany(x => x.Hausanschluss.Select(y => y.Isn)).ToHashSet();
            List <int>           invalidIsns      = new List <int>();
            const int            randomlyChosenHa = 0;
            const int            reassignedHAs    = 0;
            List <BusinessEntry> businesses       = new List <BusinessEntry>();

            dbHouse.BeginTransaction();
            foreach (var pb in potentialBusinesses)
            {
                if ((pb.MyCategory == "Immobilien" || pb.MyCategory == "WEG") &&
                    pb.Standort != "Einschlagweg 59, Geschoss unbekannt, 3400 Burgdorf" &&
                    pb.Standort != "Fabrikweg 6, Geschoss unbekannt, 3400 Burgdorf")
                {
                    if (pb.HighVoltageYearlyElectricityUse > 0)
                    {
                        throw new FlaException("Building infrastructure with MS?");
                    }

                    var pbi = new PotentialBuildingInfrastructure(pb.HouseGuid,
                                                                  pb.BusinessName,
                                                                  pb.LowVoltageYearlyElectricityUse,
                                                                  pb.HighVoltageYearlyElectricityUse,
                                                                  pb.LowVoltageLocalnetEntries,
                                                                  pb.HighVoltageLocalnetEntries,
                                                                  pb.Standort, Guid.NewGuid().ToString());
                    dbHouse.Save(pbi);
                }
                else
                {
                    BusinessType bt = GetTypeFromDescription(pb.MyCategory);
                    var          be = new BusinessEntry(pb, bt);
                    be.HouseComponentType = HouseComponentType.BusinessNoLastgangLowVoltage;
                    //var hasIndustry = false;

                    /*if (pb.LowVoltageLocalnetEntries.Any(x => x.Rechnungsart == "Industrie") ||
                     *  pb.HighVoltageLocalnetEntries.Any(x => x.Rechnungsart == "Industrie")) {
                     *  hasIndustry = true;
                     * }*/

/*                    if (hasIndustry) {
 *                      be.BusinessType = BusinessType.Industrie;
 *                  }
 *                  else {
 *                      ;
 *                  }*/

                    //isn kontrolle
                    int validisn = 0;
                    foreach (int hhIsn in be.OriginalISNs)
                    {
                        if (!validIsns.Contains(hhIsn))
                        {
                            invalidIsns.Add(hhIsn);
                        }
                        else
                        {
                            validisn = hhIsn;
                        }
                    }

                    var house = houses.Single(x => x.Guid == be.HouseGuid);
                    if (validisn == 0)
                    {
                        be.FinalIsn = house.Hausanschluss[0].Isn;
                    }
                    else
                    {
                        be.FinalIsn = validisn;
                    }


                    var ha = hausanschlusses.Single(x => x.Guid == pb.HausAnschlussGuid);
                    if (ha == null)
                    {
                        throw new FlaException("ha was null");
                    }

                    /*     hausanschlusses.Where(x => x.HouseGuid == be.HouseGuid && x.Isn == be.FinalIsn).ToList();
                     * if (ha.Count == 0)
                     * {
                     *     //throw new FlaException("Kein Hausanschluss gefunden.");
                     *     reassignedHAs++;
                     *     be.HausAnschlussGuid = house.Hausanschluss[0].HausanschlussGuid;
                     * }
                     *
                     * if (ha.Count == 1)
                     * {
                     *     be.HausAnschlussGuid = ha[0].HausanschlussGuid;
                     * }
                     *
                     * if (ha.Count > 1)
                     * {
                     *     randomlyChosenHa++;
                     *     be.HausAnschlussGuid = ha[Services.Rnd.Next(ha.Count)].HausanschlussGuid;
                     *     //throw new FlaException("zu viele Hausanschlüsse gefunden.: " + ha.Count);
                     * }*/
                    be.HausAnschlussGuid = pb.HausAnschlussGuid;
                    businesses.Add(be);
                }
            }
            dbHouse.CompleteTransaction();
            AssignRlmProfiles(businesses, houses);
            foreach (var businessEntry in businesses)
            {
                if (businessEntry.LocalnetHighVoltageYearlyTotalElectricityUse > 0)
                {
                    if (businessEntry.HouseComponentType == HouseComponentType.BusinessNoLastgangLowVoltage)
                    {
                        businessEntry.HouseComponentType = HouseComponentType.BusinessNoLastgangHighVoltage;
                    }
                    else if (businessEntry.HouseComponentType == HouseComponentType.BusinessWithLastgangLowVoltage)
                    {
                        businessEntry.HouseComponentType = HouseComponentType.BusinessWithLastgangHighVoltage;
                    }
                }
            }

            Debug("Invalid Isns: " + invalidIsns.Distinct().Count());
            Debug("Zufällig ausgewählte Hausanschlüsse bei Häusern mit mehr als einem HA: " + randomlyChosenHa);
            Debug("Wohnungen mit neuem Hausanschluss wegen nicht gefundener ISN: " + reassignedHAs);
            int normalBusinesses = businesses.Count(x => x.HouseComponentType == HouseComponentType.BusinessNoLastgangLowVoltage);
            int rlmBusinesses    = businesses.Count(x => x.HouseComponentType == HouseComponentType.BusinessWithLastgangLowVoltage);

            Debug("Businesses without rlm:" + normalBusinesses + " with: " + rlmBusinesses);
            dbHouse.BeginTransaction();
            foreach (BusinessEntry entry in businesses)
            {
                dbHouse.Save(entry);
            }

            dbHouse.CompleteTransaction();
            RowCollection rc = new RowCollection("Businesses", "");

            foreach (var entry in businesses)
            {
                RowBuilder rb = RowBuilder.Start("Name", entry.BusinessName);
                rb.Add("Verbrauchstyp", entry.EnergyType);
                rb.Add("Verbrauch", entry.EffectiveEnergyDemand);
                rb.Add("Businesstype", entry.BusinessType.ToString());
                rc.Add(rb);
            }

            var fn = MakeAndRegisterFullFilename("Businesses.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }
예제 #22
0
파일: Table.cs 프로젝트: ChrisMoreton/Test3
			public RowCollection Clone()
			{
				RowCollection rc = new RowCollection();
				foreach (Row r in this)
					rc.Add(r.Clone());
				return rc;
			}
        protected override void RunActualProcess()
        {
            var dbHouses            = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice);
            var dbHousesPersistence =
                Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Houses, Constants.PresentSlice, DatabaseCode.Persistence);

            dbHousesPersistence.CreateTableIfNotExists <PersistentHouseholdResidents>();
            var dbRaw      = Services.SqlConnectionPreparer.GetDatabaseConnection(Stage.Raw, Constants.PresentSlice);
            var households = dbHouses.Fetch <Household>();
            var jahrgänge  = dbRaw.Fetch <Jahrgang>();

            if (households.Count == 0)
            {
                throw new Exception("no households founds");
            }

            if (households.Count != A05_HouseholdMaker.HouseholdAccordingToStadtverwaltung)
            {
                throw new FlaException("Missing households!?");
            }

            foreach (var household in households)
            {
                household.Occupants.Clear();
            }
            var persistentResidents  = dbHousesPersistence.Fetch <PersistentHouseholdResidents>();
            int householdCountBefore = households.Count;

            dbHouses.BeginTransaction();
            Info("found " + persistentResidents.Count + " persistent residents");
            foreach (var persistentHouseholdResidentse in persistentResidents)
            {
                var household = households.FirstOrDefault(x => x.HouseholdKey == persistentHouseholdResidentse.HouseholdKey);
                if (household == null)
                {
                    Info("Invalid persistence for " + persistentHouseholdResidentse.HouseholdKey);
                    dbHousesPersistence.Delete(persistentHouseholdResidentse);
                    continue;
                }

                foreach (var persistentOccupant in persistentHouseholdResidentse.Occupants)
                {
                    var occupant = new Occupant(household.Guid,
                                                Guid.NewGuid().ToString(),
                                                persistentOccupant.Age,
                                                persistentOccupant.Gender,
                                                household.HouseGuid,
                                                household.HouseholdKey);
                    household.Occupants.Add(occupant);
                    var jahrgang = 2018 - persistentOccupant.Age;
                    var einträge = jahrgänge.Single(x => x.Jahr == jahrgang);
                    einträge.Count--;
                    if (einträge.Count < 0)
                    {
                        throw new FlaException("Negative population");
                    }
                }

                dbHouses.Save(household);
                households.Remove(household);
            }

            Info("Covered " + (householdCountBefore - households.Count) + " households from persistence, households left: " + households.Count);

            var hmfc             = new HouseMemberFuzzyCalc(Services.MyLogger, MyStage);
            var potentialPersons = new List <PotentialPerson>();

            foreach (var jahrgang in jahrgänge)
            {
                var age = 2018 - jahrgang.Jahr;
                var g   = Gender.Male;
                for (var i = 0; i < jahrgang.Count; i++)
                {
                    var pp = new PotentialPerson(g, age);
                    potentialPersons.Add(pp);
                    g = g == Gender.Male ? Gender.Female : Gender.Male;
                }
            }

            var r = new Random(1);

            foreach (var household in households)
            {
                household.HeuristicFamiliySize = hmfc.GetPeopleCountForEnergy(household.EffectiveEnergyDemand);
                household.Occupants.Clear();
            }

            //put at least one person into every household
            foreach (var household in households)
            {
                var eligiablePersons = potentialPersons.Where(x => x.Age >= 18).ToList();
                var occ = MakeOccupant(eligiablePersons, r, potentialPersons, household);
                household.Occupants.Add(occ);
                dbHouses.Save(occ);
            }

            //put a second person into the households that might have a second one
            foreach (var household in households)
            {
                if (household.HeuristicFamiliySize < 2)
                {
                    continue;
                }

                var g                = household.Occupants[0].Gender;
                var otherGender      = g == Gender.Male ? Gender.Female : Gender.Male;
                var eligiablePersons = potentialPersons.Where(x => x.Age >= 18 && x.Gender == otherGender).ToList();
                if (eligiablePersons.Count == 0)
                {
                    eligiablePersons = potentialPersons;
                }

                var occ2 = MakeOccupant(eligiablePersons, r, potentialPersons, household);
                household.Occupants.Add(occ2);
            }

            var count = 0;

            while (potentialPersons.Count > 0)
            {
                count++;
                if (count > 100000)
                {
                    throw new Exception("Couldnt allocate everything after " + count + " iterations," + potentialPersons.Count + " left.");
                }

                var allocatedCount = 0;
                foreach (var household in households)
                {
                    if (household.Occupants.Count >= household.HeuristicFamiliySize)
                    {
                        continue;
                    }

                    if (potentialPersons.Count == 0)
                    {
                        break;
                    }

                    var eligiablePersonsKids = potentialPersons.Where(x => x.Age < 18).ToList();
                    if (eligiablePersonsKids.Count == 0)
                    {
                        eligiablePersonsKids = potentialPersons;
                    }

                    var occ3 = MakeOccupant(eligiablePersonsKids, r, potentialPersons, household);
                    household.Occupants.Add(occ3);
                    allocatedCount++;
                }

                if (allocatedCount == 0 && potentialPersons.Count > 0)
                {
                    var hhs = households.Where(x => x.HeuristicFamiliySize > 2).ToList();
                    if (hhs.Count == 0)
                    {
                        hhs = households;
                    }

                    var idx = Services.Rnd.Next(hhs.Count);
                    hhs[idx].HeuristicFamiliySize++;
                }
            }

            List <PersistentHouseholdResidents> newPersistentResidents = new List <PersistentHouseholdResidents>();
            int peopleCount = 0;

            foreach (var hh in households)
            {
                dbHouses.Save(hh);
                PersistentHouseholdResidents phhr = new PersistentHouseholdResidents(hh.HouseholdKey);

                foreach (var occupant in hh.Occupants)
                {
                    phhr.Occupants.Add(new PersistentOccupant(occupant.Age, occupant.Gender));
                    peopleCount++;
                }

                newPersistentResidents.Add(phhr);
            }

            dbHouses.CompleteTransaction();
            dbHousesPersistence.BeginTransaction();
            foreach (var phhr in newPersistentResidents)
            {
                dbHousesPersistence.Save(phhr);
            }

            Info("Saved " + newPersistentResidents.Count + " persistence records with a total of " + peopleCount + " people");
            dbHousesPersistence.CompleteTransaction();
            var allhouseholds = dbHouses.FetchAsRepo <Household>();
            var rc            = new RowCollection("occupants", "occupants");

            foreach (var hh in allhouseholds)
            {
                foreach (var occupant in hh.Occupants)
                {
                    var rb = RowBuilder.Start("age", occupant.Age).Add("Gender", occupant.Gender);
                    rc.Add(rb);
                }
            }

            var fn = MakeAndRegisterFullFilename("OccupantList.xlsx", Constants.PresentSlice);

            XlsxDumper.WriteToXlsx(fn, rc);
        }