/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "netcdf_io.h"

#ifdef GCTL_NETCDF

void gctl::show_netcdf_info(std::string filename, std::ostream& out_stream)
{
	if (filename == "")
	{
		throw domain_error("The input filename is empty. From read_netcdf_axis(...)");
	}

	// 判断文件名结尾是否为.nc如果不是则添加.nc结尾
	std::string full_name;
	if (filename.length() <= 3 || filename.substr(filename.length()-3, filename.length()) != ".nc")
	{
		full_name = filename + ".nc";
	}
	else full_name = filename;

	NcFile input_nc(full_name.c_str(), NcFile::ReadOnly);
	if (!input_nc.is_valid())
	{
		std::string err_str = "Can't open file: " + filename + ". From show_netcdf_info(...)";
		throw runtime_error(err_str);
	}

	std::string type_str;
	NcFile::FileFormat file_type = input_nc.get_format();
	if (file_type == NcFile::Classic)
		type_str = "Classic";
	else if (file_type == NcFile::Offset64Bits)
		type_str = "Offset64Bits";
	else if (file_type == NcFile::Netcdf4)
		type_str = "Netcdf4";
	else if (file_type == NcFile::Netcdf4Classic)
		type_str = "Netcdf4Classic";

	out_stream << "File: " << full_name << " | Format: " << type_str << " | ";
	out_stream << input_nc.num_dims() << " dimension(s) | ";
	out_stream << input_nc.num_vars() << " variable(s) | ";
	out_stream << input_nc.num_atts() << " attribute(s)" << std::endl;
	out_stream << "----------" << std::endl;

	NcDim *a_dim;
	for (int i = 0; i < input_nc.num_dims(); i++)
	{
		a_dim = input_nc.get_dim(i);
		out_stream << "Dimension: " << a_dim->name() << " | size: " << a_dim->size() << std::endl;
	}
	out_stream << "----------" << std::endl;

	NcType var_type;
	NcVar *a_var;
	for (int n = 0; n < input_nc.num_vars(); n++)
	{
		a_var = input_nc.get_var(n);
		out_stream << "Variable: " << a_var->name() << " | ";

		NcDim *a_var_dim;
		out_stream << "dimension: ";
		for (int i = 0; i < a_var->num_dims(); i++)
		{
			a_var_dim = a_var->get_dim(i);
			out_stream << a_var_dim->name() << ", ";
		}

		out_stream << "; size: " << a_var->num_vals();

		var_type = a_var->type();
		if (var_type == ncByte)
			out_stream << " ncByte";
		else if (var_type == ncChar)
			out_stream << " ncChar";
		else if (var_type == ncShort)
			out_stream << " ncShort";
		else if (var_type == ncInt)
			out_stream << " ncInt";
		else if (var_type == ncFloat)
			out_stream << " ncFloat";
		else if (var_type == ncDouble)
			out_stream << " ncDouble";

		NcAtt *a_var_att;
		NcValues *a_var_att_val;
		out_stream << " ; attribute: ";
		for (int i = 0; i < a_var->num_atts(); i++)
		{
			a_var_att = a_var->get_att(i);

			a_var_att_val = a_var_att->values();
			var_type = a_var_att->type();

			if (var_type == ncByte)
			{
				out_stream << a_var_att->name() << " ";
				for (int i = 0; i < a_var_att_val->num(); i++)
				{
					out_stream << a_var_att_val->as_ncbyte(i);
				}
				out_stream << ", ";
			}
			else if (var_type == ncChar)
			{
				out_stream << a_var_att->name() << " ";
				for (int i = 0; i < a_var_att_val->num(); i++)
				{
					out_stream << a_var_att_val->as_char(i);
				}
				out_stream << ", ";
			}
			else if (var_type == ncShort)
			{
				out_stream << a_var_att->name() << " (";
				out_stream << a_var_att_val->as_short(0);
				for (int i = 1; i < a_var_att_val->num(); i++)
				{
					out_stream << ", " << a_var_att_val->as_short(i);
				}
				out_stream << "), ";
			}
			else if (var_type == ncInt)
			{
				out_stream << a_var_att->name() << " (";
				out_stream << a_var_att_val->as_int(0);
				for (int i = 1; i < a_var_att_val->num(); i++)
				{
					out_stream << ", " << a_var_att_val->as_int(i);
				}
				out_stream << "), ";
			}
			else if (var_type == ncFloat)
			{
				out_stream << a_var_att->name() << " (";
				out_stream << a_var_att_val->as_float(0);
				for (int i = 1; i < a_var_att_val->num(); i++)
				{
					out_stream << ", " << a_var_att_val->as_float(i);
				}
				out_stream << "), ";
			}
			else if (var_type == ncDouble)
			{
				out_stream << a_var_att->name() << " (";
				out_stream << a_var_att_val->as_double(0);
				for (int i = 1; i < a_var_att_val->num(); i++)
				{
					out_stream << ", " << a_var_att_val->as_double(i);
				}
				out_stream << "), ";
			}
			delete a_var_att_val;
		}
		out_stream << std::endl;
	}
	out_stream << "----------" << std::endl;

	NcAtt *a_att;
	NcValues *a_att_val;
	for (int i = 0; i < input_nc.num_atts(); i++)
	{
		a_att = input_nc.get_att(i);
		out_stream << "Attribute: " << a_att->name() << " | ";

		out_stream << a_att->num_vals();

		var_type = a_att->type();
		if (var_type == ncByte)
			out_stream << " ncByte: ";
		else if (var_type == ncChar)
			out_stream << " ncChar: ";
		else if (var_type == ncShort)
			out_stream << " ncShort: ";
		else if (var_type == ncInt)
			out_stream << " ncInt: ";
		else if (var_type == ncFloat)
			out_stream << " ncFloat: ";
		else if (var_type == ncDouble)
			out_stream << " ncDouble: ";

		a_att_val = a_att->values();
		if (var_type == ncByte)
		{
			for (int i = 0; i < a_att_val->num(); i++)
			{
				out_stream << a_att_val->as_ncbyte(i);
			}
		}
		else if (var_type == ncChar)
		{
			for (int i = 0; i < a_att_val->num(); i++)
			{
				out_stream << a_att_val->as_char(i);
			}
		}
		else if (var_type == ncShort)
		{
			out_stream << " (" << a_att_val->as_short(0);
			for (int i = 1; i < a_att_val->num(); i++)
			{
				out_stream << ", " << a_att_val->as_short(i);
			}
			out_stream << ")";
		}
		else if (var_type == ncInt)
		{
			out_stream << " (" << a_att_val->as_int(0);
			for (int i = 1; i < a_att_val->num(); i++)
			{
				out_stream << ", " << a_att_val->as_int(i);
			}
			out_stream << ")";
		}
		else if (var_type == ncFloat)
		{
			out_stream << " (" << a_att_val->as_float(0);
			for (int i = 1; i < a_att_val->num(); i++)
			{
				out_stream << ", " << a_att_val->as_float(i);
			}
			out_stream << ")";
		}
		else if (var_type == ncDouble)
		{
			out_stream << " (" << a_att_val->as_double(0);
			for (int i = 1; i < a_att_val->num(); i++)
			{
				out_stream << ", " << a_att_val->as_double(i);
			}
			out_stream << ")";
		}
		out_stream << std::endl;

		delete a_att_val;
	}
	out_stream << "==========" << std::endl;
	return;
}

#endif // GCTL_NETCDF