3

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
sebastian
  • 31
  • 1
  • 3
  • Maybe [this](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html#accessing-the-data) helps: I.e. what is the output of `temp2m.data`? – dario Feb 11 '20 at 10:05
  • Use cfgrib. It is state of the art and much easier to use. – dl.meteo Feb 12 '20 at 19:19

2 Answers2

3

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()
1

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
BenT
  • 3,172
  • 3
  • 18
  • 38