- Notifications
You must be signed in to change notification settings - Fork15
Python version of geopack and Tsyganenko models
License
tsssss/geopack
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Author: Sheng Tian, UCLA,ts0110@atmos.ucla.edu
This pythongeopack
has integrated two modules originally written in Fortran: thegeopack
and the Tsyganenko models (T89, T96, T01, and T04). The Fortrangeopack05
is available athttps://ccmc.gsfc.nasa.gov/modelweb/magnetos/data-based/Geopack_2005.html andgeopack08
is available athttp://geo.phys.spbu.ru/~tsyganenko/Geopack-2008.html. Their DLM in IDL is available athttp://ampere.jhuapl.edu/code/idl_geopack.html. As a crucial complement togeopack05
andgeopack08
, the Tsyganenko models are available in Fortran athttps://ccmc.gsfc.nasa.gov/models/modelinfo.php?model=Tsyganenko%20Magnetic%20Field.
Test results are attached in./test_geopack1.md
to demonstrate that the Pythongeopack
returns the same outputs as the Fortran and IDL counterparts. However, invisible at the user level, several improvements have been internally implemented:
The latest IGRF coefficients are used, which cover the time range from 1900 to 2025. Years beyond this range are valid inputs and the corresponding IGRF coefficients will be extrapolated, whereas the Fortran and IDL versions do not extrapolate well if at all.
The IGRF coefficients in the Python
geopack
are time series at a milli-second cadence, whereas the coefficients are daily in the Fortrangeopack
.igrf_gsm
is changed to a wrapper ofigrf_geo
plus the proper coordinate transforms. There are many places in the Fortran version where pages of codes are copied and pasted. Though not aesthetically pleasing, I let them live in the Python version, because it requires tremendous effort to fix them all. However, the igrf_geo is the one place that is obvious and easy to fix, so I did it.All
goto
statements in the Fortrangeopack
and Tsyganenko models are eliminated.A
gswgsm
is added to support the new GSW coordinate introduced ingeopack08
.
The package requires Python pre-installed and depends on thenumpy
andscipy
packages. I've only tested the Pythongeopack
on Mac OS in Python 3.6. Performance on other platform and other versions of Python is unclear.
To install the Pythongeopack
throughpip
, type> pip3 install geopack
in the terminal.
To install thelatest version, manually install it on a Mac (and hopefully Linux):
- Download the latest package athttps://github.com/tsssss/geopack/.
- Unzip it, open a terminal, and
cd
to the unzipped directory - Install the package to Python by typing
python3 setup.py install
in the terminal
The Python version ofgeopack
tries to be compatible with both Fortrangeopack05
andgeopack08
. The major change ofgeopack08
is a new coordinate calledGSW
, which is similar to the widely usedGSM
but more suitable for studying tail physics. To be backward compatible withgeopack05
, the Python version still usesGSM
as the major coordinate for vectors. However, to keep updated withgeopack08
, the Python version provides a new coordinate transform functionGSWGSM
, so that users can easily switch to their favorite coordinate. A new TsyganenkoT07d
model has been released with a new algorithm. Support for T07d is under development.
There are two G parameters used as optional inputs to the T01 model. Their definitions are in Tsyganenko (2001). Similarly, there are six W parameters used as optional inputs to the T04 model, as defined in Tsyganenko (2005). The Python version does not support the calculations of the G and W parameters. For users interested, here is the link for the Qin-Denton W and G parameters athttps://rbsp-ect.newmexicoconsortium.org/data_pub/QinDenton/. Thanks to Dr Shawn Young for providing the references and relevant information.
Back in my mind, there are some potential ways to implement the G and W parameters. But please do understand that the package does not have any funding support. I usually do major updates during summer or winter break, when it's easier to find spare time. For users who are familiar with the G and W parameters, let me know if you have any suggestions or ideas on solutions to implement them in the package!
The model needs to be updated for each new time step. The time used is the unix timestamp, which is the seconds from 1970-01-01/00:00. Here are some examples in Python to get the time from intuitive inputs.
# Test for 2001-01-02/03:04:05 UTimportdatetimefromdateutilimportparser# From date and timet1=datetime.datetime(2001,1,2,3,4,5)t0=datetime.datetime(1970,1,1)ut= (t1-t0).total_seconds()print(ut)978404645.0# From string, need the package dateutilt1=parser.parse('2001-01-02/03:04:05')ut= (t1-t0).total_seconds()print(ut)978404645.0
Here is a short example of importing the package and call functions. A detailed explanation of all functions is listed in the next section.
fromgeopackimportgeopack,t89ut=100# 1970-01-01/00:01:40 UT.xgsm,ygsm,zgsm= [1,2,3]ps=geopack.recalc(ut)b0xgsm,b0ygsm,b0zgsm=geopack.dip(xgsm,ygsm,zgsm)# calc dipole B in GSM.dbxgsm,dbygsm,dbzgsm=t89.t89(2,ps,xgsm,ygsm,zgsm)# calc T89 dB in GSM.bxgsm,bygsm,bzgsm= [b0xgsm+dbxgsm,b0ygsm+dbygsm,b0zgsm+dbzgsm]print(bxgsm,bygsm,bzgsm)-539.5083883330017-569.5906371610358-338.8680547453352
And here is another way to import the package and refer to the functions.
importgeopackut=100# 1970-01-01/00:01:40 UT.xgsm,ygsm,zgsm= [1,2,3]ps=geopack.geopack.recalc(ut)b0xgsm,b0ygsm,b0zgsm=geopack.geopack.dip(xgsm,ygsm,zgsm)dbxgsm,dbygsm,dbzgsm=geopack.t89.t89(2,ps,xgsm,ygsm,zgsm)print(b0xgsm,b0ygsm,b0zgsm)-544.425907831383-565.7731166717405-321.43413443108597
Another way to import the package.
importgeopack.geopackasgput=100# 1970-01-01/00:01:40 UT.xgsm,ygsm,zgsm= [2,1,100]ps=gp.recalc(ut)xgsm,ygsm,zgsm=gp.geogsm(2,1,100,1)print(xgsm,ygsm,zgsm)(-41.00700906453125,-19.962123759781406,89.0221254665413)
To use the feature ingeopack08
, users can supply the solar wind magnetic field in GSE and express vectors in GSW
fromgeopackimportgeopackut=100# 1970-01-01/00:01:40 UT.xgsm,ygsm,zgsm= [1,2,3]vgse= [-400,0,10]# solar wind velocity in GSE.ps=geopack.recalc(ut,*vgse)# init with time & SW velocity.# or use ps = geopack.recalc(ut, vgse[0],vgse[1],vgse[2])xgsw,ygsw,zgsw=gswgsm(xgsm,ygsm,zgsm,-1)# convert position to GSW.b0xgsw,b0ygsw,b0zgsw=geopack.dip_gsw(xgsw,ygsw,zgsw)# calc dipole B in GSW.print(b0xgsw,b0ygsw,b0zgsw)-540.5392569443875-560.7296994901754-336.47913346240205print((geopack.gswgsm(b0xgsw,b0ygsw,b0zgsw,1)))# dipole B in GSM.(-544.4259078313833,-565.7731166717405,-321.4341344310859)
The Pythongeopack
follows the Python way: function parameters are all input parameters and the outputs are returned. (This is very different from the Fortran and IDLgeopack
.)
When changing to a new time of interest
recalc
. Re-calculate the dipole tilt angle (and other internal parameters) for a given time.Exampleps = recalc(ut)ps = recalc(ut, vxgse,vygse,vzgse)Inputut: The given time in the universal time in second.vxgse,vygse,vzgse: The solar wind velocity in GSE. If they are omitted, a default value of [-400,0,0] is used so that GSM and GSW are the same.Returnps: Dipole tilt angle in radian (defined in GSM, not GSW).
Get the internal model magnetic fields
dip
. Calculate the internal magnetic field from the dipole model for a given position and time (The time dependence is taken care of byrecalc
), in the GSM coordinate.Examplebxgsm,bygsm,bzgsm = dip(xgsm,ygsm,zgsm)Inputxgsm,ygsm,zgsm: The given position in cartesian GSM coordinate in Re (earth radii, 1 Re = 6371.2 km).Returnbxgsm,bygsm,bzgsm: Cartesian GSM components of the internal magnetic field in nT.
dip_gsw
. Calculate the internal magnetic field from the dipole model for a given position and time (The time dependence is taken care of byrecalc
), in the GSW coordinate.Examplebxgsw,bygsw,bzgsw = dip_gsw(xgsw,ygsw,zgsw)Inputxgsw,ygsw,zgsw: The given position in cartesian GSW coordinate in Re (earth radii, 1 Re = 6371.2 km).Returnbxgsw,bygsw,bzgsw: Cartesian GSW components of the internal magnetic field in nT.
igrf_gsm
. Calculate the internal magnetic field from the IGRF model (http://www.ngdc.noaa.gov/iaga/vmod/igrf.html) for a given position and time, in the GSM coordinate.Examplebxgsm,bygsm,bzgsm = igrf_gsm(xgsm,ygsm,zgsm)Inputxgsm,ygsm,zgsm: The given position in cartesian GSM coordinate in Re (earth radii, 1 Re = 6371.2 km).Returnbxgsm,bygsm,bzgsm: Cartesian GSM components of the internal magnetic field in nT.
igrf_gsw
. Calculate the internal magnetic field from the IGRF model (http://www.ngdc.noaa.gov/iaga/vmod/igrf.html) for a given position and time, in the GSW coordinate.Examplebxgsw,bygsw,bzgsw = igrf_gsw(xgsw,ygsw,zgsw)Inputxgsw,ygsw,zgsw: The given position in cartesian GSW coordinate in Re (earth radii, 1 Re = 6371.2 km).Returnbxgsw,bygsw,bzgsw: Cartesian GSW components of the internal magnetic field in nT.
igrf_geo
. Calculate the internal magnetic field from the IGRF model (http://www.ngdc.noaa.gov/iaga/vmod/igrf.html) for a given position and time, in the GEO coordinate.Examplebr,btheta,bphi = igrf_gsm(r,theta,phi)Inputr,theta,phi: The given position in spherical GEO coordinate. r is the radia distance in Re; theta is the co-latitude in radian; phi is the longitude in radian.Returnbr,btheta,bphi: Spherical GSM components of the internal magnetic field in nT. br is outward; btheta is southward; bphi is eastward.
Get the external model magntic fields
Four models (T89, T96, T01, and T04) developed by Dr. Tsyganenko are implemented in the package.
t89
. Calculate the external magnetic field from the T89 model for a given position and time, in the GSM coordinate.Examplebxgsm,bygsm,bzgsm = t89(par, ps, xgsm,ygsm,zgsm)Inputpar: A model parameter. It is an integer (1-7) maps to the Kp index| par | 1 | 2 | 3 | 4 | 5 | 6 | 7 || Kp | 0,0+ | 1-,1,1+ | 2-,2,2+ | 3-,3,3+ | 4-,4,4+ | 5-,5,5+ | > 6- |ps: Dipole tilt angle in radian.xgsm,ygsm,zgsm: The given position in cartesian GSM coordinate in Re (earth radii, 1 Re = 6371.2 km).
t96
. Calculate the external magnetic field from the T96 model for a given position and time, in the GSM coordinate.Examplebxgsm,bygsm,bzgsm = t96(par, ps, xgsm,ygsm,zgsm)Inputps: Dipole tilt angle in radian.xgsm,ygsm,zgsm: The given position in cartesian GSM coordinate in Re (earth radii, 1 Re = 6371.2 km).par: A model paramter. It is a 10-element array, whose elements are (1-10)| par | 1 | 2 | 3-4 | 5-10 || Var | Pdyn | Dst | ByIMF,BzIMF | not used |where Pdyn is the solar wind dynamic pressure in nPa; Dst is the Dst index in nT; ByImf,BzImf are the y and z components of the IMF (interplanetary magnetif field) in GSM.
t01
. Calculate the external magnetic field from the T01 model for a given position and time, in the GSM coordinate.Examplebxgsm,bygsm,bzgsm = t01(par, ps, xgsm,ygsm,zgsm)Inputps: Dipole tilt angle in radian.xgsm,ygsm,zgsm: The given position in cartesian GSM coordinate in Re (earth radii, 1 Re = 6371.2 km).par: A model paramter. It is a 10-element array, whose elements are (1-10)| par | 1 | 2 | 3-4 | 5-6 | 7-10 || Var | Pdyn | Dst | ByIMF,BzIMF | G1,G2 | not used |where Pdyn is the solar wind dynamic pressure in nPa; Dst is the Dst index in nT; ByImf,BzImf are the y and z components of the IMF (interplanetary magnetif field) in GSM; G1,G2 are two indices defined in Tsyganenko (2001).N. A. Tsyganenko, A new data-based model of the near magnetosphere magnetic field: 1. Mathematical structure. 2. Parameterization and fitting to observations (submitted to JGR, July 2001)
t04
. Calculate the external magnetic field from the T04 model for a given position and time, in the GSM coordinate.Examplebxgsm,bygsm,bzgsm = t04(par, ps, xgsm,ygsm,zgsm)Inputps: Dipole tilt angle in radian.xgsm,ygsm,zgsm: The given position in cartesian GSM coordinate in Re (earth radii, 1 Re = 6371.2 km).par: A model paramter. It is a 10-element array, whose elements are (1-10)| par | 1 | 2 | 3-4 | 5-10 || Var | Pdyn | Dst | ByIMF,BzIMF | W1 to W6 |where Pdyn is the solar wind dynamic pressure in nPa; Dst is the Dst index in nT; ByImf,BzImf are the y and z components of the IMF (interplanetary magnetif field) in GSM; W1,W2,...,W6 are six indices defined in Tsyganenko (2005).N. A. Tsyganenko and M. I. Sitnov, Modeling the dynamics of the inner magnetosphere during strong geomagnetic storms, J. Geophys. Res., v. 110 (A3), A03208, doi: 10.1029/2004JA010798, 2005.
Note: All 4 models share the same interface, but the meanings of
par
are very different.Convert a cartesian vector among coordinates
The supported coordinates are: GEO, GEI, MAG, GSM, GSE, and SM. They are defined in Hapgood (1992). And GSW, defined in Hones+(1986) is added in
geopack_08
. The functions for the coordinate transform are:geomag
,geigeo
,magsm
,gsmgse
,smgsm
,geogsm
,gswgsm
. They share the same interface, so they are explained together.Usageb1,b2,b3 = geomag(h1,h2,h3, flag)Examplexmag,ymag,zmag = geomag(xgeo,ygeo,zgeo, 1)xgeo,ygeo,zgeo = geomag(xmag,ymag,zmag, -1)...Input and Returnh1,h2,h3: Cartesian components of a vector in "coord1"b1,b2,b3: Cartesian components of the vector in "coord2"flag: flag > 0 -- coord1 to coord2; flag < 0 -- coord2 to coord1
In addition
geodgeo
converts a position between altitude (in km)/geodetic latitude (in rad) and geocentric distance (in km)/colatitude (in rad).Usageb1,b2 = geodgeo(h1,h2, flag)Examplergeo,thetageo = geodgeo(hgeod,xmugeod, 1)hgeod,xmugeod = geodgeo(rgeo,thetageo, -1)Input and Returnh1,h2: Components of a vector in "coord1"b1,b2: Components of a vector in "coord2"flag: flag > 0 -- coord1 to coord2; flag < 0 -- coord2 to coord1
Trace along model magnetic fields:
trace
Examplex1gsm,y1gsm,z1gsm = trace(x0gsm,y0gsm,z0gsm, dir, rlim, r0, par, exname, inname)Inputx0gsm,y0gsm,z0gsm: The given position in cartesian GSM coordinate in Re (earth radii, 1 Re = 6371.2 km).dir: Direction of tracing. dir = -1 for parallel; dir = 1 for anti-parallel.rlim: Maximum tracing radius in Re. Default value is 10 Re.r0: Minimum tracing radius in Re. Default value is 1 Re.inname: A string specifies the internal model, one of 'dipole','igrf'. The default value is 'igrf'.exname: A string specifies the external model, one of 't89','t96','t01','t04'. The default value is 't89' and its par is default to be 2.par: The model parameter. Its dimension and the meaning depend on the external model. Please check the interface of the models for details.
Functions do not appear in the above list are considered as internal functions. For usages of them, advanced users can check the source code of the Pythongeopack
.
Hapgood, M. A. (1992). Space physics coordinate transformations: A user guide. Planetary and Space Science, 40(5), 711–717.http://doi.org/10.1016/0032-0633(92)90012-D
N. A. Tsyganenko, A new data-based model of the near magnetosphere magnetic field: 1. Mathematical structure. 2. Parameterization and fitting to observations (submitted to JGR, July 2001)
N. A. Tsyganenko and M. I. Sitnov, Modeling the dynamics of the inner magnetosphere during strong geomagnetic storms, J. Geophys. Res., v. 110 (A3), A03208, doi: 10.1029/2004JA010798, 2005.