satellite-output-fstream-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("SatOutputFileStreamDoubleContainer");
28 
29 namespace ns3
30 {
31 
32 TypeId
34 {
35  static TypeId tid = TypeId("ns3::SatOutputFileStreamDoubleContainer")
36  .SetParent<Object>()
37  .AddConstructor<SatOutputFileStreamDoubleContainer>();
38  return tid;
39 }
40 
42  std::ios::openmode filemode,
43  uint32_t valuesInRow)
44  : m_outputFileStreamWrapper(),
45  m_outputFileStream(),
46  m_container(),
47  m_fileName(filename),
48  m_fileMode(filemode),
49  m_valuesInRow(valuesInRow),
50  m_printFigure(false),
51  m_figureUnitConversionType(RAW),
52  m_style(Gnuplot2dDataset::LINES)
53 {
54  NS_LOG_FUNCTION(this << m_fileName << m_fileMode);
55 
56  if (!(m_valuesInRow > 0))
57  {
58  NS_FATAL_ERROR("SatOutputFileStreamDoubleContainer::SatOutputFileStreamDoubleContainer - "
59  "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  m_style()
73 {
74  NS_LOG_FUNCTION(this);
75  NS_FATAL_ERROR("SatOutputFileStreamDoubleContainer::SatOutputFileStreamDoubleContainer - "
76  "Constructor not in use");
77 }
78 
80 {
81  NS_LOG_FUNCTION(this);
82 
83  Reset();
84 }
85 
86 void
88 {
89  NS_LOG_FUNCTION(this);
90 
91  Reset();
92  Object::DoDispose();
93 }
94 
95 void
97 {
98  NS_LOG_FUNCTION(this);
99 
100  OpenStream();
101 
102  if (m_outputFileStream->is_open())
103  {
104  for (uint32_t i = 0; i < m_container.size(); i++)
105  {
106  for (uint32_t j = 0; j < m_valuesInRow; j++)
107  {
108  if (j + 1 == m_valuesInRow)
109  {
110  *m_outputFileStream << m_container[i].at(j);
111  }
112  else
113  {
114  *m_outputFileStream << m_container[i].at(j) << "\t";
115  }
116  }
117  *m_outputFileStream << std::endl;
118  }
119  m_outputFileStream->close();
120  }
121  else
122  {
123  NS_ABORT_MSG("Output stream is not valid for writing.");
124  }
125 
126  if (m_printFigure)
127  {
128  PrintFigure();
129  }
130 
131  Reset();
132 }
133 
134 void
136 {
137  NS_LOG_FUNCTION(this);
138 
139  Gnuplot2dDataset dataset = GetGnuplotDataset();
140  Gnuplot plot = GetGnuplot();
141  plot.AddDataset(dataset);
142 
143  std::string plotFileName = m_fileName + ".plt";
144  std::ofstream plotFile(plotFileName.c_str());
145  plot.GenerateOutput(plotFile);
146  plotFile.close();
147 
148  std::string conversionCommand = "gnuplot " + m_fileName + ".plt";
149 
150  int result = system(conversionCommand.c_str());
151 
152  if (result < 0)
153  {
154  std::cout << "Unable to open shell process for Gnuplot file conversion for " << m_fileName
155  << ", conversion not done!" << std::endl;
156  }
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION(this);
163 
164  if (newItem.size() != m_valuesInRow)
165  {
166  NS_FATAL_ERROR("SatOutputFileStreamDoubleContainer::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(m_style);
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("SatOutputFileStreamDoubleContainer::GetGnuplotDataset - Figure output "
248  "not implemented for "
249  << m_valuesInRow << " columns.");
250  }
251  }
252  }
253  return ret;
254 }
255 
256 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<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<double>::min());
279  }
280  default: {
281  NS_ABORT_MSG("SatOutputFileStreamDoubleContainer::ConvertValue - Invalid conversion type.");
282  break;
283  }
284  }
285  return -1;
286 }
287 
288 Gnuplot
290 {
291  NS_LOG_FUNCTION(this);
292 
293  Gnuplot ret(m_fileName + ".png");
294  ret.SetTitle(m_title);
295  ret.SetTerminal("png");
296  ret.SetLegend(m_legendY, m_legendX);
297  ret.AppendExtra(m_keyPosition);
298  ret.AppendExtra("set grid xtics mxtics ytics");
299  return ret;
300 }
301 
302 void
304  std::string title,
305  std::string legendY,
306  std::string legendX,
307  std::string keyPosition,
308  FigureUnitConversion_t figureUnitConversionType,
309  Gnuplot2dDataset::Style style)
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  m_style = style;
320 }
321 
322 } // namespace ns3
double ConvertValue(double value)
Function for converting the container data samples.
void OpenStream()
Function for opening the output file stream.
std::vector< std::vector< double > > m_container
Container for value rows.
SatOutputFileStreamWrapper * m_outputFileStreamWrapper
Pointer to output file stream wrapper.
bool m_printFigure
Enable / disable printing of container contents into a figure.
Gnuplot2dDataset GetGnuplotDataset()
Function for creating Gnuplot datasets.
void ClearContainer()
Function for clearing the container.
Gnuplot2dDataset::Style m_style
2D dataset figure style
FigureUnitConversion_t m_figureUnitConversionType
Describes which unit conversion should be used with the figure.
void AddToContainer(std::vector< double > newItem)
Function for adding the values to container.
void PrintFigure()
Function for printing the container contents into a figure.
std::ofstream * m_outputFileStream
Pointer to output file stream.
void EnableFigureOutput(std::string title, std::string legendY, std::string legendX, std::string keyPosition, FigureUnitConversion_t figureUnitConversionType, Gnuplot2dDataset::Style style)
Function for enabling the figure output.
void WriteContainerToFile()
Function for writing the container contents to file.
static TypeId GetTypeId(void)
NS-3 function for type id.
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.