Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
CubeWriter Class Reference

Write an NPV cube to a human readable text file. More...

#include <orea/cube/cubewriter.hpp>

+ Collaboration diagram for CubeWriter:

Public Member Functions

 CubeWriter (const std::string &filename)
 ctor More...
 
const std::string & filename ()
 Return the filename this writer is writing too. More...
 
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. More...
 

Private Attributes

std::string filename_
 

Detailed Description

Write an NPV cube to a human readable text file.

Definition at line 37 of file cubewriter.hpp.

Constructor & Destructor Documentation

◆ CubeWriter()

CubeWriter ( const std::string &  filename)

ctor

Definition at line 31 of file cubewriter.cpp.

const std::string & filename()
Return the filename this writer is writing too.
Definition: cubewriter.hpp:43

Member Function Documentation

◆ filename()

const std::string & filename ( )

Return the filename this writer is writing too.

Definition at line 43 of file cubewriter.hpp.

43{ return filename_; }

◆ write()

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 at line 33 of file cubewriter.cpp.

34 {
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}
+ Here is the caller graph for this function:

Member Data Documentation

◆ filename_

std::string filename_
private

Definition at line 50 of file cubewriter.hpp.