public static void main(String[] args) { // when dods using no ending // NetcdfFile doesnt work // http://thredds1.pfeg.noaa.gov:8080/thredds/dodsC/satellite/BA/ssta/mday.dods?BAssta[0:1:20][0:1:0][0:10:0][0:10:0] String url = "file:/Users/bermudez/downloads/SADCec95-UNC_WANAFp50-UFL_20080921T1800_20080921T1900_20080926T1800_11dvel_Z.nc"; // BAssta[0:1:73][0:1:0][1000:1:1000][2400:1:2400] try { NetcdfDataset netcdfdataset = NetcdfDataset.openDataset(url); NCdump.print(netcdfdataset, "", System.out, null); // List<Variable> vars = netcdfdataset.getVariables(); // for (Iterator iterator = vars.iterator(); iterator.hasNext();) { // ucar.nc2.Variable var = (ucar.nc2.Variable) iterator.next(); // doVar(netcdfdataset,var.getName(),"0:2:2"); //// NCdump.printVariableData(var,null); // System.out.println(var.getName()); // } // doVar(netcdfdataset, "BAssta", "1:73,0,1000:1010,2400"); // doVar(netcdfdataset, "lon", "0:100"); // doVar(netcdfdataset, "lat", "0:100"); // doVar(netcdfdataset, "time", "0:30"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static void doVar(NetcdfDataset netcdfdataset, String varS, String limits) { System.out.println("\r---------\r\r"); ucar.nc2.Variable var = netcdfdataset.findVariable(varS); System.out.println(var.getName() + " " + var.getSize()); Array arr; try { arr = var.read(limits); NCdump.printArray(arr, "array", System.out, null); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvalidRangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void read5kmlatlong() { NetcdfFile ncfile = null; try { ncfile = NetcdfFile.open("/home/kerryn/Desktop/Temp/treecover_30pts_lat.nc"); String xrange = "Band1"; Variable xrangeVar = ncfile.findVariable(xrange); Array xrangeData = xrangeVar.read(); NCdump.printArray(xrangeData, xrange, System.out, null); String yrange = "GDAL_Geographics"; Variable yrangeVar = ncfile.findVariable(yrange); Array yrangeData = yrangeVar.read(); NCdump.printArray(yrangeData, yrange, System.out, null); List<ucar.nc2.Attribute> yattr = yrangeVar.getAttributes(); for (ucar.nc2.Attribute attr : yattr) { String attrName = attr.getName(); Array attrValue = attr.getValues(); attrValue.getElementType(); double value = attrValue.getDouble(0); System.out.println(attrName + "=" + value); } // [Northernmost_Northing = -37.51903101958847, Southernmost_Northing = -38.49309598467985, // Easternmost_Easting = 145.56979076552813, Westernmost_Easting = 144.4949604592204, // spatial_ref = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS // 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]]", GeoTransform = "144.495 0.0335884 0 -37.519 0 -0.0335884 ", grid_mapping_name = "Geographics Coordinate System", long_name = "Grid_latitude"] // String zrange = "z_range"; // Variable zrangeVar = ncfile.findVariable(zrange); // Array zrangeData = zrangeVar.read(); // NCdump.printArray(zrangeData, zrange, System.out, null); // // String spacing = "spacing"; // Variable spacingVar = ncfile.findVariable(spacing); // Array spacingData = spacingVar.read(); // NCdump.printArray(spacingData, spacing, System.out, null); // // String dimension = "dimension"; // Variable dimensionVar = ncfile.findVariable(dimension); // Array dimensionData = dimensionVar.read(); // NCdump.printArray(dimensionData, dimension, System.out, null); // // String z = "z"; // Variable zVar = ncfile.findVariable(z); // Array zData = zVar.read(); // NCdump.printArray(zData, z, System.out, null); // int count = 0; // //for (int x=0;x<94;x++) // for (int x=0;x<30;x++) // 106.92 km // { // //for (int y=0;y<108;y++) // for (int y=0;y<30;y++) // 93.06 km // { // double item = zData.getDouble(count); // long percent = Math.round(item * 100); // if (percent < 0) // { // percent = 0; // } // String percentStr = new Integer((int)percent).toString(); // if (percentStr.length() == 1) // { // percentStr = "0" + percentStr; // } // if (percentStr.equals("00")) // { // percentStr = " "; // } // // //System.out.println(x + " " + y + " " + item); // System.out.print( " " + percentStr); // count ++; // } // System.out.println(); // } } catch (IOException ioe) { ioe.printStackTrace(); } }
public void read5km() { NetcdfFile ncfile = null; try { ncfile = NetcdfFile.open("treecover_30pts.nc"); String xrange = "x_range"; Variable xrangeVar = ncfile.findVariable(xrange); Array xrangeData = xrangeVar.read(); NCdump.printArray(xrangeData, xrange, System.out, null); String yrange = "y_range"; Variable yrangeVar = ncfile.findVariable(yrange); Array yrangeData = yrangeVar.read(); NCdump.printArray(yrangeData, yrange, System.out, null); String zrange = "z_range"; Variable zrangeVar = ncfile.findVariable(zrange); Array zrangeData = zrangeVar.read(); NCdump.printArray(zrangeData, zrange, System.out, null); String spacing = "spacing"; Variable spacingVar = ncfile.findVariable(spacing); Array spacingData = spacingVar.read(); NCdump.printArray(spacingData, spacing, System.out, null); String dimension = "dimension"; Variable dimensionVar = ncfile.findVariable(dimension); Array dimensionData = dimensionVar.read(); NCdump.printArray(dimensionData, dimension, System.out, null); String z = "z"; Variable zVar = ncfile.findVariable(z); Array zData = zVar.read(); NCdump.printArray(zData, z, System.out, null); int count = 0; // for (int x=0;x<94;x++) for (int x = 0; x < 30; x++) // 106.92 km { // for (int y=0;y<108;y++) for (int y = 0; y < 30; y++) // 93.06 km { double item = zData.getDouble(count); long percent = Math.round(item * 100); if (percent < 0) { percent = 0; } String percentStr = new Integer((int) percent).toString(); if (percentStr.length() == 1) { percentStr = "0" + percentStr; } if (percentStr.equals("00")) { percentStr = " "; } // System.out.println(x + " " + y + " " + item); System.out.print(" " + percentStr); count++; } System.out.println(); } } catch (IOException ioe) { ioe.printStackTrace(); } }