From 74f6a03806a67e8455aaedd0a4e9d40f5d315d48 Mon Sep 17 00:00:00 2001 From: Leo Date: Wed, 17 Jul 2024 12:33:31 -0500 Subject: [PATCH] Make it shorter and nicer --- .../java/ucar/nc2/dataset/VariableDS.java | 19 ++++------- .../main/java/ucar/nc2/filter/Classifier.java | 32 ++----------------- .../ucar/nc2/filter/EnhancementProvider.java | 8 +---- .../main/java/ucar/nc2/filter/Normalizer.java | 26 +++------------ .../java/ucar/nc2/filter/Standardizer.java | 25 +++------------ 5 files changed, 18 insertions(+), 92 deletions(-) diff --git a/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java b/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java index 0091766593..626b4e9de8 100644 --- a/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java +++ b/cdm/core/src/main/java/ucar/nc2/dataset/VariableDS.java @@ -287,14 +287,8 @@ Array convert(Array data, Set 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); @@ -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 loadedEnhancements = new ArrayList<>(); + private ConvertMissing convertMissing; private Set enhanceMode = EnumSet.noneOf(Enhance.class); // The set of enhancements that were made. @@ -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)); } } } diff --git a/cdm/core/src/main/java/ucar/nc2/filter/Classifier.java b/cdm/core/src/main/java/ucar/nc2/filter/Classifier.java index 0ad3a1ff81..6e8b1fb9bc 100644 --- a/cdm/core/src/main/java/ucar/nc2/filter/Classifier.java +++ b/cdm/core/src/main/java/ucar/nc2/filter/Classifier.java @@ -147,12 +147,6 @@ 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() { @@ -160,35 +154,15 @@ public String getName() { } @Override - public boolean canDo(Set 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); } - - - } - - } diff --git a/cdm/core/src/main/java/ucar/nc2/filter/EnhancementProvider.java b/cdm/core/src/main/java/ucar/nc2/filter/EnhancementProvider.java index b2718d9c33..614a8667f9 100644 --- a/cdm/core/src/main/java/ucar/nc2/filter/EnhancementProvider.java +++ b/cdm/core/src/main/java/ucar/nc2/filter/EnhancementProvider.java @@ -32,13 +32,7 @@ public interface EnhancementProvider { String getName(); - boolean canDo(Set 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); diff --git a/cdm/core/src/main/java/ucar/nc2/filter/Normalizer.java b/cdm/core/src/main/java/ucar/nc2/filter/Normalizer.java index 11a82e4974..65ea5880c1 100644 --- a/cdm/core/src/main/java/ucar/nc2/filter/Normalizer.java +++ b/cdm/core/src/main/java/ucar/nc2/filter/Normalizer.java @@ -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 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); } diff --git a/cdm/core/src/main/java/ucar/nc2/filter/Standardizer.java b/cdm/core/src/main/java/ucar/nc2/filter/Standardizer.java index a0a5f6c4da..6ca0620b2d 100644 --- a/cdm/core/src/main/java/ucar/nc2/filter/Standardizer.java +++ b/cdm/core/src/main/java/ucar/nc2/filter/Standardizer.java @@ -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 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); } } }