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_init_timeseries(): 'test init succeeds via different methods' constant = (datetime(2014, 6, 25, 10, 14), (0, 0)) ts = [constant, (datetime(2014, 6, 25, 10, 20), (1, 1))] Wind(timeseries=constant, units='mps') Wind(timeseries=ts, units='mps') Wind(timeseries=np.asarray(constant, dtype=datetime_value_2d), units='mps') Wind(timeseries=np.asarray(ts, dtype=datetime_value_2d), units='mps') with raises(TimeseriesError): Wind(timeseries=(1, 2), units='mps')
def test_variable_wind_after_model_time(self): ''' test to make sure the wind mover is behaving properly with out-of-bounds winds. A variable wind should not extrapolate if it is out of bounds, so prepare_for_model_step() should fail with an exception in this case. ''' wind_time = datetime(2012, 8, 21, 13) # one day after model time time_series = (np.zeros((3, ), dtype=datetime_value_2d) .view(dtype=np.recarray)) time_series.time = [sec_to_date(date_to_sec(wind_time) + self.time_step * i) for i in range(3)] time_series.value = np.array(((2., 25.), (2., 25.), (2., 25.))) wind = Wind(timeseries=time_series.reshape(3), units='meter per second') wm = WindMover(wind) wm.prepare_for_model_run() for ix in range(2): curr_time = sec_to_date(date_to_sec(self.model_time) + self.time_step * ix) with raises(RuntimeError): wm.prepare_for_model_step(self.sc, self.time_step, curr_time)
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:' c_mover = GridCurrentMover(filename=setup.curr_fn, extrapolate=True) model.movers += c_mover print 'adding a WindMover:' w = Wind(filename=setup.wind_fn) w_mover = WindMover(w) # 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_av_from_variable_wind(): ''' test variable wind to running average ''' # ts = np.zeros((4,), dtype=datetime_value_2d) # ts[:] = [(datetime(2012, 9, 7, 8, 0), (10, 270)), # (datetime(2012, 9, 7, 14, 0), (28, 270)), # (datetime(2012, 9, 7, 20, 0), (28, 270)), # (datetime(2012, 9, 8, 02, 0), (10, 270))] # wm = Wind(timeseries=ts, units='m/s') wm = Wind(timeseries=[(datetime(2012, 9, 7, 8, 0), (10, 270)), (datetime(2012, 9, 7, 14, 0), (28, 270)), (datetime(2012, 9, 7, 20, 0), (28, 270)), (datetime(2012, 9, 8, 02, 0), (10, 270))], units='m/s') # wm = Wind(filename=wind_file) av = RunningAverage(wm) # print "wm.ossm.timeseries" # print wm.ossm.timeseries[:] # print "av.ossm.timeseries" # print av.ossm.timeseries[:] assert av.ossm.timeseries['time'][0] == wm.ossm.timeseries['time'][0] assert av.ossm.timeseries['value']['u'][0] == wm.ossm.timeseries['value'][ 'u'][0] assert av.ossm.timeseries['value']['u'][1] == 10.5
def test_constant_wind_after_model_time(self): ''' test to make sure the wind mover is behaving properly with out-of-bounds winds. A constant wind should extrapolate if it is out of bounds, so prepare_for_model_step() should not fail. We are testing that the wind extrapolates properly, so the windages should be updated in the same way as the in-bounds test ''' wind_time = datetime(2012, 8, 21, 13) # one day after model time wind = Wind(timeseries=np.array((wind_time, (2., 25.)), dtype=datetime_value_2d).reshape(1), units='meter per second') wm = WindMover(wind) wm.prepare_for_model_run() for ix in range(2): curr_time = sec_to_date(date_to_sec(self.model_time) + self.time_step * ix) print 'curr_time = ', curr_time old_windages = np.copy(self.sc['windages']) wm.prepare_for_model_step(self.sc, self.time_step, curr_time) mask = self.sc['windage_persist'] == -1 assert np.all(self.sc['windages'][mask] == old_windages[mask]) mask = self.sc['windage_persist'] > 0 assert np.all(self.sc['windages'][mask] != old_windages[mask])
def test_active(): """ test that mover must be both active and on to get movement """ time_step = 15 * 60 # seconds start_pos = (3., 6., 0.) rel_time = datetime(2012, 8, 20, 13) # yyyy/month/day/hr/min/sec sc = sample_sc_release(5, start_pos, rel_time) # value is given as (r,theta) time_val = np.zeros((1, ), dtype=datetime_value_2d) time_val['time'] = rel_time time_val['value'] = (2., 25.) wm = WindMover(Wind(timeseries=time_val, units='meter per second'), on=False) wm.prepare_for_model_run() wm.prepare_for_model_step(sc, time_step, rel_time) delta = wm.get_move(sc, time_step, rel_time) wm.model_step_is_done() assert wm.active is False assert np.all(delta == 0) # model_time + time_step = active_start
def test_prepare_for_model_step(): """ explicitly test to make sure windages are being updated for persistence != 0 and windages are not being changed for persistance == -1 """ time_step = 15 * 60 # seconds model_time = datetime(2012, 8, 20, 13) # yyyy/month/day/hr/min/sec sc = sample_sc_release(5, (3., 6., 0.), model_time) sc['windage_persist'][:2] = -1 wind = Wind(timeseries=np.array((model_time, (2., 25.)), dtype=datetime_value_2d).reshape(1), units='meter per second') wm = WindMover(wind) wm.prepare_for_model_run() for ix in range(2): curr_time = sec_to_date(date_to_sec(model_time) + time_step * ix) old_windages = np.copy(sc['windages']) wm.prepare_for_model_step(sc, time_step, curr_time) mask = [sc['windage_persist'] == -1] assert np.all(sc['windages'][mask] == old_windages[mask]) mask = [sc['windage_persist'] > 0] assert np.all(sc['windages'][mask] != old_windages[mask])
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 test_default_init(): wind = Wind() assert wind.timeseries == np.array( [(sec_to_date(zero_time()), [0.0, 0.0])], dtype=datetime_value_2d) assert wind.units == 'mps'
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'creating the maps' mapfile = get_datafile(os.path.join(base_dir, 'LowerMississippiMap.bna')) gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours print 'initializing the model' start_time = datetime(2012, 9, 15, 12, 0) # default to now, rounded to the nearest hour model = Model(time_step=600, start_time=start_time, duration=timedelta(days=1), map=gnome_map, uncertain=True) print 'adding outputters' model.outputters += Renderer(mapfile, images_dir, image_size=(800, 600)) netcdf_file = os.path.join(base_dir, 'script_lower_mississippi.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all') print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=10000) print 'adding a wind mover:' series = np.zeros((5, ), dtype=datetime_value_2d) series[0] = (start_time, (2, 45)) series[1] = (start_time + timedelta(hours=18), (2, 90)) series[2] = (start_time + timedelta(hours=30), (2, 135)) series[3] = (start_time + timedelta(hours=42), (2, 180)) series[4] = (start_time + timedelta(hours=54), (2, 225)) w_mover = WindMover(Wind(timeseries=series, units='m/s')) model.movers += w_mover print 'adding a cats mover:' curr_file = get_datafile(os.path.join(base_dir, 'LMiss.CUR')) c_mover = CatsMover(curr_file) # but do need to scale (based on river stage) c_mover.scale = True c_mover.scale_refpoint = (-89.699944, 29.494558) # based on stage height 10ft (range is 0-18) c_mover.scale_value = 1.027154 model.movers += c_mover print 'adding a spill' spill = point_line_release_spill(num_elements=1000, start_position=(-89.699944, 29.494558, 0.0), release_time=start_time) model.spills += spill return model
def test_init_units(self, all_winds): """ check default wind object is created Also check that init doesn't fail if timeseries given in (u,v) format """ Wind(timeseries=all_winds['uv'], format='uv', units='meter per second') assert True
def test_read_file_init(): """ initialize from a long wind file """ wm = Wind(filename=wind_file) # have to test something: assert wm.units == 'knots'
def test_read_file_init(): """ initialize from a long wind file """ wm = Wind(filename=wind_file) print print '----------------------------------' print 'Units: ' + str(wm.units) assert True
def test_units(): """ just make sure there are no errors """ wm = Wind(filename=wind_file) new_units = 'meter per second' assert wm.units != new_units wm.units = new_units assert wm.units == new_units
def test_past_hours_to_average(): """ just make sure there are no errors """ wm = Wind(filename=wind_file) av = RunningAverage(wm) assert av.past_hours_to_average == 3 av.past_hours_to_average = 6 assert av.past_hours_to_average == 6
def test_array_types(): """ Check the array_types property of WindMover contains array_types.WindMover """ # WindMover does not modify Wind object! wm = WindMover(Wind(filename=file_)) for t in ('windages', 'windage_range', 'windage_persist'): assert t in wm.array_types
def test_eq(): """ tests the filename is not used for testing equality even if filename changes but other attributes are the same, the objects are equal """ w = Wind(filename=wind_file) w1 = Wind(filename=wind_file) assert w == w1 p, f = os.path.split(wind_file) f, e = os.path.splitext(f) w_copy = os.path.join(p, '{0}_copy{1}'.format(f, e)) shutil.copy(wind_file, w_copy) w2 = Wind(filename=w_copy) w2.updated_at = w.updated_at # match these before testing assert w == w2
def test_timeseries_res_sec(): '''check the timeseries resolution is changed to minutes. Drop seconds from datetime, if given''' ts = np.zeros((3, ), dtype=datetime_value_2d) ts[:] = [(datetime(2014, 1, 1, 10, 10, 30), (1, 10)), (datetime(2014, 1, 1, 11, 10, 10), (2, 10)), (datetime(2014, 1, 1, 12, 10), (3, 10))] w = Wind(timeseries=ts, units='m/s') # check that seconds resolution has been dropped for ix, dt in enumerate(w.timeseries['time'].astype(datetime)): assert ts['time'][ix].astype(datetime).replace(second=0) == dt
def test_peak_wave_period(wind_speed, expected): "fully developed seas" series = np.array((start_time, (wind_speed, 45)), dtype=datetime_value_2d).reshape((1, )) test_wind = Wind(timeseries=series, units='meter per second') w = Waves(test_wind, default_water) print 'Wind speed:', w.wind.get_value(start_time) T_w = w.peak_wave_period(start_time) assert np.isclose(T_w, expected)
def test_serialize_deserialize(wind_circ): """ tests and illustrate the funcitonality of serialize/deserialize for WindMover. """ wind = Wind(filename=file_) wm = WindMover(wind) serial = wm.serialize() assert 'wind' in serial wm2 = wm.deserialize(serial) assert wm == wm2
def wind_mover_from_file(filename, **kwargs): """ Creates a wind mover from a wind time-series file (OSM long wind format) :param filename: The full path to the data file :param kwargs: All keyword arguments are passed on to the WindMover constructor :returns mover: returns a wind mover, built from the file """ w = Wind(filename=filename, format='r-theta') return WindMover(w, name=w.name, **kwargs)
def test_serialize_deserialize(wind_circ): """ tests and illustrate the funcitonality of serialize/deserialize for WindMover. """ wind = Wind(filename=file_) wm = WindMover(wind) serial = wm.serialize('webapi') assert 'wind' in serial dict_ = wm.deserialize(serial) dict_['wind'] = wind_circ['wind'] wm.update_from_dict(dict_) assert wm.wind == wind_circ['wind']
def make_model(images_dir=os.path.join(base_dir,"images")): print "initializing the model" start_time = datetime(2013, 7, 23, 0) model = Model(start_time = start_time, duration = timedelta(hours=47), # n+1 of data in file time_step = 900, # 4 hr in seconds uncertain = False, ) mapfile = os.path.join(base_dir, './coast.bna') print "adding the map" gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours print "adding renderer" model.outputters += Renderer(mapfile, images_dir, size=(1800, 1600)) print "adding a wind mover from a time-series" ## this is wind wind_file=get_datafile(os.path.join(base_dir, 'wind.WND')) wind = Wind(filename=wind_file) w_mover = WindMover(wind) model.movers += w_mover print "adding a current mover:" ## this is currents curr_file = get_datafile(os.path.join(base_dir, 'current.txt')) model.movers += GridCurrentMover(curr_file) ## ## Add some spills (sources of elements) ## print "adding 13 points in a cluster that has some small initial separation as the source of spill" for i in range(len(coor)): aaa=utmToLatLng(14,coor[i][0],coor[i][1],northernHemisphere=True) model.spills += point_line_release_spill(num_elements=1, start_position = (aaa[1],aaa[0], 0.0), release_time = start_time, ) print "adding netcdf output" netcdf_output_file = os.path.join(base_dir,'GNOME_output.nc') scripting.remove_netcdf(netcdf_output_file) model.outputters += NetCDFOutput(netcdf_output_file, which_data='all') return model
def test_full_run_extended(): ''' test algorithm resets two day running average with a new time ''' ts = np.zeros((20, ), dtype=datetime_value_2d) ts[:] = [(datetime(2015, 1, 1, 1, 0), (10, 0)), (datetime(2015, 1, 1, 2, 0), (20, 0)), (datetime(2015, 1, 1, 3, 0), (10, 0)), (datetime(2015, 1, 1, 4, 0), (20, 0)), (datetime(2015, 1, 1, 5, 0), (10, 0)), (datetime(2015, 1, 1, 6, 0), (20, 0)), (datetime(2015, 1, 1, 7, 0), (10, 0)), (datetime(2015, 1, 1, 8, 0), (20, 0)), (datetime(2015, 1, 1, 9, 0), (10, 0)), (datetime(2015, 1, 1, 10, 0), (20, 0)), (datetime(2015, 1, 5, 1, 0), (10, 0)), (datetime(2015, 1, 5, 2, 0), (20, 0)), (datetime(2015, 1, 5, 3, 0), (10, 0)), (datetime(2015, 1, 5, 4, 0), (20, 0)), (datetime(2015, 1, 5, 5, 0), (10, 0)), (datetime(2015, 1, 5, 6, 0), (20, 0)), (datetime(2015, 1, 5, 7, 0), (10, 0)), (datetime(2015, 1, 5, 8, 0), (20, 0)), (datetime(2015, 1, 5, 9, 0), (10, 0)), (datetime(2015, 1, 5, 10, 0), (20, 0))] wm = Wind(filename=wind_file, units='mps') start_time = datetime(2015, 1, 1, 1) model_time = start_time running_av = RunningAverage(wind=wm) # running_av = RunningAverage(timeseries=ts) running_av.prepare_for_model_run(model_time) running_av.prepare_for_model_step(model_time) print "running_av" print running_av.ossm.timeseries[:] model_time = datetime(2015, 1, 5, 4, 0) running_av.prepare_for_model_run(model_time) running_av.prepare_for_model_step(model_time) print "running_av2" print running_av.ossm.timeseries[:] assert np.all(running_av.ossm.timeseries['value']['u'][:] == 15)
def __init__(self): wind_file = testdata['ComponentMover']['wind'] #self.ossm = cy_ossm_time.CyOSSMTime(wind_file,file_contains=basic_types.ts_format.magnitude_direction) wm = Wind(filename=wind_file) cats1_file = testdata['ComponentMover']['curr'] cats2_file = testdata['ComponentMover']['curr2'] self.component = cy_component_mover.CyComponentMover() self.component.set_ossm(wm.ossm) #self.component.set_ossm(self.ossm) self.component.text_read(cats1_file, cats2_file) self.component.ref_point = (-75.262319, 39.142987, 0) super(ComponentMove, self).__init__() self.ref[:] = (-75.262319, 39.142987, 0) self.component.prepare_for_model_run() self.component.prepare_for_model_step(self.model_time, self.time_step, 1, self.spill_size)
def test_timespan(): """ Ensure the active flag is being set correctly and checked, such that if active=False, the delta produced by get_move = 0 """ time_step = 15 * 60 # seconds start_pos = (3., 6., 0.) rel_time = datetime(2012, 8, 20, 13) # yyyy/month/day/hr/min/sec sc = sample_sc_release(5, start_pos, rel_time) # value is given as (r,theta) model_time = rel_time time_val = np.zeros((1, ), dtype=datetime_value_2d) time_val['time'] = rel_time time_val['value'] = (2., 25.) wm = WindMover(Wind(timeseries=time_val, units='meter per second'), active_range=(model_time + timedelta(seconds=time_step), InfDateTime('inf'))) wm.prepare_for_model_run() wm.prepare_for_model_step(sc, time_step, model_time) delta = wm.get_move(sc, time_step, model_time) wm.model_step_is_done() assert wm.active is False assert np.all(delta == 0) # model_time + time_step = active_start wm.active_range = (model_time - timedelta(seconds=time_step / 2), InfDateTime('inf')) wm.prepare_for_model_step(sc, time_step, model_time) delta = wm.get_move(sc, time_step, model_time) wm.model_step_is_done() assert wm.active is True print '''\ntest_timespan delta \n{0}'''.format(delta) assert np.all(delta[:, :2] != 0) # model_time + time_step > active_start
def test_set_wind_data(self, all_winds): """ get_wind_data with default output format """ # check get_time_value() wm = Wind(timeseries=all_winds['wind'].get_wind_data(), format='r-theta', units='meter per second') gtime_val = wm.get_wind_data() x = gtime_val[:2] x['value'] = [(1, 10), (2, 20)] # default format is 'r-theta' wm.set_wind_data(x, 'meter per second') # only matches to 10^-14 assert np.allclose(wm.get_wind_data()['value'][:, 0], x['value'][:, 0], atol, rtol) assert np.all(wm.get_wind_data()['time'] == x['time'])
def make_model(images_dir): print 'initializing the model' timestep = timedelta(minutes=15) # this is already default start_time = datetime(2012, 9, 15, 12, 0) model = Model(timestep, start_time) # timeseries for wind data. The value is interpolated if time is between # the given datapoints series = np.zeros((4, ), dtype=datetime_value_2d) series[:] = [(start_time, (5, 180)), (start_time + timedelta(hours=6), (10, 180)), (start_time + timedelta(hours=12), (12, 180)), (start_time + timedelta(hours=18), (8, 180))] wind = Wind(timeseries=series, units='m/s') model.environment += wind # include a wind mover and random diffusion print 'adding movers' model.movers += [WindMover(wind), RandomMover()] # add particles print 'adding particles' release = release_from_splot_data(start_time, 'GL.2013267._LE_WHOLELAKE.txt') model.spills += Spill(release) # output data as png images and in netcdf format print 'adding outputters' netcdf_file = os.path.join(base_dir, 'script_example.nc') # ignore renderer for now model.outputters += [ Renderer(images_dir=images_dir, size=(800, 800), projection_class=GeoProjection), NetCDFOutput(netcdf_file) ] print 'model complete' return model
def __init__(self, wind=None, timeseries=None, past_hours_to_average=3, **kwargs): """ Initializes a running average object from a wind and past hours to average If no wind is given, timeseries gets initialized as: timeseries = np.zeros((1,), dtype=basic_types.datetime_value_2d) units = 'mps' (note: probably should be an error) All other keywords are optional. Optional parameters (kwargs): :param past_hours_to_average: default is 3 """ self.units = 'mps' self.format = 'uv' self._past_hours_to_average = past_hours_to_average self.wind = wind if (wind is None and timeseries is None): mvg_timeseries = np.array([(sec_to_date(zero_time()), [0.0, 0.0])], dtype=basic_types.datetime_value_2d) moving_timeseries = self._convert_to_time_value_pair(mvg_timeseries) else: if wind is not None: moving_timeseries = wind.ossm.create_running_average(self._past_hours_to_average) else: self.wind = Wind(timeseries, units='mps', format='uv') moving_timeseries = self.wind.ossm.create_running_average(self._past_hours_to_average) # print "moving_timeseries" # print moving_timeseries self.ossm = CyTimeseries(timeseries=moving_timeseries) super(RunningAverage, self).__init__(**kwargs)