#!/usr/bin/env python

import argparse

import iris


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Summarise/display data using Iris.')
    parser.add_argument('-d', '--display', action='store_true')
    parser.add_argument('-c', '--coords', action='store_true')
    parser.add_argument('file_names', nargs='+')

    args = parser.parse_args()

    cubes = iris.load(args.file_names)

    if args.display:
        import matplotlib.pyplot as plt
        import iris.quickplot as qplt
        for cube in cubes:
            if cube.ndim > 2:
                # For higher-dimensional Cubes we try to plot a map.
                x = cube.coords(axis='X', dim_coords=True)
                y = cube.coords(axis='Y', dim_coords=True)
                index = [0] * cube.ndim
                if len(x) == 1 and len(y) == 1:
                    xd = cube.coord_dims(x[0])[0]
                    yd = cube.coord_dims(y[0])[0]
                    index[xd] = slice(None)
                    index[yd] = slice(None)
                else:
                    index[-1] = slice(None)
                    index[-2] = slice(None)
                cube = cube[tuple(index)]
            qplt.contourf(cube)
            try:
                plt.gca().coastlines()
            except AttributeError:
                pass
            plt.show()
    else:
        for cube in cubes:
            print cube
            if args.coords:
                for coord in cube.dim_coords:
                    print coord
