Example #1
0
 long computeFieldSize(BaseType bt, boolean isAscii) throws Exception {
   long fieldsize = 0;
   // Figure out what this field is (e.g. primitive or not)
   // Somewhat convoluted.
   if (bt instanceof DConstructor) {
     // simple struct, seq, or grid => recurse
     fieldsize = computeSize((DConstructor) bt, isAscii);
   } else if (bt instanceof DArray) {
     SDArray da = (SDArray) bt;
     // Separate structure arrays from primitive arrays
     if (da.getContainerVar() instanceof DPrimitive) {
       fieldsize = computeArraySize(da);
     } else if (da.getContainerVar() instanceof DStructure) {
       fieldsize = computeSize((DStructure) da.getContainerVar(), isAscii); // recurse
     } else { // Some kind of problem
       throw new NoSuchTypeException("Computesize: unexpected type for " + bt.getLongName());
     }
   } else if (bt instanceof DPrimitive) {
     DPrimitive dp = (DPrimitive) bt;
     if (dp instanceof DString) {
       String v = ((DString) dp).getValue();
       fieldsize = (v == null ? 0 : v.length());
     } else {
       DataType dtype = DODSNetcdfFile.convertToNCType(bt);
       fieldsize = dtype.getSize();
     }
   } else { // Some kind of problem
     throw new NoSuchTypeException("Computesize: unknown type for " + bt.getLongName());
   }
   return fieldsize;
 }
Example #2
0
  private int addAttributes(opendap.dap.AttributeTable table, Variable v, Iterator iter) {
    int count = 0;

    // add attribute table for this variable
    while (iter.hasNext()) {
      Attribute att = (Attribute) iter.next();
      int dods_type = DODSNetcdfFile.convertToDODSType(att.getDataType(), false);

      try {
        String attName = NcDDS.escapeName(att.getName());
        if (att.isString()) {
          /* FIX String value = escapeAttributeStringValues(att.getStringValue());
          table.appendAttribute(attName, dods_type, "\""+value+"\"");
          */
          table.appendAttribute(attName, dods_type, att.getStringValue());
        } else {
          // cant send signed bytes
          if (att.getDataType() == DataType.BYTE) {
            boolean signed = false;
            for (int i = 0; i < att.getLength(); i++) {
              if (att.getNumericValue(i).byteValue() < 0) signed = true;
            }
            if (signed) // promote to signed short
            dods_type = opendap.dap.Attribute.INT16;
          }

          for (int i = 0; i < att.getLength(); i++)
            table.appendAttribute(attName, dods_type, att.getNumericValue(i).toString());
        }
        count++;

      } catch (Exception e) {
        log.error(
            "Error appending attribute " + att.getName() + " = " + att.getStringValue() + "\n" + e);
      }
    } // loop over variable attributes

    // kludgy thing to map char arrays to DODS Strings
    if ((v != null) && (v.getDataType().getPrimitiveClassType() == char.class)) {
      int rank = v.getRank();
      int strlen = (rank == 0) ? 0 : v.getShape(rank - 1);
      Dimension dim = (rank == 0) ? null : v.getDimension(rank - 1);
      try {
        opendap.dap.AttributeTable dodsTable = table.appendContainer("DODS");
        dodsTable.appendAttribute("strlen", opendap.dap.Attribute.INT32, Integer.toString(strlen));
        if ((dim != null) && dim.isShared())
          dodsTable.appendAttribute("dimName", opendap.dap.Attribute.STRING, dim.getName());
        count++;
      } catch (Exception e) {
        log.error("Error appending attribute strlen\n" + e);
      }
    }

    return count;
  }
Example #3
0
 long computeArraySize(SDArray da) throws Exception {
   assert (da.getContainerVar() instanceof DPrimitive);
   BaseType base = da.getPrimitiveVector().getTemplate();
   DataType dtype = DODSNetcdfFile.convertToNCType(base);
   int elemSize = dtype.getSize();
   int n = da.numDimensions();
   List<Range> ranges = new ArrayList<Range>(n);
   long size = 0;
   for (int i = 0; i < n; i++) {
     ranges.add(new Range(da.getStart(i), da.getStop(i), da.getStride(i)));
     Section s = new Section(ranges);
     size += s.computeSize() * elemSize;
   }
   return size;
 }
  private NetcdfDataset openDataset(
      InvAccess access, boolean acquire, ucar.nc2.util.CancelTask task, Result result)
      throws IOException {
    InvDataset invDataset = access.getDataset();
    String datasetId = invDataset.getID();
    String title = invDataset.getName();

    String datasetLocation = access.getStandardUrlName();
    ServiceType serviceType = access.getService().getServiceType();
    if (debugOpen) System.out.println("ThreddsDataset.openDataset= " + datasetLocation);

    // deal with RESOLVER type
    if (serviceType == ServiceType.RESOLVER) {
      InvDatasetImpl rds = openResolver(datasetLocation, task, result);
      if (rds == null) return null;
      return openDataset(rds, acquire, task, result);
    }

    // ready to open it through netcdf API
    NetcdfDataset ds;

    // open DODS type
    if ((serviceType == ServiceType.OPENDAP) || (serviceType == ServiceType.DODS)) {
      String curl = DODSNetcdfFile.canonicalURL(datasetLocation);
      ds =
          acquire
              ? NetcdfDataset.acquireDataset(curl, enhanceMode, task)
              : NetcdfDataset.openDataset(curl, enhanceMode, task);
    }

    // open CdmRemote
    else if (serviceType == ServiceType.CdmRemote) {
      String curl = CdmRemote.canonicalURL(datasetLocation);
      ds =
          acquire
              ? NetcdfDataset.acquireDataset(curl, enhanceMode, task)
              : NetcdfDataset.openDataset(curl, enhanceMode, task);
    }

    /* open ADDE type
    else if (serviceType == ServiceType.ADDE) {
      try {
        ds = ucar.nc2.adde.AddeDatasetFactory.openDataset(access, task);

      } catch (IOException e) {
        log.append("Cant open as ADDE dataset= "+datasetLocation);
        accessList.remove( access);
        continue;
      }
    } */

    else {
      // open through NetcdfDataset API
      ds =
          acquire
              ? NetcdfDataset.acquireDataset(datasetLocation, enhanceMode, task)
              : NetcdfDataset.openDataset(datasetLocation, enhanceMode, task);
    }

    if (ds != null) {
      ds.setId(datasetId);
      ds.setTitle(title);
      annotate(invDataset, ds);
    }

    // see if there's metadata LOOK whats this
    List list = invDataset.getMetadata(MetadataType.NcML);
    if (list.size() > 0) {
      InvMetadata ncmlMetadata = (InvMetadata) list.get(0);
      NcMLReader.wrapNcML(ds, ncmlMetadata.getXlinkHref(), null);
    }

    result.accessUsed = access;
    return ds;
  }
Example #5
0
  @Test
  public void testDuplicates() throws Exception {
    // Check if we are running against motherlode or localhost, or what.
    String testserver = System.getProperty("testserver");
    if (testserver == null) testserver = DFALTTESTSERVER;

    List<Result> results = new ArrayList<Result>();
    if (true) {
      results.add(
          new Result(
              "Top and field vars have same names",
              "http://" + testserver + "/dts/structdupname",
              "netcdf dods://"
                  + testserver
                  + "/dts/structdupname {\n"
                  + " variables:\n"
                  + "   int time;\n"
                  + "Structure {\n"
                  + "   float time;\n"
                  + "} record;\n"
                  + "}"));
    }
    if (true) {
      results.add(
          new Result(
              "TestFailure",
              "http://" + testserver + "/dts/simplestruct",
              "netcdf dods://"
                  + testserver
                  + "/dts/simplestruct {\n"
                  + " variables:\n"
                  + "Structure {\n"
                  + "   int i32;\n"
                  + "} types;\n"
                  + "}"));
    }
    boolean pass = true;
    for (Result result : results) {
      System.out.println("TestDuplicates: " + result.url);
      boolean localpass = true;
      try {
        DODSNetcdfFile ncfile = new DODSNetcdfFile(result.url);
        if (ncfile == null) throw new Exception("Cannot read: " + result.url);
        StringWriter ow = new StringWriter();
        PrintWriter pw = new PrintWriter(ow);
        ncfile.writeCDL(pw, false);
        try {
          pw.close();
          ow.close();
        } catch (IOException ioe) {
        }
        ;
        StringReader baserdr = new StringReader(result.cdl);
        String captured = ow.toString();
        StringReader resultrdr = new StringReader(captured);
        // Diff the two files
        Diff diff = new Diff("Testing " + result.title);
        localpass = !diff.doDiff(baserdr, resultrdr);
        baserdr.close();
        resultrdr.close();
        // Dump the output for visual comparison
        if (System.getProperty("visual") != null) {
          System.out.println("Testing " + result.title + " visual:");
          System.out.println("---------------");
          System.out.print(captured);
          System.out.println("---------------");
        }
      } catch (IllegalArgumentException e) {
        localpass = false;
      }
      if (!localpass) pass = false;
    }
    System.out.flush();
    System.err.flush();
    junit.framework.Assert.assertTrue("Testing " + getTitle(), pass);
  }