by Jacob Davis | Updated: 01/06/2016 | Comments: 2
Do you feel lost when sifting through wind direction data that is listed in degrees? For example, can you picture in your mind what direction 195 degrees is? Would it be easier if your data logger could put your data in compass directions instead? In this article, I’ll share with you an easy method for converting your wind direction data from degrees to compass directions using the CRBasic programming language.
If you’re like me, compass directions (north, south, east, and west) are easier to understand than degrees. For example, if someone asks me for driving directions, I don’t say, “Travel five blocks on a 270 degree heading.” It’s easier for me, and the lost driver, if I say instead, “Go west for five blocks."
Similarly, when it comes to describing wind direction, compass points tend to be easier to understand than degrees. Meteorologists use compass points in their weather forecasts because they are easier for people to visualize and remember. For example, a meteorologist might say, “The storm is going to blow in from the west.”
In terms of wind direction, it is both fortunate and unfortunate that data loggers don’t “think” like us humans do.
To make sense out of wind direction data that is in degrees, let’s get started in our conversion process. To convert degrees to compass directions, I first divide the compass into 16 sectors of 22.5 degrees each. The sectors are like slices of pie, centered on the compass points.
Tip: If you would like to use eight sectors instead of 16, with a shorter lookup table, divide by 45 degrees instead of 22.5 degrees.
To do the conversion, I use an indexed array as a lookup table instead of a Case instruction. I prefer the indexed array because the code is faster and more compact than the code of a Case instruction. In addition, an indexed array works well for evenly spaced data bins, such as a set number of degrees. (Data bins are categories for ranges of numbers.) In contrast, a Case instruction is a better choice for applications where your data bins aren’t evenly spaced, such as determining temperature ranges that describe water as hot, warm, or cold. (“Hot” and “cold” values would be larger bins, and “warm” would be a smaller bin.)
Because north on a compass could be described as 0 degrees or 360 degrees, we have to use two data bins for it. This means that, for our 16 compass sectors, we actually need 17 values in our lookup table. The first value and last value are both north. The table below shows the 17 values with the 16 different compass sectors:
Values | Compass Sectors |
1 |
N |
2 |
NNE |
3 |
NE |
4 |
ENE |
5 |
E |
6 |
ESE |
7 |
SE |
8 |
SSE |
9 |
S |
10 |
SSW |
11 |
SW |
12 |
WSW |
13 |
W |
14 |
WNW |
15 |
NW |
16 |
NNW |
17 |
N |
You can use an array in your data logger program as a lookup table. A simple way to fill the array is to assign initial values. Be sure you use type String and enclose each value within quotes. The line of code is a bit long, but it is fairly simple:
Dim Sector(17) As String * 3 = {"N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"}
Now we need to convert wind direction to integer values that correspond with the 17 index values within our array.
To limit wind direction to 360 degrees, we need to perform a modulo operation to find the remainder after dividing the total degrees by 360.
For example, if we are working with 405 degrees, we divide that value by 360. Doing so gives us 1 with a remainder (or modulus) of 45:
In this example, the remainder of 45 degrees is what needs to be converted to a compass direction. |
Note: Although this step isn’t necessary with most sensors, it won’t cause any problems if you include it in your programs.
If we divide the wind direction by 22.5 (degrees for each sector) and round, we get numbers ranging from 0 to 16. Because the labels stored in the array are indexed from 1 to 17, we must add 1 to fit the range:
Index = WindDir MOD 360 Index = Round(Index/ 22.5,0)+1 CompassDir = Sector(Index)
In our previous example, if we take the remainder of 45 degrees and divide it by 22.5, we get a result of 2:
If we add 1, the result is 3:
On our table the value of “3” corresponds to a direction of “NE.” So, our remainder of 45 degrees converts to a direction of northeast. |
We can place the entire math function within the index parameter of the array. The parentheses force the order of operation from the inside out:
CompassDir = Sector(Round((WindDir MOD 360)/ 22.5,0)+1)
Putting all of the pieces together, we get the following data logger program. Just add your measurements and data tables.
'Declare Public Variables Public WindDir As Float Units WindDir = degrees Dim Sector(17) As String * 3 = {"N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N"} Public CompassDir As String * 3 'Main Program BeginProg Scan (1,Sec,0,0) 'Add wind sensor measurement here CompassDir = Sector(Round((WindDir MOD 360)/ 22.5,0)+1) 'Rounds the wind direction out into 17 sectors. Sectors 1 and 17 are both N. NextScan EndProg
In this article, I hope I helped you find your way to better understand, use, and share your wind direction data using some CRBasic programming code. If you have any of our RTMC software products, this software is able to take your wind direction data and directly convert the degrees into a compass display.
Feel free to share situations where you want to report wind direction using compass points. Can you think of similar cases with evenly spaced data bins where you could use this method? Post your ideas below.
Comments
Thiago | 10/31/2018 at 01:11 PM
Nice post.
How do I write the calculate compass data into a Table?
I need to calculate a 30 minute average data from wind sensor e write the compass data from it in a table. I don´t know how to do it.
JDavis | 10/31/2018 at 01:29 PM
The compass sector, being a string variable, can only be saved to a table with the Sample instruction. The difficulty, in your case is the vector average of the wind must be calculated first. It is possible to read values back out of a data table. However, you can't save that compass sector to the same table.
We have worked around limitations before by making a table with just the Windvector. You then read that value back out, and can save it with other data in your main table. GetRecord is the easiest way to read values back out of a table.
Please log in or register to comment.