/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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.
 ******************************************************/

#ifndef _GCTL_GMT_H
#define _GCTL_GMT_H

#include <vector>
#include <exception>
#include <stdexcept>
#include <ctime>

#include "gmt/gmt.h"
#include "../io/file_io.h"
#include "../io/netcdf_io.h"

#ifndef GMT_VF_LEN
#define GMT_VF_LEN 16
#endif

namespace gctl
{
    /**
     * @brief 这是一个gmt命令管道库
     */
    class gmt
    {
    public:
        /**
         * @brief Construct a new gmt object
         */
        gmt();
        // destructor
        virtual ~gmt();

        /**
         * @brief Start a session.
         * 
         * @param pic_name Output picture name.
         * @param pic_type Output picture type. Multiple types can be specified by comma.
         * @param name Session name.
         */
        void begin_session(const std::string& pic_name, const std::string& pic_type = "eps", 
            const std::string& name = "gctl_gmt_session");

        /**
         * @brief Destroy a session object
         * 
         * @param show Whether to show the result after finish the plotting.
         * @param clear_tmp Whether to clear the temporary files.
         */
        void end_session(bool show = false, bool clear_tmp = true);

        /**
         * @brief Save the session to a file.
         *
         * @param filename File name.
         */
        void save_session(const std::string& filename);

        /**
         * @brief Call a GMT module.
         * 
         * @param module Name of the module.
         * @param cmd Module command.
         */
        void call_module(const std::string& module, const std::string& cmd);

        /**
         * @brief Create a temporary grid file that could be used for plotting.
         * 
         * @param data Data array.
         * @param xnum Number of data points in x direction.
         * @param ynum Number of data points in y direction.
         * @param xmin minimal x coordinate of the data.
         * @param dx increment of x coordinate of the data.
         * @param ymin minimal y coordinate of the data.
         * @param dy increment of y coordinate of the data.
         * @return Name of the virtual grid which could be used for plotting.
         */
        std::string create_grid(const array<double> &data, int xnum, int ynum, 
            double xmin, double dx, double ymin, double dy);

    private:
        gmt(gmt const&) = delete;
        void operator=(gmt const&) = delete;

        bool seesion_ok_;
        void* GMT_API_;
        std::string session_name_;
        std::vector<std::string> buffer_;
        std::vector<std::string> tmpfiles_;
    };
};

#endif // _GCTL_GMT_H