HDF5Library.cpp 4.23 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

bool UHDF5Library::ImportData(FString FileName, TArray<float>& X, TArray<float>& Y, TArray<float>& Z, TArray<float>& U, TArray<float>& V, TArray<float>& W)
75 76 77 78 79
{
	return false;
}

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

	/*
	 * Open the file and group
	 */
90 91 92

	FString relpath = FPaths::ProjectDir();

93 94 95 96 97 98 99
	//TCHAR *dirname = TEXT("Resources");
	//relpath.PathAppend(dirname, _tcslen(dirname));

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

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

101
	relpath += FileName;
102 103 104 105

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

106 107 108
	H5File File = H5File(pathAsCharArray, H5F_ACC_RDONLY);
	
	Group Root = File.openGroup("/");
Jan Kremer's avatar
Jan Kremer committed
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	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.)
135
	DataSet dataset = getDatasetByName(group, "X");
Jan Kremer's avatar
Jan Kremer committed
136 137 138 139 140 141 142
	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?

143
	// Output buffer initialization
Jan Kremer's avatar
Jan Kremer committed
144 145
	float* data_out = new float[dimension];

146
	// Read datasets
Jan Kremer's avatar
Jan Kremer committed
147 148 149
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	X = TArray<float>(data_out, dims_out[0]);

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

154
	dataset = getDatasetByName(group, "Z");
Jan Kremer's avatar
Jan Kremer committed
155 156 157
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	Z = TArray<float>(data_out, dims_out[0]);

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

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

166
	dataset = getDatasetByName(group, "W");
167 168 169
	dataset.read(data_out, PredType::NATIVE_FLOAT);
	W = TArray<float>(data_out, dims_out[0]);

Jan Kremer's avatar
Jan Kremer committed
170 171 172
	delete[] data_out;

	return true;
173
}