You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
8.6 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import glob
  2. from datetime import date
  3. import copy
  4. import numpy as np
  5. from uncertainties import unumpy as unp
  6. import xarray as xr
  7. def get_mask(dataArray):
  8. """Generate a bool mask array for given dataArray
  9. :param dataArray: The given dataArray
  10. :type dataArray: xarray DataArray
  11. :return: the mask array
  12. :rtype: numpy array of bool elements
  13. """
  14. return np.ones(dataArray.shape, dtype=bool)
  15. def remove_bad_shots(dataArray, **kwargs):
  16. """Copy and remove bad shots from the dataArray by setting the value to np.nan.
  17. If you want fully delete those nan data, please use the function xarray.DataArray.dropna() (see this link https://docs.xarray.dev/en/stable/generated/xarray.DataArray.dropna.html).
  18. Here is an example for indexing the bad shots: remove_bad_shots(dataArray, axis_1 = the value (not index) of axis_1, axis_2 = the value of axis_2, ...)
  19. For more detials please read 'Positional indexing' section in this link https://docs.xarray.dev/en/stable/user-guide/indexing.html#positional-indexing.
  20. :param dataArray: The given dataArray
  21. :type dataArray: xarray DataArray
  22. :return: The dataArray after removement
  23. :rtype: xarray DataArray
  24. """
  25. dataArray = copy.deepcopy(dataArray)
  26. dataArray.loc[dict(kwargs)] = np.nan
  27. return dataArray
  28. def auto_rechunk(dataSet):
  29. """Rechunk the dataSet or dataArray using auto rechunk function
  30. :param dataSet: The given dataArray or dataSet
  31. :type dataSet: xarray DataArray or xarray DataSet
  32. :return: The chuncked dataArray or dataSet
  33. :rtype: xarray DataArray or xarray DataSet
  34. """
  35. kwargs = {
  36. key: "auto"
  37. for key in dataSet.dims
  38. }
  39. return dataSet.chunk(**kwargs)
  40. def copy_chunk(dataSet, dataChunk):
  41. """Copy the chunk and apply to another dataArray or dataSet
  42. :param dataSet: The dataArray or dataSet will be chunked
  43. :type dataSet: xarray DataArray or xarray DataSet
  44. :param dataChunk: The dataArray or dataSet giving the chunk
  45. :type dataChunk: xarray DataArray or xarray DataSet
  46. :return: The chuncked dataArray or dataSet
  47. :rtype: xarray DataArray or xarray DataSet
  48. """
  49. kwargs = {
  50. key: dataChunk.chunksizes[key]
  51. for key in dataChunk.chunksizes
  52. if key in dataSet.dims
  53. }
  54. return dataSet.chunk(**kwargs)
  55. def get_h5_file_path(folderpath, maxFileNum=None, filename='*.h5',):
  56. """Get all the path of HDF5 files in specific folder
  57. :param folderpath: the path of the folder
  58. :type folderpath: str
  59. :param maxFileNum: the maximal number of returned files, defaults to None
  60. :type maxFileNum: int, optional
  61. :param filename: a string to specify the type of the file to read, defaults to '*.h5'
  62. :type filename: str, optional
  63. :return: the found file path
  64. :rtype: 1D numpy array
  65. """
  66. filepath = np.sort(glob.glob(folderpath + filename))
  67. if maxFileNum is None:
  68. return filepath
  69. else:
  70. return filepath[:maxFileNum]
  71. def get_date():
  72. """Return the date of today in a format compatible with file path
  73. :return: the date of today in a format compatible with file path
  74. :rtype: str
  75. """
  76. today = date.today()
  77. return today.strftime("%Y/%m/%d")
  78. def _combine_uncertainty(value, std):
  79. """Give a list of value and standard deviation, combine them to a number with unceuncertainty (ufloat), and return them in another list.
  80. See this link https://pythonhosted.org/uncertainties/
  81. :param value: The value
  82. :type value: float, or array like
  83. :param std: The standard deviation
  84. :type std: float, or array like
  85. :return: The combined value and standard deviation
  86. :rtype: ufloat, or uncertainties uarray
  87. """
  88. return unp.uarray(value, std)
  89. def combine_uncertainty(value, std, dask='parallelized', **kwargs):
  90. """Give a xarray DataArray of value and standard deviation, combine them to a number with unceuncertainty (ufloat), and return them in another xarray DataArray .
  91. See this link https://pythonhosted.org/uncertainties/
  92. :param value: The value
  93. :type value: xarray DataArray
  94. :param std: The standard deviation
  95. :type std: xarray DataArray
  96. :param dask: over write of the same argument in xarray.apply_ufunc, defaults to 'parallelized'
  97. :type dask: str, optional
  98. :return: The combined value and standard deviation
  99. :rtype: xarray DataArray
  100. """
  101. kwargs.update(
  102. {
  103. "dask": dask,
  104. }
  105. )
  106. return xr.apply_ufunc(_combine_uncertainty, value, std, **kwargs)
  107. def _seperate_uncertainty_single(data):
  108. """From a number with unceuncertainty, read out the value and standard deviation
  109. :param data: The number with unceuncertainty
  110. :type data: ufloat
  111. :return: a tuple of (value, standard deviations)
  112. :rtype: tuple of two floats
  113. """
  114. return data.n, data.s
  115. def _seperate_uncertainty(data):
  116. """From a list of numbers with unceuncertainty, read out the values and standard deviations
  117. :param data: The list of numbers with unceuncertainty
  118. :type data: ufloat, or uncertainties uarray
  119. :return: a tuple of (a numpy array of value, a numpy array of standard deviations)
  120. :rtype: tuple of two numpy arrays
  121. """
  122. func = np.vectorize(_seperate_uncertainty_single)
  123. return func(data)
  124. def seperate_uncertainty(data, dask='parallelized', **kwargs):
  125. """From a xarray DataArray of numbers with unceuncertainty, read out the values and standard deviations
  126. :param data: The xarray DataArray of numbers with unceuncertainty
  127. :type data: xarray DataArray
  128. :param dask: over write of the same argument in xarray.apply_ufunc, defaults to 'parallelized'
  129. :type dask: str, optional
  130. :return: a tuple of (a xarray DataArray of value, a xarray DataArray of standard deviations)
  131. :rtype: tuple of two xarray DataArray
  132. """
  133. kwargs.update(
  134. {
  135. "dask": dask,
  136. "output_core_dims": [[], []],
  137. }
  138. )
  139. return xr.apply_ufunc(_seperate_uncertainty, data, **kwargs)
  140. def get_scanAxis(dataSet):
  141. """Give a numpy array of names of scan axes.
  142. :param dataSet: The xarray DataSet stored the data.
  143. :type dataSet: xarray DataSet
  144. :return: The names of scan axes
  145. :rtype: a numpy array
  146. """
  147. res = dataSet.scanAxis
  148. if len(res) == 0:
  149. res = [None, None]
  150. elif len(res) == 1:
  151. res = [res[0], None]
  152. elif len(res) == 2 and res[0] == 'runs':
  153. res = [res[1], res[0]]
  154. return res
  155. def print_scanAxis(dataSet):
  156. """Print the names and the values of scan axes.
  157. :param dataSet: The xarray DataSet stored the data.
  158. :type dataSet: xarray DataSet
  159. """
  160. scanAxis = dataSet.scanAxis
  161. scan = {}
  162. for key in scanAxis:
  163. scanValue = np.array(dataSet[key])
  164. scanValue, indices = np.unique(scanValue, return_index=True)
  165. scan.update(
  166. {
  167. key: scanValue[indices]
  168. }
  169. )
  170. print("The detected scaning axes and values are: \n")
  171. print(scan)
  172. def print_scanAxis_original(dataSet):
  173. # print the original (unsorted) scan axes.
  174. pass
  175. def calculate_mean(dataSet):
  176. """Calculte the mean along repetition axis 'runs'
  177. :param dataSet: The xarray DataSet or DataArray stored the data.
  178. :type dataSet: xarray DataSet or DataArray
  179. :return: The mean value
  180. :rtype: xarray DataSet or DataArray
  181. """
  182. if 'runs' in dataSet.dims:
  183. return dataSet.mean(dim='runs')
  184. else:
  185. return dataSet
  186. def calculate_std(dataSet):
  187. """Calculte the standard deviation along repetition axis 'runs'
  188. :param dataSet: The xarray DataSet or DataArray stored the data.
  189. :type dataSet: xarray DataSet or DataArray
  190. :return: The standard deviation
  191. :rtype: xarray DataSet or DataArray
  192. """
  193. if 'runs' in dataSet.dims:
  194. return dataSet.std(dim='runs')
  195. else:
  196. return None
  197. def extract_temperature_from_fit():
  198. # by giving the shot number, read data, fit the center of could and fit with ToF, give the temperature of the clound
  199. pass
  200. def extract_condensate_fraction_from_fit():
  201. # by giving the shot number, read data, fit the of could, give the condenstate fraction of the clound
  202. pass
  203. def swap_xy(dataSet):
  204. """Swap the x ans y axis.
  205. :param dataSet: The xarray DataSet or DataArray stored the data.
  206. :type dataSet: xarray DataSet or DataArray
  207. :return: The xarray DataSet or DataArray with swapped x and y axis.
  208. :rtype: xarray DataSet or DataArray
  209. """
  210. dataSet = dataSet.rename_dims(dict(x='__x'))
  211. dataSet = dataSet.rename_dims(dict(y='x'))
  212. dataSet = dataSet.rename_dims(dict(__x='y'))
  213. return dataSet