Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
cubewriter.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20#include <ostream>
21#include <ql/errors.hpp>
22#include <stdio.h>
23
24using QuantLib::Date;
25using std::string;
26using std::vector;
27
28namespace ore {
29namespace analytics {
30
31CubeWriter::CubeWriter(const std::string& filename) : filename_(filename) {}
32
33void CubeWriter::write(const QuantLib::ext::shared_ptr<NPVCube>& cube, const std::map<std::string, std::string>& nettingSetMap,
34 bool append) {
35
36 // Convert dates into strings
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();
42 }
43 std::ostringstream oss;
44 oss << QuantLib::io::iso_date(cube->asof());
45 string asofString = oss.str();
46
47 const std::map<string, Size>& ids = cube->idsAndIndexes();
48
49 FILE* fp = fopen(filename_.c_str(), append ? "a" : "w");
50 QL_REQUIRE(fp, "error opening file " << filename_);
51 if (!append)
52 fprintf(fp, "Id,NettingSet,DateIndex,Date,Sample,Depth,Value\n");
53 const char* fmt = "%s,%s,%lu,%s,%lu,%lu,%.4f\n";
54
55 // Get netting Set Ids (or "" if not there)
56 vector<const char*> nettingSetIds(ids.size());
57 // T0
58
59 for (const auto& [id,idx] : ids) {
60 if (nettingSetMap.find(id) != nettingSetMap.end())
61 nettingSetIds[idx] = nettingSetMap.at(id).c_str();
62 else
63 nettingSetIds[idx] = "";
64
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));
67 }
68 // Cube
69
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));
76 }
77 }
78 }
79 }
80 fclose(fp);
81}
82} // namespace analytics
83} // namespace ore
CubeWriter(const std::string &filename)
ctor
Definition: cubewriter.cpp:31
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.
Definition: cubewriter.cpp:33
A Class to write a cube out to file.