Skip to content

Commit

Permalink
Refactor manure export calls
Browse files Browse the repository at this point in the history
  • Loading branch information
holos-aafc committed Sep 19, 2024
1 parent 081dff0 commit 750960c
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void CalculateTotalN2ONFromExportedManure()
var farm = base.GetTestFarm();
farm.StageStates.Add(base.GetFieldStageState());

var result = _sut.CalculateTotalDirectN2ONFromExportedManureForFarmAndYear(100, 0.5);
var result = _sut.CalculateTotalDirectN2ONFromExportedManure(100, 0.5);

Assert.AreEqual(result, 50);
}
Expand Down
4 changes: 0 additions & 4 deletions H.Core/Calculators/Carbon/CarbonCalculatorBase.Nitrogen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,6 @@ protected void CalculateDirectNitrousOxide(CropViewItem currentYearResults, Farm

this.N2O_NFromOrganicNitrogenExcludeRemainingAmounts =
(this.OrganicPool * emissionFactorForOrganicNitrogen) + ((directN2ONFromLandAppliedManureExcludingRemaining + directN2ONFromLandAppliedDigestateExludingRemaining + directN2ONFromGrazingAnimals) / this.CurrentYearResults.Area);

// Equation 2.6.5-6
// Equation 2.7.4-6
this.N2O_NFromExportedNitrogen = (N2OEmissionFactorCalculator.CalculateTotalDirectN2ONFromExportedManureForFarmAndYear(farm, this.Year) / farm.GetTotalAreaOfFarm(false, this.Year));
}

protected void CalculateNitricOxide(double nORatio)
Expand Down
6 changes: 6 additions & 0 deletions H.Core/Calculators/Nitrogen/IN2OEmissionFactorCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using H.Core.Models;
using H.Core.Models.LandManagement.Fields;

namespace H.Core.Calculators.Nitrogen
{
public interface IN2OEmissionFactorCalculator
{
void Initialize(Farm farm);

/// <summary>
/// (kg N2O-N ha^-1)
/// </summary>
double CalculateTotalDirectN2ONFromExportedManure(Farm farm, ManureExportViewItem manureExportViewItem);
}
}
40 changes: 30 additions & 10 deletions H.Core/Calculators/Nitrogen/N2OEmissionFactorCalculator.Exports.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using H.Core.Models;
using System;
using System.Linq;
using H.Core.Models.LandManagement.Fields;

namespace H.Core.Calculators.Nitrogen
{
Expand All @@ -10,32 +12,50 @@ public partial class N2OEmissionFactorCalculator
/// <summary>
/// Equation 2.6.5-6
/// Equation 2.7.4-6
///
/// (kg N2O-N ha^-1)
/// </summary>
/// <returns>Returns the total direct N2O emissions from exported manure (kg N2O-N ha^-1)</returns>
/// <exception cref="NotImplementedException"></exception>
public double CalculateTotalDirectN2ONFromExportedManure(Farm farm, int year)
{
var emissionsForFarm = this.CalculateTotalDirectN2ONFromExportedManureForFarmAndYear(farm, year);
var result = emissionsForFarm / farm.GetTotalAreaOfFarm(false, year);
var result = 0d;

foreach (var manureExportViewItem in farm.ManureExportViewItems.Where(x => x.DateOfExport.Year == year))
{
result += this.CalculateTotalDirectN2ONFromExportedManure(farm, manureExportViewItem);
}

return result;
}

/// <summary>
/// (kg N2O-N ha^-1)
/// </summary>
public double CalculateTotalDirectN2ONFromExportedManure(Farm farm, ManureExportViewItem manureExportViewItem)
{
var totalNitrogen = _manureService.GetTotalNitrogenFromExportedManure(manureExportViewItem);
var viewItemsByYear = farm.GetCropDetailViewItemsByYear(manureExportViewItem.DateOfExport.Year, false);
var weightedEmissionFactor = this.CalculateWeightedOrganicNitrogenEmissionFactor(viewItemsByYear, farm);
var emissionsForFarm = this.CalculateTotalDirectN2ONFromExportedManure(totalNitrogen, weightedEmissionFactor);
var result = emissionsForFarm / farm.GetTotalAreaOfFarm(false, manureExportViewItem.DateOfExport.Year);

return result;
}

/// <summary>
/// Equation 4.6.1-9
///
/// (kg N2O-N year^-1)
/// Returns the total N2O-N from exported manure for entire farm
///
/// (kg N2O-N year^-1 farm^-1)
/// </summary>
public double CalculateTotalDirectN2ONFromExportedManureForFarmAndYear(Farm farm, int year)
{
var viewItemsByYear = farm.GetCropDetailViewItemsByYear(year, false);
var result = 0d;

var weightedEmissionFactor = this.CalculateWeightedOrganicNitrogenEmissionFactor(viewItemsByYear, farm);
var totalExportedManureNitrogen = _manureService.GetTotalNitrogenFromExportedManure(year, farm);
result = this.CalculateTotalDirectN2ONFromExportedManure(farm, year) * farm.GetTotalAreaOfFarm(false, year);

var emissions = this.CalculateTotalDirectN2ONFromExportedManureForFarmAndYear(totalExportedManureNitrogen, weightedEmissionFactor);

return emissions;
return result;
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public double CalculateVolumeFromLeftOverManureForField(
///
/// (kg N2O-N)
/// </summary>
public double CalculateTotalDirectN2ONFromExportedManureForFarmAndYear(
public double CalculateTotalDirectN2ONFromExportedManure(
double totalExportedManureNitrogen,
double weightedEmissionFactor)
{
Expand Down
32 changes: 31 additions & 1 deletion H.Core/Models/Results/ManureExportResultViewItem.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
namespace H.Core.Models.Results
using System;
using H.Core.CustomAttributes;
using H.Core.Enumerations;

namespace H.Core.Models.Results
{
public class ManureExportResultViewItem : ResultsViewItemBase
{
#region Fields

private DateTime _dateOfExport;
private double _n2ON;

#endregion

#region Propeties

public DateTime DateOfExport
{
get => _dateOfExport;
set => SetProperty(ref _dateOfExport, value);
}

/// <summary>
/// (kg N2O-N ha^-1)
/// </summary>
[Units(MetricUnitsOfMeasurement.KilogramsN2ONPerHectare)]
public double N2ON
{
get => _n2ON;
set => SetProperty(ref _n2ON, value);
}

#endregion
}
}
1 change: 1 addition & 0 deletions H.Core/Services/Animals/IManureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,6 @@ List<MonthlyManureSpreadingData> GetMonthlyManureSpreadingData(

void SetValidManureStateTypes(ManureSubstrateViewItem manureSubstrateViewItem, Farm farm);
List<AnimalType> GetValidManureForDigestorImports();
double GetTotalNitrogenFromExportedManure(ManureExportViewItem exportViewItem);
}
}
25 changes: 18 additions & 7 deletions H.Core/Services/Animals/ManureService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,25 @@ public double GetTotalNitrogenFromExportedManure(int year, Farm farm)

foreach (var manureExportViewItem in farm.ManureExportViewItems.Where(x => x.DateOfExport.Year == year))
{
var nitrogenContent = 0d;
var amountOfManure = manureExportViewItem.Amount;
if (manureExportViewItem.DefaultManureCompositionData != null)
{
nitrogenContent = manureExportViewItem.DefaultManureCompositionData.NitrogenContent;
}
var nitrogen = this.GetTotalNitrogenFromExportedManure(manureExportViewItem);

result += (amountOfManure * nitrogenContent);
result += nitrogen;
}

return result;
}

/// <summary>
/// (kg N)
/// </summary>
public double GetTotalNitrogenFromExportedManure(ManureExportViewItem exportViewItem)
{
var result = 0d;

var amountOfManure = exportViewItem.Amount;
if (exportViewItem.DefaultManureCompositionData != null)
{
result = amountOfManure * exportViewItem.DefaultManureCompositionData.NitrogenContent;
}

return result;
Expand Down
18 changes: 18 additions & 0 deletions H.Core/Services/FarmResultsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using H.Core.Models;
using H.Core.Models.Animals;
using H.Core.Models.LandManagement.Fields;
using H.Core.Models.Results;
using H.Core.Providers;
using H.Core.Providers.Animals;
using H.Core.Providers.Climate;
Expand Down Expand Up @@ -275,6 +276,23 @@ public FarmEmissionResults CalculateFarmEmissionResults(Farm farm)
return farmResults;
}

public List<ManureExportResultViewItem> CalculateExportEmissions(Farm farm)
{
var result = new List<ManureExportResultViewItem>();

foreach (var manureExportViewItem in farm.ManureExportViewItems)
{
var manureExportResultItem = new ManureExportResultViewItem
{
DateOfExport = manureExportViewItem.DateOfExport,
};

manureExportResultItem.N2ON = _n2OEmissionFactorCalculator.CalculateTotalDirectN2ONFromExportedManure(farm, manureExportViewItem);
}

return result;
}

public List<CropViewItem> CalculateFieldResults(Farm farm)
{
// Field results
Expand Down

0 comments on commit 750960c

Please sign in to comment.