def animateWind(wind_source, create_movie=True): time = wind_source.t max_stress = max(np.max(np.abs(wind_source.X)), np.max(np.abs(wind_source.Y))) wind_stress = np.sqrt(wind_source.X**2, wind_source.Y**2) fig = plt.figure(figsize=(12,3)) ax = [None]*3 sc = [None]*3 ax[0] = plt.subplot(1,3,1) plt.title("Wind stress (total)") sc[0] = plt.imshow(wind_stress[0], origin='lower', vmin=0, cmap='Reds') plt.colorbar(shrink=0.6) ax[1] = plt.subplot(1,3,2) plt.title("Wind stress u") sc[1] = plt.imshow(wind_source.X[0], origin='lower', cmap='bwr', vmin=-max_stress, vmax=max_stress) plt.colorbar(shrink=0.6) ax[2] = plt.subplot(1,3,3) plt.title("Wind stress v") sc[2] = plt.imshow(wind_source.Y[0], origin='lower', cmap='bwr', vmin=-max_stress, vmax=max_stress) plt.colorbar(shrink=0.6) progress = Common.ProgressPrinter(5) pp = display(progress.getPrintString(0),display_id=True) #Helper function which simulates and plots the solution def animate(i): fig.suptitle("Time = {:04.0f} h".format(time[i]/3600), fontsize=18) fig.sca(ax[0]) sc[0].set_data(wind_stress[i]) fig.sca(ax[1]) sc[1].set_data(wind_source.X[i]) fig.sca(ax[2]) sc[2].set_data(wind_source.Y[i]) pp.update(progress.getPrintString(i / (len(time)-1))) #Matplotlib for creating an animation if (create_movie): anim = animation.FuncAnimation(fig, animate, range(len(time)), interval=250) plt.close(fig) return anim else: pass
def checkCachedNetCDF(source_url, download_data=True): """ Checks if the file represented by source_url is available locally already. We search for the file in the working directory, or in a folder called 'netcdf_cache' in the working directory. If download_data is true, it will download the netcfd file into 'netcdf_cache' if it is not found locally already. """ ### Check if local file exists: filename = os.path.abspath(os.path.basename(source_url)) cache_folder = 'netcdf_cache' cache_filename = os.path.abspath( os.path.join(cache_folder, os.path.basename(source_url))) if (os.path.isfile(filename)): source_url = filename elif (os.path.isfile(cache_filename)): source_url = cache_filename elif (download_data): import requests download_url = source_url.replace("dodsC", "fileServer") req = requests.get(download_url, stream=True) filesize = int(req.headers.get('content-length')) is_notebook = False if (in_ipynb()): progress = Common.ProgressPrinter() pp = display(progress.getPrintString(0), display_id=True) is_notebook = True os.makedirs(cache_folder, exist_ok=True) print("Downloading data to local file (" + str(filesize // (1024 * 1024)) + " MB)") with open(cache_filename, "wb") as outfile: for chunk in req.iter_content(chunk_size=10 * 1024 * 1024): if chunk: outfile.write(chunk) if (is_notebook): pp.update( progress.getPrintString(outfile.tell() / filesize)) source_url = cache_filename return source_url
def simulate_gpuocean_deterministic(source_url, domain, initx, inity, sim_args, norkyst_data = True, erode_land = 1, wind_drift_factor = 0.0, rescale=0, forecast_file = None, start_forecast_hours = 0, duration = 23, ocean_state_file = None, netcdf_frequency = 5 ): """ source_url: url or local file or list of either with fielddata in NetCDF-format domain: array/list on form [x0,x1,y0,y1] defining the domain for the simulation initx, inity = initial coordinates of single drifter or lists with coordinates for multiple drifters. In local cartesian coordinates of simulation-domain. norkyst_data: (default True) If True, assumes data from norkyst800. Else, works with norfjords160m(and probably other ROMS data). sim_args, erode_land, observation_type: arguments needed for simulator and observation object. sim_args must be given. wind_drift_factor: fraction of wind-speed at which objects will be advected. Default is 0 (no direct wind-drift) rescale: factor setting resolution of simulation-grid. 0 indicates no rescaling(original resolution), while any other number changes the resolution (ie 2 gives double resolution) forecast_file: optional file for storing trajectory (pickle) ocean_state_file: optional file for storing ocean state (netcdf) netcdf_frequency: frequency(in hours) for storing of ocean states. start_forecast_hours = number hours after which to start simulating drifttrajectories(ocean model starts at beginning of field-data) Default is at beginning of field-data. forecast_duration = duration of simulation(including possibly only ocean simulation). Default is 24 hours. """ end_forecast_hours = start_forecast_hours + duration #Create simulator data_args = NetCDFInitialization.getInitialConditions(source_url, domain[0], domain[1], domain[2],domain[3] , timestep_indices = None,norkyst_data = norkyst_data, erode_land = erode_land, download_data = False) if wind_drift_factor: wind_data = data_args.pop('wind', None) else: wind_data = None if rescale: data_args = NetCDFInitialization.rescaleInitialConditions(data_args, scale=rescale) sim = CDKLM16.CDKLM16(**sim_args, **NetCDFInitialization.removeMetadata(data_args)) #Forecast observation_type = dautils.ObservationType.UnderlyingFlow observation_args = {'observation_type': observation_type, 'nx': sim.nx, 'ny': sim.ny, 'domain_size_x': sim.nx*sim.dx, 'domain_size_y': sim.ny*sim.dy, 'land_mask': sim.getLandMask() } trajectory_forecast = Observation.Observation(**observation_args) #Drifters #Assumes initx, inity same format/shape if type(initx) is not list: initx = [initx] inity = [inity] num_drifters = len(initx) drifters = GPUDrifterCollection.GPUDrifterCollection(sim_args['gpu_ctx'], num_drifters, wind = wind_data, wind_drift_factor = wind_drift_factor, boundaryConditions = sim.boundary_conditions, domain_size_x = trajectory_forecast.domain_size_x, domain_size_y = trajectory_forecast.domain_size_y, gpu_stream = sim.gpu_stream) drifter_pos_init = np.array([initx, inity]).T try: if ocean_state_file is not None: print("Storing ocean state to netCDF-file: " + ocean_state_file) ncfile = Dataset(ocean_state_file, 'w') var = {} var['eta'], var['hu'], var['hv'] = sim.download(interior_domain_only=False) _, var['Hm'] = sim.downloadBathymetry(interior_domain_only=False) ny, nx = var['eta'].shape # Create dimensions ncfile.createDimension('time', None) # unlimited ncfile.createDimension('x', nx) ncfile.createDimension('y', ny) ncvar = {} # Create variables for dimensions ncvar['time'] = ncfile.createVariable('time', 'f8', ('time',)) ncvar['x'] = ncfile.createVariable('x', 'f4', ('x',)) ncvar['y'] = ncfile.createVariable('y', 'f4', ('y',)) # Fill dimension variables ncvar['x'][:] = np.linspace(0, nx*sim.dx, nx) ncvar['y'][:] = np.linspace(0, ny*sim.dy, ny) # Create static variables ncvar['Hm'] = ncfile.createVariable('Hm', 'f8', ('y', 'x',), zlib=True) ncvar['Hm'][:,:] = var['Hm'][:,:] # Create time varying data variables for varname in ['eta', 'hu', 'hv']: ncvar[varname] = ncfile.createVariable(varname, 'f8', ('time', 'y', 'x',), zlib=True) ncvar['num_iterations'] = ncfile.createVariable('num_iterations', 'i4', ('time',)) #Run simulation num_total_hours = end_forecast_hours five_mins_in_an_hour = 12 sub_dt = 5*60 # five minutes progress = Common.ProgressPrinter(5) pp = display(progress.getPrintString(0), display_id=True) netcdf_counter = 0 for hour in range(num_total_hours): if hour == start_forecast_hours: # Attach drifters drifters.setDrifterPositions(drifter_pos_init) sim.attachDrifters(drifters) trajectory_forecast.add_observation_from_sim(sim) for mins in range(five_mins_in_an_hour): t = sim.step(sub_dt) if hour >= start_forecast_hours: trajectory_forecast.add_observation_from_sim(sim) if ocean_state_file is not None and hour%netcdf_frequency == 0: var['eta'], var['hu'], var['hv'] = sim.download(interior_domain_only=False) ncvar['time'][netcdf_counter] = sim.t ncvar['num_iterations'][netcdf_counter] = sim.num_iterations abort=False for varname in ['eta', 'hu', 'hv']: ncvar[varname][netcdf_counter,:,:] = var[varname][:,:] #np.ma.masked_invalid(var[varname][:,:]) if (np.any(np.isnan(var[varname]))): print("Variable " + varname + " contains NaN values!") abort=True netcdf_counter += 1 if (abort): print("Aborting at t=" + str(sim.t)) ncfile.sync() break pp.update(progress.getPrintString(hour/(end_forecast_hours-1))) if forecast_file is not None: trajectory_forecast.to_pickle(forecast_file) except Exception as e: print("Something went wrong:" + str(e)) raise e finally: if ocean_state_file is not None: ncfile.close() return trajectory_forecast
def bcAnimation(bc_data, x0, x1, y0, y1, create_movie=True, **kwargs): nx = x1-x0 ny = y1-y0 x_north = np.linspace(x0, x1, nx) y_north = np.linspace(y1, y1, nx) x_south = np.linspace(x0, x1, nx) y_south = np.linspace(y0, y0, nx) x_east = np.linspace(x1, x1, ny) y_east = np.linspace(y0, y1, ny) x_west = np.linspace(x0, x0, ny) y_west = np.linspace(y0, y1, ny) fig = plt.figure(figsize=(12,3)) sc = [None]*12 ax = [None]*3 plt.suptitle("Time t=" + str(bc_data.t[0]/3600) + " h") ax[0] = plt.subplot(1,3,1) plt.title("eta") sc[0] = plt.scatter(x_north, y_north, c=bc_data.north.h[0,:], marker='s', vmax=0.5, vmin=-0.5) sc[1] = plt.scatter(x_south, y_south, c=bc_data.south.h[0,:], marker='s', vmax=0.5, vmin=-0.5) sc[2] = plt.scatter(x_east, y_east, c=bc_data.east.h[0,:], marker='s', vmax=0.5, vmin=-0.5) sc[3] = plt.scatter(x_west, y_west, c=bc_data.west.h[0,:], marker='s', vmax=0.5, vmin=-0.5) plt.axis('image') plt.colorbar(shrink=0.6) ax[1] = plt.subplot(1,3,2) plt.title("hu") sc[4] = plt.scatter(x_north, y_north, c=bc_data.north.hu[0,:], marker='s', vmax=75, vmin=-75) sc[5] = plt.scatter(x_south, y_south, c=bc_data.south.hu[0,:], marker='s', vmax=75, vmin=-75) sc[6] = plt.scatter(x_east, y_east, c=bc_data.east.hu[0,:], marker='s', vmax=75, vmin=-75) sc[7] = plt.scatter(x_west, y_west, c=bc_data.west.hu[0,:], marker='s', vmax=75, vmin=-75) plt.axis('image') plt.colorbar(shrink=0.6) ax[2] = plt.subplot(1,3,3) plt.title("hv") sc[8] = plt.scatter(x_north, y_north, c=bc_data.north.hv[0,:], marker='s', vmax=75, vmin=-75) sc[9] = plt.scatter(x_south, y_south, c=bc_data.south.hv[0,:], marker='s', vmax=75, vmin=-75) sc[10] = plt.scatter(x_east, y_east, c=bc_data.east.hv[0,:], marker='s', vmax=75, vmin=-75) sc[11] = plt.scatter(x_west, y_west, c=bc_data.west.hv[0,:], marker='s', vmax=75, vmin=-75) plt.axis('image') plt.colorbar(shrink=0.6) progress = Common.ProgressPrinter(5) pp = display(progress.getPrintString(0),display_id=True) #Helper function which simulates and plots the solution def animate(i): fig.suptitle("Time = {:04.0f} h".format(bc_data.t[i]/3600), fontsize=18) fig.sca(ax[0]) sc[0].set_array(bc_data.north.h[i]) sc[1].set_array(bc_data.south.h[i]) sc[2].set_array(bc_data.east.h[i]) sc[3].set_array(bc_data.west.h[i]) fig.sca(ax[1]) sc[4].set_array(bc_data.north.hu[i]) sc[5].set_array(bc_data.south.hu[i]) sc[6].set_array(bc_data.east.hu[i]) sc[7].set_array(bc_data.west.hu[i]) fig.sca(ax[2]) sc[8].set_array(bc_data.north.hv[i]) sc[9].set_array(bc_data.south.hv[i]) sc[10].set_array(bc_data.east.hv[i]) sc[11].set_array(bc_data.west.hv[i]) pp.update(progress.getPrintString(i / (len(bc_data.t)-1))) #Matplotlib for creating an animation if (create_movie): anim = animation.FuncAnimation(fig, animate, range(len(bc_data.t)), interval=250) plt.close(fig) return anim else: pass
def ncAnimation(filename, title=None, movie_frames=None, create_movie=True, fig=None, save_movie=True, **kwargs): if (title is None): title = filename.replace('_', ' ').replace('.nc', '') #Create figure and plot initial conditions if fig is None: fig = plt.figure(figsize=(14, 4)) try: ncfile = Dataset(filename) x = ncfile.variables['x'][:] y = ncfile.variables['y'][:] t = ncfile.variables['time'][:] H_m = ncfile.variables['Hm'][:,:] eta = ncfile.variables['eta'][:,:,:] hu = ncfile.variables['hu'][:,:,:] hv = ncfile.variables['hv'][:,:,:] except Exception as e: raise e finally: ncfile.close() if movie_frames is None: movie_frames = len(t) dx = x[1] - x[0] dy = y[1] - y[0] progress = Common.ProgressPrinter(5) pp = display(progress.getPrintString(0),display_id=True) if (create_movie): ax, sp = plotSolution(fig, eta[0], hu[0], hv[0], H_m+eta[0], dx, dy, t[0], title, **kwargs) else: ax, sp = plotSolution(fig, eta[-1], hu[-1], hv[-1], H_m+eta[-1], dx, dy, t[-1], title, **kwargs) return #Helper function which simulates and plots the solution def animate(i): t_now = t[0] + (i / (movie_frames-1)) * (t[-1] - t[0]) k = np.searchsorted(t, t_now) if (k >= eta.shape[0]): k = eta.shape[0] - 1 j = max(0, k-1) if (j == k): k += 1 s = (t_now - t[j]) / (t[k] - t[j]) plotSolution(fig, (1-s)*eta[j] + s*eta[k], (1-s)*hu[j] + s*hu[k], (1-s)*hv[j] + s*hv[k], H_m+(1-s)*eta[j] + s*eta[k], dx, dy, t_now, title, **kwargs, ax=ax, sp=sp) pp.update(progress.getPrintString(i / (movie_frames-1))) #Matplotlib for creating an animation anim = animation.FuncAnimation(fig, animate, range(movie_frames), interval=100) if (save_movie): anim.save(filename + '.mp4') plt.close(fig) return Video(filename + '.mp4') else: plt.close(fig) return anim
dx, dy, timesteps[0], "Reference solution $t_0$=" + datetime.datetime.fromtimestamp(t0).isoformat(timespec='seconds'), **kwargs) else: ax, sp = plotSolution(fig, eta[-1], hu[-1], hv[-1], H_m+eta[-1], dx, dy, timesteps[-1], "Reference solution $t_0$=" + datetime.datetime.fromtimestamp(t0).isoformat(timespec='seconds'), **kwargs) return progress = Common.ProgressPrinter(5) pp = display(progress.getPrintString(0),display_id=True) #Helper function which simulates and plots the solution def animate(i): t_now = timesteps[0] + (i / (movie_frames-1)) * (timesteps[-1] - timesteps[0]) k = np.searchsorted(timesteps, t_now) if (k >= eta.shape[0]): k = eta.shape[0] - 1 j = max(0, k-1) if (j == k): k += 1 s = (t_now - timesteps[j]) / (timesteps[k] - timesteps[j])
def simulate_gpuocean_deterministic(source_url, domain, initx, inity, sim_args, erode_land=1, wind_drift_factor=0.0, rescale=0, outfolder=None, start_forecast_hours=0, forecast_duration=23): """ source_url: url or local file or list of either with fielddata in NetCDF-format domain: array/list on form [x0,x1,y0,y1] defining the domain for the simulation initx, inity = initial coordinates of single drifter or lists with coordinates for multiple drifters. In local cartesian coordinates of simulation-domain. sim_args, erode_land, observation_type: arguments needed for simulator and observation object. sim_args must be given. wind_drift_factor: fraction of wind-speed at which objects will be advected. Default is 0 (no direct wind-drift) rescale: factor setting resolution of simulation-grid. 0 indicates no rescaling(original resolution), while any other number changes the resolution (ie 2 gives double resolution) outfolder: outfolder start_forecast_hours = number hours after which to start simulating drifttrajectories(ocean model starts at beginning of field-data) Default is at beginning of field-data. forecast_duration = duration of simulation(including possibly only ocean simulation). Default is 24 hours. """ end_forecast_hours = start_forecast_hours + forecast_duration #Create simulator data_args = NetCDFInitialization.getInitialConditions( source_url, domain[0], domain[1], domain[2], domain[3], timestep_indices=None, erode_land=erode_land, download_data=False) if wind_drift_factor: wind_data = data_args.pop('wind', None) else: wind_data = None if rescale: data_args = NetCDFInitialization.rescaleInitialConditions( data_args, scale=rescale) sim = CDKLM16.CDKLM16(**sim_args, **NetCDFInitialization.removeMetadata(data_args)) #Forecast observation_type = dautils.ObservationType.UnderlyingFlow observation_args = { 'observation_type': observation_type, 'nx': sim.nx, 'ny': sim.ny, 'domain_size_x': sim.nx * sim.dx, 'domain_size_y': sim.ny * sim.dy, 'land_mask': sim.getLandMask() } trajectory_forecast = Observation.Observation(**observation_args) if outfolder is not None: trajectory_forecast_filename = 'trajectory_forecast_' + str( start_forecast_hours) + '_to_' + str( end_forecast_hours) + '.pickle' #Drifters #Assumes initx, inity same format/shape if type(initx) is not list: initx = [initx] inity = [inity] num_drifters = len(initx) drifters = GPUDrifterCollection.GPUDrifterCollection( sim_args['gpu_ctx'], num_drifters, wind=wind_data, wind_drift_factor=wind_drift_factor, boundaryConditions=sim.boundary_conditions, domain_size_x=trajectory_forecast.domain_size_x, domain_size_y=trajectory_forecast.domain_size_y, gpu_stream=sim.gpu_stream) drifter_pos_init = np.array([initx, inity]).T #Run simulation num_total_hours = end_forecast_hours five_mins_in_an_hour = 12 sub_dt = 5 * 60 # five minutes progress = Common.ProgressPrinter(5) pp = display(progress.getPrintString(0), display_id=True) for hour in range(num_total_hours): if hour == start_forecast_hours: # Attach drifters drifters.setDrifterPositions(drifter_pos_init) sim.attachDrifters(drifters) trajectory_forecast.add_observation_from_sim(sim) for mins in range(five_mins_in_an_hour): t = sim.step(sub_dt) if hour >= start_forecast_hours: trajectory_forecast.add_observation_from_sim(sim) pp.update(progress.getPrintString(hour / (end_forecast_hours - 1))) if outfolder is not None: trajectory_forecast.to_pickle(trajectory_forecast_path) return trajectory_forecast