Пример #1
0
    def write(self, filename, data):

        """write overwrites an existing dfs0 file.

        Parameters
        ----------
        filename: str
            Full path and filename to dfs0 to be modified.
        data: list[np.array]
            data to overwrite
        """

        if not os.path.exists(filename):
            raise Warning("filename - File does not Exist %s", filename)

        try:
            dfs = DfsFileFactory.DfsGenericOpenEdit(filename)
        except IOError:
            print("cannot open", filename)

        delete_value = dfs.FileInfo.DeleteValueFloat

        n_items = len(dfs.ItemInfo)
        n_time_steps = dfs.FileInfo.TimeAxis.NumberOfTimeSteps

        # Makes sure the data to write to the file matches the dfs0 file
        if n_time_steps != data[0].shape[0]:
            raise Exception(
                f"Inconsistent data size. nt (row count) must be size {n_time_steps}"
            )

        if n_items != len(data):
            raise Exception(f"Number of items must be size {n_items}")

        for i in range(n_items):
            d = data[i]

            d[np.isnan(d)] = delete_value

        # Get the date times in seconds (from start)
        dfsdata = Dfs0Util.ReadDfs0DataDouble(dfs)
        t_seconds = [dfsdata[i,0] for i in range(n_time_steps)]

        dfs.Reset()

        data1 = np.stack(data, axis=1)
        Dfs0Util.WriteDfs0DataDouble(dfs, t_seconds, to_dotnet_array(data1))

        dfs.Close()
Пример #2
0
    def __read(self, filename):
        """Read data from the dfs0 file
        """
        if not os.path.exists(filename):
            raise Warning("filename - File does not Exist %s", filename)

        dfs = DfsFileFactory.DfsGenericOpen(filename)
        self._dfs = dfs

        n_items = safe_length(dfs.ItemInfo)
        nt = dfs.FileInfo.TimeAxis.NumberOfTimeSteps

        items = []
        for i in range(n_items):
            eumItem = dfs.ItemInfo[i].Quantity.Item
            eumUnit = dfs.ItemInfo[i].Quantity.Unit
            name = dfs.ItemInfo[i].Name
            itemtype = EUMType(eumItem)
            unit = EUMUnit(eumUnit)
            item = ItemInfo(name, itemtype, unit)
            items.append(item)

        # BULK READ THE DFS0
        dfsdata = Dfs0Util.ReadDfs0DataDouble(dfs)

        t = []
        starttime = dfs.FileInfo.TimeAxis.StartDateTime

        # EMPTY Data Block for copying the Results
        for it in range(dfs.FileInfo.TimeAxis.NumberOfTimeSteps):
            t.append(
                starttime.AddSeconds(
                    dfsdata[it, 0]).ToString("yyyy-MM-dd HH:mm:ss"))

        # Copies the System Array to a numpy matrix
        # First column in the time (the rest is the data)
        data = np.fromiter(dfsdata, np.float64).reshape(nt, n_items + 1)[:,
                                                                         1::]

        mask = np.isclose(data, dfs.FileInfo.DeleteValueFloat, atol=1e-36)
        data[mask] = np.nan
        # mask = np.isclose(
        #    data, dfs.FileInfo.DeleteValueDouble, atol=1e-34
        # )  # TODO needs to be verified
        # data[mask] = np.nan

        dfs.Close()

        return data, t, items
Пример #3
0
    def __read(self, filename):
        """Read data from the dfs0 file
        """
        if not os.path.exists(filename):
            raise Warning("filename - File does not Exist %s", filename)

        dfs = DfsFileFactory.DfsGenericOpen(filename)
        self._dfs = dfs

        n_items = safe_length(dfs.ItemInfo)
        nt = dfs.FileInfo.TimeAxis.NumberOfTimeSteps

        items = []
        for i in range(n_items):
            eumItem = dfs.ItemInfo[i].Quantity.Item
            eumUnit = dfs.ItemInfo[i].Quantity.Unit
            name = dfs.ItemInfo[i].Name
            itemtype = EUMType(eumItem)
            unit = EUMUnit(eumUnit)
            item = ItemInfo(name, itemtype, unit)
            items.append(item)

        # BULK READ THE DFS0
        dfsdata = Dfs0Util.ReadDfs0DataDouble(dfs)
        # First column is the time (the rest is the data)
        t_seconds = [dfsdata[i, 0] for i in range(nt)]

        start_time = from_dotnet_datetime(dfs.FileInfo.TimeAxis.StartDateTime)
        time = [start_time + timedelta(seconds=tsec) for tsec in t_seconds]

        # TODO use to_numpy ?
        data = np.fromiter(dfsdata, np.float64).reshape(nt, n_items + 1)[:,
                                                                         1::]

        mask = np.isclose(data, dfs.FileInfo.DeleteValueFloat, atol=1e-36)
        data[mask] = np.nan
        # mask = np.isclose(
        #    data, dfs.FileInfo.DeleteValueDouble, atol=1e-34
        # )  # TODO needs to be verified
        # data[mask] = np.nan

        dfs.Close()

        return data, time, items
Пример #4
0
    def __read(self, filename):
        """
        Read all data from a dfs0 file.
        """
        self._time_column_index = 0  # First column is time (the rest is data)

        self._dfs = DfsFileFactory.DfsGenericOpen(filename)
        raw_data = Dfs0Util.ReadDfs0DataDouble(self._dfs)  # Bulk read the data

        matrix = self.__to_numpy_with_nans(raw_data)

        data = []
        for i in range(matrix.shape[1]):
            data.append(matrix[:, i])

        time = list(self.__get_time(raw_data))
        items = list(self.__get_items())

        self._dfs.Close()

        return Dataset(data, time, items)
Пример #5
0
    def __read(self, filename):
        """
        Read data from a dfs0 file.
        """
        if not os.path.exists(filename):
            raise FileNotFoundError(f"File {filename} not found.")

        self._dfs = DfsFileFactory.DfsGenericOpen(filename)
        self._n_items = safe_length(self._dfs.ItemInfo)
        self._n_timesteps = self._dfs.FileInfo.TimeAxis.NumberOfTimeSteps
        self._time_column_index = 0  # First column is time (the rest is data)

        raw_data = Dfs0Util.ReadDfs0DataDouble(self._dfs)  # Bulk read the data

        data = self.__to_numpy_with_nans(raw_data)
        time = list(self.__get_time(raw_data))
        items = list(self.__get_items())

        self._dfs.Close()

        return data, time, items
Пример #6
0
    def __read(self, filename):
        """Read data from the dfs0 file
        """
        if not os.path.exists(filename):
            raise Warning("filename - File does not Exist %s", filename)

        dfs = DfsFileFactory.DfsGenericOpen(filename)

        n_items = safe_length(dfs.ItemInfo)
        nt = dfs.FileInfo.TimeAxis.NumberOfTimeSteps

        names = []
        for i in range(n_items):
            names.append(dfs.ItemInfo[i].Name)

        # BULK READ THE DFS0
        dfsdata = Dfs0Util.ReadDfs0DataDouble(dfs)

        t = []
        starttime = dfs.FileInfo.TimeAxis.StartDateTime

        # EMPTY Data Block for copying the Results
        for it in range(dfs.FileInfo.TimeAxis.NumberOfTimeSteps):
            t.append(
                starttime.AddSeconds(
                    dfsdata[it, 0]).ToString("yyyy-MM-dd HH:mm:ss"))

        # Copies the System Array to a numpy matrix
        # First column in the time (the rest is the data)
        data = np.fromiter(dfsdata, np.float64).reshape(nt, n_items + 1)[:,
                                                                         1::]

        data[data == -1.0000000180025095e-35] = np.nan
        data[data == -1.0000000031710769e-30] = np.nan
        data[data == dfs.FileInfo.DeleteValueFloat] = np.nan
        data[data == dfs.FileInfo.DeleteValueDouble] = np.nan

        dfs.Close()

        return data, t, names
Пример #7
0
    def write(self, filename, data):
        """
        Overwrite data in an existing dfs0 file.

        Parameters
        ----------
        filename: str
            Full path and filename to dfs0 to be modified.
        data: list[np.array]
            New data to write.
        """
        dfs, n_items, n_time_steps = self._validate_and_open_dfs(
            filename, data)

        # Get time in seconds from start
        existing_data = Dfs0Util.ReadDfs0DataDouble(dfs)
        time = [existing_data[i, 0] for i in range(n_time_steps)]

        # Overwrite with new data
        dfs.Reset()
        new_data = np.nan_to_num(data, nan=dfs.FileInfo.DeleteValueFloat)
        new_data_dotnet = to_dotnet_array(np.stack(new_data, axis=1))
        Dfs0Util.WriteDfs0DataDouble(dfs, time, new_data_dotnet)
        dfs.Close()
Пример #8
0
    def write(self, filename, data):
        """write overwrites an existing dfs0 file.

        Parameters
        ----------
        filename: str
            Full path and filename to dfs0 to be modified.
        data: list[np.array]
            data to overwrite
        """

        if not os.path.exists(filename):
            raise Warning("filename - File does not Exist %s", filename)

        try:
            dfs = DfsFileFactory.DfsGenericOpenEdit(filename)
        except IOError:
            print("cannot open", filename)

        delete_value = dfs.FileInfo.DeleteValueFloat

        n_items = len(dfs.ItemInfo)
        n_time_steps = dfs.FileInfo.TimeAxis.NumberOfTimeSteps

        # Makes sure the data to write to the file matches the dfs0 file
        if n_time_steps != data[0].shape[0]:
            raise Exception(
                f"Inconsistent data size. nt (row count) must be size {n_time_steps}"
            )

        if n_items != len(data):
            raise Exception(f"Number of items must be size {n_items}")

        for i in range(n_items):
            d = data[i]

            d[np.isnan(d)] = delete_value

        d = Array.CreateInstance(System.Single, 1)

        # Get the date times in seconds (from start)
        dfsdata = Dfs0Util.ReadDfs0DataDouble(dfs)

        t = []
        # starttime = dfs.FileInfo.TimeAxis.StartDateTime

        for it in range(dfs.FileInfo.TimeAxis.NumberOfTimeSteps):
            t.append(dfsdata[it, 0])

        dfs.Reset()

        # COPY OVER THE DATA
        for it in range(n_time_steps):

            for ii in range(n_items):

                d = Array[System.Single](np.array(data[ii][it:it + 1]))

                # dt = (t[it] - t[0]).total_seconds()
                dt = t[it]
                dfs.WriteItemTimeStepNext(dt, d)

        dfs.Close()