Stride Reference Manual  - generated for commit 9643b11
HouseholdCSVReader.cpp
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2019, Jan Broeckhove.
14  */
15 
16 #include "HouseholdCSVReader.h"
17 
18 #include "util/CSV.h"
19 
20 namespace geopop {
21 
22 using namespace std;
23 using namespace stride::util;
24 
25 HouseholdCSVReader::HouseholdCSVReader(std::unique_ptr<std::istream> inputStream)
26  : m_input_stream(std::move(inputStream))
27 {
28 }
29 
30 void HouseholdCSVReader::SetReferenceHouseholds(unsigned int& ref_person_count,
31  std::vector<std::vector<unsigned int>>& ref_ages)
32 {
33  CSV reader(*(m_input_stream.get()));
34 
35  unsigned int p_count = 0U;
36  for (const CSVRow& row : reader) {
37 
38  vector<unsigned int> temp;
39  for (unsigned int i = 0; i < 12; i++) {
40  unsigned int age;
41  try {
42  age = row.GetValue<unsigned int>(i);
43  } catch (const std::bad_cast& e) {
44  // NA
45  break;
46  }
47  temp.emplace_back(age);
48  }
49  p_count += temp.size();
50  ref_ages.emplace_back(temp);
51  }
52  ref_person_count = p_count;
53 }
54 
55 } // namespace geopop
Row in CSV file.
Definition: CSVRow.h:36
std::unique_ptr< std::istream > m_input_stream
Input stream connected to input data file.
Utilities for the project.
HouseholdCSVReader(std::unique_ptr< std::istream > inputStream)
Construct the HouseholdCSVReader with an istream containing the CSV data.
STL namespace.
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
void SetReferenceHouseholds(unsigned int &ref_person_count, std::vector< std::vector< unsigned int >> &ref_ages) override
Add the locations to the GeoGrid.
A collection of CSVRow elements.
Definition: CSV.h:46
Header file of base class for config that needs to be read from a file.