This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
You can look at the user manual at ftp://ftp.unidata.ucar.edu/pub/netcdf-java/v2.2/NetcdfJavaUserManual-2.2.doc it has some examples in the appendices. good luck! address@hidden wrote:
Hi John ,
Thanks for your answer but I have some problems again. So if you can show me an
example to read the data I join a netcdf file I want to read. The problem is
that I want to create a plot in 3 Dimensions with these data so I have to put
them into some tabs. I would like to create a program which works for all kind
of netcdf files.
This is the program I wrote .
import java.rmi.RemoteException;
import ucar.netcdf.Attribute;
import ucar.netcdf.Netcdf;
import ucar.netcdf.NetcdfFile;
import ucar.netcdf.Variable;
import visad.VisADException;
public class Read {
    static String fileName = "c:/output.nc";
    public static void main(String[] args) throws RemoteException,
VisADException{
        if (args.length > 0)
            fileName = args[0];
        try {
            Netcdf nc = new NetcdfFile(fileName, true);
            Variable lat = nc.get("NBLATITUDES75_103");
            int nlats = lat.getLengths()[0];
            double [] lats = new double[nlats];
            int[] index = new int[1];
            for (int ilat = 0; ilat < nlats; ilat++) {
                index [0] = ilat;
                lats[ilat] = lat.getDouble(index);
            }
            String latUnits = lat.getAttribute("units").getStringValue();
            Variable lon = nc.get("NBLONGITUDES231_281");
            int nlons = lon.getLengths()[0];
            double [] lons = new double[nlons];
            int[] index1 = new int[1];
            for (int ilon = 0; ilon < nlons; ilon++) {
                index1 [0] = ilon;
                lons[ilon] = lon.getDouble(index1);
            }
            String lonUnits = lon.getAttribute("units").getStringValue();
            Variable rh = nc.get("GRID_0001");
            String rhUnits = rh.getAttribute("units").getStringValue();
            System.out.println("Units rh : " +rhUnits);
            int[] rhShape = rh.getLengths();
            double[][] rhData = new double[rhShape[0]][rhShape[1]];
            int[] ix = new int[2];
            for (int ilat = 0; ilat < rhData.length; ilat++) {
                ix[0] = ilat;
                for (int ilon = 0; ilon < rhData[0].length ; ilon++) {
                    ix[1] = ilon;
                        rhData[ilat][ilon] = rh.getInt(ix);
                }
            }
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}
Thanks and Have a nice day