Search code examples
pythonpython-xarraygribcfgribndfd

NDFD GRIB2 how to fix mirrored data when using xarray


My code code for pulling in a grib file of windspeeds in New England:

import pandas as pd
import numpy as np

import requests
import cfgrib
import xarray as xr

resp = requests.get('https://tgftp.nws.noaa.gov/SL.us008001/ST.opnl/DF.gr2/DC.ndfd/AR.neast/VP.001-003/ds.wspd.bin', stream=True)

f = open('..\\001_003wspd.grib2', 'wb')
f.write(resp1.content)
f.close()

xr_set = xr.load_dataset('..\\001_003wspd.grib2', engine='cfgrib')

xr_set.si10[0].plot(cmap=matplotlib.pyplot.cm.coolwarm)

This gives: Grib2 pyplot

As you can see, it is mirrored every other line east to west. Maine is the most obvious.


Solution

  • I believe this is not a code problem, but rather the file which wasn't written correctly. If you take only one line every two lines, you get a correct map :

    import numpy as np
    import requests
    import xarray as xr
    from fs.tempfs import TempFS
    
    resp = requests.get('https://tgftp.nws.noaa.gov/SL.us008001/ST.opnl/DF.gr2/DC.ndfd/AR.neast/VP.001-003/ds.wspd.bin', stream=True)
    
    with TempFS() as tempfs:
        path = tempfs.getsyspath("001_003wspd.grib2")
        f = open(path, 'wb')
        f.write(resp.content)
        f.close()
    
        ds = xr.load_dataset(path, engine='cfgrib')
        ds = ds.isel(y=np.arange(len(ds.y))[1::2])
    
    ds.si10.isel(step=15).plot(cmap="coolwarm", x='longitude', y='latitude')
    

    xarray_plot