Exemple #1
0
def test_constant_wind_mover():
    """
    tests the constant_wind_mover utility function
    """
    with raises(Exception):
        # it should raise an InvalidUnitError, but I don't want to have to
        # import unit_conversion just for that...
        wm = constant_wind_mover(10, 45, units='some_random_string')

    wm = constant_wind_mover(10, 45, units='m/s')

    sc = sample_sc_release(1)

    print wm
    print repr(wm.wind)
    print wm.wind.get_wind_data()

    time_step = 1000
    model_time = datetime(2013, 3, 1, 0)
    wm.prepare_for_model_step(sc, time_step, model_time)
    delta = wm.get_move(sc, time_step, model_time)
    print 'delta:', delta

    # 45 degree wind at the equator -- u,v should be the same
    assert delta[0][0] == delta[0][1]
def test_constant_wind_mover():
    """
    tests the constant_wind_mover utility function
    """

    with pytest.raises(Exception):

        # it should raise an InvalidUnitError, but I don't want to have to
        # import unit_conversion just for that...

        wm = constant_wind_mover(10, 45, units='some_random_string')

    wm = constant_wind_mover(10, 45, units='m/s')

    sc = sample_sc_release(1)

    print wm
    print repr(wm.wind)
    print wm.wind.get_timeseries()
    time_step = 1000
    model_time = datetime(2013, 3, 1, 0)
    wm.prepare_for_model_step(sc, time_step, model_time)
    delta = wm.get_move(sc, time_step, model_time)
    print 'delta:', delta

    # 45 degree wind at the equator -- u,v should be the same

    assert delta[0][0] == delta[0][1]
def test_constant_wind():
    """
    make sure wind doesn't move the LEs marked as on_tideflat
    """
    wm = constant_wind_mover(10, 270, units='m/s')

    windage = 0.03
    sc = sample_sc_release(
        10,  # ten elements
        windage_range=(windage, windage),
    )
    time_step = 1000
    model_time = datetime(2018, 1, 1, 0)

    # make sure all is "normal"
    assert np.all(sc['status_codes'] == oil_status.in_water)

    delta = run_one_timestep(sc, wm, time_step, model_time)

    assert np.all(delta[:, 0] > 0.0)
    assert np.all(delta[:, 1] == 0.0)

    # now set the on_tideflat code on half the elements
    sc['status_codes'][5:] = oil_status.on_tideflat

    delta = run_one_timestep(sc, wm, time_step, model_time)

    print delta
    # west wind
    # only the first 5 should move
    assert np.all(delta[:5, 0] > 0.0)
    assert np.all(delta[5:, 0] == 0.0)
    assert np.all(delta[:, 1] == 0.0)
def model(sample_model_fcn, output_filename):
    """
    Use fixture model_surface_release_spill and add a few things to it for the
    test
    """
    model = sample_model_fcn['model']

    model.cache_enabled = True
    model.spills += \
        point_line_release_spill(num_elements=5,
                                 start_position=sample_model_fcn['release_start_pos'],
                                 release_time=model.start_time,
                                 end_release_time=model.start_time + model.duration,
                                 substance=test_oil,
                                 amount=1000,
                                 units='kg')

    water = Water()
    model.movers += RandomMover(diffusion_coef=100000)
    model.movers += constant_wind_mover(1.0, 0.0)
    model.weatherers += Evaporation(water, model.movers[-1].wind)

    model.outputters += NetCDFOutput(output_filename)

    model.rewind()

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2015, 9, 24, 3, 0)

    # 1 day of data in file
    # 1/2 hr in seconds
    model = Model(start_time=start_time,
                  duration=timedelta(hours = 48),
                  time_step=3600)

    mapfile = get_datafile(os.path.join(base_dir, 'Perfland.bna'))

    print 'adding the map'
    model.map = MapFromBNA(mapfile, refloat_halflife=1, raster_size=1024*1024)  # seconds

    # draw_ontop can be 'uncertain' or 'forecast'
    # 'forecast' LEs are in black, and 'uncertain' are in red
    # default is 'forecast' LEs draw on top
    renderer = Renderer(mapfile, images_dir, image_size=(800, 600),
                        output_timestep=timedelta(hours=1),
                        timestamp_attrib={'size': 'medium', 'color':'uncert_LE'})
    renderer.set_timestamp_attrib(format='%a %c')
    renderer.graticule.set_DMS(True)
#     renderer.viewport = ((-124.25, 47.5), (-122.0, 48.70))


    print 'adding outputters'
    model.outputters += renderer

    print 'adding a spill'
    # for now subsurface spill stays on initial layer
    # - will need diffusion and rise velocity
    # - wind doesn't act
    # - start_position = (-76.126872, 37.680952, 5.0),
    spill1 = point_line_release_spill(num_elements=5000,
                                     start_position=(0.0,
                                                     0.0,
                                                     0.0),
                                     release_time=start_time)

    model.spills += spill1

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=50000)

    print 'adding a wind mover:'

    model.movers += constant_wind_mover(13, 270, units='m/s')

    print 'adding a current mover:'
#     curr_file = get_datafile(os.path.join(base_dir, 'COOPSu_CREOFS24.nc'))
#
#     # uncertain_time_delay in hours
#     c_mover = GridCurrentMover(curr_file)
#     c_mover.uncertain_cross = 0  # default is .25
#
#     model.movers += c_mover

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2015, 9, 24, 3, 0)

    # 1 day of data in file
    # 1/2 hr in seconds
    model = Model(start_time=start_time,
                  duration=timedelta(hours = 48),
                  time_step=3600)

    mapfile = get_datafile(os.path.join(base_dir, 'Perfland.bna'))

    print 'adding the map'
    model.map = MapFromBNA(mapfile, refloat_halflife=1, raster_size=1024*1024)  # seconds

    # draw_ontop can be 'uncertain' or 'forecast'
    # 'forecast' LEs are in black, and 'uncertain' are in red
    # default is 'forecast' LEs draw on top
    renderer = Renderer(mapfile, images_dir, image_size=(800, 600),
                        output_timestep=timedelta(hours=1),
                        timestamp_attrib={'size': 'medium', 'color':'uncert_LE'})
    renderer.set_timestamp_attrib(format='%a %c')
    renderer.graticule.set_DMS(True)
#     renderer.viewport = ((-124.25, 47.5), (-122.0, 48.70))


    print 'adding outputters'
    model.outputters += renderer

    print 'adding a spill'
    # for now subsurface spill stays on initial layer
    # - will need diffusion and rise velocity
    # - wind doesn't act
    # - start_position = (-76.126872, 37.680952, 5.0),
    spill1 = point_line_release_spill(num_elements=5000,
                                     start_position=(0.0,
                                                     0.0,
                                                     0.0),
                                     release_time=start_time)

    model.spills += spill1

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=50000)

    print 'adding a wind mover:'

    model.movers += constant_wind_mover(13, 270, units='m/s')

    print 'adding a current mover:'
#     curr_file = get_datafile(os.path.join(base_dir, 'COOPSu_CREOFS24.nc'))
#
#     # uncertain_time_delay in hours
#     c_mover = GridCurrentMover(curr_file)
#     c_mover.uncertain_cross = 0  # default is .25
#
#     model.movers += c_mover

    return model
def make_model(img_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2013, 5, 18, 0)

    model = Model(start_time=start_time, duration=timedelta(days=3),
                  time_step=3600, uncertain=False)


    print 'adding the map'
    p_map = model.map = ParamMap(center = (0,0), distance=20, bearing = 20, units='km' )  # hours

    #
    # Add the outputters -- render to images, and save out as netCDF
    #

    print 'adding renderer'
    rend = Renderer( output_dir=img_dir,
                                 image_size=(800, 600),
                                 map_BB=p_map.get_map_bounds(),
                                 land_polygons=p_map.get_land_polygon(),
                                 )
    
    rend.graticule.set_DMS(True)
    model.outputters += rend
#                                 draw_back_to_fore=True)

    # print "adding netcdf output"
    # netcdf_output_file = os.path.join(base_dir,'mariana_output.nc')
    # scripting.remove_netcdf(netcdf_output_file)
    # model.outputters += NetCDFOutput(netcdf_output_file, which_data='all')

    #
    # Set up the movers:
    #

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=100000)

    print 'adding a simple wind mover:'
    model.movers += constant_wind_mover(10, 225, units='m/s')

    print 'adding a current mover:'

#     # # this is HYCOM currents
#     curr_file = get_datafile(os.path.join(base_dir, 'HYCOM.nc'))
#     model.movers += GridCurrentMover(curr_file,
#                                      num_method=numerical_methods.euler);

    # #
    # # Add some spills (sources of elements)
    # #

    print 'adding four spill'
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(0.0,0.0,
                                                             0.0),
                                             release_time=start_time)

    return model
Exemple #8
0
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2012, 10, 25, 0, 1)
    # start_time = datetime(2015, 12, 18, 06, 01)

    # 1 day of data in file
    # 1/2 hr in seconds
    model = Model(start_time=start_time,
                  duration=timedelta(hours=6),
                  time_step=900)

    mapfile = get_datafile(os.path.join(base_dir, 'nyharbor.bna'))

    print 'adding the map'
    '''TODO: sort out MapFromBna's map_bounds parameter...
    it does nothing right now, and the spill is out of bounds'''
    model.map = MapFromBNA(mapfile, refloat_halflife=0.0)  # seconds

    # draw_ontop can be 'uncertain' or 'forecast'
    # 'forecast' LEs are in black, and 'uncertain' are in red
    # default is 'forecast' LEs draw on top
    renderer = Renderer(mapfile, images_dir, image_size=(1024, 768))
    #     renderer.viewport = ((-73.5, 40.5), (-73.1, 40.75))
    #     renderer.viewport = ((-122.9, 45.6), (-122.6, 46.0))

    print 'adding outputters'
    model.outputters += renderer

    print 'adding a spill'
    # for now subsurface spill stays on initial layer
    # - will need diffusion and rise velocity
    # - wind doesn't act
    # - start_position = (-76.126872, 37.680952, 5.0),
    spill1 = point_line_release_spill(num_elements=1000,
                                      start_position=(-74.15, 40.5, 0.0),
                                      release_time=start_time)

    model.spills += spill1

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=50000)

    print 'adding a wind mover:'

    model.movers += constant_wind_mover(4, 270, units='m/s')

    print 'adding a current mover:'

    # url is broken, fix and include the following section
    #     url = ('http://geoport.whoi.edu/thredds/dodsC/clay/usgs/users/jcwarner/Projects/Sandy/triple_nest/00_dir_NYB05.ncml')
    # #     cf = roms_field('nos.tbofs.fields.n000.20160406.t00z_sgrid.nc')
    #     cf = GridCurrent.from_netCDF(url)
    #     renderer.add_grid(cf.grid)
    #     renderer.delay = 25
    #     u_mover = PyCurrentMover(cf, default_num_method='Euler')
    #     model.movers += u_mover
    #

    return model
Exemple #9
0
def setup_model():
    print 'initializing the model'
    # start with default time,duration...this will be changed when model is run
    model = Model(
    )  #change to use all defaults and set time_step also in Setup_TAP!!
    mapfile = os.path.join(setup.MapFileDir, setup.MapFileName)
    print 'adding the map: ', mapfile
    model.map = MapFromBNA(mapfile, refloat_halflife=0.0)  # seconds

    print 'adding a GridCurrentMover:'
    if setup.curr_fn.endswith('.nc'):
        c_mover = GridCurrentMover(filename=setup.curr_fn, extrapolate=True)
    elif setup.curr_fn.endswith('.cur'):
        tide = Tide(setup.tide_fn)
        c_mover = CatsMover(filename=setup.curr_fn, tide=tide)
    model.movers += c_mover

    print 'adding a WindMover:'
    if setup.wind_fn is not None:
        w = Wind(filename=setup.wind_fn)
        w_mover = WindMover(w)
    elif setup.wind_data is not None:
        w_mover = constant_wind_mover(setup.wind_data[0],
                                      setup.wind_data[1],
                                      units='knots')
    # w_mover = GridWindMover(wind_file=setup.w_filelist)
    model.movers += w_mover

    if setup.diff_coef is not None:
        print 'adding a RandomMover:'
        random_mover = RandomMover(diffusion_coef=setup.diff_coef)  #in cm/s
        model.movers += random_mover

    return model
Exemple #10
0
def model(sample_model_fcn, output_filename):
    """
    Use fixture model_surface_release_spill and add a few things to it for the
    test
    """
    model = sample_model_fcn['model']

    model.cache_enabled = True
    model.spills += \
        point_line_release_spill(num_elements=5,
                                 start_position=sample_model_fcn['release_start_pos'],
                                 release_time=model.start_time,
                                 end_release_time=model.start_time + model.duration,
                                 substance=test_oil,
                                 amount=1000,
                                 units='kg')

    water = Water()
    model.movers += RandomMover(diffusion_coef=100000)
    model.movers += constant_wind_mover(1.0, 0.0)
    model.weatherers += Evaporation(water=water, wind=model.movers[-1].wind)

    model.outputters += NetCDFOutput(output_filename)

    model.rewind()

    return model
Exemple #11
0
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2015, 9, 24, 1, 1)

    # 1 day of data in file
    # 1/2 hr in seconds
    model = Model(start_time=start_time,
                  duration=timedelta(hours=48),
                  time_step=900)

    mapfile = get_datafile(os.path.join(base_dir, 'columbia_river.bna'))

    print 'adding the map'
    model.map = MapFromBNA(mapfile, refloat_halflife=0.0)  # seconds

    # draw_ontop can be 'uncertain' or 'forecast'
    # 'forecast' LEs are in black, and 'uncertain' are in red
    # default is 'forecast' LEs draw on top
    renderer = Renderer(mapfile, images_dir, image_size=(600, 1200))
    renderer.graticule.set_DMS(True)
    #     renderer.viewport = ((-123.35, 45.6), (-122.68, 46.13))
    #     renderer.viewport = ((-122.9, 45.6), (-122.6, 46.0))

    print 'adding outputters'
    model.outputters += renderer

    print 'adding a spill'
    # for now subsurface spill stays on initial layer
    # - will need diffusion and rise velocity
    # - wind doesn't act
    # - start_position = (-76.126872, 37.680952, 5.0),
    spill1 = point_line_release_spill(num_elements=1000,
                                      start_position=(-122.625, 45.609, 0.0),
                                      release_time=start_time)

    model.spills += spill1

    print 'adding a RandomMover:'
    # model.movers += RandomMover(diffusion_coef=50000)

    print 'adding a wind mover:'

    model.movers += constant_wind_mover(0.5, 0, units='m/s')

    print 'adding a current mover:'
    curr_file = get_datafile(os.path.join(base_dir, 'COOPSu_CREOFS24.nc'))

    # uncertain_time_delay in hours
    # vec_field = TriVectorField('COOPSu_CREOFS24.nc')
    # u_mover = UGridCurrentMover(vec_field)
    c_mover = GridCurrentMover(curr_file)
    # c_mover.uncertain_cross = 0  # default is .25

    # model.movers += u_mover
    model.movers += c_mover
    model.save

    return model
def test_gnome_obj_reference():
    '''
    create two equal but different objects and make sure a new reference is
    created for each
    '''
    objs = [constant_wind_mover(0, 0) for _i in range(2)]
    assert objs[0] is not objs[1]

    refs = References()
    r_objs = [refs.reference(item) for item in objs]
    assert len(r_objs) == len(objs)
    assert r_objs[0] != r_objs[1]

    for ix, ref in enumerate(r_objs):
        assert refs.retrieve(ref) is objs[ix]
        assert objs[ix] in refs  # double check __contains__

    unknown = constant_wind_mover(0, 0)
    assert unknown not in refs  # check __contains__
def test_gnome_obj_reference():
    '''
    create two equal but different objects and make sure a new reference is
    created for each
    '''
    objs = [constant_wind_mover(0, 0) for _i in range(2)]
    assert objs[0] is not objs[1]

    refs = References()
    r_objs = [refs.reference(item) for item in objs]
    assert len(r_objs) == len(objs)
    assert r_objs[0] != r_objs[1]

    for ix, ref in enumerate(r_objs):
        assert refs.retrieve(ref) is objs[ix]
        assert objs[ix] in refs   # double check __contains__

    unknown = constant_wind_mover(0, 0)
    assert unknown not in refs  # check __contains__
Exemple #14
0
def test_windage_index():
    """
    A very simple test to make sure windage is set for the correct sc
    if staggered release
    """
    sc = SpillContainer()
    rel_time = datetime(2013, 1, 1, 0, 0)
    timestep = 30
    for i in range(2):
        spill = point_line_release_spill(num_elements=5,
                                         start_position=(0., 0., 0.),
                                         release_time=rel_time + i * timedelta(hours=1),
                                         element_type=floating(windage_range=(i * .01 +
                                                               .01, i * .01 + .01),
                                                               windage_persist=900)
                                         )
        sc.spills.add(spill)

    windage = ['windages', 'windage_range', 'windage_persist']
    sc.prepare_for_model_run(array_types=windage)
    sc.release_elements(timestep, rel_time)

    wm = constant_wind_mover(5, 0)
    wm.prepare_for_model_step(sc, timestep, rel_time)
    wm.model_step_is_done()  # need this to toggle _windage_is_set_flag

    def _check_index(sc):
        '''
        internal function for doing the test after windage is set
        - called twice so made a function
        '''
        # only 1st sc is released
        for sp in sc.spills:
            mask = sc.get_spill_mask(sp)
            if np.any(mask):
                assert np.all(sc['windages'][mask] ==
                              sp.windage_range[0])

    # only 1st spill is released
    _check_index(sc)  # 1st ASSERT

    sc.release_elements(timestep, rel_time + timedelta(hours=1))
    wm.prepare_for_model_step(sc, timestep, rel_time)
    _check_index(sc)  # 2nd ASSERT
def test_windage_index():
    """
    A very simple test to make sure windage is set for the correct sc
    if staggered release
    """
    sc = SpillContainer()
    rel_time = datetime(2013, 1, 1, 0, 0)
    timestep = 30
    for i in range(2):
        spill = point_line_release_spill(num_elements=5,
                                         start_position=(0., 0., 0.),
                                         release_time=rel_time + i * timedelta(hours=1),
                                         element_type=floating(windage_range=(i * .01 +
                                                               .01, i * .01 + .01),
                                                               windage_persist=900)
                                         )
        sc.spills.add(spill)

    windage = ['windages', 'windage_range', 'windage_persist']
    sc.prepare_for_model_run(array_types=windage)
    sc.release_elements(timestep, rel_time)

    wm = constant_wind_mover(5, 0)
    wm.prepare_for_model_step(sc, timestep, rel_time)
    wm.model_step_is_done()  # need this to toggle _windage_is_set_flag

    def _check_index(sc):
        '''
        internal function for doing the test after windage is set
        - called twice so made a function
        '''
        # only 1st sc is released
        for sp in sc.spills:
            mask = sc.get_spill_mask(sp)
            if np.any(mask):
                assert np.all(sc['windages'][mask] ==
                              sp.windage_range[0])

    # only 1st spill is released
    _check_index(sc)  # 1st ASSERT

    sc.release_elements(timestep, rel_time + timedelta(hours=1))
    wm.prepare_for_model_step(sc, timestep, rel_time)
    _check_index(sc)  # 2nd ASSERT
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2013, 5, 18, 0)

    model = Model(start_time=start_time, duration=timedelta(days=8),
                  time_step=1 * 3600, uncertain=False)

    mapfile = get_datafile(os.path.join(base_dir, './mariana_island.bna'))

    print 'adding the map'
    model.map = MapFromBNA(mapfile, refloat_halflife=6)  # hours

    #
    # Add the outputters -- render to images, and save out as netCDF
    #

    print 'adding renderer'
    model.outputters += Renderer(mapfile, images_dir, size=(800, 600))

    # print "adding netcdf output"
    # netcdf_output_file = os.path.join(base_dir,'mariana_output.nc')
    # scripting.remove_netcdf(netcdf_output_file)
    # model.outputters += gnome.netcdf_outputter.NetCDFOutput(netcdf_output_file,
    #                                                         which_data='all')

    # #
    # # Set up the movers:
    # #

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=10000)

    print 'adding a simple wind mover:'
    model.movers += constant_wind_mover(5, 315, units='m/s')

    print 'adding a current mover:'

    # # this is HYCOM currents
    curr_file = get_datafile(os.path.join(base_dir, r"./HYCOM.nc"))
    model.movers += GridCurrentMover(curr_file)

    # #
    # # Add some spills (sources of elements)
    # #

    print 'adding four spill'
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(145.25, 15.0,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(146.25, 15.0,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(145.75, 15.25,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(145.75, 14.75,
                                                             0.0),
                                             release_time=start_time)

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2015, 9, 24, 1, 1)

    # 1 day of data in file
    # 1/2 hr in seconds
    model = Model(start_time=start_time,
                  duration=timedelta(hours=48),
                  time_step=900)

    mapfile = get_datafile(os.path.join(base_dir, 'columbia_river.bna'))

    print 'adding the map'
    model.map = MapFromBNA(mapfile, refloat_halflife=0.0)  # seconds

    # draw_ontop can be 'uncertain' or 'forecast'
    # 'forecast' LEs are in black, and 'uncertain' are in red
    # default is 'forecast' LEs draw on top
    renderer = Renderer(mapfile, images_dir, image_size=(600, 1200))
    renderer.graticule.set_DMS(True)
#     renderer.viewport = ((-123.35, 45.6), (-122.68, 46.13))
#     renderer.viewport = ((-122.9, 45.6), (-122.6, 46.0))

    print 'adding outputters'
    model.outputters += renderer

    print 'adding a spill'
    # for now subsurface spill stays on initial layer
    # - will need diffusion and rise velocity
    # - wind doesn't act
    # - start_position = (-76.126872, 37.680952, 5.0),
    spill1 = point_line_release_spill(num_elements=1000,
                                      start_position=(-122.625,
                                                      45.609,
                                                      0.0),
                                      release_time=start_time)

    model.spills += spill1

    print 'adding a RandomMover:'
    # model.movers += RandomMover(diffusion_coef=50000)

    print 'adding a wind mover:'

    model.movers += constant_wind_mover(0.5, 0, units='m/s')

    print 'adding a current mover:'
    curr_file = get_datafile(os.path.join(base_dir, 'COOPSu_CREOFS24.nc'))

    # uncertain_time_delay in hours
    # vec_field = TriVectorField('COOPSu_CREOFS24.nc')
    # u_mover = UGridCurrentMover(vec_field)
    c_mover = GridCurrentMover(curr_file)
    # c_mover.uncertain_cross = 0  # default is .25

    # model.movers += u_mover
    model.movers += c_mover
    model.save

    return model
Exemple #18
0
def test_constant_wind_mover_bounds():
    wm = constant_wind_mover(10, 45, units='knots')

    assert wm.data_start == wm.data_stop
def make_model(img_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2013, 5, 18, 0)

    model = Model(start_time=start_time,
                  duration=timedelta(days=3),
                  time_step=3600,
                  uncertain=False)

    print 'adding the map'
    p_map = model.map = ParamMap(center=(0, 0),
                                 distance=20,
                                 bearing=20,
                                 units='km')  # hours

    #
    # Add the outputters -- render to images, and save out as netCDF
    #

    print 'adding renderer'
    rend = Renderer(
        output_dir=img_dir,
        image_size=(800, 600),
        map_BB=p_map.get_map_bounds(),
        land_polygons=p_map.get_land_polygon(),
    )

    rend.graticule.set_DMS(True)
    model.outputters += rend
    #                                 draw_back_to_fore=True)

    # print "adding netcdf output"
    # netcdf_output_file = os.path.join(base_dir,'mariana_output.nc')
    # scripting.remove_netcdf(netcdf_output_file)
    # model.outputters += NetCDFOutput(netcdf_output_file, which_data='all')

    #
    # Set up the movers:
    #

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=100000)

    print 'adding a simple wind mover:'
    model.movers += constant_wind_mover(10, 225, units='m/s')

    print 'adding a current mover:'

    #     # # this is HYCOM currents
    #     curr_file = get_datafile(os.path.join(base_dir, 'HYCOM.nc'))
    #     model.movers += GridCurrentMover(curr_file,
    #                                      num_method=numerical_methods.euler);

    # #
    # # Add some spills (sources of elements)
    # #

    print 'adding four spill'
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(0.0, 0.0, 0.0),
                                             release_time=start_time)

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    # set up the modeling environment
    start_time = datetime(2016, 9, 18, 1, 0)
    model = Model(start_time=start_time,
                  duration=timedelta(days=3),
                  time_step=30 * 60,
                  uncertain=False)

    print 'adding the map'
    model.map = GnomeMap()  # this is a "water world -- no land anywhere"

    # renderere is only top-down view on 2d -- but it's something
    renderer = Renderer(output_dir=images_dir,
                        image_size=(1024, 768),
                        output_timestep=timedelta(hours=1),
                        )
    renderer.viewport = ((-87.295, 27.795), (-87.705, 28.205))

    print 'adding outputters'
    model.outputters += renderer

    # Also going to write the results out to a netcdf file
    netcdf_file = os.path.join(base_dir, 'gulf_tamoc.nc')
    scripting.remove_netcdf(netcdf_file)

    model.outputters += NetCDFOutput(netcdf_file,
                                     which_data='most',
                                     # output most of the data associated with the elements
                                     output_timestep=timedelta(hours=2))

    print "adding Horizontal and Vertical diffusion"

    # Horizontal Diffusion
    #model.movers += RandomMover(diffusion_coef=100000)
    # vertical diffusion (different above and below the mixed layer)
    model.movers += RandomVerticalMover(vertical_diffusion_coef_above_ml=50,
                                        vertical_diffusion_coef_below_ml=10,
                                        horizontal_diffusion_coef_above_ml=100000,
                                        horizontal_diffusion_coef_below_ml=100,
                                        mixed_layer_depth=10)

    print 'adding Rise Velocity'
    # droplets rise as a function of their density and radius
    model.movers += TamocRiseVelocityMover()

    print 'adding the 3D current mover'
    gc = GridCurrent.from_netCDF('HYCOM_3d.nc')

    model.movers += PyCurrentMover('HYCOM_3d.nc')
#    model.movers += SimpleMover(velocity=(0., 0, 0.))
    model.movers += constant_wind_mover(10, 315, units='knots')

    # Wind from a buoy
    #w = Wind(filename='KIKT.osm')
    #model.movers += WindMover(w)


    # Now to add in the TAMOC "spill"
    print "Adding TAMOC spill"

    model.spills += tamoc_spill.TamocSpill(release_time=start_time,
                                        start_position=(-87.5, 28.0, 1000),
                                        num_elements=1000,
                                        end_release_time=start_time + timedelta(days=2),
                                        name='TAMOC plume',
                                        TAMOC_interval=None,  # how often to re-run TAMOC
                                        )

    model.spills[0].data_sources['currents'] = gc

    return model
Exemple #21
0
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2012, 10, 25, 0, 1)
    # start_time = datetime(2015, 12, 18, 06, 01)

    # 1 day of data in file
    # 1/2 hr in seconds
    model = Model(start_time=start_time,
                  duration=timedelta(hours=6),
                  time_step=900)

    mapfile = get_datafile(os.path.join(base_dir, 'nyharbor.bna'))

    print 'adding the map'
    '''TODO: sort out MapFromBna's map_bounds parameter...
    it does nothing right now, and the spill is out of bounds'''
    model.map = MapFromBNA(mapfile, refloat_halflife=0.0)  # seconds

    # draw_ontop can be 'uncertain' or 'forecast'
    # 'forecast' LEs are in black, and 'uncertain' are in red
    # default is 'forecast' LEs draw on top
    renderer = Renderer(mapfile, images_dir, image_size=(1024, 768))
#     renderer.viewport = ((-73.5, 40.5), (-73.1, 40.75))
#     renderer.viewport = ((-122.9, 45.6), (-122.6, 46.0))

    print 'adding outputters'
    model.outputters += renderer

    print 'adding a spill'
    # for now subsurface spill stays on initial layer
    # - will need diffusion and rise velocity
    # - wind doesn't act
    # - start_position = (-76.126872, 37.680952, 5.0),
    spill1 = point_line_release_spill(num_elements=1000,
                                      start_position=(-74.15,
                                                      40.5,
                                                      0.0),
                                      release_time=start_time)

    model.spills += spill1

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=50000)

    print 'adding a wind mover:'

    model.movers += constant_wind_mover(4, 270, units='m/s')

    print 'adding a current mover:'

    url = ('http://geoport.whoi.edu/thredds/dodsC/clay/usgs/users/jcwarner/Projects/Sandy/triple_nest/00_dir_NYB05.ncml')
#     cf = roms_field('nos.tbofs.fields.n000.20160406.t00z_sgrid.nc')
    cf = GridCurrent.from_netCDF(url)
    renderer.add_grid(cf.grid)
    renderer.delay = 25
    u_mover = PyGridCurrentMover(cf, default_num_method='Euler')
    model.movers += u_mover

    # curr_file = get_datafile(os.path.join(base_dir, 'COOPSu_CREOFS24.nc'))
    # c_mover = GridCurrentMover(curr_file)
    # model.movers += c_mover

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2013, 5, 18, 0)

    model = Model(start_time=start_time, duration=timedelta(days=8),
                  time_step=4 * 3600, uncertain=False)

    mapfile = get_datafile(os.path.join(base_dir, 'mariana_island.bna'))

    print 'adding the map'
    model.map = MapFromBNA(mapfile, refloat_halflife=6)  # hours

    #
    # Add the outputters -- render to images, and save out as netCDF
    #

    print 'adding renderer'
    rend =  Renderer(mapfile,
                                 images_dir,
                                 size=(800, 600),
                                 draw_map_bounds=True
                                 )
    rend.draw_raster_map()
    model.outputters += rend
#                                 draw_back_to_fore=True)

    # print "adding netcdf output"
    # netcdf_output_file = os.path.join(base_dir,'mariana_output.nc')
    # scripting.remove_netcdf(netcdf_output_file)
    # model.outputters += NetCDFOutput(netcdf_output_file, which_data='all')

    #
    # Set up the movers:
    #
    rend.zoom(0.5)
    rend.zoom(2)

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=10000)

    print 'adding a simple wind mover:'
    model.movers += constant_wind_mover(7, 90, units='m/s')

    print 'adding a current mover:'

#     # # this is HYCOM currents
#     curr_file = get_datafile(os.path.join(base_dir, 'HYCOM.nc'))
#     model.movers += GridCurrentMover(curr_file,
#                                      num_method=numerical_methods.euler);

    # #
    # # Add some spills (sources of elements)
    # #

    print 'adding four spill'
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(146, 15.25,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(146, 15.125,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(146, 15.0,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(146, 14.875,
                                                             0.0),
                                             release_time=start_time)

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    # set up the modeling environment
    start_time = datetime(2016, 9, 18, 1, 0)
    model = Model(start_time=start_time,
                  duration=timedelta(days=3),
                  time_step=30 * 60,
                  uncertain=False)

    print 'adding the map'
    model.map = GnomeMap()  # this is a "water world -- no land anywhere"

    # renderere is only top-down view on 2d -- but it's something
    renderer = Renderer(
        output_dir=images_dir,
        image_size=(1024, 768),
        output_timestep=timedelta(hours=1),
    )
    renderer.viewport = ((-87.295, 27.795), (-87.705, 28.205))

    print 'adding outputters'
    model.outputters += renderer

    # Also going to write the results out to a netcdf file
    netcdf_file = os.path.join(base_dir, 'gulf_tamoc.nc')
    scripting.remove_netcdf(netcdf_file)

    model.outputters += NetCDFOutput(
        netcdf_file,
        which_data='most',
        # output most of the data associated with the elements
        output_timestep=timedelta(hours=2))

    print "adding Horizontal and Vertical diffusion"

    # Horizontal Diffusion
    #model.movers += RandomMover(diffusion_coef=100000)
    # vertical diffusion (different above and below the mixed layer)
    model.movers += RandomMover3D(vertical_diffusion_coef_above_ml=50,
                                  vertical_diffusion_coef_below_ml=10,
                                  horizontal_diffusion_coef_above_ml=100000,
                                  horizontal_diffusion_coef_below_ml=100,
                                  mixed_layer_depth=10)

    print 'adding Rise Velocity'
    # droplets rise as a function of their density and radius
    model.movers += TamocRiseVelocityMover()

    print 'adding the 3D current mover'
    gc = GridCurrent.from_netCDF('HYCOM_3d.nc')

    model.movers += PyCurrentMover('HYCOM_3d.nc')
    #    model.movers += SimpleMover(velocity=(0., 0, 0.))
    model.movers += constant_wind_mover(10, 315, units='knots')

    # Wind from a buoy
    #w = Wind(filename='KIKT.osm')
    #model.movers += WindMover(w)

    # Now to add in the TAMOC "spill"
    print "Adding TAMOC spill"

    model.spills += tamoc_spill.TamocSpill(
        release_time=start_time,
        start_position=(-87.5, 28.0, 1000),
        num_elements=1000,
        end_release_time=start_time + timedelta(days=2),
        name='TAMOC plume',
        TAMOC_interval=None,  # how often to re-run TAMOC
    )

    model.spills[0].data_sources['currents'] = gc

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    # set up the modeling environment
    start_time = datetime(2016, 9, 23, 0, 0)
    model = Model(start_time=start_time,
                  duration=timedelta(days=2),
                  time_step=30 * 60,
                  uncertain=False)

    print 'adding the map'
    model.map = GnomeMap()  # this is a "water world -- no land anywhere"

    # renderere is only top-down view on 2d -- but it's something
    renderer = Renderer(output_dir=images_dir,
                        image_size=(1024, 768),
                        output_timestep=timedelta(hours=1),
                        )
    renderer.viewport = ((196.14, 71.89), (196.18, 71.93))

    print 'adding outputters'
    model.outputters += renderer

    # Also going to write the results out to a netcdf file
    netcdf_file = os.path.join(base_dir, 'script_arctic_plume.nc')
    scripting.remove_netcdf(netcdf_file)

    model.outputters += NetCDFOutput(netcdf_file,
                                     which_data='most',
                                     # output most of the data associated with the elements
                                     output_timestep=timedelta(hours=2))

    print "adding Horizontal and Vertical diffusion"

    # Horizontal Diffusion
    model.movers += RandomMover(diffusion_coef=500)
    # vertical diffusion (different above and below the mixed layer)
    model.movers += RandomMover3D(vertical_diffusion_coef_above_ml=5,
                                        vertical_diffusion_coef_below_ml=.11,
                                        mixed_layer_depth=10)

    print 'adding Rise Velocity'
    # droplets rise as a function of their density and radius
    model.movers += TamocRiseVelocityMover()

    print 'adding a circular current and eastward current'
    fn = 'hycom_glb_regp17_2016092300_subset.nc'
    fn_ice = 'hycom-cice_ARCu0.08_046_2016092300_subset.nc'
    iconc = IceConcentration.from_netCDF(filename=fn_ice)
    ivel = IceVelocity.from_netCDF(filename=fn_ice, grid = iconc.grid)
    ic = IceAwareCurrent.from_netCDF(ice_concentration = iconc, ice_velocity= ivel, filename=fn)

    model.movers += PyCurrentMover(current = ic)
    model.movers += SimpleMover(velocity=(0., 0., 0.))
    model.movers += constant_wind_mover(20, 315, units='knots')

    # Now to add in the TAMOC "spill"
    print "Adding TAMOC spill"

    model.spills += tamoc_spill.TamocSpill(release_time=start_time,
                                        start_position=(196.16, 71.91, 40.0),
                                        num_elements=1000,
                                        end_release_time=start_time + timedelta(days=1),
                                        name='TAMOC plume',
                                        TAMOC_interval=None,  # how often to re-run TAMOC
                                        )

    model.spills[0].data_sources['currents'] = ic

    return model
def test_constant_wind_mover_bounds():
    wm = constant_wind_mover(10, 45, units='knots')

    assert wm.data_start == InfDateTime("-inf")
    assert wm.data_stop == InfDateTime("inf")
Exemple #26
0
def test_constant_wind_mover_bounds():
    wm = constant_wind_mover(10, 45, units='knots')

    assert wm.data_start == InfDateTime("-inf")
    assert wm.data_stop == InfDateTime("inf")
def test_full_model_run():
    """
    This will have a couple movers, and make sure that the
    on_tideflat status code stops all movers.

    only winds and currents for now
    """
    start_time = "2018-09-10T12:00"

    model = Model(
        start_time=start_time,
        time_step=sc.minutes(10),
    )

    #                   start_time=round_time(datetime.now(), 3600),

    #                   duration=timedelta(days=1),
    #                  weathering_substeps=1,
    #                  map=None,
    #                  uncertain=False,
    #                  cache_enabled=False,
    #                  mode=None,
    #                  location=[],
    #                  environment=[],
    #                  outputters=[],
    #                  movers=[],
    #                  weatherers=[],
    #                  spills=[],
    #                  uncertain_spills=[],
    #                  **kwargs):
    # )

    model.movers += constant_wind_mover(speed=10, direction=225, units='m/s')

    model.movers += RandomMover()  # defaults are fine

    model.spills += point_line_release_spill(
        num_elements=10,
        start_position=(0.0, 0.0, 0.0),
        release_time=start_time,
    )

    # fixme: should maybe add currents -- though if the currents are smart,
    #        they should be zero on the tideflats

    # run one step:
    print model.step()
    # step zero -- should be released, but not yet moved
    positions = model.get_spill_property('positions').copy()
    assert np.all(positions == 0.0)
    prev_positions = positions

    print model.step()
    # step one -- all elements should have moved in horizontal
    positions = model.get_spill_property('positions').copy()
    assert np.all(positions[:, 0:1] != prev_positions[:, 0:1])
    assert np.all(positions[:, 2] == prev_positions[:, 2])
    prev_positions = positions

    # Now set the flags for half of the elements
    status_codes = model.get_spill_property('status_codes')
    new_status_codes = np.array([oil_status.on_tideflat] * 5 +
                                [oil_status.in_water] * 5)
    status_codes[:] = new_status_codes

    # make sure it took
    assert np.all(model.get_spill_property('status_codes') == new_status_codes)

    # step the model again
    model.step()
    positions = model.get_spill_property('positions').copy()
    delta = positions - prev_positions
    # first five should not have moved
    assert np.all(delta[:5, 0:1] == 0.0)
    assert np.all(delta[5:, 0:1] != 0.0)
    prev_positions = positions

    # reset status codes to in_water:
    model.get_spill_property('status_codes')[:] = oil_status.in_water

    # step the model again
    print model.step()
    positions = model.get_spill_property('positions').copy()
    delta = positions - prev_positions
    # they all should have moved again
    assert np.all(delta[:, 0:1] != 0.0)
Exemple #28
0
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    start_time = datetime(2013, 5, 18, 0)

    model = Model(start_time=start_time,
                  duration=timedelta(days=8),
                  time_step=4 * 3600,
                  uncertain=False)

    mapfile = get_datafile(os.path.join(base_dir, 'mariana_island.bna'))

    print 'adding the map'
    model.map = MapFromBNA(mapfile, refloat_halflife=6)  # hours

    #
    # Add the outputters -- render to images, and save out as netCDF
    #

    print 'adding renderer'
    model.outputters += Renderer(
        mapfile,
        images_dir,
        size=(800, 600),
    )
    #                                 draw_back_to_fore=True)

    # print "adding netcdf output"
    # netcdf_output_file = os.path.join(base_dir,'mariana_output.nc')
    # scripting.remove_netcdf(netcdf_output_file)
    # model.outputters += NetCDFOutput(netcdf_output_file, which_data='all')

    #
    # Set up the movers:
    #

    print 'adding a RandomMover:'
    model.movers += RandomMover(diffusion_coef=10000)

    print 'adding a simple wind mover:'
    model.movers += constant_wind_mover(5, 315, units='m/s')

    print 'adding a current mover:'

    # # this is HYCOM currents
    curr_file = get_datafile(os.path.join(base_dir, 'HYCOM.nc'))
    model.movers += GridCurrentMover(curr_file,
                                     num_method=numerical_methods.euler)

    # #
    # # Add some spills (sources of elements)
    # #

    print 'adding four spill'
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(145.25, 15.0,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(146.25, 15.0,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(145.75, 15.25,
                                                             0.0),
                                             release_time=start_time)
    model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4,
                                             start_position=(145.75, 14.75,
                                                             0.0),
                                             release_time=start_time)

    return model
def make_model(images_dir=os.path.join(base_dir, 'images')):
    print 'initializing the model'

    # set up the modeling environment
    start_time = datetime(2016, 9, 23, 0, 0)
    model = Model(start_time=start_time,
                  duration=timedelta(days=2),
                  time_step=30 * 60,
                  uncertain=False)

    print 'adding the map'
    model.map = GnomeMap()  # this is a "water world -- no land anywhere"

    # renderere is only top-down view on 2d -- but it's something
    renderer = Renderer(output_dir=images_dir,
                        image_size=(1024, 768),
                        output_timestep=timedelta(hours=1),
                        )
    renderer.viewport = ((196.14, 71.89), (196.18, 71.93))

    print 'adding outputters'
    model.outputters += renderer

    # Also going to write the results out to a netcdf file
    netcdf_file = os.path.join(base_dir, 'script_arctic_plume.nc')
    scripting.remove_netcdf(netcdf_file)

    model.outputters += NetCDFOutput(netcdf_file,
                                     which_data='most',
                                     # output most of the data associated with the elements
                                     output_timestep=timedelta(hours=2))

    print "adding Horizontal and Vertical diffusion"

    # Horizontal Diffusion
    model.movers += RandomMover(diffusion_coef=500)
    # vertical diffusion (different above and below the mixed layer)
    model.movers += RandomVerticalMover(vertical_diffusion_coef_above_ml=5,
                                        vertical_diffusion_coef_below_ml=.11,
                                        mixed_layer_depth=10)

    print 'adding Rise Velocity'
    # droplets rise as a function of their density and radius
    model.movers += TamocRiseVelocityMover()

    print 'adding a circular current and eastward current'
    fn = 'hycom_glb_regp17_2016092300_subset.nc'
    fn_ice = 'hycom-cice_ARCu0.08_046_2016092300_subset.nc'
    iconc = IceConcentration.from_netCDF(filename=fn_ice)
    ivel = IceVelocity.from_netCDF(filename=fn_ice, grid = iconc.grid)
    ic = IceAwareCurrent.from_netCDF(ice_concentration = iconc, ice_velocity= ivel, filename=fn)

    model.movers += PyCurrentMover(current = ic)
    model.movers += SimpleMover(velocity=(0., 0., 0.))
    model.movers += constant_wind_mover(20, 315, units='knots')

    # Now to add in the TAMOC "spill"
    print "Adding TAMOC spill"

    model.spills += tamoc_spill.TamocSpill(release_time=start_time,
                                        start_position=(196.16, 71.91, 40.0),
                                        num_elements=1000,
                                        end_release_time=start_time + timedelta(days=1),
                                        name='TAMOC plume',
                                        TAMOC_interval=None,  # how often to re-run TAMOC
                                        )

    model.spills[0].data_sources['currents'] = ic

    return model