Java Code Examples for ucar.nc2.Variable#setDataType()

The following examples show how to use ucar.nc2.Variable#setDataType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public void open(RandomAccessFile raf, NetcdfFile ncfile, CancelTask cancelTask) throws IOException {
  super.open(raf, ncfile, cancelTask);

  readHDR();

  ncfile.addDimension(null, new Dimension("lat", nlats));
  ncfile.addDimension(null, new Dimension("lon", nlons));

  Variable elev = new Variable(ncfile, null, null, "elevation");
  elev.setDataType(DataType.SHORT);
  elev.setDimensions("lat lon");

  elev.addAttribute(new Attribute(CDM.UNITS, "m"));
  elev.addAttribute(new Attribute("units_desc", "meters above sea level"));
  elev.addAttribute(new Attribute(CDM.LONG_NAME, "digital elevation in meters above mean sea level"));
  elev.addAttribute(new Attribute(CDM.MISSING_VALUE, (short) -9999));
  ncfile.addVariable(null, elev);

  Variable lat = new Variable(ncfile, null, null, "lat");
  lat.setDataType(DataType.FLOAT);
  lat.setDimensions("lat");
  lat.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS));
  ncfile.addVariable(null, lat);
  Array data = Array.makeArray(DataType.FLOAT, nlats, starty, -incr);
  lat.setCachedData(data, false);

  Variable lon = new Variable(ncfile, null, null, "lon");
  lon.setDataType(DataType.FLOAT);
  lon.setDimensions("lon");
  lon.addAttribute(new Attribute(CDM.UNITS, CDM.LON_UNITS));
  ncfile.addVariable(null, lon);
  Array lonData = Array.makeArray(DataType.FLOAT, nlons, startx, incr);
  lon.setCachedData(lonData, false);

  ncfile.addAttribute(null, new Attribute(CDM.CONVENTIONS, "CF-1.0"));
  ncfile.addAttribute(null, new Attribute("History", "Direct read by Netcdf-Java CDM library"));
  ncfile.addAttribute(null, new Attribute("Source", "http://eros.usgs.gov/products/elevation/gtopo30.html"));

  ncfile.finish();
}
 
Example 2
private void addLatLon2D(NetcdfFile ncfile, List<Variable> varList, Projection proj, CoordinateAxis xaxis,
    CoordinateAxis yaxis) throws IOException {

  double[] xData = (double[]) xaxis.read().get1DJavaArray(double.class);
  double[] yData = (double[]) yaxis.read().get1DJavaArray(double.class);

  List<Dimension> dims = new ArrayList<Dimension>();
  dims.add(yaxis.getDimension(0));
  dims.add(xaxis.getDimension(0));

  Variable latVar = new Variable(ncfile, null, null, "lat");
  latVar.setDataType(DataType.DOUBLE);
  latVar.setDimensions(dims);
  latVar.addAttribute(new Attribute(CDM.UNITS, CDM.LAT_UNITS));
  latVar.addAttribute(new Attribute(CDM.LONG_NAME, "latitude coordinate"));
  latVar.addAttribute(new Attribute(CF.STANDARD_NAME, "latitude"));
  latVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));

  Variable lonVar = new Variable(ncfile, null, null, "lon");
  lonVar.setDataType(DataType.DOUBLE);
  lonVar.setDimensions(dims);
  lonVar.addAttribute(new Attribute(CDM.UNITS, CDM.LON_UNITS));
  lonVar.addAttribute(new Attribute(CDM.LONG_NAME, "longitude coordinate"));
  lonVar.addAttribute(new Attribute(CF.STANDARD_NAME, "longitude"));
  lonVar.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));

  int nx = xData.length;
  int ny = yData.length;

  // create the data
  ProjectionPointImpl projPoint = new ProjectionPointImpl();
  LatLonPointImpl latlonPoint = new LatLonPointImpl();
  double[] latData = new double[nx * ny];
  double[] lonData = new double[nx * ny];
  for (int i = 0; i < ny; i++) {
    for (int j = 0; j < nx; j++) {
      projPoint.setLocation(xData[j], yData[i]);
      proj.projToLatLon(projPoint, latlonPoint);
      latData[i * nx + j] = latlonPoint.getLatitude();
      lonData[i * nx + j] = latlonPoint.getLongitude();
    }
  }
  Array latDataArray = Array.factory(DataType.DOUBLE, new int[] {ny, nx}, latData);
  latVar.setCachedData(latDataArray, false);

  Array lonDataArray = Array.factory(DataType.DOUBLE, new int[] {ny, nx}, lonData);
  lonVar.setCachedData(lonDataArray, false);

  varList.add(latVar);
  varList.add(lonVar);
}
 
Example 3
public static DataType setDataType(short type, Variable v) {
  DataType dt;
  switch (type) {
    case 3:
    case 21:
      dt = DataType.UBYTE;
      break;
    case 4:
      dt = DataType.CHAR;
      break;
    case 5:
      dt = DataType.FLOAT;
      break;
    case 6:
      dt = DataType.DOUBLE;
      break;
    case 20:
      dt = DataType.BYTE;
      break;
    case 22:
      dt = DataType.SHORT;
      break;
    case 23:
      dt = DataType.USHORT;
      break;
    case 24:
      dt = DataType.INT;
      break;
    case 25:
      dt = DataType.UINT;
      break;
    case 26:
      dt = DataType.LONG;
      break;
    case 27:
      dt = DataType.ULONG;
      break;
    default:
      throw new IllegalStateException("unknown type= " + type);
  }

  if (v != null) {
    v.setDataType(dt);
  }

  return dt;
}