-
I have a case study to create a import xarray as xr
import numpy as np
import pandas as pd
import iris
from iris.coords import DimCoord
from iris.time import PartialDateTime
from iris.cube import Cube
def build_cube(da, varbname, units):
"""
Build an Iris cube from a dataArray. Doing this manually due to lack of proper metadata in the dataArray.
"""
dt =da.coords['time']
pdt = PartialDateTime(year=dt.year, month=dt.month, day=dt.day, hour=dt.hour, minute=dt.minute)
time = DimCoord(pdt,
standard_name='time')
lev = DimCoord(da.coords['lev'].values, standard_name='lev', units='Height MSL')
latitude = DimCoord(da.coords['lat'].values,
standard_name='latitude',
units='degrees')
longitude = DimCoord(da.coords['lon'].values,
standard_name='longitude',
units='degrees')
cube = Cube(da.values,
standard_name=varbname.values,
long_name=varbname.values,
var_name=varbname.values,
units='K',
attributes=None, # Will add variable attributes here.
dim_coords_and_dims=[ (time, 0), (lev, 1), (latitude, 2), (longitude, 3) ]
)
return out 1.) I'm having trouble understanding |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
1.) I'm having trouble understanding Cube dimension time and how to construct it.Iris stores the time points as numbers since an epoch, which is the standard NetCDF (and CF) representation for datetimes. For example, you could create a coordinate with Xarray typically converts the numbers into numpy.datetimes64 (and discards the units), although you can switch this off by including However, if you need to convert already loaded numpy.datetimes64 objects back into numbers, you can do this as follows:
2.) Where to add the grib2 information in the Cube.Iris follows CF as its data model, so you will need to represent your metadata following these conventions. Then at save time, Iris will then convert this metadata into GRIB constructs. As an example, below we create a cube:
When we print out the cube it looks like:
We then save out the cube:
If you have a look at a grib dump of the file: You will see that:
3.) What Cube needs to write the grib2 file?As demonstrated above, you should be able to save out your Cube as follows It is worth noting that Iris doesn't support all GRIB product/grib definition templates (there are quite a few!) so whether it can save out your data or not will depend on what is contained. |
Beta Was this translation helpful? Give feedback.
1.) I'm having trouble understanding Cube dimension time and how to construct it.
Iris stores the time points as numbers since an epoch, which is the standard NetCDF (and CF) representation for datetimes. For example, you could create a coordinate with
points=(6, 12, 18)
andunits='hours since 1970-01-01 00:00:00'
which would represent time points of1970-01-01 06:00:00, 1970-01-01 12:00:00, 1970-01-01 18:00:00
.Xarray typically converts the numbers into numpy.datetimes64 (and discards the units), although you can switch this off by including
decode_times=False
when you open the file (e.g. with xr.open_dataset).However, if you need to convert already loaded numpy.datetimes64 objects back…