[PYTHON] Read the GRIB2 file of the Japan Meteorological Agency with pygrib

Introduction

Here, the following files in List of sample data of Japan Meteorological Agency are targeted.

--Global Numerical Weather Prediction Model GPV (GSM) --Meso Numerical Weather Prediction Model GPV (MSM) --Local Numerical Weather Prediction Model GPV (LFM)

Both adopt a binary format called GRIB2.

Verification environment

I used Anaconda's virtual environment.

Library installation

As the article title suggests, use pygrib. Can be installed from pip or conda.

conda install -c conda-forge pygrib

Implementation example

Obtain the temperature [℃] of Jingu Stadium (35.6745, 139.7169) (closest mesh point) at each time (JST) from the sample file (MSM) of the Japan Meteorological Agency.

from datetime import timedelta
import pygrib
import pandas as pd

time_diff = timedelta(hours=9)

gpv_file = pygrib.open("Z__C_RJTD_20171205000000_MSM_GPV_Rjp_Lsurf_FH00-15_grib2.bin")
t_messages = gpv_file.select(name="Temperature")

df = pd.DataFrame({
    "validDate": [msg.validDate + time_diff for msg in t_messages],
    "temperature": [
        msg.data(
            lat1=35.6745-0.025,
            lat2=35.6745+0.025,
            lon1=139.7169-0.03125,
            lon2=139.7169+0.03125,
        )[0][0][0] - 273.15 for msg in t_messages
    ]
})

print(df)

↓ Output result

             validDate  temperature
0  2017-12-05 09:00:00     9.810709
1  2017-12-05 10:00:00    11.409219
2  2017-12-05 11:00:00    12.358789
3  2017-12-05 12:00:00    13.116861
4  2017-12-05 13:00:00    13.770517
5  2017-12-05 14:00:00    14.698541
6  2017-12-05 15:00:00    14.488687
7  2017-12-05 16:00:00    13.063196
8  2017-12-05 17:00:00    11.467722
9  2017-12-05 18:00:00    10.320886
10 2017-12-05 19:00:00     9.592111
11 2017-12-05 20:00:00     8.797952
12 2017-12-05 21:00:00     8.171686
13 2017-12-05 22:00:00     7.832407
14 2017-12-05 23:00:00     7.461450
15 2017-12-06 00:00:00     6.884409

Implementation explanation

File connection

pygrib.open Create an interface with the file using the class.

import pygrib

gpv_file = pygrib.open("Z__C_RJTD_20171205000000_MSM_GPV_Rjp_Lsurf_FH00-15_grib2.bin")

--The pygrib.open instance itself is an iterator for the pygrib.gribmessage instance. --Can be used as an interface to extract specific information contained in a file as a pygrib.gribmessage instance or its list (reusable)

message method

Takes the argument N (int) and returns the Nth (1 origin) pygrib.gribmessage instance on the iterator.

In[]


gpv_file.message(2)

Out[]


2:Surface pressure:Pa (instant):regular_ll:surface:level 0:fcst time 0 hrs:from 201712050000

select method

It takes an argument in the form ** kwargs and returns a list of pygrib.gribmessage that matches that condition.

If multiple search conditions are specified, they are evaluated as AND conditions.

--1 condition

In[]


gpv_file.select(name="Temperature")

Out[]


[5:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 0 hrs:from 201712050000,
 15:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 1 hrs:from 201712050000,
 27:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 2 hrs:from 201712050000,
 39:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 3 hrs:from 201712050000,
 51:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 4 hrs:from 201712050000,
 63:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 5 hrs:from 201712050000,
 75:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 6 hrs:from 201712050000,
 87:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 7 hrs:from 201712050000,
 99:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 8 hrs:from 201712050000,
 111:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 9 hrs:from 201712050000,
 123:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 10 hrs:from 201712050000,
 135:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 11 hrs:from 201712050000,
 147:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 12 hrs:from 201712050000,
 159:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 13 hrs:from 201712050000,
 171:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 14 hrs:from 201712050000,
 183:Temperature:K (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 15 hrs:from 201712050000]

--2 conditions

In[]


gpv_file.select(shortName="r", forecastTime=3)

Out[]


[40:Relative humidity:% (instant):regular_ll:heightAboveGround:level 1.5 m:fcst time 3 hrs:from 201712050000]

The items that can be realistically used for search conditions are as follows.

item name Input type
name str
shortName str
forecastTime(※) int
validDate datetime.datetime
parameterCategory int
parameterNumber(※※) int

Data value acquisition

The pygrib.gribmessage instance extracted above has ** data of all meshes of a certain time / data item ** It is included.

Main variables

Variable name Mold Contents
validDate datetime.datetime Forecast date and time(UTC)
analDate datetime.datetime Forecast analysis reference date and time(UTC)
forecastTime int analDateFromvalidDateTime difference to
distinctLatitudes numpy.ndarray Latitude list of mesh(1D)
distinctLongitudes numpy.ndarray Longitude list of mesh(1D)
values numpy.ndarray List of data values for all meshes(2D)

latlons method

The latitude and longitude of the mesh are listed for each mesh and returned. Both return types are numpy.ndarray.

In[]


message = gpv_file.message(2)
lats, lons = message.latlons()

Out[]


# lats
array([[47.6 , 47.6 , 47.6 , ..., 47.6 , 47.6 , 47.6 ],
       [47.55, 47.55, 47.55, ..., 47.55, 47.55, 47.55],
       [47.5 , 47.5 , 47.5 , ..., 47.5 , 47.5 , 47.5 ],
       ...,
       [22.5 , 22.5 , 22.5 , ..., 22.5 , 22.5 , 22.5 ],
       [22.45, 22.45, 22.45, ..., 22.45, 22.45, 22.45],
       [22.4 , 22.4 , 22.4 , ..., 22.4 , 22.4 , 22.4 ]])

# lons
array([[120.    , 120.0625, 120.125 , ..., 149.875 , 149.9375, 150.    ],
       [120.    , 120.0625, 120.125 , ..., 149.875 , 149.9375, 150.    ],
       [120.    , 120.0625, 120.125 , ..., 149.875 , 149.9375, 150.    ],
       ...,
       [120.    , 120.0625, 120.125 , ..., 149.875 , 149.9375, 150.    ],
       [120.    , 120.0625, 120.125 , ..., 149.875 , 149.9375, 150.    ],
       [120.    , 120.0625, 120.125 , ..., 149.875 , 149.9375, 150.    ]])

It seems that the order is such that the case where it is written as a matrix and the case where it is displayed on a map visually match.

data method

Get the data value of the mesh within the range of the arguments lat1 (latitude minimum), lat2 (latitude maximum), lon1 (longitude minimum), lon2 (longitude maximum) and the latitude / longitude of that mesh. To do. The return value is a tuple of mesh-like 2D numpy.ndarray for each data value, latitude, and longitude.

In[]


message.data(lat1=22.45, lat2=22.55, lon1=120.0625, lon2=120.1875)

Out[]


(array([[101985.52856445, 101985.52856445, 101985.52856445],
        [101985.52856445, 101985.52856445, 101985.52856445]]),
 array([[22.5 , 22.5 , 22.5 ],
        [22.45, 22.45, 22.45]]),
 array([[120.0625, 120.125 , 120.1875],
        [120.0625, 120.125 , 120.1875]]))

reference

Recommended Posts

Read the GRIB2 file of the Japan Meteorological Agency with pygrib
Download the wind data of the Japan Meteorological Agency
Convert GRIB of Japan Meteorological Agency radar polar coordinates GPV to netCDF (CF / Radial standard)
Download the wind data of the Japan Meteorological Agency
Read the GRIB2 file of the Japan Meteorological Agency with pygrib
Check the existence of the file with python
Let's read the RINEX file with Python ①
Convert the character code of the file with Python3
Scraping the rainfall data of the Japan Meteorological Agency and displaying it on M5Stack
Template of python script to read the contents of the file
Read the power of the smart meter with M5StickC (BP35C0-J11-T01)
Read the csv file with jupyter notebook and write the graph on top of it
A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it
[Python] Read the csv file and display the figure with matplotlib
Read the VTK file and display the color map with jupyter.
Convert GRIB of Japan Meteorological Agency radar polar coordinates GPV to netCDF (CF / Radial standard)
Get information from the Japan Meteorological Agency and notify Slack of weather warnings in the 23 wards of Tokyo
Process the contents of the file in order with a shell script
How to make a command to read the configuration file with pyramid
Scraping and tabulating weather warnings and warnings nationwide from the Japan Meteorological Agency
Format the CSV file of "National Holiday" of the Cabinet Office with pandas
Read a file in Python with a relative path from the program
The idea of feeding the config file with a python file instead of yaml
Extract the xz file with python
Follow the file hierarchy with fts
Download the file deployed with appcfg.py
The story of the "hole" in the file
Open the file with the default app
[Image recognition] How to read the result of automatic annotation with VoTT
Edit the file of the SSH connection destination server on the server with VS Code
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Open an Excel file in Python and color the map of Japan
Display the status of COVID 19 infection in Japan with Splunk (GitHub version)
Read the file with python and delete the line breaks [Notes on reading the file]
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
[Implementation example] Read the file line by line with Cython (Python) from the last line
Align the size of the colorbar with matplotlib
Read the file line by line in Python
Read the file line by line in Python
Read the file by specifying the character code.
The third night of the loop with for
Read all the contents of proc / [pid]
I read the implementation of golang channel
The second night of the loop with for
Try rewriting the file with the less command
[Python] Get the character code of the file
Check the file size with du -sh *
Read the implementation of ARM global timer
Read a character data file with numpy
Download the file with PHP [Under construction]
[Python] Read the specified line in the file
Count the number of characters with echo
[Automation] Read mail (msg file) with Python
[Python3] Understand the basics of file operations
Various ways to read the last line of a csv file in Python
[Bash] While read, pass the contents of the file to variables for each column
Replace the named entity in the read text file with a label (using GiNZA)
I made a plug-in from the Japan Meteorological Agency GPV to easily create an animated contour diagram with QGIS.