import os, sys, time

import matplotlib.pyplot as plt
from collections import defaultdict
import numpy as np
import pandas as pd

import binary_c
from binaryc_python_utils.defaults import physics_defaults
from binaryc_python_utils.functions import create_arg_string, parse_output, run_system

"""
Script to test some auto reading out.

todo: make to hdf5
"""

start = time.time()
output = run_system(M_1=10, M_2=20, separation=0, orbital_period=100000000000)
result = parse_output(output, 'DAVID_SINGLE_ANALYSIS')
stop = time.time()
print("Took {:.2f}s to run single system".format(stop-start))
print("The following keys are present in the results:\n{}".format(result.keys()))

#### Now do whatever you want with it: 
#t_res = np.asarray(result['t'], dtype=np.float64, order='C')
#m_res = np.asarray(result['mass'], dtype=np.float64, order='C')

# Cast the data into a dataframe. 
df = pd.DataFrame.from_dict(result, dtype=np.float64)
sliced_df = df[df.t < 1000] # Cut off late parts of evolution
print(sliced_df["t"])

plt.plot(sliced_df['t'], sliced_df['radius'])
plt.xlabel('Time (Myr)')
plt.ylabel('Radius (Rsol)')
plt.show()