HDF5Library.cpp 2.47 KB
Newer Older
Jan Kremer's avatar
Jan Kremer committed
1 2 3
// Fill out your copyright notice in the Description page of Project Settings.

#include "HDF5Library.h"
4 5
#include "Runtime/Core/Public/Misc/Paths.h"
#include "Runtime/Core/Public/GenericPlatform/GenericPlatformFile.h"
Jan Kremer's avatar
Jan Kremer committed
6 7 8
#include "H5Cpp.h"
using namespace H5;

Jan Kremer's avatar
Jan Kremer committed
9
bool UHDF5Library::ImportData(TArray<float>& X, TArray<float>& Y, TArray<float>& Z, TArray<float>& U, TArray<float>& V, TArray<float>& W)
Jan Kremer's avatar
Jan Kremer committed
10 11 12 13 14 15 16 17 18 19
{
	/*
	* Turn off the auto-printing when failure occurs so that we can
	* handle the errors appropriately
	*/
	Exception::dontPrint();

	/*
	 * Open the file and group
	 */
20 21 22 23 24 25 26 27 28 29 30 31 32

	FString relpath = FPaths::ProjectDir();

	TCHAR *dirname = TEXT("Resources");
	relpath.PathAppend(dirname, _tcslen(dirname));

	TCHAR *filename = TEXT("data.h5part");
	relpath.PathAppend(filename, _tcslen(filename));

	FString abspath = IFileManager::Get().ConvertToAbsolutePathForExternalAppForRead(*relpath);
	char* pathAsCharArray = TCHAR_TO_ANSI(*abspath);

	H5File file(pathAsCharArray, H5F_ACC_RDONLY);
Jan Kremer's avatar
Jan Kremer committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	Group group = file.openGroup("Step#150");

	/*
	* Get dataspace of one dataset to get the dimension. (Dimensions must be equal for all sets.)
	*/
	DataSet dataset = group.openDataSet("X"); // TODO if "X" does not exist, search for "x". do the same for the other 5 dimensions.
	DataSpace dataspace = dataset.getSpace();
	int rank = dataspace.getSimpleExtentNdims();
	hsize_t dims_out[1];
	int ndims = dataspace.getSimpleExtentDims(dims_out, NULL);
	int32 dimension = dims_out[0];
	dataspace.selectAll(); // needed?

	/*
	* Output buffer initialization.
	*/
	//float data_out[262144]; /* output buffer */
	float* data_out = new float[dimension];

	/*
	 * Read X
	 */
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	X = TArray<float>(data_out, dims_out[0]);

	/*
	 * Read Y
	 */
	dataset = group.openDataSet("Y");
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	Y = TArray<float>(data_out, dims_out[0]);

	/*
	 * Read Z
	 */
	dataset = group.openDataSet("Z");
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	Z = TArray<float>(data_out, dims_out[0]);

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	/*
	 * Read U
	 */
	dataset = group.openDataSet("u");
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	U = TArray<float>(data_out, dims_out[0]);

	/*
	 * Read V
	 */
	dataset = group.openDataSet("v");
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	V = TArray<float>(data_out, dims_out[0]);

	/*
	 * Read W
	 */
	dataset = group.openDataSet("w");
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	W = TArray<float>(data_out, dims_out[0]);

Jan Kremer's avatar
Jan Kremer committed
93 94 95 96 97
	delete[] data_out;

	// success
	return true;
}