"""
Created on 11.11.21

@author: Joschka
"""
import numpy as np
import matplotlib.pyplot as plt
from src import coil_class as BC
from src import physical_constants as cs
import logging as log




def main():
    step = 0.05
    xlim_mm = 30
    xlim = int(xlim_mm / step) + 1
    print(xlim)
    ylim_mm = 30
    ylim = int(ylim_mm / step) + 1
    zlim_mm = 20
    zlim = int(zlim_mm / step) + 1

    x = np.linspace(0, xlim_mm, xlim) * 1e-3
    y = np.linspace(0, ylim_mm, ylim) * 1e-3
    z = np.linspace(0, zlim_mm, zlim) * 1e-3

    b_qu_polar = np.load('output/final/B_quarter_1Gcm.npy')



    # print(f"shape of B_quarter = {np.shape(b_qu_polar)}")
    shape = np.shape(b_qu_polar)
    print(shape)
    new_shape = (shape[0], shape[1], shape[2], shape[3] + 1)
    b_cart_1 = np.zeros(new_shape,dtype = np.float32)

    # add z-component
    b_cart_1[:, :, :, 2] = b_qu_polar[:, :, :, 1]

    for xx in range(0, xlim):
        for yy in range(0, ylim):
            if xx == 0:
                phi = np.pi / 2
            else:
                phi = np.arctan(y[yy] / x[xx])
            b_cart_1[xx, yy, :, 0] = b_qu_polar[xx, yy, :, 0] * np.cos(phi)
            b_cart_1[xx, yy, :, 1] = b_qu_polar[xx, yy, :, 0] * np.sin(phi)

    np.save('output/final/b_cart_1Gcm.npy', b_cart_1)
    del b_qu_polar
    del b_cart_1


if __name__ == '__main__':
    main()
