Exemple #1
0
    def write(self, filename, data):
        """
        Function: write to a pre-created dfs3 file.

        filename:
            full path and filename to existing dfs3 file

        data:
            list of arrays. len(data) must equal the number of items in the dfs3.
            Each array must be of dimension t,z,y,x
        """

        # Open the dfs file for writing
        dfs = DfsFileFactory.Dfs3FileOpenEdit(filename)

        # Determine the size of the grid
        number_y = dfs.SpatialAxis.YCount
        number_x = dfs.SpatialAxis.XCount
        number_z = dfs.SpatialAxis.ZCount
        n_time_steps = dfs.FileInfo.TimeAxis.NumberOfTimeSteps
        n_items = safe_length(dfs.ItemInfo)

        deletevalue = dfs.FileInfo.DeleteValueFloat

        if not all(np.shape(d)[0] == n_time_steps for d in data):
            raise Warning(
                "ERROR data matrices in the time dimension do not all match in the data list. "
                "Data is list of matices [time,y,x]")
        if not all(np.shape(d)[1] == number_z for d in data):
            raise Warning(
                "ERROR data matrices in the Y dimension do not all match in the data list. "
                "Data is list of matices [time,y,x]")
        if not all(np.shape(d)[2] == number_y for d in data):
            raise Warning(
                "ERROR data matrices in the Y dimension do not all match in the data list. "
                "Data is list of matices [time,y,x]")
        if not all(np.shape(d)[3] == number_x for d in data):
            raise Warning(
                "ERROR data matrices in the X dimension do not all match in the data list. "
                "Data is list of matices [time, y, x]")
        if not len(data) == n_items:
            raise Warning(
                "The number of matrices in data do not match the number of items in the dfs3 file."
            )

        for i in range(n_time_steps):
            for item in range(n_items):
                d = data[item][i]
                d[np.isnan(d)] = deletevalue
                d = np.flipud(d)
                darray = to_dotnet_float_array(d.reshape(d.size, 1)[:, 0])

                dfs.WriteItemTimeStepNext(0, darray)

        dfs.Close()
Exemple #2
0
    def write(self, dfs3file, data):
        """
        Function: write to a pre-created dfs3 file. Only ONE item supported.

        NOTE:
            The dfs3 file must be pre-created with corresponding y,x, z dimensions and number of time steps.

        The Data Matrix
            size ( y, x, z, nt)

        usage:
            write( filename, data) where  data( y, x, z, nt)

        Returns:
            Nothing
        """

        # Open the dfs file for writing
        dfs = DfsFileFactory.Dfs3FileOpenEdit(dfs3file)

        # Determine the size of the grid
        yNum = dfs.SpatialAxis.YCount
        xNum = dfs.SpatialAxis.XCount
        zNum = dfs.SpatialAxis.ZCount

        nt = dfs.FileInfo.TimeAxis.NumberOfTimeSteps
        deletevalue = dfs.FileInfo.DeleteValueFloat

        if data.shape[0] != yNum:
            sys.exit("ERROR Y dimension does not match")
        elif data.shape[1] != xNum:
            sys.exit("ERROR X dimension does not match")
        elif data.shape[2] != zNum:
            sys.exit("ERROR X dimension does not match")
        elif data.shape[3] != nt:
            sys.exit("ERROR Number of Time Steps dimension does not match")

        for it in range(nt):
            d = data[:, :, :, it]
            d[np.isnan(d)] = deletevalue
            d = d.swapaxes(1, 2).swapaxes(1, 0)
            d = d[:, ::-1, :]
            darray = Array[System.Single](np.asarray(d).reshape(-1))
            dfs.WriteItemTimeStepNext(0, darray)

        dfs.Close()