def proc_cube(cube):
    for i, frt_cube in enumerate(cube.slices_over("time")):
        print "Processing timestep ", i, "...",
        img_array = imageproc.tileArray(frt_cube.data)
        img_array /= img_array.max()
        img_array *= 255
        print "Writing image"
        with open("data%03d.png" % i, "wb") as img:
            imageproc.writePng(img_array, img, nchannels=3, alpha=False)
    
    print "Writing video"
    sp.call(["avconv", "-y",
            "-r", "1", "-i", "data%03d.png",
            "-r", "1", "-vcodec", os.getenv("CODEC", "libtheora"), "-qscale:v", os.getenv("QUALITY", 2), os.getenv("FILE_OUT", "out.ogv"])
    print "Cleaning up"
    fs = glob.glob("./data???.png")
    for f in fs:
        os.remove(f)
        
if __name__=="__main__":
    varname = os.getenv("VAR_NAME")
    if varname != None:
        cube = iris.load_cube(os.getenv("FILE_IN"), iris.Constraint(varname))
    else:
        cube = iris.load_cube(os.getenv("FILE_IN"))
    proc_cube(cube)
def procDataToImage(data,
                    image_dest,
                    profile):
    """
    Main processing function. Processes an model_level_number, lat, lon cube,
    including all regridding and restratification of data,
    calculates shadows, and then ultimately posts a tiled
    image to the data service.

    Args:
        * data (iris cube): lat, lon, model_level_number cube 
        * image_dest (str): URL to the data service image destination
        * regrid_shape (tuple): lon, lat, alt dimensions to regrid to

    """

    # # tidy up any problems arising from the on-the-fly altitude calc
    # print "Sanitizing data after altitude restratification"
    # san_data = dataproc.sanitizeAlt(data) #TESTING ONLY
    # # regrid and restratify the data
    print "Regridding data to " + str(data.shape)
    rg_data = dataproc.regridData(data,
                                len(data.coords(axis="Y")[0].points),
                                len(data.coords(axis="X")[0].points),
                                len(data.coords(axis="Z")[0].points),
                                extent=profile.extent)
    # # do any further processing (saturation etc) and convert to 8 bit uints
    # try:
    #     print "Applying custom data processing from profile"
    #     rg_data = profile.proc_fn(rg_data)
    # except AttributeError:
    #     pass


    print "Applying standard data processing (e.g. 8 bit scaling)"
    proced_data = dataproc.procDataCube(rg_data)

    print "Tiling data"
    data_tiled = imageproc.tileArray(proced_data.data)

    return data_tiled, proced_data
def procDataToImage(data, image_dest, profile):
    """
    Main processing function. Processes an model_level_number, lat, lon cube,
    including all regridding and restratification of data,
    calculates shadows, and then ultimately posts a tiled
    image to the data service.

    Args:
        * data (iris cube): lat, lon, model_level_number cube 
        * image_dest (str): URL to the data service image destination
        * regrid_shape (tuple): lon, lat, alt dimensions to regrid to

    """

    # # tidy up any problems arising from the on-the-fly altitude calc
    # print "Sanitizing data after altitude restratification"
    # san_data = dataproc.sanitizeAlt(data) #TESTING ONLY
    # # regrid and restratify the data
    print "Regridding data to " + str(data.shape)
    rg_data = dataproc.regridData(
        data,
        len(data.coords(axis="Y")[0].points),
        len(data.coords(axis="X")[0].points),
        len(data.coords(axis="Z")[0].points),
        extent=profile.extent,
    )
    # # do any further processing (saturation etc) and convert to 8 bit uints
    # try:
    #     print "Applying custom data processing from profile"
    #     rg_data = profile.proc_fn(rg_data)
    # except AttributeError:
    #     pass

    print "Applying standard data processing (e.g. 8 bit scaling)"
    proced_data = dataproc.procDataCube(rg_data)

    print "Tiling data"
    data_tiled = imageproc.tileArray(proced_data.data)

    return data_tiled, proced_data