unifhy.LatLonGrid.from_field

classmethod LatLonGrid.from_field(field)

Instantiate a LatLonGrid from spatial dimension coordinates of a cf.Field.

Parameters
field: cf.Field

The field object that will be used to instantiate a LatLonGrid instance. This field must feature a ‘latitude’ and a ‘longitude’ dimension coordinates, and these coordinates must feature bounds.

Returns

LatLonGrid

Examples

Instantiating from a 2D field:

>>> import cf
>>> f = cf.Field()
>>> lat = f.set_construct(
...     cf.DimensionCoordinate(
...         properties={'standard_name': 'latitude',
...                     'units': 'degrees_north',
...                     'axis': 'Y'},
...         data=cf.Data([15, 45, 75]),
...         bounds=cf.Bounds(data=cf.Data([[0, 30], [30, 60], [60, 90]]))
...     ),
...     axes=f.set_construct(cf.DomainAxis(size=3))
... )
>>> lon = f.set_construct(
...     cf.DimensionCoordinate(
...         properties={'standard_name': 'longitude',
...                     'units': 'degrees_east',
...                     'axis': 'X'},
...         data=cf.Data([30, 90, 150]),
...         bounds=cf.Bounds(data=cf.Data([[0, 60], [60, 120], [120, 180]]))
...     ),
...     axes=f.set_construct(cf.DomainAxis(size=3))
... )
>>> sd = LatLonGrid.from_field(f)
>>> print(sd)
LatLonGrid(
    shape {Y, X}: (3, 3)
    Y, latitude (3,): [15, 45, 75] degrees_north
    X, longitude (3,): [30, 90, 150] degrees_east
    Y_bounds (3, 2): [[0, ..., 90]] degrees_north
    X_bounds (3, 2): [[0, ..., 180]] degrees_east
)

Using the field interface back and forth:

>>> sd1 = LatLonGrid.from_extent_and_resolution(
...     latitude_extent=(30, 70),
...     longitude_extent=(0, 90),
...     latitude_resolution=5,
...     longitude_resolution=10,
...     latitude_longitude_location='upper right'
... )
>>> sd2 = LatLonGrid.from_field(sd1.to_field())
>>> sd2 == sd1
True