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

Detailed Description

Demonstrates reading an SDDS file using the SDDS Python module.

This script showcases how to utilize the SDDS Python 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 object.
  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 SDDS Python module is installed and the input SDDS file (output_all_data_types.sdds) is present in the working directory.

Definition in file example_load.py.

Go to the source code of this file.

Functions

 example_load.main ()
 

Function Documentation

◆ main()

example_load.main ( )

Definition at line 26 of file example_load.py.

26def main():
27 # Specify the input SDDS file.
28 # This example uses the output file generated by the example_save.py script
29 input_file = "output_all_data_types.sdds"
30
31 # Load the SDDS file into the SDDS object
32 sdds_obj = sdds.load(input_file)
33
34 # Determine and display the file mode: Binary or ASCII
35 if sdds_obj.mode == sdds.SDDS_BINARY:
36 print("SDDS file mode: SDDS_BINARY")
37 else:
38 print("SDDS file mode: SDDS_ASCII")
39
40 # Display the description text if available
41 if sdds_obj.description[0]:
42 print(f"SDDS file description text: {sdds_obj.description[0]}")
43
44 # Display additional description contents if available
45 if sdds_obj.description[1]:
46 print(f"SDDS file description contents: {sdds_obj.description[1]}")
47
48 # Check and print parameter definitions if any are present
49 if sdds_obj.parameterName:
50 print("\nParameters:")
51 for i, definition in enumerate(sdds_obj.parameterDefinition):
52 name = sdds_obj.parameterName[i]
53 datatype = sdds.sdds_data_type_to_string(definition[4])
54 units = definition[1]
55 description = definition[2]
56 print(f" {name}")
57 print(f" Datatype: {datatype}", end="")
58 if units:
59 print(f", Units: {units}", end="")
60 if description:
61 print(f", Description: {description}", end="")
62 print("") # Newline for readability
63
64 # Check and print array definitions if any are present
65 if sdds_obj.arrayName:
66 print("\nArrays:")
67 for i, definition in enumerate(sdds_obj.arrayDefinition):
68 name = sdds_obj.arrayName[i]
69 datatype = sdds.sdds_data_type_to_string(definition[5])
70 units = definition[1]
71 description = definition[2]
72 dimensions = definition[7]
73 print(f" {name}")
74 print(f" Datatype: {datatype}, Dimensions: {dimensions}", end="")
75 if units:
76 print(f", Units: {units}", end="")
77 if description:
78 print(f", Description: {description}", end="")
79 print("") # Newline for readability
80
81 # Check and print column definitions if any are present
82 if sdds_obj.columnName:
83 print("\nColumns:")
84 for i, definition in enumerate(sdds_obj.columnDefinition):
85 name = sdds_obj.columnName[i]
86 datatype = sdds.sdds_data_type_to_string(definition[4])
87 units = definition[1]
88 description = definition[2]
89 print(f" {name}")
90 print(f" Datatype: {datatype}", end="")
91 if units:
92 print(f", Units: {units}", end="")
93 if description:
94 print(f", Description: {description}", end="")
95 print("") # Newline for readability
96
97 # Iterate through each loaded page and display parameter, array, and column data
98 for page in range(sdds_obj.loaded_pages):
99 print(f"\nPage: {page + 1}")
100
101 # Display parameter data for the current page
102 for i, name in enumerate(sdds_obj.parameterName):
103 value = sdds_obj.parameterData[i][page]
104 print(f" Parameter '{name}': {value}")
105
106 # Display array data for the current page
107 for i, name in enumerate(sdds_obj.arrayName):
108 value = sdds_obj.arrayData[i][page]
109 print(f" Array '{name}': {value}")
110
111 # Display column data for the current page
112 for i, name in enumerate(sdds_obj.columnName):
113 value = sdds_obj.columnData[i][page]
114 print(f" Column '{name}': {value}")
115
116 # Opitonally delete the SDDS object
117 del sdds_obj
118
sdds_data_type_to_string(data_type_code)
Converts a numeric SDDS data type code to its string representation.
Definition sdds.py:1416
SDDS load(input_file)
Loads an SDDS file into the SDDS object.
Definition sdds.py:1323