mpl.style.use('default')
file_path = pathlib.Path(__file__).parent.absolute()
sys.path.append(str(file_path) + "/../LIB")
from plot_utils import save_fig
from fourier_solver import solve_pacman
from utils import read_performance_file
from numpy import pi
###############################################################################
# LATEX FONT
font = {'family': 'serif', 'size': 18}
rc('font', **font)
rc('text', usetex=True)
###############################################################################
# COLORMAP:
fc = farge_colormaps.farge_colormap_multi(taille=600,
                                          limite_faible_fort=0.2,
                                          etalement_du_zero=0.04,
                                          type='pressure')
###############################################################################
# %% Specify:
###############################################################################
# %% get the data
quantity = "q_"
plt.close("all")
case = "CDF44_dealias_pacman"
#case = "CDF44_pacman"
ini = "pacman.ini"
eps_dir = "./adaptive_" + case + "_Bs17_Jmax7*eps1.0e-04_iter1/"
norm = 2
# %%

files = glob.glob(eps_dir + '/' + quantity + '*.h5')
#plt.close('all')
fig = plt.figure(2)
a = 2
plt.gcf().set_size_inches([5.0 * a, 5.0 * a])
plt.gcf().subplots_adjust(top=0.95, bottom=0.05, left=0.02, right=0.97)

overwrite = True

# for prefix, cmap in zip(['vort', 'p', 'div', 'Ux', 'Uy'],['vorticity','pressure','streamfunction','velocity','velocity']):
for prefix, cmap in zip(['div'], ['streamfunction']):
    files = glob.glob('/home/engels/dev/WABBIT4-new-physics/WABBIT_div_test/' +
                      prefix + '_*.h5')
    files.sort()

    fc = farge_colormaps.farge_colormap_multi(taille=600,
                                              limite_faible_fort=0.2,
                                              etalement_du_zero=0.02,
                                              type=cmap)

    for file, i in zip(files[1:], range(len(files[1:]))):
        file2 = file.replace('.h5', '.png')
        if not os.path.isfile(file2) or overwrite:
            wabbit_tools.plot_wabbit_file(file,
                                          savepng=True,
                                          cmap=fc,
                                          ticks=False,
                                          colorbar=True,
                                          dpi=300,
                                          caxis=[-0.06, 0.06],
                                          title=True,
                                          block_edge_color='w',
                                          caxis_symmetric=True,
import flusi_tools as flusi
import numpy as np
import insect_tools
import os
import farge_colormaps
import glob
from matplotlib import rc
import matplotlib.gridspec as gs
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
font = {'family' : 'sans-serif',
        'sans-serif':['Helvetica'],
        'size'   : 22}
rc('font',**font)
rc('text', usetex=True)     

fc_default = farge_colormaps.farge_colormap_multi(taille=600,etalement_du_zero=0.04, type='vorticity' )

def plot_modes(files,acoef_file,quantity,quantity_replace,pic_dir="./",fc=fc_default):
    
    f = open(acoef_file)
    data = [[float(num) for num in line.split()] for line in f]
    f.close
    data = np.asarray(data)
    amin = np.amin(data)
    amax = np.amax(data)
    print("min(a_coef):", amin)
    print("max(a_coef):", amax)
    for mode,file in enumerate(files):
        plt_file = file.split("/")[-1]
        plt_file = plt_file.replace(quantity+"_",quantity_replace+"_")
        plt_file = plt_file.replace('h5','png')
                      [float("%1.1e" % eps) for eps in np.logspace(-8, 0, 9)])
wdir = dirs["wabbit"]
fig_adapt = [plt.figure(41)]
ax_adapt = [fig.add_subplot() for fig in fig_adapt]
h5_fname = [
    sorted(glob.glob(data["folder"] + '/Jmax6/' + qty + '*550*.h5'))[0]
    for qty in data["qname"]
]
[l2error, linferror, Nblocks, Nblocksdense]=adapt2eps(h5_fname, \
wdir, eps_list, wabbit_setup['memory'], wabbit_setup['mpicommand'], wavelets="CDF44",\
normalization = "L2",create_log_file=True, show_plot=True, pic_dir=dir_list["images"])

# %% get the data
# COLORMAP:
fc = farge_colormaps.farge_colormap_multi(taille=600,
                                          limite_faible_fort=0.2,
                                          etalement_du_zero=0.04,
                                          type='vorticity')
plt.close("all")

fig, axes = plt.subplots(nrows=3, ncols=2)
fig.subplots_adjust(bottom=0.001, top=0.999, wspace=0.01, hspace=-0.8)

for i, eps in enumerate(np.take(eps_list, [0, 2, 4, 6, 7, 8])):

    files = sorted(glob.glob('./adapt_files/u[1,2]-eps%1.1e_' % eps + '*.h5'))
    success = compute_vorticity(wdir, files[0], files[1],
                                wabbit_setup["memory"], "")
    file = files[0].replace('u1-eps%1.1e_' % eps, 'vorx_')
    file = os.path.split(file)[1]
    ax,cb,hplot = wt.plot_wabbit_file(file,cmap=fc,caxis_symmetric=True,dpi=300, \
                                 shading='flat', caxis=[-6,6], \