=================================
Solar wind direction in IMA frame
=================================

In this document, you may find the way to calculate the
solar wind direction in the IMA frame.

The description is on-the-fly document
during the feature branch of feature/swdir_in_imaframe.

The solar wind direction
========================

The solar wind is flowing almost parallel to the Sun-Mars line,
while 2-3 degrees of deviation is found both in |phi| and |theta| directions
(:ref:`issue_swparams_statistics`).
Here |phi| is measured from ``-x`` direction with positive toward ``+y``
and |theta| is the angle from ``x-y`` plane.
The MSO frame is used, while no abberation is yet considered.

The above statement can be expressed by the following math form.

.. math::

        Vx &= -V\cos\theta\cos\phi \\
        Vy &= V\cos\theta\sin\phi \\
        Vz &= V\sin\theta

In addition, the abberation should be considered.
This is originated from the motion of the spacecraft
relative to the inertia frame (more precisely speaking,
the Sun fixed frame).

It is not very trivial which velocity in what frame with
what correction is most simple to get this information.

Which velocity is most simple (I think).  The spacecraft
velocity with respective to the Sun.
It is very close to the Mars velocity relative to the Sun.

In what frame? It should be in the "Spacecraft" frame
(or IMA frame, where translation in between is rigidly fixed).
Who is the object? Spacecraft.

Mmm. Very confused. Spacecraft velocity in the spacecraft
frame should be zero, intuitively...
(And, indeed spice also returns so)

Velocity in SPICE
=================

Try spice.

.. code-block:: python

        >>> ms.get_posvel(datetime.datetime(2010, 1, 1),
        ... target='MEX', origin='SUN', frame='MEX_SPACECRAFT',
        ... correction='None')
        1.18832037e+00,  -1.82601002e-03,   2.05399347e+00

Well, it is not very clear what velocity it is.
I expect velocity something like 25 km/s.

Indeed, this is something like "apparent" velocity in a non-inertia frame.  In the calculation, spice derives the veloicyt as

.. math::

        \lim_{dt\to 0}\frac{\vec{x}(t+dt) - \vec{x}(t)}{dt}

and :math:`\vec{x}` is derived in the frame at each time.
Therefore, a very "distant" non-inertia frame will derive very large
velocity quantity.

.. code-block:: python

        >>> print ms.get_posvel(datetime.datetime(2010, 1, 1),
        ... target='MEX', origin='SUN', frame='IAU_SUN',
        ... correction='None')[3:]
        511.71998832    -438.67762313   -3.43707870

This is because the ``IAU_SUN`` frame rotates as the Sun rotates, and
the velocity of the "frame" at Mars orbit is something like
:math:`2\pi \cdot 2.5\times 10^8 \mathrm{km}/ (30 \cdot 86400 \mathrm{s})~600 \mathrm{km/s}`.

Indeed, the correction is not effective. No very big difference is found.
However, considering the principle,
no correction on the light time is needed, and
no correction on the abberation of frame is needed.
So that in SPICE, you may use "None" for correction.

An exercise
-----------

.. code-block:: python

        >>> import datetime
        >>> from irfpy.mexpvat import mexspice as ms
        >>> ms.init()
        >>> target='MEX'
        >>> origin='MARS'
        >>> frame='J2000'
        >>> for correction in ('None', 'LT', 'LT+S'):
        ...     print ms.get_posvel(datetime.datetime(2010, 1, 1),
        ...          target=target, origin=origin, frame=frame,
        ...          correction=correction)[3:]

By changing target, origin and frame, one can search for the
velocity vector.

The results is as below.

+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|Target |Origin |Frame          |Corr   |Vx             |Vy             |Vz             |Vtot           |theta   |phi    |
+=======+=======+===============+=======+===============+===============+===============+===============+========+=======+
|MEX    |SUN    |MEX_SPACECRAFT |None   |1.18832037     |-0.00182601    |2.05399347     |2.37297236     |30.051  |90.0880|
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT     |1.10997952     |-0.00147104    |1.91886902     |2.21678033     |30.047  |90.0759|
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT+S   |1.10997990     |-0.00147093    |1.91886880     |2.21678033     |30.047  |90.0759|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |Mars   |MEX_SPACECRAFT |None   |1.26637929     |-1.16411868    |0.35015275     |1.75541897     |78.494  |132.590|
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT     |1.26631973     |-1.16414917    |0.35017201     |1.75540006     |78.493  |132.592|
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT+S   |1.26634670     |-1.16408683    |0.35014719     |1.75537323     |78.493  |132.590|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |MEX    |MEX_SPACECRAFT |None   |0.00000000     |0.00000000     |0.00000000     |0.00000000     |0       |0      |
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT     |0.00000000     |0.00000000     |0.00000000     |0.00000000     |0       |0      |
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT+S   |0.00000000     |0.00000000     |0.00000000     |0.00000000     |0       |0      |
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |Earth  |MEX_SPACECRAFT |None   |-13.77548614   |-5.87634770    |5.37282297     |15.91108756    |70.264  |-113.10|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |SUN    |J2000          |None   |-21.59374840   |-7.05161227    |-4.20846585    |23.10251913    |100.49  |-108.08|
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT     |-21.62033610   |-7.10624758    |-4.52783773    |23.20428845    |101.25  |-108.19|
+       +       +               +-------+---------------+---------------+---------------+---------------+--------+-------+
|       |       |               |LT+S   |-21.62033620   |-7.10624830    |-4.52783809    |23.20428883    |101.25  |-108.19|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |Mars   |J2000          |None   |-0.85566535    |1.15877170     |-1.00270765    |1.75508908     |124.84  |-36.443|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |MEX    |J2000          |None   |0.00000000     |0.00000000     |0.00000000     |0.00000000     |0       |0      |
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |SUN    |IAU_SUN        |None   |511.71998832   |-438.67762313  |-3.43707870    |674.02315761   |90.292  |130.605|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |SUN    |J2000          |None   |-21.59374840   |-7.05161227    |-4.20846585    |23.10251913    |100.49  |-108.08|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |SUN    |ECLIPJ2000     |None   |-21.59374836   |-8.14375934    |-1.05622166    |23.10251910    |92.620  |-110.66|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |SUN    |GALACTIC       |None   |9.38031143     |-10.67655867   |18.21392987    |23.10251910    |37.964  |138.697|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+
|MEX    |SUN    |MSO            |None   |-2.37291154    |-0.44958875    |-1.41509286    |2.79916542     |120.36  |-100.72|
+-------+-------+---------------+-------+---------------+---------------+---------------+---------------+--------+-------+


Now, what is the solar wind direction?
======================================

How to calculate the solar wind direction?
Ok, assume that the solar wind is "fixed" in any inertia frame of Sun center.
This is derived from the fact that the solar wind propagate almost straightly
from the Sun without feeling any forces (force-free).

On the other hand, Mars frame (MSO) or spacecraft frame (SC) is traveling
across the frame. This is the 'abberation' having discussed in this section.

Ok, I assume you know the solar wind velocity in the J2000 frame (:math:`\vec{V_{sw}}`).
It is usually assumed "radial" velocity vector with a few degrees deviation
(indicated by WIND data. See :ref:`issue_swparams_statistics`.

Then, you may know the spacecraft velocity (or Mars velocity) in J2000 (:math:`\vec{V_{sc}}`).
You can subtract it from the solar wind velocity in J2000, you may know
the s/c abberrated solar wind velocity, :math:`\vec{v_{sw}}=\vec{V_{sw}}-\vec{V_{sc}}`.
Note that this is yet J2000 reference frame.

You also know, independently, a conversion matrix from J2000 to Spacecraft coordinate system.
Multiplying the matrix, :math:`\textbf{M}`, to  the solar wind vector, you may get the
abberated solar wind velocity vector in the SC frame.

.. math::

        \vec{w_{sw}} = \textbf{M}\cdot\vec{v_{sw}}

To me, this is the most simple way to calculate the solar wind velocity in the SC frame.

The key is that the spacecraft velocity should be calculated from the "solar wind" as origin.

An exercise
-----------

.. code-block:: python

        >>> import numpy as np
        >>> import datetime
        >>> from irfpy.mexpvat import mexspice as ms
        >>> ms.init()
        >>> t0 = datetime.datetime(2007, 12, 10)
        >>> et0 = ms.spice.str2et(t0.strftime('%FT%T'))
        >>> rvm = ms.get_posvel(t0, target='MaRS', origin='SUN', correction='None', frame='J2000')
        >>> rm = rvm[:3]
        >>> vm = rvm[3:]
        >>> print rm, vm
        [  1.95308260e+07   2.11300227e+08   9.63893224e+07] [-23.2270546    3.48866494   2.22763659]
        >>> rvsc = ms.get_posvel(t0, target='MEX', origin='SUN', correction='None', frame='J2000')
        >>> rsc = rvsc[:3]
        >>> vsc = rvsc[3:]
        >>> print rsc, vsc
        [  1.95239861e+07   2.11297999e+08   9.63996136e+07] [-21.93529536   3.04962571   2.33319464]
        >>> Vsw = 400 * rm / np.sqrt((rm ** 2).sum())
        >>> print 'Vsw (J2000)', Vsw
        Vsw (J2000) [  33.51970279  362.64317846  165.42779284]
        >>> vsw = Vsw - vsc
        >>> print 'vsw (J2000/abr)', vsw
        vsw (J2000/abr) [  55.45499815  359.59355275  163.0945982 ]
        >>> M=np.array(ms.spice.pxform('J2000', 'MSO', et0))
        >>> print 'M(J2000->MSO)', M
        M(J2000->MSO) [[-0.08379926 -0.90660795 -0.41356948]
         [ 0.99618024 -0.06599384 -0.05718175]
         [ 0.02454839 -0.41678153  0.90867515]]
        >>> print 'wsw (MSO)', M.dot(vsw)
        wsw (MSO) [ -3.98108408e+02   2.21861803e+01  -3.10612044e-01]
        >>> M=np.array(ms.spice.pxform('J2000', 'MEX_SPACECRAFT', et0))
        >>> print 'M(J2000->S/C)', M
        M(J2000->S/C) [[-0.0534572   0.8955422   0.44175389]
         [ 0.00678712 -0.44205037  0.89696455]
         [ 0.99854708  0.05094745  0.01755263]]
        >>> print 'wsw (S/C)', M.dot(vsw)
        wsw (S/C) [ 391.11440447  -12.29201183   76.55754057]