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_weathering_data_attr(): ''' mass_balance is initialized/written if we have weatherers ''' ts = 900 s1_rel = datetime.now().replace(microsecond=0) s2_rel = s1_rel + timedelta(seconds=ts) s = [point_line_release_spill(10, (0, 0, 0), s1_rel), point_line_release_spill(10, (0, 0, 0), s2_rel)] model = Model(time_step=ts, start_time=s1_rel) model.spills += s model.step() for sc in model.spills.items(): assert len(sc.mass_balance) == 2 for key in ('beached', 'off_maps'): assert key in sc.mass_balance model.environment += [Water(), constant_wind(0., 0)] model.weatherers += [Evaporation(model.environment[0], model.environment[1])] # use different element_type and initializers for both spills s[0].amount = 10.0 s[0].units = 'kg' model.rewind() model.step() for sc in model.spills.items(): # since no substance is defined, all the LEs are marked as # nonweathering assert sc.mass_balance['non_weathering'] == sc['mass'].sum() assert sc.mass_balance['non_weathering'] == s[0].amount s[1].amount = 5.0 s[1].units = 'kg' model.rewind() exp_rel = 0.0 for ix in range(2): model.step() exp_rel += s[ix].amount for sc in model.spills.items(): assert sc.mass_balance['non_weathering'] == sc['mass'].sum() assert sc.mass_balance['non_weathering'] == exp_rel model.rewind() assert sc.mass_balance == {} # weathering data is now empty for all steps del model.weatherers[0] for ix in xrange(2): model.step() for sc in model.spills.items(): assert len(sc.mass_balance) == 2 assert (len(set(sc.mass_balance.keys()) - {'beached', 'off_maps'}) == 0)
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 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 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(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_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 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(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 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 sample_model2(): """ 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/long_island_sound', 'LongIslandSoundMap.BNA') # the land-water map map_ = MapFromBNA(mapfile, refloat_halflife=06) # seconds model = Model( time_step=timedelta(minutes=10), 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[:] = (-72.83, 41.13, 0) end_points[:] = (-72.83, 41.13, 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 '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_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_run_model(spill, nc_name): 'internal funtion' m = Model() m.outputters += NetCDFOutput(nc_name) m.spills += spill _run_model(m) return m
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 _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_merge_from_empty_model(self, model): ''' merge empty model - nothing to merge deepcopy fails on model due to cache - we don't anticipate needing to deepcopy models so do a hack for testing for now. ''' m = Model() model.merge(m) for oc in m._oc_list: for item in getattr(m, oc): assert item in getattr(model, oc)
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 make_model_incomplete_waves(self): ''' create a model with waves objects with no referenced wind, water. Include Spill so we don't get warnings for it ''' model = Model(start_time=self.start_time) model.spills += Spill(Release(self.start_time, 1)) waves = Waves() model.environment += waves return (model, waves)
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_make_default_refs(): ''' ensure make_default_refs is a thread-safe operation once object is instantiated, object.make_default_refs is an attribute of instance ''' model = Model() model1 = Model() wind = Wind(timeseries=[(t, (0, 1))], units='m/s') water = Water() waves = Waves(name='waves') waves1 = Waves(name='waves1', make_default_refs=False) model.environment += [wind, water, waves] model1.environment += waves1 # waves should get auto hooked up/waves1 should not model.step() assert waves.wind is wind assert waves.water is water with pytest.raises(ReferencedObjectNotSet): model1.step()
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 (3, 4, 2, 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(3, burn) model.weatherers += exp_order[3] # 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
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 define_mdl(test=0): ''' WebAPI will update/replace nested objects so do that for the test as well Setup some test cases: 0 - empty model w/ no changes 1 - model with l_mv and l_spills but no changes 2 - empty model but add l_mv and l_spills to json_ so it changed 3 - add l_mv and l_spills to model, then delete some elements and update via json ''' def get_json(mdl): json_ = Model.deserialize(mdl.serialize('webapi')) json_['map'] = mdl.map return json_ mdl = Model() if test == 0: return (mdl, get_json(mdl), False) elif test == 1: 'add a mover and spill but no change' mdl.movers += l_mv mdl.spills += l_spills json_ = get_json(mdl) json_['movers'] = l_mv json_['spills'] = l_spills return (mdl, json_, False) elif test == 2: 'add l_mv, l_spills to empty model' json_ = get_json(mdl) json_['movers'] = l_mv json_['spills'] = l_spills return (mdl, json_, True) elif test == 3: 'add l_mv, l_spills to model, then delete some items' mdl.movers += l_mv mdl.spills += l_spills copy_l_mv = copy.deepcopy(l_mv) copy_l_spills = copy.deepcopy(l_spills) del copy_l_mv[-1] del copy_l_spills[-2] del copy_l_spills[0] json_ = get_json(mdl) json_['movers'] = copy_l_mv json_['spills'] = copy_l_spills return (mdl, json_, True)
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 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_callback_add_weather(): ''' Test callback when weatherer is added ''' model = Model() water = Water() wind = constant_wind(1, 30) assert len(model.environment) == 0 model.weatherers += Evaporation(water, wind) # wind and water added to environment collection assert len(model.environment) == 2 assert wind in model.environment assert water in model.environment