Conversion from geographic to geomagnetic coordinates
Question: I'm trying to convert between geographic and geomagnetic coordinates. I've
found the following Prolog script, but I don't understand it enough to convert
it myself. The target language is Java, but anything understandable is fine
(C, Python, VB, whatever).

<http://idlastro.gsfc.nasa.gov/ftp/pro/astro/geo2mag.pro>

If someone could either help with the conversion of this script or explain
what exactly it is doing (those array operations are baffling to me), I'd
really appreciate it.

Thanks


Answer: Depending on the application, altitude can be an important variables in this
coordinate conversion since geomagnetic coordinates are a mapping of the
Earth's dipole magnetic field.

In Python you can easily convert geographic coordinates to geomagnetic (and
vice versa) with SpacePy (<http://sourceforge.net/projects/spacepy/>).

Since you are looking for the source code to convert to Java, SpacePy is
implementing the Fortran International Radiation Belt Environment Modeling
(IRBEM) library, the source of which is available
(<http://irbem.svn.sourceforge.net/viewvc/irbem/web/index.html>)

In Python, in case others are looking for a quick solution:

    
    
    import spacepy.coordinates as coord
    from spacepy.time import Ticktock
    import numpy as np
    def geotomag(alt,lat,lon):
        #call with altitude in kilometers and lat/lon in degrees 
        Re=6371.0 #mean Earth radius in kilometers
        #setup the geographic coordinate object with altitude in earth radii 
        cvals = coord.Coords([np.float((alt/Re+Re))/Re,np.float(lat),np.float(lon)], 'GEO', 'sph',['Re','deg','deg'])
        #set time epoch for coordinates:
        cvals.ticks=Ticktock(['2012-01-01T12:00:00'], 'ISO')
        #return the magnetic coords in the same units as the geographic:
        return cvals.convert('MAG','sph')
    

