Skip to content

Commit

Permalink
Further adjustments after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
mjunkin committed Sep 10, 2024
1 parent 210d841 commit b1b7d54
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ca.bc.gov.nrs.vdyp.model.UtilizationClass;
import ca.bc.gov.nrs.vdyp.model.VdypLayer;
import ca.bc.gov.nrs.vdyp.model.VdypPolygon;
import ca.bc.gov.nrs.vdyp.model.VdypSite;
import ca.bc.gov.nrs.vdyp.model.VdypSpecies;
import ca.bc.gov.nrs.vdyp.model.VdypUtilizationHolder;

Expand All @@ -29,9 +30,9 @@
*/
public class VdypOutputWriter implements Closeable {

private OutputStream polygonFile;
private OutputStream speciesFile;
private OutputStream utilizationFile;
protected final OutputStream polygonFile;
protected final OutputStream speciesFile;
protected final OutputStream utilizationFile;
@SuppressWarnings("unused")
private Optional<OutputStream> compatibilityVariablesFile;

Expand Down Expand Up @@ -175,7 +176,6 @@ void writeSpecies(VdypLayer layer, VdypSpecies spec) throws IOException {
Stream.generate(() -> new Sp64Distribution(0, "", 0f))
).limit(4).toList();
// 082E004 615 1988 P 9 L LW 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9
boolean isSiteSpec = layer.getSiteGenus().map(spec.getGenus()::equals).orElse(false);
writeFormat(
speciesFile, //
SPEC_FORMAT, //
Expand All @@ -195,15 +195,13 @@ void writeSpecies(VdypLayer layer, VdypSpecies spec) throws IOException {
specDistributionEntries.get(3).getGenusAlias(), //
specDistributionEntries.get(3).getPercentage(), //

layer.getSiteIndex().filter(x -> isSiteSpec).orElse(EMPTY_FLOAT), //
layer.getHeight().filter(x -> isSiteSpec).orElse(EMPTY_FLOAT), //
layer.getAgeTotal().filter(x -> isSiteSpec).orElse(EMPTY_FLOAT), //
layer.getBreastHeightAge().filter(x -> isSiteSpec).orElse(EMPTY_FLOAT), //
layer.getYearsToBreastHeight().filter(x -> isSiteSpec).orElse(EMPTY_FLOAT), //
layer.getSiteGenus().filter(x -> isSiteSpec).map(id -> id.equals(spec.getGenus())).orElse(false) ? 1
: 0, //
layer.getSiteCurveNumber().filter(x -> isSiteSpec).orElse(EMPTY_INT)

spec.getSite().flatMap(VdypSite::getSiteIndex).orElse(EMPTY_FLOAT), //
spec.getSite().flatMap(VdypSite::getHeight).orElse(EMPTY_FLOAT), //
spec.getSite().flatMap(VdypSite::getAgeTotal).orElse(EMPTY_FLOAT), //
spec.getSite().flatMap(VdypSite::getYearsAtBreastHeight).orElse(EMPTY_FLOAT), //
spec.getSite().flatMap(VdypSite::getYearsToBreastHeight).orElse(EMPTY_FLOAT), //
layer.getPrimaryGenus().map(spec.getGenus()::equals).orElse(false) ? 1 : 0, //
spec.getSite().flatMap(VdypSite::getSiteCurveNumber).orElse(EMPTY_INT) //
);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import ca.bc.gov.nrs.vdyp.common.ControlKey;
import ca.bc.gov.nrs.vdyp.common.Utils;
import ca.bc.gov.nrs.vdyp.common_calculators.BaseAreaTreeDensityDiameter;
import ca.bc.gov.nrs.vdyp.io.FileResolver;
import ca.bc.gov.nrs.vdyp.model.BaseVdypSpecies;
import ca.bc.gov.nrs.vdyp.model.LayerType;
Expand Down Expand Up @@ -68,6 +74,7 @@ static OutputStream getOutputStream(Map<String, Object> controlMap, FileResolver
* @param polygon
* @throws IOException
*/
@Override
void writePolygon(VdypPolygon polygon) throws IOException {
writeFormat(
polygonFile, //
Expand All @@ -91,12 +98,13 @@ void writePolygon(VdypPolygon polygon) throws IOException {
* @param spec
* @throws IOException
*/
@Override
void writeSpecies(VdypLayer layer, VdypSpecies spec) throws IOException {

// Ensure we have a list of 4 distribution entries
var specDistributionEntries = Stream.concat(
spec.getSp64DistributionSet().getSp64DistributionList().stream(), Stream
.generate(() -> new Sp64Distribution(0, "", 0f))
spec.getSp64DistributionSet().getSp64DistributionList().stream(),
Stream.generate(() -> new Sp64Distribution(0, "", 0f))
).limit(4).toList();
// 082E004 615 1988 P 9 L LW 100.0 0.0 0.0 0.0 -9.00 -9.00 -9.0 -9.0 -9.0 0 -9
writeFormat(
Expand Down Expand Up @@ -137,6 +145,7 @@ void writeSpecies(VdypLayer layer, VdypSpecies spec) throws IOException {
* @param utils
* @throws IOException
*/
@Override
// V7W_AIU Internalized loop over utilization classes
void writeUtilization(VdypLayer layer, VdypUtilizationHolder utils) throws IOException {
Optional<String> specId = Optional.empty();
Expand All @@ -155,8 +164,8 @@ void writeUtilization(VdypLayer layer, VdypUtilizationHolder utils) throws IOExc
if (utils.getBaseAreaByUtilization().getCoe(uc.index) > 0) {
quadMeanDiameter = Optional.of(
BaseAreaTreeDensityDiameter.quadMeanDiameter(
utils.getBaseAreaByUtilization().getCoe(uc.index), utils
.getTreesPerHectareByUtilization().getCoe(uc.index)
utils.getBaseAreaByUtilization().getCoe(uc.index),
utils.getTreesPerHectareByUtilization().getCoe(uc.index)
)
);
}
Expand Down Expand Up @@ -197,6 +206,7 @@ void writeUtilization(VdypLayer layer, VdypUtilizationHolder utils) throws IOExc
* @throws IOException
*/
// VDYP_OUT when JPROGRAM = 1 (FIPSTART) or 3 (VRISTART)
@Override
public void writePolygonWithSpeciesAndUtilization(VdypPolygon polygon) throws IOException {

writePolygon(polygon);
Expand Down Expand Up @@ -226,6 +236,7 @@ private void writeSpeciesEndRecord(VdypPolygon polygon) throws IOException {
writeEndRecord(speciesFile, polygon);
}

@Override
void writeFormat(OutputStream os, String format, Object... params) throws IOException {
os.write(String.format(format, params).getBytes());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public LinkedHashMap<String, S> getSpecies() {
}

public void setSpecies(Map<String, S> species) {
this.speciesBySp0.clear();
this.speciesByIndex.clear();
this.speciesBySp0.putAll(species);
setSpecies(species.values());
}

public void setSpecies(Collection<S> species) {
Expand Down Expand Up @@ -93,6 +91,7 @@ public Optional<S> getPrimarySpeciesRecord() {
@Computed
public Optional<I> getPrimarySite() {
return getPrimaryGenus().map(this.getSites()::get);
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ca.bc.gov.nrs.vdyp.model;


public interface InputLayer {

public float getCrownClosure();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public class VdypLayer extends BaseVdypLayer<VdypSpecies, VdypSite> implements V
private UtilizationVector closeUtilizationVolumeNetOfDecayWasteAndBreakageByUtilization = //
VdypUtilizationHolder.emptyUtilization(); // LVCOM/VOL_DWB species 0

private Optional<Integer> empericalRelationshipParameterIndex = Optional.empty(); // INXL1/GRPBA1
private Optional<Integer> empiricalRelationshipParameterIndex = Optional.empty(); // INXL1/GRPBA1
private Optional<String> primarySp0;

public VdypLayer(
PolygonIdentifier polygonIdentifier, LayerType layer, Optional<Integer> inventoryTypeGroup,
Optional<Integer> empericalRelationshipParameterIndex, Optional<String> primarySp0
Optional<Integer> empiricalRelationshipParameterIndex, Optional<String> primarySp0
) {
super(polygonIdentifier, layer, inventoryTypeGroup);
this.empericalRelationshipParameterIndex = empericalRelationshipParameterIndex;
this.empiricalRelationshipParameterIndex = empiricalRelationshipParameterIndex;
this.primarySp0 = primarySp0;
}

Expand Down Expand Up @@ -161,8 +161,8 @@ public Optional<Integer> getEmpiricalRelationshipParameterIndex() {
return empiricalRelationshipParameterIndex;
}

public void setEmpericalRelationshipParameterIndex(Optional<Integer> empericalRelationshipParameterIndex) {
this.empiricalRelationshipParameterIndex = empericalRelationshipParameterIndex;
public void setEmpiricalRelationshipParameterIndex(Optional<Integer> empiricalRelationshipParameterIndex) {
this.empiricalRelationshipParameterIndex = empiricalRelationshipParameterIndex;
}

@Override
Expand Down Expand Up @@ -233,7 +233,7 @@ public VdypLayer.Builder primaryGenus(String primarySp0) {
return primaryGenus(Optional.of(primarySp0));
}

UtilizationVector loreyHeight = VdypUtilizationHolder.emptyLoreyHeightUtilization();
UtilizationVector loreyHeightByUtilization = VdypUtilizationHolder.emptyLoreyHeightUtilization();

public void loreyHeightByUtilization(float height) {
this.loreyHeightByUtilization = Utils.heightVector(0, height);
Expand Down Expand Up @@ -382,7 +382,7 @@ protected VdypLayer doBuild() {
polygonIdentifier.get(), //
layerType.get(), //
inventoryTypeGroup, //
empericalRelationshipParameterIndex, //
empiricalRelationshipParameterIndex, //
primarySp0
);
}
Expand All @@ -396,29 +396,19 @@ protected VdypSpecies buildSpecies(Consumer<VdypSpecies.Builder> config) {
});
}

public void baseAreaByUtilization(UtilizationVector utilizationVector) {
// TODO Auto-generated method stub

}

@Override
public <S2 extends BaseVdypSpecies<I2>, I2 extends BaseVdypSite>
BaseVdypLayer.Builder<VdypLayer, VdypSpecies, VdypSite, VdypSpecies.Builder, VdypSite.Builder>
adaptSpecies(
BaseVdypLayer<S2, ?> toCopy, BiConsumer<VdypSpecies.Builder, S2> config
) {
adaptSpecies(BaseVdypLayer<S2, ?> toCopy, BiConsumer<VdypSpecies.Builder, S2> config) {
this.primaryGenus(toCopy.getPrimaryGenus());
return super.adaptSpecies(toCopy, config);
}

@Override
public BaseVdypLayer.Builder<VdypLayer, VdypSpecies, VdypSite, VdypSpecies.Builder, VdypSite.Builder>
copySpecies(
VdypLayer toCopy, BiConsumer<VdypSpecies.Builder, VdypSpecies> config
) {
copySpecies(VdypLayer toCopy, BiConsumer<VdypSpecies.Builder, VdypSpecies> config) {
this.primaryGenus(toCopy.getPrimaryGenus());
return super.copySpecies(toCopy, config);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void testWritePolygon() throws IOException {
});

// FIXME Add to builder
layer.setEmpericalRelationshipParameterIndex(Optional.of(119));
layer.setEmpiricalRelationshipParameterIndex(Optional.of(119));
layer.setInventoryTypeGroup(Optional.of(28));

unit.writePolygon(polygon);
Expand Down Expand Up @@ -369,7 +369,7 @@ void testWritePolygonWithChildren() throws IOException {
var species = layer.getSpecies().get("PL");

// fixme add to builder
layer.setEmpericalRelationshipParameterIndex(Optional.of(119));
layer.setEmpiricalRelationshipParameterIndex(Optional.of(119));
layer.setInventoryTypeGroup(Optional.of(28));

layer.setBaseAreaByUtilization(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package ca.bc.gov.nrs.vdyp.model;

import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import org.hamcrest.Matchers;
import static org.hamcrest.Matchers.is;

import java.util.Optional;

import org.junit.jupiter.api.Test;

class VdypUtilizationTest {
Expand Down
Loading

0 comments on commit b1b7d54

Please sign in to comment.