Replies: 2 comments 1 reply
-
I've not looked at your code in any great detail, but you seem to be doing everything for yourself. Certainly, building a coordinate out of a PartialDateTime is not going to work. Anyway ... you should certainly try out the xarray
In principle, once you have a suitable Iris cube it will just save to a grib file on command. |
Beta Was this translation helpful? Give feedback.
-
So I've read and explored and came up a little short but I think with a little help I can get a working example across the finish line. Here's the idea:
data = xr.open_dataset(ifile, engine='netcdf4', group='galwem')
# see image below for: print(data)
RE_PARSE_FOURNUMS = re.compile(4 * r'[^\d]*(\d*)')
gribEdition = 2
for i in range(1): # just take one variable, aka feature for now
name = data.names3d.isel(features3d=i)
varb = data.variables3d.isel(features3d=i)
varb = varb.rename({'lat':'Y', 'lon':'X'})
# using a local LUT for grib info on each variable
df = gribInfo.where(gribInfo.Parameter == name).dropna()
key = df.SearchKey.item()
print(key) # 0-2-10-L100-1
print(RE_PARSE_FOURNUMS.match(key).groups()) # ('0', '2', '10', '100')
key = f'{gribEdition}-{key}'
for lev in range(1):
qslice = varb.isel(time=0, msllevels=lev)
qslice.attrs={'long_name':str(name.values), 'GRIB_PARAM': key}
print(name.values, qslice)
# (array('Absolute_vorticity', dtype=object),
# <xarray.DataArray 'variables3d' (Y: 721, X: 1440)>
# [1038240 values with dtype=float64]
# Coordinates:
# time datetime64[ns] 2021-09-01T18:00:00
# * Y (Y) float32 90.0 89.75 89.5 89.25 ... -89.25 -89.5 -89.75 -90.0
# * X (X) float32 -180.0 -179.8 -179.5 -179.2 ... 179.2 179.5 179.8
# msllevels int32 110000
# Attributes:
# long_name: Absolute_vorticity
# GRIB_PARAM: 2-0-2-10-L100-1)
# Convert to cube
cube = qslice.to_iris()
# trying to edit the cube
#cube.coord(axis='X').coord_system=iris.coord_systems.GeogCS(654321)
#cube.coord(axis='Y').coord_system=iris.coord_systems.GeogCS(654321)
#cube.add_aux_coord(iris.coords.DimCoord(0, standard_name='forecast_period', units='hours'))
#cube.add_aux_coord(iris.coords.DimCoord(0, "height", units="ft"))
#cube.data.max()
#cube.data=ma.masked_invalid(cube.data)
print(cube)
# Absolute_vorticity / (unknown) (latitude: 721; longitude: 1440)
# Dimension coordinates:
# latitude x -
# longitude - x
# Scalar coordinates:
# msllevels 110000
# time 2021-09-01 18:00:00
# Attributes:
# GRIB_PARAM '2-0-2-10-L100-1'
# test writing to grib2
iris.save(cube, 'test.grib2')
# File ~/mambaforge-pypy3/envs/contrail/lib/python3.11/site-packages/iris_grib/__init__.py:801, in save_pairs_from_cube(cube)
# 799 y_coords = cube.coords(axis='y', dim_coords=True)
# 800 if len(x_coords) != 1 or len(y_coords) != 1:
# --> 801 raise TranslationError("Did not find one (and only one) x or y coord")
# 803 # Save each latlon slice2D in the cube
# 804 for slice2D in cube.slices([y_coords[0], x_coords[0]]):
# TranslationError: Did not find one (and only one) x or y coord So here is where I am. I have changed the dimensions but the conversion to |
Beta Was this translation helpful? Give feedback.
-
I have a case study to create a grib2 from a xarray dataArray. But I'm having trouble creating a proper cube with the information needed to write to grib2. I have not yet tried to dump the cube to grib2 because I'm stuck in the following areas:
1.) I'm having trouble understanding Cube dimension time and how to construct it.
2.) Where to add the grib2 information in the Cube.
3.) What Cube needs to write the grib2 file?
Beta Was this translation helpful? Give feedback.
All reactions