Vespucci  1.0.0
binaryimport.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright (C) 2014-2016 Wright State University - All Rights Reserved
3  Daniel P. Foose - Maintainer/Lead Developer
4 
5  This file is part of Vespucci.
6 
7  Vespucci is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Vespucci is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Vespucci. If not, see <http://www.gnu.org/licenses/>.
19 *******************************************************************************/
21 #include "Global/vespucci.h"
22 
23 #include <H5Cpp.h>
24 bool BinaryImport::ImportVespucciBinary(std::string filename,
25  arma::mat &spectra,
26  arma::vec &abscissa,
27  arma::vec &x, arma::vec &y)
28 {
29  Vespucci::ResetDataset(spectra, x, y, abscissa);
30  using namespace H5;
31  bool success;
32  try{
33  H5File file(filename, H5F_ACC_RDONLY);
34 
35  DataSet ds = file.openDataSet("Spectra");
36  DataSpace dspace = ds.getSpace();
37  hsize_t dims[2];
38  dspace.getSimpleExtentDims(dims);
39  spectra.set_size(dims[0], dims[1]);
40  ds.read(spectra.memptr(), PredType::NATIVE_DOUBLE);
41 
42  ds = file.openDataSet("x");
43  dspace = ds.getSpace();
44  dspace.getSimpleExtentDims(dims);
45  if (dims[1] == 1){
46  x.set_size(dims[0]);
47  ds.read(x.memptr(), PredType::NATIVE_DOUBLE);
48  }
49 
50  ds = file.openDataSet("y");
51  dspace = ds.getSpace();
52  dspace.getSimpleExtentDims(dims);
53  if (dims[1] == 1) {
54  y.set_size(dims[0]);
55  ds.read(y.memptr(), PredType::NATIVE_DOUBLE);
56  }
57 
58  ds = file.openDataSet("Spectral Abscissa");
59  dspace = ds.getSpace();
60  dspace.getSimpleExtentDims(dims);
61  if (dims[1] == 1){
62  abscissa.set_size(dims[0]);
63  ds.read(abscissa.memptr(), PredType::NATIVE_DOUBLE);
64  }
65 
66  success = true;
67  }catch (...){
68  success = false;
69  }
70 
71  if(success){
72  //check to make sure everything is sorted the way Vespucci expects
73  arma::uvec sorted_indices = arma::stable_sort_index(abscissa);
74  abscissa = abscissa.rows(sorted_indices);
75  spectra = spectra.rows(sorted_indices);
76  }
77  return success;
78 
79 }
80 
81 bool BinaryImport::ImportOldVespucciBinary(std::string filename,
82  arma::mat &spectra,
83  arma::vec &abscissa,
84  arma::vec &x, arma::vec &y)
85 {
86  Vespucci::ResetDataset(spectra, x, y, abscissa);
87  arma::field<arma::mat> input_data;
88  bool success = input_data.load(filename);
89  std::cout << (success ? "success" : "failure") << std::endl;
90  if(success){
91  spectra = input_data(0);
92  abscissa = input_data(1);
93  x = input_data(2);
94  y = input_data(3);
95  }
96  return success;
97 }
VESPUCCI_EXPORT bool ImportVespucciBinary(std::string filename, arma::mat &spectra, arma::vec &abscissa, arma::vec &x, arma::vec &y)
VESPUCCI_EXPORT bool ImportOldVespucciBinary(std::string filename, arma::mat &spectra, arma::vec &abscissa, arma::vec &x, arma::vec &y)
VESPUCCI_EXPORT void ResetDataset(arma::mat &spectra, arma::vec &x, arma::vec &y, arma::vec &abscissa)
Definition: vespucci.cpp:226