classdef Environment < Component
% Environment: Represents launch site dependent variables.
%
%   Constructor:
%       - Environment: Creates an instance of the Environment class.
%           Loaded config: environmentConfig.m
%           Loaded data: -
%           Arguments:
%               - mission: Mission, mission object
%               - motor: Motor, used to compute pin distance
%               - varIn: (optional) config source. Alternative to config.m
%               file

    properties
        lat0            double                  % [deg] Launchpad latitude
        lon0            double                  % [deg] Launchpad longitude
        z0              double                  % [m] Launchpad Altitude
        omega           double                  % [deg] Launchpad Elevation
        phi             double                  % [deg] Launchpad Azimuth
        pin1Length      double                  % [m] Distance from the upper pin to the upper tank cap
        pin2Length      double                  % [m] Distance from the lower pin to the lower tank cap
        rampLength      double                  % [m] Total launchpad length

        temperature     double = 288.15         % [K] Ground temperature
        pressure        double                  % [Pa] Ground pressure
        rho             double                  % [Kg/m^3] Ground air density
        gamma           double = 1.4            % [-] Gas constant
        R               double = 287            % [-] Gas constant
    end

    properties(SetAccess = private)
        g0                      double              % [-] Gravity costant at launch latitude and altitude
        earthRadius             double = 6371007    % [m] Radius used for g computation
        local                   double              % [-] Vector conatining inputs for atmosphereData
        pinDistance             double              % [m] Distance of the upper pin from the rail base (upper pin-boat + boat-rail base)
        effectiveRampLength     double              % [m] Total launchpad length
        effectiveRampAltitude   double              % [m] Projection of effectiveRampLength on z axis
    end

    properties(Access = protected)
        configName = 'environmentConfig.m'
        variableName = ''
        mission Mission
        motor Motor
    end

    methods
        function obj = Environment(mission, motor, varIn)
            arguments(Input)
                mission Mission = Mission()
                motor Motor = Motor()
                varIn = []
            end
            obj@Component(mission, varIn);
            obj.motor = motor;

            %% Update angles
            obj.omega = deg2rad(obj.omega);
            obj.phi = deg2rad(obj.phi);

            obj.updateAll();
        end
    end
    
    % Updaters
    methods
        function updateAll(obj)
            obj.updateG0;
            obj.updatePinDistance;
            obj.updateRamp;
            obj.updateLocal;
        end
        
        function updateAllExcetpG0(obj) % useful for parallel computing based on threads (gravitywgs84 not supported)
            obj.updatePinDistance;
            obj.updateRamp;
            obj.updateLocal;
        end

        function obj = updateG0(obj)
            if(~isempty(obj.lat0))
                obj.g0 = gravitywgs84(obj.z0, obj.lat0);
            end
        end

        function obj = updatePinDistance(obj)
            obj.pinDistance = obj.pin1Length + obj.pin2Length ... 
                + obj.motor.tankLength;
        end

        function obj = updateRamp(obj)
            obj.effectiveRampLength = obj.rampLength - obj.pinDistance;
            obj.effectiveRampAltitude = obj.effectiveRampLength*sin(obj.omega);
        end

        function obj = updateLocal(obj)
            if isempty(obj.temperature), obj.temperature = 288.15; end
            if isempty(obj.gamma), obj.gamma = 1.4; end

            obj.local = [obj.z0, obj.temperature, ...                       % vector containing inputs for atmosphereData
                            obj.pressure, obj.rho];
        end
    end
end