InfoOutputSeries.C
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 #ifndef TRANSPORT_INFOOUTPUTSERIES_C_DEFINED
00039 #define TRANSPORT_INFOOUTPUTSERIES_C_DEFINED
00040 
00041 #include "Transport/InfoOutputSeries.H"
00042 
00043 #include "Component/GlobalOpts.H"
00044 #include "Image/Dims.H"
00045 #include "Raster/GenericFrame.H"
00046 #include "Util/FileUtil.H"
00047 #include "Util/log.H"
00048 #include "rutz/time.h"
00049 
00050 #include <map>
00051 
00052 
00053 struct InfoOutputSeries::Impl
00054 {
00055   struct ChanInfo
00056   {
00057     ChanInfo() : spec(), nframes(0) {}
00058 
00059     GenericFrameSpec spec;
00060     int nframes;
00061     rutz::time first_time;
00062     rutz::time last_time;
00063   };
00064 
00065   typedef std::map<std::string, ChanInfo> map_type;
00066 
00067   map_type infoMap;
00068   std::string fileName;
00069 };
00070 
00071 
00072 InfoOutputSeries::InfoOutputSeries(OptionManager& mgr)
00073   :
00074   FrameOstream(mgr, "InfoOutputSeries", "Info Output Series"),
00075   itsTestMode(&OPT_TestMode, this),
00076   rep(new Impl)
00077 {}
00078 
00079 
00080 InfoOutputSeries::~InfoOutputSeries()
00081 {
00082   delete rep;
00083 }
00084 
00085 
00086 void InfoOutputSeries::setConfigInfo(const std::string& filename)
00087 {
00088   
00089   
00090   
00091 
00092   this->setFileName(filename);
00093 }
00094 
00095 
00096 void InfoOutputSeries::writeFrame(const GenericFrame& frame,
00097                                   const std::string& shortname,
00098                                   const FrameInfo& auxinfo)
00099 {
00100   const rutz::time t = rutz::time::wall_clock_now();
00101 
00102   Impl::ChanInfo& info = rep->infoMap[shortname];
00103 
00104   info.spec = frame.frameSpec();
00105   if (info.nframes == 0)
00106     info.first_time = t;
00107   info.last_time = t;
00108   info.nframes++;
00109 }
00110 
00111 
00112 void InfoOutputSeries::stop2()
00113 {
00114   try
00115     {
00116       bool owned = false;
00117       FILE* f = stdOutputFileOpen(rep->fileName, &owned); 
00118 
00119       for (Impl::map_type::const_iterator
00120              itr = rep->infoMap.begin(),
00121              stop = rep->infoMap.end();
00122            itr != stop; ++itr)
00123         {
00124           const double secs =
00125             ((*itr).second.last_time - (*itr).second.first_time).sec();
00126 
00127           const double fps =
00128             (*itr).second.nframes >= 2
00129             ? ((*itr).second.nframes - 1) / secs
00130             : 0.0;
00131 
00132           if (itsTestMode.getVal())
00133             fprintf(f, "InfoOutputSeries: summary[%s]: %d frames (%s)\n",
00134                     (*itr).first.c_str(),
00135                     (*itr).second.nframes,
00136                     (*itr).second.spec.getDescription().c_str());
00137           else
00138             fprintf(f, "InfoOutputSeries: summary[%s]: %d frames @ %.2ffps (%s)\n",
00139                     (*itr).first.c_str(),
00140                     (*itr).second.nframes,
00141                     fps,
00142                     (*itr).second.spec.getDescription().c_str());
00143         }
00144 
00145       fflush(f);
00146 
00147       if (owned)
00148         fclose(f);
00149     }
00150   catch (...)
00151     {
00152       
00153       
00154     }
00155 }
00156 
00157 
00158 void InfoOutputSeries::closeStream(const std::string& shortname)
00159 {
00160   Impl::map_type::iterator itr = rep->infoMap.find(shortname);
00161   if (itr != rep->infoMap.end())
00162     rep->infoMap.erase(itr);
00163 }
00164 
00165 
00166 void InfoOutputSeries::setFileName(const std::string& filename)
00167 {
00168   rep->fileName = filename;
00169 }
00170 
00171 
00172 
00173 
00174 
00175 
00176 
00177 
00178 #endif // TRANSPORT_INFOOUTPUTSERIES_C_DEFINED