Leaf Input Parsing complete

This commit is contained in:
2015-11-20 11:39:23 -05:00
parent 750b2aee7b
commit 1fb73b9f76
26 changed files with 1294 additions and 1 deletions
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
+84
View File
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{25BAED75-7E75-4D11-90D9-358472054DF6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LeafWeb.Core</RootNamespace>
<AssemblyName>Core</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CsvHelper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8c4959082be5c823, processorArchitecture=MSIL">
<HintPath>..\packages\CsvHelper.2.13.2.0\lib\net40-client\CsvHelper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Fasterflect, Version=2.1.3.0, Culture=neutral, PublicKeyToken=38d18473284c1ca7, processorArchitecture=MSIL">
<HintPath>..\packages\fasterflect.2.1.3\lib\net40\Fasterflect.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\LeafInput.cs" />
<Compile Include="Models\LeafInputData.cs" />
<Compile Include="Models\LeafInputFile.cs" />
<Compile Include="Models\LeafInputPhotosynthetic.cs" />
<Compile Include="Models\LeafInputSite.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\ParseInfoAttribute.cs" />
<Compile Include="Services\LeafInputCsvParser.cs" />
<Compile Include="Services\ParsedObjectFactory.cs" />
<Compile Include="Services\ParseException.cs" />
<Compile Include="Utility\StringExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+54
View File
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using LeafWeb.Core.Services;
namespace LeafWeb.Core.Models
{
/// <summary>
/// Descriptive information about the investigator,
/// contact information, the site,the sample leaf and its general environmental condition
/// </summary>
public class LeafInput
{
[Key]
public string LeafInputId { get; set; }
public string FileName { get; set; }
[ParseInfo(1, exampleValue: "First and last name")]
public string InvestigatorName { get; set; }
[ParseInfo(2, exampleValue: "Your email / mail addresses")]
public string ContactInformation { get; set; }
[ParseInfo(3, alternateTitle: "Site name in full", exampleValue: "Your site's identifier / name")]
public string SiteName { get; set; }
[ParseInfo(4, exampleValue: "Mixed forest / grasslands / croplands/ etc")]
public string VegetationType { get; set; }
[ParseInfo(5, exampleValue: "Soil type at your site")]
public string SoilType { get; set; }
[ParseInfo(6, exampleValue: "List of major species at the site")]
public string MajorSpecies { get; set; }
[ParseInfo(7,
alternateTitle: "Sample leaf light environment",
exampleValue: "The general light environment in which the leaf is in (e.g. heavily shaded from above)")]
public string SampleLeafLightEnv { get; set; }
[ParseInfo(8, exampleValue: "Indicate whether there is water stress at the time of sampling")]
public string WaterStressAssessment { get; set; }
[ParseInfo(9, exampleValue: "For example - Licor-6400")]
public string InstrumentUsed { get; set; }
[ParseInfo(10, exampleValue: "Any extra information you feel would be helpful to put the sampled leaf in context")]
public string ExtraInfo { get; set; }
public virtual LeafInputSite Site { get; set; }
public virtual LeafInputPhotosynthetic Photosynthetic { get; set; }
public virtual IEnumerable<LeafInputData> Data { get; set; }
}
}
+145
View File
@@ -0,0 +1,145 @@
using System.ComponentModel.DataAnnotations;
using LeafWeb.Core.Services;
namespace LeafWeb.Core.Models
{
/// <summary>
/// A/Ci data for a variety of analyses, including photosynthesis, stomatal conductance, internal conductance, water use efficiency.
/// </summary>
public class LeafInputData
{
public int ListOrder { get; set; }
/// <summary>the data point No.</summary>
[ParseInfo(1, units: "no unit")]
public double? Obs { get; set; }
/// <summary>clock time</summary>
[StringLength(8)]
[ParseInfo(2, units: "hh:mm:ss")]
public string HHMMSS { get; set; }
/// <summary>number of seconds since file opened</summary>
[ParseInfo(3, units: "seconds")]
public double? FTime { get; set; }
/// <summary>photosynthetic rate</summary>
[ParseInfo(4, units: "umol/m2/s")]
public double? Photo { get; set; }
/// <summary>adjusted photosynthetic rate (leakage corrected)</summary>
/// <remarks>required</remarks>
[ParseInfo(5, alternateTitle: "!AdjPhoto", units: "umol/m2/s")]
public double? AdjPhoto { get; set; }
/// <summary>stomatal conductance</summary>
/// <remarks>required</remarks>
[ParseInfo(6, alternateTitle: "!StomCond", units: "mol/m2/s")]
public double? StomCond { get; set; }
/// <summary>intercellular CO2 concentration (umol CO2 / mol moist air)</summary>
[ParseInfo(7, alternateTitle: "!Ci", units: "umol/mol")]
public double? Ci { get; set; }
/// <summary>transpiration rate</summary>
/// <remarks>must provide</remarks>
[ParseInfo(8, alternateTitle: "!Trmmol", units:"mmol/m2/s")]
public double? Trmmol { get; set; }
/// <summary>water vapor pressure deficit based on leaf temperature, must provide</summary>
[ParseInfo(9, alternateTitle: "!VpdL", units: "KPa")]
public double? VpdL { get; set; }
/// <summary>leaf area</summary>
[ParseInfo(10, units:"cm2")]
public double? Area { get; set; }
/// <summary>estimate of the ratio of stomatal conductances of one side of the leaf to the other</summary>
[ParseInfo(11, units: "NA")]
public double? StmRat { get; set; }
/// <summary>total boundary layer conductance</summary>
[ParseInfo(12, units: "mol/m2/s")]
public double? BLCond { get; set; }
/// <summary>temperature in sample cell</summary>
[ParseInfo(13, units: "oC")]
public double? Tair { get; set; }
/// <summary>temperature of leaf thermocouple</summary>
/// <remarks>required</remarks>
[ParseInfo(14, alternateTitle: "!Tleaf", units: "oC")]
public double? Tleaf { get; set; }
/// <summary>IRGA Block temperature</summary>
[ParseInfo(15, units: "oC")]
public double? TBlk { get; set; }
/// <summary>reference CO2 concentration</summary>
[ParseInfo(16, units: "umol/mol")]
public double? CO2R { get; set; }
/// <summary>Sample CO2 concentration</summary>
[ParseInfo(17, units: "umol/mol")]
public double? CO2S { get; set; }
/// <summary>reference cell water vapor concentration</summary>
[ParseInfo(18, units: "mmol/mol")]
public double? H2OR { get; set; }
/// <summary>sample cell water vapor concentration</summary>
[ParseInfo(19, units: "mmol/mol")]
public double? H2OS { get; set; }
/// <summary>(%) Reference cell relative humidity</summary>
[ParseInfo(20)]
public double? RH_R { get; set; }
/// <summary>(%) sample cell relative humidity</summary>
[ParseInfo(21)]
public double? RH_S { get; set; }
/// <summary>(umol/s) molar flow rate of air entering the leaf chamber</summary>
[ParseInfo(22)]
public double? Flow { get; set; }
/// <summary> PAR measured by the in-chamber quantum sensor</summary>
/// <remarks>required</remarks>
[ParseInfo(23, alternateTitle: "!PARi", units: "umol/m2/s")]
public double? PARi { get; set; }
/// <summary>PAR measured by the external quantum sensor</summary>
[ParseInfo(24, units: "umol/m2/s")]
public double? PARo { get; set; }
/// <summary>atmospheric pressure</summary>
[ParseInfo(25, units: "KPa")]
public double? Press { get; set; }
/// <summary>sample CO2 offset</summary>
[ParseInfo(26, units: "umol/mol")]
public double? CsMch { get; set; }
/// <summary>sample H2O offset</summary>
[ParseInfo(27, units: "mmol/mol")]
public double? HsMch { get; set; }
/// <summary>a stability indicator as a decimal value</summary>
[ParseInfo(28, units: "NA")]
public double? StableF { get; set; }
/// <summary>status flag</summary>
[ParseInfo(29, units: "NA")]
public string Status { get; set; }
/// <summary>DeltaF/Fm, the fraction of absorbed PSII photons that are used in photochemistry</summary>
[ParseInfo(30, units: "NA")]
public double? PhiPS2 { get; set; }
/// <summary>atmospheric O2 partial pressure</summary>
[ParseInfo(31, units: "Pa")]
public double? OxygenPress { get; set; }
public virtual LeafInput LeafInput { get; set; }
}
}
+11
View File
@@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace LeafWeb.Core.Models
{
public class LeafInputFile
{
[Key]
public string LeafInputId { get; set; }
public byte[] File { get; set; }
}
}
+41
View File
@@ -0,0 +1,41 @@
using LeafWeb.Core.Services;
namespace LeafWeb.Core.Models
{
/// <summary>
/// Photosynthetic parameters of Leaf Input
/// </summary>
public class LeafInputPhotosynthetic
{
/// <summary>chloroplastic CO2 photocompensation point</summary>
/// <remarks>must be positive</remarks>
[ParseInfo(1, alternateTitle:"Gamma*", units: "Pa")]
public double GammaStar { get; set; }
/// <summary>Michaelis-Menten constant for RuBP carboxylation</summary>
/// <remarks>must be positive</remarks>
[ParseInfo(2, units: "Pa")]
public double Kc { get; set; }
/// <summary>Michaelis-Menten constant for RuBP oxygenation</summary>
/// <remarks>must be positive</remarks>
[ParseInfo(3, units: "Pa")]
public double Ko { get; set; }
/// <summary>the fraction of glycolate carbon not returned to the chloroplast</summary>
/// <remarks>must be between 0~1 if provided</remarks>
[ParseInfo(4)]
public double Alpha { get; set; }
/// <summary>dark respiration</summary>
/// <remarks>must be positive if provided</remarks>
[ParseInfo(5, units: "umol/m2/s")]
public double Rd { get; set; }
/// <summary>internal (also known as mesophyll) conductance</summary>
[ParseInfo(6, units: "umol/m2/s/Pa")]
public double gi { get; set; }
public virtual LeafInput LeafInput { get; set; }
}
}
+96
View File
@@ -0,0 +1,96 @@
using LeafWeb.Core.Services;
namespace LeafWeb.Core.Models
{
/// <summary>
/// Contains info about the site such as elevation, canopy height, site ID, etc
/// </summary>
public class LeafInputSite
{
/// <summary>Site identifier</summary>
/// <remarks>do not leave blank between letters</remarks>
[ParseInfo(1)]
public string SiteID { get; set; }
/// <summary>Site latitude, northern hermisphere positive</summary>
[ParseInfo(2, units:"degrees")]
public double? Latitude { get; set; }
/// <summary>Site longitude, east positive</summary>
[ParseInfo(3, units: "degrees")]
public double? Longitude { get; set; }
/// <summary>site elevation</summary>
[ParseInfo(4, units: "m")]
public double? Elevation { get; set; }
/// <summary>the year when the A/Ci data is taken</summary>
[ParseInfo(5, units: "year")]
public int? SampleYear { get; set; }
/// <summary>the day of year (since 1 Jan) when the A/Ci data is taken</summary>
[ParseInfo(6, units: "day")]
public int? SampleDayOfYear { get; set; }
/// <summary>the approximate start day (since 1 Jan) of growing season</summary>
[ParseInfo(7, units: "day")]
public int? GrowSeasonStart { get; set; }
/// <summary>the approximate end day (since 1 Jan) of growing season</summary>
[ParseInfo(8, units: "day")]
public int? GrowSeasonEnd { get; set; }
/// <summary>stand age since the last disturbance</summary>
[ParseInfo(9, units: "year")]
public double? StandAge { get; set; }
/// <summary>the height of the canopy</summary>
[ParseInfo(10, units: "m")]
public double? CanopyHeight { get; set; }
/// <summary>the leaf area index in the middle of growing season</summary>
[ParseInfo(11, units: "m2/m2")]
public double? LeafAreaIndex { get; set; }
/// <summary>the species of the leaf sample</summary>
/// <remarks>don't leave blank between letters</remarks>
[ParseInfo(12, units: "dimensionless")]
public string SpeciesSampled { get; set; }
/// <summary>the average time interval between two consecutive A/Ci data points</summary>
[ParseInfo(13, alternateTitle: "AveTimeResolution", units: "minutes")]
public double? AverageTimeResolution { get; set; }
/// <summary>the height at which the leaf is located</summary>
[ParseInfo(14, units: "m")]
public double? SampleHeight { get; set; }
/// <summary>the age of the leaf</summary>
[ParseInfo(15, units: "day")]
public int? LeafAge { get; set; }
/// <summary>specific leaf area of the sample</summary>
[ParseInfo(16, units: "cm2/g")]
public double? SpecificLeafArea { get; set; }
/// <summary>dry leaf nitrogen content of the sample</summary>
[ParseInfo(17, units: "%")]
public double? LfNitrogenContent { get; set; }
/// <summary>dry leaf carbon content of the sample</summary>
[ParseInfo(18, units: "%")]
public double? LfCarbonContent { get; set; }
/// <summary>dry leaf phosphorus content of the sample</summary>
[ParseInfo(19, units: "%")]
public double? LfPhosphContent { get; set; }
// WoodPorosity
// SapWoodDensity
// DataType,TissueArea,TissueMass,Fo'_or_Fo,Fm'_or_Fm,Fs,MeasLight
public virtual LeafInput LeafInput { get; set; }
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Core")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Core")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("678132a7-2bfc-426b-a14a-761822c06bd5")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+155
View File
@@ -0,0 +1,155 @@
using System;
using System.Collections.Generic;
using System.IO;
using CsvHelper;
using CsvHelper.Configuration;
using LeafWeb.Core.Models;
namespace LeafWeb.Core.Services
{
public class LeafInputCsvParser : IDisposable
{
private readonly FileSystemInfo _csvFile;
private readonly StreamReader _reader;
private readonly CsvReader _csv;
public LeafInputCsvParser(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};
_csv = new CsvReader(_reader, csvConfiguration);
}
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 (!_csv.Read())
throw new ParseException("Could not read line number " + _csv.Row);
string field;
if (!_csv.TryGetField(0, out field))
throw new ParseException("Could not read first field on line number " + _csv.Row);
items.Add(field);
}
return ParsedObjectFactory<LeafInput>.Create(items.ToArray());
}
private string[] GetNextCsvRowValues()
{
// get values from row
if (!_csv.Read())
return null;
// put all the values from this row into an array
var values = new List<string>();
var index = 0;
string value;
while (_csv.TryGetField(index, out value))
{
values.Add(value);
index++;
}
return values.ToArray();
}
private LeafInputSite ParseLeafInputSite()
{
var titles = GetNextCsvRowValues();
if (titles == null)
throw new ParseException("Could not read site header row on line number " + _csv.Row);
var units = GetNextCsvRowValues();
if (units == null)
throw new ParseException("Could not read site units row on line number " + _csv.Row);
var values = GetNextCsvRowValues();
if (values == null)
throw new ParseException("Could not read site value row on line number " + _csv.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 " + _csv.Row);
var units = GetNextCsvRowValues();
if (units == null)
throw new ParseException("Could not read photosynthetic units row on line number " + _csv.Row);
var values = GetNextCsvRowValues();
if (values == null)
throw new ParseException("Could not read photosynthetic value row on line number " + _csv.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 " + _csv.Row);
var units = GetNextCsvRowValues();
if (units == null)
throw new ParseException("Could not read data units row on line number " + _csv.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 void Dispose()
{
_reader.Dispose();
}
public static void ExportCsv(string filename, IEnumerable<LeafInput> leafInputs)
{
using (var textWriter = new StreamWriter(filename))
{
var csv = new CsvWriter(textWriter);
csv.WriteRecords(leafInputs);
}
}
}
}
+12
View File
@@ -0,0 +1,12 @@
using System;
namespace LeafWeb.Core.Services
{
public class ParseException : Exception
{
public ParseException(string message)
: base(message)
{
}
}
}
+46
View File
@@ -0,0 +1,46 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using LeafWeb.Core.Utility;
namespace LeafWeb.Core.Services
{
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class ParseInfoAttribute : Attribute
{
public int Position { get; private set; }
public string Units { get; private set; }
public string Title { get; private set; }
public string AlterateTitle { get; private set; }
public string ExampleValue { get; private set; }
public string[] FieldNames
{
get
{
var fieldNames = new[] {Title, Title.SplitCamelCase()};
if (!string.IsNullOrEmpty(AlterateTitle))
fieldNames = fieldNames.Concat(new[] { AlterateTitle }).ToArray();
return fieldNames;
}
}
public ParseInfoAttribute(int position, [CallerMemberName]string title = null, string units = null, string alternateTitle = null, string exampleValue = null)
{
AlterateTitle = alternateTitle;
ExampleValue = exampleValue;
Title = title;
Position = position;
Units = units;
}
public bool IsTitleMatch(string title)
{
return FieldNames.Any(t => string.Compare(title, t, StringComparison.InvariantCultureIgnoreCase) == 0);
}
public bool IsPositionMatch(int index)
{
return Position == index;
}
}
}
+159
View File
@@ -0,0 +1,159 @@
using System;
using System.Linq;
using System.Reflection;
using Fasterflect;
namespace LeafWeb.Core.Services
{
public static class ParsedObjectFactory<T> where T : new()
{
private static PropertyInfo[] GetProperties()
{
var propertyInfos = typeof(T).Properties();
return propertyInfos.Where(p => p.HasAttribute<ParseInfoAttribute>()).ToArray();
}
/// <summary>
/// Create an object type T filling properties from the given title values
/// </summary>
/// <param name="titleValues">Colon separated title: values</param>
public static T Create(string[] titleValues)
{
var properties = GetProperties();
var obj = new T();
// take each of the
for (var index = 0; index < titleValues.Length; index++)
{
PropertyInfo property = null;
string value = null;
var lineNumber = index + 1;
var row = titleValues[index];
var split = row.Split(':');
if (split.Length > 1)
{
// handles case for "Title : Value"
var title = split[0].Trim();
value = split[1].Trim();
property =
properties
.FirstOrDefault(p => p.Attribute<ParseInfoAttribute>().IsTitleMatch(title));
}
if (property == null)
{
// handles case for row number, i.e. "Value"
value = row.Trim();
property =
properties
.FirstOrDefault(p => p.Attribute<ParseInfoAttribute>().IsPositionMatch(lineNumber));
}
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
throw new ParseException(string.Format("Cannot convert value '{0}' for {1} at line number {2}", value, property.Name, lineNumber));
property.Set(obj, convertedVal);
}
}
return obj;
}
public static T[] Create(string[] titles, string[][] valueArrays)
{
var properties = GetProperties();
var objs = new T[valueArrays.Length];
for (var vIndex = 0; vIndex < valueArrays.Length; vIndex++)
{
var obj = new T();
var values = valueArrays[vIndex];
for (var tIndex = 0; tIndex < titles.Length; tIndex++)
{
var title = titles[tIndex];
var value = values[tIndex];
var position = tIndex + 1;
if (IsMissingValue(value))
continue;
var property = MatchProperty(properties, title, position);
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
throw new ParseException(string.Format("Cannot convert value '{0}' for {1} in position {2}", value, property.Name, position));
property.Set(obj, convertedVal);
}
}
objs[vIndex] = obj;
}
return objs;
}
public static T Create(Tuple<string, string>[] titleValues)
{
var properties = GetProperties();
var obj = new T();
for (var index = 0; index < titleValues.Length; index++)
{
var item = titleValues[index];
var position = index + 1;
var title = item.Item1.Trim();
var value = item.Item2.Trim();
if (IsMissingValue(value))
continue;
var property = MatchProperty(properties, title, position);
if (property != null)
{
object convertedVal;
if (!TryConvertValue(property, value, out convertedVal))
throw new ParseException(string.Format("Cannot convert value '{0}' for {1} in position {2}", value, property.Name, position));
property.Set(obj, convertedVal);
}
}
return obj;
}
private static bool IsMissingValue(string value)
{
return string.IsNullOrEmpty(value) || value == "NA" || value == "-9999";
}
private static PropertyInfo MatchProperty(PropertyInfo[] properties, string title, int position)
{
var property =
properties
.FirstOrDefault(p => p.Attribute<ParseInfoAttribute>().IsTitleMatch(title));
if (property == null)
{
property =
properties
.FirstOrDefault(p => p.Attribute<ParseInfoAttribute>().IsPositionMatch(position));
}
return property;
}
private static bool TryConvertValue(PropertyInfo property, object value, out object convertedValue)
{
try
{
// http://stackoverflow.com/questions/3531318/convert-changetype-fails-on-nullable-types
var t = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
convertedValue = Convert.ChangeType(value, t);
}
catch (Exception)
{
convertedValue = null;
return false;
}
return true;
}
}
}
+17
View File
@@ -0,0 +1,17 @@
using System;
using System.Globalization;
using System.Linq;
namespace LeafWeb.Core.Utility
{
public static class StringExtensions
{
public static string SplitCamelCase(this string str)
{
return str.Aggregate(
String.Empty,
(current, c) =>
current + (Char.IsUpper(c) && current.Length > 0 ? " " + c : c.ToString(CultureInfo.InvariantCulture)));
}
}
}
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CsvHelper" version="2.13.2.0" targetFramework="net45" />
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="fasterflect" version="2.1.3" targetFramework="net45" />
</packages>