1
+ #include < string>
2
+
3
+ #include < AMReX.H>
4
+ #include < AMReX_ParmParse.H>
5
+ #include < AMReX_PlotFileUtil.H>
6
+ #include < AMReX_MultiFab.H>
7
+ #include < hdf5.h>
8
+
9
+ void WriteMultiFabToHDF5 (const std::string& plotfile, const std::string& hdf5file) {
10
+
11
+ amrex::PlotFileData plotfile_data (plotfile);
12
+ amrex::Vector<std::string> varnames = plotfile_data.varNames ();
13
+ double time = plotfile_data.time ();
14
+ int time_step = plotfile_data.levelStep (0 );
15
+
16
+ amrex::MultiFab mf;
17
+
18
+ amrex::VisMF::Read (mf, plotfile+" /Level_0/Cell" );
19
+
20
+ amrex::BoxArray ba = mf.boxArray ();
21
+
22
+ // Expecting a cell centered box with low and high index bounds {0,0} to {nx-1,ny-1}
23
+ amrex::Box minimal_box = ba.minimalBox ();
24
+ AMREX_ASSERT (minimal_box.smallEnd (0 ) == 0 );
25
+ AMREX_ASSERT (minimal_box.smallEnd (1 ) == 0 );
26
+
27
+ hid_t file_id = H5Fcreate (hdf5file.c_str (), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
28
+
29
+ {
30
+ hid_t attr_space_id = H5Screate (H5S_SCALAR);
31
+ hid_t attr_id = H5Acreate (file_id, " time" , H5T_NATIVE_DOUBLE, attr_space_id, H5P_DEFAULT, H5P_DEFAULT);
32
+ H5Awrite (attr_id, H5T_NATIVE_DOUBLE, &time );
33
+ H5Aclose (attr_id);
34
+ H5Sclose (attr_space_id);
35
+ }
36
+
37
+ {
38
+ hid_t attr_space_id = H5Screate (H5S_SCALAR);
39
+ hid_t attr_id = H5Acreate (file_id, " time_step" , H5T_NATIVE_INT, attr_space_id, H5P_DEFAULT, H5P_DEFAULT);
40
+ H5Awrite (attr_id, H5T_NATIVE_INT, &time_step);
41
+ H5Aclose (attr_id);
42
+ H5Sclose (attr_space_id);
43
+ }
44
+
45
+ // Get the dimensions of the MultiFab
46
+ hsize_t dims[2 ] = {static_cast <hsize_t >(minimal_box.length (0 )), static_cast <hsize_t >(minimal_box.length (1 ))};
47
+
48
+ // Iterate over the components of the MultiFab
49
+ for (int component_idx = 0 ; component_idx < mf.nComp (); ++component_idx) {
50
+ // Create a dataset for this component
51
+ hid_t dataspace_id = H5Screate_simple (2 , dims, NULL );
52
+ hid_t dataset_id = H5Dcreate (file_id, varnames[component_idx].c_str (), H5T_NATIVE_DOUBLE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
53
+
54
+ // Write the data to the dataset
55
+ for (amrex::MFIter mfi (mf); mfi.isValid (); ++mfi) {
56
+ const amrex::Box& bx = mfi.validbox ();
57
+ const amrex::Array4<const amrex::Real> &fab = mf.array (mfi);
58
+ std::vector<double > data (bx.numPts ());
59
+ int idx = 0 ;
60
+ for (int j = bx.smallEnd (1 ); j <= bx.bigEnd (1 ); ++j) {
61
+ for (int i = bx.smallEnd (0 ); i <= bx.bigEnd (0 ); ++i) {
62
+ data[idx++] = fab (i, j, 0 , component_idx);
63
+ }
64
+ }
65
+ H5Dwrite (dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data.data ());
66
+ }
67
+
68
+ H5Dclose (dataset_id);
69
+ H5Sclose (dataspace_id);
70
+ }
71
+
72
+ H5Fclose (file_id);
73
+ }
74
+
75
+ int main (int argc, char * argv[]) {
76
+ amrex::Initialize (argc, argv);
77
+
78
+ // Read in parameters from inputs file
79
+ amrex::ParmParse pp;
80
+
81
+ // Read in plotfile name
82
+ std::string plotfile;
83
+ pp.query (" infile" , plotfile);
84
+ if (plotfile.empty ()) {
85
+ amrex::Abort (" You must specify `infile'" );
86
+ }
87
+
88
+ // Read in optional output hdf5 file name
89
+ // Output hdf5 file (default to input plotfile name with .h5 extension)
90
+ std::string hdf5file = plotfile + " .h5" ;
91
+ pp.query (" outfile" , hdf5file);
92
+
93
+ amrex::Print () << " Input Plotfile: " << plotfile << std::endl;
94
+
95
+ WriteMultiFabToHDF5 (plotfile, hdf5file);
96
+
97
+ amrex::Print () << " Output HDF5 file: " << hdf5file << std::endl;
98
+
99
+ amrex::Finalize ();
100
+ return 0 ;
101
+ }
0 commit comments