<commit_before>import pytest
import mock
from climt import Prognostic, Leapfrog, AdamsBashforth
from datetime import timedelta
import numpy as np


class MockPrognostic(Prognostic):

    def ensure_state_is_valid_input(self, state):
        return

    def __call__(self):
        return {}, {}

class TimesteppingBase(object):

    timestepper_class = None

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_no_change_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 0.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_no_change_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 0.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 1.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 274.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 1.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 274.}
        state = new_state
        new_state = time_stepper.step(new_state, timestep)
        assert state == {'air_temperature': 274.}
        assert new_state == {'air_temperature': 275.}
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 275.}
        assert new_state == {'air_temperature': 276.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_no_change_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.zeros((3, 3))}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_no_change_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.ones((3, 3))*0.}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.ones((3, 3))*1.}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*274.).all()

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.ones((3, 3))*1.}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*274.).all()
        state = new_state
        new_state = time_stepper.step(new_state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*274.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*275.).all()
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*275.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*276.).all()


class TestLeapfrog(TimesteppingBase):
    timestepper_class = Leapfrog
<commit_msg>Added DataArray tests to ensure units preserved<commit_after>import pytest
import mock
from climt import Prognostic, Leapfrog, AdamsBashforth, DataArray
from datetime import timedelta
import numpy as np


class MockPrognostic(Prognostic):

    def ensure_state_is_valid_input(self, state):
        return

    def __call__(self):
        return {}, {}

class TimesteppingBase(object):

    timestepper_class = None

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_no_change_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 0.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_no_change_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 0.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 273.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 1.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 274.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_float_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = ({'air_temperature': 1.}, {})
        state = {'air_temperature': 273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 273.}
        assert new_state == {'air_temperature': 274.}
        state = new_state
        new_state = time_stepper.step(new_state, timestep)
        assert state == {'air_temperature': 274.}
        assert new_state == {'air_temperature': 275.}
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert state == {'air_temperature': 275.}
        assert new_state == {'air_temperature': 276.}

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_no_change_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.zeros((3, 3))}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_no_change_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.ones((3, 3))*0.}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.ones((3, 3))*1.}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*274.).all()

    @mock.patch.object(MockPrognostic, '__call__')
    def test_array_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature': np.ones((3, 3))*1.}, {})
        state = {'air_temperature': np.ones((3, 3))*273.}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*274.).all()
        state = new_state
        new_state = time_stepper.step(new_state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*274.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*275.).all()
        state = new_state
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*275.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*276.).all()

    @mock.patch.object(MockPrognostic, '__call__')
    def test_dataarray_no_change_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature':
                 DataArray(np.zeros((3, 3)), attrs={'units': 'K/s'})},
            {})
        state = {'air_temperature': DataArray(np.ones((3, 3))*273.,
                                              attrs={'units': 'K'})}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'].values == np.ones((3, 3))*273.).all()
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'].values == np.ones((3, 3))*273.).all()
        assert len(new_state['air_temperature'].attrs) == 1
        assert new_state['air_temperature'].attrs['units'] == 'K'

    @mock.patch.object(MockPrognostic, '__call__')
    def test_dataarray_no_change_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature':
                DataArray(np.zeros((3, 3)), attrs={'units': 'K/s'})},
            {})
        state = {'air_temperature': DataArray(np.ones((3, 3))*273.,
                                              attrs={'units': 'K'})}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()
        state = new_state
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()
        state = new_state
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert len(new_state['air_temperature'].attrs) == 1
        assert new_state['air_temperature'].attrs['units'] == 'K'

    @mock.patch.object(MockPrognostic, '__call__')
    def test_dataarray_one_step(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature':
                DataArray(np.ones((3, 3)), attrs={'units': 'K/s'})},
            {})
        state = {'air_temperature': DataArray(np.ones((3, 3))*273.,
                                              attrs={'units': 'K'})}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*274.).all()
        assert len(new_state['air_temperature'].attrs) == 1
        assert new_state['air_temperature'].attrs['units'] == 'K'

    @mock.patch.object(MockPrognostic, '__call__')
    def test_dataarray_three_steps(self, mock_prognostic_call):
        mock_prognostic_call.return_value = (
            {'air_temperature':
                DataArray(np.ones((3, 3)), attrs={'units': 'K/s'})},
            {})
        state = {'air_temperature': DataArray(np.ones((3, 3))*273.,
                                              attrs={'units': 'K'})}
        timestep = timedelta(seconds=1.)
        time_stepper = self.timestepper_class([MockPrognostic()])
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*273.).all()
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*274.).all()
        state = new_state
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        new_state = time_stepper.step(new_state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*274.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*275.).all()
        state = new_state
        assert len(state['air_temperature'].attrs) == 1
        assert state['air_temperature'].attrs['units'] == 'K'
        new_state = time_stepper.step(state, timestep)
        assert list(state.keys()) == ['air_temperature']
        assert (state['air_temperature'] == np.ones((3, 3))*275.).all()
        assert list(new_state.keys()) == ['air_temperature']
        assert (new_state['air_temperature'] == np.ones((3, 3))*276.).all()
        assert len(new_state['air_temperature'].attrs) == 1
        assert new_state['air_temperature'].attrs['units'] == 'K'


class TestLeapfrog(TimesteppingBase):
    timestepper_class = Leapfrog

{'fraction_non_alphanumeric': 0.16081093913208622, 'fraction_numerical': 0.023264647231981767, 'mean_word_length': 3.2680851063829786, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5256562', 'n_tokens_mistral': 7208, 'n_tokens_neox': 6508, 'n_words': 1113}