<commit_before>#! /usr/local/bin/python
""" Basic Model Interface implementation for River Module"""

import numpy as np

from bmi import Bmi
from avulsion_main import RiverModule


class BmiRiverModule(Bmi):

    _name = 'Avulsion Module'
    _input_var_names = ()
    # not sure what's the most appropriate CSDMS river mouth stuff?
    _output_var_names = ('channel_centerline__x_coordinate',
                         'channel_centerline__y_coordinate',
                         'channel_water_sediment~bedload__mass_flow_rate')
    def __init__(self):
        """Create a BmiRiver module that is ready for initialization."""
        self._model = None
        self._values = {}
        self._var_units = {}

    def initialize(self, filename):
        """Initialize the River module"""
        self._model = RiverModule.from_path(filename)

        self._values = {
            'channel_centerline__x_coordinate': 'river_x_coordinates',
            'channel_centerline__y_coordinate': 'river_y_coordinates',
            'channel_water_sediment~bedload__volume_flow_rate': 'sediment_flux',
        }

        self._var_units = {
            'channel_water_sediment~bedload__volume_flow_rate': "kg s^-1"
        }

    def update(self):
        """Advance model by one time step."""
        self._model.advance_in_time()
        self.river_mouth_location = (self._model.river_x_coordinates[-1],
    							     self._model.river_y_coordinates[-1])

    def update_frac(self, time_frac):
        """Update model by a fraction of a time step."""
        time_step = self.get_time_step()
        self._model.time_step = time_frac * time_step
        self.update()
        self._model.time_step = time_step

    def update_until(self, then):
        """Update model until a particular time."""
        n_steps = (then - self.get_current_time()) / self.get_time_step()

        for _ in xrange(int(n_steps)):
            self.update()
        self.update_frac(n_steps - int(n_steps))

    def finalize(self):
        """Clean up & save avulsion file"""
        
        if self._model.savefiles == 1:
            np.savetxt('avulsions', self._model.avulsions,
                       fmt='%i %i %i %.3f %.3f %.3f')
        pass

    def get_var_type(self, var_name):
        """Data type of variable."""
        return str(self.get_value_ref(var_name).dtype)

    def get_var_units(self, var_name):
        """Get units of variable."""
        return self._var_units[var_name]

    def get_var_nbytes(self, var_name):
        """Get units of variable."""
        return self.get_value_ref(var_name).nbytes

    def get_value_ref(self, var_name):
        """Reference to values."""
        return np.array(getattr(self._model, self._values[var_name]))

    def get_value(self, var_name):
        """Copy of values."""
        return np.array(getattr(self._model, self._values[var_name]))

    def get_component_name(self):<commit_msg>Use lambdas to get values from avulsion model.<commit_after>#! /usr/local/bin/python
""" Basic Model Interface implementation for River Module"""

import numpy as np

from bmi import Bmi
from avulsion_main import RiverModule


class BmiRiverModule(Bmi):

    _name = 'Avulsion Module'
    _input_var_names = ()
    _output_var_names = ('channel_centerline__x_coordinate',
                         'channel_centerline__y_coordinate',
                         'channel_water_sediment~bedload__mass_flow_rate',
                         'channel_exit__x_coordinate',
                         'channel_exit__y_coordinate',
                        )
    def __init__(self):
        """Create a BmiRiver module that is ready for initialization."""
        self._model = None
        self._values = {}
        self._var_units = {}

    def initialize(self, filename):
        """Initialize the River module"""
        self._model = RiverModule.from_path(filename)

        self._values = {
            'channel_centerline__x_coordinate': lambda: np.array(self._model.river_x_coordinates),
            'channel_centerline__y_coordinate': lambda: np.array(self._model.river_y_coordinates),
            'channel_water_sediment~bedload__volume_flow_rate': lambda: np.array(self._model.sediment_flux),
            'channel_exit__x_coordinate': lambda: np.array([self._model.river_x_coordinates[-1]]),
            'channel_exit__y_coordinate': lambda: np.array([self._model.river_y_coordinates[-1]]),
        }

        self._var_units = {
            'channel_water_sediment~bedload__volume_flow_rate': "kg s^-1"
        }

    def update(self):
        """Advance model by one time step."""
        self._model.advance_in_time()
        self.river_mouth_location = (self._model.river_x_coordinates[-1],
                                     self._model.river_y_coordinates[-1])

    def update_frac(self, time_frac):
        """Update model by a fraction of a time step."""
        time_step = self.get_time_step()
        self._model.time_step = time_frac * time_step
        self.update()
        self._model.time_step = time_step

    def update_until(self, then):
        """Update model until a particular time."""
        n_steps = (then - self.get_current_time()) / self.get_time_step()

        for _ in xrange(int(n_steps)):
            self.update()
        self.update_frac(n_steps - int(n_steps))

    def finalize(self):
        """Clean up & save avulsion file"""
        
        if self._model.savefiles == 1:
            np.savetxt('avulsions', self._model.avulsions,
                       fmt='%i %i %i %.3f %.3f %.3f')
        pass

    def get_var_type(self, var_name):
        """Data type of variable."""
        return str(self.get_value(var_name).dtype)

    def get_var_units(self, var_name):
        """Get units of variable."""
        return self._var_units[var_name]

    def get_var_nbytes(self, var_name):
        """Get units of variable."""
        return self.get_value(var_name).nbytes

    def get_value(self, var_name):
        """Copy of values."""
        return self._values[var_name]()

    def get_component_name(self):
{'fraction_non_alphanumeric': 0.1107454336021063, 'fraction_numerical': 0.0026328780648346224, 'mean_word_length': 2.495112133410006, '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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6293857', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1776, 'n_words': 383}