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._next_output_time is None assert not o_put._write_step
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)
def __init__( self, filename, 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 polygons = haz_files.ReadBNA(filename, 'PolygonSet') 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