本文整理汇总了Python中gnome.model.Model类的典型用法代码示例。如果您正苦于以下问题:Python Model类的具体用法?Python Model怎么用?Python Model使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Model类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_simple_run_with_map
def test_simple_run_with_map():
'''
pretty much all this tests is that the model will run
'''
start_time = datetime(2012, 9, 15, 12, 0)
model = Model()
model.map = gnome.map.MapFromBNA(testmap, refloat_halflife=6) # hours
a_mover = SimpleMover(velocity=(1., 2., 0.))
model.movers += a_mover
assert len(model.movers) == 1
spill = point_line_release_spill(num_elements=10,
start_position=(0., 0., 0.),
release_time=start_time)
model.spills += spill
assert len(model.spills) == 1
model.start_time = spill.release.release_time
# test iterator
for step in model:
print 'just ran time step: %s' % step
assert step['step_num'] == model.current_time_step
# reset and run again
model.reset()
# test iterator is repeatable
for step in model:
print 'just ran time step: %s' % step
assert step['step_num'] == model.current_time_step
示例2: make_model
def make_model():
start_time = datetime(2015, 5, 14, 0)
model = Model(time_step=3600*24, # one day
start_time=start_time,
duration=timedelta(days=3),)
model.cache_enabled = False
model.uncertain = False
# N = 10 # a line of ten points
# line_pos = np.zeros((N, 3), dtype=np.float64)
# line_pos[:, 0] = np.linspace(rel_start_pos[0], rel_end_pos[0], N)
# line_pos[:, 1] = np.linspace(rel_start_pos[1], rel_end_pos[1], N)
# start_pos = (-164.01696, 72.921024, 0)
# model.spills += point_line_release_spill(1,
# start_position=start_pos,
# release_time=model.start_time,
# end_position=start_pos)
# release = SpatialRelease(start_position=line_pos,
# release_time=model.start_time)
# model.spills += Spill(release)
c_ice_mover = IceMover(curr_file, topology_file)
model.movers += c_ice_mover
model.outputters += IceImageOutput(c_ice_mover,
viewport=((-175.0, 65.0),
(-145.0, 75.05))
)
return model
示例3: test_load_location_file
def test_load_location_file(self, saveloc_, model):
'''
create a model
load save file from script_boston which contains a spill. Then merge
the created model into the model loaded from save file
'''
m = Model()
m.environment += [Water(), constant_wind(1., 0.)]
m.weatherers += Evaporation(m.environment[0], m.environment[-1])
m.spills += point_line_release_spill(10, (0, 0, 0),
datetime(2014, 1, 1, 12, 0))
# create save model
sample_save_file = os.path.join(saveloc_, 'SampleSaveModel.zip')
model.save(saveloc_, name='SampleSaveModel.zip')
if os.path.exists(sample_save_file):
model = load(sample_save_file)
model.merge(m)
for oc in m._oc_list:
for item in getattr(m, oc):
model_oc = getattr(model, oc)
assert item is model_oc[item.id]
for spill in m.spills:
assert spill is model.spills[spill.id]
# merge the other way and ensure model != m
m.merge(model)
assert model != m
示例4: test_ice_image_mid_run
def test_ice_image_mid_run():
"""
Test image outputter with a model
NOTE: could it be tested with just a mover, and not a full model?
-- that gets tricky with the cache and timesteps...
"""
start_time = datetime(2015, 5, 14, 0)
model = Model(time_step=3600 * 24, start_time=start_time, duration=timedelta(days=3)) # one day
model.cache_enabled = False
model.uncertain = False
c_ice_mover = IceMover(curr_file, topology_file)
model.movers += c_ice_mover
# run the model a couple steps
step = model.step()
step = model.step()
# now add the outputter
model.outputters += IceImageOutput(c_ice_mover, viewport=((-175.0, 65.0), (-145.0, 75.05)))
# and run some more:
step = model.step()
step = model.step()
# and check the output
ice_output = step["IceImageOutput"]
for key in ("time_stamp", "thickness_image", "concentration_image", "bounding_box", "projection"):
assert key in ice_output
print "thickness img size:", len(ice_output["thickness_image"])
print "concentration img size:", len(ice_output["concentration_image"])
示例5: make_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
示例6: make_model
def make_model(images_dir=os.path.join(base_dir, "images")):
print "initializing the model"
# set up the modeling environment
start_time = datetime(2004, 12, 31, 13, 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, size=(1024, 768), output_timestep=timedelta(hours=1))
renderer.viewport = ((-0.15, -0.35), (0.15, 0.35))
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_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=5)
# vertical diffusion (different above and below the mixed layer)
model.movers += RandomVerticalMover(
vertical_diffusion_coef_above_ml=5, vertical_diffusion_coef_below_ml=0.11, mixed_layer_depth=10
)
print "adding Rise Velocity"
# droplets rise as a function of their density and radius
model.movers += RiseVelocityMover()
print "adding a circular current and eastward current"
# This is .3 m/s south
model.movers += PyGridCurrentMover(current=vg, default_num_method="Trapezoid", extrapolate=True)
model.movers += SimpleMover(velocity=(0.0, -0.1, 0.0))
# Now to add in the TAMOC "spill"
print "Adding TAMOC spill"
model.spills += tamoc_spill.TamocSpill(
release_time=start_time,
start_position=(0, 0, 1000),
num_elements=1000,
end_release_time=start_time + timedelta(days=1),
name="TAMOC plume",
TAMOC_interval=None, # how often to re-run TAMOC
)
return model
示例7: test_simple_run_with_image_output_uncertainty
def test_simple_run_with_image_output_uncertainty(tmpdir):
'''
Pretty much all this tests is that the model will run and output images
'''
images_dir = tmpdir.mkdir('Test_images2').strpath
if os.path.isdir(images_dir):
shutil.rmtree(images_dir)
os.mkdir(images_dir)
start_time = datetime(2012, 9, 15, 12, 0)
# the land-water map
gmap = gnome.map.MapFromBNA(testdata['MapFromBNA']['testmap'],
refloat_halflife=6) # hours
renderer = gnome.outputters.Renderer(testdata['MapFromBNA']['testmap'],
images_dir, size=(400, 300))
model = Model(start_time=start_time,
time_step=timedelta(minutes=15), duration=timedelta(hours=1),
map=gmap,
uncertain=True, cache_enabled=False,
)
model.outputters += renderer
a_mover = SimpleMover(velocity=(1., -1., 0.))
model.movers += a_mover
N = 10 # a line of ten points
start_points = np.zeros((N, 3), dtype=np.float64)
start_points[:, 0] = np.linspace(-127.1, -126.5, N)
start_points[:, 1] = np.linspace(47.93, 48.1, N)
# print start_points
release = SpatialRelease(start_position=start_points,
release_time=start_time)
model.spills += Spill(release)
# model.add_spill(spill)
model.start_time = release.release_time
# image_info = model.next_image()
model.uncertain = True
num_steps_output = 0
while True:
try:
image_info = model.step()
num_steps_output += 1
print image_info
except StopIteration:
print 'Done with the model run'
break
# there is the zeroth step, too.
calculated_steps = (model.duration.total_seconds() / model.time_step) + 1
assert num_steps_output == calculated_steps
示例8: make_model
def make_model(images_dir=os.path.join(base_dir, 'images')):
print 'initializing the model'
start_time = datetime(2013, 1, 1, 1) # data starts at 1:00 instead of 0:00
model = Model(start_time=start_time, duration=timedelta(days=1),
time_step=900, uncertain=False)
try:
mapfile = get_datafile(os.path.join(base_dir, './pearl_harbor.bna'))
except HTTPError:
print ('Could not download Pearl Harbor data from server - '
'returning empty model')
return model
print 'adding the map'
model.map = MapFromBNA(mapfile, refloat_halflife=1) # hours
#
# Add the outputters -- render to images, and save out as netCDF
#
print 'adding renderer and netcdf output'
model.outputters += Renderer(mapfile, images_dir, size=(800, 600))
netcdf_output_file = os.path.join(base_dir, 'pearl_harbor_output.nc')
scripting.remove_netcdf(netcdf_output_file)
model.outputters += NetCDFOutput(netcdf_output_file, which_data='all')
# #
# # Set up the movers:
# #
print 'adding a random mover:'
model.movers += RandomMover(diffusion_coef=10000)
print 'adding a wind mover:'
series = np.zeros((3, ), dtype=datetime_value_2d)
series[0] = (start_time, (4, 180))
series[1] = (start_time + timedelta(hours=12), (2, 270))
series[2] = (start_time + timedelta(hours=24), (4, 180))
w_mover = WindMover(Wind(timeseries=series, units='knots'))
model.movers += w_mover
model.environment += w_mover.wind
print 'adding a current mover:'
# this is CH3D currents
curr_file = os.path.join(base_dir, r"./ch3d2013.nc")
topology_file = os.path.join(base_dir, r"./PearlHarborTop.dat")
model.movers += GridCurrentMover(curr_file, topology_file)
# #
# # Add a spill (sources of elements)
# #
print 'adding spill'
model.spills += point_line_release_spill(num_elements=1000,
start_position=(-157.97064,
21.331524, 0.0),
release_time=start_time)
return model
示例9: make_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=5000,
start_position=(-122.625,
45.609,
0.0),
release_time=start_time)
model.spills += spill1
print 'adding a RandomMover:'
model.movers += RandomMover(diffusion_coef=5000)
print 'adding a wind mover:'
# model.movers += constant_wind_mover(8, 90, 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
示例10: sample_model_fixture_base
def sample_model_fixture_base():
"""
sample model with no outputter and no spills. Use this as a template for
fixtures to add spills
Uses:
sample_data/MapBounds_Island.bna
Contains: gnome.movers.SimpleMover(velocity=(1.0, -1.0, 0.0))
duration is 1 hour with 15min intervals so 5 timesteps total,
including initial condition,
model is uncertain and cache is not enabled
No spills or outputters defined
To use:
add a spill and run
:returns: It returns a dict -
{'model':model,
'release_start_pos':start_points,
'release_end_pos':end_points}
The release_start_pos and release_end_pos can be used by test to define
the spill's 'start_position' and 'end_position'
"""
release_time = datetime(2012, 9, 15, 12, 0)
# the image output map
mapfile = os.path.join(os.path.dirname(__file__),
'sample_data',
'MapBounds_Island.bna')
# the land-water map
map_ = MapFromBNA(mapfile, refloat_halflife=06) # seconds
model = Model(time_step=timedelta(minutes=15),
start_time=release_time,
duration=timedelta(hours=1),
map=map_,
uncertain=True,
cache_enabled=False,
)
model.movers += SimpleMover(velocity=(1., -1., 0.0))
model.uncertain = True
start_points = np.zeros((3, ), dtype=np.float64)
end_points = np.zeros((3, ), dtype=np.float64)
start_points[:] = (-127.1, 47.93, 0)
end_points[:] = (-126.5, 48.1, 0)
return {'model': model,
'release_start_pos': start_points,
'release_end_pos': end_points,
}
示例11: test_mover_api
def test_mover_api():
'''
Test the API methods for adding and removing movers to the model.
'''
start_time = datetime(2012, 1, 1, 0, 0)
model = Model()
model.duration = timedelta(hours=12)
model.time_step = timedelta(hours=1)
model.start_time = start_time
mover_1 = SimpleMover(velocity=(1., -1., 0.))
mover_2 = SimpleMover(velocity=(1., -1., 0.))
mover_3 = SimpleMover(velocity=(1., -1., 0.))
mover_4 = SimpleMover(velocity=(1., -1., 0.))
# test our add object methods
model.movers += mover_1
model.movers += mover_2
# test our get object methods
assert model.movers[mover_1.id] == mover_1
assert model.movers[mover_2.id] == mover_2
with raises(KeyError):
temp = model.movers['Invalid']
print temp
# test our iter and len object methods
assert len(model.movers) == 2
assert len([m for m in model.movers]) == 2
for (m1, m2) in zip(model.movers, [mover_1, mover_2]):
assert m1 == m2
# test our add objectlist methods
model.movers += [mover_3, mover_4]
assert [m for m in model.movers] == [mover_1, mover_2, mover_3, mover_4]
# test our remove object methods
del model.movers[mover_3.id]
assert [m for m in model.movers] == [mover_1, mover_2, mover_4]
with raises(KeyError):
# our key should also be gone after the delete
temp = model.movers[mover_3.id]
print temp
# test our replace method
model.movers[mover_2.id] = mover_3
assert [m for m in model.movers] == [mover_1, mover_3, mover_4]
assert model.movers[mover_3.id] == mover_3
with raises(KeyError):
# our key should also be gone after the delete
temp = model.movers[mover_2.id]
print temp
示例12: test_timestep
def test_timestep():
model = Model()
ts = timedelta(hours=1)
model.time_step = ts
assert model.time_step == ts.total_seconds()
dur = timedelta(days=3)
model.duration = dur
assert model._duration == dur
示例13: test_callback_add_mover
def test_callback_add_mover():
'Test callback after add mover'
units = 'meter per second'
model = Model()
model.start_time = datetime(2012, 1, 1, 0, 0)
model.duration = timedelta(hours=10)
model.time_step = timedelta(hours=1)
# start_loc = (1.0, 2.0, 0.0) # random non-zero starting points
# add Movers
model.movers += SimpleMover(velocity=(1., -1., 0.))
series = np.array((model.start_time, (10, 45)),
dtype=datetime_value_2d).reshape((1, ))
model.movers += WindMover(Wind(timeseries=series, units=units))
# this should create a Wind object
new_wind = Wind(timeseries=series, units=units)
model.environment += new_wind
assert new_wind in model.environment
assert len(model.environment) == 2
tide_file = get_datafile(os.path.join(tides_dir, 'CLISShio.txt'))
tide_ = Tide(filename=tide_file)
d_file = get_datafile(os.path.join(lis_dir, 'tidesWAC.CUR'))
model.movers += CatsMover(d_file, tide=tide_)
model.movers += CatsMover(d_file)
for mover in model.movers:
assert mover.active_start == inf_datetime.InfDateTime('-inf')
assert mover.active_stop == inf_datetime.InfDateTime('inf')
if hasattr(mover, 'wind'):
assert mover.wind in model.environment
if hasattr(mover, 'tide'):
if mover.tide is not None:
assert mover.tide in model.environment
# Add a mover with user defined active_start / active_stop values
# - these should not be updated
active_on = model.start_time + timedelta(hours=1)
active_off = model.start_time + timedelta(hours=4)
custom_mover = SimpleMover(velocity=(1., -1., 0.),
active_start=active_on,
active_stop=active_off)
model.movers += custom_mover
assert model.movers[custom_mover.id].active_start == active_on
assert model.movers[custom_mover.id].active_stop == active_off
示例14: make_model
def make_model(images_dir=os.path.join(base_dir, 'images')):
print 'initializing the model'
start_time = datetime(2006, 3, 31, 20, 0)
model = Model(start_time=start_time,
duration=timedelta(days=3), time_step=30 * 60,
uncertain=True)
print 'adding the map'
mapfile = get_datafile(os.path.join(base_dir, './coastSF.bna'))
model.map = MapFromBNA(mapfile, refloat_halflife=1) # seconds
renderer = Renderer(mapfile, images_dir, size=(800, 600),
draw_ontop='forecast')
renderer.viewport = ((-124.5, 37.), (-120.5, 39))
print 'adding outputters'
model.outputters += renderer
netcdf_file = os.path.join(base_dir, 'script_sf_bay.nc')
scripting.remove_netcdf(netcdf_file)
model.outputters += NetCDFOutput(netcdf_file, which_data='all')
print 'adding a spill'
spill = point_line_release_spill(num_elements=1000,
start_position=(-123.57152, 37.369436,
0.0),
release_time=start_time,
element_type=floating(windage_range=(0.01,
0.04)
)
)
model.spills += spill
# print 'adding a RandomMover:'
# r_mover = gnome.movers.RandomMover(diffusion_coef=50000)
# model.movers += r_mover
print 'adding a grid wind mover:'
wind_file = get_datafile(os.path.join(base_dir, r"./WindSpeedDirSubset.nc")
)
topology_file = get_datafile(os.path.join(base_dir,
r"./WindSpeedDirSubsetTop.dat"))
w_mover = GridWindMover(wind_file, topology_file)
#w_mover.uncertain_time_delay = 6
#w_mover.uncertain_duration = 6
w_mover.uncertain_speed_scale = 1
w_mover.uncertain_angle_scale = 0.2 # default is .4
w_mover.wind_scale = 2
model.movers += w_mover
return model
示例15: make_models
def make_models():
print 'initializing the model'
# start_time = datetime(2015, 12, 18, 06, 01)
# 1 day of data in file
# 1/2 hr in seconds
models = []
start_time = datetime(2012, 10, 27, 0, 30)
duration_hrs=23
time_step=450
num_steps = duration_hrs * 3600 / time_step
names = [
'Euler',
'Trapezoid',
'RK4',
]
mapfile = get_datafile(os.path.join(base_dir, 'long_beach.bna'))
print 'gen map'
map = MapFromBNA(mapfile, refloat_halflife=0.0) # seconds
fn = ('00_dir_roms_display.ncml.nc4')
curr = GridCurrent.from_netCDF(filename=fn)
models = []
for method in names:
mod = Model(start_time=start_time,
duration=timedelta(hours=duration_hrs),
time_step=time_step)
mod.map = map
spill = point_line_release_spill(num_elements=1000,
start_position=(-74.1,
39.7525,
0.0),
release_time=start_time)
mod.spills += spill
mod.movers += RandomMover(diffusion_coef=100)
mod.movers += PyGridCurrentMover(current=curr, default_num_method=method)
images_dir = method + '-' + str(time_step / 60) + 'min-' + str(num_steps) + 'steps'
renderer = Renderer(mapfile, images_dir, image_size=(1024, 768))
renderer.delay = 25
# renderer.add_grid(curr.grid)
mod.outputters += renderer
netCDF_fn = os.path.join(base_dir, images_dir + '.nc')
mod.outputters += NetCDFOutput(netCDF_fn, which_data='all')
models.append(mod)
print 'returning models'
return models