PageRenderTime 70ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/MAD/Classes/Hdfoutput.cs

http://mad.codeplex.com
C# | 315 lines | 221 code | 84 blank | 10 comment | 38 complexity | 420c5046f6aa2bf3840fcfd8b6f40afc MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using HDF5DotNet;
  6. using System.IO;
  7. namespace MAD
  8. {
  9. public enum TypeHDFMad{
  10. Matrix,
  11. Vector
  12. }
  13. public class Hdfoutput
  14. {
  15. private H5DataTypeId dataTypeId;
  16. private H5DataSpaceId space;
  17. private H5FileId file_id;
  18. private string nameFile;
  19. private int numberOfRecords;
  20. private int rows;
  21. private int cols;
  22. private TypeHDFMad typeHdf;
  23. public Hdfoutput(string nameFile, int numberOfRecords)
  24. {
  25. this.numberOfRecords = numberOfRecords;
  26. this.nameFile = nameFile;
  27. typeHdf = TypeHDFMad.Vector;
  28. }
  29. public Hdfoutput(string nameFile, int rows, int cols)
  30. {
  31. this.rows = rows;
  32. this.cols = cols;
  33. this.nameFile = nameFile;
  34. this.typeHdf = TypeHDFMad.Matrix;
  35. }
  36. public void Close()
  37. {
  38. if ( space != null)
  39. H5S.close(space);
  40. if (file_id.Id != -1 )
  41. H5F.close (file_id);
  42. }
  43. public bool Load()
  44. {
  45. if (File.Exists(nameFile))
  46. {
  47. file_id = H5F.open(nameFile, H5F.OpenMode.ACC_RDONLY);
  48. return true;
  49. }
  50. else
  51. {
  52. return false;
  53. }
  54. }
  55. public float[][] GetSamplesArray(int realization, int[] steps, int numSamples,int numRealizations)
  56. {
  57. if (typeHdf != TypeHDFMad.Vector) return null;
  58. float[][] data = new float[numSamples][];
  59. for (int i = 1; i <= numSamples; i++)
  60. {
  61. float[] temp = new float[steps.Sum()];
  62. H5DataSetId dataset_id = H5D.open(file_id, "sample" +i.ToString());
  63. H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
  64. int reaPos = Utils.CalculatePositionArray(1, 1, realization,numRealizations, steps);
  65. //Define file dataspace
  66. long[] start = new long[] { reaPos,0 };
  67. long[] sizeBlock = new long[] { steps.Sum(), 1 };
  68. H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
  69. //Define memory dataspace
  70. H5DataSpaceId memspace_id = H5S.create_simple( 1, new long[] { steps.Sum() });
  71. H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] { 0 }, new long[] { steps.Sum() });
  72. H5DataTypeId datatype_id = H5D.getType(dataset_id);
  73. var clas = H5T.getClass(datatype_id);
  74. H5Array<float> read = new H5Array<float>(temp);
  75. H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
  76. H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
  77. H5S.close(dataspace_id);
  78. H5S.close(memspace_id);
  79. H5D.close(dataset_id);
  80. data[i - 1] = temp;
  81. }
  82. return data;
  83. }
  84. public float[,] GetRealizationsArray3(int sample, int[] steps, int numRealizations)
  85. {
  86. // if (typeHdf != TypeHDFMad.Linear) return null;
  87. float[,] data = new float[numRealizations, steps.Sum()];
  88. H5DataSetId dataset_id = H5D.open(file_id, "sample" + sample.ToString());
  89. long[] start = new long[] { 0, 0 };
  90. long[] sizeBlock = new long[] { numRealizations, steps.Sum() };
  91. H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
  92. H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
  93. //Define memory dataspace
  94. H5DataSpaceId memspace_id = H5S.create_simple(2, new long[] { numRealizations, steps.Sum() });
  95. H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] {0, 0 }, new long[] { numRealizations, steps.Sum() });
  96. H5DataTypeId datatype_id = H5D.getType(dataset_id);
  97. var clas = H5T.getClass(datatype_id);
  98. H5Array<float> read = new H5Array<float>(data);
  99. H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
  100. H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
  101. H5S.close(dataspace_id);
  102. H5S.close(memspace_id);
  103. H5D.close(dataset_id);
  104. return data;
  105. }
  106. public float[,] GetRealizationsArray2(int sample, int[] steps, int numRealizations)
  107. {
  108. // if (typeHdf != TypeHDFMad.Linear) return null;
  109. float[,] data = new float[numRealizations,steps.Sum()];
  110. H5DataSetId dataset_id = H5D.open(file_id, "sample" + sample.ToString());
  111. for (int realization = 1; realization <= numRealizations; realization++)
  112. {
  113. float[] temp = new float[steps.Sum()];
  114. H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
  115. int reaPos = Utils.CalculatePositionArray(1, 1, realization, numRealizations, steps);
  116. //Define file dataspace
  117. long[] start = new long[] { reaPos, 0 };
  118. long[] sizeBlock = new long[] { steps.Sum(), 1 };
  119. H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
  120. //Define memory dataspace
  121. H5DataSpaceId memspace_id = H5S.create_simple(1, new long[] { steps.Sum() });
  122. H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] { 0 }, new long[] { steps.Sum() });
  123. H5DataTypeId datatype_id = H5D.getType(dataset_id);
  124. var clas = H5T.getClass(datatype_id);
  125. H5Array<float> read = new H5Array<float>(temp);
  126. H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
  127. H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
  128. H5S.close(dataspace_id);
  129. H5S.close(memspace_id);
  130. for (int i = 0; i < steps.Sum(); i++)
  131. {
  132. data[realization - 1, i] = temp[i];
  133. }
  134. }
  135. H5D.close(dataset_id);
  136. return data;
  137. }
  138. public float[][] GetRealizationsArray(int sample, int[] steps, int numRealizations)
  139. {
  140. if (typeHdf != TypeHDFMad.Vector) return null;
  141. float[][] data = new float[numRealizations][];
  142. H5DataSetId dataset_id = H5D.open(file_id, "sample" + sample.ToString());
  143. for (int realization = 1; realization <= numRealizations; realization++)
  144. {
  145. float[] temp = new float[steps.Sum()];
  146. H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
  147. int reaPos = Utils.CalculatePositionArray(1, 1, realization, numRealizations, steps);
  148. //Define file dataspace
  149. long[] start = new long[] { reaPos,0 };
  150. long[] sizeBlock = new long[] { steps.Sum(), 1 };
  151. H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
  152. //Define memory dataspace
  153. H5DataSpaceId memspace_id = H5S.create_simple(1, new long[] { steps.Sum() });
  154. H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] { 0 }, new long[] { steps.Sum() });
  155. H5DataTypeId datatype_id = H5D.getType(dataset_id);
  156. var clas = H5T.getClass(datatype_id);
  157. H5Array<float> read = new H5Array<float>(temp);
  158. H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
  159. H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
  160. H5S.close(dataspace_id);
  161. H5S.close(memspace_id);
  162. data[realization - 1] = temp;
  163. }
  164. H5D.close(dataset_id);
  165. return data;
  166. }
  167. public void Activate( )
  168. {
  169. // dataTypeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
  170. dataTypeId = H5T.getNativeType(H5T.H5Type.NATIVE_FLOAT, H5T.Direction.DEFAULT);
  171. if (this.typeHdf == TypeHDFMad.Vector)
  172. {
  173. int size = this.numberOfRecords;
  174. space = H5S.create_simple(1, new long[1] { size }, new long[1] { size });
  175. }
  176. else
  177. {
  178. space = H5S.create_simple(2, new long[2] { this.rows, this.cols }, new long[2] { this.rows, this.cols });
  179. }
  180. if (File.Exists(nameFile))
  181. file_id = H5F.open(nameFile, H5F.OpenMode.ACC_RDWR);
  182. else
  183. file_id = H5F.create(nameFile, H5F.CreateMode.ACC_TRUNC);
  184. }
  185. private void AddDataSetHDF5( int j, float[,] data1, string name)
  186. {
  187. H5DataSetId dataset_id = H5D.create(file_id, name + j, H5T.H5Type.NATIVE_FLOAT, space);
  188. H5Array<float> array = new H5Array<float>(data1);
  189. H5D.write(dataset_id, dataTypeId, array);
  190. H5D.close(dataset_id);
  191. }
  192. public void Add(int sampleId, float[,] data, string name)
  193. {
  194. if (file_id.Id != -1 && dataTypeId.Id != -1 && space.Id != -1 )
  195. {
  196. AddDataSetHDF5(sampleId,data,name);
  197. }
  198. }
  199. public void AddMatrix(float[,] data, string name)
  200. {
  201. if (this.typeHdf != TypeHDFMad.Matrix) return;
  202. if (file_id.Id != -1 && dataTypeId.Id != -1 && space.Id != -1)
  203. {
  204. H5DataSetId dataset_id = H5D.create(file_id, name , H5T.H5Type.NATIVE_FLOAT, space);
  205. H5Array<float> array = new H5Array<float>(data);
  206. H5D.write(dataset_id, dataTypeId, array);
  207. H5D.close(dataset_id);
  208. }
  209. }
  210. public void AddVector(float[] data, string name)
  211. {
  212. if (this.typeHdf != TypeHDFMad.Vector) return;
  213. if (file_id.Id != -1 && dataTypeId.Id != -1 && space.Id != -1)
  214. {
  215. H5DataSetId dataset_id = H5D.create(file_id, name, H5T.H5Type.NATIVE_FLOAT, space);
  216. H5Array<float> array = new H5Array<float>(data);
  217. H5D.write(dataset_id, dataTypeId, array);
  218. H5D.close(dataset_id);
  219. }
  220. }
  221. }
  222. public struct DataBlockII
  223. {
  224. public float value;
  225. public int sample ;
  226. public int realization;
  227. public int layer;
  228. public int step;
  229. public int period;
  230. }
  231. }