Skip to content

Commit

Permalink
Implemented import for BGS HYBNAT result files (*.wel, *.bcs)
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasRosskopf committed Dec 11, 2023
1 parent 1bd9eea commit 25be62e
Show file tree
Hide file tree
Showing 4 changed files with 473 additions and 4 deletions.
29 changes: 25 additions & 4 deletions source/Classes/TimeSeriesFile.vb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Public MustInherit Class TimeSeriesFile
CSV
DFS0
GISMO_WEL
HYBNAT_BCS
HYBNAT_WEL
HYDRO_AS_DAT
HYSTEM_WEL
JAMS
Expand Down Expand Up @@ -63,6 +65,7 @@ Public MustInherit Class TimeSeriesFile
''' </remarks>
Public MustInherit Class FileExtensions
Public Shared ReadOnly ASC As String = ".ASC"
Public Shared ReadOnly BCS As String = ".BCS" 'BGS HYBNAT BCS format
Public Shared ReadOnly BIN As String = ".BIN" 'SYDRO binary format
Public Shared ReadOnly CSV As String = ".CSV"
Public Shared ReadOnly DAT As String = ".DAT"
Expand All @@ -73,7 +76,7 @@ Public MustInherit Class TimeSeriesFile
Public Shared ReadOnly REG As String = ".REG"
Public Shared ReadOnly SMB As String = ".SMB"
Public Shared ReadOnly TEN As String = ".TEN"
Public Shared ReadOnly TXT As String = ".TXT" 'SWMM routing interface file, SWMM LID report file or generic text file
Public Shared ReadOnly TXT As String = ".TXT" 'SWMM routing interface file, SWMM LID report file or generic text file
Public Shared ReadOnly UVF As String = ".UVF"
Public Shared ReadOnly WBL As String = ".WBL" 'SYDRO binary WEL format
Public Shared ReadOnly WEL As String = ".WEL"
Expand All @@ -92,6 +95,7 @@ Public MustInherit Class TimeSeriesFile
"Text files (*.txt)|*.txt|" &
"CSV files (*.csv)|*.csv|" &
"DHI MIKE DFS0 files (*.dfs0)|*.dfs0|" &
"HYBNAT files (*.bcs, *.wel)|*.bcs;*.wel|" &
"HYDRO_AS-2D result files (*.dat)|*.dat|" &
"Hystem-Extran files (*.dat, *.reg)|*.dat;*.reg|" &
"JAMS timeseries result files (*.dat)|*.dat|" &
Expand Down Expand Up @@ -154,7 +158,7 @@ Public MustInherit Class TimeSeriesFile
End Property

''' <summary>
''' Stores the TimeSeries read from the file.
''' Stores the TimeSeries read from the file.
''' The key corresponds to the column index stored as <seealso cref="TimeSeriesInfo.Index"/> in <seealso cref="TimeSeriesInfos"/>.
''' </summary>
Public TimeSeries As Dictionary(Of Integer, TimeSeries)
Expand Down Expand Up @@ -347,7 +351,7 @@ Public MustInherit Class TimeSeriesFile
''' </summary>
''' <param name="index">Index of the desired timeseries within the file</param>
''' <returns>The timeseries</returns>
''' <remarks>If the timeseries has not been imported yet, an import is initiated.
''' <remarks>If the timeseries has not been imported yet, an import is initiated.
''' Throws an exception if the timeseries cannot be found in the file.</remarks>
Public ReadOnly Property getTimeSeries(Optional index As Integer = 0) As TimeSeries
Get
Expand All @@ -369,7 +373,7 @@ Public MustInherit Class TimeSeriesFile
''' </summary>
''' <param name="title">Title of the desired timeseries.</param>
''' <returns>The timeseries</returns>
''' <remarks>If the timeseries has not been imported yet, an import is initiated.
''' <remarks>If the timeseries has not been imported yet, an import is initiated.
''' Throws an exception if the timeseries cannot be found in the file.</remarks>
Public ReadOnly Property getTimeSeries(title As String) As TimeSeries
Get
Expand Down Expand Up @@ -530,6 +534,10 @@ Public MustInherit Class TimeSeriesFile
Return FileExtensions.DFS0
Case FileTypes.GISMO_WEL
Return FileExtensions.ASC
Case FileTypes.HYBNAT_BCS
Return FileExtensions.BCS
Case FileTypes.HYBNAT_WEL
Return FileExtensions.WEL
Case FileTypes.HYDRO_AS_DAT
Return FileExtensions.DAT
Case FileTypes.HYSTEM_WEL
Expand Down Expand Up @@ -635,6 +643,11 @@ Public MustInherit Class TimeSeriesFile
fileType = FileTypes.ASC
End If

Case FileExtensions.BCS
'HYBNAT BCS file
Log.AddLogEntry(levels.info, $"Assuming HYBNAT BCS format for file {fileName}.")
fileType = FileTypes.HYBNAT_BCS

Case FileExtensions.BIN
'SYDRO binary file
Log.AddLogEntry(levels.info, $"Assuming SYDRO binary format for file {fileName}.")
Expand Down Expand Up @@ -754,6 +767,10 @@ Public MustInherit Class TimeSeriesFile
'SYDRO binary WEL file
Log.AddLogEntry(levels.info, $"Detected SYDRO binary WEL format for file {fileName}.")
fileType = FileTypes.WBL
ElseIf Fileformats.HYBNAT_WEL.verifyFormat(file) Then
'HYBNAT WEL file
Log.AddLogEntry(levels.info, $"Detected HYBNAT WEL format for file {fileName}.")
fileType = FileTypes.HYBNAT_WEL
End If

Case FileExtensions.WVP
Expand Down Expand Up @@ -816,6 +833,10 @@ Public MustInherit Class TimeSeriesFile
FileInstance = New Fileformats.DFS0(file)
Case FileTypes.GISMO_WEL
FileInstance = New Fileformats.GISMO_WEL(file)
Case FileTypes.HYBNAT_BCS
FileInstance = New Fileformats.HYBNAT_BCS(file)
Case FileTypes.HYBNAT_WEL
FileInstance = New Fileformats.HYBNAT_WEL(file)
Case FileTypes.HYSTEM_REG
FileInstance = New Fileformats.HystemExtran_REG(file)
Case FileTypes.HYSTEM_WEL
Expand Down
186 changes: 186 additions & 0 deletions source/FileFormats/HYBNAT_BCS.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
'BlueM.Wave
'Copyright (C) BlueM Dev Group
'<https://www.bluemodel.org>
'
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU Lesser General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU Lesser General Public License for more details.
'
'You should have received a copy of the GNU Lesser General Public License
'along with this program. If not, see <https://www.gnu.org/licenses/>.
'
Imports System.IO

Namespace Fileformats

''' <summary>
''' Class for HYBNAT BCS files
''' </summary>
Public Class HYBNAT_BCS
Inherits TimeSeriesFile

''' <summary>
''' The unit of the time series
''' </summary>
''' <remarks> Is fixed for each type of file</remarks>
Private _unit As String

''' <summary>
''' Referencedate for the beginning of the simulation
''' </summary>
''' <remarks>default: 01.01.2000 00:00:00</remarks>
Public refDate As DateTime

''' <summary>
''' Set if the import dialog should be used
''' </summary>
''' <value></value>
''' <returns>True</returns>
''' <remarks></remarks>
Public Overrides ReadOnly Property UseImportDialog() As Boolean
Get
Return True
End Get
End Property

''' <summary>
''' Instanciates a new HYBNAT BCS file
''' </summary>
''' <param name="file">Path to file</param>
''' <remarks></remarks>
Public Sub New(file As String)

Call MyBase.New(file)

'Default settings
iLineHeadings = 1
iLineData = 2
UseUnits = False
_unit = "m³/s"
IsColumnSeparated = True
Separator = Constants.semicolon
DateTimeColumnIndex = 0
refDate = New DateTime(2000, 1, 1, 0, 0, 0)

Call readSeriesInfo()

End Sub

''' <summary>
''' Checks if the file is a HYBNAT BCS file
''' </summary>
''' <param name="file">Path to file</param>
''' <returns>Boolean</returns>
''' <remarks>Check is based on file extension and line 1 (must start with "time;")</remarks>
Public Shared Function verifyFormat(file As String) As Boolean
'Check if file name ends with ".bcs"
Dim filename As String = Path.GetFileName(file).ToLower()
If Not filename.EndsWith(".bcs") Then Return False

'Open file
Dim stream As New FileStream(file, FileMode.Open, IO.FileAccess.Read)
Dim reader As New StreamReader(stream, System.Text.Encoding.Default)
Dim syncReader = TextReader.Synchronized(reader)

'Check if first line starts with "time;"
If Not syncReader.ReadLine.ToString.Trim().StartsWith("time;") Then Return False

'Close file
syncReader.Close()
reader.Close()
stream.Close()

'File is valid
Return True
End Function

''' <summary>
''' Read number of columns and their names
''' </summary>
''' <remarks></remarks>
Public Overrides Sub readSeriesInfo()
'Clear series infos
TimeSeriesInfos.Clear()

'Open file
Dim stream As New FileStream(File, FileMode.Open, IO.FileAccess.Read)
Dim reader As New StreamReader(stream, System.Text.Encoding.Default)
Dim syncReader = TextReader.Synchronized(reader)

'Read first line for column names
Dim columnNames() As String = syncReader.ReadLine.Split(Separator.ToChar)

'Close file
syncReader.Close()
reader.Close()
stream.Close()

'Store series info
For i = 0 To columnNames.Count - 1
'Overjump time column and empty columns
If i = DateTimeColumnIndex Then Continue For
If columnNames(i).Trim() = "" Then Continue For

'Add series info
Dim seriesInfo = New TimeSeriesInfo With {
.Name = columnNames(i).Trim(),
.Unit = _unit,
.Index = i + 1
}
TimeSeriesInfos.Add(seriesInfo)
Next
End Sub

''' <summary>
''' Reads the time series from the file
''' </summary>
''' <remarks></remarks>
Public Overrides Sub readFile()
'Show dialog for setting the reference date
Dim dlg As New HYDRO_AS_2D_Diag()
dlg.ShowDialog()
refDate = dlg.DateTimePicker_refDate.Value

'Instantiate time series
For Each seriesInfo As TimeSeriesInfo In SelectedSeries
Dim timeSeries = New TimeSeries(seriesInfo.Name) With {
.Unit = seriesInfo.Unit,
.DataSource = New TimeSeriesDataSource(File, seriesInfo.Name),
.Interpretation = BlueM.Wave.TimeSeries.InterpretationEnum.Instantaneous
}
MyBase.TimeSeries.Add(seriesInfo.Index, timeSeries)
Next

'Open file and overjump line with headings
Dim stream As New FileStream(Me.File, FileMode.Open, IO.FileAccess.Read)
Dim reader As New StreamReader(stream, Me.Encoding)
Dim syncReader = TextReader.Synchronized(reader)
syncReader.ReadLine()

'Read data until end of file
Do
'Get values and datetime (calculate from reference date and seconds)
Dim values = syncReader.ReadLine.Split(Separator.ToChar)
Dim datetime = refDate + New TimeSpan(0, 0, Helpers.StringToDouble(values(DateTimeColumnIndex)))

'Add nodes to time series
For Each seriesInfo As TimeSeriesInfo In SelectedSeries
TimeSeries(seriesInfo.Index).AddNode(datetime, Helpers.StringToDouble(values(seriesInfo.Index - 1)))
Next
Loop Until syncReader.Peek() = -1

'Close file
syncReader.Close()
reader.Close()
stream.Close()
End Sub

End Class

End Namespace
Loading

0 comments on commit 25be62e

Please sign in to comment.