SDDS ToolKit Programs and Libraries for C and Python
All Classes Files Functions Variables Macros Pages
example_load_with_sddsdata.py File Reference

Detailed Description

Demonstrates reading an SDDS file using the sddsdata module directly.

This script showcases how to utilize the sddsdata module to read and display the contents of an SDDS (Self Describing Data Set) file. The script performs the following operations:

  1. Initializes an SDDS dataset for reading.
  2. Loads an existing SDDS file specified by the user.
  3. Determines and displays the file mode (binary or ASCII).
  4. Prints the description text and contents if available.
  5. Lists all parameters, arrays, and columns along with their metadata.
  6. Iterates through each loaded page and displays the data for parameters, arrays, and columns.

Usage: Ensure that the sddsdata module is installed and the input SDDS file (output_all_data_types.sdds) is present in the working directory.

Definition in file example_load_with_sddsdata.py.

Go to the source code of this file.

Functions

 example_load_with_sddsdata.main ()
 

Function Documentation

◆ main()

example_load_with_sddsdata.main ( )

Definition at line 30 of file example_load_with_sddsdata.py.

30def main():
31 fileIndex = 0 # Dataset index for this SDDS file
32 input_file = "output_all_data_types.sdds"
33
34 # Initialize SDDS dataset for input
35 if InitializeInput(fileIndex, input_file) != 1:
36 raise ValueError("Failed to initialize SDDS input file.")
37
38 # Determine and display the file mode: Binary or ASCII
39 mode = GetMode(fileIndex)
40 if mode == sdds.SDDS_BINARY:
41 print("SDDS file mode: SDDS_BINARY")
42 else:
43 print("SDDS file mode: SDDS_ASCII")
44
45 # Display the description text if available
46 description = GetDescription(fileIndex)
47 if description[0]:
48 print(f"SDDS file description text: {description[0]}")
49 if description[1]:
50 print(f"SDDS file description contents: {description[1]}")
51
52 # Print parameter definitions
53 parameterNames = GetParameterNames(fileIndex)
54 if parameterNames:
55 print("\nParameters:")
56 for name in parameterNames:
57 definition = GetParameterDefinition(fileIndex, name)
58 print(f" {name}")
59 print(f" Datatype: {sdds.sdds_data_type_to_string(definition[4])}", end="")
60 if definition[1]:
61 print(f", Units: {definition[1]}", end="")
62 if definition[2]:
63 print(f", Description: {definition[2]}", end="")
64 print("")
65
66 # Print array definitions
67 arrayNames = GetArrayNames(fileIndex)
68 if arrayNames:
69 print("\nArrays:")
70 for name in arrayNames:
71 definition = GetArrayDefinition(fileIndex, name)
72 print(f" {name}")
73 print(f" Datatype: {sdds.sdds_data_type_to_string(definition[5])}, Dimensions: {definition[7]}", end="")
74 if definition[1]:
75 print(f", Units: {definition[1]}", end="")
76 if definition[2]:
77 print(f", Description: {definition[2]}", end="")
78 print("")
79
80 # Print column definitions
81 columnNames = GetColumnNames(fileIndex)
82 if columnNames:
83 print("\nColumns:")
84 for name in columnNames:
85 definition = GetColumnDefinition(fileIndex, name)
86 print(f" {name}")
87 print(f" Datatype: {sdds.sdds_data_type_to_string(definition[4])}", end="")
88 if definition[1]:
89 print(f", Units: {definition[1]}", end="")
90 if definition[2]:
91 print(f", Description: {definition[2]}", end="")
92 print("")
93
94 # Read pages and display parameter, array, and column data
95 page = ReadPage(fileIndex)
96 while page > 0:
97 print(f"\nPage: {page}")
98
99 # Display parameter data for the current page
100 for name in parameterNames:
101 value = GetParameter(fileIndex, name)
102 print(f" Parameter '{name}': {value}")
103
104 # Display array data for the current page
105 for name in arrayNames:
106 data = GetArray(fileIndex, name)
107 dimensions = GetArrayDimensions(fileIndex, name)
108 print(f" Array '{name}': {data}, Dimensions: {dimensions}")
109
110 # Display column data for the current page
111 for name in columnNames:
112 data = GetColumn(fileIndex, name)
113 print(f" Column '{name}': {data}")
114
115 page = ReadPage(fileIndex)
116
117 # Terminate the SDDS dataset
118 if Terminate(fileIndex) != 1:
119 raise ValueError("Failed to terminate SDDS dataset.")
120