private static InputPayload GetPayloadFromLine(string line) { var fields = GetFields(line); var payload = new InputPayload() { Pen = fields[0], TRT = fields[1], Rep = fields[2], Ration = fields[3], ID = fields[4], Weights = fields.Skip(5).Select(s => !String.IsNullOrWhiteSpace(s) ? Decimal.Parse(s) : 0).ToArray() }; return(payload); }
private static List <OutputPayload> BuildOutputs(InputPayload line, DateTime[] dates) { List <OutputPayload> outputs = new List <OutputPayload>(); for (int i = 0; i < line.Weights.Length; i++) { var o = new OutputPayload() { Pen = line.Pen, TRT = line.TRT, Rep = line.Rep, Ration = line.Ration, ID = line.ID, Date = dates[i], Weight = line.Weights[i] }; outputs.Add(o); } return(outputs); }
private static string[][] BuildOutput(InputPayload line, DateTime[] dates) { // only process files with 1 or more dates if (dates.Length == 0) { return(null); } List <OutputPayload> outputs = BuildOutputs(line, dates); if (dates.Length > 1) { // check if the first two dates are contiguous if (AreDatesContiguous(dates[0], dates[1])) { // remove the first output row outputs.RemoveAt(0); // set the weight value of the first output to the first two weights in the input outputs[0].Weight = line.Weights.Take(2).Average(); } // check if the last two dates are contiguous if (AreDatesContiguous(dates[dates.Length - 2], dates[dates.Length - 1])) { // remove the 2nd to last output row outputs.RemoveAt(outputs.Count - 2); // set the weight value of the last output to the last two weights in the input outputs[outputs.Count - 1].Weight = line.Weights.Skip(line.Weights.Length - 2).Average(); } } string[][] results = new string[outputs.Count][]; for (int i = 0; i < outputs.Count; i++) { if (results[i] == null) { results[i] = new string[11]; } // get previous values DateTime?prevDate = i != 0 ? outputs[i - 1].Date : (DateTime?)null; int? prevRunningDays = i != 0 ? outputs[i - 1].RunningDays : (int?)null; decimal? prevWeight = i != 0 ? outputs[i - 1].Weight : (decimal?)null; // set Period, Running Days, and ADG outputs[i].Period = i.ToString(); outputs[i].DaysInPeriod = prevDate.HasValue ? (outputs[i].Date - prevDate).Value.Days : 1; outputs[i].RunningDays = prevRunningDays.HasValue ? prevRunningDays.Value + outputs[i].DaysInPeriod : 1; outputs[i].ADG = prevWeight.HasValue ? (outputs[i].Weight - prevWeight.Value) / outputs[i].DaysInPeriod : 0; // 0) Pen // 1) Treatment // 2) Replication // 3) Ration // 4) Tag // 5) Date // 6) WT // 7) Period // 8) DaysInPeriod // 9) RunningDays // 10) ADG results[i][0] = line.Pen; results[i][1] = line.TRT; results[i][2] = line.Rep; results[i][3] = line.Ration; results[i][4] = line.ID; results[i][5] = outputs[i].Date.ToShortDateString(); results[i][6] = outputs[i].Weight.ToString(); results[i][7] = outputs[i].Period; results[i][8] = outputs[i].DaysInPeriod.ToString(); results[i][9] = outputs[i].RunningDays.ToString(); results[i][10] = outputs[i].ADG.ToString(); } return(results); }