Skip to content

Commit abd53d7

Browse files
committed
SAR/hdf5: fixes and improvements dealing with the legacy format
Signed-off-by: Thorsten Liebig <thorsten.liebig@gmx.de>
1 parent 92ba7ff commit abd53d7

8 files changed

Lines changed: 43 additions & 18 deletions

File tree

Common/processfields_fd.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,14 @@ void ProcessFieldsFD::DumpFDData()
185185
if (m_HDF5_Dump_File->WriteVectorField<std::complex<float>>(ss.str(), *m_FD_Fields.at(n), g_settings.GetLegacyHFD5Dumps())==false)
186186
cerr << "ProcessFieldsFD::Process: can't dump to file...! " << endl;
187187

188-
if (m_HDF5_Dump_File->WriteAtrribute("/FieldData/FD/"+ss.str(), "frequency", m_FD_Samples.at(n))==false)
188+
if (g_settings.GetLegacyHFD5Dumps())
189+
{
190+
if (m_HDF5_Dump_File->WriteAtrribute("/FieldData/FD/"+ss.str()+"_real", "frequency", m_FD_Samples.at(n))==false)
191+
cerr << "ProcessFieldsFD::Process: can't dump to file...! " << endl;
192+
if (m_HDF5_Dump_File->WriteAtrribute("/FieldData/FD/"+ss.str()+"_imag", "frequency", m_FD_Samples.at(n))==false)
193+
cerr << "ProcessFieldsFD::Process: can't dump to file...! " << endl;
194+
}
195+
else if (m_HDF5_Dump_File->WriteAtrribute("/FieldData/FD/"+ss.str(), "frequency", m_FD_Samples.at(n))==false)
189196
cerr << "ProcessFieldsFD::Process: can't dump to file...! " << endl;
190197
}
191198
return;

matlab/CalcSAR.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function CalcSAR(sar_fn, sar_out, varargin)
2121
% -----------------------
2222
% author: Thorsten Liebig, 2025
2323

24-
bin_args = ' ';
24+
bin_args = ' --legacyHDF5Dumps';
2525

2626
for n=1:2:numel(varargin)
2727
if (strcmp(varargin{n},'verbose'))

matlab/RunOpenEMS.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ function RunOpenEMS(Sim_Path, Sim_File, opts, Settings)
7575
opts = '';
7676
end
7777

78+
opts = [opts ' --legacyHDF5Dumps'];
79+
7880
if (nargin<4)
7981
Settings = [];
8082
end

matlab/Tutorials/Dipole_SAR.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181

182182
SAR = SAR_field.FD.values{1};
183183
ptotal = ReadHDF5Attribute(SAR_fn,'/FieldData/FD/f0','power');
184-
mass = ReadHDF5Attribute(SAR_fn,'/FieldData/FD/f0','mass');
184+
mass = ReadHDF5Attribute(SAR_fn,'/','mass');
185185

186186
%% calculate 3D pattern
187187
phi = 0:3:360;

tools/arraylib/impl/array_base.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class ArrayLib::ArrayBase
6060
using AllocatorType = Allocator;
6161

6262
std::string m_name="";
63+
bool m_swapped=false; // record if swapped axis
6364
std::array<IndexType, rank> m_extent = {};
6465
std::array<IndexType, rank> m_stride = {};
6566
IndexType m_size=0, m_bytes=0;
@@ -114,10 +115,14 @@ class ArrayLib::ArrayBase
114115
{
115116
if ((a>=rank) || (b>=rank) || (a==b))
116117
return;
118+
m_swapped = true;
117119
std::swap(m_extent[a], m_extent[b]);
118120
std::swap(m_stride[a], m_stride[b]);
119121
}
120122

123+
// return if any axis are swapped (important for linear data access)
124+
bool wasSwapped() const {return m_swapped;}
125+
121126
// return the number of array elements
122127
IndexType size() const { return m_size; }
123128

tools/hdf5_file_reader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,9 @@ bool HDF5_File_Reader::ReadScalarDataSet(std::string ds_name, ArrayLib::ArrayIJK
795795
data.Init(ds_name, ui_dim);
796796
bool ok = ReadDataSet(ds_name, type, (void*)data.data());
797797
delete[] dims;
798+
if (m_legacy_fmt)
799+
data.swapAxis(0,2);
798800
return ok;
799-
800-
return false;
801801
}
802802

803803
// Explicit template instantiation
@@ -829,6 +829,7 @@ bool HDF5_File_Reader::GetFDVectorData(size_t idx, ArrayLib::ArrayNIJK<std::comp
829829

830830
if (!ReadVectorDataSet<float>(ds_name.str() + "_imag", tmp))
831831
return false;
832+
tmp_buffer = tmp.data();
832833
for (size_t n=0;n<tmp.size();++n)
833834
buffer[n].imag(tmp_buffer[n]); // assign imag part
834835

tools/hdf5_file_writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ bool HDF5_File_Writer::WriteAtrribute(std::string locName, std::string attr_name
398398
hid_t loc = H5Oopen(hdf5_file, locName.c_str(), H5P_DEFAULT);
399399
if (loc<0)
400400
{
401-
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to open location: """ << locName << """" << endl;
401+
cerr << "HDF5_File_Writer::WriteAtrribute: Error, failed to open location: """ << locName << """: " << attr_name << endl;
402402
H5Fclose(hdf5_file);
403403
return false;
404404
}

tools/sar_calculation.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ void SAR_Calculation::AddEFieldAndCondictivity(float freq, ArrayLib::ArrayNIJK<s
176176
float* loc_cpd_data = loc_cpd->data();
177177
float* cell_vol = m_cell_volume->data();
178178
float* cell_dens = m_cell_density->data();
179+
float cond=0;
179180
double max_lp=0;
180181
unsigned int loc=0;
181182
unsigned int pos[3];
@@ -193,9 +194,10 @@ void SAR_Calculation::AddEFieldAndCondictivity(float freq, ArrayLib::ArrayNIJK<s
193194
{
194195
for (pos[2]=0; pos[2]<m_numLines[2]; ++pos[2])
195196
{
196-
l_pow = (*cell_conductivity)(pos[0], pos[1], pos[2])*abs((*e_field)(0, pos[0], pos[1], pos[2])) * abs((*e_field)(0, pos[0], pos[1], pos[2]));
197-
l_pow += (*cell_conductivity)(pos[0], pos[1], pos[2])*abs((*e_field)(1, pos[0], pos[1], pos[2])) * abs((*e_field)(1, pos[0], pos[1], pos[2]));
198-
l_pow += (*cell_conductivity)(pos[0], pos[1], pos[2])*abs((*e_field)(2, pos[0], pos[1], pos[2])) * abs((*e_field)(2, pos[0], pos[1], pos[2]));
197+
cond = (*cell_conductivity)(pos[0], pos[1], pos[2]);
198+
l_pow = cond*abs((*e_field)(0, pos[0], pos[1], pos[2])) * abs((*e_field)(0, pos[0], pos[1], pos[2]));
199+
l_pow += cond*abs((*e_field)(1, pos[0], pos[1], pos[2])) * abs((*e_field)(1, pos[0], pos[1], pos[2]));
200+
l_pow += cond*abs((*e_field)(2, pos[0], pos[1], pos[2])) * abs((*e_field)(2, pos[0], pos[1], pos[2]));
199201
l_pow *= 0.5*(cell_dens[loc]>0);
200202
max_lp = max(max_lp, l_pow);
201203
power += l_pow*cell_vol[loc];
@@ -287,6 +289,12 @@ bool SAR_Calculation::CheckValid()
287289
if (m_avg_mass<0)
288290
return false;
289291

292+
// check that read in data was not swapped
293+
if (m_cell_density->wasSwapped())
294+
return false;
295+
if (m_cell_volume->wasSwapped())
296+
return false;
297+
290298
// vaildate sizes
291299
for (int n=0;n<3;++n)
292300
{
@@ -429,6 +437,8 @@ bool SAR_Calculation::WriteToHDF5(std::string out_name, bool legacyHDF5)
429437
out_file.WriteAtrribute("/","openEMS_HDF5_version", OPENEMS_HDF5_VERSION);
430438
out_file.SetCurrentGroup("/FieldData/FD");
431439
out_file.WriteAtrribute("/FieldData/FD","frequency",m_freq);
440+
if (legacyHDF5)
441+
out_file.WriteAtrribute("/", "legacy_fmt", true);
432442

433443
return WriteToHDF5(out_file, legacyHDF5);
434444
}
@@ -462,25 +472,25 @@ bool SAR_Calculation::WriteToHDF5(HDF5_File_Writer &out_file, bool legacyHDF5)
462472
stringstream ss;
463473
ss << "f" << n;
464474
if (out_file.WriteScalarField<float>(ss.str(), *m_SAR.at(n), legacyHDF5)==false)
465-
cerr << "SAR_Calculation::CalcFromHDF5: can't dump to file...! " << endl;
475+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
466476
if (m_cube_type.data()!=NULL)
467477
if (out_file.WriteData(ss.str() + "_CubeType", H5T_NATIVE_UCHAR, m_cube_type.data(), 3, dims)==false)
468-
cerr << "SAR_Calculation::CalcFromHDF5: can't dump to file...! " << endl;
478+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
469479
if (m_cube_mass.data()!=NULL)
470480
if (out_file.WriteData(ss.str() + "_CubeMass", H5T_NATIVE_FLOAT, m_cube_mass.data(), 3, dims)==false)
471-
cerr << "SAR_Calculation::CalcFromHDF5: can't dump to file...! " << endl;
481+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
472482
if (m_cube_volume.data()!=NULL)
473483
if (out_file.WriteData(ss.str() + "_CubeVol", H5T_NATIVE_FLOAT, m_cube_volume.data(), 3, dims)==false)
474-
cerr << "SAR_Calculation::CalcFromHDF5: can't dump to file...! " << endl;
484+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
475485
if (out_file.WriteAtrribute("/FieldData/FD/"+ss.str(),"frequency",m_freq.at(n))==false)
476-
cerr << "ProcessFieldsSAR::DumpFDData: can't dump to file...! " << endl;
486+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
477487
if (out_file.WriteAtrribute("/FieldData/FD/"+ss.str(),"power",pwr)==false)
478-
cerr << "ProcessFieldsSAR::DumpFDData: can't dump to file...! " << endl;
488+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
479489
if (out_file.WriteAtrribute("/FieldData/FD/"+ss.str(),"maxSAR", m_maxSAR.at(n))==false)
480-
cerr << "ProcessFieldsSAR::DumpFDData: can't dump to file...! " << endl;
490+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
481491
std::vector<unsigned int> idx = {m_maxSAR_Idx.at(n)[0],m_maxSAR_Idx.at(n)[1],m_maxSAR_Idx.at(n)[2]};
482492
if (out_file.WriteAtrribute("/FieldData/FD/"+ss.str(),"maxSAR_idx", idx)==false)
483-
cerr << "ProcessFieldsSAR::DumpFDData: can't dump to file...! " << endl;
493+
cerr << "SAR_Calculation::WriteToHDF5: can't dump to file...! " << endl;
484494
}
485495

486496
out_file.WriteAtrribute("/FieldData/FD","valid_cubes",m_Used);
@@ -1313,7 +1323,7 @@ bool SAR_Calculation::CalcAveragedSAR(unsigned int numThreads)
13131323
if (m_Valid+m_Used+m_Unused+m_AirVoxel!=Vx_Valid.size())
13141324
{
13151325
cerr << "SAR_Calculation::CalcAveragedSAR: critical error, mismatch in voxel status count... EXIT" << endl;
1316-
// exit(1);
1326+
exit(1);
13171327
}
13181328

13191329
if (m_DebugLevel>0)

0 commit comments

Comments
 (0)