using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.IO; namespace Bubbler { public class ParseData { public int frequency = 0; double hertz; public double[] BoardChannel = new double[16]; public string fileWithP; public uint Average; private string Port; public int threshold = 150; public int dt = 550; /* the minimum time interval between data points for neighboring pulses */ public uint lasttimemark = 2; public ParseData(string Port){ this.Port=Port; } public string FormatData(string UploadedFileName) { /* Open File and Remove all line feeds and carrage returns*/ string FileToParse = ""; FileToParse = System.IO.File.ReadAllText(UploadedFileName); Regex re = new Regex(@"([\x00-\x1F\x7E-\xFF]+)", RegexOptions.Compiled); string StringWithEverythingRemoved = ""; int y = 0; string op = re.Replace(FileToParse, ""); /* Find anything between all 22's and all 33's inclusive and store it in m * If you find a match remove any whitespace*/ Regex regex = new Regex(@"(22 ){10,}(?.+)(33 ){10,}"); Match m = regex.Match(op); if (m.Success) { Regex r = new Regex(@"\s"); StringWithEverythingRemoved = r.Replace(m.Groups[@"centervalue"].Value, ""); } /* DataArrayOfSamples holds all the data.Here we go through * StringWithEverythingRemoved and put each * time based chunck into an index */ string[] DataArrayOfSamples = new string[(StringWithEverythingRemoved.Length / 68)]; //CGL 72 int NumberOfSamples = ((StringWithEverythingRemoved.Length / 68) - 1); //CGL 72 for (int x = 0; x <= NumberOfSamples; x++) { DataArrayOfSamples[x] = StringWithEverythingRemoved.Substring(y, 68); //CGL 72 y += 68; //CGL 72 } /*Here we take each DataArrayOfSamples and break it apart into * samples and data.multiArrayofData[,] holds our data */ string[,] multiArrayofData = new string[DataArrayOfSamples.Length, 18]; //CGL 18 int SampleNumber = 0; int TotalBytesInEachSample = DataArrayOfSamples[0].Length / 2; foreach (string ArrayString2 in DataArrayOfSamples) { int a1 = 0; int c1 = 0; for (int tb = 0; tb < TotalBytesInEachSample; tb += 2) { multiArrayofData[SampleNumber, a1] = ArrayString2.Substring(c1 + 2, 2) + ArrayString2.Substring(c1, 2); a1++; c1 = c1 + 4; } SampleNumber++; } /*Variables used to hold data as its converted from hex to Int and written to the file ending in _p*/ uint time = 0; int LengthofFileName = 0; List chans = new List(); for (uint i = 0; i < 16; i++) { chans.Add(0); } /* File name is input file name with _p appended to it TextWritter is used to Write data to a file.Data is converted to ints and Then written*/ LengthofFileName = UploadedFileName.Length; UploadedFileName = UploadedFileName.Substring(0, (LengthofFileName - 4)); fileWithP = (UploadedFileName + "_p.txt"); TextWriter tw = new StreamWriter(fileWithP); for (int x = 0; x <= (DataArrayOfSamples.Length - 1); x++) { UInt32.TryParse((multiArrayofData[x, 0]), System.Globalization.NumberStyles.HexNumber, null, out time); for (int r = 0; r < 16; r++) { int b = r + 1; chans[r] = UInt32.Parse((multiArrayofData[x, b]), System.Globalization.NumberStyles.HexNumber); } //CGL UInt32.TryParse((multiArrayofData[x, 17]), System.Globalization.NumberStyles.HexNumber, null, out time1); tw.Write(time); tw.Write("\t"); for (int ch = 0; ch < 16; ch++) { tw.Write(chans[ch]); tw.Write("\t"); } tw.Write("\t"); //CGL tw.Write(time1); tw.Write("\t"); tw.Write("\r\n"); } lasttimemark = time; tw.Close(); return fileWithP; } public double[] GetFrequency(string permanantfilename) { string FileName = ""; FileName = File.ReadAllText(permanantfilename); /*Split File along newlines which are time based chuncks and store them in TimeSeperatedChunk[]*/ string[] lineSeparators = new String[] { "\r\n" }; string[] TimeSeperatedChunk = FileName.Split(lineSeparators, StringSplitOptions.None); /*Take each TimeSeperatedChunk[] Array and store it in a new array Sample[]*/ string[][] Sample = new string[TimeSeperatedChunk.Length][]; int i = 0; string[] stringSeparators = new String[] { "\t" }; foreach (string ArrayString in TimeSeperatedChunk) { Sample[i] = ArrayString.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries); i++; } i = 0; uint[][] intArray = new uint[Sample.Length][]; for (i=0;i<(Sample.Length);i++) { intArray[i] = new uint[Sample[i].Length]; for(int j=0;j<(Sample[i].Length);j++) { intArray[i][j]=UInt32.Parse((Sample[i][j]),System.Globalization.NumberStyles.Integer); } } /* Find the Average of all the datapoints including the time(irrelevent)*/ uint Sum = 0; uint count = 1; // uint Average; for (int r = 0; r < Sample.Length; r++) { for (int d = 1; d < Sample[r].Length; d++) { Sum = Sum + (intArray[r][d]); count++; } } Average = Sum / count; //09/11/2008 Removed for (int r = 0; r < Sample.Length; r++) { for (int d = 1; d < Sample[r].Length; d++) { // if (((Convert.ToInt32(Sample[r][d]) - threshold) < Average) & (((Convert.ToInt32(Sample[r][d]) + threshold)) > Average)) // if (Math.Abs(Convert.ToInt32(Sample[r][d]) - Average) < threshold) // CGL 7/28/2009 replace this line with the following line. if ((Convert.ToInt32(Sample[r][d]) - Average) > (- threshold) ) //only pick the negative pulse, CGL 7/28/2009. Tested it works! { Sample[r][d] = null; } } } int[,] CleanedSample = new int[10000, 3]; int numsamples = 0; int time = 0; for (int r = 0; r < Sample.Length - 1; r++) //for (int r = 0; r < intArray.Length - 1; r++) { //time = Convert.ToInt32(intArray[r][0]); time = Convert.ToInt32(Sample[r][0]); for (int q = 1; q < 17; q++) { if ((Sample[r][q]) != null) { numsamples++; //Structure for CleanedSample is Channel,Time,Data CleanedSample[numsamples, 1] = time; CleanedSample[numsamples, 0] = q; CleanedSample[numsamples, 2] = Convert.ToInt32(Sample[r][q]); } } } ArrayList ChannelData1 = new ArrayList(); ArrayList ChannelData2 = new ArrayList(); ArrayList ChannelData3 = new ArrayList(); ArrayList ChannelData4 = new ArrayList(); ArrayList ChannelData5 = new ArrayList(); ArrayList ChannelData6 = new ArrayList(); ArrayList ChannelData7 = new ArrayList(); ArrayList ChannelData8 = new ArrayList(); ArrayList ChannelData9 = new ArrayList(); ArrayList ChannelData10 = new ArrayList(); ArrayList ChannelData11 = new ArrayList(); ArrayList ChannelData12 = new ArrayList(); ArrayList ChannelData13 = new ArrayList(); ArrayList ChannelData14 = new ArrayList(); ArrayList ChannelData15 = new ArrayList(); ArrayList ChannelData16 = new ArrayList(); for (int ch = 0; ch < ((CleanedSample.Length) / 3) - 1; ch++) { for (int ch2 = 0; ch2 < 2; ch2++) { if ((CleanedSample[ch, 1]) > 10) { switch (CleanedSample[ch, ch2]) { case 1: ChannelData1.Add(CleanedSample[ch, ch2 + 1]); ChannelData1.Add(CleanedSample[ch, ch2 + 2]); break; case 2: ChannelData2.Add(CleanedSample[ch, ch2 + 1]); ChannelData2.Add(CleanedSample[ch, ch2 + 2]); break; case 3: ChannelData3.Add(CleanedSample[ch, ch2 + 1]); ChannelData3.Add(CleanedSample[ch, ch2 + 2]); break; case 4: ChannelData4.Add(CleanedSample[ch, ch2 + 1]); ChannelData4.Add(CleanedSample[ch, ch2 + 2]); break; case 5: ChannelData5.Add(CleanedSample[ch, ch2 + 1]); ChannelData5.Add(CleanedSample[ch, ch2 + 2]); break; case 6: ChannelData6.Add(CleanedSample[ch, ch2 + 1]); ChannelData6.Add(CleanedSample[ch, ch2 + 2]); break; case 7: ChannelData7.Add(CleanedSample[ch, ch2 + 1]); ChannelData7.Add(CleanedSample[ch, ch2 + 2]); break; case 8: ChannelData8.Add(CleanedSample[ch, ch2 + 1]); ChannelData8.Add(CleanedSample[ch, ch2 + 2]); break; case 9: ChannelData9.Add(CleanedSample[ch, ch2 + 1]); ChannelData9.Add(CleanedSample[ch, ch2 + 2]); break; case 10: ChannelData10.Add(CleanedSample[ch, ch2 + 1]); ChannelData10.Add(CleanedSample[ch, ch2 + 2]); break; case 11: ChannelData11.Add(CleanedSample[ch, ch2 + 1]); ChannelData11.Add(CleanedSample[ch, ch2 + 2]); break; case 12: ChannelData12.Add(CleanedSample[ch, ch2 + 1]); ChannelData12.Add(CleanedSample[ch, ch2 + 2]); break; case 13: ChannelData13.Add(CleanedSample[ch, ch2 + 1]); ChannelData13.Add(CleanedSample[ch, ch2 + 2]); break; case 14: ChannelData14.Add(CleanedSample[ch, ch2 + 1]); ChannelData14.Add(CleanedSample[ch, ch2 + 2]); break; case 15: ChannelData15.Add(CleanedSample[ch, ch2 + 1]); ChannelData15.Add(CleanedSample[ch, ch2 + 2]); break; case 16: ChannelData16.Add(CleanedSample[ch, ch2 + 1]); ChannelData16.Add(CleanedSample[ch, ch2 + 2]); break; default: break; } } } } //Structure for ChannelData(x) is Time,Data AddFrequencyToFrequencyArray(ChannelData1, "1"); BoardChannel[0] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData2, "2"); BoardChannel[1] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData3, "3"); BoardChannel[2] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData4, "4"); BoardChannel[3] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData5, "5"); BoardChannel[4] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData6, "6"); BoardChannel[5] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData7, "7"); BoardChannel[6] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData8, "8"); BoardChannel[7] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData9, "9"); BoardChannel[8] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData10, "10"); BoardChannel[9] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData11, "11"); BoardChannel[10] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData12, "12"); BoardChannel[11] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData13, "13"); BoardChannel[12] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData14, "14"); BoardChannel[13] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData15, "15"); BoardChannel[14] = Math.Round(hertz, 2); AddFrequencyToFrequencyArray(ChannelData16, "16"); BoardChannel[15] = Math.Round(hertz, 2); return BoardChannel; } public double AddFrequencyToFrequencyArray(ArrayList EachChannel, string ChannelNumber) { Double initialtime = 0 ; Double totaltime = 0 ; Double totalpulse = 0 ; Double lasttime = 0 ; int lastchannel = 0; InsertData privateInsertData = new InsertData(); if (EachChannel.Count > 6) { // writetestdatatofile(EachChannel); //CGL // calculate the center of mass for the first bubble cluster, the data points with delta T < dt form a cluster. initialtime = Convert.ToInt32(EachChannel[0]) * Math.Abs(Average - Convert.ToInt32(EachChannel[1])); totalpulse = Math.Abs(Average - Convert.ToInt32(EachChannel[1])); for (int w = 2; w < EachChannel.Count; w = w + 2) { if ((Convert.ToInt32(EachChannel[w-2]) + dt) > (Convert.ToInt32(EachChannel[w]))) { initialtime = initialtime + Convert.ToInt32(EachChannel[w]) * Math.Abs(Average - Convert.ToInt32(EachChannel[w + 1])); totalpulse = totalpulse + Math.Abs(Average - Convert.ToInt32(EachChannel[w + 1])); lastchannel = w; } else { break; } } initialtime = initialtime / totalpulse; /* for (int w = 0; w < lastchannel + 1; w = w+2) { EachChannel.RemoveAt(0); EachChannel.RemoveAt(1); } */ // Calculate the center of mass for the next bubble cluster if ((EachChannel.Count - lastchannel) > 4) { lasttime = Convert.ToInt32(EachChannel[lastchannel + 2]) * Math.Abs(Average - Convert.ToInt32(EachChannel[lastchannel + 3])); totalpulse = Math.Abs(Average - Convert.ToInt32(EachChannel[lastchannel + 3])); for (int w = lastchannel + 2; w < EachChannel.Count; w = w + 2) { if ((Convert.ToInt32(EachChannel[w - 2]) + dt) > (Convert.ToInt32(EachChannel[w]))) { lasttime = lasttime + Convert.ToInt32(EachChannel[w]) * Math.Abs(Average - Convert.ToInt32(EachChannel[w + 1])); totalpulse = totalpulse + Math.Abs(Average - Convert.ToInt32(EachChannel[w + 1])); } else { break; } } lasttime = lasttime / totalpulse; totaltime = lasttime - initialtime; hertz = Convert.ToDouble(4096 / totaltime); if (hertz > 3 | hertz < 0) { hertz = hertz; // for debug use } } else // only one pulse presented, set hertz = minimum (4096/lasttimemark); { hertz = 4096/Convert.ToDouble(lasttimemark); } //InsertData privateInsertData = new InsertData(); privateInsertData.InsertFrequencyIntoDatabase(this.Port, Convert.ToInt16(ChannelNumber), hertz); return hertz; } else { hertz = 0 ; // No pulse detected, set hertz = 0; privateInsertData.InsertFrequencyIntoDatabase(this.Port, Convert.ToInt16(ChannelNumber), hertz); return hertz; } } private void writetestdatatofile(ArrayList testarray) { string testfilename = ("C\\testnew.txt"); TextWriter testw = new StreamWriter(testfilename); for(int r=0;r