AbstractHdf5Converter.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "AbstractHdf5Converter.hpp"
00037 #include "Version.hpp"
00038 
00039 
00040 /*
00041  * Operator function to be called by H5Literate [HDF5 1.8.x] or H5Giterate [HDF5 1.6.x] (in TestListingDatasetsInAnHdf5File).
00042  */
00043 herr_t op_func (hid_t loc_id,
00044                 const char *name,
00045 #if H5_VERS_MAJOR >= 1 && H5_VERS_MINOR >=8
00046                 const H5L_info_t *info,
00047 #endif
00048                 void *operator_data);
00049 
00050 
00051 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00052 AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::AbstractHdf5Converter(const FileFinder& rInputDirectory,
00053                                                                      const std::string& rFileBaseName,
00054                                                                      AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>* pMesh,
00055                                                                      const std::string& rSubdirectoryName,
00056                                                                      unsigned precision)
00057     : mrH5Folder(rInputDirectory),
00058       mFileBaseName(rFileBaseName),
00059       mOpenDatasetIndex(UNSIGNED_UNSET),
00060       mpMesh(pMesh),
00061       mRelativeSubdirectory(rSubdirectoryName),
00062       mPrecision(precision)
00063 {
00064     GenerateListOfDatasets(mrH5Folder, mFileBaseName);
00065 
00066     // Create new directory in which to store everything
00067     FileFinder sub_directory(mRelativeSubdirectory, mrH5Folder);
00068     mpOutputFileHandler = new OutputFileHandler(sub_directory, false);
00069 
00070     MoveOntoNextDataset();
00071 }
00072 
00073 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00074 void AbstractHdf5Converter<ELEMENT_DIM, SPACE_DIM>::WriteInfoFile()
00075 {
00076     // Note that we don't want the child processes to write info files
00077     if (PetscTools::AmMaster())
00078     {
00079         std::string time_info_filename;
00080 
00081         // If the dataset is just "Data" then we will leave the original filename as it is (to avoid confusion!)
00082         // If the dataset is a new variant like "Postprocessing" then we will put the dataset name in the output.
00083         if (mDatasetNames[mOpenDatasetIndex]=="Data")
00084         {
00085            time_info_filename = mFileBaseName + "_times.info";
00086         }
00087         else
00088         {
00089            time_info_filename = mDatasetNames[mOpenDatasetIndex] + "_times.info";
00090         }
00091         out_stream p_file = mpOutputFileHandler->OpenOutputFile(time_info_filename);
00092 
00093         std::vector<double> time_values = mpReader->GetUnlimitedDimensionValues();
00094         unsigned num_timesteps = time_values.size();
00095         double first_timestep = time_values.front();
00096         double last_timestep = time_values.back();
00097 
00098         double timestep = num_timesteps > 1 ? time_values[1] - time_values[0] : DOUBLE_UNSET;
00099 
00100         *p_file << "Number of timesteps " << num_timesteps << std::endl;
00101         *p_file << "timestep " << timestep << std::endl;
00102         *p_file << "First timestep " << first_timestep << std::endl;
00103         *p_file << "Last timestep " << last_timestep << std::endl;
00104         *p_file << ChasteBuildInfo::GetProvenanceString();
00105 
00106         p_file->close();
00107     }
00108 }
00109 
00110 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00111 AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>::~AbstractHdf5Converter()
00112 {
00113     delete mpOutputFileHandler;
00114 }
00115 
00116 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00117 std::string AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>::GetSubdirectory()
00118 {
00119     return mRelativeSubdirectory;
00120 }
00121 
00122 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00123 bool AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>::MoveOntoNextDataset()
00124 {
00125     // If we are already at the end just return false.
00126     if (mDatasetNames.size() == mOpenDatasetIndex+1u)
00127     {
00128         return false;
00129     }
00130 
00131     // If we haven't read anything yet, start at the beginning, otherwise increment by one.
00132     if (mOpenDatasetIndex==UNSIGNED_UNSET)
00133     {
00134         mOpenDatasetIndex = 0u;
00135     }
00136     else
00137     {
00138         mOpenDatasetIndex++;
00139     }
00140 
00141     // Store directory, mesh and filenames and create the reader
00142     mpReader.reset(new Hdf5DataReader(mrH5Folder, mFileBaseName, mDatasetNames[mOpenDatasetIndex]));
00143 
00144     // Check the data file for basic validity
00145     std::vector<std::string> variable_names = mpReader->GetVariableNames();
00146     mNumVariables = variable_names.size();
00147 
00148     if (mpReader->GetNumberOfRows() != mpMesh->GetNumNodes())
00149     {
00150         delete mpOutputFileHandler;
00151         EXCEPTION("Mesh and HDF5 file have a different number of nodes");
00152     }
00153     WriteInfoFile();
00154 
00155     return true;
00156 }
00157 
00158 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00159 void AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>::GenerateListOfDatasets(const FileFinder& rH5Folder,
00160                                                                           const std::string& rFileName)
00161 {
00162     /*
00163      * Open file.
00164      */
00165     std::string file_name = rH5Folder.GetAbsolutePath() + rFileName + ".h5";
00166     hid_t file = H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
00167 
00168     /*
00169      * Begin HDF5 iteration, calls a method that populates mDatasetNames.
00170      */
00171 #if H5_VERS_MAJOR >= 1 && H5_VERS_MINOR >=8
00172     //std::cout << "HDF5 1.8.x or above detected.\n";
00173     H5Literate(file, H5_INDEX_NAME, H5_ITER_NATIVE, NULL, op_func, &mDatasetNames);
00174 #else
00175     //std::cout << "HDF5 1.6.x  detected.\n";
00176     H5Giterate(file, "/", NULL, op_func, &mDatasetNames);
00177 #endif
00178 
00179     H5Fclose(file);
00180 
00181     // Remove datasets that end in "_Unlimited", as these are paired up with other ones!
00182     std::string ending = "_Unlimited";
00183 
00184     // Strip off the independent variables from the list
00185     std::vector<std::string>::iterator iter;
00186     for (iter = mDatasetNames.begin(); iter != mDatasetNames.end(); )
00187     {
00188         // If the dataset name is "Time" OR ...
00189         // it is longer than the ending we are looking for ("_Unlimited") ...
00190         // ... AND it ends with the string we are looking for,
00191         // then erase it.
00192         if ( (*(iter) == "Time") ||
00193              ( ( iter->length() > ending.length() ) &&
00194                ( 0 == iter->compare(iter->length() - ending.length(), ending.length(), ending) ) ) )
00195         {
00196             iter = mDatasetNames.erase(iter);
00197         }
00198         else
00199         {
00200             ++iter;
00201         }
00202     }
00203 }
00204 
00205 /*
00206  * HDF5 Operator function.
00207  *
00208  * Puts the name of the objects (in this case 'datasets')
00209  * in an HDF5 file into a std::vector for us to use for
00210  * iterating over the file.
00211  *
00212  * This was based on a couple of HDF5 example files.
00213  */
00214 herr_t op_func (hid_t loc_id, const char *name,
00215 #if H5_VERS_MAJOR >= 1 && H5_VERS_MINOR >=8
00216                 const H5L_info_t *info,
00217 #endif
00218                 void *operator_data)
00219 {
00220     std::vector<std::string>* p_dataset_names = static_cast<std::vector< std::string > * >(operator_data);
00221 
00222     /*
00223      * Get type of the object and display its name and type.
00224      * The name of the object is passed to this function by
00225      * the Library.
00226      */
00227 #if H5_VERS_MAJOR >= 1 && H5_VERS_MINOR >=8
00228     H5O_info_t infobuf;
00229     H5Oget_info_by_name (loc_id, name, &infobuf, H5P_DEFAULT);
00230     switch (infobuf.type)
00231     {
00232 //          case H5O_TYPE_GROUP:
00233 //              printf ("  Group: %s\n", name);
00234 //              break;
00235         case H5O_TYPE_DATASET:
00236             p_dataset_names->push_back(name);
00237             break;
00238 //          case H5O_TYPE_NAMED_DATATYPE:
00239 //              printf ("  Datatype: %s\n", name);
00240 //              break;
00241 #else // HDF5 1.6.x
00242     H5G_stat_t statbuf;
00243     H5Gget_objinfo (loc_id, name, 0, &statbuf);
00244     switch (statbuf.type)
00245     {
00246 //        case H5G_GROUP:
00247 //            printf ("  Group: %s\n", name);
00248 //            break;
00249         case H5G_DATASET:
00250             //printf ("  Dataset: %s\n", name);
00251             p_dataset_names->push_back(name);
00252             break;
00253 //        case H5G_TYPE:
00254 //            printf ("  Datatype: %s\n", name);
00255 //            break;
00256 #endif
00257         default:
00258             NEVER_REACHED;
00259             // If you ever do reach here, it means that an HDF5 file you are trying to convert contains
00260             // something other than a 'Dataset', which is the usual data structure we write out in Chaste.
00261             // The above commented out lines should help you figure out what it is, and how it got there.
00262     }
00263     return 0;
00264 }
00265 
00266 
00268 // Explicit instantiation
00270 
00271 template class AbstractHdf5Converter<1,1>;
00272 template class AbstractHdf5Converter<1,2>;
00273 template class AbstractHdf5Converter<2,2>;
00274 template class AbstractHdf5Converter<1,3>;
00275 template class AbstractHdf5Converter<2,3>;
00276 template class AbstractHdf5Converter<3,3>;

Generated by  doxygen 1.6.2