Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use numpy functions to convert angles between degrees and radians #7

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions src/ppigrf/ppigrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import numpy as np
import pandas as pd
import os
d2r = np.pi/180

basepath = os.path.dirname(__file__)
shc_fn = basepath + '/IGRF13.shc' # Default shc file
Expand Down Expand Up @@ -171,11 +170,12 @@ def get_legendre(theta, keys):
nmax, mmax = np.max(n), np.max(m)

theta = theta.flatten()[:, np.newaxis]
theta_rad = np.radians(theta)

P = {}
dP = {}
sinth = np.sin(d2r*theta)
costh = np.cos(d2r*theta)
sinth = np.sin(theta_rad)
costh = np.cos(theta_rad)

# Initialize Schmidt normalization
S = {}
Expand Down Expand Up @@ -325,22 +325,26 @@ def geod2geoc(gdlat, height, Bn, Bu):
a = WGS84_a
b = a*np.sqrt(1 - WGS84_e2)

sin_alpha_2 = np.sin(gdlat*d2r)**2
cos_alpha_2 = np.cos(gdlat*d2r)**2
# Convert geodetic latitude angles to radians
gdlat_rad = np.radians(gdlat)

sin_alpha_2 = np.sin(gdlat_rad)**2
cos_alpha_2 = np.cos(gdlat_rad)**2

# calculate geocentric latitude and radius
tmp = height * np.sqrt(a**2 * cos_alpha_2 + b**2 * sin_alpha_2)
beta = np.arctan((tmp + b**2)/(tmp + a**2) * np.tan(gdlat * d2r))
beta = np.arctan((tmp + b**2)/(tmp + a**2) * np.tan(gdlat_rad))
theta = np.pi/2 - beta
r = np.sqrt(height**2 + 2 * tmp + a**2 * (1 - (1 - (b/a)**4) * sin_alpha_2) / (1 - (1 - (b/a)**2) * sin_alpha_2))

# calculate geocentric components
psi = np.sin(gdlat*d2r) * np.sin(theta) - np.cos(gdlat*d2r) * np.cos(theta)
psi = np.sin(gdlat_rad) * np.sin(theta) - np.cos(gdlat_rad) * np.cos(theta)

B_r = -np.sin(psi) * Bn + np.cos(psi) * Bu
B_th = -np.cos(psi) * Bn - np.sin(psi) * Bu

theta = theta/d2r
# Convert theta to degrees
theta = np.degrees(theta)

return theta, r, B_th, B_r

Expand Down Expand Up @@ -401,7 +405,7 @@ def geoc2geod(theta, r, B_th, B_r):
A84 = 320.*E8 /2048.

GCLAT = (90-theta)
SCL = np.sin(GCLAT * d2r)
SCL = np.sin(np.radians(GCLAT))

RI = a/r
A2 = RI*(A21 + RI * (A22 + RI* A23))
Expand All @@ -418,16 +422,18 @@ def geoc2geod(theta, r, B_th, B_r):
S6CL = S2CL * C4CL + C2CL * S4CL

DLTCL = S2CL * A2 + S4CL * A4 + S6CL * A6 + S8CL * A8
gdlat = DLTCL + GCLAT * d2r
gdlat = DLTCL + np.radians(GCLAT)
height = r * np.cos(DLTCL)- a * np.sqrt(1 - E2 * np.sin(gdlat) ** 2)


# magnetic components
psi = np.sin(gdlat) * np.sin(theta*d2r) - np.cos(gdlat) * np.cos(theta*d2r)
theta_rad = np.radians(theta)
psi = np.sin(gdlat) * np.sin(theta_rad) - np.cos(gdlat) * np.cos(theta_rad)
Bn = -np.cos(psi) * B_th - np.sin(psi) * B_r
Bu = -np.sin(psi) * B_th + np.cos(psi) * B_r

gdlat = gdlat / d2r
# Convert gdlat to degrees
gdlat = np.degrees(gdlat)

return gdlat, height, Bn, Bu

Expand Down Expand Up @@ -511,8 +517,9 @@ def igrf_gc(r, theta, phi, date, coeff_fn = shc_fn):
h = h.interpolate(method = 'time').loc[date, :]

# compute cosmlon and sinmlon:
cosmphi = np.cos(phi * d2r * m) # shape (n_coords x n_model_params/2)
sinmphi = np.sin(phi * d2r * m)
phi_rad = np.radians(phi)
cosmphi = np.cos(phi_rad * m) # shape (n_coords x n_model_params/2)
sinmphi = np.sin(phi_rad * m)

# make versions of n and m that are repeated twice
nn, mm = np.tile(n, 2), np.tile(m, 2)
Expand All @@ -528,7 +535,7 @@ def igrf_gc(r, theta, phi, date, coeff_fn = shc_fn):

# calculate Bphi:
G = -(RE / r) ** (nn + 1) * mm * np.hstack((-P * sinmphi, P * cosmphi)) \
* RE / r / np.sin(theta * d2r)
* RE / r / np.sin(np.radians(theta))
Bphi = G.dot(np.hstack((g.values, h.values)).T).T # shape (n_times, n_coords)

# reshape and return
Expand Down