Search code examples
python-iris

Turn a cube into a list of data points and lists of x, y and z


This is a bit of a long shot, but I thought I'd ask here before writing this myself.

I have a 3D cube of data with lon, lat and height coordinates. I want 4 1D vectors of the data at all points, the lon, lat, height and data. This is so that I can then write it to an ASCII file as a list of points with their locations. Doing this for the data is easy with a reshape, but the part that is trickier is turning the coordinates into the right vectors.

Has anyone done this already and have some hints?


Solution

  • I'm not 100% sure I've understood the aim correctly, but would something like this do what you want?

    lats = []
    lons = []
    heights = []
    data = []
    for point_cube in cube.slices_over(['latitude', 'longitude', 'height']):
        lats.append(point_cube.coord('latitude').points[0])
        lons.append(point_cube.coord('longitude').points[0])
        heights.append(point_cube.coord('height').points[0])
        data.append(point_cube.data)
    

    Or for something (almost certainly) more efficient, you could explore using the numpy.meshgrid function to turn your 1-d coord.points arrays into 3-d arrays, which you could then handle in the same way as the data array.