def test_exceptions(): """ Test correct exceptions are raised """ bad_file = 'CLISShio.txtX' bad_yeardata_path = os.path.join('Data', 'yeardata') with raises(IOError): Tide(bad_file) with raises(IOError): Tide(shio_file, yeardata=bad_yeardata_path)
def test_serialize_deserialize(filename): ''' create - it creates new object after serializing original object and tests equality of the two update - tests serialize/deserialize and update_from_dict methods don't fail. It doesn't update any properties. ''' tide = Tide(filename) serial = tide.serialize() new_t = Tide.deserialize(serial) assert new_t is not tide assert new_t == tide
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' start_time = datetime(2012, 9, 15, 12, 0) mapfile = get_datafile(os.path.join(base_dir, './LongIslandSoundMap.BNA')) gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours # # the image output renderer # global renderer # one hour timestep model = Model(start_time=start_time, duration=timedelta(hours=48), time_step=3600, map=gnome_map, uncertain=True, cache_enabled=True) netcdf_file = os.path.join(base_dir, 'script_long_island.nc') scripting.remove_netcdf(netcdf_file) print 'adding outputters' model.outputters += Renderer(mapfile, images_dir, size=(800, 600)) model.outputters += NetCDFOutput(netcdf_file, which_data='all') print 'adding a spill' spill = point_line_release_spill(num_elements=1000, start_position=(-72.419992, 41.202120, 0.0), release_time=start_time) model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=500000, uncertain_factor=2) print 'adding a wind mover:' series = np.zeros((5, ), dtype=datetime_value_2d) series[0] = (start_time, (10, 45)) series[1] = (start_time + timedelta(hours=18), (10, 90)) series[2] = (start_time + timedelta(hours=30), (10, 135)) series[3] = (start_time + timedelta(hours=42), (10, 180)) series[4] = (start_time + timedelta(hours=54), (10, 225)) wind = Wind(timeseries=series, units='m/s') model.movers += WindMover(wind) print 'adding a cats mover:' curr_file = get_datafile(os.path.join(base_dir, r"./LI_tidesWAC.CUR")) tide_file = get_datafile(os.path.join(base_dir, r"./CLISShio.txt")) c_mover = CatsMover(curr_file, tide=Tide(tide_file)) model.movers += c_mover model.environment += c_mover.tide print 'viewport is:', [ o.viewport for o in model.outputters if isinstance(o, Renderer) ] return model
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
def test_file(filename): """ (WIP) simply tests that the file loads correctly """ td = Tide(filename) assert td.filename == os.path.split(filename)[1]
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_ = Tide(filename=testdata['CatsMover']['tide']) d_file = testdata['CatsMover']['curr'] 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
def test_serialize_deserialize(filename, json_): ''' create - it creates new object after serializing original object and tests equality of the two update - tests serialize/deserialize and update_from_dict methods don't fail. It doesn't update any properties. ''' tide = Tide(filename) serial = tide.serialize(json_) dict_ = Tide.deserialize(serial) if json_ == 'save': new_t = Tide.new_from_dict(dict_) assert new_t is not tide assert new_t == tide else: tide.update_from_dict(dict_) assert True
def test_serialize_deserialize(filename, json_): """ create - it creates new object after serializing original object and tests equality of the two update - tests serialize/deserialize and update_from_dict methods don't fail. It doesn't update any properties. """ tide = Tide(filename) serial = tide.serialize(json_) dict_ = Tide.deserialize(serial) if json_ == "save": new_t = Tide.new_from_dict(dict_) assert new_t is not tide assert new_t == tide else: tide.update_from_dict(dict_) assert True
def test_serialize_deserialize(filename, do): ''' create - it creates new object after serializing original object and tests equality of the two update - tests serialize/deserialize and from_dict methods don't fail. It doesn't update any properties. ''' tide = Tide(filename) json_ = tide.serialize(do) dict_ = Tide.deserialize(json_) if do == 'create': new_t = Tide.new_from_dict(dict_) assert new_t is not tide assert new_t == tide else: tide.from_dict(dict_) assert True
def make_model(uncertain=False, mode='gnome'): ''' Create a model from the data in sample_data/boston_data It contains: - the GeoProjection - wind mover - random mover - cats shio mover - cats ossm mover - plain cats mover ''' start_time = datetime(2013, 2, 13, 9, 0) model = Model(start_time=start_time, duration=timedelta(days=2), time_step=timedelta(minutes=30).total_seconds(), uncertain=uncertain, map=MapFromBNA(testdata['boston_data']['map'], refloat_halflife=1), mode=mode) print 'adding a spill' start_position = (144.664166, 13.441944, 0.0) end_release_time = start_time + timedelta(hours=6) spill_amount = 1000.0 spill_units = 'kg' model.spills += point_line_release_spill(num_elements=1000, start_position=start_position, release_time=start_time, end_release_time=end_release_time, amount=spill_amount, units=spill_units, substance=test_oil) spill = model.spills[-1] spill_volume = spill.get_mass() / spill.substance.density_at_temp() # need a scenario for SimpleMover # model.movers += SimpleMover(velocity=(1.0, -1.0, 0.0)) print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=100000) print 'adding a wind mover:' series = np.zeros((2, ), dtype=datetime_value_2d) series[0] = (start_time, (5, 180)) series[1] = (start_time + timedelta(hours=18), (5, 180)) w_mover = WindMover(Wind(timeseries=series, units='m/s')) model.movers += w_mover model.environment += w_mover.wind print 'adding a cats shio mover:' c_mover = CatsMover(testdata['boston_data']['cats_curr2'], tide=Tide(testdata['boston_data']['cats_shio'])) # c_mover.scale_refpoint should automatically get set from tide object c_mover.scale = True # default value c_mover.scale_value = -1 # tide object automatically gets added by model model.movers += c_mover print 'adding a cats ossm mover:' c_mover = CatsMover(testdata['boston_data']['cats_curr2'], tide=Tide(testdata['boston_data']['cats_ossm'])) c_mover.scale = True # but do need to scale (based on river stage) c_mover.scale_refpoint = (-70.65, 42.58333, 0.0) c_mover.scale_value = 1. print 'adding a cats mover:' c_mover = CatsMover(testdata['boston_data']['cats_curr3']) c_mover.scale = True # but do need to scale (based on river stage) c_mover.scale_refpoint = (-70.78333, 42.39333, 0.0) # the scale factor is 0 if user inputs no sewage outfall effects c_mover.scale_value = .04 model.movers += c_mover # TODO: seg faulting for component mover - comment test for now # print "adding a component mover:" # comp_mover = ComponentMover(testdata['boston_data']['component_curr1'], # testdata['boston_data']['component_curr2'], # w_mover.wind) # TODO: callback did not work correctly below - fix! # comp_mover = ComponentMover(component_file1, # component_file2, # Wind(timeseries=series, units='m/s')) # comp_mover.ref_point = (-70.855, 42.275) # comp_mover.pat1_angle = 315 # comp_mover.pat1_speed = 19.44 # comp_mover.pat1_speed_units = 1 # comp_mover.pat1ScaleToValue = .138855 # comp_mover.pat2_angle = 225 # comp_mover.pat2_speed = 19.44 # comp_mover.pat2_speed_units = 1 # comp_mover.pat2ScaleToValue = .05121 # model.movers += comp_mover print 'adding a Weatherer' model.environment += Water(311.15) skim_start = start_time + timedelta(hours=3) model.weatherers += [ Evaporation(), Skimmer(spill_amount * .5, spill_units, efficiency=.3, active_range=(skim_start, skim_start + timedelta(hours=2))), Burn(0.2 * spill_volume, 1.0, (skim_start, InfDateTime('inf')), efficiency=0.9) ] model.outputters += \ CurrentJsonOutput(model.find_by_attr('_ref_as', 'current_movers', model.movers, allitems=True)) return model
import datetime import os import numpy as np import pytest from gnome.movers import CurrentCycleMover from gnome.environment import Tide from gnome.utilities import time_utils from ..conftest import sample_sc_release, testdata curr_file = testdata['CurrentCycleMover']['curr'] topology_file = testdata['CurrentCycleMover']['top'] td = Tide(filename=testdata['CurrentCycleMover']['tide']) def test_exceptions(): """ Test correct exceptions are raised """ with pytest.raises(ValueError): 'file does not exis' CurrentCycleMover(os.path.join('./', 'ChesBay.CUR')) with pytest.raises(OSError): CurrentCycleMover(testdata['CurrentCycleMover']['curr_bad_file']) with pytest.raises(TypeError):
def make_model(images_dir=os.path.join(base_dir, 'images')): # create the maps: print 'creating the maps' mapfile = get_datafile(os.path.join(base_dir, 'DelawareRiverMap.bna')) gnome_map = MapFromBNA(mapfile, refloat_halflife=1) # hours renderer = Renderer(mapfile, images_dir, image_size=(800, 800), projection_class=GeoProjection) print 'initializing the model' start_time = datetime(2012, 8, 20, 13, 0) # 15 minutes in seconds # Default to now, rounded to the nearest hour model = Model(time_step=900, start_time=start_time, duration=timedelta(days=1), map=gnome_map, uncertain=False) print 'adding outputters' model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_delaware_bay.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all') print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=100000) print 'adding a wind mover:' # wind_file = get_datafile(os.path.join(base_dir, 'ConstantWind.WND')) # wind = Wind(filename=wind_file) series = np.zeros((2, ), dtype=datetime_value_2d) series[0] = (start_time, (5, 270)) series[1] = (start_time + timedelta(hours=25), (5, 270)) wind = Wind(timeseries=series, units='m/s') # w_mover = WindMover(Wind(timeseries=series, units='knots')) w_mover = WindMover(wind) model.movers += w_mover print 'adding a cats shio mover:' curr_file = get_datafile(os.path.join(base_dir, 'FloodTides.cur')) tide_file = get_datafile(os.path.join(base_dir, 'FloodTidesShio.txt')) c_mover = CatsMover(curr_file, tide=Tide(tide_file)) # this is the value in the file (default) c_mover.scale_refpoint = (-75.081667, 38.7995) c_mover.scale = True c_mover.scale_value = 1 model.movers += c_mover # TODO: cannot add this till environment base class is created model.environment += c_mover.tide print 'adding a cats mover:' curr_file = get_datafile(os.path.join(base_dir, 'Offshore.cur')) c_mover = CatsMover(curr_file) # but do need to scale (based on river stage) c_mover.scale = True c_mover.scale_refpoint = (-74.7483333, 38.898333) c_mover.scale_value = .03 model.movers += c_mover # # these are from windows they don't match Mac values... # pat1Angle 315; # pat1Speed 30; pat1SpeedUnits knots; # pat1ScaleToValue 0.314426 # # pat2Angle 225; # pat2Speed 30; pat2SpeedUnits knots; # pat2ScaleToValue 0.032882 # scaleBy WindStress print 'adding a component mover:' # if only using one current pattern # comp_mover = ComponentMover(curr_file1, None, wind) # # todo: following is not working when model is saved out - fix # comp_mover = ComponentMover(curr_file1, curr_file2, # Wind(timeseries=series, units='m/s')) # comp_mover = ComponentMover(curr_file1, curr_file2, # wind=Wind(filename=wind_file)) curr_file1 = get_datafile(os.path.join(base_dir, 'NW30ktwinds.cur')) curr_file2 = get_datafile(os.path.join(base_dir, 'SW30ktwinds.cur')) comp_mover = ComponentMover(curr_file1, curr_file2, wind) comp_mover.scale_refpoint = (-75.263166, 39.1428333) comp_mover.pat1_angle = 315 comp_mover.pat1_speed = 30 comp_mover.pat1_speed_units = 1 # comp_mover.pat1ScaleToValue = .314426 comp_mover.pat1_scale_to_value = .502035 comp_mover.pat2_angle = 225 comp_mover.pat2_speed = 30 comp_mover.pat2_speed_units = 1 # comp_mover.pat2ScaleToValue = .032882 comp_mover.pat2_scale_to_value = .021869 model.movers += comp_mover print 'adding a spill' end_time = start_time + timedelta(hours=12) spill = point_line_release_spill(num_elements=1000, release_time=start_time, # end_release_time=end_time, start_position=(-75.262319, 39.142987, 0.0), ) model.spills += spill return model
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' start_time = datetime(2012, 9, 15, 12, 0) mapfile = get_datafile(os.path.join(base_dir, './LongIslandSoundMap.BNA')) gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours # # the image output renderer # global renderer # one hour timestep model = Model(start_time=start_time, duration=timedelta(hours=48), time_step=3600, map=gnome_map, uncertain=False, cache_enabled=False) print 'adding a spill' et = floating_weathering(substance='FUEL OIL NO.6') spill = point_line_release_spill(num_elements=1000, start_position=(-72.419992, 41.202120, 0.0), release_time=start_time, amount=1000, units='kg', element_type=et) spill.amount_uncertainty_scale = 1.0 model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=500000, uncertain_factor=2) print 'adding a wind mover:' series = np.zeros((5, ), dtype=datetime_value_2d) series[0] = (start_time, (10, 45)) series[1] = (start_time + timedelta(hours=18), (10, 90)) series[2] = (start_time + timedelta(hours=30), (10, 135)) series[3] = (start_time + timedelta(hours=42), (10, 180)) series[4] = (start_time + timedelta(hours=54), (10, 225)) wind = Wind(timeseries=series, units='m/s', speed_uncertainty_scale=0.5) model.movers += WindMover(wind) print 'adding a cats mover:' curr_file = get_datafile(os.path.join(base_dir, r"./LI_tidesWAC.CUR")) tide_file = get_datafile(os.path.join(base_dir, r"./CLISShio.txt")) c_mover = CatsMover(curr_file, tide=Tide(tide_file)) model.movers += c_mover model.environment += c_mover.tide print 'adding Weatherers' water_env = Water(311.15) model.environment += water_env model.weatherers += [Evaporation(water_env, wind), Dispersion(), Burn(), Skimmer()] print 'adding outputters' model.outputters += WeatheringOutput() return model
from datetime import datetime import numpy as np import pytest # from gnome.basic_types import oil_status from gnome.utilities import time_utils from gnome.environment import Tide from gnome.spill import SpatialRelease, Spill, point_line_release_spill from gnome.movers import CatsMover from gnome.outputters import CurrentJsonOutput from ..conftest import testdata td = Tide(filename=testdata['CatsMover']['tide']) c_cats = CatsMover(testdata['CatsMover']['curr'], tide=td) rel_time = datetime(2012, 9, 15, 12) time_step = 15 * 60 # seconds model_time = time_utils.sec_to_date(time_utils.date_to_sec(rel_time)) @pytest.fixture(scope='function') def model(sample_model, output_dir): model = sample_model['model'] rel_start_pos = sample_model['release_start_pos'] rel_end_pos = sample_model['release_end_pos'] model.cache_enabled = True
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' start_time = datetime(2014, 6, 9, 0, 0) mapfile = get_datafile(os.path.join(base_dir, 'PassamaquoddyMap.bna')) gnome_map = MapFromBNA(mapfile, refloat_halflife=1) # hours # # the image output renderer # global renderer # one hour timestep model = Model(start_time=start_time, duration=timedelta(hours=24), time_step=360, map=gnome_map, uncertain=False, cache_enabled=True) print 'adding outputters' renderer = Renderer(mapfile, images_dir, size=(800, 600), # output_timestep=timedelta(hours=1), draw_ontop='uncertain') renderer.viewport = ((-67.15, 45.), (-66.9, 45.2)) model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_passamaquoddy.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=(-66.991344, 45.059316, 0.0), release_time=start_time) model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=30000, uncertain_factor=2) print 'adding a wind mover:' series = np.zeros((5, ), dtype=datetime_value_2d) series[0] = (start_time, (5, 90)) series[1] = (start_time + timedelta(hours=18), (5, 180)) series[2] = (start_time + timedelta(hours=30), (5, 135)) series[3] = (start_time + timedelta(hours=42), (5, 180)) series[4] = (start_time + timedelta(hours=54), (5, 225)) wind = Wind(timeseries=series, units='m/s') model.movers += WindMover(wind) print 'adding a current mover:' curr_file = get_datafile(os.path.join(base_dir, 'PQBayCur.nc4')) topology_file = get_datafile(os.path.join(base_dir, 'PassamaquoddyTOP.dat') ) tide_file = get_datafile(os.path.join(base_dir, 'EstesHead.txt')) cc_mover = CurrentCycleMover(curr_file, topology_file, tide=Tide(tide_file)) model.movers += cc_mover model.environment += cc_mover.tide print 'viewport is:', [o.viewport for o in model.outputters if isinstance(o, Renderer)] return model
def make_model(images_dir=os.path.join(base_dir, 'images')): # create the maps: print 'creating the maps' mapfile = get_datafile(os.path.join(base_dir, './MassBayMap.bna')) gnome_map = MapFromBNA(mapfile, refloat_halflife=1) # hours renderer = Renderer(mapfile, images_dir, size=(800, 800), projection_class=GeoProjection) print 'initializing the model' start_time = datetime(2013, 3, 12, 10, 0) # 15 minutes in seconds # Default to now, rounded to the nearest hour model = Model(time_step=900, start_time=start_time, duration=timedelta(days=1), map=gnome_map, uncertain=False) print 'adding outputters' model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_boston.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all') print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=100000) print 'adding a wind mover:' series = np.zeros((2, ), dtype=datetime_value_2d) series[0] = (start_time, (5, 180)) series[1] = (start_time + timedelta(hours=18), (5, 180)) w_mover = WindMover(Wind(timeseries=series, units='m/s')) model.movers += w_mover model.environment += w_mover.wind print 'adding a cats shio mover:' curr_file = get_datafile(os.path.join(base_dir, r"./EbbTides.cur")) tide_file = get_datafile(os.path.join(base_dir, r"./EbbTidesShio.txt")) c_mover = CatsMover(curr_file, tide=Tide(tide_file)) # this is the value in the file (default) c_mover.scale_refpoint = (-70.8875, 42.321333) c_mover.scale = True c_mover.scale_value = -1 model.movers += c_mover # TODO: cannot add this till environment base class is created model.environment += c_mover.tide print 'adding a cats ossm mover:' # ossm_file = get_datafile(os.path.join(base_dir, # r"./MerrimackMassCoastOSSM.txt")) curr_file = get_datafile( os.path.join(base_dir, r"./MerrimackMassCoast.cur")) tide_file = get_datafile( os.path.join(base_dir, r"./MerrimackMassCoastOSSM.txt")) c_mover = CatsMover(curr_file, tide=Tide(tide_file)) # but do need to scale (based on river stage) c_mover.scale = True c_mover.scale_refpoint = (-70.65, 42.58333) c_mover.scale_value = 1. model.movers += c_mover model.environment += c_mover.tide print 'adding a cats mover:' curr_file = get_datafile(os.path.join(base_dir, r"MassBaySewage.cur")) c_mover = CatsMover(curr_file) # but do need to scale (based on river stage) c_mover.scale = True c_mover.scale_refpoint = (-70.78333, 42.39333) # the scale factor is 0 if user inputs no sewage outfall effects c_mover.scale_value = .04 model.movers += c_mover # pat1Angle 315; # pat1Speed 19.44; pat1SpeedUnits knots; # pat1ScaleToValue 0.138855 # # pat2Angle 225; # pat2Speed 19.44; pat2SpeedUnits knots; # pat2ScaleToValue 0.05121 # # scaleBy WindStress print "adding a component mover:" component_file1 = get_datafile(os.path.join(base_dir, r"./WAC10msNW.cur")) component_file2 = get_datafile(os.path.join(base_dir, r"./WAC10msSW.cur")) comp_mover = ComponentMover(component_file1, component_file2, w_mover.wind) # todo: callback did not work correctly below - fix! # comp_mover = ComponentMover(component_file1, # component_file2, # Wind(timeseries=series, units='m/s')) comp_mover.scale_refpoint = (-70.855, 42.275) comp_mover.pat1_angle = 315 comp_mover.pat1_speed = 19.44 comp_mover.pat1_speed_units = 1 comp_mover.pat1ScaleToValue = .138855 comp_mover.pat2_angle = 225 comp_mover.pat2_speed = 19.44 comp_mover.pat2_speed_units = 1 comp_mover.pat2ScaleToValue = .05121 model.movers += comp_mover print 'adding a spill' end_time = start_time + timedelta(hours=12) spill = point_line_release_spill(num_elements=1000, start_position=(-70.911432, 42.369142, 0.0), release_time=start_time, end_release_time=end_time) model.spills += spill return model
unknown = constant_wind_mover(0, 0) assert unknown not in refs # check __contains__ ''' Run the following save/load test on multiple pygnome objects so collect tests here and parametrize it by the objects ''' base_dir = os.path.dirname(__file__) # For WindMover test_save_load in test_wind_mover g_objects = ( GridCurrent.from_netCDF(testdata['GridCurrentMover']['curr_tri']), Tide(testdata['CatsMover']['tide']), Wind(filename=testdata['ComponentMover']['wind']), Wind(timeseries=(sec_to_date(24 * 60 * 60), (0, 0)), units='mps'), Water(temperature=273), RandomMover(), CatsMover(testdata['CatsMover']['curr']), CatsMover(testdata['CatsMover']['curr'], tide=Tide(testdata['CatsMover']['tide'])), ComponentMover(testdata['ComponentMover']['curr']), ComponentMover(testdata['ComponentMover']['curr'], wind=Wind(filename=testdata['ComponentMover']['wind'])), RandomMover3D(), SimpleMover(velocity=(10.0, 10.0, 0.0)), map.MapFromBNA(testdata['MapFromBNA']['testmap'], 6), NetCDFOutput(os.path.join(base_dir, u'xtemp.nc')), Renderer(testdata['Renderer']['bna_sample'],
def make_model(uncertain=False, geojson_output=False): print 'initializing the model' start_time = datetime(2012, 9, 15, 12, 0) mapfile = testdata["lis"]["map"] gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours # # the image output renderer # global renderer # one hour timestep model = Model(start_time=start_time, duration=timedelta(hours=48), time_step=3600, map=gnome_map, uncertain=uncertain, cache_enabled=False) print 'adding a spill' spill = point_line_release_spill(num_elements=1000, start_position=(-72.419992, 41.202120, 0.0), release_time=start_time, amount=1000, substance=test_oil, units='kg') spill.amount_uncertainty_scale = 1.0 model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=500000, uncertain_factor=2) print 'adding a wind mover:' series = np.zeros((5, ), dtype=datetime_value_2d) series[0] = (start_time, (20, 45)) series[1] = (start_time + timedelta(hours=18), (20, 90)) series[2] = (start_time + timedelta(hours=30), (20, 135)) series[3] = (start_time + timedelta(hours=42), (20, 180)) series[4] = (start_time + timedelta(hours=54), (20, 225)) wind = Wind(timeseries=series, units='m/s', speed_uncertainty_scale=0.05) model.movers += WindMover(wind) print 'adding a cats mover:' c_mover = CatsMover(testdata["lis"]["cats_curr"], tide=Tide(testdata["lis"]["cats_tide"])) model.movers += c_mover model.environment += c_mover.tide print 'adding Weatherers' rel_time = model.spills[0].get('release_time') skim_start = rel_time + timedelta(hours=4) amount = spill.amount units = spill.units # define skimmer/burn cleanup options skimmer = Skimmer(0.3 * amount, units=units, efficiency=0.3, active_start=skim_start, active_stop=skim_start + timedelta(hours=4)) # thickness = 1m so area is just 20% of volume volume = spill.get_mass() / spill.get('substance').get_density() burn = Burn(0.2 * volume, 1.0, active_start=skim_start, efficiency=.9) c_disp = ChemicalDispersion(0.1, efficiency=0.5, active_start=skim_start, active_stop=skim_start + timedelta(hours=1)) water_env = Water(311.15) model.environment += water_env model.weatherers += [Evaporation(water_env, wind), c_disp, burn, skimmer] print 'adding outputters' model.outputters += WeatheringOutput() if geojson_output: model.outputters += TrajectoryGeoJsonOutput() return model
def main(): """ Configure a model with the parameters of the Boston script. """ env = bootstrap('../development.ini') settings = env['registry'].settings location_file_dir = settings.location_file_dir boston_dir = os.path.join(location_file_dir, 'boston') boston_data = os.path.join(boston_dir, 'data') location_file = os.path.join(boston_dir, 'location.json') if os.path.exists(location_file): message = 'File already exists:\n %s\nRemove? (y/n) ' % location_file if raw_input(message).lower() == 'y': os.unlink(location_file) else: print 'Cancelled.' return model = settings.Model.create() # Map file path is relative to package root map_file = os.path.join('location_files', 'boston', 'data', 'MassBayMap.bna') model.add_bna_map(map_file, { 'refloat_halflife': 1 * 3600, # seconds }) start_time = datetime.datetime(2013, 3, 12, 10, 0) model.time_step = 900 model.start_time = start_time model.duration = datetime.timedelta(days=1) model.uncertain = False model.movers += gnome.movers.RandomMover(diffusion_coef=100000) # adding a wind mover series = numpy.zeros((2,), dtype=gnome.basic_types.datetime_value_2d) series[0] = (start_time, (5, 180)) series[1] = (start_time + datetime.timedelta(hours=18), (5, 180)) wind = WebWind(timeseries=series, units='m/s') model.environment += wind w_mover = WebWindMover(wind) model.movers += w_mover # adding a random mover random_mover = WebRandomMover(diffusion_coef=100000) model.movers += random_mover # adding a cats shio mover: curr_file = os.path.join(boston_data, "EbbTides.CUR") shio_file = os.path.join(boston_data, "EbbTidesShio.txt") c_mover = WebCatsMover(curr_file, tide=Tide(shio_file)) c_mover.scale_refpoint = ( -70.8875, 42.321333) # this is the value in the file (default) c_mover.scale = True # default value c_mover.scale_value = -1 model.movers += c_mover # todo: cannot add this till environment base class is created model.environment += c_mover.tide # adding a cats ossm mover ossm_file = os.path.join(boston_data, 'MerrimackMassCoastOSSM.txt') curr_file = os.path.join(boston_data, 'MerrimackMassCoast.CUR') c_mover = WebCatsMover(curr_file, tide=Tide(ossm_file)) # but do need to scale (based on river stage) c_mover.scale = True c_mover.scale_refpoint = (-70.65, 42.58333) c_mover.scale_value = 1. model.movers += c_mover model.environment += c_mover.tide # adding a cats mover curr_file = os.path.join(boston_data, 'MassBaySewage.CUR') c_mover = WebCatsMover(curr_file) # but do need to scale (based on river stage) c_mover.scale = True c_mover.scale_refpoint = (-70.78333, 42.39333) # the scale factor is 0 if user inputs no sewage outfall effects c_mover.scale_value = .04 model.movers += c_mover # adding a spill spill = WebPointSourceRelease(num_elements=1000, start_position=( -70.911432, 42.369142, 0.0), release_time=start_time) model.spills += spill serialized_model = ModelSchema().bind().serialize(model.to_dict()) model_json = json.dumps(serialized_model, default=util.json_encoder, indent=4) with open(location_file, 'wb') as f: f.write(model_json) env['closer']()
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' start_time = datetime(2013, 2, 13, 9, 0) # 1/2 hr in seconds model = Model(start_time=start_time, duration=timedelta(days=2), time_step=30 * 60, uncertain=False) print 'adding the map' mapfile = get_datafile(os.path.join(base_dir, 'GuamMap.bna')) model.map = MapFromBNA(mapfile, refloat_halflife=6) # hours print 'adding outputters' renderer = Renderer(mapfile, images_dir, image_size=(800, 600)) renderer.viewport = ((144.6, 13.4), (144.7, 13.5)) model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_guam.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all') print 'adding a spill' end_time = start_time + timedelta(hours=6) spill = point_line_release_spill(num_elements=10, start_position=(144.664166, 13.441944, 0.0), release_time=start_time, end_release_time=end_time) model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=50000) print 'adding a wind mover:' series = np.zeros((4, ), dtype=datetime_value_2d) series[0] = (start_time, (5, 135)) series[1] = (start_time + timedelta(hours=23), (5, 135)) series[2] = (start_time + timedelta(hours=25), (5, 0)) series[3] = (start_time + timedelta(hours=48), (5, 0)) wind = Wind(timeseries=series, units='knot') w_mover = WindMover(wind) model.movers += w_mover model.environment += w_mover.wind print 'adding a cats mover:' curr_file = get_datafile(os.path.join(base_dir, 'OutsideWAC.cur')) c_mover = CatsMover(curr_file) c_mover.scale = True c_mover.scale_refpoint = (144.601, 13.42) c_mover.scale_value = .15 model.movers += c_mover print 'adding a cats shio mover:' curr_file = get_datafile(os.path.join(base_dir, 'WACFloodTide.cur')) tide_file = get_datafile(os.path.join(base_dir, 'WACFTideShioHts.txt')) c_mover = CatsMover(curr_file, tide=Tide(tide_file)) # this is different from the value in the file! c_mover.scale_refpoint = (144.621667, 13.45) c_mover.scale = True c_mover.scale_value = 1 # will need the fScaleFactor for heights files # c_mover.time_dep.scale_factor = 1.1864 c_mover.tide.scale_factor = 1.1864 model.movers += c_mover model.environment += c_mover.tide return model
def make_model(images_dir=os.path.join(base_dir, 'images')): # create the maps: print 'creating the maps' mapfile = get_datafile(os.path.join(base_dir, 'SanJuanMap.bna')) gnome_map = MapFromBNA(mapfile, refloat_halflife=1, raster_size=1024 * 1024) renderer = Renderer(mapfile, images_dir, image_size=(800, 800), projection_class=GeoProjection) renderer.viewport = ((-66.24, 18.39), (-66.1, 18.55)) print 'initializing the model' start_time = datetime(2014, 9, 3, 13, 0) # 15 minutes in seconds # Default to now, rounded to the nearest hour model = Model(time_step=900, start_time=start_time, duration=timedelta(days=1), map=gnome_map, uncertain=False) print 'adding outputters' model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_san_juan.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all') print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=100000) print 'adding a wind mover:' series = np.zeros((2, ), dtype=datetime_value_2d) series[0] = (start_time, (0, 270)) series[1] = (start_time + timedelta(hours=18), (0, 270)) wind = Wind(timeseries=series, units='m/s') w_mover = WindMover(wind) wind.extrapolation_is_allowed = True model.movers += w_mover print 'adding a cats shio mover:' # need to add the scale_factor for the tide heights file curr_file = get_datafile(os.path.join(base_dir, 'EbbTides.cur')) tide_file = get_datafile(os.path.join(base_dir, 'EbbTidesShioHt.txt')) c_mover = CatsMover(curr_file, tide=Tide(tide_file, scale_factor=.15)) # this is the value in the file (default) c_mover.scale_refpoint = (-66.116667, 18.458333) c_mover.scale = True c_mover.scale_value = 1.0 # c_mover.tide.scale_factor = 0.15 model.movers += c_mover print 'adding a cats mover:' curr_file = get_datafile(os.path.join(base_dir, 'Offshore.cur')) c_mover = CatsMover(curr_file) # this is the value in the file (default) # c_mover.scale_refpoint = (-66.082836, 18.469334) c_mover.scale_refpoint = (-66.084333333, 18.46966667) c_mover.scale = True c_mover.scale_value = 0.1 model.movers += c_mover print 'adding a spill' end_time = start_time + timedelta(hours=12) spill = point_line_release_spill( num_elements=1000, release_time=start_time, start_position=(-66.16374, 18.468054, 0.0), # start_position=(-66.129099, # 18.465332, 0.0), # end_release_time=end_time, ) model.spills += spill return model