Re: [netcdf-java] Creating NetCDF files with GeoGrids

Hi Anthony:

If you already have a file with georeferencing info in it which can be made into a geogrid, then geogrid.writeFile() (or its replacement in 4.0 NetcdfCFWriter) can be used. If you are writing the file from scratch, then its probably easier to use a metadata convention, and we recommend CF:
 http://cf-pcmdi.llnl.gov/


Creating a GeoGrid directly can be done, but you would have to understand a lot of details about how the Coordinate Systems are put together. Probably better to just write the file's contents directly. In that case you would just use NetcdfFileWriteable, but not GeoGrid. The fragment below is a good start, but you need to add coordinate variables and some other metadata.

There are example CF files here:

 http://www.unidata.ucar.edu/software/netcdf/examples/files.html


Anthony Fox wrote:
Hello,

I am trying to create a NetCDF file with a GeoGrid.  My data is a
200x200 grid indexed by latitude and longitude.  Each grid point has a
"prediction" value that is a double ranging from 0 to 1.  Can I create
a NetCDF file from this data?  I have tried the code following but ran
into trouble trying add a coordinate system.  Any help in creating a
NetCDF file from this data set would be appreciated.

Thanks,
Anthony


        NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew(
                "mypredfile");
        // add dimensions
        Dimension latDim = ncfile.addDimension("lat", 200);
        Dimension lonDim = ncfile.addDimension("lon", 200);
        ArrayList<Dimension> dims = new ArrayList<Dimension>();
        dims.add(latDim);
        dims.add(lonDim);

        ncfile.addVariable("prediction", DataType.DOUBLE, dims);
        Variable predvar = new Variable(ncfile, ncfile.getRootGroup(),
null, "prediction");
        predvar.setDataType(DataType.DOUBLE);
        predvar.setDimensions(dims);
        VariableDS predVariable = new
VariableDS(ncfile.getRootGroup(), predvar, true);

        // ???????????????????????
        //  predVariable.addCoordinateSystem(new
CoordinateSystem(ncfile, arg1, arg2));
        ncfile.create();

        ArrayDouble preds = new ArrayDouble.D2(latDim.getLength(), lonDim.
                getLength());
        Random rand = new Random();
        for (int i = 0; i < latDim.getLength(); i++) {
            for (int j = 0; j < lonDim.getLength(); j++) {
                preds.setDouble(preds.getIndex().set(i, j), rand.nextDouble());
            }
        }
        ncfile.write("prediction", new int[2], preds);
        ncfile.close();


       /* IS THIS CORRECT?????
        GridDataset gd = new ucar.nc2.dt.grid.GridDataset(NetcdfDataset.
                openDataset("mypred"));
        GeoGrid grid = new GeoGrid(gd, new
VariableDS(ncfile.getRootGroup(), ncfile.getVariables().
                get(0), true), new GridCoordSys(null, null));
                ncfile.create();
        grid.writeFile("mypredwithgrid");
        */
_______________________________________________
netcdf-java mailing list
netcdf-java@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/


  • 2008 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: