Files
LeafWeb/Core/Parsers/LeafInputCsvParser.cs
T
poprhythm 279551e9be Rename Core Models to Core Entities
modified:   Core.Tests/Charter/CurveDataConverterTests.cs
	modified:   Core.Tests/Parsers/CntrlComparisonParserTests.cs
	modified:   Core.Tests/Parsers/LeafInputCsvParserTests.cs
	modified:   Core.Tests/Remote/PiscalSshClientTests.cs
	modified:   Core/Charter/CurveDataConverter.cs
	modified:   Core/Core.csproj
	modified:   Core/DAL/DataService.cs
	modified:   Core/DAL/LeafWebContext.cs
	modified:   Core/DAL/LeafWebInitializer.cs
	renamed:    Core/Models/CntrlComparison.cs -> Core/Entities/CntrlComparison.cs
	renamed:    Core/Models/CntrlComparisonFittingInfo.cs -> Core/Entities/CntrlComparisonFittingInfo.cs
	renamed:    Core/Models/CntrlComparisonPhotosyntheticInfo.cs -> Core/Entities/CntrlComparisonPhotosyntheticInfo.cs
	renamed:    Core/Models/FluxnetSite.cs -> Core/Entities/FluxnetSite.cs
	renamed:    Core/Models/LeafInput.cs -> Core/Entities/LeafInput.cs
	renamed:    Core/Models/LeafInputData.cs -> Core/Entities/LeafInputData.cs
	renamed:    Core/Models/LeafInputFile.cs -> Core/Entities/LeafInputFile.cs
	renamed:    Core/Models/LeafInputInfo.cs -> Core/Entities/LeafInputInfo.cs
	renamed:    Core/Models/LeafInputPhotosynthetic.cs -> Core/Entities/LeafInputPhotosynthetic.cs
	renamed:    Core/Models/LeafInputSite.cs -> Core/Entities/LeafInputSite.cs
	renamed:    Core/Models/LeafInputStatus.cs -> Core/Entities/LeafInputStatus.cs
	renamed:    Core/Models/PhotosynthesisType.cs -> Core/Entities/PhotosynthesisType.cs
	modified:   Core/Parsers/CntrlComparisonParser.cs
	modified:   Core/Parsers/FluxnetSiteCsvParser.cs
	modified:   Core/Parsers/LeafInputCsvParser.cs
	modified:   Web/Controllers/LeafCharterController.cs
	modified:   Web/Controllers/LeafInputController.cs
	modified:   Web/ViewModels/LeafInput/CreateViewModel.cs
2016-02-09 15:27:16 -05:00

124 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using CsvHelper;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
namespace LeafWeb.Core.Parsers
{
public class LeafInputCsvParser : CsvParserBase
{
public LeafInputCsvParser(FileSystemInfo csvFile) : base(csvFile)
{
}
public LeafInputInfo Parse()
{
// First 10 lines
var leafInput = ParseLeafInput();
// filename will be stored in LeafInputFile
//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 LeafInputInfo 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<LeafInputInfo>.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<LeafInputInfo> leafInputs)
{
using (var textWriter = new StreamWriter(filename))
{
var csv = new CsvWriter(textWriter);
csv.WriteRecords(leafInputs);
}
}
}
}