# -*- coding: utf-8 -*-
"""
Created on Wed Dec  6 11:19:43 2023

@author: nielbock
"""
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import matplotlib.ticker as ticker
import numpy as np
import pandas as pd
from scipy.interpolate import InterpolatedUnivariateSpline
import os

# Get the directory
wdir=os.getcwd()

LANG="DE"
#LANG="EN"

DOTS = 100000

# Create a figure and axes
fig, ax = plt.subplots()


# Read empirical data from CSV file
data = pd.read_csv('massradius_100percentH2O_1000K_1mbar.txt')

# Extract mass and radius from empirical data (Earth values)
empirical_mass = data['mass']
empirical_radius = data['radius']

# Perform spline interpolation on empirical data
spline = InterpolatedUnivariateSpline(empirical_mass, empirical_radius)

# Generate interpolated dataset
interpolated_mass = np.linspace(empirical_mass.min(), empirical_mass.max(), DOTS)  # Adjust the number of points as needed
interpolated_radius = spline(interpolated_mass)

# Plot interpolated data
if LANG == "DE":
    model="Wasser bei 1000 Kelvin"
else:
    model="Water @ 1000 Kelvin"
ax.plot(interpolated_mass, interpolated_radius, color='cyan', linestyle='dashed', label=model)



# Read empirical data from CSV file
data = pd.read_csv('massradius_100percentH2O_300K_1mbar.txt')

# Extract mass and radius from empirical data (Earth values)
empirical_mass = data['mass']
empirical_radius = data['radius']

# Perform spline interpolation on empirical data
spline = InterpolatedUnivariateSpline(empirical_mass, empirical_radius)

# Generate interpolated dataset
interpolated_mass = np.linspace(empirical_mass.min(), empirical_mass.max(), DOTS)  # Adjust the number of points as needed
interpolated_radius = spline(interpolated_mass)

# Plot interpolated data
if LANG == "DE":
    model="Wasser bei 300 Kelvin"
else:
    model="Water @ 300 Kelvin"
ax.plot(interpolated_mass, interpolated_radius, color='blue', linestyle='dashed', label=model)


# Read empirical data from CSV file
data = pd.read_csv('massradiusmgsio3.txt')

# Extract mass and radius from empirical data (Earth values)
empirical_mass = data['mass']
empirical_radius = data['radius']

# Perform spline interpolation on empirical data
spline = InterpolatedUnivariateSpline(empirical_mass, empirical_radius)

# Generate interpolated dataset
interpolated_mass = np.linspace(empirical_mass.min(), empirical_mass.max(), DOTS)  # Adjust the number of points as needed
interpolated_radius = spline(interpolated_mass)

# Plot interpolated data
if LANG == "DE":
    model='Silikatplanet (MgSiO$_3$)'
else:
    model='Silicate Planet (MgSiO$_3$)'
ax.plot(interpolated_mass, interpolated_radius, color='grey', linestyle='dashed', label=model)


# Read empirical data from CSV file
data = pd.read_csv('massradiusEarthlikeRocky.txt')

# Extract mass and radius from empirical data (Earth values)
empirical_mass = data['mass']
empirical_radius = data['radius']

# Perform spline interpolation on empirical data
spline = InterpolatedUnivariateSpline(empirical_mass, empirical_radius)

# Generate interpolated dataset
interpolated_mass = np.linspace(empirical_mass.min(), empirical_mass.max(), DOTS)  # Adjust the number of points as needed
interpolated_radius = spline(interpolated_mass)

# Plot interpolated data
if LANG == "DE":
    model='Erdähnlicher Felsenplanet'
else:
    model='Earth-like Rocky Planet'
ax.plot(interpolated_mass, interpolated_radius, color='green', linestyle='dashed', label=model)


# Read empirical data from CSV file
data = pd.read_csv('massradiusFe.txt')

# Extract mass and radius from empirical data (Earth values)
empirical_mass = data['mass']
empirical_radius = data['radius']

# Perform spline interpolation on empirical data
spline = InterpolatedUnivariateSpline(empirical_mass, empirical_radius)

# Generate interpolated dataset
interpolated_mass = np.linspace(empirical_mass.min(), empirical_mass.max(), DOTS)  # Adjust the number of points as needed
interpolated_radius = spline(interpolated_mass)

# Plot interpolated data
if LANG == "DE":
    model='Eisenplanet'
else:
    model='Iron Planet'
ax.plot(interpolated_mass, interpolated_radius, color='red', linestyle='dashed', label=model)


# Set labels and title
if LANG == "DE":
    ax.set_xlabel('Masse (Erdmassen)')
    ax.set_ylabel('Radius (Erdradien)')
    #ax.set_title('Planetenradius vs. Masse')
else:
    ax.set_xlabel('Mass (Earth mass)')
    ax.set_ylabel('Radius (Earth radius)')
    #ax.set_title('Planet Radius vs. Mass')

# Change x-axis tick format from point to comma for decimals
if LANG == "DE":
    formatter = ticker.FuncFormatter(lambda x, pos: f'{x:.1f}'.replace('.', ','))
    plt.gca().xaxis.set_major_formatter(formatter)
    formatter = ticker.FuncFormatter(lambda y, pos: f'{y:.1f}'.replace('.', ','))
    plt.gca().yaxis.set_major_formatter(formatter)

# Adjust the aspect ratio to make both axes have the same scaling
plt.gca().set_aspect('equal', adjustable='box')

# Set custom axis limits
x_min, x_max = 0, 5.1  # Custom x-axis limits
y_min, y_max = 0, 3.1  # Custom y-axis limits
ax.set_xlim(x_min, x_max)
ax.set_ylim(y_min, y_max)

# Set major and minor ticks
ax.xaxis.set_major_locator(MultipleLocator(0.5))  # Major ticks at every integer
ax.xaxis.set_minor_locator(MultipleLocator(0.1))  # Minor ticks at intervals of 0.1

ax.yaxis.set_major_locator(MultipleLocator(0.5))  # Major ticks at every integer
ax.yaxis.set_minor_locator(MultipleLocator(0.1))  # Minor ticks at intervals of 0.1

# Add grid lines
ax.grid(True, linestyle='-', linewidth=0.2, which='major', color='grey')  # Solid lines for integers
ax.grid(True, linestyle='--', linewidth=0.1, which='minor', color='grey')  # Dashed lines for decimals

# Add legend with custom labels
ax.legend()

# Add legend with custom labels
legend_x = 0.99
legend_y = 0.02
ax.legend(loc='lower right', bbox_to_anchor=(legend_x, legend_y), fontsize=0.7 * plt.rcParams['font.size'])

# Export the plot to PNG and PDF with high resolution
dpi = 600  # Resolution in dots per inch

output_path = rf'{wdir}\Plots\Exoplanets_M_vs_R_models_grid'

if LANG == "DE":
    plt.savefig(f'{output_path}_de.png', dpi=dpi)
    plt.savefig(f'{output_path}_de.pdf', dpi=dpi)
else:
    plt.savefig(f'{output_path}_en.png', dpi=dpi)
    plt.savefig(f'{output_path}_en.pdf', dpi=dpi)

# Display the plot
plt.show()