Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VDYP-206 FIPStart output to VRIAdjust input format #26

Merged
merged 15 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ca.bc.gov.nrs.vdyp.common;

public class ControlKeys {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't these a specific kind of control key? If so, perhaps rename the class. Also, wouldn't this be better as an enumeration?


private ControlKeys() {
}

public static final String VDYP_POLYGON = "VDYP_POLYGON";
public static final String VDYP_LAYER_BY_SPECIES = "VDYP_LAYER_BY_SPECIES";
public static final String VDYP_LAYER_BY_SP0_BY_UTIL = "VDYP_LAYER_BY_SP0_BY_UTIL";

}
15 changes: 15 additions & 0 deletions 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 @@ -11,6 +11,8 @@

import javax.annotation.Nullable;

import ca.bc.gov.nrs.vdyp.model.Coefficients;

public class Utils {

private Utils() {
Expand Down Expand Up @@ -76,4 +78,17 @@ public static <K, V> Map<K, V> constMap(Consumer<Map<K, V>> body) {
body.accept(map);
return Collections.unmodifiableMap(map);
}

public static Coefficients utilizationVector(float small, float all, float u1, float u2, float u3, float u4) {
return new Coefficients(new float[] { small, all, u1, u2, u3, u4 }, -1);
}

public static Coefficients utilizationVector(float small, float u1, float u2, float u3, float u4) {
return new Coefficients(new float[] { small, u1 + u2 + u3 + u4, u1, u2, u3, u4 }, -1);
}

public static Coefficients utilizationVector() {
return new Coefficients(new float[] { 0f, 0f, 0f, 0f, 0f, 0f }, -1);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ca.bc.gov.nrs.vdyp.common_calculators;

import static ca.bc.gov.nrs.vdyp.math.FloatMath.sqrt;

/*
* Converts between trees per hectare and quad mean diameter given a base area
*/
public class BaseAreaTreeDensityDiameter {

private BaseAreaTreeDensityDiameter() {
}

public static final float PI_40K = 0.78539816E-04f;

// FT_BD
public static float treesPerHectare(float baseArea, float quadraticMeanDiameter) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should adopt a naming convention wherein units are always suffixed to variable names. In this case, "baseArea" is a number, but what are the units? It's apparent that they're hectares but this should be made explicit. For time, I always use _ms = milliseconds, _s = seconds, and _m, _h, _d, _w, _y. For area, I suggest _h for hectares. I'm not sure what other ones are used in the system. Suggestions?

if (baseArea != 0) {
return baseArea / PI_40K / (quadraticMeanDiameter * quadraticMeanDiameter);
}
return 0f;
}

// FD_BT
public static float quadMeanDiameter(float baseArea, float treesPerHectare) {
if (baseArea > 1e6f || treesPerHectare > 1e6f || Float.isNaN(baseArea) || Float.isNaN(treesPerHectare)) {
return 0f;
} else if (baseArea > 0f && treesPerHectare > 0f) {
return sqrt(baseArea / treesPerHectare / PI_40K);
}
return 0f;

}

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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public interface FileResolver {
InputStream resolve(String filename) throws IOException;
InputStream resolveForInput(String filename) throws IOException;

OutputStream resolveForOutput(String filename) throws IOException;

String toString(String filename) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package ca.bc.gov.nrs.vdyp.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileSystemFileResolver implements FileResolver {

@Override
public InputStream resolve(String filename) throws IOException {
public InputStream resolveForInput(String filename) throws IOException {
return new FileInputStream(filename);
}

@Override
public OutputStream resolveForOutput(String filename) throws IOException {
return new FileOutputStream(filename);
}

@Override
public String toString(String filename) throws IOException {
return String.format("file:%s", filename);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -24,8 +23,8 @@ public class ControlFileParser implements ResourceParser<Map<String, Object>> {
public static final int CONTROL_LENGTH_EXTENDED = 120;
public static final int CONTROL_LENGTH = 50;

public static final List<String> EXTEND_FLAGS = Arrays.asList("X", ">");
public static final List<String> COMMENT_FLAGS = Arrays.asList("C");
public static final List<String> EXTEND_FLAGS = List.of("X", ">");
public static final List<String> COMMENT_FLAGS = List.of("C");
public static final String COMMENT_MARKER = "!";

private Map<Integer, String> identifiers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class GenusDefinitionParser implements ControlMapSubResourceParser<List<G

public static final String CONTROL_KEY = "SP0_DEF";

private int num_sp0;
private int numSp0;

LineParser lineParser = new LineParser().strippedString(2, "alias").space(1).strippedString(32, "name").space(1)
.value(
Expand All @@ -33,19 +33,19 @@ public class GenusDefinitionParser implements ControlMapSubResourceParser<List<G

public GenusDefinitionParser() {
super();
this.num_sp0 = 16;
this.numSp0 = 16;
}

public GenusDefinitionParser(int num_sp0) {
super();
this.num_sp0 = num_sp0;
this.numSp0 = num_sp0;
}

@SuppressWarnings("unchecked")
@Override
public List<GenusDefinition> parse(InputStream is, Map<String, Object> control)
throws IOException, ResourceParseException {
GenusDefinition[] result = new GenusDefinition[num_sp0];
GenusDefinition[] result = new GenusDefinition[numSp0];
result = lineParser.parse(is, result, (v, r) -> {
String alias = (String) v.get("alias");
Optional<Integer> preference = (Optional<Integer>) v.get("preference");
Expand All @@ -55,9 +55,9 @@ public List<GenusDefinition> parse(InputStream is, Map<String, Object> control)
var defn = new GenusDefinition(alias, preference, name);
int p = preference.orElse(lineNumber);

if (p > num_sp0) {
if (p > numSp0) {
throw new ValueParseException(
Integer.toString(p), String.format("Preference %d is larger than %d", p, num_sp0)
Integer.toString(p), String.format("Preference %d is larger than %d", p, numSp0)
);
}
if (p < 1) {
Expand Down Expand Up @@ -96,7 +96,7 @@ public static List<GenusDefinition> getSpecies(final Map<String, Object> control
}

public static List<String> getSpeciesAliases(final Map<String, Object> controlMap) {
return getSpecies(controlMap).stream().map(GenusDefinition::getAlias).collect(Collectors.toList());
return getSpecies(controlMap).stream().map(GenusDefinition::getAlias).toList();
}

/**
Expand All @@ -111,6 +111,10 @@ public static GenusDefinition getSpeciesByIndex(final int index, final Map<Strin
return getSpecies(controlMap).get(index - 1);
}

public static Optional<Integer> getIndex(final String alias, final Map<String, Object> controlMap) {
return Optional.of(getSpeciesAliases(controlMap).indexOf(alias) + 1).filter(x -> x != 0);
}

@Override
public String getControlKey() {
return CONTROL_KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default void modify(Map<String, Object> control, FileResolver fileResolver)
@SuppressWarnings("unchecked")
var filename = (Optional<String>) control.get(getControlKey());
if (filename.isPresent()) {
try (InputStream data = fileResolver.resolve(filename.get())) {
try (InputStream data = fileResolver.resolveForInput(filename.get())) {
modify(control, data);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface ResourceControlMapModifier extends ControlMapModifier, KeyedCon
default void modify(Map<String, Object> control, FileResolver fileResolver)
throws IOException, ResourceParseException {
var filename = (String) control.get(getControlKey());
try (InputStream data = fileResolver.resolve(filename)) {
try (InputStream data = fileResolver.resolveForInput(filename)) {
modify(control, data);
}
}
Expand Down
Loading