parameters_io.h Source File

CPP API: parameters_io.h Source File
memilio/io/parameters_io.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Henrik Zunker
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_IO_PARAMETERS_IO_H
21 #define MIO_IO_PARAMETERS_IO_H
22 
23 #include "memilio/config.h"
24 
25 #ifdef MEMILIO_HAS_JSONCPP
26 
27 #include "memilio/io/epi_data.h"
28 #include "memilio/io/result_io.h"
29 
30 #include <string>
31 #include <vector>
32 
33 namespace mio
34 {
45 template <class EpiDataEntry>
46 int get_region_id(const EpiDataEntry& data_entry)
47 {
48  if (data_entry.county_id) {
49  return data_entry.county_id->get();
50  }
51  else if (data_entry.state_id) {
52  return data_entry.state_id->get();
53  }
54  else if (data_entry.district_id) {
55  return data_entry.district_id->get();
56  }
57  else {
58  return 0;
59  }
60 }
61 
75 template <typename FP = ScalarType>
76 IOResult<void> compute_divi_data(const std::vector<DiviEntry>& divi_data, const std::vector<int>& vregion, Date date,
77  std::vector<FP>& vnum_icu)
78 {
79  auto max_date_entry = std::max_element(divi_data.begin(), divi_data.end(), [](auto&& a, auto&& b) {
80  return a.date < b.date;
81  });
82  if (max_date_entry == divi_data.end()) {
83  log_error("DIVI data is empty.");
84  return failure(StatusCode::InvalidValue, "DIVI data is empty.");
85  }
86  auto max_date = max_date_entry->date;
87  if (max_date < date) {
88  log_error("DIVI data does not contain the specified date.");
89  return failure(StatusCode::OutOfRange, "DIVI data does not contain the specified date.");
90  }
91 
92  for (auto&& entry : divi_data) {
93  auto it = std::find_if(vregion.begin(), vregion.end(), [&entry](auto r) {
94  return r == 0 || r == get_region_id(entry);
95  });
96  auto date_df = entry.date;
97  if (it != vregion.end() && date_df == date) {
98  auto region_idx = size_t(it - vregion.begin());
99  vnum_icu[region_idx] = entry.num_icu;
100  }
101  }
102 
103  return success();
104 }
105 
118 template <typename FP = ScalarType>
119 IOResult<void> read_divi_data(const std::string& path, const std::vector<int>& vregion, Date date,
120  std::vector<FP>& vnum_icu)
121 {
122  BOOST_OUTCOME_TRY(auto&& divi_data, mio::read_divi_data(path));
123  return compute_divi_data(divi_data, vregion, date, vnum_icu);
124 }
125 
134 IOResult<std::vector<std::vector<ScalarType>>>
135 read_population_data(const std::vector<PopulationDataEntry>& population_data, const std::vector<int>& vregion);
136 
145 IOResult<std::vector<std::vector<ScalarType>>> read_population_data(const std::string& path,
146  const std::vector<int>& vregion);
147 
148 } // namespace mio
149 
150 #endif // MEMILIO_HAS_JSONCPP
151 
152 #endif // MIO_IO_PARAMETERS_IO_H
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
auto failure(const IOStatus &s)
Create an object that is implicitly convertible to an error IOResult<T>.
Definition: io.h:380
auto success()
Create an object that is implicitly convertible to a succesful IOResult<void>.
Definition: io.h:359
void log_error(spdlog::string_view_t fmt, const Args &... args)
Definition: logging.h:100