Re: [netcdf-java] Remove z levels

Ciao Giovanni,

First of all I'd suggest updating to netCDF-JAVA 4.3 and use NetcdfFileWriter or NetcdfCFWriter instead of NetcdfFileWriteable which is now deprecated. Changing the length of the dimensions don't actually change the shape of the underlying array of data. To do that you need to subset the variable and get a new variable from the original one.You can use the section method for that purpose: http://www.unidata.ucar.edu/software/netcdf-java/v4.3/javadoc/ucar/nc2/Variable.html#section(java.util.List) <http://www.unidata.ucar.edu/software/netcdf-java/v4.3/javadoc/ucar/nc2/Variable.html#section%28java.util.List%29>

Also, if working with the variable objects in memory works for you and you have valid CDM grids, you can simply make a GridDataset and then subset the grids with the makeSubset method. That could be the code snippet:

        NetcdfDataset ds = NetcdfDataset.openDataset(fileIn);
        GridDataset gds = new GridDataset(ds);

        GridDatatype uGrid = gds.findGridByName("u");
        GridDatatype vGrid = gds.findGridByName("v");

        Range zRange = new Range(0,0);
        uGrid = uGrid.makeSubset(null, zRange, null, 1, 1, 1);
        vGrid = vGrid.makeSubset(null, zRange, null, 1, 1, 1);

If you have to write out a file, I think this could work:

        String fileOut ="out.nc";
        NetcdfDataset ds = NetcdfDataset.openDataset(fileIn);
        GridDataset gds = new GridDataset(ds);
        List<String> wantedVars = new ArrayList<String>();
        wantedVars.add("u");
        wantedVars.add("v");
        Range zRange = new Range(0,0);
        NetcdfCFWriter writer = new NetcdfCFWriter();
writer.makeFile(fileOut, gds, wantedVars, gds.getBoundingBox(), 1, zRange, null, 1, false, NetcdfFileWriter.Version.netcdf3 );

Hope it helps.


On 03/13/2013 08:47 AM, Giovanni wrote:
Hi all,
I'm new to netCDF and I've a simple (I think) problem.
I've a netCDF file containing currents forecast (u and v variables) for 3 days and 33 deep ocean levels. Now I would to remove all deep levels but the first one (0.0 - surface level).
How I can do it?
I tryed this but does't works:

        String fileIn = "C:/Users/Test/Desktop/latest.nc";
NetcdfFileWriteable aa = NetcdfFileWriteable.openExisting(fileIn);
        List<Variable> vars2 = aa.getVariables();
        for (Variable va : vars2) {
            System.out.println("Variable name :" + va.getFullName());
            List<Dimension> dims = va.getDimensions();
            for (Dimension di : dims) {
System.out.println("dim name: " + di.getName() + " len:" + di.getLength());

                if (di.getName().equals("Depth")) di.setLength(1);
            }
        }
        aa.finish();
        aa.finish();
        aa.close();

This is my output:
Variable name :u
dim name: MT   len:3
dim name: Depth   len:33
dim name: Y   len:30
dim name: X   len:38
Variable name :MT
dim name: MT   len:3
Variable name :Depth
dim name: Depth   len:1
Variable name :Latitude
dim name: Y   len:30
dim name: X   len:38
Variable name :Longitude
dim name: Y   len:30
dim name: X   len:38
Variable name :v
dim name: MT   len:3
dim name: Depth   len:1
dim name: Y   len:30
dim name: X   len:38


What could be wrong?

Best regards
Giovanni

_______________________________________________
netcdf-java mailing list
netcdf-java@xxxxxxxxxxxxxxxx
For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/

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