Experiment for fetching OPeNDAP data

Experiment for fetching OPeNDAP data. Use Java-OPeNDAP from here instead of using the Swiss Army Knife - NetCDF for Java from here.

package applicationtest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import opendap.dap.DAP2Exception;
import opendap.dap.DArray;
import opendap.dap.DConnect;
import opendap.dap.DDSException;
import opendap.dap.DGrid;
import opendap.dap.DataDDS;
import opendap.dap.Float32PrimitiveVector;
import opendap.dap.Float64PrimitiveVector;
import opendap.dap.PrimitiveVector;
import opendap.dap.parser.ParseException;

public class Main {

    public static void main(String[] args) {
        DConnect url = null;
        try {
            url = new opendap.dap.DConnect("http://motherlode.ucar.edu:8080/thredds/dodsC/fmrc/NCEP/GFS/Global_0p5deg/files/GFS_Global_0p5deg_20110129_0000.grib2");

            double requriedLat = 22.5;
            double requriedLon = 114;
            int latPos = 0;
            int lonPos = 0;

            DataDDS data = url.getData("lat", null);
            PrimitiveVector latPrimitiveVector = data.getVar(0).newPrimitiveVector();
            DArray latArray = (DArray) latPrimitiveVector.getTemplate();
            Float64PrimitiveVector latVector = (Float64PrimitiveVector) (latArray.getPrimitiveVector());
            for (int i = 0; i < latVector.getLength(); i++) {
                double lat = latVector.getValue(i);
                if (lat == requriedLat) {
                    latPos = i;
                }
            }

            data = url.getData("lon", null);
            PrimitiveVector lonPrimitiveVector = data.getVar(0).newPrimitiveVector();
            DArray lonArray = (DArray) lonPrimitiveVector.getTemplate();
            Float64PrimitiveVector lonVector = (Float64PrimitiveVector) (lonArray.getPrimitiveVector());
            for (int i = 0; i < lonVector.getLength(); i++) {
                double lon = lonVector.getValue(i);
                if (lon == requriedLon) {
                    lonPos = i;
                }
            }
            System.out.println();


            DataDDS data2 = url.getData("Temperature_height_above_ground[0:64][0][" + latPos + "][" + lonPos + "]", null);
            PrimitiveVector a = data2.getVar(0).newPrimitiveVector();

            DGrid b = (DGrid) a.getTemplate();
            DArray c = (DArray) (b.getVar(0));
            Float32PrimitiveVector d = (Float32PrimitiveVector) (c.getPrimitiveVector());
            System.out.println(d.getValue(0));
            System.out.println(d.getValue(1));

            System.out.println();

            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            data2.printVal(outStream);
            System.out.println(outStream);

            System.out.println();
        } catch (MalformedURLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DDSException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DAP2Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

Sample Output:
282.09
284.45

Grid {
 ARRAY:
    Float32 Temperature_height_above_ground[time = 65][height_above_ground = 1][lat = 1][lon = 1];
 MAPS:
    Int32 time[time = 65];
    Float64 height_above_ground[height_above_ground = 1];
    Float64 lat[lat = 1];
    Float64 lon[lon = 1];
} Temperature_height_above_ground = { ARRAY: {{{{282.09}}},{{{284.45}}},{{{288.29}}},{{{288.64}}},{{{285.92}}},{{{284.4}}},{{{282.76}}},{{{281.52}}},{{{281.52}}},{{{283.9}}},{{{287.4}}},{{{289.1}}},{{{286.5}}},{{{285.2}}},{{{283.86}}},{{{283.12}}},{{{283.1}}},{{{285.7}}},{{{288.5}}},{{{289.6}}},{{{287.1}}},{{{287.0}}},{{{287.0}}},{{{286.33}}},{{{286.16}}},{{{288.2}}},{{{291.4}}},{{{292.3}}},{{{289.2}}},{{{287.77}}},{{{287.78}}},{{{285.6}}},{{{285.9}}},{{{289.86}}},{{{292.49}}},{{{292.83}}},{{{289.36}}},{{{288.71}}},{{{288.11}}},{{{287.25}}},{{{287.63}}},{{{291.37}}},{{{293.49}}},{{{293.32}}},{{{289.89}}},{{{289.09}}},{{{288.3}}},{{{287.5}}},{{{289.03}}},{{{293.02}}},{{{295.09}}},{{{294.89}}},{{{290.57}}},{{{289.42}}},{{{288.45}}},{{{287.8}}},{{{288.85}}},{{{293.02}}},{{{294.5}}},{{{293.83}}},{{{289.87}}},{{{289.11}}},{{{288.62}}},{{{288.04}}},{{{289.4}}}} MAPS: {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 102, 105, 108, 111, 114, 117, 120, 123, 126, 129, 132, 135, 138, 141, 144, 147, 150, 153, 156, 159, 162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 192}, {2.0}, {22.5}, {114.0} };


本文連結