Hello Fabien, I pasted below a rough example that I put together several years ago. It is outdated, surely won't compile, uses Geotools 2.3.5 and NetCDF 2.2.22, but might help you get on the right track. The general principles are still the same.
Another good resource might be the ncWMS project, which uses GeoTools. http://www.resc.rdg.ac.uk/trac/ncWMS/ I hope this helps. Steve package steve.test; import java.io.*; import java.util.*; import java.net.*; import java.awt.image.*; import javax.media.jai.RasterFactory; import javax.media.jai.*; import org.geotools.coverage.processing.*; import ucar.ma2.*; import ucar.nc2.NetcdfFile; import ucar.nc2.dataset.NetcdfDataset; import ucar.nc2.dt.TypedDatasetFactory; import ucar.nc2.dt.grid.GeoGrid; import ucar.nc2.dt.grid.GridDataset; import ucar.nc2.util.DiskCache; import ucar.unidata.geoloc.*; import org.geotools.coverage.grid.GridCoverage2D; import org.geotools.coverage.grid.GridCoverageFactory; import org.geotools.geometry.*; import org.geotools.referencing.CRS; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.opengis.referencing.FactoryException; public class ReprojectST4 { private URL ncURL; private GridDataset gridDataset; private GridCoverageFactory gcFactory = new GridCoverageFactory(); private WritableRaster raster = null; public ReprojectST4(URL ncURL) throws IOException { System.out.println("SETTING DISK CACHE TO: "+System.getProperty("java.io.tmpdir")); DiskCache.setRootDirectory(System.getProperty("java.io.tmpdir")); this.ncURL = ncURL; NetcdfFile ncfile = NetcdfFile.open(ncURL.toString()); NetcdfDataset ds = new NetcdfDataset(ncfile); // NetcdfDataset ds = NetcdfDataset.openDataset(ncURL.toString()); StringBuffer errlog = new StringBuffer(); this.gridDataset = (GridDataset)TypedDatasetFactory.open( thredds.catalog.DataType.GRID, ds, null, errlog); if (null == gridDataset) { throw new IOException("Cant open GRID at location= "+ncURL.toString()+"; error message= "+errlog); } } public GridDataset getGridDataset() { return gridDataset; } private void listGrids() { List grids = gridDataset.getGrids(); for (int i = 0; i < grids.size(); i++) { GeoGrid grid = (GeoGrid) grids.get(i); System.out.println(grid.toString()); } } public GridCoverage2D getGridCoverage(String geoGridName) throws IOException, FactoryException { return getGridCoverage(gridDataset.findGridByName(geoGridName)); } public GridCoverage2D getGridCoverage(GeoGrid geoGrid) throws IOException, FactoryException { int height = geoGrid.getYDimension().getLength(); int width = geoGrid.getXDimension().getLength(); int heightIndex = geoGrid.getYDimensionIndex()-1; int widthIndex = geoGrid.getXDimensionIndex()-1; raster = RasterFactory.createBandedRaster(DataBuffer.TYPE_FLOAT, width, height, 1, null); System.out.println("H-INDEX = " + heightIndex + " height=" + height); System.out.println("W-INDEX = " + widthIndex + " width=" + width); Array dataArray = geoGrid.readYXData(0, 0); Index dataIndex = dataArray.getIndex(); int[] shape = dataArray.getShape(); System.out.println("shape.length=" + shape.length + " shape[0]=" + shape[0] + " shape[1]=" + shape[1]); int count = 0; int x = 0; int y = 0; try { for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { count++; float val; if (heightIndex == 0) { val = dataArray.getFloat(dataIndex.set(y, x)); } else { val = dataArray.getFloat(dataIndex.set(x, y)); } raster.setSample(x, y, 0, val); } // System.out.println("count="+count+" y="+y); } } catch (Exception e) { System.out.println(e); e.printStackTrace(); System.out.println(x + " , " + y + " , " + count); return null; } LatLonRect rect = geoGrid.getCoordinateSystem().getLatLonBoundingBox(); LatLonPoint llCorner = rect.getLowerLeftPoint(); LatLonPoint urCorner = rect.getUpperRightPoint(); System.out.println("llCorner: " + llCorner); System.out.println("urCorner: " + urCorner); // GeneralEnvelope env = new GeneralEnvelope(new // java.awt.geom.Rectangle2D.Double(-95.0, 35.0, 10.0, 10.0)); GeneralEnvelope env = new GeneralEnvelope( new java.awt.geom.Rectangle2D.Double(llCorner.getLongitude(), llCorner.getLatitude(), Math.abs(llCorner.getLongitude() - urCorner.getLongitude()), Math.abs(llCorner.getLatitude() - urCorner.getLatitude()) ) ); // Start reprojection stuff String HRAPSTEREO_WKT = "PROJCS[\"Stereographic_North_Pole\",GEOGCS[\"Sphere\","+ "DATUM[\"Sphere\",SPHEROID[\"Sphere\",6371200.0,0],TOWGS84[0,0,0,0,0,0,0]],"+ "PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],"+ "PROJECTION[\"Polar_Stereographic\"],"+ "PARAMETER[\"false_easting\",0.0],PARAMETER[\"false_northing\",0.0],"+ "PARAMETER[\"central_meridian\",-105.0],"+ "PARAMETER[\"latitude_of_origin\",60.0],UNIT[\"metre\",1.0]]"; env.setCoordinateReferenceSystem(CRS.parseWKT(HRAPSTEREO_WKT)); //env.setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84); GridCoverage2D gc = gcFactory.create("Original", raster, env); return gc; } Fabien Carrion wrote: > Hello the list, > > I am trying to read a grib file with geotools through netcdf. I found > out there was some code in the unsupported module gt-coverageio-netcdf. > I haven't found out any example in the test part of the module. I would > like to know where can I found an example or any kind of documentation. > > Thanks! > ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev _______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users ------------------------------------------------------------------------------ Download Intel® Parallel Studio Eval Try the new software tools for yourself. Speed compiling, find bugs proactively, and fine-tune applications for parallel performance. See why Intel Parallel Studio got high marks during beta. http://p.sf.net/sfu/intel-sw-dev _______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
