Reading multiple files¶
First we prepare the data.
[1]:
import earthkit.data as ekd
ekd.download_example_file(["test.grib", "test4.grib"])
[2]:
!test -d _grib_dir_no_sql || (mkdir -p _grib_dir_no_sql; cp -f test.grib test4.grib _grib_dir_no_sql/)
Multiple GRIB files can be read in various ways:
Option 1: using wildcards¶
[3]:
d = ekd.from_source("file", "test*.grib")
d
[3]:
GRIB file
| path | |
| types | fieldlist, pandas, xarray, numpy, array |
[4]:
fl = d.to_fieldlist()
[5]:
len(fl)
[5]:
12
[6]:
fl
[6]:
SimpleFieldList, 12 fields
Option 2: using a list¶
[7]:
fl = ekd.from_source("file", ["test.grib", "test4.grib"]).to_fieldlist()
[8]:
len(fl)
[8]:
6
[9]:
fl
[9]:
SimpleFieldList, 6 fields
Option 3: reading all files in a directory¶
[10]:
fl = ekd.from_source("file", "./_grib_dir_no_sql").to_fieldlist()
[11]:
len(fl)
[11]:
6
[12]:
fl
[12]:
SimpleFieldList, 6 fields
Working with the object¶
The resulting GRIB object behaves in the same way as if it was created from a single file:
[13]:
fl.ls()
[13]:
| parameter.variable | time.valid_datetime | time.base_datetime | time.step | vertical.level | vertical.level_type | ensemble.member | geography.grid_type | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2t | 2020-05-13 12:00:00 | 2020-05-13 12:00:00 | 0 days | 0 | surface | 0 | regular_ll |
| 1 | msl | 2020-05-13 12:00:00 | 2020-05-13 12:00:00 | 0 days | 0 | surface | 0 | regular_ll |
| 2 | t | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 500 | pressure | 0 | regular_ll |
| 3 | z | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 500 | pressure | 0 | regular_ll |
| 4 | t | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
| 5 | z | 2007-01-01 12:00:00 | 2007-01-01 12:00:00 | 0 days | 850 | pressure | 0 | regular_ll |
[14]:
fl[2]
[14]:
Field
| number_of_values | 65160 |
| array_type | ndarray |
| array_dtype | float64 |
| variable | t |
| units | kelvin |
| valid_datetime | 2007-01-01 12:00:00 |
| base_datetime | 2007-01-01 12:00:00 |
| step | 0:00:00 |
| level | 500 |
| layer | None |
| level_type | pressure |
| member | 0 |
| grid_spec | {'grid': [1, 1]} |
| grid_type | regular_ll |
| shape | (181, 360) |
| area | (90.0, 0.0, -90.0, 359.0) |