Example #1
0
def test_rewind():
    """ test rewind resets data """
    o_put = Outputter(output_timestep=timedelta(minutes=30))
    o_put.rewind()

    assert o_put._model_start_time is None
    assert o_put._dt_since_lastoutput is None
    assert o_put._write_step
Example #2
0
def test_rewind():
    """ test rewind resets data """
    o_put = Outputter(output_timestep=timedelta(minutes=30))
    o_put.rewind()

    assert o_put._model_start_time is None
    assert o_put._dt_since_lastoutput is None
    assert o_put._write_step
Example #3
0
def test_output_timestep(model, model_ts, output_ts):
    """
    test the partial functionality implemented in base class
    For different output_timestep values as compared to model_timestep,
    this test ensures the internal _write_step flag is set correcting when
    outputting data. Since both the Renderer and NetCDFOutput call base method,
    using super - testing that _write_step toggles correctly should be
    sufficient
    """
    o_put = Outputter(model._cache, output_timestep=output_ts[0])
    model.duration = model_ts * output_ts[1]
    model.time_step = model_ts

    model.outputters += o_put
    factor = output_ts[0].total_seconds() / model_ts.total_seconds()

    # output timestep aligns w/ model timestep after these many steps
    match_after = output_ts[1]

    # rewind and make sure outputter resets values
    model.rewind()
    assert o_put._model_start_time is None
    assert o_put._dt_since_lastoutput is None
    assert o_put._write_step
    print("\n\nmodel_ts: {0}, output_ts: {1}").format(model_ts.seconds,
                                                      output_ts[0].seconds)
    print("num outputs: {0}, for model steps: {1}\n").format(
        output_ts[2], match_after)
    while True:
        try:
            model.step()

            # write_output checks to see if it is zero or last time step and if
            # flags to output these steps are set. It changes _write_step
            # accordingly
            if model.current_time_step == 0:
                assert (o_put._write_step
                        if o_put.output_zero_step else not o_put._write_step)
            elif model.current_time_step == model.num_time_steps - 1:
                assert (o_put._write_step
                        if o_put.output_last_step else not o_put._write_step)
            else:
                # The check for writing output is as follows:
                #     frac_step = current_time_step % factor < 1.0
                #
                # In words,
                #  if frac_step == 0.0,
                #    output is written and
                #    dt_since_lastoutput == output_timestep
                #    so no fractional time and dt_since_lastoutput is resets 0
                #  if frac_step < 1.0,
                #    output is written and there is a fraction time
                #    dt_since_lastoutput > output_timestep at end of step
                #    so reset dt_since_lastoutput to remainder
                #  if frac_step >= 1.0,
                #    no output is written in this time step since not enough
                #    time has elapsed since last output was written, so
                #    dt_since_lastoutput < output_timestep
                #    keep accumulating dt_since_lastoutput - no reset
                #
                # NOTE: mod doesn't work correctly on floating points so
                # multiply by ten, operate on integers, divide by 10.0 and
                # round so check doesn't fail because of rounding issue
                frac_step = round(
                    (model.current_time_step * 10 % int(factor * 10)) / 10.0,
                    6)
                if frac_step < 1.0:
                    assert o_put._write_step
                else:
                    # it is greater than 1 so no output yet
                    assert not o_put._write_step

                if factor < 1.0:
                    # output every time step
                    assert o_put._dt_since_lastoutput == 0.0
                else:
                    remainder = frac_step * model.time_step
                    assert (o_put._dt_since_lastoutput == remainder)

                # check frac_step == 0 every match_after steps
                if model.current_time_step % match_after == 0:
                    assert frac_step == 0.0

                print("step_num, _write_step, _dt_since_lastoutput:\t"
                      "{0}, {1}, {2}").format(model.current_time_step,
                                              o_put._write_step,
                                              o_put._dt_since_lastoutput)

        except StopIteration:
            break
def test_output_timestep(model, output_ts, model_ts):
    """
    test the partial functionality implemented in base class
    For different output_timestep values as compared to model_timestep,
    this test ensures the internal _write_step flag is set correcting when
    outputting data. Since both the Renderer and NetCDFOutput call base method,
    using super - testing that _write_step toggles correctly should be
    sufficient
    """
    o_put = Outputter(model._cache, output_timestep=output_ts)
    model.duration = timedelta(hours=3)
    model.time_step = model_ts

    model.outputters += o_put
    factor = math.ceil(float(output_ts.seconds) / model_ts.seconds)

    # rewind and make sure outputter resets values
    model.rewind()
    assert o_put._model_start_time is None
    assert o_put._next_output_time is None
    assert not o_put._write_step

    # call each part of model.step separately so we can validate
    # how the outputter is setting its values, especially _write_step
    for step_num in range(-1, model.num_time_steps - 1):
        if step_num == -1:
            model.setup_model_run()

            assert o_put._model_start_time == model.start_time
            assert o_put._next_output_time == (model.model_time +
                                               o_put.output_timestep)
            assert not o_put._write_step
        else:
            model.setup_time_step()

            if o_put.output_timestep <= timedelta(seconds=model.time_step):
                assert o_put._write_step    # write every step
            else:
                if (step_num + 1) % factor == 0:
                    assert o_put._write_step

            # No need to call following since outputter doesn't use them, but
            # leave for completeness.
            model.move_elements()
            model.step_is_done()

        model.current_time_step += 1

        # no need to release elements or save to cache since we're not
        # interested in the actual data - just want to see if the step_num
        # should be written out or not
        #model._cache.save_timestep(model.current_time_step, model.spills)
        model.write_output()

        # each outputter, like the Renderer or NetCDFOutputter will update the
        # output timestep after completing the write_output method. Base class
        # not designed to be included as an outputter for the Model, as it only
        # implements partial functionality; need to manually update the output
        # timestep below.
        if o_put._write_step:
            # no update happens for last time step
            o_put._update_next_output_time(model.current_time_step,
                                           model.model_time)
            if model.current_time_step < model.num_time_steps - 1:
                assert o_put._next_output_time == (model.model_time +
                                                   o_put.output_timestep)

        # write_output checks to see if it is zero or last time step and if
        # flags to output these steps are set. It changes _write_step
        # accordingly
        if model.current_time_step == 0:
            assert (o_put._write_step if o_put.output_zero_step
                    else not o_put._write_step)
        elif model.current_time_step == model.num_time_steps - 1:
            assert (o_put._write_step if o_put.output_last_step
                    else not o_put._write_step)

        print 'Completed step: {0}'.format(model.current_time_step)
Example #5
0
    def __init__(
        self,
        filename=None,
        images_dir='./',
        image_size=(800, 600),
        cache=None,
        output_timestep=None,
        output_zero_step=True,
        output_last_step=True,
        draw_ontop='forecast',
        **kwargs
        ):
        """
        Init the image renderer.

        Following args are passed to base class Outputter's init:

        :param cache: sets the cache object from which to read data. The model
            will automatically set this param

        :param output_timestep: default is None in which case everytime the
            write_output is called, output is written. If set, then output is
            written every output_timestep starting from model_start_time.
        :type output_timestep: timedelta object

        :param output_zero_step: default is True. If True then output for
            initial step (showing initial release conditions) is written
            regardless of output_timestep
        :type output_zero_step: boolean

        :param output_last_step: default is True. If True then output for
            final step is written regardless of output_timestep
        :type output_last_step: boolean

        :param draw_ontop: draw 'forecast' or 'uncertain' LEs on top. Default
            is to draw 'forecast' LEs, which are in black on top
        :type draw_ontop: str

        Remaining kwargs are passed onto baseclass's __init__ with a direct
        call: MapCanvas.__init__(..)

        Optional parameters (kwargs)

        :param projection_class: gnome.utilities.projections class to use.
            Default is gnome.utilities.projections.FlatEarthProjection
        :param map_BB:  map bounding box. Default is to use
            land_polygons.bounding_box. If land_polygons is None, then this is
            the whole world, defined by ((-180,-90),(180, 90))
        :param viewport: viewport of map -- what gets drawn and on what scale.
            Default is to set viewport = map_BB
        :param image_mode: Image mode ('P' for palette or 'L' for Black and
            White image). BW_MapCanvas inherits from MapCanvas and sets the
            mode to 'L'. Default image_mode is 'P'.
        :param id: unique identifier for a instance of this class (UUID given
            as a string). This is used when loading an object from a persisted
            model.
        """

        # set up the canvas

        self._filename = filename
        if filename is not None:
            polygons = haz_files.ReadBNA(filename, 'PolygonSet')
        else:
            polygons = None
        Outputter.__init__(self, cache, output_timestep, output_zero_step,
                           output_last_step)
        MapCanvas.__init__(self, image_size, land_polygons=polygons,
                           **kwargs)

        self.images_dir = images_dir

        self.last_filename = ''

        self.draw_ontop = draw_ontop