#include "field.h"
#include <fstream>
#include "cenos_exception.h"

Field::Field()
{
	nr_of_nodes = 0;
}
Field::~Field()
{
}

Field::Field(std::string tempfile) //read temperature files
{
	std::ifstream file_stream;
	file_stream.open(tempfile);

	if (!file_stream.is_open())
	{
		throw cenos_exception("Could not open temperature file");
	}
	else
	{
		std::string line;
		getline(file_stream, line);
		std::istringstream line_stream1(line);
		int nr;
		line_stream1 >> nr;
		nr_of_nodes = nr;
		// read all temperatures from previous temperature file
		while (file_stream.good())
		{
			getline(file_stream, line);
			std::istringstream line_stream(line);
			int node_nr;
			double temp_value;
			line_stream >> node_nr >> temp_value;
			values.push_back(temp_value);
			nodes.push_back(node_nr);
		}
	}
	file_stream.close();
}


double Field::getValueByNodeNr(int nr) //get specific node's temperature
{
	for (int i = 0; i < nr_of_nodes; i++)
	{
		if (nodes[i] == nr)
			return values[i];
	}
}
void Field::operator=(const Field& that)
{
	nodes = that.nodes;
	values = that.values;
	nr_of_nodes = that.nr_of_nodes;
}