def setup_test(self, end_time_delay, num_les, ts=900.): stime = datetime(2015, 1, 1, 12, 0) etime = stime + end_time_delay st_pos = (0, 0, 0) oil = test_oil m1 = Model(start_time=stime, time_step=ts) m1.environment += [constant_wind(0, 0), Water()] m1.weatherers += Evaporation() m1.spills += point_line_release_spill(num_les[0], st_pos, stime, end_release_time=etime, substance=oil, amount=36000, units='kg') m1.outputters += WeatheringOutput() m2 = Model(start_time=stime, time_step=ts) m2.environment += [constant_wind(0, 0), Water()] m2.weatherers += Evaporation() m2.spills += point_line_release_spill(num_les[1], st_pos, stime, end_release_time=etime, substance=oil, amount=36000, units='kg') m2.outputters += WeatheringOutput() return (m1, m2)
def test_validate_model_spills_time_mismatch_warning(self): ''' test warning messages output for no spills and model start time mismatch with release time ''' model = Model(start_time=self.start_time) (msgs, isvalid) = model.check_inputs() print model.environment print msgs, isvalid assert len(msgs) == 1 and isvalid assert ('{0} contains no spills'.format(model.name) in msgs[0]) model.spills += Spill(Release(self.start_time + timedelta(hours=1), 1)) (msgs, isvalid) = model.check_inputs() assert len(msgs) == 1 and isvalid assert ('{} has release time after model start time'.format( model.spills[0].name) in msgs[0]) model.spills[0].release_time = self.start_time - timedelta(hours=1) (msgs, isvalid) = model.check_inputs() assert len(msgs) == 1 and not isvalid assert ('{} has release time before model start time'.format( model.spills[0].name) in msgs[0])
def allWeatherers(timeStep, start_time, duration, weatheringSteps, map, uncertain, data_path, curr_path, wind_path, map_path, reFloatHalfLife, windFile, currFile, tidalFile, num_elements, depths, lat, lon, output_path, wind_scale, save_nc, timestep_outputs, weatherers, td): print 'initializing the model:' model = Model(time_step=timeStep, start_time=start_time, duration=duration) print 'adding the map:' map_folder = os.path.join(data_path, map_path) if not(os.path.exists(map_folder)): print('The map folder is incorrectly set:', map_folder) mapfile = get_datafile( os.path.join(map_folder,map) ) model.map = MapFromBNA(mapfile, refloat_halflife=reFloatHalfLife) print 'adding a renderer' model.outputters += Renderer(mapfile, output_path, size=(800, 600), output_timestep=timedelta(hours=1)) if save_nc: nc_outputter = NetCDFOutput(netcdf_file, which_data='most', output_timestep=timedelta(hours=1)) model.outputters += nc_outputter print 'adding a wind mover:' wind_file = get_datafile(os.path.join(data_path, wind_path, windFile)) wind = GridWindMover(wind_file) wind.wind_scale = wind_scale model.movers += wind print 'adding a current mover: ' curr_file = get_datafile(os.path.join(data_path, curr_path, currFile)) model.movers += GridCurrentMover(curr_file, num_method='RK4') if td: random_mover = RandomMover(diffusion_coef=10000) model.movers += random_mover print 'adding spill' model.spills += point_line_release_spill(num_elements=num_elements, start_position=(lon, lat, 0), release_time=start_time, end_release_time=start_time + duration) print 'adding weatherers' water = Water(280.92) wind = constant_wind(20.0, 117, 'knots') waves = Waves(wind, water) model.weatherers += Evaporation(water, wind) model.weatherers += Emulsification(waves) model.weatherers += NaturalDispersion(waves, water) return model
def CurrentsAndWinds(timeStep, start_time, duration, weatheringSteps, mapfile, uncertain, data_path, curr_path, wind_path, map_path, reFloatHalfLife, windFile, currFile, tidalFile, num_elements, depths, lat, lon, output_path, wind_scale, save_nc, timestep_outputs, weatherers, td): print 'initializing the model:' model = Model(time_step=timeStep, start_time=start_time, duration=duration) print 'adding the map:' print (data_path, map_path, mapfile) mapfile = get_datafile(os.path.join(data_path, map_path, mapfile)) model.map = MapFromBNA(mapfile, refloat_halflife=reFloatHalfLife) print 'adding a renderer' model.outputters += Renderer(mapfile, output_path, size=(800, 600), output_timestep=timedelta(hours=timestep_outputs)) if save_nc: nc_outputter = NetCDFOutput('currentsAndWinds_example.nc', which_data='standard', output_timestep=timedelta(hours=timestep_outputs)) model.outputters += nc_outputter print 'adding a wind mover:' wind_file = get_datafile(os.path.join(data_path, wind_path, windFile)) wind = GridWindMover(wind_file) wind.wind_scale = wind_scale model.movers += wind print 'adding a current mover: ' curr_file = get_datafile(os.path.join(data_path, curr_path, currFile)) model.movers += GridCurrentMover(curr_file, num_method='RK4') if td: random_mover = RandomMover(diffusion_coef=10000) model.movers += random_mover print 'adding spill' model.spills += point_line_release_spill(num_elements=num_elements, start_position=(lon, lat, 0), release_time=start_time, end_release_time=start_time + duration) return 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
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
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
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 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
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
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
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 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"])
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 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
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
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
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 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
def sample_model(): """ 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, }
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
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, }
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
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
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, image_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, substance=NonWeatheringSubstance(windage_range=(0.01, .04)) #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, 'WindSpeedDirSubset.nc')) topology_file = get_datafile( os.path.join(base_dir, '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
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
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
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
def test_init_with_mode(): model = Model() assert model.mode == 'gnome' model = Model(mode='gnome') assert model.mode == 'gnome' model = Model(mode='adios') assert model.mode == 'adios' with raises(ValueError): model = Model(mode='bogus')
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
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
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_callback_add_mover(): """ Test callback after add mover """ units = 'meter per second' model = Model() model.time_step = timedelta(hours=1) model.duration = timedelta(hours=10) model.start_time = datetime(2012, 1, 1, 0, 0) # 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)) tide_file = get_datafile(os.path.join(os.path.dirname(__file__), r"sample_data", 'tides', 'CLISShio.txt')) tide_ = Tide(filename=tide_file) d_file = get_datafile(os.path.join(datadir, r"long_island_sound/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 isinstance(mover, WindMover): assert mover.wind.id in model.environment if isinstance(mover, CatsMover): if mover.tide is not None: assert mover.tide.id 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 make_model(timeStep,start_time, duration, weatheringSteps, map, uncertain, data_path, reFloatHalfLife, windFile, currFile, tidalFile, num_elements, depths, lat, lon, output_path, evaporation): #initalizing the model print 'initializing the model:' # model = Model(time_step = timeStep, start_time= start_time, duration=duration, uncertain = uncertain) model = Model(time_step = timeStep, start_time= start_time, duration=duration) #adding the map print 'adding the map:' print 'pinche path', data_path mapfile = get_datafile(os.path.join(data_path, map)) model.map = MapFromBNA(mapfile, refloat_halflife = reFloatHalfLife) #model.map = GnomeMap() print 'adding a renderer' # renderer is a class that writes map images for GNOME results model.outputters += Renderer(mapfile, output_path, size=(800, 600), output_timestep=timedelta(hours=1)) ##scripting.remove_netcdf(netcdf_file) #nc_outputter = NetCDFOutput(netcdf_file, which_data='most', output_timestep=timedelta(hours=1)) #model.outputters += nc_outputter #adding the movers print 'adding a wind mover:' wind_file = get_datafile(os.path.join(data_path, windFile)) wind = GridWindMover(wind_file) wind.wind_scale = 2 model.movers += wind print 'adding a current mover: ' curr_file = get_datafile(os.path.join(data_path,currFile)) model.movers+= GridCurrentMover(curr_file, num_method='RK4') #random_mover = RandomMover(diffusion_coef=10000) #in cm/sdfd #model.movers += random_mover if evaporation: #wind for evaporation print'adding evaporation' wind = constant_wind(1, 0, 'knots') water = Water(temperature=300.0, salinity=35.0) model.weatherers += Evaporation(wind=wind, water=water) # print 'adding a spill' # for i in depths: # model.spills+= point_line_release_spill(num_elements=num_elements, start_position=(lon,lat,i), release_time=start_time) model.spills+= point_line_release_spill(num_elements=num_elements, start_position=(lon,lat,0), release_time=start_time, end_release_time=start_time+timedelta(days=93)) return model
def make_model(base_dir='.'): #,images_dir=os.path.join(base_dir, 'images',gdat_dir='/data/dylan/ArcticTAP/data_gnome/ROMS_h2ouv/')): print 'initializing the model' print base_dir start_time = datetime(1985, 1, 1, 13, 31) # start with generic times...this will be changed when model is run model = Model(start_time=start_time, time_step=setup.time_step) mapfile = get_datafile(os.path.join(base_dir, setup.MapFileName)) print mapfile print 'adding the map' model.map = MapFromBNA(mapfile, refloat_halflife=0.0) # seconds return model
def test_update_model(): mdl = Model() d = {'name': 'Model2'} assert mdl.name == 'Model' upd = mdl.update(d) assert mdl.name == d['name'] assert upd is True d['duration'] = 43200 upd = mdl.update(d) assert mdl.duration.seconds == 43200
def test_model_rt(self, images_dir, uncertain): model = make_model(images_dir, uncertain) deserial = Model.deserialize(model.serialize('webapi')) # update the dict so it gives a valid model to load deserial['map'] = model.map for coll in ['movers', 'weatherers', 'environment', 'outputters', 'spills']: for ix, item in enumerate(deserial[coll]): deserial[coll][ix] = getattr(model, coll)[item['id']] m2 = Model.new_from_dict(deserial) assert m2 == model print m2
def make_model(base_dir='.'): #,images_dir=os.path.join(base_dir, 'images',gdat_dir='/data/dylan/ArcticTAP/data_gnome/ROMS_h2ouv/')): print 'initializing the model' print base_dir start_time = datetime(1985, 1, 1, 13, 31) # start with generic times...this will be changed when model is run model = Model(start_time=start_time, duration=timedelta(hours=96), time_step=120*60) mapfile = get_datafile(os.path.join(base_dir, 'arctic_coast3.bna')) print mapfile print 'adding the map' model.map = MapFromBNA(mapfile, refloat_halflife=0.0) # seconds return model
def test_start_time(): model = Model() st = datetime.now() model.start_time = st assert model.start_time == st assert model.current_time_step == -1 model.step() st = datetime(2012, 8, 12, 13) model.start_time = st assert model.current_time_step == -1 assert model.start_time == st
def load_location_file(location_file, request): ''' We would like to merge the current active model into the new model created by our location file prior to clearing our session ''' if isdir(location_file): active_model = get_active_model(request) new_model = Model.load(location_file) new_model._cache.enabled = False if active_model is not None: active_model._map = new_model._map active_model._time_step = new_model._time_step active_model._num_time_steps = new_model._num_time_steps active_model.merge(new_model) else: active_model = new_model name = split(location_file)[1] if name != '': active_model.name = name init_session_objects(request, force=True) log.debug("model loaded - begin registering objects") RegisterObject(active_model, request) set_active_model(request, active_model.id)
def test_model_time_and_current_time_in_sc(): model = Model() model.start_time = datetime.now() assert model.current_time_step == -1 assert model.model_time == model.start_time for step in range(4): model.step() assert model.current_time_step == step assert (model.model_time == model.start_time + timedelta(seconds=step * model.time_step)) for sc in model.spills.items(): assert model.model_time == sc.current_time_stamp
def test_model_time_and_current_time_in_sc(): model = Model() model.start_time = datetime.now() assert model.current_time_step == -1 assert model.model_time == model.start_time for step in range(4): model.step() assert model.current_time_step == step assert (model.model_time == model.start_time + timedelta(seconds=step * model.time_step)) for sc in model.spills.items(): assert model.model_time == sc.current_time_stamp
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 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 _make_run_model(spill, nc_name): 'internal funtion' m = Model() m.outputters += NetCDFOutput(nc_name) m.spills += spill _run_model(m) return m
def test_model_rt(self, uncertain): model = make_model(uncertain) serial = model.serialize('webapi') deserial = Model.deserialize(serial) # update the dict so it gives a valid model to load deserial['map'] = model.map water = model.find_by_attr('_ref_as', 'water', model.environment) deserial['water'] = water for coll in ['movers', 'weatherers', 'environment', 'outputters', 'spills']: for ix, item in enumerate(deserial[coll]): deserial[coll][ix] = getattr(model, coll)[item['id']] m2 = Model.new_from_dict(deserial) assert m2 == model print m2
def test_model_rt(self, uncertain): model = make_model(uncertain) serial = model.serialize() m2 = Model.deserialize(serial) # update the dict so it gives a valid model to load assert m2 == model print m2
def test_release_at_right_time(): ''' Tests that the elements get released when they should There are issues in that we want the elements to show up in the output for a given time step if they were supposed to be released then. Particularly for the first time step of the model. ''' # default to now, rounded to the nearest hour seconds_in_minute = 60 minutes_in_hour = 60 seconds_in_hour = seconds_in_minute * minutes_in_hour start_time = datetime(2013, 1, 1, 0) time_step = 2 * seconds_in_hour model = Model(time_step=time_step, start_time=start_time, duration=timedelta(hours=12)) # add a spill that starts right when the run begins model.spills += point_line_release_spill(num_elements=12, start_position=(0, 0, 0), release_time=datetime(2013, 1, 1, 0), end_release_time=datetime(2013, 1, 1, 6) ) # before the run - no elements present since data_arrays get defined after # 1st step (prepare_for_model_run): assert model.spills.items()[0].num_released == 0 model.step() assert model.spills.items()[0].num_released == 4 model.step() assert model.spills.items()[0].num_released == 8 model.step() assert model.spills.items()[0].num_released == 12 model.step() assert model.spills.items()[0].num_released == 12
def test_model_rt(self, uncertain): model = make_model(uncertain) serial = model.serialize('webapi') deserial = Model.deserialize(serial) # update the dict so it gives a valid model to load deserial['map'] = model.map water = model.find_by_attr('_ref_as', 'water', model.environment) deserial['water'] = water for coll in ['movers', 'weatherers', 'environment', 'outputters', 'spills']: for ix, item in enumerate(deserial[coll]): deserial[coll][ix] = getattr(model, coll)[item['id']] m2 = Model.new_from_dict(deserial) assert m2 == model print m2
def test_model_rt(self, uncertain): model = make_model(uncertain) serial = model.serialize() m2 = Model.deserialize(serial) # update the dict so it gives a valid model to load assert m2 == model print m2
def test_simple_run_rewind(): ''' Pretty much all this tests is that the model will run and the seed is set during first run, then set correctly after it is rewound and run again ''' start_time = datetime(2012, 9, 15, 12, 0) model = Model() model.map = gnome.map.GnomeMap() 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.add_spill(spill) model.start_time = spill.release.release_time # test iterator for step in model: print 'just ran time step: %s' % model.current_time_step assert step['step_num'] == model.current_time_step pos = np.copy(model.spills.LE('positions')) # rewind and run again: print 'rewinding' model.rewind() # test iterator is repeatable for step in model: print 'just ran time step: %s' % model.current_time_step assert step['step_num'] == model.current_time_step assert np.all(model.spills.LE('positions') == pos)
def _make_run_model(spill, nc_name): 'internal function' release_time = spill.release.release_time m = Model(start_time=release_time) m.outputters += NetCDFOutput(nc_name) m.spills += spill _run_model(m) return m
def test_serialize_deserialize(json_): ''' todo: this behaves in unexpected ways when using the 'model' testfixture. For now, define a model in here for the testing - not sure where the problem lies ''' s_time = datetime(2014, 1, 1, 1, 1, 1) model = Model(start_time=s_time) model.spills += point_line_release_spill(num_elements=5, start_position=(0, 0, 0), release_time=model.start_time) o_put = NetCDFOutput(os.path.join(base_dir, u'xtemp.nc')) model.outputters += o_put model.movers += RandomMover(diffusion_coef=100000) #========================================================================== # o_put = [model.outputters[outputter.id] # for outputter in model.outputters # if isinstance(outputter, NetCDFOutput)][0] #========================================================================== model.rewind() print "step: {0}, _start_idx: {1}".format(-1, o_put._start_idx) for ix in range(2): model.step() print "step: {0}, _start_idx: {1}".format(ix, o_put._start_idx) #for json_ in ('save', 'webapi'): dict_ = o_put.deserialize(o_put.serialize(json_)) o_put2 = NetCDFOutput.new_from_dict(dict_) if json_ == 'save': assert o_put == o_put2 else: # _start_idx and _middle_of_run should not match assert o_put._start_idx != o_put2._start_idx assert o_put._middle_of_run != o_put2._middle_of_run assert o_put != o_put2 if os.path.exists(o_put.netcdf_filename): print '\n{0} exists'.format(o_put.netcdf_filename)
def test_release_at_right_time(): ''' Tests that the elements get released when they should There are issues in that we want the elements to show up in the output for a given time step if they were supposed to be released then. Particularly for the first time step of the model. ''' # default to now, rounded to the nearest hour seconds_in_minute = 60 minutes_in_hour = 60 seconds_in_hour = seconds_in_minute * minutes_in_hour start_time = datetime(2013, 1, 1, 0) time_step = 2 * seconds_in_hour model = Model(time_step=time_step, start_time=start_time, duration=timedelta(hours=12)) # add a spill that starts right when the run begins model.spills += point_line_release_spill(num_elements=12, start_position=(0, 0, 0), release_time=datetime(2013, 1, 1, 0), end_release_time=datetime(2013, 1, 1, 6) ) # before the run - no elements present since data_arrays get defined after # 1st step (prepare_for_model_run): assert model.spills.items()[0].num_released == 0 model.step() assert model.spills.items()[0].num_released == 4 model.step() assert model.spills.items()[0].num_released == 8 model.step() assert model.spills.items()[0].num_released == 12 model.step() assert model.spills.items()[0].num_released == 12
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 = PointLineSource(num_elements=10, start_position=(0., 0., 0.), release_time=start_time) model.spills += spill # model.add_spill(spill) assert len(model.spills) == 1 model.start_time = spill.release_time # test iterator: for step in model: print 'just ran time step: %s' % step # reset and run again: model.reset() # test iterator: for step in model: print 'just ran time step: %s' % step assert True
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, # one day start_time=start_time, duration=timedelta(days=3),) 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 iio = IceImageOutput(c_ice_mover) model.outputters += iio ## and run some more: step = model.step() step = model.step() ## and check the output ice_output = step['IceImageOutput'] # print ice_output['time_stamp'] # print ice_output['concentration_image'][:50] # could be really big! # print ice_output['bounding_box'] # print ice_output['projection'] for key in ('time_stamp', 'thickness_image', 'concentration_image', 'bounding_box', 'projection'): assert key in ice_output
def test_validate_model_spills_time_mismatch_warning(self): ''' test warning messages output for no spills and model start time mismatch with release time ''' model = Model(start_time=self.start_time) (msgs, isvalid) = model.validate() assert len(msgs) == 1 and isvalid assert ('{0} contains no spills'.format(model.name) in msgs[0]) model.spills += Spill(Release(self.start_time + timedelta(hours=1), 1)) (msgs, isvalid) = model.validate() assert len(msgs) == 1 and isvalid assert ('Spill has release time after model start time' in msgs[0]) model.spills[0].release_time = self.start_time - timedelta(hours=1) (msgs, isvalid) = model.validate() assert len(msgs) == 1 and not isvalid assert ('Spill has release time before model start time' in msgs[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:' c_mover = GridCurrentMover(filename=setup.curr_fn,topology_file=setup.curr_topo) model.movers += c_mover # print 'adding a GridWindMover:' # w_mover = GridWindMover(wind_file=setup.w_filelist,topology_file=setup.w_Topology) # 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_weatherer_sort(): ''' Sample model with weatherers - only tests sorting of weathereres. The Model will likely not run ''' model = Model() skimmer = Skimmer(100, 'kg', efficiency=0.3, active_start=datetime(2014, 1, 1, 0, 0), active_stop=datetime(2014, 1, 1, 0, 3)) burn = Burn(100, 1, active_start=datetime(2014, 1, 1, 0, 0)) c_disp = ChemicalDispersion(.3, active_start=datetime(2014, 1, 1, 0, 0), active_stop=datetime(2014, 1, 1, 0, 3), efficiency=0.2) weatherers = [Emulsification(), Evaporation(Water(), constant_wind(1, 0)), burn, c_disp, skimmer] #exp_order = [weatherers[ix] for ix in (2, 4, 3, 1, 0)] exp_order = [weatherers[ix] for ix in (4, 2, 3, 1, 0)] model.environment += [Water(), constant_wind(5, 0), Waves()] model.weatherers += weatherers # WeatheringData and FayGravityViscous automatically get added to # weatherers. Only do assertion on weatherers contained in list above assert model.weatherers.values()[:len(exp_order)] != exp_order model.setup_model_run() assert model.weatherers.values()[:len(exp_order)] == exp_order # check second time around order is kept model.rewind() assert model.weatherers.values()[:len(exp_order)] == exp_order # Burn, ChemicalDispersion are at same sorting level so appending # another Burn to the end of the list will sort it to be just after # ChemicalDispersion so index 2 burn = Burn(50, 1, active_start=datetime(2014, 1, 1, 0, 0)) exp_order.insert(2, burn) model.weatherers += exp_order[2] # add this and check sorting still works assert model.weatherers.values()[:len(exp_order)] != exp_order model.setup_model_run() assert model.weatherers.values()[:len(exp_order)] == exp_order