Ejemplo n.º 1
0
    def test_UpdateCustomBlockDataTest(self):

        originalFilename = "testdata/OresundHD.dfs2"
        filename = "testdata/testtmp/test_copy_OresundHD_cb.dfs2"

        testUtil.copy_file(originalFilename, filename)

        # Check initial value
        dfsFile = DfsFileFactory.DfsGenericOpen(filename)
        fileInfo = dfsFile.FileInfo
        customBlock = fileInfo.CustomBlocks[0]
        assert_equal(10, customBlock.Values[3])
        dfsFile.Close()

        # Modify value
        dfsFile = DfsFileFactory.DfsGenericOpenEdit(filename)
        fileInfo = dfsFile.FileInfo
        customBlock = fileInfo.CustomBlocks[0]
        customBlock.Values[3] = 25
        dfsFile.Close()

        # Check new value
        dfsFile = DfsFileFactory.DfsGenericOpen(filename)
        fileInfo = dfsFile.FileInfo
        customBlock = fileInfo.CustomBlocks[0]
        assert_equal(25, customBlock.Values[3])
        dfsFile.Close()
Ejemplo n.º 2
0
def test_iteminfo():
    dfs = DfsFileFactory.DfsGenericOpen("testdata/TemporalEqCal.dfs0")
    #iinfo1 = dfs.GetItemInfo(1)  # Note 1-based
    iinfo1 = dfs.ItemInfo[0]

    dfs.Close()
    assert iinfo1.Name == "WaterLevel item"
Ejemplo n.º 3
0
def test_read_itemtimestepnext():

    dfs = DfsFileFactory.DfsGenericOpen("testdata/TemporalEqCal.dfs0")
    dfs.Reset()

    data = dfs.ReadItemTimeStepNext()

    dfs.Close()

    assert data.Data.shape == (1, )
Ejemplo n.º 4
0
    def test_ModifyEqCalTest(self):
        sourcefilename = "testdata/TemporalEqCal.dfs0"
        filename = "testdata/testtmp/test_temporal_modifyEqCal.dfs0"

        testUtil.copy_file(sourcefilename, filename)

        dfsFile = DfsFileFactory.DfsGenericOpen(filename)
        timeAxis = dfsFile.FileInfo.TimeAxis

        assert_equal(0, timeAxis.FirstTimeStepIndex)
        assert_equal(4, timeAxis.StartTimeOffset)
        assert_equal(eumUnit.eumUsec, timeAxis.TimeUnit)
        assert_equal(datetime.datetime(2010, 1, 4, 12, 34, 00),
                     timeAxis.StartDateTime)
        assert_equal(10, timeAxis.TimeStep)

        dfsFile.Close()

        # Update temporal axis
        dfsFile = DfsFileFactory.DfsGenericOpenEdit(filename)
        timeAxis = dfsFile.FileInfo.TimeAxis

        timeAxis.FirstTimeStepIndex = 3
        timeAxis.StartTimeOffset = 6
        timeAxis.StartDateTime = datetime.datetime(2009, 2, 2, 21, 43, 00)
        timeAxis.TimeUnit = eumUnit.eumUminute
        timeAxis.TimeStep = 1

        dfsFile.Close()

        # Load file from disc again, and check time axis
        dfsFile = DfsFileFactory.DfsGenericOpen(filename)
        timeAxis = dfsFile.FileInfo.TimeAxis

        assert_equal(3, timeAxis.FirstTimeStepIndex)
        assert_equal(6, timeAxis.StartTimeOffset)
        assert_equal(eumUnit.eumUminute, timeAxis.TimeUnit)
        assert_equal(datetime.datetime(2009, 2, 2, 21, 43, 00),
                     timeAxis.StartDateTime)
        assert_equal(1, timeAxis.TimeStep)

        dfsFile.Close()
Ejemplo n.º 5
0
    def ModifyDfs2ItemAxis(filename):

        file = DfsFileFactory.Dfs2FileOpenEdit(filename)

        axisEqD2 = (file.SpatialAxis)
        axisEqD2.X0 = 55
        axisEqD2.Dx = 905
        axisEqD2.Y0 = -55
        axisEqD2.Dy = 915

        file.Close()
Ejemplo n.º 6
0
def test_read_itemtimestep():

    dfs = DfsFileFactory.DfsGenericOpen("testdata/TemporalEqCal.dfs0")
    dfs.Reset()

    for _ in range(2 * 5):
        data = dfs.ReadItemTimeStepNext()

    dfs.Close()

    assert data.Data[0] == 104
Ejemplo n.º 7
0
    def ModifyDfs2ItemInfo(filename):

        # Open the file for editing
        file = DfsFileFactory.Dfs2FileOpenEdit(filename)

        # Original name is "Landuse" (7 characters), "GroundUse" is truncated to "GroundU"
        file.ItemInfo[0].Name = "GroundUse"
        # Provide a new quantity (updating the item and unit of the quantity directly does not work!)
        file.ItemInfo[0].Quantity = eumQuantity(eumItem.eumIAreaFraction,
                                                eumUnit.eumUPerCent)

        # done
        file.Close()
Ejemplo n.º 8
0
    def test_dfsu(self):
        dfsFile = DfsFileFactory.DfsGenericOpen("testdata/OresundHD.dfsu")

        assert_equal(1, len(dfsFile.FileInfo.CustomBlocks))

        customBlock = dfsFile.FileInfo.CustomBlocks[0]
        assert_equal(DfsSimpleType.Int, customBlock.SimpleType)
        assert_equal("MIKE_FM", customBlock.Name)
        assert_equal(5, len(customBlock.Values))
        assert_equal(2057, customBlock.Values[0])
        assert_equal(3636, customBlock.Values[1])
        assert_equal(2, customBlock.Values[2])
        assert_equal(0, customBlock.Values[3])
        assert_equal(0, customBlock.Values[4])

        dfsFile.Close()
Ejemplo n.º 9
0
    def ModifyDfs2FileData(filename):

        # Open the file for editing
        file = DfsFileFactory.Dfs2FileOpenEdit(filename)

        # Load and modify data from the first item and timestep
        data2D = file.ReadItemTimeStepNext(reshape=True)
        data2D.Data[21, 61] = 7
        data2D.Data[21, 62] = 6
        data2D.Data[21, 63] = 5
        data2D.Data[21, 64] = 4
        data2D.Data[21, 65] = 3

        # Write modified data back
        file.WriteItemTimeStep(1, 0, data2D.Time, data2D.Data)

        # done
        file.Close()
Ejemplo n.º 10
0
    def ReadingDfs2File(filename):

        # Open the file as a dfs2 file
        dfs2File = DfsFileFactory.Dfs2FileOpen(filename)
        dfs2File.Reshape(True)

        # Spatial axis for this file is a 2D equidistant axis
        axisEqD2 = dfs2File.SpatialAxis
        dx = axisEqD2.Dx
        # 900
        dy = axisEqD2.Dy
        # 900

        # Header information is contained in the IDfsFileInfo
        fileInfo = dfs2File.FileInfo
        steps = fileInfo.TimeAxis.NumberOfTimeSteps
        # 13
        projectionString = fileInfo.Projection.WKTString
        # "UTM-33"

        # Information on each of the dynamic items, here the first one
        dynamicItemInfo = dfs2File.ItemInfo[0]
        nameOfFirstDynamicItem = dynamicItemInfo.Name
        # "H Water Depth m"
        typeOfFirstDynamicItem = dynamicItemInfo.DataType
        # Float

        # Read data of first item, third time step (items start by 1, timesteps by 0),
        # assuming data is of type float.
        data2D = dfs2File.ReadItemTimeStep(1, 2)
        # Get the value at (i,j) = (3,4) of the item and timestep
        value = data2D.Data[3, 4]
        # 11.3634329
        print("ReadingDfs2File: data2D.Data[3, 4] = {}".format(value))

        # This iterates through all the timesteps and items in the file
        # For performance reasons it is important to iterate over time steps
        # first and items second.
        for i in range(steps):
            for j in range(1, len(dfs2File.ItemInfo)):
                data2D = dfs2File.ReadItemTimeStep(j, i)
                value = data2D.Data[3, 4]
Ejemplo n.º 11
0
    def ModifyDfs2Bathymetry(bathyFilename):

        # Open file
        dfs2 = DfsFileFactory.Dfs2FileOpenEdit(bathyFilename)

        # Second custom block (index 1) contains the M21_MISC values,
        # where the 4th (index 3) is the land value
        landValue = dfs2.FileInfo.CustomBlocks[1][3]

        # Read bathymetry data
        bathyData = dfs2.ReadItemTimeStepNext()

        # Modify bathymetry data
        for i in range(bathyData.Data.size):
            if (bathyData.Data[i] != landValue):
                bathyData.Data[i] -= 5.61

        # Write back bathymetry data
        dfs2.WriteItemTimeStep(1, 0, 0, bathyData.Data)
        dfs2.Close()
Ejemplo n.º 12
0
    def test_dfs2(self):
        dfsFile = DfsFileFactory.DfsGenericOpen("testdata/OresundHD.dfs2")

        assert_equal(1, len(dfsFile.FileInfo.CustomBlocks))

        customBlock = dfsFile.FileInfo.CustomBlocks[0]
        assert_equal("M21_Misc", customBlock.Name)
        assert_equal(DfsSimpleType.Float, customBlock.SimpleType)
        assert_equal(7, len(customBlock.Values))
        assert_equal(327, customBlock.Values[0]
                     )  # Orientation - matching that in the projection info
        assert_allclose(0.2, customBlock.Values[1])  # Drying depth
        assert_equal(-900, customBlock.Values[2]
                     )  # -900 = contains geographic information (projection)
        assert_equal(10, customBlock.Values[3])
        # Land value
        assert_equal(0, customBlock.Values[4])
        assert_equal(0, customBlock.Values[5])
        assert_equal(0, customBlock.Values[6])

        dfsFile.Close()
Ejemplo n.º 13
0
    def CreateDfs2File(sourceFilename, filename):

        source = DfsFileFactory.Dfs2FileOpen(sourceFilename)

        factory = DfsFactory()
        builder = DfsBuilder.Create(
            "", r"C:\Program Files\DHI\2010\bin\nmodel.exe", 0)

        # Set up the header
        builder.SetDataType(1)
        builder.SetGeographicalProjection(
            factory.CreateProjectionGeoOrigin("UTM-33", 12.438741600559766,
                                              55.225707842436385,
                                              326.99999999999955))
        builder.SetTemporalAxis(
            factory.CreateTemporalEqCalendarAxis(
                eumUnit.eumUsec, datetime.datetime(1993, 12, 2, 0, 0, 0), 0,
                86400))
        builder.SetSpatialAxis(
            factory.CreateAxisEqD2(eumUnit.eumUmeter, 71, 0, 900, 91, 0, 900))
        builder.DeleteValueFloat = -1e-30

        # Add custom block
        # M21_Misc : {orientation (should match projection), drying depth, -900=has projection, land value, 0, 0, 0}
        builder.AddCustomBlock(
            factory.CreateCustomBlock(
                "M21_Misc", np.array([327, 0.2, -900, 10, 0, 0, 0],
                                     np.float32)))

        # Set up dynamic items
        builder.AddCreateDynamicItem(
            "H Water Depth m",
            eumQuantity.Create(eumItem.eumIWaterLevel, eumUnit.eumUmeter),
            DfsSimpleType.Float, DataValueType.Instantaneous)
        builder.AddCreateDynamicItem(
            "P Flux m^3/s/m",
            eumQuantity.Create(eumItem.eumIFlowFlux, eumUnit.eumUm3PerSecPerM),
            DfsSimpleType.Float, DataValueType.Instantaneous)
        builder.AddCreateDynamicItem(
            "Q Flux m^3/s/m",
            eumQuantity.Create(eumItem.eumIFlowFlux, eumUnit.eumUm3PerSecPerM),
            DfsSimpleType.Float, DataValueType.Instantaneous)

        # Create file
        builder.CreateFile(filename)

        # Add static items containing bathymetri data, use data from source
        sourceStaticItem = source.ReadStaticItemNext()
        builder.AddCreateStaticItem("Static item", eumQuantity.UnDefined(),
                                    sourceStaticItem.Data)

        # Get the file
        file = builder.GetFile()

        # Loop over all time steps
        for i in range(source.FileInfo.TimeAxis.NumberOfTimeSteps):
            # Loop over all items
            for j in range(len(source.ItemInfo)):
                # Add data for all item-timesteps, copying data from source file.

                # Read data from source file
                sourceData = source.ReadItemTimeStepNext(reshape=True)

                # Create empty item data, and copy over data from source
                # The IDfsItemData2D can handle 2D indexing, on the form data2D[k,l].
                # An ordinary array, float[], can also be used, though indexing from 2D to 1D must be
                # handled by user code i.e. using data1D[k + l*xCount] compared to data2D[k,l]
                itemData2D = file.CreateEmptyItemData(j + 1, reshape=True)
                for k in range(71):
                    for l in range(91):
                        itemData2D.Data[k, l] = sourceData.Data[k, l]

                # the itemData2D.Data is a float[], so any float[] of the correct size is valid here.
                file.WriteItemTimeStep(j + 1, i, sourceData.Time,
                                       itemData2D.Data)

        source.Close()
        file.Close()
Ejemplo n.º 14
0
def test_timeaxis():
    dfs = DfsFileFactory.DfsGenericOpen("testdata/TemporalEqCal.dfs0")
    timeaxistype = dfs.FileInfo.TimeAxis.TimeAxisType
    dfs.Close()

    assert timeaxistype == TimeAxisType.CalendarEquidistant
Ejemplo n.º 15
0
    def test_static_item(self):
        dfsFile = DfsFileFactory.DfsGenericOpen("testdata/OresundHD.dfsu")

        staticItems = []
        staticItemNumber = 1
        while (True):
            varstaticItem = dfsFile.ReadStaticItem(staticItemNumber)
            if (varstaticItem is None):
                break
            staticItems.append(varstaticItem)
            staticItemNumber += 1

        assert staticItems != None
        assert 9 == len(staticItems)

        # Check x-coord static item
        staticItem = staticItems[1]
        assert 2 == staticItem.ItemNumber
        assert 2057 == staticItem.ElementCount
        #assert 2057 == staticItem.UsedElementCount
        assert "X-coord" == staticItem.Name

        if (eumItem.eumIItemUndefined == staticItem.Quantity.Item):
            assert eumUnit.eumUUnitUndefined == staticItem.Quantity.Unit
        elif (eumItem.eumIGeographicalCoordinate == staticItem.Quantity.Item):
            assert eumUnit.eumUmeter == staticItem.Quantity.Unit
        else:
            assert "X coordinate axis item type mismatch" == ""

        if (staticItem.DataType != DfsSimpleType.Double
                and staticItem.DataType != DfsSimpleType.Float):
            assert "DataType of X static item mismatch" == ""

        #assert deleteValueFloat == staticItem.ReferenceCoordinateX
        #assert deleteValueFloat == staticItem.ReferenceCoordinateY
        #assert deleteValueFloat == staticItem.ReferenceCoordinateZ
        #assert deleteValueFloat == staticItem.OrientationAlpha
        #assert deleteValueFloat == staticItem.OrientationPhi
        #assert deleteValueFloat == staticItem.OrientationTheta

        # Check dummy spatial axis
        axis = staticItem.SpatialAxis
        assert SpaceAxisType.EqD1 == axis.AxisType
        assert 1 == axis.Dimension
        assert 2057 == axis.XCount
        assert 0 == axis.X0
        assert 1 == axis.Dx

        # Check data - first and last coordinate
        assert_allclose(359978.8, staticItem.Data[0])
        assert_allclose(338109.5, staticItem.Data[2056])

        #--------------------------------------
        # Check element type static item
        staticItem = staticItems[6]
        assert 7 == staticItem.ItemNumber
        assert 3636 == staticItem.ElementCount
        #assert 3636 == staticItem.UsedElementCount

        if (eumItem.eumIItemUndefined == staticItem.Quantity.Item):
            assert eumUnit.eumUUnitUndefined == staticItem.Quantity.Unit
        elif (eumItem.eumIIntegerCode == staticItem.Quantity.Item):
            assert eumUnit.eumUintCode == staticItem.Quantity.Unit
        else:
            raise Exception("Element Type coordinate axis item type mismatch")

        assert DfsSimpleType.Int == staticItem.DataType
        assert "Element type" == staticItem.Name

        # Check dummy spatial axis
        axis = staticItem.SpatialAxis
        assert SpaceAxisType.EqD1 == axis.AxisType
        assert 3636, axis.XCount

        # Check data - first and last elements
        assert 21 == staticItem.Data[0]
        assert 21 == staticItem.Data[3635]

        #--------------------------------------
        # Check connectivity static item
        staticItem = staticItems[8]
        assert 9 == staticItem.ItemNumber
        assert 3 * 3636 == staticItem.ElementCount
        #assert 3 * 3636 == staticItem.UsedElementCount

        if (eumItem.eumIItemUndefined == staticItem.Quantity.Item):
            assert eumUnit.eumUUnitUndefined == staticItem.Quantity.Unit
        elif (eumItem.eumIIntegerCode == staticItem.Quantity.Item):
            assert eumUnit.eumUintCode == staticItem.Quantity.Unit
        else:
            raise Exception("Connectivity coordinate axis item type mismatch")

        assert DfsSimpleType.Int == staticItem.DataType
        assert "Connectivity" == staticItem.Name

        #assert deleteValueFloat == staticItem.ReferenceCoordinateX
        #assert deleteValueFloat == staticItem.ReferenceCoordinateY
        #assert deleteValueFloat == staticItem.ReferenceCoordinateZ
        #assert deleteValueFloat == staticItem.OrientationAlpha
        #assert deleteValueFloat == staticItem.OrientationPhi
        #assert deleteValueFloat == staticItem.OrientationTheta

        # Check dummy spatial axis
        axis = staticItem.SpatialAxis
        assert SpaceAxisType.EqD1 == axis.AxisType
        assert 1 == axis.Dimension
        # TODO: Assert.AreEqual(eumUnit.eumUmeter, axis.AxisUnit
        assert 3 * 3636 == axis.XCount
        assert 0 == axis.X0
        assert 1 == axis.Dx

        # Check data - first and last element
        assert 1 == staticItem.Data[0]
        assert 2 == staticItem.Data[1]
        assert 3 == staticItem.Data[2]
        assert 1698 == staticItem.Data[10905]
        assert 1697 == staticItem.Data[10906]
        assert 2056 == staticItem.Data[10907]