void metroBackWorker_DoWork(object sender, DoWorkEventArgs e) { DateTime earliestDatetime = DateTime.Now; List <MetroDataPoint> listOfData = new List <MetroDataPoint>(); List <DateTime> listofDates = new List <DateTime>(); List <string> listofMetros = new List <string>(); using (Stream stream = File.Open(InputFileName, FileMode.Open)) { using (StreamReader reader = new StreamReader(stream)) { do { string raw = reader.ReadLine(); string[] values = raw.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); metroBackWorker.ReportProgress(10, "Reading..."); if (values.Length == 10 && values[0].StartsWith("MT")) { MetroDataPoint thisDataPoint = new MetroDataPoint(values[0].Trim(), values[1].Trim(), values[2].Trim(), values[3].Trim(), values[4].Trim(), values[5].Trim(), values[6].Trim(), values[7].Trim(), values[8].Trim(), values[9].Trim()); listOfData.Add(thisDataPoint); if (!listofDates.Contains(thisDataPoint.DateOfData)) { listofDates.Add(thisDataPoint.DateOfData); } if (!listofMetros.Contains(thisDataPoint.Area)) { listofMetros.Add(thisDataPoint.Area); } } } while (reader.Peek() != -1); System.IO.StreamWriter outputFileLF = new System.IO.StreamWriter(OutputFileName.Insert(OutputFileName.Length - 4, "_LaborForce"), true); System.IO.StreamWriter outputFileE = new System.IO.StreamWriter(OutputFileName.Insert(OutputFileName.Length - 4, "_Employment"), true); System.IO.StreamWriter outputFileUE = new System.IO.StreamWriter(OutputFileName.Insert(OutputFileName.Length - 4, "_Unemployment"), true); System.IO.StreamWriter outputFileUER = new System.IO.StreamWriter(OutputFileName.Insert(OutputFileName.Length - 4, "_UnempoymentRate"), true); // Write headers string header = "Date"; foreach (string metro in listofMetros) { header = header + "," + metro; } outputFileLF.WriteLine(header); outputFileE.WriteLine(header); outputFileUE.WriteLine(header); outputFileUER.WriteLine(header); foreach (DateTime dt in listofDates) { string laborLine = "" + dt.Month.ToString() + "/" + dt.Day.ToString() + "/" + dt.Year.ToString(); string employLine = laborLine; string unemployLine = laborLine; string unemployRateLine = laborLine; metroBackWorker.ReportProgress(80, "Date: " + dt.Year.ToString() + "/" + dt.Month.ToString()); foreach (string metro in listofMetros) { bool hasValue = false; foreach (MetroDataPoint mdp in listOfData) { if (mdp.DateOfData == dt && mdp.Area == metro) { laborLine = laborLine + "," + mdp.CivilianLaborForce.ToString("N0").Replace(",", ""); unemployLine = unemployLine + "," + mdp.Unemployment.ToString("N0").Replace(",", ""); unemployRateLine = unemployRateLine + "," + mdp.UnempoymentRate.ToString("N1").Replace(",", ""); employLine = employLine + "," + mdp.Employment.ToString("N0").Replace(",", ""); hasValue = true; break; } } if (!hasValue) { laborLine = laborLine + ","; unemployLine = unemployLine + ","; unemployRateLine = unemployRateLine + ","; employLine = employLine + ","; } } outputFileLF.WriteLine(laborLine); outputFileE.WriteLine(employLine); outputFileUE.WriteLine(unemployLine); outputFileUER.WriteLine(unemployRateLine); } outputFileLF.Close(); outputFileE.Close(); outputFileUE.Close(); outputFileUER.Close(); } } }
void blsStateBackWorker_DoWork(object sender, DoWorkEventArgs e) { System.IO.StreamWriter outputFile = new System.IO.StreamWriter(OutputFileName, true); Dictionary <string, Dictionary <DateTime, BLSValue> > bulkDataHolder = new Dictionary <string, Dictionary <DateTime, BLSValue> >(); List <DateTime> possibleDates = new List <DateTime>(); Dictionary <string, StreamWriter> outputFiles = new Dictionary <string, StreamWriter>(); Dictionary <string, string> industryOutputs = new Dictionary <string, string>(); int countFromAdjust = 0; using (Stream stream = File.Open(InputFileName, FileMode.Open)) { using (StreamReader reader = new StreamReader(stream)) { bool nextIsTitle = false; int titleCount = 0; string currentSeries = ""; do { string raw = reader.ReadLine(); string[] values = raw.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); //Find out what metric we're looking at // This will need to be tweaked on a case by case basis if (nextIsTitle) { #region for state industry data if (titleCount == 3) { string[] titleValues = raw.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (titleValues.Length == 2) { string industry = titleValues[1].Trim().Replace(',', '_'); if (!outputFiles.ContainsKey(industry)) { string newFilePath = OutputFileName.Insert(OutputFileName.Length - 4, industry); outputFiles.Add(industry, new StreamWriter(newFilePath, true)); } currentSeries = currentSeries + " - " + industry; if (!bulkDataHolder.ContainsKey(currentSeries)) { bulkDataHolder.Add(currentSeries, new Dictionary <DateTime, BLSValue>()); } } nextIsTitle = false; } else if (titleCount == 0) { string[] titleValues = raw.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (titleValues.Length == 2) { currentSeries = titleValues[1].Replace(',', '/'); } titleCount++; } else { titleCount++; } #endregion } //Whatever is under "Seasonally Adjusted" if (raw.StartsWith("Seasonally Adjusted")) { nextIsTitle = true; titleCount = 0; } // Now we can look at the data if (values.Length == 4 && values[0] != "Series id" && values[0] != "Series Id") { BLSValue blsVal = new BLSValue(currentSeries, values[0], values[1], values[2], values[3]); bulkDataHolder[currentSeries].Add(blsVal.ValueDate, blsVal); if (!possibleDates.Contains(blsVal.ValueDate)) { possibleDates.Add(blsVal.ValueDate); } } } while (reader.Peek() != -1); //Now we should have all the data, so let's start writing it out // First we need headers for the big file string headerData = "Date"; foreach (KeyValuePair <string, Dictionary <DateTime, BLSValue> > kv in bulkDataHolder) { headerData = headerData + "," + kv.Key.Replace(',', ' '); string[] indParse = kv.Key.Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries); if (!industryOutputs.ContainsKey(indParse[1])) { industryOutputs.Add(indParse[1], "Year," + indParse[0].Replace(',', ' ')); } else { industryOutputs[indParse[1]] = industryOutputs[indParse[1]] + "," + indParse[0]; } } outputFile.WriteLine(headerData); foreach (KeyValuePair <string, string> kv in industryOutputs) { if (outputFiles.ContainsKey(kv.Key)) { outputFiles[kv.Key].WriteLine(kv.Value); } } // Then we need to sort our dates (just in case) var sortedDates = from date in possibleDates orderby date ascending select date; // foreach (DateTime d in sortedDates) { string writeDateLine = "" + d.Month + "/" + d.Day + "/" + d.Year; string yearStart = "" + d.Month + "/" + d.Day + "/" + d.Year; industryOutputs = new Dictionary <string, string>(); foreach (KeyValuePair <string, Dictionary <DateTime, BLSValue> > kv in bulkDataHolder) { writeDateLine = writeDateLine + ","; if (kv.Value.ContainsKey(d)) { writeDateLine = writeDateLine + kv.Value[d].Value; } string[] indParse = kv.Key.Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries); string industryLine = ""; if (kv.Value.ContainsKey(d)) { industryLine = "," + kv.Value[d].Value; } else { industryLine = ","; } if (industryOutputs.ContainsKey(indParse[1])) { industryOutputs[indParse[1]] = industryOutputs[indParse[1]] + industryLine; } else { industryOutputs.Add(indParse[1], yearStart + industryLine); } } foreach (KeyValuePair <string, string> kvp in industryOutputs) { if (outputFiles.ContainsKey(kvp.Key)) { outputFiles[kvp.Key].WriteLine(kvp.Value); } } outputFile.WriteLine(writeDateLine); } foreach (KeyValuePair <string, StreamWriter> kvp in outputFiles) { kvp.Value.Close(); } outputFile.Close(); } } }