# -*- coding: utf-8 -*-
"""
Created on Thu Nov 28 12:10:11 2019

@author: Omer
"""

## File handler
## This file was initially intended purely to generate the matrices for the near earth code found in: https://public.ccsds.org/Pubs/131x1o2e2s.pdf
## The values from the above pdf were copied manually to a txt file, and it is the purpose of this file to parse it.
## The emphasis here is on correctness, I currently do not see a reason to generalise this file, since matrices will be saved in either json or some matrix friendly format.

import numpy as np
from scipy.linalg import circulant
#import matplotlib.pyplot as plt
import scipy.io
import common
import hashlib
import os

projectDir = os.environ.get('LDPC')
if projectDir == None:
    import pathlib
    projectDir = pathlib.Path(__file__).parent.absolute()
## <NAME>: added on 01/12/2020, need to make sure this doesn't break anything.
import sys
sys.path.insert(1, projectDir)


FILE_HANDLER_INT_DATA_TYPE = np.int32
GENERAL_CODE_MATRIX_DATA_TYPE = np.int32
NIBBLE_CONVERTER = np.array([8, 4, 2, 1], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)



def nibbleToHex(inputArray):
    n = NIBBLE_CONVERTER.dot(inputArray)
    if n == 10:
        h = 'A'
    elif n== 11:
        h = 'B'
    elif n== 12:
        h = 'C'
    elif n== 13:
        h = 'D'
    elif n== 14:
        h = 'E'
    elif n== 15:
        h = 'F'
    else:
        h = str(n)
    return h
    
def binaryArraytoHex(inputArray):
    d1 = len(inputArray)
    assert (d1 % 4 == 0)
    
    outputArray = np.zeros(d1//4, dtype = str)
    outputString = ''
    for j in range(d1//4):
        nibble = inputArray[4 * j : 4 * j + 4]
        h = nibbleToHex(nibble)
        outputArray[j] = h
        outputString = outputString + h
    return outputArray, outputString
   

def hexStringToBinaryArray(hexString):
    outputBinary = np.array([], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)
    for i in hexString:
        if i == '0':
            nibble =  np.array([0,0,0,0], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)
        elif i ==  '1':
            nibble =  np.array([0,0,0,1], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)
            
        elif i ==  '2':
            nibble =  np.array([0,0,1,0], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)
            
        elif i ==  '3':
            nibble =  np.array([0,0,1,1], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)
            
        elif i ==  '4':
            nibble =  np.array([0,1,0,0], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)
            
        elif i ==  '5':
            nibble =  
np.array([0,1,0,1], dtype = GENERAL_CODE_MATRIX_DATA_TYPE)