I have make a small patch for the branch 2.6.x, so it support netcdf
4.1 I hope you can use it.

Thanks for this example, but in your example you are using a lot the
netcdf objects. I was thinking to something only based on geotools.

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageReader;
import javax.imageio.ImageReadParam;
import org.geotools.image.io.netcdf.NetcdfImageReader;
import org.geotools.image.io.netcdf.NetcdfImageReader.Spi;

public class Test {
    public static void main(String[] args) throws Exception {
        NetcdfImageReader netcdfreader = (NetcdfImageReader) (new
Spi()).createReaderInstance(new Object());
        netcdfreader.setInput("Path_to_netcdf_file", false, false);
        ImageReadParam param = netcdfreader.getDefaultReadParam();
        BufferedImage out = netcdfreader.read(0, param);
        File file = new File("Path_to_new_image_file");
        javax.imageio.ImageIO.write(out, "png", file);
    }
}

The problem is that I always get an empty image. I think my problem is
on the boundary which I should set in the ImageReadParam object.

Do you have any piece of advice to give me?

Thanks

On Tue, Mar 2, 2010 at 2:50 PM, Steve Ansari <[email protected]> wrote:
> 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&#174; 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
>
>
>



-- 
Fabien Carrion

()  Campagne du ruban ASCII -- Contre les mails en html
/\  contre les pieces-jointes Microsoft
Web: http://fabien.carrion.free.fr/
Index: modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfReadParam.java
===================================================================
--- modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfReadParam.java	(revision 34976)
+++ modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfReadParam.java	(working copy)
@@ -23,7 +23,7 @@
 import java.util.HashSet;
 import javax.imageio.ImageReader;
 
-import ucar.nc2.dataset.AxisType;
+import ucar.nc2.constants.AxisType;
 import ucar.nc2.dataset.CoordinateAxis;
 import ucar.nc2.dataset.CoordinateSystem;
 import ucar.nc2.dataset.VariableEnhanced;
Index: modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfMetadata.java
===================================================================
--- modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfMetadata.java	(revision 34976)
+++ modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfMetadata.java	(working copy)
@@ -34,7 +34,8 @@
 
 import ucar.nc2.Variable;
 import ucar.nc2.Attribute;
-import ucar.nc2.dataset.AxisType;
+import ucar.nc2.constants.AxisType;
+import ucar.nc2.constants.CF;
 import ucar.nc2.dataset.CoordinateAxis;
 import ucar.nc2.dataset.CoordinateAxis1D;
 import ucar.nc2.dataset.CoordinateSystem;
@@ -196,7 +197,7 @@
         String direction = null;
         AxisDirection directionCode = DIRECTIONS.get(type);
         if (directionCode != null) {
-            if (CoordinateAxis.POSITIVE_DOWN.equalsIgnoreCase(axis.getPositive())) {
+            if (CF.POSITIVE_DOWN.equalsIgnoreCase(axis.getPositive())) {
                 directionCode = directionCode.opposite();
             }
             direction = directionCode.name();
Index: modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/VariableMetadata.java
===================================================================
--- modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/VariableMetadata.java	(revision 34976)
+++ modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/VariableMetadata.java	(working copy)
@@ -22,7 +22,7 @@
 import ucar.nc2.Attribute;
 import ucar.nc2.Variable;
 import ucar.nc2.VariableIF;
-import ucar.nc2.dataset.VariableEnhanced;
+import ucar.nc2.dataset.VariableDS;
 
 import org.geotools.resources.XArray;
 import org.geotools.image.io.metadata.Band;
@@ -34,10 +34,10 @@
  * <p>
  * <ul>
  *   <li>I have not been able to find any method giving me directly the offset and scale factor.
- *       We can use some trick with {...@link VariableEnhanced#convertScaleOffsetMissing}, but
+ *       We can use some trick with {...@link VariableDS#convertScaleOffsetMissing}, but
  *       they are subject to rounding errors and there is no efficient way I can see to take
  *       missing values in account.</li>
- *   <li>The {...@link VariableEnhanced} methods are available only if the variable is enhanced.
+ *   <li>The {...@link VariableDS} methods are available only if the variable is enhanced.
  *       Our variable is not, because we want raw (packed) data.</li>
  *   <li>We want minimum, maximum and fill values in packed units (as opposed to the geophysics
  *       values provided by the UCAR's API), because we check for missing values before to
@@ -85,7 +85,7 @@
      * from rounding errors and is unable to get the missing values. Use this constructor
      * only for comparing our own results with the results from the UCAR's API.
      */
-    public VariableMetadata(final VariableEnhanced variable) {
+    public VariableMetadata(final VariableDS variable) {
         imageType = getRawDataType(variable);
         offset    =  variable.convertScaleOffsetMissing(0.0);
         scale     =  variable.convertScaleOffsetMissing(1.0) - offset;
Index: modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfImageReader.java
===================================================================
--- modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfImageReader.java	(revision 34976)
+++ modules/unsupported/coverageio-netcdf/src/main/java/org/geotools/image/io/netcdf/NetcdfImageReader.java	(working copy)
@@ -39,10 +39,11 @@
 import ucar.ma2.DataType;
 import ucar.ma2.IndexIterator;
 import ucar.ma2.InvalidRangeException;
-import ucar.nc2.dataset.AxisType;
+import ucar.nc2.constants.AxisType;
 import ucar.nc2.dataset.CoordinateAxis;
 import ucar.nc2.dataset.CoordinateSystem;
 import ucar.nc2.dataset.CoordSysBuilder;
+import ucar.nc2.dataset.CoordSysBuilderIF;
 import ucar.nc2.dataset.NetcdfDataset;
 import ucar.nc2.dataset.VariableDS;
 import ucar.nc2.dataset.VariableEnhanced;
@@ -397,7 +398,8 @@
      */
     private void ensureMetadataLoaded() throws IOException {
         if (!metadataLoaded) {
-            CoordSysBuilder.addCoordinateSystems(dataset, this);
+            CoordSysBuilderIF csb = CoordSysBuilder.factory(dataset, this);
+            csb.buildCoordinateSystems(dataset);
             metadataLoaded = true;
         }
     }
Index: modules/unsupported/coverageio-netcdf/pom.xml
===================================================================
--- modules/unsupported/coverageio-netcdf/pom.xml	(revision 34976)
+++ modules/unsupported/coverageio-netcdf/pom.xml	(working copy)
@@ -111,7 +111,7 @@
     </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-jdk14</artifactId>
+      <artifactId>slf4j-log4j12</artifactId>
       <!-- The version number is specified in the parent POM. -->
       <scope>runtime</scope>
     </dependency>
Index: pom.xml
===================================================================
--- pom.xml	(revision 34976)
+++ pom.xml	(working copy)
@@ -852,12 +852,12 @@
       <dependency>
         <groupId>edu.ucar</groupId>
         <artifactId>netcdf</artifactId>
-        <version>2.2.20</version>
+        <version>4.1</version>
       </dependency>
       <dependency>
         <groupId>org.slf4j</groupId>
-        <artifactId>slf4j-jdk14</artifactId> <!-- Logging system used by NetCDF. -->
-        <version>1.4.0</version>
+        <artifactId>slf4j-log4j12</artifactId> <!-- Logging system used by NetCDF. -->
+        <version>1.5.6</version>
       </dependency>
 
       <!-- EMF -->
------------------------------------------------------------------------------
Download Intel&#174; 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

Reply via email to