diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e67c747..1c0dbf6 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -22,4 +22,5 @@ add_example(mobser_tetra_ex OFF) add_example(mobser_tetra_ex2 OFF) add_example(mobser_tetra_sph_ex OFF) add_example(mobser_tesseroid_ex OFF) -add_example(read_IGRF_ex OFF) \ No newline at end of file +add_example(read_IGRF_ex OFF) +add_example(power_spectrum_ex OFF) \ No newline at end of file diff --git a/example/power_spectrum_ex.cpp b/example/power_spectrum_ex.cpp new file mode 100644 index 0000000..0c4cc74 --- /dev/null +++ b/example/power_spectrum_ex.cpp @@ -0,0 +1,65 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * 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 . + * + * 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/io.h" +#include "gctl/geometry.h" +#include "../lib/potential.h" + +using namespace gctl; + +int main(int argc, char const *argv[]) try +{ + std::string data_dir = "/Volumes/DataArchive/Earth/Global/MagneticModels/EMM2017_Linux"; + text_content tc; + tc.head_num_ = 1; + tc.load_text(data_dir + "/EMM2017", ".COF"); + + int n, m; + _1d_array P, C, S; + std::stringstream tmp_ss; + + C.resize(tc.lines_.size()); + S.resize(tc.lines_.size()); + for (size_t i = 0; i < tc.lines_.size(); i++) + { + str2ss(tc.lines_[i], tmp_ss); + tmp_ss >> n >> m >> C[i] >> S[i]; + } + + power_spectrum_shc(P, C, S, 6371200, 6371200, 1, 65); + + for (size_t i = 0; i < P.size(); i++) + { + std::cout << i + 1 << " " << P[i] << "\n"; + } + + return 0; +} +catch (std::exception &e) +{ + GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0); +} \ No newline at end of file diff --git a/lib/potential/gm_data.cpp b/lib/potential/gm_data.cpp index 2b2eeb9..a011f6f 100644 --- a/lib/potential/gm_data.cpp +++ b/lib/potential/gm_data.cpp @@ -239,4 +239,27 @@ void gctl::magnetic_tensors2deltaTs_sph(const array &Mag_tensors, const deltaTs[i].z = za_f*Mag_tensors[i][0][2] - hay_f*Mag_tensors[i][1][2] + hax_f*Mag_tensors[i][2][2]; } return; +} + +void gctl::power_spectrum_shc(array &P, const array &C, const array &S, double R, double r, int n, int N) +{ + if (n < 0 || n >= N || C.size() < (N + 1)*(N + 2)/2 || S.size() < (N + 1)*(N + 2)/2) + { + throw std::runtime_error("[gctl::power_spectrum_shc] Invalid input parameters."); + } + + P.resize(N - n + 1, 0); + + int id; + for (int i = n; i < N + 1; i++) + { + id = i*(i + 1)/2; + for (int j = 0; j < i + 1; j++) + { + P[i - n] += (C[id + j]*C[id + j] + S[id + j]*S[id + j]); + } + + P[i - n] *= (n + 1)*pow(R/r, 2*n + 4); + } + return; } \ No newline at end of file diff --git a/lib/potential/gm_data.h b/lib/potential/gm_data.h index c1166b8..c48d71c 100644 --- a/lib/potential/gm_data.h +++ b/lib/potential/gm_data.h @@ -196,6 +196,20 @@ namespace gctl * @param T0_declina Declination degrees of the earth normal magnetic field. */ void magnetic_tensors2deltaTs_sph(const array &Mag_tensors, const _1d_array &T0_inclina, const _1d_array &T0_declina, array &deltaTs); + + /** + * @brief Calculate the power spectrum of given spherical harmonic coefficients. + * + * @param P Returned power spectrum + * @param C Cosine coefficients strats from 0 order 0 degree up to least N order N degree + * @param S Sine coefficients strats from 0 order 0 degree up to least N order N degree + * @param R Reference radius (usually is 6371.2 km) + * @param r Observation radius + * @param n Strat order + * @param N End order + */ + void power_spectrum_shc(array &P, const array &C, const array &S, + double R, double r, int n, int N); } #endif // _GCTL_GM_DATA_H \ No newline at end of file