I'm trying to read Grib1 wind data downloaded from Grib.us. What I've looked at so far comes through as a simple long/lat grid with 16bit values and a float reference offset. This is part of an experiment to plot efficient sailing routes based on a combination of boat performance and weather predictions.
Only problem is I'm getting the wrong values. They bear some resemblance to the original data with wind patterns recognizable but it looks like I've got the powers or the reference values wrong I just don't understand how. Final results have the wrong magnitudes / directions but seem to follow the right patterns (and u/v have independent offsets from correct).
I didn't intend on writing a decoder but didn't find code / libs I could easily include in a win32 program. So here I am.
Basic decoding of the binary data section is below - any hints or tips or pointers towards someone who can help appreciated!
float GribPower(int s, float n)
float divisor = 1.0;
while (s < 0)
divisor /= n;
while (s > 0)
divisor *= n;
bool Read(HANDLE hFile, int decimalScaleFactor)
ReadItem(hFile, m_DataSize, 3);
ReadItem(hFile, bsf); m_BinaryScaleFactor = bsf & 0x8000 ? -(bsf & ~0x8000) : bsf;
if (m_Byte4.m_Flags & 0x1)
m_iNumValues = (m_DataSize - 12) / 2; //Check size assumption a bit later
//Not going in here
int iCount = m_Byte4.m_NumUnusedBits / 8;
if (m_NumBitsPerDatumPoint != 16)
mp_Values = new float[m_iNumValues];
//Y * 10^D = R + (X * 2^E)
float fScale = GribPower(m_BinaryScaleFactor, 2.0f);
float fDecimalScale = GribPower(-decimalScaleFactor, 10.0f);
for (int i = 0; i < m_iNumValues; i++)
mp_Values[i] = (m_RefValue + (float(iDatum) * fScale)) * fDecimalScale;;