From cb788786043e8967c5de63a195b29c9cc14bc02f Mon Sep 17 00:00:00 2001 From: Thorsten Liebig Date: Mon, 30 Jul 2012 11:13:36 +0200 Subject: [PATCH] matlab: some fixes and updates handling hdf5 files --- matlab/ReadHDF5FieldData.m | 49 ++++++++++---------------------------- matlab/WriteHDF5.m | 17 +++++++------ 2 files changed, 21 insertions(+), 45 deletions(-) diff --git a/matlab/ReadHDF5FieldData.m b/matlab/ReadHDF5FieldData.m index 2d49c41..b10cadf 100644 --- a/matlab/ReadHDF5FieldData.m +++ b/matlab/ReadHDF5FieldData.m @@ -8,8 +8,7 @@ function hdf_fielddata = ReadHDF5FieldData(file) % hdf_fielddata.TD.values % % % frequency domain data (if exist) -% hdf_fielddata.FD.time -% hdf_fielddata.FD.names +% hdf_fielddata.FD.frequency % hdf_fielddata.FD.values % % example: values of timestep 12: @@ -32,7 +31,6 @@ end info = hdf5info(file); TD.names = {}; -FD.names = {}; hdf_fielddata = []; for n=1:numel(info.GroupHierarchy.Groups) @@ -49,16 +47,6 @@ for n=1:numel(info.GroupHierarchy.Groups) end end end - %search and read FD data - if strcmp(info.GroupHierarchy.Groups(n).Groups(nGroup).Name,'/FieldData/FD') - for m=1:numel(info.GroupHierarchy.Groups(n).Groups(nGroup).Datasets) - FD.names{m} = info.GroupHierarchy.Groups(n).Groups(nGroup).Datasets(m).Name; - for a = 1:numel(info.GroupHierarchy.Groups(n).Groups(nGroup).Datasets(m).Attributes) - str = regexp(info.GroupHierarchy.Groups(n).Groups(nGroup).Datasets(m).Attributes(a).Name,'\w/*\w*','match'); - FD.(str{end})(m) = double(info.GroupHierarchy.Groups(n).Groups(nGroup).Datasets(m).Attributes(a).Value); - end - end - end end end @@ -71,24 +59,15 @@ if (numel(TD.names)>0) end end -if (numel(FD.names)>0) - hdf_fielddata.FD=FD; - Nr_freq = numel(FD.names); - for n=1:Nr_freq - name = ['/FieldData/FD/f' int2str(n-1) '_real']; - ind = find(strcmp(FD.names,name)); - if isempty(ind) - ind = find(strcmp(FD.names,['/FieldData/FD/f' int2str(n-1)])); - if ~isempty(ind) - hdf_fielddata.FD.values{n} = double(hdf5read(file,FD.names{ind})); - end - else - hdf_fielddata.FD.values{n} = double(hdf5read(file,FD.names{ind})); - name = ['/FieldData/FD/f' int2str(n-1) '_imag']; - ind = find(strcmp(FD.names,name)); - hdf_fielddata.FD.values{n} = hdf_fielddata.FD.values{n} + 1j*double(hdf5read(file,FD.names{ind})); - end - end +% extract FD data +try + hdf_fielddata.FD.frequency = double(hdf5read(file,'/FieldData/FD','frequency')); +catch err + disp(err) + return +end +for n=1:numel(hdf_fielddata.FD.frequency) + hdf_fielddata.FD.values{n} = double(hdf5read(file,['/FieldData/FD/f' int2str(n-1) '_real']) + 1i*hdf5read(file,['/FieldData/FD/f' int2str(n-1) '_imag'])); end function hdf_fielddata = ReadHDF5FieldData_octave(file) @@ -107,10 +86,8 @@ if isfield(hdf.FieldData,'TD') end if isfield(hdf.FieldData,'FD') %read FD data - hdf_fielddata_names = fieldnames(hdf.FieldData.FD); - for n=1:numel(hdf_fielddata_names)/2 - hdf_fielddata.FD.values{n} = hdf.FieldData.FD.(hdf_fielddata_names{2*n}) + 1j*hdf.FieldData.FD.(hdf_fielddata_names{2*n-1}); - hdf_fielddata.FD.names{n} = ['/FieldData/FD/' hdf_fielddata_names{2*n-1}(1:end-5)]; - hdf_fielddata.FD.frequencies(n) = h5readatt_octave(file,['/FieldData/FD/' hdf_fielddata_names{2*n}],'frequency'); + hdf_fielddata.FD.frequency = h5readatt_octave(file,'/FieldData/FD/','frequency'); + for n=1:numel(hdf_fielddata.FD.frequency) + hdf_fielddata.FD.values{n} = double(hdf.FieldData.FD.(['f' int2str(n-1) '_real']) +1i*hdf.FieldData.FD.(['f' int2str(n-1) '_imag']) ); end end diff --git a/matlab/WriteHDF5.m b/matlab/WriteHDF5.m index 130e7ca..df6a8d7 100644 --- a/matlab/WriteHDF5.m +++ b/matlab/WriteHDF5.m @@ -38,26 +38,25 @@ if isfield( hdf_fielddata, 'TD' ) end if isfield( hdf_fielddata, 'FD' ) % this is a frequency domain data set - freq = hdf_fielddata.FD.freq; + freq = hdf_fielddata.FD.frequency; for n=1:numel(freq) name = ['/FieldData/FD/f' int2str(n-1) '_real']; [details.Location, details.Name] = fileparts(name); - attribute_details.AttachedTo = name; - attribute_details.AttachType = 'dataset'; - attribute_details.Name = 'freq'; hdf5write( filename, details, real(hdf_fielddata.FD.values{n}), ... - attribute_details, freq(n), ... 'WriteMode', writemode ); name = ['/FieldData/FD/f' int2str(n-1) '_imag']; [details.Location, details.Name] = fileparts(name); - attribute_details.AttachedTo = name; - attribute_details.AttachType = 'dataset'; - attribute_details.Name = 'freq'; hdf5write( filename, details, imag(hdf_fielddata.FD.values{n}), ... - attribute_details, freq(n), ... 'WriteMode', 'append' ); writemode = 'append'; end + name = '/FieldData/FD'; + [details.Location, details.Name] = fileparts(name); + attribute_details.AttachedTo = name; + attribute_details.AttachType = 'group'; + attribute_details.Name = 'frequency'; + hdf5write( filename, attribute_details, freq, ... + 'WriteMode', 'append' ); end names = hdf_mesh.names; % names is a cell array