//Satellite helpers public static void ChangeSatelliteInterval(IAgSatellite sat, string startTime, string stopTime, bool astgRun) { AgEVePropagatorType propType = sat.PropagatorType; //IAgVePropagator prop = sat.Propagator; switch (propType) { case AgEVePropagatorType.eUnknownPropagator: break; case AgEVePropagatorType.ePropagatorHPOP: IAgVePropagatorHPOP prop = sat.Propagator as IAgVePropagatorHPOP; prop.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop.Propagate(); break; case AgEVePropagatorType.ePropagatorJ2Perturbation: IAgVePropagatorJ2Perturbation prop1 = sat.Propagator as IAgVePropagatorJ2Perturbation; prop1.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop1.Propagate(); break; case AgEVePropagatorType.ePropagatorJ4Perturbation: IAgVePropagatorJ4Perturbation prop2 = sat.Propagator as IAgVePropagatorJ4Perturbation; prop2.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop2.Propagate(); break; case AgEVePropagatorType.ePropagatorLOP: IAgVePropagatorLOP prop3 = sat.Propagator as IAgVePropagatorLOP; prop3.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop3.Propagate(); break; case AgEVePropagatorType.ePropagatorSGP4: IAgVePropagatorSGP4 prop4 = sat.Propagator as IAgVePropagatorSGP4; prop4.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop4.Propagate(); break; case AgEVePropagatorType.ePropagatorSPICE: IAgVePropagatorSPICE prop5 = sat.Propagator as IAgVePropagatorSPICE; prop5.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop5.Propagate(); break; case AgEVePropagatorType.ePropagatorStkExternal: IAgVePropagatorStkExternal prop6 = sat.Propagator as IAgVePropagatorStkExternal; //prop6.EphemerisInterval.SetExplicitInterval(startTime, stopTime); break; case AgEVePropagatorType.ePropagatorTwoBody: IAgVePropagatorTwoBody prop7 = sat.Propagator as IAgVePropagatorTwoBody; prop7.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop7.Propagate(); break; case AgEVePropagatorType.ePropagatorUserExternal: break; case AgEVePropagatorType.ePropagatorGreatArc: IAgVePropagatorGreatArc prop8 = sat.Propagator as IAgVePropagatorGreatArc; prop8.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop8.Propagate(); break; case AgEVePropagatorType.ePropagatorBallistic: break; case AgEVePropagatorType.ePropagatorSimpleAscent: break; case AgEVePropagatorType.ePropagatorAstrogator: if (astgRun) { IAgVADriverMCS driver = sat.Propagator as IAgVADriverMCS; driver.RunMCS(); } break; case AgEVePropagatorType.ePropagatorRealtime: break; case AgEVePropagatorType.ePropagatorGPS: IAgVePropagatorGPS prop9 = sat.Propagator as IAgVePropagatorGPS; prop9.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop9.Propagate(); break; case AgEVePropagatorType.ePropagatorAviator: break; case AgEVePropagatorType.ePropagator11Param: IAgVePropagator11Param prop10 = sat.Propagator as IAgVePropagator11Param; prop10.EphemerisInterval.SetExplicitInterval(startTime, stopTime); prop10.Propagate(); break; case AgEVePropagatorType.ePropagatorSP3: IAgVePropagatorSP3 prop11 = sat.Propagator as IAgVePropagatorSP3; //prop11.EphemerisInterval.SetExplicitInterval(startTime, stopTime); break; default: break; } }
private void Generate_Click(object sender, EventArgs e) { CommonData.StkRoot.ExecuteCommand("BatchGraphics * On"); IAgConstellation myConst; if ((string)ImportType.SelectedItem == "TLE/TCE") { string filepath = TCEFile.Text; IAgScenario scenario = (IAgScenario)CommonData.StkRoot.CurrentScenario; string startTime = scenario.StartTime; string stopTime = scenario.StopTime; string constellation = null; string cmd; try { IAgExecCmdResult result = CommonData.StkRoot.ExecuteCommand("DoesObjExist / */Constellation/Assets"); if ((string)ConstType.SelectedItem == "Default Constellation") { if (result[0] == "0") { IAgConstellation assets = (IAgConstellation)CommonData.StkRoot.CurrentScenario.Children.New(AgESTKObjectType.eConstellation, "Assets"); } constellation = "Assets"; } else if ((string)ConstType.SelectedItem == "Existing Constellation") { result = CommonData.StkRoot.ExecuteCommand("DoesObjExist / */Constellation/" + ExistingConst.Text); if (result[0] == "0") { IAgConstellation assets = (IAgConstellation)CommonData.StkRoot.CurrentScenario.Children.New(AgESTKObjectType.eConstellation, ExistingConst.Text); } constellation = ExistingConst.Text; } else if ((string)ConstType.SelectedItem == "New Constellation") { result = CommonData.StkRoot.ExecuteCommand("DoesObjExist / */Constellation/" + ConstName.Text); if (result[0] == "0") { IAgConstellation assets = (IAgConstellation)CommonData.StkRoot.CurrentScenario.Children.New(AgESTKObjectType.eConstellation, ConstName.Text); } constellation = ConstName.Text; } //must parse satellites into constellation because of bug associated with ImportTLEFile connect command //Get list of sats prior to import result = CommonData.StkRoot.ExecuteCommand("ShowNames * Class Satellite"); string[] prevSatArray = null; if (result[0] != "None") { prevSatArray = result[0].Split(null); } //Import TLE cmd = "ImportTLEFile * " + "\"" + filepath + "\"" + " AutoPropagate On TimeStep 30.0 StartStop " + "\"" + startTime + "\" " + "\"" + stopTime + "\""; CommonData.StkRoot.ExecuteCommand(cmd); myConst = CommonData.StkRoot.GetObjectFromPath("Constellation/" + constellation) as IAgConstellation; IAgSatellite sat; //Compare prev satellite list to new satellite list result = CommonData.StkRoot.ExecuteCommand("ShowNames * Class Satellite"); string[] newSatArray = null; if (result[0] != "None") { newSatArray = result[0].Split(null); } if (prevSatArray == null) { if (newSatArray != null) { foreach (var item in newSatArray) { if (item != "") { string newItem = item.Split('/').Last(); string objPath = "Satellite/" + newItem; myConst.Objects.Add(objPath); sat = CommonData.StkRoot.GetObjectFromPath(item.ToString()) as IAgSatellite; if (CoordSystem.SelectedIndex == 0) { sat.VO.OrbitSystems.FixedByWindow.IsVisible = true; sat.VO.OrbitSystems.InertialByWindow.IsVisible = false; } cmd = "VO */" + item.ToString() + " ModelDetail Set ModelLabel 2000000000 MarkerLabel 2000000000"; CommonData.StkRoot.ExecuteCommand(cmd); } } } } else { bool exists = false; foreach (var newSat in newSatArray) { if (newSat != "") { exists = false; foreach (var prevSat in prevSatArray) { if (prevSat == newSat) { exists = true; } } if (exists != true) { string newItem = newSat.Split('/').Last(); string objPath = "Satellite/" + newItem; myConst.Objects.Add(objPath); sat = CommonData.StkRoot.GetObjectFromPath(objPath) as IAgSatellite; if (CoordSystem.SelectedIndex == 0) { sat.VO.OrbitSystems.FixedByWindow.IsVisible = true; sat.VO.OrbitSystems.InertialByWindow.IsVisible = false; } cmd = "VO */" + objPath + " ModelDetail Set ModelLabel 2000000000 MarkerLabel 2000000000"; CommonData.StkRoot.ExecuteCommand(cmd); } } } } } catch (Exception) { MessageBox.Show("Could not load asset(s)"); } } else if ((string)ImportType.SelectedItem == "Orbital Elements") { int check = FieldCheck(); if (check == 0) { int errorNum = 0; try { errorNum = 1; IAgStkObject sat = CreatorFunctions.GetCreateSatellite(NameValue.Text); IAgSatellite mySat = sat as IAgSatellite; if (CoordSystem.SelectedIndex == 0) { mySat.VO.OrbitSystems.FixedByWindow.IsVisible = true; mySat.VO.OrbitSystems.InertialByWindow.IsVisible = false; string cmd = "VO */Satellite/" + NameValue.Text + " ModelDetail Set ModelLabel 2000000000 MarkerLabel 2000000000"; CommonData.StkRoot.ExecuteCommand(cmd); } ((IAgSatellite)mySat).SetPropagatorType(AgEVePropagatorType.ePropagatorJ2Perturbation); IAgOrbitStateClassical keplerian; keplerian = ((IAgOrbitStateClassical)((IAgVePropagatorJ2Perturbation)((IAgSatellite)mySat).Propagator).InitialState.Representation.ConvertTo(AgEOrbitStateType.eOrbitStateClassical)); keplerian.SizeShapeType = AgEClassicalSizeShape.eSizeShapeSemimajorAxis; ((IAgVePropagatorJ2Perturbation)((IAgSatellite)mySat).Propagator).InitialState.Representation.AssignClassical(AgECoordinateSystem.eCoordinateSystemICRF, Double.Parse(SMAValue.Text), Double.Parse(EccValue.Text), Double.Parse(IncValue.Text), Double.Parse(AoPValue.Text), Double.Parse(RAANValue.Text), Double.Parse(TAValue.Text)); ((IAgVePropagatorJ2Perturbation)((IAgSatellite)mySat).Propagator).Propagate(); IAgExecCmdResult result = CommonData.StkRoot.ExecuteCommand("DoesObjExist / */Constellation/Assets"); errorNum = 2; if ((string)ConstType.SelectedItem == "Default Constellation") { IAgStkObject conste = CreatorFunctions.GetCreateConstellation("Assets"); IAgConstellation assets = conste as IAgConstellation; string objPath = "Satellite/" + NameValue.Text; assets.Objects.Add(objPath); } else if ((string)ConstType.SelectedItem == "Existing Constellation") { IAgConstellation assets = (IAgConstellation)CommonData.StkRoot.GetObjectFromPath("Constellation/" + ExistingConst.Text); string objPath = "Satellite/" + NameValue.Text; assets.Objects.Add(objPath); } else if ((string)ConstType.SelectedItem == "New Constellation") { IAgConstellation assets = (IAgConstellation)CommonData.StkRoot.CurrentScenario.Children.New(AgESTKObjectType.eConstellation, ConstName.Text); string objPath = "Satellite/" + NameValue.Text; assets.Objects.Add(objPath); } } catch (Exception) { string errorMes = null; if (errorNum == 1) { errorMes = "Could not load satellite- Improper Orbital Elements"; } else if (errorNum == 2) { errorMes = "Could not add satellite to constellation"; } MessageBox.Show(errorMes); } } } else if ((string)ImportType.SelectedItem == "Satellite Database") { string sscString = null; string satName = null; int errorNum = 0; string errorSsc = null; string objPath = null; IAgSatellite mySat = null; IAgConstellation assets = null; IAgScenario scenario = CommonData.StkRoot.CurrentScenario as IAgScenario; string startTime = scenario.StartTime; string stopTime = scenario.StopTime; string cmd; int errorId = 0; CommonData.StkRoot.ExecuteCommand("BatchGraphics * On"); foreach (var item in _selectedSatList) { //Determine the proper satellite name based on catalog errorId = 5; int index = CommonData.SatCatItemList.IndexOf(CommonData.SatCatItemList.Where(p => p.Ssc == item).FirstOrDefault()); SatCatItem currentSat = CommonData.SatCatItemList[index]; if (currentSat.OtherName != "Unspecified") { string otherName = currentSat.OtherName.Replace(" ", "_"); otherName = Regex.Replace(otherName, @"[^0-9a-zA-Z_]+", ""); //CommonData.StkRoot.ExecuteCommand("Rename */" + objPath + " " + otherName); objPath = "Satellite/" + otherName; satName = otherName; } else if (currentSat.CommonName != "Unspecified") { string commonName = currentSat.CommonName.Replace(" ", "_"); commonName = Regex.Replace(commonName, @"[^0-9a-zA-Z_]+", ""); objPath = "Satellite/" + commonName; satName = commonName; } else { objPath = "Satellite/" + item; satName = item; } //SSCString = "SSCNumber " + item + " "; //cmd = "ImportTLEFile * " + "\"" + CommonData.SatDatabaseLocation + "\"" +SSCString+ "AutoPropagate On TimeStep 30.0 StartStop " + "\"" + startTime + "\" " + "\"" + stopTime + "\""; try { errorId = 1; //Create or get handle to satellite based on given name mySat = (IAgSatellite)CreatorFunctions.GetCreateSatellite(satName); //Erase old TLE data and replace it with new data from specified file mySat.SetPropagatorType(AgEVePropagatorType.ePropagatorSGP4); IAgVePropagatorSGP4 tleprop = mySat.Propagator as IAgVePropagatorSGP4; tleprop.Segments.RemoveAllSegs();//clear before adding new tleprop.CommonTasks.AddSegsFromFile(item, CommonData.Preferences.SatDatabaseLocation); tleprop.Propagate(); mySat.Graphics.Resolution.Orbit = 20; //Change trajectory representation to fixed if required. Inertial by default if (CoordSystem.SelectedIndex == 0) { mySat.VO.OrbitSystems.FixedByWindow.IsVisible = true; mySat.VO.OrbitSystems.InertialByWindow.IsVisible = false; string cmd1 = "VO */Satellite/" + satName + " ModelDetail Set ModelLabel 2000000000 MarkerLabel 2000000000"; CommonData.StkRoot.ExecuteCommand(cmd1); } else if (CoordSystem.SelectedIndex == 1) { mySat.VO.OrbitSystems.FixedByWindow.IsVisible = false; mySat.VO.OrbitSystems.InertialByWindow.IsVisible = true; string cmd1 = "VO */Satellite/" + satName + " ModelDetail Set ModelLabel 2000000000 MarkerLabel 2000000000"; CommonData.StkRoot.ExecuteCommand(cmd1); } //Get handle to constellation or create if necessary. Add satellite to constellation if ((string)ConstType.SelectedItem == "Default Constellation") { errorId = 2; assets = CreatorFunctions.GetCreateConstellation("Assets") as IAgConstellation; } else if ((string)ConstType.SelectedItem == "Existing Constellation") { errorId = 3; assets = CreatorFunctions.GetCreateConstellation(ExistingConst.Text) as IAgConstellation; } else if ((string)ConstType.SelectedItem == "New Constellation") { errorId = 4; assets = CreatorFunctions.GetCreateConstellation(ConstName.Text) as IAgConstellation; } if (!assets.Objects.Contains("Satellite/" + satName)) { objPath = "Satellite/" + satName; assets.Objects.Add(objPath); } errorId = 8; CreatorFunctions.ChangeSatColor(objPath, index); //Create sensor if applicable. Place sensor in appropiate constellation for sensors if (currentSat.Fov != 0 && SensorToggle.Checked) { try { errorId = 6; IAgStkObject assetsObject = assets as IAgStkObject; IAgStkObject sat = CommonData.StkRoot.GetObjectFromPath(objPath); IAgSensor sensor = CreatorFunctions.GetCreateSensor(sat, sat.InstanceName + "_Sensor") as IAgSensor; IAgStkObject sensorObject = sensor as IAgStkObject; sensor.CommonTasks.SetPatternSimpleConic(currentSat.Fov / 2, 1); sensor.VO.ProjectionType = AgESnVOProjectionType.eProjectionEarthIntersections; sensor.VO.PercentTranslucency = 70; errorId = 7; assets = CreatorFunctions.GetCreateConstellation(assetsObject.InstanceName + "_Sensors") as IAgConstellation; try { assets.Objects.AddObject(sensorObject); } catch (Exception) { //Likely already in the constellation } } catch (Exception) { } } } catch (Exception) { string errorIdString = null; if (errorId == 1) { errorIdString = "- Could not load from database"; } else if (errorId == 2) { errorIdString = "- Could not add to Default constellation"; } else if (errorId == 3) { errorIdString = "- Could not add to existing constellation"; } else if (errorId == 4) { errorIdString = "- Could not add to new constellation"; } else if (errorId == 5) { errorIdString = "- Could not change satellite name"; } else if (errorId == 6) { errorIdString = "- Could not load sensor or update sensor properties"; } else if (errorId == 7) { errorIdString = "- Could not add sensor to constellation"; } else if (errorId == 8) { errorIdString = "- Could not change satellite color"; } errorSsc = errorSsc + item + errorIdString + "\n\r"; errorNum++; } } if (errorNum != 0) { MessageBox.Show("Error loading the following satellites: \n\r" + errorSsc); } CommonData.StkRoot.ExecuteCommand("BatchGraphics * Off"); } else if ((string)ImportType.SelectedItem == "Ephemeris") { try { IAgConstellation assets = null; string constellation = null; if ((string)ConstType.SelectedItem == "Default Constellation") { IAgStkObject obj = CreatorFunctions.GetCreateConstellation("Assets"); assets = obj as IAgConstellation; constellation = "Assets"; } else if ((string)ConstType.SelectedItem == "Existing Constellation") { IAgStkObject obj = CreatorFunctions.GetCreateConstellation(ExistingConst.Text); assets = obj as IAgConstellation; constellation = ExistingConst.Text; } else if ((string)ConstType.SelectedItem == "New Constellation") { IAgStkObject obj = CreatorFunctions.GetCreateConstellation(ConstName.Text); assets = obj as IAgConstellation; constellation = ConstName.Text; } string filepath = TCEFile.Text; string[] separator1 = { "\\" }; string[] separator2 = { "." }; string fileName = filepath.Split(separator1, StringSplitOptions.None).Last(); string satName = fileName.Split(separator2, StringSplitOptions.None).First(); satName = Regex.Replace(satName, @"[^0-9a-zA-Z_]+", ""); IAgStkObject satObj = CreatorFunctions.GetCreateSatellite(satName); IAgSatellite sat = satObj as IAgSatellite; sat.SetPropagatorType(AgEVePropagatorType.ePropagatorStkExternal); IAgVePropagatorStkExternal prop = sat.Propagator as IAgVePropagatorStkExternal; prop.Filename = filepath; if (TimeSlip.Checked) { try { prop.Override = true; prop.EphemerisStartEpoch.SetExplicitTime(SlipTime.Text); } catch (Exception) { MessageBox.Show("Could not apply time slip"); } } prop.Propagate(); try { assets.Objects.AddObject(satObj); } catch (Exception) { MessageBox.Show("Could not add to constellation"); } if (CoordSystem.SelectedIndex == 0) { sat.VO.OrbitSystems.FixedByWindow.IsVisible = true; sat.VO.OrbitSystems.InertialByWindow.IsVisible = false; string cmd = "VO */Satellite/" + satName + " ModelDetail Set ModelLabel 2000000000 MarkerLabel 2000000000"; CommonData.StkRoot.ExecuteCommand(cmd); } } catch (Exception) { MessageBox.Show("Error loading ephemeris"); } } CommonData.StkRoot.ExecuteCommand("BatchGraphics * Off"); PopulateExistingConstellations(); }
public static void MergeEphemeris(IAgStkObject[] objectArray, string outputFilePath) { Dictionary <double, IAgStkObject> ephems = new Dictionary <double, IAgStkObject>(); Root.UnitPreferences.SetCurrentUnit("DateFormat", "EpSec"); foreach (IAgStkObject stkObject in objectArray) { if (stkObject is IAgSatellite && (stkObject as IAgSatellite).PropagatorType == AgEVePropagatorType.ePropagatorStkExternal) { IAgVePropagatorStkExternal exProp = (stkObject as IAgSatellite).Propagator as IAgVePropagatorStkExternal; if (!ephems.ContainsKey(double.Parse(exProp.StartTime.ToString()))) { ephems.Add(double.Parse(exProp.StartTime.ToString()), stkObject); } } } List <KeyValuePair <double, IAgStkObject> > testing = ephems.OrderBy(i => i.Key).ToList(); List <string> ephemLines = new List <string>(); for (int i = 0; i < testing.Count(); i++) { IAgDataProviderGroup dp = (IAgDataProviderGroup)testing[i].Value.DataProviders["Cartesian Position"]; IAgDataProvider dpICRF = (IAgDataProvider)dp.Group["ICRF"]; IAgDataPrvTimeVar dpTimeVarying = (IAgDataPrvTimeVar)dpICRF; string stopTime = i < (testing.Count() - 1) ? testing[i + 1].Key.ToString() : ((testing[i].Value as IAgSatellite).Propagator as IAgVePropagatorStkExternal).StopTime.ToString(); string startTime = testing[i].Key.ToString(); IAgDrResult results = dpTimeVarying.ExecNativeTimes(startTime, stopTime); Array times = results.DataSets.GetDataSetByName("Time").GetValues(); Array xs = results.DataSets.GetDataSetByName("x").GetValues(); Array ys = results.DataSets.GetDataSetByName("y").GetValues(); Array zs = results.DataSets.GetDataSetByName("z").GetValues(); dp = (IAgDataProviderGroup)testing[i].Value.DataProviders["Cartesian Velocity"]; dpICRF = (IAgDataProvider)dp.Group["ICRF"]; dpTimeVarying = (IAgDataPrvTimeVar)dpICRF; results = dpTimeVarying.ExecNativeTimes(startTime, stopTime); Array xds = results.DataSets.GetDataSetByName("x").GetValues(); Array yds = results.DataSets.GetDataSetByName("y").GetValues(); Array zds = results.DataSets.GetDataSetByName("z").GetValues(); for (int j = 0; j < (times.Length - 2); j++) { ephemLines.Add(string.Format("{0} {1} {2} {3} {4} {5} {6}", times.GetValue(j), xs.GetValue(j), ys.GetValue(j), zs.GetValue(j), xds.GetValue(j), yds.GetValue(j), zds.GetValue(j))); } //MessageBox.Show("stored values"); } int count = ephemLines.Count; ephemLines.Insert(0, "stk.v.10.0"); ephemLines.Insert(1, "BEGIN Ephemeris"); ephemLines.Insert(2, "NumberOfEphemerisPoints " + count); ephemLines.Insert(3, "InterpolationMethod Lagrange"); ephemLines.Insert(4, "InterpolationOrder 5"); ephemLines.Insert(5, "ScenarioEpoch " + Root.ConversionUtility.ConvertDate(Root.UnitPreferences.GetCurrentUnitAbbrv("DateFormat"), "UTCG", (Root.CurrentScenario as IAgScenario).Epoch.ToString())); ephemLines.Insert(6, "CentralBody Earth"); ephemLines.Insert(7, "CoordinateSystem ICRF"); ephemLines.Insert(8, "DistanceUnit Meters"); ephemLines.Insert(9, "EphemerisTimePosVel"); ephemLines.Add("END Ephemeris"); File.WriteAllLines(outputFilePath, ephemLines.ToArray()); }