Files

Top  Previous  Next

FreeFlyer provides a variety of interfaces for interacting with external files. For information on working with JSON data, please see the JSON-Formatted Data page.

 

The following Sample Mission Plans (included with your FreeFlyer installation) demonstrate the use of the FileInterface object:

 

Interfacing with External Resources Samples

Excel File Interface

File Interface

 

FreeFlyer Data Files


For information on FreeFlyer Data Files and Formats, including Ephemeris Files, see the Data Files reference in the Appendix. See also Working with Ephemerides.

 

 

Creating External Object Files


For information on saving the state of a FreeFlyer object to an external file, see the Put command. See the Get command for information on retrieving states from a file. The Object.PutToFile(), Object.PutToString(), Object.GetFromFile(), and Object.GetFromString() methods can also be used in place of the Put and Get commands.

 

Note: The Save and Restore commands provide similar functionality to the Put and Get commands, while saving object data to internal memory instead of external files.

 

 

Determining Information about External Files and Directories


The FileSystem object can be used to gather information about files and directories, such as whether a file exists in a directory or when a file was last modified. The FileSystem object cannot be created and is only accessible via FreeForm script.

 

This example shows how the FileSystem object can be used to create a copy of a file:

 

Spacecraft Spacecraft1;

ReportInterface ReportInterface1;

 

ReportInterface1.Filename = "temp.txt";

 

While (Spacecraft1.ElapsedTime < TS(1 days));

    Report Spacecraft1.EpochText, Spacecraft1.Position, Spacecraft1.Velocity to ReportInterface1;

    Step Spacecraft1;

End;

 

Close ReportInterface1;   // Close the file to allow the FileSystem object to control

 

FileSystem.Copy(ReportInterface1.Filename, "temp2.txt");   // Create a copy of the file in the same directory

 

The FileSystem object can be used to delete a file:

 

FileSystem.Delete("temp2.txt");   // Delete file

 

The FileSystem object can be used to create a directory:

 

FileSystem.CreateDirectory("./destination");   // Create a directory

 

The FileSystem object can be used to move and rename a file:

 

FileSystem.Move(ReportInterface1.Filename, "./destination/file.txt");   // Move the file and rename it

 

Reading Arbitrary File Formats


Two mechanisms are available to open and parse input files. The first mechanism is the simple StringArray.ReadFromFile method. The second mechanism is the more generic and more functional FileInterface object.

 

Using these functionalities, you can:

1.Examine iput and output files.

2.Read arbitrary ephemeris file formats.

3.Integrate into other systems by reading their relative formats.

 

To help parse data out of each line read, review the StringTokenizer object and the String.Split method. See Parsing Arbitrary String Data for more information.

 

Note: The StringArray and FileInterface objects also support the creation of arbitrarily formatted output files. Refer to StringArray.WriteToFile, FileInterface.WriteMode, and FileInterface.Write to get started.

 

Example 1: StringArray ReadFromFile Method

The StringArray.ReadFromFile method is a convenient way to read small ASCII files without having to worry about the details of the format of the file. In this example, a data file is read into a StringArray called fileContents, and then each line is parsed into individual data elements in the lineContents StringArray.

 

Input File: myFile.txt

 

Spacecraft1.A        Variable1

     km                      

 7088.4366419       0.0000000

 7088.1359928       2.0000000

 7087.8048512       4.0000000

 7087.4445534       6.0000000

 7087.0565503       8.0000000

 7086.6424014      10.0000000

 

Mission Plan

 

StringArray fileContents[0];

StringArray lineContents[0];

Variable numLines;

Variable i = 0;

 

//Read the entire contents of the file and place each line into an element of "fileContents"

numLines = fileContents.ReadFromFile("myFile.txt");

 

While (i < numLines);

    //Split each line into entries, using spaces as the delimiter

    fileContents[i].Split(" ",lineContents);

 

    //Ignore the first two lines of the file (Headers) and report the parsed data

    If (i > 1);

          Report lineContents[0], lineContents[1];

    End;

 

    i++;

End;

 

Output Report

 

lineContents[0]

lineContents[1]

7088.4366419

0.0000000

7088.1359928

2.0000000

7087.8048512

4.0000000

7087.4445534

6.0000000

7087.0565503

8.0000000

7086.6424014

10.0000000

 

Note: The StringArray object also has a StringArray.WriteToFile method, which writes the elements of a StringArray to a file. Each element becomes a new line in the file.

 

Example 2: The FileInterface Object

The FileInterface object has many options that provide the ability to read and write ASCII and binary files. The image below shows an example of the FileInterface object editor, which allows the user to configure a FileInterface's filename, read/write modes, format flags, and other properties.

 

FileInterface Object Editor

FileInterface Object Editor

 

In this example, additional acceleration data is retrieved from a randomly formatted external file called "UnmodAccel.txt", and this data is assigned to a Spacecraft ForceModel's "Other Accelerations" properties.

 

Input File: UnmodAccel.txt

 

OtherAccelX  OtherAccelY OtherAccelZ OtherAccelRateX OtherAccelRateY OtherAccelRateZ

0.1 0.12 0 0.01 0.02 0

 

Mission Plan

 

FileInterface myFileInterface;

Variable status;

StringArray lineContents;

String line;

 

myFileInterface.Filename = "UnmodAccel.txt";  // Defining the filename

myFileInterface.ReadMode = 1;                 // Set Mode to "Read"

 

status = myFileInterface.Open;   //Open the FileInterface

line = myFileInterface.GetLine;  //Retrieve the first line of the file, but ignore it

line = myFileInterface.GetLine;  //Retrieve the next line (second line) of the file

 

line.Split(" ", lineContents);   //Splits the contents of the second line and stores each portion as an element in a StringArray

 

Alias forcemodel = (Spacecraft1.Propagator AsType Integrator).ForceModel;

 

//Set the Spacecraft ForceModel "Other Accelerations" using the elements of the StringArray

forcemodel.OtherAccelerations[0]     = StringToFloat(lineContents[0]);

forcemodel.OtherAccelerations[1]     = StringToFloat(lineContents[1]);

forcemodel.OtherAccelerations[2]     = StringToFloat(lineContents[2]);

forcemodel.OtherAccelerationsRate[0] = StringToFloat(lineContents[3]);

forcemodel.OtherAccelerationsRate[1] = StringToFloat(lineContents[4]);

forcemodel.OtherAccelerationsRate[2] = StringToFloat(lineContents[5]);

 

//Verify that the Force Model's Other Accelerations reflect the values read from the external file

Watch forcemodel.OtherAccelerations[0],

      forcemodel.OtherAccelerations[1],

      forcemodel.OtherAccelerations[2],

      forcemodel.OtherAccelerationsRate[0],

      forcemodel.OtherAccelerationsRate[1],

      forcemodel.OtherAccelerationsRate[2];

 

Output Report

 

Spacecraft1_ForceModel.OtherAccelerations[0]

0.1

Spacecraft1_ForceModel.OtherAccelerations[1]

0.12

Spacecraft1_ForceModel.OtherAccelerations[2]

0

Spacecraft1_ForceModel.OtherAccelerationsRate[0]

0.01

Spacecraft1_ForceModel.OtherAccelerationsRate[1]

0.02

Spacecraft1_ForceModel.OtherAccelerationsRate[2]

0

 

 

See Also


StringArray Properties and Methods

FileInterface Properties and Methods

FileSystem Properties and Methods

StringTokenizer Properties and Methods

Parsing Arbitrary String Data

JSON-Formatted Data