Skip to content
Snippets Groups Projects
CSVWriter.cpp 2.21 KiB
Newer Older
  • Learn to ignore specific revisions
  • Radim Vavřík's avatar
    Radim Vavřík committed
    #include "CSVWriter.h"
    
    namespace math1d_cl
    {
    	void CSVWriter::saveMCResult(std::shared_ptr<math1d_cl::MatData> matData, std::vector<math1d_cl::Hydrograph>& hydrographs, int nTimeSteps, std::string qFileName, std::string hFileName)
    	{
    		if(matData->getChannels().size() != hydrographs.size())
    		{
    			std::cerr << "ERROR: Hydrograph count different from original channel count!" << std::endl;
    			return;
    		}
    
    		std::ofstream qFile(qFileName.c_str());
    		//std::ofstream hFile(hFileName.c_str());
    		//std::ofstream vFile(m_vCsvFilePath.c_str()); // only zeros
    
    		std::string firstLine = "time\\id;";
    
    		for(size_t i = 0; i < matData->getChannels().size(); i++)
    		{
    			firstLine += std::to_string(matData->getChannels()[i]->getStationId());
    			firstLine += (i < (matData->getChannels().size() - 1)) ? ";" : "\n";
    
    			/*if(i < matData->getChannels().size())
    			{
    				firstLine += ";";
    			}
    			else
    			{
    				firstLine += "\n";
    			}*/
    		}
    
    		qFile << firstLine;
    		//hFile << firstLine;
    		//vFile << firstLine;
    
    		for(int i = 0; i < nTimeSteps; i++)
    		{	
    			qFile << printDateTime(matData->getPrecipitations()[i].first) << ";";
    			//vFile << printDateTime(m_precipitations[i].first);
    			//hFile << printDateTime(matData->getPrecipitations()[i].first) << ";";
    			for(size_t j = 0; j < matData->getChannels().size(); j++)
    			{
    				qFile << std::fixed << std::setprecision(6) << hydrographs.at(j).getQOut()[i];
    				//if(m_channels[j]->getHydrograph().getHOut().size() > i)
    				//hFile << std::fixed << std::setprecision(6) << hydrographs->at(j).getHOut()[i];
    				//else
    				//	hFile << ";";
    				if(j < (matData->getChannels().size() - 1))
    				{
    					qFile << ";  ";
    					//hFile << ";  ";
    				}
    			}
    			qFile << "\n";
    			//hFile << "\n";
    			//vFile << "\n";
    		}
    		qFile.close();
    		//hFile.close();
    		//vFile.close();
    	}
    
    	std::string CSVWriter::printDateTime(const time_t& dateTime)
    	{
    		std::ostringstream str;
    		struct tm * timeInfo;
    		char buffer[6];
    		timeInfo = localtime(&dateTime);
    		strftime(buffer, 6, "%H:%M", timeInfo);
    		std::string month = std::to_string(timeInfo->tm_mon + 1);
    		if(month.length() == 1)
    		{
    			month = "0" + month;
    		}
    
    		str << timeInfo->tm_year + 1900 << "-" << month << "-" << timeInfo->tm_mday << " " 
    			<< buffer;
    
    		return str.str();
    
    
    	}
    }