public static Map<String, Metadata> readFile(String file) throws Exception { NetcdfFile n = NetcdfFile.open(file); System.out.println("Opened: " + file); /* Determine the size of our grid */ int xLen = n.findDimension("x").getLength(); int yLen = n.findDimension("y").getLength(); System.out.println("Grid size: " + xLen + "x" + yLen); /* What time is this set of readings for? */ Variable timeVar = n.findVariable("time"); String timeStr = timeVar.getUnitsString().toUpperCase(); timeStr = timeStr.replace("HOURS SINCE ", ""); timeStr = timeStr.replace("HOUR SINCE ", ""); /* Find the base date (the day) the reading was taken */ Date baseDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(timeStr); /* Get the number of hours since the base date this reading was taken */ int offset = timeVar.read().getInt(0); /* Generate the actual date for this reading */ Calendar calendar = Calendar.getInstance(); calendar.setTime(baseDate); calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + offset); System.out.println("Time of collection: " + calendar.getTime()); /* We'll keep a mapping of geolocations -> Galileo Metadata */ Map<String, Metadata> metaMap = new HashMap<>(); /* Determine the lat, lon coordinates for the grid points, and get each * reading at each grid point. */ NetcdfDataset dataset = new NetcdfDataset(n); @SuppressWarnings("resource") GridDataset gridData = new GridDataset(dataset); for (GridDatatype g : gridData.getGrids()) { /* Let's look at 3D variables: these have WxH dimensions, plus a * single plane. A 4D variable would contain elevation * and multiple planes as a result */ if (g.getShape().length == 3) { convert3DVariable(g, calendar.getTime(), metaMap); } } return metaMap; }
static void doOne( String dir, String filename, int ngrids, int ncoordSys, int ncoordAxes, int nVertCooordAxes) throws Exception { System.out.println("test read GridDataset = " + dir + filename); ucar.nc2.dt.grid.GridDataset gridDs = GridDataset.open(dir + filename); int countGrids = gridDs.getGrids().size(); int countCoordAxes = gridDs.getNetcdfDataset().getCoordinateAxes().size(); int countCoordSys = gridDs.getNetcdfDataset().getCoordinateSystems().size(); // count vertical axes int countVertCooordAxes = 0; List axes = gridDs.getNetcdfDataset().getCoordinateAxes(); for (int i = 0; i < axes.size(); i++) { CoordinateAxis axis = (CoordinateAxis) axes.get(i); AxisType t = axis.getAxisType(); if ((t == AxisType.GeoZ) || (t == AxisType.Height) || (t == AxisType.Pressure)) countVertCooordAxes++; } Iterator iter = gridDs.getGridsets().iterator(); while (iter.hasNext()) { GridDataset.Gridset gridset = (GridDataset.Gridset) iter.next(); GridCoordSys gcs = gridset.getGeoCoordSys(); // if (gcs.hasTimeAxis()) // System.out.println(" "+gcs.isDate()+" "+gcs.getName()); } if (showCount) { System.out.println(" grids=" + countGrids + ((ngrids < 0) ? " *" : "")); System.out.println(" coordSys=" + countCoordSys + ((ncoordSys < 0) ? " *" : "")); System.out.println(" coordAxes=" + countCoordAxes + ((ncoordAxes < 0) ? " *" : "")); System.out.println(" vertAxes=" + countVertCooordAxes + ((nVertCooordAxes < 0) ? " *" : "")); } if (ngrids >= 0) assert ngrids == countGrids : "Grids " + ngrids + " != " + countGrids; // if (ncoordSys >= 0) // assert ncoordSys == countCoordSys : "CoordSys " + ncoordSys + " != " + countCoordSys; // if (ncoordAxes >= 0) // assert ncoordAxes == countCoordAxes : "CoordAxes " + ncoordAxes + " != " + countCoordAxes; if (nVertCooordAxes >= 0) assert nVertCooordAxes == countVertCooordAxes : "VertAxes " + nVertCooordAxes + " != " + countVertCooordAxes; gridDs.close(); }
@Test public void bugReport() throws IOException { try (GridDataset dataset = GridDataset.open( "http://www.unidata.ucar.edu/software/netcdf/examples/sresa1b_ncar_ccsm3_0_run1_200001.nc")) { GridDatatype firstGridInfo = dataset.getGrids().get(0); System.out.println("Grid name =" + firstGridInfo.getName()); GeoGrid firstGrid = (GeoGrid) dataset.getGrids().get(0); System.out.println("WHOLE GRID"); GridCoordSystem gcs = firstGrid.getCoordinateSystem(); System.out.println("is lat/lon system ? " + gcs.isLatLon()); assert gcs.isLatLon(); LatLonRect rect = gcs.getLatLonBoundingBox(); System.out.println( "gcs bounding box : latmin=" + rect.getLatMin() + " latmax=" + rect.getLatMax() + " lonmin=" + rect.getLonMin() + " lonmax=" + rect.getLonMax()); System.out.println("projection : " + gcs.getProjection()); System.out.println( "width =" + gcs.getXHorizAxis().getSize() + ", height=" + gcs.getYHorizAxis().getSize()); System.out.println( "X is regular ? " + ((CoordinateAxis1D) gcs.getXHorizAxis()).isRegular()); System.out.println("X is contiguous ? " + gcs.getXHorizAxis().isContiguous()); System.out.println( "X start : " + ((CoordinateAxis1D) gcs.getXHorizAxis()).getStart()); System.out.println( "X increment : " + ((CoordinateAxis1D) gcs.getXHorizAxis()).getIncrement()); System.out.println( "Y is regular ? " + ((CoordinateAxis1D) gcs.getYHorizAxis()).isRegular()); System.out.println("Y is contiguous ? " + gcs.getYHorizAxis().isContiguous()); System.out.println( "Y start : " + ((CoordinateAxis1D) gcs.getYHorizAxis()).getStart()); System.out.println( "Y increment : " + ((CoordinateAxis1D) gcs.getYHorizAxis()).getIncrement()); LatLonPoint p = gcs.getLatLon(0, 0); System.out.println("index (0,0) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); p = gcs.getLatLon(1, 1); System.out.println("index (1,1) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); System.out.println("looking up lat=" + p.getLatitude() + " lon=" + p.getLongitude()); int[] xy = gcs.findXYindexFromLatLon(p.getLatitude(), p.getLongitude(), null); System.out.println("index= (" + xy[0] + ", " + xy[1] + ")"); Assert.assertEquals(xy[0], 1); Assert.assertEquals(xy[1], 1); // -------------------------------------------------------------------------- double latMin = -20.D, latMax = -10.D, lonMin = 35.D, lonMax = 45.D; System.out.println( "\nSUBGRID (latmin=" + latMin + " latmax=" + latMax + " lonmin=" + lonMin + " lonmax=" + lonMax + ")"); LatLonRect latLonRect = new LatLonRect(new LatLonPointImpl(latMin, lonMin), new LatLonPointImpl(latMax, lonMax)); GeoGrid gridSubset = firstGrid.subset(null, null, latLonRect, 0, 1, 1); GridCoordSystem gcs2 = gridSubset.getCoordinateSystem(); rect = gcs2.getLatLonBoundingBox(); System.out.println("is lat/lon system ? " + gcs2.isLatLon()); System.out.println( "gcs bounding box : latmin=" + rect.getLatMin() + " latmax=" + rect.getLatMax() + " lonmin=" + rect.getLonMin() + " lonmax=" + rect.getLonMax()); System.out.println("projection : " + gcs.getProjection()); System.out.println( "width =" + gcs2.getXHorizAxis().getSize() + ", height=" + gcs2.getYHorizAxis().getSize()); System.out.println( "X is regular ? " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).isRegular()); System.out.println("X is contiguous ? " + gcs2.getXHorizAxis().isContiguous()); System.out.println( "X start : " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).getStart()); System.out.println( "X increment : " + ((CoordinateAxis1D) gcs2.getXHorizAxis()).getIncrement()); System.out.println( "Y is regular ? " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).isRegular()); System.out.println("Y is contiguous ? " + gcs2.getYHorizAxis().isContiguous()); System.out.println( "Y start : " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).getStart()); System.out.println( "Y increment : " + ((CoordinateAxis1D) gcs2.getYHorizAxis()).getIncrement()); p = gcs2.getLatLon(0, 0); System.out.println("index (0,0) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); p = gcs2.getLatLon(1, 1); System.out.println("index (1,1) --> lat/lon : " + p.getLatitude() + " ; " + p.getLongitude()); System.out.println("looking up lat=" + p.getLatitude() + " lon=" + p.getLongitude()); xy = gcs2.findXYindexFromLatLon(p.getLatitude(), p.getLongitude(), null); System.out.println("index= (" + xy[0] + ", " + xy[1] + ")"); Assert.assertEquals(xy[0], 1); Assert.assertEquals(xy[1], 1); } catch (IOException | InvalidRangeException e) { e.printStackTrace(); } }