/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2022  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 "gctl/core.h"
#include "gctl/io.h"
#include "gctl/potential.h"
// 多线程异步头文件
#include <thread>
#include <future>

using namespace gctl;

shc_data sd;
array<point3ds> obsp;

// Read SHC coefficients
int read_shc_file()
{
    dsv_io shc_in;
    shc_in.head_number(9);
    shc_in.load_text("data/Ravaetal2020/LP1999_L1_d450", ".cof");
    shc_in.info(HeadInfo|ColInfo);

    _1d_array gnm, hnm;
    shc_in.get_column(gnm, 3);
    shc_in.get_column(hnm, 4);

    sd.init(gnm, hnm, 1, 0, 450, 450, 1737.4);

    std::cout << "Done reading SHC coefficients\n";
    return 0;
}

// Prepare observation sites
int init_observations()
{
    grid_points_2d(obsp, -179.5, 179.5, -89.5, 89.5, 1.0, 1.0, 1737.4 + 100.0, TopLeft);

    std::cout << "Done initializing obervations points\n";
    return 0;
}

// Main process
int main(int argc, char *argv[]) try
{
    std::future<int> future1 = std::async(std::launch::async, read_shc_file);
    std::future<int> future2 = std::async(std::launch::async, init_observations);

    int result1 = future1.get();
    int result2 = future2.get();
    if (result1 != 0 || result2 != 0)
        throw std::runtime_error("Terminated with unexpected errors.");

    // Calculate over all observation sites
    array<point3dc> B;
    magobser(B, sd, obsp, FullMsg);

    array<double> Br = B.extract<double>([](const point3dc &b) -> double {return b.z;});
    array<double> Bt = B.extract<double>([](const point3dc &b) -> double {return b.x;});
    array<double> Bp = B.extract<double>([](const point3dc &b) -> double {return b.y;});

    // Save resutls
    geodsv_io magout;
    magout.init_table(obsp.size(), 6);
    magout.column_names({"lon", "lat", "rad", "Br", "Bt", "Bp"});
    magout.fill_column_point3ds(obsp, "rad", "lon", "lat");
    magout.fill_column(Br, "Br");
    magout.fill_column(Bt, "Bt");
    magout.fill_column(Bp, "Bp");
    magout.save_csv("data/Ravaetal2020/LP1999_L1_d450_30km_grid");

    // Create netcdf files
    save_netcdf_grid("data/Ravaetal2020/LP1999_L1_d450_30km_grid", Br, 360, 180, -179.5, 1.0, -89.5, 1.0, TopLeft, "lon", "lat", "Br");
    append_netcdf_grid("data/Ravaetal2020/LP1999_L1_d450_30km_grid", Bt, "lon", "lat", "Bt", TopLeft);
    append_netcdf_grid("data/Ravaetal2020/LP1999_L1_d450_30km_grid", Bp, "lon", "lat", "Bp", TopLeft);
    return 0;
}
catch (std::exception &e)
{
    GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}