Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.*" />
<PackageReference Include="SharpKml.Core" Version="6.1.*" />
<PackageReference Include="AutoMapper" Version="14.0.0" />
<PackageReference Include="APSIM.Graphs" Version="8.0.0" />
<PackageReference Include="APSIM.Graphs" Version="8.1.0" />
<PackageReference Include="APSIM.Soils" Version="8.0.0" />
<PackageReference Include="APSIM.Numerics" Version="8.2.0" />
</ItemGroup>
Expand Down
36 changes: 28 additions & 8 deletions API/Services/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> cll = null;
double pawc = double.NaN;
IReadOnlyList<double> ll;
IReadOnlyList<double> 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);
}

/// <summary>Get the crop lower limit (volumetric) for a soil.</summary>
Expand Down
3 changes: 2 additions & 1 deletion API/Services/SoilServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public static double PAW(SoilDbContext context, string fullName, string cropName
.Include(s => s.SoilWater)
.Include(s => s.Analysis)
.First();

IReadOnlyList<double> ll = null;
IReadOnlyList<double> xf = null;
if (cropName != null)
Expand Down Expand Up @@ -197,7 +198,7 @@ public static double PAW(SoilDbContext context, string fullName, string cropName
/// <param name="values">The values to map.</param>
/// <param name="thickness">The thickness to map to.</param>
/// <returns>The mapped values.</returns>
private static double[] SWMappedTo(this IReadOnlyList<double> values, IReadOnlyList<double> fromThickness, IReadOnlyList<double> toThickness, IReadOnlyList<double> ll)
public static double[] SWMappedTo(this IReadOnlyList<double> values, IReadOnlyList<double> fromThickness, IReadOnlyList<double> toThickness, IReadOnlyList<double> ll)
{
List<double> sw = values.ToList();
List<double> thickness = fromThickness.ToList();
Expand Down