# -*- coding: utf-8 -*-
"""
Created on Thu Jul 15 17:49:12 2021

@author: Guido di Pasquo
"""

from scipy.interpolate import interp1d
from numpy import pi

DEG2RAD = pi/180

class FinWindTunnelData:

    def __init__(self):
        self.alpha = [[0.0, 0.01643725618530523, 0.03472279512549624, 0.05302910128765242, 0.06951827552787006, 0.08782458169002612, 0.10613088785218223, 0.12262006209239994, 0.13913000355458263, 0.15744669332772124, 0.1757841503228249, 0.1922837081740251, 0.20879364963620778, 0.22712072302032887, 0.24363066448251158, 0.26196812147761517, 0.2803263456946838, 0.29866380268978737, 0.31519451137393506, 0.33353196836903876, 0.35186942536414223, 0.36840013404829003, 0.38675835826535865, 0.4051061988714446, 0.4234540394775308, 0.4418018800836168, 0.4583118215457996, 0.4766596621518857, 0.49317998722505085, 0.5115174442201544, 0.5298445176042756, 0.5463648426774409, 0.5646919160615619, 0.583018989445683, 0.6013564464407866, 0.617856004291987, 0.6361726940651254, 0.652661868305343, 0.6709785580784816, 0.6911019961625762, 0.7075704031808289, 0.7258870929539673, 0.7423658835832028, 0.7624997052782796, 0.7789784959075149, 0.7972744184586883, 0.8137532090879236, 0.8320179808061496, 0.8502723689133933],
                      [0.0, 0.016468407018252713, 0.03477471318040877, 0.051274271031609024, 0.06959096080474758, 0.08609051865594783, 0.10440720842908645, 0.12275504903517248, 0.13925460688637267, 0.15759206388147629, 0.17410200534365897, 0.19244984594974512, 0.20898055463389287, 0.22732839523997903, 0.2438487203131442, 0.26219656091923027, 0.2805547851362989, 0.29891300935336745, 0.31543333442653265, 0.33380194225458387, 0.35214978286066995, 0.3705080070777384, 0.3888766149057896, 0.40538655636797233, 0.4237447805850409, 0.4421030048021095, 0.4586337134862573, 0.4769711704813608, 0.4935018791655086, 0.5118393361606121, 0.5301871767666982, 0.5485350173727844, 0.5650449588349671, 0.5833824158300707, 0.6016991056032093, 0.6200365625983129, 0.6365257368385305, 0.6548320430006865, 0.6731383491628427, 0.6895859889591304, 0.7078611442883389, 0.7261051487845999, 0.7443180024479136, 0.7607448750222362],
                      [0.0, 0.016478790629235268, 0.03477471318040877, 0.05312255378649493, 0.06961172802671263, 0.08612166948889538, 0.10446951009498147, 0.12280696709008501, 0.13933767577423267, 0.15769589999130135, 0.176033356986405, 0.19256406567055268, 0.20909477435470042, 0.22745299857176907, 0.24581122278883766, 0.26234193147298535, 0.2807105393010364, 0.299068763518105, 0.3357955955632247, 0.3523263042473725, 0.37069491207542354, 0.38722562075957134, 0.40740097689857824, 0.42393168558272604, 0.4422899097997946, 0.46062736679489813, 0.4771476918680634, 0.49547476525218453, 0.5119950903253497, 0.5303325473204533, 0.548649237093592, 0.5651384113338096, 0.5834447174959656, 0.6017406400471392, 0.618209047065392, 0.6364945860055831, 0.6547697413347916, 0.6711966139091142, 0.6894510020163579, 0.7076950065126189, 0.7259182437869148, 0.7441310974502284, 0.7623543347245245, 0.7787189056329522],
                      [0.0, 0.016468407018252713, 0.034785096791391266, 0.05315370461944247, 0.0696532624706426, 0.0861839711547904, 0.10454219537185899, 0.12289003597794503, 0.13942074466209275, 0.15777896887916135, 0.17612680948524756, 0.19266790178037774, 0.20919861046452543, 0.22755683468159413, 0.2459358261206277, 0.2624873020267405, 0.2808559098547915, 0.29921413407186004, 0.31758274189991126, 0.33410306697307657, 0.352461291190145, 0.3708091317962311, 0.38914658879133474, 0.40747366217545583, 0.4239836036376385, 0.4423210606327421, 0.4606585176278456, 0.47715807547904593, 0.4936368661082811, 0.5119535558814196, 0.5320458431325665, 0.5466867346178986, 0.5649722735580895, 0.5832681961092631, 0.6015537350494542, 0.6180013748457419, 0.6362246121200379, 0.6542401771746837, 0.6706255153050764, 0.6888383689683901, 0.7052340907097652]]
        self.cl = [[0.0, 0.0, 0.0, 0.016182288722967453, 0.03864116369693904, 0.05538864265103949, 0.07213612160514038, 0.09459499657911175, 0.12841716988428442, 0.15084629800398597, 0.18463872445488883, 0.21277924859446085, 0.24660142189963397, 0.27471219918493595, 0.30853437249010907, 0.3423267989410117, 0.3874825237231161, 0.42127495017401895, 0.4664604218103934, 0.500252848261296, 0.5340452747121991, 0.5792307463485733, 0.6243864711306775, 0.6638605467471812, 0.7033346223636847, 0.7428086979801882, 0.7766308712853611, 0.8161049469018649, 0.8556087693726386, 0.8894011958235413, 0.9175119731088434, 0.957015795579617, 0.9851265728649192, 1.0132373501502212, 1.047029776601124, 1.0751703007406963, 1.0975994288603976, 1.120058303834369, 1.1424874319540703, 1.1535235148882999, 1.16461909153107, 1.1870482196507712, 1.2038254454591422, 1.2205431775589726, 1.2373204033673435, 1.248386233155843, 1.265163458964214, 1.265163458964214, 1.265163458964214],
                   [0.0, 0.011095576642770055, 0.027843055596870725, 0.055983579736442746, 0.07841270785614429, 0.10655323199571631, 0.12898236011541742, 0.16845643573192115, 0.1965969598714934, 0.23038938632239647, 0.26421155962756937, 0.30368563524407266, 0.3488711068804471, 0.3883451824969508, 0.4278490049677244, 0.4673230805842279, 0.5124788053663323, 0.5576345301484367, 0.5971383526192104, 0.6479757265669153, 0.6874498021834188, 0.732605526965523, 0.7834429009132282, 0.8172650742184011, 0.8624207990005053, 0.9075765237826097, 0.9527619954189842, 0.986554421869887, 1.0317398935062614, 1.065532319957164, 1.1050063955736678, 1.144480471190171, 1.1783026444953442, 1.212095070946247, 1.2345241990659483, 1.2683166255168512, 1.2907755004908226, 1.3075229794449232, 1.324270458399024, 1.324270458399024, 1.324270458399024, 1.324270458399024, 1.324270458399024, 1.324270458399024],
                   [0.0, 0.01677722580837071, 0.027843055596870725, 0.06731713121337402, 0.0897760061873456, 0.1235981794925185, 0.163072255109022, 0.19686468155992487, 0.24205015319629952, 0.2872058779784039, 0.3209983044293063, 0.36618377606568075, 0.4113692477020552, 0.4565249724841596, 0.5016806972662637, 0.5468661689026382, 0.5977035428503434, 0.6428592676324478, 0.738852366362257, 0.7840378379986315, 0.8348752119463363, 0.8800606835827107, 0.9195050123449443, 0.9646904839813187, 1.009846208763423, 1.0436386352143256, 1.0831424576850996, 1.1112532349704014, 1.1507570574411752, 1.184549483892078, 1.2069786120117794, 1.2294374869857507, 1.2461849659398516, 1.2572507957283512, 1.2683463723711212, 1.2737305529940204, 1.2737305529940204, 1.2737305529940204, 1.2737305529940204, 1.2737305529940204, 1.2737305529940204, 1.2737305529940204, 1.2737305529940204, 1.2737305529940204],
                   [0.0, 0.011095576642770055, 0.03352470476247138, 0.08436207871017642, 0.11250260284974867, 0.1576880744861231, 0.20284379926822727, 0.242317874884731, 0.28750334652110543, 0.3326590713032098, 0.3721331469197131, 0.4230002677216882, 0.4681857393580626, 0.513341464140167, 0.5698604872534727, 0.6264092572210485, 0.6772466311687537, 0.7224023559508581, 0.7732397298985629, 0.8127435523693367, 0.8578992771514411, 0.8973733527679444, 0.9311657792188475, 0.9592765565041494, 0.9930987298093223, 1.0268911562602252, 1.060683582711128, 1.0888241068507, 1.105601332659071, 1.1280304607787723, 1.1280304607787723, 1.1331469197132398, 1.1385311003361391, 1.149596930124639, 1.154981110747538, 1.154981110747538, 1.154981110747538, 1.154981110747538, 1.154981110747538, 1.154981110747538, 1.154981110747538]]
        # self._fill_alpha_and_cl()
        self.cl_f_of_alpha = {
            "1.25": interp1d(self.alpha[3], self.cl[3]),
            "1": interp1d(self.alpha[2], self.cl[2]),
            "0.75": interp1d(self.alpha[1], self.cl[1]),
            "0.5": interp1d(self.alpha[0], self.cl[0])
            }
        self.alpha_f_of_cl = {
            "1.25": interp1d(self.cl[3], self.alpha[3]),
            "1": interp1d(self.cl[2], self.alpha[2]),
            "0.75": interp1d(self.cl[1], self.alpha[1]),
            "0.5": interp1d(self.cl[0], self.alpha[0])
            }

    def _fill_alpha_and_cl(self):
        for i in range(4):
            with open(".\\src\\aerodynamics\\Wind Tunnel Data\\InvZimmerman.csv", "r") as file:
                for line in file:
                    try:
                        a = float(line.split(",")[0 + i*2])
                        b = float(line.split(",")[1 + i*2])
                        if self.cl[i] != []:
                            if b < self.cl[i][-1]:
                                b = self.cl[i][-1]
                        self.alpha[i].append(a * DEG2RAD)
                        self.cl[i].append(b)
                    except ValueError:
                        pass
