cf-python icon indicating copy to clipboard operation
cf-python copied to clipboard

Is it possible to get a domain summary (aka bounding box) in a simpler way that the following?

Open bnlawrence opened this issue 2 years ago • 9 comments

 def domain_summary(f):
        """ For a given field and it's domain, get a domain summary """
        #FIXME: should be outer edge of bounds, and I am not sure about the coords and ancils, is that sufficient?
        coords = [serialise(cf.Data.concatenate([b.min(), b.max()])) for b in [a.data for k,a in f.domain.dimension_coordinates().items()]]
        ancils = [serialise(cf.Data.concatenate([b.min(), b.max()])) for b in [a.data for k,a in f.domain.auxiliary_coordinates().items()]] 
        bbox = coords+ancils
        methods = get_cell_methods(f)
        return bbox, methods

In doing this I'm making use of the following stub for serialising a data object, e.g. to pass to another application using json (is there a better way to do this)?

def serialise_data(o):
    units = str(o.Units)
    values = o.array.squeeze()
    shape = o.array.shape
    if len(values) == 1:
        values = float(values[0])
    else:
        values = values.tolist()
    return {'units': units, 'values': values, 'shape': shape}

def serialise(object):
    methods = {
        "<class 'cf.data.data.Data'>": serialise_data
    }
    otype = str(type(object))
    if otype in methods:
        return methods[otype](object)
    else:
        return ValueError(f'Cannot serialise {otype}')

bnlawrence avatar Nov 02 '22 15:11 bnlawrence