HDF5Library.cpp 4.05 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
using namespace H5;

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
DataSet UHDF5Library::getDatasetByName(Group & group, char * name)
{
	DataSet dataset;
	try {
		dataset = group.openDataSet(name);
	}
	catch (...)
	{
		FString adjustedName = FString(name);
		if (islower(*name))
		{
			adjustedName = adjustedName.ToUpper();
		}
		else
		{
			adjustedName = adjustedName.ToLower();
		}
		char* adjustedNameAsCharArray = TCHAR_TO_ANSI(*adjustedName);

		dataset = group.openDataSet(adjustedNameAsCharArray);
	}
	return dataset;
}

32 33 34 35 36 37 38 39 40
Group UHDF5Library::getGroupByName(H5File & file, FString name)
{
	char* NameAsCharArray = NULL;
	NameAsCharArray = TCHAR_TO_ANSI(*name);
	Group group = file.openGroup(NameAsCharArray);
	return group;
}

/*
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 UHDF5Library::getGroupByName(H5File & file, FString name)
{
	for (int i = 0; i < 1000; ++i)
	{
		Group group;
		FString Number;
		FString Cat;
		char* CatAsCharArray = NULL;
		try {
			Number = FString::FromInt(i);
			Cat = name + Number;
			CatAsCharArray = TCHAR_TO_ANSI(*Cat);
			group = file.openGroup(CatAsCharArray);
			return group;
		}
		catch (...)
		{
			FString adjustedName = FString(CatAsCharArray).ToLower();
			char* adjustedNameAsCharArray = TCHAR_TO_ANSI(*adjustedName);

			try {
				group = file.openGroup(adjustedNameAsCharArray);
				return group;
			}
			catch (...)
			{
			}
		}
	}

	return Group();
72
}*/
73

74
H5File UHDF5Library::LoadFile(FString FileName, TArray<FString>& GroupNames)
Jan Kremer's avatar
Jan Kremer committed
75 76 77 78 79 80 81 82 83 84
{
	/*
	* Turn off the auto-printing when failure occurs so that we can
	* handle the errors appropriately
	*/
	Exception::dontPrint();

	/*
	 * Open the file and group
	 */
85 86 87

	FString relpath = FPaths::ProjectDir();

88 89 90 91 92 93 94
	//TCHAR *dirname = TEXT("Resources");
	//relpath.PathAppend(dirname, _tcslen(dirname));

	relpath += FString("/Resources/");

	//TCHAR *filename = TEXT("abc64x64x64.h5part");
	//relpath.PathAppend(filename, _tcslen(filename));
95

96
	relpath += FileName;
97 98 99 100

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

101 102 103
	H5File File = H5File(pathAsCharArray, H5F_ACC_RDONLY);
	
	Group Root = File.openGroup("/");
Jan Kremer's avatar
Jan Kremer committed
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	GroupNames = TArray<FString>();

	for (int i = 0; i < Root.getNumObjs(); i++) {
		std::string GroupName;

		switch (Root.getObjTypeByIdx((size_t)i))
		{
		case H5G_GROUP:
			GroupName = Root.getObjnameByIdx((size_t)i);
			GroupNames.Emplace(FString(GroupName.c_str()));
			break;
		default:
			break;
		}

	}

	return File;
}

bool UHDF5Library::LoadGroup(H5File File, FString GroupName, TArray<float>& X, TArray<float>& Y, TArray<float>& Z, TArray<float>& U, TArray<float>& V, TArray<float>& W)
{
	Group group = getGroupByName(File, GroupName);

	// Get dataspace of one dataset to get the dimension. (Dimensions must be equal for all sets.)
130
	DataSet dataset = getDatasetByName(group, "X");
Jan Kremer's avatar
Jan Kremer committed
131 132 133 134 135 136 137
	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?

138
	// Output buffer initialization
Jan Kremer's avatar
Jan Kremer committed
139 140
	float* data_out = new float[dimension];

141
	// Read datasets
Jan Kremer's avatar
Jan Kremer committed
142 143 144
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	X = TArray<float>(data_out, dims_out[0]);

145
	dataset = getDatasetByName(group, "Y");
Jan Kremer's avatar
Jan Kremer committed
146 147 148
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	Y = TArray<float>(data_out, dims_out[0]);

149
	dataset = getDatasetByName(group, "Z");
Jan Kremer's avatar
Jan Kremer committed
150 151 152
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	Z = TArray<float>(data_out, dims_out[0]);

153
	dataset = getDatasetByName(group, "U");
154 155 156
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	U = TArray<float>(data_out, dims_out[0]);

157
	dataset = getDatasetByName(group, "V");
158 159 160
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	V = TArray<float>(data_out, dims_out[0]);

161
	dataset = getDatasetByName(group, "W");
162 163 164
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	W = TArray<float>(data_out, dims_out[0]);

Jan Kremer's avatar
Jan Kremer committed
165 166 167
	delete[] data_out;

	return true;
168
}