Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mjunkin committed Oct 2, 2024
1 parent 717aa14 commit 384029a
Show file tree
Hide file tree
Showing 47 changed files with 415 additions and 320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.time.LocalTime;
import java.util.Properties;

@SuppressWarnings({ "java:S100", "java:S116" })
public class VdypComponent {
private static final String COPYRIGHT_HOLDER = "Government of British Columbia";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,6 @@ protected List<S> findPrimarySpecies(Collection<S> allSpecies) {
* @return
* @throws ProcessingException
*/
@SuppressWarnings(
{ //
"java:S1301", // Using switch instead of if for consistency
"java:S3776" // Inherently a lot of branching in a consistent manner, breaking into more
// functions would make it less comprehensible
}
)
protected int findItg(List<S> primarySecondary) throws StandProcessingException {
var primary = primarySecondary.get(0);

Expand Down Expand Up @@ -747,7 +740,6 @@ public static <T> Coefficients weightedCoefficientSum(
}

// FIPLAND
@SuppressWarnings("java:S3655")
public float estimatePercentForestLand(P polygon, Optional<L> vetLayer, L primaryLayer) throws ProcessingException {
if (polygon.getPercentAvailable().isPresent()) {
return polygon.getPercentAvailable().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public ComputationMethods(EstimationMethods estimationMethods, VdypApplicationId
}

/**
* YUC1 - compute Utilization components (quad-mean-diameter, basal area and trees-per-hectare) and,
* optionally, volumes for a polygon's primary layer.
*
* @param bec Bec zone
* @param vdypLayer (primary) layer in question
* @param volumeComputeMode the {@link VolumeComputeMode} under which this method is to operate
* YUC1 - compute Utilization components (quad-mean-diameter, basal area and trees-per-hectare) and, optionally,
* volumes for a polygon's primary layer.
*
* @param bec Bec zone
* @param vdypLayer (primary) layer in question
* @param volumeComputeMode the {@link VolumeComputeMode} under which this method is to operate
* @param compatibilityVariableMode the {@link CompatibilityVariableMode} under which this method is to operate.
* @throws ProcessingException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,15 +889,18 @@ public float estimateBaseAreaYield(
* @throws StandProcessingException in the event of a processing error
*/
public float estimateQuadMeanDiameterYield(
Coefficients coefficients, int controlVariable2Setting, float dominantHeight, float breastHeightAge,
Coefficients coefficients, int debugVariable2Value, float dominantHeight, float breastHeightAge,
Optional<Float> veteranBaseArea, float upperBoundQuadMeanDiameter
) throws StandProcessingException {

if (dominantHeight <= 5) {
return 7.6f;
}

final float ageUse = breastHeightAge;
float ageUse = breastHeightAge;
if (debugVariable2Value > 0) {
ageUse = Math.min(ageUse, debugVariable2Value * 100.0f);
}

if (ageUse <= 0f) {
throw new StandProcessingException("Primary breast height age must be positive but was " + ageUse);
Expand All @@ -909,8 +912,8 @@ public float estimateQuadMeanDiameterYield(
final float c1 = Math.max(coefficients.getCoe(1) + coefficients.getCoe(2) * trAge, 0f);
final float c2 = Math.max(coefficients.getCoe(3) + coefficients.getCoe(4) * trAge, 0f);

float dq = c0 + c1 * FloatMath.pow(dominantHeight - 5f, c2);

float dq = c0 + c1 * FloatMath.pow(dominantHeight - 5f, c2)
* FloatMath.exp(veteranBaseArea.orElse(0.0f) * coefficients.getCoe(5));
return FloatMath.clamp(dq, 7.6f, upperBoundQuadMeanDiameter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public static void reconcileComponents(
}
}

@SuppressWarnings("java:S3655")
public static void reconcileComponentsMode1(
UtilizationVector baseAreaUtil, UtilizationVector treesPerHectareUtil,
UtilizationVector quadMeanDiameterUtil, float tphSumHigh
Expand Down Expand Up @@ -250,7 +249,6 @@ public static void reconcileComponentsMode2(
}
}

@SuppressWarnings("java:S3655")
public static void reconcileComponentsMode3(
UtilizationVector baseAreaUtil, UtilizationVector treesPerHectareUtil,
UtilizationVector quadMeanDiameterUtil
Expand Down
32 changes: 32 additions & 0 deletions lib/vdyp-common/src/main/java/ca/bc/gov/nrs/vdyp/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ca.bc.gov.nrs.vdyp.model.GenusDefinitionMap;
import ca.bc.gov.nrs.vdyp.model.UtilizationClass;
import ca.bc.gov.nrs.vdyp.model.UtilizationVector;
import ca.bc.gov.nrs.vdyp.model.VdypEntity;
import ca.bc.gov.nrs.vdyp.model.VdypLayer;
import ca.bc.gov.nrs.vdyp.model.VdypSpecies;
import ca.bc.gov.nrs.vdyp.model.VdypUtilizationHolder;
Expand Down Expand Up @@ -382,6 +383,37 @@ public static <T> String optPretty(Optional<T> value, Function<T, String> string
return (String) optNa(value.map(stringify));
}

/**
* If <code>f</code> is <code>null</code> or <code>f.isNan()</code> is true, this method returns
* <code>Optional.empty()</code> otherwise, <code>Optional.of(f)</code> is returned.
*
* @param f the Float to be made into an Optional
* @return as described.
*/
public static Optional<Float> optFloat(Float f) {
assert VdypEntity.MISSING_FLOAT_VALUE.isNaN();
if (f == null || f.isNaN()) {
return Optional.empty();
} else {
return Optional.of(f);
}
}

/**
* If <code>f</code> is <code>null</code> or <code>f.isNan()</code> is true, this method returns
* <code>Optional.empty()</code> otherwise, <code>Optional.of(f)</code> is returned.
*
* @param i the Float to be made into an Optional
* @return as described.
*/
public static Optional<Integer> optInt(Integer i) {
if (i == null || i == VdypEntity.MISSING_INTEGER_VALUE) {
return Optional.empty();
} else {
return Optional.of(i);
}
}

/**
* Iterates over all but the last entry, passing them to the first consumer then passes the last entry to the second
* consumer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
* @param <Value>
* @param <Marker>
*/
@SuppressWarnings("java:S119")
public class ValueOrMarker<Value, Marker> {
private final boolean isMarker;
private final Object obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
public class AgeToAge {

@SuppressWarnings("java:S3776, java:S6541, java:S1479")
public static double ageToAge(
SiteIndexEquation cuIndex, double sourceAge, SiteIndexAgeType sourceAgeType, SiteIndexAgeType targetAgeType,
double years2BreastHeight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
* Height2SiteIndex.java - given age and height, computes site index. - if age is total, site index and years to breast
* height are iterated until stable.
*/
@SuppressWarnings("java:S1479")
public class Height2SiteIndex {
// Taken from sindex.h

/* error codes */
private static final int SI_ERR_NO_ANS = -4;

@SuppressWarnings("java:S3776, java:S6541")
public static double heightToIndex(
SiteIndexEquation cuIndex, double age, SiteIndexAgeType ageType, double height,
SiteIndexEstimationType siEstType
Expand Down Expand Up @@ -91,7 +89,6 @@ public static double heightToIndex(
return index;
}

@SuppressWarnings("java:S3776, java:S6541")
public static double
baHeightToIndex(SiteIndexEquation cuIndex, double bhage, double height, SiteIndexEstimationType siEstType)
throws CommonCalculatorException {
Expand Down Expand Up @@ -3756,7 +3753,6 @@ public static double siteIterate(SiteIndexEquation cuIndex, double age, SiteInde

}

@SuppressWarnings("java:S3776, java:S6541")
public static double huGarciaQ(double siteIndex, double breastHeightAge) {
double h, q, step, diff, lastdiff;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
/**
* SiteIndex2Height.java - given site index and age, computes site height.
*/
@SuppressWarnings("java:S1479")
public class SiteIndex2Height {

/**
Expand Down Expand Up @@ -2083,7 +2082,7 @@ private static double wiley(
}

height = Utils.computeInFeet(siteIndex, siteIndexFt -> {
double x1 = 2500 / (siteIndex - 4.5);
double x1 = 2500 / (siteIndexFt - 4.5);

double x2 = -1.7307 + 0.1394 * x1;
double x3 = -0.0616 + 0.0137 * x1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ public boolean isIgnoredLine(String line) {
speciesIndicies, speciesIndicies, regionIndicies, MatrixMap3Impl.emptyDefault()
);
lineParser.parse(is, result, (value, r, line) -> {
@SuppressWarnings("java:S117")
var sp0_1 = (String) value.get(NON_PRIMARY_SPECIES_KEY);
@SuppressWarnings("java:S117")
var sp0_2 = (String) value.get(PRIMARY_SPECIES_KEY);
var ieqn = (Integer) value.get(EQUATION_KEY);
var region = (Region) value.get(REGION_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* @param <Result>
*/
@FunctionalInterface
@SuppressWarnings("java:S119")
public interface ParseEntryHandler<Entry, Result> {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void setPolygonYear(int currentYear) {
* Write the given polygon record for the given year to the polygon file.
*
* @param polygon the polygon to be written
* @param year the year of the polygon
* @param year the year of the polygon
* @throws IOException
*/
public void writePolygonWithSpeciesAndUtilizationForYear(VdypPolygon polygon, int year) throws IOException {
Expand All @@ -143,9 +143,13 @@ public void writePolygonWithSpeciesAndUtilizationForYear(VdypPolygon polygon, in
*/
// VDYP_OUT when JPROGRAM = 1 (FIPSTART) or 3 (VRISTART)
public void writePolygonWithSpeciesAndUtilization(VdypPolygon polygon) throws IOException {

writePolygon(polygon);
for (var layer : polygon.getLayers().values()) {

// Primary then Veteran (if present)
var sortedLayers = polygon.getLayers().values().stream()
.sorted((l1, l2) -> l1.getLayerType().getIndex() - l2.getLayerType().getIndex()).toList();
for (var layer : sortedLayers) {
writeUtilization(polygon, layer, layer);
List<VdypSpecies> specs = new ArrayList<>(layer.getSpecies().size());
specs.addAll(layer.getSpecies().values());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public static float ceil(float f) {
return (float) Math.ceil(f);
}

@SuppressWarnings("squid:S4274")
public static float clamp(float x, float min, float max) {
assert max >= min : "Maximum " + max + " was less than minimum " + min;
if (x < min)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class GenusDefinition extends AliasedEntity implements Comparable<GenusDe

private final int index;

@SuppressWarnings("java:S2789")
public GenusDefinition(String alias, int index, String name) {
super(alias, name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
public class VdypMatchers {

static final float EPSILON = 0.001f;

private static float currentEpsilon = EPSILON;

public static void setEpsilon(float newValue) {
currentEpsilon = newValue;
}
Expand Down
52 changes: 32 additions & 20 deletions lib/vdyp-forward/src/main/java/ca/bc/gov/nrs/vdyp/forward/Bank.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.slf4j.LoggerFactory;

import ca.bc.gov.nrs.vdyp.application.ProcessingException;
import ca.bc.gov.nrs.vdyp.common.Utils;
import ca.bc.gov.nrs.vdyp.common_calculators.BaseAreaTreeDensityDiameter;
import ca.bc.gov.nrs.vdyp.model.BecDefinition;
import ca.bc.gov.nrs.vdyp.model.Sp64DistributionSet;
Expand Down Expand Up @@ -65,8 +66,7 @@ class Bank {
public final float[/* nSpecies + 1, including 0 */][/* all ucs */] treesPerHectare; // BANK1 TPHB
public final float[/* nSpecies + 1, including 0 */][/* all ucs */] wholeStemVolumes; // BANK1 VOLWSB

public Bank(VdypLayer layer, BecDefinition becZone, Predicate<VdypSpecies> retainCriteria)
throws ProcessingException {
public Bank(VdypLayer layer, BecDefinition becZone, Predicate<VdypSpecies> retainCriteria) {

this.layer = layer;
this.becZone = becZone;
Expand Down Expand Up @@ -185,7 +185,7 @@ void refreshBank(VdypLayer layer) throws ProcessingException {
}
}

private void transferSpeciesIntoBank(int index, VdypSpecies species) throws ProcessingException {
private void transferSpeciesIntoBank(int index, VdypSpecies species) {

speciesNames[index] = species.getGenus();
sp64Distributions[index] = species.getSp64DistributionSet();
Expand Down Expand Up @@ -235,13 +235,12 @@ private void transferUtilizationSetIntoBank(int index, VdypUtilizationHolder uh)
}

/**
* For each species, set uc All to the sum of the UC values, UC 7.5 and above only,
* for the summable values, and calculate quad-mean-diameter from these values.
* For each species, set uc All to the sum of the UC values, UC 7.5 and above only, for the summable values, and
* calculate quad-mean-diameter from these values.
* <p>
* For the layer, set uc All values (for summable types) to the sum of those of the
* individual species and set the other uc values to the sum of those of the
* individual species. Calculate the uc All value for quad-mean-diameter, and the
* uc All and Small value for lorey-height.
* For the layer, set uc All values (for summable types) to the sum of those of the individual species and set the
* other uc values to the sum of those of the individual species. Calculate the uc All value for quad-mean-diameter,
* and the uc All and Small value for lorey-height.
*/
private void setCalculateUtilizationClassAllValues() {

Expand All @@ -250,7 +249,7 @@ private void setCalculateUtilizationClassAllValues() {
int ucSmallIndex = UtilizationClass.SMALL.ordinal();

// Each species

for (int sp0Index : indices) {

basalAreas[sp0Index][ucAllIndex] = sumUtilizationClassValues(
Expand All @@ -277,7 +276,7 @@ private void setCalculateUtilizationClassAllValues() {
.quadMeanDiameter(basalAreas[sp0Index][ucAllIndex], treesPerHectare[sp0Index][ucAllIndex]);
}
}

// Layer

basalAreas[layerIndex][ucAllIndex] = sumSpeciesUtilizationClassValues(basalAreas, UtilizationClass.ALL);
Expand Down Expand Up @@ -366,7 +365,7 @@ private float sumSpeciesUtilizationClassValues(float[][] ucValues, UtilizationCl
*
* @return as described
*/
VdypLayer getLayer() {
VdypLayer buildLayerFromBank() {

transferUtilizationsFromBank(0, layer);

Expand All @@ -375,7 +374,7 @@ VdypLayer getLayer() {
newSpecies.add(transferSpeciesFromBank(i, layer.getSpecies().get(speciesNames[i])));
}
layer.setSpecies(newSpecies);

return layer;
}

Expand All @@ -384,15 +383,28 @@ private VdypSpecies transferSpeciesFromBank(int index, VdypSpecies species) {
VdypSpecies newSpecies = VdypSpecies.build(speciesBuilder -> {
speciesBuilder.copy(species);
speciesBuilder.percentGenus(this.percentagesOfForestedLand[index]);
species.getSite().ifPresent(site -> speciesBuilder.addSite(VdypSite.build(siteBuilder -> {
species.getSite().ifPresentOrElse(site -> speciesBuilder.addSite(VdypSite.build(siteBuilder -> {
siteBuilder.copy(site);
siteBuilder.ageTotal(this.ageTotals[index]);
siteBuilder.height(this.dominantHeights[index]);
siteBuilder.siteCurveNumber(this.siteCurveNumbers[index]);
siteBuilder.siteGenus(this.speciesNames[index]);
siteBuilder.siteIndex(this.siteIndices[index]);
siteBuilder.yearsToBreastHeight(this.yearsToBreastHeight[index]);
})));
siteBuilder.ageTotal(Utils.optFloat(ageTotals[index]));
siteBuilder.height(Utils.optFloat(this.dominantHeights[index]));
siteBuilder.siteCurveNumber(Utils.optInt(this.siteCurveNumbers[index]));
siteBuilder.siteIndex(Utils.optFloat(this.siteIndices[index]));
siteBuilder.yearsToBreastHeight(Utils.optFloat(this.yearsToBreastHeight[index]));
})), () -> {
VdypSite site = VdypSite.build(siteBuilder -> {
siteBuilder.polygonIdentifier(species.getPolygonIdentifier());
siteBuilder.layerType(species.getLayerType());
siteBuilder.siteGenus(this.speciesNames[index]);
siteBuilder.ageTotal(Utils.optFloat(this.ageTotals[index]));
siteBuilder.height(Utils.optFloat(this.dominantHeights[index]));
siteBuilder.siteCurveNumber(Utils.optInt(this.siteCurveNumbers[index]));
siteBuilder.siteIndex(Utils.optFloat(this.siteIndices[index]));
siteBuilder.yearsToBreastHeight(Utils.optFloat(this.yearsToBreastHeight[index]));
});

speciesBuilder.addSite(site);
});
});

transferUtilizationsFromBank(index, newSpecies);
Expand Down
Loading

0 comments on commit 384029a

Please sign in to comment.