easyblock = 'Binary'

name = 'ESMValTool'
version = '2.0.0b1'
versionsuffix = '-Python-%(pyver)s'

homepage = 'https://www.esmvaltool.org/'
description = """The Earth System Model eValuation Tool (ESMValTool)
 is a community diagnostics and performance metrics tool
 for the evaluation of Earth System Models (ESMs) that
 allows for routine comparison of single or multiple models,
 either against predecessor versions or against observations."""

toolchain = {'name': 'foss', 'version': '2018b'}
toolchainopts = {'pic': True}

source_urls = ['https://github.com/ESMValGroup/ESMValTool/archive/']
sources = [{'filename': SOURCE_TAR_GZ, 'download_filename': 'v%(version)s.tar.gz'}]

dependencies = [
    ('Python', '3.6.6'),
    ('ELIC', '0.1', versionsuffix), 
]

exts_defaultclass = 'PythonPackage'
exts_default_options = {
    'source_tmpl': '%(name)s-%(version)s.tar.gz',
}

exts_list = [
    # Python deps, order is important!
    ('xarray', '0.13.0', {
        'source_urls': ['https://pypi.python.org/packages/source/x/xarray/'],
    }),
    ('eofs', '1.4.0', {
        'source_urls': ['https://pypi.python.org/packages/source/e/eofs/'],
    }),
    ('PyKE', '1.1.1', {
        'modulename': 'pyke',
        'source_tmpl': 'pyke3-%(version)s.zip',
        'source_urls': ['https://sourceforge.net/projects/%(name)s/files/%(name)s/%(version)s'],
        'use_pip': True,
    }),
    ('scitools-iris', '2.2.1', {
        'modulename': 'iris',
        'source_tmpl': 'v%(version)s.tar.gz',
        'source_urls': ['https://github.com/SciTools/iris/archive/'],
    }),
    ('iris-grib', '0.14.1', {
        'modulename': 'iris_grib',
        'source_tmpl': 'v%(version)s.tar.gz',
        'source_urls': ['https://github.com/SciTools/iris-grib/archive/'],
    }),
    ('ESMValCore', '2.0.0b2', {
        'modulename': 'esmvalcore',
        'source_tmpl': 'v%(version)s.tar.gz',
        'source_urls': ['https://github.com/ESMValGroup/ESMValCore/archive/'],
    }),
]

skipsteps = ['sanitycheck']

modextrapaths = {
    'PYTHONPATH': 'lib/python%(pyshortver)s/site-packages',
}

postinstallcmds = [
    "cd %(installdir)s && " +
    "cp ../../../sources/e/ESMValTool/extensions/v2.0.0b2.tar.gz . && " +
    "cp ../../../sources/e/ESMF/esmf_7_1_0r_src.tar.gz . && " +
    "tar xzf v2.0.0b2.tar.gz && " +
    "tar xzf ESMValTool-%(version)s.tar.gz && " +
    "tar xzf esmf_7_1_0r_src.tar.gz && " +
    "cd ESMValTool-%(version)s && " +
    'PYTHONPATH="%(installdir)s/lib/python%(pyshortver)s/site-packages":"${PYTHONPATH}" && python setup.py install --prefix=%(installdir)s && ' +
    "cd esmvaltool  && " +
    "cp -r cmorizers diag_scripts interface_scripts recipes utils %(installdir)s/lib/python%(pyshortver)s/site-packages/ESMValTool-%(version)s-py3.6.egg/esmvaltool && " +
    "cp config-references.yml %(installdir)s/lib/python%(pyshortver)s/site-packages/ESMValTool-%(version)s-py3.6.egg/esmvaltool && " +
    "cd %(installdir)s/ESMValCore-2.0.0b2/esmvalcore && " +
    "cp -r cmor utils preprocessor %(installdir)s/lib/python3.6/site-packages/ESMValCore-2.0.0b1-py3.6.egg/esmvalcore && " +
    "cd %(installdir)s/esmf/src/addon/ESMPy && " +
    "python setup.py build --ESMFMKFILE=%(installdir)s/../../ESMF/7.1.0r-foss-2018b/lib/esmf.mk && " +
    "python setup.py install --prefix=%(installdir)s && " +
    "cd %(installdir)s && " +
    'pip install xesmf xgcm --install-option "--prefix=%(installdir)s" && ' +
    "rm -rf ESMValTool-%(version)s ESMValCore-2.0.0b2 v2.0.0b2.tar.gz esmf esmf_7_1_0r_src.tar.gz"
]

#modloadmsg = (
#    "Now call 'esmvaltool -h'\n\n"
#    "To install ESMValTool in your own directory execute: \n"
#    "tar zxf $EBROOTESMVALTOOL/ESMValTool-%(version)s.tar.gz\n"
#)

moduleclass = 'geo'
