#'C:\\Users\\pkhomchuk\\My Projects\\UAS - FAA Software\\Tracker\\Radar_3D_4'
                   #'C:\\Users\\pkhomchuk\\My Projects\\UAS - FAA Delivery\\UAS - FAA Software\\Tracker\\Maneuver',
                   #'C:\\Users\\pkhomchuk\\My Projects\\UAS - FAA Delivery\\UAS - FAA Software\\Tracker\\Straight',
                   #'C:\\Users\\pkhomchuk\\My Projects\\UAS - FAA Delivery\\UAS - FAA Software\\Tracker\s\55',
                   '' )

TGT_BEACON = 2332
NMI2FT = 6076.11548

def listFilesInDir( dir, ext ) :
    os.chdir( dir )
    return glob.glob( '*.' + ext )

errors = []
for ind, folder in enumerate( output_folders ) :
    if folder != '' :
        residual_rng_rate = []

        output_file_names = listFilesInDir( folder, 'txt' )
        truth_ind = 0
        for filename in output_file_names :
            output = OutputFile.read( folder + "\\" + filename )
            trk = OutputFile.Track( output, beacon = TGT_BEACON, sensor = 'radar' );
            if trk.dataIsGood :
                residuals = np.ndarray.tolist( NMI2FT * np.transpose( trk.residuals ))
                residual_rng_rate += residuals[ 0 ]

plt.figure( 1 )
plt.boxplot( residual_rng_rate )

plt.show()
        truth_file_names = listFilesInDir( folder, 'txt' )
        for filename in truth_file_names :
            truth = TruthFile.read( folder + '\\' + filename )
            tgt_true[ ind ].append( TruthFile.Aircraft( truth, beacon = TGT_BEACON ) )
            own_true[ ind ].append( TruthFile.Aircraft( truth, beacon = OWN_BEACON ) )

errors = []
for ind, folder in enumerate( output_folders ) :
    if folder != '' :
        hist_errors = TrkErrors.HistErrors()

        output_file_names = listFilesInDir( folder, 'txt' )
        truth_ind = 0
        for filename in output_file_names :
            output = OutputFile.read( folder + "\\" + filename )
            trk = OutputFile.Track( output, rack_id = 1, track_type = 2  );
            if trk.dataIsGood :
                truth_ind = int( math.floor( ( int( filename.split( '.' )[ 0 ][ 3: ] ) - 1 ) / N_MONTE_CARLO ) )
                err = TrkErrors.Errors( own_true[ ind ][ truth_ind ], tgt_true[ ind ][ truth_ind ], trk, True )
                hist_errors = hist_errors + err.hist_errors
            #if ind % N_MONTE_CARLO == 0 and ind > 0 :
            #    truth_ind += 1
        errors.append( hist_errors )

n = len( errors )

# Plot horizontal position error
gs = gridspec.GridSpec( 1, n ) 
plt.figure( 1 )
for i in range( n ) :
import math

truth_file_name = 'C:\\Users\\pkhomchuk\\My Projects\\UAS encounters\\MIT_LL\\55\\truth_mit_ll_19235.txt'
output_file_name = 'C:\\Users\\pkhomchuk\\My Projects\\UAS - FAA Software\\Tracker\\Radar_4D_1\\out4.txt'

NMI2FT = 6076.11548
KNOTS2FT_PER_MIN = NMI2FT / 60

OWN_BEACON = 2331
TGT_BEACON = 2332

truth = TruthFile.read( truth_file_name )
tgt_true = TruthFile.Aircraft( truth, beacon = TGT_BEACON )
own_true = TruthFile.Aircraft( truth, beacon = OWN_BEACON )

output = OutputFile.read( output_file_name )
trk = OutputFile.Track( output, beacon = TGT_BEACON, sensor = 'radar' );

tgt_true_pos_own_enu = ct.geod2enu( own_true.pos_geod, tgt_true.pos_geod )
tgt_true_pos_sph = ct.enu2sph( tgt_true_pos_own_enu )

plt.figure( 1 )
plt.plot( tgt_true_pos_own_enu[ :, ct.EAST ], tgt_true_pos_own_enu[ :, ct.NORTH ], 'g-' )
plt.xlabel( 'East [nmi]' )
plt.ylabel( 'North [nmi]' )
plt.grid()

gs = gridspec.GridSpec( 1, 2 ) 
plt.figure( 2 )
plt.subplot( gs[ 0 ] )
plt.plot( trk.time, 3600 * trk.residuals[ :, 3 ], 'g-' )