Skip to main content

SifIO is a Python package for reading and writing access of SIF data types to Sesam Interface Files.

Project description

OneWorkflow

SifIO is a Python package for reading and writing access of SIF data types to Sesam Interface Files.

Installation

# QuantConnect for intellisense for the .NET libraries
pip install quantconnect-stubs==16345

# .NET Runtime
pip install dnv-net-runtime

# IntelliSense support for the `with` statement for dotnet/runtime assemblies from Python
pip dnv-net-intellisense & update-stubs

# SifIO for reading and writing SIF
pip dnv-sifio

Usage/Examples

The provided code snippets serve as practical examples of utilizing the dnv.sifio Python package to manipulate data in SESAM System Format (SIF) and SIN files. These examples underscore the versatility of SifIO, demonstrating a variety of operations such as:

  • Reading the number of occurrences of a data type and the size of the established pointer table for a datatype.
  • Reading all data for a given data type name and id.
  • Reading text data.
  • Copying a FEM file to a SIN file, then exporting to a SIF file.
  • Copying a SIN file to a new SIN file, then exporting to a SIF file.
  • Creating a SIN file, writing data to tabs, exporting to SIF format, and verifying the creation.
  • Adding new sets to a .SIN file and verifying the output file for the correct number of sets.
# Ensure that 'import dnv.net.runtime' is executed first in your script
import dnv.net.runtime

import os
from pathlib import Path

from System import Guid
from System.Collections.Generic import List

from dnv.sesam.sifapi.core import ISifData, ISifDataReader, ISifDataWriter
from dnv.sesam.sifapi.io import SesamDataFactory


def sif_io_get_count_and_tab_dimensions():
    """
    Reads the number of occurrences of a data type and the size of the established pointer table
    for a datatype.
    """

    reader: ISifDataReader
    with SesamDataFactory.CreateReader("T1.FEM") as reader:
        reader.CreateModel()

        tab_dim = reader.GetTabDimensions("BEUSLO")
        assert [element for element in tab_dim] == [5, 80]

        count = reader.GetCount("GBEAMG")
        assert count == 8

        count = reader.GetCount("GELTH")
        assert count == 1


def sif_io_read_ext():
    """Reads all data for a given data type name and id."""

    reader: ISifDataReader
    with SesamDataFactory.CreateReader("T1.FEM") as reader:
        reader.CreateModel()

        # Read all data of type BEUSLO for id={5,4}
        data = reader.ReadExt("BEUSLO", [5, 4])
        assert data[0] == 2
        assert data[1] == 17
        assert data[18] == 17


def sif_io_read_text():
    """Reads text data."""

    reader: ISifDataReader
    with SesamDataFactory.CreateReader("T1.FEM") as reader:
        reader.CreateModel()

        # Read name of a section
        data: List[ISifData] = reader.ReadText("TDSECT", [7])
        assert data.Count == 1

        sif_data: ISifData = data[0]
        assert sif_data.TextList.Count == 1
        assert sif_data.TextList[0] == "Pipe12"


def sif_io_read_all():
    """Reads all data for a given data type name and id."""

    reader: ISifDataReader
    with SesamDataFactory.CreateReader("T1.FEM") as reader:
        reader.CreateModel()

        # Must be called prior to ReadAll for the type
        reader.SetFirstTimeReadAll("GELMNT1")

        # Read all data of type GELMNT1 in blocks of 10
        data_list = List[ISifData]()
        data = reader.ReadAll("GELMNT1", 10)
        data_list.AddRange(data)

        # Read all data of type MORSMEL in blocks of 10
        data = reader.ReadAll("MORSMEL", 10)
        data_list.AddRange(data)

        assert data_list.Count == 235


def sif_io_read_data():
    """Reads all data for a given data type name and id."""

    reader: ISifDataReader
    with SesamDataFactory.CreateReader("T1.FEM") as reader:
        reader.CreateModel()

        # Read the GELMNT1 data with id 45
        data: List[ISifData] = reader.ReadData("GELMNT1", [45])
        assert data.Count == 1

        sif_data: ISifData = data[0].Data
        assert [element for element in sif_data] == [13, 45, 45, 28, 0, 45, 51, 42, 74, 72, 141, 75, 77]


def copy_fem_to_sin_to_sif():
    """Copies a FEM file to a SIN file, then exports to a SIF file."""

    with SesamDataFactory.CreateReader(test_fem_filepath) as reader:
        diagnostic = reader.CreateModel()
        assert diagnostic == 0, "Diagnostic failed"

        number_of_nodes = reader.GetCount("GNODE")
        number_of_elements = reader.GetCount("GELMNT")

        random_file_name_sin = str(Guid.NewGuid()) + ".SIN"
        with SesamDataFactory.CreateWriter(random_file_name_sin) as writer:
            random_file_name_sif = str(Guid.NewGuid()) + ".SIF"
            writer.WriteSifFile(reader, random_file_name_sif)
            assert os.path.exists(random_file_name_sif), "SIF file does not exist"

            with SesamDataFactory.CreateReader(random_file_name_sif) as new_reader:
                assert new_reader.CreateModel() == 0, "Model creation failed"
                assert number_of_nodes == new_reader.GetCount(
                    "GNODE"
                ), "Node count mismatch"
                assert number_of_elements == new_reader.GetCount(
                    "GELMNT"
                ), "Element count mismatch"
            os.remove(random_file_name_sif)

        os.remove(random_file_name_sin)


def copy_sin_to_sif():
    """Copies a SIN file to a new SIN file, then exports to a SIF file."""

    reader: ISifDataReader
    writer: ISifDataWriter
    random_file_name_sin = str(Guid.NewGuid()) + ".SIN"
    with SesamDataFactory.CreateReader("R1.SIN") as reader, \
         SesamDataFactory.CreateWriter(random_file_name_sin) as writer:
        diagnostic = reader.CreateModel()
        assert diagnostic == 0, "Diagnostic failed"

        random_file_name_sif = str(Guid.NewGuid()) + ".SIF"
        writer.WriteSifFile(random_file_name_sif)

        assert os.path.exists(random_file_name_sif), "SIF file does not exist"
        os.remove(random_file_name_sif)

    os.remove(random_file_name_sin)


def sif_io_create_tab_write_write_sif_file():
    """Creates a SIN file, writes data to tabs, exports to SIF format, and verifies the creation."""

    writer: ISifDataWriter
    sin_file = Guid.NewGuid().ToString() + ".SIN"  # PythonNet
    with SesamDataFactory.CreateWriter(sin_file) as writer:
        writer.CreateTab("HIERARCH", [1], 1)
        writer.Write("HIERARCH", [8, 1, 1, 1, 1, 0, 0, 0])

        writer.CreateTab("IDENT", [1], 1)
        writer.Write("IDENT", [1, 1, 3])

        # The maximum id is 99, but only 5 data sets are written
        writer.CreateTab("GBEAMG", [99], 5)

        # Note that the first entry is always the length of the written data array,
        # also for datatypes which do not have the length of the data as the first
        # entry according to the SIF datatype definition
        writer.Write("GBEAMG", [17, 1, 0, 8.79331768e-01, 2.12802696e00, 1.06401348e00, 1.06401348e00,
                            0, 1.33001685e00, 6.65008426e-01, 6.65008426e-01, 4.39911306e-01,
                            4.39911306e-01, 0, 0, 4.35366005e-01, 4.35366005e-01])

        writer.Write("GBEAMG", [17, 2, 0, 8.79331768e-01, 2.12802696e00, 1.06401348e00, 1.06401348e00,
                            0, 1.33001685e00, 6.65008426e-01, 6.65008426e-01, 4.39911306e-01,
                            4.39911306e-01, 0, 0, 4.35366005e-01, 4.35366005e-01])

        writer.Write("GBEAMG", [17, 3, 0, 8.79331768e-01, 2.12802696e00, 1.06401348e00, 1.06401348e00,
                            0, 1.33001685e00, 6.65008426e-01, 6.65008426e-01, 4.39911306e-01,
                            4.39911306e-01, 0, 0, 4.35366005e-01, 4.35366005e-01])

        writer.Write("GBEAMG", [17, 98, 0, 8.79331768e-01, 2.12802696e00, 1.06401348e00, 1.06401348e00,
                            0, 1.33001685e00, 6.65008426e-01, 6.65008426e-01, 4.39911306e-01,
                            4.39911306e-01, 0, 0, 4.35366005e-01, 4.35366005e-01])

        writer.Write("GBEAMG", [17, 99, 0, 8.79331768e-01, 2.12802696e00, 1.06401348e00, 1.06401348e00,
                            0, 1.33001685e00, 6.65008426e-01, 6.65008426e-01, 4.39911306e-01,
                            4.39911306e-01, 0, 0, 4.35366005e-01, 4.35366005e-01])

        random_filenames_sif = Guid.NewGuid().ToString() + ".SIF"
        writer.WriteSifFile(random_filenames_sif)


def add_sets_to_sin_file():
    """Adds new sets to a .SIN file, verifies the output file for the correct number of sets."""

    input_file, output_file = "FrameWithOneSetR1.SIN", "FrameWithThreeSetsR1.sin"
    sets_to_add, total_sets = 2, 0

    def create_set_data(set_number: int, number_of_elements: int, start_element_id: int) -> list[float]:
        INDEX, ISTYPE, ISORIG = 1, 2, 0
        initial_data = [number_of_elements + 4, set_number, 0.0, INDEX, ISTYPE, ISORIG]
        element_ids = list(range(start_element_id, start_element_id + number_of_elements))
        set_data = initial_data + element_ids
        return set_data

    sif_reader: ISifDataReader
    sif_writer: ISifDataWriter
    with SesamDataFactory.CreateReader(input_file) as sif_reader, \
        SesamDataFactory.CreateWriter(output_file) as sif_writer:
        diagnostic = sif_reader.CreateModel()
        assert diagnostic == 0

        for data_type in sif_reader.GetToc():
            if data_type.Key in ["GSETMEMB", "TDSETNAM"]:
                total_sets = data_type.Value.AllIndices[0] + sets_to_add
                data_type.Value.AllIndices[0] = total_sets
                sif_writer.CreateTab(data_type.Key,
                                    [item for item in data_type.Value.AllIndices],
                                    total_sets)
            else:
                sif_writer.CreateTab(data_type.Key,
                                    [item for item in data_type.Value.AllIndices],
                                    data_type.Value.Count)

        for data_type in sif_reader.GetToc():
            data = sif_reader.ReadAll(data_type.Key)
            sif_writer.WriteData(data_type.Key, data)

        set_info = [("SET2", 2, 27, 25), ("SET3", 3, 10, 53)]
        for set_name, set_id, num_elements, start_id in set_info:
            set_names = List[str]()
            set_names.Add(set_name)
            sif_writer.WriteText("TDSETNAM", set_id, set_names)
            sif_writer.Write("GSETMEMB", create_set_data(set_id, num_elements, start_id))

    with SesamDataFactory.CreateReader(output_file) as output_reader:
        assert output_reader.CreateModel() == 0
        for data_type in ["TDSETNAM", "GSETMEMB"]:
            assert output_reader.GetCount(data_type) == total_sets

    os.remove(output_file)

License

MIT

Support

If you encounter any issues, have questions, or want to provide feedback, please get in touch with our support team at software.support@dnv.com. We are committed to continuously improving SifIO Python package and providing timely assistance to our users.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

dnv_sifio-5.3.6-py3-none-any.whl (1.3 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page