sebastian
sebastian

Reputation: 31

How to retrieve from lat, lon value using pygrib

I'm starting in python programming and I would like to make a small script which displays the data of "Local temperature diagnosed at 2m height above the relief", "Zonal component of the west-east horizontal wind diagnosed at 10 m height" and the "Meridian component of the horizontal wind diagnosed at 10 m height" as a function of longitude and latitude.

For this, I download a file from the open data site of Meteofrance OPEN DATA by selecting:

Domain: "France - 0.01 °", Sub Package: "SP1 - Current surface parameters", Deadline group "0h" and Run date "2020-02-10 00 UTC"

So I have a file in grib2 format that I am trying to process with the pygrib library

To start simple, I'm just trying to get the temperature for a given point (longitude = 0.25, latitude = 49.21)

I created an index to read the file (It seems that it is the fastest)

indx = pygrib.index('./AROME_0.01_SP1_00H_2020021000.grib2', 'typeOfLevel', 'level', 'name')

I select the records which correspond to the temperature and I recover the values:

msg = indx.select(level=2, typeOfLevel="heightAboveGround", name="2 metre temperature")
temp2m = msg[0].values

The problem is that from there, I fumble and I don't find how to retrieve from this variable (numpy.ma.core.MaskedArray) the value that corresponds to my longitude = 0.25 and latitude = 49.21

If someone has an idea, I'm interested

import pygrib
indx = pygrib.index('./AROME_0.01_SP1_00H_2020021000.grib2', 'typeOfLevel', 'level', 'name') 
msg = indx.select(level=2, typeOfLevel="heightAboveGround", name="2 metre temperature")
temp2m = msg[0].values

Upvotes: 3

Views: 4274

Answers (2)

HumphreysCarter
HumphreysCarter

Reputation: 43

You could use the pygrib.data() method to create a bounding box around your lat/lon and then take the mean of the values within that box. Just adjust the tolerance to control how big the box is.

lat, lon, tolerence = 49.21, 0.25, 0.1
data, lats, lons = grb.data(lat1=lat-tolerence,lat2=lat+tolerence,
                            lon1=lon-tolerence,lon2=lon+tolerence)
data.mean()

Upvotes: 3

BenT
BenT

Reputation: 3200

You should be able to get your lat and lon by using .latlons() from your variable that you selected (not the values). From there you can get your index corresponding your point.

import pygrib
indx = pygrib.index('./AROME_0.01_SP1_00H_2020021000.grib2', 'typeOfLevel', 'level', 'name') 
msg = indx.select(level=2, typeOfLevel="heightAboveGround", name="2 metre temperature")

lat,lon = msg.latlons()

temp2m = msg[0].values

Upvotes: 1

Related Questions