Пример #1
0
  public void readByte2Short() throws Exception {
    Variable t2 = null;
    assert (null != (t2 = ncfileRead.findVariable("t2")));
    assert (t2.getDataType() == DataType.BYTE);

    Attribute att = t2.findAttribute(CDM.SCALE_FACTOR);
    assert (null != att);
    assert (!att.isArray());
    assert (1 == att.getLength());
    assert (2 == att.getNumericValue().doubleValue());
    assert (DataType.SHORT == att.getDataType());

    assert (null != (t2 = dsRead.findVariable("t2")));
    assert t2 instanceof VariableEnhanced;
    VariableDS vs = (VariableDS) t2;
    assert (vs.getDataType() == DataType.SHORT) : vs.getDataType();
    assert (!vs.hasMissing());

    Array A = vs.read();
    assert (A.getElementType() == short.class) : A.getElementType();
    Index ima = A.getIndex();
    int[] shape = A.getShape();
    int i, j;
    for (i = 0; i < shape[0]; i++) {
      for (j = 0; j < shape[1]; j++) {
        assert (A.getShort(ima.set(i, j)) == (2 * (i * 10 + j) + 77));
      }
    }
    System.out.println("**************TestStandardVar readByte2Short");
  }
Пример #2
0
  public void getTransientSeverity(int trfiresev[], int cid) {

    Index fireind = trseverityA.getIndex();

    for (int i = 0; i < ConstTime.MAX_TR_FIR_OCR_NUM; i++) {
      trfiresev[i] = trseverityA.getInt(fireind.set(cid, i));
    }
  };
Пример #3
0
  public void getTransientFireSeason(int trfireseason[], int cid) {

    Index fireind = trfireseasonA.getIndex();

    for (int i = 0; i < ConstTime.MAX_TR_FIR_OCR_NUM; i++) {
      trfireseason[i] = trfireseasonA.getInt(fireind.set(cid, i));
    }
  };
Пример #4
0
  public void getSpinupSeverity(int spfiresev[], int cid) {

    Index severity = spseverityA.getIndex();

    for (int i = 0; i < ConstTime.MAX_SP_FIR_OCR_NUM; i++) {
      spfiresev[i] = spseverityA.getInt(severity.set(cid, i));
    }
  };
Пример #5
0
  public void getSpinupFireSeason(int spfireseason[], int cid) {

    Index fireyri = spfireseasonA.getIndex();

    for (int i = 0; i < ConstTime.MAX_SP_FIR_OCR_NUM; i++) {
      spfireseason[i] = spfireseasonA.getInt(fireyri.set(cid, i));
    }
  };
Пример #6
0
 /**
  * Create a scalar numeric-valued Attribute.
  *
  * @param name name of Attribute
  * @param val value of Attribute
  */
 public Attribute(String name, Number val) {
   this.name = name;
   int[] shape = new int[1];
   shape[0] = 1;
   DataType dt = DataType.getType(val.getClass());
   Array vala = Array.factory(dt.getPrimitiveClassType(), shape);
   Index ima = vala.getIndex();
   vala.setDouble(ima.set0(0), val.doubleValue());
   setValues(vala);
 }
 private static Array createTargetArray(int sizeY, int sizeX) {
   final Array fillArray = Array.factory(DataType.BYTE, new int[] {1, sizeY, sizeX});
   final Index index = fillArray.getIndex();
   for (int y = 0; y < sizeY; y++) {
     for (int x = 0; x < sizeX; x++) {
       index.set(0, y, x);
       fillArray.setByte(index, Watermask.INVALID_WATER_FRACTION);
     }
   }
   return fillArray;
 }
Пример #8
0
 private Array convertToChar(Variable newVar, Array oldData) {
   ArrayChar newData = (ArrayChar) Array.factory(DataType.CHAR, newVar.getShape());
   Index ima = newData.getIndex();
   IndexIterator ii = oldData.getIndexIterator();
   while (ii.hasNext()) {
     String s = (String) ii.getObjectNext();
     int[] c = ii.getCurrentCounter();
     for (int i = 0; i < c.length; i++) ima.setDim(i, c[i]);
     newData.setString(ima, s);
   }
   return newData;
 }
Пример #9
0
  public void readShortMissing() throws Exception {
    Variable v = null;
    assert (null != (v = ncfileRead.findVariable("t4")));
    assert (v.getDataType() == DataType.SHORT);

    // default use of missing_value
    assert (null != (v = dsRead.findVariable("t4")));
    assert v instanceof VariableEnhanced;
    assert v instanceof VariableDS;
    VariableDS vs = (VariableDS) v;
    assert (vs.getDataType() == DataType.SHORT);

    Attribute att = vs.findAttribute(CDM.MISSING_VALUE);
    assert (null != att);
    assert (!att.isArray());
    assert (1 == att.getLength());
    System.out.println("missing_value = " + att.getNumericValue().shortValue());
    assert (((short) -9999) == att.getNumericValue().shortValue());
    assert (DataType.SHORT == att.getDataType());

    assert (vs.hasMissing());
    assert (vs.hasMissingValue());
    assert (vs.isMissing((double) ((short) -9999)));
    assert (vs.isMissingValue((double) ((short) -9999)));

    Array A = vs.read();
    Index ima = A.getIndex();
    int[] shape = A.getShape();
    int i, j;
    for (i = 0; i < shape[0]; i++) {
      for (j = 0; j < shape[1]; j++) {
        assert (A.getFloat(ima.set(i, j)) == (i * 10 + j));
      }
    }

    // turn off missing data
    vs.setMissingDataIsMissing(false);
    assert (vs.getDataType() == DataType.SHORT);

    assert (!vs.hasMissing());
    assert (vs.hasMissingValue());
    assert (!vs.isMissing((double) ((short) -9999)));
    assert (vs.isMissingValue((double) ((short) -9999)));

    vs.setMissingDataIsMissing(true);
    assert (vs.hasMissing());
    assert (vs.isMissing((double) ((short) -9999)));

    System.out.println("**************TestStandardVar Read readShortMissing");
  }
Пример #10
0
  private ArrayDouble.D1 makeHeight1D(
      Array eta, Array s, Array depth, Array c, double depth_c, int x_index, int y_index) {
    int nz = (int) s.getSize();
    Index sIndex = s.getIndex();
    Index cIndex = c.getIndex();

    Index etaIndex = eta.getIndex();
    Index depthIndex = depth.getIndex();

    ArrayDouble.D1 height = new ArrayDouble.D1(nz);

    for (int z = 0; z < nz; z++) {
      double sz = s.getDouble(sIndex.set(z));
      double cz = c.getDouble(cIndex.set(z));

      double term1 = depth_c * sz;

      double fac1 = depth.getDouble(depthIndex.set(y_index, x_index));
      double term2 = fac1 * cz;

      double Sterm = (term1 + term2) / (depth_c + fac1);

      double term3 = eta.getDouble(etaIndex.set(y_index, x_index));
      double term4 = (term3 + fac1) * Sterm;
      double hterm = term3 + term4;

      height.set(z, hterm);
    }

    return height;
  }
Пример #11
0
  private void doWrite2(NetcdfFileWriteable ncfile, String varName) throws Exception {
    Variable v = ncfile.findVariable(varName);
    int[] w = getWeights(v);

    int[] shape = v.getShape();
    Array aa = Array.factory(v.getDataType().getPrimitiveClassType(), shape);
    Index ima = aa.getIndex();
    for (int i = 0; i < shape[0]; i++) {
      for (int j = 0; j < shape[1]; j++) {
        aa.setDouble(ima.set(i, j), (double) (i * w[0] + j * w[1]));
      }
    }

    ncfile.write(varName, aa);
  }
Пример #12
0
  @Override
  public final Array apply(Array sourceArray, Item sourceColumn) throws RuleException {
    final int[] shape = getContext().getTargetVariable().getShape();
    final int sizeY = shape[1];
    final int sizeX = shape[2];
    final Array targetArray = createTargetArray(sizeY, sizeX);

    final Reader observationReader = getContext().getObservationReader();
    if (observationReader == null) {
      return targetArray;
    }

    final int recordNo = getContext().getObservation().getRecordNo();
    final GeoCoding geoCoding;
    try {
      geoCoding = observationReader.getGeoCoding(recordNo);
    } catch (IOException ignored) {
      return targetArray;
    }

    final Point point =
        getContext().getMatchup().getRefObs().getPoint().getGeometry().getFirstPoint();
    final double lon = point.getX();
    final double lat = point.getY();
    final PixelLocator pixelLocator = new GeoCodingWrapper(geoCoding);
    final Point2D p = new Point2D.Double();
    final boolean found = pixelLocator.getPixelLocation(lon, lat, p);
    if (found) {
      final Watermask watermask = Container.WATERMASK;
      final Index index = targetArray.getIndex();
      final int minX = (int) Math.max(p.getX() - sizeX / 2, 0.0);
      final int maxX =
          (int) Math.min(p.getX() + sizeX / 2, observationReader.getElementCount() - 1);
      final int minY = (int) Math.max(p.getY() - sizeY / 2, 0.0);
      final int maxY =
          (int) Math.min(p.getY() + sizeY / 2, observationReader.getScanLineCount() - 1);
      for (int y = minY, yi = 0; y <= maxY; y++, yi++) {
        for (int x = minX, xi = 0; x <= maxX; x++, xi++) {
          final byte waterFraction =
              watermask.getWaterFraction(x, y, pixelLocator, stepCountX, stepCountY);
          targetArray.setByte(index.set(0, yi, xi), waterFraction);
        }
      }
    }
    return targetArray;
  }
Пример #13
0
  public void readDouble() throws Exception {

    Variable t1 = null;
    assert (null != (t1 = ncfileRead.findVariable("t1")));
    assert (t1.getDataType() == DataType.DOUBLE);

    Attribute att = t1.findAttribute(CDM.SCALE_FACTOR);
    assert (null != att);
    assert (!att.isArray());
    assert (1 == att.getLength());
    assert (2.0 == att.getNumericValue().doubleValue());
    assert (DataType.DOUBLE == att.getDataType());

    // read
    Array A = t1.read();
    int i, j;
    Index ima = A.getIndex();
    int[] shape = A.getShape();

    for (i = 0; i < shape[0]; i++) {
      for (j = 0; j < shape[1]; j++) {
        assert (A.getDouble(ima.set(i, j)) == (double) (i * 10.0 + j));
      }
    }

    assert (null != (t1 = dsRead.findVariable("t1")));
    assert t1 instanceof VariableEnhanced;
    VariableEnhanced dsVar = (VariableEnhanced) t1;
    assert (dsVar.getDataType() == DataType.DOUBLE);

    A = dsVar.read();
    ima = A.getIndex();
    shape = A.getShape();

    for (i = 0; i < shape[0]; i++) {
      for (j = 0; j < shape[1]; j++) {
        assert (A.getDouble(ima.set(i, j)) == (2.0 * (i * 10.0 + j) + 77.0));
      }
    }

    assert (null == t1.findAttribute(CDM.SCALE_FACTOR));
    assert (null == t1.findAttribute("add_offset"));

    System.out.println("**************TestStandardVar ReadDouble");
  }
Пример #14
0
  public void readSlice(NetcdfFile ncfile, int[] origin, int[] shape, String name)
      throws IOException, InvalidRangeException {

    Variable v = ncfile.findVariable(name);

    Array data = v.read(origin, shape);
    assert data.getRank() == 3;
    assert data.getSize() == shape[0] * shape[1] * shape[2];
    assert data.getShape()[0] == shape[0] : data.getShape()[0] + " " + shape[0];
    assert data.getShape()[1] == shape[1];
    assert data.getShape()[2] == shape[2];
    assert data.getElementType() == double.class;

    Index tIndex = data.getIndex();
    for (int i = 0; i < shape[0]; i++)
      for (int j = 0; j < shape[1]; j++)
        for (int k = 0; k < shape[2]; k++) {
          double val = data.getDouble(tIndex.set(i, j, k));
          // System.out.println(" "+val);
          assert TestUtils.close(val, 100 * (i + origin[0]) + 10 * j + k) : val;
        }
  }
Пример #15
0
  public void readDoubleMissing() throws Exception {
    VariableDS v = null;
    assert (null != (v = (VariableDS) dsRead.findVariable("m1")));
    assert (v.getDataType() == DataType.DOUBLE);

    Array A = v.read();
    Index ima = A.getIndex();

    double val = A.getFloat(ima.set(1, 1));
    assert Double.isNaN(val);
    assert v.isMissing(val);

    // reread with useNans off
    v.setUseNaNs(false);
    v.createNewCache();
    A = v.read();
    ima = A.getIndex();

    val = A.getFloat(ima.set(1, 1));
    assert TestAll.closeEnough(val, -999.99) : val;
    assert v.isMissing(val);
  }
Пример #16
0
  public void testReadData(NetcdfFile ncfile, String name) throws IOException {

    Variable v = ncfile.findVariable(name);
    assert null != v;
    assert v.getShortName().equals(name);
    assert v.getRank() == 3;
    assert v.getSize() == 36 : v.getSize();
    assert v.getShape()[0] == 3;
    assert v.getShape()[1] == 3;
    assert v.getShape()[2] == 4;
    assert v.getDataType() == DataType.DOUBLE;

    assert !v.isCoordinateVariable();

    assert v.getDimension(0) == ncfile.findDimension("time");
    assert v.getDimension(1) == ncfile.findDimension("lat");
    assert v.getDimension(2) == ncfile.findDimension("lon");

    Array data = v.read();
    assert data.getRank() == 3;
    assert data.getSize() == 36;
    assert data.getShape()[0] == 3;
    assert data.getShape()[1] == 3;
    assert data.getShape()[2] == 4;
    assert data.getElementType() == double.class;

    int[] shape = data.getShape();
    Index tIndex = data.getIndex();
    for (int i = 0; i < shape[0]; i++)
      for (int j = 0; j < shape[1]; j++)
        for (int k = 0; k < shape[2]; k++) {
          double val = data.getDouble(tIndex.set(i, j, k));
          // System.out.println(" "+val);
          assert TestUtils.close(val, 100 * i + 10 * j + k) : val;
        }
  }
Пример #17
0
  public void readByte() throws Exception {
    Variable v = null;
    assert (null != (v = ncfileRead.findVariable("t3")));
    assert (v.getDataType() == DataType.BYTE);

    assert (null != (v = dsRead.findVariable("t3")));
    assert v instanceof VariableEnhanced;
    assert v instanceof VariableDS;
    VariableDS vs = (VariableDS) v;
    assert (vs.getDataType() == DataType.BYTE);

    Attribute att = vs.findAttribute("_FillValue");
    assert (null != att);
    assert (!att.isArray());
    assert (1 == att.getLength());
    System.out.println("_FillValue = " + att.getNumericValue().byteValue());
    assert (((byte) 255) == att.getNumericValue().byteValue());
    assert (DataType.BYTE == att.getDataType());

    assert (vs.hasMissing());
    assert (vs.hasFillValue());
    assert (vs.isMissing((double) ((byte) 255)));
    assert (vs.isFillValue((double) ((byte) 255)));

    Array A = vs.read();
    assert (A.getElementType() == byte.class) : A.getElementType();
    Index ima = A.getIndex();
    int[] shape = A.getShape();
    int i, j;
    for (i = 0; i < shape[0]; i++) {
      for (j = 0; j < shape[1]; j++) {
        assert (A.getFloat(ima.set(i, j)) == (i * 10 + j));
      }
    }
    System.out.println("**************TestStandardVar ReadByte");
  }
Пример #18
0
  @Before
  public void setUp() {
    int nlayers = 11;
    zeros_3D = Array.factory(Double.class, new int[] {1, nlayers, 3, 3});
    ones_3D = Array.factory(Double.class, new int[] {1, nlayers, 3, 3});
    u3D1 = Array.factory(Double.class, new int[] {1, nlayers, 3, 3});
    v3D1 = Array.factory(Double.class, new int[] {1, nlayers, 3, 3});
    u3D2 = Array.factory(Double.class, new int[] {1, nlayers, 3, 3});
    v3D2 = Array.factory(Double.class, new int[] {1, nlayers, 3, 3});

    Index idx = zeros_3D.getIndex();
    for (int k = 0; k < nlayers; k++) {
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          zeros_3D.setDouble(idx.set(0, k, i, j), 0);
          ones_3D.setDouble(idx, 1);
          u3D1.setDouble(idx, j);
          v3D1.setDouble(idx, i);
          u3D2.setDouble(idx, 2 - j);
          v3D2.setDouble(idx, 2 - i);
        }
      }
    }
  }
Пример #19
0
 /**
  * Projects the arrays of longitudes and latitudes to a Cylindrical Equidistant Projection to
  * bring x,y and z into a uniform coordinate system (meters).
  *
  * @param lons
  * @param lats
  * @return
  */
 private Array[] prj2meters(Array lons, Array lats) {
   Array[] out = new Array[2];
   assert Arrays.equals(lats.getShape(), lons.getShape());
   Index ltidx = Index.factory(lats.getShape());
   Index lnidx = Index.factory(lons.getShape());
   Array lats_prj = Array.factory(java.lang.Double.class, lats.getShape());
   Array lons_prj = Array.factory(java.lang.Double.class, lons.getShape());
   for (int i = 0; i < lats.getSize(); i++) {
     for (int j = 0; j < lons.getSize(); j++) {
       float lon_dd = lons.getFloat(lnidx.set(j));
       float lat_dd = lats.getFloat(ltidx.set(i));
       double[] prj = MatrixUtilities.lonlat2ceqd(new double[] {lon_dd, lat_dd});
       lons_prj.setDouble(lnidx.set(j), prj[0]);
       lats_prj.setDouble(ltidx.set(i), prj[1]);
     }
   }
   out[0] = lons_prj;
   out[1] = lats_prj;
   return out;
 }
Пример #20
0
  /**
   * Make height from the given data. <br>
   * height(x,y,z) = eta(x,y) + ( eta(x,y) + depth([n],x,y) ) * S(x,y,z)
   *
   * <p>where, S(x,y,z) = (depth_c*s(z) + (depth([n],x,y) * C(z)) / (depth_c + depth([n],x,y)) /
   *
   * @param eta eta Array
   * @param s s Array
   * @param depth depth Array
   * @param c c Array
   * @param depth_c value of depth_c
   * @return height data
   */
  private ArrayDouble.D3 makeHeight(Array eta, Array s, Array depth, Array c, double depth_c) {
    int nz = (int) s.getSize();
    Index sIndex = s.getIndex();
    Index cIndex = c.getIndex();

    int[] shape2D = eta.getShape();
    int ny = shape2D[0];
    int nx = shape2D[1];
    Index etaIndex = eta.getIndex();
    Index depthIndex = depth.getIndex();

    ArrayDouble.D3 height = new ArrayDouble.D3(nz, ny, nx);

    for (int z = 0; z < nz; z++) {
      double sz = s.getDouble(sIndex.set(z));
      double cz = c.getDouble(cIndex.set(z));

      double term1 = depth_c * sz;

      for (int y = 0; y < ny; y++) {
        for (int x = 0; x < nx; x++) {

          double fac1 = depth.getDouble(depthIndex.set(y, x));
          double term2 = fac1 * cz;

          double Sterm = (term1 + term2) / (depth_c + fac1);

          double term3 = eta.getDouble(etaIndex.set(y, x));
          double term4 = (term3 + fac1) * Sterm;
          double hterm = term3 + term4;

          height.set(z, y, x, hterm);
        }
      }
    }

    return height;
  }
Пример #21
0
 /**
  * Calculates change in the y direction
  *
  * @param arr - a 3x3 array of numbers
  * @return
  */
 private float dy(Array arr) {
   Index idx = Index.factory(arr.getShape());
   float e = arr.getFloat(idx.set(1, 1));
   if (Float.isNaN(e)) {
     return Float.NaN;
   }
   float a = arr.getFloat(idx.set(0, 0));
   float b = arr.getFloat(idx.set(0, 1));
   float c = arr.getFloat(idx.set(0, 2));
   float g = arr.getFloat(idx.set(2, 0));
   float h = arr.getFloat(idx.set(2, 1));
   float i = arr.getFloat(idx.set(2, 2));
   a = Float.isNaN(a) ? 0 : a;
   b = Float.isNaN(b) ? 0 : b;
   c = Float.isNaN(c) ? 0 : c;
   g = Float.isNaN(g) ? 0 : g;
   h = Float.isNaN(h) ? 0 : h;
   i = Float.isNaN(i) ? 0 : i;
   return ((g + 2 * h + i) - (a + 2 * b + c)) / 8;
 }
Пример #22
0
  public void testStride(String stride) throws IOException, InvalidRangeException {
    Variable time = ncfile.findVariable("time");
    ArrayInt all = (ArrayInt) time.read();

    ArrayInt correct = (ArrayInt) all.section(new Section(stride).getRanges());
    System.out.printf("correct(%s) %s", stride, NCdumpW.toString(correct));
    ArrayInt data = (ArrayInt) time.read(stride);
    System.out.printf("data(%s) %s%n", stride, NCdumpW.toString(data));
    Index ci = correct.getIndex();
    Index di = data.getIndex();
    for (int i = 0; i < data.getSize(); i++)
      assert (data.getInt(di.set(i)) == correct.getInt(ci.set(i)))
          : stride
              + " index "
              + i
              + " = "
              + data.getInt(di.set(i))
              + " != "
              + correct.getInt(ci.set(i));
  }
Пример #23
0
 /**
  * Calculates change in the x direction
  *
  * @param arr - a 3x3 array of numbers
  * @return
  */
 private float dx(Array arr) {
   Index idx = Index.factory(arr.getShape());
   float e = arr.getFloat(idx.set(1, 1));
   if (Float.isNaN(e)) {
     return Float.NaN;
   }
   float a = arr.getFloat(idx.set(0, 0));
   float c = arr.getFloat(idx.set(0, 2));
   float d = arr.getFloat(idx.set(1, 0));
   float f = arr.getFloat(idx.set(1, 2));
   float g = arr.getFloat(idx.set(2, 0));
   float i = arr.getFloat(idx.set(2, 2));
   a = Float.isNaN(a) ? 0 : a;
   c = Float.isNaN(c) ? 0 : c;
   d = Float.isNaN(d) ? 0 : d;
   f = Float.isNaN(f) ? 0 : f;
   g = Float.isNaN(g) ? 0 : g;
   i = Float.isNaN(i) ? 0 : i;
   return ((c + 2 * f + i) - (a + 2 * d + g)) / 8;
 }
 /**
  * @param index
  * @return
  */
 @Override
 public double getRT(int index) {
   Array a = this.c.getScanAcquisitionTime();
   Index idx = a.getIndex();
   return a.getDouble(idx.set(index));
 }
  /**
   * Fill all of the variables/attributes in the ncfile
   *
   * @param ncfile NetcdfFile object which will be filled.
   * @param bst number of seconds since midnight for start of sweep
   * @param yr year of start of each sweep
   * @param m month of start of each sweep
   * @param dda day of start of each sweep
   * @param varList ArrayList of Variables of ncfile
   * @param recHdr java.util.Map with values for Attributes
   */
  public void doNetcdfFileCoordinate(
      ucar.nc2.NetcdfFile ncfile,
      int[] bst,
      short[] yr,
      short[] m,
      short[] dda,
      ArrayList<Variable> varList,
      java.util.Map<String, Number> recHdr) {
    // prepare attribute values

    String[] unit = {" ", "dbZ", "dbZ", "m/sec", "m/sec", "dB"};
    String def_datafile = "SIGMET-IRIS";
    Short header_length = 80;
    Short ray_header_length = 6;
    int ngates = 0;

    float radar_lat =
        recHdr.get("radar_lat").floatValue(); // System.out.println("rad_lat="+radar_lat);
    float radar_lon =
        recHdr.get("radar_lon").floatValue(); // System.out.println("rad_lon="+radar_lon);
    short ground_height =
        recHdr.get("ground_height").shortValue(); // System.out.println("ground_H="+ground_height);
    short radar_height =
        recHdr.get("radar_height").shortValue(); // System.out.println("radar_H="+radar_height);
    int radar_alt =
        (recHdr.get("radar_alt").intValue()) / 100; // System.out.println("rad_alt="+radar_alt);
    short num_rays =
        recHdr.get("num_rays").shortValue(); // System.out.println("HERE!! num_rays="+num_rays);
    float range_first =
        (recHdr.get("range_first").intValue())
            * 0.01f; // System.out.println("range_1st="+range_first);
    float range_last =
        (recHdr.get("range_last").intValue()) * 0.01f; // System.out.println("step="+step);
    short number_sweeps = recHdr.get("number_sweeps").shortValue();
    int nparams = (recHdr.get("nparams").intValue()); // System.out.println("nparams="+nparams);
    // define date/time
    // int last_t=(int)(ray[nparams*number_sweeps-1][num_rays-1].getTime());
    int last_t = volScan.lastRay.getTime();
    String sss1 = Short.toString(m[0]);
    if (sss1.length() < 2) sss1 = "0" + sss1;
    String sss2 = Short.toString(dda[0]);
    if (sss2.length() < 2) sss2 = "0" + sss2;
    String base_date0 = String.valueOf(yr[0]) + "-" + sss1 + "-" + sss2;
    String sss11 = Short.toString(m[number_sweeps - 1]);
    if (sss11.length() < 2) sss11 = "0" + sss11;
    String sss22 = Short.toString(dda[number_sweeps - 1]);
    if (sss22.length() < 2) sss22 = "0" + sss22;
    String base_date1 = String.valueOf(yr[number_sweeps - 1]) + "-" + sss11 + "-" + sss22;
    String start_time = base_date0 + "T" + calcTime(bst[0], 0) + "Z";
    String end_time = base_date1 + "T" + calcTime(bst[number_sweeps - 1], last_t) + "Z";
    ncfile.addAttribute(null, new Attribute("time_coverage_start", start_time));
    ncfile.addAttribute(null, new Attribute("time_coverage_end", end_time));

    // set all of Variables
    try {
      int sz = varList.size();

      ArrayFloat.D2[] dataArr = new ArrayFloat.D2[nparams * number_sweeps];
      Index[] dataIndex = new Index[nparams * number_sweeps];

      Ray[] rtemp = new Ray[(int) num_rays];

      // NCdump.printArray(dataArr[0], "Total_Power", System.out, null);

      Variable[] distanceR = new Variable[number_sweeps];
      ArrayFloat.D1[] distArr = new ArrayFloat.D1[number_sweeps];
      Index[] distIndex = new Index[number_sweeps];
      String distName = "distanceR";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          distName = "distanceR_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(distName.trim())) {
            distanceR[i] = aVarList;
            break;
          }
        }
        distArr[i] = (ArrayFloat.D1) Array.factory(DataType.FLOAT, distanceR[i].getShape());
        distIndex[i] = distArr[i].getIndex();

        // for (int jj=0; jj<num_rays; jj++) { rtemp[jj]=ray[i][jj]; }
        ngates = sweep_bins[i];
        float stp = calcStep(range_first, range_last, (short) ngates);
        for (int ii = 0; ii < ngates; ii++) {
          distArr[i].setFloat(distIndex[i].set(ii), (range_first + ii * stp));
        }
      }
      // NCdump.printArray(distArr[0], "distanceR", System.out, null);
      List rgp = volScan.getTotalPowerGroups();
      if (rgp.size() == 0) rgp = volScan.getReflectivityGroups();
      List[] sgp = new ArrayList[number_sweeps];
      for (int i = 0; i < number_sweeps; i++) {
        sgp[i] = (List) rgp.get((short) i);
      }

      Variable[] time = new Variable[number_sweeps];
      ArrayInt.D1[] timeArr = new ArrayInt.D1[number_sweeps];
      Index[] timeIndex = new Index[number_sweeps];
      String t_n = "time";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          t_n = "time_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(t_n.trim())) {
            time[i] = aVarList;
            break;
          }
        }

        //                if (time[i].getShape().length == 0) {
        //                    continue;
        //                }
        timeArr[i] = (ArrayInt.D1) Array.factory(DataType.INT, time[i].getShape());
        timeIndex[i] = timeArr[i].getIndex();
        List rlist = sgp[i];

        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        for (int jj = 0; jj < num_rays; jj++) {
          timeArr[i].setInt(timeIndex[i].set(jj), rtemp[jj].getTime());
        }
      }

      // NCdump.printArray(timeArr[0], "time", System.out, null);

      Variable[] azimuthR = new Variable[number_sweeps];
      ArrayFloat.D1[] azimArr = new ArrayFloat.D1[number_sweeps];
      Index[] azimIndex = new Index[number_sweeps];
      String azimName = "azimuthR";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          azimName = "azimuthR_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(azimName.trim())) {
            azimuthR[i] = aVarList;
            break;
          }
        }
        azimArr[i] = (ArrayFloat.D1) Array.factory(DataType.FLOAT, azimuthR[i].getShape());
        azimIndex[i] = azimArr[i].getIndex();
        List rlist = sgp[i];

        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        for (int jj = 0; jj < num_rays; jj++) {
          azimArr[i].setFloat(azimIndex[i].set(jj), rtemp[jj].getAz());
        }
      }
      // NCdump.printArray(azimArr[0], "azimuthR", System.out, null);

      Variable[] elevationR = new Variable[number_sweeps];
      ArrayFloat.D1[] elevArr = new ArrayFloat.D1[number_sweeps];
      Index[] elevIndex = new Index[number_sweeps];
      String elevName = "elevationR";
      for (int i = 0; i < number_sweeps; i++) {
        if (number_sweeps > 1) {
          elevName = "elevationR_sweep_" + (i + 1);
        }
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals(elevName.trim())) {
            elevationR[i] = aVarList;
            break;
          }
        }
        elevArr[i] = (ArrayFloat.D1) Array.factory(DataType.FLOAT, elevationR[i].getShape());
        elevIndex[i] = elevArr[i].getIndex();
        List rlist = sgp[i];

        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        for (int jj = 0; jj < num_rays; jj++) {
          elevArr[i].setFloat(elevIndex[i].set(jj), rtemp[jj].getElev());
        }
      }
      // NCdump.printArray(elevArr[0], "elevationR", System.out, null);

      Variable numGates = null;
      for (int i = 0; i < number_sweeps; i++) {
        for (Variable aVarList : varList) {
          if ((aVarList.getShortName()).equals("numGates")) {
            numGates = aVarList;
            break;
          }
        }
      }
      ArrayInt.D1 gatesArr = (ArrayInt.D1) Array.factory(DataType.INT, numGates.getShape());
      Index gatesIndex = gatesArr.getIndex();

      for (int i = 0; i < number_sweeps; i++) {
        List rlist = sgp[i];
        for (int jj = 0; jj < num_rays; jj++) {
          rtemp[jj] = (Ray) rlist.get(jj);
        } // ray[i][jj]; }
        ngates = rtemp[0].getBins();
        gatesArr.setInt(gatesIndex.set(i), ngates);
      }

      for (int i = 0; i < number_sweeps; i++) {
        distanceR[i].setCachedData(distArr[i], false);
        time[i].setCachedData(timeArr[i], false);
        azimuthR[i].setCachedData(azimArr[i], false);
        elevationR[i].setCachedData(elevArr[i], false);
      }
      numGates.setCachedData(gatesArr, false);
      // startSweep.setCachedData(sweepArr, false);

      //          -------------------------------------------------
      // int b=(int)ray[0][0].getBins();

      // -- Test of readData() and readToByteChannel() -----------------
      /*
      Range r1=new Range(356, 359);
      Range r2=new Range(0, 15);
      java.util.List arlist=new ArrayList();
      arlist.add(r1);
      arlist.add(r2);
      Array testArr=readData(v[0], new Section(arlist));
      NCdump.printArray(testArr, "Total_Power_sweep_1", System.out, null);
      WritableByteChannel channel=new FileOutputStream(new File("C:\\netcdf\\tt.dat")).getChannel();
      long ikk=readToByteChannel(v[0], new Section(arlist), channel);
      System.out.println("IKK="+ikk);
      channel.close();
            */
      // ---------------------------------------------------

    } catch (Exception e) {
      System.out.println(e.toString());
      e.printStackTrace();
    }
  } // ----------- end of doNetcdf ----------------------------------
Пример #26
0
  public void testWritePermute() throws Exception {
    NetcdfFileWriteable ncfile = new NetcdfFileWriteable();
    ncfile.setName(TestLocal.cdmTestDataDir + "permuteTest.nc");

    // define dimensions
    Dimension xDim = ncfile.addDimension("x", 3);
    Dimension yDim = ncfile.addDimension("y", 5);
    Dimension zDim = ncfile.addDimension("z", 4);
    Dimension tDim = ncfile.addDimension("time", 2);

    // define Variables
    ncfile.addVariable("time", double.class, new Dimension[] {tDim});
    ncfile.addVariableAttribute("time", "units", "secs since 1-1-1 00:00");

    ncfile.addVariable("z", double.class, new Dimension[] {zDim});
    ncfile.addVariableAttribute("z", "units", "meters");
    ncfile.addVariableAttribute("z", "positive", "up");

    ncfile.addVariable("y", double.class, new Dimension[] {yDim});
    ncfile.addVariableAttribute("y", "units", "degrees_north");

    ncfile.addVariable("x", double.class, new Dimension[] {xDim});
    ncfile.addVariableAttribute("x", "units", "degrees_east");

    ncfile.addVariable("tzyx", double.class, new Dimension[] {tDim, zDim, yDim, xDim});
    ncfile.addVariableAttribute("tzyx", "units", "K");

    ncfile.addVariable("tzxy", double.class, new Dimension[] {tDim, zDim, xDim, yDim});
    ncfile.addVariableAttribute("tzxy", "units", "K");

    ncfile.addVariable("tyxz", double.class, new Dimension[] {tDim, yDim, xDim, zDim});
    ncfile.addVariableAttribute("tyxz", "units", "K");

    ncfile.addVariable("txyz", double.class, new Dimension[] {tDim, xDim, yDim, zDim});
    ncfile.addVariableAttribute("txyz", "units", "K");

    ncfile.addVariable("zyxt", double.class, new Dimension[] {zDim, yDim, xDim, tDim});
    ncfile.addVariableAttribute("zyxt", "units", "K");

    ncfile.addVariable("zxyt", double.class, new Dimension[] {zDim, xDim, yDim, tDim});
    ncfile.addVariableAttribute("zxyt", "units", "K");

    ncfile.addVariable("yxzt", double.class, new Dimension[] {yDim, xDim, zDim, tDim});
    ncfile.addVariableAttribute("yxzt", "units", "K");

    ncfile.addVariable("xyzt", double.class, new Dimension[] {xDim, yDim, zDim, tDim});
    ncfile.addVariableAttribute("xyzt", "units", "K");

    // missing one dimension
    ncfile.addVariable("zyx", double.class, new Dimension[] {zDim, yDim, xDim});
    ncfile.addVariable("txy", double.class, new Dimension[] {tDim, xDim, yDim});
    ncfile.addVariable("yxz", double.class, new Dimension[] {yDim, xDim, zDim});
    ncfile.addVariable("xzy", double.class, new Dimension[] {xDim, zDim, yDim});
    ncfile.addVariable("yxt", double.class, new Dimension[] {yDim, xDim, tDim});
    ncfile.addVariable("xyt", double.class, new Dimension[] {xDim, yDim, tDim});
    ncfile.addVariable("xyz", double.class, new Dimension[] {xDim, yDim, zDim});

    // missing two dimension
    ncfile.addVariable("yx", double.class, new Dimension[] {yDim, xDim});
    ncfile.addVariable("xy", double.class, new Dimension[] {xDim, yDim});
    ncfile.addVariable("yz", double.class, new Dimension[] {yDim, zDim});
    ncfile.addVariable("xz", double.class, new Dimension[] {xDim, zDim});
    ncfile.addVariable("yt", double.class, new Dimension[] {yDim, tDim});
    ncfile.addVariable("xt", double.class, new Dimension[] {xDim, tDim});
    ncfile.addVariable("ty", double.class, new Dimension[] {tDim, yDim});
    ncfile.addVariable("tx", double.class, new Dimension[] {tDim, xDim});

    // add global attributes
    ncfile.addGlobalAttribute("Convention", "COARDS");

    // create the file
    try {
      ncfile.create();
    } catch (IOException e) {
      System.err.println("ERROR creating file");
      assert (false);
    }

    // write time data
    int len = tDim.getLength();
    ArrayDouble A = new ArrayDouble.D1(len);
    Index ima = A.getIndex();
    for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3600));
    int[] origin = new int[1];
    try {
      ncfile.write("time", origin, A);
    } catch (IOException e) {
      System.err.println("ERROR writing time");
      assert (false);
    }

    // write z data
    len = zDim.getLength();
    A = new ArrayDouble.D1(len);
    ima = A.getIndex();
    for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 10));
    try {
      ncfile.write("z", origin, A);
    } catch (IOException e) {
      System.err.println("ERROR writing z");
      assert (false);
    }

    // write y data
    len = yDim.getLength();
    A = new ArrayDouble.D1(len);
    ima = A.getIndex();
    for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 3));
    try {
      ncfile.write("y", origin, A);
    } catch (IOException e) {
      System.err.println("ERROR writing y");
      assert (false);
    }

    // write x data
    len = xDim.getLength();
    A = new ArrayDouble.D1(len);
    ima = A.getIndex();
    for (int i = 0; i < len; i++) A.setDouble(ima.set(i), (double) (i * 5));
    try {
      ncfile.write("x", origin, A);
    } catch (IOException e) {
      System.err.println("ERROR writing x");
      assert (false);
    }

    // write tzyx data
    doWrite4(ncfile, "tzyx");
    doWrite4(ncfile, "tzxy");
    doWrite4(ncfile, "txyz");
    doWrite4(ncfile, "tyxz");
    doWrite4(ncfile, "zyxt");
    doWrite4(ncfile, "zxyt");
    doWrite4(ncfile, "xyzt");
    doWrite4(ncfile, "yxzt");

    doWrite3(ncfile, "zyx");
    doWrite3(ncfile, "txy");
    doWrite3(ncfile, "yxz");
    doWrite3(ncfile, "xzy");
    doWrite3(ncfile, "yxt");
    doWrite3(ncfile, "xyt");
    doWrite3(ncfile, "yxt");
    doWrite3(ncfile, "xyz");

    doWrite2(ncfile, "yx");
    doWrite2(ncfile, "xy");
    doWrite2(ncfile, "yz");
    doWrite2(ncfile, "xz");
    doWrite2(ncfile, "yt");
    doWrite2(ncfile, "xt");
    doWrite2(ncfile, "ty");
    doWrite2(ncfile, "tx");

    if (show) System.out.println("ncfile = " + ncfile);

    // all done
    try {
      ncfile.close();
    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert (false);
    }

    System.out.println("*****************Test Write done");
  }
Пример #27
0
  public void testNC3Read() throws IOException {
    NetcdfFile ncfile = TestDir.openFileLocal("testWrite.nc");

    assert (null != ncfile.findDimension("lat"));
    assert (null != ncfile.findDimension("lon"));

    Variable temp = null;
    assert (null != (temp = ncfile.findVariable("temperature")));

    // read entire array
    Array A;
    try {
      A = temp.read();
    } catch (IOException e) {
      System.err.println("ERROR reading file");
      assert (false);
      return;
    }
    assert (A.getRank() == 2);

    int i, j;
    Index ima = A.getIndex();
    int[] shape = A.getShape();
    assert shape[0] == 64;
    assert shape[1] == 128;

    for (i = 0; i < shape[0]; i++) {
      for (j = 0; j < shape[1]; j++) {
        double dval = A.getDouble(ima.set(i, j));
        assert (dval == (double) (i * 1000000 + j * 1000)) : dval;
      }
    }

    // read part of array
    int[] origin2 = new int[2];
    int[] shape2 = new int[2];
    shape2[0] = 1;
    shape2[1] = temp.getShape()[1];
    try {
      A = temp.read(origin2, shape2);
    } catch (InvalidRangeException e) {
      System.err.println("ERROR reading file " + e);
      assert (false);
      return;
    } catch (IOException e) {
      System.err.println("ERROR reading file");
      assert (false);
      return;
    }
    assert (A.getRank() == 2);

    for (j = 0; j < shape2[1]; j++) {
      assert (A.getDouble(ima.set(0, j)) == (double) (j * 1000));
    }

    // rank reduction
    Array Areduce = A.reduce();
    Index ima2 = Areduce.getIndex();
    assert (Areduce.getRank() == 1);

    for (j = 0; j < shape2[1]; j++) {
      assert (Areduce.getDouble(ima2.set(j)) == (double) (j * 1000));
    }

    // read char variable
    Variable c = null;
    assert (null != (c = ncfile.findVariable("svar")));
    try {
      A = c.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac = (ArrayChar) A;
    String val = ac.getString(ac.getIndex());
    assert val.equals("Testing 1-2-3") : val;
    // System.out.println( "val = "+ val);

    // read char variable 2
    Variable c2 = null;
    assert (null != (c2 = ncfile.findVariable("svar2")));
    try {
      A = c2.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac2 = (ArrayChar) A;
    assert (ac2.getString().equals("Two pairs of ladies stockings!"));

    // read String Array
    Variable c3 = null;
    assert (null != (c3 = ncfile.findVariable("names")));
    try {
      A = c3.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac3 = (ArrayChar) A;
    ima = ac3.getIndex();

    assert (ac3.getString(ima.set(0)).equals("No pairs of ladies stockings!"));
    assert (ac3.getString(ima.set(1)).equals("One pair of ladies stockings!"));
    assert (ac3.getString(ima.set(2)).equals("Two pairs of ladies stockings!"));

    // read String Array - 2
    Variable c4 = null;
    assert (null != (c4 = ncfile.findVariable("names2")));
    try {
      A = c4.read();
    } catch (IOException e) {
      assert (false);
    }
    assert (A instanceof ArrayChar);
    ArrayChar ac4 = (ArrayChar) A;
    ima = ac4.getIndex();

    assert (ac4.getString(0).equals("0 pairs of ladies stockings!"));
    assert (ac4.getString(1).equals("1 pair of ladies stockings!"));
    assert (ac4.getString(2).equals("2 pairs of ladies stockings!"));

    // System.out.println( "ncfile = "+ ncfile);
    ncfile.close();
    System.out.println("**************TestRead done");
  }
Пример #28
0
  public void testWriteStandardVar() throws Exception {
    NetcdfFileWriteable ncfile = new NetcdfFileWriteable(filename, false);

    // define dimensions
    Dimension latDim = ncfile.addDimension("lat", 2);
    Dimension lonDim = ncfile.addDimension("lon", 3);

    ArrayList dims = new ArrayList();
    dims.add(latDim);
    dims.add(lonDim);

    // case 1
    ncfile.addVariable("t1", DataType.DOUBLE, dims);
    ncfile.addVariableAttribute("t1", CDM.SCALE_FACTOR, new Double(2.0));
    ncfile.addVariableAttribute("t1", "add_offset", new Double(77.0));

    // case 2
    ncfile.addVariable("t2", DataType.BYTE, dims);
    ncfile.addVariableAttribute("t2", CDM.SCALE_FACTOR, new Short((short) 2));
    ncfile.addVariableAttribute("t2", "add_offset", new Short((short) 77));

    // case 3
    ncfile.addVariable("t3", DataType.BYTE, dims);
    ncfile.addVariableAttribute("t3", "_FillValue", new Byte((byte) 255));

    // case 4
    ncfile.addVariable("t4", DataType.SHORT, dims);
    ncfile.addVariableAttribute("t4", CDM.MISSING_VALUE, new Short((short) -9999));

    // case 5
    ncfile.addVariable("t5", DataType.SHORT, dims);
    ncfile.addVariableAttribute("t5", CDM.MISSING_VALUE, new Short((short) -9999));
    ncfile.addVariableAttribute("t5", CDM.SCALE_FACTOR, new Short((short) 2));
    ncfile.addVariableAttribute("t5", "add_offset", new Short((short) 77));

    // case 1
    ncfile.addVariable("m1", DataType.DOUBLE, dims);
    ncfile.addVariableAttribute("m1", CDM.MISSING_VALUE, -999.99);

    // create the file
    ncfile.create();

    // write t1
    ArrayDouble A = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength());
    int i, j;
    Index ima = A.getIndex();
    // write
    for (i = 0; i < latDim.getLength(); i++)
      for (j = 0; j < lonDim.getLength(); j++) A.setDouble(ima.set(i, j), (double) (i * 10.0 + j));
    int[] origin = new int[2];
    ncfile.write("t1", origin, A);

    // write t2
    ArrayByte Ab = new ArrayByte.D2(latDim.getLength(), lonDim.getLength());
    ima = Ab.getIndex();
    for (i = 0; i < latDim.getLength(); i++)
      for (j = 0; j < lonDim.getLength(); j++) Ab.setByte(ima.set(i, j), (byte) (i * 10 + j));
    ncfile.write("t2", origin, Ab);

    // write t3
    ncfile.write("t3", origin, Ab);

    // write t4
    Array As = new ArrayShort.D2(latDim.getLength(), lonDim.getLength());
    ima = As.getIndex();
    for (i = 0; i < latDim.getLength(); i++)
      for (j = 0; j < lonDim.getLength(); j++) As.setShort(ima.set(i, j), (short) (i * 10 + j));
    ncfile.write("t4", origin, As);

    As.setShort(ima.set(0, 0), (short) -9999);
    ncfile.write("t5", origin, As);

    // write m1
    ArrayDouble.D2 Ad = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength());
    for (i = 0; i < latDim.getLength(); i++)
      for (j = 0; j < lonDim.getLength(); j++) Ad.setDouble(ima.set(i, j), (double) (i * 10.0 + j));
    Ad.set(1, 1, -999.99);
    ncfile.write("m1", new int[2], Ad);

    // all done
    ncfile.close();

    System.out.println("**************TestStandardVar Write done");
  }
Пример #29
0
  /**
   * Add this coord as a variable in the netCDF file
   *
   * @param ncfile netCDF file to add to
   * @param g group in file
   */
  void addToNetcdfFile(NetcdfFile ncfile, Group g) {
    if (dontUseVertical) {
      typicalRecord = null;
      return;
    }

    if (g == null) {
      g = ncfile.getRootGroup();
    }

    // coordinate axis
    Variable v = new Variable(ncfile, g, null, getVariableName());
    v.setDataType(DataType.DOUBLE);

    String desc = lookup.getLevelDescription(typicalRecord);
    if (lookup instanceof Grib2GridTableLookup && usesBounds) {
      desc = "Layer between " + desc;
    }

    v.addAttribute(new Attribute("long_name", desc));
    v.addAttribute(new Attribute("units", lookup.getLevelUnit(typicalRecord)));

    // positive attribute needed for CF-1 Height and Pressure
    if (positive != null) {
      v.addAttribute(new Attribute("positive", positive));
    }

    if (units != null) {
      AxisType axisType;
      if (SimpleUnit.isCompatible("millibar", units)) {
        axisType = AxisType.Pressure;
      } else if (SimpleUnit.isCompatible("m", units)) {
        axisType = AxisType.Height;
      } else {
        axisType = AxisType.GeoZ;
      }

      if (lookup instanceof Grib2GridTableLookup || lookup instanceof Grib1GridTableLookup) {
        v.addAttribute(
            new Attribute("GRIB_level_type", Integer.toString(typicalRecord.getLevelType1())));
      } else {
        v.addAttribute(
            new Attribute("level_type", Integer.toString(typicalRecord.getLevelType1())));
      }
      v.addAttribute(new Attribute(_Coordinate.AxisType, axisType.toString()));
    }

    if (coordValues == null) {
      coordValues = new double[levels.size()];
      for (int i = 0; i < levels.size(); i++) {
        LevelCoord lc = (LevelCoord) levels.get(i);
        coordValues[i] = lc.mid;
      }
    }
    Array dataArray = Array.factory(DataType.DOUBLE, new int[] {coordValues.length}, coordValues);

    v.setDimensions(getVariableName());
    v.setCachedData(dataArray, true);

    ncfile.addVariable(g, v);

    if (usesBounds) {
      String boundsDimName = "bounds_dim";
      if (g.findDimension(boundsDimName) == null) {
        ncfile.addDimension(g, new Dimension(boundsDimName, 2, true));
      }

      String bname = getVariableName() + "_bounds";
      v.addAttribute(new Attribute("bounds", bname));
      v.addAttribute(new Attribute(_Coordinate.ZisLayer, "true"));

      Variable b = new Variable(ncfile, g, null, bname);
      b.setDataType(DataType.DOUBLE);
      b.setDimensions(getVariableName() + " " + boundsDimName);
      b.addAttribute(new Attribute("long_name", "bounds for " + v.getName()));
      b.addAttribute(new Attribute("units", lookup.getLevelUnit(typicalRecord)));

      Array boundsArray = Array.factory(DataType.DOUBLE, new int[] {coordValues.length, 2});
      ucar.ma2.Index ima = boundsArray.getIndex();
      for (int i = 0; i < coordValues.length; i++) {
        LevelCoord lc = (LevelCoord) levels.get(i);
        boundsArray.setDouble(ima.set(i, 0), lc.value1);
        boundsArray.setDouble(ima.set(i, 1), lc.value2);
      }
      b.setCachedData(boundsArray, true);

      ncfile.addVariable(g, b);
    }

    if (factors != null) {
      // check if already created
      if (g == null) {
        g = ncfile.getRootGroup();
      }
      if (g.findVariable("hybrida") != null) return;
      v.addAttribute(new Attribute("standard_name", "atmosphere_hybrid_sigma_pressure_coordinate"));
      v.addAttribute(new Attribute("formula_terms", "ap: hybrida b: hybridb ps: Pressure"));
      // create  hybrid factor variables
      // add hybrida variable
      Variable ha = new Variable(ncfile, g, null, "hybrida");
      ha.setDataType(DataType.DOUBLE);
      ha.addAttribute(new Attribute("long_name", "level_a_factor"));
      ha.addAttribute(new Attribute("units", ""));
      ha.setDimensions(getVariableName());
      // add data
      int middle = factors.length / 2;
      double[] adata;
      double[] bdata;
      if (levels.size() < middle) { // only partial data wanted
        adata = new double[levels.size()];
        bdata = new double[levels.size()];
      } else {
        adata = new double[middle];
        bdata = new double[middle];
      }
      for (int i = 0; i < middle && i < levels.size(); i++) adata[i] = factors[i];
      Array haArray = Array.factory(DataType.DOUBLE, new int[] {adata.length}, adata);
      ha.setCachedData(haArray, true);
      ncfile.addVariable(g, ha);

      // add hybridb variable
      Variable hb = new Variable(ncfile, g, null, "hybridb");
      hb.setDataType(DataType.DOUBLE);
      hb.addAttribute(new Attribute("long_name", "level_b_factor"));
      hb.addAttribute(new Attribute("units", ""));
      hb.setDimensions(getVariableName());
      // add data
      for (int i = 0; i < middle && i < levels.size(); i++) bdata[i] = factors[i + middle];
      Array hbArray = Array.factory(DataType.DOUBLE, new int[] {bdata.length}, bdata);
      hb.setCachedData(hbArray, true);
      ncfile.addVariable(g, hb);

      /*  // TODO: delete next time modifying code
      double[] adata = new double[ middle ];
      for( int i = 0; i < middle; i++ )
        adata[ i ] = factors[ i ];
      Array haArray = Array.factory(DataType.DOUBLE, new int[]{adata.length}, adata);
      ha.setCachedData(haArray, true);
      ncfile.addVariable(g, ha);

      // add hybridb variable
      Variable hb = new Variable(ncfile, g, null, "hybridb");
      hb.setDataType(DataType.DOUBLE);
      hb.addAttribute(new Attribute("long_name",  "level_b_factor" ));
      //hb.addAttribute(new Attribute("standard_name", "atmosphere_hybrid_sigma_pressure_coordinate" ));
      hb.addAttribute(new Attribute("units", ""));
      hb.setDimensions(getVariableName());
      // add data
      double[] bdata = new double[ middle ];
      for( int i = 0; i < middle; i++ )
        bdata[ i ] = factors[ i + middle ];
      Array hbArray = Array.factory(DataType.DOUBLE, new int[]{bdata.length}, bdata);
      hb.setCachedData(hbArray, true);
      ncfile.addVariable(g, hb);
      */
    }
  }
Пример #30
0
  public void readShort2FloatMissing() throws Exception {
    Variable v = null;
    assert (null != (v = ncfileRead.findVariable("t5")));
    assert (v.getDataType() == DataType.SHORT);

    // standard convert with missing data
    assert (null != (v = dsRead.findVariable("t5")));
    assert v instanceof VariableEnhanced;
    assert v instanceof VariableDS;
    VariableDS vs = (VariableDS) v;
    assert (vs.getDataType() == DataType.FLOAT);

    assert (vs.hasMissing());
    assert (vs.hasMissingValue());
    double mv = 2 * (-9999) + 77;
    assert (vs.isMissing((double) mv));
    assert (vs.isMissingValue((double) mv));

    Array A = vs.read();
    Index ima = A.getIndex();
    int[] shape = A.getShape();
    int i, j;

    assert (vs.isMissing(A.getFloat(ima.set(0, 0))));

    for (i = 0; i < shape[0]; i++) {
      for (j = 1; j < shape[1]; j++) {
        float val = A.getFloat(ima.set(i, j));
        float want = 2 * (i * 10 + j) + 77;
        if (val != want) System.out.println(i + " " + j + " " + val + " " + want);
        assert (val == want);
      }
    }

    // useNaNs
    vs.setUseNaNs(true);
    assert (vs.getDataType() == DataType.FLOAT);

    assert (vs.hasMissing());
    assert (vs.hasMissingValue());
    double mv2 = 2 * (-9999) + 77;
    assert (vs.isMissing((double) mv2));
    assert (vs.isMissingValue((double) mv2));

    Array A2 = vs.read();
    Index ima2 = A2.getIndex();
    int[] shape2 = A2.getShape();

    double mval = A2.getFloat(ima2.set(0, 0));
    assert vs.isMissing(mval);
    assert Double.isNaN(mval);

    for (i = 0; i < shape2[0]; i++) {
      for (j = 1; j < shape2[1]; j++) {
        float val = A2.getFloat(ima2.set(i, j));
        float want = 2 * (i * 10 + j) + 77;
        if (val != want) System.out.println(i + " " + j + " " + val + " " + want);
        assert (val == want) : val + " != " + want;
      }
    }

    assert (null == vs.findAttribute(CDM.SCALE_FACTOR));
    assert (null == vs.findAttribute("add_offset"));
    assert (null == vs.findAttribute(CDM.MISSING_VALUE));

    System.out.println("**************TestStandardVar Read readShort2FloatMissing");
  }