<commit_before>"""Integration test using a simple dummy model definition."""
# Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
import os
from os.path import join as pjoin
import shutil
import sys
from tempfile import NamedTemporaryFile, TemporaryDirectory
import unittest

from scmtiles.task import TileTask
from scmtiles.runner import TileRunner, CellResult
from scmtiles.test import _get_test_data_path


class SimpleRunner(TileRunner):
    """
    A simple implementation of a `TileRunner` class that doesn't do much
    other than write an input file and an output file.

    """

    def run_cell(self, cell, logger):
        """Run the model for the given cell."""
        # Create a run directory and create links to the template.
        run_directory = self.create_run_directory()
        self.link_template(run_directory)
        # Write the input netcdf file.
        cell_ds = self.get_cell(cell)
        cell_ds.to_netcdf(pjoin(run_directory, 'input.nc'))
        # Simulate a model doing somthing by writing an output text file
        # consisting of the xarray representation of the input dataset and
        # the single required input file.
        output_file = pjoin(run_directory, 'output.txt')
        with open(output_file, 'w') as fh:
            fh.write('{!s}\n'.format(cell_ds))
            with open(pjoin(run_directory, 'required.txt'), 'r') as rh:
                fh.write(rh.read())
        # Archive the run output to the output direcrtory.
        outputs = self.archive_output(cell, run_directory)
        return CellResult(cell, outputs)

    def archive_output(self, cell, run_directory):
        """Archive the model output."""
        # Construct an output file path including the run start time and
        # the cell's location in the full grid.
        time_portion = self.config.start_time.strftime('%Y%m')
        grid_portion = 'y{:02d}x{:02d}'.format(cell.y_global, cell.x_global)
        archive_file_name = 'output.{!s}.{!s}.txt'.format(time_portion,
                                                          grid_portion)
        archive_file_path = pjoin(self.config.output_directory,
                                  archive_file_name)
        # Move the model output file to the output directory.
        shutil.move(pjoin(run_directory, 'output.txt'), archive_file_path)
        return [archive_file_path]


#: A string template for an SCM Tiles configuration file, to be filled
#: in using string formatting.
_CONFIG_TEMPLATE = """
[default]

start_time = 2016-01-01T00:00:00
forcing_step_seconds = 1
forcing_num_steps = 0

xname = lon
yname = lat
xsize = 28
ysize = 6

input_directory = {input_directory:s}
template_directory = {template_directory:s}
work_directory = {work_directory:s}
output_directory = {output_directory:s}
input_file_pattern = grid.{{time.year:04d}}{{time.month:02d}}.nc
"""
<commit_msg>Correct forcing steps in integration tests<commit_after>"""Integration test using a simple dummy model definition."""
# Copyright 2016 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
import os
from os.path import join as pjoin
import shutil
import sys
from tempfile import NamedTemporaryFile, TemporaryDirectory
import unittest

from scmtiles.task import TileTask
from scmtiles.runner import TileRunner, CellResult
from scmtiles.test import _get_test_data_path


class SimpleRunner(TileRunner):
    """
    A simple implementation of a `TileRunner` class that doesn't do much
    other than write an input file and an output file.

    """

    def run_cell(self, cell, logger):
        """Run the model for the given cell."""
        # Create a run directory and create links to the template.
        run_directory = self.create_run_directory()
        self.link_template(run_directory)
        # Write the input netcdf file.
        cell_ds = self.get_cell(cell)
        cell_ds.to_netcdf(pjoin(run_directory, 'input.nc'))
        # Simulate a model doing somthing by writing an output text file
        # consisting of the xarray representation of the input dataset and
        # the single required input file.
        output_file = pjoin(run_directory, 'output.txt')
        with open(output_file, 'w') as fh:
            fh.write('{!s}\n'.format(cell_ds))
            with open(pjoin(run_directory, 'required.txt'), 'r') as rh:
                fh.write(rh.read())
        # Archive the run output to the output direcrtory.
        outputs = self.archive_output(cell, run_directory)
        return CellResult(cell, outputs)

    def archive_output(self, cell, run_directory):
        """Archive the model output."""
        # Construct an output file path including the run start time and
        # the cell's location in the full grid.
        time_portion = self.config.start_time.strftime('%Y%m')
        grid_portion = 'y{:02d}x{:02d}'.format(cell.y_global, cell.x_global)
        archive_file_name = 'output.{!s}.{!s}.txt'.format(time_portion,
                                                          grid_portion)
        archive_file_path = pjoin(self.config.output_directory,
                                  archive_file_name)
        # Move the model output file to the output directory.
        shutil.move(pjoin(run_directory, 'output.txt'), archive_file_path)
        return [archive_file_path]


#: A string template for an SCM Tiles configuration file, to be filled
#: in using string formatting.
_CONFIG_TEMPLATE = """
[default]

start_time = 2016-01-01T00:00:00
forcing_step_seconds = 1
forcing_num_steps = 1

xname = lon
yname = lat
xsize = 28
ysize = 6

input_directory = {input_directory:s}
template_directory = {template_directory:s}
work_directory = {work_directory:s}
output_directory = {output_directory:s}
input_file_pattern = grid.{{time.year:04d}}{{time.month:02d}}.nc
"""

{'fraction_non_alphanumeric': 0.08519211397323356, 'fraction_numerical': 0.010073391854943157, 'mean_word_length': 3.263803680981595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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}}
{'dir': 'git-commits-cleaned', 'id': '5513253', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1933, 'n_words': 675}