Reading files¶
[1]:
import earthkit.data as ekd
First, we ensure the example files used for this notebook are available.
[2]:
ekd.download_example_file(["test.grib", "test4.grib", "test6.grib", "test.nc"])
Files can be read by from_source as a “file” source. The resulting object provides some details about the data and the types it can be converted to for further work.
[3]:
# grib data
ds_in = ekd.from_source("file", "test.grib")
ds_in
[3]:
GRIB file
| path | test.grib |
| size | 720 |
| types | fieldlist, pandas, xarray, numpy, array |
[4]:
ds = ds_in.to_fieldlist().ls()
[5]:
# netcdf data
ds_in = ekd.from_source("file", "test.nc")
ds_in
[5]:
NetCDF file
| path | test.nc |
| size | 28.1 KiB |
| types | xarray, pandas, fieldlist, numpy, array |
[6]:
ds = ds_in.to_xarray()
ds
[6]:
<xarray.Dataset> Size: 1kB
Dimensions: (latitude: 8, longitude: 13)
Coordinates:
* latitude (latitude) float64 64B 70.0 65.0 60.0 55.0 50.0 45.0 40.0 35.0
* longitude (longitude) float64 104B -20.0 -15.0 -10.0 ... 30.0 35.0 40.0
number int64 8B ...
time datetime64[ns] 8B ...
step timedelta64[ns] 8B ...
surface float64 8B ...
valid_time datetime64[ns] 8B ...
Data variables:
t2m (latitude, longitude) float32 416B dask.array<chunksize=(8, 13), meta=np.ndarray>
msl (latitude, longitude) float32 416B dask.array<chunksize=(8, 13), meta=np.ndarray>
Attributes:
GRIB_edition: 1
GRIB_centre: ecmf
GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts
GRIB_subCentre: 0
Conventions: CF-1.7
institution: European Centre for Medium-Range Weather Forecasts
history: 2026-03-23T18:21 GRIB to CDM+CF via cfgrib-0.9.1...See the other file related notebooks for more details:
Reading files using patterns¶
The file source can also be specified by using patterns. In the example below when pattern “id” is substituted it will match two files: test4.grib and test6.grib:
[7]:
ds = ekd.from_source("file-pattern", "./test{id}.grib", {"id": [4, 6]})
ds.to_fieldlist().ls()
[7]:
| parameter.variable | time.valid_datetime | time.base_datetime | time.step | vertical.level | vertical.level_type | ensemble.member | geography.grid_type | |
|---|---|---|---|---|---|---|---|---|
| 0 | t | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 500 | pressure | 0 | regular_ll |
| 1 | z | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 500 | pressure | 0 | regular_ll |
| 2 | t | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
| 3 | z | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
| 4 | t | 2018-08-01 12:00:00 | 2018-08-01 12:00:00 | 0 days | 1000 | pressure | 0 | regular_ll |
| 5 | u | 2018-08-01 12:00:00 | 2018-08-01 12:00:00 | 0 days | 1000 | pressure | 0 | regular_ll |
| 6 | v | 2018-08-01 12:00:00 | 2018-08-01 12:00:00 | 0 days | 1000 | pressure | 0 | regular_ll |
| 7 | t | 2018-08-01 12:00:00 | 2018-08-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
| 8 | u | 2018-08-01 12:00:00 | 2018-08-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
| 9 | v | 2018-08-01 12:00:00 | 2018-08-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
[ ]: