Click here to Skip to main content
15,881,812 members
Home / Discussions / C#
   

C#

 
GeneralRe: Basic Polymorphism Problem Pin
BobJanova5-Oct-12 3:04
BobJanova5-Oct-12 3:04 
QuestionC# linq group by Pin
rachel_m4-Oct-12 17:43
rachel_m4-Oct-12 17:43 
AnswerRe: C# linq group by Pin
OriginalGriff4-Oct-12 23:19
mveOriginalGriff4-Oct-12 23:19 
GeneralRe: C# linq group by Pin
rachel_m5-Oct-12 2:48
rachel_m5-Oct-12 2:48 
GeneralRe: C# linq group by Pin
OriginalGriff5-Oct-12 3:29
mveOriginalGriff5-Oct-12 3:29 
QuestionCrystal Reports : Database Login Prompt Issue Pin
Quam Chang4-Oct-12 16:36
Quam Chang4-Oct-12 16:36 
AnswerRe: Crystal Reports : Database Login Prompt Issue Pin
AmbiguousName4-Oct-12 20:00
AmbiguousName4-Oct-12 20:00 
QuestionAggregate data into 5 min interval Pin
bad_kid4-Oct-12 11:59
bad_kid4-Oct-12 11:59 
Hello everyone,

I'm pretty new to this forum and have a question regarding to using C# to aggregate a csv file containing the vehicle position data into certain time interval. The format of the data is like follows:

XML
Timestamp(s),    linkNo,   ......... NOx, PM10, CO2,
27000,           10,    .....         0.4,  1.2,  2.0
27000,           12,    .....         0.3,  1.4   3.0
27010,           10,    .....         0.4,  1.2,  2.0
27010,           12,    .....         0.3,  1.4   3.0
27020,           10,    .....         0.4,  1.2,  2.0
27020,           12,    .....         0.3,  1.4   3.0
.......
</pre>

The problem i'm trying to solve is to aggregate these data into 5 minutes interval and sum up all the pollutants (i.e. NOX, PM1O and CO2) on each link, the final results i want to get is:
<pre lang="xml">
TimeSlot,    linkNo,   ......... SumNOx, SumPM10, SumCO2,
27000,           10,    .....         1.2,  3.6,  6.0
27000,           12,    .....         0.3,  4.2   9.0
27300,           10,    .....         xx,  xx,  xx
27600,           12,    .....         xx,  xx,   xx
.......
<pre lang="c#"><


i have written some code to get the sum of these pollutants based on links, but i don't know how to aggregate them into each 5 minutes slot. here is the code i written so far:

C#
    public partial class FormMain : Form
    {
        private double TimeSlotFunction(double SimulationTime)
        {
            double Time = 0;
            for (int i = 1; i <= 24; i++)
            {
                double StartTime = 27000 + 300 * (i - 1);
                double EndTime = StartTime + 300 * i;
                if (StartTime < SimulationTime && SimulationTime <= EndTime)
                {

                    Time = StartTime;
                    break;
                }

            }
            return Time;
        }

        private class LinkData
        {
            public string LinkName;
            public List<double> SimTime = new List<double>();
            public List<double> timeSlot = new List<double>();
            public List<double> NOx = new List<double>();
            public List<double> PM10 = new List<double>();
            public List<double> TotalCarbon = new List<double>();
            public double SumNOx;
            public double SumPM10;
            public double SumTotalCarbon;
            public double SumCO2;
        }

            foreach (string[] row in parsedData)
            {
                Entries.Add(row);
            }
/********Compute NOX, PM10, TotalCarbon for each link***********/

            var LinkDict = new Dictionary<string, LinkData>();
            for (int index = 1; index < Entries.Count; index++)
            {
                var row = Entries[index];
                if (!LinkDict.ContainsKey(row[1]))
                {
                    var newLink = new LinkData { LinkName = row[1] };
                    LinkDict.Add(row[1], newLink);
                }

                LinkDict[row[1]].SimTime.Add(Convert.ToDouble((row[0])));
                LinkDict[row[1]].NOx.Add(Convert.ToDouble((row[19])));
                LinkDict[row[1]].PM10.Add(Convert.ToDouble((row[20])));
                LinkDict[row[1]].TotalCarbon.Add(Convert.ToSingle((row[21])));
            }

            foreach (var linkValue in LinkDict.Values)
                linkValue.SumNOx = linkValue.NOx.Sum();
            foreach (var linkValue in LinkDict.Values)
                linkValue.SumPM10 = linkValue.PM10.Sum();
            foreach (var linkValue in LinkDict.Values)
                linkValue.SumTotalCarbon = linkValue.TotalCarbon.Sum();
            foreach (var linkValue in LinkDict.Values)

            using (var sw = new StreamWriter(@"D:\Work\C#\my project\my project\AlteredData.csv"))
            {

                foreach (var linkValue in LinkDict.Values)
                {
                    var sb = new StringBuilder();
                    //sb.Append(TimeSlotFunction(linkValue.SimTime.ToString());
                    //sb.Append(",");
                    sb.Append(linkValue.LinkName);
                    sb.Append(",");
                    sb.Append(linkValue.SumNOx.ToString());
                    sb.Append(",");
                    sb.Append(linkValue.SumPM10.ToString());
                    sb.Append(",");
                    sb.Append(linkValue.SumTotalCarbon.ToString());
                    sb.Append(",");
                    sb.Append(linkValue.SumCO2.ToString());
                    sb.Append(",");
                    sw.WriteLine(sb.ToString());
                }

            }
        }


I will be grateful if anyone can give me some advice.

cheers,
Simon
QuestionC# linq Pin
rachel_m4-Oct-12 9:36
rachel_m4-Oct-12 9:36 
Question.exe Reference Problem - maybe Pin
dirsow4-Oct-12 7:52
dirsow4-Oct-12 7:52 
AnswerRe: .exe Reference Problem - maybe Pin
Eddy Vluggen4-Oct-12 9:01
professionalEddy Vluggen4-Oct-12 9:01 
GeneralRe: .exe Reference Problem - maybe Pin
dirsow4-Oct-12 9:27
dirsow4-Oct-12 9:27 
GeneralRe: .exe Reference Problem - maybe Pin
Eddy Vluggen4-Oct-12 9:34
professionalEddy Vluggen4-Oct-12 9:34 
GeneralRe: .exe Reference Problem - maybe Pin
dirsow4-Oct-12 9:41
dirsow4-Oct-12 9:41 
AnswerRe: .exe Reference Problem - maybe Pin
Bernhard Hiller4-Oct-12 21:00
Bernhard Hiller4-Oct-12 21:00 
Questionhow to setting Four tier application in my application? Pin
somasundarapandian4-Oct-12 4:28
somasundarapandian4-Oct-12 4:28 
AnswerRe: how to setting Four tier application in my application? Pin
Dave Kreskowiak4-Oct-12 6:45
mveDave Kreskowiak4-Oct-12 6:45 
QuestionRe: how to setting Four tier application in my application? Pin
Eddy Vluggen4-Oct-12 9:02
professionalEddy Vluggen4-Oct-12 9:02 
AnswerRe: how to setting Four tier application in my application? Pin
OriginalGriff4-Oct-12 9:34
mveOriginalGriff4-Oct-12 9:34 
Questionsqldatabaseconnectivity Pin
Member 94545634-Oct-12 4:05
Member 94545634-Oct-12 4:05 
AnswerRe: sqldatabaseconnectivity Pin
Ravi Bhavnani4-Oct-12 4:31
professionalRavi Bhavnani4-Oct-12 4:31 
AnswerRe: sqldatabaseconnectivity Pin
Richard MacCutchan4-Oct-12 4:32
mveRichard MacCutchan4-Oct-12 4:32 
AnswerRe: sqldatabaseconnectivity Pin
palakshamb4-Oct-12 23:28
palakshamb4-Oct-12 23:28 
QuestionCan an Attribute class find out, to which class it is actually attached? Pin
Andy4114-Oct-12 2:42
Andy4114-Oct-12 2:42 
AnswerRe: Can an Attribute class find out, to which class it is actually attached? Pin
Dave Kreskowiak4-Oct-12 6:56
mveDave Kreskowiak4-Oct-12 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.