Exemplo n.º 1
0
def test_serialize_deserialize(json_, output_dir):
    # non-defaults to check properly..
    r = Renderer(map_filename=bna_sample,
                 output_dir=output_dir,
                 image_size=(1000, 800),
                 viewport=((-126.5, 47.5),
                           (-126.0, 48.0)),
                 )

    toserial = r.serialize(json_)
    dict_ = r.deserialize(toserial)

    # check our Renderer attributes
    if json_ == 'webapi':
        assert toserial['map_filename'] == basename(toserial['map_filename'])
    else:
        # in save context, we expect a full path to file
        assert toserial['map_filename'] != basename(toserial['map_filename'])

    assert toserial['map_filename'] == dict_['map_filename']
    dict_['map_filename'] = bna_sample  # put our full filename back in

    r2 = Renderer.new_from_dict(dict_)

    assert r == r2
Exemplo n.º 2
0
def test_serialize_deserialize(json_, output_dir):
    # non-defaults to check properly..
    r = Renderer(
        map_filename=bna_sample,
        output_dir=output_dir,
        image_size=(1000, 800),
        viewport=((-126.5, 47.5), (-126.0, 48.0)),
    )

    toserial = r.serialize(json_)
    dict_ = r.deserialize(toserial)

    # check our Renderer attributes
    if json_ == 'webapi':
        assert toserial['map_filename'] == basename(toserial['map_filename'])
    else:
        # in save context, we expect a full path to file
        assert toserial['map_filename'] != basename(toserial['map_filename'])

    assert toserial['map_filename'] == dict_['map_filename']
    dict_['map_filename'] = bna_sample  # put our full filename back in

    r2 = Renderer.new_from_dict(dict_)

    assert r == r2
Exemplo n.º 3
0
def test_exception(output_dir):
    # wrong name for draw on top
    with pytest.raises(ValueError):
        Renderer(bna_sample, output_dir, draw_ontop='forecasting')

    r = Renderer(bna_sample, output_dir)
    with pytest.raises(ValueError):
        r.draw_ontop = 'forecasting'
Exemplo n.º 4
0
def test_render_basemap_with_bounds(output_dir):
    """
    render the basemap
    """
    r = Renderer(bna_sample, output_dir, image_size=(600, 600))

    r.draw_background()
    r.save_background(os.path.join(output_dir, 'basemap_bounds.png'))
Exemplo n.º 5
0
def test_exception(output_dir):
    # wrong name for draw on top
    with pytest.raises(ValueError):
        Renderer(bna_sample, output_dir, draw_ontop='forecasting')

    r = Renderer(bna_sample, output_dir)
    with pytest.raises(ValueError):
        r.draw_ontop = 'forecasting'
Exemplo n.º 6
0
def test_show_hide_map_bounds(output_dir):
    r = Renderer(bna_star, output_dir, image_size=(600, 600))

    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_background.png'))

    # try again without the map bounds:

    r.draw_map_bounds = False
    r.draw_background()
    r.save_background(os.path.join(output_dir,
                      'star_background_no_bound.png'))
Exemplo n.º 7
0
def test_serialize_deserialize(output_dir):
    # non-defaults to check properly..
    r = Renderer(map_filename=bna_sample,
                 output_dir=output_dir,
                 image_size=(1000, 800),
                 viewport=((-126.5, 47.5), (-126.0, 48.0)))

    toserial = r.serialize()

    r2 = Renderer.deserialize(toserial)

    assert r == r2
Exemplo n.º 8
0
def test_rewind(output_dir):
    'test rewind calls base function and clear_output_dir'
    output_dir = os.path.join(output_dir, 'clear_test')
    r = Renderer(bna_sample, output_dir)
    bg_name = r.background_map_name
    fg_format = r.foreground_filename_format

    # dump some files into output dir:

    open(os.path.join(output_dir, bg_name), 'w').write('some junk')

    for i in range(5):
        open(os.path.join(output_dir,
             fg_format.format(i)),
             'w'
             ).write('some junk')

    now = datetime.now()
    r.prepare_for_model_run(model_start_time=now)

    assert r._model_start_time == now

    # prepare for model run clears output dir, but adds in the background map
    files = sorted(os.listdir(output_dir))
    assert files == sorted([os.path.basename(r.anim_filename),
                            r.background_map_name])

    r.rewind()

    # check super is called correctly
    assert r._model_start_time is None
    assert r._dt_since_lastoutput is None
    assert r._write_step is True

    # changed renderer and netcdf ouputter to delete old files in
    # prepare_for_model_run() rather than rewind()
    # -- rewind() was getting called a lot
    # -- before there was time to change the ouput file names, etc.
    # So for this unit test, there should only be a background image now.
    files = sorted(os.listdir(output_dir))
    assert files == sorted([os.path.basename(r.anim_filename),
                            r.background_map_name])
Exemplo n.º 9
0
def test_file_delete(output_dir):

    output_dir = os.path.join(output_dir, 'clear_test')

    r = Renderer(bna_sample, output_dir)
    bg_name = r.background_map_name + "png"
    fg_format = r.foreground_filename_format

    # dump some files into output dir:
    open(os.path.join(output_dir, bg_name), 'w').write('some junk')

    for i in range(5):
        open(os.path.join(output_dir,
                          fg_format.format(i) + "png"), 'w').write('some junk')

    r.prepare_for_model_run(model_start_time=datetime.now())

    # The default output formats are ['png','gif']
    # so now there should only be a background image and the animated gif.
    files = sorted(os.listdir(output_dir))
    assert files == sorted(
        [os.path.basename(r.anim_filename), r.background_map_name + "png"])
Exemplo n.º 10
0
def test_draw_raster_map(output_dir):
    """
    tests drawing the raster map

    Note: you need to look at the output to know if it did it right...

    """
    import gnome

    r = Renderer(bna_sample, image_size=(1000, 1000))
    r.viewport = ((-127.47, 48.10), (-127.22, 48.24))

    r.draw_background()

    # make a raster map out of the BNA:
    r.raster_map = gnome.map.MapFromBNA(bna_sample,
                                        raster_size=10000)

    r.raster_map_outline = True
    r.draw_raster_map()

    r.save_background(os.path.join(output_dir, 'raster_map_render.png'))
Exemplo n.º 11
0
def test_render_basemap_with_bounds(output_dir):
    """
    render the basemap
    """
    r = Renderer(bna_sample, output_dir, image_size=(600, 600))

    r.draw_background()
    r.save_background(os.path.join(output_dir, 'basemap_bounds.png'))
Exemplo n.º 12
0
def test_file_delete(output_dir):

    output_dir = os.path.join(output_dir, 'clear_test')

    r = Renderer(bna_sample, output_dir)
    bg_name = r.background_map_name
    fg_format = r.foreground_filename_format

    # dump some files into output dir:

    open(os.path.join(output_dir, bg_name), 'w').write('some junk')

    for i in range(5):
        open(os.path.join(output_dir, fg_format.format(i)), 'w'
             ).write('some junk')

    r.prepare_for_model_run(model_start_time=datetime.now())

    # The default output formats are ['png','gif']
    # so now there should only be a background image and the animated gif.
    files = sorted(os.listdir(output_dir))
    assert files == sorted([os.path.basename(r.anim_filename),
                            r.background_map_name])
Exemplo n.º 13
0
def test_render_beached_elements(output_dir):

    r = Renderer(bna_sample,
                 output_dir,
                 image_size=(800, 600))

    BB = r.map_BB
    (min_lon, min_lat) = BB[0]
    (max_lon, max_lat) = BB[1]

    N = 100

    # create some random particle positions:

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    # create a sc

    sc = sample_sc_release(num_elements=N)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    # make half of them on land

    sc['status_codes'][::2] = oil_status.on_land

    r.draw_elements(sc)

    # create an uncertainty sc

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    sc = sample_sc_release(num_elements=N, uncertain=True)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    # make half of them on land

    sc['status_codes'][::2] = oil_status.on_land

    r.draw_elements(sc)

    # save the image

    r.save_foreground(os.path.join(output_dir, 'elements2.png'))
Exemplo n.º 14
0
def test_render_beached_elements(output_dir):

    r = Renderer(bna_sample,
                 output_dir,
                 image_size=(800, 600))

    BB = r.map_BB
    (min_lon, min_lat) = BB[0]
    (max_lon, max_lat) = BB[1]

    N = 100

    # create some random particle positions:

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    # create a sc

    sc = sample_sc_release(num_elements=N)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    # make half of them on land

    sc['status_codes'][::2] = oil_status.on_land

    r.draw_elements(sc)

    # create an uncertainty sc

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    sc = sample_sc_release(num_elements=N, uncertain=True)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    # make half of them on land

    sc['status_codes'][::2] = oil_status.on_land

    r.draw_elements(sc)

    # save the image

    r.save_foreground(os.path.join(output_dir, 'elements2.png'))
Exemplo n.º 15
0
def test_render_elements(output_dir):
    """
    See if the "splots" get rendered corectly
    """

    r = Renderer(bna_sample, output_dir, image_size=(400, 400))

    BB = r.map_BB
    (min_lon, min_lat) = BB[0]
    (max_lon, max_lat) = BB[1]

    N = 1000

    # create some random particle positions:

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    # create a sc

    sc = sample_sc_release(num_elements=N)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    r.draw_elements(sc)

    # create an uncertainty sc

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    sc = sample_sc_release(num_elements=N, uncertain=True)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    r.draw_elements(sc)

    # save the image

    r.save_foreground(os.path.join(output_dir, 'elements1.png'))
Exemplo n.º 16
0
def test_render_elements(output_dir):
    """
    See if the "splots" get rendered corectly
    """

    r = Renderer(bna_sample, output_dir, image_size=(400, 400))

    BB = r.map_BB
    (min_lon, min_lat) = BB[0]
    (max_lon, max_lat) = BB[1]

    N = 1000

    # create some random particle positions:

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    # create a sc

    sc = sample_sc_release(num_elements=N)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    r.draw_elements(sc)

    # create an uncertainty sc

    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    sc = sample_sc_release(num_elements=N, uncertain=True)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    r.draw_elements(sc)

    # save the image

    r.save_foreground(os.path.join(output_dir, 'elements1.png'))
Exemplo n.º 17
0
def test_rewind(output_dir):
    'test rewind calls base function and clear_output_dir'
    output_dir = os.path.join(output_dir, 'clear_test')
    r = Renderer(bna_sample, output_dir)
    bg_name = r.background_map_name
    fg_format = r.foreground_filename_format

    # dump some files into output dir:

    open(os.path.join(output_dir, bg_name), 'w').write('some junk')

    for i in range(5):
        open(os.path.join(output_dir,
             fg_format.format(i)),
             'w'
             ).write('some junk')

    now = datetime.now()
    r.prepare_for_model_run(model_start_time=now)

    assert r._model_start_time == now

    # prepare for model run clears output dir, but adds in the background map
    files = sorted(os.listdir(output_dir))
    assert files == sorted([os.path.basename(r.anim_filename),
                            r.background_map_name])

    r.rewind()

    # check super is called correctly
    assert r._model_start_time is None
    assert r._dt_since_lastoutput is None
    assert r._write_step is True

    # changed renderer and netcdf ouputter to delete old files in
    # prepare_for_model_run() rather than rewind()
    # -- rewind() was getting called a lot
    # -- before there was time to change the ouput file names, etc.
    # So for this unit test, there should only be a background image now.
    files = sorted(os.listdir(output_dir))
    assert files == sorted([os.path.basename(r.anim_filename),
                            r.background_map_name])
Exemplo n.º 18
0
def test_set_viewport(output_dir):
    """
    tests various rendering, re-zooming, etc

    NOTE: this will only test if the code crashes, you have to look
          at the rendered images to see if it does the right thing
    """

    r = Renderer(bna_star,
                 output_dir,
                 image_size=(600, 600),
                 projection=GeoProjection(),
                 )

    # re-scale:
    # should show upper right corner

    r.viewport = ((-73, 40), (-70, 43))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_upper_right.png'))

    # re-scale:
    # should show lower right corner

    r.viewport = ((-73, 37), (-70, 40))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_lower_right.png'))

    # re-scale:
    # should show lower left corner

    r.viewport = ((-76, 37), (-73, 40))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_lower_left.png'))

    # re-scale:
    # should show upper left corner

    r.viewport = ((-76, 40), (-73, 43))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_upper_left.png'))
Exemplo n.º 19
0
def test_write_output(output_dir):
    """
    render the basemap
    """
    r = Renderer(bna_star,
                 output_dir,
                 image_size=(600, 600),
                 draw_back_to_fore=True,
                 formats=['png'])

    r.draw_background()

    BB = r.map_BB
    (min_lon, min_lat) = BB[0]
    (max_lon, max_lat) = BB[1]

    N = 100
    # create some random particle positions:
    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    # create a sc
    sc = sample_sc_release(num_elements=N)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    r.cache = FakeCache(sc)

    r.write_output(0)
    r.save_foreground(os.path.join(output_dir, 'map_and_elements.png'))

    r.draw_back_to_fore = False
    r.clear_foreground()
    r.write_output(1)
    r.save_foreground(os.path.join(output_dir, 'just_elements.png'))
Exemplo n.º 20
0
def test_write_output(output_dir):
    """
    render the basemap
    """
    r = Renderer(bna_star,
                 output_dir,
                 image_size=(600, 600),
                 draw_back_to_fore=True,
                 formats=['png'])

    r.draw_background()

    BB = r.map_BB
    (min_lon, min_lat) = BB[0]
    (max_lon, max_lat) = BB[1]

    N = 100
    # create some random particle positions:
    lon = random.uniform(min_lon, max_lon, (N, ))
    lat = random.uniform(min_lat, max_lat, (N, ))

    # create a sc
    sc = sample_sc_release(num_elements=N)
    sc['positions'][:, 0] = lon
    sc['positions'][:, 1] = lat

    r.cache = FakeCache(sc)

    r.write_output(0)
    r.save_foreground(os.path.join(output_dir, 'map_and_elements.png'))

    r.draw_back_to_fore = False
    r.clear_foreground()
    r.write_output(1)
    r.save_foreground(os.path.join(output_dir, 'just_elements.png'))
Exemplo n.º 21
0
def test_show_hide_map_bounds(output_dir):
    r = Renderer(bna_star, output_dir, image_size=(600, 600))

    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_background.png'))

    # try again without the map bounds:

    r.draw_map_bounds = False
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_background_no_bound.png'))
Exemplo n.º 22
0
def test_init(output_dir):
    r = Renderer(bna_sample, output_dir)

    assert True
Exemplo n.º 23
0
def test_set_viewport(output_dir):
    """
    tests various rendering, re-zooming, etc

    NOTE: this will only test if the code crashes, you have to look
          at the rendered images to see if it does the right thing
    """

    r = Renderer(
        bna_star,
        output_dir,
        image_size=(600, 600),
        projection=GeoProjection(),
    )

    # re-scale:
    # should show upper right corner

    r.viewport = ((-73, 40), (-70, 43))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_upper_right.png'))

    # re-scale:
    # should show lower right corner

    r.viewport = ((-73, 37), (-70, 40))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_lower_right.png'))

    # re-scale:
    # should show lower left corner

    r.viewport = ((-76, 37), (-73, 40))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_lower_left.png'))

    # re-scale:
    # should show upper left corner

    r.viewport = ((-76, 40), (-73, 43))
    r.draw_background()
    r.save_background(os.path.join(output_dir, 'star_upper_left.png'))
Exemplo n.º 24
0
def test_draw_raster_map(output_dir):
    """
    tests drawing the raster map

    Note: you need to look at the output to know if it did it right...

    """
    import gnome

    r = Renderer(bna_sample, image_size=(1000, 1000))
    r.viewport = ((-127.47, 48.10), (-127.22, 48.24))

    r.draw_background()

    # make a raster map out of the BNA:
    r.raster_map = gnome.map.MapFromBNA(bna_sample, raster_size=10000)

    r.raster_map_outline = True
    r.draw_raster_map()

    r.save_background(os.path.join(output_dir, 'raster_map_render.png'))