# coding: utf-8

""" Classes for accessing simulation data related to Sagittarius. """

from __future__ import division, print_function

__author__ = "adrn <adrn@astro.columbia.edu>"

# Standard library
import os, sys

# Third-party
import numpy as np
import numexpr
import astropy.units as u
from astropy.constants import G

# Project
from .core import read_table, table_to_particles, table_to_orbits
from ..util import project_root, u_galactic
from ..dynamics import Particle, Orbit
from ..integrate.leapfrog import LeapfrogIntegrator
from ..potential.lm10 import LawMajewski2010

__all__ = ["particle_table", "particles_today", "satellite_today", \
           "time", "satellite_orbit"]

_lm10_path = os.path.join(project_root, "data", "simulation", "LM10")

class LM10Simulation(SimulationData):

    def __init__(self,
                 filename=os.path.join(project_root,"SgrTriax_DYN.dat")):
        """ ...

            Parameters
            ----------

        """
        super(LM10Simulation, self).__init__(...)
        self._hel_colnames = ("l","b","D","mul","mub","vr") # TODO: FIX

    @property
    def table(self):
        tbl = super(LM10Simulation, self).table
        tbl.rename_column("xgc","x")
        tbl.rename_column("ygc","y")
        tbl.rename_column("zgc","z")

        tbl.rename_column("u","vx")
        tbl.rename_column("v","vy")
        tbl.rename_column("w","vz")

        tbl["x"] = -tbl["x"]
        tbl["vx"] = -tbl["vx"]

        for x in "xyz":
            tbl[x].unit = u.kpc

        for x in ("vx","vy","vz"):
            tbl[x].unit = u.km/u.s

        return tbl


def satellite_today():
    """ Read in the position and velocity of the Sgr satellite at the end of
        the Law & Majewski 2010 simulation (e.g., present day position to be
        back-integrated).

    """

    # Here are the true parameters from the last block in R601LOG
    r0 = np.array([[2.3279727753E+01,2.8190329987,-6.8798148785]])*length_unit
    v0 = np.array([[3.9481694047,-6.1942673069E-01,3.4555581435]])*length_unit/time_unit

    satellite = Particle(r=r0.to(u.kpc),
                         v=v0.to(u.kpc/u.Myr),
                         m=[6E8]*u.M_sun)

    return satellite

def satellite_orbit():
    """ Read in the full orbit of the satellite. """
    sat_colnames = ["t","x","y","z","vx","vy","vz","col8","col9","col10"]
    data = read_table(filename="orb780.dat", expr="t<0", path=_lm10_path)

    oc = table_to_orbits(data, lm10_usys,
                         position_columns=["x","y","z"],
                         velocity_columns=["vx","vy","vz"])

    return oc.to(u_galactic)

def time():
    """ Time information for the Law & Majewski 2010 simulation """
    t1 = 0.
    t2 = -8000.

    return t1, t2

@@ -351,341 +351,82 @@
 ort 
-read_table, table_to_particles, table_to_orbits%0Afrom ..util import project_root, u_galactic%0Afrom ..dynamics import Particle, Orbit%0Afrom ..integrate.leapfrog import LeapfrogIntegrator%0Afrom ..potential.lm10 import LawMajewski2010%0A%0A__all__ = %5B%22particle_table%22, %22particles_today%22, %22satellite_today%22, %5C%0A           %22time%22, %22satellite_orbit
+SimulationData%0Afrom ..util import project_root%0A%0A__all__ = %5B%22LM10Simulation
 %22%5D%0A%0A
@@ -597,28 +597,26 @@
 th.join(
-project_root
+_lm10_path
 ,%22SgrTri
@@ -827,16 +827,62 @@
 ODO: FIX
+%0A        self.t1 = 0.%0A        self.t2 = -8000.
 %0A%0A    @p
@@ -1388,17 +1388,20 @@
 rn tbl%0A%0A
-%0A
+    
 def sate
@@ -1409,1134 +1409,296 @@
 lite
-_today():%0A    %22%22%22 Read in the position and velocity of the Sgr satellite at the end of%0A        the Law & Majewski 2010 simulation (e.g., present day position to be%0A        back-integrated).%0A%0A    %22%22%22%0A%0A    # Here are the true parameters from the last block in R601LOG%0A    r0 = np.array(%5B%5B2.3279727753E+01,2.8190329987,-6.8798148785%5D%5D)*length_unit%0A    v0 = np.array(%5B%5B3.9481694047,-6.1942673069E-01,3.4555581435%5D%5D)*length_unit/time_unit%0A%0A    satellite = Particle(r=r0.to(u.kpc),%0A                         v=v0.to(u.kpc/u.Myr),%0A                         m=%5B6E8%5D*u.M_sun)%0A%0A    return satellite%0A%0Adef satellite_orbit():%0A    %22%22%22 Read in the full orbit of the satellite. %22%22%22%0A    sat_colnames = %5B%22t%22,%22x%22,%22y%22,%22z%22,%22vx%22,%22vy%22,%22vz%22,%22col8%22,%22col9%22,%22col10%22%5D%0A    data = read_table(filename=%22orb780.dat%22, expr=%22t%3C0%22, path=_lm10_path)%0A%0A    oc = table_to_orbits(data, lm10_usys,%0A                         position_columns=%5B%22x%22,%22y%22,%22z%22%5D,%0A                         velocity_columns=%5B%22vx%22,%22vy%22,%22vz%22%5D)%0A%0A    return oc.to(u_galactic)%0A%0Adef time():%0A    %22%22%22 Time information for the Law & Majewski 2010 simulation %22%22%22%0A    t1 = 0.%0A    t2 = -8000.%0A%0A    return t1, t2
+(self, bound_expr=%22Pcol%3E-1%22, frame=%22galactocentric%22,%0A                  column_names=None):%0A        super(LM10Simulation, self).satellite(bound_expr=bound_expr,%0A                                              frame=frame,%0A                                              column_names=column_names)
 %0A

remove old shite