Skip to content

Commit

Permalink
Make it shorter and nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
matakleo authored and haileyajohnson committed Jul 17, 2024
1 parent bfbc4ff commit 74f6a03
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 92 deletions.
19 changes: 6 additions & 13 deletions cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,8 @@ Array convert(Array data, Set<NetcdfDataset.Enhance> enhancements) {
toApply.add(scaleOffset);
convertedType = scaleOffset.getScaledOffsetType();
}
/** this == variableDS */
for (Enhance enhance : enhancements) {
for (EnhancementProvider service : ServiceLoader.load(EnhancementProvider.class)) {
if (service.appliesTo(enhance, this)) {
toApply.add(service.returnObject(this));
}
}
}

toApply.addAll(loadedEnhancements);


double[] dataArray = (double[]) data.get1DJavaArray(DataType.DOUBLE);
Expand Down Expand Up @@ -870,9 +864,8 @@ public Array convert(Array in, boolean convertUnsigned, boolean applyScaleOffset
// TODO make immutable in version 6
private UnsignedConversion unsignedConversion;
private ScaleOffset scaleOffset;
public Standardizer standardizer;
public Normalizer normalizer;
public Classifier classifier;
private List<Enhancement> loadedEnhancements = new ArrayList<>();

private ConvertMissing convertMissing;
private Set<Enhance> enhanceMode = EnumSet.noneOf(Enhance.class); // The set of enhancements that were made.

Expand Down Expand Up @@ -943,8 +936,8 @@ private void createEnhancements() {
for (Enhance enhance : this.enhanceMode) {
for (EnhancementProvider service : ServiceLoader.load(EnhancementProvider.class)) {
/** Stand and Norm require floatingpoint, buc classifier can do just numeric? */
if (service.appliesTo(enhance, this.attributes()) && dataType.isFloatingPoint()) {
service.Create(this);
if (service.appliesTo(enhance, this.attributes(), dataType)) {
loadedEnhancements.add(service.returnObject(this));
}
}
}
Expand Down
32 changes: 3 additions & 29 deletions cdm/core/src/main/java/ucar/nc2/filter/Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,48 +147,22 @@ public static int[] stringToIntArray(String str) {

public static class Provider implements EnhancementProvider {

@Override
public void Create(VariableDS var) {
var.classifier = Classifier.createFromVariable(var);
}



@Override
public String getName() {
return name;
}

@Override
public boolean canDo(Set<Enhance> enhancements) {
if (enhancements.contains(Enhance.ApplyClassifier)) {
return true;
}
return false;
}

// Attribute findAttribute(String attName);

@Override
public boolean appliesTo(Enhance enhance, AttributeContainer attributes) {
return enhance == Enhance.ApplyClassifier && attributes.findAttribute(CDM.CLASSIFY) != null;
}

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

public Classifier returnObject(VariableDS var) {
return var.classifier;
return createFromVariable(var);
}



}



}


Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ public interface EnhancementProvider {

String getName();

boolean canDo(Set<ucar.nc2.dataset.NetcdfDataset.Enhance> enhancements);

boolean appliesTo(Enhance enhance, AttributeContainer attributes);

boolean appliesTo(Enhance enhance, VariableDS var);

void Create(VariableDS var);
boolean appliesTo(Enhance enhance, AttributeContainer attributes, DataType dt);

Enhancement returnObject(VariableDS var);

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

public static class Provider implements EnhancementProvider {

@Override
public void Create(VariableDS var) {
var.normalizer = Normalizer.createFromVariable(var);

}

@Override
public String getName() {
return name;
}

@Override
public boolean canDo(Set<Enhance> enhancements) {
if (enhancements.contains(Enhance.ApplyNormalizer)) {
return true;
}
return false;
}

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

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

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


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

public static class Provider implements EnhancementProvider {

@Override
public void Create(VariableDS var) {
var.standardizer = Standardizer.createFromVariable(var);

}

@Override
public String getName() {
return name;
}

@Override
public boolean canDo(Set<Enhance> enhancements) {
if (enhancements.contains(Enhance.ApplyStandardizer)) {
return true;
}
return false;
}

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

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

@Override
public Standardizer returnObject(VariableDS var) {
return var.standardizer;
return createFromVariable(var);
}
}
}
Expand Down

0 comments on commit 74f6a03

Please sign in to comment.