Advertisement
SVXX

FrameClasses

Jun 24th, 2014
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.33 KB | None | 0 0
  1. """
  2. SunPy's built-in coordinate frames.
  3. Part of the proposed Coordinates API.
  4. @author: Pritish C. (VaticanCameos)
  5. """
  6.  
  7. # NumPy import
  8. import numpy as np
  9.  
  10. # Astropy imports
  11. from astropy.extern import six
  12. from astropy.utils.compat.odict import OrderedDict
  13. from astropy import units as u
  14. from astropy.coordinates.representation import (SphericalRepresentation, CylindricalRepresentation,
  15.                                                 CartesianRepresentation)
  16. from astropy.coordinates.baseframe import (BaseCoordinateFrame, frame_transform_graph,
  17.                                            RepresentationMapping)
  18. from astropy.coordinates.transformations import FunctionTransform, DynamicMatrixTransform
  19. from astropy.coordinates import FrameAttribute
  20.  
  21. # SunPy imports
  22. from sunpy import sun as s # For Carrington rotation number
  23.  
  24. RSUN_METERS = s.constants.constant('radius').si.value
  25. DSUN_METERS = s.constants.constant('mean distance').si.value
  26.  
  27. class HelioGraphicStonyhurst(BaseCoordinateFrame):
  28.     """
  29.    A coordinate or frame in the Stonyhurst Heliographic
  30.    system.
  31.    This system is known to remain fixed with respect to
  32.    the center of the Earth, and its quantities, the
  33.    latitude and longitude, are specified in degrees.
  34.  
  35.    Parameters
  36.    ----------
  37.    representation: `~astropy.coordinates.BaseRepresentation` or None
  38.        A representation object or None to have no data.
  39.    hlon: `Angle` object.
  40.        The longitude for this object (``lat`` must also be given and ``representation``
  41.        must be None).
  42.    hlat: `Angle` object.
  43.        The latitude for this object (``lon`` must also be given and ``representation``
  44.        must be None).
  45.    rad: `astropy.units.Quantity` object.
  46.        This quantity holds the radial distance. If not specified, it is, by default,
  47.        the solar radius. Optional, must be keyword
  48.    """
  49.    
  50.     default_representation = SphericalRepresentation
  51.  
  52.     _frame_specific_representation_info = {
  53.         'spherical': [RepresentationMapping('lon', 'hlon', 'recommended'),
  54.                       RepresentationMapping('lat', 'hlat', 'recommended'),
  55.                       RepresentationMapping('distance', 'rad', 'recommended')],
  56.         }
  57.  
  58.     #rad = FrameAttribute(default=((RSUN_METERS/1000)*u.km))
  59.  
  60.     def __init__(self, *args, **kwargs):
  61.         print(args, kwargs)
  62.         if not args:
  63.             print('In kwargs section')
  64.             if 'rad' not in kwargs:
  65.                 kwargs['rad'] = (RSUN_METERS/1000)*u.km
  66.         elif not kwargs:
  67.             print('In args section')
  68.             if len(args) == 2:
  69.                 args = list(args)
  70.                 args.append((RSUN_METERS/1000)*u.km)
  71.                 args = tuple(args)
  72.         print(args, kwargs)
  73.         super(HelioGraphicStonyhurst, self).__init__(*args, **kwargs)
  74.  
  75. def _carrington_offset():
  76.     # This method is to return the Carrington offset.
  77.     return s.heliographic_solar_center()[0]
  78.  
  79. class HelioGraphicCarrington(HelioGraphicStonyhurst):
  80.     """
  81.    A coordinate or frame in the Carrington Heliographic
  82.    system.
  83.    This frame differs from the Stonyhurst version in the
  84.    definition of the longitude, which is defined using
  85.    an offset which is a time-dependent scalar value.
  86.    representation: `~astropy.coordinates.BaseRepresentation` or None.
  87.        A representation object. If specified, other parameters must
  88.        be in keyword form.
  89.    hlon: `Angle` object.
  90.        The longitude for this object (``lat`` must also be given and ``representation``
  91.        must be None).
  92.    hlat: `Angle` object.
  93.        The latitude for this object (``lon`` must also be given and ``representation``
  94.        must be None).
  95.    rad: `astropy.units.Quantity` object, optional, must be keyword.
  96.        This quantity holds the radial distance. If not specified, it is, by default,
  97.        the solar radius. Optional, must be keyword.
  98.    """
  99.    
  100.     default_representation = SphericalRepresentation
  101.  
  102.     _frame_specific_representation_info = {
  103.         'spherical': [RepresentationMapping('lon', 'hlon', 'recommended'),
  104.                       RepresentationMapping('lat', 'hlat', 'recommended'),
  105.                       RepresentationMapping('distance', 'rad', 'recommended')]
  106.         }
  107.  
  108.     #rad = FrameAttribute(default=((RSUN_METERS/1000)*u.km))
  109.  
  110.     def __init__(self, *args, **kwargs):
  111.         super(HelioGraphicCarrington, self).__init__(*args, **kwargs)
  112.  
  113. class HelioCentric(BaseCoordinateFrame):
  114.     """
  115.    A coordinate or frame in the Heliocentric system.
  116.    This frame may either be specified in Cartesian
  117.    or cylindrical representation.
  118.    Cylindrical representation replaces (x, y) with
  119.    (rho, psi) where rho is the impact parameter and
  120.    psi is the position angle in degrees.
  121.  
  122.    Parameters
  123.    ----------
  124.    representation: `~astropy.coordinates.BaseRepresentation` or None.
  125.        A representation object. If specified, other parameters must
  126.        be in keyword form and if x, y and z are specified, it must
  127.        be None.
  128.    x: `Quantity` object.
  129.        X-axis coordinate, optional, must be keyword.
  130.    y: `Quantity` object.
  131.        Y-axis coordinate, optional, must be keyword.
  132.    z: `Quantity` object. Shared by both representations.
  133.        Z-axis coordinate, optional, must be keyword.
  134.    D0: `Quantity` object.
  135.        Represents the distance between the observer and the Sun center.
  136.        Defaults to 1AU.
  137.    """
  138.  
  139.     default_representation = CartesianRepresentation
  140.  
  141.     _frame_specific_representation_info = {
  142.         'cylindrical': [RepresentationMapping('phi', 'psi', u.deg)]}
  143.  
  144.    # d = FrameAttribute(default=(1*u.au).to(u.km))
  145.     D0 = FrameAttribute(default=(1*u.au).to(u.km))
  146.    
  147. class HelioProjective(BaseCoordinateFrame):
  148.     """
  149.    A coordinate or frame in the Helioprojective
  150.    system.
  151.    This is the projected equivalent of the Heliocentric
  152.    coordinate system. As such, the Cartesian representation
  153.    has degrees for each of the units, and the cylindrical
  154.    representation has the rho parameter replaced by Trho,
  155.    or theta_rho.
  156.  
  157.    Parameters
  158.    ----------
  159.    representation: `~astropy.coordinates.BaseRepresentation` or None.
  160.        A representation object. If specified, other parameters must
  161.        be in keyword form.
  162.    Tx: `Angle` object.
  163.        X-axis coordinate, specified in degrees.
  164.    Ty: `Angle` object.
  165.        Y-axis coordinate, specified in degrees.
  166.    zeta: Z-axis coordinate.
  167.        Represents the radial distance between the solar center
  168.        and the observer.
  169.    d: `Quantity` object.
  170.        Represents the distance between observer and feature/point.
  171.        Defaults to 1AU.
  172.    D0: `Quantity` object.
  173.        Represents the distance between observer and solar center.
  174.        Defaults to 1AU.
  175.    """
  176.  
  177.     default_representation = CartesianRepresentation
  178.  
  179.     _frame_specific_representation_info = {
  180.         'cartesian': [RepresentationMapping('x', 'Tx', u.deg),
  181.                       RepresentationMapping('y', 'Ty', u.deg),
  182.                       RepresentationMapping('z', 'zeta', u.km)],
  183.         'cylindrical': [RepresentationMapping('rho', 'Trho', u.deg),
  184.                         RepresentationMapping('phi', 'psi', u.deg)]}
  185.  
  186.     d = FrameAttribute(default=(1*u.au).to(u.km))
  187.     D0 = FrameAttribute(default=(1*u.au).to(u.km))
  188.    
  189.     # Note that Trho = Drho + 90, and Drho is the declination parameter.
  190.     # According to Thompson, we use Trho internally and Drho as part of
  191.     # the (Drho, psi) pair when defining a coordinate in this system.
  192.  
  193. # ------------------ Transformation Framework -------------------------
  194. # This portion is reserved for the implementation of transformations
  195. # as defined by Thompson.
  196.  
  197. @frame_transform_graph.transform(FunctionTransform, HelioGraphicStonyhurst, HelioGraphicCarrington)
  198. def hcs_to_hcg(hcscoord, hcgframe):
  199.     c_lon = hcscoord.spherical.lon + _carrington_offset()
  200.     representation = SphericalRepresentation(c_lon, hcscoord.spherical.lat)
  201.     return HelioGraphicCarrington(representation)
  202.  
  203. @frame_transform_graph.transform(FunctionTransform, HelioGraphicCarrington, HelioGraphicStonyhurst)
  204. def hcg_to_hcs(hcgcoord, hcsframe):
  205.     s_lon = hcgcoord.spherical.lon - _carrington_offset()
  206.     representation = SphericalRepresentation(s_lon, hcgcoord.spherical.lat)
  207.     return HelioGraphicStonyhurst(representation)
  208.  
  209. @frame_transform_graph.transform(FunctionTransform, HelioCentric, HelioProjective)
  210. def helioc_to_heliop(helioccoord, heliopframe):
  211.     x = helioccoord.x.to(u.m)
  212.     y = helioccoord.y.to(u.m)
  213.     z = helioccoord.z.to(u.m)
  214.  
  215.     # d is calculated as the distance between the points
  216.     # (x,y,z) and (0,0,D0).
  217.     d = np.sqrt(x**2 + y**2 + (z - (helioccoord.D0.to(u.m)))**2)
  218.     # zeta is then calculated as given in Thompson.
  219.     zeta = helioccoord.D0.to(u.m) - d
  220.  
  221.     distance = np.sqrt(x ** 2 + y ** 2 + zeta ** 2)
  222.     hpcx = np.rad2deg(np.arctan2(x, zeta))
  223.     hpcy = np.rad2deg(np.arcsin(y / distance))
  224.  
  225.     representation = CartesianRepresentation(hpcx, hpcy, zeta)
  226.     return HelioProjective(representation)
  227.    
  228. @frame_transform_graph.transform(FunctionTransform, HelioProjective, HelioCentric)
  229. def heliop_to_helioc(heliopcoord, heliocframe):
  230.     x = heliopcoord.x
  231.     y = heliopcoord.y
  232.     c = np.array([np.deg2rad(1), np.deg2rad(1)])
  233.  
  234.     cosx = np.cos(x * c[0])
  235.     sinx = np.sin(x * c[0])
  236.     cosy = np.cos(y * c[1])
  237.     siny = np.sin(y * c[1])
  238.    
  239.     q = heliocframe.d * 1000 * cosy * cosx
  240.     distance = (q ** 2 - (heliopcoord.d.to(u.m)) ** 2 +
  241.     (heliopcoord.D0.to(u.m)) ** 2)
  242.     distance = q - np.sqrt(distance)
  243.  
  244.     rx = distance * cosy * sinx
  245.     ry = distance * siny
  246.     rz = (heliopcoord.d.to(u.m)) - distance * cosy * cosx
  247.  
  248.     representation = CartesianRepresentation(rx, ry, rz)
  249.     return HelioCentric(representation)
  250.  
  251. @frame_transform_graph.transform(FunctionTransform, HelioCentric, HelioGraphicStonyhurst)
  252. def hcc_to_hgs(helioccoord, heliogframe):
  253.     x = helioccoord.x.to(u.m)
  254.     y = helioccoord.y.to(u.m)
  255.     z = helioccoord.z.to(u.m)
  256.    
  257.     l0_deg = _carrington_offset()
  258.     b0_deg = s.heliographic_solar_center()[1]
  259.  
  260.     cosb = np.cos(np.deg2rad(b0_deg))
  261.     sinb = np.sin(np.deg2rad(b0_deg))
  262.  
  263.     hecr = np.sqrt(x**2 + y**2 + z**2)
  264.     hgln = np.arctan2(x, z * cosb - y * sinb) + np.deg2rad(l0_deg)
  265.     hglt = np.arcsin((y * cosb + z * sinb) / hecr)
  266.  
  267.     representation = SphericalRepresentation(np.rad2deg(hgln),
  268.                                              np.rad2deg(hglt),
  269.                                              hecr)
  270.     return HelioGraphicStonyhurst(representation)
  271.  
  272. @frame_transform_graph.transform(FunctionTransform, HelioGraphicStonyhurst, HelioCentric)
  273. def hgs_to_hcc(heliogcoord, heliopframe):
  274.     hglon = heliogcoord.hlon
  275.     hglat = heliogcoord.hlat
  276.     r = heliogcoord.rad.to(u.m)
  277.  
  278.     l0_deg = _carrington_offset()
  279.     b0_deg = s.heliographic_solar_center()[1]
  280.  
  281.     lon = np.deg2rad(hglon)
  282.     lat = np.deg2rad(hglat)
  283.  
  284.     cosb = np.cos(np.deg2rad(b0_deg))
  285.     sinb = np.sin(np.deg2rad(b0_deg))
  286.  
  287.     lon = lon - np.deg2rad(l0_deg)
  288.  
  289.     cosx = np.cos(lon)
  290.     sinx = np.sin(lon)
  291.     cosy = np.cos(lat)
  292.     siny = np.sin(lat)
  293.  
  294.     x = r * cosy * sinx
  295.     y = r * (siny * cosb - cosy * cosx * sinb)
  296.     zz = r * (siny * sinb + cosy * cosx * cosb)
  297.  
  298.     representation = CartesianRepresentation(x, y, zz)
  299.     return HelioCentric(representation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement