Skip to content

Commit

Permalink
refactor some of the enhancement SPI lgoic and provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyajohnson committed Oct 14, 2024
1 parent 5fa45ef commit d432159
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 21 deletions.
3 changes: 0 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/constants/CDM.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public class CDM {
public static final String TIME_OFFSET = "time offset from runtime";
public static final String TIME_OFFSET_HOUR = "hoursFrom0z";
public static final String RUNTIME_COORDINATE = "runtimeCoordinate";
public static final String STANDARDIZE = "standardize";
public static final String NORMALIZE = "normalize";
public static final String CLASSIFY = "classify";

// Special attributes

Expand Down
12 changes: 9 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/dataset/NetcdfDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,17 @@ public enum Enhance {
* x<0 --> 0 x>0 --> 1
*/
ApplyClassifier,
/**
* All other Enhancement implementations that are loaded by service provider
* This includes third party implementations found on the classpath and loaded at runtime.
*/
ApplyRuntimeLoadedEnhancements,
}

private static Set<Enhance> EnhanceAll = Collections.unmodifiableSet(
EnumSet.of(Enhance.ConvertEnums, Enhance.ConvertUnsigned, Enhance.ApplyScaleOffset, Enhance.ConvertMissing,
Enhance.CoordSystems, Enhance.ApplyStandardizer, Enhance.ApplyNormalizer, Enhance.ApplyClassifier));
private static Set<Enhance> EnhanceAll =
Collections.unmodifiableSet(EnumSet.of(Enhance.ConvertEnums, Enhance.ConvertUnsigned, Enhance.ApplyScaleOffset,
Enhance.ConvertMissing, Enhance.CoordSystems, Enhance.ApplyStandardizer, Enhance.ApplyNormalizer,
Enhance.ApplyClassifier, Enhance.ApplyRuntimeLoadedEnhancements));
private static Set<Enhance> EnhanceNone = Collections.unmodifiableSet(EnumSet.noneOf(Enhance.class));
private static Set<Enhance> defaultEnhanceMode = EnhanceAll;

Expand Down
8 changes: 5 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ private void createEnhancements() {

if (this.enhanceMode.contains(Enhance.ConvertUnsigned)) {
this.unsignedConversion = UnsignedConversion.createFromVar(this);
this.dataType = unsignedConversion != null ? unsignedConversion.getOutType() : dataType;
this.dataType = unsignedConversion.getOutType();
}
if (this.enhanceMode.contains(Enhance.ConvertMissing)) {
this.convertMissing = ConvertMissing.createFromVariable(this);
Expand All @@ -932,9 +932,11 @@ private void createEnhancements() {
}
this.dataType = scaleOffset != null ? scaleOffset.getScaledOffsetType() : this.dataType;
}
for (Enhance enhance : this.enhanceMode) {

if (this.enhanceMode.contains(Enhance.ApplyRuntimeLoadedEnhancements)) {
for (EnhancementProvider service : ENHANCEMENT_PROVIDERS) {
if (service.appliesTo(enhance, this.attributes(), dataType)) {
if (this.attributes().findAttribute(service.getAttributeName()) != null
&& service.appliesTo(this.enhanceMode, dataType)) {
loadedEnhancements.add(service.create(this));
}
}
Expand Down
17 changes: 12 additions & 5 deletions cdm/core/src/main/java/ucar/nc2/filter/Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import ucar.nc2.util.Misc;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import ucar.ma2.*;
import ucar.nc2.*;

Expand All @@ -20,8 +22,7 @@ public class Classifier implements Enhancement {
private List<int[]> rules = new ArrayList<>();

private static String name = "Classifier";


private static final String ATTRIBUTE_NAME = "classify";

public Classifier() {
this.AttCat = new String[0];
Expand All @@ -41,7 +42,7 @@ public static Classifier createFromVariable(VariableDS var) {

for (Attribute attribute : attributes) {
// check like this, or something else?
if (attribute == var.attributes().findAttribute(CDM.CLASSIFY)) {
if (attribute == var.attributes().findAttribute(ATTRIBUTE_NAME)) {
String[] sets = attribute.getStringValue().split(";");
for (int i = 0; i < sets.length; i++) {
// trim and clean so it's ready
Expand Down Expand Up @@ -126,9 +127,15 @@ public static int[] stringToIntArray(String str) {
}

public static class Provider implements EnhancementProvider {

@Override
public String getAttributeName() {
return ATTRIBUTE_NAME;
}

@Override
public boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt) {
return enhance == Enhance.ApplyClassifier && attributes.findAttribute(CDM.CLASSIFY) != null && dt.isNumeric();
public boolean appliesTo(Set<Enhance> enhance, DataType dt) {
return enhance.contains(Enhance.ApplyClassifier) && dt.isNumeric();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import ucar.nc2.dataset.NetcdfDataset.Enhance;
import ucar.nc2.dataset.VariableDS;

import java.util.Set;


/**
* A Service Provider of {@link Enhancement}.
*/
public interface EnhancementProvider {
String getAttributeName();

boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt);
boolean appliesTo(Set<Enhance> enhance, DataType dt);

Enhancement create(VariableDS var);

Expand Down
11 changes: 8 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/filter/Normalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ public double getRange() {

public static class Provider implements EnhancementProvider {

private static final String ATTRIBUTE_NAME = "normalize";

@Override
public boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt) {
return enhance == Enhance.ApplyNormalizer && attributes.findAttribute(CDM.NORMALIZE) != null
&& dt.isFloatingPoint();
public String getAttributeName() {
return ATTRIBUTE_NAME;
}

@Override
public boolean appliesTo(Set<Enhance> enhance, DataType dt) {
return enhance.contains(Enhance.ApplyNormalizer) && dt.isFloatingPoint();
}

@Override
Expand Down
11 changes: 8 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/filter/Standardizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ public double getStdDev() {

public static class Provider implements EnhancementProvider {

private static final String ATTRIBUTE_NAME = "standardize";

@Override
public boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt) {
return enhance == Enhance.ApplyStandardizer && attributes.findAttribute(CDM.STANDARDIZE) != null
&& dt.isFloatingPoint();
public String getAttributeName() {
return ATTRIBUTE_NAME;
}

@Override
public boolean appliesTo(Set<Enhance> enhance, DataType dt) {
return enhance.contains(Enhance.ApplyStandardizer) && dt.isFloatingPoint();
}

@Override
Expand Down

0 comments on commit d432159

Please sign in to comment.