21#include <ql/errors.hpp>
33void CubeWriter::write(
const QuantLib::ext::shared_ptr<NPVCube>& cube,
const std::map<std::string, std::string>& nettingSetMap,
37 vector<string> dateStrings(cube->numDates());
38 for (Size i = 0; i < cube->numDates(); ++i) {
39 std::ostringstream oss;
40 oss << QuantLib::io::iso_date(cube->dates()[i]);
41 dateStrings[i] = oss.str();
43 std::ostringstream oss;
44 oss << QuantLib::io::iso_date(cube->asof());
45 string asofString = oss.str();
47 const std::map<string, Size>& ids = cube->idsAndIndexes();
49 FILE* fp = fopen(
filename_.c_str(), append ?
"a" :
"w");
50 QL_REQUIRE(fp,
"error opening file " <<
filename_);
52 fprintf(fp,
"Id,NettingSet,DateIndex,Date,Sample,Depth,Value\n");
53 const char* fmt =
"%s,%s,%lu,%s,%lu,%lu,%.4f\n";
56 vector<const char*> nettingSetIds(ids.size());
59 for (
const auto& [
id,idx] : ids) {
60 if (nettingSetMap.find(
id) != nettingSetMap.end())
61 nettingSetIds[idx] = nettingSetMap.at(
id).c_str();
63 nettingSetIds[idx] =
"";
65 fprintf(fp, fmt,
id.c_str(), nettingSetIds[idx],
static_cast<Size
>(0), asofString.c_str(),
66 static_cast<Size
>(0),
static_cast<Size
>(0), cube->getT0(idx));
70 for (
const auto& [
id, idx] : ids) {
71 for (Size j = 0; j < cube->numDates(); j++) {
72 for (Size k = 0; k < cube->samples(); k++) {
73 for (Size l = 0; l < cube->depth(); l++) {
74 fprintf(fp, fmt,
id.c_str(), nettingSetIds[idx], j + 1, dateStrings[j].c_str(), k + 1, l,
75 cube->get(idx, j, k, l));
CubeWriter(const std::string &filename)
ctor
void write(const QuantLib::ext::shared_ptr< NPVCube > &cube, const std::map< std::string, std::string > &nettingSetMap, bool append=false)
Write a cube out to file.
A Class to write a cube out to file.