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 test_serialize_deserialize(): """ test to_dict function for Grid Current object create a new grid_current object and make sure it has same properties """ c_grid = GridCurrentMover(curr_file, topology_file) serial = c_grid.serialize() c2 = GridCurrentMover.deserialize(serial) assert c_grid == c2
def test_new_from_dict(): """ test to_dict function for Grid Current object create a new grid_current object and make sure it has same properties """ c_grid = GridCurrentMover(curr_file,topology_file) dict_ = c_grid.to_dict('create') c2 = GridCurrentMover.new_from_dict(dict_) assert c_grid == c2
def test_exceptions(): """ Test correct exceptions are raised """ with pytest.raises(ValueError): # file does not exist GridCurrentMover(os.path.join('./', 'ChesBay.CUR')) with pytest.raises(OSError): GridCurrentMover(testdata['CurrentCycleMover']['curr_bad_file']) with pytest.raises(TypeError): GridCurrentMover(curr_file, topology_file=10)
def test_uncertain_loop(uncertain_time_delay=0): """ test one time step with uncertainty on the spill checks there is non-zero motion. """ pSpill = sample_sc_release(num_le, start_pos, rel_time, uncertain=True) curr = GridCurrentMover(curr_file, topology_file) curr.uncertain_time_delay = uncertain_time_delay u_delta = _uncertain_loop(pSpill, curr) _assert_move(u_delta) return u_delta
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 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(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(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 test_serialize_deserialize(): """ test to_dict function for Grid Current object create a new grid_current object and make sure it has same properties """ c_grid = GridCurrentMover(curr_file, topology_file) serial = c_grid.serialize('webapi') dict_ = GridCurrentMover.deserialize(serial) c2 = GridCurrentMover.new_from_dict(dict_) assert c_grid == c2 c_grid.update_from_dict(dict_) # tests no failures
def make_model(images_dir=os.path.join(base_dir,"images")): print "initializing the model" start_time = datetime(2013, 7, 23, 0) model = Model(start_time = start_time, duration = timedelta(hours=47), # n+1 of data in file time_step = 900, # 4 hr in seconds uncertain = False, ) mapfile = os.path.join(base_dir, './coast.bna') print "adding the map" gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours print "adding renderer" model.outputters += Renderer(mapfile, images_dir, size=(1800, 1600)) print "adding a wind mover from a time-series" ## this is wind wind_file=get_datafile(os.path.join(base_dir, 'wind.WND')) wind = Wind(filename=wind_file) w_mover = WindMover(wind) model.movers += w_mover print "adding a current mover:" ## this is currents curr_file = get_datafile(os.path.join(base_dir, 'current.txt')) model.movers += GridCurrentMover(curr_file) ## ## Add some spills (sources of elements) ## print "adding 13 points in a cluster that has some small initial separation as the source of spill" for i in range(len(coor)): aaa=utmToLatLng(14,coor[i][0],coor[i][1],northernHemisphere=True) model.spills += point_line_release_spill(num_elements=1, start_position = (aaa[1],aaa[0], 0.0), release_time = start_time, ) print "adding netcdf output" netcdf_output_file = os.path.join(base_dir,'GNOME_output.nc') scripting.remove_netcdf(netcdf_output_file) model.outputters += NetCDFOutput(netcdf_output_file, which_data='all') return model
def test_loop(): """ test one time step with no uncertainty on the spill checks there is non-zero motion. also checks the motion is same for all LEs """ pSpill = sample_sc_release(num_le, start_pos, rel_time) curr = GridCurrentMover(curr_file, topology_file) delta = _certain_loop(pSpill, curr) _assert_move(delta) assert np.all(delta[:, 0] == delta[0, 0]) # lat move matches for all LEs assert np.all(delta[:, 1] == delta[0, 1]) # long move matches for all LEs assert np.all(delta[:, 2] == 0) # 'z' is zeros return delta
def make_modelF(timeStep, start_time, duration, weatheringSteps, map, uncertain, data_path, curr_path, wind_path, map_path, reFloatHalfLife, windFile, currFile, num_elements, depths, lat, lon, output_path, wind_scale, save_nc, timestep_outputs, weatherers, td, dif_coef,temp_water): print 'initializing the model:' model = Model(time_step=timeStep, start_time=start_time, duration=duration, uncertain=uncertain) print 'adding the map:' mapfile = get_datafile(os.path.join(data_path, map_path, map)) model.map = MapFromBNA(mapfile, refloat_halflife=reFloatHalfLife) print 'adding a renderer' if save_nc: scripting.remove_netcdf(output_path+'/'+'output.nc') nc_outputter = NetCDFOutput(output_path+'/'+'output.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=dif_coef) 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)#, substance='AD04001', amount=9600000, units='kg') if weatherers: print 'adding weatherers' water = Water(temp_water) wind = constant_wind(0.0001, 0, 'knots') waves = Waves(wind, water) model.weatherers += Evaporation(water, wind) # model.weatherers += Emulsification(waves) model.weatherers += NaturalDispersion(waves, water) 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(2016, 9, 23, 0, 0) model = Model(start_time=start_time, duration=timedelta(days=2), 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 = ((196.14, 71.89), (196.18, 71.93)) 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_arctic_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=500) # vertical diffusion (different above and below the mixed layer) model.movers += RandomVerticalMover(vertical_diffusion_coef_above_ml=5, vertical_diffusion_coef_below_ml=.11, mixed_layer_depth=10) print 'adding Rise Velocity' # droplets rise as a function of their density and radius model.movers += TamocRiseVelocityMover() print 'adding a circular current and eastward current' fn = 'hycom_glb_regp17_2016092300_subset.nc' fn_ice = 'hycom-cice_ARCu0.08_046_2016092300_subset.nc' import pysgrid import netCDF4 as nc df = nc.Dataset(fn) lon = df['lon'][:] lat = df['lat'][:] grd = pysgrid.SGrid(node_lon=np.repeat(lon.reshape(1, -1), len(lat), axis=0), node_lat=np.repeat(lat.reshape(-1, 1), len(lon), axis=1)) print(grd.node_lon.shape) print(grd.node_lat.shape) gc = GridCurrent.from_netCDF(fn, units='m/s', grid=grd) model.movers += IceMover(fn_ice) model.movers += GridCurrentMover(fn) model.movers += SimpleMover(velocity=(0., 0., 0.)) model.movers += constant_wind_mover(20, 315, units='knots') # Now to add in the TAMOC "spill" print "Adding TAMOC spill" model.spills += tamoc_spill.TamocSpill( release_time=start_time, start_position=(196.16, 71.91, 40.0), num_elements=1000, end_release_time=start_time + timedelta(days=1), name='TAMOC plume', TAMOC_interval=None, # how often to re-run TAMOC ) model.spills[0].data_sources['currents'] = gc return model
def make_model(images_dir=os.path.join(base_dir, 'images')): print('initializing the model') #print (start_date) start_time = new_start_date model = Model( start_time=start_time, duration=timedelta(days=30), #weathering_substeps = 6, time_step=24 * 3600, uncertain=True) mapfile = get_datafile(os.path.join(base_dir, 'gulf.bna')) #mapfile='gulf.bna' print('adding the map') model.map = MapFromBNA(mapfile, refloat_halflife=6) # hours # # Add the outputters -- render to images, and save out as netCDF # print('adding renderer') #model.outputters += Renderer(mapfile, # images_dir, #size=(800, 600), #draw_back_to_fore=True) #print ("adding netcdf output") # netcdf_output_file = os.path.join(base_dir,'gulf_output.nc') #scripting.remove_netcdf(netcdf_output_file) # model.outputters += NetCDFOutput(netcdf_output_file, which_data='all', # output_timestep=timedelta(hours=24)) print("adding shapefile output") # with open("Result {}".format(spill_num), "w") as fp: # fp.write("text") dir_name = os.path.join(base_dir, "Result{}_{}".format(spill_num, position)) if not os.path.exists(dir_name): os.mkdir(dir_name) for i in range(1, 31, 1): model.outputters += ShapeOutput(os.path.join( dir_name, 'gnome_result{id}_{spillnum}'.format(id=i, spillnum=spill_num)), zip_output=False, output_timestep=timedelta(days=i)) # # Set up the movers: # print('adding a RandomMover:') model.movers += RandomMover(diffusion_coef=10000) print('adding a simple wind mover:') # model.movers += constant_wind_mover(5, 315, units='m/s') wind_file = get_datafile(os.path.join(base_dir, 'nc3ECMWF2016.nc')) model.movers += GridWindMover(wind_file) #water = Water (temperature=290.367946, salinity=35.0) #wind = GridWindMover (wind_file) #waves = Waves () print('adding a current mover:') # # this is NEMO currents curr_file = get_datafile(os.path.join(base_dir, 'current2016nc3.nc')) model.movers += GridCurrentMover(curr_file, num_method='Euler') # # # # Add some spills (sources of elements) # # print('adding one spill') spill = point_line_release_spill(num_elements=1000, amount=4000 * spilldur, units='m^3', start_position=position, release_time=start_time, substance=(u'BAHRGANSAR, OIL & GAS')) model.spills += spill ####### open excel file print('adding Excel file') #name = 'Result{}_{}'.format(spill_num, position) workbook = xlsxwriter.Workbook( os.path.join(dir_name, 'Result{}_{}.xlsx'.format(spill_num, position))) worksheet = workbook.add_worksheet() worksheet.write('A1', spilldur * 3200) #workbook.close() print('adding weatherers and cleanup options:') model.environment += [water, wind, waves] model.add_weathering() #model.weatherers += Evaporation() #model.weatherers += Emulsification() #model.weatherers += NaturalDispersion() model.full_run() return model
# break # w_file_list.append( next_fn ) # pad the list with next file to cover special case of last file. # # awkward. fix later # print 'number of wind files :: ', len(w_file_list) # for i in range(0, 1000 ): # curr_t, curr_fn = setup.Time_Map[i] # file_list.append( curr_fn ) # set up model for this start_time/duration, adding required forcing files model = make_model(setup.RootDir) model.duration = run_time # model.movers.clear() print 'adding a GridCurrentMover:' c_mover = GridCurrentMover(filename=setup.c_filelist, topology_file=setup.c_Topology) 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 print 'adding a RandomMover:' random_mover = RandomMover(diffusion_coef=10000) #in cm/s model.movers += random_mover # # print 'creating MFDataset' # ds = nc4.MFDataset(file_list)
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' # start_time = datetime(1985, 7, 1, 13, 30) start_time = datetime(1985, 1, 2, 0, 0) # model time-step in seconds model = Model(start_time=start_time, duration=timedelta(hours=3 * 24 + 23), time_step=15 * 60, uncertain=False) print 'adding the map' mapfile = get_datafile(os.path.join(base_dir, 'coast_SBbig.bna')) model.map = MapFromBNA(mapfile, refloat_halflife=6) # hours print 'adding outputters' renderer = Renderer(mapfile, images_dir, size=(800, 600)) renderer.viewport = ((-122, 33), (-117, 35)) model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_SB') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all') print 'adding a spill' end_time = start_time + timedelta(hours=24) spill = point_line_release_spill(num_elements=1000, start_position=(202.294666, 71.922333, 0.0), release_time=start_time, end_release_time=end_time) model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=50000) print 'adding a current mover:' # currents from ROMS Santa Barbara run, provided by UCLA curr_file = os.path.join(base_dir, 'BOEM', 'Currentfilelist.txt') print curr_file topology_file = os.path.join(base_dir, 'TopologyCurrent.DAT') model.movers += GridCurrentMover(curr_file, topology_file) # model.movers += GridCurrentMover(curr_file) print 'adding a wind mover:' # winds from the ROMS Arctic run, provided by Walter Johnson wind_file = os.path.join(base_dir, 'BOEM', 'Windfilelist.txt') print wind_file topology_file = os.path.join(base_dir, 'TopologyCurrent.DAT') model.movers += GridWindMover(wind_file, topology_file) # model.movers += GridWindMover(wind_file, topology_file) # print 'adding an ice mover:' # ice from the ROMS Arctic run, provided by Walter Johnson # ice_file = os.path.join(base_dir, 'data_gnome', 'ROMS_h2ouv', 'arctic_filelist.txt') # topology_file = os.path.join(base_dir, 'data_gnome', 'arctic_subset_newtopo2.DAT') # model.movers += IceMover(ice_file, topology_file) # model.movers += IceMover(ice_file) # print ice_file return model
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' # data starts at 1:00 instead of 0:00 start_time = datetime(2013, 1, 1, 1) 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, 'ch3d2013.nc') topology_file = os.path.join(base_dir, '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 make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' start_time = datetime(2004, 12, 31, 13, 0) # 1 day of data in file # 1/2 hr in seconds model = Model(start_time=start_time, duration=timedelta(days=1), time_step=30 * 60, uncertain=True) mapfile = get_datafile(os.path.join(base_dir, 'ChesapeakeBay.bna')) print 'adding the map' model.map = MapFromBNA(mapfile, refloat_halflife=1) # 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, size=(800, 600), output_timestep=timedelta(hours=2), draw_ontop='uncertain') renderer.viewport = ((-76.5, 37.), (-75.8, 38.)) print 'adding outputters' model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_chesapeake_bay.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all', output_timestep=timedelta(hours=2)) 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), spill = point_line_release_spill(num_elements=1000, start_position=(-76.126872, 37.680952, 0.0), release_time=start_time) model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=50000) print 'adding a wind mover:' series = np.zeros((2, ), dtype=datetime_value_2d) series[0] = (start_time, (30, 0)) series[1] = (start_time + timedelta(hours=23), (30, 0)) wind = Wind(timeseries=series, units='knot') # default is .4 radians w_mover = WindMover(wind, uncertain_angle_scale=0) model.movers += w_mover print 'adding a current mover:' curr_file = get_datafile(os.path.join(base_dir, 'ChesapeakeBay.nc')) topology_file = get_datafile(os.path.join(base_dir, 'ChesapeakeBay.dat')) # uncertain_time_delay in hours c_mover = GridCurrentMover(curr_file, topology_file, uncertain_time_delay=3) c_mover.uncertain_along = 0 # default is .5 # c_mover.uncertain_cross = 0 # default is .25 model.movers += c_mover return model
_json_, zipfile_, _refs = obj.save(saveloc_, ) obj2 = obj.__class__.load(zipfile_) assert obj == obj2 # Following movers fail on windows with fixture. This is causing an issue in # windows for the NetCDF files - for some reason it is not able to delete the # netcdf data files. All files are being closed in C++. l_movers2 = ( CurrentCycleMover(testdata['CurrentCycleMover']['curr'], topology_file=testdata['CurrentCycleMover']['top'], tide=Tide(testdata['CurrentCycleMover']['tide'])), CurrentCycleMover(testdata['CurrentCycleMover']['curr'], topology_file=testdata['CurrentCycleMover']['top']), GridCurrentMover(testdata['GridCurrentMover']['curr_tri'], testdata['GridCurrentMover']['top_tri']), GridWindMover(testdata['GridWindMover']['wind_curv'], testdata['GridWindMover']['top_curv']), ) @pytest.mark.parametrize("obj", l_movers2) def test_serialize_deserialize_grids(saveloc_, obj): 'test serialize/deserialize functionality' json_ = obj.serialize() obj2 = obj.__class__.deserialize(json_) assert obj == obj2 @pytest.mark.parametrize("obj", l_movers2)
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' start_time = datetime(2004, 12, 31, 13, 0) # 1 day of data in file # 1/2 hr in seconds model = Model(start_time=start_time, duration=timedelta(days=1), time_step=30 * 60, uncertain=True) mapfile = get_datafile(os.path.join(base_dir, 'ChesapeakeBay.bna')) print 'adding the map' model.map = MapFromBNA(mapfile, refloat_halflife=1) # 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=2), draw_ontop='forecast') # set the viewport to zoom in on the map: renderer.viewport = ((-76.5, 37.), (-75.8, 38.)) # add the raster map, so we can see it... # note: this is really slow, so only use for diagnostics # renderer.raster_map = model.map print 'adding outputters' model.outputters += renderer netcdf_file = os.path.join(base_dir, 'script_chesapeake_bay.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='all', output_timestep=timedelta(hours=2)) 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), spill = point_line_release_spill(num_elements=1000, start_position=(-76.126872, 37.680952, 0.0), release_time=start_time) model.spills += spill print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=50000) print 'adding a wind mover:' series = np.zeros((2, ), dtype=datetime_value_2d) series[0] = (start_time, (30, 0)) series[1] = (start_time + timedelta(hours=23), (30, 0)) wind = Wind(timeseries=series, units='knot') # default is .4 radians w_mover = WindMover(wind, uncertain_angle_scale=0) wind.extrapolation_is_allowed = True model.movers += w_mover print 'adding a current mover:' curr_file = get_datafile(os.path.join(base_dir, 'ChesapeakeBay.nc')) topology_file = get_datafile(os.path.join(base_dir, 'ChesapeakeBay.dat')) # uncertain_time_delay in hours c_mover = GridCurrentMover(curr_file, topology_file, uncertain_time_delay=3) c_mover.uncertain_along = 0 # default is .5 # c_mover.uncertain_cross = 0 # default is .25 model.movers += c_mover return model
def make_model(images_dir=os.path.join(base_dir, 'images')): print('get contiguous') kml_file = os.path.join(base_dir, 'contigua.kml') with open(kml_file) as f: contiguous = parser.parse(f).getroot().Document coordinates = contiguous.Placemark.LineString.coordinates.text.split(' ') cont_coord = [] for x in coordinates: x = x.split(',') if len(x) > 1 and float(x[1]) > -12 and float(x[1]) < -3: cont_coord.append([float(x[0]), float(x[1])]) print('initializing the model') start_time = datetime(2022, 1, 22, 12, 0) mapfile = get_datafile(os.path.join(base_dir, './brazil-coast.BNA')) gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours duration = timedelta(days=1) timestep = timedelta(minutes=5) end_time = start_time + duration steps = duration.total_seconds() / timestep.total_seconds() print("Total step: %.4i " % (steps)) # one hour timestep model = Model(start_time=start_time, duration=duration, time_step=timestep, map=gnome_map, uncertain=False, cache_enabled=False) oil_name = 'GENERIC MEDIUM CRUDE' wd = UniformDistribution(low=.0002, high=.0002) subs = GnomeOil(oil_name, initializers=plume_initializers(distribution=wd)) #model.spills += point_line_release_spill(release_time=start_time, start_position=(-35.153, -8.999, 0.0), num_elements=1000, end_release_time=end_time, substance= subs, units='kg') #model.spills += point_line_release_spill(release_time=start_time, start_position=(-35.176, -9.135, 0.0), num_elements=1000, end_release_time=end_time, substance= subs, units='kg') #model.spills += point_line_release_spill(release_time=start_time, start_position=(-35.062, -9.112, 0.0), num_elements=1000, end_release_time=end_time, substance= subs, units='kg') #model.spills += point_line_release_spill(release_time=start_time, start_position=(-34.994, -9.248, 0.0), num_elements=1000, end_release_time=end_time, substance= subs, units='kg') for idx in range(0, len(cont_coord)): model.spills += point_line_release_spill( num_elements=500, start_position=(cont_coord[idx][0], cont_coord[idx][1], 0.0), release_time=start_time, end_release_time=start_time + timedelta(days=1), amount=500, substance=subs, units='kg') #shp_file = os.path.join(base_dir, 'surface_concentration') #scripting.remove_netcdf(shp_file + ".zip") #model.outputters += ShapeOutput(shp_file, # zip_output=False, # surface_conc="kde", # ) print('adding movers:') print('adding a RandomMover:') model.movers += RandomMover(diffusion_coef=10000) print('adding a current mover:') # # this is HYCOM currents curr_file = get_datafile(os.path.join(base_dir, 'currents.nc')) model.movers += GridCurrentMover(curr_file, num_method='Euler') print('adding a grid wind mover:') wind_file = get_datafile(os.path.join(base_dir, 'wind.nc')) #topology_file = get_datafile(os.path.join(base_dir, 'WindSpeedDirSubsetTop.dat')) #w_mover = GridWindMover(wind_file, topology_file) w_mover = GridWindMover(wind_file) 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 print('adding outputters') renderer = Renderer(mapfile, images_dir, image_size=(900, 600), output_timestep=timestep, draw_ontop='forecast') #set the viewport to zoom in on the map: #renderer.viewport = ((-37, -11), (-34, -8)) #alagoas renderer.viewport = ((-36, -10), (-30, 5)) model.outputters += renderer netcdf_file = os.path.join(base_dir, 'contigua.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='standard', surface_conc='kde') return model
def make_model(): print ('initializing the model') #print (start_date) start_time = startday model = Model(start_time=start_time, duration=timedelta(days=30), #weathering_substeps = 6, time_step=24 * 3600, uncertain=False) mapfile = get_datafile(os.path.join(base_dir,'gulf.bna')) #mapfile='gulf.bna' print ('adding the map') model.map = MapFromBNA(mapfile, refloat_halflife=6) # hours # # Add the outputters -- render to images, and save out as netCDF # print ("adding shapefile output") # with open("Result {}".format(spill_num), "w") as fp: # fp.write("text") dir_name = os.path.join (base_dir, season, str(position), "Spillnum {}".format(spill_num)) if not os.path.exists(dir_name): os.makedirs(dir_name) #os.makedirs(dir_name, exist_ok =True) for i in range(1, 31, 1): model.outputters += ShapeOutput(os.path.join(dir_name, 'gnome_result {id}'.format(id=i)), zip_output=False, output_timestep=timedelta(days=i)) images_dir = os.path.join(dir_name, 'image') # print 'adding renderer' # model.outputters += Renderer(mapfile, # images_dir, # image_size=(800, 600)) # print ('adding renderer') # dir_image = os.path.join(dir_name) # model.outputters += Renderer(mapfile, # dir_image, # size=(800, 600)) # # Set up the movers: # print ('adding a RandomMover:') model.movers += RandomMover(diffusion_coef=10000) print ('adding a simple wind mover:') wind_file = get_datafile(os.path.join(base_dir, 'ECMWF.nc')) model.movers += GridWindMover(wind_file) print ('adding a current mover:') # # this is NEMO currents curr_file = get_datafile(os.path.join(base_dir, Currents)) model.movers += GridCurrentMover(curr_file, num_method='Euler'); # # Add some spills (sources of elements) print ('adding one spill') spill = point_line_release_spill(num_elements=1000, amount= 3200000000 * spilldur , units='grams', start_position = position, release_time = start_time, substance = (sub)) model.spills += spill ####### open excel file print ('adding Excel file') workbook = xlsxwriter.Workbook(os.path.join(dir_name, 'Result {}_{}.xlsx'.format(spill_num, position))) worksheet = workbook.add_worksheet () a = ((spilldur*3200)**(-0.3))*0.000069 worksheet.write ('A1', a) workbook.close() print ('adding weatherers and cleanup options:') model.environment += [water,wind,waves] model.weatherers += Evaporation() model.weatherers += Emulsification() model.weatherers += NaturalDispersion() print ('model full run:') model.full_run() 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(2016, 9, 18, 1, 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 = ((-87.095, 27.595), (-87.905, 28.405)) print 'adding outputters' model.outputters += renderer # Also going to write the results out to a netcdf file netcdf_file = os.path.join(base_dir, 'gulf_tamoc.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=100000) # vertical diffusion (different above and below the mixed layer) model.movers += RandomVerticalMover( vertical_diffusion_coef_above_ml=50, vertical_diffusion_coef_below_ml=10, horizontal_diffusion_coef_above_ml=100000, horizontal_diffusion_coef_below_ml=100, mixed_layer_depth=10) print 'adding Rise Velocity' # droplets rise as a function of their density and radius model.movers += TamocRiseVelocityMover() print 'adding the 3D current mover' gc = GridCurrent.from_netCDF('HYCOM_3d.nc') model.movers += GridCurrentMover('HYCOM_3d.nc') # model.movers += SimpleMover(velocity=(0., 0, 0.)) # model.movers += constant_wind_mover(5, 315, units='knots') # Wind from a buoy w = Wind(filename='KIKT.osm') model.movers += WindMover(w) # Now to add in the TAMOC "spill" print "Adding TAMOC spill" model.spills += tamoc_spill.TamocSpill( release_time=start_time, start_position=(-87.5, 28.0, 2000), num_elements=30000, end_release_time=start_time + timedelta(days=2), name='TAMOC plume', TAMOC_interval=None, # how often to re-run TAMOC ) model.spills[0].data_sources['currents'] = gc return model
# model.movers += c_mover # print 'creating wind MFDataset' # ds_w = nc4.MFDataset(file_list_w) # print 'adding a WindMover (Euler):' # g_wind = GridWind.from_netCDF(filename=file_list_w, # dataset=ds_w, # grid_topology={'node_lon':'lonc','node_lat':'latc'}) # w_mover = PyWindMover(wind = g_wind, default_num_method='Euler') # model.movers += w_mover print 'adding a CurrentMover (Trapeziod/RK4):' c_mover = GridCurrentMover(os.path.join(setup.Data_Dir, setup.CurrCatFile), os.path.join(setup.Data_Dir, setup.CurrTopoFile), num_method='RK4') model.movers += c_mover print 'adding a WindMover (Euler):' w_mover = GridWindMover( os.path.join(setup.Data_DirW, setup.WindCatFile), os.path.join(setup.Data_DirW, setup.WindTopoFile)) model.movers += w_mover print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=50000) # for pos_idx, start_position in enumerate(start_positions): for pos_idx in setup.RunSites:
""" delta = test_loop() u_delta = test_uncertain_loop() print print delta print u_delta assert np.all(delta[:, :2] != u_delta[:, :2]) assert np.all(delta[:, 2] == u_delta[:, 2]) uncertain_time_delay = 3 u_delta = test_uncertain_loop(uncertain_time_delay) print u_delta assert np.all(delta[:, :2] == u_delta[:, :2]) c_grid = GridCurrentMover(curr_file, topology_file) def test_default_props(): """ test default properties """ assert c_grid.current_scale == 1 assert c_grid.uncertain_time_delay == 0 assert c_grid.uncertain_duration == 24 assert c_grid.uncertain_cross == .25 assert c_grid.uncertain_along == .5 assert c_grid.extrapolate is False assert c_grid.time_offset == 0
def make_model(images_dir=os.path.join(base_dir, 'images')): print 'initializing the model' start_time = datetime(2013, 5, 18, 0) model = Model(start_time=start_time, duration=timedelta(days=8), time_step=4 * 3600, uncertain=False) mapfile = get_datafile(os.path.join(base_dir, 'mariana_island.bna')) print 'adding the map' model.map = MapFromBNA(mapfile, refloat_halflife=6) # hours # # Add the outputters -- render to images, and save out as netCDF # print 'adding renderer' model.outputters += Renderer( mapfile, images_dir, size=(800, 600), ) # draw_back_to_fore=True) # print "adding netcdf output" # netcdf_output_file = os.path.join(base_dir,'mariana_output.nc') # scripting.remove_netcdf(netcdf_output_file) # model.outputters += NetCDFOutput(netcdf_output_file, which_data='all') # # Set up the movers: # print 'adding a RandomMover:' model.movers += RandomMover(diffusion_coef=10000) print 'adding a simple wind mover:' model.movers += constant_wind_mover(5, 315, units='m/s') print 'adding a current mover:' # # this is HYCOM currents curr_file = get_datafile(os.path.join(base_dir, 'HYCOM.nc')) model.movers += GridCurrentMover(curr_file, num_method=numerical_methods.euler) # # # # Add some spills (sources of elements) # # print 'adding four spill' model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4, start_position=(145.25, 15.0, 0.0), release_time=start_time) model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4, start_position=(146.25, 15.0, 0.0), release_time=start_time) model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4, start_position=(145.75, 15.25, 0.0), release_time=start_time) model.spills += point_line_release_spill(num_elements=NUM_ELEMENTS // 4, start_position=(145.75, 14.75, 0.0), release_time=start_time) return model
def make_model(images_dir=os.path.join(base_dir, 'images2')): print('initializing the model') start_time = datetime(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]), int(sys.argv[5])) mapfile = get_datafile(os.path.join(base_dir, './brazil-coast.bna')) gnome_map = MapFromBNA(mapfile, refloat_halflife=6) # hours # # the image output renderer # global renderer #duration = timedelta(minutes=5) #timestep = timedelta(minutes=5) duration = timedelta(minutes=5) timestep = timedelta(minutes=5) endtime = start_time + duration steps = duration.total_seconds() / timestep.total_seconds() print("Total step: %.4i " % (steps)) model = Model(start_time=start_time, duration=duration, time_step=timestep, map=gnome_map, uncertain=False, cache_enabled=False) oil_name = 'GENERIC MEDIUM CRUDE' wd = UniformDistribution(low=.0002, high=.0002) subs = GnomeOil(oil_name, initializers=plume_initializers(distribution=wd)) #print 'adding a spill' #spill = point_line_release_spill(num_elements=122, # start_position=(-35.14, # -9.40, 0.0), # release_time=start_time) #model.spills += spill #spill2 = spatial_release_spill(-35.14,-9.40, 0.0, start_time) #model.spills += spill2 #print 'load nc' #netcdf_file = os.path.join(base_dir, 'maceio.nc') #relnc = InitElemsFromFile(netcdf_file,release_time=start_time) #relnc = InitElemsFromFile(netcdf_file,index=5) #spillnc = Spill(release=relnc) #print spillnc.release.num_elements #print spillnc.release.name #print spillnc.substance #print relnc._init_data['age'] #print relnc.release_time #model.spills += spillnc #model._load_spill_data() #for sc in model.spills.items(): # sc.prepare_for_model_run() #print(relnc.num_elements) #print(relnc.num_released) # add particles - it works print('adding particles') # Persistent oil spill in contiguous zone border if int(sys.argv[6]) == 1: release = release_from_splot_data(start_time, 'contiguous.txt') print("Adding new particles") model.spills += Spill(release=release, substance=subs) # Particles from previows simulation step try: f = open('step.txt') f.close() release2 = release_from_splot_data(start_time, 'step.txt') model.spills += Spill(release=release2, substance=subs) except IOError: print('No previous step, using only contiguous.txt') #assert rel.num_elements == exp_num_elems #assert len(rel.start_position) == exp_num_elems #cumsum = np.cumsum(exp) #for ix in xrange(len(cumsum) - 1): # assert np.all(rel.start_position[cumsum[ix]] == # rel.start_position[cumsum[ix]:cumsum[ix + 1]]) #assert np.all(rel.start_position[0] == rel.start_position[:cumsum[0]]) #spnc = Spill(release=None) #spnc.release = relnc print('adding a RandomMover:') #model.movers += RandomMover(diffusion_coef=10000, uncertain_factor=2) model.movers += RandomMover(diffusion_coef=10000) print('adding a current mover:') # # this is HYCOM currents curr_file = get_datafile(os.path.join(base_dir, 'corrente15a28de09.nc')) model.movers += GridCurrentMover(curr_file, num_method='Euler') print('adding a grid wind mover:') wind_file = get_datafile(os.path.join(base_dir, 'vento15a28de09.nc')) #topology_file = get_datafile(os.path.join(base_dir, 'WindSpeedDirSubsetTop.dat')) #w_mover = GridWindMover(wind_file, topology_file) w_mover = GridWindMover(wind_file) 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 print('adding outputters') renderer = Renderer(mapfile, images_dir, image_size=(900, 600), output_timestep=timestep, draw_ontop='forecast') #set the viewport to zoom in on the map: #renderer.viewport = ((-37, -11), (-34, -8)) #alagoas renderer.viewport = ((-55, -34), (-30, 5)) #1/4 N alagoas model.outputters += renderer netcdf_file = os.path.join(base_dir, 'step.nc') scripting.remove_netcdf(netcdf_file) model.outputters += NetCDFOutput(netcdf_file, which_data='standard', surface_conc='kde') return model