Exemplo n.º 1
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);
        }
      }
    }
  }
Exemplo n.º 2
0
  private ProductData.UTC getUTCAttribute(String key, List<Attribute> globalAttributes) {
    Attribute attribute = findAttribute(key, globalAttributes);
    Boolean isModis = false;
    try {
      isModis = findAttribute("MODIS_Resolution", globalAttributes).isString();
    } catch (Exception ignored) {
    }

    if (attribute != null) {
      String timeString = attribute.getStringValue().trim();
      final DateFormat dateFormat = ProductData.UTC.createDateFormat("yyyyDDDHHmmssSSS");
      final DateFormat dateFormatModis =
          ProductData.UTC.createDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
      final DateFormat dateFormatOcts =
          ProductData.UTC.createDateFormat("yyyyMMdd HH:mm:ss.SSSSSS");
      try {
        if (isModis) {
          final Date date = dateFormatModis.parse(timeString);
          String milliSeconds = timeString.substring(timeString.length() - 3);
          return ProductData.UTC.create(date, Long.parseLong(milliSeconds) * 1000);
        } else if (productReader.getProductType() == SeadasProductReader.ProductType.Level1A_OCTS) {
          final Date date = dateFormatOcts.parse(timeString);
          String milliSeconds = timeString.substring(timeString.length() - 3);
          return ProductData.UTC.create(date, Long.parseLong(milliSeconds) * 1000);
        } else {
          final Date date = dateFormat.parse(timeString);
          String milliSeconds = timeString.substring(timeString.length() - 3);
          return ProductData.UTC.create(date, Long.parseLong(milliSeconds) * 1000);
        }
      } catch (ParseException ignored) {
      }
    }
    return null;
  }
Exemplo n.º 3
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");
  }
Exemplo n.º 4
0
  /**
   * 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);
      }
    }
  }
Exemplo n.º 5
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();
   }
 }
Exemplo n.º 6
0
 public float getFloatAttribute(String key, List<Attribute> globalAttributes)
     throws ProductIOException {
   Attribute attribute = findAttribute(key, globalAttributes);
   if (attribute == null) {
     throw new ProductIOException("Global attribute '" + key + "' is missing.");
   } else {
     return attribute.getNumericValue(0).floatValue();
   }
 }
  /* (non-Javadoc)
   * @see loci.formats.NetCDFService#getAttributeValue(java.lang.String)
   */
  public String getAttributeValue(String path) {
    String groupName = getDirectory(path);
    String attributeName = getName(path);
    Group group = getGroup(groupName);

    Attribute attribute = group.findAttribute(attributeName);
    if (attribute == null) return null;
    return arrayToString(attribute.getValues());
  }
Exemplo n.º 8
0
 public String getStringAttribute(String key, List<Attribute> attributeList)
     throws ProductIOException {
   Attribute attribute = findAttribute(key, attributeList);
   if (attribute == null || attribute.getLength() != 1) {
     throw new ProductIOException("Global attribute '" + key + "' is missing.");
   } else {
     return attribute.getStringValue().trim();
   }
 }
Exemplo n.º 9
0
 protected void addAttributesToElement(
     List<Attribute> globalAttributes, final MetadataElement element) {
   for (Attribute attribute : globalAttributes) {
     // if (attribute.getName().contains("EV")) {
     if (attribute.getShortName().matches(".*(EV|Value|Bad|Nois|Electronics|Dead|Detector).*")) {
     } else {
       addAttributeToElement(element, attribute);
     }
   }
 }
  /* (non-Javadoc)
   * @see loci.formats.NetCDFService#getVariableAttributes(java.lang.String)
   */
  public Hashtable<String, Object> getVariableAttributes(String name) {
    String groupName = getDirectory(name);
    String variableName = getName(name);
    Group group = getGroup(groupName);

    Variable variable = group.findVariable(variableName);
    List<Attribute> attributes = variable.getAttributes();
    Hashtable<String, Object> toReturn = new Hashtable<String, Object>();
    for (Attribute attribute : attributes) {
      toReturn.put(attribute.getName(), arrayToString(attribute.getValues()));
    }
    return toReturn;
  }
Exemplo n.º 11
0
 /**
  * Is this the right format for this adapter
  *
  * @param ncd NetcdfDataset to check
  * @return true if the right format
  */
 public static boolean isValidFile(NetcdfDataset ncd) {
   // return (buildConfig(ncd) != null);
   // Check for "center" attribute w/ value of "UCAR/CDAAC".
   Attribute attrib = ncd.findGlobalAttributeIgnoreCase("center");
   if (attrib == null) {
     return false;
   }
   if (!attrib.isString()) {
     return false;
   }
   if (!attrib.getStringValue().equals("UCAR/CDAAC")) {
     return false;
   }
   return true;
 }
Exemplo n.º 12
0
  public static ThreddsMetadata.Variables extractVariables(
      InvDatasetImpl threddsDataset, GridDataset gridDataset) {

    thredds.catalog.DataFormatType fileFormat = threddsDataset.getDataFormatType();
    if ((fileFormat != null)
        && (fileFormat.equals(DataFormatType.GRIB1) || fileFormat.equals(DataFormatType.GRIB2))) {
      boolean isGrib1 = fileFormat.equals(DataFormatType.GRIB1);
      ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables(fileFormat.toString());
      for (GridDatatype grid : gridDataset.getGrids()) {
        ThreddsMetadata.Variable v = new ThreddsMetadata.Variable();
        v.setName(grid.getName());
        v.setDescription(grid.getDescription());
        v.setUnits(grid.getUnitsString());

        // ucar.nc2.Attribute att = grid.findAttributeIgnoreCase("GRIB_param_number");
        // String paramNumber = (att != null) ? att.getNumericValue().toString() : null;
        if (isGrib1) {
          v.setVocabularyName(grid.findAttValueIgnoreCase("GRIB_param_name", "ERROR"));
          v.setVocabularyId(grid.findAttributeIgnoreCase("GRIB_param_id"));
        } else {
          String paramDisc = grid.findAttValueIgnoreCase("GRIB_param_discipline", "");
          String paramCategory = grid.findAttValueIgnoreCase("GRIB_param_category", "");
          String paramName = grid.findAttValueIgnoreCase("GRIB_param_name", "");
          v.setVocabularyName(paramDisc + " / " + paramCategory + " / " + paramName);
          v.setVocabularyId(grid.findAttributeIgnoreCase("GRIB_param_id"));
        }
        vars.addVariable(v);
      }
      vars.sort();
      return vars;

    } else { // GRID but not GRIB
      ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.0");
      for (GridDatatype grid : gridDataset.getGrids()) {
        ThreddsMetadata.Variable v = new ThreddsMetadata.Variable();
        vars.addVariable(v);

        v.setName(grid.getName());
        v.setDescription(grid.getDescription());
        v.setUnits(grid.getUnitsString());

        ucar.nc2.Attribute att = grid.findAttributeIgnoreCase("standard_name");
        v.setVocabularyName((att != null) ? att.getStringValue() : "N/A");
      }
      vars.sort();
      return vars;
    }
  }
Exemplo n.º 13
0
 public String getValue() {
   Array value = att.getValues();
   try {
     return NCdumpW.printArray(value, null, null);
   } catch (IOException e) {
     return e.getMessage();
   }
 }
Exemplo n.º 14
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");
  }
Exemplo n.º 15
0
  /**
   * Extract a list of data variables (and their canonical names if possible) from the dataset.
   *
   * @param threddsDataset open this dataset
   * @return ThreddsMetadata.Variables, or null if unable.
   * @throws IOException on read error
   */
  public static ThreddsMetadata.Variables extractVariables(InvDatasetImpl threddsDataset)
      throws IOException {
    ThreddsDataFactory.Result result = null;

    try {
      result = new ThreddsDataFactory().openFeatureDataset(threddsDataset, null);
      if (result.fatalError) {
        System.out.println(" openDatatype errs=" + result.errLog);
        return null;
      }

      if (result.featureType == FeatureType.GRID) {
        // System.out.println(" extractVariables GRID=" + result.location);
        GridDataset gridDataset = (GridDataset) result.featureDataset;
        return extractVariables(threddsDataset, gridDataset);

      } else if ((result.featureType == FeatureType.STATION)
          || (result.featureType == FeatureType.POINT)) {
        PointObsDataset pobsDataset = (PointObsDataset) result.featureDataset;
        ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.0");
        for (VariableSimpleIF vs : pobsDataset.getDataVariables()) {
          ThreddsMetadata.Variable v = new ThreddsMetadata.Variable();
          vars.addVariable(v);

          v.setName(vs.getName());
          v.setDescription(vs.getDescription());
          v.setUnits(vs.getUnitsString());

          ucar.nc2.Attribute att = vs.findAttributeIgnoreCase("standard_name");
          v.setVocabularyName((att != null) ? att.getStringValue() : "N/A");
        }
        vars.sort();
        return vars;
      }

    } finally {
      try {
        if ((result != null) && (result.featureDataset != null)) result.featureDataset.close();
      } catch (IOException ioe) {
        logger.error("Closing dataset " + result.featureDataset, ioe);
      }
    }

    return null;
  }
 /**
  * Recursively parses attribute and variable paths, filling <code>attributeList</code> and <code>
  * variableList</code>.
  *
  * @param groups List of groups to recursively parse.
  */
 private void parseAttributesAndVariables(List<Group> groups) {
   for (Group group : groups) {
     String groupName = group.getName();
     List<Attribute> attributes = group.getAttributes();
     for (Attribute attribute : attributes) {
       String attributeName = attribute.getName();
       if (!groupName.endsWith("/")) attributeName = "/" + attributeName;
       attributeList.add(groupName + attributeName);
     }
     List<Variable> variables = group.getVariables();
     for (Variable variable : variables) {
       String variableName = variable.getName();
       if (!groupName.endsWith("/")) variableName = "/" + variableName;
       variableList.add(variableName);
     }
     groups = group.getGroups();
     parseAttributesAndVariables(groups);
   }
 }
Exemplo n.º 17
0
  public static ThreddsMetadata.Variables extractVariables(FeatureDatasetPoint fd) {
    ThreddsMetadata.Variables vars = new ThreddsMetadata.Variables("CF-1.5");
    List<VariableSimpleIF> dataVars = fd.getDataVariables();
    if (dataVars == null) return vars;

    for (VariableSimpleIF v : dataVars) {
      ThreddsMetadata.Variable tv = new ThreddsMetadata.Variable();
      vars.addVariable(tv);

      tv.setName(v.getName());
      tv.setDescription(v.getDescription());
      tv.setUnits(v.getUnitsString());

      ucar.nc2.Attribute att = v.findAttributeIgnoreCase("standard_name");
      tv.setVocabularyName((att != null) ? att.getStringValue() : "N/A");
    }
    vars.sort();
    return vars;
  }
  private static IndexCoding readIndexCoding(Variable variable, String indexCodingName)
      throws ProductIOException {
    final IndexCoding indexCoding = CfIndexCodingPart.readIndexCoding(variable, indexCodingName);

    if (indexCoding != null) {
      final Attribute descriptionsAtt = variable.findAttributeIgnoreCase(INDEX_DESCRIPTIONS);
      if (descriptionsAtt != null) {
        final String[] descriptions = descriptionsAtt.getStringValue().split(DESCRIPTION_SEPARATOR);
        if (indexCoding.getNumAttributes() == descriptions.length) {
          for (int i = 0; i < descriptions.length; i++) {
            indexCoding.getAttributeAt(i).setDescription(descriptions[i]);
          }
        }
      }
      final Attribute nameAtt = variable.findAttributeIgnoreCase(INDEX_CODING_NAME);
      if (nameAtt != null) {
        indexCoding.setName(nameAtt.getStringValue());
      }
    }
    return indexCoding;
  }
Exemplo n.º 19
0
  /** Instances which have same content are equal. */
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if ((o == null) || !(o instanceof Attribute)) return false;

    final Attribute att = (Attribute) o;

    if (!name.equals(att.name)) return false;
    if (nelems != att.nelems) return false;
    if (!dataType.equals(att.dataType)) return false;

    if (svalue != null) return svalue.equals(att.getStringValue());

    if (values != null) {
      for (int i = 0; i < getLength(); i++) {
        int r1 = isString() ? getStringValue(i).hashCode() : getNumericValue(i).hashCode();
        int r2 =
            att.isString() ? att.getStringValue(i).hashCode() : att.getNumericValue(i).hashCode();
        if (r1 != r2) return false;
      }
    }

    return true;
  }
Exemplo n.º 20
0
 MetadataAttribute attributeToMetadata(Attribute attribute) {
   final int productDataType = getProductDataType(attribute.getDataType(), false, false);
   if (productDataType != -1) {
     ProductData productData;
     if (attribute.isString()) {
       productData = ProductData.createInstance(attribute.getStringValue());
     } else if (attribute.isArray()) {
       productData = ProductData.createInstance(productDataType, attribute.getLength());
       productData.setElems(attribute.getValues().getStorage());
     } else {
       productData = ProductData.createInstance(productDataType, 1);
       productData.setElems(attribute.getValues().getStorage());
     }
     return new MetadataAttribute(attribute.getShortName(), productData, true);
   }
   return null;
 }
Exemplo n.º 21
0
  private void fixAttributes(Group g) {
    for (Variable v : g.getVariables()) {
      for (Attribute a : v.getAttributes()) {
        if (a.getShortName().equalsIgnoreCase("UNIT") || a.getShortName().equalsIgnoreCase("UNITS"))
          a.setShortName(CDM.UNITS);
        if (a.getShortName().equalsIgnoreCase("SCALE_FACTOR")) a.setShortName(CDM.SCALE_FACTOR);
        if (a.getShortName().equalsIgnoreCase("OFFSET")) a.setShortName(CDM.ADD_OFFSET);
      }
    }

    for (Group ng : g.getGroups()) {
      fixAttributes(ng);
    }
  }
Exemplo n.º 22
0
  private double[] getValueAsDouble(Attribute att) {
    int n = att.getLength();
    double[] value = new double[n];

    if (debugMissing) System.out.printf("missing_data: ");
    for (int i = 0; i < n; i++) {
      if (isUnsigned && att.getDataType() == DataType.BYTE)
        value[i] = convertScaleOffsetMissing(att.getNumericValue(i).byteValue());
      else if (isUnsigned && att.getDataType() == DataType.SHORT)
        value[i] = convertScaleOffsetMissing(att.getNumericValue(i).shortValue());
      else if (isUnsigned && att.getDataType() == DataType.INT)
        value[i] = convertScaleOffsetMissing(att.getNumericValue(i).intValue());
      else value[i] = scale * att.getNumericValue(i).doubleValue() + offset;
      if (debugMissing) System.out.print(" " + value[i]);
    }
    if (debugMissing) System.out.println();
    return value;
  }
Exemplo n.º 23
0
 /**
  * Returns the phenomenon that the given variable represents.
  *
  * <p>This name will be, in order of preference:
  *
  * <p>The standard name
  *
  * <p>The long name
  *
  * <p>The variable name
  */
 private static String getVariableName(Variable var) {
   Attribute stdNameAtt = var.findAttributeIgnoreCase("standard_name");
   if (stdNameAtt == null || stdNameAtt.getStringValue().trim().equals("")) {
     Attribute longNameAtt = var.findAttributeIgnoreCase("long_name");
     if (longNameAtt == null || longNameAtt.getStringValue().trim().equals("")) {
       return var.getFullName();
     } else {
       return longNameAtt.getStringValue();
     }
   } else {
     return stdNameAtt.getStringValue();
   }
 }
Exemplo n.º 24
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");
  }
Exemplo n.º 25
0
  protected Band addNewBand(Product product, Variable variable) {
    final int sceneRasterWidth = product.getSceneRasterWidth();
    final int sceneRasterHeight = product.getSceneRasterHeight();
    Band band = null;

    int variableRank = variable.getRank();
    if (variableRank == 2) {
      final int[] dimensions = variable.getShape();
      final int height = dimensions[0] - leadLineSkip - tailLineSkip;
      final int width = dimensions[1];
      if (height == sceneRasterHeight && width == sceneRasterWidth) {
        final String name = variable.getShortName();
        final int dataType = getProductDataType(variable);
        band = new Band(name, dataType, width, height);
        final String validExpression = bandInfoMap.get(name);
        if (validExpression != null && !validExpression.equals("")) {
          band.setValidPixelExpression(validExpression);
        }
        product.addBand(band);

        try {
          band.setNoDataValue(
              (double) variable.findAttribute("bad_value_scaled").getNumericValue().floatValue());
          band.setNoDataValueUsed(true);
        } catch (Exception ignored) {
        }

        final List<Attribute> list = variable.getAttributes();
        for (Attribute hdfAttribute : list) {
          final String attribName = hdfAttribute.getShortName();
          if ("units".equals(attribName)) {
            band.setUnit(hdfAttribute.getStringValue());
          } else if ("long_name".equals(attribName)) {
            band.setDescription(hdfAttribute.getStringValue());
          } else if ("slope".equals(attribName)) {
            band.setScalingFactor(hdfAttribute.getNumericValue(0).doubleValue());
          } else if ("intercept".equals(attribName)) {
            band.setScalingOffset(hdfAttribute.getNumericValue(0).doubleValue());
          }
        }
      }
    }
    return band;
  }
Exemplo n.º 26
0
  public void testCoordVar(NetcdfFile ncfile) {

    Variable lat = ncfile.findVariable("lat");
    assert null != lat;
    assert lat.getShortName().equals("lat");
    assert lat.getRank() == 1;
    assert lat.getSize() == 3;
    assert lat.getShape()[0] == 3;
    assert lat.getDataType() == DataType.FLOAT;

    assert !lat.isUnlimited();
    assert lat.getDimension(0).equals(ncfile.findDimension("lat"));

    Attribute att = lat.findAttribute("units");
    assert null != att;
    assert !att.isArray();
    assert att.isString();
    assert att.getDataType() == DataType.STRING;
    assert att.getStringValue().equals("degrees_north");
    assert att.getNumericValue() == null;
    assert att.getNumericValue(3) == null;

    try {
      Array data = lat.read();
      assert data.getRank() == 1;
      assert data.getSize() == 3;
      assert data.getShape()[0] == 3;
      assert data.getElementType() == float.class;

      IndexIterator dataI = data.getIndexIterator();
      assert TestUtils.close(dataI.getDoubleNext(), 41.0);
      assert TestUtils.close(dataI.getDoubleNext(), 40.0);
      assert TestUtils.close(dataI.getDoubleNext(), 39.0);
    } catch (IOException io) {
    }
  }
Exemplo n.º 27
0
  public static boolean isValidFile(NetcdfFile ds) {
    Attribute cdmDtAtt = ds.findGlobalAttributeIgnoreCase("cdm_data_type");
    if (cdmDtAtt == null) cdmDtAtt = ds.findGlobalAttributeIgnoreCase("cdm_datatype");
    if (cdmDtAtt == null) return false;
    if (!cdmDtAtt.isString()) return false;

    String cdmDtString = cdmDtAtt.getStringValue();
    if (cdmDtString == null) return false;
    if (!cdmDtString.equalsIgnoreCase(FeatureType.TRAJECTORY.toString())) return false;

    Attribute conventionsAtt = ds.findGlobalAttributeIgnoreCase("Conventions");
    if (conventionsAtt == null) return (false);
    if (!conventionsAtt.isString()) return (false);
    String convString = conventionsAtt.getStringValue();

    StringTokenizer stoke = new StringTokenizer(convString, ",");
    while (stoke.hasMoreTokens()) {
      String toke = stoke.nextToken().trim();
      if (toke.equalsIgnoreCase("Unidata Observation Dataset v1.0")) return true;
    }

    return false;
  }
Exemplo n.º 28
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");
  }
Exemplo n.º 29
0
  /**
   * Build the configuration from the dataset
   *
   * @param ncd NetcdfDataset
   * @return the trajectory configuration
   */
  private static Config buildConfig(NetcdfDataset ncd) {

    // already did this in isValid, but we'll keep here for later refactor
    Attribute attrib = ncd.findGlobalAttributeIgnoreCase("center");
    if (attrib == null) {
      return null;
    }
    if (!attrib.isString()) {
      return null;
    }
    if (!attrib.getStringValue().equals("UCAR/CDAAC")) {
      return null;
    }

    // Check for start_time, stop_time
    attrib = ncd.findGlobalAttributeIgnoreCase("start_time");
    if (attrib == null) {
      return null;
    }
    if (attrib.isString()) {
      return null;
    }
    double startTime = attrib.getNumericValue().doubleValue();
    attrib = ncd.findGlobalAttributeIgnoreCase("stop_time");
    if (attrib == null) {
      return null;
    }
    if (attrib.isString()) {
      return null;
    }
    double endTime = attrib.getNumericValue().doubleValue();

    // Check that only one dimension and that it is the alt dimension.
    List list = ncd.getRootGroup().getDimensions();
    if (list.size() != 1) {
      return null;
    }
    Dimension d = (Dimension) list.get(0);
    if (!d.getName().equals(timeDimName)) {
      return null;
    }

    Config trajConfig = new Config();
    trajConfig.setTimeDim(d);

    // Check for latitude variable with time dimension and units convertable to "degrees_north".
    Variable var = ncd.getRootGroup().findVariable(latVarName);
    if (var == null) {
      return null;
    }
    list = var.getDimensions();
    if (list.size() != 1) {
      return null;
    }
    d = (Dimension) list.get(0);
    if (!d.getName().equals(timeDimName)) {
      return null;
    }
    String units = var.findAttribute("units").getStringValue();
    if (!SimpleUnit.isCompatible(units, "degrees_north")) {
      return null;
    }

    trajConfig.setLatVar(var);

    // Make the time Variable
    int numTimes = d.getLength();
    double[] times = new double[numTimes];
    // Variable timeVar = new Variable(var);
    // timeVar.setName(timeVarName);
    VariableDS timeVar =
        new VariableDS(
            ncd,
            ncd.getRootGroup(),
            null,
            timeVarName,
            DataType.DOUBLE,
            timeDimName,
            "seconds since 1980-01-06 00:00:00",
            "Time coordinate");
    // Variable timeVar = new Variable(ncd, ncd.getRootGroup(), null,
    //                              timeVarName);
    // timeVar.setDataType(DataType.DOUBLE);
    // timeVar.setDimensions(list);
    // Attribute newUnits =
    //    new Attribute("units", "seconds since 1980-01-06 00:00:00");
    // timeVar.addAttribute(newUnits);
    timeVar.setCachedData(
        Array.makeArray(DataType.DOUBLE, numTimes, endTime, ((startTime - endTime) / numTimes)),
        true);
    ncd.addVariable(ncd.getRootGroup(), timeVar);
    trajConfig.setTimeVar(timeVar);

    // Check for longitude variable with time dimension and units convertable to "degrees_east".
    var = ncd.getRootGroup().findVariable(lonVarName);
    if (var == null) {
      return null;
    }
    list = var.getDimensions();
    if (list.size() != 1) {
      return null;
    }
    d = (Dimension) list.get(0);
    if (!d.getName().equals(timeDimName)) {
      return null;
    }
    units = var.findAttribute("units").getStringValue();
    if (!SimpleUnit.isCompatible(units, "degrees_east")) {
      return null;
    }

    trajConfig.setLonVar(var);

    // Check for altitude variable with time dimension and units convertable to "m".
    var = ncd.getRootGroup().findVariable(elevVarName);
    if (var == null) {
      return null;
    }
    list = var.getDimensions();
    if (list.size() != 1) {
      return null;
    }
    d = (Dimension) list.get(0);
    if (!d.getName().equals(timeDimName)) {
      return null;
    }
    units = var.findAttribute("units").getStringValue();
    if (!SimpleUnit.isCompatible(units, "meters")) {
      return null;
    }

    trajConfig.setElevVar(var);

    trajConfig.setTrajectoryId(trajId);

    return trajConfig;
  }
    // create from a dataset
    public ObsBean(Structure obs, StructureData sdata) {
      // first choice
      for (Variable v : obs.getVariables()) {
        Attribute att = v.findAttribute("BUFR:TableB_descriptor");
        if (att == null) continue;
        String val = att.getStringValue();
        if (val.equals("0-5-1") && Double.isNaN(lat)) {
          lat = sdata.convertScalarDouble(v.getShortName());
        } else if (val.equals("0-6-1") && Double.isNaN(lon)) {
          lon = sdata.convertScalarDouble(v.getShortName());
        } else if (val.equals("0-7-30") && Double.isNaN(alt)) {

          alt = sdata.convertScalarDouble(v.getShortName());
        } else if (val.equals("0-4-1") && (year < 0)) {
          year = sdata.convertScalarInt(v.getShortName());
        } else if (val.equals("0-4-2") && (month < 0)) {
          month = sdata.convertScalarInt(v.getShortName());
        } else if (val.equals("0-4-3") && (day < 0)) {
          day = sdata.convertScalarInt(v.getShortName());
        } else if (val.equals("0-4-4") && (hour < 0)) {
          hour = sdata.convertScalarInt(v.getShortName());
        } else if (val.equals("0-4-5") && (minute < 0)) {
          minute = sdata.convertScalarInt(v.getShortName());
        } else if (val.equals("0-4-6") && (sec < 0)) {
          sec = sdata.convertScalarInt(v.getShortName());

        } else if (val.equals("0-1-1") && (wmo_block < 0)) {
          wmo_block = sdata.convertScalarInt(v.getShortName());
        } else if (val.equals("0-1-2") && (wmo_id < 0)) {
          wmo_id = sdata.convertScalarInt(v.getShortName());

        } else if ((stn == null)
            && (val.equals("0-1-7")
                || val.equals("0-1-194")
                || val.equals("0-1-11")
                || val.equals("0-1-18"))) {
          if (v.getDataType().isString()) stn = sdata.getScalarString(v.getShortName());
          else stn = Integer.toString(sdata.convertScalarInt(v.getShortName()));
        }
      }

      // second choice
      for (Variable v : obs.getVariables()) {
        Attribute att = v.findAttribute("BUFR:TableB_descriptor");
        if (att == null) continue;
        String val = att.getStringValue();
        if (val.equals("0-5-2") && Double.isNaN(lat)) {
          lat = sdata.convertScalarDouble(v.getShortName());
        } else if (val.equals("0-6-2") && Double.isNaN(lon)) {
          lon = sdata.convertScalarDouble(v.getShortName());
        } else if (val.equals("0-7-1") && Double.isNaN(alt)) {
          alt = sdata.convertScalarDouble(v.getShortName());
        } else if ((val.equals("0-4-7")) && (sec < 0)) {
          sec = sdata.convertScalarInt(v.getShortName());
        }
      }

      // third choice
      for (Variable v : obs.getVariables()) {
        Attribute att = v.findAttribute("BUFR:TableB_descriptor");
        if (att == null) continue;
        String val = att.getStringValue();
        if (val.equals("0-7-10") && Double.isNaN(alt)) {
          alt = sdata.convertScalarDouble(v.getShortName());
        } else if (val.equals("0-7-2") && Double.isNaN(alt)) {
          alt = sdata.convertScalarDouble(v.getShortName());
        }
      }
    }