NetcdfDataReader extends DataReader { @Override public VortexData getDto(int idx) { try (NetcdfDataset ncd = NetcdfDatasets.openDataset(path); Formatter errlog = new Formatter()) { FeatureDataset dataset = FeatureDatasetFactoryManager.wrap(FeatureType.GRID, ncd, null, errlog); if (dataset != null) { FeatureType ftype = dataset.getFeatureType(); if (ftype == FeatureType.GRID) { assert (dataset instanceof GridDataset); GridDataset gridDataset = (GridDataset) dataset; return getData(gridDataset, variableName, idx); } } else { List<Variable> variables = ncd.getVariables(); for (Variable variable : variables){ if (variable.getShortName().equals(variableName) && variable instanceof VariableDS) { VariableDS variableDS = (VariableDS) variable; List<CoordinateSystem> coordinateSystems = variableDS.getCoordinateSystems(); if (!coordinateSystems.isEmpty()) { VariableDsReader reader = VariableDsReader.builder() .setNetcdfFile(ncd) .setVariableName(variableName) .build(); return reader.read(idx); } } } } } catch (IOException e) { logger.log(Level.SEVERE, e, e::getMessage); } return null; } NetcdfDataReader(DataReaderBuilder builder); @Override List<VortexData> getDtos(); @Override VortexData getDto(int idx); @Override int getDtoCount(); static Set<String> getVariables(String path);  }
@Disabled @Test void CmipPrecipImport(){ String inFile = new File("D:/data/CMIP/Extraction_pr.nc").toString(); DataReader reader = DataReader.builder() .path(inFile) .variable("pr") .build(); VortexData vortexData = reader.getDto(0); VortexGrid vortexGrid = (VortexGrid) vortexData; float[] data = vortexGrid.data(); List<Double> values = new ArrayList<>(); for (int i = 1; i < data.length; i++) { if (!Float.isNaN(data[i])) { values.add((double) data[i]); } } double max = Collections.max(values); double min = Collections.min(values); assertEquals(66.21134185791016, max, 1E-5); assertEquals(0.04108993336558342, min, 1E-5); }
@Test void ArizonaSweDaily(){ String inFile = new File(getClass().getResource("/01.nc").getFile()).toString(); DataReader reader = DataReader.builder() .path(inFile) .variable("SWE") .build(); VortexData vortexData = reader.getDto(0); VortexGrid vortexGrid = (VortexGrid) vortexData; assertEquals(ZonedDateTime.of(2020, 3, 1, 0, 0, 0, 0, ZoneId.of("Z")), vortexGrid.startTime()); assertEquals(ZonedDateTime.of(2020, 3, 2, 0, 0, 0, 0, ZoneId.of("Z")), vortexGrid.endTime()); }