Skip to content

Commit 21c9787

Browse files
author
jaffar.rumith@gmail.com
committed
Moved project files to trunk for SVN compatibility.
0 parents  commit 21c9787

File tree

7 files changed

+900
-0
lines changed

7 files changed

+900
-0
lines changed

LICENSE

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
** About **
2+
This program contains two Python modules: igrf and cxform.
3+
4+
Module igrf provides several useful methods based on IGRF v.10.
5+
The wrapper has been written by David Parunakian, Skobeltsyn Institute of Nuclear Physics of the Moscow State University.
6+
Feel free to contact me with improvement suggestions, questions etc at either rumith@srd.sinp.msu.ru or dp@xientific.info.
7+
The model itself is the result of hard work of many people from different institutions, such as IAGA and NASA.
8+
The source code and coefficient files used can be found at ftp://nssdcftp.gsfc.nasa.gov/models/geomagnetic/igrf/fortran_code/
9+
More information on IGRF can be found here: http://www.ngdc.noaa.gov/IAGA/vmod/igrf.html
10+
11+
Module cxform provides access to one function - transform(), which can be used for transforming cartesian coordinates
12+
from one coordinate system to another. Complete documentation on cxform, as well as the list of coordinate systems supported,
13+
can be found at CXFORM official website http://nssdcftp.gsfc.nasa.gov/selected_software/coordinate_transform/
14+
15+
16+
** Installation **
17+
tar -xzf igrf.tar.gz
18+
cd igrf
19+
./install.sh
20+
21+
The above procedure will install coefficient files to /usr/local/lib/igrf. If you wish to install them to another directory (e.g. /var/foobar), run
22+
./install.sh /var/foobar
23+
instead. PLEASE ONLY SPECIFY ABSOLUTE PATHS.
24+
25+
** Usage **
26+
To use igrf in your software after you have installed it, include the following line in your code:
27+
28+
import igrf
29+
30+
After that, you can use the following functions:
31+
32+
igrf.dimo(year):
33+
Find the geomagnetic dipole moment in Gauss (normalized to the Earth's radius) at the specified time (decimal year)
34+
35+
igrf.lb(lat, lon, alt, year):
36+
Find the L-shell and geomagnetic field intensity (l, b tuple) at the specified latitude, longitude, altitude and decimal year
37+
38+
igrf.b(lat, lon, alt, year):
39+
Find the Earth's magnetic field value (Bnorth, Beast, Bdown, Babs tuple) using the spherical harmonics model
40+
41+
igrf.b0(lat, lon, alt, year, stps):
42+
Find the smallest magnetic field strength on a field line. Stps is step size for field line tracing
43+
44+
Latitude and longitude should be specified in the geographic coordinate system.
45+
Altitude should be specified in kilometers above sea level.
46+
Decimal year is a float and looks like 2003.45.
47+
48+
49+
** Legal **
50+
This project is distributed under the terms of the GNU General Public License v.3. The full text of the license is available
51+
in the LICENSE file.

cxformmodule.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <python2.5/Python.h>
2+
#include "cxform.h"
3+
4+
static PyObject *cxform_transform(PyObject *self, PyObject *args) {
5+
const char *from, *to;
6+
int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
7+
Vec v_in = {0.0, 0.0, 0.0};
8+
Vec v_out = {0.0, 0.0, 0.0};
9+
PyObject dt;
10+
11+
// if (!PyArg_ParseTuple(args, "ssdddO!", &from, &to, &v_in[0], &v_in[1], &v_in[2], "DateTime", &dt))
12+
// return NULL;
13+
if (!PyArg_ParseTuple(args, "ssdddiiiiii", &from, &to, &v_in[0], &v_in[1], &v_in[2], &year, &month, &day, &hour, &minute, &second))
14+
return NULL;
15+
16+
double es = date2es(year, month, day, hour, minute, second);
17+
cxform(from, to, es, v_in, v_out);
18+
return Py_BuildValue("ddd", v_out[0], v_out[1], v_out[2]);
19+
}
20+
21+
static PyMethodDef CxformMethods[] = {
22+
{"transform", cxform_transform, METH_VARARGS, "transform(from, to, x, y, z, year, month, day, hour, minute, second)\n\nTransform coordinates from one system to another."},
23+
{NULL, NULL, 0, NULL}
24+
};
25+
26+
PyMODINIT_FUNC initcxform(void) {
27+
(void) Py_InitModule("cxform", CxformMethods);
28+
}

igrfmodule.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <python2.5/Python.h>
2+
#include "f2c.h"
3+
4+
int MAIN__() { return 0; }
5+
6+
static PyObject *igrf_dimo(PyObject *self, PyObject *args) {
7+
real year, dimo;
8+
if (!PyArg_ParseTuple(args, "f", &year)) {
9+
return NULL;
10+
}
11+
feldcof_(&year, &dimo);
12+
return Py_BuildValue("f", dimo);
13+
}
14+
15+
static PyObject *igrf_lb(PyObject *self, PyObject *args) {
16+
real glat, glon, alt, dimo, fl, b0, year;
17+
integer icode;
18+
if (!PyArg_ParseTuple(args, "ffff", &glat, &glon, &alt, &year)) {
19+
return NULL;
20+
}
21+
feldcof_(&year, &dimo);
22+
shellg_(&glat, &glon, &alt, &dimo, &fl, &icode, &b0);
23+
return Py_BuildValue("iff", icode, fl, b0);
24+
}
25+
26+
static PyObject *igrf_b(PyObject *self, PyObject *args) {
27+
real glat, glon, alt, dimo, bnorth, beast, bdown, babs, year;
28+
if (!PyArg_ParseTuple(args, "ffff", &glat, &glon, &alt, &year)) {
29+
return NULL;
30+
}
31+
feldcof_(&year, &dimo);
32+
feldg_(&glat, &glon, &alt, &bnorth, &beast, &bdown, &babs);
33+
return Py_BuildValue("ffff", bnorth, beast, bdown, babs);
34+
}
35+
36+
static PyObject *igrf_b0(PyObject *self, PyObject *args) {
37+
real glat, glon, alt, dimo, fl, b0, year, stps, bdel, bequ, rr0;
38+
integer icode;
39+
logical value;
40+
if (!PyArg_ParseTuple(args, "fffff", &glat, &glon, &alt, &year, &stps)) {
41+
return NULL;
42+
}
43+
feldcof_(&year, &dimo);
44+
shellg_(&glat, &glon, &alt, &dimo, &fl, &icode, &b0);
45+
findb0_(&stps, &bdel, &value, &bequ, &rr0);
46+
return Py_BuildValue("lfff", value, bequ, rr0, bdel);
47+
}
48+
49+
char *trimwhitespace(char *str) {
50+
char *end;
51+
// Trim leading space
52+
while(isspace(*str)) str++;
53+
54+
// Trim trailing space
55+
end = str + strlen(str) - 1;
56+
while(end > str && isspace(*end)) end--;
57+
58+
// Write new null terminator
59+
*(end+1) = 0;
60+
61+
return str;
62+
}
63+
64+
static PyMethodDef IgrfMethods[] = {
65+
{ "dimo", igrf_dimo, METH_VARARGS, "dimo(year)\n\nFind the geomagnetic dipole moment in Gauss (normalized to the Earth's radius) at the specified time (decimal year)" },
66+
{ "lb", igrf_lb, METH_VARARGS, "igrf.lb(lat, lon, alt, year)\n\nFind the L-shell and geomagnetic field intensity at the specified latitude, longitude, altitude and decimal year" },
67+
{ "b", igrf_b, METH_VARARGS, "igrf.b(lat, lon, alt, year)\n\nFind the Earth's magnetic field value using the spherical harmonics model" },
68+
{ "b0", igrf_b0, METH_VARARGS, "igrf.b0(lat, lon, alt, year, stps)\n\nFind the smallest magnetic field strength on a field line" },
69+
{ NULL, NULL, 0, NULL }
70+
};
71+
72+
PyMODINIT_FUNC initigrf(void) {
73+
(void) Py_InitModule("igrf", IgrfMethods);
74+
initize_();
75+
}

install.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
if test -z "$1"
4+
then
5+
TARGET=/usr/local/lib/pygrf/
6+
else
7+
TARGET=$1
8+
fi
9+
10+
if [ ! -d "$TARGET" ]
11+
then
12+
sudo mkdir $TARGET
13+
fi
14+
15+
if ( ! type -P f2c > /dev/null )
16+
then
17+
echo "F2C not installed, aborting"
18+
exit
19+
fi
20+
21+
wget -r ftp://nssdcftp.gsfc.nasa.gov/models/geomagnetic/igrf/fortran_code/
22+
if [ ! -d nssdcftp.gsfc.nasa.gov]
23+
then
24+
echo "Failed to download IGRF source code. Please check your Internet connection."
25+
else
26+
mv nssdcftp.gsfc.nasa.gov/models/geomagnetic/igrf/fortran_code/* .
27+
rm -rf nssdcftp.gsfc.nasa.gov
28+
for x in `ls *.for`; do mv $x `echo $x | sed s/.for/.F/`; done
29+
f2c igrf_sub.F
30+
31+
cat igrf_sub.c | sed "s^o__1.ofnm = fout^extern char *trimwhitespace(char *str); char prefix_dp[28] = \"$TARGET\"; strcat(prefix_dp, fspec); o__1.ofnm = trimwhitespace(prefix_dp)^" > igrf_sub2.c
32+
mv igrf_sub2.c igrf_sub.c
33+
34+
35+
sudo mv *.dat $TARGET
36+
sudo python setup-igrf.py install
37+
sudo rm -rf build *.F bilcal.log igrf_sub.c
38+
fi
39+
40+
41+
wget http://nssdcftp.gsfc.nasa.gov/selected_software/coordinate_transform/archive/cxform-0.71_source.tar.gz
42+
if [ ! -f cxform-0.71_source.tar.gz ]
43+
then
44+
echo "Failed to download CXFORM source code. Please check your Internet connection."
45+
else
46+
tar -xzf cxform-0.71_source.tar.gz
47+
mv cxform-0.71/cxform-manual.c cxform-0.71/cxform-auto.c cxform-0.71/cxform.h .
48+
sudo python setup-cxform.py install
49+
sudo rm -rf build cxform-0.71/ cxform-auto.c cxform-manual.c cxform-0.71_source.tar.gz cxform.h
50+
fi

setup-cxform.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/python
2+
from distutils.core import setup, Extension
3+
4+
cxform = Extension('cxform', sources = ['cxformmodule.c', 'cxform-manual.c', 'cxform-auto.c'])
5+
6+
setup (name = 'cxform',
7+
version = '1.0',
8+
description = 'NASA CXFORM 0.71 wrapper',
9+
author = 'David Parunakian',
10+
author_email = 'dp@xientific.info',
11+
ext_modules = [cxform])

setup-igrf.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/python
2+
from distutils.core import setup, Extension
3+
4+
igrf = Extension('igrf', sources = ['igrf_sub.c', 'igrfmodule.c'], libraries = ['m', 'f2c'], library_dirs = ['/usr/lib/'])
5+
6+
setup(name = 'igrf',
7+
version = '1.0',
8+
description = 'IGRF v.10 Python wrapper',
9+
author = 'David Parunakian',
10+
author_email = 'dp@xientific.info',
11+
ext_modules = [igrf])

0 commit comments

Comments
 (0)