Search code examples
pythonpython-xarraygrib

How do I resample a high-resolution GRIB grid to a coarser resolution using xESMF?


I'm trying to resample a set of GRIB2 arrays at 0.25 degree resolution to a coarser 0.5 degree resolution using the xESMF package (xarray's coarsen method does not work here because there is an odd number of coordinates in the latitude).

I have converted the GRIB data to xarray format through the pygrib package, which then subsets out the specific grid I need:

fhr = 96

gridDefs = {
    "0.25": 
        {'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/landsfc.pgrb2.0p25"},
    "0.5": 
        {'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/landsfc.pgrb2.0p50"},
}

fileDefs = {
    "0.25":
        {'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/GEFSv12/reforecast/2019/2019051900/c00/Days%3A1-10/tmp_pres_2019051900_c00.grib2",
         'localfile': "tmp_pres.grib2"},
    "0.5":
        {'url': "https://noaa-gefs-retrospective.s3.amazonaws.com/GEFSv12/reforecast/2019/2019051900/c00/Days%3A1-10/tmp_pres_abv700mb_2019051900_c00.grib2",
         'localfile': "tmp_pres_abv_700.grib2"},
}

def grib_to_xs(grib, vName):
    arr = xr.DataArray(grib.values)
    arr = arr.rename({'dim_0':'lat', 'dim_1':'lon'})
    xs = arr.to_dataset(name=vName)
    return xs

gribs = {}

for key, item in gridDefs.items():
    if not os.path.exists(item['url'][item['url'].rfind('/')+1:]):
        os.system("wget " + item['url'])
    lsGrib = pygrib.open(item['url'][item['url'].rfind('/')+1:])
    landsea = lsGrib[1].values
  
    gLats = lsGrib[1]["distinctLatitudes"]
    gLons = lsGrib[1]["distinctLongitudes"]
    
    gribs["dataset" + key] = xr.Dataset({'lat': gLats, 'lon': gLons})

    lsGrib.close()

for key, item in fileDefs.items():
    if not os.path.exists(item['localfile']):
        os.system("wget " + item['url'])
        os.system("mv " + item['url'][item['url'].rfind('/')+1:] + " " + item['localfile'])

for key, item in fileDefs.items():        
    hold = pygrib.open(item['localfile'])
    subsel = hold.select(forecastTime=fhr)
    
    #Grab the first item
    gribs[key] = grib_to_xs(subsel[1], "TT" + key)
    
    hold.close()

The above code downloads two constant files (landsfc) at the two grid domains (0.25 and 0.5), then downloads two GRIB files at each of the resolutions as well. I'm trying to resample the 0.25 degree GRIB file (tmp_pres.grib2) to a 0.5 degree domain as such:

regridder = xe.Regridder(ds, gribs['dataset0.5'], 'bilinear')
print(regridder)
ds2 = regridder(ds)

My issue is that I generate two warning messages when trying to use the regridder:

/media/robert/HDD/Anaconda3/envs/wrf-work/lib/python3.8/site-packages/xarray/core/dataarray.py:682: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  return key in self.data
/media/robert/HDD/Anaconda3/envs/wrf-work/lib/python3.8/site-packages/xesmf/backend.py:53: UserWarning: Latitude is outside of [-90, 90]
  warnings.warn('Latitude is outside of [-90, 90]')

The output xarray does have the correct coordinates, however the values inside the grid are way off (Outside the maxima/minima of the finer resolution grid), and exhibit these strange banding patterns that make no physical sense.

What I would like to know is, is this the correct process to upscale an array using xEMSF, and if not, how would I address the problem?

Any assistance would be appreciated, thanks!


Solution

  • I would recommend first trying conservative instead of bilinear (it's recommended on their documentation) and maybe check if you're using the parameters correctly because it seems something is wrong, my first guess would be that something you're doing moves the latitud around for some reason, I'm leaving the docs link here and hope someone knows more.

    Regridder docs: https://xesmf.readthedocs.io/en/latest/user_api.html?highlight=regridder#xesmf.frontend.Regridder.__init__

    Upscaling recommendation (search for upscaling, there's also a guide for increasing resolution): https://xesmf.readthedocs.io/en/latest/notebooks/Compare_algorithms.html?highlight=upscaling