" spar loading for gust case "
import os
from numpy import pi, hstack, cos
import pandas as pd
from gpkit import parse_variables
from gpfit.fit_constraintset import FitCS
from .sparloading import SparLoading

#pylint: disable=invalid-name, no-member, arguments-differ, exec-used
#pylint: disable=attribute-defined-outside-init, undefined-variable

class GustL(SparLoading):
    """ Gust Loading Model

    Variables
    ---------
    vgust       10      [m/s]       gust velocity
    Ww                  [lbf]       wing weight
    v                   [m/s]       vehicle speed
    cl                  [-]         wing lift coefficient

    Variables of length wing.N
    --------------------------
    agust                           [-]         gust angle of attack
    cosminus1   self.return_cosm1   [-]         1 minus cosine factor

    Upper Unbounded
    ---------------
    v, cl, wing.spar.I, wing.spar.tshear, wing.spar.Sy
    J (if wingSparJ)
    theta (if not wingSparJ), M (if not wingSparJ)

    Lower Unbounded
    ---------------
    Ww, wing.planform.b, wing.planform.cave
    wing.planform.CM (if wingSparJ), qne (if wingSparJ)
    theta (if not wingSparJ), M (if not wingSparJ)

    LaTex Strings
    -------------
    vgust               V_{\\mathrm{gust}}
    Ww                  W_{\\mathrm{w}}
    cl                  c_l
    agust               \\alpha_{\\mathrm{gust}}
    cosminus1           (cos(x)-1)

    """
    new_qbarFun = None
    new_SbarFun = None

    return_cosm1 = lambda self, c: hstack(
        [1e-10, 1-cos(c[self.wing.planform.eta][1:]*pi/2)])

    def setup(self, wing, state):
        exec parse_variables(GustL.__doc__)
        self.load = SparLoading.setup(self, wing, state)

        cbar = self.wing.planform.cbar
        W = self.W  # from SparLoading

        path = os.path.dirname(os.path.abspath(__file__))
        df = pd.read_csv(path + os.sep + "arctan_fit.csv").to_dict(
            orient="records")[0]

        constraints = [
            # fit for arctan from 0 to 1, RMS = 0.044
            FitCS(df, agust, [cosminus1*vgust/v]),
            self.beam["\\bar{q}"] >= cbar*(1 + 2*pi*agust/cl*(1+Ww/W)),
            ]

        return self.load, constraints

@@ -63,16 +63,68 @@
  hstack,
+ array%0Afrom ad import adnumber%0Afrom ad.admath import
  cos%0Aimp
@@ -1587,17 +1587,33 @@
    %5B
+adnumber(
 1e-10
+)
 , 1-
+array(
 cos(
@@ -1647,16 +1647,17 @@
 :%5D*pi/2)
+)
 %5D)%0A%0A    

fix auto-diff for cosminus1 in gust loading (#158)