-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #610 from nickssl/master
Added sm2mlt
- Loading branch information
Showing
5 changed files
with
175 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
""" | ||
Convert Cartesian coordinates to spherical coordinates. | ||
This function is similar to cart2spc.pro in IDL SPEDAS. | ||
""" | ||
|
||
import numpy as np | ||
|
||
|
||
def cart2spc(x, y, z): | ||
""" | ||
Convert Cartesian coordinates to spherical coordinates. | ||
Parameters: | ||
----------- | ||
x : float | ||
X coordinate in Cartesian system. | ||
y : float | ||
Y coordinate in Cartesian system. | ||
z : float | ||
Z coordinate in Cartesian system. | ||
Returns: | ||
-------- | ||
r : float | ||
Radial distance from the origin. | ||
theta : float | ||
Polar angle (angle from the positive z-axis to the point) in radians. It ranges from [0, pi]. | ||
phi : float | ||
Azimuthal angle (angle in the xy-plane from the positive x-axis to the point) in radians. It ranges from [0, 2*pi]. | ||
Notes: | ||
------ | ||
- Uses the following equations for conversion: | ||
r = sqrt(x^2 + y^2 + z^2) | ||
theta = arccos(z/r) | ||
phi = 2*pi - arccos(x/sqrt(x^2 + y^2)) if y < 0, otherwise arccos(x/sqrt(x^2 + y^2)) | ||
""" | ||
|
||
x, y, z = np.array(x), np.array(y), np.array(z) | ||
r = np.sqrt(x**2 + y**2 + z**2) | ||
|
||
# Calculate phi | ||
maskn = np.where(y < 0, 1, 0) | ||
maskp = np.where(y > 0, 1, 0) | ||
phi = 2*np.pi - maskn * np.arccos(x/np.sqrt(x**2 + y**2)) + maskp * np.arccos(x/np.sqrt(x**2 + y**2)) | ||
|
||
# Calculate theta | ||
theta = np.arccos(z/r) | ||
|
||
return r, theta, phi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
''' | ||
Convert Solar Magnetic (SM) coordinates to Magnetic Local Time (MLT). | ||
This function is similar to sm2mlt.pro in IDL SPEDAS. | ||
''' | ||
import numpy as np | ||
from pyspedas import cart2spc | ||
|
||
|
||
def sm2mlt(x_sm, y_sm, z_sm): | ||
""" | ||
Convert Solar Magnetic (SM) coordinates to Magnetic Local Time (MLT). | ||
Parameters: | ||
----------- | ||
x_sm : float | ||
X coordinate in Solar Magnetic system. | ||
y_sm : float | ||
Y coordinate in Solar Magnetic system. | ||
z_sm : float | ||
Z coordinate in Solar Magnetic system. | ||
Returns: | ||
-------- | ||
mlt_0 : float | ||
Magnetic Local Time. | ||
Notes: | ||
------ | ||
- First, the SM coordinates are converted to spherical coordinates. | ||
- The azimuthal angle from the conversion (phi) is then used to calculate MLT. | ||
- If the resulting MLT is greater than 24, it is corrected by subtracting 24. | ||
""" | ||
|
||
# Convert to spherical coordinates | ||
r, theta, phi = cart2spc(x_sm, y_sm, z_sm) | ||
|
||
# Calculate initial MLT | ||
mlt_0 = 12.0 + phi * 24.0 / (2.0 * np.pi) | ||
|
||
# Fix MLTs greater than 24 | ||
mlt_0[mlt_0 > 24.0] -= 24.0 | ||
|
||
return mlt_0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
''' | ||
Convert spherical coordinates (r, theta, phi) to Cartesian coordinates (x, y, z). | ||
This function is similar to spc2cart.pro in IDL SPEDAS. | ||
''' | ||
import numpy as np | ||
|
||
|
||
def spc2cart(r, theta, phi): | ||
""" | ||
Convert spherical coordinates (r, theta, phi) to Cartesian coordinates (x, y, z). | ||
Parameters: | ||
----------- | ||
r : float | ||
Radial distance from the origin. | ||
theta : float | ||
Polar angle (angle from the positive z-axis to the point) in radians. It ranges from [0, pi]. | ||
phi : float | ||
Azimuthal angle (angle in the xy-plane from the positive x-axis to the point) in radians. It ranges from [0, 2*pi]. | ||
Returns: | ||
-------- | ||
x : float | ||
X coordinate in Cartesian system. | ||
y : float | ||
Y coordinate in Cartesian system. | ||
z : float | ||
Z coordinate in Cartesian system. | ||
Notes: | ||
------ | ||
- Uses the following equations for conversion: | ||
x = r*sin(theta)*cos(phi) | ||
y = r*sin(theta)*sin(phi) | ||
z = r*cos(theta) | ||
""" | ||
r, theta, phi = np.array(r), np.array(theta), np.array(phi) | ||
x = r * np.sin(theta) * np.cos(phi) | ||
y = r * np.sin(theta) * np.sin(phi) | ||
z = r * np.cos(theta) | ||
|
||
return x, y, z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters