CntrlComparison parsing
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using LeafWeb.Core.Models;
|
||||
using LeafWeb.Core.Utility;
|
||||
|
||||
namespace LeafWeb.Core.Parsers
|
||||
{
|
||||
public class CntrlComparisonParser : CsvParserBase
|
||||
{
|
||||
public CntrlComparisonParser(FileSystemInfo csvFile) : base(csvFile)
|
||||
{
|
||||
}
|
||||
|
||||
public CntrlComparison[] Parse()
|
||||
{
|
||||
var fittingTitles = GetNextCsvRowValues();
|
||||
if (fittingTitles == null)
|
||||
throw new ParseException($"Could not read data header row on line number {CsvReader.Row}");
|
||||
var fitUnits = GetNextCsvRowValues();
|
||||
if (fitUnits == null)
|
||||
throw new ParseException($"Could not read data units row on line number {CsvReader.Row}");
|
||||
|
||||
return ParseCntrlComparisonSet(fittingTitles).ToArray();
|
||||
}
|
||||
|
||||
private IEnumerable<CntrlComparison> ParseCntrlComparisonSet(string[] fittingTitles)
|
||||
{
|
||||
var endOfFile = false;
|
||||
while (!endOfFile)
|
||||
{
|
||||
// First Section
|
||||
var fittingValues = new List<string[]>();
|
||||
string[] photosyntheticTitles;
|
||||
while (true)
|
||||
{
|
||||
var values = GetNextCsvRowValues();
|
||||
if (CsvReader.IsRecordEmpty())
|
||||
throw new ParseException($"Encountered empty line while readding fitting info on line {CsvReader.Row}");
|
||||
if (values == null) // end of file
|
||||
yield break;
|
||||
if (ParsedObjectFactory<CntrlComparisonPhotosyntheticInfo>.IsPropertiesTitlesMatch(values))
|
||||
{
|
||||
photosyntheticTitles = values;
|
||||
break;
|
||||
}
|
||||
fittingValues.Add(values);
|
||||
}
|
||||
|
||||
var photosyntheticValues = new List<string[]>();
|
||||
while (true)
|
||||
{
|
||||
var values = GetNextCsvRowValues();
|
||||
if (CsvReader.IsRecordEmpty()) // end of set
|
||||
break;
|
||||
if (values == null)
|
||||
{
|
||||
endOfFile = true;
|
||||
break;
|
||||
}
|
||||
photosyntheticValues.Add(values);
|
||||
}
|
||||
|
||||
var fittingInfo =
|
||||
ParsedObjectFactory<CntrlComparisonFittingInfo>
|
||||
.Create(fittingTitles, fittingValues.ToArray());
|
||||
var photosyntheticInfo =
|
||||
ParsedObjectFactory<CntrlComparisonPhotosyntheticInfo>
|
||||
.Create(photosyntheticTitles, photosyntheticValues.ToArray());
|
||||
|
||||
yield return new CntrlComparison
|
||||
{
|
||||
FittingInfo = fittingInfo,
|
||||
PhotosyntheticInfo = photosyntheticInfo
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using CsvHelper;
|
||||
using CsvHelper.Configuration;
|
||||
|
||||
namespace LeafWeb.Core.Parsers
|
||||
{
|
||||
public class CsvParserBase : IDisposable
|
||||
{
|
||||
protected readonly FileSystemInfo CsvFile;
|
||||
private readonly StreamReader _reader;
|
||||
protected readonly CsvReader CsvReader;
|
||||
|
||||
protected CsvParserBase(FileSystemInfo csvFile)
|
||||
{
|
||||
CsvFile = csvFile;
|
||||
if (!csvFile.Exists)
|
||||
throw new FileNotFoundException($"Cannot find file '{csvFile.Name}'");
|
||||
|
||||
_reader = File.OpenText(csvFile.FullName);
|
||||
var csvConfiguration = new CsvConfiguration { HasHeaderRecord = false, IgnoreBlankLines = false};
|
||||
CsvReader = new CsvReader(_reader, csvConfiguration);
|
||||
}
|
||||
|
||||
protected string[] GetNextCsvRowValues()
|
||||
{
|
||||
// get values from row
|
||||
if (!CsvReader.Read())
|
||||
return null;
|
||||
|
||||
// put all the values from this row into an array
|
||||
var values = new List<string>();
|
||||
var index = 0;
|
||||
string value;
|
||||
while (CsvReader.TryGetField(index, out value))
|
||||
{
|
||||
values.Add(value);
|
||||
index++;
|
||||
}
|
||||
return values.ToArray();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_reader.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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