SCIPUFF Binary Sampler Output

Steven Schneider, Sage Management

The default sampler output for SCIPUFF is an ASCII file with the project name and .smp extension (i.e. project.smp). The ASCII file, while easy to read and understand, is not well suited to outputting large numbers of samplers, materials, and/or time steps. As an alternative, SCIPUFF has been modified to output a binary sampler file (project.sps).

The sensor location file (project.sam) below highlights the required option for outputting binary results.

SCIPUFF SENSOR 

OPTIONS
  OUTPUT 900 SECONDS
  BINARY
END

CLASS
  CONC MAT_1
END

-90.28528  44.7976  0
-90.22189  44.7976  0
-90.15849  44.7976  0
-90.09509  44.7976  0
...

Unlike the ASCII format, reading the binary sampler file will require knowledge of computer programming. Example code for C# and Visual Basic has been included below for your convenience. The basic outline of the output binary sampler file (project.sps) though is as follows:

file type & version
data position
projection/time parameters
sampler list
variable list
for each time
   for each variable
      for each sampler
         data value
      next
   next
next

The example C# and Visual Basic code below should be able to be easily integrated into a Microsoft Visual Studio project.

using System;
using System.Text;
using System.IO;

        private void ReadBinary()
        {
            //open file
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);

            //file type & version
            string fileType = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
            float fileVersion = br.ReadSingle();

            //data position in file
            long dataPosition = br.ReadInt64();

            //projection/time reference
            string projection = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
            string datum = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
            string horizontalUnits = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
            string verticalUnits = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
            int utmZone = br.ReadInt32();
            string hemisphere = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
            float originLongitude = br.ReadSingle();
            float originLatitude = br.ReadSingle();
            float standardParallel1 = br.ReadSingle();
            float standardParallel2 = br.ReadSingle();
            float azimuth = br.ReadSingle();
            float scaleFactor = br.ReadSingle();
            float falseEasting = br.ReadSingle();
            float falseNorthing = br.ReadSingle();
            int timeZone = br.ReadInt32();
            bool localTime = Convert.ToBoolean(br.ReadInt32()); //1=local, 0=utc

            //samplers
            int samplers = br.ReadInt32();
            for (int i = 1; i <= samplers; i++)
            {
                float x = br.ReadSingle();
                float y = br.ReadSingle();
                float z = br.ReadSingle();
            }

            //variables
            int variables = br.ReadInt32();
            for (int i = 1; i <= variables; i++)
            {
                string varName = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
                string varClass = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
                string varUnit = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32())).Trim();
            }

            //times
            int times = br.ReadInt32();
            for (int i = 1; i <= times; i++)
            {
                //date/time
                int year = br.ReadInt32();
                int month = br.ReadInt32();
                int day = br.ReadInt32();
                int hour = br.ReadInt32();
                int minute = br.ReadInt32();
                int second = br.ReadInt32();
                System.DateTime timeStep = new System.DateTime(year, month, day, hour, minute, second);

                //data length for this time step
                long dataLength = br.ReadInt64();

                //read data
                for (int j = 1; j <= variables; j++)
                {
                    for (int k = 1; k <= samplers; k++)
                    {
                        float value = br.ReadSingle();
                    }
                }

            }

            //close file
            br.Close();
            fs.Close();

        }

.

Imports System.IO
Imports System.Text.Encoding

    Private Sub ReadBinary()

        'open file
        Dim fs As New FileStream(fileName, FileMode.Open, FileAccess.Read)
        Dim br As New BinaryReader(fs)

        'file type & version
        Dim fileType As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
        Dim fileVersion As Single = br.ReadSingle

        'data position in file
        Dim dataPosition As Long = br.ReadInt64

        'projection/time reference
        Dim projection As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
        Dim datum As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
        Dim horizontalUnits As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
        Dim verticalUnits As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
        Dim utmZone As Integer = br.ReadInt32
        Dim hemisphere As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
        Dim originLongitude As Single = br.ReadSingle
        Dim originLatitude As Single = br.ReadSingle
        Dim standardParallel1 As Single = br.ReadSingle
        Dim standardParallel2 As Single = br.ReadSingle
        Dim azimuth As Single = br.ReadSingle
        Dim scaleFactor As Single = br.ReadSingle
        Dim falseEasting As Single = br.ReadSingle
        Dim falseNorthing As Single = br.ReadSingle
        Dim timeZone As Integer = br.ReadInt32
        Dim localTime As Boolean = CBool(br.ReadInt32) '1=local, 0=utc

        'samplers
        Dim samplers As Integer = br.ReadInt32
        For i As Integer = 1 To samplers
            Dim x As Single = br.ReadSingle
            Dim y As Single = br.ReadSingle
            Dim z As Single = br.ReadSingle
        Next i

        'variables
        Dim variables As Integer = br.ReadInt32
        For i As Integer = 1 To variables
            Dim varName As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
            Dim varClass As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
            Dim varUnit As String = ASCII.GetString(br.ReadBytes(br.ReadInt32)).Trim
        Next i

        'times
        Dim times As Integer = br.ReadInt32
        For i As Integer = 1 To times

            'date/time
            Dim year As Integer = br.ReadInt32
            Dim month As Integer = br.ReadInt32
            Dim day As Integer = br.ReadInt32
            Dim hour As Integer = br.ReadInt32
            Dim minute As Integer = br.ReadInt32
            Dim second As Integer = br.ReadInt32
            Dim timeStep As Date = New Date(year, month, day, hour, minute, second)

            'data length for this time step
            Dim dataLength As Long = br.ReadInt64

            'read data
            For j As Integer = 1 To variables
                For k As Integer = 1 To samplers
                    Dim value As Single = br.ReadSingle
                Next k
            Next j

        Next i

        'close file
        br.Close()
        fs.Close()

    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *