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.
I used Anaconda's virtual environment.
As the article title suggests, use pygrib. Can be installed from pip or conda.
conda install -c conda-forge pygrib
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
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)
pygrib.gribmessage
will be described later.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
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 |
forecastTime
has a unit of [hour] in GSM and MSM, but a unit of [minute] in LFM.ParameterNumber
should be used in combination with parameterCategory
The pygrib.gribmessage instance extracted above has ** data of all meshes of a certain time / data item ** It is included.
Variable name | Mold | Contents |
---|---|---|
validDate | datetime.datetime | Forecast date and time(UTC) |
analDate | datetime.datetime | Forecast analysis reference date and time(UTC) |
forecastTime | int | analDate FromvalidDate Time 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) |
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.
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]]))
Recommended Posts