Search in sources :

Example 1 with CDM

use of ucar.nc2.constants.CDM in project ncWMS by Unidata.

the class NcDiag method main.

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: NcDiag <filename>");
        System.exit(-1);
    }
    String filename = args[0];
    PrintStream ps = System.out;
    printHeader(ps, filename);
    // Open the file using the Unidata CDM
    NetcdfDataset nc = null;
    try {
        nc = NetcdfDataset.openDataset(filename);
        // TODO: print information about the NetCDF file as a whole
        GridDataset gd = CdmUtils.getGridDataset(nc);
        // Read all the metadata from the file
        Collection<CoverageMetadata> coverages = CdmUtils.readCoverageMetadata(gd);
        // Cycle through all the coverages, printing out information from each
        for (CoverageMetadata cm : coverages) {
            printInfo(ps, nc, cm);
        }
    } finally {
        if (nc != null)
            nc.close();
        printFooter(ps);
        ps.close();
    }
}
Also used : PrintStream(java.io.PrintStream) CoverageMetadata(uk.ac.rdg.resc.edal.coverage.CoverageMetadata) GridDataset(ucar.nc2.dt.GridDataset) NetcdfDataset(ucar.nc2.dataset.NetcdfDataset)

Example 2 with CDM

use of ucar.nc2.constants.CDM in project ncWMS by Unidata.

the class NcImageGen method main.

public static void main(String[] args) throws IOException {
    if (args.length != 9) {
        System.err.println("Usage: NcImageGen <filename> <output_width> <output_height> <min_lon> <max_lon> <min_lat> <max_lat> <height> <tIndex>");
        System.exit(-1);
    }
    String filename = args[0];
    int xsize = Integer.parseInt(args[1]);
    int ysize = Integer.parseInt(args[2]);
    double lonmin = Double.parseDouble(args[3]);
    double lonmax = Double.parseDouble(args[4]);
    double latmin = Double.parseDouble(args[5]);
    double latmax = Double.parseDouble(args[6]);
    double zVal = Double.parseDouble(args[7]);
    int tIndex = Integer.parseInt(args[8]);
    GeographicBoundingBox bbox = new DefaultGeographicBoundingBox(lonmin, lonmax, latmin, latmax);
    // Open the file using the Unidata CDM
    NetcdfDataset nc = null;
    try {
        nc = NetcdfDataset.openDataset(filename);
        GridDataset gd = CdmUtils.getGridDataset(nc);
        // Read all the metadata from the file
        Collection<CoverageMetadata> coverages = CdmUtils.readCoverageMetadata(gd);
        // Cycle through all the coverages, printing out information from each
        for (CoverageMetadata cm : coverages) {
            List<Double> zVals = cm.getElevationValues();
            double min_diff = 1000000.0;
            int zIndex = 0;
            for (int i = 0; i < zVals.size(); i++) {
                double z = zVals.get(i);
                if (Math.abs(z - zVal) < min_diff) {
                    min_diff = Math.abs(z - zVal);
                    zIndex = i;
                }
            }
            if (zVals.isEmpty()) {
                System.out.println("Variable: " + cm.getId() + " only has one point in the vertical axis.");
            } else {
                System.out.println("Variable: " + cm.getId() + " has " + zVals.size() + " elevation data.  Nearest elevation to " + zVal + " is " + zVals.get(zIndex) + ".");
            }
            List<DateTime> tVals = cm.getTimeValues();
            if (tVals.isEmpty() || tVals.size() == 1) {
                System.out.println("Variable: " + cm.getId() + " only has one point in the time axis.");
                tIndex = 0;
            } else if (tIndex > tVals.size()) {
                System.out.println("Variable: " + cm.getId() + " has " + tVals.size() + " time values.  Index " + tIndex + " is too large.  Using maximum, which corresponds to " + tVals.get(tVals.size() - 1) + ".");
                tIndex = tVals.size() - 1;
            } else if (tIndex < 0) {
                System.out.println("Variable: " + cm.getId() + " has " + tVals.size() + " time values.  Index " + tIndex + " is negative.  Using first time value, which corresponds to " + tVals.get(0) + ".");
                tIndex = 0;
            } else {
                System.out.println("Variable: " + cm.getId() + " has " + tVals.size() + " time values.  Index " + tIndex + " corresponds to " + tVals.get(tIndex) + ".");
            }
            List<Float> data = readData(nc, cm, bbox, xsize, ysize, tIndex, zIndex);
            BufferedImage image = createImage(data, xsize, ysize);
            String imageFilename = cm.getId().toLowerCase() + ".png";
            ImageIO.write(image, "png", new File(imageFilename));
        }
    } finally {
        if (nc != null)
            nc.close();
    }
}
Also used : CoverageMetadata(uk.ac.rdg.resc.edal.coverage.CoverageMetadata) DefaultGeographicBoundingBox(org.geotoolkit.metadata.iso.extent.DefaultGeographicBoundingBox) GeographicBoundingBox(org.opengis.metadata.extent.GeographicBoundingBox) NetcdfDataset(ucar.nc2.dataset.NetcdfDataset) DateTime(org.joda.time.DateTime) BufferedImage(java.awt.image.BufferedImage) DefaultGeographicBoundingBox(org.geotoolkit.metadata.iso.extent.DefaultGeographicBoundingBox) GridDataset(ucar.nc2.dt.GridDataset) File(java.io.File)

Aggregations

NetcdfDataset (ucar.nc2.dataset.NetcdfDataset)2 GridDataset (ucar.nc2.dt.GridDataset)2 CoverageMetadata (uk.ac.rdg.resc.edal.coverage.CoverageMetadata)2 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 DefaultGeographicBoundingBox (org.geotoolkit.metadata.iso.extent.DefaultGeographicBoundingBox)1 DateTime (org.joda.time.DateTime)1 GeographicBoundingBox (org.opengis.metadata.extent.GeographicBoundingBox)1