|
| 1 | +from datetime import datetime |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | + |
| 5 | +def nxsource(spicelogs, instrument_config_params): |
| 6 | + source = { |
| 7 | + "attrs": {"NX_class": "NXsource", "EX_required": "true"}, |
| 8 | + "name": { |
| 9 | + "attrs": {"type": "NX_CHAR", "EX_required": "true"}, |
| 10 | + "dataset": "HFIR", |
| 11 | + }, |
| 12 | + "probe": { |
| 13 | + "attrs": {"type": "NX_CHAR", "EX_required": "true"}, |
| 14 | + "dataset": "neutron", |
| 15 | + }, |
| 16 | + } |
| 17 | + # Effective distance from sample Distance as seen by radiation from sample. |
| 18 | + # This number should be negative to signify that it is upstream of the sample. |
| 19 | + # nxsource.attrs["distance"] = -0.0 |
| 20 | + return source |
| 21 | + |
| 22 | + |
| 23 | +def nxmono(spicelogs, instrument_config_params): |
| 24 | + metadata = spicelogs["attrs"] |
| 25 | + |
| 26 | + try: |
| 27 | + mono = { |
| 28 | + "attrs": {"NX_class": "NXcrystal", "EX_required": "true"}, |
| 29 | + "ei": { |
| 30 | + "dataset": spicelogs["ei"], |
| 31 | + "attrs": {"type": "NX_FLOAT", "EX_required": "true", "units": "meV"}, |
| 32 | + }, |
| 33 | + "type": {"dataset": metadata["monochromator"], "attrs": {"type": "NX_CHAR"}}, |
| 34 | + "sense": {"dataset": metadata["sense"][0], "attrs": {"type": "NX_CHAR"}}, |
| 35 | + "m1": { |
| 36 | + "dataset": spicelogs["m1"], |
| 37 | + "attrs": {"type": "NX_FLOAT", "units": "degrees"}, |
| 38 | + }, |
| 39 | + "m2": { |
| 40 | + "dataset": spicelogs["m2"], |
| 41 | + "attrs": {"type": "NX_FLOAT", "units": "degrees"}, |
| 42 | + }, |
| 43 | + "mfocus": {"dataset": spicelogs["mfocus"], "attrs": {"type": "NX_FLOAT"}}, |
| 44 | + "marc": {"dataset": spicelogs["marc"], "attrs": {"type": "NX_FLOAT"}}, |
| 45 | + "mtrans": {"dataset": spicelogs["mtrans"], "attrs": {"type": "NX_FLOAT"}}, |
| 46 | + "focal_length": {"dataset": spicelogs["focal_length"], "attrs": {"type": "NX_FLOAT"}}, |
| 47 | + } |
| 48 | + except KeyError: |
| 49 | + pass |
| 50 | + return mono |
| 51 | + |
| 52 | + |
| 53 | +def nxcoll(spicelogs, instrument_config_params): |
| 54 | + return {} |
| 55 | + |
| 56 | + |
| 57 | +def nxana(spicelogs, instrument_config_params): |
| 58 | + return {} |
| 59 | + |
| 60 | + |
| 61 | +def nxdet(spicelogs, instrument_config_params): |
| 62 | + return {} |
| 63 | + |
| 64 | + |
| 65 | +def nxmonitor(spicelogs, instrument_config_params): |
| 66 | + return {} |
| 67 | + |
| 68 | + |
| 69 | +def nxsample(spicelogs, sample_config_params): |
| 70 | + return {} |
| 71 | + |
| 72 | + |
| 73 | +def nxslit(spicelogs, instrument_config_params): |
| 74 | + return {} |
| 75 | + |
| 76 | + |
| 77 | +def nxflipper(spicelogs, instrument_config_params): |
| 78 | + return {} |
| 79 | + |
| 80 | + |
| 81 | +def _spicelogs_to_nexus(spicelogs, instrument_config_params, sample_config_params): |
| 82 | + metadata = spicelogs["attrs"] |
| 83 | + # TODO timezone |
| 84 | + start_date_time = "{} {}".format(metadata["date"], metadata["time"]) |
| 85 | + start_time = datetime.strptime(start_date_time, "%m/%d/%Y %I:%M:%S %p").isoformat() |
| 86 | + # if "end_time" in das_logs.attrs: # last scan never finished |
| 87 | + end_date_time = metadata["end_time"] |
| 88 | + end_time = datetime.strptime(end_date_time, "%m/%d/%Y %I:%M:%S %p").isoformat() |
| 89 | + |
| 90 | + scan_dict = { |
| 91 | + "attrs": { |
| 92 | + "NX_class": "NXentry", |
| 93 | + "EX_required": "true", |
| 94 | + }, |
| 95 | + "SPICElogs": spicelogs, |
| 96 | + "definition": { |
| 97 | + "attrs": {"EX_required": "true", "type": "NX_CHAR"}, |
| 98 | + "dataset": "NXtas", |
| 99 | + }, |
| 100 | + "title": { |
| 101 | + "attrs": {"EX_required": "true", "type": "NX_CHAR"}, |
| 102 | + "dataset": metadata["scan_title"], |
| 103 | + }, |
| 104 | + "start_time": { |
| 105 | + "attrs": {"EX_required": "true", "type": "NX_DATE_TIME"}, |
| 106 | + "dataset": start_time, |
| 107 | + }, |
| 108 | + "end_time": { |
| 109 | + "attrs": {"type": "NX_DATE_TIME"}, |
| 110 | + "dataset": end_time, |
| 111 | + }, |
| 112 | + "instrument": { |
| 113 | + "attrs": {"EX_required": "true", "NX_class": "NXinstrument"}, |
| 114 | + "name": {"attrs": {"type": "NX_CHAR"}, "dataset": metadata["instrument"]}, |
| 115 | + "source": nxsource(spicelogs, instrument_config_params), |
| 116 | + "collimator": nxcoll(spicelogs, instrument_config_params), |
| 117 | + "monochromator": nxmono(spicelogs, instrument_config_params), |
| 118 | + "analyser": nxana(spicelogs, instrument_config_params), |
| 119 | + "detector": nxdet(spicelogs, instrument_config_params), |
| 120 | + "slit": nxslit(spicelogs, instrument_config_params), |
| 121 | + "flipper": nxflipper(spicelogs, instrument_config_params), |
| 122 | + }, |
| 123 | + "monitor": nxmonitor(spicelogs, instrument_config_params), |
| 124 | + "sample": nxsample(spicelogs, sample_config_params), |
| 125 | + "data": {}, |
| 126 | + } |
| 127 | + return scan_dict |
| 128 | + |
| 129 | + |
| 130 | +def daslogs_to_nexus_dict( |
| 131 | + daslogs: dict, |
| 132 | + instrument_config_params: Optional[str], |
| 133 | + sample_config_params: Optional[str], |
| 134 | +) -> dict: |
| 135 | + """Format DASlogs dict into NeXus dict""" |
| 136 | + |
| 137 | + match (das_key := tuple(daslogs.keys())[0]): |
| 138 | + case "SPICElogs": |
| 139 | + scan_dict = _spicelogs_to_nexus( |
| 140 | + daslogs["SPICElogs"], |
| 141 | + instrument_config_params, |
| 142 | + sample_config_params, |
| 143 | + ) |
| 144 | + case _: |
| 145 | + raise KeyError(f"Unrecogonized DASlogs key {das_key}.") |
| 146 | + |
| 147 | + return scan_dict |
0 commit comments