Dave
Dave

Reputation: 1208

How to plot gridded data in basemap and find grid points along a specific azimuth

Update: I am trying to map some data. I have a set of measured back-azimuths (baz) from a reference point in a grid. I want to find all points on the grid that a great circle along the baz would cross. To do this I iterate through each point in the grid, calculate expected back-azimuth between that point and the reference point and compare to each measured baz. If the difference between the two is small (less than 2 degrees) I weight that point. I then put it all on a map. The code I use is below but the results look a bit strange, does anyone know where I have gone wrong, or if there is a better approach (faster) then what I have done??

from matplotlib.colorbar import ColorbarBase
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
import mpl_toolkits.basemap.pyproj as pyproj


llcrnrlon = -30.0
llcrnrlat = 45.0
urcrnrlon = 0.0
urcrnrlat = 65.0
lon_0 = (urcrnrlon + llcrnrlon) / 2.
lat_0 = (urcrnrlat + llcrnrlat) / 2.

lat = 51.58661577  # reference point
lon = -9.18822525


# Generate random back-azimuths.
baz = zeros((20))
for i in xrange(len(baz)):
  baz[i] = random.randint(200,230)


####################################################################
## Set up the map background.
m = Basemap(llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat,urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat,
    resolution='i',projection='lcc',lon_0=lon_0,lat_0=lat_0)
m.drawcoastlines()
m.fillcontinents() 

# draw parallels
m.drawparallels(np.arange(10,70,10),labels=[1,0,0,0])
# draw meridians
m.drawmeridians(np.arange(-80, 25, 10),labels=[0,0,0,1])

# Plot station locations.
x, y = m(lon, lat)            # array ref points
m.plot(x,y,'ro', ms=5)


####################################################################
## Set up the grids etc.
glons = np.linspace(llcrnrlon, urcrnrlon, 100)
glats = np.linspace(llcrnrlat, urcrnrlat, 100)

# Convert to map coords.
xlons, ylats = m(glons, glats)

# create grid for pcolormesh.
grid_lon, grid_lat = np.meshgrid(xlons, ylats)

# create weights for pcolormesh.
weights = np.zeros(np.shape(grid_lon))

# create grid of lat-lon coords for baz calculation.
gln, glt = np.meshgrid(glons, glats)


####################################################################
## calculate baz from grid_lon, grid_lat to lon, lat. If less 
## than error weight grid point.

# method for BAZ calculation via pyproj.
def get_baz(lon1, lat1, lon2, lat2):
  g = pyproj.Geod(ellps='WGS84')
  az, baz, dist = g.inv(lon1, lat1, lon2, lat2)
  return baz

# BAZ calcultion for each point in grid.
ll=0
for mBAZ in baz:
  for i in xrange(len(gln)):
    for k in xrange(len(gln[i])):
      nbaz = get_baz(lon, lat, gln[i][k], glt[i][k])
      nbaz += 180
      if abs(nbaz - mBAZ) < 2:
    weights[i][k] = 1
  ll+=1


# plot grid.
m.pcolormesh(grid_lon, grid_lat, weights, cmap=plt.cm.YlOrBr)
plt.colorbar()
plt.show()

Original question below, out of date now.

I am trying to map some data. I have a dataset that gives a range of values (frequencies) for each direction. I want to plot them on a grid so each grid point along a particular azimuth is weighted by the power for a particular frequency. I have created a map with basemap and plotted a grid over it as follows,

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
from shoot import *

llcrnrlon = -20.0
llcrnrlat = 45.0
urcrnrlon = 10.0
urcrnrlat = 65.0
lon_0 = (urcrnrlon + llcrnrlon) / 2.
lat_0 = (urcrnrlat + llcrnrlat) / 2.

m = Basemap(llcrnrlon=llcrnrlon,llcrnrlat=llcrnrlat,urcrnrlon=urcrnrlon,urcrnrlat=urcrnrlat,
        resolution='i',projection='lcc',lon_0=lon_0,lat_0=lat_0)

## Set up the grid.
glons = np.linspace(-20,10,50)
glats = np.linspace(45, 65, 50)
xlons, ylats = m(glons, glats)
grid_lon, grid_lat = np.meshgrid(xlons, ylats) 
pwr = np.zeros((50,50))

m.drawcoastlines()
m.fillcontinents() 

# draw parallels
m.drawparallels(np.arange(10,70,10),labels=[1,0,0,0])
# draw meridians
m.drawmeridians(np.arange(-80, 25, 10),labels=[0,0,0,1])

lats = [54.8639587, 51.5641564]
lons = [-8.1778180, -9.2754284]

x, y = m(lons, lats)            # array ref points

# Plot station locations.
m.plot(x,y,'ro', ms=5)
m.pcolormesh(grid_lon, grid_lat, pwr)

then I shoot out the great circle I want using some functions I found at this nice site

glon1 = lons[0]
glat1 = lats[0]
azimuth = 280.
maxdist = 200.
great(m, glon1, glat1, azimuth, color='orange', lw=2.0)
plt.show()

However, plotting the line is not enough, I want to be able to find the grid points that the great circle crosses so I can assign a value to them. Does anyone know how to go about this??

Upvotes: 0

Views: 1937

Answers (1)

nit
nit

Reputation: 113

Can you specify which crossing point do you mean? Running your code returns only one line ...

enter image description here

Upvotes: 0

Related Questions