Skip to content

Commit c46f966

Browse files
authored
Merge pull request #1393 from haileyajohnson/enhancements
refactor some of the enhancement SPI lgoic and provider interface
2 parents 5fa45ef + 3b509f6 commit c46f966

File tree

7 files changed

+43
-43
lines changed

7 files changed

+43
-43
lines changed

cdm/core/src/main/java/ucar/nc2/constants/CDM.java

-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public class CDM {
5757
public static final String TIME_OFFSET = "time offset from runtime";
5858
public static final String TIME_OFFSET_HOUR = "hoursFrom0z";
5959
public static final String RUNTIME_COORDINATE = "runtimeCoordinate";
60-
public static final String STANDARDIZE = "standardize";
61-
public static final String NORMALIZE = "normalize";
62-
public static final String CLASSIFY = "classify";
6360

6461
// Special attributes
6562

cdm/core/src/main/java/ucar/nc2/dataset/NetcdfDataset.java

+6-16
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,15 @@ public enum Enhance {
116116
*/
117117
IncompleteCoordSystems,
118118
/**
119-
* Calculate mean and standard deviation and apply to data: (z-mean)/standard_deviation.
120-
* If the enhanced data type is not {@code FLOAT} or {@code DOUBLE}, this has no effect.
119+
* All other Enhancement implementations that are loaded by service provider
120+
* This includes third party implementations found on the classpath and loaded at runtime.
121121
*/
122-
ApplyStandardizer,
123-
/**
124-
* Calculate minimum value and range (maximum - minimum) and apply to data: (z - min)/range.
125-
* If the enhanced data type is not {@code FLOAT} or {@code DOUBLE}, this has no effect.
126-
*/
127-
ApplyNormalizer,
128-
/**
129-
* Classify doubles or floats based on positive/negative into 1 or 0 {@code}
130-
* x<0 --> 0 x>0 --> 1
131-
*/
132-
ApplyClassifier,
122+
ApplyRuntimeLoadedEnhancements,
133123
}
134124

135-
private static Set<Enhance> EnhanceAll = Collections.unmodifiableSet(
136-
EnumSet.of(Enhance.ConvertEnums, Enhance.ConvertUnsigned, Enhance.ApplyScaleOffset, Enhance.ConvertMissing,
137-
Enhance.CoordSystems, Enhance.ApplyStandardizer, Enhance.ApplyNormalizer, Enhance.ApplyClassifier));
125+
private static Set<Enhance> EnhanceAll =
126+
Collections.unmodifiableSet(EnumSet.of(Enhance.ConvertEnums, Enhance.ConvertUnsigned, Enhance.ApplyScaleOffset,
127+
Enhance.ConvertMissing, Enhance.CoordSystems, Enhance.ApplyRuntimeLoadedEnhancements));
138128
private static Set<Enhance> EnhanceNone = Collections.unmodifiableSet(EnumSet.noneOf(Enhance.class));
139129
private static Set<Enhance> defaultEnhanceMode = EnhanceAll;
140130

cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ private void createEnhancements() {
921921

922922
if (this.enhanceMode.contains(Enhance.ConvertUnsigned)) {
923923
this.unsignedConversion = UnsignedConversion.createFromVar(this);
924-
this.dataType = unsignedConversion != null ? unsignedConversion.getOutType() : dataType;
924+
this.dataType = unsignedConversion.getOutType();
925925
}
926926
if (this.enhanceMode.contains(Enhance.ConvertMissing)) {
927927
this.convertMissing = ConvertMissing.createFromVariable(this);
@@ -932,9 +932,11 @@ private void createEnhancements() {
932932
}
933933
this.dataType = scaleOffset != null ? scaleOffset.getScaledOffsetType() : this.dataType;
934934
}
935-
for (Enhance enhance : this.enhanceMode) {
935+
936+
if (this.enhanceMode.contains(Enhance.ApplyRuntimeLoadedEnhancements)) {
936937
for (EnhancementProvider service : ENHANCEMENT_PROVIDERS) {
937-
if (service.appliesTo(enhance, this.attributes(), dataType)) {
938+
if (this.attributes().findAttribute(service.getAttributeName()) != null
939+
&& service.appliesTo(this.enhanceMode, dataType)) {
938940
loadedEnhancements.add(service.create(this));
939941
}
940942
}

cdm/core/src/main/java/ucar/nc2/filter/Classifier.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
import ucar.nc2.util.Misc;
1010
import java.util.ArrayList;
1111
import java.util.List;
12+
import java.util.Set;
13+
1214
import ucar.ma2.*;
1315
import ucar.nc2.*;
1416

1517

1618
public class Classifier implements Enhancement {
1719

18-
1920
private String[] AttCat;
2021
private List<int[]> rules = new ArrayList<>();
2122

2223
private static String name = "Classifier";
23-
24-
24+
private static final String ATTRIBUTE_NAME = "classify";
2525

2626
public Classifier() {
2727
this.AttCat = new String[0];
@@ -35,13 +35,12 @@ public Classifier(String[] AttCat) {
3535
}
3636

3737
// Factory method to create a Classifier from a Variable
38-
3938
public static Classifier createFromVariable(VariableDS var) {
4039
List<Attribute> attributes = var.attributes().getAttributes();
4140

4241
for (Attribute attribute : attributes) {
4342
// check like this, or something else?
44-
if (attribute == var.attributes().findAttribute(CDM.CLASSIFY)) {
43+
if (attribute == var.attributes().findAttribute(ATTRIBUTE_NAME)) {
4544
String[] sets = attribute.getStringValue().split(";");
4645
for (int i = 0; i < sets.length; i++) {
4746
// trim and clean so it's ready
@@ -52,11 +51,8 @@ public static Classifier createFromVariable(VariableDS var) {
5251
}
5352

5453
return new Classifier();
55-
5654
}
5755

58-
59-
6056
public int[] classifyWithAttributes(Array arr) {
6157
int[] classifiedArray = new int[(int) arr.getSize()];
6258
IndexIterator iterArr = arr.getIndexIterator();
@@ -73,8 +69,6 @@ public int[] classifyWithAttributes(Array arr) {
7369
return classifiedArray;
7470
}
7571

76-
77-
7872
public int classifyArrayAttribute(double val) {
7973
for (int[] rule : rules) {
8074
if (val > rule[0] && val <= rule[1] + Misc.defaultMaxRelativeDiffFloat) {
@@ -126,9 +120,15 @@ public static int[] stringToIntArray(String str) {
126120
}
127121

128122
public static class Provider implements EnhancementProvider {
123+
124+
@Override
125+
public String getAttributeName() {
126+
return ATTRIBUTE_NAME;
127+
}
128+
129129
@Override
130-
public boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt) {
131-
return enhance == Enhance.ApplyClassifier && attributes.findAttribute(CDM.CLASSIFY) != null && dt.isNumeric();
130+
public boolean appliesTo(Set<Enhance> enhance, DataType dt) {
131+
return dt.isNumeric();
132132
}
133133

134134
@Override

cdm/core/src/main/java/ucar/nc2/filter/EnhancementProvider.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
import ucar.nc2.dataset.NetcdfDataset.Enhance;
1111
import ucar.nc2.dataset.VariableDS;
1212

13+
import java.util.Set;
14+
1315

1416
/**
1517
* A Service Provider of {@link Enhancement}.
1618
*/
1719
public interface EnhancementProvider {
20+
String getAttributeName();
1821

19-
boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt);
22+
boolean appliesTo(Set<Enhance> enhance, DataType dt);
2023

2124
Enhancement create(VariableDS var);
2225

cdm/core/src/main/java/ucar/nc2/filter/Normalizer.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,23 @@ public double getRange() {
7979

8080
public static class Provider implements EnhancementProvider {
8181

82+
private static final String ATTRIBUTE_NAME = "normalize";
8283

8384
@Override
84-
public boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt) {
85-
return enhance == Enhance.ApplyNormalizer && attributes.findAttribute(CDM.NORMALIZE) != null
86-
&& dt.isFloatingPoint();
85+
public String getAttributeName() {
86+
return ATTRIBUTE_NAME;
87+
}
88+
89+
@Override
90+
public boolean appliesTo(Set<Enhance> enhance, DataType dt) {
91+
return dt.isFloatingPoint();
8792
}
8893

8994
@Override
9095
public Normalizer create(VariableDS var) {
9196
return Normalizer.createFromVariable(var);
9297
}
9398

94-
95-
9699
}
97100

98101
}

cdm/core/src/main/java/ucar/nc2/filter/Standardizer.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ public double getStdDev() {
8181

8282
public static class Provider implements EnhancementProvider {
8383

84+
private static final String ATTRIBUTE_NAME = "standardize";
8485

8586
@Override
86-
public boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt) {
87-
return enhance == Enhance.ApplyStandardizer && attributes.findAttribute(CDM.STANDARDIZE) != null
88-
&& dt.isFloatingPoint();
87+
public String getAttributeName() {
88+
return ATTRIBUTE_NAME;
89+
}
90+
91+
@Override
92+
public boolean appliesTo(Set<Enhance> enhance, DataType dt) {
93+
return dt.isFloatingPoint();
8994
}
9095

9196
@Override

0 commit comments

Comments
 (0)