/scripts/SANS/sans/algorithm_detail/save_workspace.py

https://github.com/mantidproject/mantid · Python · 125 lines · 65 code · 14 blank · 46 comment · 11 complexity · 1e35928ff3264007547149ef00552551 MD5 · raw file

  1. # Mantid Repository : https://github.com/mantidproject/mantid
  2. #
  3. # Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
  4. # NScD Oak Ridge National Laboratory, European Spallation Source,
  5. # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
  6. # SPDX - License - Identifier: GPL - 3.0 +
  7. from collections import namedtuple
  8. from mantid.api import MatrixWorkspace
  9. from mantid.dataobjects import EventWorkspace
  10. from sans.common.general_functions import create_unmanaged_algorithm
  11. from sans.common.constants import EMPTY_NAME
  12. from sans.common.enums import SaveType
  13. # from sans.algorithm_detail.strip_end_nans_and_infs import strip_end_nans
  14. ZERO_ERROR_DEFAULT = 1e6
  15. file_format_with_append = namedtuple('file_format_with_append', 'file_format, append_file_format_name')
  16. def save_to_file(workspace, file_format, file_name, transmission_workspaces, additional_run_numbers):
  17. """
  18. Save a workspace to a file.
  19. :param workspace: the workspace to save.
  20. :param file_format: the selected file format type.
  21. :param file_name: the file name.
  22. :param transmission_workspaces: a dict of additional save algorithm inputs
  23. e.g. Transmission and TransmissionCan for SaveCanSAS1D-v2
  24. :param additional_run_numbers: a dict of workspace type to run number. Used in SaveNXCanSAS only.
  25. :return:
  26. """
  27. save_options = {"InputWorkspace": workspace}
  28. save_alg = get_save_strategy(file_format, file_name, save_options, transmission_workspaces, additional_run_numbers)
  29. save_alg.setRethrows(True)
  30. save_alg.execute()
  31. def get_save_strategy(file_format_bundle, file_name, save_options, transmission_workspaces, additional_run_numbers):
  32. """
  33. Provide a save strategy based on the selected file format
  34. :param file_format_bundle: the selected file_format_bundle
  35. :param file_name: the name of the file
  36. :param save_options: the save options such as file name and input workspace
  37. :param transmission_workspaces: a dict of additional inputs for SaveCanSAS algorithm
  38. :param additional_run_numbers: a dict of workspace type to run number
  39. :return: a handle to a save algorithm
  40. """
  41. file_format = file_format_bundle.file_format
  42. if file_format is SaveType.NEXUS:
  43. file_name = get_file_name(file_format_bundle, file_name, "", ".nxs")
  44. save_name = "SaveNexusProcessed"
  45. elif file_format is SaveType.CAN_SAS:
  46. file_name = get_file_name(file_format_bundle, file_name, "", ".xml")
  47. save_name = "SaveCanSAS1D"
  48. save_options.update(transmission_workspaces)
  49. save_options.update(additional_run_numbers)
  50. elif file_format is SaveType.NX_CAN_SAS:
  51. file_name = get_file_name(file_format_bundle, file_name, "_nxcansas", ".h5")
  52. save_name = "SaveNXcanSAS"
  53. save_options.update(transmission_workspaces)
  54. save_options.update(additional_run_numbers)
  55. elif file_format is SaveType.NIST_QXY:
  56. file_name = get_file_name(file_format_bundle, file_name, "_nistqxy", ".dat")
  57. save_name = "SaveNISTDAT"
  58. elif file_format is SaveType.RKH:
  59. file_name = get_file_name(file_format_bundle, file_name, "", ".txt")
  60. save_name = "SaveRKH"
  61. save_options.update({"Append": False})
  62. elif file_format is SaveType.CSV:
  63. file_name = get_file_name(file_format_bundle, file_name, "", ".csv")
  64. save_name = "SaveCSV"
  65. else:
  66. raise RuntimeError("SaveWorkspace: The requested data {0} format is "
  67. "currently not supported.".format(file_format))
  68. save_options.update({"Filename": file_name})
  69. return create_unmanaged_algorithm(save_name, **save_options)
  70. def get_file_name(file_format, file_name, post_fix, extension):
  71. if file_format.append_file_format_name:
  72. file_name += post_fix
  73. file_name += extension
  74. return file_name
  75. def get_zero_error_free_workspace(workspace):
  76. """
  77. Creates a cloned workspace where all zero-error values have been replaced with a large value
  78. :param workspace: The input workspace
  79. :return: The zero-error free workspace
  80. """
  81. clone_name = "CloneWorkspace"
  82. clone_options = {"InputWorkspace": workspace,
  83. "OutputWorkspace": EMPTY_NAME}
  84. clone_alg = create_unmanaged_algorithm(clone_name, **clone_options)
  85. clone_alg.execute()
  86. cloned_workspace = clone_alg.getProperty("OutputWorkspace").value
  87. remove_zero_errors_from_workspace(cloned_workspace)
  88. return cloned_workspace
  89. def remove_zero_errors_from_workspace(workspace):
  90. """
  91. Removes the zero errors from a matrix workspace
  92. :param workspace: The workspace which will have its zero error values removed.
  93. :return: A zero-error free workspace
  94. """
  95. # Make sure we are dealing with a MatrixWorkspace
  96. if not isinstance(workspace, MatrixWorkspace) or isinstance(workspace, EventWorkspace):
  97. raise ValueError('Cannot remove zero errors from a workspace which is not a MatrixWorkspace.')
  98. # Uncomment the next line and tests fail for checking error values should not be zero, and
  99. # comparing loaded workspace to calculated workspace. If we want to remove RuntimeWarning for nan
  100. # values strip_end_nans should be moved up the workflow
  101. # workspace = strip_end_nans(workspace, None)
  102. # Iterate over the workspace and replace the zero values with a large default value
  103. number_of_spectra = workspace.getNumberHistograms()
  104. errors = workspace.dataE
  105. for index in range(0, number_of_spectra):
  106. spectrum = errors(index)
  107. spectrum[spectrum <= 0.0] = ZERO_ERROR_DEFAULT