Skip to content

Commit

Permalink
Prepare stubble grazing, unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holos-aafc committed Apr 12, 2024
1 parent 5170534 commit a46b098
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 8 deletions.
102 changes: 102 additions & 0 deletions H.Core.Test/Calculators/Nitrogen/N2OEmissionFactorCalculatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,108 @@ public void GetTotalDigestateVolumeAppliedFromLivestockAndImportsInYearReturnsZe
Assert.AreEqual(0, result);
}

[TestMethod]
public void GetTotalManureVolumeAppliedFromLivestockAndImportsInYearReturnsZero()
{
_viewItem.Area = 50;
_viewItem.ManureApplicationViewItems.Clear();

var result = _sut.GetTotalManureVolumeAppliedFromLivestockAndImportsInYear(_viewItem, _farm);

Assert.AreEqual(0, result);
}

[TestMethod]
public void GetTotalManureVolumeAppliedFromLivestockAndImportsInYear()
{
_viewItem.Area = 50;
_viewItem.ManureApplicationViewItems.Clear();

_viewItem.ManureApplicationViewItems.Add(new ManureApplicationViewItem() { ManureLocationSourceType = ManureLocationSourceType.Livestock, DateOfApplication = DateTime.Now, AmountOfManureAppliedPerHectare = 100 });
_viewItem.ManureApplicationViewItems.Add(new ManureApplicationViewItem() { ManureLocationSourceType = ManureLocationSourceType.Imported, DateOfApplication = DateTime.Now, AmountOfManureAppliedPerHectare = 100 });

var result = _sut.GetTotalManureVolumeAppliedFromLivestockAndImportsInYear(_viewItem, _farm);

Assert.AreEqual((50 * 100) * 2, result);
}

[TestMethod]
public void GetTotalManureVolumeAppliedFromLivestockInYear()
{
_viewItem.Area = 50;
_viewItem.ManureApplicationViewItems.Clear();

_viewItem.ManureApplicationViewItems.Add(new ManureApplicationViewItem() { ManureLocationSourceType = ManureLocationSourceType.Livestock, DateOfApplication = DateTime.Now, AmountOfManureAppliedPerHectare = 100 });

var result = _sut.GetTotalManureVolumeAppliedFromLivestockAndImportsInYear(_viewItem, _farm);

Assert.AreEqual((50 * 100), result);
}

[TestMethod]
public void GetTotalManureVolumeAppliedFromImportsInYear()
{
_viewItem.Area = 50;
_viewItem.ManureApplicationViewItems.Clear();

_viewItem.ManureApplicationViewItems.Add(new ManureApplicationViewItem() { ManureLocationSourceType = ManureLocationSourceType.Imported, DateOfApplication = DateTime.Now, AmountOfManureAppliedPerHectare = 100 });

var result = _sut.GetTotalManureVolumeAppliedFromLivestockAndImportsInYear(_viewItem, _farm);

Assert.AreEqual((50 * 100), result);
}

[TestMethod]
public void CalculateWeightedOrganicNitrogenEmissionFactor()
{
var viewItem1 = new CropViewItem();
var viewItem2 = new CropViewItem();

var items = new List<CropViewItem>();
items.Add(viewItem1);
items.Add(viewItem2);

var field = base.GetTestFieldComponent();
_farm.Components.Add(field);

field.CropViewItems.Add(viewItem1);
field.CropViewItems.Add(viewItem2);

viewItem1.FieldSystemComponentGuid = field.Guid;
viewItem2.FieldSystemComponentGuid = field.Guid;

var result = _sut.CalculateWeightedOrganicNitrogenEmissionFactor(items, _farm);

Assert.AreEqual(0.00010811767111339097, result);
}

[TestMethod]
public void CalculateDirectN2ONFromLeftOverManureForField()
{
var stageState = _farm.GetFieldSystemDetailsStageState();
stageState.DetailsScreenViewCropViewItems.Add(_viewItem);
_farm.StageStates.Add(stageState);

var fieldWithManureApplication = base.GetTestFieldComponent();
fieldWithManureApplication.FieldArea = 133;

_farm.Components.Clear();
_farm.Components.Add(fieldWithManureApplication);
_viewItem.FieldSystemComponentGuid = fieldWithManureApplication.Guid;

var fieldWithOutManureApplications = new FieldSystemComponent();
fieldWithOutManureApplications.FieldArea = 222;
fieldWithOutManureApplications.CropViewItems.Add(new CropViewItem() {CropType = CropType.Barley});

_farm.Components.Add(fieldWithOutManureApplications);

_mockManureService.Setup(x => x.GetTotalNitrogenRemainingForFarmAndYear(It.IsAny<int>(), It.IsAny<Farm>())).Returns(10);

var result = _sut.CalculateDirectN2ONFromLeftOverManureForField(_farm, _viewItem);

Assert.AreEqual(0.0021318977402640473, result);
}

#endregion
}
}
2 changes: 1 addition & 1 deletion H.Core/Calculators/Carbon/ICBMSoilCarbonCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public CropViewItem SetCarbonInputs(
nextYearViewItem: nextYearViewItem,
farm: farm);

if (currentYearViewItem.TotalCarbonLossesByGrazingAnimals > 0 && farm.CropHasGrazingAnimals(currentYearViewItem) && farm.YieldAssignmentMethod != YieldAssignmentMethod.Custom)
if (currentYearViewItem.TotalCarbonLossesByGrazingAnimals > 0 && farm.CropHasGrazingAnimals(currentYearViewItem) && farm.YieldAssignmentMethod != YieldAssignmentMethod.Custom && currentYearViewItem.HarvestMethod != HarvestMethods.StubbleGrazing)
{
// Total C losses from grazing animals is calculated in Equation 11.3.2-4

Expand Down
1 change: 0 additions & 1 deletion H.Core/Calculators/Carbon/IPCCTier2SoilCarbonCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ public void CalculateInputs(CropViewItem viewItem, Farm farm)

viewItem.AboveGroundResidueDryMatter = this.CalculateAboveGroundResidueDryMatter(harvestIndex: harvestIndex, viewItem: viewItem);


viewItem.AboveGroundResidueDryMatterExported = this.CalculateAboveGroundResidueDryMatterExported(
freshWeightOfYield: viewItem.Yield,
harvestIndex: harvestIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public double GetTotalManureVolumeAppliedFromLivestockAndImportsInYear(CropViewI
///
/// There can be multiple fields on a farm and the emission factor calculations are field-dependent (accounts for crop type, fertilizer, etc.). So
/// we take the weighted average of these fields when calculating the EF for organic nitrogen (ON). This is to be used when calculating direct emissions
/// from land applied manure. Native rangeland is not included.
/// from land applied manure or digestate. Native rangeland is not included.
/// </summary>
public double CalculateWeightedOrganicNitrogenEmissionFactor(
List<CropViewItem> itemsByYear,
Expand Down
3 changes: 3 additions & 0 deletions H.Core/Enumerations/HarvestMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public enum HarvestMethods
/// </summary>
[LocalizedDescription("EnumNone", typeof(Resources))]
None,

[LocalizedDescription("EnumStubbleGrazing", typeof(Resources))]
StubbleGrazing,
}
}
27 changes: 27 additions & 0 deletions H.Core/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions H.Core/Properties/Resources.fr-CA.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5708,4 +5708,13 @@
<data name="MessageTheSelectedYieldAssignmentMethod" xml:space="preserve">
<value>Please note that the selected yield assignement method chosen will be used for all fields on this farm. Select the 'Custom Yield' or 'Input File' assignement methods to modify the yields for individual fields</value>
</data>
<data name="EnumStubbleGrazing" xml:space="preserve">
<value>Stubble grazing</value>
</data>
<data name="LabelStrawReturnedToSoil" xml:space="preserve">
<value>Straw returned to soil</value>
</data>
<data name="LabelStrawReturnedToSoilAfterStubbleGrazing" xml:space="preserve">
<value>Straw returned to soil after stubble grazing</value>
</data>
</root>
9 changes: 9 additions & 0 deletions H.Core/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -5769,4 +5769,13 @@ Daily emissions estimates are multipled by the total number of production days i
<data name="MessageTheSelectedYieldAssignmentMethod" xml:space="preserve">
<value>Please note that the selected yield assignement method chosen will be used for all fields on this farm. Select the 'Custom Yield' or 'Input File' assignement methods to modify the yields for individual fields</value>
</data>
<data name="EnumStubbleGrazing" xml:space="preserve">
<value>Stubble grazing</value>
</data>
<data name="LabelStrawReturnedToSoil" xml:space="preserve">
<value>Straw returned to soil</value>
</data>
<data name="LabelStrawReturnedToSoilAfterStubbleGrazing" xml:space="preserve">
<value>Straw returned to soil after stubble grazing</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public double GetUtilizationRate(CropType cropType)
case CropType.TameMixed:
return 60;

// Annuals
default:
{
Trace.TraceError($"No data found for '{cropType.GetDescription()}'");

return 0;
}
{
return 70;
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions H.Core/Services/LandManagement/FieldResultsService.Carbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@ public void CalculateCarbonLostByGrazingAnimals(Farm farm,
{
foreach (var cropViewItem in viewItems)
{
if (cropViewItem.HarvestMethod == HarvestMethods.StubbleGrazing)
{
continue;
}

var totalCarbonLossesForField = 0d;
var totalCarbonUptakeForField = 0d;

Expand Down

0 comments on commit a46b098

Please sign in to comment.