Exemple #1
0
 def _load_file(self):
     """Load the file."""
     if not os.path.exists(self.file_path):
         raise FileExistsError(f"File {self.file_path} does not exist.")
     self.file = ResultData()
     self.file.Connection = Connection.Create(self.file_path)
     self.file.Load()
     self._closed = False
Exemple #2
0
    def _load_file(self):
        if not os.path.exists(self.file_path):
            raise FileExistsError(f"File {self.file_path} does not exist.")

        self._data = ResultData()
        self._data.Connection = Connection.Create(self.file_path)
        self._data.Load(Diagnostics())
        self._query = ResultDataQuery(self._data)
Exemple #3
0
    def __read(file_path):
        """
        Read the res1d file
        """
        if not os.path.exists(file_path):
            raise FileExistsError(f"File does not exist {file_path}")

        file = ResultData()
        file.Connection = Connection.Create(file_path)
        file.Load()
        return file
Exemple #4
0
    def __init__(self, filename, useFilter=None, outputDataItem=True):
        # Load result file
        self.diagnostics = Diagnostics("Loading file")
        self.resultData = ResultData()
        self.resultData.Connection = Connection.Create(filename)
        self.useFilter = useFilter
        self.outputDataItem = outputDataItem

        if useFilter:
            self.SetupFilter()
        else:
            self.Load()

        # Searcher is helping to find reaches, nodes and catchments
        self.searcher = ResultDataSearch(self.resultData)
Exemple #5
0
    def _load_header(self):
        if not os.path.exists(self.file_path):
            raise FileExistsError(f"File {self.file_path} does not exist.")

        self._data = ResultData()
        self._data.Connection = Connection.Create(self.file_path)
        self._diagnostics = Diagnostics("Loading header")

        if self._lazy_load:
            self._data.Connection.BridgeName = "res1dlazy"

        if self._use_filter:
            self._data.LoadHeader(True, self._diagnostics)
        else:
            self._data.LoadHeader(self._diagnostics)
# by MIKE 1D, and convert to res1d file. This example is converting
# a MOUSE RR file (.crf) to res1d.

import sys
import clr

# The SetupLatest method will make your script find the latest MIKE assemblies at runtime.
clr.AddReference(
    "DHI.Mike.Install, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c513450b5d0bf0bf"
)
from DHI.Mike.Install import MikeImport, MikeProducts
MikeImport.SetupLatest()
print('Found MIKE in: ' + MikeImport.ActiveProduct().InstallRoot)

clr.AddReference("DHI.Mike1D.ResultDataAccess")
clr.AddReference("DHI.Mike1D.Generic")
from DHI.Mike1D.ResultDataAccess import ResultData, ResultDataSearch
from DHI.Mike1D.Generic import Connection

resultData = ResultData()
resultData.Connection = Connection.Create("DemoBase.crf")
resultData.Load()

# For crf files, set Type to "" if None (null) - work-around for a bug
for c in resultData.Catchments:
    if (c.Type is None):
        c.Type = ""

resultData.Connection = Connection.Create("DemoBase-crf.res1d")
resultData.Save()
Exemple #7
0
def read(res1DFile, extractionPoints):
    import clr
    import pandas as pd
    import datetime
    import numpy as np
    import os.path

    clr.AddReference("DHI.Mike1D.ResultDataAccess")
    from DHI.Mike1D.ResultDataAccess import ResultData

    clr.AddReference("DHI.Mike1D.Generic")
    from DHI.Mike1D.Generic import Connection

    clr.AddReference("System")

    if os.path.isfile(res1DFile) is False:
        print("ERROR, File Not Found: " + res1DFile)

    # Create a ResultData object and read the data file.
    rd = ResultData()
    rd.Connection = Connection.Create(res1DFile)
    rd.Load()

    reachNums = []
    dataItemTypes = []
    indices = []

    tol = 0.1

    # Find the Item
    for ep in extractionPoints:
        item = -1
        reachNumber = -1
        idx = -1
        for i in range(0, rd.Reaches.Count):
            if rd.Reaches.get_Item(i).Name.lower().strip() == ep.BranchName.lower().strip():

                reach = rd.Reaches.get_Item(i)
                for j in range(0, reach.GridPoints.Count):
                    # print(str(j))
                    if abs(float(reach.GridPoints.get_Item(j).Chainage) - ep.Chainage) < tol:
                        if 'waterlevel' in ep.VariableType.lower().strip().replace(" ", ""):
                            idx = int(j / 2)
                        elif 'discharge' in ep.VariableType.lower().strip().replace(" ", ""):

                            idx = int((j - 1) / 2)
                        elif 'pollutant' in ep.VariableType.lower().strip().replace(" ", ""):
                            idx = int((j - 1) / 2)
                        else:
                            print('ERROR. Variable Type must be either Water Level, Discharge, or Pollutant')
                        reachNumber = i
                        break
                        break
                        break

        for i in range(0, rd.get_Quantities().Count):
            if ep.VariableType.lower().strip().replace(" ", "") == rd.get_Quantities().get_Item(
                    i).Description.lower().strip().replace(" ", ""):
                item = i
                break

        indices.append(idx)
        reachNums.append(reachNumber)
        dataItemTypes.append(item)

    if -1 in reachNums:
        print('ERROR. Reach Not Found')
        quit()
    if -1 in dataItemTypes:
        print('ERROR. Item Not Found')
        quit()
    if -1 in indices:
        print('ERROR. Chainage Not Found')
        quit()

        # Get the Data
    df = pd.DataFrame()
    for i in range(0, len(indices)):
        d = rd.Reaches.get_Item(reachNums[i]).get_DataItems().get_Item(dataItemTypes[i]).CreateTimeSeriesData(
            indices[i])
        name = extractionPoints[i].VariableType + ' ' + str(extractionPoints[i].BranchName) + ' ' + str(
            extractionPoints[i].Chainage)
        d = pd.Series(list(d))
        d = d.rename(name)
        df[name] = d

    # Get the Times
    times = []
    for i in range(0, rd.TimesList.Count):
        it = rd.TimesList.get_Item(i)
        t = pd.Timestamp(
            datetime.datetime(it.get_Year(), it.get_Month(), it.get_Day(), it.get_Hour(), it.get_Minute(),
                              it.get_Second()))
        times.append(t)

    df.index = pd.DatetimeIndex(times)

    rd.Dispose()

    return df