Java Code Examples for ucar.nc2.dataset.NetcdfDataset#Builder

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatasetEnhancer.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public NetcdfDataset.Builder enhance() throws IOException {
  // CoordSystemBuilder may enhance dataset: add new variables, attributes, etc
  CoordSystemBuilder coordSysBuilder = null;
  if (wantEnhance.contains(Enhance.CoordSystems) && !dsBuilder.getEnhanceMode().contains(Enhance.CoordSystems)) {
    Optional<CoordSystemBuilder> hasNewBuilder = CoordSystemFactory.factory(dsBuilder, cancelTask);
    if (hasNewBuilder.isPresent()) {
      coordSysBuilder = hasNewBuilder.get();
      coordSysBuilder.augmentDataset(cancelTask);
      dsBuilder.setConventionUsed(coordSysBuilder.getConventionUsed());
    }
  }

  enhanceGroup(dsBuilder.rootGroup);

  // now find coord systems which may change some Variables to axes, etc
  if (coordSysBuilder != null) {
    // temporarily set enhanceMode if incomplete coordinate systems are allowed
    if (wantEnhance.contains(Enhance.IncompleteCoordSystems)) {
      dsBuilder.addEnhanceMode(Enhance.IncompleteCoordSystems);
      coordSysBuilder.buildCoordinateSystems();
      dsBuilder.removeEnhanceMode(Enhance.IncompleteCoordSystems);
    } else {
      coordSysBuilder.buildCoordinateSystems();
    }
  }

  dsBuilder.addEnhanceModes(wantEnhance);
  return dsBuilder;
}
 
Example 2
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * parse a netcdf JDOM Element, and add contents to the targetDS NetcdfDataset.
 * <p/>
 * This is a bit tricky, because it handles several cases When targetDS == refds, we are just modifying targetDS. When
 * targetDS != refds,
 * we keep them seperate, and copy from refds to newds.
 * <p/>
 * The user may be defining new elements or modifying old ones. The only way to tell is by seeing if the elements
 * already exist.
 *
 * @param ncmlLocation NcML URL location, or may be just a unique name for caching purposes.
 * @param builder add the info to this one
 * @param netcdfElem JDOM netcdf element
 * @param cancelTask allow user to cancel the task; may be null
 * @throws IOException on read error
 */
private void readNetcdf(String ncmlLocation, NetcdfDataset.Builder builder, Element netcdfElem,
    @Nullable CancelTask cancelTask) throws IOException {
  this.location = ncmlLocation; // log messages need this

  // detect incorrect namespace
  Namespace use = netcdfElem.getNamespace();
  if (!use.equals(ncNSHttp) && !use.equals(ncNSHttps)) {
    String message = String.format("Namespace specified in NcML must be either '%s' or '%s', but was '%s'.",
        ncNSHttp.getURI(), ncNSHttps.getURI(), use.getURI());
    throw new IllegalArgumentException(message);
  }

  if (ncmlLocation != null) {
    builder.setLocation(ncmlLocation);
  }
  builder.setId(netcdfElem.getAttributeValue("id"));
  builder.setTitle(netcdfElem.getAttributeValue("title"));

  Element aggElem = netcdfElem.getChild("aggregation", ncNS);
  if (aggElem != null) {
    Aggregation agg = readAgg(aggElem, ncmlLocation, builder, cancelTask);
    builder.setAggregation(agg);
    agg.build(cancelTask);

    // LOOK seems like we should add the agg metadata here, so that it can be modified.
  }

  // read the root group and recurse
  readGroup(builder, null, null, netcdfElem);
  String errors = errlog.toString();
  if (!errors.isEmpty()) {
    throw new IllegalArgumentException("NcML had fatal errors:" + errors);
  }

  // enhance means do scale/offset and/or add CoordSystems
  Set<NetcdfDataset.Enhance> mode = parseEnhanceMode(netcdfElem.getAttributeValue("enhance"));
  if (mode != null) {
    // cant just set enhance mode
    if (DatasetEnhancer.enhanceNeeded(mode, null)) {
      DatasetEnhancer enhancer = new DatasetEnhancer(builder, mode, cancelTask);
      enhancer.enhance();
      builder.setEnhanceMode(mode);
    }
  }

  /*
   * LOOK optionally add record structure to netcdf-3
   * String addRecords = netcdfElem.getAttributeValue("addRecords");
   * if ("true".equalsIgnoreCase(addRecords))
   * targetDS.sendIospMessage(NetcdfFile.IOSP_MESSAGE_ADD_RECORD_STRUCTURE);
   */
}
 
Example 3
Source File: CoardsConventions.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new CoardsConventions(datasetBuilder);
}
 
Example 4
Source File: CoordSystemBuilder.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new CoordSystemBuilder(datasetBuilder);
}
 
Example 5
Source File: CF1Convention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new CF1Convention(datasetBuilder);
}
 
Example 6
Source File: GIEFConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
GIEFConvention(NetcdfDataset.Builder datasetBuilder) {
  super(datasetBuilder);
  this.conventionName = CONVENTION_NAME;
}
 
Example 7
Source File: BuilderHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private static void transferGroup(NetcdfFile ds, NetcdfDataset.Builder targetDs, Group src, Group.Builder targetGroup,
    ReplaceVariableCheck replaceCheck) {
  boolean unlimitedOK = true; // LOOK why not allowed?

  // group attributes
  transferAttributes(src, targetGroup.getAttributeContainer());

  // dimensions
  for (Dimension d : src.getDimensions()) {
    if (!targetGroup.findDimensionLocal(d.getShortName()).isPresent()) {
      Dimension newd = Dimension.builder().setName(d.getShortName()).setIsShared(d.isShared())
          .setIsUnlimited(unlimitedOK && d.isUnlimited()).setIsVariableLength(d.isVariableLength())
          .setLength(d.getLength()).build();
      targetGroup.addDimension(newd);
    }
  }

  // variables
  for (Variable v : src.getVariables()) {
    Optional<Variable.Builder<?>> targetV = targetGroup.findVariableLocal(v.getShortName());
    boolean replace = (replaceCheck != null) && replaceCheck.replace(v); // replaceCheck not currently used
    if (replace || !targetV.isPresent()) { // replace it
      // LOOK not needed ??
      /*
       * if ((v instanceof Structure) && !(v instanceof StructureDS)) {
       * v = new StructureDS(targetGroup, (Structure) v);
       * } else
       */
      VariableDS.Builder<?> vb;
      if (!(v instanceof VariableDS)) {
        vb = VariableDS.builder().copyFrom(v);
      } else {
        vb = ((VariableDS) v).toBuilder().setProxyReader(null);
      }

      targetGroup.replaceVariable(vb);
      // LOOK not needed? v.resetDimensions(); // dimensions will be different

    } /*
       * LOOK was else if (!targetV.hasCachedData() && (targetVe.getOriginalVariable() == null)) {
       * // this is the case where we defined the variable, but didnt set its data. we now set it with the first
       * nested
       * // dataset that has a variable with the same name
       * targetVe.setOriginalVariable(v);
       * }
       */
  }

  // nested groups - check if target already has it
  for (Group srcNested : src.getGroups()) {
    Optional<Builder> existing = targetGroup.findGroupLocal(srcNested.getShortName());
    if (!existing.isPresent()) {
      Group.Builder nested = Group.builder().setName(srcNested.getShortName());
      targetGroup.addGroup(nested);
      transferGroup(ds, targetDs, srcNested, nested, replaceCheck);
    } else {
      transferGroup(ds, targetDs, srcNested, existing.get(), replaceCheck);
    }
  }
}
 
Example 8
Source File: Nimbus.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new Nimbus(datasetBuilder);
}
 
Example 9
Source File: HdfEosOmiConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new HdfEosOmiConvention(datasetBuilder);
}
 
Example 10
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read the NcML group element, and nested elements.
 *
 * @param parent the parent group builder, or null when its the root group.
 * @param refParent parent Group in referenced dataset, may be null
 * @param groupElem ncml group element
 */
private Group.Builder readGroup(NetcdfDataset.Builder builder, @Nullable Group.Builder parent,
    @Nullable Group refParent, Element groupElem) {
  Group.Builder groupBuilder;
  Group refGroup = null;

  if (parent == null) {
    refGroup = this.refFile == null ? null : this.refFile.getRootGroup();
    groupBuilder = builder.rootGroup;

  } else {
    String name = groupElem.getAttributeValue("name");
    if (name == null) {
      errlog.format("NcML Group name is required (%s)%n", groupElem);
      return null;
    }
    String nameInFile = groupElem.getAttributeValue("orgName");
    if (nameInFile == null) {
      nameInFile = name;
    }
    // see if it exists in referenced dataset
    if (refParent != null) {
      refGroup = refParent.findGroupLocal(nameInFile);
    }
    if (refGroup == null) { // new
      groupBuilder = Group.builder().setName(name);
      parent.addGroup(groupBuilder);
      if (debugConstruct) {
        System.out.println(" add new group = " + name);
      }

    } else { // exists in refGroup.
      if (explicit) {
        groupBuilder = Group.builder();
        parent.addGroup(groupBuilder);
      } else {
        String finalName = nameInFile;
        groupBuilder = parent.findGroupLocal(finalName)
            .orElseThrow(() -> new IllegalStateException("Cant find Group " + finalName));
      }
      groupBuilder.setName(name);
    }
  }

  // look for attributes
  java.util.List<Element> attList = groupElem.getChildren("attribute", ncNS);
  for (Element attElem : attList) {
    readAtt(groupBuilder.getAttributeContainer(), refGroup, attElem);
  }

  // look for enumTypedef
  java.util.List<Element> etdList = groupElem.getChildren("enumTypedef", ncNS);
  for (Element elem : etdList) {
    readEnumTypedef(groupBuilder, elem);
  }

  // look for dimensions
  java.util.List<Element> dimList = groupElem.getChildren("dimension", ncNS);
  for (Element dimElem : dimList) {
    readDim(groupBuilder, refGroup, dimElem);
  }

  // look for variables
  java.util.List<Element> varList = groupElem.getChildren("variable", ncNS);
  for (Element varElem : varList) {
    readVariable(groupBuilder, refGroup, varElem);
  }

  // process remove command
  java.util.List<Element> removeList = groupElem.getChildren("remove", ncNS);
  for (Element e : removeList) {
    cmdRemove(groupBuilder, e.getAttributeValue("type"), e.getAttributeValue("name"));
  }

  // look for nested groups
  java.util.List<Element> groupList = groupElem.getChildren("group", ncNS);
  for (Element gElem : groupList) {
    readGroup(builder, groupBuilder, refGroup, gElem);
  }
  return groupBuilder;
}
 
Example 11
Source File: HdfEosModisConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private HdfEosModisConvention(NetcdfDataset.Builder datasetBuilder) {
  super(datasetBuilder);
  this.conventionName = CONVENTION_NAME;
}
 
Example 12
Source File: NcMLReaderNew.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Read an NcML file from a URL location, and construct a NetcdfDataset.
 *
 * @param ncmlLocation the URL location string of the NcML document
 * @param referencedDatasetUri if null (usual case) get this from NcML, otherwise use URI as the location of the
 *        referenced dataset.
 * @param cancelTask allow user to cancel the task; may be null
 * @return the resulting NetcdfDataset
 * @throws IOException on read error, or bad referencedDatasetUri URI
 */
public static NetcdfDataset.Builder readNcML(String ncmlLocation, String referencedDatasetUri, CancelTask cancelTask)
    throws IOException {
  URL url = new URL(ncmlLocation);

  if (debugURL) {
    System.out.println(" NcMLReader open " + ncmlLocation);
    System.out.println("   URL = " + url);
    System.out.println("   external form = " + url.toExternalForm());
    System.out.println("   protocol = " + url.getProtocol());
    System.out.println("   host = " + url.getHost());
    System.out.println("   path = " + url.getPath());
    System.out.println("  file = " + url.getFile());
  }

  org.jdom2.Document doc;
  try {
    SAXBuilder builder = new SAXBuilder();
    if (debugURL) {
      System.out.println(" NetcdfDataset URL = <" + url + ">");
    }
    doc = builder.build(url);
  } catch (JDOMException e) {
    throw new IOException(e.getMessage());
  }
  if (debugXML) {
    System.out.println(" SAXBuilder done");
  }

  if (showParsedXML) {
    XMLOutputter xmlOut = new XMLOutputter();
    System.out.println("*** NetcdfDataset/showParsedXML = \n" + xmlOut.outputString(doc) + "\n*******");
  }

  Element netcdfElem = doc.getRootElement();

  if (referencedDatasetUri == null) {
    // the ncml probably refers to another dataset, but doesnt have to
    referencedDatasetUri = netcdfElem.getAttributeValue("location");
    if (referencedDatasetUri == null) {
      referencedDatasetUri = netcdfElem.getAttributeValue("url");
    }
  }
  if (referencedDatasetUri != null) {
    referencedDatasetUri = AliasTranslator.translateAlias(referencedDatasetUri);
  }

  NcMLReaderNew reader = new NcMLReaderNew();
  return reader.readNcML(ncmlLocation, referencedDatasetUri, netcdfElem, cancelTask);
}
 
Example 13
Source File: ZebraConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new ZebraConvention(datasetBuilder);
}
 
Example 14
Source File: HdfEosOmiConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private HdfEosOmiConvention(NetcdfDataset.Builder datasetBuilder) {
  super(datasetBuilder);
  this.conventionName = CONVENTION_NAME;
}
 
Example 15
Source File: AWIPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new AWIPSConvention(datasetBuilder);
}
 
Example 16
Source File: IFPSConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new IFPSConvention(datasetBuilder);
}
 
Example 17
Source File: DefaultConventions.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new DefaultConventions(datasetBuilder);
}
 
Example 18
Source File: Cosmic1Convention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Cosmic1Convention(NetcdfDataset.Builder datasetBuilder) {
  super(datasetBuilder);
  this.conventionName = CONVENTION_NAME;
}
 
Example 19
Source File: ATDRadarConvention.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public CoordSystemBuilder open(NetcdfDataset.Builder datasetBuilder) {
  return new ATDRadarConvention(datasetBuilder);
}
 
Example 20
Source File: BuilderHelper.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Copy contents of "src" to "target". skip ones that already exist (by name).
 * Dimensions and Variables are replaced with equivalent elements, but unlimited dimensions are turned into regular
 * dimensions.
 * Attribute doesnt have to be replaced because its immutable, so its copied by reference.
 *
 * @param src transfer from here. If src is a NetcdfDataset, transferred variables get reparented to target group.
 * @param target transfer to this NetcdfDataset.
 * @param replaceCheck if null, add if a Variable of the same name doesnt already exist, otherwise
 *        replace if replaceCheck.replace( Variable v) is true
 */
static void transferDataset(NetcdfFile src, NetcdfDataset.Builder target, ReplaceVariableCheck replaceCheck) {
  transferGroup(src, target, src.getRootGroup(), target.rootGroup, replaceCheck);
}