From a751329d07368081678602c0ddd212d16a51e1cc Mon Sep 17 00:00:00 2001 From: Leo Date: Tue, 18 Jun 2024 14:23:17 -0600 Subject: [PATCH] applied style --- .../main/java/ucar/nc2/filter/Classifier.java | 106 ++++-------------- .../ncml/enhance/testAddToClassifier.ncml | 8 +- .../ucar/nc2/ncml/TestEnhanceClassifier.java | 28 +++-- 3 files changed, 46 insertions(+), 96 deletions(-) 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 e501f39f72..40842fa8b5 100644 --- a/cdm/core/src/main/java/ucar/nc2/filter/Classifier.java +++ b/cdm/core/src/main/java/ucar/nc2/filter/Classifier.java @@ -2,59 +2,26 @@ import java.io.IOException; import ucar.ma2.Array; -import ucar.ma2.DataType; import ucar.ma2.IndexIterator; import ucar.nc2.NetcdfFile; import ucar.nc2.Variable; import ucar.nc2.dataset.NetcdfDatasets; import ucar.nc2.dataset.VariableDS; import ucar.nc2.Attribute; -import ucar.nc2.dataset.NetcdfDatasets; -import ucar.nc2.constants.CDM; -import java.nio.*; -import java.util.HashMap; -import java.util.Map; -import ucar.nc2.Attribute; -import ucar.nc2.Variable; import java.util.ArrayList; import java.util.List; import java.util.Arrays; -import java.util.Arrays; - -import static ucar.ma2.DataType.*; -import static ucar.ma2.MAMath.nearlyEquals; public class Classifier implements Enhancement { private Classifier classifier = null; private static Classifier emptyClassifier; - private static Classifier specidifedClassifier; private int classifiedVal; - private static int CatStart; - private static int CatEnd; - private static int CatLabel; - private int[] classifiedArray; private List AttCat; private List rules = new ArrayList<>(); public static Classifier createFromVariable(VariableDS var) { try { Array arr = var.read(); -// // DataType type = var.getDataType(); - Attribute classifier_config = var.findAttribute(CDM.RANGE_CAT); - if (classifier_config != null && !classifier_config.isString()) { - System.out.println(classifier_config.getNumericValue()); - -// var.remove(scaleAtt); - } - return emptyClassifier(); - } catch (IOException e) { - return emptyClassifier(); - } - } - - public static Classifier createFromVariable(Variable var) { - try { - Array arr = var.read(); return emptyClassifier(); } catch (IOException e) { return emptyClassifier(); @@ -69,22 +36,22 @@ public static Classifier emptyClassifier() { /** Enough of a constructor */ public Classifier() {} + public Classifier(List AttCat) { this.AttCat = AttCat; this.rules = loadClassificationRules(); } - public int[] classifyWithAttributes (Array arr){ + public int[] classifyWithAttributes(Array arr) { int[] classifiedArray = new int[(int) arr.getSize()]; IndexIterator iterArr = arr.getIndexIterator(); - int i =0; + int i = 0; while (iterArr.hasNext()) { Number value = (Number) iterArr.getObjectNext(); if (!Double.isNaN(value.doubleValue())) { - classifiedArray[i] = classifyArrayAttribute(value.doubleValue(),this.rules); - } - else { + classifiedArray[i] = classifyArrayAttribute(value.doubleValue()); + } else { classifiedArray[i] = Integer.MIN_VALUE; } i++; @@ -109,10 +76,6 @@ public int[] classifyDoubleArray(Array arr) { return classifiedArray; } - - - - /** for a single double */ public int classifyArray(double val) { if (val >= 0) { @@ -120,17 +83,16 @@ public int classifyArray(double val) { } else { classifiedVal = 0; } - return classifiedVal; } - public int classifyArrayAttribute(double val, List rules) { + public int classifyArrayAttribute(double val) { for (int[] rule : rules) { if (val > rule[0] && val <= rule[1]) { - return rule[2]; // Return the matched rule's value + return rule[2]; // Return the matched rule's value } } - // Return Integer.MIN_VALUE if no rule matches + // Return min possible int if no rule matches return Integer.MIN_VALUE; } @@ -147,58 +109,34 @@ public List loadClassificationRules() { public double convert(double val) { return emptyClassifier.classifyArray(val); } + public static int[] stringToIntArray(String str) { String[] stringArray = str.split(" "); // Split the string by spaces int[] intArray = new int[stringArray.length]; // Create an array to hold the parsed integers for (int i = 0; i < stringArray.length; i++) { - try { - double value = Double.parseDouble(stringArray[i]); // Parse each string to a double - - if (Double.isNaN(value)) { - // Check if the entry is NaN and assign Integer.MIN_VALUE or Integer.MAX_VALUE based on the index - if (i == 0) { - intArray[i] = Integer.MIN_VALUE; - } else if (i == 1) { - intArray[i] = Integer.MAX_VALUE; - } else { - intArray[i] = -99999; // Default value for other indices if needed - } + + double value = Double.parseDouble(stringArray[i]); // Parse each string to a double + + if (Double.isNaN(value)) { + // Check if the entry is NaN and assign Integer.MIN_VALUE or Integer.MAX_VALUE based on the index + if (i == 0) { + intArray[i] = Integer.MIN_VALUE; + } else if (i == 1) { + intArray[i] = Integer.MAX_VALUE; } else { - intArray[i] = (int) value; // Convert the value to int if it is not NaN + intArray[i] = -99999; // Default value for other indices if needed } - } catch (NumberFormatException e) { - // Handle the case where the string cannot be parsed to a number - intArray[i] = 0; // Default value for invalid numbers + } else { + intArray[i] = (int) value; // Convert the value to int if it is not NaN } - } - - return intArray; - } - - public static void main(String[] args) { - - String dataDir = "/Users/lmatak/Desktop/netCDF-Java/netcdf-java/cdm/core/src/test/data/ncml/enhance/"; - - try (NetcdfFile ncfile = NetcdfDatasets.openDataset(dataDir + "testAddToClassifier.ncml", true, null)) { - Variable Classify_Specsx = ncfile.findVariable("class_specs"); - Array Data = Classify_Specsx.read(); - List Whatever = Classify_Specsx.getAttributes(); - Classifier classifier = new Classifier(Whatever); - int[] ClassifiedArray = classifier.classifyWithAttributes(Data); - // Print the entire array - System.out.println(Arrays.toString(ClassifiedArray)); - - } catch (IOException e) { - throw new RuntimeException(e); } - + return intArray; } - } diff --git a/cdm/core/src/test/data/ncml/enhance/testAddToClassifier.ncml b/cdm/core/src/test/data/ncml/enhance/testAddToClassifier.ncml index 31199a5cdd..0e9d5ac343 100644 --- a/cdm/core/src/test/data/ncml/enhance/testAddToClassifier.ncml +++ b/cdm/core/src/test/data/ncml/enhance/testAddToClassifier.ncml @@ -7,12 +7,12 @@ - - + + - - -5000 -20 -10 0 1 2 3 11 25 29 99 100 150 121 102 + + -500000 NaN -10 0 1 2 3 11 25 29 NaN 100 150 121 102 199999 12211 diff --git a/cdm/core/src/test/java/ucar/nc2/ncml/TestEnhanceClassifier.java b/cdm/core/src/test/java/ucar/nc2/ncml/TestEnhanceClassifier.java index 8035cbf781..c7b514fd5c 100644 --- a/cdm/core/src/test/java/ucar/nc2/ncml/TestEnhanceClassifier.java +++ b/cdm/core/src/test/java/ucar/nc2/ncml/TestEnhanceClassifier.java @@ -4,12 +4,16 @@ import static ucar.ma2.MAMath.nearlyEquals; import java.io.IOException; +import java.util.Arrays; +import java.util.List; import org.junit.Test; import ucar.ma2.Array; import ucar.ma2.DataType; +import ucar.nc2.Attribute; import ucar.nc2.NetcdfFile; import ucar.nc2.Variable; import ucar.nc2.dataset.NetcdfDatasets; +import ucar.nc2.filter.Classifier; import ucar.unidata.util.test.TestDir; public class TestEnhanceClassifier { @@ -22,7 +26,9 @@ public class TestEnhanceClassifier { public static final Array DATA_all_zeroes = Array.makeFromJavaArray(all_zeroes); public static final int[] mixNumbers = {1, 0, 1, 1, 0}; public static final Array DATA_mixNumbers = Array.makeFromJavaArray(mixNumbers); - + public static final int[] Classification_test = + {0, -2147483648, 0, 0, 10, 10, 10, 100, 100, 100, -2147483648, 100, 1000, 1000, 1000, 1000, 1000}; + public static final Array CLASSIFICATION_TEST = Array.makeFromJavaArray(Classification_test); /** test on doubles, all positives, all negatives and a mixed array */ @Test @@ -112,18 +118,24 @@ public void testEnhanceClassifier_integers() throws IOException { } } + @Test public void testEnhanceClassifier_classification() throws IOException { + try (NetcdfFile ncfile = NetcdfDatasets.openDataset(dataDir + "testAddToClassifier.ncml", true, null)) { - Variable class_specs = ncfile.findVariable("class_specs"); - assertThat((Object) class_specs).isNotNull(); - assertThat(!class_specs.attributes().isEmpty()); - assertThat(class_specs.attributes().hasAttribute("habla")); - Array numbers_to_classify = class_specs.read(); - /** So here we are, we want to read an array and apply the attribute classifications */ -// assertThat(nearlyEquals(datafloats, DATA_all_ones)).isTrue(); + Variable Classify_Specsx = ncfile.findVariable("class_specs"); + assertThat((Object) Classify_Specsx).isNotNull(); + assertThat(!Classify_Specsx.attributes().isEmpty()).isTrue(); + Array Data = Classify_Specsx.read(); + List Whatever = Classify_Specsx.getAttributes(); + Classifier classifier = new Classifier(Whatever); + int[] ClassifiedArray = classifier.classifyWithAttributes(Data); + assertThat(nearlyEquals(Array.makeFromJavaArray(ClassifiedArray), CLASSIFICATION_TEST)).isTrue(); + + } catch (IOException e) { + throw new RuntimeException(e); } }