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

The following examples show how to use ucar.nc2.Variable#setDimensions() . 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
private void createTimeCoordinate(NetcdfDataset ds, Variable ncVar) {
  // Time coordinate is stored in the attribute validTimes
  // One caveat is that the times have two bounds an upper and a lower

  // get the times values
  Attribute timesAtt = ncVar.findAttribute("validTimes");
  if (timesAtt == null)
    return;
  Array timesArray = timesAtt.getValues();

  // get every other one LOOK this is awkward
  try {
    int n = (int) timesArray.getSize();
    List<Range> list = new ArrayList<>();
    list.add(new Range(0, n - 1, 2));
    timesArray = timesArray.section(list);
  } catch (InvalidRangeException e) {
    throw new IllegalStateException(e);
  }

  // make sure it matches the dimension
  DataType dtype = DataType.getType(timesArray);
  int nTimesAtt = (int) timesArray.getSize();

  // create a special dimension and coordinate variable
  Dimension dimTime = ncVar.getDimension(0);
  int nTimesDim = dimTime.getLength();
  if (nTimesDim != nTimesAtt) {
    parseInfo.format(" **error ntimes in attribute (%d) doesnt match dimension length (%d) for variable %s%n",
        nTimesAtt, nTimesDim, ncVar.getFullName());
    return;
  }

  // add the dimension
  String dimName = ncVar.getFullName() + "_timeCoord";
  Dimension newDim = new Dimension(dimName, nTimesDim);
  ds.addDimension(null, newDim);

  // add the coordinate variable
  String units = "seconds since 1970-1-1 00:00:00";
  String desc = "time coordinate for " + ncVar.getFullName();

  CoordinateAxis1D timeCoord = new CoordinateAxis1D(ds, null, dimName, dtype, dimName, units, desc);
  timeCoord.setCachedData(timesArray, true);
  timeCoord.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Time.toString()));
  ds.addCoordinateAxis(timeCoord);

  parseInfo.format(" added coordinate variable %s%n", dimName);

  // now make the original variable use the new dimension
  List<Dimension> dimsList = new ArrayList(ncVar.getDimensions());
  dimsList.set(0, newDim);
  ncVar.setDimensions(dimsList);

  // better to explicitly set the coordinate system
  ncVar.addAttribute(new Attribute(_Coordinate.Axes, dimName + " yCoord xCoord"));

  // fix the attributes
  Attribute att = ncVar.findAttribute("fillValue");
  if (att != null)
    ncVar.addAttribute(new Attribute(CDM.FILL_VALUE, att.getNumericValue()));
  att = ncVar.findAttribute("descriptiveName");
  if (null != att)
    ncVar.addAttribute(new Attribute(CDM.LONG_NAME, att.getStringValue()));

  // ncVar.enhance();
}