<jupyter_start><jupyter_text>This notebooks provides an example of how to create plots displaying the results of the analysis.As a 4D analysis is made up of of several 2D interpolations (loops on time and depth), we can use the tools of the `pydiva2d` module to produce the figures for the contours, meshes and results.The main difference with [plot_diva4D_results](./plot_diva4D_results.ipynb) is that here the figures are produced using the [Basemap](http://matplotlib.org/basemap/api/basemap_api.html) toolkit.<jupyter_code>import os
import sys
import logging
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from importlib import reload
reload(pydiva4d)
sys.path.insert(0, "../")
import pydiva4d
import pydiva2d<jupyter_output><empty_output><jupyter_text>Set the logging levels.<jupyter_code>pydiva2d.logger.setLevel(logging.DEBUG)
pydiva4d.logger.setLevel(logging.DEBUG)<jupyter_output><empty_output><jupyter_text>Define a directory for the figures:<jupyter_code>figdir = './figures/BlackSea/'
if not os.path.exists(figdir):
    os.makedirs(figdir)<jupyter_output><empty_output><jupyter_text>Define the directory where Diva will be executed:<jupyter_code>divamaindir = '/home/ctroupin/Software/DIVA/DIVA-diva-4.7.1/'
Diva4Ddirs = pydiva4d.Diva4DDirectories(divamaindir)
Diva4Dfiles = pydiva4d.Diva4Dfiles(Diva4Ddirs.diva4d)<jupyter_output><empty_output><jupyter_text>Load the depth levels<jupyter_code>contourdepth = pydiva4d.Contourdepth()
contourdepth.read_from(Diva4Dfiles.contourdepth)
contourdepth.depthlist<jupyter_output><empty_output><jupyter_text>Make the plots First we initialise the Basemap by selecting a projection and an area of interest.The domain limits are extracted from the parameter file.<jupyter_code>Param = pydiva2d.Diva2DParameters()
Param.read_from(os.path.join(Diva4Ddirs.diva4dinput, 'param.par'))
m = Basemap(projection='merc', llcrnrlon=Param.xori, llcrnrlat=Param.yori, urcrnrlon=Param.xend,
            urcrnrlat=Param.yend, resolution='i')<jupyter_output><empty_output><jupyter_text>Loop on the contour<jupyter_code>for idepth, depth in enumerate(contourdepth.depthlist):
    
    contourfile = os.path.join(Diva4Ddirs.diva4dparam, 'coast.cont.{0}'.format(str(10001 + idepth)))

    contour2D = pydiva4d.Diva2DContours()
    contour2D.read_from(contourfile)

    fig = plt.figure()
    contour2D.add_to_plot(m, color='k', linewidth=.5)
    plt.title("Contour at depth: {0} m".format(depth))
    m.drawmeridians(np.linspace(Param.xori, Param.xend, 4), labels=[0, 0, 0, 1], linewidth=0.2)
    m.drawparallels(np.linspace(Param.yori, Param.yend, 4), labels=[1, 0, 0, 0], linewidth=0.2)
    # plt.show()
    plt.savefig(os.path.join(figdir, "BlackSea_basemap_contour{0}".format(idepth)))
    plt.close()<jupyter_output>/usr/local/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3260: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
  b = ax.ishold()
/usr/local/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3269: MatplotlibDeprecationWarning: axes.hold is deprecated.
    See the API Changes document (http://matplotlib.org/api/api_changes.html)
    for more details.
  ax.hold(b)<jupyter_text>Loop on the mesh<jupyter_code>for idepth, depth in enumerate(contourdepth.depthlist):
    # Create the file names
    meshtopofile = os.path.join(Diva4Ddirs.diva4dmesh, "mesh.dat.{0}".format(str(10000 + idepth + 1)))
    meshfile = os.path.join(Diva4Ddirs.diva4dmesh, "meshtopo.{0}".format(str(10000 + idepth + 1)))
    
    # Mesh object
    Mesh = pydiva4d.Diva2DMesh()
    Mesh.read_from(meshfile, meshtopofile)
    
    # Make the plot
    fig = plt.figure()
    ax = plt.subplot(111)
    Mesh.add_to_plot(m, linewidth=0.1, color='k')
    m.drawmeridians(np.linspace(Param.xori, Param.xend, 4), labels=[0, 0, 0, 1], linewidth=0.2)
    m.drawparallels(np.linspace(Param.yori, Param.yend, 4), labels=[1, 0, 0, 0], linewidth=0.2)
    plt.title("Mesh at depth: {0} m".format(depth))
    plt.savefig(os.path.join(figdir, "BlackSea_basemap_mesh{0}".format(idepth)))
    plt.close()<jupyter_output>/usr/local/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3260: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
  b = ax.ishold()
/usr/local/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3269: MatplotlibDeprecationWarning: axes.hold is deprecated.
    See the API Changes document (http://matplotlib.org/api/api_changes.html)
    for more details.
  ax.hold(b)<jupyter_text>Loop on the times and depth levels for the results Load year and month periods:<jupyter_code>Monthlist = pydiva4d.Monthlist()
Monthlist.read_from(Diva4Dfiles.monthlist)
Monthlist.monthlist
Yearlist = pydiva4d.Yearlist()
Yearlist.read_from(Diva4Dfiles.yearlist)
Yearlist.yearlist<jupyter_output><empty_output><jupyter_text>Load the variables<jupyter_code>Varlist = pydiva4d.Varlist()
Varlist.read_from(Diva4Dfiles.varlist)
Varlist.varlist<jupyter_output><empty_output><jupyter_text>Loop on levels, variables, periods...<jupyter_code>for variables in Varlist.varlist:
    for yearperiods in Yearlist.yearlist:
        for monthperiods in Monthlist.monthlist:
            for idepth, depthlevels in enumerate(contourdepth.depthlist):
                
                resultfile = "{0}.{1}.{2}.{3}.anl.nc".format(variables, yearperiods,
                                                             monthperiods, str(10000 + idepth + 1))
                
                figname = ''.join((resultfile.replace('.', '_'), '_basemap.png'))
                figtitle = "{0}, Year: {1}, Months: {2}, Depth: {3} m".format(variables, yearperiods,
                                                                                       monthperiods, str(depthlevels))
                
                if os.path.exists(os.path.join(Diva4Ddirs.diva4dfields, resultfile)):
                    Results = pydiva4d.Diva2DResults(os.path.join(Diva4Ddirs.diva4dfields, resultfile))
                    fig = plt.figure()
                    ax = plt.subplot(111)
                    resultplot = Results.add_to_plot(field='analysis', m=m)
                    m.drawmeridians(np.linspace(Param.xori, Param.xend, 4), labels=[0, 0, 0, 1], linewidth=0.2)
                    m.drawparallels(np.linspace(Param.yori, Param.yend, 4), labels=[1, 0, 0, 0], linewidth=0.2)
                    plt.colorbar(resultplot)
                    plt.title(figtitle)
                    plt.savefig(os.path.join(figdir, figname))
                    plt.close()<jupyter_output>/usr/local/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3413: MatplotlibDeprecationWarning: The ishold function was deprecated in version 2.0.
  b = ax.ishold()
/usr/local/lib/python3.6/site-packages/mpl_toolkits/basemap/__init__.py:3422: MatplotlibDeprecationWarning: axes.hold is deprecated.
    See the API Changes document (http://matplotlib.org/api/api_changes.html)
    for more details.
  ax.hold(b)
{'fraction_non_alphanumeric': 0.12006836632958268, 'fraction_numerical': 0.0262070930066942, 'mean_word_length': 3.8899721448467965, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '523349', 'license': 'MIT', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2517, 'n_words': 444, 'path': 'Notebooks/plot_diva4D_results_basemap.ipynb', 'repo_name': 'hhourston/DivaPythonTools', 'size': '7021'}