def print_contour_map(cs2cs_args, humlon, humlat, glon, glat, datm, sonpath, p, vmin, vmax): #merge, #levels = [0,0.25,0.5,0.75,1.25,1.5,1.75,2,3,5] print("drawing and printing map ...") fig = plt.figure(frameon=False) map = Basemap( projection='merc', epsg=cs2cs_args.split(':')[1], #26949, resolution='i', #h #f llcrnrlon=np.min(humlon) - 0.001, llcrnrlat=np.min(humlat) - 0.001, urcrnrlon=np.max(humlon) + 0.001, urcrnrlat=np.max(humlat) + 0.001) #if dogrid==1: gx, gy = map.projtran(glon, glat) ax = plt.Axes( fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) try: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=300) except: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='ESRI_Imagery_World_2D', xpixels=1000, ypixels=None, dpi=300) #finally: # print "error: map could not be created..." #if dogrid==1: if 2 > 1: if datm.size > 25000000: print( "matrix size > 25,000,000 - decimating by factor of 5 for display" ) #map.contourf(gx[::5,::5], gy[::5,::5], datm[::5,::5], levels, cmap='YlOrRd') map.pcolormesh(gx, gy, datm, cmap='YlOrRd', vmin=vmin, vmax=vmax) else: #map.contourf(gx, gy, datm, levels, cmap='YlOrRd') map.pcolormesh(gx[::5, ::5], gy[::5, ::5], datm[::5, ::5], cmap='pink', vmin=vmin, vmax=vmax) custom_save2(sonpath, 'class_map_imagery' + str(p)) del fig
def make_NYC_basemap(self, ax=None, lllat=40.45, urlat=40.95, lllon=-74.3, urlon=-73.68): ''' This function creates the initial NYC basemap. It is only called once (in the initialization of an NYC_school_interactive_map instance), since it has a relatively long run-time (it queries the ESRI REST server to get a relatively high-resolution basemap image). ''' fig = plt.figure(figsize=(12,12)) ax = plt.subplot(111) m = Basemap(ax = ax, lon_0=(urlon + lllon)/2, lat_0=(urlat + lllat)/2, llcrnrlat=lllat, urcrnrlat=urlat, llcrnrlon=lllon, urcrnrlon=urlon, resolution='f', epsg=3857) ###epsg is the projection code for ESRI world shaded relief basemap #m.arcgisimage queries the ESRI REST API (server below) to get the basemap #http://server.arcgisonline.com/arcgis/rest/services m.arcgisimage(service='World_Shaded_Relief', xpixels = 1500) # Add county lines, since boroughs map onto counties m.drawcounties(linewidth=0.5) return m, ax, fig
def draw_eu(): plt.figure(figsize=(16, 8)) m = Basemap() #m = Basemap(llcrnrlon=-7, llcrnrlat = 32, urcrnrlon=50, urcrnrlat=57, projection= 'lcc' , lat_0 = 46, lon_0=15) m.arcgisimage() for i in range(len(countries)): m.readshapefile( '/Users/tony/MyWork/Python-Exercise/gadm36_eur_shp/gadm36_' + countries[i] + '_shp/gadm36_' + countries[i] + '_0', 'states', drawbounds=True) #m.readshapefile('/Users/tony/MyWork/python/gadm36_IND_shp/gadm36_IND_0', 'states', drawbounds=True) #m.readshapefile('/Users/tony/MyWork/python/gadm36_levels_shp/gadm36_0', 'states', drawbounds=True) #m.drawrivers(color='blue',linewidth=0.3) '''parallels = np.linspace(30,75,5) m.drawparallels(parallels,labels=[True,False,False,False]) meridians = np.linspace(-10,90,5) m.drawmeridians(meridians,labels=[False,False,False,True]) #m.drawlsmask(land_color='coral',ocean_color='aqua',lakes='') #m.bluemarble()''' plt.title(r'$Europe$', fontsize=24) plt.show()
def global_wind(global_path,netcdf_file,title,hours,levels,lon1,lon2,lat1,lat2): wl=[] xx = netcdf_file.variables['x'][:] yy = netcdf_file.variables['y'][:] gridvars = netcdf_file.variables var_element = 'element' elems = gridvars[var_element][:,:]-1 m = Basemap(projection='cyl',llcrnrlat=lat1,urcrnrlat=lat2,llcrnrlon=lon1,urcrnrlon=lon2,resolution='h', epsg = 4269) for i in range(0,hours): i=i+1 u = netcdf_file.variables['windx'][i,:] v = netcdf_file.variables['windy'][i,:] file_number = '%02d'%i m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 900, verbose= False) m.drawcoastlines(color='k') plt.quiver(xx,yy,u,v) plt.xlim([lon1, lon2]) plt.ylim([lat1, lat2]) wl.append('WL{}.png'.format(file_number)) plt.title(title + '\n') plt.savefig('WL{}.png'.format(file_number),dpi=300, bbox_inches = 'tight', pad_inches = 0.1) plt.close() images = [] for ii in range(0,len(wl)): frames = Image.open(wl[ii]) images.append(frames) images[0].save(title.split(' ')[0]+title.split(' ')[1]+'.gif', save_all=True, append_images=images[1:], delay=.1, duration=300, loop=0) for f in glob.glob('WL*'): os.remove(f) return
def spatialSubPlot(self, platformLineStringHash, ax, e, resolution='l'): ''' Make subplot of tracks for all the platforms within the time range. ''' m = Basemap(llcrnrlon=e[0], llcrnrlat=e[1], urcrnrlon=e[2], urcrnrlat=e[3], projection='cyl', resolution=resolution, ax=ax) ##m.wmsimage('http://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?', layers=['GEBCO_08_Grid']) # Works, but coarse m.arcgisimage(server='http://services.arcgisonline.com/ArcGIS', service='Ocean_Basemap') for pl, LS in platformLineStringHash.iteritems(): x, y = zip(*LS) m.plot(x, y, '-', c=self._getColor(pl), linewidth=3) if self.args.mapLabels: m.drawparallels(np.linspace(e[1], e[3], num=3), labels=[True, False, False, False], linewidth=0) m.drawmeridians(np.linspace(e[0], e[2], num=3), labels=[False, False, False, True], linewidth=0) return ax
class LocationOverlay(PlotOverlay): def __init__(self, layout): super().__init__(layout) ratio = float(layout.width) / layout.height map_width = 3e6 map_height = (map_width / ratio) padding = 0.005 self.map = Basemap(epsg=4326, lat_0=data['latitude'].mean(), llcrnrlat=data['latitude'].min() - padding, urcrnrlat=data['latitude'].max() + padding, llcrnrlon=data['longitude'].min() - padding, urcrnrlon=data['longitude'].max() + padding, lon_0=data['longitude'].mean()) self.map.arcgisimage(service='ESRI_Imagery_World_2D') def update(self, df): self.ax.plot(df['longitude'], df['latitude'], color='red') #self.ax.plot(x, y, 'ok', markersize=5) #self.ax.text(x, y, ' Seattle', fontsize=12) super().update()
def draw_map(plot_title, data_dict): """ Slightly changed version of Assignment-3 mapping code. Draws a map of Southern Ontario with temperature station names. The location of values are determined by the latitude and longitude. A dictionary (data_dict) is used to provide a map from station_name names to a tuple containing the (latitude, longitude, value) used for drawing. :param plot_title: Title for the plot. :param data_dict: A dictionary Station ID -> tuple(Latitude, Longitude, Uncertainty Value) """ fig = plt.figure(figsize=(12, 12)) map1 = Basemap(projection='cyl', resolution='c', width=8E6, height=8E6, llcrnrlat=42, urcrnrlat=45., llcrnrlon=-84., urcrnrlon=-74) map1.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=2000, verbose=True) for station_name_name in data_dict: data = data_dict[station_name_name] print(station_name_name, data) x, y = map1(data[1], data[0]) value = data[2] color = 'white' plt.plot(x, y, 'ok', markersize=3, color='red') plt.text(x, y, '{}'.format(value), fontsize=8, color=color) plt.title(plot_title) plt.show()
def map_prep(LLcoord, URcoord, layer, axes): resolution = 700 EPSG = 3978 map1 = Basemap(llcrnrlon=LLcoord[1], llcrnrlat=LLcoord[0], urcrnrlon=URcoord[1], urcrnrlat=URcoord[0], epsg=EPSG, ax=axes, anchor="NW") if layer == "satellite": s = 'ESRI_Imagery_World_2D' elif layer == "streets": s = 'ESRI_StreetMap_World_2D' else: return "Not a valid imagery type.\n Imagery type must be 'satellite' or 'streets'." map1.arcgisimage(service=s, verbose=True, xpixels=1000, dpi=resolution, ax=axes) return map1
def plot_scatter_map(figsize, x, y, c=None, colormap=None, cbar=True, legend_labels=None, c_bar_shrink=1, xlabel=None, ylabel=None, clabel=None, s=25, linewidth=0.25, alpha=1, epsg_code=4326, dpi=160, service='Canvas/World_Light_Gray_Base'): m = Basemap( urcrnrlat=y.max(), # top urcrnrlon=x.max(), # bottom llcrnrlat=y.min(), # left llcrnrlon=x.min(), # right epsg=epsg_code) width = figsize[0] height = figsize[1] fig = plt.gcf() fig.set_size_inches(width, height) dpi = dpi xpixels = dpi * width m.arcgisimage(service=service, xpixels=xpixels) plt.scatter(x=x, y=y, c=c, cmap=colormap, s=s, alpha=alpha, linewidth=linewidth, edgecolor='Black', label=legend_labels) plt.xticks(np.linspace(start=x.min(), stop=x.max(), num=np.ceil(width / 2).astype(int)).round(3), fontsize=15) plt.yticks(np.linspace(start=y.min(), stop=y.max(), num=np.ceil(height / 2).astype(int)).round(3), fontsize=15) plt.xlabel(x.name if xlabel is None else xlabel, fontsize=20) plt.ylabel(y.name if ylabel is None else ylabel, fontsize=20) if c is not None and cbar: cbar = plt.colorbar(orientation='vertical', shrink=c_bar_shrink) cbar.set_label(c.name if clabel is None else clabel, rotation=90, fontsize=20) if legend_labels: cbar.set_ticks([*range(len(legend_labels))]) cbar.set_ticklabels(legend_labels) plt.clim(-0.5, len(legend_labels) - 0.5) cbar.ax.set_yticklabels(cbar.ax.get_yticklabels(), fontsize=15) return plt.gcf(), plt.gca()
def make_NYC_basemap(self, ax=None, lllat=40.45, urlat=40.95, lllon=-74.3, urlon=-73.68): ''' This function creates the initial NYC basemap. It is only called once (in the initialization of an NYC_school_interactive_map instance), since it has a relatively long run-time (it queries the ESRI REST server to get a relatively high-resolution basemap image). ''' fig = plt.figure(figsize=(12, 12)) ax = plt.subplot(111) m = Basemap( ax=ax, lon_0=(urlon + lllon) / 2, lat_0=(urlat + lllat) / 2, llcrnrlat=lllat, urcrnrlat=urlat, llcrnrlon=lllon, urcrnrlon=urlon, resolution='f', epsg=3857 ) ###epsg is the projection code for ESRI world shaded relief basemap #m.arcgisimage queries the ESRI REST API (server below) to get the basemap #http://server.arcgisonline.com/arcgis/rest/services m.arcgisimage(service='World_Shaded_Relief', xpixels=1500) # Add county lines, since boroughs map onto counties m.drawcounties(linewidth=0.5) return m, ax, fig
def createMap(self, res='l', proj='merc', xpix=1000, figsize=(10, 10)): fig = plt.figure(figsize=(10, 10)) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) m = Basemap(llcrnrlon=self.lons[0], llcrnrlat=self.lats[0], urcrnrlon=self.lons[1], urcrnrlat=self.lats[1],\ resolution=res,projection=proj, epsg=self.EPSG) m.arcgisimage(service=self.service, xpixels=xpix, verbose=True) return m
def map_rupture(rupture): """ Method for making a simple representation of a Rupture instance. This method draws the surface projection of the rupture on a map. Args: rupture: A Rupture instance. """ rlats = rupture.lats rlons = rupture.lons minbufx = 0.2 minbufy = 0.2 lat1 = np.nanmin(rupture.lats) lat2 = np.nanmax(rupture.lats) dlat = lat2 - lat1 bufy = np.max([dlat/2, minbufy]) lon1 = np.nanmin(rupture.lons) lon2 = np.nanmax(rupture.lons) dlon = lon2 - lon1 bufx = np.max([dlon/2, minbufx]) m = Basemap(llcrnrlat=lat1-bufy,urcrnrlat=lat2+bufy, llcrnrlon=lon1-bufy,urcrnrlon=lon2+bufy) m.arcgisimage(service='World_Shaded_Relief', xpixels = 500, verbose= True) x, y = m(rlons, rlats) m.plot(x, y, 'r')
def make_grid_map(x, y, themap='', service='shadedrelief', xpixels=500, EPSG=2229, **scatter_args): """Overlays a grid of points (passed as x,y for longitude, latitude) on a map, which is by default shaded relief (from www.shadedrelief.com). Can also use arcGIS ESRI map services (e.g. StreetMap_World). The EPSG code defines the map projection (default value here covers Southern California). """ fig, ax = plt.subplots(figsize=(6, 6)) if themap == '': themap = Basemap(llcrnrlon=min(x), llcrnrlat=min(y), urcrnrlon=max(x), urcrnrlat=max(y), epsg=EPSG, ax=ax) elif not isinstance(themap, Basemap): raise Exception('Input projection map themap is not Basemap instance.') if service == 'shadedrelief': themap.shadedrelief() else: try: themap.arcgisimage(service=service, xpixels=xpixels, ax=ax) except: themap.arcgisimage(service='World_Street_Map', xpixels=xpixels, ax=ax) xm, ym = themap(x, y) ax.scatter(xm, ym, **scatter_args) if themap == '': return fig, themap else: return fig
def EQ_map(lon,lat,M,source, region, ID): llcrnrlon, llcrnrlat, urcrnrlon, urcrnrlat = -84, -7, -64, 15 if (llcrnrlon < float(lon) < urcrnrlon) and (llcrnrlat < float(lat) < urcrnrlat): map = Basemap(llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat,urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat, projection='cyl', resolution=None) x, y = map(float(lon), float(lat)) map.scatter(x, y, 500, color="yellow", marker="*", edgecolor="k", zorder=3) map.arcgisimage(service='NatGeo_World_Map', xpixels = 1500, verbose= True) plt.title('M:'+M+','+region+', Fuente:'+source, fontsize='20') name = ID+'.jpg' if os.path.exists(name) == False: plt.savefig(name) os.chmod(sfile_name,stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) else: pass else: map = Basemap(projection='ortho', lat_0=float(lat), lon_0=float(lon), resolution='l') map.drawmapboundary(fill_color=(0.478,0.859,0.988)) map.fillcontinents(color=(0.235,0.705,0.427), lake_color=(0.478,0.859,0.988)) map.drawparallels(np.arange(-90.,120.,30.)) map.drawmeridians(np.arange(0.,420.,60.)) x, y = map(float(lon), float(lat)) map.scatter(x, y, 800, color="yellow", marker="*", edgecolor="k", zorder=3) plt.title('M:'+M+','+region+', Fuente:'+source, fontsize='20') name = ID+'.jpg' if os.path.exists(name) == False: plt.savefig(name) os.chmod(name,stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) else: pass return name
def drawMap_basemap(base_coords, calculated_coords, ratios): deg_diff = 1.5 m = Basemap(llcrnrlon=(float(base_coords[3][1])), llcrnrlat=(float(base_coords[3][0]) - deg_diff), urcrnrlon=(float(base_coords[3][1]) + deg_diff), urcrnrlat=(float(base_coords[3][0]) + deg_diff), epsg=5520) m.arcgisimage(service='ESRI_StreetMap_World_2D', xpixels=1500, verbose=False) #Paint coords colors = ['red', 'green', 'blue', 'purple'] counter = 0 for coord in base_coords: drawPoint(coord[0], coord[1], m, colors[counter]) counter += 1 #Paint polygon lats = [] lons = [] for coord in calculated_coords: lats.append(coord[0]) lons.append(coord[1]) x, y = m(lons, lats) xy = zip(x, y) poly = Polygon(list(xy), facecolor='yellow', alpha=0.4) plt.gca().add_patch(poly) plt.show()
def get_projection(projection="germany", countries=True, regions=False, labels=False): if projection=="germany": m = Basemap(projection='cyl', llcrnrlon=5, llcrnrlat=46,\ urcrnrlon=16, urcrnrlat=56, resolution='i') m.drawcoastlines() if countries: m.drawcountries() if regions: m.readshapefile('/home/mpim/m300382/shapefiles/DEU_adm_shp/DEU_adm1', 'DEU_adm1',linewidth=0.2,color='black') if labels: m.drawparallels(np.arange(-80.,81.,10), linewidth=0.2, labels=[True, False, False, True]) m.drawmeridians(np.arange(-180.,181.,10), linewidth=0.2, labels=[True, False, False, True]) img=m.arcgisimage(service='World_Shaded_Relief', xpixels = 1000, verbose= True) img.set_alpha(0.8) elif projection=="italy": m = Basemap(projection='cyl', llcrnrlon=5, llcrnrlat=43.5,\ urcrnrlon=15, urcrnrlat=48, resolution='i') m.drawcoastlines() if countries: m.drawcountries() if regions: m.readshapefile('/home/mpim/m300382/shapefiles/ITA_adm_shp/ITA_adm1', 'ITA_adm1',linewidth=0.2,color='black') if labels: m.drawparallels(np.arange(-80.,81.,10), linewidth=0.2, labels=[True, False, False, True]) m.drawmeridians(np.arange(-180.,181.,10), linewidth=0.2, labels=[True, False, False, True]) img=m.arcgisimage(service='World_Shaded_Relief', xpixels = 1000, verbose= True) img.set_alpha(0.8) return(m)
def arc_map(region=[-33.7, 149.4, -31.8, 151.8], res=1000, service='World_Topo_Map',epsg=4326, resolution='h',projection='mill'): ''' Create and return the thingy Inputs: region: [bot,left,top,right] service= one of 'World_Imagery', or 'World_Topo_Map' Imagery is more like blue marble, with no site labels Topo has inbuilt site labels, which render based on the resolution -> higher res for more zoomed in image. epsg: tell arcGIS where to pull image from, affects the image orientation Use 4326 for Sydney centric maps Use 3577 for Australia centred view ''' # set up basemap projection # epsg is used to inform basemap that we will pull an image from ArcGIS servers # epsg=28355 does not work... # 3577 is centred at middle of Australia... # 4326 works for East Australia! m = Basemap(projection=projection, epsg=epsg, resolution=resolution, llcrnrlon=region[1], llcrnrlat=region[0], urcrnrlon=region[3], urcrnrlat=region[2]) # Download backgroud to basemap's projection. #m.arcgisimage(service='World_Topo_Map', xpixels=res, verbose=True) m.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service=service, xpixels=res, verbose=True) return m
def draw_map(name, coords, dataset): llon = coords['ll_long'] llat = coords['ll_lat'] rlon = coords['ur_long'] rlat = coords['ur_lat'] clat = (llat + rlat) / 2 clon = (llon + rlon) / 2 m = Basemap(llcrnrlon=llon, llcrnrlat=llat, urcrnrlon=rlon, urcrnrlat=rlat, epsg=2163) m.arcgisimage(service='NatGeo_World_Map', xpixels=2000, verbose=True) data = get_data_for_region(dataset, name) case_lats = data['Lat'].values.transpose() case_longs = data['Long'].values.transpose() x, y = m(case_longs, case_lats) m.scatter(x, y, c='r') for city, coord in coords['cities'].items(): x, y = m(*coord) m.plot(x, y, 'b,') plt.text(x, y, city)
def ss_plot(): #Pandas method of importing data frame and getting extents conn = psycopg2.connect("dbname='reach_4a' user='******' host='localhost' port='9000'") df = pd.read_sql_query('select easting, northing, texture, sidescan_intensity from mosaic_2014_09', con=conn) minE = min(df['easting']) maxE = max(df['easting']) minN = min(df['northing']) maxN = max(df['northing']) conn.close() print 'Done Importing Data from Database' #Create grid for countourf plot res = 0.25 grid_x, grid_y = np.meshgrid( np.arange(np.floor(minE), np.ceil(maxE), res), np.arange(np.floor(minN), np.ceil(maxN), res)) grid_lon, grid_lat = trans(grid_x,grid_y,inverse=True) #Re-sampling procedure m_lon, m_lat = trans(df['easting'].values.flatten(), df['northing'].values.flatten(), inverse=True) orig_def = geometry.SwathDefinition(lons=m_lon, lats=m_lat) target_def = geometry.SwathDefinition(lons=grid_lon.flatten(), lats=grid_lat.flatten()) print 'Now Resampling...' result = kd_tree.resample_nearest(orig_def, df['sidescan_intensity'].values.flatten(), target_def, radius_of_influence=1, fill_value=None, nprocs = cpu_count()) print 'Done Resampling!!!' #format side scan intensities grid for plotting gridded_result = np.reshape(result,np.shape(grid_lon)) gridded_result = np.squeeze(gridded_result) gridded_result[np.isinf(gridded_result)] = np.nan gridded_result[gridded_result<=0] = np.nan grid2plot = np.ma.masked_invalid(gridded_result) # x = df['easting'] # y = df['northing'] # z = df['sidescan_intensity'] # # xi = df['easting'] # yi = df['northing'] # # X,Y= np.meshgrid(xi,yi) # grid_lon, grid_lat = trans(X,Y,inverse=True) # Z = griddata((x, y), z, (X, Y),method='nearest') # print 'Done Gridding Data' print 'Now mapping...' #Create Figure fig = plt.figure(frameon=True) ax = plt.subplot(1,1,1) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], llcrnrlon=np.min(grid_lon)-0.0009, llcrnrlat=np.min(grid_lat)-0.0009,urcrnrlon=np.max(grid_lon)+0.0009, urcrnrlat=np.max(grid_lat)+0.0009) gx,gy = map.projtran(grid_lon,grid_lat) map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=1200) im = map.pcolormesh(gx, gy, grid2plot, cmap='gray',vmin=0.1, vmax=30) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.1) cbr = plt.colorbar(im, cax=cax) cbr.set_label('Sidescan Intensity [dBw]', size=8) for t in cbr.ax.get_yticklabels(): t.set_fontsize(8) plt.show()
def Map(self): df, label = self.coordinates() # we get the dataframe and label from the previous func. # we created a box that will contain the coordinates of the borders of our plot # we added some adjustment to the border in order to leave some space between the # points and the borders BBox = (df.longitude.min()-0.07, df.latitude.min()-0.01, df.longitude.max()+0.07, df.latitude.max()+0.01) x0, y0 ,x1, y1 = BBox cx, cy = (x0 + x1) / 2, (y0 + y1) / 2 fig = plt.figure(figsize=(18,18)) # we increase the size of our plot for a better # visualization map = Basemap( epsg = 2872, projection='merc', lat_0 = cy, lon_0 = cx, resolution = 'i', llcrnrlon=x0, llcrnrlat=y0, urcrnrlon=x1, urcrnrlat=y1) # apply the basemap function # we plot the street on the background map.arcgisimage(service='ESRI_StreetMap_World_2D', xpixels = 12000, verbose= True) # then we plot the plots and the lines connecting them lon1, lat1 = map(df.longitude.values, df.latitude.values) map.plot(lon1, lat1, marker="o") # we mark with a start the original nodes for i in label: x,y = map(i[0], i[1]) if self.flag == 2: map.plot(x, y, 'r*', markersize=16) else: f = df[df.longitude == i[0]].index.tolist()[0] map.plot(x, y, '*', markersize=16, label = f) plt.legend() plt.show()
def make_map(lon_e=-32.08, lat_e=-64.53, width_e=0.01, height_e=0.02, angle_e=30, lon_p=-32.085, lat_p=-64.53, lon_s=-32.083, lat_s=-64.532): ''' Crea una imagen centrada en la persona, dibuja la zona de peligro y la salida. Parameters: ----------- lon_e: lon elipse (float) lat_e: lat elipse (float) width_e: width elipse (float) height_e: height elipse (float) angle_e: angle elipse (float) lon_p: lon persona (float) lat_p: lat persona (float) lon_s: lon salida (float) lat_s: lat salida (float) Return: ------- file_name: nombre del archivo ''' plt.figure(figsize=(12,12)) # Encontramos las esquinas del mapa, centrado en la persona. m = Basemap(llcrnrlon=lat_p - 0.05,llcrnrlat=lon_p - 0.05 , urcrnrlon=lat_p + 0.05,urcrnrlat=lon_p + 0.05, epsg=4221) #http://server.arcgisonline.com/arcgis/rest/services # One can use two kinds of streetmaps: ESRI_Imagery_World_2D or World_Street_Map m.arcgisimage(service='World_Street_Map', xpixels = 500, ypixels=500, verbose= True) #x, y = m(lat,lon) e = Ellipse(xy=[lat_e,lon_e], width=width_e, height=height_e, angle=angle_e) e.set_alpha(0.3) ax = plt.gca() ax.add_artist(e) # Agregamos a la persona m.scatter(lat_p, lon_p, marker='o',alpha=0.5,color='k',s=100) # Agregamos el fuego #m.scatter(lat_f, lon_f, marker='o', alpha=0.7, color='r', s=200) dx = lat_p - lat_s dy = lon_p - lon_s # Agregamos la flecha de la direccion de salida. ax.arrow(lat_p, lon_p, dx, dy, head_width=0.002, head_length=0.002, fc='k', ec='k') # Si no existe la carpeta la creamos. if not os.path.isdir('maps'): os.mkdir('maps') # Creamos el nombre en funcion de la pos de la persona file_name = "map_" + str(lon_p) + "_" + str(lat_p) + ".png" plt.savefig("maps/" + file_name)
def plot_arcgis_nyc_map(coords, hotel_name, directory, service='World_Street_Map', xpixels=800, dpi=150, title=None): ''' Given a set of (longitude, latitude) coordinates, plot a heatmap of them onto an ARCGIS basemap of NYC. ''' if title == None: print('Plotting heatmap for %s\n' % hotel_name) else: print('Plotting heatmap for %s\n' % title) plt.rcParams["figure.figsize"] = (18.5, 9.75) # Create Basemap object (bounds NYC) and draw map of NYC. basemap = Basemap(llcrnrlon=-74.025, llcrnrlat=40.63, urcrnrlon=-73.76, urcrnrlat=40.85, epsg=4269) basemap.arcgisimage(service=service, xpixels=xpixels, dpi=dpi) # set up grid coordinates and get binned coordinates from taxicab data x, y = np.linspace(basemap.llcrnrlon, basemap.urcrnrlon, 250), np.linspace(basemap.llcrnrlat, basemap.urcrnrlat, 250) bin_coords, xedges, yedges = np.histogram2d(coords[1], coords[0], bins=(x, y), normed=True) x, y = np.meshgrid(xedges, yedges) to_draw = np.ma.masked_array(bin_coords, bin_coords < 0.001) / np.sum(bin_coords) plt.pcolormesh(x, y, to_draw.T, cmap='rainbow', vmin=0.001, vmax=1.0) plt.colorbar(norm=mcolors.NoNorm) if title == None: plt.title('%s (satisfying trips: %d)' % (hotel_name, len(coords[1]))) else: plt.title('%s (satisfying trips: %d)' % (title, len(coords[1]))) if not os.path.isdir(directory): os.makedirs(directory) # Save ARCGIS plot. plt.savefig(os.path.join(directory, hotel_name)) plt.clf() plt.close() # Set zero-valued bins to small non-zero values and normalize. bin_coords[np.where(bin_coords == 0)] = 1e-32 normed = (bin_coords / np.sum(bin_coords)).ravel() return normed
def plot_arcgis_nyc_scatter_plot(coords, hotel_name, directory, service='World_Street_Map', xpixels=800, dpi=150, title=None): ''' Given a set of (longitude, latitude) coordinates, plot a heatmap of them onto an ARCGIS basemap of NYC. ''' if title == None: print('Plotting scatter plot for %s\n' % hotel_name) else: print('Plotting scatter plot for %s\n' % title) # size of figures in inches plt.rcParams["figure.figsize"] = (18.5, 9.75) # create Basemap object (bounds NYC) and draw high-resolution map of NYC on it basemap = Basemap(llcrnrlon=-74.025, llcrnrlat=40.63, urcrnrlon=-73.76, urcrnrlat=40.85, epsg=4269) basemap.arcgisimage(service=service, xpixels=xpixels, dpi=dpi) # set up grid coordinates and get binned coordinates from taxicab data x, y = np.linspace(basemap.llcrnrlon, basemap.urcrnrlon, 250), np.linspace(basemap.llcrnrlat, basemap.urcrnrlat, 250) bin_coords, xedges, yedges = np.histogram2d(coords[1], coords[0], bins=(x, y), normed=True) x, y = np.meshgrid(xedges, yedges) to_draw = np.ma.masked_array(bin_coords, bin_coords < 0.001) / np.sum(bin_coords) plt.scatter(coords[1], coords[0], s=5) if title == None: plt.title('%s (satisfying trips: %d)' % (hotel_name, len(coords[1]))) else: plt.title('%s (satisfying trips: %d)' % (title, len(coords[1]))) if not os.path.isdir(directory): os.makedirs(directory) # Save ARCGIS plot. plt.savefig(os.path.join(directory, hotel_name + '.png')) plt.clf() plt.close() # setting zero-valued bins to small non-zero values (for KL divergence) bin_coords[np.where(bin_coords == 0)] = 1e-32 normed = (bin_coords / np.sum(bin_coords)).ravel() return normed
def pressure_wind(global_path,netcdf_file,netcdf_file2,title,hours,levels,lon1,lon2,lat1,lat2,start=None,grid_space=None): start_date = datetime.strptime(start,'%Y%m%d%H') wl=[] xx = netcdf_file.variables['x'][:] yy = netcdf_file.variables['y'][:] xx2 = netcdf_file2.variables['x'][:] yy2 = netcdf_file2.variables['y'][:] xg = np.linspace(lon1+0.5,lon2-0.5,grid_space) yg = np.linspace(lat1+0.5,lat2-0.5,grid_space) xgrid,ygrid = np.meshgrid(xg,yg) gridvars = netcdf_file.variables var_element = 'element' elems = gridvars[var_element][:,:]-1 m = Basemap(projection='cyl',llcrnrlat=lat1,urcrnrlat=lat2,llcrnrlon=lon1, urcrnrlon=lon2,resolution='h', epsg = 4269) for i in range(0,hours): i=i+1 data1 = netcdf_file.variables['pressure'][i,:] u = netcdf_file2.variables['windx'][i,:] v = netcdf_file2.variables['windy'][i,:] ugrid = scipy.interpolate.griddata((xx2,yy2),u,(xgrid,ygrid),method='nearest') vgrid = scipy.interpolate.griddata((xx2,yy2),v,(xgrid,ygrid),method='nearest') #u_norm = ugrid / np.sqrt(ugrid ** 2.0 + vgrid ** 2.0) #v_norm = vgrid / np.sqrt(ugrid ** 2.0 + vgrid ** 2.0) file_number = '%05d'%i triang = tri.Triangulation(xx,yy, triangles=elems) m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 600, verbose= False) m.drawcoastlines(color='k') if data1.mask.any(): point_mask_indices = np.where(data1.mask) tri_mask = np.any(np.in1d(elems, point_mask_indices).reshape(-1, 3), axis=1) triang.set_mask(tri_mask) plt.xlim([lon1, lon2]) plt.ylim([lat1, lat2]) plt.tricontourf(triang, data1, levels=levels,alpha=0.9,vmin=8.75, vmax=10.6, aspect='auto',cmap='jet') wl.append('WL{}.png'.format(file_number)) cb = plt.colorbar(cmap='jet',fraction=0.026,pad=0.04) cb.set_label('Pressure (kPa)',fontsize=10) plt.quiver(xgrid,ygrid,ugrid,vgrid, pivot='mid', scale = 600, color='w') plt.title(title + '\n') plt.xlabel('\nDate:{}'.format(start_date+ timedelta(hours=i))) plt.savefig('WL{}.png'.format(file_number),dpi=400, bbox_inches = 'tight', pad_inches = 0.1) plt.close() images = [] for ii in range(0,len(wl)): frames = Image.open(wl[ii]) images.append(frames) images[0].save('PressureWind.gif', save_all=True, append_images=images[1:], delay=.1, duration=300, loop=0) for f in glob.glob('WL*'): os.remove(f) return
def Helheim_map(service='ESRI_Imagery_World_2D', epsg=3413, xpixels=2000): """Function using Basemap to plot map for only Helheim Glacier """ m = Basemap(projection='npstere', boundinglat=70, lon_0=315, epsg=epsg, llcrnrlon=320.3, llcrnrlat=66.1, urcrnrlon=322.4, urcrnrlat=67.6, resolution='h') plt.figure() m.arcgisimage(service=service, xpixels=xpixels) plt.show() return m
def SermeqKujalleq_map(service='ESRI_Imagery_World_2D', epsg=3413, xpixels=2000): """Function using Basemap to plot map for only Sermeq Kujalleq """ m = Basemap(projection='npstere', boundinglat=70, lon_0=315, epsg=epsg, llcrnrlon=309.6, llcrnrlat=68.9, urcrnrlon=314.6, urcrnrlat=69.6, resolution='h') plt.figure() m.arcgisimage(service=service, xpixels=xpixels) plt.show() return m
def KogeBugt_map(service='ESRI_Imagery_World_2D', epsg=3413, xpixels=2000): """Function using Basemap to plot map for only Kangerlussuaq """ m = Basemap(projection='npstere', boundinglat=70, lon_0=315, epsg=epsg, llcrnrlon=318.2, llcrnrlat=64.9, urcrnrlon=319.4, urcrnrlat=65.4, resolution='h') plt.figure() m.arcgisimage(service=service, xpixels=xpixels) plt.show() return m
def Greenland_map(service='ESRI_Imagery_World_2D', epsg=3413, xpixels=5000): """Function using Basemap to plot map for all of Greenland """ m = Basemap(projection='npstere', boundinglat=70, lon_0=315, epsg=epsg, llcrnrlon=300, llcrnrlat=57, urcrnrlon=20, urcrnrlat=80, resolution='h') plt.figure() m.arcgisimage(service=service, xpixels=xpixels) plt.show() return m
def main(): G = nx.Graph() G = nx.read_gpickle("mendocino.gpickle") ############################################################################### # The following code is just for plotting a figure. ############################################################################### m = Basemap( projection='merc', ellps = 'WGS84', llcrnrlon=-123.85, llcrnrlat=38.7, urcrnrlon=-123.5, urcrnrlat=39.3, lat_ts=0, epsg = 4269, resolution='h', suppress_ticks=True) colors = [] size = [] for node in G.nodes(): lat,lng = G.node[node]['pos'] if lat == 0 or lng == 0: G.remove_node(node) continue nodetype = G.node[node]['type'] if nodetype == 'cpe': #G.remove_node(node) #colors.append('b') #size.append(5) continue #elif nodetype == 'wireless-bridge-master': #colors.append('k') #size.append(20) #elif nodetype == 'wireless-bridge-client': # colors.append('w') # size.append(20) #elif nodetype == 'router': # colors.append('yellow') # size.append(35) #elif 'switch' in nodetype: # colors.append('green') # size.append(5) else: colors.append('red') size.append(25) edges,edgecolors = zip(*nx.get_edge_attributes(G,'color').items()) m.arcgisimage(service='World_Shaded_Relief', xpixels = 5000, verbose= True,dpi=300) scalebar = ScaleBar(101233.3,length_fraction=0.3) # 1 pixel = 6.074 meter plt.gca().add_artist(scalebar) nx.draw(G, nx.get_node_attributes(G, 'pos'), node_color=colors, with_labels=False, weight=0.25, node_size=size) plt.savefig("mendocino_topologymap.pdf",bbox_inches='tight',dpi=300)
def swan(global_path,netcdf_file,title,hours1,levels,lon1,lon2,lat1,lat2,start=None): start_date = datetime.strptime(start,'%Y%m%d%H') wl=[] xx = netcdf_file.variables['x'][:] yy = netcdf_file.variables['y'][:] gridvars = netcdf_file.variables var_element = 'element' elems = gridvars[var_element][:,:]-1 m = Basemap(projection='cyl',llcrnrlat=lat1,urcrnrlat=lat2, llcrnrlon=lon1,urcrnrlon=lon2,resolution='h', epsg = 4269) #data2 = netcdf_file.variables['zeta'][:] for i in range(0,hours1): #i=i+1 data1 = netcdf_file.variables['swan_HS'][i,:]*3.28084 #data2 = netcdf_file2.variables['swan_DIR'][i,:] #ugrid = scipy.interpolate.griddata((xx2,yy2),u,(xgrid,ygrid),method='nearest') #vgrid = scipy.interpolate.griddata((xx2,yy2),v,(xgrid,ygrid),method='nearest') file_number = '%05d'%i triang = tri.Triangulation(xx,yy, triangles=elems) m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=600, verbose= False) #m.drawcoastlines(color='k') if data1.mask.any(): point_mask_indices = np.where(data1.mask) tri_mask = np.any(np.in1d(elems, point_mask_indices).reshape(-1, 3), axis=1) triang.set_mask(tri_mask) plt.xlim([lon1, lon2]) plt.ylim([lat1, lat2]) plt.tricontourf(triang, data1, levels=levels,alpha=0.75, vmin=np.min(levels), vmax=np.max(levels), aspect='auto',cmap='jet') wl.append('WL{}.png'.format(file_number)) cb = plt.colorbar(cmap='jet',fraction=0.026,pad=0.04) cb.set_label('Wave Height (ft)',fontsize=10) plt.title(title + '\n') plt.xlabel('\nDate:{}'.format(start_date+ timedelta(hours=i))) plt.savefig('WL{}.png'.format(file_number),dpi=400, bbox_inches = 'tight', pad_inches = 0.1) plt.close() images = [] for ii in range(0,len(wl)): frames = Image.open(wl[ii]) images.append(frames) images[0].save('gifs\\swan.gif', save_all=True, append_images=images[1:], delay=.1, duration=200, loop=0) for f in glob.glob('WL*'): os.remove(f) return
def global_wind_mag(global_path,netcdf_file,title,hours,levels,lon1,lon2,lat1,lat2,start=None): start_date = datetime.strptime(start,'%Y%m%d%H') wl=[] xx = netcdf_file.variables['x'][:] yy = netcdf_file.variables['y'][:] gridvars = netcdf_file.variables var_element = 'element' elems = gridvars[var_element][:,:]-1 m = Basemap(projection='cyl',llcrnrlat=lat1,urcrnrlat=lat2,llcrnrlon=lon1, urcrnrlon=lon2,resolution='h', epsg = 4269) for i in range(0,hours): i=i+1 x,y,u2,v2 = [],[],[],[] u = netcdf_file.variables['windx'][i,:] v = netcdf_file.variables['windy'][i,:] triang = tri.Triangulation(xx,yy, triangles=elems) mag = np.sqrt(np.square(u)+np.square(v)) if mag.mask.any(): point_mask_indices = np.where(mag.mask) tri_mask = np.any(np.in1d(elems, point_mask_indices).reshape(-1, 3), axis=1) triang.set_mask(tri_mask) plt.xlim([lon1, lon2]) plt.ylim([lat1, lat2]) plt.tricontourf(triang,mag, levels=levels,alpha=0.9,vmin=-1, vmax=70, aspect='auto',cmap='jet') file_number = '%05d'%i m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 700, verbose= False) m.drawcoastlines(color='k') cb = plt.colorbar(cmap='jet',fraction=0.026,pad=0.04) cb.set_label('Wind Magnitude (m/s)',fontsize=10) plt.xlim([lon1, lon2]) plt.ylim([lat1, lat2]) wl.append('WL{}.png'.format(file_number)) plt.title(title + '\n') plt.xlabel('\nDate:{}'.format(start_date+ timedelta(hours=i))) plt.savefig('WL{}.png'.format(file_number),dpi=400, bbox_inches = 'tight', pad_inches = 0.1) plt.close() images = [] for ii in range(0,len(wl)): frames = Image.open(wl[ii]) images.append(frames) images[0].save('WindMag.gif', save_all=True, append_images=images[1:], delay=.1, duration=300, loop=0) for f in glob.glob('WL*'): os.remove(f) return
def visualize(df): splits, paths_df = get_continuous_paths(df) m = Basemap(llcrnrlon=-122.56, llcrnrlat=37.4, urcrnrlon=-122, urcrnrlat=37.99, epsg=4269) for path_df in paths_df[:50]: if path_df.iloc[0]['Whether_Occupied'] == 1: trace_trajectory(path_df, m) m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=2500, verbose=True) plt.figure() plt.show()
def plot_rides(ride_clusters): nof_rows = 1 nof_columns = len(ride_clusters) ride_cluster_bounding_boxes = get_ride_cluster_bounding_boxes( ride_clusters) gs = gridspec.GridSpec(nof_rows, nof_columns, width_ratios=[ bounding_box['width'] for bounding_box in ride_cluster_bounding_boxes ]) for i, ride_cluster in enumerate(ride_clusters): ride_cluster_bounding_box = ride_cluster_bounding_boxes[i] ax = plt.subplot(gs[i]) map_ax = Basemap( llcrnrlon=ride_cluster_bounding_box["min_lon"], # Left llcrnrlat=ride_cluster_bounding_box["min_lat"], # Down urcrnrlon=ride_cluster_bounding_box["min_lon"] + ride_cluster_bounding_box["width"], # Right urcrnrlat=ride_cluster_bounding_box["min_lat"] + ride_cluster_bounding_box["height"], # Up epsg=23095, ax=ax) map_ax.arcgisimage(service="World_Imagery", xpixels=min( 2000, 600 * ride_cluster_bounding_box['width'])) for ride in ride_cluster: ride_longitudes = [ coordinate[1] for coordinate in ride["coordinates"] ] ride_latitudes = [ coordinate[0] for coordinate in ride["coordinates"] ] x, y = map_ax(ride_longitudes, ride_latitudes) map_ax.plot(x, y, 'r-', alpha=1) plt.subplots_adjust(left=0.03, bottom=0.05, right=0.97, top=0.95, wspace=0.1, hspace=0.1) plt.show()
def ss_plot(): #Pandas method of importing data frame and getting extents db_connect="dbname='reach_4a' user='******' host='localhost' port='9000'" conn = psycopg2.connect(db_connect) df = pd.read_sql_query('SELECT * from mb_may_2012_1m tt inner join ( SELECT s.easting, s.northing, s.texture, s.sidescan_intensity FROM ss_2012_05 s) ss on tt.easting=ss.easting and tt.northing=ss.northing;', con=conn) minE = df['easting'].min()[0] maxE = df['easting'].max()[0] minN = df['northing'].min()[0] maxN = df['northing'].max()[0] conn.close() print 'Done Importing Data from Database' #Create grid for countourf plot res = 1 grid_x, grid_y = np.meshgrid( np.arange(np.floor(minE), np.ceil(maxE), res), np.arange(np.floor(minN), np.ceil(maxN), res)) grid_lon, grid_lat = trans(grid_x,grid_y,inverse=True) #Re-sampling procedure m_lon, m_lat = trans(df['easting'].values.flatten(), df['northing'].values.flatten(), inverse=True) orig_def = geometry.SwathDefinition(lons=m_lon, lats=m_lat) target_def = geometry.SwathDefinition(lons=grid_lon.flatten(), lats=grid_lat.flatten()) print 'Now Resampling...' result = kd_tree.resample_nearest(orig_def, df['sidescan_intensity'].values.flatten(), target_def, radius_of_influence=1, fill_value=None, nprocs = cpu_count()) print 'Done Resampling!!!' #format side scan intensities grid for plotting gridded_result = np.reshape(result,np.shape(grid_lon)) gridded_result = np.squeeze(gridded_result) gridded_result[np.isinf(gridded_result)] = np.nan gridded_result[gridded_result<=0] = np.nan grid2plot = np.ma.masked_invalid(gridded_result) print 'Now mapping...' #Create Figure fig = plt.figure(frameon=True) ax = plt.subplot(1,1,1) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], llcrnrlon=np.min(grid_lon)-0.0009, llcrnrlat=np.min(grid_lat)-0.0009,urcrnrlon=np.max(grid_lon)+0.0009, urcrnrlat=np.max(grid_lat)+0.0009) gx,gy = map.projtran(grid_lon,grid_lat) map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=1200) im = map.pcolormesh(gx, gy, grid2plot, cmap='gray',vmin=0.1, vmax=30) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.1) cbr = plt.colorbar(im, cax=cax) cbr.set_label('Sidescan Intensity [dBw]', size=8) for t in cbr.ax.get_yticklabels(): t.set_fontsize(8) plt.savefig(r'C:\workspace\Texture_Classification\output\May2012_1m_sidescan_intensity.png')
def per_boat_map(df,path_save,dpi=300): '''plot per month, per vessel map oupput a map: below is the information in map: the mean temperature in historical the mean temperature of observation, the number is the Standard deviation in Parentheses the time period''' start_t,end_t=week_start_end(df['time']) fig=plt.figure(figsize=(8,10)) size=min(fig.get_size_inches()) fig.suptitle('F/V '+df['name'],fontsize=3*size, fontweight='bold') ax=fig.add_axes([0.03,0.2,0.85,0.68]) ax.set_title(start_t.strftime('%Y/%m/%d')+'-'+end_t.strftime('%Y/%m/%d')) ax.axes.title.set_size(2*size) while(not zl.isConnected()):#check the internet is good or not time.sleep(120) #if no internet, sleep 2 minates try again try: service = 'Ocean_Basemap' xpixels = 5000 #Build a map background map=Basemap(projection='mill',llcrnrlat=df['lat']-1,urcrnrlat=df['lat']+1,llcrnrlon=df['lon']-1,urcrnrlon=df['lon']+1,\ resolution='f',lat_0=df['lat'],lon_0=df['lon'],epsg = 4269) map.arcgisimage(service=service, xpixels = xpixels, verbose=False) # draw parallels. parallels = np.arange(0.,90.0,0.5) map.drawparallels(parallels,labels=[0,1,0,0],fontsize=size,linewidth=0.0) # draw meridians meridians = np.arange(180.,360.,0.5) map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=size,linewidth=0.0) #Draw a scatter plot tele_lat,tele_lon=df['lat'],df['lon'] x,y=map(tele_lon,tele_lat) s='HT:'+str(df['climtemp'])+'\nOBS:'+str(round(df['obstemp'],4))+'('+str(round(df['Stdtemp'],2))+')' ax.plot(x, y,'b*',markersize=2*size, alpha=0.5) ax.text(x+0.05,y-0.05,s,fontsize =2*size) xlabel='\nHT:the mean temperature in historical\nOBS:the mean temperature of observation,the number is the Standard deviation in Parentheses' ax.set_xlabel(xlabel,position=(0., 1e6),horizontalalignment='left',fontsize =size) # if the path of the picture save is not there, creat the folder if not os.path.exists(path_save+'/picture'+df['time'].strftime('%Y-%m')+'/'): os.makedirs(path_save+'/picture'+df['time'].strftime('%Y-%m')+'/') #save the map plt.savefig(path_save+'/picture'+df['time'].strftime('%Y-%m')+'/'+df['name']+'_map'+'_'+end_t.strftime('%Y-%m-%d')+'.ps',dpi=dpi) #save picture print(df['name']+' finished draw!') except KeyboardInterrupt: sys.exit() except: print(df['name']+' need redraw!')
def spatialSubPlot(self, platformLineStringHash, ax, e, resolution='l'): ''' Make subplot of tracks for all the platforms within the time range. ''' m = Basemap(llcrnrlon=e[0], llcrnrlat=e[1], urcrnrlon=e[2], urcrnrlat=e[3], projection='cyl', resolution=resolution, ax=ax) ##m.wmsimage('http://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?', layers=['GEBCO_08_Grid']) # Works, but coarse m.arcgisimage(server='http://services.arcgisonline.com/ArcGIS', service='Ocean_Basemap') for pl, LS in platformLineStringHash.iteritems(): x,y = zip(*LS) m.plot(x, y, '-', c=self._getColor(pl), linewidth=3) if self.args.mapLabels: m.drawparallels(np.linspace(e[1],e[3],num=3), labels=[True,False,False,False], linewidth=0) m.drawmeridians(np.linspace(e[0],e[2],num=3), labels=[False,False,False,True], linewidth=0) return ax
def print_contour_map(cs2cs_args, humlon, humlat, glon, glat, datm, sonpath, p, vmin, vmax): #merge, #levels = [0,0.25,0.5,0.75,1.25,1.5,1.75,2,3,5] print("drawing and printing map ...") fig = plt.figure(frameon=False) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], #26949, resolution = 'i', #h #f llcrnrlon=np.min(humlon)-0.001, llcrnrlat=np.min(humlat)-0.001, urcrnrlon=np.max(humlon)+0.001, urcrnrlat=np.max(humlat)+0.001) #if dogrid==1: gx,gy = map.projtran(glon, glat) ax = plt.Axes(fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) try: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=300) except: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='ESRI_Imagery_World_2D', xpixels=1000, ypixels=None, dpi=300) #finally: # print "error: map could not be created..." #if dogrid==1: if 2>1: if datm.size > 25000000: print("matrix size > 25,000,000 - decimating by factor of 5 for display") #map.contourf(gx[::5,::5], gy[::5,::5], datm[::5,::5], levels, cmap='YlOrRd') map.pcolormesh(gx, gy, datm, cmap='YlOrRd', vmin=vmin, vmax=vmax) else: #map.contourf(gx, gy, datm, levels, cmap='YlOrRd') map.pcolormesh(gx[::5,::5], gy[::5,::5], datm[::5,::5], cmap='pink', vmin=vmin, vmax=vmax) custom_save2(sonpath,'class_map_imagery'+str(p)) del fig
def createPNG(self, fileName=None, forGeotiff=False): '''Draw processed data on a map and save it as a .png file ''' if not forGeotiff: fig = plt.figure(figsize=(18, 12)) ax = plt.axes() else: fig = plt.figure() ax = fig.add_axes((0,0,1,1)) if not fileName: fileName = self.args.pngFileName e = self.getExtent() m = Basemap(llcrnrlon=e[0], llcrnrlat=e[1], urcrnrlon=e[2], urcrnrlat=e[3], projection='cyl', resolution='l', ax=ax) if not forGeotiff: m.arcgisimage(server='http://services.arcgisonline.com/ArcGIS', service='Ocean_Basemap') for depth, drift in list(self.adcpDrift.items()): m.plot(drift['lon'], drift['lat'], '-', c='black', linewidth=1) plt.text(drift['lon'][-1], drift['lat'][-1], '%i m' % depth, size='small') for platform, drift in list(self.trackDrift.items()): # Ad hoc coloring of platforms... if platform.startswith('stella'): color = 'yellow' elif platform.startswith('daphne'): color = 'orange' elif platform.startswith('makai'): color = 'magenta' else: color = 'red' m.plot(drift['lon'], drift['lat'], '-', c=color, linewidth=2) plt.text(drift['lon'][-1], drift['lat'][-1], platform, size='small') # Plot each data point with it's own color based on the activity statistics from STOQS coloredDotSize = 30 clt = readCLT(os.path.join(settings.ROOT_DIR('static'), 'colormaps', 'jetplus.txt')) cm_jetplus = matplotlib.colors.ListedColormap(np.array(clt)) for key, drift in list(self.stoqsDrift.items()): min, max = key.split(',')[2:4] ax.scatter(drift['lon'], drift['lat'], c=drift['datavalue'], s=coloredDotSize, cmap=cm_jetplus, lw=0, vmin=min, vmax=max) label = '%s from %s' % tuple(key.split(',')[:2]) plt.text(drift['lon'][-1], drift['lat'][-1], label, size='small') nowLocal = str(pytz.utc.localize(datetime.now()).astimezone(pytz.timezone('America/Los_Angeles'))).split('.')[0] plt.text(0.99, 0.01, 'Created: ' + nowLocal + ' Local', horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) if not forGeotiff: m.drawparallels(np.linspace(e[1],e[3],num=3), labels=[True,False,False,False], linewidth=0) m.drawmeridians(np.linspace(e[0],e[2],num=3), labels=[False,False,False,True], linewidth=0) try: plt.title(self.title) except AttributeError: pass fig.savefig(fileName) print('Wrote file', self.args.pngFileName) else: plt.axis('off') try: plt.text(0.5, 0.95, self.title, horizontalalignment='center', verticalalignment='top', transform=ax.transAxes) except AttributeError: pass fig.savefig(fileName, transparent=True, dpi=300, bbox_inches='tight', pad_inches=0) plt.clf() plt.close()
class IHM(): def onclick_map(self, event): if event.xdata != None: lon, lat = self.map(event.xdata, event.ydata, inverse = True) x, y = self.map(lon, lat) self.map.scatter(x,y,150,marker='*',color='g') way = pattern.ratissage_sc([x, y], 50,5,5,0) patx=[] paty=[] way = way.transpose() self.map.plot(way[0], way[1]) self.canvas.draw() else: showerror("Error", "Veuillez selectionner un point dans la carte") #recevoir les coordonnées def update(self): try: temp = self.queue.get_nowait() if(temp.startswith("GPS")): self.zone_reception.delete("1.0", END) self.zone_reception.insert(INSERT, temp[3:]) coord = parseGPS(temp[3:]) if coord[0] != 0: lat = coord[3]*(coord[0]+coord[1]/60.0+coord[2]/3600.0) lon = coord[7]*(coord[4]+coord[5]/60.0+coord[6]/3600.0) x, y = self.map(lon, lat) # (-4.47, 48.418) if hasattr(self, 'drone_dot'): self.drone_dot.remove() self.drone_dot = self.map.scatter(x,y,150,marker='o',color='r') self.canvas.draw() except queue.Empty: # It's ok if there's no data to read. # We'll just check again later. pass self.fenetre.after(2000, self.update) #envoyer des coordonnées def envoyer_pos(self): deg_lat=self.msg_deg_lat.get() min_lat=self.msg_min_lat.get() sec_lat=self.msg_sec_lat.get() deg_long=self.msg_deg_long.get() min_long=self.msg_min_long.get() sec_long=self.msg_sec_long.get() if (identifier(deg_lat) & identifier(min_lat) & identifier(sec_lat)) and\ (identifier(deg_long) & identifier(min_long) & identifier(sec_long)): wp = str(deg_lat)+','+str(min_lat) + ',' + str(sec_lat)+','+str(self.value_lat.get())+ ','+ str(deg_long)+','+str(min_long) + ',' + str(sec_long)+','+str(self.value_long.get()) self.base.sender.send_text("ADDWAY"+wp) '''zone_deg_lat.delete(0,END) zone_sec_lat.delete(0,END) zone_min_lat.delete(0,END) zone_deg_long.delete(0,END) zone_sec_long.delete(0,END) zone_min_long.delete(0,END) zone_reception.delete("1.0", END)''' def __init__(self, base, queue): self.base = base self.queue = queue #Gestion de la fenetre self.fenetre = Tk() #création de la fenêtre, nom au choix self.fenetre.title("Centre de contrôle SAUVMER") #nom de la fenetre #self.fenetre['bg']='grey' FWidth=self.fenetre.winfo_screenwidth() FHeight=self.fenetre.winfo_screenheight() label = Label(self.fenetre, text="Bienvenue au centre de contrôle SAUVMER", ) label['fg']='blue' #création du texte de couleur bleue label['bg']='white' #couleur fond de texte label.pack(fill=X)#insère le texte dans la fenetre #gestion des onglets nb = ttk.Notebook(self.fenetre) #premier onglet onglet1 = ttk.Frame(nb) nb.add(onglet1, text='Positionnement du drone') #deuxieme onglet onglet2 = ttk.Frame(nb) nb.add(onglet2, text='Données de navigation') #troisieme onglet onglet3 = ttk.Frame(nb) nb.add(onglet3, text='Flux vidéo') #quatrieme onglet onglet4 = ttk.Frame(nb) nb.add(onglet4, text='Information sur la batterie') nb.pack(fill=X) #case envoyer & recevoir data coordonnees l_pos = LabelFrame(onglet1, text="Positionnement du drone", padx=20, pady=20) l_pos.pack(fill="both", expand="yes") #case afficher carte l_map = LabelFrame(onglet1, text="Carte", padx=20, pady=20) l_map.pack(fill="both", expand="yes") # Bouton actualisation des coordonnees #bouton_coord= Button (l_pos, text="Obtenir les coordonnées", command=self.afficher_pos) #bouton_coord.grid(row=0,column=0) #zone_reception = Canvas(l_pos,width=200, height= 25,background='white') #Définit les dimensions du canevas self.zone_reception = Text(l_pos,width=25, height= 1,background='white') self.zone_reception.grid(row= 0, column =1) #Affiche le canevas # label latitude label_lat = Label(l_pos, text="Latitude") label_lat.grid(row= 1, column =0) # label longitude label_long = Label(l_pos, text="Longitude") label_long.grid(row= 2, column =0) #bouton radio Nord et Sud self.value_lat = StringVar() bouton1 = Radiobutton(l_pos, text="N", variable=self.value_lat, value='N') bouton2 = Radiobutton(l_pos, text="S", variable=self.value_lat, value='S') bouton1.grid(row= 1, column =7) bouton2.grid(row= 1, column =8) #bouton radio Est et Ouest self.value_long = StringVar() bouton3 = Radiobutton(l_pos, text="O", variable=self.value_long, value='O') bouton4 = Radiobutton(l_pos, text="E", variable=self.value_long, value='E') bouton3.grid(row= 2, column =7) bouton4.grid(row= 2, column =8) #zones degré self.msg_deg_lat = StringVar() zone_deg_lat = Entry(l_pos,background='white',textvariable=self.msg_deg_lat) zone_deg_lat.grid(row = 1, column = 1) self.msg_deg_long = StringVar() zone_deg_long = Entry(l_pos,background='white',textvariable=self.msg_deg_long) zone_deg_long.grid(row = 2, column = 1) #label degré label_deg_lat=Label(l_pos, text='°') label_deg_lat.grid(row=1,column = 2) label_deg_long=Label(l_pos, text='°') label_deg_long.grid(row=2,column = 2) #zones minutes self.msg_min_lat = StringVar() zone_min_lat = Entry(l_pos,background='white',textvariable=self.msg_min_lat) zone_min_lat.grid(row = 1, column = 3) self.msg_min_long = StringVar() zone_min_long = Entry(l_pos,background='white',textvariable=self.msg_min_long) zone_min_long.grid(row = 2, column = 3) #label minutes label_min_lat=Label(l_pos, text="'") label_min_lat.grid(row=1,column = 4) label_min_long=Label(l_pos, text="'") label_min_long.grid(row=2,column = 4) #zones secondes self.msg_sec_lat = StringVar() zone_sec_lat = Entry(l_pos,background='white',textvariable=self.msg_sec_lat) zone_sec_lat.grid(row = 1, column = 5) self.msg_sec_long = StringVar() zone_sec_long = Entry(l_pos,background='white',textvariable=self.msg_sec_long) zone_sec_long.grid(row = 2, column = 5) #label secondes label_sec_lat=Label(l_pos, text='\"') label_sec_lat.grid(row=1,column = 6) label_sec_long=Label(l_pos, text='\"') label_sec_long.grid(row=2,column = 6) #bouton envoie coordonnées bouton_envoyer = Button(l_pos, text= "Envoyer les coordonnées", command = self.envoyer_pos) bouton_envoyer.grid(row=1, column=9) #Affichage de la carte fig = matplotlib.pyplot.figure(figsize=(12, 6)) self.map = Basemap(llcrnrlon=-4.476,llcrnrlat=48.417,urcrnrlon=-4.469,urcrnrlat=48.42, epsg=2154) self.map.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 1500, verbose= True) self.map.drawcountries() self.map.drawcoastlines(linewidth=.5) self.canvas = FigureCanvasTkAgg(fig, master=l_map) self.canvas.get_tk_widget().pack(side=tk.TOP, fill=X, expand=1) self.canvas.mpl_connect('button_press_event', self.onclick_map) #gestion de la batterie #Frame de la batterie l_bat = Label(onglet4) l_bat.pack(fill="both", expand = "yes") #Voltage label_volt = Label(l_bat, text="Voltage battery") label_volt.grid(row= 0, column =0) zone_reception_volt = Text(l_bat,width=25, height= 1,background='white') zone_reception_volt.grid(row= 0, column =1) #Etat label_cur = Label(l_bat, text="Current battery") label_cur.grid(row= 1, column =0) zone_reception_cur = Text(l_bat,width=25, height= 1,background='white') zone_reception_cur.grid(row= 1, column =1) #Etat label_rem = Label(l_bat, text="battery remaining") label_rem.grid(row= 2, column =0) zone_reception_rem = Text(l_bat,width=25, height= 1,background='white') zone_reception_rem.grid(row= 2, column =1) #gestion de la navigation #Frame de la vitesse (tangage, roulis, lacet) l_vit = LabelFrame(onglet2, text="Vitesse") l_vit.pack(fill="both", expand="yes") # label roulis label_roulis = Label(l_vit, text="Roulis") label_roulis.grid(row= 0, column =0) zone_reception_roulis = Text(l_vit,width=25, height= 1,background='white') zone_reception_roulis.grid(row= 0, column =1) #label tangage label_tangage = Label(l_vit, text="Tangage") label_tangage.grid(row= 1, column =0) zone_reception_tangage = Text(l_vit,width=25, height= 1,background='white') zone_reception_tangage.grid(row= 1, column =1) #label lacet label_lacet= Label(l_vit, text="Lacet") label_lacet.grid(row= 2, column =0) zone_reception_lacet = Text(l_vit,width=25, height= 1,background='white') zone_reception_lacet.grid(row= 2, column =1) #Frame de l'accélération l_acc = LabelFrame(onglet2, text = "Accélération") l_acc.pack(fill="both", expand = "yes") # label roulis label_x = Label(l_acc, text="X") label_x.grid(row= 0, column =0) zone_reception_x = Text(l_acc,width=25, height= 1,background='white') zone_reception_x.grid(row= 0, column =1) #label tangage label_y = Label(l_acc, text="Y") label_y.grid(row= 1, column =0) zone_reception_y = Text(l_acc,width=25, height= 1,background='white') zone_reception_y.grid(row= 1, column =1) #label lacet label_z= Label(l_acc, text="Z") label_z.grid(row= 2, column =0) zone_reception_z= Text(l_acc,width=25, height= 1,background='white') zone_reception_z.grid(row= 2, column =1) #Frame rotation l_rot= LabelFrame(onglet2, text = "Rotation") l_rot.pack(fill="both", expand = "yes") zone_reception_rot= Text(l_rot,width=25, height= 1,background='white') zone_reception_rot.grid(row= 2, column =1) #Bouton fermeture de la fenetre bouton=Button(self.fenetre, text="Fermer", command=self.quit) #Bouton qui détruit la fenêtre bouton.pack(side =BOTTOM, padx =1, pady = 1) #insère le bouton dans la boucle self.update() def quit(self): self.fenetre.quit() self.fenetre.destroy()
class IHM(): def __init__(self, rece_queue, send_queue, video_queue): self.rece_queue = rece_queue self.send_queue = send_queue self.video_queue = video_queue #Gestion de la fenetre self.fenetre = Tk() self.fenetre.title("Centre de contrôle SAUVMER") #Titre label = Label(self.fenetre, text="Bienvenue au centre de contrôle SAUVMER", ) label['fg']='blue' label['bg']='white' label.pack(fill=X) #region GESTION_ONGLET nb = ttk.Notebook(self.fenetre) #premier onglet onglet1 = ttk.Frame(nb) nb.add(onglet1, text='Positionnement du drone') #deuxieme onglet onglet2 = ttk.Frame(nb) nb.add(onglet2, text='Données de navigation') #troisieme onglet onglet3 = ttk.Frame(nb) nb.add(onglet3, text='Flux vidéo') #quatrieme onglet '''onglet4 = ttk.Frame(nb) nb.add(onglet4, text='Information sur la batterie')''' nb.pack(fill=X) #endregion #case envoyer & recevoir data coordonnees l_pos = LabelFrame(onglet1, text="Positionnement du drone", padx=20, pady=20) l_pos.pack(fill=X, expand="yes") l_mis = LabelFrame(onglet1, text="Mission du drone", padx=20, pady=20) l_mis.pack(fill=X, expand="yes") #case afficher carte l_map = LabelFrame(onglet1, text="Carte", padx=20, pady=20) l_map.pack(fill=X, expand="yes") #zone_reception = Canvas(l_pos,width=200, height= 25,background='white') #Définit les dimensions du canevas self.zone_reception = Text(l_pos,width=25, height= 1,background='white') self.zone_reception.grid(row= 0, column =0) #Affiche le canevas print("Initiailsé") #region SEND_COORDONNEE ''' # label latitude/longitude label_lat = Label(l_pos, text="Latitude") label_lat.grid(row= 1, column =0) label_long = Label(l_pos, text="Longitude") label_long.grid(row= 2, column =0) #zones degré self.msg_deg_lat = StringVar() zone_deg_lat = Entry(l_pos,background='white',textvariable=self.msg_deg_lat) zone_deg_lat.grid(row = 1, column = 1) self.msg_deg_long = StringVar() zone_deg_long = Entry(l_pos,background='white',textvariable=self.msg_deg_long) zone_deg_long.grid(row = 2, column = 1) #label degré label_deg_lat=Label(l_pos, text='°') label_deg_lat.grid(row=1,column = 2) label_deg_long=Label(l_pos, text='°') label_deg_long.grid(row=2,column = 2) #bouton envoie coordonnées bouton_envoyer = Button(l_pos, text= "Envoyer les coordonnées", command = self.envoyer_pos) bouton_envoyer.grid(row=1, column=9) ''' self.var = IntVar() R1 = Radiobutton(l_mis, text="waypoint", variable=self.var, value=1) R1.grid(row=0, column=3, sticky=W) R2 = Radiobutton(l_mis, text="spirale carrée", variable=self.var, value=2) R2.grid(row=1, column=3, sticky=W) R3 = Radiobutton(l_mis, text="lacet", variable=self.var, value=3) R3.grid(row=2, column=3, sticky=W) launch_button=Button(l_mis, text="Launch mission", command=self.launch, height = 2, width = 20, padx=20) launch_button.grid(row=0, column=4, sticky=W) clear_button=Button(l_mis, text="Clear mission", command=self.clear_waypoint, height = 2, width = 20, padx=20) clear_button.grid(row=1, column=4) takeoff_button=Button(l_mis, text="Take off!", command=self.takeoff, height = 2, width = 20, padx=20) takeoff_button.grid(row=0, column=5) land_button=Button(l_mis, text="Land", command=self.land, height = 2, width = 20, padx=20) land_button.grid(row=1, column=5) #endregion print("Initiailsé") #region AFFICHER_CARTE fig = plt.figure(figsize=(12, 6)) self.map = Basemap(llcrnrlon=-4.476,llcrnrlat=48.417, urcrnrlon=-4.469, urcrnrlat=48.42, epsg=2154) try: self.map.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=2000, verbose=True) print("Carte chargé online") except: im = plt.imread('ensta.png') self.map.imshow(im) print("Carte chargé offline") self.mapplot = None #self.map.drawcountries() #self.map.drawcoastlines(linewidth=.5) self.canvas = FigureCanvasTkAgg(fig, master=l_map) self.canvas.get_tk_widget().pack(side=tk.TOP, fill=X, expand=1) self.canvas.mpl_connect('button_press_event', self.onclick_map) #bouton_waypoint = Button(self.canvas, text= "Envoyer les waypoint", command = self.envoyer_way) #endregion #region BATTERIE '''#Frame de la batterie l_bat = Label(onglet4) l_bat.pack(fill="both", expand = "yes") #Voltage label_volt = Label(l_bat, text="Voltage battery") label_volt.grid(row= 0, column =0) zone_reception_volt = Text(l_bat,width=25, height= 1,background='white') zone_reception_volt.grid(row= 0, column =1) #Etat label_cur = Label(l_bat, text="Current battery") label_cur.grid(row= 1, column =0) zone_reception_cur = Text(l_bat,width=25, height= 1,background='white') zone_reception_cur.grid(row= 1, column =1) #Etat label_rem = Label(l_bat, text="battery remaining") label_rem.grid(row= 2, column =0) zone_reception_rem = Text(l_bat,width=25, height= 1,background='white') zone_reception_rem.grid(row= 2, column =1) ''' #endregion #region NAVIGATION #Frame de la vitesse (tangage, roulis, lacet) l_vit = LabelFrame(onglet2, text="Vitesse") l_vit.pack(fill="both", expand="yes") # label roulis label_roulis = Label(l_vit, text="Roulis") label_roulis.grid(row= 0, column =0) zone_reception_roulis = Text(l_vit,width=25, height= 1,background='white') zone_reception_roulis.grid(row= 0, column =1) #label tangage label_tangage = Label(l_vit, text="Tangage") label_tangage.grid(row= 1, column =0) zone_reception_tangage = Text(l_vit,width=25, height= 1,background='white') zone_reception_tangage.grid(row= 1, column =1) #label lacet label_lacet= Label(l_vit, text="Lacet") label_lacet.grid(row= 2, column =0) zone_reception_lacet = Text(l_vit,width=25, height= 1,background='white') zone_reception_lacet.grid(row= 2, column =1) #Frame de l'accélération l_acc = LabelFrame(onglet2, text = "Accélération") l_acc.pack(fill="both", expand = "yes") # label roulis label_x = Label(l_acc, text="X") label_x.grid(row= 0, column =0) zone_reception_x = Text(l_acc,width=25, height= 1,background='white') zone_reception_x.grid(row= 0, column =1) #label tangage label_y = Label(l_acc, text="Y") label_y.grid(row= 1, column =0) zone_reception_y = Text(l_acc,width=25, height= 1,background='white') zone_reception_y.grid(row= 1, column =1) #label lacet label_z= Label(l_acc, text="Z") label_z.grid(row= 2, column =0) zone_reception_z= Text(l_acc,width=25, height= 1,background='white') zone_reception_z.grid(row= 2, column =1) #Frame rotation l_rot= LabelFrame(onglet2, text = "Rotation") l_rot.pack(fill="both", expand = "yes") zone_reception_rot= Text(l_rot,width=25, height= 1,background='white') zone_reception_rot.grid(row= 2, column =1) #endregion #region VIDEO #Graphics window imageFrame = tk.Frame(onglet3) #, width=600, height=500 imageFrame.grid(row=0, column=0, padx=10, pady=2) self.image = tk.Label(imageFrame) self.image.grid(row=0, column=0) #endregion print("Initiailsé") #region FERMETURE FENETRE bouton=Button(self.fenetre, text="Fermer", command=self.quit) #Bouton qui détruit la fenêtre bouton.pack(side =BOTTOM, padx =1, pady = 1) #insère le bouton dans la boucle #endregion self.waypoint = [] print("Initiailsé") self.update() def onclick_map(self, event): if event.xdata != None: lon, lat = self.map(event.xdata, event.ydata, inverse = True) x, y = self.map(lon, lat) if self.mapplot is None: self.mapplot, = self.map.plot(x,y,linestyle='-', marker='*', color='k') if self.var.get() == 1: self.mapplot.set_xdata(numpy.append(self.mapplot.get_xdata(), x)) self.mapplot.set_ydata(numpy.append(self.mapplot.get_ydata(), y)) self.waypoint.append((lon, lat)) elif self.var.get() == 2: way = pattern.ratissage_sc([x, y], 50,5,5,0) way = way.transpose() #self.map.plot(way[0], way[1]) for k in range(0, len(way[0])): self.mapplot.set_xdata(numpy.append(self.mapplot.get_xdata(), way[0,k])) self.mapplot.set_ydata(numpy.append(self.mapplot.get_ydata(), way[1,k])) self.waypoint.append(self.map(way[0,k],way[1,k], inverse=True)) elif self.var.get() == 3: way = pattern.balai([x, y], [x+30, y+30],10,0) way = way.transpose() #self.map.plot(way[0], way[1]) for k in range(0, len(way[0])): self.mapplot.set_xdata(numpy.append(self.mapplot.get_xdata(), way[0,k])) self.mapplot.set_ydata(numpy.append(self.mapplot.get_ydata(), way[1,k])) self.waypoint.append(self.map(way[0,k],way[1,k], inverse=True)) self.canvas.draw() print(self.waypoint) else: showerror("Error", "Veuillez selectionner un point dans la carte") def update(self): try: temp = self.rece_queue.get_nowait() if(temp.startswith("GPS")): self.zone_reception.delete("1.0", END) self.zone_reception.insert(INSERT, temp[3:]) coord = parseGPS(temp[3:]) if coord[0] != 0: lat = coord[0] lon = coord[1] x, y = self.map(lon, lat) # (-4.47, 48.418) if hasattr(self, 'drone_dot'): self.drone_dot.remove() self.drone_dot = self.map.scatter(x,y,70,marker='o',color='r') self.canvas.draw() elif(temp.startswith("DETECT")): print("Victim detected") coord = parseGPS(temp[6:]) if coord[0] != 0: lat = coord[0] lon = coord[1] x, y = self.map(lon, lat) # (-4.47, 48.418) self.map.scatter(x,y,150,marker='o',color='b') self.canvas.draw() except queue.Empty: pass try: data = self.video_queue.get_nowait() decimg=cv2.imdecode(data,1) cv2image = cv2.cvtColor(decimg, cv2.COLOR_BGR2RGBA) img = Image.fromarray(cv2image) imgtk = ImageTk.PhotoImage(image=img) self.image.imgtk = imgtk self.image.configure(image=imgtk) self.fenetre.update() except queue.Empty: pass self.fenetre.after(50, self.update) #envoyer des coordonnées def envoyer_pos(self): deg_lat=self.msg_deg_lat.get() deg_long=self.msg_deg_long.get() wp = str(deg_lat)+';'+str(deg_long) self.send_queue.put("ADDWAY"+wp) self.send_queue.put("GO") def quit(self): self.send_queue.put("END") self.fenetre.quit() self.fenetre.destroy() def clear_waypoint(self): self.mapplot.remove() self.waypoint = [] self.canvas.draw() self.mapplot = None def launch(self): if len(self.waypoint) != 0: print(self.waypoint) for pos in self.waypoint: self.send_queue.put("ADDWAY"+str(pos[1])+';'+str(pos[0])) self.send_queue.put("GO") pass def land(self): self.send_queue.put("LAND") def takeoff(self): self.send_queue.put("SIMPLETAKEOFF")
from obspy.imaging.beachball import Beach focmec, lat, lon = [], [], [] datos = open('datos.txt') counter = 0 for line in datos: if counter > 0: focmec.append([float(line.split('\t')[7]),float(line.split('\t')[9]),float(line.split('\t')[9])]) lat.append(float(line.split('\t')[1])) lon.append(float(line.split('\t')[2])) counter+= 1 map = Basemap(llcrnrlon=-83.5,llcrnrlat=-4.5,urcrnrlon=-76,urcrnrlat=6, projection='cyl', resolution=None) #map.drawcoastlines() x, y = map(lon, lat) #map.scatter(x, y, 200, color="r", marker="v", edgecolor="k", zorder=3) ax = plt.gca() for i in range(len(focmec)): b = Beach(focmec[i], xy=(x[i], y[i]), width=0.35, linewidth=1) b.set_zorder(1) ax.add_collection(b) map.arcgisimage(service='NatGeo_World_Map', xpixels = 1500, verbose= True) plt.show()
elif 0.6 <= dep[i] < 0.8: d4 = map.scatter(x[i], y[i], 50, color="r", marker="o", edgecolor="k", zorder=50) elif 0.8 <= dep[i] < 1.0: d5 = map.scatter(x[i], y[i], 50, color="aqua", marker="o", edgecolor="k", zorder=50) elif 1.0 <= dep[i] < 1.2: d6 = map.scatter(x[i], y[i], 50, color="g", marker="o", edgecolor="k", zorder=50) labels = ['z<0.2','z<0.4','z<0.6','z<0.8','z<1.0','z<1.2'] map.arcgisimage(service='World_Topo_Map', xpixels = 1500, verbose= True) leg = ax1.legend([d1, d2, d3, d4, d5, d6], labels, frameon= True, title='Profundidad (km)', scatterpoints = 1) ax1.tick_params(labelsize=20) ax2 = fig.add_subplot(212) for i in range(len(dep)): if dep[i] < 0.2: d1 = ax2.scatter(x[i], dep[i], 50, color="ivory", marker="o", edgecolor="k", zorder=50) elif 0.2 <= dep[i] < 0.4: d2 = ax2.scatter(x[i], dep[i], 50, color="y", marker="o", edgecolor="k", zorder=50) elif 0.4 <= dep[i] < 0.6:
def run_main(): [superdict,superlista] = IdDictionary.run_main() stations = np.genfromtxt("/home/ns3/Documents/BicingProject/BigData/Process_Data/Prematrix_data.txt", delimiter=';', dtype=[('lat', np.float32), ('lon', np.float32),('id', np.int16)], usecols=(3, 4,0)) ocupation = np.genfromtxt("/home/ns3/Documents/BicingProject/BigData/Process_Data/Data_history/1458255793_4001.txt", delimiter=' ', dtype=[('ocupacionabsoluta', np.float32), ('status', np.float32)], usecols=(1, 2)) m = Basemap(llcrnrlon=2.031819,llcrnrlat=41.334322,urcrnrlon=2.240496,urcrnrlat=41.496240, resolution = 'l',epsg=5520) #http://server.arcgisonline.com/arcgis/rest/services m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 1500, verbose= True) lats=stations['lat'] lons=stations['lon'] ids=stations['id'] OcupacionAbsoluta=ocupation['ocupacionabsoluta'] Status=ocupation['status'] OcupacionPorcentual=[] # convert lat and lon to map projection mx,my=m(lons,lats) pos={} counter=0 for element in ids: pos[element]=(mx[counter],my[counter]) if int(superdict[str(element)]['total'])==0: OcupacionPorcentual.append(100) #print superdict[str(element)]['id'] else: OcupacionPorcentual.append(int(round((float(OcupacionAbsoluta[counter])/float(superdict[str(element)]['total']))*100))) counter=counter+1 #print "/n" #print OcupacionPorcentual Tuple = () ListOfEdges = [] ContadorFilas = 0 ContadorColumnas = 0 G = nx.Graph() # with open("/home/ns3/Documents/BicingProject/BigData/Process_Data/RDD/TrafficMatrix_data_python.txt","r") as fid: with open("/home/ns3/Documents/BicingProject/BigData/Process_Data/AdjacentMatrix_distance_300.dat", "r") as fid: for line in fid: ContadorColumnas = 0 f = line.split(' ') Tamano = len(f) for element in f: Tuple = str(ContadorFilas) + str(ContadorColumnas) if element == "1": index1 = ids[ContadorFilas] index2 = ids[ContadorColumnas] #ListOfEdges.append((index1, index2)) #ListOfEdges.append((index1, index2)) index1 = ids[ContadorFilas] index2 = ids[ContadorColumnas] G.add_node(index1) G.add_node(index2) ContadorColumnas = ContadorColumnas + 1 ContadorFilas = ContadorFilas + 1 #for EdgePair in ListOfEdges: #G.add_edge(EdgePair[0], EdgePair[1]) color = [] #min_altura = float(min(altura)) #max_altura = float(max(altura)) #step = (max_altura - min_altura) / 5 #rango0 = min_altura #rango1 = min_altura + step * 1 #rango2 = min_altura + step * 2 #rango3 = min_altura + step * 3 #rango4 = min_altura + step * 4 #rango5 = max_altura # print min_altura # print max_altura # print step # print "/n" # print rango0 # print rango1 # print rango2 # print rango3 # print rango4 # print rango5 # Mientras el valor sea mas pequeno, sera mas morado. # Mientras el valor de altura sea mas alto, sera mas rojo counter=0 for element in OcupacionPorcentual: color.append(OcupacionPorcentual[counter]) # if element >= rango0 and element < rango1: # color.append(10) # if element >= rango1 and element < rango2: # color.append(30) # if element >= rango2 and element < rango3: # color.append(50) # if element >= rango3 and element < rango4: # color.append(70) # if element >= rango4 and element <= rango5: # color.append(90) counter=counter+1 #print color nx.draw_networkx(G, pos, node_size=150, node_color=color) plt.title('Bicing network classified according to occupation') plt.colorbar() plt.show()
def plotEPSG(self, epsg=3857, llspace=1., fsize=(8,8), title = None, saveFig = False, figName = 'epsg'): """ Plot a map using ARCGIS image in the background showing the extend of the area to use for Badlands model. Parameters ---------- variable : epsg EPSG code the default is 3857 which is a global ProjectedCRS grid using spherical development of ellipsoidal coordinates. The data is relative to WGS 84/World Mercator. variable: llspace Spacing between the parallels and meridians for Basemap plotting (in degrees). variable: fsize Size of the figure to plot. variable: title Title of the plot. variable: saveFig Saving the figure (boolean). variable: figName Name of the saved file. """ plt.figure(figsize = fsize) self.llspace = llspace # Create the map with basemap epsgMap = Basemap(epsg = epsg, resolution = 'h', \ llcrnrlon = self.llcrnrlon, \ llcrnrlat = self.llcrnrlat, \ urcrnrlon = self.urcrnrlon, \ urcrnrlat = self.urcrnrlat) # Load ARCGIS image epsgMap.arcgisimage() # Draw meridians and parallels epsgMap.drawmeridians( np.arange(int(self.llcrnrlon)-self.llspace, \ int(self.urcrnrlon)+1+self.llspace,self.llspace), \ labels=[0,0,0,1], color='w', fontsize=8 ) epsgMap.drawparallels( np.arange(int(self.llcrnrlat)-self.llspace, \ int(self.urcrnrlat)+1+self.llspace,self.llspace), \ labels=[1,0,0,0], color='w', fontsize=8 ) epsgMap.drawcoastlines( linewidth=2, color='0.5' ) #epsgMap.drawcountries() titlepos = plt.title(str(title), fontsize = 10, weight='bold') titlepos.set_y( 1.01 ) if saveFig: plotfile = str(figName)+'.pdf' plt.savefig(plotfile,dpi=150,orientation='portrait') print 'PDF figure saved: ',plotfile plt.show() return
from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import numpy as np # test arcgisimage method for retrieving images from web map servers. plt.figure(figsize=(8,8)) epsg=4326 m=Basemap(projection='cyl',llcrnrlon=-90,llcrnrlat=30,urcrnrlon=-60,urcrnrlat=60,resolution='i') # default 'blue marble' image. m.arcgisimage(verbose=True) m.drawmeridians(np.arange(-180,180,10),labels=[0,0,0,1],color='y') m.drawparallels(np.arange(-90,90,10),labels=[1,0,0,0],color='y') m.drawcoastlines(linewidth=0.5,color='0.5') plt.title('test WMS map background EPSG=%s'%epsg) plt.figure(figsize=(8,8)) epsg = 3413; width = 18000.e3; height = 18000.e3 m=Basemap(epsg=epsg,resolution='l',width=width,height=height) # default 'blue marble' image. m.arcgisimage() m.drawmeridians(np.arange(-180,180,30),labels=[0,0,0,1],color='y') m.drawparallels(np.arange(0,80,10),labels=[1,0,0,0],color='y') m.drawcoastlines(linewidth=0.5,color='0.5') plt.title('test WMS map background EPSG=%s' % epsg) plt.figure(figsize=(10,6.6666)) epsg = 2263; width=600.e3; height = 400.e3 m=Basemap(epsg=epsg,resolution='h',width=width,height=height) # specify a different server. m.arcgisimage(server='http://maps.ngdc.noaa.gov',service='etopo1',verbose=True)
class QWGUI(Frame): '''Base class controlling QuakeWatch GUI aspects''' def __init__(self, parent, width=1100, height=400, **options): Frame.__init__(self,parent, **options) Grid.rowconfigure(self, 0, weight=1) Grid.columnconfigure(self, 0, weight=1) self.grid(sticky=E+W+N+S) self.userentries = {} top=self.winfo_toplevel() #Configure the width of rows and columns when tthe gui gets stretched. The second number provides the relative #weight of the column or row given by the first number norows = 14 nocols = 13 for i in range(norows): Grid.rowconfigure(self,i,weight=1) Grid.rowconfigure(parent,i,weight=1) for i in range(nocols): Grid.columnconfigure(self,i,weight=1) Grid.columnconfigure(parent,i,weight=1) #Various default settings self.momenttensors = True #display moment tensors where possible self.quakesplotted = None self.MTs = None t2 = str(datetime.datetime.today()).split(' ') #Current time t2 = t2[0]+'T'+t2[1][:-3] self.now = UTCDateTime(t2) self.starttime = 604800 #1 week quakes #-------------------------- #Default map boundaries #-------------------------- self.minlon = -179.9 self.maxlon = 179.9 self.minlat = -89 self.maxlat = 89 #-------------------------- self.minmag = 4.5 self.maxmag = 10.0 self.mtradius = 10 self.datacenter = 'USGS' #default datacenter to retrieve quake data from #create subplot where the map will go self.f = plt.figure(dpi=250,facecolor='white') #set the size of the figure for use with global map. Will need to choose this on the fly when #resizing the figure self.f.set_size_inches(5.0,2.2) self.a = self.f.add_subplot(111) #Call function to download some earhquakes and ... #Initial meap setup and quakes print 'Drawing map....' self.map = Basemap(ax=self.a,lat_0=38,lon_0=-122.0,resolution ='l',llcrnrlon=-179.9,llcrnrlat=-89.9,urcrnrlon=179.9,urcrnrlat=89.9) self.map.fillcontinents() self.map.drawcoastlines(linewidth=0.2) #plot the plate boundaries print 'Drawing plate boundaries.....' self.faults = quaketools.read_faults('usgs_plates.txt.gmtdat') for i in self.faults: faults_lons = self.faults[i][0] faults_lats = self.faults[i][1] x,y = self.map(faults_lons, faults_lats) self.map.plot(x,y,'b-',linewidth=0.5) self.map.drawparallels(np.arange(-90,90,30),labels=[1,1,0,0],linewidth=0.5,fontsize=4) self.map.drawmeridians(np.arange(-180,180,30),labels=[0,0,0,1],linewidth=0.5,fontsize=4) self.canvas = FigureCanvasTkAgg(self.f, self) #plot quakes for the last week on the globe map with default parameters self.worldquakes() Browse.addobjs(self.canvas,self.map) self.f.canvas.mpl_connect('button_press_event',Browse.onpick) self.f.canvas.mpl_connect('motion_notify_event', Browse.motion) self.f.canvas.mpl_connect('button_release_event',Browse.releasepick) self.canvas.get_tk_widget().grid(row=1,sticky=W+S+N+E,columnspan=14,rowspan=10) self.canvas.show() self.SetElements() parent.title("QuakeWatch Mapper") self.Createmenubar(parent) def SetElements(self): '''Sets up the the GUI elements''' Label(self,text='Earthquakes',bg='azure',height=2,pady=2,font='Helvetica 22 bold').grid(row=0,column=0,columnspan=14,sticky=W+E+S+N) #Zoom box bounds Label(self,text='Bounds of zoom box [lon/lat]',bg='azure',height=4,pady=2,padx=1,font='Helvetica 14 bold').grid(row=11,column=1,columnspan=4,sticky=W+E+S+N) Ncord = Entry(self) Ncord.grid(row=12,column=2,columnspan=1,sticky=E) Label(self,text='Northeastern corner').grid(row=12,column=1,columnspan=1,sticky=W) self.userentries['Northeast_box'] = Ncord Scord = Entry(self) Scord.grid(row=13,column=2,columnspan=1,sticky=E) Label(self,text='Southwestern corner').grid(row=13,column=1,columnspan=1,sticky=W) self.userentries['Southwest_box'] = Scord Button(self, text='Zoom',pady=1,padx=1,command=self.zoomin).grid(row=14,column=1,sticky=W+S+E+N,columnspan=1) Button(self, text='Reset',pady=1,padx=1,command=self.resetzoom).grid(row=14,column=2,sticky=W+S+E+N,columnspan=1) Button(self, text='Draw',pady=1,padx=1,command=self.drawbox).grid(row=14,column=3,sticky=W+S+E+N,columnspan=1) #Drawing profiles Label(self,text='Bounds of profile [lon/lat]',bg='azure',height=4,pady=2,padx=1,font='Helvetica 14 bold').grid(row=11,column=6,columnspan=4,sticky=W+E+S+N) Stcord = Entry(self) Stcord.grid(row=12,column=7,columnspan=1,sticky=E) Label(self,text='Start:').grid(row=12,column=6,columnspan=3,sticky=W) self.userentries['profile_start'] = Stcord Edcord = Entry(self) Edcord.grid(row=13,column=7,columnspan=1,sticky=E) Label(self,text='End:').grid(row=13,column=6,columnspan=3,sticky=W) self.userentries['profile_end'] = Edcord Button(self, text='Plot profile',pady=1,padx=1,command=self.plotprofile).grid(row=14,column=6,sticky=W+S+E+N,columnspan=4) #Event labels Label(self,text='Event options',bd=5,bg='azure',height=4,pady=2,padx=1,font='Helvetica 14 bold').grid(row=11,column=11,columnspan=4,sticky=W+E+S+N) Evtmags = Entry(self) Evtmags.grid(row=12,column=12,columnspan=2,sticky=E) Label(self,text='Event magnitude range [min-max]').grid(row=12,column=11,columnspan=1,sticky=W) self.userentries['magrange'] = Evtmags Evttime = Entry(self) Evttime.grid(row=13,column=12,columnspan=2,sticky=E) Label(self,text='Event start date [yyyy-mm-dd]').grid(row=13,column=11,columnspan=1,sticky=W) self.userentries['evttime'] = Evttime Button(self, text='Set',pady=1,padx=1,command=self.setquakesmanual).grid(row=14,column=11,sticky=W+S+E+N,columnspan=1) Button(self, text='Reset',pady=1,padx=1,command=self.removemapobjs).grid(row=14,column=12,sticky=W+S+E+N,columnspan=1) #Set up the label showing when the datasets are refreshed: the current time goes into that label self.timer = StringVar() Label(self,textvariable=self.timer,bg='azure',height=1,pady=1,padx=1,font='Helvetica 10 bold').grid(row=0,column=0,columnspan=4,sticky=W+E+S+N) self.timer.set('Updated %s' %str(time.asctime())) self.quakeinfo = StringVar() Label(self,textvariable=self.quakeinfo,bg='azure',height=1,padx=1,pady=1,font='Helvetica 10 bold').grid(row=0,column=8,columnspan=4,sticky=W+E+S+N) self.quakeinfo.set('Displaying: M%s to M%s' %(self.minmag,self.maxmag)) Quitter(self).grid(row=0,column=13,sticky=E) def SetStartMap(self): '''Make global pretty map. Takes a long time so only make on startup''' self.map = Basemap(ax=self.a,lat_0=38,lon_0=-122.0,resolution ='l',llcrnrlon=-179.9,llcrnrlat=-89,urcrnrlon=179.9,urcrnrlat=89) #self.map.shadedrelief() #self.map.arcgisimage(service='NatGeo_World_Map',verbose=False,xpixels=10000) #placeholder - makes things run much faster for debugging self.map.fillcontinents() #plot the plate boundaries for i in self.faults: faults_lons = self.faults[i][0] faults_lats = self.faults[i][1] x,y = self.map(faults_lons, faults_lats) self.map.plot(x,y,'b-',linewidth=0.5) self.map.drawparallels(np.arange(-90,90,30),labels=[1,1,0,0],linewidth=0.5,fontsize=4) self.map.drawmeridians(np.arange(-180,180,30),labels=[0,0,0,1],linewidth=0.5,fontsize=4) self.canvas.draw() #-------------------------- #Default map boundaries #-------------------------- self.minlon = -179.9 self.maxlon = 179.9 self.minlat = -89 self.maxlat = 89 #-------------------------- def SetZoomMap(self,lon1,lon2,lat1,lat2): '''Function that handles the zoom in map creation''' self.a.clear() #Ensure that no extra elememts are plotted self.MTs = None self.quakesplotted = None #scale the longitude and latiude grid increments latinc = (max(lat1,lat2)-min(lat1,lat2))/5 loninc = abs((lon2-lon1)/4) lon0 = int(abs(lon2)-abs(lon1))/2 lat0 = int((lat2-lat1)/2) #choose the resolution: high resolition maps take AGES to load, however if 2.0 < abs(lat2-lat1) < 6.0: res = 'i' elif 0.1 < abs(lat2-lat1) < 1.0: res = 'h' elif 0 < abs(lat2-lat1) < 0.1: res = 'f' else: res = 'l' #choose a resolution based on the size of the provided box print 'Lower left lon: %g' %lon1 print 'Upper right lon: %g' %lon2 print 'lower left lat: %g' %lat1 print 'upper right lat: %g' %lat2 print 'resolution = %s' %res #-------------------------- #Set map boundaries #-------------------------- self.minlat = lat1 self.maxlat = lat2 self.minlon = lon1 self.maxlon = lon2 self.map = Basemap(ax=self.a,lat_0=lat0,lon_0=lon0,resolution=res,llcrnrlon=lon1,llcrnrlat=lat1,urcrnrlon=lon2,urcrnrlat=lat2) self.map.drawparallels(np.arange(lat1,lat2,latinc),labels=[1,1,0,0],linewidth=0.5,fontsize=4) self.map.drawmeridians(np.arange(lon1,lon2,loninc),labels=[0,0,0,1],linewidth=0.5,fontsize=4) self.map.fillcontinents() self.map.drawcountries(linewidth=0.2) self.map.drawcoastlines(linewidth=0.2) self.map.drawstates(linewidth=0.1) #plot the plate boundaries for i in self.faults: faults_lons = self.faults[i][0] faults_lats = self.faults[i][1] x,y = self.map(faults_lons, faults_lats) self.map.plot(x,y,'b-',linewidth=0.5) #Determine the new sizes of the map objects, so that they don't crowd the map mt_width,mt_rad,min_dot,max_dot,min_quake,max_quake = quaketools.determinemtsize(self.minlon,self.maxlon,self.minlat,self.maxlat) print min_quake,max_quake print 'Mt width and radius are as follows:' print mt_width print mt_rad #redraw the earthquakes, if they exist (they should always exist) #keeps the catalog object thats already been saved in memory and just replots the events (unless we want moment tensors) if self.momenttensors == True: self.mtlines, self.MTs, self.quakedots, xs, ys, urls = quaketools.plot_mt(self.map,self.a,self.f,self.quakes,self.mts,self.events,llat=lat1,ulat=lat2,llon=lon1,ulon=lon2,dist_bt=200,radius=mt_rad,mt_width=mt_width) Browse.updatedata(xs,ys,urls,mt_rad) else: quaketools.plot_events(self.map,self.a,self.quakes,llat=lat1,ulat=lat2,llon=lon1,ulon=lon2,dist_bt=100,min_size=min_quake,max_size=max_quake) #self.map = Basemap(ax=self.a,projection='merc',llcrnrlat=lat1,urcrnrlat=lat2,llcrnrlon=lon1,urcrnrlon=lon2,lat_ts=true_scale_lat,resolution='l') #self.map.arcgisimage(service='NatGeo_World_Map',verbose=False,xpixels=10000) #self.map.drawlsmask(land_color="#ddaa66", ocean_color="#7777ff",resolution='i') self.canvas.draw() def zoomin(self): '''Choose how to zoom - using a draw-on box (default) or a user-defined box. The GetBoxCoors() function deals with whether to choose from a user-drawn box or a coordinate-entered box''' try: NElat,NElon,SWlat,SWlon = self.GetBoxCoors() self.SetZoomMap(SWlon,NElon,SWlat,NElat) Browse.updateboxcoords() except: print 'User coordinates not entered correctly' def drawbox(self): '''Draw a box on the map according to the user's choice of coordinates''' boxcoordsNE = self.userentries['Northeast_box'].get() boxcoordsSW = self.userentries['Southwest_box'].get() try: NElon = float(boxcoordsNE.split('/')[0]) NElat = float(boxcoordsNE.split('/')[1]) SWlon = float(boxcoordsSW.split('/')[0]) SWlat = float(boxcoordsSW.split('/')[1]) except: print 'User coordinates not entered correctly' boxlats = [SWlat,SWlat,NElat,NElat,SWlat] boxlons = [SWlon,NElon,NElon,SWlon,SWlon] xevent,yevent = self.map(boxlons,boxlats) self.map.plot(xevent,yevent,'r-',linewidth=1,alpha=0.9) self.map.plot(xevent,yevent,'k.') self.canvas.draw() def resetzoom(self,resettensors=False): '''Reset the zoom to the global map''' if resettensors == True: self.momenttensors = True self.a.clear() self.MTs = None self.quakesplotted = None #reset defaults self.minmag = 4.5 self.maxmag = 10.0 #reset the display defaults self.quakeinfo.set('Displaying: M%s to M%s' %(self.minmag,self.maxmag)) Browse.updateboxcoords() #Return to the default quake map self.SetStartMap() self.WDquakes() def plotprofile(self): '''Draw a profile on the map according to the user's choice of coordinates. Will be extended to draw a depth cross section along the profile line, showing the depth distribition of the seismicity on the map''' linecoordsstart = self.userentries['profile_start'].get() linecoordsend = self.userentries['profile_end'].get() try: Slon = linecoordsstart.split('/')[0] Slat = linecoordsstart.split('/')[1] Elon = linecoordsend.split('/')[0] Elat = linecoordsend.split('/')[1] except: print 'User coordinates not entered correctly' lats = [Slat,Elat] lons = [Slon,Elon] xevent,yevent = self.map(lons,lats) self.map.plot(xevent,yevent,'r-',linewidth=1,alpha=0.9) self.map.plot(xevent,yevent,'k.') self.canvas.draw() print '-----------------------------------------------' print 'Building depth slice' print '-----------------------------------------------' quakestats.depthslicequakes(self.quakes,self.mts,float(lons[0]),float(lats[0]),float(lons[1]),float(lats[1])) def setquakesmanual(self,t1=None): '''Fetch an earthquake catalog corresponding to the user's choice''' #Reset the map self.removemapobjs() if not t1: starttime = self.userentries['evttime'].get() t1 = str(starttime)+'T00:00:00.000' t2 = self.now try: t1 = UTCDateTime(t1) except: print 'Alert: Times not entered correctly!' print 'Default time range : 1970-01-01 to today' t1 = UTCDateTime("1970-01-01T00:00:00.000") mags = self.userentries['magrange'].get() try: mag1 = mags.split('-')[0].strip() mag2 = mags.split('-')[1].strip() except: print 'Alert: Magnitudes not entered correctly!' if self.minmag: mag1 = self.minmag mag2 = self.maxmag else: print 'Default magnitude range: 6-10' mag1 = 6 mag2 = 10 #get the box coordinates, if they exist try: NElat,NElon,SWlat,SWlon = self.GetBoxCoors() #set the dafault map coordinates to be those corresponding to the inputs self.minlon = SWlon self.maxlon = NElon self.minlat = SWlat self.maxlat = NElat except: print 'No user defined coordinates found - using coordinates stored in memory!' #get the earthquake catalog just for the region of interest (may be a zoom box) #Note that the earthquake statistics package uses the catalog output from this call to get_cat, so ensure to #update the quake catalog before looking at the stats mt_width,mt_rad,min_dot,max_dot,min_quake,max_quake = quaketools.determinemtsize(self.minlon,self.maxlon,self.minlat,self.maxlat) print 'Mt width and radius are as follows:' print mt_width print mt_rad self.catalog = quaketools.get_cat(data_center=self.datacenter,includeallorigins=True,starttime=t1,endtime=t2,minmagnitude=mag1,maxmagnitude=mag2,maxlongitude=self.maxlon,minlongitude=self.minlon,maxlatitude=self.maxlat,minlatitude=self.minlat) self.quakes, self.mts, self.events, self.qblasts = quaketools.cat2list(self.catalog) if self.momenttensors == True: #plot the moment tensors and redraw self.mtlines, self.MTs, self.quakedots, xs, ys, urls = quaketools.plot_mt(self.map,self.a,self.f,self.quakes,self.mts,self.events,llat=self.minlat,ulat=self.maxlat,llon=self.minlon,ulon=self.maxlon,dist_bt=200,radius=mt_rad,mt_width=mt_width) Browse.updatedata(xs,ys,urls,mt_rad) else: #only plotting events, so continue self.quakesplotted = quaketools.plot_events(self.map,self.a,self.quakes,llat=self.minlat,ulat=self.maxlat,llon=self.minlon,ulon=self.maxlon,dist_bt=100,min_size=min_quake,max_size=max_quake) #update display information self.minmag = mag1 self.maxmag = mag2 self.quakeinfo.set('Displaying: M%s to M%s' %(self.minmag,self.maxmag)) self.canvas.draw() def removemapobjs(self): '''Remove all objects painted on top of the map, but does not redraw the map itself''' Browse.updatedata() Browse.updateboxcoords() if self.quakesplotted != None: self.quakesplotted.remove() self.canvas.draw() self.quakesplotted = None if self.MTs != None: for e1 in self.MTs: e1.remove() for e2 in self.mtlines: if e2 is not None: e2[0].remove() self.quakedots.remove() #self.mtlines.remove() #self.mtdots.remove() #self.MTs.remove() self.canvas.draw() self.MTs = None else: print 'Default reset function' def refreshloop(self): '''Refresh the map data accordingly''' print 'Refreshing quake dataset' self.timer.set(str(time.asctime())) #GetLatestseismicity(14) #Get seismicity from the last 14 days, down to magnitude 0.1 #self.Autoupdateplot() #self.Autoupdate() tk.after(210000,self.refreshloop) #Refresh the dataset every 10 mins def Createmenubar(self,parent): '''Create the drop down menu: allows user to add data layers to the Alaska''' menubar = Menu(self) parent.config(menu=menubar) filemenu = Menu(menubar,tearoff=0,font="Helvetica 16 bold") #insert a drop-down menu submenu1 = Menu(filemenu) submenu1.add_command(label='M2.5+ 1 Week',command=self.M25_1wk) submenu1.add_command(label='M4.5+ 1 Week',command=self.M45_1wk) submenu1.add_command(label='M2.5+ 30 days',command=self.M25_30d) submenu1.add_command(label='M4.5+ 30 days',command=self.M45_30d) submenu1.add_command(label='M6.0+ 365 days',command=self.M60_365d) filemenu.add_cascade(label='Event options',menu=submenu1,underline=0) filemenu.add_separator() submenu2 = Menu(filemenu) submenu2.add_command(label='World',command=self.WDquakes) submenu2.add_command(label='USA',command=self.USAquakes) submenu2.add_command(label='California',command=self.CAquakes) submenu2.add_command(label='Oklahoma',command=self.OKquakes) submenu2.add_command(label='Alaska',command=self.AKquakes) filemenu.add_cascade(label='Region options',menu=submenu2,underline=0) #add the drop down menu to the menu bar filemenu.add_separator() submenu3 = Menu(filemenu) submenu3.add_command(label='Save current frame',command=self.SaveasPDF) submenu3.add_command(label='Pretty map (may take a long time to load!)',command=lambda: self.PrettyMap(self.minlat,self.maxlat,self.minlon,self.maxlon)) submenu3.add_command(label='Display moment tensors',command=self.setMTs) submenu3.add_command(label='Display events only',command=self.setevents) filemenu.add_cascade(label='Other options',menu=submenu3) #add the drop down menu to the menu bar menubar.add_cascade(label="Options",menu=filemenu) subm1 = Menu(menubar,tearoff=0) subm1.add_command(label='Gutenburg-Richter plot',command=self.placeholder) subm1.add_command(label='Cumulative moment release',command=self.cumulate_moment) subm1.add_command(label='Binned quake activity',command=self.quaketimeplot) menubar.add_cascade(label="Statistics",menu=subm1) ############################################################################# # Some standard options for plotting quakes for various lengths of time ############################################################################# def M25_1wk(self): self.minmag = 2.5 self.maxmag = 10 #Set the map to display all events in the last week t1=self.now-604800 self.setquakesmanual(t1) self.starttime = t1 def M45_1wk(self): self.minmag = 4.5 self.maxmag = 10 #Set the map to display all events in the last week t1=self.now-604800 self.setquakesmanual(t1) self.starttime = t1 def M25_30d(self): self.minmag = 2.5 self.maxmag = 10 #Set the map to display all events of M2.5+ in the last 30 days t1=self.now-2592000 self.setquakesmanual(t1) self.starttime = t1 def M45_30d(self): self.minmag = 4.5 self.maxmag = 10 #Set the map to display all events of M4.5+ in the last 30 days t1=self.now-2592000 self.setquakesmanual(t1) self.starttime = t1 def M60_365d(self): self.minmag = 6.0 self.maxmag = 10 #Set the map to display all events in the last year t1=self.now-31536000 self.setquakesmanual(t1) self.starttime = t1 ############################################################################# # Some standard options for plotting quakes for various lengths of time ############################################################################# def quaketimeplot(self): '''Link to quake_atats historgram of quake activity binned by time''' if ((self.now-self.starttime)) < 3e6: fq = 'day' else: fq = 'week' quakestats.quakeswithtime(self.quakes,freq=fq) def cumulate_moment(self): '''Link to quake_stats plot of cumulative moment release''' quakestats.cumulativemoment(self.quakes) def placeholder(self): '''Will link to some other command''' print 'not yet coded!' def worldquakes(self): '''Get world catalog of quakes and plot. If we already on a global map, then we just plot the elemments''' print 'Getting world quakes!!' t2 = self.now t1 = t2-self.starttime #Determine the new sizes of the map objects, so that they don't crowd the map mt_width,mt_rad,min_dot,max_dot,min_quake,max_quake = quaketools.determinemtsize(self.minlon,self.maxlon,self.minlat,self.maxlat) print min_quake,max_quake self.catalog = quaketools.get_cat(data_center=self.datacenter,includeallorigins=True,starttime=t1,endtime=t2,minmagnitude=self.minmag,maxmagnitude=self.maxmag) self.quakes, self.mts, self.events, self.qblasts = quaketools.cat2list(self.catalog) if self.momenttensors == True: self.quakesplotted = quaketools.plot_events(self.map,self.a,self.quakes) self.mtlines, self.MTs, self.quakedots, xs, ys, urls = quaketools.plot_mt(self.map,self.a,self.f,self.quakes,self.mts,self.events,mt_width=3,radius=self.mtradius,angle_step=40) Browse.updatedata(xs,ys,urls,self.mtradius) #print xs,ys,urls else: #only plotting events, so continue self.quakesplotted = quaketools.plot_events(self.map,self.a,self.quakes,min_size=min_quake,max_side=max_quake) self.canvas.draw() def Autoupdate(self): '''For use with the auto-updater - get only the quakes within the map region and update''' print 'Autoupdating catalog!' self.setquakesmanual(t1=self.starttime) def USAquakes(self): '''Zoom in on USA region''' self.SetZoomMap(-132,-70,27,52) def CAquakes(self): '''Zoom in on CA region''' self.SetZoomMap(-132,-115,32,42) def AKquakes(self): print 'display' def OKquakes(self): '''Zoom in on OK/Texas region''' self.SetZoomMap(-105,-90.5,29,38) def WDquakes(self): self.SetZoomMap(-179.9,179.9,-89.9,89.9) def PrettyMap(self,lat1,lat2,lon1,lon2): '''Display topography and street names on the current map - could be modified for various map types''' #scale the longitude and latiude grid increments latinc = (max(lat1,lat2)-min(lat1,lat2))/5 loninc = abs((lon2-lon1)/4) lon0 = int(abs(lon2)-abs(lon1))/2 lat0 = int((lat2-lat1)/2) if 2.0 < abs(lat2-lat1) < 6.0: res = 'i' elif 0.1 < abs(lat2-lat1) < 1.0: res = 'h' elif 0 < abs(lat2-lat1) < 0.1: res = 'f' else: res = 'l' self.a.clear() #Determine the new sizes of the map objects, so that they don't crowd the map mt_width,mt_rad,min_dot,max_dot,min_quake,max_quake = quaketools.determinemtsize(self.minlon,self.maxlon,self.minlat,self.maxlat) #Map setup and quakes self.map = Basemap(ax=self.a,lat_0=lat0,lon_0=lon0,resolution ='l',llcrnrlon=lon1,llcrnrlat=lat1,urcrnrlon=lon2,urcrnrlat=lat2) self.map.arcgisimage(service='NatGeo_World_Map',verbose=False,xpixels=600) #plot some extra information self.map.drawparallels(np.arange(lat1,lat2,latinc),labels=[1,1,0,0],linewidth=0.5,fontsize=4) self.map.drawmeridians(np.arange(lon1,lon2,loninc),labels=[0,0,0,1],linewidth=0.5,fontsize=4) self.map.drawcountries() self.map.drawcoastlines(linewidth=0.1) #replot the quakes if self.momenttensors == True: #plot the moment tensors and redraw #self.quakesplotted = quaketools.plot_events(self.map,self.a,self.quakes) self.mtlines, self.MTs, self.quakedots, xs, ys, urls = quaketools.plot_mt(self.map,self.a,self.f,self.quakes,self.mts,self.events,mt_width=mt_width,radius=mt_rad,angle_step=40,llat=lat1,ulat=lat2,llon=lon1,ulon=lon2) Browse.updatedata(xs,ys,urls,mt_rad) else: #only plotting events, so continue self.quakesplotted = quaketools.plot_events(self.map,self.a,self.quakes,llat=self.minlat,ulat=self.maxlat,llon=self.minlon,ulon=self.maxlon,dist_bt=100,min_size=min_quake,max_size=max_quake) self.canvas.draw() def setMTs(self): self.momenttensors = True self.removemapobjs() self.setquakesmanual(t1=self.starttime) def setevents(self): self.momenttensors = None self.removemapobjs() self.setquakesmanual(t1=self.starttime) def SaveasPDF(self): '''Saves the current frame as a .pdf file''' #self.f.savefig('test.pdf',format='pdf') filelocation = tkFileDialog.asksaveasfilename(defaultextension='.pdf') self.f.savefig(filelocation,format='pdf') print 'Saved current figure' def GetBoxCoors(self): '''Returns the coordinates of user defined zoom box''' #first try to get the coordinates of a zoom region: blats,blons = Browse.returnboxcoords() if blats: SWlon = blons[0] NElon = blons[2] SWlat = blats[0] NElat = blats[1] if NElat < SWlat: tmp = NElat.copy() NElat = SWlat SWlat = tmp if NElon < SWlon: tmp = NElon.copy() NElon = SWlon SWlon = tmp return NElat,NElon,SWlat,SWlon else: boxcoordsNE = self.userentries['Northeast_box'].get() boxcoordsSW = self.userentries['Southwest_box'].get() try: NElon = float(boxcoordsNE.split('/')[0]) NElat = float(boxcoordsNE.split('/')[1]) SWlon = float(boxcoordsSW.split('/')[0]) SWlat = float(boxcoordsSW.split('/')[1]) return NElat,NElon,SWlat,SWlon except: return None
# -*- coding: utf-8 -*- import matplotlib.pyplot as plt import numpy as np from mpl_toolkits.basemap import Basemap from sklearn.cluster import KMean # Crée une projection centrée sur San Francisco frame = ((-122.52, 37.68), (-122.38,37.82)) map = Basemap( llcrnrlon=frame[0][0], llcrnrlat=frame[0][1], urcrnrlon=frame[1][0], urcrnrlat=frame[1][1], epsg=3493) # Charge le plan associé à la projection map.arcgisimage( service='ESRI_StreetMap_World_2D', xpixels = 1000, verbose= True) latitudes = np.array([-122.425891675136 ,-122.425891675136]); longitudes = [37.7745985956747, 37.7745985956747] couleurs = ['r', 'r' ]; rayons =[0.1, 0.1] # Convertit (long,lat) en position image (X,Y)=map(longitudes, latitudes) # Appel classique aux fonctions matplotlib plt.scatter(X, Y, rayons, marker = 'o', color = couleurs, alpha = 0.4) # Lance l’affichage plt.show()
def e1e2(humfile, sonpath, cs2cs_args, ph, temp, salinity, beam, transfreq, integ, numclusters, doplot): ''' Analysis of first (e1, 'roughness') and second (e2, 'hardness') echo returns from the high-frequency downward looking echosounder Generates generalised acoustic parameters for the purposes of point classification of submerged substrates/vegetation Accounts for the absorption of sound in water Does a basic k-means cluster of e1 and e2 coefficients into specified number of 'acoustic classes' based on code by Barb Fagetter ([email protected]) Syntax ---------- [] = PyHum.e1e2(humfile, sonpath, cs2cs_args, ph, temp, salinity, beam, transfreq, integ, numclusters, doplot) Parameters ---------- humfile : str path to the .DAT file sonpath : str path where the *.SON files are cs2cs_args : int, *optional* [Default="epsg:26949"] arguments to create coordinates in a projected coordinate system this argument gets given to pyproj to turn wgs84 (lat/lon) coordinates into any projection supported by the proj.4 libraries ph : float, *optional* [Default=7.0] water acidity in pH temp : float, *optional* [Default=10.0] water temperature in degrees Celsius salinity : float, *optional* [Default=0.0] salinity of water in parts per thousand beam : float, *optional* [Default=20.0] beam width in degrees transfreq : float, *optional* [Default=200.0] transducer frequency in kHz integ : int, *optional* [Default=5] number of pings over which to integrate numclusters : int, *optional* [Default=3] transducer frequency in kHz doplot : int, *optional* [Default=1] 1 = make plots, otherwise do not Returns ------- sonpath+base+'rough_and_hard'+str(p)+'.csv' : csv file contains the following fields: 'longitude', 'latitude', 'easting', 'northing', 'depth', 'roughness', 'hardness', 'average roughness', 'average hardness','k-mean label' of the pth chunk 'average' implies average over 'integ' successive pings The following are returned if doplot==1: sonpath+'e1e2_scan'+str(p).png : png image file png image file showing the downward echosounder echogram overlain with the locations of the start and end of the first and second echo region envelope sonpath+'e1e2_kmeans'+str(p).png: png image file png image file showing 1) (left) volume scattering coefficient 1 versus volume scattering coefficient 2, colour-coded by k-means acoustic class, and 2) (right) e1 versus e2, colour-coded by k-means acoustic class sonpath+'rgh_hard_kmeans'+str(p).png : png image file png image file showing scatter plot of easting versus northing colour-coded by k-means acoustic class sonpath+'map_rgh'+str(p).png : png image file png image file showing scatter plot of 'roughness' (e1) overlying an aerial image pulled from an ESRI image server sonpath+'map_hard'+str(p).png : png image file png image file showing scatter plot of 'hardness' (e2) overlying an aerial image pulled from an ESRI image server sonpath,'Rough'+str(p).png : png image file png image overlay associated with the kml file, sonpath,'Hard'+str(p).kml sonpath,'Rough'+str(p).kml : kml file kml overlay for showing roughness scatter plot (sonpath,'Rough'+str(p).png) sonpath,'Hard'+str(p).png : png image file png image overlay associated with the kml file, sonpath,'Hard'+str(p).kml sonpath,'Hard'+str(p).kml : kml file kml overlay for showing harness scatter plot (sonpath,'Hard'+str(p).png) ''' # prompt user to supply file if no input file given if not humfile: print 'An input file is required!!!!!!' Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing inputfile = askopenfilename(filetypes=[("DAT files","*.DAT")]) # prompt user to supply directory if no input sonpath is given if not sonpath: print 'A *.SON directory is required!!!!!!' Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing sonpath = askdirectory() # print given arguments to screen and convert data type where necessary if humfile: print 'Input file is %s' % (humfile) if sonpath: print 'Sonar file path is %s' % (sonpath) if cs2cs_args: print 'cs2cs arguments are %s' % (cs2cs_args) if beam: beam = np.asarray(beam,float) print 'Beam is %s deg' % (str(beam)) if salinity: salinity = np.asarray(salinity,float) print 'Salinity is %s ppt' % (str(salinity)) if ph: ph = np.asarray(ph,float) print 'pH is %s' % (str(ph)) if temp: temp = np.asarray(temp,float) print 'Temperature is %s' % (str(temp)) if transfreq: transfreq = np.asarray(transfreq,float) print 'Dwnward sonar freq. is %s' % (str(transfreq)) if integ: integ = np.asarray(integ,int) print 'number of records for integration is %s' % (str(integ)) if numclusters: numclusters = np.asarray(numclusters,int) print 'number of returned acoustic clusters is %s' % (str(numclusters)) if doplot: doplot = int(doplot) if doplot==0: print "Plots will not be made" if not beam: beam = 20.0 print '[Default] Beam is %s deg' % (str(beam)) if not salinity: if salinity != 0.0: salinity = 0.0 print '[Default] Salinity is %s ppt' % (str(salinity)) if not ph: ph = 7.0 print '[Default] pH is %s' % (str(ph)) if not integ: integ = 5 print '[Default] Number of records for integration is %s' % (str(ph)) if not numclusters: numclusters = 3 print '[Default] Number of acoustic clusters is %s' % (str(ph)) if not temp: temp = 10.0 print '[Default] Temperature is %s degC' % (str(temp)) if not transfreq: transfreq = 200.0 print '[Default] Dwnward freq. is %s kHz' % (str(transfreq)) if not cs2cs_args: # arguments to pass to cs2cs for coordinate transforms cs2cs_args = "epsg:26949" print '[Default] cs2cs arguments are %s' % (cs2cs_args) if not doplot: if doplot != 0: doplot = 1 print "[Default] Plots will be made" # if son path name supplied has no separator at end, put one on if sonpath[-1]!=os.sep: sonpath = sonpath + os.sep base = humfile.split('.DAT') # get base of file name for output base = base[0].split(os.sep)[-1] # remove underscores, negatives and spaces from basename if base.find('_')>-1: base = base[:base.find('_')] if base.find('-')>-1: base = base[:base.find('-')] if base.find(' ')>-1: base = base[:base.find(' ')] beamwidth = beam*(np.sqrt(0.5)) equivbeam = (5.78/(np.power(1.6,2)))*(np.power((np.sin((beamwidth*np.pi)/(2*180))),2)) meta = loadmat(sonpath+base+'meta.mat') c = np.squeeze(meta['c']) t = np.squeeze(meta['t']) f = np.squeeze(meta['f']) maxW = np.squeeze(meta['maxW']) lat = np.squeeze(meta['lat']) lon = np.squeeze(meta['lon']) es = np.squeeze(meta['es']) ns = np.squeeze(meta['ns']) dep = np.squeeze(meta['dep_m']) del meta # load memory mapped scans shape_hi= np.squeeze(loadmat(sonpath+base+'meta.mat')['shape_hi']) if shape_hi!='': try: dwnhi_fp = np.memmap(sonpath+base+'_data_dwnhi.dat', dtype='int16', mode='r', shape=tuple(shape_hi)) except: shape_lo= np.squeeze(loadmat(sonpath+base+'meta.mat')['shape_low']) dwnhi_fp = np.memmap(sonpath+base+'_data_dwnhi.dat', dtype='int16', mode='r', shape=tuple(shape_lo)) if 'dwnhi_fp' in locals(): theta3dB = np.arcsin(c/(t*(f*1000))) # *(180/pi) # to see in degs ft = (np.pi/2)*(1/theta3dB) bed = ft*dep i = np.linspace(1,shape_hi[0]*shape_hi[2], len(bed)) #np.shape(beam_data)[1],len(bed)) #bedi = np.interp(np.linspace(1,np.shape(beam_data)[1],np.shape(beam_data)[1]), i, bed) bedi = np.interp(np.linspace(1,shape_hi[0]*shape_hi[2],shape_hi[0]*shape_hi[2]), i, bed) ei = np.interp(np.linspace(1,shape_hi[0]*shape_hi[2],shape_hi[0]*shape_hi[2]), i, es) ni = np.interp(np.linspace(1,shape_hi[0]*shape_hi[2],shape_hi[0]*shape_hi[2]), i, ns) lati = np.interp(np.linspace(1,shape_hi[0]*shape_hi[2],shape_hi[0]*shape_hi[2]), i, lat) loni = np.interp(np.linspace(1,shape_hi[0]*shape_hi[2],shape_hi[0]*shape_hi[2]), i, lon) del i bedi = np.asarray(bedi,'int') depi = ((1/ft)*bedi) # near-field region nf = int(ft*(1000*(0.105**2)*f/(4*1500))) #absorption = calcAb(c, ph, salinity, temp, np.asarray(depi), transfreq) absorption = water_atten(np.asarray(depi), transfreq, c, ph, temp, salinity) for p in xrange(len(dwnhi_fp)): #make an index of every other record ind = range(0,np.shape(dwnhi_fp[p])[1]) Zdepi = depi[shape_hi[2]*p:shape_hi[2]*(p+1)] Zabsorp = absorption[shape_hi[2]*p:shape_hi[2]*(p+1)] Zlat = lati[shape_hi[2]*p:shape_hi[2]*(p+1)] Zlon = loni[shape_hi[2]*p:shape_hi[2]*(p+1)] Zes = ei[shape_hi[2]*p:shape_hi[2]*(p+1)] Zns = ni[shape_hi[2]*p:shape_hi[2]*(p+1)] try: #parallel processing with all available cores w = Parallel(n_jobs=-1, verbose=0)(delayed(get_rgh_hrd)(dwnhi_fp[p][:,i],Zdepi[i],Zabsorp[i],c,nf,transfreq,equivbeam,maxW,pi,ft) for i in ind) except: #fall back to serial w = Parallel(n_jobs=1, verbose=0)(delayed(get_rgh_hrd)(dwnhi_fp[p][:,i],Zdepi[i],Zabsorp[i],c,nf,transfreq,equivbeam,maxW,pi,ft) for i in ind) rough, hard, sv_e1, sv_e2, e1a, e1b, e2a, e2b = zip(*w) rough = np.array(rough,'float') rough[rough==0.0] = np.nan hard = np.array(hard,'float') hard[hard==0.0] = np.nan sv_e1 = np.array(sv_e1,'float') sv_e1[sv_e1==0.0] = np.nan sv_e2 = np.array(sv_e2,'float') sv_e2[sv_e2==0.0] = np.nan try: nans, y= humutils.nan_helper(rough) rough[nans]= np.interp(y(nans), y(~nans), rough[~nans]) except: continue try: nans, y= humutils.nan_helper(hard) hard[nans]= np.interp(y(nans), y(~nans), hard[~nans]) except: continue try: nans, y= humutils.nan_helper(sv_e1) sv_e1[nans]= np.interp(y(nans), y(~nans), sv_e1[~nans]) except: continue try: nans, y= humutils.nan_helper(sv_e2) sv_e2[nans]= np.interp(y(nans), y(~nans), sv_e2[~nans]) except: continue data = np.column_stack([sv_e1, sv_e2]) k_means = MiniBatchKMeans(numclusters) # fit the model k_means.fit(data) values = k_means.cluster_centers_.squeeze() labels = k_means.labels_ hardav = humutils.runningMeanFast(hard,integ) roughav = humutils.runningMeanFast(rough,integ) f = open(sonpath+base+'rough_and_hard'+str(p)+'.csv', 'wt') writer = csv.writer(f) writer.writerow( ('longitude', 'latitude', 'easting', 'northing', 'depth', 'roughness', 'hardness', 'average roughness', 'average hardness','k-mean label') ) for i in range(0, len(rough)): writer.writerow(( float(Zlon[i]),float(Zlat[i]),float(Zes[i]),float(Zns[i]),float(Zdepi[i]),float(rough[i]),float(hard[i]),float(roughav[i]),float(hardav[i]), labels[i].astype(int) )) f.close() if doplot==1: try: fig = plt.figure() plt.imshow(dwnhi_fp[p], cmap='gray') plt.plot(e1a,'r'); plt.plot(e1b,'y'); plt.plot(e2a,'c'); plt.plot(e2b,'m'); plt.axis('tight') #plt.show() custom_save(sonpath,'e1e2_scan'+str(p)) del fig except: print "plot could not be produced" if doplot==1: try: fig = plt.figure() fig.subplots_adjust(wspace = 0.4, hspace=0.4) plt.subplot(221) plt.plot(sv_e1[labels==0],sv_e2[labels==0],'ko'); plt.plot(sv_e1[labels==1],sv_e2[labels==1],'ro'); plt.plot(sv_e1[labels==2],sv_e2[labels==2],'bo'); plt.xlabel('SV1'); plt.ylabel('SV2') plt.xlim(0,1); plt.ylim(0,1) plt.subplot(222) plt.plot(rough[labels==0],hard[labels==0],'ko'); plt.plot(rough[labels==1],hard[labels==1],'ro'); plt.plot(rough[labels==2],hard[labels==2],'bo'); plt.xlabel('E1'); plt.ylabel('E2') plt.xlim(1,8); plt.ylim(1,8) #plt.show() custom_save(sonpath,'e1e2_kmeans'+str(p)) del fig except: print "plot could not be produced" if doplot==1: try: fig = plt.figure() s=plt.scatter(Zes[labels==0],Zns[labels==0],marker='o',c='k', s=10, linewidth=0, vmin=0, vmax=8); s=plt.scatter(Zes[labels==1],Zns[labels==1],marker='o',c='r', s=10, linewidth=0, vmin=0, vmax=8); s=plt.scatter(Zes[labels==2],Zns[labels==2],marker='o',c='b', s=10, linewidth=0, vmin=0, vmax=8); custom_save(sonpath,'rgh_hard_kmeans'+str(p)) del fig except: print "plot could not be produced" if doplot==1: try: print "drawing and printing map ..." fig = plt.figure(frameon=False) #fig.subplots_adjust(wspace = 0.4, hspace=0.4) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], #epsg=26949, resolution = 'i', #h #f llcrnrlon=np.min(Zlon)-0.0001, llcrnrlat=np.min(Zlat)-0.0001, urcrnrlon=np.max(Zlon)+0.0001, urcrnrlat=np.max(Zlat)+0.0001) # draw point cloud x,y = map.projtran(Zlon, Zlat) cs = map.scatter(x.flatten(), y.flatten(), 1, rough.flatten(), linewidth=0, vmin=0, vmax=8) map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=300) cbar = map.colorbar(cs,location='bottom',pad="5%") cbar.set_label('E1') cbar.set_ticks([0,2,4,6,8]) custom_save(sonpath,'map_rgh'+str(p)) del fig except: print "plot could not be produced" if doplot==1: try: fig = plt.figure() #fig.subplots_adjust(wspace = 0.4, hspace=0.4) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], resolution = 'i', #h #f llcrnrlon=np.min(Zlon)-0.0001, llcrnrlat=np.min(Zlat)-0.0001, urcrnrlon=np.max(Zlon)+0.0001, urcrnrlat=np.max(Zlat)+0.0001) # draw point cloud x,y = map.projtran(Zlon, Zlat) cs = map.scatter(x.flatten(), y.flatten(), 1, hard.flatten(), linewidth=0, vmin=0, vmax=8) map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=300) cbar = map.colorbar(cs,location='bottom',pad="5%") cbar.set_label('E2') cbar.set_ticks([0,2,4,6,8]) custom_save(sonpath,'map_hard'+str(p)) del fig except: print "plot could not be produced" if doplot==1: try: print "drawing and printing map ..." fig = plt.figure(frameon=False) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], #26949, resolution = 'i', #h #f llcrnrlon=np.min(Zlon)-0.001, llcrnrlat=np.min(Zlat)-0.001, urcrnrlon=np.max(Zlon)+0.001, urcrnrlat=np.max(Zlat)+0.001) ax = plt.Axes(fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) ## draw point cloud x,y = map.projtran(Zlon, Zlat) map.scatter(x.flatten(), y.flatten(), 1, rough.flatten(), linewidth = '0', vmin=0, vmax=8) custom_save(sonpath,'Rough'+str(p)) del fig kml = simplekml.Kml() ground = kml.newgroundoverlay(name='GroundOverlay') ground.icon.href = 'Rough'+str(p)+'.png' ground.latlonbox.north = np.min(Zlat)-0.001 ground.latlonbox.south = np.max(Zlat)+0.001 ground.latlonbox.east = np.max(Zlon)+0.001 ground.latlonbox.west = np.min(Zlon)-0.001 ground.latlonbox.rotation = 0 kml.save(sonpath+'Rough'+str(p)+'.kml') except: print "plot could not be produced" if doplot==1: try: print "drawing and printing map ..." fig = plt.figure(frameon=False) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], #26949, resolution = 'i', #h #f llcrnrlon=np.min(Zlon)-0.001, llcrnrlat=np.min(Zlat)-0.001, urcrnrlon=np.max(Zlon)+0.001, urcrnrlat=np.max(Zlat)+0.001) ax = plt.Axes(fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) ## draw point cloud x,y = map.projtran(Zlon, Zlat) map.scatter(x.flatten(), y.flatten(), 1, hard.flatten(), linewidth = '0', vmin=0, vmax=8) custom_save(sonpath,'Hard'+str(p)) del fig kml = simplekml.Kml() ground = kml.newgroundoverlay(name='GroundOverlay') ground.icon.href = 'Hard'+str(p)+'.png' ground.latlonbox.north = np.min(Zlat)-0.001 ground.latlonbox.south = np.max(Zlat)+0.001 ground.latlonbox.east = np.max(Zlon)+0.001 ground.latlonbox.west = np.min(Zlon)-0.001 ground.latlonbox.rotation = 0 kml.save(sonpath+'Hard'+str(p)+'.kml') except: print "plot could not be produced" else: print "high-frequency downward echosounder data not available"
def plotmap(lons, lats, labels=None, legendlabels=None, markers="o", colors="#FF4400", sizes=20, cmap=None, fontsize=None, fontweight='regular', fontcolor='k', labels_h_offset=0, labels_v_offset=0, mapmargins='0.5deg', figmargins=2, arcgis_service='World_Street_Map', arcgis_xpixels=1500, arcgis_dpi=96, urlfail='ignore', maxmeridians=5, maxparallels=5, legend_pos='bottom', legend_borderaxespad=1.5, legend_ncol=1, title=None, show=False, **kwargs): # @UnusedVariable """ Makes a scatter plot of points on a map background using ArcGIS REST API. :param lons: (array-like of length N or scalar) Longitudes of the data points, in degreee :param lats: (array-like of length N or scalar) Latitudes of the data points, in degree :param labels: (array-like of length N or string. Default: None, no labels) Annotations (labels) for the individual data points on the map. If non-array (e.g. string), the same value will be applied to all points :param legendlabels: (array-like of length N or string. Default: None, no legend) Annotations (labels) for the legend. You can supply a sparse array where only some points will be displayed on the legend. All points with no legend label will not show up in the legend :param sizes: (array-like of length N or number. Default: 20) Sizes (in points^2) of the individual points in the scatter plot. :param markers: (array-like of length N, `MarkerStyle<http://matplotlib.org/api/markers_api.html#matplotlib.markers.MarkerStyle>`_ or string. Default: 'o' - circle) The markers (shapes) to be drawn for each point on the map. See `markers <http://matplotlib.org/api/markers_api.html#module-matplotlib.markers>`_ for more information on the different styles of markers scatter supports. Marker can be either an instance of the class or the text shorthand for a particular marker. :param colors: (array-like of length N, `matplotlib color <http://matplotlib.org/api/colors_api.html>`_, e.g. string. Default: "#FF4400") Colors for the markers (fill color). You can type color transparency by supplying string of 9 elements where the last two characters denote the transparency ('00' fully transparent, 'ff' fully opaque). Note that this is a feature not implemented in `matplotlib` colors, where transparency is given as the last element of the numeric tuple (r, g, b, a) :param fontsize: (numeric or None. Default: None) The fontsize for all texts drawn on the map (labels, axis tick labels, legend). None uses the default figure font size for all. Custom values for the individual text types (e.g. legend texts vs labels texts) can be supplied via the `kwargs` argument and a given prefix (see below) :param fontweight: (string or number. Default: 'regular') The font weight for all texts drawn on the map (labels, axis tick labels, legend). Accepts the values (see http://matplotlib.org/api/text_api.html#matplotlib.text.Text.set_weight): ``` [a numeric value in range 0-1000 | 'ultralight' | 'light' | 'normal' | 'regular' | 'book' | 'medium' | 'roman' | 'semibold' | 'demibold' | 'demi' | 'bold' | 'heavy' | 'extra bold' | 'black' ] ``` Custom values for the individual text types (e.g. legend texts vs labels texts) can be supplied via the `kwargs` argument and a given prefix (see below) :param fontcolor: (`matplotlib color <http://matplotlib.org/api/colors_api.html>`_ or string. Default: 'k', black) The font color for all texts drawn on the map (labels, axis tick labels, legend). Custom values for the individual text types (e.g. legend texts vs labels texts) can be supplied via the `kwargs` argument and a given prefix (see below) :param labels_h_offset: (string, number. Defaults None=0) The horizontal offset to be applied to each label on the map relative to its point coordinates. Negative values will shift the labels westward, positive values eastward. Useful for not overlapping markers and labels. If numeric, it is assumed to be the expressed in degrees. Otherwise, you can supply a string with a number followed by one of the units 'm', 'km' or 'deg' (e.g., '5km', '0.5deg'). Note that this value affects the `horizontalalignment` and `multialignment` properties of the labels (for info see http://matplotlib.org/api/text_api.html#matplotlib.text.Text). Supplying `labels_horizontalalignment` or `labels_ha` as optional argument will override this behaviour (see `kwargs` below) :param labels_v_offset: (string, number. Defaults None=0) The vertical offset to be applied to each label on the map relative to its point coordinates. Negative values will shift the labels southhward, positive values northward. See notes on `labels_h_offset` for details Note that this value affects the `verticalalignment` property of the labels (for info see http://matplotlib.org/api/text_api.html#matplotlib.text.Text). Supplying `labels_verticalalignment` or `labels_va` as optional argument will override this behaviour (see `kwargs` below) :param mapmargins: (array-like of 1,2,3,4 elements, numeric or string, or None=0. Default: '0.5deg'). The map margins, i.e. how much the map has to 'expand/shrink' in any direction, relative to the bounding box calculated to include all points. If array-like, it behaves like the css 'margin' property of html: 4 elements will denote [top, right, bottom, left], two elements will denote [top/bottom, left/right], three elements [top, right/left, bottom], a single element array (or a single number or a string) applies the value to all directions. Finally, elements of the array must be expressed as the arguments `labels_h_offset` or `labels_v_offset`: numbers denoting degrees or strings with units 'm', 'km', 'deg'. Negative values will shrink the map. If string, the argument will be first splitted using commas, semicolon or spaces as delimiters (if no delimiter is found, the string is taken as a single chunk) and converted to an array-like object. :param figmargins: (array-like of 1,2,3,4 elements, number or None=0. Default:2) The figure margins *in font height units* (e.g., 2 means: twice the font height). This argument behaves exactly as `mapmargins` but expands/shrinks the distances between map and figure (image) bounds. Useful to include axis tick labels or legend, if they overflow. Note also that strings are allowed only if they are parsable to float (e.g. "5,6; -12 1") :param arcgis_service: (string, default: 'World_Street_Map'). The map image type, or more technically the service for the map hosted on ArcGIS server. Other values are 'ESRI_Imagery_World_2D' (default in `Basemap.arcgisimage <http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap.arcgisimage>`_), 'World_Topo_Map', 'World_Terrain_Base'. For details, see: http://server.arcgisonline.com/arcgis/rest/services. :param arcgis_xpixels: (numeric, default: 3000). Requested number of image pixels in x-direction (default is 400 in `Basemap.arcgisimage <http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap.arcgisimage>`_). The documentation is quite unclear but this parameter seems to set the zoom of the image. From this `link <http://basemaptutorial.readthedocs.io/en/latest/backgrounds.html#arcgisimage>`_: A bigger number will ask a bigger image, so the image will have more detail. So when the zoom is bigger, `xsize` must be bigger to maintain the resolution :param urlfail: (string, 'raise' or 'ignore'. Default: 'ignore'). Tells what to do if the ArcGIS requet fails (URLError, no internet connection etcetera). By default, on failure a raw map with continents contour, and oceans will be plotted (good for debug). Otherwise, the exception resulting from the web request is raised :param maxmeridians: (numeric default: 5). The number of maximum meridians to be drawn. Set to <=0 to hide meridians. Note that also x-axis labels are drawn. To further manipulate meridians display, use any argument starting with 'mlabels_', 'mlines_' or 'meridians' (see `kwargs` below). E.g., to show only the labels and not the lines, supply as argument `meridians_linewidth=0` or 'mlines_linewidth=0'. :param maxparallels: (numeric default: 5). The number of maximum parallels to be drawn. Set to <=0 to hide parallels. Note that also y-axis labels are drawn. To further manipulate parallels display, use any argument starting with 'plabels_', 'plines_' or 'parallels' (see `kwargs` below). E.g., to show only the labels and not the lines, supply as argument `parallels_linewidth=0` or 'plines_linewidth=0'. :param legend_pos: (string in ['top'. 'bottom', 'right', 'left'], default='bottom'). The legend location with respect to the map. It also adjusts the bounding box that the legend will be anchored to. For customizing entirely the legend placement overriding this parameter, provide `legend_loc` (and optionally `legend_bbox_to_anchor`) in `kwargs` (see below) :param legend_borderaxespad: (numeric, default 1.5) The pad between the axes and legend border, in font units :param legend_ncol: (integer, default=1) The legend number of columns :param title (string or None. Default: None): Title above plot (Note: not tested) :param show (boolean, default: False): Whether to show the figure after plotting or not (Note: not tested). Can be used to do further customization of the plot before showing it. :param fig: (matplotlib figure or None, default: None). Note: deprecated, pass None as supplying an already existing figure with other axes might break the figure layout :param kwargs: any kind of additional argument passed to `matplotlib` and `Basemap` functions or objects. The name of the argument must be of the form ``` prefix_propertyname=propertyvalue ``` where prefix indicates the function/object to be called with keyword argument: ``` propertyname=propertyvalue ``` Current supported prefixes are (for available property names see links): Prefix Passes `propertyname` to ============ ================================================================================== arcgis `Basemap.arcgisimage <http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap.arcgisimage>_ used to retrieve the background map using ArgGIS Server REST API. See also http://basemaptutorial.readthedocs.io/en/latest/backgrounds.html#arcgisimage basemap `Basemap <http://matplotlib.org/basemap/api/basemap_api.html#mpl_toolkits.basemap.Basemap>`_ the object responsible of drawing and managing the map. Note that `basemap_resolution=h` and `basemap_epsg=4326` by default. labels All `texts <http://matplotlib.org/api/text_api.html#matplotlib.text.Text>`_ used to display the point labels on the map legend The `legend <http://matplotlib.org/api/legend_api.html#matplotlib.legend.Legend>`_. See the already implemented arguments `legend_borderaxespad`, `legend_ncol` legendlabels All `texts <http://matplotlib.org/api/text_api.html#matplotlib.text.Text>`_ used to display the text labels of the legend meridians `Basemap.drawmeridians`. For more detailed settings on meridians, see `mlines` and `mlabels` parallels `Basemap.drawparallels`. For more detailed settings on parallels, see `plines` and `plabels` plines All `lines <http://matplotlib.org/api/lines_api.html#matplotlib.lines.Line2D>`_ used to display the parallels plabels All `texts <http://matplotlib.org/api/text_api.html#matplotlib.text.Text>`_ used to display the parallels labels on the y axis mlines All `lines <http://matplotlib.org/api/lines_api.html#matplotlib.lines.Line2D>`_ used to display the meridians mlabels All `texts <http://matplotlib.org/api/text_api.html#matplotlib.text.Text>`_ used to display the meridians labels on the x axis ============ ================================================================================== Examples -------- - `legend_title='abc'` will call `legend(..., title='abc', ...)` - `labels_path_effects=[PathEffects.withStroke(linewidth=2, foreground='white')]` will set the a white contour around each label text - `meridians_labelstyle="+/-"` will call `Basemap.drawmeridians(..., labelstyle="+/-", ...)` Notes: ------ The objects referenced by `plines`, `plabels`, `mlines`, `mlabels` and `legendlabels` cannot be initialized directly with the given properties, which will be set after they are created assuming that for any property `foo` passed as keyword argument in their constructor there exist a method `set_foo(...)` (which will be called with the given propertyvalue). This is most likely always true according to matplotlib api, but we cannot assure it works 100% of the times """ lons, lats, labels, sizes, colors, markers, legendlabels =\ _shapeargs(lons, lats, labels, sizes, colors, markers, legendlabels) # convert html strings to tuples of rgba values in [0.1] if the former are in string format, # because (maybe too old matplotlib version?) colors in the format '#RGBA' are not supported # Also, if cmap is provided, basemap.scatter calls matplotlib.scatter which # wants float sequenes in case of color map if colors.dtype.char in ('U', 'S'): # pylint: disable=no-member colors = np.array([torgba(c) for c in colors]) fig = plt.figure() map_ax = fig.add_axes([0, 0, 1, 1]) # set axes size the same as figure # setup handler for managing basemap coordinates and meridians / parallels calculation: handler = MapHandler(lons, lats, mapmargins) kwa = _joinargs('basemap', kwargs, llcrnrlon=handler.llcrnrlon, llcrnrlat=handler.llcrnrlat, urcrnrlon=handler.urcrnrlon, urcrnrlat=handler.urcrnrlat, epsg='4326', # 4326, # 3395, # 3857, resolution='i', # 'h', ax=map_ax) bmap = Basemap(**kwa) try: kwa = _joinargs("arcgis", kwargs, service=arcgis_service, xpixels=arcgis_xpixels, dpi=arcgis_dpi) # set the map image via a map service. In case you need the returned values, note that # This function returns an ImageAxis (or AxisImage, check matplotlib doc) bmap.arcgisimage(**kwa) except (URLError, HTTPError, socket.error) as exc: # failed, maybe there is not internet connection if urlfail == 'ignore': # Print a simple map offline bmap.drawcoastlines() watercolor = '#4444bb' bmap.fillcontinents(color='#eebb66', lake_color=watercolor) bmap.drawmapboundary(fill_color=watercolor) else: raise # draw meridians and parallels. From basemap.drawmeridians / drawparallels doc: # returns a dictionary whose keys are the meridian values, and # whose values are tuples containing lists of the # matplotlib.lines.Line2D and matplotlib.text.Text instances # associated with each meridian. Deleting an item from the # dictionary removes the correpsonding meridian from the plot. if maxparallels > 0: kwa = _joinargs("parallels", kwargs, linewidth=1, fontsize=fontsize, labels=[0, 1, 1, 0], fontweight=fontweight) parallels = handler.get_parallels(maxparallels) # Old basemap versions have problems with non-integer parallels. try: # Note: the method below # returns a list of text object # represeting the tick labels _dict = bmap.drawparallels(parallels, **kwa) except KeyError: parallels = sorted(list(set(map(int, parallels)))) _dict = bmap.drawparallels(parallels, **kwa) # set custom properties: kwa_lines = _joinargs("plines", kwargs) kwa_labels = _joinargs("plabels", kwargs, color=fontcolor) _mp_set_custom_props(_dict, kwa_lines, kwa_labels) if maxmeridians > 0: kwa = _joinargs("meridians", kwargs, linewidth=1, fontsize=fontsize, labels=[1, 0, 0, 1], fontweight=fontweight) meridians = handler.get_meridians(maxmeridians) _dict = bmap.drawmeridians(meridians, **kwa) # set custom properties: kwa_lines = _joinargs("mlines", kwargs) kwa_labels = _joinargs("mlabels", kwargs, color=fontcolor) _mp_set_custom_props(_dict, kwa_lines, kwa_labels) # fig.get_axes()[0].tick_params(direction='out', length=15) # does not work, check basemap fig.bmap = bmap # compute the native bmap projection coordinates for events. # from the docs (this is kind of outdated, however leave here for the moment): # Calling a Basemap class instance with the arguments lon, lat will # convert lon/lat (in degrees) to x/y map projection # coordinates (in meters). If optional keyword ``inverse`` is # True (default is False), the inverse transformation from x/y # to lon/lat is performed. # For cylindrical equidistant projection (``cyl``), this # does nothing (i.e. x,y == lon,lat). # For non-cylindrical projections, the inverse transformation # always returns longitudes between -180 and 180 degrees. For # cylindrical projections (self.projection == ``cyl``, # ``cea``, ``mill``, ``gall`` or ``merc``) # the inverse transformation will return longitudes between # self.llcrnrlon and self.llcrnrlat. # Input arguments lon, lat can be either scalar floats, # sequences, or numpy arrays. # parse hoffset and voffset and assure they are at least arrays of 1 elements # (for aligning text labels, see below) hoffset = np.array(parse_distance(labels_h_offset, lats), copy=False, ndmin=1) voffset = np.array(parse_distance(labels_v_offset), copy=False, ndmin=1) lbl_lons = lons + hoffset lbl_lats = lats + voffset # convert labels coordinates: xlbl, ylbl = bmap(lbl_lons, lbl_lats) # plot point labels max_points = -1 # negative means: plot all if max_points < 0 or len(lons) < max_points: # Set alignments which control also the corner point reference when placing labels # from (FIXME: add ref?) # horizontalalignment controls whether the x positional argument for the text indicates # the left, center or right side of the text bounding box. # verticalalignment controls whether the y positional argument for the text indicates # the bottom, center or top side of the text bounding box. # multialignment, for newline separated strings only, controls whether the different lines # are left, center or right justified ha = 'left' if hoffset[0] > 0 else 'right' if hoffset[0] < 0 else 'center' va = 'bottom' if voffset[0] > 0 else 'top' if voffset[0] < 0 else 'center' ma = ha kwa = _joinargs("labels", kwargs, fontweight=fontweight, color=fontcolor, zorder=100, fontsize=fontsize, horizontalalignment=ha, verticalalignment=va, multialignment=ma) for name, xpt, ypt in zip(labels, xlbl, ylbl): # Check if the point can actually be seen with the current bmap # projection. The bmap object will set the coordinates to very # large values if it cannot project a point. if xpt > 1e25: continue map_ax.text(xpt, ypt, name, **kwa) # plot points x, y = bmap(lons, lats) # store handles to points, and relative labels, if any leg_handles, leg_labels = [], [] # bmap.scatter accepts all array-like args except markers. Avoid several useless loops # and do only those for distinct markers: # unique markers (sorted according to their index in markers, not their value): mrks = markers[np.sort(np.unique(markers, return_index=True)[1])] for mrk in mrks: # Note using masks with '==' (numpy==1.11.3): # # >>> a = np.array([1,2,3]) # >>> a == 3 # array([False, False, True], dtype=bool) # OK # >>> a == None # False # NOT AS EXPECTED! # >>> np.equal(a, None) # array([False, False, False], dtype=bool) # OK # # (Note also that a == None issues: # FutureWarning: comparison to `None` will result in an elementwise object # comparison in the future.) # # So the correct way is to write # mask = np.equal(array, val) if val is None else (a == val) m_mask = np.equal(markers, mrk) if mrk is None else markers == mrk # see above __x = x[m_mask] __y = y[m_mask] __m = mrk __s = sizes[m_mask] __c = colors[m_mask] __l = legendlabels[m_mask] # unique legends (sorted according to their index in __l, not their value): for leg in __l[np.sort(np.unique(__l, return_index=True)[1])]: l_mask = np.equal(__l, leg) if leg is None else __l == leg # see above _scatter = bmap.scatter(__x[l_mask], __y[l_mask], marker=mrk, s=__s[l_mask], c=__c[l_mask], cmap=cmap, zorder=10) if leg: leg_handles.append(_scatter) leg_labels.append(leg) if leg_handles: # if we provided `legend_loc`, use that: loc = kwargs.get('legend_loc', None) bbox_to_anchor = None # defaults in matplotlib legend # we do have legend to show. Adjust legend reference corner: if loc is None: if legend_pos == 'bottom': loc = 'upper center' bbox_to_anchor = (0.5, -0.05) elif legend_pos == 'top': loc = 'lower center' bbox_to_anchor = (0.5, 1.05) elif legend_pos == 'left': loc = 'center right' bbox_to_anchor = (-0.05, 0.5) elif legend_pos == 'right': loc = 'center left' bbox_to_anchor = (1, 0.5) else: raise ValueError('invalid legend_pos value:"%s"' % legend_pos) # The plt.legend has the prop argument which sets the font properties: # family, style, variant, weight, stretch, size, fname. See # http://matplotlib.org/api/font_manager_api.html#matplotlib.font_manager.FontProperties # However, that property does not allow to set font color. So we # use the get_text method of Legend. Note that we pass font size *now* even if # setting it later works as well (the legend frame is resized accordingly) kwa = _joinargs("legend", kwargs, scatterpoints=1, ncol=legend_ncol, loc=loc, bbox_to_anchor=bbox_to_anchor, borderaxespad=legend_borderaxespad, fontsize=fontsize) # http://stackoverflow.com/questions/17411940/matplotlib-scatter-plot-legend leg = map_ax.legend(leg_handles, leg_labels, **kwa) # set properties supplied via 'legend_' _setprop(leg.get_texts(), _joinargs("legendlabels", kwargs, color=fontcolor)) # re-position the axes. The REAL map aspect ratio seems to be this: realratio_h_w = bmap.aspect fig_w, fig_h = fig.get_size_inches() figratio_h_w = np.true_divide(fig_h, fig_w) if figratio_h_w >= realratio_h_w: # we have margins (blank space) above and below # thus, we assume: map_w = fig_w # and we calculate map_h map_h = map_w * realratio_h_w # assume there is the same amount of space above and below: vpad = (fig_h - map_h) / 2.0 # hpad is zero: hpad = 0 else: # we have margins (blank space) left and right # thus, we assume: map_h = fig_h # and consequently: map_w = map_h / realratio_h_w # assume there is the same amount of space above and below: hpad = (fig_w - map_w) / 2.0 # wpad is zero: vpad = 0 # calculate new fig dimensions EXACTLY as contour of the map new_fig_w = fig_w - 2 * hpad new_fig_h = fig_h - 2 * vpad # now margins: marginz = parse_margins(figmargins) # margins are in fontheight units. Get font height: fontsize_inch = 0 if len(np.nonzero(marginz)[0]): # Calculate the font size in pixels. # We want to be consistent with matplotlib way of getting fontsize. # inspecting matplotlib.legend.Legend.draw we end up with: # 1. Get the renderer rend = fig.canvas.get_renderer() # 2. get the fontsize in points. We might use `fontsize` but it might be None and we want # the default in case. There are several 'defaults' (rcParams['font.size'], # rcParams["legend.fontsize"])... we don't care for now, use the first. How to get # rcParams['font.size'] ? Either this: (see at matplotlib.Legend.__init__): # fontsize_pt = FontProperties(size=fontsize, weight=fontweight).get_size_in_points() # or simply do: fontsize_pt = fontsize or rcParams['font.size'] # Now use renderer to convert to pixels: # For info see matplotlib.text.Text.get_window_extent fontsize_px = rend.points_to_pixels(fontsize_pt) # finally inches: fontsize_inch = pix2inch(rend.points_to_pixels(fontsize_px), fig) # calculate insets in inches (top right bottom left) insets_inch = marginz * fontsize_inch # set to fig dimensions new_fig_w += insets_inch[1] + insets_inch[3] new_fig_h += insets_inch[0] + insets_inch[2] fig.set_size_inches(new_fig_w, new_fig_h, forward=True) # (forward necessary if fig is in GUI, let's set for safety) # now the axes which are relative to the figure. Thus first normalize inches: insets_inch /= [fig_h, fig_w, fig_h, fig_w] # pos1 = map_ax.get_position() # get the original position # NOTE: it seems that pos[0], pos[1] indicate the x and y of the LOWER LEFT corner, not # upper left! pos2 = [insets_inch[3], insets_inch[2], 1 - (insets_inch[1] + insets_inch[3]), 1 - (insets_inch[0] + insets_inch[2])] map_ax.set_position(pos2) if title: plt.suptitle(title) if show: plt.show() return fig
def createPlot(self, startDatetime, endDatetime): if len(self.data) == 0: logger.debug('no data found to plot') raise Exception('no data found to plot') # GridSpecs for plots outer_gs = gridspec.GridSpec(nrows=2, ncols=1, height_ratios=[1,3]) # tighten up space between plots outer_gs.update(left=0.10, right=0.90, hspace=0.05) map_gs = gridspec.GridSpecFromSubplotSpec(nrows=1, ncols=1, subplot_spec=outer_gs[0]) lower_gs = gridspec.GridSpecFromSubplotSpec(nrows=len(self.plotGroupValid), ncols=1, subplot_spec=outer_gs[1]) STATIC_ROOT = '/var/www/html/stoqs/static' # Warning: Hard-coded clt = self.readCLT(os.path.join(STATIC_ROOT, 'colormaps', 'jetplus.txt')) self.cm_jetplus = mpl.colors.ListedColormap(np.array(clt)) # start a new figure - size is in inches fig = plt.figure(figsize=(8, 10)) fig.suptitle(self.title+'\n'+self.subtitle1+'\n'+self.subtitle2, fontsize=8) pn = self.platformName[0] # bound the depth to cover max of all parameter group depths # and flag removal of scatter plot if have more than 2000 points in any parameter maxy = 0 for group in self.plotGroupValid: parm = [x.strip() for x in group.split(',')] for name in parm: y = max(self.data[pn+name]['depth']) sz = len(self.data[pn+name]['datavalue']) if y > maxy: maxy = y # pad the depth by 20 meters to make room for parameter name to be displayed at bottom rangey = [0.0, int(maxy) + 20] i = 0 # add contour plots for each parameter group for group in self.plotGroupValid: parm = [x.strip() for x in group.split(',')] plot_step = sum([self.data[pn+p]['units'].count('bool') for p in parm]) # counter the number of boolean plots in the groups plot_scatter = len(parm) - plot_step # otherwise all other plots are scatter plots #plot_dense = sum([val for val in len(self.data[pn+name]['datavalue']) > 2000]) # if more than 2000 points, skip the scatter plot # choose the right type of gridspec to display the data if plot_scatter: # one row for scatter and one for contour plot_gs = gridspec.GridSpecFromSubplotSpec(nrows=len(parm)*2, ncols=2, subplot_spec=lower_gs[i], width_ratios=[30,1], wspace=0.05) else: # one row for step plots/or contour plots plot_gs = gridspec.GridSpecFromSubplotSpec(nrows=len(parm), ncols=2, subplot_spec=lower_gs[i], width_ratios=[30,1], wspace=0.05) j = 0 i += 1 for name in parm: title = name x = [time.mktime(xe.timetuple()) for xe in self.data[pn+name]['datetime']] y = self.data[pn+name]['depth'] z = self.data[pn+name]['datavalue'] sdt_count = self.data[pn+name]['sdt_count'] units = '(' + self.data[pn+p]['units'] + ')' if len(z): if self.autoscale: rangez = [self.data[pn+p]['p010'],self.data[pn+p]['p990']] else: rangez = [min(z), max(z)] else: rangez = [0, 0] if name.find('chlorophyll') != -1 : if not self.autoscale: rangez = [0.0, 10.0] if name.find('salinity') != -1 : if not self.autoscale: rangez = [33.3, 34.9] units = '' if name.find('temperature') != -1 : if not self.autoscale: rangez = [10.0, 14.0] units = ' ($^\circ$C)' logger.debug('getting subplot ax0') gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=plot_gs[j]) ax0_plot = plt.Subplot(fig, gs[:]) fig.add_subplot(ax0_plot) gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=plot_gs[j+1]) ax0_colorbar = plt.Subplot(fig, gs[:]) fig.add_subplot(ax0_colorbar) if plot_scatter: logger.debug('getting subplot ax1') gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=plot_gs[j + 2]) ax1_plot = plt.Subplot(fig, gs[:]) fig.add_subplot(ax1_plot) gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=plot_gs[j + 3]) ax1_colorbar = plt.Subplot(fig, gs[:]) fig.add_subplot(ax1_colorbar) # if no data found add in some fake data and plot a placeholder time/depth plot if not x: tmin = time.mktime(startDatetime.timetuple()) tmax = time.mktime(endDatetime.timetuple()) x.append(tmin) x.append(tmax) y.append(np.NaN) y.append(np.NaN) z.append(np.NaN) z.append(np.NaN) if plot_scatter: cs0, zi = self.createContourPlot(title + pn,ax0_plot,x,y,z,rangey,rangez,startDatetime,endDatetime,sdt_count) cs1 = self.createScatterPlot(title + pn,ax1_plot,x,y,z,rangey,rangez,startDatetime,endDatetime) else: if plot_step: cs0 = self.createStepPlot(title + pn,title,ax0_plot,x,z,rangez,startDatetime,endDatetime) else: cs0, zi = self.createContourPlot(title + pn,ax0_plot,x,y,z,rangey,rangez,startDatetime,endDatetime,sdt_count) if plot_scatter: ax1_plot.text(0.95,0.02, name, verticalalignment='bottom', horizontalalignment='right',transform=ax1_plot.transAxes,color='black',fontsize=8) else: ax0_plot.text(0.95,0.02, name, verticalalignment='bottom', horizontalalignment='right',transform=ax0_plot.transAxes,color='black',fontsize=8) self.shadeNight(ax0_plot,sorted(x),rangey[0], rangey[1]) if plot_scatter: self.shadeNight(ax1_plot,sorted(x),rangey[0], rangey[1]) logger.debug('plotting colorbars') if plot_scatter: cbFormatter = FormatStrFormatter('%.2f') cb = plt.colorbar(cs0, cax=ax0_colorbar, ticks=[min(rangez), max(rangez)], format=cbFormatter, orientation='vertical') cb.set_label(units,fontsize=8)#,labelpad=5) cb.ax.xaxis.set_ticks_position('top') for t in cb.ax.yaxis.get_ticklabels(): t.set_fontsize(8) cb = plt.colorbar(cs1, cax=ax1_colorbar, ticks=[min(rangez), max(rangez)], format=cbFormatter, orientation='vertical') cb.set_label(units,fontsize=8)#,labelpad=5) cb.ax.xaxis.set_ticks_position('top') for t in cb.ax.yaxis.get_ticklabels(): t.set_fontsize(8) else: if plot_step: xaxis = ax0_colorbar.xaxis ax0_colorbar.xaxis.set_major_locator(plt.NullLocator()) ax0_colorbar.yaxis.set_ticks_position('right') for t in ax0_colorbar.yaxis.get_ticklabels(): t.set_fontsize(8) else: cbFormatter = FormatStrFormatter('%.2f') cb = plt.colorbar(cs0, cax=ax0_colorbar, ticks=[min(rangez), max(rangez)], format=cbFormatter, orientation='vertical') cb.set_label(units,fontsize=8)#,labelpad=5) cb.ax.xaxis.set_ticks_position('top') for t in cb.ax.yaxis.get_ticklabels(): t.set_fontsize(8) # Rotate and show the date with date formatter in the last plot of all the groups logger.debug('rotate and show date with date formatter') if name is parm[-1] and group is self.plotGroupValid[-1]: x_fmt = self.DateFormatter(self.scale_factor) if plot_scatter: ax = ax1_plot # Don't show on the upper contour plot ax0_plot.xaxis.set_ticks([]) else: ax = ax0_plot ax.xaxis.set_major_formatter(x_fmt) # Rotate date labels for label in ax.xaxis.get_ticklabels(): label.set_rotation(10) else: ax0_plot.xaxis.set_ticks([]) if plot_scatter: ax1_plot.xaxis.set_ticks([]) if plot_scatter: j+=4 else: j+=2 # plot tracks ax = plt.Subplot(fig, map_gs[:]) fig.add_subplot(ax, aspect='equal') logger.debug('computing activity extents') z = [] maptracks = None z, points, maptracks = self.getMeasuredPPData(startDatetime, endDatetime, pn, self.plotDotParmName) # get the percentile ranges for this to autoscale pointsnp = np.array(points) lon = pointsnp[:,0] lat = pointsnp[:,1] if self.extent: ltmin = self.extent[1] ltmax = self.extent[3] lnmin = self.extent[0] lnmax = self.extent[2] lndiff = abs(lnmax - lnmin) ltdiff = abs(ltmax - ltmin) logger.debug("lon diff %f lat diff %f" %(lndiff, ltdiff)) mindeg = .02 paddeg = .01 if lndiff < mindeg : lnmin -= mindeg lnmax += mindeg if ltdiff < mindeg: ltmin -= mindeg ltmax += mindeg e = (lnmin - paddeg, ltmin - paddeg, lnmax + paddeg, ltmax + paddeg) else: # default map to Monterey Bay region ltmin = 36.61 ltmax = 36.97 lnmin = -122.21 lnmax = -121.73 e = (lnmin, ltmin, lnmax, ltmax) logger.debug('Extent found %f,%f,%f,%f)' % (e[0], e[1],e[2],e[3])) # retry up to 5 times to get the basemap for i in range(0, 5): mp = Basemap(llcrnrlon=e[0], llcrnrlat=e[1], urcrnrlon=e[2], urcrnrlat=e[3], projection='cyl', resolution='l', ax=ax) try: ##mp.wmsimage('http://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?', layers=['GEBCO_08_Grid']) # Works, but coarse mp.arcgisimage(server='http://services.arcgisonline.com/ArcGIS', service='Ocean_Basemap') mp.drawparallels(np.linspace(e[1],e[3],num=3), labels=[True,False,False,False], fontsize=8, linewidth=0) mp.drawmeridians(np.linspace(e[0],e[2],num=3), labels=[False,False,False,True], fontsize=8, linewidth=0) except Exception, e: logger.error('Could not download ocean basemap ') mp = None if mp is not None : break
def createPNG(self, fileName=None, forGeotiff=False): """Draw processed data on a map and save it as a .png file """ if not forGeotiff: fig = plt.figure(figsize=(18, 12)) ax = plt.axes() else: fig = plt.figure() ax = fig.add_axes((0, 0, 1, 1)) if not fileName: fileName = self.args.pngFileName e = self.getExtent() m = Basemap( llcrnrlon=e[0], llcrnrlat=e[1], urcrnrlon=e[2], urcrnrlat=e[3], projection="cyl", resolution="l", ax=ax ) if not forGeotiff: m.arcgisimage(server="http://services.arcgisonline.com/ArcGIS", service="Ocean_Basemap") for depth, drift in self.adcpDrift.iteritems(): m.plot(drift["lon"], drift["lat"], "-", c="black", linewidth=1) plt.text(drift["lon"][-1], drift["lat"][-1], "%i m" % depth, size="small") for platform, drift in self.trackDrift.iteritems(): # Ad hoc coloring of platforms... if platform.startswith("stella"): color = "yellow" elif platform.startswith("daphne"): color = "orange" elif platform.startswith("makai"): color = "magenta" else: color = "red" m.plot(drift["lon"], drift["lat"], "-", c=color, linewidth=2) plt.text(drift["lon"][-1], drift["lat"][-1], platform, size="small") # Plot each data point with it's own color based on the activity statistics from STOQS coloredDotSize = 30 STATIC_ROOT = "/var/www/html/stoqs/static" # Warning: Hard-coded clt = readCLT(os.path.join(STATIC_ROOT, "colormaps", "jetplus.txt")) cm_jetplus = matplotlib.colors.ListedColormap(np.array(clt)) for key, drift in self.stoqsDrift.iteritems(): min, max = key.split(",")[2:4] ax.scatter( drift["lon"], drift["lat"], c=drift["datavalue"], s=coloredDotSize, cmap=cm_jetplus, lw=0, vmin=min, vmax=max, ) label = "%s from %s" % tuple(key.split(",")[:2]) plt.text(drift["lon"][-1], drift["lat"][-1], label, size="small") nowLocal = str(pytz.utc.localize(datetime.now()).astimezone(pytz.timezone("America/Los_Angeles"))).split(".")[0] plt.text( 0.99, 0.01, "Created: " + nowLocal + " Local", horizontalalignment="right", verticalalignment="bottom", transform=ax.transAxes, ) if not forGeotiff: m.drawparallels(np.linspace(e[1], e[3], num=3), labels=[True, False, False, False], linewidth=0) m.drawmeridians(np.linspace(e[0], e[2], num=3), labels=[False, False, False, True], linewidth=0) try: plt.title(self.title) except AttributeError: pass fig.savefig(fileName) print "Wrote file", self.args.pngFileName else: plt.axis("off") try: plt.text( 0.5, 0.95, self.title, horizontalalignment="center", verticalalignment="top", transform=ax.transAxes ) except AttributeError: pass fig.savefig(fileName, transparent=True, dpi=300, bbox_inches="tight", pad_inches=0) plt.clf() plt.close()
def map_texture(humfile, sonpath, cs2cs_args, dogrid, calc_bearing, filt_bearing, res, cog, dowrite): ''' Create plots of the texture lengthscale maps made in PyHum.texture module using the algorithm detailed by Buscombe et al. (forthcoming) This textural lengthscale is not a direct measure of grain size. Rather, it is a statistical representation that integrates over many attributes of bed texture, of which grain size is the most important. The technique is a physically based means to identify regions of texture within a sidescan echogram, and could provide a basis for objective, automated riverbed sediment classification. Syntax ---------- [] = PyHum.map_texture(humfile, sonpath, cs2cs_args, dogrid, calc_bearing, filt_bearing, res, cog, dowrite) Parameters ---------- humfile : str path to the .DAT file sonpath : str path where the *.SON files are cs2cs_args : int, *optional* [Default="epsg:26949"] arguments to create coordinates in a projected coordinate system this argument gets given to pyproj to turn wgs84 (lat/lon) coordinates into any projection supported by the proj.4 libraries dogrid : float, *optional* [Default=1] if 1, textures will be gridded with resolution 'res'. Otherwise, point cloud will be plotted calc_bearing : float, *optional* [Default=0] if 1, bearing will be calculated from coordinates filt_bearing : float, *optional* [Default=0] if 1, bearing will be filtered res : float, *optional* [Default=0.1] grid resolution of output gridded texture map cog : int, *optional* [Default=1] if 1, heading calculated assuming GPS course-over-ground rather than using a compass dowrite: int, *optional* [Default=1] if 1, point cloud data from each chunk is written to ascii file if 0, processing times are speeded up considerably but point clouds are not available for further analysis Returns ------- sonpath+'x_y_class'+str(p)+'.asc' : text file contains the point cloud of easting, northing, and texture lengthscales of the pth chunk sonpath+'class_GroundOverlay'+str(p)+'.kml': kml file contains gridded (or point cloud) texture lengthscale map for importing into google earth of the pth chunk sonpath+'class_map'+str(p)+'.png' : image overlay associated with the kml file sonpath+'class_map_imagery'+str(p)+'.png' : png image file gridded (or point cloud) texture lengthscale map overlain onto an image pulled from esri image server References ---------- .. [1] Buscombe, D., Grams, P.E., and Smith, S.M.C., Automated riverbed sediment classification using low-cost sidescan sonar. submitted to Journal of Hydraulic Engineering ''' # prompt user to supply file if no input file given if not humfile: print 'An input file is required!!!!!!' Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing inputfile = askopenfilename(filetypes=[("DAT files","*.DAT")]) # prompt user to supply directory if no input sonpath is given if not sonpath: print 'A *.SON directory is required!!!!!!' Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing sonpath = askdirectory() # print given arguments to screen and convert data type where necessary if humfile: print 'Input file is %s' % (humfile) if sonpath: print 'Sonar file path is %s' % (sonpath) if cs2cs_args: print 'cs2cs arguments are %s' % (cs2cs_args) if dogrid: dogrid = int(dogrid) if dogrid==1: print "Data will be gridded" if calc_bearing: calc_bearing = int(calc_bearing) if calc_bearing==1: print "Bearing will be calculated from coordinates" if filt_bearing: filt_bearing = int(filt_bearing) if filt_bearing==1: print "Bearing will be filtered" if res: res = np.asarray(res,float) print 'Gridding resolution: %s' % (str(res)) if cog: cog = int(cog) if cog==1: print "Heading based on course-over-ground" if dowrite: dowrite = int(dowrite) if dowrite==0: print "Point cloud data will be written to ascii file" if not cs2cs_args: # arguments to pass to cs2cs for coordinate transforms cs2cs_args = "epsg:26949" print '[Default] cs2cs arguments are %s' % (cs2cs_args) if not dogrid: if dogrid != 0: dogrid = 1 print "[Default] Data will be gridded" if not calc_bearing: if calc_bearing != 1: calc_bearing = 0 print "[Default] Heading recorded by instrument will be used" if not filt_bearing: if filt_bearing != 1: filt_bearing = 0 print "[Default] Heading will not be filtered" if not res: res = 0.5 print '[Default] Grid resolution is %s m' % (str(res)) if not cog: if cog != 0: cog = 1 print "[Default] Heading based on course-over-ground" if not dowrite: if dowrite != 0: dowrite = 1 print "[Default] Point cloud data will be written to ascii file" trans = pyproj.Proj(init=cs2cs_args) # if son path name supplied has no separator at end, put one on if sonpath[-1]!=os.sep: sonpath = sonpath + os.sep base = humfile.split('.DAT') # get base of file name for output base = base[0].split(os.sep)[-1] # remove underscores, negatives and spaces from basename if base.find('_')>-1: base = base[:base.find('_')] if base.find('-')>-1: base = base[:base.find('-')] if base.find(' ')>-1: base = base[:base.find(' ')] esi = np.squeeze(loadmat(sonpath+base+'meta.mat')['e']) nsi = np.squeeze(loadmat(sonpath+base+'meta.mat')['n']) pix_m = np.squeeze(loadmat(sonpath+base+'meta.mat')['pix_m']) dep_m = np.squeeze(loadmat(sonpath+base+'meta.mat')['dep_m']) c = np.squeeze(loadmat(sonpath+base+'meta.mat')['c']) dist_m = np.squeeze(loadmat(sonpath+base+'meta.mat')['dist_m']) # over-ride measured bearing and calc from positions if calc_bearing==1: lat = np.squeeze(loadmat(sonpath+base+'meta.mat')['lat']) lon = np.squeeze(loadmat(sonpath+base+'meta.mat')['lon']) #point-to-point bearing bearing = np.zeros(len(lat)) for k in xrange(len(lat)-1): bearing[k] = bearingBetweenPoints(lat[k], lat[k+1], lon[k], lon[k+1]) del lat, lon else: # reported bearing by instrument (Kalman filtered?) bearing = np.squeeze(loadmat(sonpath+base+'meta.mat')['heading']) ## bearing can only be observed modulo 2*pi, therefore phase unwrap #bearing = np.unwrap(bearing) # if stdev in heading is large, there's probably noise that needs to be filtered out if np.std(bearing)>180: print "WARNING: large heading stdev - attempting filtering" from sklearn.cluster import MiniBatchKMeans # can have two modes data = np.column_stack([bearing, bearing]) k_means = MiniBatchKMeans(2) # fit the model k_means.fit(data) values = k_means.cluster_centers_.squeeze() labels = k_means.labels_ if np.sum(labels==0) > np.sum(labels==1): bearing[labels==1] = np.nan else: bearing[labels==0] = np.nan nans, y= humutils.nan_helper(bearing) bearing[nans]= np.interp(y(nans), y(~nans), bearing[~nans]) if filt_bearing ==1: bearing = humutils.runningMeanFast(bearing, len(bearing)/100) theta = np.asarray(bearing, 'float')/(180/np.pi) # this is standard course over ground if cog==1: #course over ground is given as a compass heading (ENU) from True north, or Magnetic north. #To get this into NED (North-East-Down) coordinates, you need to rotate the ENU # (East-North-Up) coordinate frame. #Subtract pi/2 from your heading theta = theta - np.pi/2 # (re-wrap to Pi to -Pi) theta = np.unwrap(-theta) # load memory mapped scans shape_port = np.squeeze(loadmat(sonpath+base+'meta.mat')['shape_port']) if shape_port!='': port_fp = np.memmap(sonpath+base+'_data_port_l.dat', dtype='float32', mode='r', shape=tuple(shape_port)) shape_star = np.squeeze(loadmat(sonpath+base+'meta.mat')['shape_star']) if shape_star!='': star_fp = np.memmap(sonpath+base+'_data_star_l.dat', dtype='float32', mode='r', shape=tuple(shape_star)) shape = shape_port.copy() shape[1] = shape_port[1] + shape_star[1] class_fp = np.memmap(sonpath+base+'_data_class.dat', dtype='float32', mode='r', shape=tuple(shape)) tvg = ((8.5*10**-5)+(3/76923)+((8.5*10**-5)/4))*c dist_tvg = ((np.tan(np.radians(25)))*dep_m)-(tvg) for p in xrange(len(class_fp)): e = esi[shape_port[-1]*p:shape_port[-1]*(p+1)] n = nsi[shape_port[-1]*p:shape_port[-1]*(p+1)] t = theta[shape_port[-1]*p:shape_port[-1]*(p+1)] d = dist_tvg[shape_port[-1]*p:shape_port[-1]*(p+1)] len_n = len(n) merge = class_fp[p].copy() merge[np.isnan(merge)] = 0 merge[np.isnan(np.vstack((np.flipud(port_fp[p]),star_fp[p])))] = 0 extent = shape_port[1] R1 = merge[extent:,:] R2 = np.flipud(merge[:extent,:]) merge = np.vstack((R2,R1)) del R1, R2 # get number pixels in scan line extent = int(np.shape(merge)[0]/2) yvec = np.linspace(pix_m,extent*pix_m,extent) print "getting point cloud ..." # get the points by rotating the [x,y] vector so it lines up with boat heading X=[]; Y=[]; for k in range(len(n)): x = np.concatenate((np.tile(e[k],extent) , np.tile(e[k],extent))) #y = np.concatenate((n[k]+yvec, n[k]-yvec)) rangedist = np.sqrt(np.power(yvec, 2.0) - np.power(d[k], 2.0)) y = np.concatenate((n[k]+rangedist, n[k]-rangedist)) # Rotate line around center point xx = e[k] - ((x - e[k]) * np.cos(t[k])) - ((y - n[k]) * np.sin(t[k])) yy = n[k] - ((x - e[k]) * np.sin(t[k])) + ((y - n[k]) * np.cos(t[k])) xx, yy = calc_beam_pos(d[k], t[k], xx, yy) X.append(xx) Y.append(yy) del e, n, t, x, y # merge flatten and stack X = np.asarray(X,'float') X = X.flatten() # merge flatten and stack Y = np.asarray(Y,'float') Y = Y.flatten() merge[merge==0] = np.nan if len(merge.flatten()) != len(X): merge = merge[:,:len_n] merge = merge.T.flatten() index = np.where(np.logical_not(np.isnan(merge)))[0] X = X.flatten()[index] Y = Y.flatten()[index] merge = merge.flatten()[index] X = X[np.where(np.logical_not(np.isnan(Y)))] merge = merge.flatten()[np.where(np.logical_not(np.isnan(Y)))] Y = Y[np.where(np.logical_not(np.isnan(Y)))] Y = Y[np.where(np.logical_not(np.isnan(X)))] merge = merge.flatten()[np.where(np.logical_not(np.isnan(X)))] X = X[np.where(np.logical_not(np.isnan(X)))] X = X[np.where(np.logical_not(np.isnan(merge)))] Y = Y[np.where(np.logical_not(np.isnan(merge)))] merge = merge[np.where(np.logical_not(np.isnan(merge)))] if dowrite==1: # write raw bs to file outfile = sonpath+'x_y_class'+str(p)+'.asc' with open(outfile, 'w') as f: np.savetxt(f, np.hstack((humutils.ascol(X),humutils.ascol(Y), humutils.ascol(merge))), delimiter=' ', fmt="%8.6f %8.6f %8.6f") humlon, humlat = trans(X, Y, inverse=True) if dogrid==1: grid_x, grid_y = np.meshgrid( np.arange(np.min(X), np.max(X), res), np.arange(np.min(Y), np.max(Y), res) ) dat = griddata(np.c_[X.flatten(),Y.flatten()], merge.flatten(), (grid_x, grid_y), method='nearest') ## create mask for where the data is not tree = KDTree(np.c_[X.flatten(),Y.flatten()]) dist, _ = tree.query(np.c_[grid_x.ravel(), grid_y.ravel()], k=1) dist = dist.reshape(grid_x.shape) del X, Y if dogrid==1: ## mask dat[dist> 1 ] = np.nan del dist, tree dat[dat==0] = np.nan dat[np.isinf(dat)] = np.nan datm = np.ma.masked_invalid(dat) glon, glat = trans(grid_x, grid_y, inverse=True) del grid_x, grid_y try: print "drawing and printing map ..." fig = plt.figure(frameon=False) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], #26949, resolution = 'i', #h #f llcrnrlon=np.min(humlon)-0.001, llcrnrlat=np.min(humlat)-0.001, urcrnrlon=np.max(humlon)+0.001, urcrnrlat=np.max(humlat)+0.001) if dogrid==1: gx,gy = map.projtran(glon, glat) ax = plt.Axes(fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) if dogrid==1: map.pcolormesh(gx, gy, datm, cmap='YlOrRd', vmin=0.5, vmax=2) del dat else: ## draw point cloud x,y = map.projtran(humlon, humlat) map.scatter(x.flatten(), y.flatten(), 0.5, merge.flatten(), cmap='YlOrRd', linewidth = '0') custom_save(sonpath,'class_map'+str(p)) del fig except: print "error: map could not be created..." kml = simplekml.Kml() ground = kml.newgroundoverlay(name='GroundOverlay') ground.icon.href = 'class_map'+str(p)+'.png' ground.latlonbox.north = np.min(humlat)-0.001 ground.latlonbox.south = np.max(humlat)+0.001 ground.latlonbox.east = np.max(humlon)+0.001 ground.latlonbox.west = np.min(humlon)-0.001 ground.latlonbox.rotation = 0 kml.save(sonpath+'class_GroundOverlay'+str(p)+'.kml') if dowrite==1: X = []; Y = []; S = []; for p in xrange(len(class_fp)): dat = np.genfromtxt(sonpath+'x_y_class'+str(p)+'.asc', delimiter=' ') X.append(dat[:,0]) Y.append(dat[:,1]) S.append(dat[:,2]) del dat # merge flatten and stack X = np.asarray(np.hstack(X),'float') X = X.flatten() # merge flatten and stack Y = np.asarray(np.hstack(Y),'float') Y = Y.flatten() # merge flatten and stack S = np.asarray(np.hstack(S),'float') S = S.flatten() humlon, humlat = trans(X, Y, inverse=True) if dogrid==1: grid_x, grid_y = np.meshgrid( np.arange(np.min(X), np.max(X), res), np.arange(np.min(Y), np.max(Y), res) ) dat = griddata(np.c_[X.flatten(),Y.flatten()], S.flatten(), (grid_x, grid_y), method='nearest') ## create mask for where the data is not tree = KDTree(np.c_[X.flatten(),Y.flatten()]) dist, _ = tree.query(np.c_[grid_x.ravel(), grid_y.ravel()], k=1) dist = dist.reshape(grid_x.shape) del X, Y if dogrid==1: ## mask dat[dist> 1 ] = np.nan del dist, tree dat[dat==0] = np.nan dat[np.isinf(dat)] = np.nan datm = np.ma.masked_invalid(dat) glon, glat = trans(grid_x, grid_y, inverse=True) del grid_x, grid_y levels = [0.5,0.75,1.25,1.5,1.75,2,3] try: print "drawing and printing map ..." fig = plt.figure() map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], resolution = 'i', llcrnrlon=np.min(humlon)-0.001, llcrnrlat=np.min(humlat)-0.001, urcrnrlon=np.max(humlon)+0.001, urcrnrlat=np.max(humlat)+0.001) map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=300) if dogrid==1: gx,gy = map.projtran(glon, glat) if dogrid==1: map.contourf(gx, gy, datm, levels, cmap='YlOrRd') else: ## draw point cloud x,y = map.projtran(humlon, humlat) map.scatter(x.flatten(), y.flatten(), 0.5, S.flatten(), cmap='YlOrRd', linewidth = '0') custom_save2(sonpath,'class_map_imagery'+str(p)) del fig except: print "error: map could not be created..."
# Generated: 2/14/2016 # Python 2.7 # Description: basemap plot of basin shapefiles from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import os os.chdir("../..") maindir = os.getcwd() ################### user input ######################### shp_file = maindir + os.sep + 'GIS\SERFC\SERFC_FY2016\Shapefiles_fromSERFC\calb_basins\calb_basins' out_map = maindir + os.sep + 'GIS\\SERFC\\SERFC_FY2016\\map_output\\basemap_plots' ################# end user input ######################### fig = plt.figure() m = Basemap(llcrnrlon=-86.0,llcrnrlat=30.3,urcrnrlon=-80.5,urcrnrlat=35.5, resolution='i', projection='cyl') m.arcgisimage(service='World_Shaded_Relief', xpixels = 1500, verbose= True) m.drawmapboundary(fill_color='aqua') #m.fillcontinents(color='#ddaa66',lake_color='aqua') m.drawcoastlines() m.drawstates(color='1.0', linewidth=1.0) m.drawrivers(color='#0000ff') m.readshapefile(shp_file, 'SERFC _basins') plt.savefig(out_map + os.sep + 'SERFC_basins.png', dpi=200, bbox_inches='tight') plt.show() print 'Map completed!!'
def mosaic_texture(humfile, sonpath, cs2cs_args = "epsg:26949", res = 99, nn = 5, weight = 1): ''' Create mosaics of the spatially referenced sidescan echograms Syntax ---------- [] = PyHum.mosaic_texture(humfile, sonpath, cs2cs_args, res, nn, weight) Parameters ---------- humfile : str path to the .DAT file sonpath : str path where the *.SON files are cs2cs_args : int, *optional* [Default="epsg:26949"] arguments to create coordinates in a projected coordinate system this argument gets given to pyproj to turn wgs84 (lat/lon) coordinates into any projection supported by the proj.4 libraries res : float, *optional* [Default=0] grid resolution of output gridded texture map if res=99, res will be determined automatically from the spatial resolution of 1 pixel nn: int, *optional* [Default=5] number of nearest neighbours for gridding weight: int, *optional* [Default=1] specifies the type of pixel weighting in the gridding process weight = 1, based on grazing angle and inverse distance weighting weight = 2, based on grazing angle only weight = 3, inverse distance weighting only weight = 4, no weighting Returns ------- sonpath+'GroundOverlay.kml': kml file contains gridded (or point cloud) sidescan intensity map for importing into google earth of the pth chunk sonpath+'map.png' : image overlay associated with the kml file ''' # prompt user to supply file if no input file given if not humfile: print('An input file is required!!!!!!') Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing humfile = askopenfilename(filetypes=[("DAT files","*.DAT")]) # prompt user to supply directory if no input sonpath is given if not sonpath: print('A *.SON directory is required!!!!!!') Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing sonpath = askdirectory() # print given arguments to screen and convert data type where necessary if humfile: print('Input file is %s' % (humfile)) if sonpath: print('Sonar file path is %s' % (sonpath)) if cs2cs_args: print('cs2cs arguments are %s' % (cs2cs_args)) if res: res = np.asarray(res,float) print('Gridding resolution: %s' % (str(res))) if nn: nn = int(nn) print('Number of nearest neighbours for gridding: %s' % (str(nn))) if weight: weight = int(weight) print('Weighting for gridding: %s' % (str(weight))) ##nn = 5 #number of nearest neighbours in gridding ##noisefloor=10 # noise threshold in dB W # start timer if os.name=='posix': # true if linux/mac or cygwin on windows start = time.time() else: # windows start = time.clock() trans = pyproj.Proj(init=cs2cs_args) # if son path name supplied has no separator at end, put one on if sonpath[-1]!=os.sep: sonpath = sonpath + os.sep base = humfile.split('.DAT') # get base of file name for output base = base[0].split(os.sep)[-1] # remove underscores, negatives and spaces from basename base = humutils.strip_base(base) meta = loadmat(os.path.normpath(os.path.join(sonpath,base+'meta.mat'))) esi = np.squeeze(meta['e']) nsi = np.squeeze(meta['n']) theta = np.squeeze(meta['heading'])/(180/np.pi) # load memory mapped scans shape_port = np.squeeze(meta['shape_port']) if shape_port!='': if os.path.isfile(os.path.normpath(os.path.join(sonpath,base+'_data_port_lar.dat'))): port_fp = io.get_mmap_data(sonpath, base, '_data_port_lar.dat', 'float32', tuple(shape_port)) else: port_fp = io.get_mmap_data(sonpath, base, '_data_port_la.dat', 'float32', tuple(shape_port)) shape_star = np.squeeze(meta['shape_star']) if shape_star!='': if os.path.isfile(os.path.normpath(os.path.join(sonpath,base+'_data_star_lar.dat'))): star_fp = io.get_mmap_data(sonpath, base, '_data_star_lar.dat', 'float32', tuple(shape_star)) else: star_fp = io.get_mmap_data(sonpath, base, '_data_star_la.dat', 'float32', tuple(shape_star)) # time varying gain tvg = ((8.5*10**-5)+(3/76923)+((8.5*10**-5)/4))*meta['c'] # depth correction dist_tvg = np.squeeze(((np.tan(np.radians(25)))*np.squeeze(meta['dep_m']))-(tvg)) # read in range data R_fp = io.get_mmap_data(sonpath, base, '_data_range.dat', 'float32', tuple(shape_star)) dx = np.arcsin(meta['c']/(1000*meta['t']*meta['f'])) pix_m = meta['pix_m'] c = meta['c'] if not os.path.isfile( os.path.normpath(os.path.join(sonpath,base+"S.p")) ): #if 2 > 1: inputfiles = [] if len(shape_star)>2: for p in range(len(star_fp)): e = esi[shape_port[-1]*p:shape_port[-1]*(p+1)] n = nsi[shape_port[-1]*p:shape_port[-1]*(p+1)] t = theta[shape_port[-1]*p:shape_port[-1]*(p+1)] d = dist_tvg[shape_port[-1]*p:shape_port[-1]*(p+1)] dat_port = port_fp[p] dat_star = star_fp[p] data_R = R_fp[p] print("writing chunk %s " % (str(p))) write_points(e, n, t, d, dat_port, dat_star, data_R, pix_m, res, cs2cs_args, sonpath, p, c, dx) inputfiles.append(os.path.normpath(os.path.join(sonpath,'x_y_class'+str(p)+'.asc'))) else: p=0 print("writing chunk %s " % (str(p))) write_points(esi, nsi, theta, dist_tvg, port_fp, star_fp, R_fp, meta['pix_m'], res, cs2cs_args, sonpath, 0, c, dx) inputfiles.append(os.path.normpath(os.path.join(sonpath,'x_y_class'+str(p)+'.asc'))) #trans = pyproj.Proj(init=cs2cs_args) # D, R, h, t print("reading points from %s files" % (str(len(inputfiles)))) X,Y,S,D,R,h,t,i = getxys(inputfiles) print("%s points read from %s files" % (str(len(S)), str(len(inputfiles)))) # remove values where sidescan intensity is zero ind = np.where(np.logical_not(S==0))[0] X = X[ind]; Y = Y[ind] S = S[ind]; D = D[ind] R = R[ind]; h = h[ind] t = t[ind]; i = i[ind] del ind # save to file for temporary storage pickle.dump( S, open( os.path.normpath(os.path.join(sonpath,base+"S.p")), "wb" ) ); del S pickle.dump( D, open( os.path.normpath(os.path.join(sonpath,base+"D.p")), "wb" ) ); del D pickle.dump( t, open( os.path.normpath(os.path.join(sonpath,base+"t.p")), "wb" ) ); del t pickle.dump( i, open( os.path.normpath(os.path.join(sonpath,base+"i.p")), "wb" ) ); del i pickle.dump( X, open( os.path.normpath(os.path.join(sonpath,base+"X.p")), "wb" ) ); del X pickle.dump( Y, open( os.path.normpath(os.path.join(sonpath,base+"Y.p")), "wb" ) ); del Y pickle.dump( R, open( os.path.normpath(os.path.join(sonpath,base+"R.p")), "wb" ) ); pickle.dump( h, open( os.path.normpath(os.path.join(sonpath,base+"h.p")), "wb" ) ); #grazing angle g = np.arctan(R.flatten(),h.flatten()) pickle.dump( g, open( os.path.normpath(os.path.join(sonpath,base+"g.p")), "wb" ) ); del g, R, h print("creating grids ...") if res==0: res=99 if res==99: #### prepare grids R = pickle.load( open( os.path.normpath(os.path.join(sonpath,base+"R.p")), "rb" ) ) ## actual along-track resolution is this: dx times dy = Af tmp = R * dx * (c*0.007 / 2) del R resg = np.min(tmp[tmp>0]) del tmp else: resg = res X = pickle.load( open( os.path.normpath(os.path.join(sonpath,base+"X.p")), "rb" ) ) Y = pickle.load( open( os.path.normpath(os.path.join(sonpath,base+"Y.p")), "rb" ) ) humlon, humlat = trans(X, Y, inverse=True) grid_x, grid_y = np.meshgrid( np.arange(np.min(X), np.max(X), resg), np.arange(np.min(Y), np.max(Y), resg) ) shape = np.shape(grid_x) tree = KDTree(zip(X.flatten(), Y.flatten())) del X, Y print("mosaicking ...") #k nearest neighbour try: dist, inds = tree.query(zip(grid_x.flatten(), grid_y.flatten()), k = nn, n_jobs=-1) except: #print ".... update your scipy installation to use faster kd-tree" dist, inds = tree.query(zip(grid_x.flatten(), grid_y.flatten()), k = nn) #del grid_x, grid_y if weight==1: g = pickle.load( open( os.path.normpath(os.path.join(sonpath,base+"g.p")), "rb" ) ) w = g[inds] + 1.0 / dist**2 del g elif weight==2: g = pickle.load( open( os.path.normpath(os.path.join(sonpath,base+"g.p")), "rb" ) ) w = g[inds] del g elif weight==3: w = 1.0 / dist**2 elif weight==4: w = 1.0 #g = pickle.load( open( os.path.normpath(os.path.join(sonpath,base+"g.p")), "rb" ) ) #w = g[inds] + 1.0 / dist**2 #del g if weight < 4: w[np.isinf(w)]=1 w[np.isnan(w)]=1 w[w>10000]=10000 w[w<=0]=1 # load in sidescan intensity S = pickle.load( open( os.path.normpath(os.path.join(sonpath,base+"S.p")), "rb" ) ) # filter out noise pixels S[S<noisefloor] = np.nan if nn==1: Sdat_g = (w * S.flatten()[inds]).reshape(shape) del w dist = dist.reshape(shape) else: if weight < 4: Sdat_g = (np.nansum(w * S.flatten()[inds], axis=1) / np.nansum(w, axis=1)).reshape(shape) else: Sdat_g = (np.nansum(S.flatten()[inds], axis=1)).reshape(shape) del w dist = np.nanmean(dist,axis=1).reshape(shape) del S Sdat_g[dist>1] = np.nan Sdat_g[Sdat_g<noisefloor] = np.nan dat = Sdat_g.copy() dat[dist>1] = 0 dat2 = replace_nans.RN(dat.astype('float64'),1000,0.01,2,'localmean').getdata() dat2[dat==0] = np.nan del dat dat2[dat2<noisefloor] = np.nan Sdat_g = dat2.copy() del dat2 Sdat_g[Sdat_g==0] = np.nan Sdat_g[np.isinf(Sdat_g)] = np.nan Sdat_gm = np.ma.masked_invalid(Sdat_g) del Sdat_g glon, glat = trans(grid_x, grid_y, inverse=True) del grid_x, grid_y # ========================================================= print("creating kmz file ...") ## new way to create kml file pixels = 1024 * 10 fig, ax = humutils.gearth_fig(llcrnrlon=glon.min(), llcrnrlat=glat.min(), urcrnrlon=glon.max(), urcrnrlat=glat.max(), pixels=pixels) cs = ax.pcolormesh(glon, glat, Sdat_gm) ax.set_axis_off() fig.savefig(os.path.normpath(os.path.join(sonpath,'class_overlay1.png')), transparent=True, format='png') fig = plt.figure(figsize=(1.0, 4.0), facecolor=None, frameon=False) ax = fig.add_axes([0.0, 0.05, 0.2, 0.9]) cb = fig.colorbar(cs, cax=ax) cb.set_label('Texture lengthscale [m]', rotation=-90, color='k', labelpad=20) fig.savefig(os.path.normpath(os.path.join(sonpath,'class_legend.png')), transparent=False, format='png') humutils.make_kml(llcrnrlon=glon.min(), llcrnrlat=glat.min(), urcrnrlon=glon.max(), urcrnrlat=glat.max(), figs=[os.path.normpath(os.path.join(sonpath,'class_overlay1.png'))], colorbar=os.path.normpath(os.path.join(sonpath,'class_legend.png')), kmzfile=os.path.normpath(os.path.join(sonpath,'class_GroundOverlay.kmz')), name='Sidescan Intensity') # ========================================================= print("drawing and printing map ...") fig = plt.figure(frameon=False) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], resolution = 'i', #h #f llcrnrlon=np.min(humlon)-0.001, llcrnrlat=np.min(humlat)-0.001, urcrnrlon=np.max(humlon)+0.001, urcrnrlat=np.max(humlat)+0.001) gx,gy = map.projtran(glon, glat) try: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='ESRI_Imagery_World_2D', xpixels=1000, ypixels=None, dpi=300) except: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=300) #finally: # print "error: map could not be created..." ax = plt.Axes(fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) if Sdat_gm.size > 25000000: print("matrix size > 25,000,000 - decimating by factor of 5 for display") map.pcolormesh(gx[::5,::5], gy[::5,::5], Sdat_gm[::5,::5], vmin=np.nanmin(Sdat_gm), vmax=np.nanmax(Sdat_gm)) else: map.pcolormesh(gx, gy, Sdat_gm, vmin=np.nanmin(Sdat_gm), vmax=np.nanmax(Sdat_gm)) custom_save2(sonpath,'class_map_imagery') del fig if os.name=='posix': # true if linux/mac elapsed = (time.time() - start) else: # windows elapsed = (time.clock() - start) print("Processing took "+str(elapsed)+"seconds to analyse") print("Done!")
class EDITOR: ''' Launches the main canvas, widgets and dialogues''' def __init__(self,master,filename=None,exit_mode=None): self.exit_mode = exit_mode self.master = master self.main = tk.Frame(self.master) # GEOJSON structure self.DATA = None self.Nfeatures = 0 # Trajectory info self.Trajectory_name = tk.StringVar() self.Trajectory_length = tk.IntVar() self.Trajectory_name.set('') self.Trajectory_length.set(0) self.Trajectory_read = False self.Trajectory_POINTS = None self.Trajectory_date = None self.Trajectory_lon = None self.Trajectory_lat = None self.Trajectory_time = None self.Trajectory_distance = None self.Trajectory_speed = None self.Trajectory_reject = None # Map info self.Mapxmin = tk.DoubleVar() self.Mapxmax = tk.DoubleVar() self.Mapymin = tk.DoubleVar() self.Mapymax = tk.DoubleVar() self.aspectratio = False self.Deploy_date = tk.StringVar() self.Recover_date = tk.StringVar() self.Deploy_date.set('') self.Recover_date.set('') # Info about the selected stations self.Station_date = tk.StringVar() self.Station_lon = tk.DoubleVar() self.Station_lat = tk.DoubleVar() self.Station_speed = tk.DoubleVar() self.Station_pointer = tk.IntVar() self.Station_reject = tk.IntVar() self.Station_date.set('') self.Station_lon.set(None) self.Station_lat.set(None) self.Station_lon.set(None) self.Station_speed.set(None) self.Station_pointer.set(None) self.Station_reject.set(False) self.map_size = 1.0 # Info about events 0 and 1 self.serial_number = tk.StringVar() self.buoy_name = tk.StringVar() self.source = tk.StringVar() self.owner = tk.StringVar() self.contact = tk.StringVar() self.time_units = tk.StringVar() self.event0_lon = tk.DoubleVar() self.event0_lat = tk.DoubleVar() self.event0_date = tk.StringVar() self.event0_qc = tk.IntVar() self.event1_lon = tk.DoubleVar() self.event1_lat = tk.DoubleVar() self.event1_date = tk.StringVar() self.event1_qc = tk.IntVar() self.serial_number.set(None) self.buoy_name.set(None) self.source.set(None) self.owner.set(None) self.contact.set(None) self.time_units.set(None) self.event0_lon.set(None) self.event0_lat.set(None) self.event0_date.set(None) self.event0_qc.set(None) self.event1_lon.set(None) self.event1_lat.set(None) self.event1_date.set(None) self.event1_qc.set(None) self.event0_lon_orig = None self.event0_lat_orig = None self.event0_date_orig = None self.event0_qc_orig = None self.event1_lon_orig = None self.event1_lat_orig = None self.event1_date_orig = None self.event1_qc_orig = None self.buoy_name_orig = None self.source_orig = None self.owner_orig = None self.contact_orig = None self.time_units_orig = None # ------------------------- # Design of the main window # ------------------------- # Filename frame (top of the window) F0 = ttk.Frame(self.main,padding=5) ttk.Label(F0,text='Filename',padding=5).grid(row=0,column=0,sticky='w') ttk.Entry(F0,textvariable=self.Trajectory_name,justify='left',width=89). \ grid(row=0,column=1,columnspan=10,sticky='ew') ttk.Button(F0,text='Load',state='enabled', \ command=self.open_geojson,padding=3). \ grid(row=0,column=12,sticky='e') F0.grid() F0.grid_columnconfigure(0,weight=0) # Define tabs: self.nb = ttk.Notebook(self.main) self.page1 = ttk.Frame(self.nb) self.page2 = ttk.Frame(self.nb) self.nb.add(self.page1,text='Canvas') self.nb.add(self.page2,text='Attributes') # PAGE 1: CANVAS F1 = ttk.Frame(self.page1) ttk.Label(F1,text='Deployment date = ',padding=3).grid(row=0,column=0,columnspan=2) self.wdeploy = ttk.Entry(F1,textvariable=self.Deploy_date,width=10) self.wdeploy.grid(row=0,column=2,columnspan=2,sticky='ew') self.wdeploy.bind("<Return>", lambda f: self.deploytime()) ttk.Label(F1,text='Recovery date = ').grid(row=1,column=0,columnspan=2) self.wrecover = ttk.Entry(F1,textvariable=self.Recover_date,width=10) self.wrecover.grid(row=1,column=2,columnspan=2,sticky='ew') self.wrecover.bind('<Return>', lambda f: self.recovertime()) ttk.Label(F1,text='West = ',padding=3).grid(row=0,column=4) ttk.Entry(F1,textvariable=self.Mapxmin,width=10).grid(row=0,column=5,columnspan=2,sticky='ew') ttk.Label(F1,text='East = ',padding=3).grid(row=1,column=4) ttk.Entry(F1,textvariable=self.Mapxmax,width=10).grid(row=1,column=5,columnspan=2,sticky='ew') ttk.Label(F1,text='South = ',padding=3).grid(row=0,column=7) ttk.Entry(F1,textvariable=self.Mapymin,width=10).grid(row=0,column=8,columnspan=2,sticky='ew') ttk.Label(F1,text='North = ',padding=3).grid(row=1,column=7) ttk.Entry(F1,textvariable=self.Mapymax,width=10).grid(row=1,column=8,columnspan=2,sticky='ew') ttk.Button(F1,text='Redraw',command=self.redraw,padding=5).grid(row=0,column=10,rowspan=2,padx=3) ttk.Button(F1,text='Zoom in',command=self.zoom_in,padding=5).grid(row=0,column=11,rowspan=2,padx=3) ttk.Button(F1,text='Zoom out',command=self.zoom_out,padding=5).grid(row=0,column=12,rowspan=2,padx=3) ttk.Button(F1,text='Aspect Ratio',command=self.ratio,padding=5).grid(row=0,column=13,rowspan=2,padx=3) ttk.Button(F1,text='Reset',command=self.reset,padding=5).grid(row=0,column=14,rowspan=2,padx=3) F1.grid(sticky='nswe') F1.grid_rowconfigure(0,weight=0) F1.grid_columnconfigure(0,weight=0) self.fig = Figure(dpi=150) self.ax1 = self.fig.add_subplot(111) self.canvas = FigureCanvasTkAgg(self.fig,master=self.page1) self.canvas.show() self.canvas.get_tk_widget().grid(sticky='nsew') self.canvas._tkcanvas.grid() self.ax1.get_xaxis().set_visible(False) self.ax1.get_yaxis().set_visible(False) # Bottom menu F2 = ttk.Frame(self.page1,padding=5) ttk.Button(F2,text='+',command=self.station_up,padding=3). \ grid(row=0,column=0) ttk.Label(F2,text='station = ',padding=3).grid(row=0,column=1) self.wstat = ttk.Entry(F2,textvariable=self.Station_pointer) self.wstat.grid(row=0,column=2,columnspan=2) self.wstat.bind('<Return>', lambda f: self.station_manual()) ttk.Label(F2,text='/ ',padding=3).grid(row=0,column=4) ttk.Entry(F2,textvariable=self.Trajectory_length).grid(row=0,column=5,columnspan=2) ttk.Checkbutton(F2,text='Reject',command=self.reject_this, \ variable=self.Station_reject).grid(row=0,column=7) ttk.Button(F2,text='Reject stations before this', \ command=self.reject_before).grid(row=0,column=8,columnspan=2) ttk.Button(F2,text='Reject stations after this', \ command=self.reject_after).grid(row=0,column=11,columnspan=2) ttk.Button(F2,text='-',command=self.station_down,padding=3). \ grid(row=1,column=0) ttk.Label(F2,text='Date = ',padding=3).grid(row=1,column=1) ttk.Entry(F2,textvariable=self.Station_date).grid(row=1,column=2,columnspan=2) ttk.Label(F2,text='Longitude = ',padding=3).grid(row=1,column=4) ttk.Entry(F2,textvariable=self.Station_lon).grid(row=1,column=5,columnspan=2) ttk.Label(F2,text='Latitude = ',padding=3).grid(row=1,column=7) ttk.Entry(F2,textvariable=self.Station_lat).grid(row=1,column=8,columnspan=2) ttk.Label(F2,text='Speed = ',padding=3).grid(row=1,column=10) ttk.Entry(F2,textvariable=self.Station_speed).grid(row=1,column=11,columnspan=2) ttk.Button(F2,text='Purge',command=self.purge).grid(row=2,column=10) ttk.Button(F2,text='Save as',command=self.save).grid(row=2,column=11) ttk.Button(F2,text='Close',command=self.close).grid(row=2,column=12) F2.grid(sticky='ew') # PAGE 2: ATTRIBUTES ttk.Label(self.page2,text='Properties',padding=3,font='Helvetical 12 bold').grid(row=0,column=0) ttk.Label(self.page2,text='Serial number',padding=3).grid(row=1,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.serial_number,state='disabled').grid(row=1,column=2,columnspan=3) ttk.Button(self.page2,text='Restore',command=self.restore,padding=3).grid(row=1,column=6,padx=5) ttk.Label(self.page2,text='Name',padding=3).grid(row=2,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.buoy_name).grid(row=2,column=2,columnspan=3) ttk.Label(self.page2,text='Source',padding=3).grid(row=3,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.source).grid(row=3,column=2,columnspan=3) ttk.Label(self.page2,text='Owner',padding=3).grid(row=4,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.owner).grid(row=4,column=2,columnspan=3) ttk.Label(self.page2,text='Contact',padding=3).grid(row=5,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.contact).grid(row=5,column=2,columnspan=3) ttk.Label(self.page2,text="Initial point ('event':0)",padding=3,font='Helvetical 12 bold').grid(row=6,column=0) ttk.Label(self.page2,text='Longitude',padding=3).grid(row=7,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event0_lon).grid(row=7,column=2,columnspan=3) ttk.Button(self.page2,text='Update',command=self.update_event0,padding=3).grid(row=7,column=5,padx=5) ttk.Label(self.page2,text='Latitude',padding=3).grid(row=8,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event0_lat).grid(row=8,column=2,columnspan=3) ttk.Label(self.page2,text='Date',padding=3).grid(row=9,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event0_date).grid(row=9,column=2,columnspan=3) ttk.Label(self.page2,text='Date units',padding=3).grid(row=10,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.time_units).grid(row=10,column=2,columnspan=3) ttk.Label(self.page2,text='Date QC',padding=3).grid(row=11,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event0_qc).grid(row=11,column=2,columnspan=3) ttk.Label(self.page2,text="Final point ('event':1)",padding=3,font='Helvetical 12 bold').grid(row=12,column=0) ttk.Label(self.page2,text='Longitude',padding=3).grid(row=13,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event1_lon).grid(row=13,column=2,columnspan=3) ttk.Button(self.page2,text='Update',command=self.update_event1,padding=3).grid(row=13,column=5,padx=5) ttk.Label(self.page2,text='Latitude',padding=3).grid(row=14,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event1_lat).grid(row=14,column=2,columnspan=3) ttk.Label(self.page2,text='Date',padding=3).grid(row=15,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event1_date).grid(row=15,column=2,columnspan=3) ttk.Label(self.page2,text='Date units',padding=3).grid(row=16,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.time_units).grid(row=16,column=2,columnspan=3) ttk.Label(self.page2,text='Date QC',padding=3).grid(row=17,column=1,padx=5) ttk.Entry(self.page2,textvariable=self.event1_qc).grid(row=17,column=2,columnspan=3) # PACK THE WHOLE THING self.nb.grid() self.main.grid() if filename is None: pass else: self.open_geojson(ask=False,filename=filename) # --------------------- def close(self): # --------------------- if self.exit_mode == 'quit': ''' Closing the main widget ''' messagebox.askquestion('Close','Are you sure?',icon='warning') if 'yes': quit() else: self.master.destroy() return # --------------------------------------------- def open_geojson(self,ask=True,filename=None): # --------------------------------------------- ''' Opening geojson file ''' if ask or filename is None: backup = self.Trajectory_name.get() try: nn = filedialog.askopenfile(filetypes=[('GEOJSON','*.geojson'),('JSON','*.json'),('ALL','*.*')]) self.Trajectory_name.set(nn.name) except: self.Trajectory_name.set(backup) else: self.Trajectory_name.set(filename) if not empty(self.Trajectory_name.get()): # Read the GEOJSON file using json.load # ::::::::::::::::::::::::::::::::::::: with open(self.Trajectory_name.get()) as data_file: self.DATA = json.load(data_file) # Split the information into LON, LAT, SST, etc # ::::::::::::::::::::::::::::::::::::::::::::: self.Trajectory_read = True self.Nfeatures = len(self.DATA["features"]) for i in range(self.Nfeatures): if self.DATA["features"][i]["geometry"]["type"] == "LineString": self.Trajectory_POINTS = self.DATA["features"][i]["geometry"]["coordinates"] self.Trajectory_date = self.DATA["features"][i]["properties"]["time"]["data"] try: self.Trajectory_temp = self.DATA["features"][i]["properties"]["sst"]["data"] except: self.Trajectory_temp = [] self.Trajectory_length.set(len(self.Trajectory_date)) self.Trajectory_lon = [self.Trajectory_POINTS[j][0] for j in range(self.Trajectory_length.get())] self.Trajectory_lat = [self.Trajectory_POINTS[j][1] for j in range(self.Trajectory_length.get())] self.Trajectory_seconds = [] for i in range(self.Trajectory_length.get()): tt = dparser.parse(self.Trajectory_date[i]) self.Trajectory_seconds.append(int(tt.strftime('%s'))) # Get travelled distance (in meters) # :::::::::::::::::::::::::::::::::: self.Trajectory_reject = [] self.Trajectory_distance = [] for i in range(self.Trajectory_length.get()): self.Trajectory_reject.append(tk.IntVar()) self.Trajectory_reject[i].set(0) if i == 0: self.Trajectory_distance.append(float(0)) dist = 0 else: dr = haversine(self.Trajectory_POINTS[i],self.Trajectory_POINTS[i-1]) dist += dr self.Trajectory_distance.append(dist) # Get time between stations self.Trajectory_time = [] for i in range(self.Trajectory_length.get()): if i == 0: self.Trajectory_time.append(float(0)) else: #now = iso8601.parse_date(self.Trajectory_date[i]) #pre = iso8601.parse_date(self.Trajectory_date[i-1]) now = datetime.datetime.strptime(self.Trajectory_date[i][0:18],'%Y-%m-%dT%H:%M:%S') pre = datetime.datetime.strptime(self.Trajectory_date[i-1][0:18],'%Y-%m-%dT%H:%M:%S') secs = (now-pre).seconds self.Trajectory_time.append(secs) # Speed required to move between stations self.Trajectory_speed = [] self.Trajectory_speed.append(0) # AAA print(self.Trajectory_time) for i in range(1,self.Trajectory_length.get()): if self.Trajectory_time[i] < 1E-3: u = 0 else: u = (self.Trajectory_distance[i]- self.Trajectory_distance[i-1])/self.Trajectory_time[i] self.Trajectory_speed.append(u) self.map_size = 1.0 self.data_xmin = min(self.Trajectory_lon) self.data_xmax = max(self.Trajectory_lon) self.data_ymin = min(self.Trajectory_lat) self.data_ymax = max(self.Trajectory_lat) self.data_xsize = self.data_xmax - self.data_xmin self.data_ysize = self.data_ymax - self.data_ymin self.Mapxmin.set(myround(self.data_xmin - \ self.map_size*self.data_xsize,2)) self.Mapxmax.set(myround(self.data_xmax + \ self.map_size*self.data_xsize,2)) self.Mapymin.set(myround(self.data_ymin - \ self.map_size*self.data_ysize,2)) self.Mapymax.set(myround(self.data_ymax + \ self.map_size*self.data_ysize,2)) #self.Mapxmin.set(myround(self.data_xmin-0.5,1)) #self.Mapxmax.set(myround(self.data_xmax+0.5,1)) #self.Mapymin.set(myround(self.data_ymin-0.5,1)) #self.Mapymax.set(myround(self.data_ymax+0.5,1)) self.Station_pointer.set(0) self.Station_date.set(self.Trajectory_date[0]) self.Station_lon.set(self.Trajectory_lon[0]) self.Station_lat.set(self.Trajectory_lat[0]) self.Station_speed.set(self.Trajectory_speed[0]) self.Station_reject.set(self.Trajectory_reject[0].get()) self.Deploy_date.set(self.Trajectory_date[0]) self.Recover_date.set(self.Trajectory_date[self.Trajectory_length.get()-1]) self.ax1.clear() self.draw_map() self.make_plot() # Scan for info about points: for i in range(self.Nfeatures): if self.DATA["features"][i]["geometry"]["type"] == "Point": #print(self.DATA["features"][i]["properties"]) if self.DATA["features"][i]["properties"]["event"] == 0: self.event0_lon.set(self.DATA["features"][i]["geometry"]["coordinates"][0]) self.event0_lat.set(self.DATA["features"][i]["geometry"]["coordinates"][1]) self.serial_number.set(self.DATA["features"][i]["properties"]["code_sn"]) self.buoy_name.set(self.DATA["features"][i]["properties"]["name"]) self.contact.set(self.DATA["features"][i]["properties"]["contact"]) self.source.set(self.DATA["features"][i]["properties"]["source"]) self.owner.set(self.DATA["features"][i]["properties"]["owner"]) self.event0_date.set(self.DATA["features"][i]["properties"]["time"]["data"][0]) self.event0_qc.set(self.DATA["features"][i]["properties"]["time"]["qc_data"]) try: a = self.event0_qc.get() except: self.event0_qc.set(0) #self.event0_qc.set(self.DATA["features"][i]["properties"]["qc"]) self.time_units.set(self.DATA["features"][i]["properties"]["time"]["units"]) if self.DATA["features"][i]["properties"]["event"] == 1: self.event1_lon.set(self.DATA["features"][i]["geometry"]["coordinates"][0]) self.event1_lat.set(self.DATA["features"][i]["geometry"]["coordinates"][1]) self.event1_date.set(self.DATA["features"][i]["properties"]["time"]["data"][0]) self.event1_qc.set(self.DATA["features"][i]["properties"]["time"]["qc_data"]) try: a = self.event1_qc.get() except: self.event1_qc.set(0) #self.event1_qc.set(self.DATA["features"][i]["properties"]["qc"]) self.buoy_name_orig = self.buoy_name.get() self.source_orig = self.source.get() self.owner_orig = self.owner.get() self.contact_orig = self.contact.get() self.time_units_orig = self.time_units.get() self.event0_lon_orig = self.event0_lon.get() self.event0_lat_orig = self.event0_lat.get() self.event0_date_orig = self.event0_date.get() self.event0_qc_orig = self.event0_qc.get() self.event1_lon_orig = self.event1_lon.get() self.event1_lat_orig = self.event1_lat.get() self.event1_date_orig = self.event0_date.get() self.event1_qc_orig = self.event0_qc.get() else: self.DATA = None self.Trajectory = None self.Trajectory_read = False self.Nfeatures = 0 # -------------- def restore(self): # -------------- '''Restore GEOJSON properties''' self.buoy_name.set(self.buoy_name_orig) self.source.set(self.source_orig) self.owner.set(self.owner_orig) self.contact.set(self.contact_orig) self.time_units.set(self.time_units_orig) self.event0_lon.set(self.event0_lon_orig) self.event0_lat.set(self.event0_lat_orig) self.event0_date.set(self.event0_date_orig) self.event0_qc.set(self.event0_qc_orig) self.event1_lon.set(self.event1_lon_orig) self.event1_lat.set(self.event1_lat_orig) self.event1_date.set(self.event1_date_orig) self.event1_qc.set(self.event1_qc_orig) # -------------- def update_event0(self): # -------------- '''Update coordinates and date of the first point''' self.event0_lon.set(self.Trajectory_lon[0]) self.event0_lat.set(self.Trajectory_lat[0]) self.event0_date.set(self.Trajectory_date[0]) self.event0_qc.set(1) # -------------- def update_event1(self): # -------------- '''Update coordinates and date of the first point''' i = self.Trajectory_length.get()-1 self.event1_lon.set(self.Trajectory_lon[i]) self.event1_lat.set(self.Trajectory_lat[i]) self.event1_date.set(self.Trajectory_date[i]) self.event1_qc.set(1) # -------------- def purge(self): # -------------- '''Purge rejected stations ''' for i in reversed(range(self.Trajectory_length.get())): if self.Trajectory_reject[i].get() == 1: print('Removing station = ',i) del self.Trajectory_POINTS[i] del self.Trajectory_lon[i] del self.Trajectory_lat[i] del self.Trajectory_date[i] try: del self.Trajectory_temp[i] except: pass self.Trajectory_length.set(len(self.Trajectory_lon)) self.Station_pointer.set(0) # Get travelled distance (in meters) # :::::::::::::::::::::::::::::::::: self.Trajectory_reject = [] self.Trajectory_distance = [] for i in range(self.Trajectory_length.get()): self.Trajectory_reject.append(tk.IntVar()) self.Trajectory_reject[i].set(0) if i == 0: self.Trajectory_distance.append(float(0)) dist = 0 else: dr = haversine(self.Trajectory_POINTS[i],self.Trajectory_POINTS[i-1]) dist += dr self.Trajectory_distance.append(dist) # Get time between stations self.Trajectory_time = [] for i in range(self.Trajectory_length.get()): if i == 0: self.Trajectory_time.append(float(0)) else: #now = iso8601.parse_date(self.Trajectory_date[i]) #pre = iso8601.parse_date(self.Trajectory_date[i-1]) now = datetime.datetime.strptime(self.Trajectory_date[i][0:18],'%Y-%m-%dT%H:%M:%S') pre = datetime.datetime.strptime(self.Trajectory_date[i-1][0:18],'%Y-%m-%dT%H:%M:%S') secs = (now-pre).seconds self.Trajectory_time.append(secs) # Speed required to move between stations self.Trajectory_speed = [] self.Trajectory_speed.append(0) for i in range(1,self.Trajectory_length.get()): u = (self.Trajectory_distance[i]-self.Trajectory_distance[i-1])/self.Trajectory_time[i] self.Trajectory_speed.append((self.Trajectory_distance[i]-self.Trajectory_distance[i-1])/ self.Trajectory_time[i]) self.Station_date.set(self.Trajectory_date[self.Station_pointer.get()]) self.Station_lon.set(self.Trajectory_lon[self.Station_pointer.get()]) self.Station_lat.set(self.Trajectory_lat[self.Station_pointer.get()]) self.Station_speed.set(self.Trajectory_speed[self.Station_pointer.get()]) self.Station_reject.set(self.Trajectory_reject[self.Station_pointer.get()].get()) self.draw_map() self.make_plot() # ------------- def save(self): # ------------- '''Save new file''' nn = filedialog.asksaveasfilename(title='Save',confirmoverwrite=True) if nn is not None: outfile = '%s' % nn # The following values may have changed: POINTS, DATE, TEMP for i in range(self.Nfeatures): if self.DATA["features"][i]["geometry"]["type"] == "LineString": self.DATA["features"][i]["geometry"]["coordinates"] = self.Trajectory_POINTS self.DATA["features"][i]["properties"]["time"]["data"] = self.Trajectory_date if len(self.Trajectory_temp) > 0: self.DATA["features"][i]["properties"]["sst"]["data"] = self.Trajectory_temp # The following values may have changed: COORDINATES, PROPERTIES for i in range(self.Nfeatures): if self.DATA["features"][i]["geometry"]["type"] == "Point": if self.DATA["features"][i]["properties"]["event"] == 0: self.DATA["features"][i]["geometry"]["coordinates"][0] = self.event0_lon.get() self.DATA["features"][i]["geometry"]["coordinates"][1] = self.event0_lat.get() self.DATA["features"][i]["properties"]["name"] = self.buoy_name.get() self.DATA["features"][i]["properties"]["source"] = self.source.get() self.DATA["features"][i]["properties"]["owner"] = self.owner.get() self.DATA["features"][i]["properties"]["contact"] = self.contact.get() self.DATA["features"][i]["properties"]["time"]["data"][0] = self.event0_date.get() self.DATA["features"][i]["properties"]["time"]["units"] = self.time_units.get() self.DATA["features"][i]["properties"]["time"]["qc_data"] = self.event0_qc.get() if self.DATA["features"][i]["properties"]["event"] == 1: self.DATA["features"][i]["geometry"]["coordinates"][0] = self.event1_lon.get() self.DATA["features"][i]["geometry"]["coordinates"][1] = self.event1_lat.get() self.DATA["features"][i]["properties"]["name"] = self.buoy_name.get() self.DATA["features"][i]["properties"]["source"] = self.source.get() self.DATA["features"][i]["properties"]["owner"] = self.owner.get() self.DATA["features"][i]["properties"]["contact"] = self.contact.get() self.DATA["features"][i]["properties"]["time"]["data"][0] = self.event1_date.get() self.DATA["features"][i]["properties"]["time"]["units"] = self.time_units.get() self.DATA["features"][i]["properties"]["time"]["qc_data"] = self.event1_qc.get() with open(outfile,'w') as fp: json.dump(self.DATA,fp) print('File %s written' % outfile) # -------------------- def reject_this(self): # -------------------- '''Rejects a station''' if self.Trajectory_read: self.Trajectory_reject[self.Station_pointer.get()].set(self.Station_reject.get()) # ---------------------- def reject_before(self): # ---------------------- '''Rejects a station''' if self.Trajectory_read: for i in range(self.Station_pointer.get()): self.Trajectory_reject[i].set(1) self.make_plot() # ---------------------- def reject_after(self): # ---------------------- '''Rejects a station''' if self.Trajectory_read: for i in range(self.Station_pointer.get()+1,self.Trajectory_length.get()): self.Trajectory_reject[i].set(1) self.make_plot() # ----------------------- def recovertime(self): # ----------------------- '''Manual selection of the final period''' if self.Trajectory_read: dd = self.wrecover.get() tt = dparser.parse(dd) t0 = int(tt.strftime('%s')) for i in range(self.Trajectory_length.get()): if self.Trajectory_seconds[i] > t0: self.Trajectory_reject[i].set(1) self.Station_reject.set(self.Trajectory_reject[self.Station_pointer.get()].get()) self.make_plot() # ----------------------- def deploytime(self): # ----------------------- '''Manual selection of the initial period''' if self.Trajectory_read: dd = self.wdeploy.get() tt = dparser.parse(dd) t0 = int(tt.strftime('%s')) for i in range(self.Trajectory_length.get()): if self.Trajectory_seconds[i] < t0: self.Trajectory_reject[i].set(1) self.Station_reject.set(self.Trajectory_reject[self.Station_pointer.get()].get()) self.make_plot() # ----------------------- def station_manual(self): # ----------------------- '''Manual selection of an station''' if self.Trajectory_read: num = self.wstat.get() self.Station_pointer.set(num) self.Station_date.set(self.Trajectory_date[self.Station_pointer.get()]) self.Station_lon.set(self.Trajectory_lon[self.Station_pointer.get()]) self.Station_lat.set(self.Trajectory_lat[self.Station_pointer.get()]) self.Station_speed.set(self.Trajectory_speed[self.Station_pointer.get()]) self.Station_reject.set(self.Trajectory_reject[self.Station_pointer.get()].get()) self.make_plot() # ------------------- def station_up(self): # ------------------- '''Moves pointer up one''' if self.Station_pointer.get() < self.Trajectory_length.get()-1: i = self.Station_pointer.get() if self.Trajectory_reject[i].get() == 1: self.m.plot(self.xx[i], \ self.yy[i],'o',ms=4,color='grey') else: self.m.plot(self.xx[i], \ self.yy[i],'o',ms=4,color='white') self.Station_pointer.set(self.Station_pointer.get()+1) self.m.plot(self.xx[self.Station_pointer.get()], \ self.yy[self.Station_pointer.get()],'o',ms=4,color='red') self.Station_date.set(self.Trajectory_date[self.Station_pointer.get()]) self.Station_lon.set(self.Trajectory_lon[self.Station_pointer.get()]) self.Station_lat.set(self.Trajectory_lat[self.Station_pointer.get()]) self.Station_speed.set(self.Trajectory_speed[self.Station_pointer.get()]) self.Station_reject.set(self.Trajectory_reject[self.Station_pointer.get()].get()) self.canvas.draw() # -------------------- def station_down(self): # -------------------- '''Moves pointer down one''' if self.Station_pointer.get() > 0: i = self.Station_pointer.get() if self.Trajectory_reject[i].get() == 1: self.m.plot(self.xx[i], \ self.yy[i],'o',ms=4,color='grey') else: self.m.plot(self.xx[i], \ self.yy[i],'o',ms=4,color='white') #self.m.plot(self.xx[self.Station_pointer.get()], \ # self.yy[self.Station_pointer.get()],'o',ms=4,color='white') self.Station_pointer.set(self.Station_pointer.get()-1) self.m.plot(self.xx[self.Station_pointer.get()], \ self.yy[self.Station_pointer.get()],'o',ms=4,color='red') self.Station_date.set(self.Trajectory_date[self.Station_pointer.get()]) self.Station_lon.set(self.Trajectory_lon[self.Station_pointer.get()]) self.Station_lat.set(self.Trajectory_lat[self.Station_pointer.get()]) self.Station_speed.set(self.Trajectory_speed[self.Station_pointer.get()]) self.Station_reject.set(self.Trajectory_reject[self.Station_pointer.get()].get()) self.canvas.draw() # --------------- def reset(self): # --------------- '''Resets the map limits''' self.map_size = 1.0 self.data_xmin = min(self.Trajectory_lon) self.data_xmax = max(self.Trajectory_lon) self.data_ymin = min(self.Trajectory_lat) self.data_ymax = max(self.Trajectory_lat) self.data_xsize = self.data_xmax - self.data_xmin self.data_ysize = self.data_ymax - self.data_ymin #self.Mapxmin.set(#np.trunc(self.data_xmin - \ # self.map_size*self.data_xsize)) #self.Mapxmax.set(np.trunc(self.data_xmax + \ # self.map_size*self.data_xsize)) #self.Mapymin.set(np.trunc(self.data_ymin - \ # self.map_size*self.data_ysize)) #self.Mapymax.set(np.trunc(self.data_ymax + \ # self.map_size*self.data_ysize)) self.Mapxmin.set(myround(self.data_xmin - \ self.map_size*self.data_xsize,2)) self.Mapxmax.set(myround(self.data_xmax + \ self.map_size*self.data_xsize,2)) self.Mapymin.set(myround(self.data_ymin - \ self.map_size*self.data_ysize,2)) self.Mapymax.set(myround(self.data_ymax + \ self.map_size*self.data_ysize,2)) #self.Mapxmin.set(myround(self.data_xmin-0.5,1)) #self.Mapxmax.set(myround(self.data_xmax+0.5,1)) #self.Mapymin.set(myround(self.data_ymin-0.5,1)) #self.Mapymax.set(myround(self.data_ymax+0.5,1)) self.Deploy_date.set(self.Trajectory_date[0]) self.Recover_date.set(self.Trajectory_date[self.Trajectory_length.get()-1]) for i in range(self.Trajectory_length.get()): self.Trajectory_reject[i].set(0) self.ax1.clear() self.draw_map() self.make_plot() # --------------- def zoom_in(self): # --------------- '''Zooms in the map''' self.map_size = 0.50*self.map_size self.Mapxmin.set(self.data_xmin - self.map_size*self.data_xsize) self.Mapxmax.set(self.data_xmax + self.map_size*self.data_xsize) self.Mapymin.set(self.data_ymin - self.map_size*self.data_ysize) self.Mapymax.set(self.data_ymax + self.map_size*self.data_ysize) self.draw_map() self.make_plot() # --------------- def zoom_out(self): # --------------- '''Zooms out the map''' self.map_size = self.map_size/0.50 self.Mapxmin.set(self.data_xmin - self.map_size*self.data_xsize) self.Mapxmax.set(self.data_xmax + self.map_size*self.data_xsize) self.Mapymin.set(self.data_ymin - self.map_size*self.data_ysize) self.Mapymax.set(self.data_ymax + self.map_size*self.data_ysize) self.draw_map() self.make_plot() def ratio(self): '''Redraws the map''' self.aspectratio = not self.aspectratio self.draw_map() self.make_plot() def redraw(self): '''Redraws the map''' self.draw_map() self.make_plot() def draw_map(self): '''Draws the map''' self.ax1.clear() projection = 'cyl' projection = 'merc' try: self.m = Basemap( \ #projection=projection, \ #resolution='i', \ llcrnrlat=self.Mapymin.get(), \ urcrnrlat=self.Mapymax.get(), \ llcrnrlon=self.Mapxmin.get(), \ urcrnrlon=self.Mapxmax.get(), \ #fix_aspect=self.aspectratio, \ ax=self.ax1) self.m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 700, verbose= False) except: self.m = Basemap( \ projection=projection, \ resolution='i', \ llcrnrlat=self.Mapymin.get(), \ urcrnrlat=self.Mapymax.get(), \ llcrnrlon=self.Mapxmin.get(), \ urcrnrlon=self.Mapxmax.get(), \ ax=self.ax1) self.m.fillcontinents(color='Coral') self.m.drawmapboundary(fill_color='aqua') self.m.drawcoastlines(linewidth=1,color='black') dx = myround((self.Mapxmax.get()-self.Mapxmin.get())/5,1) xw = myround(self.Mapxmin.get()-15,0) xe = myround(self.Mapxmax.get()+15,0) dy = myround((self.Mapymax.get()-self.Mapymin.get())/5,1) ys = myround(self.Mapymin.get()-15,0) yn = myround(self.Mapymax.get()+15,0) self.m.drawmeridians(np.arange(xw,xe,dx),labels=[1,0,0,1]) self.m.drawparallels(np.arange(ys,yn,dy),labels=[0,1,0,1]) self.xx,self.yy = self.m(self.Trajectory_lon,self.Trajectory_lat) self.m.plot(self.xx[self.Station_pointer.get()], \ self.yy[self.Station_pointer.get()],'o',ms=4,color='red') def make_plot(self): '''Draws the trajectory over the map''' self.m.plot(self.xx,self.yy,'--',linewidth=0.8,color='grey') #self.m.plot(self.xx,self.yy,'o',ms=4,linewidth=1,color='white') for i in range(self.Trajectory_length.get()): if self.Trajectory_reject[i].get() == 1: self.m.plot(self.xx[i],self.yy[i],'o',ms=4,linewidth=1,color='grey') else: self.m.plot(self.xx[i],self.yy[i],'o',ms=4,linewidth=1,color='white') self.m.plot(self.xx[self.Station_pointer.get()], \ self.yy[self.Station_pointer.get()],'o',ms=4,color='red') self.canvas.draw()
def make_map(e, n, t, d, dat_port, dat_star, data_R, pix_m, res, cs2cs_args, sonpath, p, mode, nn, numstdevs, c, dx, use_uncorrected, scalemax): #dogrid, influence,dowrite, thres=5 trans = pyproj.Proj(init=cs2cs_args) mp = np.nanmean(dat_port) ms = np.nanmean(dat_star) if mp>ms: merge = np.vstack((dat_port,dat_star*(mp/ms))) else: merge = np.vstack((dat_port*(ms/mp),dat_star)) del dat_port, dat_star merge[np.isnan(merge)] = 0 merge = merge[:,:len(n)] ## actual along-track resolution is this: dx times dy = Af tmp = data_R * dx * (c*0.007 / 2) #dx = np.arcsin(c/(1000*meta['t']*meta['f'])) res_grid = np.sqrt(np.vstack((tmp, tmp))) del tmp res_grid = res_grid[:np.shape(merge)[0],:np.shape(merge)[1]] #if use_uncorrected != 1: # merge = merge - 10*np.log10(res_grid) res_grid = res_grid.astype('float32') merge[np.isnan(merge)] = 0 merge[merge<0] = 0 merge = merge.astype('float32') merge = denoise_tv_chambolle(merge.copy(), weight=.2, multichannel=False).astype('float32') R = np.vstack((np.flipud(data_R),data_R)) del data_R R = R[:np.shape(merge)[0],:np.shape(merge)[1]] # get number pixels in scan line extent = int(np.shape(merge)[0]/2) yvec = np.squeeze(np.linspace(np.squeeze(pix_m),extent*np.squeeze(pix_m),extent)) X, Y, D, h, t = getXY(e,n,yvec,np.squeeze(d),t,extent) X = X.astype('float32') Y = Y.astype('float32') D = D.astype('float32') h = h.astype('float32') t = t.astype('float32') X = X.astype('float32') D[np.isnan(D)] = 0 h[np.isnan(h)] = 0 t[np.isnan(t)] = 0 X = X[np.where(np.logical_not(np.isnan(Y)))] merge = merge.flatten()[np.where(np.logical_not(np.isnan(Y)))] res_grid = res_grid.flatten()[np.where(np.logical_not(np.isnan(Y)))] Y = Y[np.where(np.logical_not(np.isnan(Y)))] D = D[np.where(np.logical_not(np.isnan(Y)))] R = R.flatten()[np.where(np.logical_not(np.isnan(Y)))] h = h[np.where(np.logical_not(np.isnan(Y)))] t = t[np.where(np.logical_not(np.isnan(Y)))] Y = Y[np.where(np.logical_not(np.isnan(X)))] merge = merge.flatten()[np.where(np.logical_not(np.isnan(X)))] res_grid = res_grid.flatten()[np.where(np.logical_not(np.isnan(X)))] X = X[np.where(np.logical_not(np.isnan(X)))] D = D[np.where(np.logical_not(np.isnan(X)))] R = R.flatten()[np.where(np.logical_not(np.isnan(X)))] h = h[np.where(np.logical_not(np.isnan(X)))] t = t[np.where(np.logical_not(np.isnan(X)))] X = X[np.where(np.logical_not(np.isnan(merge)))] Y = Y[np.where(np.logical_not(np.isnan(merge)))] merge = merge[np.where(np.logical_not(np.isnan(merge)))] res_grid = res_grid.flatten()[np.where(np.logical_not(np.isnan(merge)))] D = D[np.where(np.logical_not(np.isnan(merge)))] R = R[np.where(np.logical_not(np.isnan(merge)))] h = h[np.where(np.logical_not(np.isnan(merge)))] t = t[np.where(np.logical_not(np.isnan(merge)))] X = X[np.where(np.logical_not(np.isinf(merge)))] Y = Y[np.where(np.logical_not(np.isinf(merge)))] merge = merge[np.where(np.logical_not(np.isinf(merge)))] res_grid = res_grid.flatten()[np.where(np.logical_not(np.isinf(merge)))] D = D[np.where(np.logical_not(np.isinf(merge)))] R = R[np.where(np.logical_not(np.isinf(merge)))] h = h[np.where(np.logical_not(np.isinf(merge)))] t = t[np.where(np.logical_not(np.isinf(merge)))] print("writing point cloud") #if dowrite==1: ## write raw bs to file outfile = os.path.normpath(os.path.join(sonpath,'x_y_ss_raw'+str(p)+'.asc')) ##write.txtwrite( outfile, np.hstack((humutils.ascol(X.flatten()),humutils.ascol(Y.flatten()), humutils.ascol(merge.flatten()), humutils.ascol(D.flatten()), humutils.ascol(R.flatten()), humutils.ascol(h.flatten()), humutils.ascol(t.flatten()) )) ) np.savetxt(outfile, np.hstack((humutils.ascol(X.flatten()),humutils.ascol(Y.flatten()), humutils.ascol(merge.flatten()), humutils.ascol(D.flatten()), humutils.ascol(R.flatten()), humutils.ascol(h.flatten()), humutils.ascol(t.flatten()) )) , fmt="%8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f") del D, R, h, t sigmas = 0.1 #m eps = 2 print("gridding ...") #if dogrid==1: if 2>1: if res==99: resg = np.min(res_grid[res_grid>0])/2 print('Gridding at resolution of %s' % str(resg)) else: resg = res tree = KDTree(np.c_[X.flatten(),Y.flatten()]) complete=0 while complete==0: try: grid_x, grid_y, res = getmesh(np.min(X), np.max(X), np.min(Y), np.max(Y), resg) longrid, latgrid = trans(grid_x, grid_y, inverse=True) longrid = longrid.astype('float32') latgrid = latgrid.astype('float32') shape = np.shape(grid_x) ## create mask for where the data is not if pykdtree==1: dist, _ = tree.query(np.c_[grid_x.ravel(), grid_y.ravel()], k=1) else: try: dist, _ = tree.query(np.c_[grid_x.ravel(), grid_y.ravel()], k=1, n_jobs=cpu_count()) except: #print ".... update your scipy installation to use faster kd-tree queries" dist, _ = tree.query(np.c_[grid_x.ravel(), grid_y.ravel()], k=1) dist = dist.reshape(grid_x.shape) targ_def = pyresample.geometry.SwathDefinition(lons=longrid.flatten(), lats=latgrid.flatten()) del longrid, latgrid humlon, humlat = trans(X, Y, inverse=True) orig_def = pyresample.geometry.SwathDefinition(lons=humlon.flatten(), lats=humlat.flatten()) del humlon, humlat if 'orig_def' in locals(): complete=1 except: print("memory error: trying grid resolution of %s" % (str(resg*2))) resg = resg*2 if mode==1: complete=0 while complete==0: try: try: dat = pyresample.kd_tree.resample_nearest(orig_def, merge.flatten(), targ_def, radius_of_influence=res*20, fill_value=None, nprocs = cpu_count(), reduce_data=1) except: dat = pyresample.kd_tree.resample_nearest(orig_def, merge.flatten(), targ_def, radius_of_influence=res*20, fill_value=None, nprocs = 1, reduce_data=1) try: r_dat = pyresample.kd_tree.resample_nearest(orig_def, res_grid.flatten(), targ_def, radius_of_influence=res*20, fill_value=None, nprocs = cpu_count(), reduce_data=1) except: r_dat = pyresample.kd_tree.resample_nearest(orig_def, res_grid.flatten(), targ_def, radius_of_influence=res*20, fill_value=None, nprocs = 1, reduce_data=1) stdev = None counts = None if 'dat' in locals(): complete=1 except: del grid_x, grid_y, targ_def, orig_def wf = None humlon, humlat = trans(X, Y, inverse=True) dat, stdev, counts, resg, complete, shape = getgrid_lm(humlon, humlat, merge, res*10, min(X), max(X), min(Y), max(Y), resg*2, mode, trans, nn, wf, sigmas, eps) r_dat, stdev, counts, resg, complete, shape = getgrid_lm(humlon, humlat, res_grid, res*10, min(X), max(X), min(Y), max(Y), resg*2, mode, trans, nn, wf, sigmas, eps) del humlon, humlat elif mode==2: # custom inverse distance wf = lambda r: 1/r**2 complete=0 while complete==0: try: try: dat, stdev, counts = pyresample.kd_tree.resample_custom(orig_def, merge.flatten(),targ_def, radius_of_influence=res*20, neighbours=nn, weight_funcs=wf, fill_value=None, with_uncert = True, nprocs = cpu_count(), reduce_data=1) except: dat, stdev, counts = pyresample.kd_tree.resample_custom(orig_def, merge.flatten(),targ_def, radius_of_influence=res*20, neighbours=nn, weight_funcs=wf, fill_value=None, with_uncert = True, nprocs = 1, reduce_data=1) try: r_dat = pyresample.kd_tree.resample_custom(orig_def, res_grid.flatten(), targ_def, radius_of_influence=res*20, neighbours=nn, weight_funcs=wf, fill_value=None, with_uncert = False, nprocs = cpu_count(), reduce_data=1) except: r_dat = pyresample.kd_tree.resample_custom(orig_def, res_grid.flatten(), targ_def, radius_of_influence=res*20, neighbours=nn, weight_funcs=wf, fill_value=None, with_uncert = False, nprocs = 1, reduce_data=1) if 'dat' in locals(): complete=1 except: del grid_x, grid_y, targ_def, orig_def humlon, humlat = trans(X, Y, inverse=True) dat, stdev, counts, resg, complete, shape = getgrid_lm(humlon, humlat, merge, res*2, min(X), max(X), min(Y), max(Y), resg*2, mode, trans, nn, wf, sigmas, eps) r_dat, stdev, counts, resg, complete, shape = getgrid_lm(humlon, humlat, res_grid, res*2, min(X), max(X), min(Y), max(Y), resg*2, mode, trans, nn, wf, sigmas, eps) del humlat, humlon del stdev_null, counts_null elif mode==3: wf = None complete=0 while complete==0: try: try: dat, stdev, counts = pyresample.kd_tree.resample_gauss(orig_def, merge.flatten(), targ_def, radius_of_influence=res*20, neighbours=nn, sigmas=sigmas, fill_value=None, with_uncert = True, nprocs = cpu_count(), epsilon = eps, reduce_data=1) except: dat, stdev, counts = pyresample.kd_tree.resample_gauss(orig_def, merge.flatten(), targ_def, radius_of_influence=res*20, neighbours=nn, sigmas=sigmas, fill_value=None, with_uncert = True, nprocs = 1, epsilon = eps, reduce_data=1) try: r_dat = pyresample.kd_tree.resample_gauss(orig_def, res_grid.flatten(), targ_def, radius_of_influence=res*20, neighbours=nn, sigmas=sigmas, fill_value=None, with_uncert = False, nprocs = cpu_count(), epsilon = eps, reduce_data=1) except: r_dat = pyresample.kd_tree.resample_gauss(orig_def, res_grid.flatten(), targ_def, radius_of_influence=res*20, neighbours=nn, sigmas=sigmas, fill_value=None, with_uncert = False, nprocs = 1, epsilon = eps, reduce_data=1) if 'dat' in locals(): complete=1 except: del grid_x, grid_y, targ_def, orig_def humlon, humlat = trans(X, Y, inverse=True) dat, stdev, counts, resg, complete, shape = getgrid_lm(humlon, humlat, merge, res*10, min(X), max(X), min(Y), max(Y), resg*2, mode, trans, nn, wf, sigmas, eps) r_dat, stdev_null, counts_null, resg, complete, shape = getgrid_lm(humlon, humlat, res_grid, res*10, min(X), max(X), min(Y), max(Y), resg*2, mode, trans, nn, wf, sigmas, eps) del humlat, humlon del stdev_null, counts_null humlon, humlat = trans(X, Y, inverse=True) del X, Y, res_grid, merge dat = dat.reshape(shape) dat[dist>res*30] = np.nan del dist r_dat = r_dat.reshape(shape) r_dat[r_dat<1] = 1 r_dat[r_dat > 2*np.pi] = 1 r_dat[np.isnan(dat)] = np.nan dat = dat + r_dat #np.sqrt(np.cos(np.deg2rad(r_dat))) #dat*np.sqrt(r_dat) + dat del r_dat if mode>1: stdev = stdev.reshape(shape) counts = counts.reshape(shape) mask = dat.mask.copy() dat[mask==1] = np.nan #dat[mask==1] = 0 if mode>1: dat[(stdev>numstdevs) & (mask!=0)] = np.nan dat[(counts<nn) & (counts>0)] = np.nan #if dogrid==1: dat[dat==0] = np.nan dat[np.isinf(dat)] = np.nan dat[dat<thres] = np.nan datm = np.ma.masked_invalid(dat) glon, glat = trans(grid_x, grid_y, inverse=True) #del grid_x, grid_y try: from osgeo import gdal,ogr,osr proj = osr.SpatialReference() proj.ImportFromEPSG(int(cs2cs_args.split(':')[-1])) #26949) datout = np.squeeze(np.ma.filled(dat))#.astype('int16') datout[np.isnan(datout)] = -99 driver = gdal.GetDriverByName('GTiff') #rows,cols = np.shape(datout) cols,rows = np.shape(datout) outFile = os.path.normpath(os.path.join(sonpath,'geotiff_map'+str(p)+'.tif')) ds = driver.Create( outFile, rows, cols, 1, gdal.GDT_Float32, [ 'COMPRESS=LZW' ] ) if proj is not None: ds.SetProjection(proj.ExportToWkt()) xmin, ymin, xmax, ymax = [grid_x.min(), grid_y.min(), grid_x.max(), grid_y.max()] xres = (xmax - xmin) / float(rows) yres = (ymax - ymin) / float(cols) geotransform = (xmin, xres, 0, ymax, 0, -yres) ds.SetGeoTransform(geotransform) ss_band = ds.GetRasterBand(1) ss_band.WriteArray(np.flipud(datout)) #datout) ss_band.SetNoDataValue(-99) ss_band.FlushCache() ss_band.ComputeStatistics(False) del ds except: print("error: geotiff could not be created... check your gdal/ogr install") try: # ========================================================= print("creating kmz file ...") ## new way to create kml file pixels = 1024 * 10 fig, ax = humutils.gearth_fig(llcrnrlon=glon.min(), llcrnrlat=glat.min(), urcrnrlon=glon.max(), urcrnrlat=glat.max(), pixels=pixels) cs = ax.pcolormesh(glon, glat, datm, vmax=scalemax, cmap='gray') ax.set_axis_off() fig.savefig(os.path.normpath(os.path.join(sonpath,'map'+str(p)+'.png')), transparent=True, format='png') del fig, ax # ========================================================= fig = plt.figure(figsize=(1.0, 4.0), facecolor=None, frameon=False) ax = fig.add_axes([0.0, 0.05, 0.2, 0.9]) cb = fig.colorbar(cs, cax=ax) cb.set_label('Intensity [dB W]', rotation=-90, color='k', labelpad=20) fig.savefig(os.path.normpath(os.path.join(sonpath,'legend'+str(p)+'.png')), transparent=False, format='png') del fig, ax, cs, cb # ========================================================= humutils.make_kml(llcrnrlon=glon.min(), llcrnrlat=glat.min(), urcrnrlon=glon.max(), urcrnrlat=glat.max(), figs=[os.path.normpath(os.path.join(sonpath,'map'+str(p)+'.png'))], colorbar=os.path.normpath(os.path.join(sonpath,'legend'+str(p)+'.png')), kmzfile=os.path.normpath(os.path.join(sonpath,'GroundOverlay'+str(p)+'.kmz')), name='Sidescan Intensity') except: print("error: map could not be created...") #y1 = np.min(glat)-0.001 #x1 = np.min(glon)-0.001 #y2 = np.max(glat)+0.001 #x2 = np.max(glon)+0.001 print("drawing and printing map ...") fig = plt.figure(frameon=False) map = Basemap(projection='merc', epsg=cs2cs_args.split(':')[1], resolution = 'i', #h #f llcrnrlon=np.min(humlon)-0.001, llcrnrlat=np.min(glat)-0.001, urcrnrlon=np.max(humlon)+0.001, urcrnrlat=np.max(glat)+0.001) try: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='World_Imagery', xpixels=1000, ypixels=None, dpi=300) except: map.arcgisimage(server='http://server.arcgisonline.com/ArcGIS', service='ESRI_Imagery_World_2D', xpixels=1000, ypixels=None, dpi=300) #finally: # print "error: map could not be created..." #if dogrid==1: gx,gy = map.projtran(glon, glat) ax = plt.Axes(fig, [0., 0., 1., 1.], ) ax.set_axis_off() fig.add_axes(ax) #if dogrid==1: if 2>1: if datm.size > 25000000: print("matrix size > 25,000,000 - decimating by factor of 5 for display") map.pcolormesh(gx[::5,::5], gy[::5,::5], datm[::5,::5], cmap='gray', vmin=np.nanmin(datm), vmax=scalemax) #vmax=np.nanmax(datm) else: map.pcolormesh(gx, gy, datm, cmap='gray', vmin=np.nanmin(datm), vmax=scalemax) #vmax=np.nanmax(datm) del datm, dat else: ## draw point cloud x,y = map.projtran(humlon, humlat) map.scatter(x.flatten(), y.flatten(), 0.5, merge.flatten(), cmap='gray', linewidth = '0') #map.drawmapscale(x1+0.001, y1+0.001, x1, y1, 200., units='m', barstyle='fancy', labelstyle='simple', fontcolor='k') #'#F8F8FF') #map.drawparallels(np.arange(y1-0.001, y2+0.001, 0.005),labels=[1,0,0,1], linewidth=0.0, rotation=30, fontsize=8) #map.drawmeridians(np.arange(x1, x2, 0.002),labels=[1,0,0,1], linewidth=0.0, rotation=30, fontsize=8) custom_save2(sonpath,'map_imagery'+str(p)) del fig del humlat, humlon return res #return the new resolution
def run_main(): [superdict,superlista] = IdDictionary.run_main() stations = np.genfromtxt("/home/ns3/Documents/BicingProject/BigData/Process_Data/Prematrix_data.txt", delimiter=';', dtype=[('lat', np.float32), ('lon', np.float32),('id', np.int16)], usecols=(3, 4,0)) m = Basemap(llcrnrlon=2.031819,llcrnrlat=41.334322,urcrnrlon=2.240496,urcrnrlat=41.496240, resolution = 'l',epsg=5520) #http://server.arcgisonline.com/arcgis/rest/services m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 1500, verbose= True) lats=stations['lat'] lons=stations['lon'] ids=stations['id'] altura=[] # convert lat and lon to map projection mx,my=m(lons,lats) pos={} counter=0 for element in ids: pos[element]=(mx[counter],my[counter]) altura.append(float(superdict[str(element)]['alt'])) counter=counter+1 #print counter #print "aaaaaaaaaaa" #print altura # print lats #print ids[0] Tuple=() ListOfEdges =[] ContadorFilas=0 ContadorColumnas=0 G=nx.Graph() #with open("/home/ns3/Documents/BicingProject/BigData/Process_Data/RDD/TrafficMatrix_data_python.txt","r") as fid: with open("/home/ns3/Documents/BicingProject/BigData/Process_Data/AdjacentMatrix_distance_1000.dat", "r") as fid: for line in fid: ContadorColumnas=0 f=line.split(' ') Tamano=len(f) #print Tamano for element in f: Tuple= str(ContadorFilas) + str(ContadorColumnas) #Matrix[ContadorFilas, ContadorColumnas] = element if element=="1": #print "(" + str(ContadorFilas) + "," + str(ContadorColumnas) + ")" index1=ids[ContadorFilas] index2=ids[ContadorColumnas] ListOfEdges.append((index1, index2)) ListOfEdges.append((index1,index2)) index1 = ids[ContadorFilas] index2 = ids[ContadorColumnas] G.add_node(index1) G.add_node(index2) ContadorColumnas=ContadorColumnas+1 ContadorFilas=ContadorFilas+1 for EdgePair in ListOfEdges: #print EdgePair[0] G.add_edge(EdgePair[0],EdgePair[1]) color=[] min_altura=float(min(altura)) max_altura=float(max(altura)) step=(max_altura-min_altura)/5 rango0=min_altura rango1=min_altura+step*1 rango2=min_altura+step*2 rango3=min_altura+step*3 rango4=min_altura+step*4 rango5=max_altura #Mientras el valor sea mas pequeno, sera mas morado. #Mientras el valor de altura sea mas alto, sera mas rojo for element in altura: if element>=rango0 and element<rango1: color.append(10) if element>= rango1 and element<rango2: color.append(30) if element>=rango2 and element<rango3: color.append(50) if element>=rango3 and element<rango4: color.append(70) if element>=rango4 and element<=rango5: color.append(90) print color # draw nx.draw_networkx(G,pos,node_size=150,node_color=color) plt.title('Bicing network classified according to height') plt.show()
clat = np.floor(filtered[0]['origins'][0]['latitude']) m = Basemap(projection='cyl',llcrnrlat=clat - 5,urcrnrlat= clat + 5,\ llcrnrlon= clon-5,urcrnrlon=clon+5,resolution='l') plt.figure(figsize=(8., 8.)) ax = plt.subplot(111) ########### #Earthquakes ########### for event in filtered: fc = event.focal_mechanisms[0] plane = fc.nodal_planes.nodal_plane_1 origin = event.origins[0] depth = origin bb = beach([plane.strike, plane.dip, plane.rake], xy=m(origin.longitude, origin.latitude), width=0.6, linewidth=0.5, facecolor='b') ax.add_collection(bb) m.fillcontinents(alpha=0.5) m.drawparallels(np.arange(-90.,91.,10.), labels=[False,True,True,False]) m.drawmeridians(np.arange(-180.,181.,10.), labels=[True,False,False,True]) #m.etopo(alpha=0.5) m.drawcoastlines(linewidth=0.5) m.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 1500, verbose= True) plt.title("Magnitude 8.3 Offshore Coquimbo, Chile") plt.show()
## Mapas de sismicidad para intervalos de 2 meses en Pto Gaitan from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import numpy as np import glob ## Defining the map map = Basemap(llcrnrlon=-71.7,llcrnrlat=3.64,urcrnrlon=-71.1,urcrnrlat=4.1, projection='cyl', resolution=None) #map.arcgisimage(service='ESRI_StreetMap_World_2D', xpixels = 3000, verbose= True) # Could Be #map.arcgisimage(service='World_Topo_Map', xpixels = 3000, verbose= True) # Could be map.arcgisimage(service='NatGeo_World_Map', xpixels = 1500, verbose= True) # THIS IS THE ONE!!!!! #map.arcgisimage(service='ESRI_Imagery_World_2D', xpixels = 3000, verbose= True) # Too cloudy ## Ploting Wells fil = open('CoordenadasPozos.csv', 'r') cont1, cont2, cont3, cont4, cont5, cont6, cont7, cont9, contq1, contq3, contq4 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 for line in fil: lat_pozos = float(line.split(';')[1]) lon_pozos = float(line.split(';')[0]) #print lon_pozos, lat_pozos pad = line.split(';')[-1] #print pad, type(pad) x, y = map(lon_pozos, lat_pozos) if pad == '1\n': cont1 = cont1 + 1 col = 'b' pad1 = map.scatter(x, y, color=col, marker='^', s=150, edgecolor='black', zorder=50)