示例#1
1
    // create from a dataset
    public VariableBean(Variable vs) {
      this.vs = vs;
      // vs = (v instanceof VariableEnhanced) ? (VariableEnhanced) v : new VariableStandardized( v);

      setName(vs.getShortName());
      setDescription(vs.getDescription());
      setUnits(vs.getUnitsString());
      setDataType(vs.getDataType().toString());

      // Attribute csAtt = vs.findAttribute("_coordSystems");
      // if (csAtt != null)
      //  setCoordSys( csAtt.getStringValue());

      // collect dimensions
      StringBuilder lens = new StringBuilder();
      StringBuilder names = new StringBuilder();
      java.util.List dims = vs.getDimensions();
      for (int j = 0; j < dims.size(); j++) {
        ucar.nc2.Dimension dim = (ucar.nc2.Dimension) dims.get(j);
        if (j > 0) {
          lens.append(",");
          names.append(",");
        }
        String name = dim.isShared() ? dim.getName() : "anon";
        names.append(name);
        lens.append(dim.getLength());
      }
      setDimensions(names.toString());
      setShape(lens.toString());
    }
示例#2
0
  public void addBandMetadata(Product product) throws ProductIOException {
    Group group = ncFile.findGroup("Geophysical_Data");
    if (productReader.getProductType() == SeadasProductReader.ProductType.Level2_Aquarius) {
      group = ncFile.findGroup("Aquarius_Data");
    }
    if (productReader.getProductType() == SeadasProductReader.ProductType.Level1B_HICO) {
      group = ncFile.findGroup("products");
    }
    if (group != null) {
      final MetadataElement bandAttributes = new MetadataElement("Band_Attributes");
      List<Variable> variables = group.getVariables();
      for (Variable variable : variables) {
        final String name = variable.getShortName();
        final MetadataElement sdsElement = new MetadataElement(name + ".attributes");
        final int dataType = getProductDataType(variable);
        final MetadataAttribute prodtypeattr = new MetadataAttribute("data_type", dataType);

        sdsElement.addAttribute(prodtypeattr);
        bandAttributes.addElement(sdsElement);

        final List<Attribute> list = variable.getAttributes();
        for (Attribute varAttribute : list) {
          addAttributeToElement(sdsElement, varAttribute);
        }
      }
      final MetadataElement metadataRoot = product.getMetadataRoot();
      metadataRoot.addElement(bandAttributes);
    }
  }
  /**
   * Clones the attributes of one NetCDF file into another.
   *
   * @param infile - The source NetCDF file
   * @param inVarName - The variable name to be copied
   * @param outfile - The destination NetCDF file
   * @param outVarName - The output variable name
   */
  private void cloneAttributes(
      NetcdfFile infile, String inVarName, NetcdfFileWriteable outfile, String outVarName) {

    // Find the variable

    Variable vi = infile.findVariable(inVarName);

    // Grab all of its attributes - unchecked, but should be OK.

    List<Attribute> l = vi.getAttributes();
    for (Attribute a : l) {
      if (a.getName().equalsIgnoreCase("units") && inVarName.equalsIgnoreCase("time")) {
        Attribute aa = new Attribute("units", "days since 1900-12-31 00:00:00");
        outfile.addVariableAttribute(outVarName, aa);
      } else if (a.getName().equalsIgnoreCase("time_origin")
          && inVarName.equalsIgnoreCase("time")) {
        Attribute aa = new Attribute("time_origin", "1900-12-31 00:00:00");
        outfile.addVariableAttribute(outVarName, aa);
      } else if (a.getName().equalsIgnoreCase("calendar")) {
        Attribute aa = new Attribute("calendar", "standard");
        outfile.addVariableAttribute(outVarName, aa);
      } else {
        outfile.addVariableAttribute(outVarName, a);
      }
    }
  }
  /* Structure {
      int LAT[0];
      ...
      int LAT[149];
  } IMAGE_LAT_ARRAY(3600);
     type = Layout(8);  type= 2 (chunked) storageSize = (1,600) dataSize=0 dataAddress=2548046
  */
  @Test
  public void testReadOneAtATime() throws java.io.IOException, InvalidRangeException {
    try (NetcdfFile ncfile = TestH5.openH5("IASI/IASI.h5")) {

      Variable dset = ncfile.findVariable("U-MARF/EPS/IASI_xxx_1C/DATA/IMAGE_LAT_ARRAY");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 3600);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 3600);

      Structure s = (Structure) dset;

      // read last one - chunked
      StructureData sd = s.readStructure(3599);
      assert sd.getScalarInt("LAT[0]") == 70862722;
      assert sd.getScalarInt("LAT[149]") == 85302263;

      // read one at a time
      for (int i = 3590; i < d.getLength(); i++) {
        s.readStructure(i);
        System.out.println(" read structure " + i);
      }
    }
    System.out.println("*** testReadIASI ok");
  }
  /*
   Structure {
    int a_name;
    byte b_name(3);
    byte c_name(3);
    short d_name(3);
    int e_name(3);
    long f_name(3);
    int g_name(3);
    short h_name(3);
    int i_name(3);
    long j_name(3);
    float k_name(3);
    double l_name(3);
  } CompoundNative(15);
      type = Layout(8);  type= 1 (contiguous) storageSize = (15,144) dataSize=0 dataAddress=2048
  */
  @Test
  public void testReadH5StructureArrayMembers() throws java.io.IOException {
    try (NetcdfFile ncfile = TestH5.openH5("complex/compound_native.h5")) {

      Variable dset = ncfile.findVariable("CompoundNative");
      assert (null != dset);
      assert (dset.getDataType() == DataType.STRUCTURE);
      assert (dset.getRank() == 1);
      assert (dset.getSize() == 15);

      Dimension d = dset.getDimension(0);
      assert (d.getLength() == 15);

      Structure s = (Structure) dset;

      // read all with the iterator
      StructureDataIterator iter = s.getStructureIterator();
      while (iter.hasNext()) {
        StructureData sd = iter.next();

        for (StructureMembers.Member m : sd.getMembers()) {
          Array data = sd.getArray(m);
          NCdumpW.printArray(data, m.getName(), out, null);
        }
      }
    }
    System.out.println("*** testReadH5StructureArrayMembers ok");
  }
示例#6
0
  private void handleMetadataGroup(Group group, MetadataElement metadataElement)
      throws ProductIOException {
    List<Variable> variables = group.getVariables();
    for (Variable variable : variables) {
      final String name = variable.getShortName();
      final int dataType = getProductDataType(variable);
      Array array;
      try {
        array = variable.read();
      } catch (IOException e) {
        throw new ProductIOException(e.getMessage());
      }
      final ProductData data = ProductData.createInstance(dataType, array.getStorage());
      final MetadataAttribute attribute = new MetadataAttribute("data", data, true);

      final MetadataElement sdsElement = new MetadataElement(name);
      sdsElement.addAttribute(attribute);
      metadataElement.addElement(sdsElement);

      final List<Attribute> list = variable.getAttributes();
      for (Attribute hdfAttribute : list) {
        final String attribName = hdfAttribute.getShortName();
        if ("units".equals(attribName)) {
          attribute.setUnit(hdfAttribute.getStringValue());
        } else if ("long_name".equals(attribName)) {
          attribute.setDescription(hdfAttribute.getStringValue());
        } else {
          addAttributeToElement(sdsElement, hdfAttribute);
        }
      }
    }
  }
  public Float10TrajectoryObsDataset(NetcdfDataset ncd) throws IOException {
    super(ncd);

    // Get the names of the two coordinate variables and dimensions
    // and grab the variables and dimensions themselves.
    trajDimName = trajDimNameDefault;
    trajVarName = trajVarNameDefault;
    timeDimName = timeDimNameDefault;
    timeVarName = timeVarNameDefault;

    latVarName = latVarNameDefault;
    lonVarName = lonVarNameDefault;
    elevVarName = elevVarNameDefault;

    Variable latVar = ncd.getRootGroup().findVariable(latVarName);
    latVar.addAttribute(new Attribute("units", "degrees_north"));

    Variable lonVar = ncd.getRootGroup().findVariable(lonVarName);
    lonVar.addAttribute(new Attribute("units", "degrees_east"));

    this.setTrajectoryInfo(
        netcdfDataset.getRootGroup().findDimension(trajDimName),
        netcdfDataset.getRootGroup().findVariable(trajVarName),
        netcdfDataset.getRootGroup().findDimension(timeDimName),
        netcdfDataset.getRootGroup().findVariable(timeVarName),
        netcdfDataset.getRootGroup().findVariable(latVarName),
        netcdfDataset.getRootGroup().findVariable(lonVarName),
        netcdfDataset.getRootGroup().findVariable(elevVarName));
  }
  /**
   * Wrap the given Variable, making it into a VariableDS. Delegate data reading to the original
   * variable. Take all metadata from original variable. Does not share cache, iosp.
   *
   * @param g logical container, if null use orgVar's group
   * @param orgVar the original Variable to wrap. The original Variable is not modified. Must not be
   *     a Structure, use StructureDS instead.
   * @param enhance if true, use NetcdfDataset.defaultEnhanceMode to define what enhancements are
   *     made. Note that this can change DataType and data values. You can also call enhance()
   *     later. If orgVar is VariableDS, then enhance is inherited from there, and this parameter is
   *     ignored.
   */
  public VariableDS(Group g, Variable orgVar, boolean enhance) {
    super(orgVar);
    if (g != null)
      this.group =
          g; // otherwise super() sets group; this affects the long name and the dimensions.
    setDimensions(getDimensionsString()); // reset the dimensions

    if (orgVar instanceof Structure)
      throw new IllegalArgumentException(
          "VariableDS must not wrap a Structure; name=" + orgVar.getName());

    // dont share cache, iosp : all IO is delegated
    this.ncfile = null;
    this.spiObject = null;
    createNewCache();

    this.orgVar = orgVar;
    this.orgDataType = orgVar.getDataType();

    if (orgVar instanceof VariableDS) {
      VariableDS ncVarDS = (VariableDS) orgVar;
      this.enhanceProxy = ncVarDS.enhanceProxy;
      this.scaleMissingProxy = ncVarDS.scaleMissingProxy;
      this.enhanceMode = ncVarDS.enhanceMode;

    } else {
      this.enhanceProxy = new EnhancementsImpl(this);
      if (enhance) {
        enhance(NetcdfDataset.getDefaultEnhanceMode());
      } else {
        this.scaleMissingProxy = new EnhanceScaleMissingImpl();
      }
    }
  }
示例#9
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");
  }
示例#10
0
  private void doVariable(Variable v, opendap.dap.AttributeTable parentTable) {

    List dims = v.getDimensions();
    for (int i = 0; i < dims.size(); i++) {
      Dimension dim = (Dimension) dims.get(i);
      if (dim.isShared()) usedDims.put(dim.getName(), dim);
    }

    // if (v.getAttributes().size() == 0) return; // LOOK DAP 2 say must have empty

    String name = NcDDS.escapeName(v.getShortName());
    opendap.dap.AttributeTable table;

    if (parentTable == null) {
      table = new opendap.dap.AttributeTable(name);
      try {
        addAttributeTable(name, table);
      } catch (AttributeExistsException e) {
        log.error("Cant add " + name, e);
      }
    } else {
      table = parentTable.appendContainer(name);
    }

    addAttributes(table, v, v.getAttributes().iterator());

    if (v instanceof Structure) {
      Structure s = (Structure) v;
      List nested = s.getVariables();
      for (int i = 0; i < nested.size(); i++) {
        Variable nv = (Variable) nested.get(i);
        doVariable(nv, table);
      }
    }
  }
示例#11
0
  public void addInputParamMetadata(Product product) throws ProductIOException {

    Variable inputParams = ncFile.findVariable("Input_Parameters");
    if (inputParams != null) {
      final MetadataElement inputParamsMeta = new MetadataElement("Input_Parameters");
      Array array;
      try {
        array = inputParams.read();
      } catch (IOException e) {
        throw new ProductIOException(e.getMessage());
      }

      String[] lines = array.toString().split("\n");
      for (String line : lines) {
        String[] parts = line.split("=");
        if (parts.length == 2) {
          final String name = parts[0].trim();
          final String value = parts[1].trim();
          final ProductData data = ProductData.createInstance(ProductData.TYPE_ASCII, value);
          final MetadataAttribute attribute = new MetadataAttribute(name, data, true);
          inputParamsMeta.addAttribute(attribute);
        }
      }

      final MetadataElement metadataRoot = product.getMetadataRoot();
      metadataRoot.addElement(inputParamsMeta);
    }
  }
示例#12
0
  private static String getStructMetadata(Group eosGroup) throws IOException {
    StringBuilder sbuff = null;
    String structMetadata = null;

    int n = 0;
    while (true) {
      Variable structMetadataVar = eosGroup.findVariable("StructMetadata." + n);
      if (structMetadataVar == null) break;
      if ((structMetadata != null) && (sbuff == null)) { // more than 1 StructMetadata
        sbuff = new StringBuilder(64000);
        sbuff.append(structMetadata);
      }

      // read and parse the ODL
      Array A = structMetadataVar.read();
      if (A instanceof ArrayChar.D1) {
        ArrayChar ca = (ArrayChar) A;
        structMetadata = ca.getString(); // common case only StructMetadata.0, avoid extra copy
      } else if (A instanceof ArrayObject.D0) {
        ArrayObject ao = (ArrayObject) A;
        structMetadata = (String) ao.getObject(0);
      } else {
        log.error("Unsupported array type {} for StructMetadata", A.getElementType());
      }

      if (sbuff != null) sbuff.append(structMetadata);
      n++;
    }
    return (sbuff != null) ? sbuff.toString() : structMetadata;
  }
示例#13
0
  /**
   * Read the data for each variable passed in
   *
   * @param v2
   * @param section
   * @return output data
   * @throws IOException
   * @throws ucar.ma2.InvalidRangeException
   */
  public Array readData(Variable v2, Section section) throws IOException, InvalidRangeException {

    // subset
    Object data;
    Array outputData;
    byte[] vdata = null;
    NOWRadheader.Vinfo vinfo;
    ByteBuffer bos;
    List<Range> ranges = section.getRanges();

    vinfo = (NOWRadheader.Vinfo) v2.getSPobject();

    try {
      vdata = headerParser.getData((int) vinfo.hoff);
    } catch (Exception e) {
    }

    bos = ByteBuffer.wrap(vdata);
    data = readOneScanData(bos, vinfo, v2.getShortName());
    outputData = Array.factory(v2.getDataType().getPrimitiveClassType(), v2.getShape(), data);
    outputData = outputData.flip(1);

    // outputData = outputData.flip(2);
    return (outputData.sectionNoReduce(ranges).copy());

    // return outputData;
  }
示例#14
0
  private void showDeclaration(BeanTableSorted from, boolean isNcml) {
    Variable v = getCurrentVariable(from);
    if (v == null) return;
    infoTA.clear();
    if (isNcml) {
      Formatter out = new Formatter();
      try {
        NCdumpW.writeNcMLVariable(v, out);
      } catch (IOException e) {
        e.printStackTrace();
      }
      infoTA.appendLine(out.toString());

    } else {
      infoTA.appendLine(v.toString());
    }

    if (Debug.isSet("Xdeveloper")) {
      infoTA.appendLine("\n");
      infoTA.appendLine("FULL NAME = " + v.getFullName());
      infoTA.appendLine("\n");
      infoTA.appendLine(v.toStringDebug());
    }
    infoTA.gotoTop();
    infoWindow.setTitle("Variable Info");
    infoWindow.show();
  }
  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;
  }
示例#16
0
 private void checkIfAxis(Variable v) {
   String name = v.getShortName();
   if (name.equalsIgnoreCase("Longitude"))
     v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lon.toString()));
   else if (name.equalsIgnoreCase("Latitude"))
     v.addAttribute(new Attribute(_Coordinate.AxisType, AxisType.Lat.toString()));
 }
示例#17
0
  private void copySome(NetcdfFileWriteable ncfile, Variable oldVar, int nelems)
      throws IOException {
    String newName = N3iosp.makeValidNetcdfObjectName(oldVar.getShortName());

    int[] shape = oldVar.getShape();
    int[] origin = new int[oldVar.getRank()];
    int size = shape[0];

    for (int i = 0; i < size; i += nelems) {
      origin[0] = i;
      int left = size - i;
      shape[0] = Math.min(nelems, left);

      Array data;
      try {
        data = oldVar.read(origin, shape);
        if (oldVar.getDataType() == DataType.STRING) {
          data = convertToChar(ncfile.findVariable(newName), data);
        }
        if (data.getSize() > 0) { // zero when record dimension = 0
          ncfile.write(newName, origin, data);
          if (debug) System.out.println("write " + data.getSize() + " bytes");
        }

      } catch (InvalidRangeException e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
      }
    }
  }
示例#18
0
 public static String getRasterName(Variable variable) {
   Attribute attribute = variable.findAttribute(Constants.ORIG_NAME_ATT_NAME);
   if (attribute != null) {
     return attribute.getStringValue();
   } else {
     return variable.getName();
   }
 }
示例#19
0
 /**
  * Make the missing variable
  *
  * @return the missing variable
  */
 protected Variable makeMissingVariable() {
   Variable var = new Variable(ncfile, null, null, MISSING_VAR);
   var.setDataType(DataType.BYTE);
   var.setDimensions((List<Dimension>) null);
   var.addAttribute(new Attribute("description", "missing flag - 1 means all params are missing"));
   var.addAttribute(new Attribute("missing_value", new Byte((byte) 1)));
   return var;
 }
示例#20
0
 private float testReadScalar(Variable v) throws IOException {
   if (show) System.out.printf(" read %s%n", v.getNameAndDimensions());
   assert (null != v);
   Array a = v.read();
   assert (null != a);
   IndexIterator ii = a.getIndexIterator();
   return ii.getFloatNext();
 }
示例#21
0
  public boolean compareVariables(
      Variable org, Variable copy, boolean compareData, boolean justOne) {
    boolean ok = true;

    if (showCompare)
      f.format("compare Variable %s to %s %n", org.getFullName(), copy.getFullName());
    if (!org.getFullName().equals(copy.getFullName())) {
      f.format(" ** names are different %s != %s %n", org.getFullName(), copy.getFullName());
      ok = false;
    }

    // dimensions
    ok &= checkAll(org.getDimensions(), copy.getDimensions(), null);

    // attributes
    ok &= checkAll(org.getAttributes(), copy.getAttributes(), null);

    // coord sys
    if ((org instanceof VariableEnhanced) && (copy instanceof VariableEnhanced)) {
      VariableEnhanced orge = (VariableEnhanced) org;
      VariableEnhanced copye = (VariableEnhanced) copy;
      ok &= checkAll(orge.getCoordinateSystems(), copye.getCoordinateSystems(), null);
    }

    // data !!
    if (compareData) {
      try {
        compareVariableData(org, copy, showCompare, justOne);

      } catch (IOException e) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(10000);
        e.printStackTrace(new PrintStream(bos));
        f.format("%s", bos.toString());
      }
    }

    // nested variables
    if (org instanceof Structure) {
      if (!(copy instanceof Structure)) {
        f.format("  ** %s not Structure%n", org);
        ok = false;

      } else {
        Structure orgS = (Structure) org;
        Structure ncmlS = (Structure) copy;

        List vars = new ArrayList();
        ok &= checkAll(orgS.getVariables(), ncmlS.getVariables(), vars);
        for (int i = 0; i < vars.size(); i += 2) {
          Variable orgV = (Variable) vars.get(i);
          Variable ncmlV = (Variable) vars.get(i + 1);
          ok &= compareVariables(orgV, ncmlV, false, true);
        }
      }
    }

    return ok;
  }
示例#22
0
  // convert to shared dimensions
  private void setSharedDimensions(
      Variable v, List<Element> values, List<Dimension> unknownDims, String location) {
    if (values.size() == 0) return;

    // remove the "scalar" dumbension
    Iterator<Element> iter = values.iterator();
    while (iter.hasNext()) {
      Element value = iter.next();
      String dimName = value.getText().trim();
      if (dimName.equalsIgnoreCase("scalar")) iter.remove();
    }

    // gotta have same number of dimensions
    List<Dimension> oldDims = v.getDimensions();
    if (oldDims.size() != values.size()) {
      log.error("Different number of dimensions for {} {}", v, location);
      return;
    }

    List<Dimension> newDims = new ArrayList<>();
    Group group = v.getParentGroup();

    for (int i = 0; i < values.size(); i++) {
      Element value = values.get(i);
      String dimName = value.getText().trim();
      dimName = NetcdfFile.makeValidCdmObjectName(dimName);

      Dimension dim = group.findDimension(dimName);
      Dimension oldDim = oldDims.get(i);
      if (dim == null) dim = checkUnknownDims(dimName, unknownDims, oldDim, location);

      if (dim == null) {
        log.error(
            "Unknown Dimension= {} for variable = {} {} ", dimName, v.getFullName(), location);
        return;
      }
      if (dim.getLength() != oldDim.getLength()) {
        log.error(
            "Shared dimension ("
                + dim.getShortName()
                + ") has different length than data dimension ("
                + oldDim.getShortName()
                + ") shared="
                + dim.getLength()
                + " org="
                + oldDim.getLength()
                + " for "
                + v
                + " "
                + location);
        return;
      }
      newDims.add(dim);
    }
    v.setDimensions(newDims);
    if (showWork) System.out.printf(" set shared dimensions for %s %n", v.getNameAndDimensions());
  }
示例#23
0
  void CheckS(Variable v) throws IOException {

    // string
    // assert(null != (v = dodsfile.findVariable("types.strings.s")));
    // assert v.getName().equals("types.strings.s");
    assert v.getRank() == 0;
    assert v.getDataType() == DataType.STRING : v.getDataType();
    CheckSValue(v.read());
  }
示例#24
0
  private void testReadData(Variable v) throws IOException {
    if (show) System.out.printf(" read %s%n", v.getNameAndDimensions());
    assert (null != v);

    assert (null != v.getDimension(0));
    Array a = v.read();
    assert (null != a);
    assert (v.getSize() == a.getSize());
  }
示例#25
0
 void CheckInt16(Variable v) throws IOException {
   // int16
   // assert(null != (v = dodsfile.findVariable("types.integers.i16")));
   // assert v.getName().equals("types.integers.i16");
   assert v.getRank() == 0;
   assert v.getSize() == 1;
   assert v.getDataType() == DataType.SHORT;
   CheckInt16Value(v.read());
 }
示例#26
0
  public synchronized void readBandData(
      Band destBand,
      int sourceOffsetX,
      int sourceOffsetY,
      int sourceWidth,
      int sourceHeight,
      int sourceStepX,
      int sourceStepY,
      ProductData destBuffer,
      ProgressMonitor pm)
      throws IOException, InvalidRangeException {

    if (mustFlipY) {
      sourceOffsetY = destBand.getSceneRasterHeight() - (sourceOffsetY + sourceHeight);
    }
    if (mustFlipX) {
      sourceOffsetX = destBand.getSceneRasterWidth() - (sourceOffsetX + sourceWidth);
    }
    sourceOffsetY += leadLineSkip;
    start[0] = sourceOffsetY;
    start[1] = sourceOffsetX;
    stride[0] = sourceStepY;
    stride[1] = sourceStepX;
    count[0] = sourceHeight;
    count[1] = sourceWidth;
    Object buffer = destBuffer.getElems();
    Variable variable = variableMap.get(destBand);

    pm.beginTask("Reading band '" + variable.getShortName() + "'...", sourceHeight);
    try {
      Section section = new Section(start, count, stride);

      Array array;
      int[] newshape = {sourceHeight, sourceWidth};

      array = variable.read(section);
      if (array.getRank() == 3) {
        array = array.reshapeNoCopy(newshape);
      }
      Object storage;

      if (mustFlipX && !mustFlipY) {
        storage = array.flip(1).copyTo1DJavaArray();
      } else if (!mustFlipX && mustFlipY) {
        storage = array.flip(0).copyTo1DJavaArray();
      } else if (mustFlipX && mustFlipY) {
        storage = array.flip(0).flip(1).copyTo1DJavaArray();
      } else {
        storage = array.copyTo1DJavaArray();
      }

      arraycopy(storage, 0, buffer, 0, destBuffer.getNumElems());
    } finally {
      pm.done();
    }
  }
 /**
  * Read data from a top level Variable of FLOAT data type and return a memory resident Array.
  *
  * @param index LayoutRegular object
  * @param v2 Variable has FLOAT data type.
  * @return Array of data which will be read from Variable through this call.
  */
 public Array readFloatData(LayoutRegular index, Variable v2) throws IOException {
   float[] var = (float[]) (v2.read().get1DJavaArray(v2.getDataType().getPrimitiveClassType()));
   float[] data = new float[(int) index.getTotalNelems()];
   while (index.hasNext()) {
     Layout.Chunk chunk = index.next();
     System.arraycopy(
         var, (int) chunk.getSrcPos() / 4, data, (int) chunk.getDestElem(), chunk.getNelems());
   }
   return Array.factory(data);
 }
示例#28
0
  void CheckD(Variable v) throws IOException {

    // double
    // assert(null != (v = dodsfile.findVariable("types.floats.f64")));
    // assert v.getName().equals("types.floats.f64");
    assert v.getRank() == 0;
    assert v.getSize() == 1;
    assert v.getDataType() == DataType.DOUBLE : v.getDataType();
    CheckDValue(v.read());
  }
示例#29
0
  void CheckUrl(Variable v) throws IOException {

    // url
    // assert(null != (v = dodsfile.findVariable("types.strings.u")));
    // assert v.getName().equals("types.strings.u");
    assert v.getRank() == 0;
    assert v.getDataType() == DataType.STRING : v.getDataType();
    String str = v.readScalarString();
    assert str.equals("http://www.opendap.org") || str.equals("http://www.dods.org") : str;
  }
 public void testAlias() throws IOException {
   String filename = TestAll.cdmUnitTestDir + "fmrc/ensemble/demeter/MM_cnrm_129_red.ncml";
   NetcdfDataset ncd = ucar.nc2.dataset.NetcdfDataset.openDataset(filename);
   Variable v = ncd.findCoordinateAxis("number");
   assert v != null;
   // assert v.isCoordinateVariable();
   assert v instanceof CoordinateAxis1D;
   assert null != ncd.findDimension("ensemble");
   assert v.getDimension(0) == ncd.findDimension("ensemble");
 }