从地理坐标到地磁坐标的转换

时间:2011-10-30 22:44:36

标签: java coordinates

我正在尝试在地理坐标和地磁坐标之间进行转换。我发现了以下Prolog脚本,但我不太了解它自己转换它。目标语言是Java,但任何可以理解的东西都很好(C,Python,VB,等等)。

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

如果有人可以帮助转换此脚本或解释它正在做什么(这些数组操作让我感到困惑),我真的很感激。

由于

2 个答案:

答案 0 :(得分:4)

根据应用,高度可能是此坐标转换中的重要变量,因为地磁坐标是地球偶极磁场的映射。

在Python中,您可以使用SpacePy(http://sourceforge.net/projects/spacepy/)轻松将地理坐标转换为地磁坐标(反之亦然)。

由于您正在寻找转换为Java的源代码,因此SpacePy正在实施Fortran国际辐射带环境建模(IRBEM)库,其源代码可用(http://irbem.svn.sourceforge.net/viewvc/irbem/web/index.html

在Python中,如果其他人正在寻找快速解决方案:

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, 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')

答案 1 :(得分:3)

我使用了python代码,并尝试使用此网站验证http://wdc.kugi.kyoto-u.ac.jp/igrf/gggm/index.html。我找到了

  1. 磁极是1995年的磁极。
  2. 即使我将上面的计算设置为使用1995年的值,我也没有得到正确的匹配。
  3. 我在京都,日本(35N,135.45W)使用了价值。网页计算(25.18,-155.80)。我得到了(25.33580652,-155.82724011)。所以我不确定这是否真的有用......

    import numpy as np
    
    from numpy import pi, cos, sin, arctan2, sqrt, dot
    def geo2mag(incoord):
        """geographic coordinate to magnetic coordinate:
    
            incoord is numpy array of shape (2,*)
            array([[glat0,glat1,glat2,...],
                [glon0,glon1,glon2,...])
            where glat, glon are geographic latitude and longitude
            (or if you have only one point it is [[glat,glon]])
    
            returns
            array([mlat0,mlat1,...],
                [mlon0,mlon1,...]])
            """
    
        # SOME 'constants'...
        lon = 288.59 # or 71.41W
        lat = 79.3
        r = 1.0
    
        # convert first to radians
        lon, lat = [x*pi/180 for x in lon,lat]
    
        glat = incoord[0] * pi / 180.0
        glon = incoord[1] * pi / 180.0
        galt = glat * 0. + r
    
        coord = np.vstack([glat,glon,galt])
    
        # convert to rectangular coordinates
        x = coord[2]*cos(coord[0])*cos(coord[1])
        y = coord[2]*cos(coord[0])*sin(coord[1])
        z = coord[2]*sin(coord[0])
        xyz = np.vstack((x,y,z))
    
        # computer 1st rotation matrix:
        geo2maglon = np.zeros((3,3), dtype='float64')
        geo2maglon[0,0] = cos(lon)
        geo2maglon[0,1] = sin(lon)
        geo2maglon[1,0] = -sin(lon)
        geo2maglon[1,1] = cos(lon)
        geo2maglon[2,2] = 1.
        out = dot(geo2maglon , xyz)
    
        tomaglat = np.zeros((3,3), dtype='float64')
        tomaglat[0,0] = cos(.5*pi-lat)
        tomaglat[0,2] = -sin(.5*pi-lat)
        tomaglat[2,0] = sin(.5*pi-lat)
        tomaglat[2,2] = cos(.5*pi-lat)
        tomaglat[1,1] = 1.
        out = dot(tomaglat , out)
    
        mlat = arctan2(out[2], 
                sqrt(out[0]*out[0] + out[1]*out[1]))
        mlat = mlat * 180 / pi
        mlon = arctan2(out[1], out[0])
        mlon = mlon * 180 / pi
    
        outcoord = np.vstack((mlat, mlon))
        return outcoord
    
    if __name__ == '__main__':
        mag =  geo2mag(np.array([[79.3,288.59]]).T).T
        print mag  # should be [90,0]
    
        mag =  geo2mag(np.array([[90,0]]).T).T
        print mag  # should be [79.3,*]
    
        mag =  geo2mag(np.array([
            [79.3,288.59],
            [90,0]
            ]).T).T
    
        print mag  # should be [ [90,0]. [79.3,*] ]
    
        # kyoto, japan
        mag =  geo2mag(np.array([[35.,135.45]]).T).T
        print mag  # should be [25.18, -155.80], according to 
                   # this site using value for 1995
                   # http://wdc.kugi.kyoto-u.ac.jp/igrf/gggm/index.html