Le = zeros((Nc,4))
    La = zeros(Nc)

    ## Find transformation to take alternative orientations to Corisco
    ## reference frame.
    tt = solve_transformation(Lq, Lr)
    Lr_hat = zeros((Nc,4))
    for n in range(Nc):
        Lr_hat[n] = (tt * Quat(Lq[n])).q

    dd = [{"set": s, "frame": fr}
          for s in ["set-apa09.json", "set-apa08.json"]
          for fr in range(24)]

    #print zip(dd, Lr_hat)
    tref = [dict(meta.items() + [("ori_ref", ori.tolist())])
           for meta, ori in zip(dd, Lr_hat)]

    savetxt('tardiff_apastundistorted_ref_transformed.dat', Lr_hat)

    # with open('transformed_reference_apaundistorted.dat', 'w') as fp:
    #     json.dump(tref, fp, indent=4)

    ## Calculate errors between each estimated orientation and
    ## transformed reference.
    for n in range(Nc):
        Le[n] = ( Quat(Lr_hat[n]) / Quat(Lr[n]) ).canonical().q
        La[n] = Quat(Le[n]).angle()

    ion()
    fix_reference_quaternions(Lr)

    Le = zeros((Nc, 4))
    La = zeros(Nc)

    ## Find transformation to take alternative orientations to Corisco
    ## reference frame.
    tt = solve_transformation(Lq, Lr)
    Lr_hat = zeros((Nc, 4))
    for n in range(Nc):
        Lr_hat[n] = (tt * Quat(Lq[n])).q

    dd = [{"set": s, "frame": fr} for s in ["set-apa09.json", "set-apa08.json"] for fr in range(24)]

    # print zip(dd, Lr_hat)
    tref = [dict(meta.items() + [("ori_ref", ori.tolist())]) for meta, ori in zip(dd, Lr_hat)]

    with open("transformed_reference_apa.dat", "w") as fp:
        json.dump(tref, fp, indent=4)

    ## Calculate errors between each estimated orientation and
    ## transformed reference.
    for n in range(Nc):
        Le[n] = (Quat(Lr_hat[n]) / Quat(Lr[n])).canonical().q
        La[n] = Quat(Le[n]).angle()

    ion()

    figure(4, figsize=(8 * 0.8, 6 * 0.8))
    title(u"DistribuiĆ§Ć£o acumulada de erros")
    xlabel("Erro [graus]")