gctl_potential/example/forward_mag_shc.cpp

99 lines
3.8 KiB
C++
Raw Normal View History

2025-05-08 16:33:21 +08:00
/********************************************************
*
*
*
*
*
*
* 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"
using namespace gctl;
int main(int argc, char *argv[]) try
{
// Read SHC coefficients
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);
// Prepare SHC data
//shc_data sd;
//sd.init(gnm, hnm, 1, 0, 450, 450);
// Prepare observation sites
array<point3ds> obsp;
grid_points_2d(obsp, -179.5, 179.5, -89.5, 89.5, 1.0, 1.0, 1737.4 + 30.0, TopLeft);
// Calculate over all observation sites
int id;
double rad, fx, fy, fz;
_1d_array P, dP;
_1d_array Br(obsp.size(), 0.0), Bt(obsp.size(), 0.0), Bp(obsp.size(), 0.0);
progress_bar bar(obsp.size(), "Progress");
for (int p = 0; p < obsp.size(); p++)
{
bar.progressed(p);
rad = obsp[p].rad;
schmidt_legendre(90.0 - obsp[p].lat, 450, P, dP);
// 注意跳过第0阶0次项
for (size_t i = 1; i <= 450; i++)
{
id = i*(i + 1)/2;
fx = pow(1737.4/rad, i+2);
fz = -1.0*(i+1)*pow(1737.4/rad, i+2);
for (size_t j = 0; j < i+1; j++)
{
fy = j/sind(90.0 - obsp[p].lat)*pow(1737.4/rad, i+2);
Bt[p] += -1.0*fx*(cosd(j*obsp[p].lon)*dP[id + j]*gnm[id + j] + sind(j*obsp[p].lon)*dP[id + j]*hnm[id + j]);
Bp[p] += fy*(sind(j*obsp[p].lon)*P[id + j]*gnm[id + j] - cosd(j*obsp[p].lon)*P[id + j]*hnm[id + j]);
Br[p] += fz*(cosd(j*obsp[p].lon)*P[id + j]*gnm[id + j] + sind(j*obsp[p].lon)*P[id + j]*hnm[id + j]);
}
}
}
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");
return 0;
}
catch (std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}