Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
feat: Bb 291 implement the blueberry calculation (#335)
Browse files Browse the repository at this point in the history
* Import blueberry calcLogic and check which to use

* Implement blueberry N calculation logic

* Minor clarification on blueberry json

* Lint

* Minor fixes
  • Loading branch information
GDamaso authored Aug 7, 2024
1 parent a9f23e3 commit 8a57bc6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const CropsInfoComponent: FC<InputModuleProps> = ({
cropId: values.cropId,
yield: values.yield,
plantAgeYears: values.plantAgeYears,
numberOfPlantsPerAcre: values.numberOfPlantsPerAcre,
numberOfPlantsPerAcre: plantsPerHa,
distanceBtwnPlantsRows: appendDistanceBtwnPlants(
values.distanceBtwnPlants,
values.distanceBtwnRows,
Expand Down
56 changes: 39 additions & 17 deletions frontend/src/Utils/Calculate/Calculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,51 @@ import AgronomicBalanceInterface from '@Interface/AgronomicBalanceInterface';
import CropRemovalBalanceInterface from '@Interface/CropRemovalBalance';
import MainBalanceInterface from '@Interface/MainBalanceInterface';
import CropsDetailsInterface from '@Interface/CropsDetailsInterface';
import * as calcData from './calculation-data/raspberryCalculation.json';

function calcN(calcLogic: CalcLogic, yieldValue: number, sawdust?: boolean): number {
import * as raspberryTable from './calculation-data/raspberryCalculation.json';
import * as blueberryTable from './calculation-data/blueberryCalculation.json';

function calcN(
calcLogic: CalcLogic,
yieldValue: number,
sawdust?: boolean,
isRaspberry?: boolean,
plantAge?: number,
plantDensity?: number,
): number {
const { sawdustAddition } = calcLogic;
const NYieldRanges = calcLogic.yieldRanges;
let N = 0;

if (!sawdustAddition || !NYieldRanges) {
console.error(
'Missing details in calculationTable/agronomicBalance/sawdustAddition || NYieldRanges',
);
console.error('Missing details in calculationTable/agronomicBalance/sawdustAddition');
return 0;
}

if (!isRaspberry && plantAge && plantDensity) {
NYieldRanges.forEach((range) => {
if (
(range.min === undefined || plantAge >= range.min) &&
(range.max === undefined || plantAge < range.max)
) {
N += (range.addition * plantDensity) / 1000 / 1.12;
}
});
} else {
NYieldRanges.forEach((range) => {
if (
(range.min === undefined || yieldValue >= range.min) &&
(range.max === undefined || yieldValue < range.max)
) {
N += range.addition;
}
});
}

if (sawdust) {
N += sawdustAddition;
}

NYieldRanges.forEach((range) => {
if (
(range.min === undefined || yieldValue >= range.min) &&
(range.max === undefined || yieldValue < range.max)
) {
N += range.addition;
}
});

return N * -1;
return Math.round(N * -1);
}

function calcPK(calcLogic: CalcLogic, soilTest: number, leafTissue: number): number {
Expand Down Expand Up @@ -82,15 +99,20 @@ function calcRemovalPK(rmCoeficients: CalcLogic, cropYield: number, isPruned?: b
// It is mostly a proof of correct calculations being applied
// For this reason, it is scoped to the first field first crop only at this point
function Calculate(field: FieldDetailInterface, crop: CropsDetailsInterface) {
const calcTable: CalculationTable = calcData;
const agronomicBalance: AgronomicBalanceInterface = { N: 0, P: 0, K: 0 };
const cropRemovalBalance: CropRemovalBalanceInterface = { P: 0, K: 0 };
const isRemovedFromField = crop.whereWillPruningsGo === 'Removed from field';
// Raspberry id is 76 is NMP
const isRaspberry = crop.cropId === '76';
const calcTable: CalculationTable = isRaspberry ? raspberryTable : blueberryTable;

agronomicBalance.N = calcN(
calcTable.agronomicBalance.nitrogenCalculation.logic,
crop.yield,
crop.willSawdustBeApplied,
isRaspberry,
parseInt(crop.plantAgeYears, 10) ?? undefined,
crop.numberOfPlantsPerAcre ?? undefined,
);

agronomicBalance.P = calcPK(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Calculates Nitrogen requirement in lb/ac based on sawdust usage and blueberry field plants age.",
"params": {
"sawdust": "boolean",
"Yield": "Yield (Tons/Ac)"
"Yield": "Yield (Tons/Ac) (Not used for blueberries)"
},
"logic": {
"N": 0,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/Utils/convertToNMP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ const convertToNMP = (
plantAgeYears: crop.plantAgeYears,
numberOfPlantsPerAcre: crop.numberOfPlantsPerAcre,
distanceBtwnPlantsRows: crop.distanceBtwnPlantsRows,
willPlantsBePruned: crop.willPlantsBePruned || templateCropNMP.willPlantsBePruned,
willPlantsBePruned: crop.willPlantsBePruned === true,
whereWillPruningsGo: crop.whereWillPruningsGo,
willSawdustBeApplied: crop.willSawdustBeApplied || templateCropNMP.willSawdustBeApplied,
willSawdustBeApplied: crop.willSawdustBeApplied === true,
}));

const newNutrients: FertilizerInterface[] = Array.isArray(field.Nutrients?.nutrientFertilizers)
Expand Down

0 comments on commit 8a57bc6

Please sign in to comment.