SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
sdds.py File Reference

Detailed Description

Python script providing the SDDS Python module.

This module provides the SDDS class and associated methods to load, manipulate, and save SDDS files. It supports both ASCII and binary SDDS formats and provides functionality to read, write, and manipulate SDDS data.

Dependencies: sddsdata module

Definition in file sdds.py.

Go to the source code of this file.

Functions

SDDS sdds.load (input_file)
 Loads an SDDS file into the SDDS object.
 
SDDS sdds.loadSparse (input_file, interval, offset)
 Loads an SDDS file into the SDDS object with sparse data reading.
 
SDDS sdds.loadLastRows (input_file, lastrows)
 Loads an SDDS file into the SDDS object, reading only the last few rows.
 
 sdds.save (SDDS sdds_obj, output_file)
 Saves the SDDS object's data to an SDDS file.
 
 sdds.sdds_data_type_to_string (data_type_code)
 Converts a numeric SDDS data type code to its string representation.
 
 sdds.sdds_data_type_to_short_string (data_type_code)
 Converts a numeric SDDS data type code to its short string representation.
 
 sdds.sdds_short_string_to_data_type (data_type_code)
 Converts a numeric SDDS data type code to its short string representation.
 
 sdds.demo1 (output)
 Demonstrates how to save a demo SDDS file using the SDDS class.
 
 sdds.demo2 (output)
 Demonstrates how to save a demo SDDS file using the SDDS class with simplified definitions.
 
 sdds.demo3 (output)
 Demonstrates how to save a demo SDDS file using sddsdata commands directly.
 
 sdds.demo4 (output)
 Demonstrates how to save a demo SDDS file using sddsdata commands and writing one row at a time.
 
 sdds.demo5 (output)
 Demonstrates how to open an existing SDDS file and add rows to the last page without loading the whole file into memory.
 
 sdds.demo6 (output)
 Demonstrates how to open an existing SDDS file and add a new page.
 
SddsFile sdds.read (input_file)
 Mostly backward compatible with the PyLHC sdds module read() function.
 
 sdds.write (SddsFile sdds_file, output_file)
 Mostly backward compatible with the PyLHC sdds module write() function.
 

Function Documentation

◆ demo1()

sdds.demo1 ( output)

Demonstrates how to save a demo SDDS file using the SDDS class.

Args: output (str): The output SDDS filename to save the demo data.

This function creates an SDDS object, populates it with sample data, and saves it to the specified output file.

Definition at line 1494 of file sdds.py.

1494def demo1(output):
1495 """
1496 Demonstrates how to save a demo SDDS file using the SDDS class.
1497
1498 Args:
1499 output (str): The output SDDS filename to save the demo data.
1500
1501 This function creates an SDDS object, populates it with sample data, and saves it to the specified output file.
1502 """
1503 x = SDDS()
1504 x.description[0] = "text"
1505 x.description[1] = "contents"
1506 x.parameterName = ["ShortP", "LongP", "FloatP", "DoubleP", "StringP", "CharacterP"]
1507 x.parameterData = [[1, 6], [2, 7], [3.3, 8.8], [4.4, 9.8], ["five", "ten"], ["a", "b"]]
1508 x.parameterDefinition = [
1509 ["", "", "", "", x.SDDS_SHORT, ""],
1510 ["", "", "", "", x.SDDS_LONG, ""],
1511 ["", "", "", "", x.SDDS_FLOAT, ""],
1512 ["", "", "", "", x.SDDS_DOUBLE, ""],
1513 ["", "", "", "", x.SDDS_STRING, ""],
1514 ["", "", "", "", x.SDDS_CHARACTER, ""],
1515 ]
1516
1517 x.arrayName = ["ShortA", "LongA", "FloatA", "DoubleA", "StringA", "CharacterA"]
1518 x.arrayDefinition = [
1519 ["", "", "", "", "", x.SDDS_SHORT, 0, 1],
1520 ["", "", "", "", "", x.SDDS_LONG, 0, 1],
1521 ["", "", "", "", "", x.SDDS_FLOAT, 0, 2],
1522 ["", "", "", "", "", x.SDDS_DOUBLE, 0, 1],
1523 ["", "", "", "", "", x.SDDS_STRING, 0, 1],
1524 ["", "", "", "", "", x.SDDS_CHARACTER, 0, 1],
1525 ]
1526 x.arrayDimensions = [
1527 [[6], [8]],
1528 [[5], [7]],
1529 [[2, 3], [2, 4]],
1530 [[4], [5]],
1531 [[4], [5]],
1532 [[4], [5]],
1533 ]
1534 x.arrayData = [
1535 [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7, 8]],
1536 [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6, 7]],
1537 [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7, 8]],
1538 [[1, 2, 3, 4], [1, 2, 3, 4, 5]],
1539 [["one", "two", "three", "four"], ["five", "six", "seven", "eight", "nine"]],
1540 [["a", "b", "c", "d"], ["e", "f", "g", "h", "i"]],
1541 ]
1542
1543 x.columnName = ["ShortC", "LongC", "FloatC", "DoubleC", "StringC", "CharacterC"]
1544 x.columnData = [
1545 [[1, 2, 3], [-1, -2, -3, -4]],
1546 [[1, 2, 3], [-1, -2, -3, -4]],
1547 [[1, 2, 3], [-1, -2, -3.6, -4.4]],
1548 [[1, 2, 3], [-1, -2, -3.6, -4.4]],
1549 [["row 1", "row 2", "row 3"], ["row 1", "row 2", "row 3", "row 4"]],
1550 [["x", "y", "z"], ["i", "j", "k", "l"]],
1551 ]
1552 x.columnDefinition = [
1553 ["", "", "", "", x.SDDS_SHORT, 0],
1554 ["", "", "", "", x.SDDS_LONG, 0],
1555 ["", "", "", "", x.SDDS_FLOAT, 0],
1556 ["", "", "", "", x.SDDS_DOUBLE, 0],
1557 ["", "", "", "", x.SDDS_STRING, 0],
1558 ["", "", "", "", x.SDDS_CHARACTER, 0],
1559 ]
1560
1561 x.save(output)
1562 del x
1563
1564

◆ demo2()

sdds.demo2 ( output)

Demonstrates how to save a demo SDDS file using the SDDS class with simplified definitions.

Args: output (str): The output SDDS filename to save the demo data.

This function shows how to use simplified methods to define parameters, arrays, and columns.

Definition at line 1565 of file sdds.py.

1565def demo2(output):
1566 """
1567 Demonstrates how to save a demo SDDS file using the SDDS class with simplified definitions.
1568
1569 Args:
1570 output (str): The output SDDS filename to save the demo data.
1571
1572 This function shows how to use simplified methods to define parameters, arrays, and columns.
1573 """
1574 x = SDDS()
1575 x.setDescription("text", "contents")
1576 names = ["Short", "Long", "Float", "Double", "String", "Character"]
1577 types = [x.SDDS_SHORT, x.SDDS_LONG, x.SDDS_FLOAT, x.SDDS_DOUBLE, x.SDDS_STRING, x.SDDS_CHARACTER]
1578 for i in range(6):
1579 x.defineSimpleParameter(names[i] + "P", types[i])
1580 if types[i] == x.SDDS_FLOAT:
1581 x.defineSimpleArray(names[i] + "A", types[i], 2)
1582 else:
1583 x.defineSimpleArray(names[i] + "A", types[i], 1)
1584 x.defineSimpleColumn(names[i] + "C", types[i])
1585 parameterData = [[1, 6], [2, 7], [3.3, 8.8], [4.4, 9.8], ["five", "ten"], ["a", "b"]]
1586 for i in range(6):
1587 x.setParameterValueList(names[i] + "P", parameterData[i])
1588
1589 arrayDimensions = [
1590 [[6], [8]],
1591 [[5], [7]],
1592 [[2, 3], [2, 4]],
1593 [[4], [5]],
1594 [[4], [5]],
1595 [[4], [5]],
1596 ]
1597 arrayData = [
1598 [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7, 8]],
1599 [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6, 7]],
1600 [[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7, 8]],
1601 [[1, 2, 3, 4], [1, 2, 3, 4, 5]],
1602 [["one", "two", "three", "four"], ["five", "six", "seven", "eight", "nine"]],
1603 [["a", "b", "c", "d"], ["e", "f", "g", "h", "i"]],
1604 ]
1605 for i in range(6):
1606 x.setArrayValueLists(names[i] + "A", arrayData[i], arrayDimensions[i])
1607
1608 columnData = [
1609 [[1, 2, 3], [-1, -2, -3, -4]],
1610 [[1, 2, 3], [-1, -2, -3, -4]],
1611 [[1, 2, 3], [-1, -2, -3.6, -4.4]],
1612 [[1, 2, 3], [-1, -2, -3.6, -4.4]],
1613 [["row 1", "row 2", "row 3"], ["row 1", "row 2", "row 3", "row 4"]],
1614 [["x", "y", "z"], ["i", "j", "k", "l"]],
1615 ]
1616 for i in range(6):
1617 x.setColumnValueLists(names[i] + "C", columnData[i])
1618 x.save(output)
1619
1620

◆ demo3()

sdds.demo3 ( output)

Demonstrates how to save a demo SDDS file using sddsdata commands directly.

Args: output (str): The output SDDS filename to save the demo data.

This function shows how to use sddsdata module functions directly to create and save an SDDS file.

Definition at line 1621 of file sdds.py.

1621def demo3(output):
1622 """
1623 Demonstrates how to save a demo SDDS file using `sddsdata` commands directly.
1624
1625 Args:
1626 output (str): The output SDDS filename to save the demo data.
1627
1628 This function shows how to use `sddsdata` module functions directly to create and save an SDDS file.
1629 """
1630 x = SDDS()
1631
1632 try:
1633 # Open SDDS output file
1634 if sddsdata.InitializeOutput(x.index, x.SDDS_BINARY, 1, "", "", output) != 1:
1635 raise ValueError("Failed to initialize SDDS output.")
1636 # Setting column_major to true. Only use this if you are going to write whole columns and not one row at a time.
1637 sddsdata.SetColumnMajorOrder(x.index)
1638 # Define parameters
1639 if sddsdata.DefineSimpleParameter(x.index, "ParameterA", "mm", x.SDDS_DOUBLE) != 1:
1640 raise ValueError("Failed to define parameter.")
1641 # Define arrays
1642 if sddsdata.DefineSimpleArray(x.index, "ArrayA", "DegC", x.SDDS_DOUBLE, 1) != 1:
1643 raise ValueError("Failed to define array.")
1644 if sddsdata.DefineSimpleArray(x.index, "ArrayB", "DegC", x.SDDS_DOUBLE, 2) != 1:
1645 raise ValueError("Failed to define array.")
1646 # Define columns
1647 if sddsdata.DefineSimpleColumn(x.index, "ColumnA", "Volts", x.SDDS_DOUBLE) != 1:
1648 raise ValueError("Failed to define column.")
1649 if sddsdata.DefineSimpleColumn(x.index, "ColumnB", "Amps", x.SDDS_DOUBLE) != 1:
1650 raise ValueError("Failed to define column.")
1651 # Write SDDS header
1652 if sddsdata.WriteLayout(x.index) != 1:
1653 raise ValueError("Failed to write SDDS layout.")
1654 # Start SDDS page. Allocate 100 rows.
1655 if sddsdata.StartPage(x.index, 100) != 1:
1656 raise ValueError("Failed to start SDDS page.")
1657 # Set parameter values
1658 if sddsdata.SetParameter(x.index, "ParameterA", 1.1) != 1:
1659 raise ValueError("Failed to set parameter value.")
1660 # Set array values
1661 if sddsdata.SetArray(x.index, "ArrayA", [1, 2, 3], [3]) != 1:
1662 raise ValueError("Failed to set array value.")
1663 if sddsdata.SetArray(x.index, "ArrayB", [1, 2, 3, 4, 5, 6], [2, 3]) != 1:
1664 raise ValueError("Failed to set array value.")
1665 # Set column values
1666 if sddsdata.SetColumn(x.index, "ColumnA", [1, 2, 3]) != 1:
1667 raise ValueError("Failed to set column value.")
1668 if sddsdata.SetColumn(x.index, "ColumnB", [1, 2, 3]) != 1:
1669 raise ValueError("Failed to set column value.")
1670 # Write page to disk
1671 if sddsdata.WritePage(x.index) != 1:
1672 raise ValueError("Failed to write SDDS page.")
1673 # Close SDDS output file
1674 if sddsdata.Terminate(x.index) != 1:
1675 raise ValueError("Failed to terminate SDDS output.")
1676
1677 except:
1678 sddsdata.PrintErrors(x.SDDS_VERBOSE_PrintErrors)
1679 raise
1680
1681

◆ demo4()

sdds.demo4 ( output)

Demonstrates how to save a demo SDDS file using sddsdata commands and writing one row at a time.

Args: output (str): The output SDDS filename to save the demo data.

This function shows how to write data to an SDDS file one row at a time, useful for logging applications.

Definition at line 1682 of file sdds.py.

1682def demo4(output):
1683 """
1684 Demonstrates how to save a demo SDDS file using `sddsdata` commands and writing one row at a time.
1685
1686 Args:
1687 output (str): The output SDDS filename to save the demo data.
1688
1689 This function shows how to write data to an SDDS file one row at a time, useful for logging applications.
1690 """
1691 x = SDDS()
1692
1693 try:
1694 # Open SDDS output file
1695 if sddsdata.InitializeOutput(x.index, x.SDDS_BINARY, 1, "", "", output) != 1:
1696 raise ValueError("Failed to initialize SDDS output.")
1697 # Turning on fsync mode and fixed rows count mode. These are useful for loggers.
1698 sddsdata.EnableFSync(x.index)
1699 sddsdata.SetFixedRowCountMode(x.index)
1700 # Define parameters
1701 if sddsdata.DefineSimpleParameter(x.index, "ParameterA", "mm", x.SDDS_DOUBLE) != 1:
1702 raise ValueError("Failed to define parameter.")
1703 # Define arrays
1704 if sddsdata.DefineSimpleArray(x.index, "ArrayA", "DegC", x.SDDS_DOUBLE, 1) != 1:
1705 raise ValueError("Failed to define array.")
1706 if sddsdata.DefineSimpleArray(x.index, "ArrayB", "DegC", x.SDDS_DOUBLE, 2) != 1:
1707 raise ValueError("Failed to define array.")
1708 # Define columns
1709 if sddsdata.DefineSimpleColumn(x.index, "ColumnA", "Volts", x.SDDS_DOUBLE) != 1:
1710 raise ValueError("Failed to define column.")
1711 if sddsdata.DefineSimpleColumn(x.index, "ColumnB", "Amps", x.SDDS_DOUBLE) != 1:
1712 raise ValueError("Failed to define column.")
1713 # Write SDDS header
1714 if sddsdata.WriteLayout(x.index) != 1:
1715 raise ValueError("Failed to write SDDS layout.")
1716 # Start SDDS page, allocate 2 rows.
1717 if sddsdata.StartPage(x.index, 2) != 1:
1718 raise ValueError("Failed to start SDDS page.")
1719 # Set parameter values
1720 if sddsdata.SetParameter(x.index, "ParameterA", 1.1) != 1:
1721 raise ValueError("Failed to set parameter value.")
1722 # Set array values
1723 if sddsdata.SetArray(x.index, "ArrayA", [1, 2, 3], [3]) != 1:
1724 raise ValueError("Failed to set array value.")
1725 if sddsdata.SetArray(x.index, "ArrayB", [1, 2, 3, 4, 5, 6], [2, 3]) != 1:
1726 raise ValueError("Failed to set array value.")
1727 # Set all columns, one row at a time
1728 if sddsdata.SetRowValues(x.index, 0, ["ColumnA", 1, "ColumnB", 1]) != 1:
1729 raise ValueError("Failed to set row values.")
1730 if sddsdata.SetRowValues(x.index, 1, ["ColumnA", 2, "ColumnB", 2]) != 1:
1731 raise ValueError("Failed to set row values.")
1732 # Update page because we reached the row allocation limit set in the StartPage command
1733 if sddsdata.UpdatePage(x.index, x.SDDS_FLUSH_TABLE) != 1:
1734 raise ValueError("Failed to update SDDS page.")
1735 # Set more rows
1736 if sddsdata.SetRowValues(x.index, 2, ["ColumnA", 3, "ColumnB", 3]) != 1:
1737 raise ValueError("Failed to set row values.")
1738 # Update page
1739 if sddsdata.UpdatePage(x.index, x.SDDS_FLUSH_TABLE) != 1:
1740 raise ValueError("Failed to update SDDS page.")
1741 # Close SDDS output file
1742 if sddsdata.Terminate(x.index) != 1:
1743 raise ValueError("Failed to terminate SDDS output.")
1744
1745 except:
1746 sddsdata.PrintErrors(x.SDDS_VERBOSE_PrintErrors)
1747 raise
1748
1749

◆ demo5()

sdds.demo5 ( output)

Demonstrates how to open an existing SDDS file and add rows to the last page without loading the whole file into memory.

Args: output (str): The output SDDS filename to append data.

This function shows how to append data to an existing SDDS file efficiently.

Definition at line 1750 of file sdds.py.

1750def demo5(output):
1751 """
1752 Demonstrates how to open an existing SDDS file and add rows to the last page without loading the whole file into memory.
1753
1754 Args:
1755 output (str): The output SDDS filename to append data.
1756
1757 This function shows how to append data to an existing SDDS file efficiently.
1758 """
1759 x = SDDS()
1760
1761 try:
1762 # Open SDDS output file
1763 rows = sddsdata.InitializeAppendToPage(x.index, output, 100)
1764 if rows == 0:
1765 raise ValueError("Failed to initialize appending to SDDS page.")
1766 # Set all columns, one row at a time
1767 if sddsdata.SetRowValues(x.index, rows, ["ColumnA", 4, "ColumnB", 4]) != 1:
1768 raise ValueError("Failed to set row values.")
1769 if sddsdata.SetRowValues(x.index, rows + 1, ["ColumnA", 5, "ColumnB", 5]) != 1:
1770 raise ValueError("Failed to set row values.")
1771 if sddsdata.SetRowValues(x.index, rows + 2, ["ColumnA", 6, "ColumnB", 6]) != 1:
1772 raise ValueError("Failed to set row values.")
1773 # Update page
1774 if sddsdata.UpdatePage(x.index, x.SDDS_FLUSH_TABLE) != 1:
1775 raise ValueError("Failed to update SDDS page.")
1776 # Close SDDS output file
1777 if sddsdata.Terminate(x.index) != 1:
1778 raise ValueError("Failed to terminate SDDS output.")
1779
1780 except:
1781 sddsdata.PrintErrors(x.SDDS_VERBOSE_PrintErrors)
1782 raise
1783
1784

◆ demo6()

sdds.demo6 ( output)

Demonstrates how to open an existing SDDS file and add a new page.

Args: output (str): The output SDDS filename to append data.

This function shows how to append a new page to an existing SDDS file.

Definition at line 1785 of file sdds.py.

1785def demo6(output):
1786 """
1787 Demonstrates how to open an existing SDDS file and add a new page.
1788
1789 Args:
1790 output (str): The output SDDS filename to append data.
1791
1792 This function shows how to append a new page to an existing SDDS file.
1793 """
1794 x = SDDS()
1795
1796 try:
1797 # Open SDDS output file
1798 if sddsdata.InitializeAppend(x.index, output) != 1:
1799 raise ValueError("Failed to initialize appending to SDDS file.")
1800 # Allocate rows
1801 if sddsdata.StartPage(x.index, 100) != 1:
1802 raise ValueError("Failed to start SDDS page.")
1803 # Set parameter values
1804 if sddsdata.SetParameter(x.index, "ParameterA", 1.1) != 1:
1805 raise ValueError("Failed to set parameter value.")
1806 # Set array values
1807 if sddsdata.SetArray(x.index, "ArrayA", [1, 2, 3], [3]) != 1:
1808 raise ValueError("Failed to set array value.")
1809 if sddsdata.SetArray(x.index, "ArrayB", [1, 2, 3, 4, 5, 6], [2, 3]) != 1:
1810 raise ValueError("Failed to set array value.")
1811 # Set all columns, one row at a time
1812 if sddsdata.SetRowValues(x.index, 0, ["ColumnA", 7, "ColumnB", 7]) != 1:
1813 raise ValueError("Failed to set row values.")
1814 if sddsdata.SetRowValues(x.index, 1, ["ColumnA", 8, "ColumnB", 8]) != 1:
1815 raise ValueError("Failed to set row values.")
1816 if sddsdata.SetRowValues(x.index, 2, ["ColumnA", 9, "ColumnB", 9]) != 1:
1817 raise ValueError("Failed to set row values.")
1818 # Write page
1819 if sddsdata.WritePage(x.index) != 1:
1820 raise ValueError("Failed to write SDDS page.")
1821 # Close SDDS output file
1822 if sddsdata.Terminate(x.index) != 1:
1823 raise ValueError("Failed to terminate SDDS output.")
1824
1825 except:
1826 sddsdata.PrintErrors(x.SDDS_VERBOSE_PrintErrors)
1827 raise
1828
1829@dataclass

◆ load()

SDDS sdds.load ( input_file)

Loads an SDDS file into the SDDS object.

Args: input_file (str): The input SDDS filename to load.

Raises: Exception: If unable to read the SDDS data.

This method reads the SDDS file specified by input, and populates the object's data structures with the parameters, arrays, columns, and their respective data.

Definition at line 1323 of file sdds.py.

1323def load(input_file) -> SDDS:
1324 """
1325 Loads an SDDS file into the SDDS object.
1326
1327 Args:
1328 input_file (str): The input SDDS filename to load.
1329
1330 Raises:
1331 Exception: If unable to read the SDDS data.
1332
1333 This method reads the SDDS file specified by `input`, and populates the object's data structures
1334 with the parameters, arrays, columns, and their respective data.
1335 """
1336
1337 # Initialize an SDDS object
1338 sdds_obj = SDDS()
1339
1340 # Load the file
1341 try:
1342 sdds_obj.load(input_file)
1343 except Exception as e:
1344 raise ValueError(f"Failed to load the SDDS file: {e}")
1345
1346 return sdds_obj
1347

◆ loadLastRows()

SDDS sdds.loadLastRows ( input_file,
lastrows )

Loads an SDDS file into the SDDS object, reading only the last few rows.

Args: input_file (str): The input SDDS filename to load. lastrows (int): Number of last rows to read.

Raises: Exception: If unable to read the SDDS data.

This method reads only the last lastrows rows from each page of the SDDS file.

Definition at line 1373 of file sdds.py.

1373def loadLastRows(input_file, lastrows) -> SDDS:
1374 """
1375 Loads an SDDS file into the SDDS object, reading only the last few rows.
1376
1377 Args:
1378 input_file (str): The input SDDS filename to load.
1379 lastrows (int): Number of last rows to read.
1380
1381 Raises:
1382 Exception: If unable to read the SDDS data.
1383
1384 This method reads only the last `lastrows` rows from each page of the SDDS file.
1385 """
1386 # Initialize an SDDS object
1387 sdds_obj = SDDS()
1388
1389 # Load the file
1390 try:
1391 sdds_obj.loadLastRows(input_file, lastrows)
1392 except Exception as e:
1393 raise ValueError(f"Failed to load the SDDS file: {e}")
1394
1395 return sdds_obj
1396

◆ loadSparse()

SDDS sdds.loadSparse ( input_file,
interval,
offset )

Loads an SDDS file into the SDDS object with sparse data reading.

Args: input_file (str): The input SDDS filename to load. interval (int): Interval between pages to read. offset (int): Offset to start reading from.

Raises: Exception: If unable to read the SDDS data.

This method reads every interval pages from the SDDS file, starting from the page at offset.

Definition at line 1348 of file sdds.py.

1348def loadSparse(input_file, interval, offset) -> SDDS:
1349 """
1350 Loads an SDDS file into the SDDS object with sparse data reading.
1351
1352 Args:
1353 input_file (str): The input SDDS filename to load.
1354 interval (int): Interval between pages to read.
1355 offset (int): Offset to start reading from.
1356
1357 Raises:
1358 Exception: If unable to read the SDDS data.
1359
1360 This method reads every `interval` pages from the SDDS file, starting from the page at `offset`.
1361 """
1362 # Initialize an SDDS object
1363 sdds_obj = SDDS()
1364
1365 # Load the file
1366 try:
1367 sdds_obj.loadSparse(input_file, interval, offset)
1368 except Exception as e:
1369 raise ValueError(f"Failed to load the SDDS file: {e}")
1370
1371 return sdds_obj
1372

◆ read()

SddsFile sdds.read ( input_file)

Mostly backward compatible with the PyLHC sdds module read() function.

Unlike the PyLHC version, this function reads all the SDDS pages and works with column data. The data is returned in a structured dictionary.

Args: input_file (str): The input SDDS file to be read.

Returns: dict: A dictionary with parameters, arrays, and columns data. Example structure: { pages = {integer}, binary = {boolean}, "definitions": {name: {"type:" string, "units": string, "description": string}, "values": {parameter_name: {[Page1Value, Page2Value, ...]}, {array_name: {[Page1List, Page2List, ...]}, {column_name: {[Page1List, Page2List, ...]}, "values": {array_name: {[page1DimList, Page2DimList, ...]} }

Definition at line 1870 of file sdds.py.

1870def read(input_file) -> SddsFile:
1871 """
1872 Mostly backward compatible with the PyLHC sdds module read() function.
1873 Unlike the PyLHC version, this function reads all the SDDS pages and works with column data.
1874 The data is returned in a structured dictionary.
1875
1876 Args:
1877 input_file (str): The input SDDS file to be read.
1878
1879 Returns:
1880 dict: A dictionary with parameters, arrays, and columns data.
1881 Example structure:
1882 {
1883 pages = {integer},
1884 binary = {boolean},
1885 "definitions": {name: {"type:" string, "units": string, "description": string},
1886 "values": {parameter_name: {[Page1Value, Page2Value, ...]},
1887 {array_name: {[Page1List, Page2List, ...]},
1888 {column_name: {[Page1List, Page2List, ...]},
1889 "values": {array_name: {[page1DimList, Page2DimList, ...]}
1890 }
1891 """
1892 # Initialize an SDDS object
1893 sdds_obj = SDDS()
1894
1895 # Load the file
1896 try:
1897 sdds_obj.load(input_file)
1898 except Exception as e:
1899 raise ValueError(f"Failed to load the SDDS file: {e}")
1900
1901 # Initialize the output dictionary
1902 sdds_data = SddsFile()
1903
1904 # Store the data mode (ascii or binary)
1905 if sdds_obj.mode == SDDS.SDDS_BINARY:
1906 sdds_data.binary = True
1907 else:
1908 sdds_data.binary = False
1909
1910 # Store description data
1911 text = sdds_obj.description[0] if sdds_obj.description[0] != "" else None
1912 contents = sdds_obj.description[1] if sdds_obj.description[1] != "" else None
1913 description_instance = Description(text=text, contents=contents)
1914 sdds_data.description = description_instance
1915
1916 # Store number of pages
1917 sdds_data.pages = sdds_obj.loaded_pages
1918
1919 # Store parameter data
1920 if sdds_obj.parameterName:
1921 for i, definition in enumerate(sdds_obj.parameterDefinition):
1922 name = sdds_obj.parameterName[i]
1923 symbol = definition[0] if definition[0] != '' else None
1924 units = definition[1] if definition[1] != '' else None
1925 description = definition[2] if definition[2] != '' else None
1926 formatString = definition[3] if definition[3] != '' else None
1927 datatype = sdds_data_type_to_short_string(definition[4])
1928 fixedValue = definition[5] if definition[5] != '' else None
1929 parameter_instance = Parameter(name=name, type=datatype, symbol=symbol, units=units, description=description, format_string=formatString, fixed_value=fixedValue)
1930 sdds_data.definitions.update({name: parameter_instance})
1931 sdds_data.values.update({name: sdds_obj.parameterData[i]})
1932
1933 # Store array data
1934 if sdds_obj.arrayName:
1935 for i, definition in enumerate(sdds_obj.arrayDefinition):
1936 name = sdds_obj.arrayName[i]
1937 symbol = definition[0] if definition[0] != '' else None
1938 units = definition[1] if definition[1] != '' else None
1939 description = definition[2] if definition[2] != '' else None
1940 formatString = definition[3] if definition[3] != '' else None
1941 group_name = definition[4] if definition[4] != '' else None
1942 datatype = sdds_data_type_to_short_string(definition[5])
1943 fieldLength = definition[6] if definition[6] != 0 else None
1944 dimensions = definition[7] if definition[7] != '' else None
1945 array_instance = Array(name=name, type=datatype, symbol=symbol, units=units, description=description, format_string=formatString, group_name=group_name, field_length=fieldLength, dimensions=dimensions)
1946 sdds_data.definitions.update({name: array_instance})
1947 sdds_data.values.update({name: sdds_obj.arrayData[i]})
1948 sdds_data.array_dims.update({name: sdds_obj.arrayDimensions[i]})
1949
1950 # Store column data
1951 if sdds_obj.columnName:
1952 for i, definition in enumerate(sdds_obj.columnDefinition):
1953 name = sdds_obj.columnName[i]
1954 symbol = definition[0] if definition[0] != '' else None
1955 units = definition[1] if definition[1] != '' else None
1956 description = definition[2] if definition[2] != '' else None
1957 formatString = definition[3] if definition[3] != '' else None
1958 datatype = sdds_data_type_to_short_string(definition[4])
1959 fieldLength = definition[5] if definition[5] != 0 else None
1960 column_instance = Column(name=name, type=datatype, symbol=symbol, units=units, description=description, format_string=formatString, field_length=fieldLength)
1961 sdds_data.definitions.update({name: column_instance})
1962 sdds_data.values.update({name: sdds_obj.columnData[i]})
1963
1964 # Return the dictionary
1965 return sdds_data
1966

◆ save()

sdds.save ( SDDS sdds_obj,
output_file )

Saves the SDDS object's data to an SDDS file.

Args: output (str): The output SDDS filename to save the data.

Raises: Exception: If unable to write the SDDS data.

This method writes the data stored in the SDDS object to the specified file, including the parameters, arrays, columns, and their data.

Definition at line 1397 of file sdds.py.

1397def save(sdds_obj : SDDS, output_file):
1398 """
1399 Saves the SDDS object's data to an SDDS file.
1400
1401 Args:
1402 output (str): The output SDDS filename to save the data.
1403
1404 Raises:
1405 Exception: If unable to write the SDDS data.
1406
1407 This method writes the data stored in the SDDS object to the specified file, including the
1408 parameters, arrays, columns, and their data.
1409 """
1410 # Save the file
1411 try:
1412 sdds_obj.save(output_file)
1413 except Exception as e:
1414 raise ValueError(f"Failed to load the SDDS file: {e}")
1415

◆ sdds_data_type_to_short_string()

sdds.sdds_data_type_to_short_string ( data_type_code)

Converts a numeric SDDS data type code to its short string representation.

Args: data_type_code (int): Numeric code of the SDDS data type.

Returns: str: String representation of the SDDS data type.

Definition at line 1442 of file sdds.py.

1442def sdds_data_type_to_short_string(data_type_code):
1443 """
1444 Converts a numeric SDDS data type code to its short string representation.
1445
1446 Args:
1447 data_type_code (int): Numeric code of the SDDS data type.
1448
1449 Returns:
1450 str: String representation of the SDDS data type.
1451 """
1452 # Mapping of SDDS numeric data type codes to string representations
1453 data_type_map = {
1454 1: "longdouble",
1455 2: "double",
1456 3: "float",
1457 4: "long64",
1458 5: "ulong64",
1459 6: "long",
1460 7: "ulong",
1461 8: "short",
1462 9: "ushort",
1463 10: "string",
1464 11: "character",
1465 }
1466 return data_type_map.get(data_type_code, "Unknown Data Type")
1467

◆ sdds_data_type_to_string()

sdds.sdds_data_type_to_string ( data_type_code)

Converts a numeric SDDS data type code to its string representation.

Args: data_type_code (int): Numeric code of the SDDS data type.

Returns: str: String representation of the SDDS data type.

Definition at line 1416 of file sdds.py.

1416def sdds_data_type_to_string(data_type_code):
1417 """
1418 Converts a numeric SDDS data type code to its string representation.
1419
1420 Args:
1421 data_type_code (int): Numeric code of the SDDS data type.
1422
1423 Returns:
1424 str: String representation of the SDDS data type.
1425 """
1426 # Mapping of SDDS numeric data type codes to string representations
1427 data_type_map = {
1428 1: "SDDS_LONGDOUBLE",
1429 2: "SDDS_DOUBLE",
1430 3: "SDDS_FLOAT",
1431 4: "SDDS_LONG64",
1432 5: "SDDS_ULONG64",
1433 6: "SDDS_LONG",
1434 7: "SDDS_ULONG",
1435 8: "SDDS_SHORT",
1436 9: "SDDS_USHORT",
1437 10: "SDDS_STRING",
1438 11: "SDDS_CHARACTER",
1439 }
1440 return data_type_map.get(data_type_code, "Unknown Data Type")
1441

◆ sdds_short_string_to_data_type()

sdds.sdds_short_string_to_data_type ( data_type_code)

Converts a numeric SDDS data type code to its short string representation.

Args: data_type_code (string): String representation of the SDDS data type.

Returns: int: Numeric code of the SDDS data type.

Definition at line 1468 of file sdds.py.

1468def sdds_short_string_to_data_type(data_type_code):
1469 """
1470 Converts a numeric SDDS data type code to its short string representation.
1471
1472 Args:
1473 data_type_code (string): String representation of the SDDS data type.
1474
1475 Returns:
1476 int: Numeric code of the SDDS data type.
1477 """
1478 # Mapping of SDDS numeric data type codes to string representations
1479 data_type_map = {
1480 "longdouble": 1,
1481 "double": 2,
1482 "float": 3,
1483 "long64": 4,
1484 "ulong64": 5,
1485 "long": 6,
1486 "ulong": 7,
1487 "short": 8,
1488 "ushort": 9,
1489 "string": 10,
1490 "character": 11,
1491 }
1492 return data_type_map.get(data_type_code, "Unknown Data Type")
1493

◆ write()

sdds.write ( SddsFile sdds_file,
output_file )

Mostly backward compatible with the PyLHC sdds module write() function.

Unlike the PyLHC version, this function writes all the SDDS pages and works with column data. The data is expected to be in a structured dictionary like that returned from the read() function.

Args: output_file (str): The output SDDS file to be written to.

Definition at line 1967 of file sdds.py.

1967def write(sdds_file: SddsFile, output_file):
1968 """
1969 Mostly backward compatible with the PyLHC sdds module write() function.
1970 Unlike the PyLHC version, this function writes all the SDDS pages and works with column data.
1971 The data is expected to be in a structured dictionary like that returned from the read() function.
1972
1973 Args:
1974 output_file (str): The output SDDS file to be written to.
1975 """
1976 # Initialize an SDDS object
1977 sdds_obj = SDDS()
1978
1979 # Set the data mode (ascii or binary)
1980 if hasattr(sdds_file, 'binary'):
1981 if sdds_file.binary:
1982 sdds_obj.mode = SDDS.SDDS_BINARY
1983 else:
1984 sdds_obj.mode = SDDS.SDDS_ASCII
1985
1986 # Set description data
1987 if hasattr(sdds_file, 'description'):
1988 text = sdds_file.description.text if sdds_file.description.text != None else ''
1989 contents = sdds_file.description.contents if sdds_file.description.contents != None else ''
1990 sdds_obj.setDescription(text, contents)
1991
1992 for name in sdds_file.definitions:
1993 if isinstance(sdds_file.definitions[name], Parameter):
1994 # Set parameter data
1995 parameter_instance = sdds_file.definitions[name]
1996 symbol = parameter_instance.symbol if parameter_instance.symbol != None else ""
1997 units = parameter_instance.units if parameter_instance.units != None else ""
1998 description = parameter_instance.description if parameter_instance.description != None else ""
1999 formatString = parameter_instance.format_string if parameter_instance.format_string != None else ""
2000 datatype = sdds_short_string_to_data_type(parameter_instance.type)
2001 fixedValue = parameter_instance.fixed_value if parameter_instance.fixed_value != None else ""
2002 sdds_obj.defineParameter(name, symbol=symbol, units=units, description=description,
2003 formatString=formatString, type=datatype, fixedValue=fixedValue)
2004 sdds_obj.setParameterValueList(name, sdds_file.values[name])
2005 elif isinstance(sdds_file.definitions[name], Array):
2006 # Set array data
2007 array_instance = sdds_file.definitions[name]
2008 symbol = array_instance.symbol if array_instance.symbol != None else ""
2009 units = array_instance.units if array_instance.units != None else ""
2010 description = array_instance.description if array_instance.description != None else ""
2011 formatString = array_instance.format_string if array_instance.format_string != None else ""
2012 group_name = array_instance.group_name if array_instance.group_name != None else ""
2013 datatype = sdds_short_string_to_data_type(array_instance.type)
2014 fieldLength = array_instance.field_length if array_instance.field_length != None else 0
2015 dimensions = array_instance.dimensions if array_instance.dimensions != None else 1
2016 sdds_obj.defineArray(name, symbol=symbol, units=units, description=description,
2017 formatString=formatString, group_name=group_name, type=datatype,
2018 fieldLength=fieldLength, dimensions=dimensions)
2019 sdds_obj.setArrayValueLists(name, sdds_file.values[name], sdds_file.array_dims[name])
2020 elif isinstance(sdds_file.definitions[name], Column):
2021 # Set column data
2022 column_instance = sdds_file.definitions[name]
2023 symbol = column_instance.symbol if column_instance.symbol != None else ""
2024 units = column_instance.units if column_instance.units != None else ""
2025 description = column_instance.description if column_instance.description != None else ""
2026 formatString = column_instance.format_string if column_instance.format_string != None else ""
2027 datatype = sdds_short_string_to_data_type(column_instance.type)
2028 fieldLength = column_instance.field_length if column_instance.field_length != None else 0
2029 sdds_obj.defineColumn(name, symbol=symbol, units=units, description=description,
2030 formatString=formatString, type=datatype, fieldLength=fieldLength)
2031 sdds_obj.setColumnValueLists(name, sdds_file.values[name])
2032
2033 # Set the data to the output file
2034 sdds_obj.save(output_file)