vaft 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
vaft/__init__.py ADDED
@@ -0,0 +1,12 @@
1
+ # Top-level package initialization
2
+ from .version import __version__
3
+
4
+ __all__ = ['process', 'formula', 'machine_mapping', 'plot', 'omas', 'code', 'database']
5
+
6
+ from . import process
7
+ from . import formula
8
+ from . import machine_mapping
9
+ from . import plot
10
+ from . import omas
11
+ from . import code
12
+ from . import database
vaft/code/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from . import *
vaft/code/chease.py ADDED
File without changes
vaft/code/efit.py ADDED
File without changes
vaft/code/gpec.py ADDED
File without changes
@@ -0,0 +1,2 @@
1
+ from .ods import *
2
+ from .raw import *
vaft/database/ods.py ADDED
@@ -0,0 +1,160 @@
1
+ import requests
2
+ import urllib3
3
+ import h5pyd, h5py
4
+ import omas
5
+ import subprocess
6
+ import numpy
7
+ from uncertainties import ufloat
8
+ from uncertainties.unumpy import uarray
9
+
10
+
11
+ def is_connect():
12
+ """
13
+ Check if the user is connected to the server.
14
+
15
+ input: None
16
+ output: True if the user is connected to the server, False otherwise.
17
+
18
+ """
19
+ try:
20
+ if h5pyd.getServerInfo()['state']=='READY':
21
+ username = h5pyd.getServerInfo()['username']
22
+ return True
23
+ else:
24
+ return False
25
+ except requests.exceptions.ConnectTimeout:
26
+ return False
27
+
28
+
29
+ def exist_file(username=h5pyd.getServerInfo()['username'], shot=None):
30
+ """
31
+ Check if the file exists.
32
+
33
+ If the 'username' or 'shot' parameter is provided, the function will check if a file with a matching prefix exists.
34
+ If 'username' or 'shot' is not provided, the function will list all files in the user's folder.
35
+
36
+ Parameters:
37
+ username (str): The username to access the corresponding folder.
38
+ shot (int, optional): The shot number to search for (default is None).
39
+
40
+ Returns:
41
+ bool: True if the file or folder exists, False if it does not or if a connection error occurs.
42
+ """
43
+ try:
44
+ Folder = list(h5pyd.Folder("/" + username + "/"))
45
+ if shot is not None:
46
+ file_list = list(Folder)
47
+ file_exist = False
48
+
49
+ for file in file_list:
50
+ if file.split("_")[0] == str(shot):
51
+ file_exist = True
52
+ print(file)
53
+ return True
54
+
55
+ if not file_exist:
56
+ print("File does not exist")
57
+ return False
58
+ else:
59
+ print(list(Folder))
60
+ except urllib3.exceptions.MaxRetryError:
61
+ return False
62
+ return True
63
+
64
+ def save(ods, shot, filename=None, env='server'):
65
+ """
66
+ Function to save an ODS (Open Data Structure) file either locally or to an HDF5 server.
67
+
68
+ This function handles saving the ODS data to an HDF5 file, either on the local file system or
69
+ remotely to an HDF5 server using the `hsload` command. If no filename is provided, the function
70
+ defaults to using the shot number with an `.h5` extension.
71
+
72
+ Parameters:
73
+ ods (object): The Open Data Structure (ODS) object that needs to be saved.
74
+ shot (int): The shot number associated with the ODS data, used to generate the filename if not provided.
75
+ filename (str, optional): The name of the file to save the ODS data. Defaults to `None`, which generates a name based on the shot number.
76
+ env (str, optional): The environment where the file will be saved. It can be either 'server' for server upload or 'local' for local storage. Defaults to 'server'.
77
+
78
+ Returns:
79
+ None: The function doesn't return any specific value but prints information about the saving process.
80
+ """
81
+
82
+ # If no filename is provided, use the shot number to create a default file name
83
+ if filename is None:
84
+ filename = str(shot) + '.h5'
85
+
86
+ # If the environment is 'local', save the file locally using OMAS
87
+ if env == 'local':
88
+ omas.save_omas_h5(ods, filename)
89
+
90
+ # Test the connection to the server, and exit if the connection fails
91
+ if is_connect() != True:
92
+ print('Error: Connection to the server failed')
93
+ return
94
+
95
+ # Get the current username from the HDF5 server and construct the file path
96
+ username = 'public' if h5pyd.getServerInfo()['username'] == 'admin' else h5pyd.getServerInfo()['username']
97
+ file_path = "hdf5://{}/{}".format(username, filename)
98
+ omas.save_omas_h5(ods, filename)
99
+
100
+ command = ['hsload', '--h5image', filename, file_path]
101
+ result = subprocess.run(command, capture_output=True, text=True)
102
+
103
+ subprocess.run(['rm', filename], capture_output=True, text=True)
104
+
105
+ def convertDataset(ods, data):
106
+ """
107
+ Recursive utility function to map HDF5 structure to ODS
108
+
109
+ :param ods: input ODS to be populated
110
+
111
+ :param data: HDF5 dataset of group
112
+ """
113
+ import h5py
114
+
115
+ keys = data.keys()
116
+ try:
117
+ keys = sorted(list(map(int, keys)))
118
+ except ValueError:
119
+ pass
120
+ for oitem in keys:
121
+ item = str(oitem)
122
+ if item.endswith('_error_upper'):
123
+ continue
124
+ if isinstance(data[item], h5py.Dataset):
125
+ if item + '_error_upper' in data:
126
+ if isinstance(data[item][()], (float, numpy.floating)):
127
+ ods.setraw(item, ufloat(data[item][()], data[item + '_error_upper'][()]))
128
+ else:
129
+ ods.setraw(item, uarray(data[item][()], data[item + '_error_upper'][()]))
130
+ else:
131
+ ods.setraw(item, data[item][()])
132
+ elif isinstance(data[item], h5py.Group):
133
+ convertDataset(ods.setraw(oitem, ods.same_init_ods()), data[item])
134
+
135
+ def load(shot, directory=None):
136
+
137
+ if isinstance(shot, list):
138
+ shot_list = shot
139
+ ods_list = []
140
+ for shot in shot_list:
141
+ ods = omas.ODS()
142
+ if directory is None:
143
+ filename = f'hdf5://public/{shot}.h5'
144
+ else:
145
+ filename = f'hdf5://{directory}/{shot}.h5'
146
+ with h5py.File(h5pyd.H5Image(filename)) as data:
147
+ convertDataset(ods, data)
148
+ ods_list.append(ods)
149
+ return ods_list
150
+
151
+ else:
152
+ ods = omas.ODS()
153
+ shot_list = [int(shot)]
154
+ if directory is None:
155
+ filename = f'hdf5://public/{shot}.h5'
156
+ else:
157
+ filename = f'hdf5://{directory}/{shot}.h5'
158
+ with h5py.File(h5pyd.H5Image(filename)) as data:
159
+ convertDataset(ods, data)
160
+ return ods