Java Code Examples for ucar.nc2.dataset.NetcdfDataset#wrap()

The following examples show how to use ucar.nc2.dataset.NetcdfDataset#wrap() . 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: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testUnsignedWrap() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <variable name='bvar' shape='lat' type='byte'>\n"
      + "    <attribute name='_Unsigned' value='true' />\n"
      + "    <attribute name='scale_factor' type='float' value='2.0' />\n" + "   </variable>\n" + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Variable v = ncd.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.FLOAT, v.getDataType());

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 2
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testVarWithUnsignedAttribute() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <variable name='bvar' shape='lat' type='byte'>\n"
      + "    <attribute name='_Unsigned' value='true' />\n" + "   </variable>\n" + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Variable v = ncd.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.USHORT, v.getDataType());

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 3
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testVarWithUnsignedType() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <variable name='bvar' shape='lat' type='ubyte'/>"
      + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Variable v = ncd.findVariable("bvar");
    Assert.assertNotNull(v);
    Assert.assertEquals(DataType.USHORT, v.getDataType());

    boolean hasSigned = false;
    Array data = v.read();
    while (data.hasNext()) {
      float b = data.nextFloat();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 4
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testAttWithUnsignedType() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <attribute name='gatt' type='ubyte'>1 0 -1</attribute>"
      + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Attribute att = ncd.findGlobalAttribute("gatt");
    Assert.assertNotNull(att);
    Assert.assertEquals(DataType.UBYTE, att.getDataType());

    Assert.assertEquals(3, att.getLength());
    Array gattValues = att.getValues();

    boolean hasSigned = false;
    while (gattValues.hasNext()) {
      short b = gattValues.nextShort();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 5
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testAttWithUnsignedAtt() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n"
      + "  <attribute name='gatt' type='byte' isUnsigned='true'>1 0 -1</attribute>" + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Attribute att = ncd.findGlobalAttribute("gatt");
    Assert.assertNotNull(att);
    Assert.assertEquals(DataType.UBYTE, att.getDataType());

    Assert.assertEquals(3, att.getLength());
    Array gattValues = att.getValues();

    boolean hasSigned = false;
    while (gattValues.hasNext()) {
      short b = gattValues.nextShort();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 6
Source File: TestUnsigned.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testAttWithUnsignedType2() throws IOException {
  String ncml = "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<netcdf xmlns='http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2' location='"
      + TestDir.cdmLocalTestDataDir + "testWrite.nc'>\n" + "  <attribute name='gatt' type='ubyte' value='1 0 -1' />"
      + "</netcdf>";

  try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml), null);
      NetcdfFile ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getEnhanceAll())) {

    Attribute att = ncd.findGlobalAttribute("gatt");
    Assert.assertNotNull(att);
    Assert.assertEquals(DataType.UBYTE, att.getDataType());

    Assert.assertEquals(3, att.getLength());
    Array gattValues = att.getValues();

    boolean hasSigned = false;
    while (gattValues.hasNext()) {
      short b = gattValues.nextShort();
      if (b < 0)
        hasSigned = true;
    }
    Assert.assertTrue(!hasSigned);
  }
}
 
Example 7
Source File: ThreddsWmsServlet.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void dispatchWmsRequest(String request, RequestParams params, HttpServletRequest httpServletRequest,
    HttpServletResponse httpServletResponse, WmsCatalogue catalogue) throws Exception {
  /*
   * The super implementation of this gets called with a servlet-wide
   * catalogue, which "should" have been injected with the
   * WmsServlet.setCatalogue() method. Since we want one catalogue per
   * dataset, we never call setCatalogue(), but instead we generate a
   * WmsCatalogue (or more likely in a final version, retrieve a cached
   * one) on each request, and pass that to the super implementation.
   */

  /*
   * Map the request to a file path
   */
  // Look - is setting this to null the right thing to do??
  String removePrefix = null;
  TdsRequestedDataset tdsDataset = new TdsRequestedDataset(httpServletRequest, removePrefix);
  if (catalogueCache.containsKey(tdsDataset.getPath())) {
    catalogue = catalogueCache.get(tdsDataset.getPath());
  } else {
    NetcdfFile ncf = tdsDataset.getNetcdfFile(httpServletRequest, httpServletResponse, tdsDataset.getPath());
    NetcdfDataset ncd;
    if (tdsDataset.useNetcdfJavaBuilders()) {
      ncd = NetcdfDatasets.enhance(ncf, NetcdfDataset.getDefaultEnhanceMode(), null);
    } else {
      ncd = NetcdfDataset.wrap(ncf, NetcdfDataset.getDefaultEnhanceMode());
    }

    String netcdfFilePath = ncf.getLocation();

    /*
     * Generate a new catalogue for the given dataset
     * 
     * In the full system, we should keep a cache of these
     * ThreddsWmsCatalogues, but in this example we just create each new one
     * on the fly.
     * 
     * If a feature cache is required on the WMS (a Good Idea), I recommend
     * a single cache in this servlet which gets passed to each WmsCatalogue
     * upon construction (i.e. HERE). That's a TDS implementation detail
     * though, hence not in this example.
     */
    if (netcdfFilePath == null) {
      throw new EdalLayerNotFoundException("The requested dataset is not available on this server");
    }
    catalogue = new ThreddsWmsCatalogue(ncd, tdsDataset.getPath());
    catalogueCache.put(tdsDataset.getPath(), catalogue);
  }

  /*
   * Now that we've got a WmsCatalogue, we can pass this request to the
   * super implementation which will handle things from here.
   */
  super.dispatchWmsRequest(request, params, httpServletRequest, httpServletResponse, catalogue);
}
 
Example 8
Source File: DatasetManager.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Open a file as a GridDataset, using getNetcdfFile(), so that it gets wrapped in NcML if needed.
 */
// return null means request has been handled, and calling routine should exit without further processing
public GridDataset openGridDataset(HttpServletRequest req, HttpServletResponse res, String reqPath)
    throws IOException {
  // first look for a feature collection
  DataRootManager.DataRootMatch match = dataRootManager.findDataRootMatch(reqPath);
  if ((match != null) && (match.dataRoot.getFeatureCollection() != null)) {
    // see if its under resource control
    if (!resourceAuthorized(req, res, match.dataRoot.getRestrict()))
      return null;

    FeatureCollectionRef featCollection = match.dataRoot.getFeatureCollection();
    if (log.isDebugEnabled())
      log.debug("  -- DatasetHandler found FeatureCollection= " + featCollection);

    InvDatasetFeatureCollection fc = featureCollectionCache.get(featCollection);
    GridDataset gds = fc.getGridDataset(match.remaining);
    if (gds == null)
      throw new FileNotFoundException(reqPath);
    return gds;
  }

  // fetch it as a NetcdfFile; this deals with possible NcML
  NetcdfFile ncfile = openNetcdfFile(req, res, reqPath);
  if (ncfile == null)
    return null;

  NetcdfDataset ncd = null;
  try {
    // Convert to NetcdfDataset
    if (useNetcdfJavaBuilders || isLocationObjectStore(ncfile.getLocation())) {
      ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
    } else {
      ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getDefaultEnhanceMode());
    }
    return new ucar.nc2.dt.grid.GridDataset(ncd);


  } catch (Throwable t) {
    if (ncd == null)
      ncfile.close();
    else
      ncd.close();

    if (t instanceof IOException)
      throw (IOException) t;

    String msg = ncd == null ? "Problem wrapping NetcdfFile in NetcdfDataset"
        : "Problem creating GridDataset from NetcdfDataset";
    log.error("openGridDataset(): " + msg, t);
    throw new IOException(msg + t.getMessage());
  }
}
 
Example 9
Source File: DatasetManager.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public FeatureDatasetPoint openPointDataset(HttpServletRequest req, HttpServletResponse res, String reqPath)
    throws IOException {
  // first look for a feature collection
  DataRootManager.DataRootMatch match = dataRootManager.findDataRootMatch(reqPath);
  if ((match != null) && (match.dataRoot.getFeatureCollection() != null)) {
    // see if its under resource control
    if (!resourceAuthorized(req, res, match.dataRoot.getRestrict()))
      return null;

    FeatureCollectionRef featCollection = match.dataRoot.getFeatureCollection();
    if (log.isDebugEnabled())
      log.debug("  -- DatasetHandler found FeatureCollection= " + featCollection);

    InvDatasetFeatureCollection fc = featureCollectionCache.get(featCollection);
    FeatureDatasetPoint fd = fc.getPointDataset(match.remaining);
    if (fd == null)
      throw new IllegalArgumentException("Not a Point Dataset " + fc.getName());
    return fd;
  }

  // fetch it as a NetcdfFile; this deals with possible NcML
  NetcdfFile ncfile = openNetcdfFile(req, res, reqPath);
  if (ncfile == null)
    return null;

  Formatter errlog = new Formatter();
  NetcdfDataset ncd = null;
  try {
    if (useNetcdfJavaBuilders || isLocationObjectStore(ncfile.getLocation())) {
      ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
    } else {
      ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getDefaultEnhanceMode());
    }
    return (FeatureDatasetPoint) FeatureDatasetFactoryManager.wrap(FeatureType.ANY_POINT, ncd, null, errlog);

  } catch (Throwable t) {
    if (ncd == null)
      ncfile.close();
    else
      ncd.close();

    if (t instanceof IOException)
      throw (IOException) t;

    String msg = ncd == null ? "Problem wrapping NetcdfFile in NetcdfDataset; "
        : "Problem calling FeatureDatasetFactoryManager; ";
    msg += errlog.toString();
    log.error("openGridDataset(): " + msg, t);
    throw new IOException(msg + t.getMessage());
  }
}
 
Example 10
Source File: DatasetManager.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public SimpleGeometryFeatureDataset openSimpleGeometryDataset(HttpServletRequest req, HttpServletResponse res,
    String reqPath) throws IOException {
  // first look for a feature collection
  DataRootManager.DataRootMatch match = dataRootManager.findDataRootMatch(reqPath);
  if ((match != null) && (match.dataRoot.getFeatureCollection() != null)) {
    // see if its under resource control
    if (!resourceAuthorized(req, res, match.dataRoot.getRestrict()))
      return null;

    FeatureCollectionRef featCollection = match.dataRoot.getFeatureCollection();
    if (log.isDebugEnabled())
      log.debug("  -- DatasetHandler found FeatureCollection= " + featCollection);

    InvDatasetFeatureCollection fc = featureCollectionCache.get(featCollection);
    SimpleGeometryFeatureDataset fd = fc.getSimpleGeometryDataset(match.remaining);
    if (fd == null)
      throw new IllegalArgumentException("Not a Simple Geometry Dataset " + fc.getName());
    return fd;
  }

  // fetch it as a NetcdfFile; this deals with possible NcML
  NetcdfFile ncfile = openNetcdfFile(req, res, reqPath);
  if (ncfile == null)
    return null;

  Formatter errlog = new Formatter();
  NetcdfDataset ncd = null;
  try {
    if (useNetcdfJavaBuilders || isLocationObjectStore(ncfile.getLocation())) {
      ncd = NetcdfDatasets.enhance(ncfile, NetcdfDataset.getDefaultEnhanceMode(), null);
    } else {
      ncd = NetcdfDataset.wrap(ncfile, NetcdfDataset.getDefaultEnhanceMode());
    }
    return (SimpleGeometryFeatureDataset) FeatureDatasetFactoryManager.wrap(FeatureType.SIMPLE_GEOMETRY, ncd, null,
        errlog);

  } catch (Throwable t) {
    if (ncd == null)
      ncfile.close();
    else
      ncd.close();

    if (t instanceof IOException)
      throw (IOException) t;

    String msg = ncd == null ? "Problem wrapping NetcdfFile in NetcdfDataset; "
        : "Problem calling FeatureDatasetFactoryManager; ";
    msg += errlog.toString();
    log.error("openSimpleGeometryDataset(): " + msg, t);
    throw new IOException(msg + t.getMessage());
  }
}