@Test
  public void testSaveFile() throws ScanFileHolderException {
    Dataset a = DatasetFactory.createRange(128 * 128, Dataset.FLOAT32).reshape(128, 128);
    DataHolder d = new DataHolder();
    a.idivide(10000);
    d.addDataset("a", a);

    String oname = testScratchDirectoryName + "a.tif";
    TIFFImageSaver s;
    DataHolder in;

    s = new TIFFImageSaver(oname, true);
    s.saveFile(d);

    in = new TIFFImageLoader(oname).loadFile();
    checkDataset(a, in.getDataset(0));

    s = new TIFFImageSaver(oname);
    s.saveFile(d);

    in = new TIFFImageLoader(oname).loadFile();
    checkDataset(a.cast(Dataset.INT32), in.getDataset(0));
  }
  public static Dataset convert1DDataset(
      Dataset axisData, XAxis inAxisType, XAxis outAxisType, double lambda) throws Exception {
    int[] shape = axisData.getShape();
    int rank = shape.length;

    if (rank > 1) {
      throw new Exception("Cannot convert dataset with more than one dimension.");
    }

    Dataset result = DatasetFactory.zeros(shape, Dataset.FLOAT64);

    // TODO FIXME Change the names given to the axis labels to use the ones in the enum.
    if (outAxisType.equals(XAxis.ANGLE)) {
      for (int i = 0; i < axisData.getSize(); i++) {
        Double xAxisVal = axisData.getDouble(i);
        result.set(inAxisType.convertToANGLE(xAxisVal, lambda), i);
      }
      result.setName("2theta");
    } else if (outAxisType.equals(XAxis.Q)) {
      for (int i = 0; i < axisData.getSize(); i++) {
        Double xAxisVal = axisData.getDouble(i);
        result.set(inAxisType.convertToQ(xAxisVal, lambda), i);
      }
      result.setName("Q");
    } else if (outAxisType.equals(XAxis.RESOLUTION)) {
      for (int i = 0; i < axisData.getSize(); i++) {
        Double xAxisVal = axisData.getDouble(i);
        result.set(inAxisType.convertToRESOLUTION(xAxisVal, lambda), i);
      }
      result.setName("d-spacing");
    } else {
      throw new Exception("Unknown axis type conversion requested.");
    }

    return result;
  }
  @SuppressWarnings("unchecked")
  public DataHolder loadFile(boolean loadData) {

    DataHolder dataHolder = new DataHolder();

    NexusFile file;
    try {
      file = new NexusFile(fileName, NexusFile.NXACC_READ);

      if (loadMetadata) {
        metadata = new Metadata();
        metadata.setFilePath(fileName);
      }

      // file.opengroup("entry1", "NXentry");

      // Look for an NXentry...
      Enumeration<String> topKeys = file.groupdir().keys();
      while (topKeys.hasMoreElements()) {
        String topName = topKeys.nextElement();
        String topClass = (String) file.groupdir().get(topName);
        if (topClass.compareTo("NXentry") == 0) {
          // Specify class explicitly to make sure we are opening the correct one. Otherwise it will
          // throw an
          // exception.
          file.opengroup(topName, "NXentry");
          Enumeration<String> keys = file.groupdir().keys();
          while (keys.hasMoreElements()) {
            String name = keys.nextElement();
            String className = (String) file.groupdir().get(name);
            if (className.compareTo("NXdata") == 0) {
              file.opengroup(name, "NXdata");
              // Now lets get a list of the data items
              Enumeration<String> dataKeys = file.groupdir().keys();
              while (dataKeys.hasMoreElements()) {

                // Hashtable h = file.attrdir();
                // Enumeration e = h.keys();
                // while (e.hasMoreElements()) {
                // String attname = (String)e.nextElement();
                // AttributeEntry atten = (AttributeEntry)h.get(attname);
                // JythonServerFacade.getInstance().print("Found SDS attribute: " + attname +
                // " type: "+ atten.type + " ,length: " + atten.length);
                //
                // }

                String dataName = dataKeys.nextElement();
                file.opendata(dataName);
                int[] iDim = new int[20];
                int[] iStart = new int[2];
                file.getinfo(iDim, iStart);
                // JythonServerFacade.getInstance().print(
                // "Found " + dataName + " with: rank = " + iStart[0] + " type = " + iStart[1]
                // + " dims = " + iDim[0] + ", " + iDim[1]);

                // h = file.attrdir();
                // e = h.keys();
                // while (e.hasMoreElements()) {
                // String attname = (String)e.nextElement();
                // AttributeEntry atten = (AttributeEntry)h.get(attname);
                // JythonServerFacade.getInstance().print("Found SDS attribute: " + attname +
                // " type: "+ atten.type + " ,length: " + atten.length);
                //
                // }

                // Now lets create an array of the dimensions for creating the dataset.
                final int rank = iStart[0];
                int[] shape = Arrays.copyOf(iDim, rank);
                if (loadData) {
                  final int dtype = Nexus.getDType(iStart[1]);
                  Dataset ds = DatasetFactory.zeros(shape, dtype);
                  file.getdata(ds.getBuffer());
                  ds.setName(dataName);
                  dataHolder.addDataset(dataName, ds);
                }
                if (loadMetadata) {
                  metadata.addDataInfo(dataName, shape);
                }
                file.closedata();
              }
              // Close NXdata
              file.closegroup();
            }
          }
          // Close the NXentry
          file.closegroup();
        }
      }
      // for (Iterator iterator = file.groupdir().values().iterator(); iterator.hasNext();) {
      // String type = (String) iterator.next();
      // JythonServerFacade.getInstance().print(type);
      // }

      file.close();
      if (loadMetadata) {
        dataHolder.setMetadata(metadata);
      }
      return dataHolder;

    } catch (NexusException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      // TODO Empty clause
    }

    return null;
  }