def plotparticlesforecast(data_path, point, filename, t, dts, latbox, lonbox, proj, uncertain): particles = nc_particles.Reader(data_path+'/'+filename) times = particles.times states = NaturalEarthFeature(category="cultural", scale="10m", facecolor="none", name='admin_1_states_provinces_lines') coastline = NaturalEarthFeature(category="physical", scale="10m", facecolor="none", name='coastline') land = NaturalEarthFeature(category="physical", scale="10m", facecolor='lightgray', name='land',) latmin = latbox[0] latmax = latbox[1] lonmin = lonbox[0] lonmax = lonbox[1] for i in np.arange(0, len(times[:]), dts): tnew = t + timedelta(hours=i) dt = [np.abs(((output_t - tnew).total_seconds())/3600) for output_t in times] tidx = dt.index(min(dt)) TheData = particles.get_timestep(tidx, variables=['latitude', 'longitude', 'status_codes', 'depth']) status = TheData['status_codes'] pid = np.where(status == 2)[0] on_land = np.where(status == 3)[0] fig = plt.figure(figsize=(10, 5)) ax = plt.subplot(1, 1, 1, projection=proj) ax.add_feature(coastline, edgecolor='black', zorder=6,) ax.add_feature(land, zorder=10,) ax.add_feature(states, edgecolor='gray', zorder=12,) ax_leg = ax.gridlines(draw_labels=True, linewidth=0.5, linestyle='--') ax_leg.xlabels_top = False ax_leg.ylabels_right = False ax_leg.xlocator = mticker.FixedLocator(range(int(lonmin), int(lonmax) + 2, 2)) ax_leg.xformatter = LONGITUDE_FORMATTER ax_leg.ylocator = mticker.FixedLocator(range(int(latmin), int(latmax) + 2, 2)) ax_leg.yformatter = LATITUDE_FORMATTER ax_leg.xlabel_style = {'size': 10, 'color': 'black'} ax_leg.ylabel_style = {'size': 10, 'color': 'black'} plt.xlim([lonmin, lonmax]) plt.ylim([latmin, latmax]) ax.set_title(str(tnew)+point, {'fontsize': 15}, 'center') if uncertain: particles_uncertain = nc_particles.Reader(data_path+'/'+filename[0:-3]+'_uncertain.nc') TheData_uncertain = particles_uncertain.get_timestep(tidx, variables=['latitude', 'longitude', 'status_codes', 'depth']) status_uncertain = TheData_uncertain['status_codes'] pid_uncertain = np.where(status_uncertain == 2)[0] on_land_uncertain = np.where(status_uncertain == 3)[0] plotScatter_uncertain = plt.scatter(TheData_uncertain['longitude'][pid_uncertain], TheData_uncertain['latitude'][pid_uncertain], s=20, color='red', marker='.') plotScatter_uncertain_onland = plt.scatter(TheData_uncertain['longitude'][on_land_uncertain], TheData_uncertain['latitude'][on_land_uncertain], s=20, color='red', marker='+') plotScatter = plt.scatter(TheData['longitude'][pid], TheData['latitude'][pid], s=20, color='k', marker='.') plotScatter_onland = plt.scatter(TheData['longitude'][on_land], TheData['latitude'][on_land], s=20, color='k', marker='+') plt.savefig(data_path+'/'+'foreground_'+"{0:05d}".format(i)+'.png', bbox_inches = 'tight', pad_inches = 0.1, quality=95) plt.clf() plt.close()
def plot_single_trajectory(ax,filename,particle_id,color='k',addmarker=True,marker='.',markersize='4'): ''' plot single particle trajectory ax: (matplotlib.axes object) the map on which the LEs will be plotted filename: (str) complete path filename of particle file particle_id: (int) particle id status_code: 2 for floating only, 3 for beached only ''' particles = nc_particles.Reader(filename) try: TheData = particles.get_individual_trajectory(particle_id,variables=['latitude','longitude','status_codes']) except: #GUI GNOME < 1.3.10 TheData = particles.get_individual_trajectory(particle_id,variables=['latitude','longitude','status']) TheData['status_codes'] = TheData['status'] ax.plot(TheData['longitude'],TheData['latitude'],transform=ccrs.Geodetic(),color=color) if addmarker: status = TheData['status_codes'] for sc in [2,3]: if sc==3: marker='x' pid = np.where(status==sc)[0] if len(pid) > 0: ax.plot(TheData['longitude'][pid],TheData['latitude'][pid],transform=ccrs.Geodetic(),\ color=color,marker=marker, markersize=markersize,linestyle='None') return ax
def nc2kmz(nc_file, kmz_file=None): """ convert a nc_particles file to kmz :param nc_file: name of nertcdf file to read :param kmz_file=None: name of kmz file to write. If None, the nc_file's name wil be used, with .kmz as teh extansion. """ if kmz_file is None: root = nc_file root = root[:-3] if root.endswith(".nc") else root kmz_file = root + ".kmz" reader = nc_particles.Reader(nc_file) # create a kmz writer: writer = Writer(kmz_file) variables = reader.variables # loop to read / write the data for step, time in enumerate(reader.times): try: timestep = reader.times[step + 1] - time except IndexError: timestep = reader.times[-1] - reader.times[-2] # get the data data = reader.get_timestep(step, variables) writer.write_timestep(time, timestep, data, uncertain=False) writer.close() return kmz_file
def test_get_all_timesteps(): r = nc_particles.Reader('sample.nc') data = r.get_all_timesteps(variables=['depth', 'mass', 'id']) print(data) assert 'depth' in data assert 'mass' in data assert 'id' in data
def test_read_third_timestep(): pf = nc_particles.Reader(test_filename) particles = pf.get_timestep(2, ['latitude', 'status_code']) ## checking against data in "nc_particle_build_sample.py" assert np.array_equal(particles['latitude'], (28.0, 28.0)) assert np.array_equal(particles['status_code'], (2, 3))
def plot_particles(ax,filename,t,depth=0,color='k',marker='.',markersize=4): ''' plot all LEs at one time step ax: (matplotlib.axes object) the map on which the LEs will be plotted filename: (str) complete path filename of particle file t: (datetime obj) closest time to this will be plottted ''' particles = nc_particles.Reader(filename) times = particles.times dt = [np.abs(((output_t - t).total_seconds())/3600) for output_t in times] tidx = dt.index(min(dt)) try: TheData = particles.get_timestep(tidx,variables=['latitude','longitude','status_codes','depth']) except: #GUI GNOME < 1.3.10 TheData = particles.get_timestep(tidx,variables=['latitude','longitude','status','depth']) TheData['status_codes'] = TheData['status'] status = TheData['status_codes'] label = t.isoformat() for sc in [2,3]: if sc==3: marker='x' label=None if depth is not None: pid = np.where((status==sc) & (TheData['depth']==depth))[0] else: pid = np.where(status==sc)[0] if len(pid) > 0: ax.scatter(TheData['longitude'][pid],TheData['latitude'][pid],transform=ccrs.Geodetic(),\ color=color,marker=marker,s=markersize,label=label) print 'Closest time found: ', times[tidx] return ax
def test_init(): """ Can the classes be intitialized? """ w = nc_particles.Writer('junk_file.nc') del w print(os.getcwd()) nc_particles.Reader('sample.nc')
def test_read_variables(): """ does it find the data variables ? """ r = nc_particles.Reader('sample.nc') # set(), because order doesn't matter varnames = set(r.variables) assert varnames == set(['latitude', 'depth', 'mass', 'id', 'longitude'])
def test_read_required(): """ Does it find the required variables and attributes Should be able to set up data_index """ r = nc_particles.Reader('sample.nc') assert len(r.times) == 3 assert np.array_equal(r.data_index, np.array([0, 3, 7, 9]))
def plot_particles(ax,filename,t,depth=0,varname=None,color='k',marker='.',markersize=4,bins=None,binlabs=None): ''' plot all LEs at one time step ax: (matplotlib.axes object) the map on which the LEs will be plotted filename: (str) complete path filename of particle file t: (datetime obj) closest time to this will be plottted ''' particles = nc_particles.Reader(filename) times = particles.times dt = [np.abs(((output_t - t).total_seconds())/3600) for output_t in times] tidx = dt.index(min(dt)) if varname is not None: variables = ['latitude','longitude','status_codes','depth'] + [varname] try: TheData = particles.get_timestep(tidx,variables=variables) except: #GUI GNOME < 1.3.10 TheData = particles.get_timestep(tidx,variables=['latitude','longitude','status','depth']) TheData['status_codes'] = TheData['status'] status = TheData['status_codes'] label = t.isoformat() if varname is None: for sc in [2,3]: if sc==3: marker='x' label=None if depth is not None: pid = np.where((status==sc) & (TheData['depth']==depth))[0] else: pid = np.where(status==sc)[0] if len(pid) > 0: ax.scatter(TheData['longitude'][pid],TheData['latitude'][pid],transform=ccrs.Geodetic(),\ color=color,marker=marker,s=markersize,label=label) else: bins=[6*3600,24*3600] binlabels=['> 24-hrs','< 24-hrs','< 6-hrs'] styles = ['r.','m.','b.'] if bins is not None: pid = np.where((TheData['status_codes']==2) & (TheData['depth']==depth))[0] lon = TheData['longitude'][pid] lat = TheData['latitude'][pid] var2p = TheData[varname][pid] bins = [0] + bins + [var2p.max()] for ii in range(len(bins)-2,-1,-1): print ii id = np.where((var2p>=bins[ii]) & (var2p<=bins[ii+1])) ax.plot(lon[id],lat[id],styles[ii],transform=ccrs.Geodetic()) ax.legend(binlabels) else: pid = np.where((TheData['status_codes']==2) & (TheData['depth']==depth))[0] ax.scatter(TheData['longitude'][pid],TheData['latitude'][pid],10,TheData[varname][pid],transform=ccrs.Geodetic()) print 'Closest time found: ', times[tidx] return ax
def test_get_attributes(): r = nc_particles.Reader('sample.nc') assert r.get_attributes('depth') == { 'units': "meters", 'long_name': "particle depth below sea surface", 'standard_name': "depth", 'axis': "z positive down", }
def test_read_first_timestep(): pf = nc_particles.Reader(test_filename) particles = pf.get_timestep( 0, ['latitude', 'longitude', 'mass', 'status_code']) print particles ## checking against data in "nc_particle_build_sample.py" assert np.array_equal(particles['latitude'], (28.0, 28.0, 28.1)) assert np.array_equal(particles['longitude'], (-88.0, -88.1, -88.1)) assert np.array_equal(particles['mass'], (0.01, 0.005, 0.007)) assert np.array_equal(particles['status_code'], (1, 2, 3))
def plot_particles_3d(ax, filename, t, varname='droplet_diameter', colormap='plasma', color='k', marker='.', drop_size=4, drop_scale_var=None): ''' plot all LEs at one time step ax: (matplotlib.axes object) the map on which the LEs will be plotted filename: (str) complete path filename of particle file t: (datetime obj) closest time to this will be plottted ''' particles = nc_particles.Reader(filename) times = particles.times dt = [ np.abs(((output_t - t).total_seconds()) / 3600) for output_t in times ] tidx = dt.index(min(dt)) try: TheData = particles.get_timestep(tidx, variables=[ 'latitude', 'longitude', 'depth', 'status_codes', varname ]) except: #GUI GNOME < 1.3.10 TheData = particles.get_timestep( tidx, variables=['latitude', 'longitude', 'depth', 'status']) TheData['status_codes'] = TheData['status'] status = TheData['status_codes'] label = t.isoformat() if drop_scale_var is not None: drop_size = drop_size * TheData[drop_scale_var] for sc in [2, 3]: if sc == 3: marker = 'x' label = None pid = np.where(status == sc)[0] if len(pid) > 0: import matplotlib import matplotlib.cm as cmx cs = TheData[varname] cm = plt.get_cmap(colormap) cNorm = matplotlib.colors.Normalize(vmin=min(cs), vmax=max(cs)) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=cm) ax.scatter(TheData['longitude'][pid],TheData['latitude'][pid], TheData['depth'][pid],\ color=scalarMap.to_rgba(cs),marker=marker,label=label, s = drop_size) print('Closest time found: ', times[tidx]) return ax
def points(fn, package_dir, t2convert): nc = Dataset(fn) particles = nc_particles.Reader(nc) times = particles.times dt = [ np.abs(((output_t - t2convert).total_seconds()) / 3600) for output_t in times ] t = dt.index(min(dt)) print 'Converting output from: ', times[t] w = shp.Writer(shp.POINT) w.autobalance = 1 w.field('Year', 'C') w.field('Month', 'C') w.field('Day', 'C') w.field('Hour', 'C') w.field('LE id', 'N') w.field('Depth', 'N') w.field('Mass', 'N') w.field('Age', 'N') w.field('Status_Code', 'N') TheData = particles.get_timestep(t, variables=[ 'latitude', 'longitude', 'id', 'depth', 'mass', 'age', 'status_codes' ]) for k, p in enumerate(zip(TheData['longitude'], TheData['latitude'])): w.point(p[0], p[1]) w.record(times[t].year, times[t].month, times[t].day, times[t].hour, TheData['id'][k], TheData['depth'][k], TheData['mass'][k], TheData['age'][k], TheData['status_codes'][k]) source_fdir = os.path.join(package_dir, 'source_files') shapefile_name = os.path.split(fn)[-1].split('.')[0] w.save(os.path.join(source_fdir, shapefile_name)) nc.close() # create the PRJ file prj_filename = os.path.join(source_fdir, shapefile_name) write_proj_file(prj_filename) files = os.listdir(source_fdir) zipfname = shapefile_name + '.zip' zipf = zipfile.ZipFile(os.path.join(source_fdir, zipfname), 'w') for f in files: if f.split('.')[0] == shapefile_name: zipf.write(os.path.join(source_fdir, f), arcname=f) zipf.close() return zipfname
def contour_particles_gridded(ax, filename, t, varname, depth=0, levels=[0.1, 0.4, 0.8]): ''' contour all LEs at one time step by binning ''' particles = nc_particles.Reader(filename) times = particles.times dt = [ np.abs(((output_t - t).total_seconds()) / 3600) for output_t in times ] tidx = dt.index(min(dt)) variables = ['latitude', 'longitude', 'status_codes', 'depth'] + [varname] try: TheData = particles.get_timestep(tidx, variables=variables) except: #GUI GNOME < 1.3.10 variables = ['latitude', 'longitude', 'status', 'depth'] + [varname] TheData = particles.get_timestep(tidx, variables=variables) TheData['status_codes'] = TheData['status'] pid = np.where((TheData['status_codes'] == 2) & (TheData['depth'] == depth))[0] x = TheData['longitude'][pid] y = TheData['latitude'][pid] varname = TheData[varname][pid] #set up grid x_grid = np.linspace(min(x), max(x), 50) y_grid = np.linspace(min(y), max(y), 50) pc_grid = np.zeros((len(y_grid), len(x_grid)), ) print('Num_particles:', len(x)) for px, py, v in zip(x, y, varname): ii = np.where(px >= x_grid)[0][-1] jj = np.where(py >= y_grid)[0][-1] pc_grid[jj, ii] = pc_grid[jj, ii] + 1 max_value = pc_grid.max() print(max_value) levels.sort() particle_contours = [lev * max_value for lev in levels] print(particle_contours) ax.contourf(x_grid, y_grid, pc_grid, [2, 5, 8, max_value], transform=ccrs.PlateCarree()) #ax.pcolor(xx,yy,f,transform=ccrs.PlateCarree()) print('Closest time found: ', times[tidx]) return ax
def test_get_timestep(): r = nc_particles.Reader('sample.nc') data = r.get_timestep( 2, variables=['latitude', 'depth', 'mass', 'id', 'longitude']) # specific results from the sample file assert np.array_equal(data['longitude'], [-88.3, -88.1]) assert np.array_equal(data['latitude'], [28.1, 28.0]) assert np.array_equal(data['depth'], [0.0, 0.1]) assert np.array_equal(data['mass'], [0.05, 0.06]) assert np.array_equal(data['id'], [1, 3])
def contour_particles(ax,filename,t,depth=0,varname=None,criteria=None,levels=[0.1, 0.4, 0.8, 1]): ''' contour all LEs at one time step ax: (matplotlib.axes object) the map on which the LEs will be plotted filename: (str) complete path filename of particle file t: (datetime obj) closest time to this will be plottted depth: (float) depth of particles to include (all if None) ''' import scipy.stats as st particles = nc_particles.Reader(filename) times = particles.times dt = [np.abs(((output_t - t).total_seconds())/3600) for output_t in times] tidx = dt.index(min(dt)) if varname is not None: variables = ['latitude','longitude','status_codes','depth'] + [varname] try: TheData = particles.get_timestep(tidx,variables=variables) except: #GUI GNOME < 1.3.10 TheData = particles.get_timestep(tidx,variables=['latitude','longitude','status','depth']) TheData['status_codes'] = TheData['status'] if varname is None or criteria is None: pid = np.where((TheData['status_codes']==2) & (TheData['depth']==depth))[0] else: print 'Applying criteria' print TheData[varname].min(),TheData[varname].max() pid = np.where((TheData['status_codes']==2) & (TheData['depth']==depth) & (TheData[varname]<criteria))[0] print len(pid) x = TheData['longitude'][pid] y = TheData['latitude'][pid] # Peform the kernel density estimate xx, yy = np.mgrid[min(x) - .1:max(x) + .1:100j, min(y) - .1:max(y) + .1:100j] positions = np.vstack([xx.ravel(), yy.ravel()]) values = np.vstack([x, y]) kernel = st.gaussian_kde(values) f = np.reshape(kernel(positions).T, xx.shape) max_density = f.max() levels.sort() particle_contours = [lev * max_density for lev in levels] ax.contourf(xx, yy, f, particle_contours,transform=ccrs.PlateCarree()) #ax.pcolor(xx,yy,f,transform=ccrs.PlateCarree()) print 'Closest time found: ', times[tidx] return ax
def plot_all_trajectories(ax, filename, color='slategray', addmarker=False, marker='.', markersize='4'): ''' plot particle trajectories by ids ax: (matplotlib.axes object) the map on which the LEs will be plotted filename: (str) complete path filename of particle file ''' particles = nc_particles.Reader(filename) try: TheData = particles.get_all_timesteps( variables=['latitude', 'longitude', 'id', 'status_codes']) except: #GUI GNOME < 1.3.10 TheData = particles.get_all_timesteps( variables=['latitude', 'longitude', 'id', 'status']) TheData['status_codes'] = TheData['status'] id = np.array(TheData['id']) lon = np.array(TheData['longitude']) lat = np.array(TheData['latitude']) status = np.array(TheData['status']) pids = np.unique(id) for pid in pids: x, y = np.where(id == pid) ax.plot(lon[x, y], lat[x, y], transform=ccrs.Geodetic(), color=color) if addmarker: le_marker = marker for sc in [2, 3]: if sc == 3: le_marker = 'x' sid = np.where(status[x, y] == sc)[0] if len(sid) > 0: ax.plot(lon[x,y][sid],lat[x,y][sid],transform=ccrs.Geodetic(),\ color=color,marker=le_marker, markersize=markersize,linestyle='None') return ax
def points(fn, package_dir, t2convert=None, status_code=None, shapefile_name=None): ''' status_code = 3 for beached only, status_code = 2 for floating only ''' nc = Dataset(fn) particles = nc_particles.Reader(nc) times = particles.times if shapefile_name is None: shapefile_name = os.path.split(fn)[-1].split('.')[0] w = shp.Writer(shp.POINT) w.autobalance = 1 w.field('Time', 'C') w.field('LE id', 'N') w.field('Depth', 'N') w.field('Mass', 'N') w.field('Age', 'N') w.field('status', 'N') w.field('Surf_Conc', 'N', decimal=4) if t2convert is None: ts = range(0, len(times)) else: dt = [ np.abs(((output_t - t2convert).total_seconds()) / 3600) for output_t in times ] ts = [dt.index(min(dt))] for t in ts: print('Converting output from: ', times[t]) TheData = particles.get_timestep(t, variables=[ 'latitude', 'longitude', 'id', 'depth', 'mass', 'age', 'status_codes', 'surface_concentration' ]) if status_code is not None: sc = TheData['status_codes'] id = np.where(sc == status_code)[0] print('found this many pts', len(id)) for key in TheData.keys(): TheData[key] = TheData[key][id] if t == 0: print('first time step') print(np.unique(TheData['longitude'])) print(np.unique(TheData['latitude'])) for k, p in enumerate(zip(TheData['longitude'], TheData['latitude'])): w.point(p[0], p[1]) w.record(times[t].strftime('%Y-%m-%dT%H:%M:%S'), TheData['id'][k], TheData['depth'][k], TheData['mass'][k], TheData['age'][k], TheData['status_codes'][k], TheData['surface_concentration'][k] * 1000.0) source_fdir = os.path.join(package_dir, 'source_files') #shapefile_name = os.path.split(fn)[-1].split('.')[0] if t2convert is None: shapefile_name = shapefile_name + '_all' else: shapefile_name = shapefile_name + '_' + times[t].strftime( '%Y%b%d_%H%M') print('sfn:', shapefile_name) print(os.path.join(source_fdir, shapefile_name)) w.save(os.path.join(source_fdir, shapefile_name)) nc.close() # create the PRJ file prj_filename = os.path.join(source_fdir, shapefile_name) write_proj_file(prj_filename) zipfname = zip_shape_files(source_fdir, shapefile_name) print('zipfname', zipfname) return zipfname
#!/usr/bin/env python """ script to convert nc_particle-sytle netcdf files (as exported by GNOME) to kmz for viewing in Google Earth This is very simple -- it would be nice to expand it with many options! """ import sys from post_gnome import nc_particles from post_gnome.kml_stuff.write_kmz import write_kmz if __name__ == "__main__": nc_filename = sys.argv[1] kmz_filename = nc_filename.rstrip('.nc') + ".kmz" print "processing:", nc_filename print "creating:", kmz_filename pf = nc_particles.Reader(nc_filename) data = pf.get_all_timesteps() write_kmz('kmz_filename', pf.times, data)
def contours(fn, package_dir, t2convert, levels=[0.1, 0.4, 0.8], names=['Light', 'Medium', 'Heavy'], shapefile_name=None, include_beached=False): print("contouring data in:", fn) nc = Dataset(fn) particles = nc_particles.Reader(nc) times = particles.times w = shp.Writer(shp.POLYGON) w.autobalance = 1 w.field('Time', 'C') w.field('Depth', 'N') w.field('Type', 'C') w.field('Test', 'N', decimal=4) if t2convert is None: ts = range(0, len(times)) else: dt = [ np.abs(((output_t - t2convert).total_seconds()) / 3600) for output_t in times ] ts = [dt.index(min(dt))] for t in ts: if t > 0: print('Converting output from: ', times[t]) TheData = particles.get_timestep(t, variables=[ 'latitude', 'longitude', 'id', 'depth', 'mass', 'age', 'status_codes' ]) # contouring status = TheData['status_codes'] if not include_beached: floating = np.where(status == 2)[0] x = TheData['longitude'][floating] y = TheData['latitude'][floating] else: x = TheData['longitude'] y = TheData['latitude'] # Peform the kernel density estimate xx, yy = np.mgrid[min(x) - .1:max(x) + .1:100j, min(y) - .1:max(y) + .1:100j] # to do:should the grid be static? positions = np.vstack([xx.ravel(), yy.ravel()]) values = np.vstack([x, y]) kernel = st.gaussian_kde(values) f = np.reshape(kernel(positions).T, xx.shape) max_density = f.max() levels.sort() particle_contours = [lev * max_density for lev in levels] cs = contour(xx, yy, f, particle_contours) if 0: # plot it import matplotlib as mpl mpl.pyplot.plot(x, y, 'k.') contour(xx, yy, f, particle_contours) mpl.pyplot.show() for c in range(len(cs.collections)): p = cs.collections[c].get_paths()[0] v = p.vertices coords = [[[i[0], i[1]] for i in v]] w.poly(shapeType=5, parts=coords) w.record(times[t].strftime('%Y-%m-%dT%H:%M:%S'), TheData['depth'][c], names[c], v[:, 0].min()) source_fdir = os.path.join(package_dir, 'source_files') if shapefile_name is None: shapefile_name = os.path.split(fn)[-1].split('.')[0] + 'c' if t2convert is None: shapefile_name = shapefile_name + '_all' else: shapefile_name = shapefile_name + '_' + times[t].strftime( '%Y%b%d_%H%M') print('sfn:', shapefile_name) print(os.path.join(source_fdir, shapefile_name)) w.save(os.path.join(source_fdir, shapefile_name)) nc.close() # create the PRJ file prj_filename = os.path.join(source_fdir, shapefile_name) write_proj_file(prj_filename) zipfname = zip_shape_files(source_fdir, shapefile_name) print('zipfname', zipfname) return (zipfname)
def test_read_all_timesteps(): pf = nc_particles.Reader(test_filename) particles = pf.get_all_timesteps(['latitude', 'status_code']) assert np.array_equal(particles['latitude'][2], (28.0, 28.0)) assert np.array_equal(particles['status_code'][2], (2, 3))
def test_data_not_there(): pf = nc_particles.Reader(test_filename) with pytest.raises(KeyError): particles = pf.get_timestep(0, ['random_variable_name'])
def points(fn, package_dir, t2convert): nc = Dataset(fn) particles = nc_particles.Reader(nc) times = particles.times timezone = pytz.timezone("America/Los_Angeles") #this should be passed in w = shp.Writer(shp.POINT) w.autobalance = 1 w.field('Time', 'C') w.field('LE id', 'N') w.field('Depth', 'N') w.field('Mass', 'N') w.field('Age', 'N') w.field('status', 'N') if t2convert is None: ts = range(0, len(times)) else: dt = [ np.abs(((output_t - t2convert).total_seconds()) / 3600) for output_t in times ] ts = [dt.index(min(dt))] for t in ts: print 'Converting output from: ', times[t] TheData = particles.get_timestep(t, variables=[ 'latitude', 'longitude', 'id', 'depth', 'mass', 'age', 'status_codes' ]) #new_t = datetime.datetime(times[t].year,times[t].month,times[t].day,times[t].hour,times[t].minute,times[t].second,tzinfo=timezone) for k, p in enumerate(zip(TheData['longitude'], TheData['latitude'])): if TheData['status_codes'][k] != 10: w.point(p[0], p[1]) w.record(times[t].strftime('%Y-%m-%dT%H:%M:%S -7:00'), TheData['id'][k], TheData['depth'][k], TheData['mass'][k], TheData['age'][k], TheData['status_codes'][k]) source_fdir = os.path.join(package_dir, 'source_files') #shapefile_name = os.path.split(fn)[-1].split('.')[0] if t2convert is None: shapefile_name = os.path.split(fn)[-1].split('.')[0] + '_all' else: shapefile_name = os.path.split(fn)[-1].split( '.')[0] + '_' + times[t].strftime('%Y%b%d_%H%M') print 'sfn:', shapefile_name print os.path.join(source_fdir, shapefile_name) w.save(os.path.join(source_fdir, shapefile_name)) nc.close() # create the PRJ file prj_filename = os.path.join(source_fdir, shapefile_name) write_proj_file(prj_filename) zipfname = zip_shape_files(source_fdir, shapefile_name) print 'zipfname', zipfname return zipfname
def plotraugm(data_path, point, filename, t, dts, latbox, lonbox, proj): # plt.clf() #data_path = '/DATA/forecastData/' #output_test='/DATA/forecastData/Output/2019-06-11/P2/output.nc' currfile = '/DATA/forecastData/Currents/hycom_forecast_20191022.nc' dataset = Dataset(currfile, 'r', format='NETCDF4_CLASSIC') var = dataset.variables lat = var.get('lat')[:] lon = var.get('lon')[:] particles = nc_particles.Reader(data_path + '/' + filename) times = particles.times states = NaturalEarthFeature(category="cultural", scale="10m", facecolor="none", name='admin_1_states_provinces_lines') coastline = NaturalEarthFeature(category="physical", scale="10m", facecolor="none", name='coastline') land = NaturalEarthFeature( category="physical", scale="10m", facecolor='lightgray', name='land', ) latmin = latbox[0] latmax = latbox[1] lonmin = lonbox[0] lonmax = lonbox[1] lol = 0 for i in np.arange(0, len(times[:]) + 1, 24): print lol tnew = t + timedelta(hours=i) u = var.get('u')[lol, 0, :, :] v = var.get('v')[lol, 0, :, :] mag = np.sqrt(u**2 + v**2) #print tnew, ' ', i dt = [ np.abs(((output_t - tnew).total_seconds()) / 3600) for output_t in times ] tidx = dt.index(min(dt)) TheData = particles.get_timestep( tidx, variables=['latitude', 'longitude', 'status_codes', 'depth']) status = TheData['status_codes'] pid = np.where(status == 2)[0] fig = plt.figure(figsize=(10, 5)) #fig.subplots_adjust(left=0.125,right=0.9,top=0.95,bottom=0.1) ax = plt.subplot(1, 1, 1, projection=proj) ax.add_feature( coastline, edgecolor='black', zorder=6, ) ax.add_feature( land, zorder=10, ) ax.add_feature( states, edgecolor='gray', zorder=12, ) ax_leg = ax.gridlines(draw_labels=True, linewidth=0.5, linestyle='--') ax_leg.xlabels_top = False ax_leg.ylabels_right = False ax_leg.xlocator = mticker.FixedLocator( range(int(lonmin), int(lonmax) + 2, 2)) ax_leg.xformatter = LONGITUDE_FORMATTER ax_leg.ylocator = mticker.FixedLocator( range(int(latmin), int(latmax) + 2, 2)) ax_leg.yformatter = LATITUDE_FORMATTER ax_leg.xlabel_style = {'size': 10, 'color': 'black'} ax_leg.ylabel_style = {'size': 10, 'color': 'black'} plt.xlim([lonmin, lonmax]) plt.ylim([latmin, latmax]) ubarbs = u[::6, ::6] vbarbs = v[::6, ::6] lonbarbs = lon[::6] latbarbs = lat[::6] ax.set_title(str(tnew) + ' ' + point, {'fontsize': 15}, 'center') plt.plot(-95.01622889, 25.97096444, marker='*', color='white', markersize=10) cmapm = plt.get_cmap = 'jet' cmap = mpl.cm.cool cs = plt.pcolormesh(lon, lat, mag, cmap=cmapm) plt.quiver(lonbarbs, latbarbs, ubarbs, vbarbs, headwidth=3, headlength=3, width=0.001, color='white') plt.scatter(TheData['longitude'][pid], TheData['latitude'][pid], s=10, color='fuchsia', marker='.') axins1 = inset_axes(ax, width='100%', height='2.5%', loc='lower center', borderpad=-3) bounds = [0, 0.25, 0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 2] ar = fig.colorbar(cs, cax=axins1, orientation='horizontal', ticks=bounds) #,fraction=0.05555) cs.set_clim(0, 2) ar.ax.set_xlabel('Velocidad (m/s)') plt.plot([95.01622889, 25.97096444], marker='*', color='red', markersize=12) plt.savefig(data_path + '/' + 'foreground_' + "{0:05d}".format(i) + '.png', bbox_inches='tight', pad_inches=0.1, quality=95) plt.clf() plt.close() lol = lol + 1
def test_open_with_ncfile(): nc_file = netCDF4.Dataset(test_filename) pf = nc_particles.Reader(nc_file) assert True # it would have failed already...
def test_open_with_filename_fail(): """ a non-existant file """ with pytest.raises(RuntimeError): pf = nc_particles.Reader('random_name')
def test_open_with_filename(): pf = nc_particles.Reader(test_filename) assert True # it would have failed already...
def test_individual_trajectory(): pf = nc_particles.Reader(test_filename) traj = pf.get_individual_trajectory(particle_id=1) assert np.array_equal(traj['latitude'], (28., 28., 28.)) assert np.array_equal(traj['longitude'], (-88.1, -88.1, -88.))
def test_get_units(): pf = nc_particles.Reader(test_filename) print pf.get_units('depth') assert pf.get_units('depth') == 'meters' assert pf.get_units('mass') == 'grams'