0

I'm trying to plot a subset of a field from a grib file on google colab. The issue I am finding is that due to google colab using an older version of python I can't get enough libraries to work together to 1.) get a field from the grib file and then 2.) extract a subset of that field by lat/lon, and then 3.) be able to plot with matplotlib/cartopy.

I've been able to do each of the above steps on my own PC and there are numerous answers on this forum already that work away from colab, so the issue is related to making it work on the colab environment, which uses python 3.7.

For simplicity, here are some assumptions that could be made for anybody who wants to help.

1.) Use this file, since its been what I have been trying to use:

https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.20221113/conus/hrrr.t18z.wrfnatf00.grib2

2.) You could use any field, but I've been extracting this one (output from pygrib): 14:Temperature:K (instant):lambert:hybrid:level 1:fcst time 0 hrs:from 202211131800

3.) You can get this data in zarr format from AWS, but the grib format uploads to the AWS database faster so I need to use it.

Here are some notes on what I've tried:

Downloading the data isn't an issue, it's mostly relating to extracting the data (by lat lon) that is the main issue. I've tried using condacolab or pip to download pygrib, pupygrib, pinio, or cfgrib. I can then use these to download the data above.

I could never get pupygrib or pinio to even download correctly. Cfgrib I was able to get it to work with conda, but then xarray fails when trying to extract fields due to a library conflict. Pygrib worked the best, I was able to extract fields from the grib file. However, the function grb.data(lat1=30,lat2=40,lon1=-100,lon2-90) fails. It dumps the data into 1d arrays instead of 2d as it is supposed to per the documentation found here: https://jswhit.github.io/pygrib/api.html#example-usage

Here is some code I used for the pygrib set up in case that is useful:


!pip install pyproj
!pip install pygrib

# Uninstall existing shapely
!pip uninstall --yes shapely

!apt-get install -qq libgdal-dev libgeos-dev
!pip install shapely --no-binary shapely
!pip install cartopy==0.19.0.post1
!pip install metpy
!pip install wget
!pip install s3fs

import time
from matplotlib import pyplot as plt
import numpy as np
import scipy
import pygrib
import fsspec
import xarray as xr
import metpy.calc as mpcalc
from metpy.interpolate import cross_section
from metpy.units import units
from metpy.plots import USCOUNTIES
import cartopy.crs as ccrs
import cartopy.feature as cfeature

!wget https://noaa-hrrr-bdp-pds.s3.amazonaws.com/hrrr.20221113/conus/hrrr.t18z.wrfnatf00.grib2

grbs = pygrib.open('/content/hrrr.t18z.wrfnatf00.grib2')

grb2 = grbs.message(1) 
data, lats, lons = grb2.data(lat1=30,lat2=40,lon1=-100,lon2=-90)

data.shape

This will output a 1d array for data, or lats and lons. That is as far as I can get here because existing options like meshgrib don't work on big datasets (I tried it).

The other option is to get data this way:

grb_t = grbs.select(name='Temperature')[0]

This is plottable, but I don't know of a way to extract a subset of the data from here using lat/lons.

If you can help, feel free to ask me anything I can add more details, but since I've tried like 10 different ways probably no sense in adding every failure. Really, I am open to any way to accomplish this task. Thank you.

  • probably the solution may help you. convert data in 3 dimension together with lat and lon (25x25) lat1=np.reshape(lats,(np.size(lats))) lon1=np.reshape(lons,(np.size(lons))) parameter1=np.reshape(data,(np.size(data))) #### KEEP AND SORTING THE OUTPUT FRAME data2=np.zeros((np.size(lon1),3)) data2[:,0]=lat1 data2[:,1]=lon1 data2[:,2]=parameter1 np.savetxt('Extract-GRIB.dat', data2, fmt='%9.3f') – Azam Jan 02 '23 at 03:06

0 Answers0