<commit_before>import pytest

import fair
import os
import numpy as np
from fair.RCPs import rcp3pd, rcp45, rcp6, rcp85
from fair.tools import magicc
from fair.ancil import natural
from fair.constants import molwt
from fair.forcing.ghg import myhre

def test_no_arguments():
    with pytest.raises(ValueError):
        fair.forward.fair_scm()


def test_zero_emissions():
    nt = 250
    emissions = np.zeros(nt)
    C,F,T = fair.forward.fair_scm(
        emissions=emissions, other_rf=0., useMultigas=False)
    assert np.allclose(C,np.ones(nt)*278.)
    assert np.allclose(F,np.zeros(nt))
    assert np.allclose(T,np.zeros(nt))


# Rather than change the testing values, I am going to check the old results
# can still be recreated with non-default options.


def test_ten_GtC_pulse():
    emissions = np.zeros(250)
    emissions[125:] = 10.0
    other_rf = np.zeros(emissions.size)
    for x in range(0,emissions.size):
        other_rf[x] = 0.5*np.sin(2*np.pi*(x)/14.0)

    C,F,T = fair.forward.fair_scm(
        emissions=emissions, other_rf=other_rf, useMultigas=False,
        r0=32.4, tcr_dbl=70)

    datadir = os.path.join(os.path.dirname(__file__), 'ten_GtC_pulse/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_multigas_fullemissions_error():
    with pytest.raises(ValueError):
        fair.forward.fair_scm(emissions=rcp3pd.Emissions.emissions,
            useMultigas=False)


# There must be a good way to avoid duplication here
def test_rcp3pd():
    C,F,T = fair.forward.fair_scm(emissions=rcp3pd.Emissions.emissions)
    datadir = os.path.join(os.path.dirname(__file__), 'rcp3pd/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_rcp45():
    C,F,T = fair.forward.fair_scm(emissions=rcp45.Emissions.emissions)
    datadir = os.path.join(os.path.dirname(__file__), 'rcp45/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_rcp6():
    C,F,T = fair.forward.fair_scm(emissions=rcp6.Emissions.emissions)
    datadir = os.path.join(os.path.dirname(__file__), 'rcp6/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_rcp85():
    C,F,T = fair.forward.fair_scm(emissions=rcp85.Emissions.emissions)
    datadir = os.path.join(os.path.dirname(__file__), 'rcp85/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_division():
    # Ensure parameters given as integers are treated as floats when dividing
    # (Python2 compatibility).
    _, _, T = fair.forward.fair_scm(
        emissions=fair.RCPs.rcp6.Emissions.emissions,
        useMultigas=True,
        d=np.array([239.0, 4.0]),<commit_msg>Update tests for new contrail forcing options<commit_after>import pytest

import fair
import os
import numpy as np
from fair.RCPs import rcp3pd, rcp45, rcp6, rcp85
from fair.tools import magicc
from fair.ancil import natural, cmip5_annex2_forcing
from fair.constants import molwt
from fair.forcing.ghg import myhre


def test_contrails_nox():
    C,F,T = fair.forward.fair_scm(emissions=rcp45.Emissions.emissions,
        contrail_forcing='NOx')


def test_contrails_fuel():
    kerosene_supply = np.array([0.]*175 + [8.9534883721,
        9.6511627907, 10.4651162791, 11.2790697674, 12.2093023256,
        13.1395348837, 14.3023255814, 15.3488372093, 16.6279069767,
        17.9069767442, 19.4186046512, 20.9302325581, 22.5581395349,
        24.4186046512, 26.3953488372, 28.488372093, 30.8139534884,
        33.2558139535, 35.9302325581, 38.7209302326, 41.8604651163,
        45.9302325581, 50.5813953488, 53.3720930233, 55.8139534884,
        59.6511627907, 64.6511627907, 76.2790697674, 86.3953488372,
        90.4651162791, 90.6976744186, 104.6511627907, 111.6279069767,
        115.5813953488, 111.6279069767, 111.7441860465, 112.0930232558,
        118.7209302326, 122.9069767442, 128.023255814, 128.9534883721,
        127.0930232558, 128.488372093, 130.2325581395, 138.9534883721,
        143.488372093, 151.0465116279, 157.6744186047, 164.4186046512,
        170.3488372093, 170.8139534884, 166.7441860465, 165.1162790698,
        167.5581395349, 174.4186046512, 179.4186046512, 197.349, 204.095,
        205.029, 211.022, 218.996, 213.021, 214.353, 215.012, 228.23, 236.063,
        238.329, 244.104, 247.726, 239.072, 246.544, 254.792])
    C,F,T = fair.forward.fair_scm(emissions=rcp45.Emissions.emissions[:247,:],
        contrail_forcing='fuel', kerosene_supply=kerosene_supply, natural=0.,
        F_volcanic=0., F_solar=0.)
    F_expected = np.array([
        0.        , 0.00169919, 0.0018316 , 0.00198607, 0.00214054, 0.00231708,
        0.00249362, 0.00271429, 0.0029129 , 0.00315564, 0.00339838, 0.00368526,
        0.00397214, 0.00428108, 0.00463416, 0.00500931, 0.00540652, 0.00584787,
        0.00631128, 0.00681883, 0.00734845, 0.00794427, 0.00871663, 0.00959933,
        0.01012895, 0.01059236, 0.01132059, 0.01226949, 0.01447623, 0.0163961 ,
        0.01716846, 0.01721259, 0.01986068, 0.02118473, 0.02193502, 0.02118473,
        0.02120679, 0.021273  , 0.02253084, 0.02332527, 0.02429623, 0.02447277,
        0.02411969, 0.0243845 , 0.02471551, 0.02637057, 0.0272312 , 0.02866558,
        0.02992343, 0.03120334, 0.03232878, 0.03241705, 0.03164469, 0.03133574,
        0.03179916, 0.03310114, 0.03405004, 0.03745286, 0.03873312, 0.03891037,
        0.04004772, 0.04156103, 0.04042709, 0.04067988, 0.04080494, 0.04331345,
        0.0448    , 0.04523004, 0.04632602, 0.0470134 , 0.04537105, 0.04678908,
        0.04835439])
    assert np.allclose(F[174:,7], F_expected)

def test_contrails_ext():
    C,F,T = fair.forward.fair_scm(emissions=rcp45.Emissions.emissions,
        contrail_forcing='ext', F_contrails=0.2, scale=np.ones(13)*1.5)
    assert np.allclose(F[:,7],np.ones(736)*0.3)


def test_contrails_invalid():
    with pytest.raises(ValueError):
        fair.forward.fair_scm(emissions=rcp45.Emissions.emissions,
            contrail_forcing='other')


def test_no_arguments():
    with pytest.raises(ValueError):
        fair.forward.fair_scm()


def test_zero_emissions():
    nt = 250
    emissions = np.zeros(nt)
    C,F,T = fair.forward.fair_scm(
        emissions=emissions, other_rf=0., useMultigas=False)
    assert np.allclose(C,np.ones(nt)*278.)
    assert np.allclose(F,np.zeros(nt))
    assert np.allclose(T,np.zeros(nt))


# Rather than change the testing values, I am going to check the old results
# can still be recreated with non-default options.

# This means that all multi-gas scenarios will have aerosol scalings that
# differ from the defaults.


def test_ten_GtC_pulse():
    emissions = np.zeros(250)
    emissions[125:] = 10.0
    other_rf = np.zeros(emissions.size)
    for x in range(0,emissions.size):
        other_rf[x] = 0.5*np.sin(2*np.pi*(x)/14.0)

    C,F,T = fair.forward.fair_scm(
        emissions=emissions, other_rf=other_rf, useMultigas=False,
        r0=32.4, tcr_dbl=70)

    datadir = os.path.join(os.path.dirname(__file__), 'ten_GtC_pulse/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_multigas_fullemissions_error():
    with pytest.raises(ValueError):
        fair.forward.fair_scm(emissions=rcp3pd.Emissions.emissions,
            useMultigas=False)


# There must be a good way to avoid duplication here
def test_rcp3pd():
    C,F,T = fair.forward.fair_scm(
        emissions=rcp3pd.Emissions.emissions,
        b_aero = np.array([-35.29e-4*1.3741*molwt.SO2/molwt.S, 0.0, -5.034e-4*1.3741, -5.763e-4*1.3741*molwt.NO/molwt.N, 453e-4*1.3741,-37.83e-4*1.3741, -10.35e-4*1.3741])
    )
    datadir = os.path.join(os.path.dirname(__file__), 'rcp3pd/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_rcp45():
    C,F,T = fair.forward.fair_scm(
        emissions=rcp45.Emissions.emissions,
        b_aero = np.array([-35.29e-4*1.3741*molwt.SO2/molwt.S, 0.0, -5.034e-4*1.3741, -5.763e-4*1.3741*molwt.NO/molwt.N, 453e-4*1.3741,-37.83e-4*1.3741, -10.35e-4*1.3741])
    )
    datadir = os.path.join(os.path.dirname(__file__), 'rcp45/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_rcp6():
    C,F,T = fair.forward.fair_scm(
        emissions=rcp6.Emissions.emissions,
        b_aero = np.array([-35.29e-4*1.3741*molwt.SO2/molwt.S, 0.0, -5.034e-4*1.3741, -5.763e-4*1.3741*molwt.NO/molwt.N, 453e-4*1.3741,-37.83e-4*1.3741, -10.35e-4*1.3741])
    )
    datadir = os.path.join(os.path.dirname(__file__), 'rcp6/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_rcp85():
    C,F,T = fair.forward.fair_scm(
        emissions=rcp85.Emissions.emissions,
        b_aero = np.array([-35.29e-4*1.3741*molwt.SO2/molwt.S, 0.0, -5.034e-4*1.3741, -5.763e-4*1.3741*molwt.NO/molwt.N, 453e-4*1.3741,-37.83e-4*1.3741, -10.35e-4*1.3741])
    )
    datadir = os.path.join(os.path.dirname(__file__), 'rcp85/')
    C_expected = np.load(datadir + 'C.npy')
    F_expected = np.load(datadir + 'F.npy')
    T_expected = np.load(datadir + 'T.npy')

    assert np.allclose(C, C_expected)
    assert np.allclose(F, F_expected)
    assert np.allclose(T, T_expected)


def test_division():
    # Ensure parameters given as integers are treated as floats when dividing
    # (Python2 compatibility).
    _, _, T = fair.forward.fair_scm(
        emissions=fair.RCPs.rcp6.Emissions.emissions,
        useMultigas=True,
        d=np.array([239.0, 4.0]),
{'fraction_non_alphanumeric': 0.14275322785961084, 'fraction_numerical': 0.16648481542098564, 'mean_word_length': 3.3543151227236736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6467756', 'n_tokens_mistral': 5755, 'n_tokens_neox': 4600, 'n_words': 744}