diff --git a/API/API.csproj b/API/API.csproj index cf610a4..ec2e97f 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -13,7 +13,7 @@ - + diff --git a/API/Services/Extensions.cs b/API/Services/Extensions.cs index b422205..b03e118 100644 --- a/API/Services/Extensions.cs +++ b/API/Services/Extensions.cs @@ -179,22 +179,42 @@ public static Graph ToGraph(this Models.Soil soil, double[] thickness = null, double[] sw = null, bool swIsGrav = false, string cropName = null) { - IReadOnlyList cll = null; - double pawc = double.NaN; + IReadOnlyList ll; + IReadOnlyList xf; if (cropName != null) { var crop = soil.Crop(cropName); - cll = crop.LL; - pawc = PAWC(crop, soil.Water.DUL).Multiply(soil.Water.Thickness).Sum(); + ll = crop.LL; + xf = crop.XF; + } + else + { + ll = soil.Water.LL15; + xf = Enumerable.Repeat(1.0, ll.Count).ToArray(); } - if (swIsGrav) + double paw = double.NaN; + if (sw != null) { - var bdMapped = soil.Water.BD.MappedTo(soil.Water.Thickness, thickness); - sw = sw.ConvertGravimetricToVolumetric(bdMapped).ToArray(); + if (swIsGrav) + { + var bdMapped = soil.Water.BD.MappedTo(soil.Water.Thickness, thickness); + sw = sw.ConvertGravimetricToVolumetric(bdMapped).ToArray(); + } + // Map water to bottom of profile. + sw = Soil.SWMappedTo(sw, thickness, soil.Water.Thickness, ll); + + // Calculate plant available water. + paw = SoilUtilities.CalcPAWC(soil.Water.Thickness, ll, sw, xf) + .Multiply(soil.Water.Thickness) + .Sum(); } + // Calculate PAWC for graph. + double pawc = SoilUtilities.CalcPAWC(soil.Water.Thickness, ll, soil.Water.DUL, Enumerable.Repeat(1.0, ll.Count).ToArray()) + .Multiply(soil.Water.Thickness).Sum(); + return SoilGraph.Create(soil.Name, soil.Water.Thickness.ToMidPoints(), soil.Water.AirDry, soil.Water.LL15, - soil.Water.DUL, soil.Water.SAT, cll, cropName, pawc, thickness?.ToMidPoints(), sw); + soil.Water.DUL, soil.Water.SAT, ll, cropName, pawc, paw, thickness?.ToMidPoints(), sw); } /// Get the crop lower limit (volumetric) for a soil. diff --git a/API/Services/SoilServices.cs b/API/Services/SoilServices.cs index af83913..db8ddb9 100644 --- a/API/Services/SoilServices.cs +++ b/API/Services/SoilServices.cs @@ -165,6 +165,7 @@ public static double PAW(SoilDbContext context, string fullName, string cropName .Include(s => s.SoilWater) .Include(s => s.Analysis) .First(); + IReadOnlyList ll = null; IReadOnlyList xf = null; if (cropName != null) @@ -197,7 +198,7 @@ public static double PAW(SoilDbContext context, string fullName, string cropName /// The values to map. /// The thickness to map to. /// The mapped values. - private static double[] SWMappedTo(this IReadOnlyList values, IReadOnlyList fromThickness, IReadOnlyList toThickness, IReadOnlyList ll) + public static double[] SWMappedTo(this IReadOnlyList values, IReadOnlyList fromThickness, IReadOnlyList toThickness, IReadOnlyList ll) { List sw = values.ToList(); List thickness = fromThickness.ToList();