3. Open binary datasets
Introduction
The most important part of xgrads is to load the binary data (NOT all of them but the portion that is needed right now) into memory as xarray. This is built on correctly parsing the CTL files.
Loading binary data
Single CTL file
The loading of CTL dataset does not require an explicit parsing of CTL file. So the reading is pretty simple:
from xgrads import open_CtlDataset
dset = open_CtlDataset('test10.ctl')
print(dset)
The output is in the view of xarray:
>>> print(dset)
<xarray.Dataset>
Dimensions: (time: 1, x: 360, y: 320)
Coordinates:
* time (time) datetime64[ns] 2019-01-01
* y (y) float64 0.0 1.5e+04 3e+04 ... 4.755e+06 4.77e+06 4.785e+06
* x (x) float64 0.0 1.5e+04 3e+04 ... 5.355e+06 5.37e+06 5.385e+06
Data variables:
emi_index (time, y, x) float32 dask.array<chunksize=(1, 320, 360), meta=np.ndarray>
Attributes:
comment: pm u2/m3
storage: 99
title: CUACE_emi_index
undef: -9999.0
pdef: lcc
Note that the parsing of CTL file is implicitly done within open_CtlDataset()
. So if one wants both the dset
and ctl
, adding a kwarg returnctl=True
will be OK:
from xgrads import open_CtlDataset
dset, ctl = open_CtlDataset('test10.ctl', returnctl=True)
print(dset)
print(ctl)
Multiple CTL files
If there are multiple CTL files describing similar spatial datasets of different time steps, one can also load them into a single dataset using open_mfdataset()
. This is similar to xarray’s open_mfdataset()
. Usually, this should be done with the template
functionality of CTL. But simutaneously open multiple CTL datasets is also easy because of the combining of the multiple dataset is easy by making use of xarray’s concat()
API.
from xgrads import open_mfdataset
dset = open_mfdataset('./test8_*.ctl', parallel=True)
print(ctl)