CntrlComparison parsing
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using CsvHelper;
|
||||
using LeafWeb.Core.Models;
|
||||
using LeafWeb.Core.Utility;
|
||||
|
||||
namespace LeafWeb.Core.Parsers
|
||||
{
|
||||
public class LeafInputCsvParser : CsvParserBase
|
||||
{
|
||||
public LeafInputCsvParser(FileSystemInfo csvFile) : base(csvFile)
|
||||
{
|
||||
}
|
||||
|
||||
public LeafInput Parse()
|
||||
{
|
||||
// First 10 lines
|
||||
var leafInput = ParseLeafInput();
|
||||
leafInput.FileName = CsvFile.Name;
|
||||
|
||||
// Next 3 (Header, Units, and Values)
|
||||
leafInput.Site = ParseLeafInputSite();
|
||||
|
||||
// Next 3 (Header, Units, and Values)
|
||||
leafInput.Photosynthetic = ParseLeafInputPhotosynthetic();
|
||||
|
||||
// Remaining lines (Header, Units, and Data)
|
||||
leafInput.Data = ParseLeafInputData();
|
||||
|
||||
return leafInput;
|
||||
}
|
||||
|
||||
private LeafInput ParseLeafInput()
|
||||
{
|
||||
var items = new List<string>();
|
||||
for (var i = 0; i < 10; i++)
|
||||
{
|
||||
if (!CsvReader.Read())
|
||||
throw new ParseException("Could not read line number " + CsvReader.Row);
|
||||
|
||||
string field;
|
||||
if (!CsvReader.TryGetField(0, out field))
|
||||
throw new ParseException("Could not read first field on line number " + CsvReader.Row);
|
||||
items.Add(field);
|
||||
}
|
||||
return ParsedObjectFactory<LeafInput>.Create(items.ToArray());
|
||||
}
|
||||
|
||||
private LeafInputSite ParseLeafInputSite()
|
||||
{
|
||||
var titles = GetNextCsvRowValues();
|
||||
if (titles == null)
|
||||
throw new ParseException("Could not read site header row on line number " + CsvReader.Row);
|
||||
var units = GetNextCsvRowValues();
|
||||
if (units == null)
|
||||
throw new ParseException("Could not read site units row on line number " + CsvReader.Row);
|
||||
var values = GetNextCsvRowValues();
|
||||
if (values == null)
|
||||
throw new ParseException("Could not read site value row on line number " + CsvReader.Row);
|
||||
|
||||
var items = new List<Tuple<string, string>>();
|
||||
for (var i = 0; i < titles.Length && i < values.Length; i++)
|
||||
{
|
||||
var item = Tuple.Create(titles[i], values[i]);
|
||||
items.Add(item);
|
||||
}
|
||||
return ParsedObjectFactory<LeafInputSite>.Create(items.ToArray());
|
||||
}
|
||||
|
||||
private LeafInputPhotosynthetic ParseLeafInputPhotosynthetic()
|
||||
{
|
||||
var titles = GetNextCsvRowValues();
|
||||
if (titles == null)
|
||||
throw new ParseException("Could not read photosynthetic header row on line number " + CsvReader.Row);
|
||||
var units = GetNextCsvRowValues();
|
||||
if (units == null)
|
||||
throw new ParseException("Could not read photosynthetic units row on line number " + CsvReader.Row);
|
||||
var values = GetNextCsvRowValues();
|
||||
if (values == null)
|
||||
throw new ParseException("Could not read photosynthetic value row on line number " + CsvReader.Row);
|
||||
|
||||
var items = new List<Tuple<string, string>>();
|
||||
for (var i = 0; i < titles.Length && i < values.Length; i++)
|
||||
{
|
||||
var item = Tuple.Create(titles[i], values[i]);
|
||||
items.Add(item);
|
||||
}
|
||||
return ParsedObjectFactory<LeafInputPhotosynthetic>.Create(items.ToArray());
|
||||
}
|
||||
|
||||
private LeafInputData[] ParseLeafInputData()
|
||||
{
|
||||
var titles = GetNextCsvRowValues();
|
||||
if (titles == null)
|
||||
throw new ParseException("Could not read data header row on line number " + CsvReader.Row);
|
||||
var units = GetNextCsvRowValues();
|
||||
if (units == null)
|
||||
throw new ParseException("Could not read data units row on line number " + CsvReader.Row);
|
||||
|
||||
var valueArrays = new List<string[]>();
|
||||
while (true)
|
||||
{
|
||||
var values = GetNextCsvRowValues();
|
||||
if (values == null)
|
||||
break;
|
||||
valueArrays.Add(values);
|
||||
}
|
||||
return ParsedObjectFactory<LeafInputData>.Create(titles, valueArrays.ToArray());
|
||||
}
|
||||
|
||||
public static void ExportCsv(string filename, IEnumerable<LeafInput> leafInputs)
|
||||
{
|
||||
using (var textWriter = new StreamWriter(filename))
|
||||
{
|
||||
var csv = new CsvWriter(textWriter);
|
||||
csv.WriteRecords(leafInputs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user