# -*- coding: utf-8 -*-
"""
TEST mtpy.core.mt.MT

@author: YG
"""
from unittest import TestCase
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 31 13:19:35 2017

@author: u64125
Alison.Kirkby@ga.gov.au
"""

import os
import numpy as np

from mtpy.core.mt import MT
from tests import TEST_MTPY_ROOT
import mtpy.analysis.geometry as mtg


class Test_PT(TestCase):

    def test_pt(self):
        self.mtobj = MT(os.path.normpath(os.path.join(TEST_MTPY_ROOT, "examples/data/edi_files/pb42c.edi")))
        self.pt_expected = np.array([[[  1.30644963e+00,  -2.67740187e-02],
        [ -1.33702443e-02,   1.28968939e+00]],

       [[  1.21678059e+00,  -1.07765729e-02],
        [ -8.20007589e-03,   1.23374034e+00]],

       [[  1.17164177e+00,   1.09018782e-03],
        [ -6.68510048e-03,   1.18271654e+00]],

       [[  1.22540541e+00,   4.38999476e-03],
        [ -4.20009647e-03,   1.24116127e+00]],

       [[  1.22262143e+00,  -1.27947436e-02],
        [ -4.73195876e-03,   1.25493677e+00]],

       [[  1.21501297e+00,  -8.79427102e-03],
        [  1.03830156e-02,   1.22427493e+00]],

       [[  1.22785045e+00,   1.39792917e-02],
        [ -7.08673035e-03,   1.23846962e+00]],

       [[  1.26661703e+00,  -1.11292454e-02],
        [  1.82801360e-03,   1.26240177e+00]],

       [[  1.18539706e+00,   6.39442474e-03],
        [ -1.01453767e-02,   1.25514910e+00]],

       [[  1.28549981e+00,  -1.00606766e-01],
        [  3.97760695e-02,   1.32053655e+00]],

       [[  1.22555721e+00,  -6.29531701e-02],
        [  3.36638894e-02,   1.24514491e+00]],

       [[  1.15217304e+00,   2.47597860e-02],
        [ -4.69132792e-02,   1.28928907e+00]],

       [[  1.07175797e+00,  -3.58092355e-03],
        [ -3.12450311e-02,   1.19733081e+00]],

       [[  1.00918431e+00,  -1.48723334e-02],
        [ -1.04135860e-03,   1.06274597e+00]],

       [[  9.15517149e-01,  -7.13677311e-03],
        [  4.49100302e-03,   9.67281170e-01]],

       [[  7.82696110e-01,   1.70157289e-02],
        [  1.87039067e-02,   8.29411722e-01]],

       [[  7.05442477e-01,   3.78377052e-02],
        [  2.11076586e-02,   7.39844699e-01]],

       [[  6.35185233e-01,   4.73463102e-02],
        [  3.31681155e-02,   6.45232848e-01]],

       [[  5.55546920e-01,   6.54610202e-02],
        [  6.89078895e-02,   5.23858436e-01]],

       [[  5.33096567e-01,   7.08103577e-02],
        [  6.49382268e-02,   4.46884668e-01]],

       [[  5.27354094e-01,   8.09968253e-02],
        [  1.96849609e-02,   3.71188472e-01]],

       [[  5.11384716e-01,   8.77380469e-02],
        [  1.36652476e-02,   2.64391007e-01]],

       [[  5.07676485e-01,   8.88590722e-02],
        [ -2.89224644e-03,   2.26830209e-01]],

       [[  5.32226186e-01,   7.99515723e-02],
        [ -8.08381040e-03,   1.72606458e-01]],

       [[  5.88599443e-01,   7.82062018e-02],
        [ -8.45485953e-03,   1.64746123e-01]],

       [[  6.08649155e-01,   8.25165235e-02],
        [ -2.18321304e-02,   1.89799568e-01]],

       [[  6.72877101e-01,   7.17000488e-02],
        [ -8.23242896e-02,   2.38847621e-01]],

       [[  7.83704974e-01,   9.35718439e-02],
        [ -1.08804893e-01,   2.69048188e-01]],

       [[  8.10341816e-01,   9.92141045e-02],
        [ -1.26495824e-01,   2.81539705e-01]],

       [[  9.44396211e-01,   9.79869018e-02],
        [ -1.86664281e-01,   3.53878350e-01]],

       [[  1.20372744e+00,   1.43106117e-01],
        [ -1.82486049e-01,   4.45265471e-01]],

       [[  1.16782854e+00,   1.13799885e-01],
        [ -1.75825646e-01,   4.46497807e-01]],

       [[  1.34754960e+00,   7.86821351e-02],
        [ -1.52050649e-01,   5.27637774e-01]],

       [[  1.54766037e+00,   1.07732214e-01],
        [ -1.24203091e-01,   6.35758473e-01]],

       [[  1.57964820e+00,   7.39413746e-02],
        [ -1.02148722e-01,   6.66546887e-01]],

       [[  1.62101014e+00,   9.00546725e-02],
        [ -5.05253680e-02,   7.14423033e-01]],

       [[  1.68957924e+00,   3.97165705e-02],
        [  4.57251401e-02,   7.76737215e-01]],

       [[  1.66003469e+00,   3.22243697e-02],
        [  9.00225059e-02,   8.14143062e-01]],

       [[  1.62779118e+00,   3.26316490e-03],
        [  1.68213765e-01,   7.85939990e-01]],

       [[  1.51783857e+00,  -1.45050231e-02],
        [  2.23460898e-01,   7.96441583e-01]],

       [[  1.41377974e+00,  -3.64217144e-02],
        [  2.56732302e-01,   8.12803360e-01]],

       [[  1.32448223e+00,  -9.04193565e-02],
        [  2.46858147e-01,   8.54516882e-01]],

       [[  1.22981959e+00,  -1.86648528e-01],
        [  3.20105326e-01,   8.15014902e-01]]])
        
        assert(np.all(np.abs((self.pt_expected - self.mtobj.pt.pt)/self.pt_expected) < 1e-6))

        
        alpha_expected = np.array([-33.66972565, -65.89384737, -76.59867325,  89.65473659,
           -75.76307747,  85.13326608,  73.50684783, -32.810132  ,
           -88.46092736, -59.97035554, -61.88664666, -85.4110878 ,
           -82.24967714, -81.72640079, -88.53701804,  71.29889577,
            60.1345369 ,  48.55666153,  38.3651419 ,  28.79048968,
            16.40517236,  11.16030354,   8.50965433,   5.65066256,
             4.67255493,   4.12192474,  -0.70110747,  -0.84768598,
            -1.47667976,  -4.27011302,  -1.48608617,  -2.45732916,
            -2.55670157,  -0.51738522,  -0.88470366,   1.24832387,
             2.67364329,   4.11167901,   5.75654718,   8.07694833,
            10.06615916,   9.20560479,   8.91737594])
        
        beta_expected = np.array([-0.14790673, -0.03012061,  0.09460956,  0.09976904, -0.09322928,
           -0.22522043,  0.24468941, -0.14677427,  0.19414636, -1.54172397,
           -1.11970814,  0.84076362,  0.3492499 , -0.19123344, -0.17692124,
           -0.02999968,  0.33160131,  0.31720792, -0.09148111,  0.17165854,
            1.95175741,  2.72709705,  3.56012648,  3.55975888,  3.28108606,
            3.72287137,  4.79442926,  5.44077452,  5.8397381 ,  6.18330647,
            5.58466467,  5.08560032,  3.50735531,  3.03177428,  2.24126272,
            1.7223648 , -0.06979335, -0.66910857, -1.95471268, -2.93540374,
           -3.75023764, -4.39936596, -6.95935213])
        
        azimuth_expected = alpha_expected-beta_expected
        
        
        assert(np.all(np.abs((alpha_expected - self.mtobj.pt.alpha)/alpha_expected) < 1e-6))
        assert(np.all(np.abs((beta_expected - self.mtobj.pt.beta)/beta_expected) < 1e-6))
        assert(np.all(np.abs((azimuth_expected - self.mtobj.pt.azimuth)/azimuth_expected) < 1e-6))
        
        
        # pi1 = 0.5*((self.pt_expected[:,0,0] - self.pt_expected[:,1,1])**2 +\
        #            (self.pt_expected[:,0,1] + self.pt_expected[:,1,0])**2)**0.5
        # pi2 = 0.5*((self.pt_expected[:,0,0] + self.pt_expected[:,1,1])**2 +\
        #            (self.pt_expected[:,0,1] - self.pt_expected[:,1,0])**2)**0.5
            
        # phimin_expected = np.degrees(pi2 - pi1)
        # phimax_expected = np.degrees(pi2 + pi1)

        # assert(np.all(np.abs(phimin_expected - self.mtobj.pt.phimin)/phimin_expected) < 1e-6)
        # assert(np.all(np.abs(phimax_expected - self.mtobj.pt.phimax)/phimax_expected) < 1e-6)