def test_refloat_elements(): tfm = TideflatMap(get_gnomemap(), get_simple_tideflat()) # Fake spill_container sc = { 'next_positions': np.array(( (12, 11, 0), # in water (13.5, 13.5, 0), # on_land (if the map did that) (12.5, 12.5, 0), # still on tideflat (11.5, 12.5, 0), # no longer on tideflat (12.5, 13.5, 0), # in water )), 'status_codes': np.array(( oil_status.in_water, oil_status.on_land, oil_status.on_tideflat, oil_status.on_tideflat, oil_status.in_water, )) } tfm.refloat_elements(sc, gs.minutes(10), datetime(2018, 1, 1, 12, 30)) assert np.all(sc['status_codes'] == np.array(( oil_status.in_water, oil_status.on_land, oil_status.on_tideflat, oil_status.in_water, oil_status.in_water, )))
def test_refloat_elements(): tfm = TideflatMap(get_gnomemap(), get_simple_tideflat()) # Fake spill_container sc = {'next_positions': np.array(((12, 11, 0), # in water (13.5, 13.5, 0), # on_land (if the map did that) (12.5, 12.5, 0), # still on tideflat (11.5, 12.5, 0), # no longer on tideflat (12.5, 13.5, 0), # in water )), 'status_codes': np.array((oil_status.in_water, oil_status.on_land, oil_status.on_tideflat, oil_status.on_tideflat, oil_status.in_water, ))} tfm.refloat_elements(sc, gs.minutes(10), datetime(2018, 1, 1, 12, 30)) assert np.all(sc['status_codes'] == np.array((oil_status.in_water, oil_status.on_land, oil_status.on_tideflat, oil_status.in_water, oil_status.in_water, )) )
def test_tideflat_map_with_both(): tfm = TideflatMap(get_gnomemap(), get_simple_tideflat()) # a few things to make sure they work: assert tfm.on_map((11, 11, 0)) assert not tfm.on_map((16, 16, 0)) assert tfm.in_water((11, 11, 0)) assert tfm.allowable_spill_position((13, 13, 0))
def test_with_gnome_map(): """ simplest possible -- the all water map """ # this should now work like a regular map tfm = TideflatMap(get_gnomemap(), None) # a few things to make sure they work: assert tfm.on_map((11, 11, 0)) assert not tfm.on_map((16, 16, 0)) assert tfm.in_water((11, 11, 0)) assert tfm.allowable_spill_position((13, 13, 0)) assert not tfm.allowable_spill_position((10.5, 10.5, 0))
def test_model_run_with_tideflat(simple_model): """ Add a tideflat with the simple tideflat object no tests here, but you can look at the output """ model = simple_model # make a simple tideflat model bounds = ( (5.623211, 53.309485), (5.784850, 53.348716), (5.761970, 53.368978), (5.722114, 53.376904), (5.667496, 53.367657), (5.620259, 53.354003), (5.609926, 53.328444), ) dry_start = model.start_time + gs.hours(4) dry_end = model.start_time + gs.hours(8) tf = SimpleTideflat(bounds, dry_start, dry_end) # get the map from the model and wrap it in a TideflatMap tfm = TideflatMap(model.map, tf) model.map = tfm # to make it run faster model.time_step = gs.hours(2) for step in model: print "step_num", step['step_num'] status = model.get_spill_property('status_codes') assert np.all(status == oil_status.on_land)
def main(RootDir, Data_Dir, StartSite, RunSite, NumStarts, RunStarts, ReleaseLength, TrajectoryRunLength, StartTimeFiles, TrajectoriesPath, NumLEs, MapFileName, refloat, current_files, wind_files, diffusion_coef, model_timestep, windage_range, windage_persist, OutputTimestep): timingRecord = open(os.path.join(RootDir, "timing.txt"), "w") count = len(StartTimeFiles) * len(RunStarts) timingRecord.write("This file tracks the time to process " + str(count) + " gnome runs") # model timing release_duration = timedelta(hours=ReleaseLength) run_time = timedelta(hours=TrajectoryRunLength) # initiate model model = Model(duration=run_time, time_step=model_timestep, uncertain=False) # determine boundary for model print "Adding the map:", MapFileName mapfile = get_datafile(os.path.join(Data_Dir, MapFileName)) # model.map = MapFromBNA(mapfile, refloat_halflife=refloat) no, model map needs to inclde mudflats. later # loop through seasons for Season in StartTimeFiles: timer1 = datetime.now() SeasonName = Season[1] start_times = open(Season[0], 'r').readlines()[:NumStarts] SeasonTrajDir = os.path.join(RootDir, TrajectoriesPath, SeasonName) if not os.path.isdir(SeasonTrajDir): print "Creating directory: ", SeasonTrajDir make_dir(SeasonTrajDir) print " Season:", SeasonName # get and parse start times in this season start_dt = [] for start_time in start_times: start_time = [int(i) for i in start_time.split(',')] start_time = datetime(start_time[0], start_time[1], start_time[2], start_time[3], start_time[4]) start_dt.append(start_time) ## loop through start times for time_idx in RunStarts: timer2 = datetime.now() gc.collect() model.movers.clear() ## set the start location start_time = start_dt[time_idx] end_time = start_time + run_time model.start_time = start_time print " ", start_time, "to", end_time ## get a list of the only data files needed for the start time (less data used) ## note: requires data files in year increments #Todo: needs fixing before real run years = range(start_time.year, end_time.year + 1) years = [str(i) for i in years] wind = [s for s in wind_files if any(xs in s for xs in years)] current = [ s for s in current_files if any(xs in s for xs in years) ] #Todo: add mudflats. Does it work like this? topology = {'node_lon': 'x', 'node_lat': 'y'} ## add wind movers w_mover = PyWindMover(filename=wind) model.movers += w_mover ## add current movers current_mover = gs.GridCurrent.from_netCDF(current, grid_topology=topology) c_mover = PyCurrentMover(current=current_mover) model.movers += c_mover tideflat = Matroos_Mudflats(current, grid_topology=topology) land_map = gs.MapFromBNA(mapfile) model.map = TideflatMap(land_map, tideflat) ## add diffusion model.movers += RandomMover(diffusion_coef=diffusion_coef) ## loop through start locations timer3 = datetime.now() #Todo: can it deal with the test.location.txt file?? start_position = [float(i) for i in StartSite.split(',')] OutDir = os.path.join(RootDir, TrajectoriesPath, SeasonName, 'pos_%03i' % (RunSite + 1)) make_dir(OutDir) print " ", RunSite, time_idx print " Running: start time:", start_time, print "at start location:", start_position ## set the spill to the location spill = surface_point_line_spill( num_elements=NumLEs, start_position=(start_position[0], start_position[1], 0.0), release_time=start_time, end_release_time=start_time + release_duration, windage_range=windage_range, windage_persist=windage_persist) # print "adding netcdf output" netcdf_output_file = os.path.join( OutDir, 'pos_%03i-t%03i_%08i.nc' % (RunSite + 1, time_idx, int(start_time.strftime('%y%m%d%H'))), ) model.outputters.clear() model.outputters += NetCDFOutput( netcdf_output_file, output_timestep=timedelta(hours=OutputTimestep)) model.spills.clear() model.spills += spill model.full_run(rewind=True) timer4 = datetime.now() diff = round((timer4 - timer3).total_seconds() / 60, 2) timingRecord.write("\t\t" + str(RunSite) + " took " + str(diff) + " minutes to complete") diff = round((timer4 - timer1).total_seconds() / 3600, 2) count = len(RunStarts) timingRecord.write("\t" + str(SeasonName) + " took " + str(diff) + " hours to finish " + str(count) + " Gnome runs") #OutDir.close timingRecord.close
current = gs.GridCurrent.from_netCDF(os.path.join(data_dir, currentfile), grid_topology=topology) angle = TimeseriesData.constant(name='angle', data=17.0, units='degrees') current.angle = angle current_mover = PyCurrentMover(current=current) print 'Adding map' tideflat = Matroos_Mudflats(os.path.join(data_dir, currentfile), grid_topology=topology) mapfile = 'Waddensea_ijsselmeer_6.bna' land_map = gs.MapFromBNA(mapfile) model.map = TideflatMap(land_map, tideflat) print 'Adding current' model.movers += current_mover print 'Adding RandomMover' model.movers += gs.RandomMover(diffusion_coeff=50000) print 'adding a wind mover:' model.movers += gs.constant_wind_mover(**Wind) print 'adding spill' spill = gs.surface_point_line_spill(num_elements=100, start_position=SpillPosition, end_position=(SpillPosition[0],