SDDSlib
Loading...
Searching...
No Matches
example_load.py
Go to the documentation of this file.
1#! /usr/bin/env oagpython3
2
3"""
4@file example_load.py
5@brief Demonstrates reading an SDDS file using the SDDS Python module
6@details
7This script showcases how to utilize the SDDS Python module to read and display
8the contents of an SDDS (Self Describing Data Set) file. The script performs the
9following operations:
10
111. Initializes an SDDS object.
122. Loads an existing SDDS file specified by the user.
133. Determines and displays the file mode (binary or ASCII).
144. Prints the description text and contents if available.
155. Lists all parameters, arrays, and columns along with their metadata.
166. Iterates through each loaded page and displays the data for parameters,
17 arrays, and columns.
18
19Usage:
20 Ensure that the SDDS Python module is installed and the input SDDS file
21 (`output_all_data_types.sdds`) is present in the working directory.
22"""
23
24import sdds
25
26def main():
27 # Initialize the SDDS object with an index of 0
28 sdds_obj = sdds.SDDS(0)
29
30 # Specify the input SDDS file.
31 # This example uses the output file generated by the example_save.py script
32 input_file = "output_all_data_types.sdds"
33
34 # Load the SDDS file into the SDDS object
35 sdds_obj.load(input_file)
36
37 # Determine and display the file mode: Binary or ASCII
38 if sdds_obj.mode == sdds.SDDS_BINARY:
39 print("SDDS file mode: SDDS_BINARY")
40 else:
41 print("SDDS file mode: SDDS_ASCII")
42
43 # Display the description text if available
44 if sdds_obj.description[0]:
45 print(f"SDDS file description text: {sdds_obj.description[0]}")
46
47 # Display additional description contents if available
48 if sdds_obj.description[1]:
49 print(f"SDDS file description contents: {sdds_obj.description[1]}")
50
51 # Check and print parameter definitions if any are present
52 if sdds_obj.parameterName:
53 print("\nParameters:")
54 for i, definition in enumerate(sdds_obj.parameterDefinition):
55 name = sdds_obj.parameterName[i]
56 datatype = sdds.sdds_data_type_to_string(definition[4])
57 units = definition[1]
58 description = definition[2]
59 print(f" {name}")
60 print(f" Datatype: {datatype}", end="")
61 if units:
62 print(f", Units: {units}", end="")
63 if description:
64 print(f", Description: {description}", end="")
65 print("") # Newline for readability
66
67 # Check and print array definitions if any are present
68 if sdds_obj.arrayName:
69 print("\nArrays:")
70 for i, definition in enumerate(sdds_obj.arrayDefinition):
71 name = sdds_obj.arrayName[i]
72 datatype = sdds.sdds_data_type_to_string(definition[5])
73 units = definition[1]
74 description = definition[2]
75 dimensions = definition[7]
76 print(f" {name}")
77 print(f" Datatype: {datatype}, Dimensions: {dimensions}", end="")
78 if units:
79 print(f", Units: {units}", end="")
80 if description:
81 print(f", Description: {description}", end="")
82 print("") # Newline for readability
83
84 # Check and print column definitions if any are present
85 if sdds_obj.columnName:
86 print("\nColumns:")
87 for i, definition in enumerate(sdds_obj.columnDefinition):
88 name = sdds_obj.columnName[i]
89 datatype = sdds.sdds_data_type_to_string(definition[4])
90 units = definition[1]
91 description = definition[2]
92 print(f" {name}")
93 print(f" Datatype: {datatype}", end="")
94 if units:
95 print(f", Units: {units}", end="")
96 if description:
97 print(f", Description: {description}", end="")
98 print("") # Newline for readability
99
100 # Iterate through each loaded page and display parameter, array, and column data
101 for page in range(sdds_obj.loaded_pages):
102 print(f"\nPage: {page + 1}")
103
104 # Display parameter data for the current page
105 for i, name in enumerate(sdds_obj.parameterName):
106 value = sdds_obj.parameterData[i][page]
107 print(f" Parameter '{name}': {value}")
108
109 # Display array data for the current page
110 for i, name in enumerate(sdds_obj.arrayName):
111 value = sdds_obj.arrayData[i][page]
112 print(f" Array '{name}': {value}")
113
114 # Display column data for the current page
115 for i, name in enumerate(sdds_obj.columnName):
116 value = sdds_obj.columnData[i][page]
117 print(f" Column '{name}': {value}")
118
119if __name__ == "__main__":
120 main()
A class to represent and manipulate SDDS datasets.
Definition sdds.py:17
sdds_data_type_to_string(data_type_code)
Converts a numeric SDDS data type code to its string representation.
Definition sdds.py:1300
int main(int argc, char **argv)
Main function that processes the SDDS file.