Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Configuration '<- Check if this import is needed
- Imports System.Linq
- Imports System.IO
- Imports System.IO.Compression
- Public Class frmMain
- ' Allows Folder Monitor to set the visibility
- Private isVisible As Boolean = False
- Public isHidden As Boolean = False
- Public isIgnoreCommandArguments As Boolean = False
- 'Notifiy SEDBE File Monitor
- Public Event ConversionCompleted(pct As Integer, timeLeft As String, isBusy As Boolean, filename As String) 'pct between 0 and 100, timeLeft in seconds
- Public Event ErrorReport(message As String)
- Private Enum enmTasks
- DoNothing
- ProcessDragAndDropFiles
- ProcessFolderfile
- End Enum
- Private Enum enmThresholdType
- CPFA
- SNR
- End Enum
- Private Enum enmWindow
- Rectangular
- Hann
- Blackman
- FlatTop
- End Enum
- Private Enum enmFrame_Avg
- PERIODIGRAM
- POLYPHASE
- OVERLAP50%
- End Enum
- Private Structure udtHeader
- Public VERSION As Single
- Public SENSOR_ROLE As String
- Public SENSOR_OUTPUT As String
- Public SENSOR_HOST_ID As String
- Public SENSOR_TECHNOLOGY As String
- Public SENSOR_HW_ID As String
- Public TASK_MISSION As String
- Public StartDateTime As DateTime
- Public StopDateTime As DateTime
- Public TimeZoneOffset As TimeSpan
- Public TimeZoneCorrection As Boolean
- Public FREQ_RANGE() As Double
- Public LOCATION As String
- End Structure
- Private MyHeader As udtHeader
- Private Structure udtLocationBox
- Public LatMax As Double
- Public LatMin As Double
- Public LonMax As Double
- Public LonMin As Double
- Public AltMax As Double
- Public AltMin As Double
- End Structure
- Private MyBox As udtLocationBox
- Private MyGlobalBox As udtLocationBox
- Private Structure udtSubHeader
- Public REVISIT_TIME_s As Double
- Public RESOLUTION_BW_Hz As Double
- Public THRESHOLD_TYPE As enmThresholdType
- Public THRESHOLD As Double
- Public BW_ESTIMATION_RATIO As Double
- Public FFT_WINDOW As enmWindow
- Public FRAME_AVG As enmFrame_Avg
- Public FRAME_NB As Integer
- Public ISDF As Boolean
- Public ANALYSIS_DURATION_MIN As Double
- Public MEASUREMENTFILE As String
- End Structure
- Private MySubHeader As udtSubHeader
- Private MyPeriod As Integer
- Private Structure udtRange
- Public Offset As Integer
- Public RangeStart As Integer
- Public RangeStop As Integer
- End Structure
- Private Structure Histos
- Public Power_dBmHisto() As Integer
- Public SNR_dBHisto() As Integer
- Public BandwidthPercentRangeHisto() As Integer
- Public CORangePercentHisto() As Integer
- Public BurstOn_dBmsHisto() As Integer
- Public BurstOff_dBmsHisto() As Integer
- End Structure
- Private MyHisto() As Histos
- Private Structure Channels
- Public BW() As Double
- Public Carrier() As Double
- Public Count() As Integer
- Public ID() As Integer
- Public BurstStateCurrent() As Boolean
- Public BurstStatePrevious() As Boolean
- Public BurstTime() As DateTime
- Public IndexCurrentlyOn() As Integer
- Public IndexPreviouslyOn() As Integer
- End Structure
- Private Structure MeasurementData
- Public FixedDate As String
- Public Latitude As Double
- Public Longitude As Double
- Public Altitude As Double
- Public Status As Integer
- Public MovementStatus As Integer
- Public GroundSpeed As Double
- Public GroundAcceleration As Double
- Public TrackMadeGood As Double
- Public TrueHeading As Double
- Public ChannelNum As Integer
- Public Power As Double
- Public SNR As Double
- Public Freq As Double
- Public BW As Double
- Public Dir As Double
- Public DirQuality As Double
- End Structure
- Private MyChannels As Channels
- Private Power_dBmRange As udtRange
- Private SNR_dBRange As udtRange
- Private BandwidthPercentRange As udtRange
- Private CORangePercentRange As udtRange
- Private Burst_dBmsRange As udtRange
- Private AverageRevisitCount As Integer
- Private IsPointAllowed As Boolean = False
- Private TOTAL_FREQUENCIES As Integer
- Private Description As String
- Private MyTitle As String
- Private isClosing As Boolean
- Private isCanceled As Boolean
- Private MyTimerTask As enmTasks
- Private MyDragAndDropFiles As String()
- Private folderpath As String
- Public Sub New()
- ' This call is required by the Windows Form Designer.
- InitializeComponent()
- ' Add any initialization after the InitializeComponent() call.
- Me.AllowDrop = True ' Enable drag-and-drop operations.
- ' Hide the form by default
- Me.ShowInTaskbar = False
- Me.WindowState = FormWindowState.Minimized
- End Sub
- Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- 'Default values
- MyTitle = My.Application.Info.Title()
- Me.Text = MyTitle
- dlgOpenFile.SelectedPath = ""
- btnCancel.Enabled = False
- lblVersion.Text = MyTitle & " " & My.Application.Info.Version.ToString
- lblCopyright.Text = "Copyright © 2008-2016 Communications Research Centre Canada"
- Dim listFile As New List(Of String)
- Dim MyDragAndDropFiles(0 To 0) As String
- 'Process command line arguments if provided
- 'And then quit
- If isIgnoreCommandArguments = False Then
- If My.Application.CommandLineArgs.Count > 0 Then
- 'Arguments supported:
- '- Invisible /hide
- '- One or more filename with double quotes "c:\temp\mylogfile0.txt" "c:\temp\mylogfile1.txt" "c:\temp\mylogfile2.txt" "c:\temp\mylogfile3.txt"
- For Each argument As String In My.Application.CommandLineArgs
- Select Case argument
- Case "/?"
- DisplayHelpFile()
- Me.Close()
- Case "/hide"
- isHidden = True
- Case Else
- 'This must be a file to convert
- 'Remove double quotes and add to a list of files to be converted
- argument = Replace(argument, """", "")
- If argument.Length > 0 Then
- listFile.Add(argument)
- End If
- End Select
- Next
- End If
- End If
- 'Show the main window if it's not being forced to hide with /hide argument
- If isHidden = False Then
- isVisible = True
- Me.Show()
- Me.ShowInTaskbar = True
- Me.WindowState = FormWindowState.Normal
- End If
- ' Load settings
- cmbPeriod.SelectedIndex = My.Settings.Periode
- txtSite.Text = My.Settings.SiteName
- txtHW.Text = My.Settings.HWName
- ckbHisto.Checked = My.Settings.Histo
- ckbMeasurement.Checked = My.Settings.Measurement
- txtNbFrames.Text = My.Settings.NbFrames
- txtBWRatio.Text = My.Settings.BWRatio
- txtTimezoneOffset.Text = My.Settings.TimeZone
- ckbCorrectForUTC.Checked = My.Settings.TimeZoneCorrection
- ' Start the file conversion
- ' if hidden, start right away
- ' if visible, use a seperate thread (e.g. a timer's threaded event)
- For Each fn As String In listFile
- ConvertFile(fn)
- Next
- 'Close the application once it's done processing command arguments
- 'as part of a batch file script
- If isHidden = True Then
- Me.Close()
- End If
- End Sub
- Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
- 'Inform any running algorithm of the application shutdown
- isClosing = True
- ' Save settings
- My.Settings.Periode = cmbPeriod.SelectedIndex
- My.Settings.SiteName = txtSite.Text
- My.Settings.HWName = txtHW.Text
- My.Settings.Histo = ckbHisto.Checked
- My.Settings.Measurement = ckbMeasurement.Checked
- My.Settings.NbFrames = txtNbFrames.Text
- My.Settings.BWRatio = txtBWRatio.Text
- My.Settings.TimeZone = txtTimezoneOffset.Text
- My.Settings.TimeZoneCorrection = ckbCorrectForUTC.Checked
- My.Settings.Save()
- End Sub
- Private Sub frmMain_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
- 'DirectCast(sender, Form).Visible = isVisible
- If isVisible Then
- Me.ShowInTaskbar = True
- Me.WindowState = FormWindowState.Normal
- Else
- Me.ShowInTaskbar = False
- Me.WindowState = FormWindowState.Minimized
- End If
- End Sub
- Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
- 'Inform any running algorithm of the user's intervention
- isCanceled = True
- btnCancel.Enabled = False
- End Sub
- Private Sub btnOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenFile.Click
- 'Offer the user the choice to pick a file
- If dlgOpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
- folderpath = dlgOpenFile.SelectedPath
- MyTimerTask = enmTasks.ProcessFolderfile
- tmrTimer.Enabled = True
- End If
- End Sub
- Private Sub frmMain_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
- ' If the data is a file, display the copy cursor.
- If e.Data.GetDataPresent(DataFormats.FileDrop) Then
- e.Effect = DragDropEffects.Copy
- Else
- e.Effect = DragDropEffects.None
- End If
- End Sub
- Private Sub frmMain_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
- 'Convert a file given through drag-and-drop
- Try
- Dim Files As String()
- Files = CType(e.Data.GetData(DataFormats.FileDrop), String())
- If Files.Length > 0 Then
- MyDragAndDropFiles = Files
- MyTimerTask = enmTasks.ProcessDragAndDropFiles
- tmrTimer.Enabled = True
- End If
- Catch ex As Exception
- End Try
- End Sub
- Private Function UnzipDirectory(ByVal SourceFilename As String, ByVal Path As String, ByVal IsHisto As Boolean, ByVal IsMeasurement As Boolean) As Boolean
- '"Path" should contain path of folder
- Dim isSuccess As Boolean = True
- Dim dInfo As New DirectoryInfo(SourceFilename)
- Dim OutputFiles As String = Path + "\Output_Files"
- My.Computer.FileSystem.CreateDirectory(OutputFiles)
- 'Walk through the folder and parse each individual file, only looking for .zips
- For Each File In dInfo.GetFiles()
- Dim FileValue As String = SourceFilename + "\" + File.Name
- If InStr(FileValue, ".zip") > 0 Then
- isSuccess = Consolidate(FileValue, SourceFilename, OutputFiles, IsHisto, IsMeasurement)
- End If
- If Not isSuccess Then
- Exit For
- End If
- Next
- Return isSuccess
- End Function
- Private Function Consolidate(ByVal SourceFilename As String, ByVal Path As String, ByVal DestinationPath As String, ByVal IsHisto As Boolean, ByVal IsMeasurement As Boolean) As Boolean
- Dim FailedOutputFilename As String
- Dim OutputFilename As String
- Dim Result As Boolean
- 'Protect the function
- If SourceFilename.Length <= 0 Then
- Return False
- End If
- 'Unzip file
- Dim extractPath As String = Path + "\extract"
- My.Computer.FileSystem.CreateDirectory(extractPath)
- Console.Write("Extracting ")
- Console.WriteLine(SourceFilename)
- ZipFile.ExtractToDirectory(SourceFilename, extractPath)
- 'Get the largest file - this will be the new source file.
- Dim files() As String = Directory.GetFiles(extractPath)
- Dim sorted() As String = files.OrderByDescending(
- New Func(Of String, Integer)(Function(fn As String)
- Return New FileInfo(fn).Length
- End Function)).ToArray
- Dim UnzippedSourceFilename As String = sorted(0)
- 'Lock the GUI
- btnOpenFile.Enabled = False
- lblStatus.ForeColor = Color.Black
- lblSource.Text = UnzippedSourceFilename
- OutputFilename = Replace(UnzippedSourceFilename, ".txt", ".histo")
- FailedOutputFilename = Replace(UnzippedSourceFilename, ".txt", ".fail")
- lblDestination.Text = OutputFilename
- lblStatus.Text = "Consolidation in progress..."
- pgbStatus.Value = 0
- Me.Text = "0" & "% - " & OutputFilename
- btnCancel.Enabled = True
- MyPeriod = CInt(cmbPeriod.Text)
- RaiseEvent ConversionCompleted(pgbStatus.Value, "Estimating", True, OutputFilename) 'Notifiy SEDBE File Monitor
- ConversionCompletedToStdOutput(pgbStatus.Value, "Estimating", True, UnzippedSourceFilename, OutputFilename)
- 'Delete any previously failed conversions
- Try
- If System.IO.File.Exists(FailedOutputFilename) Then
- Kill(FailedOutputFilename)
- End If
- If System.IO.File.Exists(OutputFilename) Then
- Kill(OutputFilename)
- End If
- Catch ex As Exception
- 'Ignore errors
- End Try
- 'Request an MDB conversion
- If IsHisto Then
- Result = ConvertToHisto(UnzippedSourceFilename, OutputFilename, Path, DestinationPath)
- End If
- If IsMeasurement Then
- Result = ConvertToMeasurement(UnzippedSourceFilename, OutputFilename, Path, DestinationPath)
- End If
- 'Delete extraneous directories
- My.Computer.FileSystem.DeleteDirectory(extractPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
- If Result Then
- lblStatus.Text = "Consolidation successful!"
- lblStatus.ForeColor = Color.Green
- pgbStatus.Value = 100
- RaiseEvent ConversionCompleted(pgbStatus.Value, lblStatus.Text, False, OutputFilename) 'Notifiy SEDBE File Monitor
- ConversionCompletedToStdOutput(pgbStatus.Value, lblStatus.Text, False, SourceFilename, OutputFilename)
- Else
- lblStatus.Text = "Consolidation failed."
- lblStatus.ForeColor = Color.Red
- RaiseEvent ConversionCompleted(pgbStatus.Value, lblStatus.Text, False, OutputFilename) 'Notifiy SEDBE File Monitor
- ConversionCompletedToStdOutput(pgbStatus.Value, lblStatus.Text, False, SourceFilename, OutputFilename)
- Try
- ' Leave an indiator of failure for the next time to delete failed files
- Dim FailedFileStreamWriter = System.IO.File.CreateText(FailedOutputFilename)
- FailedFileStreamWriter.Close()
- Catch ex As Exception
- ' Ignore errors
- End Try
- End If
- 'Unlock the GUI
- btnOpenFile.Enabled = True
- Me.Text = MyTitle
- btnCancel.Enabled = False
- isCanceled = False
- Return Result
- End Function
- Private Function ConvertToHisto(ByVal SourceFilename As String, ByVal DestinationFilename As String, ByVal Pathname As String, ByVal DestinationPath As String) As Boolean
- Dim SourceStreamReader As System.IO.StreamReader
- Dim SourceLine As String
- Dim SourceLine2 As String = ""
- Dim DestinationLine As String
- Dim DestinationStreamWriter As System.IO.StreamWriter
- Dim DestinationChannelMeasurement As Long = 1
- Dim NewProgressValue As Integer
- Dim EndMin As Integer
- '---------------------------------------------------------------------------------------
- 'Function input validation
- 'Make sure the source file exists
- If System.IO.File.Exists(SourceFilename) = False Then
- Return False
- End If
- 'Make sure the destination file is not equal to the source file, because it will get deleted
- If SourceFilename = DestinationFilename Then
- LogError("Source file can't equal destination file.")
- Return False
- End If
- 'Make sure the destination doesn't exist
- If System.IO.File.Exists(DestinationFilename) Then
- Try
- Kill(DestinationFilename)
- Catch ex As Exception
- LogError(ex.Message)
- Return False
- End Try
- End If
- '---------------------------------------------------------------------------------------
- 'Open source and destination files
- 'Open the source file (CRC SE Wideband Scanner *.txt)
- Try
- SourceStreamReader = System.IO.File.OpenText(SourceFilename)
- Catch ex As Exception
- LogError(ex.Message)
- Return False
- End Try
- 'Create destination file (Consolidation *.asc)
- Try
- DestinationStreamWriter = System.IO.File.CreateText(DestinationFilename)
- MySubHeader.MEASUREMENTFILE = SourceFilename
- Catch ex As Exception
- LogError(ex.Message)
- SourceStreamReader.Close()
- Return False
- End Try
- Try
- '---------------------------------------------------------------------------------------
- 'Transfer data from the source file to the destination file
- 'Initialize data related to the Time Left estimation
- Dim PastTime As Date = Now
- Dim NowTime As Date = Now
- Dim Duration As TimeSpan
- Dim PastPosition As Long = 0
- Dim NowPosition As Long = 0
- Dim DataCovered As Long = 0
- Dim CoverageRate As Double = 0
- Dim TimeLeftSeconds As Double = 0
- Dim TimeLeftsSeconds As New Queue(Of Double)
- Dim TimeLeftSecondsSum As Double = 0
- Dim TimeLeftSecondsAvg As Double = 0
- Dim PastRefresh As Date = Now
- Dim DurationRefresh As TimeSpan
- Dim ChannelID As New Dictionary(Of String, String)
- Dim IsHeader As Boolean = True
- Dim IsFirstScan As Boolean = True
- Dim ScanDate As System.DateTime
- Dim PeriodSpan As TimeSpan
- Dim MyPeriodStopDateTime As DateTime
- Dim MyPeriodStartDateTime As DateTime
- Dim Index As Integer
- Dim IntValue As Integer
- Dim NbLinesPreviousScan As Long = 0
- Dim ActiveChannelCount As Integer
- Dim NbRevisit As Integer = 0
- ' Set headers & ranges
- Power_dBmRange.Offset = 120
- Power_dBmRange.RangeStart = -120
- Power_dBmRange.RangeStop = -20
- SNR_dBRange.Offset = 0
- SNR_dBRange.RangeStart = 0
- SNR_dBRange.RangeStop = 80
- Burst_dBmsRange.Offset = 30
- Burst_dBmsRange.RangeStart = -30
- Burst_dBmsRange.RangeStop = 70
- ResetBox(MyBox)
- ResetBox(MyGlobalBox)
- 'Transfer data from the log file to mdb
- SourceLine = SourceStreamReader.ReadLine()
- While Not SourceStreamReader.EndOfStream
- 'Read a new line from the source file
- ' SourceLine = SourceStreamReader.ReadLine()
- 'Update the progress bar
- NowTime = Now
- NowPosition = SourceStreamReader.BaseStream.Position
- NewProgressValue = NowPosition / SourceStreamReader.BaseStream.Length * 99
- If pgbStatus.Value <> NewProgressValue Then
- pgbStatus.Value = NewProgressValue
- Me.Text = pgbStatus.Value & "% - " & DestinationFilename
- Me.Refresh()
- End If
- 'Read the description of the file to fill the headers
- If IsHeader Then
- If SourceLine.Contains("Start Date ") Then
- 'Set Date & Time
- Dim Moment As String
- Dim Item() As String = Split(SourceLine, ",")
- Moment = Item(1).ToCharArray
- SourceLine = SourceStreamReader.ReadLine()
- Item = Split(SourceLine, ",")
- Moment = Moment + " " + Item(1)
- ' Header
- MyHeader.VERSION = 2.0
- MyHeader.SENSOR_ROLE = "SCANNER"
- MyHeader.SENSOR_OUTPUT = "SPECTRUM_CONSOLIDATED"
- MyHeader.TASK_MISSION = "0-0"
- MyHeader.SENSOR_TECHNOLOGY = "CRC_SPECTRUM_EXPLORER"
- MyHeader.TimeZoneOffset = TimeSpan.FromHours(CDbl(txtTimezoneOffset.Text))
- ReDim MyHeader.FREQ_RANGE(0 To 1)
- MyHeader.FREQ_RANGE(0) = Double.MaxValue
- MyHeader.SENSOR_HOST_ID = txtSite.Text
- MyHeader.SENSOR_HW_ID = txtHW.Text
- ' Sub-Header
- ' Validate My.Settings.BWRatio
- If My.Settings.BWRatio <= 0 Then
- ' It is possible that My.Settings.BWRatio = 0 if the user ran a previous version of the application,
- ' that didn't have that particular setting in the user.config file located in
- ' C:\Users\[user]\AppData\Local\Communications_Research_C\SEConsolidation[...]\1.0.0.0\user.config
- Dim newBWRatio = CInt(InputBox("Please provide a Bandwidth Ratio." & vbCrLf & "Current value: " & My.Settings.BWRatio & vbCrLf & "Suggested Value: 3", "Bandwidth Ratio Error", "3"))
- If newBWRatio <= 0 Then newBWRatio = 3
- My.Settings.BWRatio = newBWRatio
- End If
- BandwidthPercentRange.Offset = 1
- BandwidthPercentRange.RangeStart = 1
- BandwidthPercentRange.RangeStop = My.Settings.BWRatio * 100
- CORangePercentRange.Offset = My.Settings.BWRatio / 2 * 100
- CORangePercentRange.RangeStart = -My.Settings.BWRatio / 2 * 100
- CORangePercentRange.RangeStop = My.Settings.BWRatio / 2 * 100
- End If 'StartDateTime
- ' Sub Header
- If SourceLine.Contains("Resolution (Hz) ") Then
- Dim Item() As String = Split(SourceLine, ",",)
- MySubHeader.RESOLUTION_BW_Hz = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- End If
- If SourceLine.Contains("Th. Type ...... ") Then
- Dim Item() As String = Split(SourceLine, ",")
- If Item(1).Contains("False") Then
- MySubHeader.THRESHOLD_TYPE = enmThresholdType.CPFA
- Else
- MySubHeader.THRESHOLD_TYPE = enmThresholdType.SNR
- SourceLine = SourceStreamReader.ReadLine()
- Item = Split(SourceLine, ",")
- MySubHeader.THRESHOLD = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- End If
- End If
- If SourceLine.Contains("Pro. False Alarm ") Then
- Dim Item() As String = Split(SourceLine, ",")
- MySubHeader.THRESHOLD = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- End If
- If SourceLine.Contains("FFT Window ") Then
- Dim Item = Split(SourceLine, ",")
- If Item(1).Contains("Blackman") Then
- MySubHeader.FFT_WINDOW = enmWindow.Blackman
- ElseIf Item(1).Contains("Hann") Then
- MySubHeader.FFT_WINDOW = enmWindow.Hann
- ElseIf Item(1).Contains("Flat") Then
- MySubHeader.FFT_WINDOW = enmWindow.FlatTop
- Else
- MySubHeader.FFT_WINDOW = enmWindow.Rectangular
- End If
- MySubHeader.FRAME_NB = CInt(txtNbFrames.Text)
- MySubHeader.FRAME_AVG = enmFrame_Avg.PERIODIGRAM
- MySubHeader.BW_ESTIMATION_RATIO = CDbl(txtBWRatio.Text)
- MySubHeader.ANALYSIS_DURATION_MIN = MyPeriod
- MySubHeader.ISDF = False
- End If
- 'Read the description of the Channels
- If SourceLine.Contains("Total Frequencies: ") Then
- 'Read number of frequencies covered
- Dim Item() As String = Split(SourceLine, ",")
- TOTAL_FREQUENCIES = Item(1)
- ReDim MyChannels.BW(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.Carrier(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.Count(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.ID(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.BurstStateCurrent(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.BurstStatePrevious(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.BurstTime(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.IndexCurrentlyOn(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.IndexPreviouslyOn(0 To TOTAL_FREQUENCIES - 1)
- 'v. 2.2 - "Channel Number","Frequency Center (MHz)","Bandwidth (Hz)","Number of FFT Points","Channel ID"
- If TOTAL_FREQUENCIES > 0 Then
- 'Skip 2 lines of header
- If Not SourceStreamReader.EndOfStream Then SourceStreamReader.ReadLine()
- If Not SourceStreamReader.EndOfStream Then SourceStreamReader.ReadLine()
- 'Read subsequent channel values & Write to file
- DestinationLine = "DATA_LABELS: ChannelNb,Frequency(MHz),Bandwidth(Hz),NbFFTBins,ChannelID"
- DestinationStreamWriter.WriteLine(DestinationLine)
- SourceLine = SourceStreamReader.ReadLine()
- Do Until SourceLine.Contains("Total Frequency Bands: ") ' Initiase the channels
- DestinationStreamWriter.WriteLine(SourceLine)
- Item = Split(SourceLine, ",")
- Index = CInt(Item(0)) - 1
- MyChannels.ID(Index) = Item(4)
- MyChannels.BW(Index) = Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture)
- MyChannels.Carrier(Index) = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- SourceLine = SourceStreamReader.ReadLine()
- Loop
- SourceLine = "---"
- DestinationStreamWriter.WriteLine(SourceLine)
- End If
- ' Set Histogrammes
- ReDim MyHisto(0 To TOTAL_FREQUENCIES - 1)
- For k = 0 To TOTAL_FREQUENCIES - 1
- With MyHisto(k)
- ReDim .Power_dBmHisto(0 To Power_dBmRange.RangeStop - Power_dBmRange.RangeStart)
- ReDim .SNR_dBHisto(0 To SNR_dBRange.RangeStop - SNR_dBRange.RangeStart)
- ReDim .BandwidthPercentRangeHisto(0 To BandwidthPercentRange.RangeStop - BandwidthPercentRange.RangeStart)
- ReDim .CORangePercentHisto(0 To CORangePercentRange.RangeStop - CORangePercentRange.RangeStart)
- ReDim .BurstOn_dBmsHisto(0 To Burst_dBmsRange.RangeStop - Burst_dBmsRange.RangeStart)
- ReDim .BurstOff_dBmsHisto(0 To Burst_dBmsRange.RangeStop - Burst_dBmsRange.RangeStart)
- End With
- Next
- IsHeader = False
- End If 'Channel List
- 'Read a new line from the source file
- SourceLine = SourceStreamReader.ReadLine()
- ElseIf SourceLine.Contains("Scan:") Then
- 'Read scans
- '"Elapsed Time (ms)","Number of GPS Data Lines","Number of Channels","Number of Signal Detections","Number of Messages in Alarm","Number of noise level measurements","Number of Occupancy bins"
- 'Read number of data lines
- Dim Item() As String = Split(SourceLine, ",")
- Dim TimeMilliseconds As Long = Item(1)
- Dim LinesChannels As Long = Item(3)
- Dim LinesDetections As Long = Item(4)
- Dim LinesAlarm As Long = Item(5)
- Dim NbLinesCurrentScan As Long = LinesChannels
- Dim BurstSpan As TimeSpan
- 'Read GPS
- 'v.2.1 "GPS scan date and time","Latitude","Longitude","Elevation"
- 'v.2.3 "GPS scan date and time","Latitude","Longitude","Elevation","GridID","Status (0=No GPS,1=Fixed Position,2=GPS Locked,3=GPS Unlocked,4=GPS Error)"
- 'v.2.33 "GPS scan date and time","Latitude","Longitude","Elevation","GridID","Status (0=No GPS,1=Fixed Position,2=GPS Locked,3=GPS Unlocked,4=GPS Error)",
- ' "Movement Status (0=Not Ready, 1 = Ready)","Ground Speed (m/sec)","Ground Acceleration (m/sec2)","Track Made Good (Degre relative to true north)",
- ' "True Heading (Degre relative to true north)"
- ' Dim MovementStatus As Integer, GroundSpeed As Double, GroundAcceleration As Double, TrackMadeGood As Double, TrueHeading As Double
- If SourceLine2.Length = 0 Then
- SourceLine = SourceStreamReader.ReadLine()
- Else
- SourceLine = SourceLine2
- End If
- Item = Split(SourceLine, ",")
- ScanDate = GetDate(Item(0))
- Index = Item(0).IndexOf(".") + 1
- ScanDate = ScanDate.AddMilliseconds(Item(0).Substring(Index, 3))
- EndMin = Math.Ceiling(ScanDate.Minute / MyPeriod + ScanDate.Second / 60.0 / MyPeriod + (ScanDate.Millisecond + 1) / 1000.0 / 60.0 / MyPeriod) * MyPeriod ' Add 1 ms to avoid case where hour is bang on
- MyPeriodStartDateTime = DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc)
- MyPeriodStopDateTime = New DateTime(ScanDate.Year, ScanDate.Month, ScanDate.Day, ScanDate.Hour, 0, 0, DateTimeKind.Utc)
- MyPeriodStopDateTime = MyPeriodStopDateTime.AddMinutes(EndMin)
- PeriodSpan = MyPeriodStopDateTime - ScanDate
- If IsFirstScan Then
- ' Set all channel burst times
- For k = 0 To TOTAL_FREQUENCIES - 1
- MyChannels.BurstTime(k) = ScanDate
- Next
- End If
- 'Get Channel Measurements for the period
- While PeriodSpan.TotalSeconds > 0 And Not SourceStreamReader.EndOfStream ' Does the period
- NbRevisit += 1
- ' Location
- MyBox.LatMin = Math.Min(MyBox.LatMin, Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture))
- MyBox.LatMax = Math.Max(MyBox.LatMax, Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture))
- MyBox.LonMin = Math.Min(MyBox.LonMin, Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture))
- MyBox.LonMax = Math.Max(MyBox.LonMax, Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture))
- MyBox.AltMin = Math.Min(MyBox.AltMin, Convert.ToDouble(Item(3), Globalization.CultureInfo.InvariantCulture))
- MyBox.AltMax = Math.Max(MyBox.AltMax, Convert.ToDouble(Item(3), Globalization.CultureInfo.InvariantCulture))
- Dim MovementStatus As Integer, GroundSpeed As Double, GroundAcceleration As Double, TrackMadeGood As Double, TrueHeading As Double
- If Item.Length > 6 Then
- MovementStatus = Item(6)
- GroundSpeed = Item(7)
- GroundAcceleration = Item(8)
- TrackMadeGood = Item(9)
- TrueHeading = Item(10)
- Else
- MovementStatus = 0 'Default value, "Not Ready"
- GroundSpeed = 0 'Default value, "No Movement"
- GroundAcceleration = 0 'Default value, "No Movement"
- TrackMadeGood = -1 'Default value, Negative value is considered invalid. Valid values cover [0 to 360[
- TrueHeading = -1 'Default value, Negative value is considered invalid. Valid values cover [0 to 360[
- End If
- 'Read Channel Measures for scan
- '"Channel Number","Power (dBm)","C/N (dB)","Frequency Center Deviation (Hz)","Bandwidth (Hz)","Direction (deg)","Direction Quality (deg)","Status","RF Switch Port Number"
- Do Until SourceStreamReader.EndOfStream Or LinesChannels <= 0 'Does one scan
- LinesChannels -= 1
- SourceLine = SourceStreamReader.ReadLine()
- Item = Split(SourceLine, ",")
- Index = CInt(Item(0)) - 1
- MyChannels.Count(Index) += 1
- If MyChannels.Carrier(Index) < MyHeader.FREQ_RANGE(0) Then
- MyHeader.FREQ_RANGE(0) = MyChannels.Carrier(Index)
- End If
- If MyChannels.Carrier(Index) > MyHeader.FREQ_RANGE(1) Then
- MyHeader.FREQ_RANGE(1) = MyChannels.Carrier(Index)
- End If
- With MyHisto(Index)
- ' Power
- IntValue = Math.Round(Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)) + Power_dBmRange.Offset
- IntValue = Math.Max(IntValue, 0)
- IntValue = Math.Min(IntValue, .Power_dBmHisto.Length - 1)
- .Power_dBmHisto(IntValue) += 1
- ' SNR
- IntValue = Math.Round(Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture)) + SNR_dBRange.Offset
- IntValue = Math.Max(IntValue, 0)
- IntValue = Math.Min(IntValue, .SNR_dBHisto.Length - 1)
- .SNR_dBHisto(IntValue) += 1
- 'Frequency Offset
- IntValue = CInt(Math.Round(CDbl(Item(3)) / MyChannels.BW(Index) * 100.0)) + CORangePercentRange.Offset
- IntValue = Math.Max(IntValue, 0)
- IntValue = Math.Min(IntValue, .CORangePercentHisto.Length - 1)
- .CORangePercentHisto(IntValue) += 1
- ' BW
- IntValue = CInt(Math.Round(CDbl(Item(4)) / MyChannels.BW(Index) * 100.0)) + BandwidthPercentRange.Offset
- IntValue = Math.Max(IntValue, 0)
- IntValue = Math.Min(IntValue, .BandwidthPercentRangeHisto.Length - 1)
- .BandwidthPercentRangeHisto(IntValue) += 1
- If IsFirstScan Then
- ' Set burst state as state of burst
- MyChannels.BurstStatePrevious(Index) = True
- MyChannels.BurstStateCurrent(Index) = True
- End If
- ' Burst Off
- If MyChannels.BurstStatePrevious(Index) = False Then
- ' Channel when from off to on
- ' Burst Off value
- BurstSpan = ScanDate - MyChannels.BurstTime(Index)
- If BurstSpan.TotalMilliseconds <= 0 Then
- ' Bug in scanner where first two scan have the same time stamp
- ScanDate = ScanDate.AddMilliseconds(My.Settings.AddMilliSecond)
- BurstSpan = ScanDate - MyChannels.BurstTime(Index)
- If BurstSpan.TotalSeconds < 0 Then
- ' Still negative in time: set to AddMilliSecond
- BurstSpan = New TimeSpan(0, 0, 0, 0, My.Settings.AddMilliSecond)
- End If
- End If
- IntValue = Math.Round(10 * Math.Log10(BurstSpan.TotalSeconds / 0.001)) + Burst_dBmsRange.Offset
- IntValue = Math.Max(IntValue, 0)
- IntValue = Math.Min(IntValue, .BurstOff_dBmsHisto.Length - 1)
- .BurstOff_dBmsHisto(IntValue) += 1
- MyChannels.BurstTime(Index) = ScanDate
- End If
- MyChannels.BurstStateCurrent(Index) = True
- MyChannels.IndexCurrentlyOn(LinesChannels) = Index
- End With
- DestinationChannelMeasurement += 1
- Loop 'End Scan
- If IsFirstScan Then
- MyHeader.StartDateTime = DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc)
- IsFirstScan = False
- End If
- ' Check for ended burst and set Burst On histogram
- For k = 0 To NbLinesPreviousScan - 1
- Index = MyChannels.IndexPreviouslyOn(k)
- If MyChannels.BurstStatePrevious(Index) = True And MyChannels.BurstStateCurrent(Index) = False Then
- ' Burst from on to off
- BurstSpan = ScanDate - MyChannels.BurstTime(Index)
- IntValue = Math.Round(10 * Math.Log10(BurstSpan.TotalSeconds / 0.001)) + Burst_dBmsRange.Offset
- IntValue = Math.Max(IntValue, 0)
- IntValue = Math.Min(IntValue, MyHisto(Index).BurstOn_dBmsHisto.Length - 1)
- MyHisto(Index).BurstOn_dBmsHisto(IntValue) += 1
- MyChannels.BurstTime(Index) = ScanDate
- End If
- Next
- ' Reset current states
- Array.Clear(MyChannels.BurstStatePrevious, 0, TOTAL_FREQUENCIES)
- For k = 0 To NbLinesCurrentScan - 1
- Index = MyChannels.IndexCurrentlyOn(k)
- MyChannels.BurstStatePrevious(Index) = MyChannels.BurstStateCurrent(Index)
- MyChannels.BurstStateCurrent(Index) = False
- MyChannels.IndexPreviouslyOn(k) = MyChannels.IndexCurrentlyOn(k)
- Next
- NbLinesPreviousScan = NbLinesCurrentScan
- ' Read for next scan
- SourceLine = SourceStreamReader.ReadLine()
- If SourceStreamReader.EndOfStream Then
- ' End of file
- Exit While
- Else
- Item = Split(SourceLine, ",")
- LinesChannels = Item(3)
- LinesDetections = Item(4)
- LinesAlarm = Item(5)
- NbLinesCurrentScan = LinesChannels
- SourceLine2 = SourceStreamReader.ReadLine()
- Item = Split(SourceLine2, ",")
- ScanDate = GetDate(Item(0))
- Index = Item(0).IndexOf(".") + 1
- ScanDate = ScanDate.AddMilliseconds(CDbl(Item(0).Substring(Index, 3)))
- PeriodSpan = MyPeriodStopDateTime - ScanDate
- AverageRevisitCount += 1
- End If
- End While 'End Period
- '
- ' TO DO: Deal with the Burst_on and Burst_off at the end of the analysis period
- '
- 'Save data period to consolidated file
- If ckbCorrectForUTC.Checked = True Then
- ' UTC correction
- Dim TmpDate As DateTime
- TmpDate = MyPeriodStartDateTime.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "StartDateTime: " + TmpDate.ToString("o")
- DestinationStreamWriter.WriteLine(DestinationLine)
- If PeriodSpan.TotalSeconds > 0 Then
- TmpDate = ScanDate.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "StopDateTime: " + DateTime.SpecifyKind(TmpDate, DateTimeKind.Utc).ToString("o")
- Else
- TmpDate = MyPeriodStopDateTime.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "StopDateTime: " + TmpDate.ToString("o")
- End If
- Else
- ' N0 UTC corrections
- DestinationLine = "StartDateTime: " + MyPeriodStartDateTime.ToString("o")
- DestinationStreamWriter.WriteLine(DestinationLine)
- If PeriodSpan.TotalSeconds > 0 Then
- DestinationLine = "StopDateTime: " + DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc).ToString("o")
- Else
- DestinationLine = "StopDateTime: " + MyPeriodStopDateTime.ToString("o")
- End If
- End If
- DestinationStreamWriter.WriteLine(DestinationLine)
- If (MyBox.LatMax = MyBox.LatMin) And
- (MyBox.LonMax = MyBox.LonMin) And IsPointAllowed Then
- ' POINT
- DestinationLine = "LOCATION: POINT " + MyBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.AltMax.ToString(Globalization.CultureInfo.InvariantCulture)
- Else
- ' BOX
- DestinationLine = "LOCATION: BOX " + MyBox.LatMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LatMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.AltMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.AltMax.ToString(Globalization.CultureInfo.InvariantCulture)
- End If
- DestinationStreamWriter.WriteLine(DestinationLine)
- ' Other parameters
- DestinationLine = "# of revisit: " + NbRevisit.ToString
- DestinationStreamWriter.WriteLine(DestinationLine)
- NbRevisit = 0
- ActiveChannelCount = 0
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- ActiveChannelCount += 1
- End If
- Next
- DestinationLine = "Active Channels count: " + ActiveChannelCount.ToString
- DestinationStreamWriter.WriteLine(DestinationLine)
- ' Position information
- ' Compare with global box
- MyGlobalBox.LatMin = Math.Min(MyBox.LatMin, MyGlobalBox.LatMin)
- MyGlobalBox.LatMax = Math.Max(MyBox.LatMax, MyGlobalBox.LatMax)
- MyGlobalBox.LonMin = Math.Min(MyBox.LonMin, MyGlobalBox.LonMin)
- MyGlobalBox.LonMax = Math.Max(MyBox.LonMax, MyGlobalBox.LonMax)
- MyGlobalBox.AltMin = Math.Min(MyBox.AltMin, MyGlobalBox.AltMin)
- MyGlobalBox.AltMax = Math.Max(MyBox.AltMax, MyGlobalBox.AltMax)
- ResetBox(MyBox)
- ' Write Histograms
- WriteHisto(DestinationStreamWriter)
- ' Reset Arrays
- ResetArrays()
- 'Update the GUI with the estimated Time Left
- Duration = NowTime.Subtract(PastTime)
- If NowPosition > PastPosition Then
- DataCovered = NowPosition - PastPosition
- CoverageRate = Duration.TotalSeconds / DataCovered
- TimeLeftSeconds = (SourceStreamReader.BaseStream.Length - NowPosition) * CoverageRate
- TimeLeftsSeconds.Enqueue(TimeLeftSeconds)
- If TimeLeftsSeconds.Count > 5 Then TimeLeftsSeconds.Dequeue()
- TimeLeftSecondsSum = 0
- For Each I As Double In TimeLeftsSeconds
- TimeLeftSecondsSum += I
- Next
- TimeLeftSecondsAvg = TimeLeftSecondsSum / TimeLeftsSeconds.Count
- 'Update the GUI if at least 1 second has passed since last update
- DurationRefresh = NowTime.Subtract(PastRefresh)
- If DurationRefresh.TotalSeconds > 1 Then
- PastRefresh = NowTime
- Dim DurationTimeSpan As New TimeSpan(TimeLeftSecondsAvg * TimeSpan.TicksPerSecond)
- Dim TimeText As String = Mid(DurationTimeSpan.ToString, 1, DurationTimeSpan.ToString.IndexOf("."c)) '& Format(TimeLeftSecondsAvg, "0.00") & " seconds"
- lblStatus.Text = "Consolidation in progress... Time remaining: " & TimeText & "."
- Me.Refresh()
- RaiseEvent ConversionCompleted(pgbStatus.Value, TimeText, True, DestinationFilename)
- ConversionCompletedToStdOutput(pgbStatus.Value, TimeText, True, SourceFilename, DestinationFilename)
- End If
- 'Get variables ready for next algorithm pass
- PastPosition = NowPosition
- PastTime = NowTime
- End If
- Else
- 'Read a new line from the source file
- SourceLine = SourceStreamReader.ReadLine()
- End If 'End Period
- If SourceStreamReader.EndOfStream Then
- ' Write Start/StopDateTime, revisit time
- PeriodSpan = ScanDate - MyHeader.StartDateTime
- MyHeader.StopDateTime = DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc)
- MySubHeader.REVISIT_TIME_s = PeriodSpan.TotalSeconds / AverageRevisitCount
- Exit While
- End If
- ' Listen to user inputs
- System.Windows.Forms.Application.DoEvents()
- ' The application is closing. Close any opened file
- If isClosing Then
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Return False
- End If
- ' Report the error and close any opened file
- If isCanceled Then
- lblStatus.Text = "Consolidation was canceled by the user."
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Return False
- End If
- End While
- Catch ex As Exception
- ' Report the error and close any opened file
- LogError(ex.Message)
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Return False
- End Try
- ' Success. Close any opened file. Combine the header with the data. Report success
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Dim Analysis As Boolean = True
- CompleteDestination(MyHeader, DestinationFilename, Pathname, Analysis)
- pgbStatus.Value = 100
- 'Zip the file
- Dim tempFolder As String = Pathname + "\temp"
- My.Computer.FileSystem.CreateDirectory(tempFolder)
- 'To zip the output into a new directory, just change "zipPath" below to the desired directory
- Dim zipPath As String = DestinationPath + "\" + MyHeader.SENSOR_HOST_ID + "-" + MyHeader.SENSOR_OUTPUT + "-" + MyHeader.TASK_MISSION + "-" +
- Replace(MyHeader.StartDateTime.ToString("o"), ":", "-") + ".zip"
- Dim targetFile As String = tempFolder + "\" + MyHeader.SENSOR_HOST_ID + "-" + MyHeader.SENSOR_OUTPUT + "-" + MyHeader.TASK_MISSION + "-" +
- Replace(MyHeader.StartDateTime.ToString("o"), ":", "-") + ".txt"
- My.Computer.FileSystem.MoveFile(DestinationFilename, targetFile)
- Console.Write("Zipping to ")
- Console.WriteLine(zipPath)
- ZipFile.CreateFromDirectory(tempFolder, zipPath)
- My.Computer.FileSystem.DeleteDirectory(tempFolder, FileIO.DeleteDirectoryOption.DeleteAllContents)
- Return True
- End Function
- Private Function ConvertToMeasurement(ByVal SourceFilename As String, ByVal DestinationFilename As String, ByVal Pathname As String, ByVal DestinationPath As String) As Boolean
- Dim SourceStreamReader As System.IO.StreamReader
- Dim SourceLine As String
- Dim SourceLine2 As String = ""
- Dim DestinationLine As String
- Dim DestinationStreamWriter As System.IO.StreamWriter
- Dim DestinationChannelMeasurement As Long = 1
- Dim NewProgressValue As Integer
- Dim EndMin As Integer
- '---------------------------------------------------------------------------------------
- 'Function input validation
- 'Make sure the source file exists
- If System.IO.File.Exists(SourceFilename) = False Then
- Return False
- End If
- 'Make sure the destination file is not equal to the source file, because it will get deleted
- If SourceFilename = DestinationFilename Then
- LogError("Source file can't equal destination file.")
- Return False
- End If
- 'Make sure the destination doesn't exist
- If System.IO.File.Exists(DestinationFilename) Then
- Try
- Kill(DestinationFilename)
- Catch ex As Exception
- LogError(ex.Message)
- Return False
- End Try
- End If
- '---------------------------------------------------------------------------------------
- 'Open source and destination files
- 'Open the source file (CRC SE Wideband Scanner *.txt)
- Try
- SourceStreamReader = System.IO.File.OpenText(SourceFilename)
- Catch ex As Exception
- LogError(ex.Message)
- Return False
- End Try
- 'Create destination file (Consolidation *.asc)
- Try
- DestinationStreamWriter = System.IO.File.CreateText(DestinationFilename)
- MySubHeader.MEASUREMENTFILE = SourceFilename
- Catch ex As Exception
- LogError(ex.Message)
- SourceStreamReader.Close()
- Return False
- End Try
- Try
- '---------------------------------------------------------------------------------------
- 'Transfer data from the source file to the destination file
- 'Initialize data related to the Time Left estimation
- Dim PastTime As Date = Now
- Dim NowTime As Date = Now
- Dim Duration As TimeSpan
- Dim PastPosition As Long = 0
- Dim NowPosition As Long = 0
- Dim DataCovered As Long = 0
- Dim CoverageRate As Double = 0
- Dim TimeLeftSeconds As Double = 0
- Dim TimeLeftsSeconds As New Queue(Of Double)
- Dim TimeLeftSecondsSum As Double = 0
- Dim TimeLeftSecondsAvg As Double = 0
- Dim PastRefresh As Date = Now
- Dim DurationRefresh As TimeSpan
- Dim ChannelID As New Dictionary(Of String, String)
- Dim IsHeader As Boolean = True
- Dim IsFirstScan As Boolean = True
- Dim ScanDate As System.DateTime
- Dim PeriodSpan As TimeSpan
- Dim MyPeriodStopDateTime As DateTime
- Dim MyPeriodStartDateTime As DateTime
- Dim Index As Integer
- Dim NbLinesPreviousScan As Long = 0
- Dim ActiveChannelCount As Integer
- Dim NbRevisit As Integer = 0
- Dim output As MeasurementData
- ' Set headers & ranges
- Power_dBmRange.Offset = 120
- Power_dBmRange.RangeStart = -120
- Power_dBmRange.RangeStop = -20
- SNR_dBRange.Offset = 0
- SNR_dBRange.RangeStart = 0
- SNR_dBRange.RangeStop = 80
- Burst_dBmsRange.Offset = 30
- Burst_dBmsRange.RangeStart = -30
- Burst_dBmsRange.RangeStop = 70
- ResetBox(MyBox)
- ResetBox(MyGlobalBox)
- 'Transfer data from the log file to mdb
- SourceLine = SourceStreamReader.ReadLine()
- While Not SourceStreamReader.EndOfStream
- 'Read a new line from the source file
- ' SourceLine = SourceStreamReader.ReadLine()
- 'Update the progress bar
- NowTime = Now
- NowPosition = SourceStreamReader.BaseStream.Position
- NewProgressValue = NowPosition / SourceStreamReader.BaseStream.Length * 99
- If pgbStatus.Value <> NewProgressValue Then
- pgbStatus.Value = NewProgressValue
- Me.Text = pgbStatus.Value & "% - " & DestinationFilename
- Me.Refresh()
- End If
- 'Read the description of the file to fill the headers
- If IsHeader Then
- If SourceLine.Contains("Start Date ") Then
- 'Set Date & Time
- Dim Moment As String
- Dim Item() As String = Split(SourceLine, ",")
- Moment = Item(1).ToCharArray
- SourceLine = SourceStreamReader.ReadLine()
- Item = Split(SourceLine, ",")
- Moment = Moment + " " + Item(1)
- ' Header
- MyHeader.VERSION = 2.0
- MyHeader.SENSOR_ROLE = "SCANNER"
- MyHeader.SENSOR_OUTPUT = "SPECTRUM_MEASUREMENT"
- MyHeader.TASK_MISSION = "0-0"
- MyHeader.SENSOR_TECHNOLOGY = "CRC_SPECTRUM_EXPLORER"
- MyHeader.TimeZoneOffset = TimeSpan.FromHours(CDbl(txtTimezoneOffset.Text))
- ReDim MyHeader.FREQ_RANGE(0 To 1)
- MyHeader.FREQ_RANGE(0) = Double.MaxValue
- MyHeader.SENSOR_HOST_ID = txtSite.Text
- MyHeader.SENSOR_HW_ID = txtHW.Text
- ' Sub-Header
- ' Validate My.Settings.BWRatio
- If My.Settings.BWRatio <= 0 Then
- ' It is possible that My.Settings.BWRatio = 0 if the user ran a previous version of the application,
- ' that didn't have that particular setting in the user.config file located in
- ' C:\Users\[user]\AppData\Local\Communications_Research_C\SEConsolidation[...]\1.0.0.0\user.config
- Dim newBWRatio = CInt(InputBox("Please provide a Bandwidth Ratio." & vbCrLf & "Current value: " & My.Settings.BWRatio & vbCrLf & "Suggested Value: 3", "Bandwidth Ratio Error", "3"))
- If newBWRatio <= 0 Then newBWRatio = 3
- My.Settings.BWRatio = newBWRatio
- End If
- BandwidthPercentRange.Offset = 1
- BandwidthPercentRange.RangeStart = 1
- BandwidthPercentRange.RangeStop = My.Settings.BWRatio * 100
- CORangePercentRange.Offset = My.Settings.BWRatio / 2 * 100
- CORangePercentRange.RangeStart = -My.Settings.BWRatio / 2 * 100
- CORangePercentRange.RangeStop = My.Settings.BWRatio / 2 * 100
- End If 'StartDateTime
- ' Sub Header
- If SourceLine.Contains("Resolution (Hz) ") Then
- Dim Item() As String = Split(SourceLine, ",",)
- MySubHeader.RESOLUTION_BW_Hz = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- End If
- If SourceLine.Contains("Th. Type ...... ") Then
- Dim Item() As String = Split(SourceLine, ",")
- If Item(1).Contains("False") Then
- MySubHeader.THRESHOLD_TYPE = enmThresholdType.CPFA
- Else
- MySubHeader.THRESHOLD_TYPE = enmThresholdType.SNR
- SourceLine = SourceStreamReader.ReadLine()
- Item = Split(SourceLine, ",")
- MySubHeader.THRESHOLD = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- End If
- End If
- If SourceLine.Contains("Pro. False Alarm ") Then
- Dim Item() As String = Split(SourceLine, ",")
- MySubHeader.THRESHOLD = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- End If
- If SourceLine.Contains("FFT Window ") Then
- Dim Item = Split(SourceLine, ",")
- If Item(1).Contains("Blackman") Then
- MySubHeader.FFT_WINDOW = enmWindow.Blackman
- ElseIf Item(1).Contains("Hann") Then
- MySubHeader.FFT_WINDOW = enmWindow.Hann
- ElseIf Item(1).Contains("Flat") Then
- MySubHeader.FFT_WINDOW = enmWindow.FlatTop
- Else
- MySubHeader.FFT_WINDOW = enmWindow.Rectangular
- End If
- MySubHeader.FRAME_NB = CInt(txtNbFrames.Text)
- MySubHeader.FRAME_AVG = enmFrame_Avg.PERIODIGRAM
- MySubHeader.BW_ESTIMATION_RATIO = CDbl(txtBWRatio.Text)
- MySubHeader.ANALYSIS_DURATION_MIN = MyPeriod
- MySubHeader.ISDF = False
- End If
- 'Read the description of the Channels
- If SourceLine.Contains("Total Frequencies: ") Then
- 'Read number of frequencies covered
- Dim Item() As String = Split(SourceLine, ",")
- TOTAL_FREQUENCIES = Item(1)
- ReDim MyChannels.BW(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.Carrier(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.Count(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.ID(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.BurstStateCurrent(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.BurstStatePrevious(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.BurstTime(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.IndexCurrentlyOn(0 To TOTAL_FREQUENCIES - 1)
- ReDim MyChannels.IndexPreviouslyOn(0 To TOTAL_FREQUENCIES - 1)
- 'v. 2.2 - "Channel Number","Frequency Center (MHz)","Bandwidth (Hz)","Number of FFT Points","Channel ID"
- If TOTAL_FREQUENCIES > 0 Then
- 'Skip 2 lines of header
- If Not SourceStreamReader.EndOfStream Then SourceStreamReader.ReadLine()
- If Not SourceStreamReader.EndOfStream Then SourceStreamReader.ReadLine()
- 'Read subsequent channel values & Write to file
- DestinationLine = "DATA_LABELS: ChannelNb,Frequency(MHz),Bandwidth(Hz),NbFFTBins,ChannelID"
- DestinationStreamWriter.WriteLine(DestinationLine)
- SourceLine = SourceStreamReader.ReadLine()
- Do Until SourceLine.Contains("Total Frequency Bands: ") ' Initiase the channels
- DestinationStreamWriter.WriteLine(SourceLine)
- Item = Split(SourceLine, ",")
- Index = CInt(Item(0)) - 1
- MyChannels.ID(Index) = Item(4)
- MyChannels.BW(Index) = Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture)
- MyChannels.Carrier(Index) = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- SourceLine = SourceStreamReader.ReadLine()
- Loop
- SourceLine = "---"
- DestinationStreamWriter.WriteLine(SourceLine)
- End If
- ' Set Histogrammes
- ReDim MyHisto(0 To TOTAL_FREQUENCIES - 1)
- For k = 0 To TOTAL_FREQUENCIES - 1
- With MyHisto(k)
- ReDim .Power_dBmHisto(0 To Power_dBmRange.RangeStop - Power_dBmRange.RangeStart)
- ReDim .SNR_dBHisto(0 To SNR_dBRange.RangeStop - SNR_dBRange.RangeStart)
- ReDim .BandwidthPercentRangeHisto(0 To BandwidthPercentRange.RangeStop - BandwidthPercentRange.RangeStart)
- ReDim .CORangePercentHisto(0 To CORangePercentRange.RangeStop - CORangePercentRange.RangeStart)
- ReDim .BurstOn_dBmsHisto(0 To Burst_dBmsRange.RangeStop - Burst_dBmsRange.RangeStart)
- ReDim .BurstOff_dBmsHisto(0 To Burst_dBmsRange.RangeStop - Burst_dBmsRange.RangeStart)
- End With
- Next
- IsHeader = False
- End If 'Channel List
- 'Read a new line from the source file
- SourceLine = SourceStreamReader.ReadLine()
- ElseIf SourceLine.Contains("Scan:") Then
- 'Read scans
- '"Elapsed Time (ms)","Number of GPS Data Lines","Number of Channels","Number of Signal Detections","Number of Messages in Alarm","Number of noise level measurements","Number of Occupancy bins"
- 'Read number of data lines
- Dim Item() As String = Split(SourceLine, ",")
- Dim TimeMilliseconds As Long = Item(1)
- Dim LinesChannels As Long = Item(3)
- Dim LinesDetections As Long = Item(4)
- Dim LinesAlarm As Long = Item(5)
- Dim NbLinesCurrentScan As Long = LinesChannels
- If SourceLine2.Length = 0 Then
- SourceLine = SourceStreamReader.ReadLine()
- Else
- SourceLine = SourceLine2
- End If
- Item = Split(SourceLine, ",")
- ScanDate = GetDate(Item(0))
- Index = Item(0).IndexOf(".") + 1
- ScanDate = ScanDate.AddMilliseconds(Item(0).Substring(Index, 3))
- EndMin = Math.Ceiling(ScanDate.Minute / MyPeriod + ScanDate.Second / 60.0 / MyPeriod + (ScanDate.Millisecond + 1) / 1000.0 / 60.0 / MyPeriod) * MyPeriod ' Add 1 ms to avoid case where hour is bang on
- MyPeriodStartDateTime = DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc)
- MyPeriodStopDateTime = New DateTime(ScanDate.Year, ScanDate.Month, ScanDate.Day, ScanDate.Hour, 0, 0, DateTimeKind.Utc)
- MyPeriodStopDateTime = MyPeriodStopDateTime.AddMinutes(EndMin)
- PeriodSpan = MyPeriodStopDateTime - ScanDate
- 'Get Channel Measurements for the period
- While PeriodSpan.TotalSeconds > 0 And Not SourceStreamReader.EndOfStream ' Does the period
- NbRevisit += 1
- ' Location
- MyBox.LatMin = Math.Min(MyBox.LatMin, Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture))
- MyBox.LatMax = Math.Max(MyBox.LatMax, Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture))
- MyBox.LonMin = Math.Min(MyBox.LonMin, Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture))
- MyBox.LonMax = Math.Max(MyBox.LonMax, Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture))
- MyBox.AltMin = Math.Min(MyBox.AltMin, Convert.ToDouble(Item(3), Globalization.CultureInfo.InvariantCulture))
- MyBox.AltMax = Math.Max(MyBox.AltMax, Convert.ToDouble(Item(3), Globalization.CultureInfo.InvariantCulture))
- Dim Status As Integer, MovementStatus As Integer, GroundSpeed As Double, GroundAcceleration As Double, TrackMadeGood As Double, TrueHeading As Double
- If Item.Length > 6 Then
- Status = Item(5)
- MovementStatus = Item(6)
- GroundSpeed = Item(7)
- GroundAcceleration = Item(8)
- TrackMadeGood = Item(9)
- TrueHeading = Item(10)
- Else
- Status = 1 'Default value, "Fixed Position"
- MovementStatus = 0 'Default value, "Not Ready"
- GroundSpeed = 0 'Default value, "No Movement"
- GroundAcceleration = 0 'Default value, "No Movement"
- TrackMadeGood = -1 'Default value, Negative value is considered invalid. Valid values cover [0 to 360[
- TrueHeading = -1 'Default value, Negative value is considered invalid. Valid values cover [0 to 360[
- End If
- 'Read the data to be written
- output.Status = Status
- output.MovementStatus = MovementStatus
- output.GroundSpeed = GroundSpeed
- output.GroundAcceleration = GroundAcceleration
- output.TrackMadeGood = TrackMadeGood
- output.TrueHeading = TrueHeading
- Dim FixedDate As String = ""
- FixedDate = FixedDate & ScanDate.Year & "-" & ScanDate.Month & "-" & ScanDate.Day & "T" & ScanDate.Hour & ":" & ScanDate.Minute & ":" & ScanDate.Second & "." & ScanDate.Millisecond
- output.FixedDate = FixedDate
- output.Latitude = MyBox.LatMin
- output.Longitude = MyBox.LonMin
- output.Altitude = MyBox.AltMin
- DestinationLine = "GPS DATA LABELS: GPS scan Date And time,Latitude,Longitude,Elevation,Status (0=No GPS,1=Fixed Position,2=GPS Locked,3=GPS Unlocked,4=GPS Error),Movement Status (0=Not Ready, 1 = Ready),Ground Speed (m/sec),Ground Acceleration (m/sec2),Track Made Good (Degre relative To True north),True Heading (Degre relative To True north)"
- DestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "CHANNEL DATA LABELS: Channel Number,Power (dBm),C/N (dB),Frequency Center Deviation (Hz),Bandwidth (Hz),Direction (deg),Direction Quality (deg)"
- DestinationStreamWriter.WriteLine(DestinationLine)
- Do Until SourceStreamReader.EndOfStream Or LinesChannels <= 0 'Does one scan
- LinesChannels -= 1
- SourceLine = SourceStreamReader.ReadLine()
- Item = Split(SourceLine, ",")
- Index = CInt(Item(0)) - 1
- MyChannels.Count(Index) += 1
- If MyChannels.Carrier(Index) < MyHeader.FREQ_RANGE(0) Then
- MyHeader.FREQ_RANGE(0) = MyChannels.Carrier(Index)
- End If
- If MyChannels.Carrier(Index) > MyHeader.FREQ_RANGE(1) Then
- MyHeader.FREQ_RANGE(1) = MyChannels.Carrier(Index)
- End If
- 'Read Power, SNR, Frequency Offset, BW
- output.Power = Convert.ToDouble(Item(1), Globalization.CultureInfo.InvariantCulture)
- output.SNR = Convert.ToDouble(Item(2), Globalization.CultureInfo.InvariantCulture)
- output.Freq = Convert.ToDouble(Item(3), Globalization.CultureInfo.InvariantCulture)
- output.BW = Convert.ToDouble(Item(4), Globalization.CultureInfo.InvariantCulture)
- output.Dir = Convert.ToDouble(Item(5), Globalization.CultureInfo.InvariantCulture)
- output.DirQuality = Convert.ToDouble(Item(6), Globalization.CultureInfo.InvariantCulture)
- output.ChannelNum = Index + 1
- WriteMeasurement(output, DestinationStreamWriter)
- DestinationChannelMeasurement += 1
- Loop 'End Scan
- If IsFirstScan Then
- MyHeader.StartDateTime = DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc)
- IsFirstScan = False
- End If
- ' Reset current states
- Array.Clear(MyChannels.BurstStatePrevious, 0, TOTAL_FREQUENCIES)
- For k = 0 To NbLinesCurrentScan - 1
- Index = MyChannels.IndexCurrentlyOn(k)
- MyChannels.BurstStatePrevious(Index) = MyChannels.BurstStateCurrent(Index)
- MyChannels.BurstStateCurrent(Index) = False
- MyChannels.IndexPreviouslyOn(k) = MyChannels.IndexCurrentlyOn(k)
- Next
- NbLinesPreviousScan = NbLinesCurrentScan
- ' Read for next scan
- SourceLine = SourceStreamReader.ReadLine()
- If SourceStreamReader.EndOfStream Then
- ' End of file
- Exit While
- Else
- Item = Split(SourceLine, ",")
- LinesChannels = Item(3)
- LinesDetections = Item(4)
- LinesAlarm = Item(5)
- NbLinesCurrentScan = LinesChannels
- SourceLine2 = SourceStreamReader.ReadLine()
- Item = Split(SourceLine2, ",")
- ScanDate = GetDate(Item(0))
- Index = Item(0).IndexOf(".") + 1
- ScanDate = ScanDate.AddMilliseconds(CDbl(Item(0).Substring(Index, 3)))
- PeriodSpan = MyPeriodStopDateTime - ScanDate
- AverageRevisitCount += 1
- End If
- End While
- 'Save data period to consolidated file
- If ckbCorrectForUTC.Checked = True Then
- ' UTC correction
- Dim TmpDate As DateTime
- TmpDate = MyPeriodStartDateTime.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "StartDateTime: " + TmpDate.ToString("o")
- DestinationStreamWriter.WriteLine(DestinationLine)
- If PeriodSpan.TotalSeconds > 0 Then
- TmpDate = ScanDate.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "StopDateTime: " + DateTime.SpecifyKind(TmpDate, DateTimeKind.Utc).ToString("o")
- Else
- TmpDate = MyPeriodStopDateTime.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "StopDateTime: " + TmpDate.ToString("o")
- End If
- Else
- ' N0 UTC corrections
- DestinationLine = "StartDateTime: " + MyPeriodStartDateTime.ToString("o")
- DestinationStreamWriter.WriteLine(DestinationLine)
- If PeriodSpan.TotalSeconds > 0 Then
- DestinationLine = "StopDateTime: " + DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc).ToString("o")
- Else
- DestinationLine = "StopDateTime: " + MyPeriodStopDateTime.ToString("o")
- End If
- End If
- DestinationStreamWriter.WriteLine(DestinationLine)
- If (MyBox.LatMax = MyBox.LatMin) And
- (MyBox.LonMax = MyBox.LonMin) And IsPointAllowed Then
- ' POINT
- DestinationLine = "LOCATION: POINT " + MyBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.AltMax.ToString(Globalization.CultureInfo.InvariantCulture)
- Else
- ' BOX
- DestinationLine = "LOCATION: BOX " + MyBox.LatMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LatMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.LonMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.AltMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyBox.AltMax.ToString(Globalization.CultureInfo.InvariantCulture)
- End If
- DestinationStreamWriter.WriteLine(DestinationLine)
- ' Other parameters
- DestinationLine = "# of revisit: " + NbRevisit.ToString
- DestinationStreamWriter.WriteLine(DestinationLine)
- NbRevisit = 0
- ActiveChannelCount = 0
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- ActiveChannelCount += 1
- End If
- Next
- DestinationLine = "Active Channels count: " + ActiveChannelCount.ToString
- DestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "---"
- DestinationStreamWriter.WriteLine(DestinationLine)
- ' Position information
- ' Compare with global box
- MyGlobalBox.LatMin = Math.Min(MyBox.LatMin, MyGlobalBox.LatMin)
- MyGlobalBox.LatMax = Math.Max(MyBox.LatMax, MyGlobalBox.LatMax)
- MyGlobalBox.LonMin = Math.Min(MyBox.LonMin, MyGlobalBox.LonMin)
- MyGlobalBox.LonMax = Math.Max(MyBox.LonMax, MyGlobalBox.LonMax)
- MyGlobalBox.AltMin = Math.Min(MyBox.AltMin, MyGlobalBox.AltMin)
- MyGlobalBox.AltMax = Math.Max(MyBox.AltMax, MyGlobalBox.AltMax)
- ResetBox(MyBox)
- ' Reset Arrays
- ResetArrays()
- 'Update the GUI with the estimated Time Left
- Duration = NowTime.Subtract(PastTime)
- If NowPosition > PastPosition Then
- DataCovered = NowPosition - PastPosition
- CoverageRate = Duration.TotalSeconds / DataCovered
- TimeLeftSeconds = (SourceStreamReader.BaseStream.Length - NowPosition) * CoverageRate
- TimeLeftsSeconds.Enqueue(TimeLeftSeconds)
- If TimeLeftsSeconds.Count > 5 Then TimeLeftsSeconds.Dequeue()
- TimeLeftSecondsSum = 0
- For Each I As Double In TimeLeftsSeconds
- TimeLeftSecondsSum += I
- Next
- TimeLeftSecondsAvg = TimeLeftSecondsSum / TimeLeftsSeconds.Count
- 'Update the GUI if at least 1 second has passed since last update
- DurationRefresh = NowTime.Subtract(PastRefresh)
- If DurationRefresh.TotalSeconds > 1 Then
- PastRefresh = NowTime
- Dim DurationTimeSpan As New TimeSpan(TimeLeftSecondsAvg * TimeSpan.TicksPerSecond)
- Dim TimeText As String = Mid(DurationTimeSpan.ToString, 1, DurationTimeSpan.ToString.IndexOf("."c)) '& Format(TimeLeftSecondsAvg, "0.00") & " seconds"
- lblStatus.Text = "Consolidation in progress... Time remaining: " & TimeText & "."
- Me.Refresh()
- RaiseEvent ConversionCompleted(pgbStatus.Value, TimeText, True, DestinationFilename)
- ConversionCompletedToStdOutput(pgbStatus.Value, TimeText, True, SourceFilename, DestinationFilename)
- End If
- 'Get variables ready for next algorithm pass
- PastPosition = NowPosition
- PastTime = NowTime
- End If
- Else
- 'Read a new line from the source file
- SourceLine = SourceStreamReader.ReadLine()
- End If 'End Period
- If SourceStreamReader.EndOfStream Then
- ' Write Start/StopDateTime, revisit time
- PeriodSpan = ScanDate - MyHeader.StartDateTime
- MyHeader.StopDateTime = DateTime.SpecifyKind(ScanDate, DateTimeKind.Utc)
- MySubHeader.REVISIT_TIME_s = PeriodSpan.TotalSeconds / AverageRevisitCount
- Exit While
- End If
- ' Listen to user inputs
- System.Windows.Forms.Application.DoEvents()
- ' The application is closing. Close any opened file
- If isClosing Then
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Return False
- End If
- ' Report the error and close any opened file
- If isCanceled Then
- lblStatus.Text = "Consolidation was canceled by the user."
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Return False
- End If
- End While
- Catch ex As Exception
- ' Report the error and close any opened file
- LogError(ex.Message)
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Return False
- End Try
- ' Success. Close any opened file. Combine the header with the data. Report success
- DestinationStreamWriter.Close()
- SourceStreamReader.Close()
- Dim Analysis As Boolean = False
- CompleteDestination(MyHeader, DestinationFilename, Pathname, Analysis)
- pgbStatus.Value = 100
- 'Zip the file
- Dim tempFolder As String = Pathname + "\temp"
- My.Computer.FileSystem.CreateDirectory(tempFolder)
- 'To zip the output into a new directory, just change "zipPath" below to the desired directory
- Dim zipPath As String = DestinationPath + "\" + MyHeader.SENSOR_HOST_ID + "-" + MyHeader.SENSOR_OUTPUT + "-" + MyHeader.TASK_MISSION + "-" +
- Replace(MyHeader.StartDateTime.ToString("o"), ":", "-") + ".zip"
- Dim targetFile As String = tempFolder + "\" + MyHeader.SENSOR_HOST_ID + "-" + MyHeader.SENSOR_OUTPUT + "-" + MyHeader.TASK_MISSION + "-" +
- Replace(MyHeader.StartDateTime.ToString("o"), ":", "-") + ".txt"
- My.Computer.FileSystem.MoveFile(DestinationFilename, targetFile)
- Console.Write("Zipping to ")
- Console.WriteLine(zipPath)
- ZipFile.CreateFromDirectory(tempFolder, zipPath)
- My.Computer.FileSystem.DeleteDirectory(tempFolder, FileIO.DeleteDirectoryOption.DeleteAllContents)
- Return True
- End Function
- Private Sub CompleteDestination(ByVal Header As udtHeader, ByRef DataFilename As String, ByVal Path As String, ByVal Analysis As Boolean)
- ' Header file to write header and sub-header.
- Dim HeadFileName As String = Replace(DataFilename, ".histo", ".hed")
- Dim HeaderDestinationStreamWriter = New IO.StreamWriter(HeadFileName, False, System.Text.Encoding.Default)
- WriteHeader(HeaderDestinationStreamWriter)
- WriteSubHeader(HeaderDestinationStreamWriter, Analysis)
- ' Close the header file
- HeaderDestinationStreamWriter.Close()
- ' Merge files: Destination file to write header, sub-header and data
- Dim DestinationFile As String
- If ckbCorrectForUTC.Checked = True Then
- Dim tmpDate As DateTime
- tmpDate = MyHeader.StartDateTime.Subtract(MyHeader.TimeZoneOffset)
- DestinationFile = Path + "\" + Header.SENSOR_HOST_ID + "-" + Header.SENSOR_OUTPUT + "-" + Header.TASK_MISSION + "-" +
- Replace(tmpDate.ToString("o"), ":", "-") + ".txt"
- Else
- DestinationFile = Path + "\" + Header.SENSOR_HOST_ID + "-" + Header.SENSOR_OUTPUT + "-" + Header.TASK_MISSION + "-" +
- Replace(MyHeader.StartDateTime.ToString("o"), ":", "-") + ".txt"
- End If
- lblDestination.Text = DestinationFile
- Dim DestinationStream As IO.FileStream = IO.File.Create(DestinationFile)
- Dim aFileStream As IO.FileStream = IO.File.OpenRead(HeadFileName)
- aFileStream.CopyTo(DestinationStream)
- aFileStream.Close()
- aFileStream = IO.File.OpenRead(DataFilename)
- aFileStream.CopyTo(DestinationStream)
- aFileStream.Close()
- DestinationStream.Close()
- Kill(HeadFileName)
- Kill(DataFilename)
- DataFilename = DestinationFile
- End Sub
- Private Sub ConversionCompletedToStdOutput(pct As Integer, timeLeft As String, isBusy As Boolean, filename As String, outputFilename As String)
- Console.WriteLine(pct.ToString + "," + timeLeft + "," + isBusy.ToString + "," + filename + "," + outputFilename)
- End Sub
- Private Sub ErrorReportToStdOutput(errMsg As String)
- Console.WriteLine(errMsg)
- End Sub
- Private Shared Function GetDate(ByVal StringDate As String) As Date
- Dim DateTimeParts() As String = Split(StringDate, " ")
- Dim DateParts() As String = Split(DateTimeParts(0), "-")
- Dim TimeParts() As String = Split(DateTimeParts(1), ":")
- Dim NewTime As Date
- Try
- 'Try in English
- NewTime = New DateTime(CInt(DateParts(2)), CInt(DateParts(1)), CInt(DateParts(0).Remove(0, 1)),
- CInt(TimeParts(0)), CInt(TimeParts(1)), CInt(Math.Floor(Val(TimeParts(2)))))
- Catch ex As Exception
- 'If an error occurs, it could be that the date was written in another order
- Try
- 'Try in French
- NewTime = New DateTime(CInt(DateParts(2)), CInt(DateParts(0).Remove(0, 1)), CInt(DateParts(1)),
- CInt(TimeParts(0)), CInt(TimeParts(1)), CInt(Math.Floor(Val(TimeParts(2)))))
- Catch ex1 As Exception
- 'Do nothing
- End Try
- End Try
- Return NewTime
- End Function
- Private Sub tmrTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
- Dim IsHisto As Boolean
- Dim IsMeasurement As Boolean
- Dim ThePath As String
- IsHisto = ckbHisto.Checked
- IsMeasurement = ckbMeasurement.Checked
- 'Disable the timer after it was initially called
- tmrTimer.Enabled = False
- 'Work on the assigned task
- Select Case MyTimerTask
- 'If files need to be converted to database
- Case enmTasks.ProcessDragAndDropFiles
- Try
- 'Convert each file
- Dim Result As Boolean
- For Each File As String In MyDragAndDropFiles
- ThePath = System.IO.Path.GetDirectoryName(folderpath)
- Result = UnzipDirectory(folderpath, ThePath, IsHisto, IsMeasurement)
- If Result = False Then
- Exit For
- End If
- Next
- Catch ex As Exception
- 'Do nothing
- End Try
- 'Clean up the files and task
- MyTimerTask = enmTasks.DoNothing
- MyDragAndDropFiles = Nothing
- Case enmTasks.ProcessFolderfile
- Try
- 'Convert each file
- Dim Result As Boolean
- ThePath = System.IO.Path.GetDirectoryName(folderpath)
- Result = UnzipDirectory(folderpath, ThePath, IsHisto, IsMeasurement)
- Catch ex As Exception
- End Try
- End Select
- End Sub
- ' This sub is used by SEDBE Folder Monitor
- Public Sub ConvertFiles(filenames As String())
- MyDragAndDropFiles = filenames
- MyTimerTask = enmTasks.ProcessDragAndDropFiles
- tmrTimer.Enabled = True
- End Sub
- ' This sub is used by SEDBE Folder Monitor
- Public Sub CancelConversion()
- 'Inform any running algorithm of the user's intervention
- If InvokeRequired Then
- Invoke(Sub() isCanceled = True)
- Invoke(Sub() btnCancel.Enabled = False)
- Else
- isCanceled = True
- btnCancel.Enabled = False
- End If
- End Sub
- ' This sub is used by SEDBE Folder Monitor
- Public Sub ConvertFile(filename As String)
- If InvokeRequired Then
- Invoke(Sub() MyDragAndDropFiles = {filename})
- Invoke(Sub() MyTimerTask = enmTasks.ProcessDragAndDropFiles)
- Invoke(Sub() tmrTimer.Enabled = True)
- Else
- MyDragAndDropFiles = {filename}
- MyTimerTask = enmTasks.ProcessDragAndDropFiles
- If isHidden Then
- tmrTimer_Tick(Nothing, Nothing)
- Else
- tmrTimer.Enabled = True
- End If
- End If
- End Sub
- ' This sub is used by SEDBE Folder Monitor
- Public Sub Quit()
- If InvokeRequired Then
- Invoke(Sub() Me.Close())
- Else
- Me.Close()
- End If
- End Sub
- Private Sub btnHelp_Click(sender As Object, e As EventArgs) Handles btnHelp.Click
- DisplayHelpFile()
- End Sub
- Private Sub btnInfo_Click(sender As Object, e As EventArgs) Handles btnInfo.Click
- ' Display the info file contents in the command line and in a message box like Remote Desktop (mstsc.exe)
- Try
- Dim helpFilename As String = Application.StartupPath & "\SEDBE info.txt"
- Dim helpMessage As String = System.IO.File.ReadAllText(helpFilename)
- System.Diagnostics.Process.Start("notepad.exe", helpFilename)
- Catch ex As Exception
- MsgBox("Error accessing the info file: " & ex.Message)
- End Try
- End Sub
- Private Sub DisplayHelpFile()
- ' Display the help file contents in the command line and in a message box like Remote Desktop (mstsc.exe)
- Try
- Dim helpFilename As String = Application.StartupPath & "\SEDBE help.txt"
- Dim helpMessage As String = System.IO.File.ReadAllText(helpFilename)
- Console.Write(helpMessage)
- System.Diagnostics.Process.Start("notepad.exe", helpFilename)
- Catch ex As Exception
- MsgBox("Error accessing the help file: " & ex.Message)
- End Try
- End Sub
- Private Sub WriteHeader(TheDestinationStreamWriter As System.IO.StreamWriter)
- Dim DestinationLine As String
- DestinationLine = "FILE_VERSION: " + MyHeader.VERSION.ToString("F1", Globalization.CultureInfo.InvariantCulture)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "SENSOR_ROLE: " + MyHeader.SENSOR_ROLE
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "SENSOR_OUTPUT: " + MyHeader.SENSOR_OUTPUT
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "SENSOR_HOST_ID: " + MyHeader.SENSOR_HOST_ID
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "SENSOR_TECHNOLOGY: " + MyHeader.SENSOR_TECHNOLOGY
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "SENSOR_HW_ID: " + MyHeader.SENSOR_HW_ID
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "TASK/MISSION: " + MyHeader.TASK_MISSION
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- If ckbCorrectForUTC.Checked = True Then
- Dim TmpDate As DateTime
- TmpDate = MyHeader.StartDateTime.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "START_TIME: " + TmpDate.ToString("o")
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- TmpDate = MyHeader.StopDateTime.Subtract(MyHeader.TimeZoneOffset)
- DestinationLine = "END_TIME: " + TmpDate.ToString("o")
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- Else
- DestinationLine = "START_TIME: " + MyHeader.StartDateTime.ToString("o")
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "END_TIME: " + MyHeader.StopDateTime.ToString("o")
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- If MyHeader.TimeZoneOffset.Hours >= 0 Then
- DestinationLine = "TIME_ZONE_OFFSET: " + MyHeader.TimeZoneOffset.ToString("hh\:mm")
- Else
- DestinationLine = "TIME_ZONE_OFFSET: " + "-" + MyHeader.TimeZoneOffset.ToString("hh\:mm")
- End If
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "FREQ_RANGE_MHZ: " +
- MyHeader.FREQ_RANGE(0).ToString("F0", Globalization.CultureInfo.InvariantCulture) + " " +
- MyHeader.FREQ_RANGE(1).ToString("F0", Globalization.CultureInfo.InvariantCulture)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- If (MyGlobalBox.LatMax = MyGlobalBox.LatMin) And
- (MyGlobalBox.LonMax = MyGlobalBox.LonMin) And IsPointAllowed Then
- ' Point
- DestinationLine = "LOCATION: POINT " + MyGlobalBox.LatMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.AltMax.ToString(Globalization.CultureInfo.InvariantCulture)
- Else
- 'Box
- DestinationLine = "LOCATION: BOX " + MyGlobalBox.LatMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LatMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LatMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LonMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LonMax.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.LonMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.AltMin.ToString(Globalization.CultureInfo.InvariantCulture) +
- " " + MyGlobalBox.AltMax.ToString(Globalization.CultureInfo.InvariantCulture)
- End If
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "---"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End Sub
- Private Sub WriteSubHeader(TheDestinationStreamWriter As System.IO.StreamWriter, ByVal Analysis As Boolean)
- Dim DestinationLine As String
- DestinationLine = "MEASUREMENTFILE: " + MySubHeader.MEASUREMENTFILE
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "REVISIT_TIME(s): " + MySubHeader.REVISIT_TIME_s.ToString(Globalization.CultureInfo.InvariantCulture)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "RESOLUTION_BW(Hz): " + MySubHeader.RESOLUTION_BW_Hz.ToString(Globalization.CultureInfo.InvariantCulture)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "THRESHOLD_TYPE: " + MySubHeader.THRESHOLD_TYPE.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "THRESHOLD: " + MySubHeader.THRESHOLD.ToString((Globalization.CultureInfo.InvariantCulture))
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "BW_ESTIMATION_RATIO: " + MySubHeader.BW_ESTIMATION_RATIO.ToString((Globalization.CultureInfo.InvariantCulture))
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "FFT_WINDOW: " + MySubHeader.FFT_WINDOW.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "FRAME_AVG: " + MySubHeader.FRAME_AVG.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "NB_BLOCK: " + MySubHeader.FRAME_NB.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "IS_DF: " + MySubHeader.ISDF.ToString(Globalization.CultureInfo.InvariantCulture)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- If Analysis Then
- DestinationLine = "ANALYSIS_DURATION(MIN): " + MySubHeader.ANALYSIS_DURATION_MIN.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "Power(dBm)_range: " + Power_dBmRange.RangeStart.ToString + "/1/" + Power_dBmRange.RangeStop.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "SNR(dB)_range: " + SNR_dBRange.RangeStart.ToString + "/1/" + SNR_dBRange.RangeStop.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "Bandwidth(%)_range: " + BandwidthPercentRange.RangeStart.ToString + "/1/" + BandwidthPercentRange.RangeStop.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "CF_offset(%)_range: " + CORangePercentRange.RangeStart.ToString + "/1/" + CORangePercentRange.RangeStop.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- DestinationLine = "Burst_(dbms)_range: " + Burst_dBmsRange.RangeStart.ToString + "/1/" + Burst_dBmsRange.RangeStop.ToString
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- End Sub
- Private Sub WriteHisto(TheDestinationStreamWriter As System.IO.StreamWriter)
- Dim k, Count As Integer
- Dim DestinationLine As String = ""
- DestinationLine = "DATA_LABELS: ChannelID,Count,Power_histogramm_dBm"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- DestinationLine = MyChannels.ID(k).ToString + " " + MyChannels.Count(k).ToString + " " + String.Join(",", MyHisto(k).Power_dBmHisto)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- Next
- DestinationLine = "DATA_LABELS: ChannelID,Count,SNR_histogram_dB"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- DestinationLine = MyChannels.ID(k).ToString + " " + MyChannels.Count(k).ToString + " " + String.Join(",", MyHisto(k).SNR_dBHisto)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- Next
- 'OVERFLOW OCCURS HERE, BELOW
- DestinationLine = "DATA_LABELS: ChannelID,Count,CF_offset_histogram_%"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- DestinationLine = MyChannels.ID(k).ToString + " " + MyChannels.Count(k).ToString + " " + String.Join(",", MyHisto(k).CORangePercentHisto)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- Next
- DestinationLine = "DATA_LABELS: ChannelID,Count,Bandwidth_histogram_%"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- DestinationLine = MyChannels.ID(k).ToString + " " + MyChannels.Count(k).ToString + " " + String.Join(",", MyHisto(k).BandwidthPercentRangeHisto)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- Next
- DestinationLine = "DATA_LABELS: ChannelID,Count,Burst_on_histogram_dbms"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- Count = MyHisto(k).BurstOn_dBmsHisto.Sum
- DestinationLine = MyChannels.ID(k).ToString + " " + Count.ToString + " " + String.Join(",", MyHisto(k).BurstOn_dBmsHisto)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- Next
- DestinationLine = "DATA_LABELS: ChannelID,Count,Burst_off_histogram_dbms"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- Count = MyHisto(k).BurstOff_dBmsHisto.Sum
- DestinationLine = MyChannels.ID(k).ToString + " " + Count.ToString + " " + String.Join(",", MyHisto(k).BurstOff_dBmsHisto)
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End If
- Next
- DestinationLine = "---"
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End Sub
- Private Sub WriteMeasurement(ByVal output As MeasurementData, TheDestinationStreamWriter As System.IO.StreamWriter)
- Dim DestinationLine As String
- DestinationLine = output.FixedDate & "," & MyBox.LatMin & "," & MyBox.LonMin & "," & MyBox.AltMin & "," & output.Status & "," & output.MovementStatus & "," & output.GroundSpeed & "," & output.GroundAcceleration & "," & output.TrackMadeGood & "," & output.TrueHeading & ","
- TheDestinationStreamWriter.Write(DestinationLine)
- DestinationLine = "" & output.ChannelNum & "," & output.Power & "," & output.SNR & "," & output.Freq & "," & output.BW & "," & output.Dir & "," & output.DirQuality
- TheDestinationStreamWriter.WriteLine(DestinationLine)
- End Sub
- Private Sub ResetArrays()
- For k = 0 To TOTAL_FREQUENCIES - 1
- If MyChannels.Count(k) Then
- Array.Clear(MyHisto(k).Power_dBmHisto, 0, MyHisto(k).Power_dBmHisto.Length)
- Array.Clear(MyHisto(k).SNR_dBHisto, 0, MyHisto(k).SNR_dBHisto.Length)
- Array.Clear(MyHisto(k).CORangePercentHisto, 0, MyHisto(k).CORangePercentHisto.Length)
- Array.Clear(MyHisto(k).BandwidthPercentRangeHisto, 0, MyHisto(k).BandwidthPercentRangeHisto.Length)
- Array.Clear(MyHisto(k).BurstOn_dBmsHisto, 0, MyHisto(k).BurstOn_dBmsHisto.Length)
- Array.Clear(MyHisto(k).BurstOff_dBmsHisto, 0, MyHisto(k).BurstOff_dBmsHisto.Length)
- MyChannels.Count(k) = 0
- End If
- Next
- End Sub
- Private Sub ResetBox(ByRef Box As udtLocationBox)
- Box.AltMax = -100
- Box.AltMin = 10000
- Box.LatMax = -90
- Box.LatMin = 90
- Box.LonMax = -180
- Box.LonMin = 180
- End Sub
- Private Sub LogError(errorMessage As String)
- RaiseEvent ErrorReport(errorMessage)
- ErrorReportToStdOutput(errorMessage)
- If Not isHidden Then
- MsgBox(errorMessage, MsgBoxStyle.Exclamation, Me.Text)
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement