/MAD/Classes/Hdfoutput.cs
C# | 315 lines | 221 code | 84 blank | 10 comment | 38 complexity | 420c5046f6aa2bf3840fcfd8b6f40afc MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using HDF5DotNet;
- using System.IO;
- namespace MAD
- {
- public enum TypeHDFMad{
- Matrix,
- Vector
- }
-
- public class Hdfoutput
- {
- private H5DataTypeId dataTypeId;
- private H5DataSpaceId space;
- private H5FileId file_id;
- private string nameFile;
- private int numberOfRecords;
- private int rows;
- private int cols;
- private TypeHDFMad typeHdf;
-
- public Hdfoutput(string nameFile, int numberOfRecords)
- {
- this.numberOfRecords = numberOfRecords;
- this.nameFile = nameFile;
- typeHdf = TypeHDFMad.Vector;
-
- }
-
- public Hdfoutput(string nameFile, int rows, int cols)
- {
- this.rows = rows;
- this.cols = cols;
- this.nameFile = nameFile;
- this.typeHdf = TypeHDFMad.Matrix;
-
- }
-
-
- public void Close()
- {
-
- if ( space != null)
- H5S.close(space);
-
- if (file_id.Id != -1 )
- H5F.close (file_id);
-
-
-
- }
-
-
-
- public bool Load()
- {
-
- if (File.Exists(nameFile))
- {
- file_id = H5F.open(nameFile, H5F.OpenMode.ACC_RDONLY);
- return true;
- }
- else
- {
- return false;
- }
- }
-
- public float[][] GetSamplesArray(int realization, int[] steps, int numSamples,int numRealizations)
- {
- if (typeHdf != TypeHDFMad.Vector) return null;
-
- float[][] data = new float[numSamples][];
-
- for (int i = 1; i <= numSamples; i++)
- {
- float[] temp = new float[steps.Sum()];
- H5DataSetId dataset_id = H5D.open(file_id, "sample" +i.ToString());
- H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
-
- int reaPos = Utils.CalculatePositionArray(1, 1, realization,numRealizations, steps);
- //Define file dataspace
- long[] start = new long[] { reaPos,0 };
- long[] sizeBlock = new long[] { steps.Sum(), 1 };
- H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
-
- //Define memory dataspace
- H5DataSpaceId memspace_id = H5S.create_simple( 1, new long[] { steps.Sum() });
- H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] { 0 }, new long[] { steps.Sum() });
-
- H5DataTypeId datatype_id = H5D.getType(dataset_id);
- var clas = H5T.getClass(datatype_id);
-
- H5Array<float> read = new H5Array<float>(temp);
- H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
- H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
- H5S.close(dataspace_id);
- H5S.close(memspace_id);
- H5D.close(dataset_id);
-
- data[i - 1] = temp;
-
-
- }
-
- return data;
-
-
- }
-
- public float[,] GetRealizationsArray3(int sample, int[] steps, int numRealizations)
- {
- // if (typeHdf != TypeHDFMad.Linear) return null;
-
-
- float[,] data = new float[numRealizations, steps.Sum()];
- H5DataSetId dataset_id = H5D.open(file_id, "sample" + sample.ToString());
-
- long[] start = new long[] { 0, 0 };
- long[] sizeBlock = new long[] { numRealizations, steps.Sum() };
- H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
- H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
- //Define memory dataspace
- H5DataSpaceId memspace_id = H5S.create_simple(2, new long[] { numRealizations, steps.Sum() });
- H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] {0, 0 }, new long[] { numRealizations, steps.Sum() });
-
- H5DataTypeId datatype_id = H5D.getType(dataset_id);
- var clas = H5T.getClass(datatype_id);
-
- H5Array<float> read = new H5Array<float>(data);
- H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
- H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
- H5S.close(dataspace_id);
- H5S.close(memspace_id);
- H5D.close(dataset_id);
-
- return data;
- }
-
- public float[,] GetRealizationsArray2(int sample, int[] steps, int numRealizations)
- {
- // if (typeHdf != TypeHDFMad.Linear) return null;
-
-
- float[,] data = new float[numRealizations,steps.Sum()];
- H5DataSetId dataset_id = H5D.open(file_id, "sample" + sample.ToString());
-
- for (int realization = 1; realization <= numRealizations; realization++)
- {
- float[] temp = new float[steps.Sum()];
-
- H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
-
- int reaPos = Utils.CalculatePositionArray(1, 1, realization, numRealizations, steps);
- //Define file dataspace
- long[] start = new long[] { reaPos, 0 };
- long[] sizeBlock = new long[] { steps.Sum(), 1 };
- H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
-
- //Define memory dataspace
- H5DataSpaceId memspace_id = H5S.create_simple(1, new long[] { steps.Sum() });
- H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] { 0 }, new long[] { steps.Sum() });
-
- H5DataTypeId datatype_id = H5D.getType(dataset_id);
- var clas = H5T.getClass(datatype_id);
-
- H5Array<float> read = new H5Array<float>(temp);
- H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
- H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
- H5S.close(dataspace_id);
- H5S.close(memspace_id);
-
-
- for (int i = 0; i < steps.Sum(); i++)
- {
- data[realization - 1, i] = temp[i];
- }
-
-
- }
- H5D.close(dataset_id);
-
- return data;
- }
-
-
- public float[][] GetRealizationsArray(int sample, int[] steps, int numRealizations)
- {
- if (typeHdf != TypeHDFMad.Vector) return null;
-
-
- float[][] data = new float[numRealizations][];
- H5DataSetId dataset_id = H5D.open(file_id, "sample" + sample.ToString());
-
- for (int realization = 1; realization <= numRealizations; realization++)
- {
- float[] temp = new float[steps.Sum()];
-
- H5DataSpaceId dataspace_id = H5D.getSpace(dataset_id);
-
- int reaPos = Utils.CalculatePositionArray(1, 1, realization, numRealizations, steps);
- //Define file dataspace
- long[] start = new long[] { reaPos,0 };
- long[] sizeBlock = new long[] { steps.Sum(), 1 };
- H5S.selectHyperslab(dataspace_id, H5S.SelectOperator.SET, start, sizeBlock);
-
- //Define memory dataspace
- H5DataSpaceId memspace_id = H5S.create_simple(1, new long[] { steps.Sum() });
- H5S.selectHyperslab(memspace_id, H5S.SelectOperator.SET, new long[] { 0 }, new long[] { steps.Sum() });
-
- H5DataTypeId datatype_id = H5D.getType(dataset_id);
- var clas = H5T.getClass(datatype_id);
-
- H5Array<float> read = new H5Array<float>(temp);
- H5PropertyListId pro = new H5PropertyListId(H5P.Template.DEFAULT);
- H5D.read<float>(dataset_id, datatype_id, memspace_id, dataspace_id, pro, read);
- H5S.close(dataspace_id);
- H5S.close(memspace_id);
-
-
- data[realization - 1] = temp;
-
-
- }
- H5D.close(dataset_id);
-
- return data;
- }
-
- public void Activate( )
- {
- // dataTypeId = H5T.copy(H5T.H5Type.NATIVE_FLOAT);
- dataTypeId = H5T.getNativeType(H5T.H5Type.NATIVE_FLOAT, H5T.Direction.DEFAULT);
- if (this.typeHdf == TypeHDFMad.Vector)
- {
- int size = this.numberOfRecords;
- space = H5S.create_simple(1, new long[1] { size }, new long[1] { size });
- }
- else
- {
- space = H5S.create_simple(2, new long[2] { this.rows, this.cols }, new long[2] { this.rows, this.cols });
-
- }
- if (File.Exists(nameFile))
- file_id = H5F.open(nameFile, H5F.OpenMode.ACC_RDWR);
- else
- file_id = H5F.create(nameFile, H5F.CreateMode.ACC_TRUNC);
- }
-
-
-
-
- private void AddDataSetHDF5( int j, float[,] data1, string name)
- {
- H5DataSetId dataset_id = H5D.create(file_id, name + j, H5T.H5Type.NATIVE_FLOAT, space);
- H5Array<float> array = new H5Array<float>(data1);
- H5D.write(dataset_id, dataTypeId, array);
- H5D.close(dataset_id);
- }
-
-
- public void Add(int sampleId, float[,] data, string name)
- {
- if (file_id.Id != -1 && dataTypeId.Id != -1 && space.Id != -1 )
- {
- AddDataSetHDF5(sampleId,data,name);
- }
- }
-
- public void AddMatrix(float[,] data, string name)
- {
- if (this.typeHdf != TypeHDFMad.Matrix) return;
- if (file_id.Id != -1 && dataTypeId.Id != -1 && space.Id != -1)
- {
- H5DataSetId dataset_id = H5D.create(file_id, name , H5T.H5Type.NATIVE_FLOAT, space);
- H5Array<float> array = new H5Array<float>(data);
- H5D.write(dataset_id, dataTypeId, array);
- H5D.close(dataset_id);
- }
-
- }
-
- public void AddVector(float[] data, string name)
- {
- if (this.typeHdf != TypeHDFMad.Vector) return;
- if (file_id.Id != -1 && dataTypeId.Id != -1 && space.Id != -1)
- {
- H5DataSetId dataset_id = H5D.create(file_id, name, H5T.H5Type.NATIVE_FLOAT, space);
- H5Array<float> array = new H5Array<float>(data);
- H5D.write(dataset_id, dataTypeId, array);
- H5D.close(dataset_id);
- }
-
- }
-
-
-
-
-
- }
-
- public struct DataBlockII
- {
- public float value;
- public int sample ;
- public int realization;
- public int layer;
- public int step;
- public int period;
- }
-
- }