3

I've got 4 different .txt files for longitude, latitude, wind speed and wind direction (so no U and V components of the wind). Is there any way to plot wind barbs from such a dataset?

Pont
  • 5,429
  • 2
  • 27
  • 43
Stavros Keppas
  • 275
  • 1
  • 3
  • 7
  • 1
    Possible duplicate of https://earthscience.stackexchange.com/questions/7012/plotting-wind-barbs-in-python – Communisty Aug 03 '17 at 09:14
  • If that answer doesn't satisfy you because it is specifically for u,v wind then maybe you should do the transition from speed and direction to u,v first. – Communisty Aug 03 '17 at 09:31
  • 1
  • @Communisty I don't think it's a duplicate. The other question is mainly about how to read the data out of a netCDF file. This question is about how to convert some wind data from polar to Cartesian co-ordinates for plotting (though not phrased in those terms). – Pont Aug 03 '17 at 11:09

2 Answers2

3

The data format you have (magnitude and direction) is usually referred to as polar co-ordinates. The data format you need (horizontal and vertical offsets) is Cartesian co-ordinates. There's plenty of good material on this online. For a friendly introduction, see e.g. this page on "Math is Fun!" (or your favourite high-school maths textbook).

Converting between the two systems involves a bit of simple trigonometry, and the exact formulae depend on how you're measuring the angle in your polar co-ordinates. Mathematicians tend to measure anticlockwise from the positive x axis in radians. Geoscientists usually measure clockwise from the positive y axis in degrees (like a compass bearing):

polar co-ordinates
Source: kartoweb.itc.nl

Assuming that this is the convention used in your data, the conversion formulae are refreshingly straighforward.

$x = d\sin(\alpha)$
$y = d\cos(\alpha)$

One thing to watch out for: like most programming languages, Python uses radians, not degrees, for its trigonometric functions. If your angles are in degrees, don't forget to run them through the math.radians() function before taking sines and cosines!

Incidentally, having each measurement in a separate text-file is probably a recipe for inconvenience and confusion. If I were you, I'd have "combine measurements as columns in a single file" as the first step in my processing workflow.

Pont
  • 5,429
  • 2
  • 27
  • 43
3

I can answer this in two steps

  1. Calculate $u$ and $v$. This site has formulas for $u$ and $v$. They are as follows: $$u=-|\vec{v}|\sin(\frac{\pi}{180}\phi)$$ and $$v=-|\vec{v}|\cos(\frac{\pi}{180}\phi)$$ where $\phi$ is the wind direction in the meteorological system, and $|\vec{v}|$ is the wind speed.

  2. Having calculated $u$ and $v$, you can plot the wind barbs by following the example found at this site.

BarocliniCplusplus
  • 8,799
  • 16
  • 42
  • Thank you for that. In addition to my previous question. Having lat, lon, u, v wind txt files, how could I use matplotlib in order to plot the wind barbs on a map (let's say UK map). I read the links that you gave me, but as I am newbie in python I can't find the way. – Stavros Keppas Aug 11 '17 at 08:30
  • Sure. In addition to those links, try looking here: https://matplotlib.org/basemap/users/examples.html. The idea is to make a basemap with whatever projection & parameters you want, use it to convert to longitude & latitude to x & y, and plot the barbs on the basemap. – BarocliniCplusplus Aug 11 '17 at 11:37
  • I finaly found the solution, doing sth like that:

    m = Basemap(projection='merc',llcrnrlat=33,urcrnrlat=43,
    llcrnrlon=18,urcrnrlon=30,lat_ts=20,resolution='i') jet = plt.cm.get_cmap('jet') x,y = m(lon, lat) barbs = m.barbs(x,y,wind_u,wind_v,wind_spd_knots, cmap=jet,length=7,linewidth=0.8,pivot='middle')

    However I can't find how to set limints on the colorscale for example like setting vmin, vmax. Any ideas on that?

    – Stavros Keppas Aug 11 '17 at 14:55
  • Did you try levels= , such as what contour and contourf use? – BarocliniCplusplus Aug 11 '17 at 16:24
  • No. Could you explain that please? – Stavros Keppas Aug 11 '17 at 18:53
  • So levels=np.arange(0,5) for example, will make contours at 0-4 in increments of 1. That is generally the other option to vmin and vmax. However, I am unsure of why you are trying to color the barbs. If that doesn't work, you could devise a brute force way of coloring the wind barbs. – BarocliniCplusplus Aug 11 '17 at 19:14
  • It seems to be quite random. I am not sure how it works. I guess the problem is that in this command:

    barbs = m.barbs(x,y,wind_u,wind_v, lelvels ,length=7,linewidth=0.8,pivot='middle')

    there are two lists instead of just one, wind_u and wind_v. So the colorscale can't refer to the combination of u and v winds.

    – Stavros Keppas Aug 11 '17 at 19:50
  • After looking through the demo (provided in the answer) I found a couple of different things. levels likely won't work. Try using things like barb_increments and barbcolor as seen in the example: https://matplotlib.org/examples/pylab_examples/barb_demo.html. – BarocliniCplusplus Aug 11 '17 at 20:31
  • It seems that barb_increments sets the shape od wind barbs for specific values. Barbcolors cannot set specific colors for specific value. – Stavros Keppas Aug 11 '17 at 20:43
  • barb_increments is a dictionary that tells Python what the different flags and barbs mean. I am unsure what the colors represent. You may want to play around with that, or just set one standard color. – BarocliniCplusplus Aug 11 '17 at 21:20
  • 1
    Finally I found it. Using clim=[0,50], I set the range between 0 and 50 knots. the very last thing is how to plot the colorbar. plt.colorbar doesn't work.

    I also found an alternative way by plotting arrows: sc=m.quiver(x,y,wind_u,wind_v, wind_spd_knots,cmap=jet,clim=[0,50])

    – Stavros Keppas Aug 11 '17 at 21:28
  • @BarocliniCplusplus What is the equation for calculating meteorological wind angle from latitude and longitude ? –  Sep 05 '21 at 04:55
  • @gansub What do you mean? Latitude and longitude are static variables, whereas wind angle changes with time. The meteorological wind angle would be what is reported on a METAR, and the mathematical wind angle would be $\tan^{-1}\left(\frac{v}{u}\right)$. If you follow the website, it shows the equations in more detail. – BarocliniCplusplus Sep 06 '21 at 21:43
  • @BarocliniCplusplus So the question should perhaps be edited ? There is no use for latitude and longitude then. OP should mention meteorological wind angle as input ? –  Sep 07 '21 at 01:32