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
5 changes: 5 additions & 0 deletions .changeset/better-snakes-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nmi-agro/fdm-calculator": patch
---

Fix nitrogen removal for crop residues: residues left on the field are no longer counted as removed, while residues removed from the field are.
6 changes: 3 additions & 3 deletions fdm-calculator/src/balance/nitrogen/removal/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ describe("calculateNitrogenRemoval", () => {
cultivationDetailsMap,
)

expect(result.total.toNumber()).toBeCloseTo(-23) // -20 from harvest + -3 from residue
expect(result.total.toNumber()).toBeCloseTo(-20) // -20 from harvest
expect(result.harvests.total.toNumber()).toBeCloseTo(-20)
expect(result.residues.total.toNumber()).toBeCloseTo(-3)
expect(result.residues.total.toNumber()).toBeCloseTo(0) // No nitrogen removed by residues since m_cropresidue is true
})

it("should handle cases with no harvests or residues", () => {
const cultivations: FieldInput["cultivations"] = [
{
b_lu: "cultivation1",
b_lu_catalogue: "catalogue1",
m_cropresidue: false,
m_cropresidue: true, // Residues left on field, not removed
b_lu_start: new Date("2022-01-01"),
b_lu_end: new Date("2022-12-31"),
b_lu_name: "Cultivation 1",
Expand Down
16 changes: 8 additions & 8 deletions fdm-calculator/src/balance/nitrogen/removal/residue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ describe("calculateNitrogenRemovalByResidue", () => {
],
])

it("should return 0 if no crop residues are left", () => {
it("should return 0 if crop residues are left on the field", () => {
const cultivations: FieldInput["cultivations"] = [
{
b_lu: "cultivation1",
b_lu_catalogue: "catalogue1",
b_lu_start: new Date("2022-01-01"),
b_lu_end: new Date("2022-12-31"),
m_cropresidue: false, // No residue left
m_cropresidue: true, // Residues left on field, not removed
b_lu_name: "Cultivation 1",
b_lu_croprotation: "cereal",
},
Expand All @@ -64,7 +64,7 @@ describe("calculateNitrogenRemovalByResidue", () => {
b_lu_catalogue: "catalogue1",
b_lu_start: new Date("2022-01-01"),
b_lu_end: new Date("2022-12-31"),
m_cropresidue: true,
m_cropresidue: false, // Residues removed from field
b_lu_name: "Cultivation 1",
b_lu_croprotation: "cereal",
},
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("calculateNitrogenRemovalByResidue", () => {
b_lu_catalogue: "catalogue1",
b_lu_start: new Date("2022-01-01"),
b_lu_end: new Date("2022-12-31"),
m_cropresidue: true,
m_cropresidue: false, // Residues removed from field
b_lu_name: "Cultivation 1",
b_lu_croprotation: "cereal",
},
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("calculateNitrogenRemovalByResidue", () => {
b_lu_catalogue: "catalogue1",
b_lu_start: new Date("2022-01-01"),
b_lu_end: new Date("2022-12-31"),
m_cropresidue: true,
m_cropresidue: false, // Residues removed from field
b_lu_name: "Cultivation 1",
b_lu_croprotation: "cereal",
},
Expand Down Expand Up @@ -228,7 +228,7 @@ describe("calculateNitrogenRemovalByResidue", () => {
)
})

it("should handle null m_cropresidue as false", () => {
it("should handle null m_cropresidue as not removed (return 0)", () => {
const cultivations: FieldInput["cultivations"] = [
{
b_lu: "cultivation1",
Expand Down Expand Up @@ -261,7 +261,7 @@ describe("calculateNitrogenRemovalByResidue", () => {
b_lu_catalogue: "catalogue1",
b_lu_start: new Date("2022-01-01"),
b_lu_end: new Date("2022-12-31"),
m_cropresidue: true,
m_cropresidue: false, // Residues removed from field
b_lu_name: "Cultivation 1",
b_lu_croprotation: "cereal",
},
Expand Down Expand Up @@ -295,7 +295,7 @@ describe("calculateNitrogenRemovalByResidue", () => {
b_lu_catalogue: "catalogue2",
b_lu_start: new Date("2022-01-01"),
b_lu_end: new Date("2022-12-31"),
m_cropresidue: true,
m_cropresidue: false, // Residues removed from field
b_lu_name: "test",
b_lu_croprotation: null,
},
Expand Down
4 changes: 2 additions & 2 deletions fdm-calculator/src/balance/nitrogen/removal/residue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export function calculateNitrogenRemovalByResidue(
)
}

// If no crop residues are left or if this is not known return 0 for the amount of Nitrogen removed by crop residues
if (!cultivation.m_cropresidue) {
// If crop residues are left or if this is not known return 0 for the amount of Nitrogen removed by crop residues
if (cultivation.m_cropresidue !== false) {
return {
id: cultivation.b_lu,
value: new Decimal(0),
Expand Down
Loading