/// <summary> /// 写数据集属性 /// </summary> public void WriteDatasetAttribute(string datasetName, string attrName, string value) { H5DataSetId datasetId = H5D.open(_fileId, datasetName); H5DataTypeId typeId = H5T.copy(H5T.H5Type.C_S1); H5DataSpaceId spaceId = H5S.create(H5S.H5SClass.SCALAR); H5T.setSize(typeId, value.Length); H5AttributeId attrId = H5A.create(datasetId, attrName, typeId, spaceId); if (value != "") { H5Array <byte> buffer = new H5Array <byte>(Encoding.Default.GetBytes(value)); H5A.write(attrId, typeId, buffer); } if (typeId != null) { H5T.close(typeId); } if (spaceId != null) { H5S.close(spaceId); } if (attrId != null) { H5A.close(attrId); } if (datasetId != null) { H5D.close(datasetId); } }
private bool setOMXFileAttributes() { // write OMX attributes H5DataSpaceId dspace; H5DataTypeId dtype; H5AttributeId attr; // OMX version dspace = H5S.create(H5S.H5SClass.SCALAR); dtype = H5T.copy(H5T.H5Type.C_S1); // string datatype H5T.setSize(dtype, dllVersion[0].Length); attr = H5A.create(fileId, omxVersionName, dtype, dspace); ASCIIEncoding ascii = new ASCIIEncoding(); H5A.write(attr, dtype, new H5Array <System.Byte>(ascii.GetBytes(dllVersion[0]))); H5A.close(attr); // OMX shape - only 2D tables dspace = H5S.create_simple(1, new long[] { 2 }); dtype = H5T.copy(H5T.H5Type.NATIVE_INT); attr = H5A.create(fileId, omxShapeAttr, dtype, dspace); int[] shape = new int[2]; shape[0] = (int)Shape[0]; shape[1] = (int)Shape[1]; H5A.write <int>(attr, dtype, new H5Array <int>(shape)); H5S.close(dspace); H5A.close(attr); return(true); }
private static void WriteFile(string filePath) { var file = H5F.create(filePath, H5F.CreateMode.ACC_TRUNC); var group = H5G.create(file, "/group"); H5G.close(group); const int RANK = 2; const int DIM0 = 3; const int DIM1 = 4; var dims = new long[RANK] { DIM0, DIM1 }; var dataSpace = H5S.create_simple(RANK, dims); var dataSet = H5D.create(file, "/group/dataset", H5T.H5Type.NATIVE_INT, dataSpace); H5S.close(dataSpace); var data = new int[DIM0, DIM1] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; H5D.write(dataSet, new H5DataTypeId(H5T.H5Type.NATIVE_INT), new H5Array <int>(data)); var dataType = new H5DataTypeId(H5T.H5Type.NATIVE_INT); dataSpace = H5S.create(H5S.H5SClass.SCALAR); var integerAttribute = H5A.create(dataSet, "int", dataType, dataSpace); H5A.write(integerAttribute, dataType, new H5Array <int>(new int[1] { 42 })); H5A.close(integerAttribute); H5S.close(dataSpace); //H5T.close(dataType); // Read-only. var str = "Hello, world!"; var strBytes = Encoding.ASCII.GetBytes(str); // There is a H5T.get_cset, but there does not seem to be a way of setting the character encoding, i.e. set_cset. dataType = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(dataType, strBytes.Length); dataSpace = H5S.create(H5S.H5SClass.SCALAR); var stringAttribute = H5A.create(dataSet, "string", dataType, dataSpace); H5A.write(stringAttribute, dataType, new H5Array <byte>(strBytes)); H5A.close(stringAttribute); H5S.close(dataSpace); H5T.close(dataType); H5D.close(dataSet); H5F.close(file); }
private static byte[] EncodeStringData(string str, out H5DataTypeId dtype) { byte[] strdata = System.Text.Encoding.UTF8.GetBytes(str); dtype = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(dtype, strdata.Length); return(strdata); }
private static H5AttributeId WriteStringAttribute(H5ObjectWithAttributes fileOrdatasetId, HDFAttributeDef hDFAttributeDef, H5DataSpaceId dataSpaceId, H5DataTypeId dataTypeId) { string attValue = Convert.ToString(hDFAttributeDef.Value); int stringLength = attValue.Length; H5T.setSize(dataTypeId, stringLength); H5AttributeId attributeId = H5A.create(fileOrdatasetId, hDFAttributeDef.Name, dataTypeId, dataSpaceId); byte[] bs = Encoding.Default.GetBytes(attValue); H5A.write(attributeId, dataTypeId, new H5Array <byte>(bs)); return(attributeId); }
/// <summary> /// Constructs a new EpochHDF5Persistor with an HDF5 file at the given path. /// </summary> /// <param name="filename">Desired HDF5 path</param> /// <param name="assocFilePrefix">Prefix for auxiliary (e.g. image) file associated with this HDF5 file</param> /// <param name="guidGenerator">Function for generating new UUIDs (e.g. Guid.NewGuid)</param> /// <param name="compression">Automatically numeric data compression (0 = none, 9 = maximum)</param> public EpochHDF5Persistor(string filename, string assocFilePrefix, Func <Guid> guidGenerator, uint compression = 9) : base(guidGenerator) { if (filename == null) { throw new ArgumentException("File name must not be null", "filename"); } if (compression > 9) { throw new ArgumentException("Compression must be 0-9", "compression"); } if (assocFilePrefix == null) { assocFilePrefix = ""; } this.AssociatedFilePrefix = assocFilePrefix; NumericDataCompression = compression; EpochGroupsIDs = new Stack <EpochGroupIDs>(); var fInfo = new FileInfo(filename); string prefixedFilePath = fInfo.DirectoryName + Path.DirectorySeparatorChar + this.AssociatedFilePrefix + fInfo.Name; var currentFile = new FileInfo(prefixedFilePath); if (currentFile.Exists) { fileId = H5F.open(prefixedFilePath, H5F.OpenMode.ACC_RDWR); string_t = H5T.open(fileId, "STRING40"); keyval_t = H5T.open(fileId, "KEY40VAR40"); measurement_t = H5T.open(fileId, "MEASUREMENT"); extdevmeasurement_t = H5T.open(fileId, "EXTDEV_MEASUREMENT"); //TODO Check persistence version } else { fileId = H5F.create(prefixedFilePath, H5F.CreateMode.ACC_EXCL); WriteAttribute(fileId, "version", Version); // Create our standard String type (string of length FIXED_STRING_LENGTH characters) string_t = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(string_t, 40); H5T.commit(fileId, "STRING40", string_t); // Create our key/value compound type (two strings of length 40 characters) keyval_t = H5T.create(H5T.CreateClass.COMPOUND, 80); H5T.insert(keyval_t, "key", 0, string_t); H5T.insert(keyval_t, "value", FIXED_STRING_LENGTH, string_t); H5T.commit(fileId, "KEY40VAR40", keyval_t); // Create the Measurement compound type measurement_t = H5T.create(H5T.CreateClass.COMPOUND, 48); // confirm 48 is enough/too much/whatever H5T.insert(measurement_t, "quantity", 0, H5T.H5Type.NATIVE_DOUBLE); H5T.insert(measurement_t, "unit", H5T.getSize(H5T.H5Type.NATIVE_DOUBLE), string_t); H5T.commit(fileId, "MEASUREMENT", measurement_t); // Create the ExtDev/Measurement compound type extdevmeasurement_t = H5T.create(H5T.CreateClass.COMPOUND, H5T.getSize(string_t) + 2 * H5T.getSize(measurement_t)); H5T.insert(extdevmeasurement_t, "externalDevice", 0, string_t); H5T.insert(extdevmeasurement_t, "measurement", H5T.getSize(string_t), measurement_t); H5T.commit(fileId, "EXTDEV_MEASUREMENT", extdevmeasurement_t); } Interlocked.Increment(ref _openHdf5FileCount); }
static void test_classes() { try { Console.Write("Testing datatype classes"); /*------------------------------------------------------------- * Check class of some atomic types. *-----------------------------------------------------------*/ H5T.H5TClass tcls = H5T.getClass(H5T.H5Type.NATIVE_INT); if (tcls != H5T.H5TClass.INTEGER) { Console.WriteLine("Test class should have been H5T_INTEGER"); nerrors++; } tcls = H5T.getClass(H5T.H5Type.NATIVE_DOUBLE); if (tcls != H5T.H5TClass.FLOAT) { Console.WriteLine("Test class should have been H5T_FLOAT"); nerrors++; } H5DataTypeId op_type = H5T.create(H5T.CreateClass.OPAQUE, 1); tcls = H5T.getClass(op_type); if (tcls != H5T.H5TClass.OPAQUE) { Console.WriteLine("Test class should have been H5T_OPAQUE"); nerrors++; } // Create a VL datatype of char. It should be a VL, not a string class. H5DataTypeId vlcId = H5T.vlenCreate(H5T.H5Type.NATIVE_UCHAR); // Make certain that the correct classes can be detected tcls = H5T.getClass(vlcId); if (tcls != H5T.H5TClass.VLEN) { Console.WriteLine("Test class should have been H5T_VLEN"); nerrors++; } // Make certain that an incorrect class is not detected */ if (tcls == H5T.H5TClass.STRING) { Console.WriteLine("Test class should not be H5T_STRING"); nerrors++; } // Create a VL string. It should be a string, not a VL class. H5DataTypeId vlsId = H5T.copy(H5T.H5Type.C_S1); H5T.setSize(vlsId, -1); // for H5T_VARIABLE tcls = H5T.getClass(vlsId); if (tcls != H5T.H5TClass.STRING) { Console.WriteLine("Test class should have been H5T_STRING"); nerrors++; } if (tcls == H5T.H5TClass.VLEN) { Console.WriteLine("Test class should not be H5T_VLEN"); nerrors++; } // Check that this is a variable-length string. if (!H5T.isVariableString(vlsId)) { Console.WriteLine("This type should be a variable-length string"); nerrors++; } // Close datatype H5T.close(vlcId); Console.WriteLine("\t\t\t\tPASSED"); } // end of try block catch (HDFException anyHDF5E) { Console.WriteLine(anyHDF5E.Message); nerrors++; } catch (System.Exception sysE) { Console.WriteLine(sysE.TargetSite); Console.WriteLine(sysE.Message); nerrors++; } } // end of test_classes
public string Get(string path, bool outfolder) { logger.Log(LogLevel.Info, "Entered AVISTEDHDF5Converter GET()"); try { string content = File.ReadAllText(path); List <Dictionary <string, string> > data = JsonConvert.DeserializeObject <List <Dictionary <string, string> > >(content); string result = "false"; string randomlyGeneratedFolderNamePart = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); string timeRelatedFolderNamePart = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); string processRelatedFolderNamePart = System.Diagnostics.Process.GetCurrentProcess().Id.ToString(); string copypath = ""; if (outfolder) { copypath = ConfigurationManager.AppSettings["Save_Downloads"].ToString(); } else { copypath = ConfigurationManager.AppSettings["Converters"].ToString(); } string temporaryDirectoryName = Path.Combine(copypath , timeRelatedFolderNamePart + processRelatedFolderNamePart + randomlyGeneratedFolderNamePart); Directory.CreateDirectory(temporaryDirectoryName); logger.Log(LogLevel.Info, "Created Directory"); string uri = Path.Combine(temporaryDirectoryName, "result" + ".h5"); H5FileId fileId = H5F.create(uri, H5F.CreateMode.ACC_TRUNC); string[] results = new string[data.Count + 1]; int i = 0, j = 0; Dictionary <string, string> resultdict = new Dictionary <string, string>(); Dictionary <string, string> tempdict = data.First(); string[] names = tempdict.Keys.ToArray(); string[] values = new string[names.Length]; foreach (Dictionary <string, string> dict in data) { var value = dict.Values.ToArray(); if (j == 0) { for (int k = 0; k < values.Length; k++) { values[k] = value[k]; } j = 1; } else { for (int k = 0; k < values.Length; k++) { values[k] += "," + value[k]; } } } int index = 0; foreach (string s in names) { if (s.Equals("date")) { string[] strings = values[index++].Split(','); byte[] bytes = Encoding.UTF8.GetBytes(String.Concat(strings)); char[,] myChars = new char[strings.Length, 10]; myChars = StringToChar(myChars, strings); // Prepare to 9create a data space for writing a 1 - dimensional // signed integer array. long[] dims = new long[1]; dims[0] = strings.Length; H5DataSpaceId spaceId = H5S.create_simple(1, dims); H5DataTypeId typeId = H5T.copy(H5T.H5Type.C_S1); // Find the size of the type int typeSize = H5T.getSize(typeId) * 10; H5T.setSize(typeId, 10); string name = "/" + s; // Create the data set. H5DataSetId dataSetId = H5D.create(fileId, s, typeId, spaceId); H5D.write(dataSetId, typeId, new H5Array <byte>(bytes)); H5D.close(dataSetId); H5S.close(spaceId); logger.Log(LogLevel.Info, "Created parameter {0}", s); } else { string[] strings = values[index++].Split(','); float[] vl = new float[strings.Length]; int l = 0; foreach (string d in strings) { vl[l++] = float.Parse(d); } // Prepare to create a data space for writing a 1 - dimensional // signed integer array. long[] dims = new long[1]; dims[0] = strings.Length; H5DataSpaceId spaceId = H5S.create_simple(1, dims); H5DataTypeId typeId1 = H5T.copy(H5T.H5Type.NATIVE_FLOAT); // Find the size of the type int typeSize = H5T.getSize(typeId1); // Set the order to big endian H5T.setOrder(typeId1, H5T.Order.BE); // Set the order to little endian H5T.setOrder(typeId1, H5T.Order.LE); string name = "/" + s; // Create the data set. H5DataSetId dataSetId = H5D.create(fileId, s, typeId1, spaceId); H5D.write(dataSetId, new H5DataTypeId(H5T.H5Type.NATIVE_FLOAT), new H5Array <float>(vl)); // dscopy.AddVariable<float>(s, vl); H5D.close(dataSetId); H5S.close(spaceId); H5T.close(typeId1); logger.Log(LogLevel.Info, "Created parameter {0}", s); } } H5F.close(fileId); string SourceFolderPath = temporaryDirectoryName; return(SourceFolderPath); } catch (Exception ex) { logger.Error("AVISTEDHDF5Converter:Failed with exception {0}", ex.Message); } return("Error"); }