Skip to content
obergshavefun edited this page Mar 30, 2016 · 2 revisions

Reading a dataset composed of an array of strings

This snipped reads an array of strings from the specified file. The array is padded with spaces to ensure that each record is aligned properly. The parameter recordLength specifies the width of each record.

    private bool ReadStringArray(hid_t fileId, string datasetPath, int recordLength, out string[] datasetOut)
    {
      datasetOut = null;
      List<string> dataset = new List<string>();

      hid_t dataSetId = 0;
      hid_t dataSpaceId = 0;
      hid_t typeId = 0;

      try
      {
        dataSetId = H5D.open(fileId, datasetPath);
        dataSpaceId = H5D.get_space(dataSetId);
        typeId = H5T.copy(H5T.C_S1);
        H5T.set_size(typeId, new IntPtr(recordLength));

        int rank = H5S.get_simple_extent_ndims(dataSpaceId);
        ulong[] dims = new ulong[rank];
        ulong[] maxDims = new ulong[rank];
        H5S.get_simple_extent_dims(dataSpaceId, dims, maxDims);
        byte[] dataBytes = new byte[dims[0] * (ulong)recordLength];

        GCHandle pinnedArray = GCHandle.Alloc(dataBytes, GCHandleType.Pinned);
        H5D.read(dataSetId, typeId, H5S.ALL, H5S.ALL, H5P.DEFAULT, pinnedArray.AddrOfPinnedObject());
        pinnedArray.Free();

        for (int i = 0; i < (int)(dims[0]); i++)
        {
          byte[] slice = dataBytes.Skip<byte>(i * recordLength).Take<byte>(recordLength).ToArray<byte>();
          var content = System.Text.Encoding.ASCII.GetString(slice).Trim();
          dataset.Add(content);
        }
      }
      catch (Exception ex)
      {
        return false;
      }
      finally
      {
        if (typeId != 0) H5T.close(typeId);
        if (dataSpaceId != 0) H5S.close(dataSpaceId);
        if (dataSetId != 0) H5D.close(dataSetId);
      }

      datasetOut = dataset.ToArray();

      return true;
    }