Skip to content

Commit

Permalink
Merge pull request #1393 from haileyajohnson/enhancements
Browse files Browse the repository at this point in the history
refactor some of the enhancement SPI lgoic and provider interface
  • Loading branch information
tdrwenski authored Oct 15, 2024
2 parents 5fa45ef + 3b509f6 commit c46f966
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 43 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
22 changes: 6 additions & 16 deletions cdm/core/src/main/java/ucar/nc2/dataset/NetcdfDataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,15 @@ public enum Enhance {
*/
IncompleteCoordSystems,
/**
* Calculate mean and standard deviation and apply to data: (z-mean)/standard_deviation.
* If the enhanced data type is not {@code FLOAT} or {@code DOUBLE}, this has no effect.
* All other Enhancement implementations that are loaded by service provider
* This includes third party implementations found on the classpath and loaded at runtime.
*/
ApplyStandardizer,
/**
* Calculate minimum value and range (maximum - minimum) and apply to data: (z - min)/range.
* If the enhanced data type is not {@code FLOAT} or {@code DOUBLE}, this has no effect.
*/
ApplyNormalizer,
/**
* Classify doubles or floats based on positive/negative into 1 or 0 {@code}
* x<0 --> 0 x>0 --> 1
*/
ApplyClassifier,
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.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
24 changes: 12 additions & 12 deletions cdm/core/src/main/java/ucar/nc2/filter/Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
import ucar.nc2.util.Misc;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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


public class Classifier implements Enhancement {


private String[] AttCat;
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 @@ -35,13 +35,12 @@ public Classifier(String[] AttCat) {
}

// Factory method to create a Classifier from a Variable

public static Classifier createFromVariable(VariableDS var) {
List<Attribute> attributes = var.attributes().getAttributes();

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 All @@ -52,11 +51,8 @@ public static Classifier createFromVariable(VariableDS var) {
}

return new Classifier();

}



public int[] classifyWithAttributes(Array arr) {
int[] classifiedArray = new int[(int) arr.getSize()];
IndexIterator iterArr = arr.getIndexIterator();
Expand All @@ -73,8 +69,6 @@ public int[] classifyWithAttributes(Array arr) {
return classifiedArray;
}



public int classifyArrayAttribute(double val) {
for (int[] rule : rules) {
if (val > rule[0] && val <= rule[1] + Misc.defaultMaxRelativeDiffFloat) {
Expand Down Expand Up @@ -126,9 +120,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 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
13 changes: 8 additions & 5 deletions cdm/core/src/main/java/ucar/nc2/filter/Normalizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,23 @@ 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 dt.isFloatingPoint();
}

@Override
public Normalizer create(VariableDS var) {
return Normalizer.createFromVariable(var);
}



}

}
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 dt.isFloatingPoint();
}

@Override
Expand Down

0 comments on commit c46f966

Please sign in to comment.