'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 1:[1]

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

Solution 2:[2]

The problem is related to how the grib files are ordered. If you perform the following command you can see the order.

$wgrib2 -grid in.grib2

You will likely see something like, "(2345 x 1597) input WE|EW:SN output WE:SN"

The WE|EW:SN need to be changed to WE:SN. To change the ordering do the following command

wgrib2 in.grib2 -ijsmall_grib 1:2345 1:1597 out.grib2

Now you will see that out.grib2 has the correct WE:SN ordering and can work with xarray

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cyril
Solution 2 Travis