Skip to content

Commit

Permalink
Merge pull request #55 from bcgov/feature/VdypPolygon-class-set-changes
Browse files Browse the repository at this point in the history
Feature/vdyp polygon class set changes
  • Loading branch information
smithkm authored Aug 15, 2024
2 parents 2015855 + f822b2e commit 60006f6
Show file tree
Hide file tree
Showing 128 changed files with 3,843 additions and 3,450 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@

import static ca.bc.gov.nrs.vdyp.test.VdypMatchers.controlMapHasEntry;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import ca.bc.gov.nrs.vdyp.common.ControlKey;
import ca.bc.gov.nrs.vdyp.common.GenusDefinitionMap;
import ca.bc.gov.nrs.vdyp.forward.test.VdypForwardTestUtils;
import ca.bc.gov.nrs.vdyp.io.parse.common.ResourceParseException;
import ca.bc.gov.nrs.vdyp.model.GenusDefinition;

class GenusDefinitionMapTest {

Expand All @@ -35,22 +31,18 @@ void before() throws IOException, ResourceParseException {
controlMap = VdypForwardTestUtils.parse(parser, "VDYP.CTR");
assertThat(
controlMap,
(Matcher) controlMapHasEntry(
ControlKey.SP0_DEF, allOf(instanceOf(List.class), hasItem(instanceOf(GenusDefinition.class)))
)
(Matcher) controlMapHasEntry(ControlKey.SP0_DEF, Matchers.instanceOf(GenusDefinitionMap.class))
);

}

@Test
void test() {
@SuppressWarnings("unchecked")
var gdMap = new GenusDefinitionMap((List<GenusDefinition>) controlMap.get(ControlKey.SP0_DEF.name()));
var gdMap = (GenusDefinitionMap) controlMap.get(ControlKey.SP0_DEF.name());

assertThat(gdMap.contains("AC"), is(true));
assertThat(gdMap.get("AC"), hasProperty("alias", is("AC")));
assertThat(gdMap.getByAlias("AC"), hasProperty("alias", is("AC")));
assertThat(gdMap.getByIndex(3), hasProperty("alias", is("B")));
assertThat(gdMap.getIndex("B"), is(3));
assertThat(gdMap.getNSpecies(), is(16));
assertThat(gdMap.getIndexByAlias("B"), is(3));
assertThat(gdMap.getNGenera(), is(16));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class LowValueException extends StandProcessingException {
final private float threshold;

public LowValueException(String message, float value, float threshold) {
super(String.format("%s %f was lass than %f", message, value, threshold));
super(String.format("%s %f was less than %f", message, value, threshold));
this.value = value;
this.threshold = threshold;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ protected Coefficients getCoeForSpecies(BaseVdypSpecies<?> species, ControlKey c
protected L requireLayer(P polygon, LayerType type) throws ProcessingException {
if (!polygon.getLayers().containsKey(type)) {
throw validationError(
"Polygon \"%s\" has no %s layer, or that layer has non-positive height or crown closure.",
polygon.getPolygonIdentifier(), type
"Polygon \"%s\" has no %s layer, or that layer has non-positive height or crown closure.", polygon
.getPolygonIdentifier(), type
);
}

Expand All @@ -293,8 +293,8 @@ protected float getPercentTotal(L layer) throws StandProcessingException {
.sum();
if (Math.abs(percentTotal - 100f) > 0.01f) {
throw validationError(
"Polygon \"%s\" has %s layer where species entries have a percentage total that does not sum to 100%%.",
layer.getPolygonIdentifier(), LayerType.PRIMARY
"Polygon \"%s\" has %s layer where species entries have a percentage total that does not sum to 100%%.", layer
.getPolygonIdentifier(), LayerType.PRIMARY
);
}
return percentTotal;
Expand Down Expand Up @@ -533,8 +533,8 @@ protected float estimatePrimaryBaseArea(

var decayBecAlias = bec.getDecayBec().getAlias();
Coefficients coe = weightedCoefficientSum(
List.of(0, 1, 2, 3, 4, 5), 9, 0, layer.getSpecies().values(), BaseVdypSpecies::getFractionGenus,
s -> coeMap.get(decayBecAlias, s.getGenus())
List.of(0, 1, 2, 3, 4, 5), 9, 0, layer.getSpecies()
.values(), BaseVdypSpecies::getFractionGenus, s -> coeMap.get(decayBecAlias, s.getGenus())
);

float ageToUse = clamp(breastHeightAge, 5f, 350f);
Expand Down Expand Up @@ -649,10 +649,6 @@ protected Optional<L> getVeteranLayer(P poly) throws StandProcessingException {
return Utils.optSafe(poly.getLayers().get(LayerType.VETERAN));
}

protected BecDefinition getBec(P poly) throws ProcessingException {
return Utils.getBec(poly.getBiogeoclimaticZone(), controlMap);
}

protected static <E extends Throwable> void throwIfPresent(Optional<E> opt) throws E {
if (opt.isPresent()) {
throw opt.get();
Expand Down Expand Up @@ -768,7 +764,7 @@ public float estimatePercentForestLand(P polygon, Optional<L> vetLayer, L primar

float yieldFactor = getYieldFactor(polygon);

var bec = Utils.getBec(polygon.getBiogeoclimaticZone(), controlMap);
var bec = polygon.getBiogeoclimaticZone();

breastHeightAge = max(5.0f, breastHeightAge);
// EMP040
Expand Down Expand Up @@ -851,8 +847,8 @@ protected float estimatePrimaryQuadMeanDiameter(

var decayBecAlias = bec.getDecayBec().getAlias();
Coefficients coe = weightedCoefficientSum(
List.of(0, 1, 2, 3, 4), 8, 0, layer.getSpecies().values(), BaseVdypSpecies::getFractionGenus,
s -> coeMap.get(decayBecAlias, s.getGenus())
List.of(0, 1, 2, 3, 4), 8, 0, layer.getSpecies()
.values(), BaseVdypSpecies::getFractionGenus, s -> coeMap.get(decayBecAlias, s.getGenus())
);

var trAge = log(clamp(breastHeightAge, 5f, 350f));
Expand Down Expand Up @@ -909,7 +905,7 @@ protected void applyGroups(BaseVdypPolygon<?, ?, ?, ?> fipPolygon, Collection<Vd
throws ProcessingException {
// Lookup volume group, Decay Group, and Breakage group for each species.

BecDefinition bec = Utils.getBec(fipPolygon.getBiogeoclimaticZone(), controlMap);
BecDefinition bec = fipPolygon.getBiogeoclimaticZone();
var volumeGroupMap = getGroupMap(ControlKey.VOLUME_EQN_GROUPS);
var decayGroupMap = getGroupMap(ControlKey.DECAY_GROUPS);
var breakageGroupMap = getGroupMap(ControlKey.BREAKAGE_GROUPS);
Expand Down Expand Up @@ -976,7 +972,7 @@ public void estimateSmallComponents(P fPoly, VdypLayer layer) throws ProcessingE
float treesPerHectareSum = 0f;
float volumeSum = 0f;

Region region = Utils.getBec(fPoly.getBiogeoclimaticZone(), controlMap).getRegion();
Region region = fPoly.getBiogeoclimaticZone().getRegion();

for (VdypSpecies spec : layer.getSpecies().values()) {
@SuppressWarnings("unused")
Expand Down Expand Up @@ -1196,8 +1192,7 @@ public void computeUtilizationComponentsPrimary(
// Calculate tree density components
for (var uc : VdypStartApplication.UTIL_CLASSES) {
treesPerHectareUtil.set(
uc,
BaseAreaTreeDensityDiameter
uc, BaseAreaTreeDensityDiameter
.treesPerHectare(baseAreaUtil.getCoe(uc.index), quadMeanDiameterUtil.get(uc))
);
}
Expand All @@ -1215,8 +1210,7 @@ public void computeUtilizationComponentsPrimary(

for (var uc : VdypStartApplication.UTIL_CLASSES) {
treesPerHectareUtil.setCoe(
uc.index,
BaseAreaTreeDensityDiameter
uc.index, BaseAreaTreeDensityDiameter
.treesPerHectare(baseAreaUtil.getCoe(uc.index), quadMeanDiameterUtil.getCoe(uc.index))
);
}
Expand All @@ -1233,8 +1227,9 @@ public void computeUtilizationComponentsPrimary(

// EMP091
estimationMethods.estimateWholeStemVolume(
UtilizationClass.ALL, adjustCloseUtil.getCoe(4), spec.getVolumeGroup(), loreyHeightSpec,
quadMeanDiameterUtil, baseAreaUtil, wholeStemVolumeUtil
UtilizationClass.ALL, adjustCloseUtil.getCoe(
4
), spec.getVolumeGroup(), loreyHeightSpec, quadMeanDiameterUtil, baseAreaUtil, wholeStemVolumeUtil
);

if (compatibilityVariableMode == CompatibilityVariableMode.ALL) {
Expand All @@ -1249,28 +1244,28 @@ public void computeUtilizationComponentsPrimary(

// EMP092
estimationMethods.estimateCloseUtilizationVolume(
UtilizationClass.ALL, adjustCloseUtil, spec.getVolumeGroup(), loreyHeightSpec,
quadMeanDiameterUtil, wholeStemVolumeUtil, closeVolumeUtil
UtilizationClass.ALL, adjustCloseUtil, spec
.getVolumeGroup(), loreyHeightSpec, quadMeanDiameterUtil, wholeStemVolumeUtil, closeVolumeUtil
);

// EMP093
estimationMethods.estimateNetDecayVolume(
spec.getGenus(), bec.getRegion(), UtilizationClass.ALL, adjustCloseUtil, spec.getDecayGroup(),
vdypLayer.getBreastHeightAge().orElse(0f), quadMeanDiameterUtil, closeVolumeUtil,
closeVolumeNetDecayUtil
spec.getGenus(), bec.getRegion(), UtilizationClass.ALL, adjustCloseUtil, spec
.getDecayGroup(), vdypLayer.getBreastHeightAge()
.orElse(0f), quadMeanDiameterUtil, closeVolumeUtil, closeVolumeNetDecayUtil
);

// EMP094
estimationMethods.estimateNetDecayAndWasteVolume(
bec.getRegion(), UtilizationClass.ALL, adjustCloseUtil, spec.getGenus(), loreyHeightSpec,
quadMeanDiameterUtil, closeVolumeUtil, closeVolumeNetDecayUtil, closeVolumeNetDecayWasteUtil
bec.getRegion(), UtilizationClass.ALL, adjustCloseUtil, spec
.getGenus(), loreyHeightSpec, quadMeanDiameterUtil, closeVolumeUtil, closeVolumeNetDecayUtil, closeVolumeNetDecayWasteUtil
);

if (this.getId().isStart()) {
// EMP095
estimationMethods.estimateNetDecayWasteAndBreakageVolume(
UtilizationClass.ALL, spec.getBreakageGroup(), quadMeanDiameterUtil, closeVolumeUtil,
closeVolumeNetDecayWasteUtil, closeVolumeNetDecayWasteBreakUtil
UtilizationClass.ALL, spec
.getBreakageGroup(), quadMeanDiameterUtil, closeVolumeUtil, closeVolumeNetDecayWasteUtil, closeVolumeNetDecayWasteBreakUtil
);
}
}
Expand Down Expand Up @@ -1321,12 +1316,12 @@ public void computeUtilizationComponentsPrimary(
"For species {}, Species LH (7.5cm+): {}, Species BA (7.5cm+): {}, Weighted LH (7.5cm+): {}"
).log();
vdypLayer.getLoreyHeightByUtilization().scalarInPlace(
UtilizationClass.SMALL,
x -> x + spec.getLoreyHeightByUtilization().getSmall() * spec.getBaseAreaByUtilization().getSmall()
UtilizationClass.SMALL, x -> x
+ spec.getLoreyHeightByUtilization().getSmall() * spec.getBaseAreaByUtilization().getSmall()
);
vdypLayer.getLoreyHeightByUtilization().scalarInPlace(
UtilizationClass.ALL,
x -> x + spec.getLoreyHeightByUtilization().getAll() * spec.getBaseAreaByUtilization().getAll()
UtilizationClass.ALL, x -> x
+ spec.getLoreyHeightByUtilization().getAll() * spec.getBaseAreaByUtilization().getAll()
);
}
{
Expand Down Expand Up @@ -1359,8 +1354,8 @@ protected void computeLayerUtilizationComponentsFromSpecies(VdypLayer vdypLayer)
vdypLayer.getSpecies().values().stream().forEach(spec -> {
var ba = spec.getBaseAreaByUtilization();
hlVector.pairwiseInPlace(
spec.getLoreyHeightByUtilization(),
(float x, float y, UtilizationClass uc) -> x + y * ba.get(uc)
spec.getLoreyHeightByUtilization(), (float x, float y, UtilizationClass uc) -> x
+ y * ba.get(uc)
);
});
var ba = vdypLayer.getBaseAreaByUtilization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ public float estimateNonPrimaryLoreyHeight(
var coe = coeMap.get(vspec, vspecPrime, bec.getRegion()).orElseThrow(
() -> new ProcessingException(
String.format(
"Could not find Lorey Height Nonprimary Coefficients for %s %s %s", vspec, vspecPrime,
bec.getRegion()
"Could not find Lorey Height Nonprimary Coefficients for %s %s %s", vspec, vspecPrime, bec
.getRegion()
)
)
);
Expand Down Expand Up @@ -324,8 +324,7 @@ public float estimateQuadMeanDiameterForSpecies(
var limits = getLimitsForHeightAndDiameter(species, region);

quadMeanDiameter1 = estimateQuadMeanDiameterClampResult(
limits, standTreesPerHectare, minQuadMeanDiameter, loreyHeightSpec, baseArea1, baseArea2,
quadMeanDiameter1, treesPerHectare2, quadMeanDiameter2
limits, standTreesPerHectare, minQuadMeanDiameter, loreyHeightSpec, baseArea1, baseArea2, quadMeanDiameter1, treesPerHectare2, quadMeanDiameter2
);
return quadMeanDiameter1;
}
Expand Down Expand Up @@ -681,8 +680,7 @@ public void estimateWholeStemVolume(
) throws ProcessingException {

estimateWholeStemVolume(
controlMap, utilizationClass, adjustCloseUtil, volumeGroup, hlSp, quadMeanDiameterUtil, baseAreaUtil,
wholeStemVolumeUtil
controlMap, utilizationClass, adjustCloseUtil, volumeGroup, hlSp, quadMeanDiameterUtil, baseAreaUtil, wholeStemVolumeUtil
);
}

Expand Down Expand Up @@ -711,8 +709,7 @@ public static void estimateWholeStemVolume(
);

estimateWholeStemVolume(
utilizationClass, adjustCloseUtil, volumeGroup, hlSp, wholeStemUtilizationComponentMap,
quadMeanDiameterUtil, baseAreaUtil, wholeStemVolumeUtil
utilizationClass, adjustCloseUtil, volumeGroup, hlSp, wholeStemUtilizationComponentMap, quadMeanDiameterUtil, baseAreaUtil, wholeStemVolumeUtil
);
}

Expand Down Expand Up @@ -786,8 +783,7 @@ public void estimateCloseUtilizationVolume(
UtilizationVector closeUtilizationVolumeUtil
) throws ProcessingException {
estimateCloseUtilizationVolume(
controlMap, utilizationClass, aAdjust, volumeGroup, hlSp, quadMeanDiameterUtil, wholeStemVolumeUtil,
closeUtilizationVolumeUtil
controlMap, utilizationClass, aAdjust, volumeGroup, hlSp, quadMeanDiameterUtil, wholeStemVolumeUtil, closeUtilizationVolumeUtil
);
}

Expand All @@ -814,8 +810,7 @@ public static void estimateCloseUtilizationVolume(
controlMap, ControlKey.CLOSE_UTIL_VOLUME, MatrixMap2.class
);
estimateCloseUtilizationVolume(
utilizationClass, aAdjust, volumeGroup, hlSp, closeUtilizationCoeMap, quadMeanDiameterUtil,
wholeStemVolumeUtil, closeUtilizationVolumeUtil
utilizationClass, aAdjust, volumeGroup, hlSp, closeUtilizationCoeMap, quadMeanDiameterUtil, wholeStemVolumeUtil, closeUtilizationVolumeUtil
);
}

Expand Down Expand Up @@ -881,8 +876,7 @@ public void estimateNetDecayVolume(
) throws ProcessingException {

estimateNetDecayVolume(
controlMap, genus, region, utilizationClass, aAdjust, decayGroup, ageBreastHeight, quadMeanDiameterUtil,
closeUtilizationUtil, closeUtilizationNetOfDecayUtil
controlMap, genus, region, utilizationClass, aAdjust, decayGroup, ageBreastHeight, quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayUtil
);
}

Expand Down Expand Up @@ -913,8 +907,7 @@ public static void estimateNetDecayVolume(
controlMap, ModifierParser.CONTROL_KEY_MOD301_DECAY, MatrixMap2.class
);
estimateNetDecayVolume(
genus, region, utilizationClass, aAdjust, decayGroup, ageBreastHeight, netDecayCoeMap, decayModifierMap,
quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayUtil
genus, region, utilizationClass, aAdjust, decayGroup, ageBreastHeight, netDecayCoeMap, decayModifierMap, quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayUtil
);
}

Expand Down Expand Up @@ -993,8 +986,7 @@ public void estimateNetDecayAndWasteVolume(
) throws ProcessingException {

estimateNetDecayAndWasteVolume(
controlMap, region, utilizationClass, aAdjust, genus, loreyHeight, quadMeanDiameterUtil,
closeUtilizationUtil, closeUtilizationNetOfDecayUtil, closeUtilizationNetOfDecayAndWasteUtil
controlMap, region, utilizationClass, aAdjust, genus, loreyHeight, quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayUtil, closeUtilizationNetOfDecayAndWasteUtil
);
}

Expand Down Expand Up @@ -1027,9 +1019,7 @@ public static void estimateNetDecayAndWasteVolume(
);

estimateNetDecayAndWasteVolume(
region, utilizationClass, aAdjust, genus, loreyHeight, netDecayWasteCoeMap, wasteModifierMap,
quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayUtil,
closeUtilizationNetOfDecayAndWasteUtil
region, utilizationClass, aAdjust, genus, loreyHeight, netDecayWasteCoeMap, wasteModifierMap, quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayUtil, closeUtilizationNetOfDecayAndWasteUtil
);
}

Expand All @@ -1056,8 +1046,9 @@ public static void estimateNetDecayAndWasteVolume(
UtilizationVector closeUtilizationNetOfDecayUtil, UtilizationVector closeUtilizationNetOfDecayAndWasteUtil
) throws ProcessingException {
estimateUtilization(
closeUtilizationNetOfDecayUtil, closeUtilizationNetOfDecayAndWasteUtil, utilizationClass,
(i, netDecay) -> {
closeUtilizationNetOfDecayUtil, closeUtilizationNetOfDecayAndWasteUtil, utilizationClass, (
i, netDecay
) -> {
if (Float.isNaN(netDecay) || netDecay <= 0f) {
return 0f;
}
Expand Down Expand Up @@ -1132,8 +1123,7 @@ public void estimateNetDecayWasteAndBreakageVolume(
) throws ProcessingException {

estimateNetDecayWasteAndBreakageVolume(
controlMap, utilizationClass, breakageGroup, quadMeanDiameterUtil, closeUtilizationUtil,
closeUtilizationNetOfDecayAndWasteUtil, closeUtilizationNetOfDecayWasteAndBreakageUtil
controlMap, utilizationClass, breakageGroup, quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayAndWasteUtil, closeUtilizationNetOfDecayWasteAndBreakageUtil
);
}

Expand All @@ -1159,8 +1149,7 @@ public static void estimateNetDecayWasteAndBreakageVolume(
.<Map<Integer, Coefficients>>expectParsedControl(controlMap, ControlKey.BREAKAGE, Map.class);

estimateNetDecayWasteAndBreakageVolume(
utilizationClass, breakageGroup, netBreakageCoeMap, quadMeanDiameterUtil, closeUtilizationUtil,
closeUtilizationNetOfDecayAndWasteUtil, closeUtilizationNetOfDecayWasteAndBreakageUtil
utilizationClass, breakageGroup, netBreakageCoeMap, quadMeanDiameterUtil, closeUtilizationUtil, closeUtilizationNetOfDecayAndWasteUtil, closeUtilizationNetOfDecayWasteAndBreakageUtil
);
}

Expand Down Expand Up @@ -1193,8 +1182,9 @@ public static void estimateNetDecayWasteAndBreakageVolume(
final var a4 = coefficients.getCoe(4);

estimateUtilization(
closeUtilizationNetOfDecayAndWasteUtil, closeUtilizationNetOfDecayWasteAndBreakageUtil,
utilizationClass, (uc, netWaste) -> {
closeUtilizationNetOfDecayAndWasteUtil, closeUtilizationNetOfDecayWasteAndBreakageUtil, utilizationClass, (
uc, netWaste
) -> {

if (netWaste <= 0f) {
return 0f;
Expand Down
Loading

0 comments on commit 60006f6

Please sign in to comment.