NetcdfFile ncFile = null; try { ncFile = NetcdfFile.open(tis.getFile().getAbsolutePath()); metadata.set("File-Type-Description", ncFile.getFileTypeDescription()); for (Attribute attr : ncFile.getGlobalAttributes()) { Property property = resolveMetadataKey(attr.getFullName()); if (attr.getDataType().isString()) { metadata.add(property, attr.getStringValue()); } else if (attr.getDataType().isNumeric()) { int value = attr.getNumericValue().intValue(); xhtml.startElement("ul"); xhtml.newline(); for (Dimension dim : ncFile.getDimensions()) { xhtml.element("li", dim.getFullName() + " = " + dim.getLength()); xhtml.startElement("ul"); xhtml.newline(); for (Variable var : ncFile.getVariables()) { xhtml.startElement("li"); xhtml.characters(var.getDataType() + " " + var.getNameAndDimensions()); xhtml.newline(); List<Attribute> attributes = var.getAttributes(); if (!attributes.isEmpty()) { xhtml.startElement("ul"); } finally { if (ncFile != null) {
public static void main(String args[]) throws Exception, IOException, InstantiationException, IllegalAccessException { //String fileIn = "/home/yuanho/dev/netcdf-java-2.2/src/ucar/nc2/n0r_20040823_2215"; // uncompressed String fileIn = "c:/data/image/gini/n0r_20041013_1852"; ucar.nc2.NetcdfFile.registerIOProvider(ucar.nc2.iosp.gini.Giniiosp.class); ucar.nc2.NetcdfFile ncf = ucar.nc2.NetcdfFile.open(fileIn); List alist = ncf.getGlobalAttributes(); ucar.nc2.Variable v = ncf.findVariable("BaseReflectivity"); int[] origin = {0, 0}; int[] shape = {3000, 4736}; ArrayByte data = (ArrayByte) v.read(origin, shape); ncf.close(); }
public Variable findVariableByAttribute(Group g, String attName, String attValue) { if (g == null) g = getRootGroup(); for (Variable v : variables) { for (Attribute att : v.getAttributes()) if (attName.equals(att.getShortName()) && attValue.equals(att.getStringValue())) return v; } for (Group nested : g.getGroups()) { Variable v = findVariableByAttribute(nested, attName, attValue); if (v != null) return v; } return null; }
protected void unravelStringMet(NetcdfFile ncFile, Group group, Metadata met) { if (group == null) { group = ncFile.getRootGroup(); } // get file type met.set("File-Type-Description", ncFile.getFileTypeDescription()); // unravel its string attrs for (Attribute attribute : group.getAttributes()) { if (attribute.isString()) { met.add(attribute.getFullName(), attribute.getStringValue()); } else { // try and cast its value to a string met.add(attribute.getFullName(), String.valueOf(attribute .getNumericValue())); } } for (Group g : group.getGroups()) { unravelStringMet(ncFile, g, met); } }
for (Attribute attr : ncFile.getGlobalAttributes()) { Property property = resolveMetadataKey(attr.getFullName()); if (attr.getDataType().isString()) { metadata.add(property, attr.getStringValue()); } else if (attr.getDataType().isNumeric()) { int value = attr.getNumericValue().intValue(); xhtml.newline(); for (Dimension dim : ncFile.getDimensions()){ xhtml.element("li", dim.getFullName() + "=" + String.valueOf(dim.getLength()) + ";"); xhtml.newline(); xhtml.newline(); for (Variable var : ncFile.getVariables()){ xhtml.element("p", String.valueOf(var.getDataType()) + var.getNameAndDimensions() + ";"); for(Attribute element : var.getAttributes()){ xhtml.element("li", " :" + element + ";"); xhtml.newline();
private void testCoordVar(NetcdfFile ncfile, int n) { Variable lat = ncfile.findVariable("y"); assert null != lat; assert lat.getShortName().equals("y"); assert lat.getRank() == 1; assert lat.getSize() == n; assert lat.getShape()[0] == n; assert lat.getDataType() == DataType.DOUBLE || lat.getDataType() == DataType.FLOAT; assert !lat.isUnlimited(); assert lat.getDimension(0).equals(ncfile.findDimension("y")); Attribute att = lat.findAttribute("units"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("km"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; try { Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == n; assert data.getShape()[0] == n; } catch (IOException io) {} }
public void testGrads2() throws IOException, InvalidRangeException { String fileIn = TestDir.cdmUnitTestDir + "formats/grads/pdef.ctl"; try (ucar.nc2.NetcdfFile ncf = ucar.nc2.NetcdfFile.open(fileIn)) { logger.debug("open {}", ncf.getLocation()); ucar.nc2.Variable v = ncf.findVariable("pdef"); assert v != null; assert v.getDataType() == DataType.FLOAT; Attribute att = v.findAttribute(CDM.MISSING_VALUE); assert att != null; assert att.getDataType() == DataType.FLOAT; Assert2.assertNearlyEquals(att.getNumericValue().floatValue(), -9999.0f); Array data = v.read(); assert Arrays.equals(data.getShape(), new int[]{1, 1, 180, 360}); } }
public void testCoordVar(NetcdfFile ncfile) throws IOException { Variable lat = ncfile.findVariable("latitude"); assert lat.getDataType() == DataType.FLOAT; assert lat.getDimension(0).equals(ncfile.findDimension("latitude")); Attribute att = lat.findAttribute("units"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("degree_N"); Array data = lat.read(); assert data.getRank() == 1; assert data.getSize() == 630; assert data.getShape()[0] == 630; assert data.getElementType() == float.class; IndexIterator dataI = data.getIndexIterator(); Assert2.assertNearlyEquals(dataI.getFloatNext(), 43.0f); Assert2.assertNearlyEquals(dataI.getFloatNext(), 43.01045f); Assert2.assertNearlyEquals(dataI.getFloatNext(), 43.020893f); }
@Test public void testUamiv() throws IOException { try (NetcdfFile ncfile = NetcdfFile.open(TestDir.cdmUnitTestDir + "formats/uamiv/uamiv.grid", null)) { logger.debug("open {}", ncfile.getLocation()); ucar.nc2.Variable v = ncfile.findVariable("UP"); assert v != null; assert v.getDataType() == DataType.FLOAT; Array data = v.read(); assert Arrays.equals(data.getShape(), new int[]{12, 5, 7, 6}); } }
static public Dimension getBoundsDimension(NetcdfFile ncfile) { Group g = ncfile.getRootGroup(); Dimension d = g.findDimension(boundsDimName); if (d == null) d = ncfile.addDimension(g, new Dimension(boundsDimName, 2, true)); return d; }
@Test public void code5_assume_WGS84() throws IOException { String filename = dir + "Albers_viirs_s.grb2"; try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { Variable v = ncfile.findVariable("AlbersEqualArea_Projection"); Attribute axis = v.findAttribute("semi_major_axis"); Assert.assertEquals(6378137., axis.getNumericValue().doubleValue(), 0.1); } }
private void initProductWidthAndHeight() { sceneRasterHeight = 0; for (Variable variable : netcdfFile.getVariables()) { Attribute gridMappingAttr = variable.findAttribute("grid_mapping_name"); if (gridMappingAttr != null) { if ("1D binned sinusoidal".equalsIgnoreCase(gridMappingAttr.getStringValue())) { Attribute numberOfLatitudeRows = variable.findAttribute("number_of_latitude_rows"); sceneRasterHeight = numberOfLatitudeRows.getNumericValue().intValue(); break; } } } if (sceneRasterHeight == 0) { final Dimension bin_index = netcdfFile.findDimension("bin_index"); if (bin_index != null) { sceneRasterHeight = bin_index.getLength(); } } if (sceneRasterHeight == 0) { sceneRasterHeight = 2160; } sceneRasterWidth = 2 * sceneRasterHeight; }
@Test public void testHybridCoordinates() throws IOException { String filename = TestDir.cdmUnitTestDir + "formats/grib1/07010418_arw_d01.GrbF01500"; System.out.println("\n\nReading File " + filename); NetcdfFile ncfile = NetcdfFile.open(filename); Group best = ncfile.findGroup("Best"); Variable hybrid = ncfile.findVariable(best, "hybrid1"); assert hybrid != null; assert (hybrid.getDimensions().size() == 1); Dimension d = hybrid.getDimension(0); assert (d.getLength() == 2); ncfile.close(); }
private static Map<Integer, Float> readGainInfo(NetcdfFile ncFile) throws IOException { Variable gainInfoVar = ncFile.getRootGroup().findVariable(ChrisConstants.VS_NAME_GAIN_INFO); if (gainInfoVar instanceof Structure) { Structure gainInfoStruct = (Structure) gainInfoVar; int recordCount = gainInfoStruct.getDimension(0).getLength(); Variable gainSetting = gainInfoStruct.findVariable(ChrisConstants.VS_NAME_GAIN_SETTING); Variable gainValue = gainInfoStruct.findVariable(ChrisConstants.VS_NAME_GAIN_VALUE); if (gainSetting != null && gainValue != null && recordCount > 0) { final Map<Integer, Float> gainInfoMap = new HashMap<Integer, Float>(recordCount); Array settingsArray = gainSetting.read(); Array valuesArray = gainValue.read(); for (int i = 0; i < recordCount; i++) { gainInfoMap.put(settingsArray.getInt(i), valuesArray.getFloat(i)); } return gainInfoMap; } } throw new IOException("Failed to read 'Gain Info' Structure."); }
@Test public void testEcmwfExtendedComplexData2() throws IOException { final String testfile = "../grib/src/test/data/complex_packing2.grib1"; try (NetcdfFile nc = NetcdfFile.open(testfile)) { Variable var = nc.findVariable("Snowfall_surface"); Array data = var.read(); float first = data.getFloat(0); Assert.assertEquals(.326607, first, 1e-6); } }
@Test public void zeroLenData() throws IOException { try (NetcdfFile ncremote = new CdmRemote(TestOnLocalServer.withHttpPath(urlPath + "/netcdf3/longOffset.nc"))) { Variable v = ncremote.findVariable(null, "time_whole"); Array data = v.read(); assert data.getSize() == 0; } }
public void testHybrid1() throws IOException { String filename = TestDir.cdmUnitTestDir + "formats/grib1/HIRLAMhybrid.grib"; System.out.println("\n\nReading File " + filename); NetcdfFile ncfile = NetcdfFile.open(filename); Group best = ncfile.findGroup("Best"); Variable hybrid = ncfile.findVariable(best, "hybrid"); assert (hybrid.getNameAndDimensions().equals("hybrid(hybrid=91)")); Variable hybrida = ncfile.findVariable(best, "hybrida"); assert (hybrida.getNameAndDimensions().equals("hybrida(hybrid=91)")); Variable hybridb = ncfile.findVariable(best, "hybridb"); assert (hybridb.getNameAndDimensions().equals("hybridb(hybrid=91)")); int idx = hybrid.findDimensionIndex("hybrid"); Dimension dim = hybrid.getDimension(idx); assert dim.getShortName().equals("hybrid"); ncfile.close(); }
public void testReplaceAtt() { // System.out.println("\nncfile = "+ncfile); Attribute att = ncfile.findGlobalAttribute("title"); assert null != att; assert !att.isArray(); assert att.isString(); assert att.getDataType() == DataType.STRING; assert att.getStringValue().equals("replaced"); assert att.getNumericValue() == null; assert att.getNumericValue(3) == null; }
@Test public void code6_assume_spherical() throws IOException { String filename = dir + "berkes.grb2"; try (NetcdfFile ncfile = NetcdfFile.open(filename, null)) { Group grp = ncfile.getRootGroup().getGroups().get(0); Variable v = grp.findVariable("LatLon_Projection"); Attribute axis = v.findAttribute("earth_radius"); Assert.assertEquals(6371229., axis.getNumericValue().doubleValue(), 0.1); } }
@Test public void testCachedData() throws IOException { NetcdfFile ncd = null; try { ncd = NetcdfDataset.openFile(TestDir.cdmLocalTestDataDir + "point/profileMultidim.ncml", null); Variable v = ncd.findVariable("data"); assert v != null; Array data = v.read(); assert data.getSize() == 50 : data.getSize(); } finally { if (ncd != null) ncd.close(); } }