Skip to content

Commit

Permalink
feat: Calculate age class interval dynamically from given yield table
Browse files Browse the repository at this point in the history
  • Loading branch information
rschiffer committed Oct 4, 2023
1 parent 7da1895 commit fee9160
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/utils/getAgeClassInterval.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import yieldTables from '../constants/yieldTables';
import getAgeClassInterval from './getAgeClassInterval';

describe('The getAgeClassInterval forest util', () => {
it('should return the age class interval for a yield table', () => {
const result = getAgeClassInterval(yieldTables['at_tirol_laercheSuedtirol']);

expect(result).toEqual(10);
});

it('should return the age class interval for a yield table', () => {
const result = getAgeClassInterval(yieldTables['de_assmann_fichteBayernOcell']);

expect(result).toEqual(5);
});
});
8 changes: 8 additions & 0 deletions src/utils/getAgeClassInterval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import YieldTable from '../models/YieldTable';

const getAgeClassInterval = (yieldTable: YieldTable): number => {
const ageClassInterval = yieldTable.rows[1].age - yieldTable.rows[0].age;
return ageClassInterval;
};

export default getAgeClassInterval;
14 changes: 14 additions & 0 deletions src/utils/getNormalStock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ describe('The getNormalStock forest util', () => {
expect(actual.toFixed(0)).toEqual(expected);
});

it('should return the normal stock for a yield class', () => {
const yieldClass = 3.7;
const rotationPeriod = 85;
const expected = '171';

const actual = getNormalStock(
yieldTables['de_assmann_fichteBayernOcell'],
yieldClass,
rotationPeriod
);

expect(actual.toFixed(0)).toEqual(expected);
});

it('throws an error if no age level is found in the given yield table for the given rotation period', () => {
const yieldClass = 7.1;
const rotationPeriod = 135;
Expand Down
7 changes: 4 additions & 3 deletions src/utils/getNormalStock.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import YieldTable, { YieldTableRow } from '../models/YieldTable';
import getAgeClassInterval from './getAgeClassInterval';
import getYieldClasses from './getYieldClasses';

const getNormalStock = (
Expand All @@ -18,8 +19,8 @@ const getNormalStock = (
// Get the yield classes via the `getYieldClasses` util (The util returns one or two yield classes)
const yieldClasses: number[] = getYieldClasses(yieldTable, siteProductivity);

// Define the age gap
const ageGap: number = 10; // TODO: get dynamically from yield table via a new forest util
// Get the age class interval
const ageClassInterval = getAgeClassInterval(yieldTable);

// Get the yield table rows for the relevant yield classes and relevant age levels
const yieldTableRowsForRelevantYieldClassesAndAgeLevels: YieldTableRow[][] = yieldClasses.map(
Expand All @@ -40,7 +41,7 @@ const getNormalStock = (
return acc + stock;
}, 0);

return (ageGap / rotationPeriod) * sumOfStocks;
return (ageClassInterval / rotationPeriod) * sumOfStocks;
}
);

Expand Down

0 comments on commit fee9160

Please sign in to comment.