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;
    }
  }
示例#2
0
  public void utestReadNcMLInputStream() throws Exception {
    String ncmlLoc = "file:C:\\data\\work\\margolis\\test.ncml";
    GridDataset fullDataset = GridDataset.open(ncmlLoc);
    System.out.printf("full size= %d%n", fullDataset.getGrids().size());

    // real ncml through a InputStream.
    String ncml =
        "<?xml version='1.0' encoding='UTF-8'?>\n"
            + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2'>\n"
            + "  <variable name='time'>\n"
            + "    <attribute name='ncmlAdded' value='timeAtt'/>\n"
            + "  </variable>\n"
            + "  <aggregation dimName='time' type='joinExisting'>\n"
            + "    <netcdf location='file:C:\\data\\work\\margolis\\f_0000000.nc'/>\n"
            + "    <netcdf location='file:C:\\data\\work\\margolis\\f_0032400.nc'/>\n"
            + "  </aggregation>\n"
            + "</netcdf>";
    NetcdfDataset aggregatedDataset =
        NcMLReader.readNcML(new ByteArrayInputStream(ncml.getBytes()), null);
    GridDataset emptyDataset = new GridDataset(aggregatedDataset);
    System.out.printf("empty= %s%n", emptyDataset.getGrids().size());

    assert emptyDataset.getGrids().size() == fullDataset.getGrids().size();
  }