示例#1
0
        /// <summary>
        /// Program entry point
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            #region preproceiing parsing the user inputs
            Console.WriteLine("Parsing given parameters...");
            //Using the filename provided as the first argument
            string fileName = args[0];

            //Using the years defined in the second argument
            string     years      = args[1];
            string[]   yearsSplit = years.Split(',');
            List <int> yearsList  = new List <int>();
            int        temp;
            foreach (string y in yearsSplit)
            {
                if (int.TryParse(y, out temp))
                {
                    yearsList.Add(temp);
                }
            }

            //Using the pathways and mix id provided in the third argument
            string   pm  = args[2];
            string[] pms = pm.Split(',');
            List <InputResourceReference> inRef = new List <InputResourceReference>();
            foreach (string s in pms)
            {
                if (s[0] == 'p')
                {//this is a pathway reference
                    if (int.TryParse(s.Substring(1, s.Length - 1), out temp))
                    {
                        InputResourceReference pRef = new InputResourceReference(-1, temp, Enumerators.SourceType.Pathway);
                        inRef.Add(pRef);
                    }
                }
                else if (s[0] == 'm')
                {//this is a mix reference
                    if (int.TryParse(s.Substring(1, s.Length - 1), out temp))
                    {
                        InputResourceReference mRef = new InputResourceReference(-1, temp, Enumerators.SourceType.Mix);
                        inRef.Add(mRef);
                    }
                }
            }
            #endregion

            #region loading units file and data file
            Console.WriteLine("Building units context...");
            //Build units context before loading the database
            Units.BuildContext();

            Console.WriteLine("Loading datafile...");
            //Loading the database
            GProject project = new GProject();
            project.Load(fileName);
            #endregion

            #region preprocessing the pathways/mixes we want to record by finding their main output resource
            //Assign main output resource IDs to all the inputsResourceReferences
            foreach (InputResourceReference iref in inRef)
            {
                if (iref.SourceType == Greet.DataStructureV4.Interfaces.Enumerators.SourceType.Pathway)
                {
                    if (project.Dataset.PathwaysData.ContainsKey(iref.SourceMixOrPathwayID))
                    {
                        iref.ResourceId = project.Dataset.PathwaysData[iref.SourceMixOrPathwayID].MainOutputResourceID;
                    }
                }
                else if (iref.SourceType == Greet.DataStructureV4.Interfaces.Enumerators.SourceType.Mix)
                {
                    if (project.Dataset.MixesData.ContainsKey(iref.SourceMixOrPathwayID))
                    {
                        iref.ResourceId = project.Dataset.MixesData[iref.SourceMixOrPathwayID].MainOutputResourceID;
                    }
                }
            }
            #endregion

            #region running the calculations for each year and storing the results
            //Creating a new instance of a dictionary used to store results of the simulations
            Dictionary <InputResourceReference, Dictionary <int, Results> > savedResults = new Dictionary <InputResourceReference, Dictionary <int, Results> >();

            //Running simulations for every provided years
            foreach (int simulationYear in yearsList)
            {
                Console.WriteLine("Running calculations for year " + simulationYear);
                //Set the current year for simulations
                BasicParameters.SelectedYear = project.Dataset.ParametersData.CreateUnregisteredParameter(project.Dataset, "", simulationYear);
                Calculator calc = new Calculator();

                //Run the simulations for the loaded project and defined year, we need to wait completion as the RunCalculationMethod is Async
                var manualEvent = new ManualResetEvent(false);
                calc.CalculationDoneEvent += () => manualEvent.Set();
                calc.RunCalculations(project);
                manualEvent.WaitOne();

                //Loop over all the pathways and mixes ID that we wish to save
                foreach (InputResourceReference pathMixToSave in inRef)
                {
                    if (!savedResults.ContainsKey(pathMixToSave))
                    {
                        savedResults.Add(pathMixToSave, new Dictionary <int, Results>());
                    }
                    if (!savedResults[pathMixToSave].ContainsKey(simulationYear))
                    {
                        //Pull the results and add them to the dictionary used to store results
                        Results results;
                        if (pathMixToSave.SourceType == Enumerators.SourceType.Pathway)
                        {
                            results = Convenience.Clone(project.Dataset.PathwaysData[pathMixToSave.SourceMixOrPathwayID].getMainOutputResults().Results);
                        }
                        else
                        {
                            results = Convenience.Clone(project.Dataset.MixesData[pathMixToSave.SourceMixOrPathwayID].getMainOutputResults().Results);
                        }
                        savedResults[pathMixToSave].Add(simulationYear, results);
                    }
                }
            }
            #endregion

            #region exporting all the results in a text file per pathway and per mix
            //Export all the desired results to an Excel spreadsheet
            Console.WriteLine("Export all selected results...");
            string preferedMass   = "g";
            string preferedEnergy = "Btu";
            string preferedVolume = "gal";

            foreach (KeyValuePair <InputResourceReference, Dictionary <int, Results> > pair in savedResults)
            {
                DataTable     dt        = new DataTable();
                List <string> resGroups = new List <string>()
                {
                    "Total Energy", "Fossil Fuel", "Coal Fuel", "Natural Gas Fuel", "Petroleum Fuel", "Water"
                };
                List <string> pollutants = new List <string>()
                {
                    "VOC", "CO", "NOx", "PM10", "PM2.5", "SOx", "BC", "POC", "CH4", "N2O", "CO2", "CO2_Biogenic"
                };
                List <string> polGroups = new List <string>()
                {
                    "GHG-100"
                };
                List <string> urbanPoll = new List <string>()
                {
                    "VOC", "CO", "NOx", "PM10", "PM2.5", "SOx", "BC", "POC", "CH4", "N2O"
                };

                Results resultsFU      = pair.Value.Values.FirstOrDefault();
                string  functionalUnit = "Per ";
                if (resultsFU != null)
                {
                    functionalUnit += GetPreferedVisualizationFunctionalUnitString(project.Dataset, resultsFU, pair.Key.ResourceId);
                }

                dt.Columns.Add("Items " + functionalUnit);
                foreach (int simulationYear in pair.Value.Keys)
                {
                    dt.Columns.Add(simulationYear.ToString());
                }
                List <string> rowString = new List <string>();

                #region total energy and energy groups
                foreach (string resGrp in resGroups)
                {
                    rowString = new List <string>();
                    if (resGrp == "Water")
                    {
                        rowString.Add(resGrp + " (" + preferedVolume + ")");
                    }
                    else
                    {
                        rowString.Add(resGrp + " (" + preferedEnergy + ")");
                    }

                    foreach (int simulationYear in pair.Value.Keys)
                    {
                        Results results     = pair.Value[simulationYear];
                        double  amountRatio = GetFunctionalRatio(project.Dataset, results, pair.Key.ResourceId);

                        if (resGrp == "Total Energy")
                        {
                            LightValue totalE = results.wellToProductEnem.materialsAmounts.TotalEnergy();
                            rowString.Add(NiceValueWithAttribute(totalE * amountRatio, preferedEnergy));
                        }
                        else
                        {
                            Dictionary <int, IValue> resGroupes = results.WellToProductResourcesGroups(project.Dataset);
                            Group resGrpSelected = project.Dataset.ResourcesData.Groups.Values.SingleOrDefault(item => item.Name == resGrp);
                            if (resGrpSelected != null)
                            {
                                int        resGrpId   = resGrpSelected.Id;
                                LightValue groupValue = new LightValue(resGroupes[resGrpId].Value, resGroupes[resGrpId].UnitExpression);
                                if (groupValue.Dim == DimensionUtils.ENERGY)
                                {
                                    rowString.Add(NiceValueWithAttribute(groupValue * amountRatio, preferedEnergy));
                                }
                                else
                                {
                                    rowString.Add(NiceValueWithAttribute(groupValue * amountRatio, preferedVolume));
                                }
                            }
                            else
                            {
                                rowString.Add("0");
                            }
                        }
                    }
                    dt.Rows.Add(rowString.ToArray());
                }
                #endregion

                #region wtp emissions
                foreach (string poll in pollutants)
                {
                    rowString = new List <string>();
                    rowString.Add(poll + " (" + preferedMass + ")");
                    foreach (int simulationYear in pair.Value.Keys)
                    {
                        Results results     = pair.Value[simulationYear];
                        double  amountRatio = GetFunctionalRatio(project.Dataset, results, pair.Key.ResourceId);

                        int polId = project.Dataset.GasesData.Values.Single(item => item.Name == poll).Id;
                        rowString.Add(NiceValueWithAttribute(
                                          new LightValue(results.wellToProductEnem.emissions[polId], DimensionUtils.MASS) * amountRatio
                                          , preferedMass));
                    }
                    dt.Rows.Add(rowString.ToArray());
                }
                #endregion

                #region wtp Groups (here only GHG 100)
                foreach (string resGrp in polGroups)
                {
                    rowString = new List <string>();
                    rowString.Add(resGrp + " (" + preferedMass + ")");
                    foreach (int simulationYear in pair.Value.Keys)
                    {
                        Results results     = pair.Value[simulationYear];
                        double  amountRatio = GetFunctionalRatio(project.Dataset, results, pair.Key.ResourceId);

                        Dictionary <int, IValue> emGroupes = pair.Value[simulationYear].WellToProductEmissionsGroups(project.Dataset);
                        Group resGrpSelected = project.Dataset.GasesData.Groups.Values.SingleOrDefault(item => item.Name == resGrp);
                        if (resGrpSelected != null)
                        {
                            int grpId = resGrpSelected.Id;
                            rowString.Add(NiceValueWithAttribute(new LightValue(emGroupes[grpId].Value, emGroupes[grpId].UnitExpression) * amountRatio, preferedMass));
                        }
                        else
                        {
                            rowString.Add("0");
                        }
                    }
                    dt.Rows.Add(rowString.ToArray());
                }
                #endregion

                #region urban emissions
                foreach (string poll in pollutants)
                {
                    rowString = new List <string>();
                    rowString.Add("Urban " + poll + " (" + preferedMass + ")");
                    foreach (int simulationYear in pair.Value.Keys)
                    {
                        Results results     = pair.Value[simulationYear];
                        double  amountRatio = GetFunctionalRatio(project.Dataset, results, pair.Key.ResourceId);

                        int polId = project.Dataset.GasesData.Values.Single(item => item.Name == poll).Id;
                        rowString.Add(NiceValueWithAttribute(new LightValue(results.wellToProductUrbanEmission[polId], DimensionUtils.MASS) * amountRatio, preferedMass));
                    }
                    dt.Rows.Add(rowString.ToArray());
                }
                #endregion


                string value = ConvertDataTableToString(dt);
                System.IO.File.WriteAllText("Results-" + pair.Key.SourceType.ToString() + "-" + pair.Key.SourceMixOrPathwayID.ToString() + ".txt", value);
            }
            #endregion
        }