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

The following examples show how to use ucar.nc2.Variable#setCachedData() . 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 writeRecord(PointObsDatatype pobs, StructureData sdata) throws IOException {
  if (debug)
    System.out.println("pobs= " + pobs);

  ucar.unidata.geoloc.EarthLocation loc = pobs.getLocation();
  int count =
      writeCoordinates(loc.getLatitude(), loc.getLongitude(), loc.getAltitude(), pobs.getObservationTimeAsDate());

  for (int i = count; i < recordVars.size(); i++) {
    Variable v = recordVars.get(i);
    if (debug)
      System.out.println(" var= " + v.getShortName());
    // assert v.hasCachedData(); ??
    v.setCachedData(sdata.getArray(v.getShortName()), false);
  }

  ncfileOut.writeRecordData(recordVars);
}
 
Example 2
private void setMeanAzimuth() {
  if (getType() != null) {
    Array spData = null;
    try {
      Variable sp = ds.findVariable("azimuth");
      spData = sp.read();
      sp.setCachedData(spData, false);

    } catch (IOException e) {
      e.printStackTrace();
      meanAzimuth = 0.0;
    }
    meanAzimuth = MAMath.sumDouble(spData) / spData.getSize();

  } else
    meanAzimuth = 0.0;

}
 
Example 3
protected void setDatasetAcquireProxy(DatasetProxyReader proxy, Group g) throws IOException {

    // all normal (non agg) variables must use a proxy to lock the file
    for (Variable v : g.getVariables()) {

      if (v.getProxyReader() != v) {
        if (debugProxy)
          System.out.println(" debugProxy: hasProxyReader " + v.getFullName());
        continue; // dont mess with agg variables
      }

      if (v.isCaching()) { // cache the small ones
        v.setCachedData(v.read()); // cache the variableDS directly

      } else { // put proxy on the rest
        v.setProxyReader(proxy);
        if (debugProxy)
          System.out.println(" debugProxy: set proxy on " + v.getFullName());
      }
    }

    // recurse
    for (Group nested : g.getGroups()) {
      setDatasetAcquireProxy(proxy, nested);
    }
  }
 
Example 4
private int writeCoordinates(double lat, double lon, double alt, Date time) {
  int count = 0;

  // time
  ArrayDouble.D0 tdata = new ArrayDouble.D0();
  double secs = (double) (time.getTime() / 1000.);
  tdata.set(secs);
  Variable v = recordVars.get(count++);
  v.setCachedData(tdata, false);

  // lat
  ArrayDouble.D0 latData = new ArrayDouble.D0();
  latData.set(lat);
  v = recordVars.get(count++);
  v.setCachedData(latData, false);

  // lon
  ArrayDouble.D0 lonData = new ArrayDouble.D0();
  lonData.set(lon);
  v = recordVars.get(count++);
  v.setCachedData(lonData, false);

  // alt
  if (useAlt) {
    ArrayDouble.D0 altData = new ArrayDouble.D0();
    altData.set(alt);
    v = recordVars.get(count++);
    v.setCachedData(altData, false);
  }

  return count;
}
 
Example 5
public void writeRecord(PointFeature pf, StructureData sdata) throws IOException {
  if (debug)
    System.out.println("PointFeature= " + pf);

  EarthLocation loc = pf.getLocation();
  int count = writeCoordinates(loc.getLatitude(), loc.getLongitude(), loc.getAltitude(),
      pf.getObservationTimeAsCalendarDate().toDate());

  for (int i = count; i < recordVars.size(); i++) {
    Variable v = recordVars.get(i);
    v.setCachedData(sdata.getArray(v.getShortName()), false);
  }

  ncfileOut.writeRecordData(recordVars);
}
 
Example 6
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 7
private void setMeanElevation() {
  if (Double.isNaN(meanElevation)) {
    try {
      Variable sp = ds.findVariable("elevation");
      Array spData = sp.read();
      sp.setCachedData(spData, false);
      meanElevation = MAMath.sumDouble(spData) / spData.getSize();
    } catch (IOException e) {
      e.printStackTrace();
      meanElevation = 0.0;
    }
  }
}
 
Example 8
public float[] getElevation() {
  float[] spArray = null;
  try {
    Variable sp = ds.findVariable("elevation");
    Array spData = sp.read();
    sp.setCachedData(spData, false);
    spArray = (float[]) spData.get1DJavaArray(float.class);

  } catch (IOException e) {
    e.printStackTrace();
  }

  return spArray;
}
 
Example 9
public float getAzimuth(int ray) throws IOException {
  Variable sp = ds.findVariable("azimuth");
  Array spData = sp.read();
  sp.setCachedData(spData, false);
  Index index = spData.getIndex();
  return spData.getFloat(index.set(ray));
}
 
Example 10
public float getRadialDistance(int gate) throws IOException {
  Variable sp = ds.findVariable("gate");
  Array spData = sp.read();
  sp.setCachedData(spData, false);
  Index index = spData.getIndex();
  return spData.getFloat(index.set(gate));
}
 
Example 11
public float getTime(int ray) throws IOException {
  Variable sp = ds.findVariable("rays_time");
  Array timeData = sp.read();
  sp.setCachedData(timeData, false);
  Index index = timeData.getIndex();
  return timeData.getFloat(index.set(ray));
}
 
Example 12
private void setNumrecs(int n) throws IOException, InvalidRangeException {
  if (n <= header.numrecs)
    return;
  int startRec = header.numrecs;

  // fileUsed = recStart + recsize * n;
  ((N3headerWriter) header).setNumrecs(n);
  // this.numrecs = n;

  // TODO udim.setLength : need UnlimitedDimension extends Dimension?
  // need to let unlimited dimension know of new shape
  for (Dimension dim : ncfile.getRootGroup().getDimensions()) {
    if (dim.isUnlimited())
      dim.setLength(n);
  }

  // need to let all unlimited variables know of new shape TODO immutable??
  for (Variable v : ncfile.getVariables()) {
    if (v.isUnlimited()) {
      v.resetShape();
      v.setCachedData(null, false);
    }
  }

  // extend file, handle filling
  if (fill)
    fillRecordVariables(startRec, n);
  else
    raf.setMinLength(header.calcFileSize());
}
 
Example 13
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 14
private void makeCoordinateDataWithMissing(String abbrev, Variable time, Variable elev, Variable azi,
    Variable nradialsVar, Variable ngatesVar, List<List<Ray>> groups) {

  Array timeData = Array.factory(time.getDataType(), time.getShape());
  Index timeIndex = timeData.getIndex();

  Array elevData = Array.factory(elev.getDataType(), elev.getShape());
  Index elevIndex = elevData.getIndex();

  Array aziData = Array.factory(azi.getDataType(), azi.getShape());
  Index aziIndex = aziData.getIndex();

  Array nradialsData = Array.factory(nradialsVar.getDataType(), nradialsVar.getShape());
  IndexIterator nradialsIter = nradialsData.getIndexIterator();

  Array ngatesData = Array.factory(ngatesVar.getDataType(), ngatesVar.getShape());
  IndexIterator ngatesIter = ngatesData.getIndexIterator();

  // first fill with missing data
  IndexIterator ii = timeData.getIndexIterator();
  while (ii.hasNext())
    ii.setIntNext(MISSING_INT);

  ii = elevData.getIndexIterator();
  while (ii.hasNext())
    ii.setFloatNext(MISSING_FLOAT);

  ii = aziData.getIndexIterator();
  while (ii.hasNext())
    ii.setFloatNext(MISSING_FLOAT);

  int nscans = groups.size();
  try {
    for (int scan = 0; scan < nscans; scan++) {
      List<Ray> scanGroup = groups.get(scan);
      int nradials = scanGroup.size();

      int radial = 0;
      boolean needFirst = true;
      for (Ray r : scanGroup) {
        if (needFirst) {
          ngatesIter.setIntNext(r.getGateCount(abbrev));
          needFirst = false;
        }

        timeData.setLong(timeIndex.set(scan, radial), r.data_msecs);
        elevData.setFloat(elevIndex.set(scan, radial), r.getElevation());
        aziData.setFloat(aziIndex.set(scan, radial), r.getAzimuth());
        radial++;
      }

      nradialsIter.setIntNext(nradials);
    }
  } catch (java.lang.ArrayIndexOutOfBoundsException ae) {
    throw new RuntimeException(ae);
  }
  time.setCachedData(timeData, false);
  elev.setCachedData(elevData, false);
  azi.setCachedData(aziData, false);
  nradialsVar.setCachedData(nradialsData, false);
  ngatesVar.setCachedData(ngatesData, false);
}
 
Example 15
public float[] getAzimuth() throws IOException {
  Variable sp = ds.findVariable("azimuth");
  Array spData = sp.read();
  sp.setCachedData(spData, false);
  return (float[]) spData.get1DJavaArray(float.class);
}
 
Example 16
public void augmentDataset(NetcdfDataset ncDataset, CancelTask cancelTask) throws IOException {
  /*
   * float lat = 40.45f;
   * float lon = -104.64f;
   * ProjectionImpl projection = new FlatEarth(lat, lon);
   * 
   * Variable ct = new Variable( ncDataset, null, null, projection.getClassName());
   * ct.setDataType( DataType.CHAR);
   * ct.setDimensions( "");
   * 
   * ct.addAttribute( new Attribute("grid_mapping_name", "flat_earth"));
   * ct.addAttribute( new Attribute(_Coordinate.TransformType, "Projection"));
   * ct.addAttribute( new Attribute(_Coordinate.Axes, "GeoX GeoY"));
   * ncDataset.addVariable(null, ct);
   */
  NcMLReader.wrapNcMLresource(ncDataset, CoordSysBuilder.resourcesDir + "CEDRICRadar.ncml", cancelTask);
  Variable lat = ncDataset.findVariable("radar_latitude");
  Variable lon = ncDataset.findVariable("radar_longitude");
  float latv = (float) lat.readScalarDouble();
  float lonv = (float) lon.readScalarDouble();
  Variable pv = ncDataset.findVariable("Projection");
  pv.addAttribute(new Attribute("longitude_of_projection_origin", lonv));
  pv.addAttribute(new Attribute("latitude_of_projection_origin", latv));

  Variable tvar = ncDataset.findVariable("time");
  /*
   * Date dt = null;
   * Variable sdate = ncDataset.findVariable("start_date");
   * Variable stime = ncDataset.findVariable("start_time");
   * String dateStr = sdate.readScalarString();
   * String timeStr = stime.readScalarString();
   * try {
   * dt = DateUtil.parse(dateStr + " " + timeStr);
   * } catch (Exception e) {}
   */

  int nt = 1;

  ArrayDouble.D1 data = new ArrayDouble.D1(nt);

  // fake
  data.setDouble(0, 0);
  // data.setDouble(0, dt.getTime()/1000);

  tvar.setCachedData(data, false);

  super.augmentDataset(ncDataset, cancelTask);
}
 
Example 17
private void transferGroup(Group srcGroup, Group targetGroup, NetcdfDataset target) throws IOException {
  // group attributes
  DatasetConstructor.transferGroupAttributes(srcGroup, targetGroup);

  // dimensions
  for (Dimension d : srcGroup.getDimensions()) {
    if (null == targetGroup.findDimensionLocal(d.getShortName())) {
      Dimension newd =
          new Dimension(d.getShortName(), d.getLength(), d.isShared(), d.isUnlimited(), d.isVariableLength());
      targetGroup.addDimension(newd);
    }
  }

  // transfer variables - eliminate any references to component files
  for (Variable v : srcGroup.getVariables()) {
    Variable targetV = targetGroup.findVariableLocal(v.getShortName());

    if (null == targetV) { // add it
      if (v instanceof Structure) {
        targetV = new StructureDS(target, targetGroup, null, v.getShortName(), v.getDimensionsString(),
            v.getUnitsString(), v.getDescription());
        // LOOK - not adding the members here - what to do ??

      } else {
        targetV = new VariableDS(target, targetGroup, null, v.getShortName(), v.getDataType(),
            v.getDimensionsString(), v.getUnitsString(), v.getDescription());
      }

      DatasetConstructor.transferVariableAttributes(v, targetV);
      VariableDS vds = (VariableDS) v;
      targetV.setSPobject(vds); // temporary, for non-agg variables when proto is made
      if (vds.hasCachedDataRecurse()) {
        if (vds.getSize() > 1000 * 1000) {
          boolean wtf = vds.hasCachedDataRecurse();
        }
        targetV.setCachedData(vds.read()); //
      }
      targetGroup.addVariable(targetV);
    }
  }

  // nested groups - check if target already has it
  for (Group srcNested : srcGroup.getGroups()) {
    Group nested = targetGroup.findGroupLocal(srcNested.getShortName());
    if (null == nested) {
      nested = new Group(target, targetGroup, srcNested.getShortName());
      targetGroup.addGroup(nested);
      for (EnumTypedef et : srcNested.getEnumTypedefs()) {
        targetGroup.addEnumeration(et);
      }
    }
    transferGroup(srcNested, nested, target);
  }
}