Example #1
0
  @Test
  public void testStride() throws InvalidRangeException {
    int nx = 1237;
    Range r = new Range(0, nx - 1, 9);
    System.out.printf("%s%n", r);
    System.out.printf("%d %d %d %d%n", r.first(), r.last(), r.stride(), r.length());
    assert r.first() == 0;
    assert r.last() == 1233;
    assert r.stride() == 9;
    assert r.length() == 138;

    Section s = new Section(r, r);
    Section.Iterator iter = s.getIterator(new int[] {nx, nx});
    int[] iterResult = new int[2];
    int count = 0;
    for (int y = r.first(); y <= r.last(); y += r.stride()) {
      for (int x = r.first(); x <= r.last(); x += r.stride()) {
        assert iter.hasNext();
        int iterN = iter.next(iterResult);
        assert iterResult[0] == y;
        assert iterResult[1] == x;
        assert iterN == y * nx + x;
        count++;
      }
    }
    assert count == 138 * 138;
  }
Example #2
0
  /**
   * Determine if a given Range intersects this one. NOTE: we dont yet support intersection when
   * both Ranges have strides
   *
   * @param r range to intersect
   * @return true if they intersect
   * @throws UnsupportedOperationException if both Ranges have strides
   */
  public boolean intersects(Range r) {
    if ((length() == 0) || (r.length() == 0)) return false;
    if (this == VLEN || r == VLEN) return true;

    int last = Math.min(this.last(), r.last());
    int stride = stride() * r.stride();

    int useFirst;
    if (stride == 1) {
      useFirst = Math.max(this.first(), r.first());

    } else if (stride() == 1) { // then r has a stride

      if (r.first() >= first()) useFirst = r.first();
      else {
        int incr = (first() - r.first()) / stride;
        useFirst = r.first() + incr * stride;
        if (useFirst < first()) useFirst += stride;
      }

    } else if (r.stride() == 1) { // then this has a stride

      if (first() >= r.first()) useFirst = first();
      else {
        int incr = (r.first() - first()) / stride;
        useFirst = first() + incr * stride;
        if (useFirst < r.first()) useFirst += stride;
      }

    } else {
      throw new UnsupportedOperationException("Intersection when both ranges have a stride");
    }

    return (useFirst <= last);
  }
  public Array readData(Variable v2, Section section) throws IOException, InvalidRangeException {
    // Vgroup vgroup = (Vgroup) v2.getSPobject();
    // Range scanRange = section.getRange(0);
    // Range radialRange = section.getRange(1);
    // Range gateRange = section.getRange(2);

    Array data = Array.factory(v2.getDataType().getPrimitiveClassType(), section.getShape());
    IndexIterator ii = data.getIndexIterator();

    List<List<Ray>> groups;
    String shortName = v2.getShortName();
    if (shortName.startsWith("Reflectivity")) groups = volScan.getReflectivityGroups();
    else if (shortName.startsWith("Velocity")) groups = volScan.getVelocityGroups();
    else if (shortName.startsWith("TotalPower")) groups = volScan.getTotalPowerGroups();
    else if (shortName.startsWith("Width")) groups = volScan.getWidthGroups();
    else if (shortName.startsWith("DiffReflectivity"))
      groups = volScan.getDifferentialReflectivityGroups();
    else throw new IllegalStateException("Illegal variable name = " + shortName);

    if (section.getRank() == 2) {
      Range radialRange = section.getRange(0);
      Range gateRange = section.getRange(1);
      List<Ray> lli = groups.get(0);
      readOneScan(lli, radialRange, gateRange, ii);
    } else {
      Range scanRange = section.getRange(0);
      Range radialRange = section.getRange(1);
      Range gateRange = section.getRange(2);
      for (int i = scanRange.first(); i <= scanRange.last(); i += scanRange.stride()) {
        readOneScan(groups.get(i), radialRange, gateRange, ii);
      }
    }
    return data;
  }
 private void readOneRadial(Ray r, Range gateRange, IndexIterator ii) throws IOException {
   if (r == null) {
     for (int i = gateRange.first(); i <= gateRange.last(); i += gateRange.stride())
       ii.setFloatNext(Float.NaN);
     return;
   }
   r.readData(volScan.raf, gateRange, ii);
 }
 private void readOneScan(
     Cinrad2Record[] mapScan, Range radialRange, Range gateRange, int datatype, IndexIterator ii)
     throws IOException {
   for (int i = radialRange.first(); i <= radialRange.last(); i += radialRange.stride()) {
     Cinrad2Record r = mapScan[i];
     readOneRadial(r, datatype, gateRange, ii);
   }
 }
 private void readOneRadial(Cinrad2Record r, int datatype, Range gateRange, IndexIterator ii)
     throws IOException {
   if (r == null) {
     for (int i = gateRange.first(); i <= gateRange.last(); i += gateRange.stride())
       ii.setByteNext(Cinrad2Record.MISSING_DATA);
     return;
   }
   r.readData(volScan.raf, datatype, gateRange, ii);
 }
Example #7
0
  /**
   * Create a new Range by making the union with a Range using same interval as this Range. NOTE: no
   * strides
   *
   * @param r range to add
   * @return intersected Range, may be EMPTY
   * @throws InvalidRangeException elements must be nonnegative
   */
  public Range union(Range r) throws InvalidRangeException {
    if (length() == 0) return r;
    if (this == VLEN || r == VLEN) return VLEN;

    if (r.length() == 0) return this;

    int first = Math.min(this.first(), r.first());
    int last = Math.max(this.last(), r.last());
    return new Range(name, first, last);
  }
Example #8
0
 /**
  * Create a new Range by composing a Range that is reletive to this Range. Revised 2013/04/19 by
  * Dennis Heimbigner to handle edge cases. See the commentary associated with the netcdf-c file
  * dceconstraints.h, function dceslicecompose().
  *
  * @param r range reletive to base
  * @return combined Range, may be EMPTY
  * @throws InvalidRangeException elements must be nonnegative, 0 <= first <= last
  */
 public Range compose(Range r) throws InvalidRangeException {
   if ((length() == 0) || (r.length() == 0)) return EMPTY;
   if (this == VLEN || r == VLEN) return VLEN;
   if (false) { // Original version
     // Note that this version assumes that range r is
     // correct with respect to this.
     int first = element(r.first());
     int stride = stride() * r.stride();
     int last = element(r.last());
     return new Range(name, first, last, stride);
   } else { // new version: handles versions all values of r.
     int sr_stride = stride() * r.stride();
     int sr_first = element(r.first()); // MAP(this,i) == element(i)
     int lastx = element(r.last());
     int sr_last = (last() < lastx ? last() : lastx); // min(last(),lastx)
     // unused int sr_length = (sr_last + 1) - sr_first;
     return new Range(name, sr_first, sr_last, sr_stride);
   }
 }
 private void readOneScan(List<Ray> mapScan, Range radialRange, Range gateRange, IndexIterator ii)
     throws IOException {
   int siz = mapScan.size();
   for (int i = radialRange.first(); i <= radialRange.last(); i += radialRange.stride()) {
     if (i >= siz) readOneRadial(null, gateRange, ii);
     else {
       Ray r = mapScan.get(i);
       readOneRadial(r, gateRange, ii);
     }
   }
 }
Example #10
0
  /**
   * Create a new GeoGrid that is a logical subset of this GeoGrid.
   *
   * @param t_range subset the time dimension, or null if you want all of it
   * @param z_range subset the vertical dimension, or null if you want all of it
   * @param bbox a lat/lon bounding box, or null if you want all x,y
   * @param z_stride use only if z_range is null, then take all z with this stride (1 means all)
   * @param y_stride use this stride on the y coordinate (1 means all)
   * @param x_stride use this stride on the x coordinate (1 means all)
   * @return subsetted GeoGrid
   * @throws InvalidRangeException if bbox does not intersect GeoGrid
   */
  public GeoGrid subset(
      Range t_range, Range z_range, LatLonRect bbox, int z_stride, int y_stride, int x_stride)
      throws InvalidRangeException {

    if ((z_range == null) && (z_stride > 1)) {
      Dimension zdim = getZDimension();
      if (zdim != null) z_range = new Range(0, zdim.getLength() - 1, z_stride);
    }

    Range y_range = null, x_range = null;
    if (bbox != null) {
      List yx_ranges = gcs.getRangesFromLatLonRect(bbox);
      y_range = (Range) yx_ranges.get(0);
      x_range = (Range) yx_ranges.get(1);
    }

    if (y_stride > 1) {
      if (y_range == null) {
        Dimension ydim = getYDimension();
        y_range = new Range(0, ydim.getLength() - 1, y_stride);
      } else {
        y_range = new Range(y_range.first(), y_range.last(), y_stride);
      }
    }

    if (x_stride > 1) {
      if (x_range == null) {
        Dimension xdim = getXDimension();
        x_range = new Range(0, xdim.getLength() - 1, x_stride);
      } else {
        x_range = new Range(x_range.first(), x_range.last(), x_stride);
      }
    }

    return subset(t_range, z_range, y_range, x_range);
  }
  public Array readData(Variable v2, Section section) throws IOException, InvalidRangeException {
    Vgroup vgroup = (Vgroup) v2.getSPobject();

    Range scanRange = section.getRange(0);
    Range radialRange = section.getRange(1);
    Range gateRange = section.getRange(2);

    Array data = Array.factory(v2.getDataType().getPrimitiveClassType(), section.getShape());
    IndexIterator ii = data.getIndexIterator();

    for (int i = scanRange.first(); i <= scanRange.last(); i += scanRange.stride()) {
      Cinrad2Record[] mapScan = vgroup.map[i];
      readOneScan(mapScan, radialRange, gateRange, vgroup.datatype, ii);
    }

    return data;
  }
Example #12
0
  /**
   * Create a new Range by intersecting with a Range using same interval as this Range. NOTE: we
   * dont yet support intersection when both Ranges have strides
   *
   * @param r range to intersect
   * @return intersected Range, may be EMPTY
   * @throws InvalidRangeException elements must be nonnegative
   */
  public Range intersect(Range r) throws InvalidRangeException {
    if ((length() == 0) || (r.length() == 0)) return EMPTY;
    if (this == VLEN || r == VLEN) return VLEN;

    int last = Math.min(this.last(), r.last());
    int stride = stride() * r.stride();

    int useFirst;
    if (stride == 1) {
      useFirst = Math.max(this.first(), r.first());

    } else if (stride() == 1) { // then r has a stride

      if (r.first() >= first()) useFirst = r.first();
      else {
        int incr = (first() - r.first()) / stride;
        useFirst = r.first() + incr * stride;
        if (useFirst < first()) useFirst += stride;
      }

    } else if (r.stride() == 1) { // then this has a stride

      if (first() >= r.first()) useFirst = first();
      else {
        int incr = (r.first() - first()) / stride;
        useFirst = first() + incr * stride;
        if (useFirst < r.first()) useFirst += stride;
      }

    } else {
      throw new UnsupportedOperationException("Intersection when both ranges have a stride");
    }

    if (useFirst > last) return EMPTY;
    return new Range(name, useFirst, last, stride);
  }
Example #13
0
 /**
  * Copy Constructor with name
  *
  * @param name result name
  * @param r copy from here
  */
 public Range(String name, Range r) {
   this.name = name;
   first = r.first();
   n = r.length();
   stride = r.stride();
 }
Example #14
0
 /**
  * Copy Constructor
  *
  * @param r copy from here
  */
 public Range(Range r) {
   first = r.first();
   n = r.length();
   stride = r.stride();
   name = r.getName();
 }