# Python file for plotting the results from the cyclic water retention test
# on Hamburg Sand with parallel CT-imaging
# --------------------------------------------------------------------------
# CT scans using the Laboratoire 3SR X-ray tomograph at Univ. Grenoble Alpes
# 21.07.2019 - 22.07.2019, Grenoble
# 
# --------------------------------------------------------------------------
# This Python script visualises the measured specific air-water and solid-water
# interfacial areas as well as specific contact line in a central subvolume
# of 800x800x800 px inside the segmented CT data.
#
# Data files: Interfacial_area_data.txt and Contact_line_data.txt

# Import libraries:
# ==========================================================================

import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

from matplotlib import rcParams
import numpy as np

# Settings:
# ==========================================================================

# Colours:
n = 19 # number of different rainbow colours
my_color = plt.cm.jet(np.linspace(0,1,n))
my_linewidth = 1.5

savefigs = True     # Save figures to file?
# ==========================================================================
SMALL_SIZE = 8
MEDIUM_SIZE = 10
BIGGER_SIZE = 12

plt.rc('font', size=MEDIUM_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=MEDIUM_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=BIGGER_SIZE)     # fontsize of the x and y labels
plt.rc('xtick', labelsize=MEDIUM_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=MEDIUM_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=MEDIUM_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)   # fontsize of the figure title
# ---------------------------
rcParams['font.family'] = 'serif'
# ---------------------------
COLOR = 'black'
plt.rcParams['grid.color'] = COLOR
plt.rcParams['text.color'] = COLOR
plt.rcParams['axes.labelcolor'] = COLOR
plt.rcParams['xtick.color'] = COLOR
plt.rcParams['ytick.color'] = COLOR
plt.rcParams['legend.edgecolor'] = COLOR
plt.rc('axes',edgecolor=COLOR)

# ==========================================================================
# Read the data:


# Interfacial area data:
my_filename = 'Interfacial_area_data.txt'


step,A_nw,a_nw,A_sw,a_sw,Sr_macro,Sr_subvol,s_before_scan,s_after_scan= np.loadtxt(my_filename,
                                   unpack = True,
                                   delimiter = None,
                                   skiprows = 1,
                                   usecols = (0,1,2,3,4,5,6,7,8))

# Contact line data:
my_filename = 'Contact_line_data.txt'


scan_number,L_contact,l_contact= np.loadtxt(my_filename,
                                   unpack = True,
                                   delimiter = None,
                                   skiprows = 1,
                                   usecols = (0,1,2))


# ==========================================================================
plot_1 = plt.figure(1)
#
ax = plot_1.add_subplot(111)
ax.patch.set_facecolor('white')

plt.plot(Sr_subvol,a_nw,
        label = ('Subvolume-averaged data'),
        linestyle='None',
        color = 'k',
        marker = 's',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0,
        markersize = 6.0,
        linewidth = my_linewidth)

# Hydraulic steps in different colours:
for i in range(0,19):
    
    plt.plot(Sr_subvol[i:i+1+1],a_nw[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,
        markersize = 3.0,
        linewidth = my_linewidth)

plt.xlabel('Degree of saturation $S_{\mathrm{r}}$ [-]')
plt.ylabel('Air-water interfacial area\n $a^{\mathrm{nw}}$ [mm²/mm³]')

plt.legend(loc ='lower left',fancybox=True,shadow=False,facecolor='white', framealpha=1)

plt.grid(True,color='k',linestyle='--')
plt.ylim(0,1.2)
plt.xlim(0,1)
plot_1.set_size_inches(18/2.54,12/2.54)
# ---
plot_1.show()

# ==========================================================================
plot_2 = plt.figure(2)
#
ax = plot_2.add_subplot(111)
ax.patch.set_facecolor('white')

plt.plot(Sr_subvol,a_sw,
        label = ('Subvolume-averaged data'),
        linestyle='None',
        color = 'k',
        marker = 's',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0,
        markersize = 6.0,
        linewidth = my_linewidth)

# Hydraulic steps in different colours:
for i in range(0,19):
    
    plt.plot(Sr_subvol[i:i+1+1],a_sw[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,
        markersize = 3.0,
        linewidth = my_linewidth)

plt.xlabel('Degree of saturation $S_{\mathrm{r}}$ [-]')
plt.ylabel('Solid-water interfacial area\n $a^{\mathrm{sw}}$ [mm²/mm³]')

plt.legend(loc ='lower right',fancybox=True,shadow=False,facecolor='white', framealpha=1)

plt.grid(True,color='k',linestyle='--')
plt.ylim(0,6.5)
plt.xlim(0,1)
plot_2.set_size_inches(18/2.54,12/2.54)
# ---
plot_2.show()

# ==========================================================================
plot_3 = plt.figure(3)
#
ax = plot_3.add_subplot(111)
ax.patch.set_facecolor('white')

plt.plot(s_before_scan,a_nw,
        label = ('Suction before scans'),
        linestyle='None',
        color = 'k',
        marker = 'o',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0, 
        markersize = 6.0,
        linewidth = my_linewidth)

plt.plot(s_after_scan,a_nw,
        label = ('Suction after scans'),
        linestyle='None',
        color = 'k',
        marker = 's',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0, 
        markersize = 6.0,
        linewidth = my_linewidth)

# Hydraulic steps in different colours:
for i in range(0,19):
    
    plt.plot(s_before_scan[i:i+1+1],a_nw[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,     
        markersize = 3.0,
        linewidth = my_linewidth)

for i in range(0,19):
    
    plt.plot(s_after_scan[i:i+1+1],a_nw[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,     
        markersize = 3.0,
        linewidth = my_linewidth)    

plt.xlabel('Matric suction $s$ [kPa]')
plt.ylabel('Air-water interfacial area\n $a^{\mathrm{nw}}$ [mm²/mm³]')

plt.legend(loc ='lower right',fancybox=True,shadow=False,facecolor='white', framealpha=1)

plt.grid(True,color='k',linestyle='--')
plt.ylim(0,1.2)
plt.xlim(0,1.4)
plot_3.set_size_inches(18/2.54,12/2.54)
# ---
plot_3.show()

# ==========================================================================
plot_4 = plt.figure(4)
#
ax = plot_4.add_subplot(111)
ax.patch.set_facecolor('white')

plt.plot(s_before_scan,a_sw,
        label = ('Suction before scans'),
        linestyle='None',
        color = 'k',
        marker = 'o',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0, 
        markersize = 6.0,
        linewidth = my_linewidth)

plt.plot(s_after_scan,a_sw,
        label = ('Suction after scans'),
        linestyle='None',
        color = 'k',
        marker = 's',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0, 
        markersize = 6.0,
        linewidth = my_linewidth)

# Hydraulic steps in different colours:
for i in range(0,19):
    
    plt.plot(s_before_scan[i:i+1+1],a_sw[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,     
        markersize = 3.0,
        linewidth = my_linewidth)

for i in range(0,19):
    
    plt.plot(s_after_scan[i:i+1+1],a_sw[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,     
        markersize = 3.0,
        linewidth = my_linewidth) 

plt.xlabel('Matric suction $s$ [kPa]')
plt.ylabel('Solid-water interfacial area\n $a^{\mathrm{sw}}$ [mm²/mm³]')

plt.legend(loc ='lower left',fancybox=True,shadow=False,facecolor='white', framealpha=1)

plt.grid(True,color='k',linestyle='--')
plt.ylim(0,6.5)
plt.xlim(0,1.4)
plot_4.set_size_inches(18/2.54,12/2.54)
# ---
plot_4.show()

# ==========================================================================
plot_5 = plt.figure(5)
#
ax = plot_5.add_subplot(111)
ax.patch.set_facecolor('white')

plt.plot(Sr_subvol,l_contact,
        label = ('Subvolume-averaged data'),
        linestyle='None',
        color = 'k',
        marker = 's',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0,
        markersize = 6.0,
        linewidth = my_linewidth)

# Hydraulic steps in different colours:
for i in range(0,19):
    
    plt.plot(Sr_subvol[i:i+1+1],l_contact[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,
        markersize = 3.0,
        linewidth = my_linewidth)

plt.xlabel('Degree of saturation $S_{\mathrm{r}}$ [-]')
plt.ylabel('Specific length of contact line\n $l_{\mathrm{c}}$ [mm/mm³]')

plt.legend(loc ='lower left',fancybox=True,shadow=False,facecolor='white', framealpha=1)

plt.grid(True,color='k',linestyle='--')
plt.ylim(0,60)
plt.xlim(0,1)
plot_5.set_size_inches(18/2.54,12/2.54)
# ---
plot_5.show()

# ==========================================================================
plot_6 = plt.figure(6)
#
ax = plot_6.add_subplot(111)
ax.patch.set_facecolor('white')

plt.plot(s_before_scan,l_contact,
        label = ('Suction before scans'),
        linestyle='None',
        color = 'k',
        marker = 'o',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0, 
        markersize = 6.0,
        linewidth = my_linewidth)

plt.plot(s_after_scan,l_contact,
        label = ('Suction after scans'),
        linestyle='None',
        color = 'k',
        marker = 's',
        markerfacecolor = 'None',
        markeredgecolor = 'k',
        markeredgewidth = 2.0, 
        markersize = 6.0,
        linewidth = my_linewidth)

# Hydraulic steps in different colours:
for i in range(0,19):
    
    plt.plot(s_before_scan[i:i+1+1],l_contact[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,     
        markersize = 3.0,
        linewidth = my_linewidth)

for i in range(0,19):
    
    plt.plot(s_after_scan[i:i+1+1],l_contact[i:i+1+1],
        linestyle='--',
        color = my_color[i],
        marker = '',
        markerfacecolor = my_color[i],
        markeredgecolor = my_color[i],
        markeredgewidth = 2.0,     
        markersize = 3.0,
        linewidth = my_linewidth) 

plt.xlabel('Matric suction $s$ [kPa]')
plt.ylabel('Specific length of contact line\n $l_{\mathrm{c}}$ [mm/mm³]')

plt.legend(loc ='lower right',fancybox=True,shadow=False,facecolor='white', framealpha=1)

plt.grid(True,color='k',linestyle='--')
plt.ylim(0,60)
plt.xlim(0,1.4)
plot_6.set_size_inches(18/2.54,12/2.54)
# ---
plot_6.show()

# ==========================================================================
# Save figures to file if savefigs == True:

if savefigs == True:
    print('Saving figures to file...')
    plot_1.savefig("a_nw_vs_Sr_CT.pdf", bbox_inches='tight')
    plot_2.savefig("a_sw_vs_Sr_CT.pdf", bbox_inches='tight')
    plot_3.savefig("a_nw_vs_s_CT.pdf", bbox_inches='tight')
    plot_4.savefig("a_sw_vs_s_CT.pdf", bbox_inches='tight')
    plot_5.savefig("l_c_vs_Sr_CT.pdf", bbox_inches='tight')
    plot_6.savefig("l_c_vs_s_CT.pdf", bbox_inches='tight') 
    print('Done.')
else:
    print('Figures not saved to file.')


  
