Results 1 to 5 of 5

Thread: Help reading Grib1 Data (win32)

  1. #1
    Join Date
    Jan 2014
    Location
    SoCal
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help reading Grib1 Data (win32)

    Hi There,


    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!


    -Tom

    Code:
    float GribPower(int s, float n)
    {
    	float divisor = 1.0;
    	while (s < 0)
    	{
    		divisor /= n;
    		s++;
    	}
    	while (s > 0)
    	{
    		divisor *= n;
    		s--;
    	}
    	return divisor;
    }
    
    
    bool Read(HANDLE hFile, int decimalScaleFactor)
    {
    	ReadItem(hFile, m_DataSize, 3);
    	ReadItem(hFile, m_Byte4.m_AsU8);
    	u16 bsf;
    	ReadItem(hFile, bsf);												m_BinaryScaleFactor = bsf & 0x8000 ? -(bsf & ~0x8000) : bsf;
    	ReadItem(hFile, m_RefValue);
    	ReadItem(hFile, m_NumBitsPerDatumPoint);
    
    
    	if (m_Byte4.m_Flags & 0x1)
    	{
    		//Spherical harmonics
    		return false;
    	}
    
    
    	m_iNumValues = (m_DataSize - 12) / 2;	//Check size assumption a bit later
    
    
    	if (m_Byte4.m_NumUnusedBits)
    	{
    		//Not going in here
    		int iCount = m_Byte4.m_NumUnusedBits / 8;
    		while (iCount)
    		{
    			u8 throwIt;
    			ReadItem(hFile, throwIt);
    			--iCount;
    		}
    	}
    
    
    	if (m_iNumValues)
    	{
    		if (m_NumBitsPerDatumPoint != 16)
    		{
    			return false;
    		}
    
    
    		delete[]mp_Values;
    		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++)
    		{
    			u16 iDatum;
    			ReadItem(hFile, iDatum);
    			mp_Values[i] = (m_RefValue + (float(iDatum) * fScale)) * fDecimalScale;;
    		}
    	}
    	return true;
    }

  2. #2
    Join Date
    Dec 2004
    Location
    Centre for Climate Change Research, Indian Institute of Tropical Meteorology, India
    Posts
    438
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Sometimes data comes with scaling factor and offsets. I guess that could be the reason in your case. For example, the metadata above shows some scalefactors. I am not sure if this is the problem, but try doing the "mp_Values[i] = (m_RefValue + (float(iDatum) * fScale)) * fDecimalScale". I am not able to understand which is the variable name though. Some softwares take the scaling and offset into consideration, while some doesn't. Which s/w are you using? If the data is small, can you attach it here?

  3. The Following User Says Thank You to rocksea For This Useful Post:

    ThomasShepherd (18th January 2014)

  4. #3
    Join Date
    Jan 2014
    Location
    SoCal
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi Rocksea, in theory that code is meant to apply the scale and offset. There is no software involved beyond the C++ that I'm writing which is intended to be a highly specialized reader of just the type of GRIB data I need to read.

    I've attached a more complete response that for some reason the website won't let me post....
    Attached Files Attached Files

  5. #4
    Join Date
    Dec 2004
    Location
    Centre for Climate Change Research, Indian Institute of Tropical Meteorology, India
    Posts
    438
    Thanks
    16
    Thanked 5 Times in 5 Posts
    I read your reply, I think it is some reserved variable in the code/text which is preventing you from posting it.

    As far as the file is concerned, I am not able to find out what is missing...!

  6. #5
    Join Date
    Jan 2014
    Location
    SoCal
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Luckily I just realised that wgrib.c (ftp://ftp.cpc.ncep.noaa.gov/wd51we/wgrib/wgrib.c) is pretty standard c and doesn't bring in any odd dependencies like the other code I'd been looking at. Their unpack function (BDS_unpack) does what I want and covers a lot more cases that source data might provide.

    At some point I'll go back and see what I was missing, but for now back to my original intention!

Similar Threads

  1. Reading ancient climate from plankton shells
    By onet_feed in forum Ocean News Feed
    Replies: 0
    Last Post: 25th October 2013, 11:32 PM
  2. reading netcdf in MATLAB
    By deba in forum Oceanographic Applications & Data Analysis
    Replies: 20
    Last Post: 16th November 2009, 10:30 AM
  3. Replies: 0
    Last Post: 19th October 2006, 07:37 PM
  4. Post doc at University of Reading, Last date:
    By praveen in forum Past Opportunities
    Replies: 0
    Last Post: 6th October 2006, 05:01 PM
  5. Replies: 0
    Last Post: 17th January 2006, 04:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •