Esempio n. 1
0
        void FixedUpdate()
        {
            check_settings();
            //If were not using point MPAS data or were not in flight return void
            if ((!HighLogic.LoadedSceneIsFlight) || (!use_point))
            {
                return;
            }
            Vessel vessel = FlightGlobals.ActiveVessel;
            //Get vehicle position
            double vheight = vessel.altitude;

            if ((wx_enabled) && (use_point))
            {
                UpdateCoords();

                if ((FlightGlobals.ActiveVessel.mainBody != kerbin))
                {
                    return;
                }

                string lsite0 = Util.get_last_lsite();
                //Check to see if launch site has changed
                string lsite = vessel.launchedFrom;
                //Util.Log("Currrent Launch Site: " + lsite);
                //Rename launchpad to KSC
                if (lsite != lsite0)
                {
                    //Util.Log("Launch site changed, update weather data | New: " + lsite + ", Old: " + lsite0);
                    gotMPAS = false;
                }

                int lidx;
                if (lsites_name.Contains(lsite))
                {
                    lidx = lsites_name.IndexOf(lsite);
                    //Util.Log("Launch site confirmed: " + lsites[lidx]);
                }
                else
                {
                    double        mlat  = vessel.latitude;
                    double        mlng  = vessel.longitude;
                    List <double> ddist = new List <double>();
                    for (int l = 0; l < lsites_lat.Count(); l++)
                    {
                        ddist.Add(Math.Pow((lsites_lat[l] - mlat), 2) + Math.Pow((lsites_lng[l] - mlng), 2));
                    }
                    lidx = ddist.IndexOf(ddist.Min());
                    //Util.Log("Retrieve longitude of nearest launch site: " + lsites[lidx]);
                }

                //Get current launch site
                Util.save_lsite(lsite);
                Util.save_lsite_short(lsites[lidx]);
                //If launch site has changed retrieved weather data for new launch site
                if (!gotMPAS)
                {
                    //Debug.Log("Current launch site: " + lsite);
                    //Util.Log("Retrieve weather data for new launch site");
                    if (lsites_name.Contains(lsite))
                    {
                        //If launch site is known (i.e., in KSP or Kerbinside remastered)
                        lidx = lsites_name.IndexOf(lsite);
                        //Util.Log("Retrieve weather data for: " + lsites[lidx]);
                        _wx_api.Refresh();
                        gotMPAS = true;
                    }
                    else
                    {
                        double        mlat  = vessel.latitude;
                        double        mlng  = vessel.longitude;
                        List <double> ddist = new List <double>();
                        for (int l = 0; l < lsites_lat.Count(); l++)
                        {
                            ddist.Add(Math.Pow((lsites_lat[l] - mlat), 2) + Math.Pow((lsites_lng[l] - mlng), 2));
                        }
                        int midx = ddist.IndexOf(ddist.Min());
                        //Util.Log("Retrieve nearby weather data for: " + lsites[midx]);
                        _wx_api.Refresh();
                        gotMPAS = true;
                    }
                    //Util.Log("GotMPAS: " + gotMPAS.ToString());
                }

                if ((FlightGlobals.ActiveVessel.mainBody == kerbin) && (vessel.altitude >= 70000))
                {
                    //Get 2D meteorological fields
                    double             olr; double precipw; double mslp; double sst; double tcld; double rain; double tsfc; double rhsfc; double uwnd_sfc; double vwnd_sfc;
                    weather_api.wx_srf weather_data2D = _wx_api.getAmbientWx2D();
                    olr = weather_data2D.olr; tcld = weather_data2D.cloudcover; mslp = weather_data2D.mslp; precipw = weather_data2D.precitable_water; rain = weather_data2D.precipitation_rate;
                    sst = weather_data2D.sst; tsfc = weather_data2D.temperature; rhsfc = weather_data2D.humidity; uwnd_sfc = weather_data2D.wind_x; vwnd_sfc = weather_data2D.wind_y;
                    double wspd_sfc = Math.Sqrt(Math.Pow(uwnd_sfc, 2) + Math.Pow(vwnd_sfc, 2));

                    if (tcld < 0.0)
                    {
                        tcld = 0.0;
                    }

                    //Save 2D surface meteorological fields to list
                    wx_list2d[0] = olr;
                    wx_list2d[1] = tcld;
                    wx_list2d[2] = precipw;
                    wx_list2d[3] = rain;
                    wx_list2d[4] = mslp;
                    wx_list2d[5] = tsfc;
                    wx_list2d[6] = rhsfc;
                    wx_list2d[7] = wspd_sfc;
                    return;
                }
                if (FlightGlobals.ready && FlightGlobals.ActiveVessel != null)
                {
                    double u; double v; double w; double t; double p; double rh; double vis; double cldfrac; double d;
                    //Retrieve 3D meteorological fields
                    weather_api.wx_atm weather_data3D = _wx_api.getAmbientWx3D(vheight);
                    u = weather_data3D.wind_x; v = weather_data3D.wind_y; w = weather_data3D.wind_z; t = weather_data3D.temperature; d = weather_data3D.density;
                    p = weather_data3D.pressure; rh = weather_data3D.humidity; vis = weather_data3D.visibility; cldfrac = weather_data3D.cloudcover;
                    if (((vessel.LandedOrSplashed) || (vessel.heightFromTerrain < 50)) && (disable_surface_wind))
                    {
                        u = 0; v = 0; w = 0;
                    }

                    //Override MPAS wind if a wind profile is selected/enabled.
                    if (cnst_wnd)
                    {
                        //Compute wind components
                        u = -wspd_prof *Math.Sin((Math.PI / 180.0) *wdir_prof);

                        v = -wspd_prof *Math.Cos((Math.PI / 180.0) *wdir_prof);

                        w = 0; //no vertical motion
                    }

                    //Retrieve 2D meteorological fields
                    double             olr; double precipw; double mslp; double sst; double tcld; double rain; double tsfc; double rhsfc; double uwnd_sfc; double vwnd_sfc;
                    weather_api.wx_srf weather_data2D = _wx_api.getAmbientWx2D();

                    olr          = weather_data2D.olr; tcld = weather_data2D.cloudcover; mslp = weather_data2D.mslp; precipw = weather_data2D.precitable_water; rain = weather_data2D.precipitation_rate;
                    sst          = weather_data2D.sst; tsfc = weather_data2D.temperature; rhsfc = weather_data2D.humidity; uwnd_sfc = weather_data2D.wind_x; vwnd_sfc = weather_data2D.wind_y;
                    windVector.x = (float)v; //North
                    windVector.y = (float)w; //Up
                    windVector.z = (float)u; //East
                    windVectorWS = worldframe * windVector;
                    //Compute horizontal wind speed
                    double wspd_sfc = Math.Sqrt(Math.Pow(uwnd_sfc, 2) + Math.Pow(vwnd_sfc, 2));

                    if (tcld < 0)
                    {
                        tcld = 0;
                    }

                    //3D atmospheric fields
                    wx_list3d[0] = v;
                    wx_list3d[1] = u;
                    wx_list3d[2] = w;
                    wx_list3d[3] = p;
                    wx_list3d[4] = t;
                    wx_list3d[5] = rh;
                    wx_list3d[6] = vis;
                    wx_list3d[7] = cldfrac;
                    wx_list3d[8] = d;

                    //2D atmospheric fields
                    wx_list2d[0] = olr;
                    wx_list2d[1] = tcld;
                    wx_list2d[2] = precipw;
                    wx_list2d[3] = rain;
                    wx_list2d[4] = mslp;
                    wx_list2d[5] = tsfc;
                    wx_list2d[6] = rhsfc;
                    wx_list2d[7] = wspd_sfc;

                    //Get vehicle/wind relative velocity
                    double             vel_ias; double vel_tas; double vel_eas; double vel_grnd; double vel_par; double vel_prp;
                    weather_api.wx_vel vdata;
                    if (cnst_wnd)
                    {
                        vdata = _wx_api.getVehicleVelCnst(vessel, u, v, w, wx_enabled);
                    }
                    else
                    {
                        vdata = _wx_api.getVehicleVel(vessel, vheight, wx_enabled);
                    }

                    vel_ias     = vdata.vel_ias; vel_tas = vdata.vel_tas; vel_eas = vdata.vel_eas; vel_grnd = vdata.vel_grnd; vel_par = vdata.vel_par; vel_prp = vdata.vel_prp;
                    vel_list[0] = vel_ias;  //Indicated airspeed
                    vel_list[1] = vel_tas;  //True airspeed (accounts for FAR Wind)
                    vel_list[2] = vel_eas;  //Equivalent Airspeed
                    vel_list[3] = vel_grnd; //Surface velocity (i.e. ground speed)
                    vel_list[4] = vel_par;  //Component of wind perpendicular to aircraft
                    vel_list[5] = vel_prp;  //Component of wind parallel to aircraft

                    Vector3d vwrld = vessel.GetWorldPos3D();
                    if (aero == false)
                    {
                        //3D atmospheric fields
                        wx_list3d[0] = 0;
                        wx_list3d[1] = 0;
                        wx_list3d[2] = 0;
                    }
                    if (thermo == false)
                    {
                        //3D atmospheric fields
                        wx_list3d[3] = Util.GetPressure(vwrld) * 1000;
                        wx_list3d[4] = Util.GetTemperature(vwrld);
                        wx_list3d[5] = 0;
                        wx_list3d[6] = 0;
                        wx_list3d[7] = 0;
                        wx_list3d[8] = Util.GetDensity(vwrld);
                    }
                }
                else
                {
                    windVectorWS = Vector3.zero;
                }
            }
            else
            {
                //Get vehicle/wind relative velocity
                double             vel_ias; double vel_tas; double vel_eas; double vel_grnd; double vel_par; double vel_prp;
                weather_api.wx_vel vdata;
                if (cnst_wnd)
                {
                    vdata = _wx_api.getVehicleVelCnst(vessel, 0, 0, 0, wx_enabled);
                }
                else
                {
                    vdata = _wx_api.getVehicleVel(vessel, vheight, wx_enabled);
                }

                vel_ias     = vdata.vel_ias; vel_tas = vdata.vel_tas; vel_eas = vdata.vel_eas; vel_grnd = vdata.vel_grnd; vel_par = vdata.vel_par; vel_prp = vdata.vel_prp;
                vel_list[0] = vel_ias;  //Indicated airspeed
                vel_list[1] = vel_tas;  //True airspeed (accounts for FAR Wind)
                vel_list[2] = vel_eas;  //Equivalent Airspeed
                vel_list[3] = vel_grnd; //Surface velocity (i.e. ground speed)
                vel_list[4] = vel_par;  //Component of wind perpendicular to aircraft
                vel_list[5] = vel_prp;  //Component of wind parallel to aircraft

                Vector3d vwrld = vessel.GetWorldPos3D();
                //3D atmospheric fields
                wx_list3d[0] = 0;
                wx_list3d[1] = 0;
                wx_list3d[2] = 0;
                wx_list3d[3] = Util.GetPressure(vwrld) * 1000;
                wx_list3d[4] = Util.GetTemperature(vwrld);
                wx_list3d[5] = 0;
                wx_list3d[6] = 0;
                wx_list3d[7] = 0;
                wx_list3d[8] = Util.GetDensity(vwrld);

                //2D atmospheric fields
                wx_list2d[0] = 0;
                wx_list2d[1] = 0;
                wx_list2d[2] = 0;
                wx_list2d[3] = 0;
                wx_list2d[4] = 0;
                wx_list2d[5] = 0;
                wx_list2d[6] = 0;
                wx_list2d[7] = 0;

                windVectorWS = Vector3.zero;
            }
        }