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.

138 lines
2.8 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. import glob
  2. from datetime import date
  3. import numpy as np
  4. from uncertainties import unumpy as unp
  5. import xarray as xr
  6. def get_mask(dataArray):
  7. return np.ones(dataArray.shape, dtype=bool)
  8. def remove_bad_shots(dataArray, **kwargs):
  9. dataArray.loc[dict(kwargs)] = np.nan
  10. def auto_rechunk(dataSet):
  11. kwargs = {
  12. key: "auto"
  13. for key in dataSet.dims
  14. }
  15. return dataSet.chunk(**kwargs)
  16. def copy_chunk(dataSet, dataChunk):
  17. kwargs = {
  18. key: dataChunk.chunksizes[key]
  19. for key in dataChunk.chunksizes
  20. if key in dataSet.dims
  21. }
  22. return dataSet.chunk(**kwargs)
  23. def get_h5_file_path(folderpath, maxFileNum=None, filename='*.h5',):
  24. filepath = np.sort(glob.glob(folderpath + filename))
  25. if maxFileNum is None:
  26. return filepath
  27. else:
  28. return filepath[:maxFileNum]
  29. def get_date():
  30. today = date.today()
  31. return today.strftime("%Y/%m/%d")
  32. def _combine_uncertainty(value, std):
  33. return unp.uarray(value, std)
  34. def combine_uncertainty(value, std, dask='parallelized', **kwargs):
  35. kwargs.update(
  36. {
  37. "dask": dask,
  38. }
  39. )
  40. return xr.apply_ufunc(_combine_uncertainty, value, std, **kwargs)
  41. def _seperate_uncertainty_single(data):
  42. return data.n, data.s
  43. def _seperate_uncertainty(data):
  44. func = np.vectorize(_seperate_uncertainty_single)
  45. return func(data)
  46. def seperate_uncertainty(data, dask='parallelized', **kwargs):
  47. kwargs.update(
  48. {
  49. "dask": dask,
  50. "output_core_dims": [[], []],
  51. }
  52. )
  53. return xr.apply_ufunc(_seperate_uncertainty, data, **kwargs)
  54. def get_scanAxis(dataSet):
  55. res = dataSet.scanAxis
  56. if len(res) == 0:
  57. res = [None, None]
  58. elif len(res) == 1:
  59. res = [res[0], None]
  60. elif len(res) == 2 and res[0] == 'runs':
  61. res = [res[1], res[0]]
  62. return res
  63. def print_scanAxis(dataSet):
  64. scanAxis = dataSet.scanAxis
  65. scan = {}
  66. for key in scanAxis:
  67. scanValue = np.array(dataSet[key])
  68. scanValue, indices = np.unique(scanValue, return_index=True)
  69. scan.update(
  70. {
  71. key: scanValue[indices]
  72. }
  73. )
  74. print("The detected scaning axes and values are: \n")
  75. print(scan)
  76. def calculate_mean(dataSet):
  77. if 'runs' in dataSet.dims:
  78. return dataSet.mean(dim='runs')
  79. else:
  80. return dataSet
  81. def calculate_std(dataSet):
  82. if 'runs' in dataSet.dims:
  83. return dataSet.mean(dim='runs')
  84. else:
  85. return None
  86. def extract_temperature_from_fit():
  87. pass
  88. def extract_condensate_fraction_from_fit():
  89. pass
  90. def swap_xy(dataSet):
  91. dataSet = dataSet.rename_dims(dict(x='__x'))
  92. dataSet = dataSet.rename_dims(dict(y='x'))
  93. dataSet = dataSet.rename_dims(dict(__x='y'))
  94. return dataSet