satellite-output-fstream-long-double-container.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Frans Laakso <frans.laakso@magister.fi>
19  */
20 
22 
23 #include "ns3/abort.h"
24 #include "ns3/log.h"
25 #include "ns3/simulator.h"
26 
27 NS_LOG_COMPONENT_DEFINE("SatOutputFileStreamLongDoubleContainer");
28 
29 namespace ns3
30 {
31 
32 TypeId
34 {
35  static TypeId tid = TypeId("ns3::SatOutputFileStreamLongDoubleContainer")
36  .SetParent<Object>()
37  .AddConstructor<SatOutputFileStreamLongDoubleContainer>();
38  return tid;
39 }
40 
42  std::string filename,
43  std::ios::openmode filemode,
44  uint32_t valuesInRow)
45  : m_outputFileStreamWrapper(),
46  m_outputFileStream(),
47  m_container(),
48  m_fileName(filename),
49  m_fileMode(filemode),
50  m_valuesInRow(valuesInRow),
51  m_printFigure(false),
52  m_figureUnitConversionType(RAW)
53 {
54  NS_LOG_FUNCTION(this << m_fileName << m_fileMode);
55 
56  if (!(m_valuesInRow > 0))
57  {
58  NS_FATAL_ERROR("SatOutputFileStreamLongDoubleContainer::"
59  "SatOutputFileStreamLongDoubleContainer - No values in the row");
60  }
61 }
62 
64  : m_outputFileStreamWrapper(),
65  m_outputFileStream(),
66  m_container(),
67  m_fileName(),
68  m_fileMode(),
69  m_valuesInRow(),
70  m_printFigure(),
71  m_figureUnitConversionType()
72 {
73  NS_LOG_FUNCTION(this);
74  NS_FATAL_ERROR("SatOutputFileStreamLongDoubleContainer::SatOutputFileStreamLongDoubleContainer "
75  "- Constructor not in use");
76 }
77 
79 {
80  NS_LOG_FUNCTION(this);
81 
82  Reset();
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION(this);
89 
90  Reset();
91  Object::DoDispose();
92 }
93 
94 void
96 {
97  NS_LOG_FUNCTION(this);
98 
99  OpenStream();
100 
101  if (m_outputFileStream->is_open())
102  {
103  for (uint32_t i = 0; i < m_container.size(); i++)
104  {
105  for (uint32_t j = 0; j < m_valuesInRow; j++)
106  {
107  if (j + 1 == m_valuesInRow)
108  {
109  *m_outputFileStream << m_container[i].at(j);
110  }
111  else
112  {
113  *m_outputFileStream << m_container[i].at(j) << "\t";
114  }
115  }
116  *m_outputFileStream << std::endl;
117  }
118  m_outputFileStream->close();
119  }
120  else
121  {
122  NS_ABORT_MSG("Output stream is not valid for writing.");
123  }
124 
125  if (m_printFigure)
126  {
127  PrintFigure();
128  }
129 
130  Reset();
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION(this);
137 
138  Gnuplot2dDataset dataset = GetGnuplotDataset();
139  Gnuplot plot = GetGnuplot();
140  plot.AddDataset(dataset);
141 
142  std::string plotFileName = m_fileName + ".plt";
143  std::ofstream plotFile(plotFileName.c_str());
144  plot.GenerateOutput(plotFile);
145  plotFile.close();
146 
147  std::string conversionCommand = "gnuplot " + m_fileName + ".plt";
148 
149  int result = system(conversionCommand.c_str());
150 
151  if (result < 0)
152  {
153  std::cout << "Unable to open shell process for Gnuplot file conversion for " << m_fileName
154  << ", conversion not done!" << std::endl;
155  }
156 }
157 
158 void
160 {
161  NS_LOG_FUNCTION(this);
162 
163  if (newItem.size() != m_valuesInRow)
164  {
165  NS_FATAL_ERROR(
166  "SatOutputFileStreamLongDoubleContainer::AddToContainer - Invalid vector size");
167  }
168 
169  m_container.push_back(newItem);
170 }
171 
172 void
174 {
175  NS_LOG_FUNCTION(this);
176 
179 }
180 
181 void
183 {
184  NS_LOG_FUNCTION(this);
185 
186  ResetStream();
187  ClearContainer();
188 }
189 
190 void
192 {
193  NS_LOG_FUNCTION(this);
194 
195  if (m_outputFileStreamWrapper != NULL)
196  {
199  }
200  m_outputFileStream = 0;
201 
202  m_fileName = "";
203  m_fileMode = std::ofstream::out;
204 }
205 
206 void
208 {
209  NS_LOG_FUNCTION(this);
210 
211  if (!m_container.empty())
212  {
213  for (uint32_t i = 0; i < m_container.size(); i++)
214  {
215  if (!m_container[i].empty())
216  {
217  m_container[i].clear();
218  }
219  }
220  m_container.clear();
221  }
222 
223  m_valuesInRow = 0;
224 }
225 
226 Gnuplot2dDataset
228 {
229  NS_LOG_FUNCTION(this);
230 
231  Gnuplot2dDataset ret;
232  ret.SetTitle(m_title);
233  ret.SetStyle(Gnuplot2dDataset::LINES);
234 
235  if (!m_container.empty())
236  {
237  switch (m_valuesInRow)
238  {
239  case 2: {
240  for (uint32_t i = 0; i < m_container.size(); i++)
241  {
242  ret.Add(m_container[i].at(0), ConvertValue(m_container[i].at(1)));
243  }
244  break;
245  }
246  default: {
247  NS_ABORT_MSG("SatOutputFileStreamLongDoubleContainer::GetGnuplotDataset - Figure "
248  "output not implemented for "
249  << m_valuesInRow << " columns.");
250  }
251  }
252  }
253  return ret;
254 }
255 
256 long double
258 {
259  NS_LOG_FUNCTION(this << value);
260 
262  {
263  case RAW: {
264  return value;
265  }
266  case DECIBEL: {
267  if (value > 0)
268  {
269  return 10.0 * std::log10(value);
270  }
271  return 10.0 * std::log10(std::numeric_limits<long double>::min());
272  }
273  case DECIBEL_AMPLITUDE: {
274  if (value > 0)
275  {
276  return 20.0 * std::log10(value);
277  }
278  return 20.0 * std::log10(std::numeric_limits<long double>::min());
279  }
280  default: {
281  NS_ABORT_MSG(
282  "SatOutputFileStreamLongDoubleContainer::ConvertValue - Invalid conversion type.");
283  break;
284  }
285  }
286  return -1;
287 }
288 
289 Gnuplot
291 {
292  NS_LOG_FUNCTION(this);
293 
294  Gnuplot ret(m_fileName + ".png");
295  ret.SetTitle(m_title);
296  ret.SetTerminal("png");
297  ret.SetLegend(m_legendY, m_legendX);
298  ret.AppendExtra(m_keyPosition);
299  ret.AppendExtra("set grid xtics mxtics ytics");
300  return ret;
301 }
302 
303 void
305  std::string title,
306  std::string legendY,
307  std::string legendX,
308  std::string keyPosition,
309  FigureUnitConversion_t figureUnitConversionType)
310 {
311  NS_LOG_FUNCTION(this);
312 
313  m_printFigure = true;
314  m_title = title;
315  m_legendY = legendY;
316  m_legendX = legendX;
317  m_keyPosition = keyPosition;
318  m_figureUnitConversionType = figureUnitConversionType;
319 }
320 
321 } // namespace ns3
void AddToContainer(std::vector< long double > newItem)
Function for adding the values to container.
void PrintFigure()
Function for printing the container contents into a figure.
std::vector< std::vector< long double > > m_container
Container for value rows.
bool m_printFigure
Enable / disable printing of container contents into a figure.
void WriteContainerToFile()
Function for writing the container contents to file.
FigureUnitConversion_t m_figureUnitConversionType
Describes which unit conversion should be used with the figure.
void EnableFigureOutput(std::string title, std::string legendY, std::string legendX, std::string keyPosition, FigureUnitConversion_t figureUnitConversionType)
Function for enabling the figure output.
SatOutputFileStreamWrapper * m_outputFileStreamWrapper
Pointer to output file stream wrapper.
long double ConvertValue(long double value)
Function for converting the container data samples.
void OpenStream()
Function for opening the output file stream.
Gnuplot2dDataset GetGnuplotDataset()
Function for creating Gnuplot datasets.
A class encapsulating an STL output stream.
std::ofstream * GetStream(void)
Return a pointer to an ofstream previously set in the wrapper.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.